uipc_mbuf.c revision 1.14 1 /* $NetBSD: uipc_mbuf.c,v 1.14 1996/02/04 02:17:46 christos Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1988, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94
36 */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/proc.h>
41 #include <sys/malloc.h>
42 #include <sys/map.h>
43 #define MBTYPES
44 #include <sys/mbuf.h>
45 #include <sys/kernel.h>
46 #include <sys/syslog.h>
47 #include <sys/domain.h>
48 #include <sys/protosw.h>
49
50 #include <vm/vm.h>
51
52 #include <kern/kern_extern.h>
53
54 extern vm_map_t mb_map;
55 struct mbuf *mbutl;
56 char *mclrefcnt;
57
58 void
59 mbinit()
60 {
61 int s;
62
63 s = splimp();
64 if (m_clalloc(max(4096/CLBYTES, 1), M_DONTWAIT) == 0)
65 goto bad;
66 splx(s);
67 return;
68 bad:
69 panic("mbinit");
70 }
71
72 /*
73 * Allocate some number of mbuf clusters
74 * and place on cluster free list.
75 * Must be called at splimp.
76 */
77 /* ARGSUSED */
78 int
79 m_clalloc(ncl, nowait)
80 register int ncl;
81 int nowait;
82 {
83 static int logged;
84 register caddr_t p;
85 register int i;
86 int npg;
87
88 npg = ncl * CLSIZE;
89 p = (caddr_t)kmem_malloc(mb_map, ctob(npg), !nowait);
90 if (p == NULL) {
91 if (logged == 0) {
92 logged++;
93 log(LOG_ERR, "mb_map full\n");
94 }
95 return (0);
96 }
97 ncl = ncl * CLBYTES / MCLBYTES;
98 for (i = 0; i < ncl; i++) {
99 ((union mcluster *)p)->mcl_next = mclfree;
100 mclfree = (union mcluster *)p;
101 p += MCLBYTES;
102 mbstat.m_clfree++;
103 }
104 mbstat.m_clusters += ncl;
105 return (1);
106 }
107
108 /*
109 * When MGET failes, ask protocols to free space when short of memory,
110 * then re-attempt to allocate an mbuf.
111 */
112 struct mbuf *
113 m_retry(i, t)
114 int i, t;
115 {
116 register struct mbuf *m;
117
118 m_reclaim();
119 #define m_retry(i, t) (struct mbuf *)0
120 MGET(m, i, t);
121 #undef m_retry
122 return (m);
123 }
124
125 /*
126 * As above; retry an MGETHDR.
127 */
128 struct mbuf *
129 m_retryhdr(i, t)
130 int i, t;
131 {
132 register struct mbuf *m;
133
134 m_reclaim();
135 #define m_retryhdr(i, t) (struct mbuf *)0
136 MGETHDR(m, i, t);
137 #undef m_retryhdr
138 return (m);
139 }
140
141 void
142 m_reclaim()
143 {
144 register struct domain *dp;
145 register struct protosw *pr;
146 int s = splimp();
147
148 for (dp = domains; dp; dp = dp->dom_next)
149 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
150 if (pr->pr_drain)
151 (*pr->pr_drain)();
152 splx(s);
153 mbstat.m_drain++;
154 }
155
156 /*
157 * Space allocation routines.
158 * These are also available as macros
159 * for critical paths.
160 */
161 struct mbuf *
162 m_get(nowait, type)
163 int nowait, type;
164 {
165 register struct mbuf *m;
166
167 MGET(m, nowait, type);
168 return (m);
169 }
170
171 struct mbuf *
172 m_gethdr(nowait, type)
173 int nowait, type;
174 {
175 register struct mbuf *m;
176
177 MGETHDR(m, nowait, type);
178 return (m);
179 }
180
181 struct mbuf *
182 m_getclr(nowait, type)
183 int nowait, type;
184 {
185 register struct mbuf *m;
186
187 MGET(m, nowait, type);
188 if (m == 0)
189 return (0);
190 bzero(mtod(m, caddr_t), MLEN);
191 return (m);
192 }
193
194 struct mbuf *
195 m_free(m)
196 struct mbuf *m;
197 {
198 register struct mbuf *n;
199
200 MFREE(m, n);
201 return (n);
202 }
203
204 void
205 m_freem(m)
206 register struct mbuf *m;
207 {
208 register struct mbuf *n;
209
210 if (m == NULL)
211 return;
212 do {
213 MFREE(m, n);
214 } while ((m = n) != NULL);
215 }
216
217 /*
218 * Mbuffer utility routines.
219 */
220
221 /*
222 * Lesser-used path for M_PREPEND:
223 * allocate new mbuf to prepend to chain,
224 * copy junk along.
225 */
226 struct mbuf *
227 m_prepend(m, len, how)
228 register struct mbuf *m;
229 int len, how;
230 {
231 struct mbuf *mn;
232
233 MGET(mn, how, m->m_type);
234 if (mn == (struct mbuf *)NULL) {
235 m_freem(m);
236 return ((struct mbuf *)NULL);
237 }
238 if (m->m_flags & M_PKTHDR) {
239 M_COPY_PKTHDR(mn, m);
240 m->m_flags &= ~M_PKTHDR;
241 }
242 mn->m_next = m;
243 m = mn;
244 if (len < MHLEN)
245 MH_ALIGN(m, len);
246 m->m_len = len;
247 return (m);
248 }
249
250 /*
251 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
252 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
253 * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
254 */
255 int MCFail;
256
257 struct mbuf *
258 m_copym(m, off0, len, wait)
259 register struct mbuf *m;
260 int off0, wait;
261 register int len;
262 {
263 register struct mbuf *n, **np;
264 register int off = off0;
265 struct mbuf *top;
266 int copyhdr = 0;
267
268 if (off < 0 || len < 0)
269 panic("m_copym");
270 if (off == 0 && m->m_flags & M_PKTHDR)
271 copyhdr = 1;
272 while (off > 0) {
273 if (m == 0)
274 panic("m_copym");
275 if (off < m->m_len)
276 break;
277 off -= m->m_len;
278 m = m->m_next;
279 }
280 np = ⊤
281 top = 0;
282 while (len > 0) {
283 if (m == 0) {
284 if (len != M_COPYALL)
285 panic("m_copym");
286 break;
287 }
288 MGET(n, wait, m->m_type);
289 *np = n;
290 if (n == 0)
291 goto nospace;
292 if (copyhdr) {
293 M_COPY_PKTHDR(n, m);
294 if (len == M_COPYALL)
295 n->m_pkthdr.len -= off0;
296 else
297 n->m_pkthdr.len = len;
298 copyhdr = 0;
299 }
300 n->m_len = min(len, m->m_len - off);
301 if (m->m_flags & M_EXT) {
302 n->m_data = m->m_data + off;
303 mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
304 n->m_ext = m->m_ext;
305 n->m_flags |= M_EXT;
306 } else
307 bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
308 (unsigned)n->m_len);
309 if (len != M_COPYALL)
310 len -= n->m_len;
311 off = 0;
312 m = m->m_next;
313 np = &n->m_next;
314 }
315 if (top == 0)
316 MCFail++;
317 return (top);
318 nospace:
319 m_freem(top);
320 MCFail++;
321 return (0);
322 }
323
324 /*
325 * Copy data from an mbuf chain starting "off" bytes from the beginning,
326 * continuing for "len" bytes, into the indicated buffer.
327 */
328 void
329 m_copydata(m, off, len, cp)
330 register struct mbuf *m;
331 register int off;
332 register int len;
333 caddr_t cp;
334 {
335 register unsigned count;
336
337 if (off < 0 || len < 0)
338 panic("m_copydata");
339 while (off > 0) {
340 if (m == 0)
341 panic("m_copydata");
342 if (off < m->m_len)
343 break;
344 off -= m->m_len;
345 m = m->m_next;
346 }
347 while (len > 0) {
348 if (m == 0)
349 panic("m_copydata");
350 count = min(m->m_len - off, len);
351 bcopy(mtod(m, caddr_t) + off, cp, count);
352 len -= count;
353 cp += count;
354 off = 0;
355 m = m->m_next;
356 }
357 }
358
359 /*
360 * Concatenate mbuf chain n to m.
361 * Both chains must be of the same type (e.g. MT_DATA).
362 * Any m_pkthdr is not updated.
363 */
364 void
365 m_cat(m, n)
366 register struct mbuf *m, *n;
367 {
368 while (m->m_next)
369 m = m->m_next;
370 while (n) {
371 if (m->m_flags & M_EXT ||
372 m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
373 /* just join the two chains */
374 m->m_next = n;
375 return;
376 }
377 /* splat the data from one into the other */
378 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
379 (u_int)n->m_len);
380 m->m_len += n->m_len;
381 n = m_free(n);
382 }
383 }
384
385 void
386 m_adj(mp, req_len)
387 struct mbuf *mp;
388 int req_len;
389 {
390 register int len = req_len;
391 register struct mbuf *m;
392 register count;
393
394 if ((m = mp) == NULL)
395 return;
396 if (len >= 0) {
397 /*
398 * Trim from head.
399 */
400 while (m != NULL && len > 0) {
401 if (m->m_len <= len) {
402 len -= m->m_len;
403 m->m_len = 0;
404 m = m->m_next;
405 } else {
406 m->m_len -= len;
407 m->m_data += len;
408 len = 0;
409 }
410 }
411 m = mp;
412 if (mp->m_flags & M_PKTHDR)
413 m->m_pkthdr.len -= (req_len - len);
414 } else {
415 /*
416 * Trim from tail. Scan the mbuf chain,
417 * calculating its length and finding the last mbuf.
418 * If the adjustment only affects this mbuf, then just
419 * adjust and return. Otherwise, rescan and truncate
420 * after the remaining size.
421 */
422 len = -len;
423 count = 0;
424 for (;;) {
425 count += m->m_len;
426 if (m->m_next == (struct mbuf *)0)
427 break;
428 m = m->m_next;
429 }
430 if (m->m_len >= len) {
431 m->m_len -= len;
432 if (mp->m_flags & M_PKTHDR)
433 mp->m_pkthdr.len -= len;
434 return;
435 }
436 count -= len;
437 if (count < 0)
438 count = 0;
439 /*
440 * Correct length for chain is "count".
441 * Find the mbuf with last data, adjust its length,
442 * and toss data from remaining mbufs on chain.
443 */
444 m = mp;
445 if (m->m_flags & M_PKTHDR)
446 m->m_pkthdr.len = count;
447 for (; m; m = m->m_next) {
448 if (m->m_len >= count) {
449 m->m_len = count;
450 break;
451 }
452 count -= m->m_len;
453 }
454 while ((m = m->m_next) != NULL)
455 m->m_len = 0;
456 }
457 }
458
459 /*
460 * Rearange an mbuf chain so that len bytes are contiguous
461 * and in the data area of an mbuf (so that mtod and dtom
462 * will work for a structure of size len). Returns the resulting
463 * mbuf chain on success, frees it and returns null on failure.
464 * If there is room, it will add up to max_protohdr-len extra bytes to the
465 * contiguous region in an attempt to avoid being called next time.
466 */
467 int MPFail;
468
469 struct mbuf *
470 m_pullup(n, len)
471 register struct mbuf *n;
472 int len;
473 {
474 register struct mbuf *m;
475 register int count;
476 int space;
477
478 /*
479 * If first mbuf has no cluster, and has room for len bytes
480 * without shifting current data, pullup into it,
481 * otherwise allocate a new mbuf to prepend to the chain.
482 */
483 if ((n->m_flags & M_EXT) == 0 &&
484 n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
485 if (n->m_len >= len)
486 return (n);
487 m = n;
488 n = n->m_next;
489 len -= m->m_len;
490 } else {
491 if (len > MHLEN)
492 goto bad;
493 MGET(m, M_DONTWAIT, n->m_type);
494 if (m == 0)
495 goto bad;
496 m->m_len = 0;
497 if (n->m_flags & M_PKTHDR) {
498 M_COPY_PKTHDR(m, n);
499 n->m_flags &= ~M_PKTHDR;
500 }
501 }
502 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
503 do {
504 count = min(min(max(len, max_protohdr), space), n->m_len);
505 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
506 (unsigned)count);
507 len -= count;
508 m->m_len += count;
509 n->m_len -= count;
510 space -= count;
511 if (n->m_len)
512 n->m_data += count;
513 else
514 n = m_free(n);
515 } while (len > 0 && n);
516 if (len > 0) {
517 (void) m_free(m);
518 goto bad;
519 }
520 m->m_next = n;
521 return (m);
522 bad:
523 m_freem(n);
524 MPFail++;
525 return (0);
526 }
527
528 /*
529 * Partition an mbuf chain in two pieces, returning the tail --
530 * all but the first len0 bytes. In case of failure, it returns NULL and
531 * attempts to restore the chain to its original state.
532 */
533 struct mbuf *
534 m_split(m0, len0, wait)
535 register struct mbuf *m0;
536 int len0, wait;
537 {
538 register struct mbuf *m, *n;
539 unsigned len = len0, remain;
540
541 for (m = m0; m && len > m->m_len; m = m->m_next)
542 len -= m->m_len;
543 if (m == 0)
544 return (0);
545 remain = m->m_len - len;
546 if (m0->m_flags & M_PKTHDR) {
547 MGETHDR(n, wait, m0->m_type);
548 if (n == 0)
549 return (0);
550 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
551 n->m_pkthdr.len = m0->m_pkthdr.len - len0;
552 m0->m_pkthdr.len = len0;
553 if (m->m_flags & M_EXT)
554 goto extpacket;
555 if (remain > MHLEN) {
556 /* m can't be the lead packet */
557 MH_ALIGN(n, 0);
558 n->m_next = m_split(m, len, wait);
559 if (n->m_next == 0) {
560 (void) m_free(n);
561 return (0);
562 } else
563 return (n);
564 } else
565 MH_ALIGN(n, remain);
566 } else if (remain == 0) {
567 n = m->m_next;
568 m->m_next = 0;
569 return (n);
570 } else {
571 MGET(n, wait, m->m_type);
572 if (n == 0)
573 return (0);
574 M_ALIGN(n, remain);
575 }
576 extpacket:
577 if (m->m_flags & M_EXT) {
578 n->m_flags |= M_EXT;
579 n->m_ext = m->m_ext;
580 mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
581 m->m_ext.ext_size = 0; /* For Accounting XXXXXX danger */
582 n->m_data = m->m_data + len;
583 } else {
584 bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
585 }
586 n->m_len = remain;
587 m->m_len = len;
588 n->m_next = m->m_next;
589 m->m_next = 0;
590 return (n);
591 }
592 /*
593 * Routine to copy from device local memory into mbufs.
594 */
595 struct mbuf *
596 m_devget(buf, totlen, off0, ifp, copy)
597 char *buf;
598 int totlen, off0;
599 struct ifnet *ifp;
600 void (*copy) __P((const void *, void *, size_t));
601 {
602 register struct mbuf *m;
603 struct mbuf *top = 0, **mp = ⊤
604 register int off = off0, len;
605 register char *cp;
606 char *epkt;
607
608 cp = buf;
609 epkt = cp + totlen;
610 if (off) {
611 /*
612 * If 'off' is non-zero, packet is trailer-encapsulated,
613 * so we have to skip the type and length fields.
614 */
615 cp += off + 2 * sizeof(u_int16_t);
616 totlen -= 2 * sizeof(u_int16_t);
617 }
618 MGETHDR(m, M_DONTWAIT, MT_DATA);
619 if (m == 0)
620 return (0);
621 m->m_pkthdr.rcvif = ifp;
622 m->m_pkthdr.len = totlen;
623 m->m_len = MHLEN;
624
625 while (totlen > 0) {
626 if (top) {
627 MGET(m, M_DONTWAIT, MT_DATA);
628 if (m == 0) {
629 m_freem(top);
630 return (0);
631 }
632 m->m_len = MLEN;
633 }
634 len = min(totlen, epkt - cp);
635 if (len >= MINCLSIZE) {
636 MCLGET(m, M_DONTWAIT);
637 if (m->m_flags & M_EXT)
638 m->m_len = len = min(len, MCLBYTES);
639 else
640 len = m->m_len;
641 } else {
642 /*
643 * Place initial small packet/header at end of mbuf.
644 */
645 if (len < m->m_len) {
646 if (top == 0 && len + max_linkhdr <= m->m_len)
647 m->m_data += max_linkhdr;
648 m->m_len = len;
649 } else
650 len = m->m_len;
651 }
652 if (copy)
653 copy(cp, mtod(m, caddr_t), (size_t)len);
654 else
655 bcopy(cp, mtod(m, caddr_t), (size_t)len);
656 cp += len;
657 *mp = m;
658 mp = &m->m_next;
659 totlen -= len;
660 if (cp == epkt)
661 cp = buf;
662 }
663 return (top);
664 }
665