uipc_mbuf.c revision 1.65 1 /* $NetBSD: uipc_mbuf.c,v 1.65 2003/04/09 18:38:03 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1982, 1986, 1988, 1991, 1993
42 * The Regents of the University of California. All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the University of
55 * California, Berkeley and its contributors.
56 * 4. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 *
72 * @(#)uipc_mbuf.c 8.4 (Berkeley) 2/14/95
73 */
74
75 #include <sys/cdefs.h>
76 __KERNEL_RCSID(0, "$NetBSD: uipc_mbuf.c,v 1.65 2003/04/09 18:38:03 thorpej Exp $");
77
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/proc.h>
81 #include <sys/malloc.h>
82 #define MBTYPES
83 #include <sys/mbuf.h>
84 #include <sys/kernel.h>
85 #include <sys/syslog.h>
86 #include <sys/domain.h>
87 #include <sys/protosw.h>
88 #include <sys/pool.h>
89 #include <sys/socket.h>
90 #include <sys/sysctl.h>
91
92 #include <net/if.h>
93
94 #include <uvm/uvm.h>
95
96
97 struct pool mbpool; /* mbuf pool */
98 struct pool mclpool; /* mbuf cluster pool */
99
100 struct pool_cache mbpool_cache;
101 struct pool_cache mclpool_cache;
102
103 struct mbstat mbstat;
104 int max_linkhdr;
105 int max_protohdr;
106 int max_hdr;
107 int max_datalen;
108
109 static int mb_ctor(void *, void *, int);
110
111 void *mclpool_alloc(struct pool *, int);
112 void mclpool_release(struct pool *, void *);
113
114 struct pool_allocator mclpool_allocator = {
115 mclpool_alloc, mclpool_release, 0,
116 };
117
118 static struct mbuf *m_copym0 __P((struct mbuf *, int, int, int, int));
119
120 const char mclpool_warnmsg[] =
121 "WARNING: mclpool limit reached; increase NMBCLUSTERS";
122
123 MALLOC_DEFINE(M_MBUF, "mbuf", "mbuf");
124
125 #ifdef MBUFTRACE
126 struct mownerhead mowners = LIST_HEAD_INITIALIZER(mowners);
127 struct mowner unknown_mowners[] = {
128 { "unknown", "free" },
129 { "unknown", "data" },
130 { "unknown", "header" },
131 { "unknown", "soname" },
132 { "unknown", "soopts" },
133 { "unknown", "ftable" },
134 { "unknown", "control" },
135 { "unknown", "oobdata" },
136 };
137 struct mowner revoked_mowner = { "revoked", "" };
138 #endif
139
140 /*
141 * Initialize the mbuf allcator.
142 */
143 void
144 mbinit(void)
145 {
146
147 KASSERT(sizeof(struct _m_ext) <= MHLEN);
148
149 pool_init(&mbpool, msize, 0, 0, 0, "mbpl", NULL);
150 pool_init(&mclpool, mclbytes, 0, 0, 0, "mclpl", &mclpool_allocator);
151
152 pool_set_drain_hook(&mbpool, m_reclaim, NULL);
153 pool_set_drain_hook(&mclpool, m_reclaim, NULL);
154
155 pool_cache_init(&mbpool_cache, &mbpool, mb_ctor, NULL, NULL);
156 pool_cache_init(&mclpool_cache, &mclpool, NULL, NULL, NULL);
157
158 /*
159 * Set the hard limit on the mclpool to the number of
160 * mbuf clusters the kernel is to support. Log the limit
161 * reached message max once a minute.
162 */
163 pool_sethardlimit(&mclpool, nmbclusters, mclpool_warnmsg, 60);
164
165 /*
166 * Set a low water mark for both mbufs and clusters. This should
167 * help ensure that they can be allocated in a memory starvation
168 * situation. This is important for e.g. diskless systems which
169 * must allocate mbufs in order for the pagedaemon to clean pages.
170 */
171 pool_setlowat(&mbpool, mblowat);
172 pool_setlowat(&mclpool, mcllowat);
173
174 #ifdef MBUFTRACE
175 {
176 /*
177 * Attach the unknown mowners.
178 */
179 int i;
180 MOWNER_ATTACH(&revoked_mowner);
181 for (i = sizeof(unknown_mowners)/sizeof(unknown_mowners[0]);
182 i-- > 0; )
183 MOWNER_ATTACH(&unknown_mowners[i]);
184 }
185 #endif
186 }
187
188 int
189 sysctl_dombuf(int *name, u_int namelen, void *oldp, size_t *oldlenp,
190 void *newp, size_t newlen)
191 {
192 int error, newval;
193
194 /* All sysctl names at this level are terminal. */
195 if (namelen != 1)
196 return (ENOTDIR); /* overloaded */
197
198 switch (name[0]) {
199 case MBUF_MSIZE:
200 return (sysctl_rdint(oldp, oldlenp, newp, msize));
201 case MBUF_MCLBYTES:
202 return (sysctl_rdint(oldp, oldlenp, newp, mclbytes));
203 case MBUF_NMBCLUSTERS:
204 /*
205 * If we have direct-mapped pool pages, we can adjust this
206 * number on the fly. If not, we're limited by the size
207 * of mb_map, and cannot change this value.
208 *
209 * Note: we only allow the value to be increased, never
210 * decreased.
211 */
212 if (mb_map == NULL) {
213 newval = nmbclusters;
214 error = sysctl_int(oldp, oldlenp, newp, newlen,
215 &newval);
216 if (error != 0)
217 return (error);
218 if (newp != NULL) {
219 if (newval >= nmbclusters) {
220 nmbclusters = newval;
221 pool_sethardlimit(&mclpool,
222 nmbclusters, mclpool_warnmsg, 60);
223 } else
224 error = EINVAL;
225 }
226 return (error);
227 } else
228 return (sysctl_rdint(oldp, oldlenp, newp, nmbclusters));
229 case MBUF_MBLOWAT:
230 case MBUF_MCLLOWAT:
231 /* New value must be >= 0. */
232 newval = (name[0] == MBUF_MBLOWAT) ? mblowat : mcllowat;
233 error = sysctl_int(oldp, oldlenp, newp, newlen, &newval);
234 if (error != 0)
235 return (error);
236 if (newp != NULL) {
237 if (newval >= 0) {
238 if (name[0] == MBUF_MBLOWAT) {
239 mblowat = newval;
240 pool_setlowat(&mbpool, newval);
241 } else {
242 mcllowat = newval;
243 pool_setlowat(&mclpool, newval);
244 }
245 } else
246 error = EINVAL;
247 }
248 return (error);
249 case MBUF_STATS:
250 return (sysctl_rdstruct(oldp, oldlenp, newp,
251 &mbstat, sizeof(mbstat)));
252 #ifdef MBUFTRACE
253 case MBUF_MOWNERS: {
254 struct mowner *mo;
255 size_t len = 0;
256 if (newp != NULL)
257 return (EPERM);
258 error = 0;
259 LIST_FOREACH(mo, &mowners, mo_link) {
260 if (oldp != NULL) {
261 if (*oldlenp - len < sizeof(*mo)) {
262 error = ENOMEM;
263 break;
264 }
265 error = copyout(mo, (caddr_t) oldp + len,
266 sizeof(*mo));
267 if (error)
268 break;
269 }
270 len += sizeof(*mo);
271 }
272 *oldlenp = len;
273 return (error);
274 }
275 #endif
276 default:
277 return (EOPNOTSUPP);
278 }
279 /* NOTREACHED */
280 }
281
282 void *
283 mclpool_alloc(struct pool *pp, int flags)
284 {
285 boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
286
287 return ((void *)uvm_km_alloc_poolpage1(mb_map, NULL, waitok));
288 }
289
290 void
291 mclpool_release(struct pool *pp, void *v)
292 {
293
294 uvm_km_free_poolpage1(mb_map, (vaddr_t)v);
295 }
296
297 /*ARGSUSED*/
298 static int
299 mb_ctor(void *arg, void *object, int flags)
300 {
301 struct mbuf *m = object;
302
303 #ifdef POOL_VTOPHYS
304 m->m_paddr = POOL_VTOPHYS(m);
305 #else
306 m->m_paddr = M_PADDR_INVALID;
307 #endif
308 return (0);
309 }
310
311 void
312 m_reclaim(void *arg, int flags)
313 {
314 struct domain *dp;
315 struct protosw *pr;
316 struct ifnet *ifp;
317 int s = splvm();
318
319 for (dp = domains; dp; dp = dp->dom_next)
320 for (pr = dp->dom_protosw;
321 pr < dp->dom_protoswNPROTOSW; pr++)
322 if (pr->pr_drain)
323 (*pr->pr_drain)();
324 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
325 if (ifp->if_drain)
326 (*ifp->if_drain)(ifp);
327 splx(s);
328 mbstat.m_drain++;
329 }
330
331 /*
332 * Space allocation routines.
333 * These are also available as macros
334 * for critical paths.
335 */
336 struct mbuf *
337 m_get(int nowait, int type)
338 {
339 struct mbuf *m;
340
341 MGET(m, nowait, type);
342 return (m);
343 }
344
345 struct mbuf *
346 m_gethdr(int nowait, int type)
347 {
348 struct mbuf *m;
349
350 MGETHDR(m, nowait, type);
351 return (m);
352 }
353
354 struct mbuf *
355 m_getclr(int nowait, int type)
356 {
357 struct mbuf *m;
358
359 MGET(m, nowait, type);
360 if (m == 0)
361 return (0);
362 memset(mtod(m, caddr_t), 0, MLEN);
363 return (m);
364 }
365
366 void
367 m_clget(struct mbuf *m, int nowait)
368 {
369 MCLGET(m, nowait);
370 }
371
372 struct mbuf *
373 m_free(struct mbuf *m)
374 {
375 struct mbuf *n;
376
377 MFREE(m, n);
378 return (n);
379 }
380
381 void
382 m_freem(struct mbuf *m)
383 {
384 struct mbuf *n;
385
386 if (m == NULL)
387 return;
388 do {
389 MFREE(m, n);
390 m = n;
391 } while (m);
392 }
393
394 #ifdef MBUFTRACE
395 void
396 m_claim(struct mbuf *m, struct mowner *mo)
397 {
398 for (; m != NULL; m = m->m_next)
399 MCLAIM(m, mo);
400 }
401 #endif
402
403 /*
404 * Mbuffer utility routines.
405 */
406
407 /*
408 * Lesser-used path for M_PREPEND:
409 * allocate new mbuf to prepend to chain,
410 * copy junk along.
411 */
412 struct mbuf *
413 m_prepend(struct mbuf *m, int len, int how)
414 {
415 struct mbuf *mn;
416
417 MGET(mn, how, m->m_type);
418 if (mn == (struct mbuf *)NULL) {
419 m_freem(m);
420 return ((struct mbuf *)NULL);
421 }
422 if (m->m_flags & M_PKTHDR) {
423 M_COPY_PKTHDR(mn, m);
424 m->m_flags &= ~M_PKTHDR;
425 } else {
426 MCLAIM(mn, m->m_owner);
427 }
428 mn->m_next = m;
429 m = mn;
430 if (len < MHLEN)
431 MH_ALIGN(m, len);
432 m->m_len = len;
433 return (m);
434 }
435
436 /*
437 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
438 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
439 * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
440 */
441 int MCFail;
442
443 struct mbuf *
444 m_copym(struct mbuf *m, int off0, int len, int wait)
445 {
446 return m_copym0(m, off0, len, wait, 0); /* shallow copy on M_EXT */
447 }
448
449 struct mbuf *
450 m_dup(struct mbuf *m, int off0, int len, int wait)
451 {
452 return m_copym0(m, off0, len, wait, 1); /* deep copy */
453 }
454
455 static struct mbuf *
456 m_copym0(struct mbuf *m, int off0, int len, int wait, int deep)
457 {
458 struct mbuf *n, **np;
459 int off = off0;
460 struct mbuf *top;
461 int copyhdr = 0;
462
463 if (off < 0 || len < 0)
464 panic("m_copym: off %d, len %d", off, len);
465 if (off == 0 && m->m_flags & M_PKTHDR)
466 copyhdr = 1;
467 while (off > 0) {
468 if (m == 0)
469 panic("m_copym: m == 0");
470 if (off < m->m_len)
471 break;
472 off -= m->m_len;
473 m = m->m_next;
474 }
475 np = ⊤
476 top = 0;
477 while (len > 0) {
478 if (m == 0) {
479 if (len != M_COPYALL)
480 panic("m_copym: m == 0 and not COPYALL");
481 break;
482 }
483 MGET(n, wait, m->m_type);
484 *np = n;
485 if (n == 0)
486 goto nospace;
487 MCLAIM(n, m->m_owner);
488 if (copyhdr) {
489 M_COPY_PKTHDR(n, m);
490 if (len == M_COPYALL)
491 n->m_pkthdr.len -= off0;
492 else
493 n->m_pkthdr.len = len;
494 copyhdr = 0;
495 }
496 n->m_len = min(len, m->m_len - off);
497 if (m->m_flags & M_EXT) {
498 if (!deep) {
499 n->m_data = m->m_data + off;
500 n->m_ext = m->m_ext;
501 MCLADDREFERENCE(m, n);
502 } else {
503 /*
504 * we are unsure about the way m was allocated.
505 * copy into multiple MCLBYTES cluster mbufs.
506 */
507 MCLGET(n, wait);
508 n->m_len = 0;
509 n->m_len = M_TRAILINGSPACE(n);
510 n->m_len = min(n->m_len, len);
511 n->m_len = min(n->m_len, m->m_len - off);
512 memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + off,
513 (unsigned)n->m_len);
514 }
515 } else
516 memcpy(mtod(n, caddr_t), mtod(m, caddr_t)+off,
517 (unsigned)n->m_len);
518 if (len != M_COPYALL)
519 len -= n->m_len;
520 off += n->m_len;
521 #ifdef DIAGNOSTIC
522 if (off > m->m_len)
523 panic("m_copym0 overrun");
524 #endif
525 if (off == m->m_len) {
526 m = m->m_next;
527 off = 0;
528 }
529 np = &n->m_next;
530 }
531 if (top == 0)
532 MCFail++;
533 return (top);
534 nospace:
535 m_freem(top);
536 MCFail++;
537 return (0);
538 }
539
540 /*
541 * Copy an entire packet, including header (which must be present).
542 * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
543 */
544 struct mbuf *
545 m_copypacket(struct mbuf *m, int how)
546 {
547 struct mbuf *top, *n, *o;
548
549 MGET(n, how, m->m_type);
550 top = n;
551 if (!n)
552 goto nospace;
553
554 MCLAIM(n, m->m_owner);
555 M_COPY_PKTHDR(n, m);
556 n->m_len = m->m_len;
557 if (m->m_flags & M_EXT) {
558 n->m_data = m->m_data;
559 n->m_ext = m->m_ext;
560 MCLADDREFERENCE(m, n);
561 } else {
562 memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
563 }
564
565 m = m->m_next;
566 while (m) {
567 MGET(o, how, m->m_type);
568 if (!o)
569 goto nospace;
570
571 MCLAIM(o, m->m_owner);
572 n->m_next = o;
573 n = n->m_next;
574
575 n->m_len = m->m_len;
576 if (m->m_flags & M_EXT) {
577 n->m_data = m->m_data;
578 n->m_ext = m->m_ext;
579 MCLADDREFERENCE(m, n);
580 } else {
581 memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
582 }
583
584 m = m->m_next;
585 }
586 return top;
587 nospace:
588 m_freem(top);
589 MCFail++;
590 return 0;
591 }
592
593 /*
594 * Copy data from an mbuf chain starting "off" bytes from the beginning,
595 * continuing for "len" bytes, into the indicated buffer.
596 */
597 void
598 m_copydata(struct mbuf *m, int off, int len, caddr_t cp)
599 {
600 unsigned count;
601
602 if (off < 0 || len < 0)
603 panic("m_copydata");
604 while (off > 0) {
605 if (m == 0)
606 panic("m_copydata");
607 if (off < m->m_len)
608 break;
609 off -= m->m_len;
610 m = m->m_next;
611 }
612 while (len > 0) {
613 if (m == 0)
614 panic("m_copydata");
615 count = min(m->m_len - off, len);
616 memcpy(cp, mtod(m, caddr_t) + off, count);
617 len -= count;
618 cp += count;
619 off = 0;
620 m = m->m_next;
621 }
622 }
623
624 /*
625 * Concatenate mbuf chain n to m.
626 * Both chains must be of the same type (e.g. MT_DATA).
627 * Any m_pkthdr is not updated.
628 */
629 void
630 m_cat(struct mbuf *m, struct mbuf *n)
631 {
632 while (m->m_next)
633 m = m->m_next;
634 while (n) {
635 if (m->m_flags & M_EXT ||
636 m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
637 /* just join the two chains */
638 m->m_next = n;
639 return;
640 }
641 /* splat the data from one into the other */
642 memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
643 (u_int)n->m_len);
644 m->m_len += n->m_len;
645 n = m_free(n);
646 }
647 }
648
649 void
650 m_adj(struct mbuf *mp, int req_len)
651 {
652 int len = req_len;
653 struct mbuf *m;
654 int count;
655
656 if ((m = mp) == NULL)
657 return;
658 if (len >= 0) {
659 /*
660 * Trim from head.
661 */
662 while (m != NULL && len > 0) {
663 if (m->m_len <= len) {
664 len -= m->m_len;
665 m->m_len = 0;
666 m = m->m_next;
667 } else {
668 m->m_len -= len;
669 m->m_data += len;
670 len = 0;
671 }
672 }
673 m = mp;
674 if (mp->m_flags & M_PKTHDR)
675 m->m_pkthdr.len -= (req_len - len);
676 } else {
677 /*
678 * Trim from tail. Scan the mbuf chain,
679 * calculating its length and finding the last mbuf.
680 * If the adjustment only affects this mbuf, then just
681 * adjust and return. Otherwise, rescan and truncate
682 * after the remaining size.
683 */
684 len = -len;
685 count = 0;
686 for (;;) {
687 count += m->m_len;
688 if (m->m_next == (struct mbuf *)0)
689 break;
690 m = m->m_next;
691 }
692 if (m->m_len >= len) {
693 m->m_len -= len;
694 if (mp->m_flags & M_PKTHDR)
695 mp->m_pkthdr.len -= len;
696 return;
697 }
698 count -= len;
699 if (count < 0)
700 count = 0;
701 /*
702 * Correct length for chain is "count".
703 * Find the mbuf with last data, adjust its length,
704 * and toss data from remaining mbufs on chain.
705 */
706 m = mp;
707 if (m->m_flags & M_PKTHDR)
708 m->m_pkthdr.len = count;
709 for (; m; m = m->m_next) {
710 if (m->m_len >= count) {
711 m->m_len = count;
712 break;
713 }
714 count -= m->m_len;
715 }
716 while (m->m_next)
717 (m = m->m_next) ->m_len = 0;
718 }
719 }
720
721 /*
722 * Rearange an mbuf chain so that len bytes are contiguous
723 * and in the data area of an mbuf (so that mtod and dtom
724 * will work for a structure of size len). Returns the resulting
725 * mbuf chain on success, frees it and returns null on failure.
726 * If there is room, it will add up to max_protohdr-len extra bytes to the
727 * contiguous region in an attempt to avoid being called next time.
728 */
729 int MPFail;
730
731 struct mbuf *
732 m_pullup(struct mbuf *n, int len)
733 {
734 struct mbuf *m;
735 int count;
736 int space;
737
738 /*
739 * If first mbuf has no cluster, and has room for len bytes
740 * without shifting current data, pullup into it,
741 * otherwise allocate a new mbuf to prepend to the chain.
742 */
743 if ((n->m_flags & M_EXT) == 0 &&
744 n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
745 if (n->m_len >= len)
746 return (n);
747 m = n;
748 n = n->m_next;
749 len -= m->m_len;
750 } else {
751 if (len > MHLEN)
752 goto bad;
753 MGET(m, M_DONTWAIT, n->m_type);
754 if (m == 0)
755 goto bad;
756 MCLAIM(m, n->m_owner);
757 m->m_len = 0;
758 if (n->m_flags & M_PKTHDR) {
759 M_COPY_PKTHDR(m, n);
760 n->m_flags &= ~M_PKTHDR;
761 }
762 }
763 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
764 do {
765 count = min(min(max(len, max_protohdr), space), n->m_len);
766 memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
767 (unsigned)count);
768 len -= count;
769 m->m_len += count;
770 n->m_len -= count;
771 space -= count;
772 if (n->m_len)
773 n->m_data += count;
774 else
775 n = m_free(n);
776 } while (len > 0 && n);
777 if (len > 0) {
778 (void) m_free(m);
779 goto bad;
780 }
781 m->m_next = n;
782 return (m);
783 bad:
784 m_freem(n);
785 MPFail++;
786 return (0);
787 }
788
789 /*
790 * Like m_pullup(), except a new mbuf is always allocated, and we allow
791 * the amount of empty space before the data in the new mbuf to be specified
792 * (in the event that the caller expects to prepend later).
793 */
794 int MSFail;
795
796 struct mbuf *
797 m_copyup(struct mbuf *n, int len, int dstoff)
798 {
799 struct mbuf *m;
800 int count, space;
801
802 if (len > (MHLEN - dstoff))
803 goto bad;
804 MGET(m, M_DONTWAIT, n->m_type);
805 if (m == NULL)
806 goto bad;
807 MCLAIM(m, n->m_owner);
808 m->m_len = 0;
809 if (n->m_flags & M_PKTHDR) {
810 M_COPY_PKTHDR(m, n);
811 n->m_flags &= ~M_PKTHDR;
812 }
813 m->m_data += dstoff;
814 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
815 do {
816 count = min(min(max(len, max_protohdr), space), n->m_len);
817 memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
818 (unsigned)count);
819 len -= count;
820 m->m_len += count;
821 n->m_len -= count;
822 space -= count;
823 if (n->m_len)
824 n->m_data += count;
825 else
826 n = m_free(n);
827 } while (len > 0 && n);
828 if (len > 0) {
829 (void) m_free(m);
830 goto bad;
831 }
832 m->m_next = n;
833 return (m);
834 bad:
835 m_freem(n);
836 MSFail++;
837 return (NULL);
838 }
839
840 /*
841 * Partition an mbuf chain in two pieces, returning the tail --
842 * all but the first len0 bytes. In case of failure, it returns NULL and
843 * attempts to restore the chain to its original state.
844 */
845 struct mbuf *
846 m_split(struct mbuf *m0, int len0, int wait)
847 {
848 struct mbuf *m, *n;
849 unsigned len = len0, remain, len_save;
850
851 for (m = m0; m && len > m->m_len; m = m->m_next)
852 len -= m->m_len;
853 if (m == 0)
854 return (0);
855 remain = m->m_len - len;
856 if (m0->m_flags & M_PKTHDR) {
857 MGETHDR(n, wait, m0->m_type);
858 if (n == 0)
859 return (0);
860 MCLAIM(m, m0->m_owner);
861 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
862 n->m_pkthdr.len = m0->m_pkthdr.len - len0;
863 len_save = m0->m_pkthdr.len;
864 m0->m_pkthdr.len = len0;
865 if (m->m_flags & M_EXT)
866 goto extpacket;
867 if (remain > MHLEN) {
868 /* m can't be the lead packet */
869 MH_ALIGN(n, 0);
870 n->m_next = m_split(m, len, wait);
871 if (n->m_next == 0) {
872 (void) m_free(n);
873 m0->m_pkthdr.len = len_save;
874 return (0);
875 } else
876 return (n);
877 } else
878 MH_ALIGN(n, remain);
879 } else if (remain == 0) {
880 n = m->m_next;
881 m->m_next = 0;
882 return (n);
883 } else {
884 MGET(n, wait, m->m_type);
885 if (n == 0)
886 return (0);
887 MCLAIM(n, m->m_owner);
888 M_ALIGN(n, remain);
889 }
890 extpacket:
891 if (m->m_flags & M_EXT) {
892 n->m_ext = m->m_ext;
893 MCLADDREFERENCE(m, n);
894 n->m_data = m->m_data + len;
895 } else {
896 memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + len, remain);
897 }
898 n->m_len = remain;
899 m->m_len = len;
900 n->m_next = m->m_next;
901 m->m_next = 0;
902 return (n);
903 }
904 /*
905 * Routine to copy from device local memory into mbufs.
906 */
907 struct mbuf *
908 m_devget(char *buf, int totlen, int off0, struct ifnet *ifp,
909 void (*copy)(const void *from, void *to, size_t len))
910 {
911 struct mbuf *m;
912 struct mbuf *top = 0, **mp = ⊤
913 int off = off0, len;
914 char *cp;
915 char *epkt;
916
917 cp = buf;
918 epkt = cp + totlen;
919 if (off) {
920 /*
921 * If 'off' is non-zero, packet is trailer-encapsulated,
922 * so we have to skip the type and length fields.
923 */
924 cp += off + 2 * sizeof(u_int16_t);
925 totlen -= 2 * sizeof(u_int16_t);
926 }
927 MGETHDR(m, M_DONTWAIT, MT_DATA);
928 if (m == 0)
929 return (0);
930 m->m_pkthdr.rcvif = ifp;
931 m->m_pkthdr.len = totlen;
932 m->m_len = MHLEN;
933
934 while (totlen > 0) {
935 if (top) {
936 MGET(m, M_DONTWAIT, MT_DATA);
937 if (m == 0) {
938 m_freem(top);
939 return (0);
940 }
941 m->m_len = MLEN;
942 }
943 len = min(totlen, epkt - cp);
944 if (len >= MINCLSIZE) {
945 MCLGET(m, M_DONTWAIT);
946 if ((m->m_flags & M_EXT) == 0) {
947 m_free(m);
948 m_freem(top);
949 return (0);
950 }
951 m->m_len = len = min(len, MCLBYTES);
952 } else {
953 /*
954 * Place initial small packet/header at end of mbuf.
955 */
956 if (len < m->m_len) {
957 if (top == 0 && len + max_linkhdr <= m->m_len)
958 m->m_data += max_linkhdr;
959 m->m_len = len;
960 } else
961 len = m->m_len;
962 }
963 if (copy)
964 copy(cp, mtod(m, caddr_t), (size_t)len);
965 else
966 memcpy(mtod(m, caddr_t), cp, (size_t)len);
967 cp += len;
968 *mp = m;
969 mp = &m->m_next;
970 totlen -= len;
971 if (cp == epkt)
972 cp = buf;
973 }
974 return (top);
975 }
976
977 /*
978 * Copy data from a buffer back into the indicated mbuf chain,
979 * starting "off" bytes from the beginning, extending the mbuf
980 * chain if necessary.
981 */
982 void
983 m_copyback(struct mbuf *m0, int off, int len, caddr_t cp)
984 {
985 int mlen;
986 struct mbuf *m = m0, *n;
987 int totlen = 0;
988
989 if (m0 == 0)
990 return;
991 while (off > (mlen = m->m_len)) {
992 off -= mlen;
993 totlen += mlen;
994 if (m->m_next == 0) {
995 n = m_getclr(M_DONTWAIT, m->m_type);
996 if (n == 0)
997 goto out;
998 n->m_len = min(MLEN, len + off);
999 m->m_next = n;
1000 }
1001 m = m->m_next;
1002 }
1003 while (len > 0) {
1004 mlen = min (m->m_len - off, len);
1005 memcpy(mtod(m, caddr_t) + off, cp, (unsigned)mlen);
1006 cp += mlen;
1007 len -= mlen;
1008 mlen += off;
1009 off = 0;
1010 totlen += mlen;
1011 if (len == 0)
1012 break;
1013 if (m->m_next == 0) {
1014 n = m_get(M_DONTWAIT, m->m_type);
1015 if (n == 0)
1016 break;
1017 n->m_len = min(MLEN, len);
1018 m->m_next = n;
1019 }
1020 m = m->m_next;
1021 }
1022 out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
1023 m->m_pkthdr.len = totlen;
1024 }
1025