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