uipc_mbuf.c revision 1.81 1 /* $NetBSD: uipc_mbuf.c,v 1.81 2004/04/22 01:01:40 matt 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.81 2004/04/22 01:01:40 matt 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(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 /*
188 * sysctl helper routine for the kern.mbuf subtree. nmbclusters may
189 * or may not be writable, and mblowat and mcllowat need range
190 * checking and pool tweaking after being reset.
191 */
192 static int
193 sysctl_kern_mbuf(SYSCTLFN_ARGS)
194 {
195 int error, newval;
196 struct sysctlnode node;
197
198 node = *rnode;
199 node.sysctl_data = &newval;
200 switch (rnode->sysctl_num) {
201 case MBUF_NMBCLUSTERS:
202 if (mb_map != NULL) {
203 node.sysctl_flags &= ~CTLFLAG_READWRITE;
204 node.sysctl_flags |= CTLFLAG_READONLY;
205 }
206 /* FALLTHROUGH */
207 case MBUF_MBLOWAT:
208 case MBUF_MCLLOWAT:
209 newval = *(int*)rnode->sysctl_data;
210 break;
211 default:
212 return (EOPNOTSUPP);
213 }
214
215 error = sysctl_lookup(SYSCTLFN_CALL(&node));
216 if (error || newp == NULL)
217 return (error);
218 if (newval < 0)
219 return (EINVAL);
220
221 switch (node.sysctl_num) {
222 case MBUF_NMBCLUSTERS:
223 if (newval < nmbclusters)
224 return (EINVAL);
225 nmbclusters = newval;
226 pool_sethardlimit(&mclpool, nmbclusters, mclpool_warnmsg, 60);
227 break;
228 case MBUF_MBLOWAT:
229 mblowat = newval;
230 pool_setlowat(&mbpool, mblowat);
231 break;
232 case MBUF_MCLLOWAT:
233 mcllowat = newval;
234 pool_setlowat(&mclpool, mcllowat);
235 break;
236 }
237
238 return (0);
239 }
240
241 #ifdef MBUFTRACE
242 static int
243 sysctl_kern_mbuf_mowners(SYSCTLFN_ARGS)
244 {
245 struct mowner *mo;
246 size_t len = 0;
247 int error = 0;
248
249 if (namelen != 0)
250 return (EINVAL);
251 if (newp != NULL)
252 return (EPERM);
253
254 LIST_FOREACH(mo, &mowners, mo_link) {
255 if (oldp != NULL) {
256 if (*oldlenp - len < sizeof(*mo)) {
257 error = ENOMEM;
258 break;
259 }
260 error = copyout(mo, (caddr_t) oldp + len,
261 sizeof(*mo));
262 if (error)
263 break;
264 }
265 len += sizeof(*mo);
266 }
267
268 if (error == 0)
269 *oldlenp = len;
270
271 return (error);
272 }
273 #endif /* MBUFTRACE */
274
275 SYSCTL_SETUP(sysctl_kern_mbuf_setup, "sysctl kern.mbuf subtree setup")
276 {
277
278 sysctl_createv(clog, 0, NULL, NULL,
279 CTLFLAG_PERMANENT,
280 CTLTYPE_NODE, "kern", NULL,
281 NULL, 0, NULL, 0,
282 CTL_KERN, CTL_EOL);
283 sysctl_createv(clog, 0, NULL, NULL,
284 CTLFLAG_PERMANENT,
285 CTLTYPE_NODE, "mbuf", NULL,
286 NULL, 0, NULL, 0,
287 CTL_KERN, KERN_MBUF, CTL_EOL);
288
289 sysctl_createv(clog, 0, NULL, NULL,
290 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
291 CTLTYPE_INT, "msize", NULL,
292 NULL, msize, NULL, 0,
293 CTL_KERN, KERN_MBUF, MBUF_MSIZE, CTL_EOL);
294 sysctl_createv(clog, 0, NULL, NULL,
295 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
296 CTLTYPE_INT, "mclbytes", NULL,
297 NULL, mclbytes, NULL, 0,
298 CTL_KERN, KERN_MBUF, MBUF_MCLBYTES, CTL_EOL);
299 sysctl_createv(clog, 0, NULL, NULL,
300 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
301 CTLTYPE_INT, "nmbclusters", NULL,
302 sysctl_kern_mbuf, 0, &nmbclusters, 0,
303 CTL_KERN, KERN_MBUF, MBUF_NMBCLUSTERS, CTL_EOL);
304 sysctl_createv(clog, 0, NULL, NULL,
305 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
306 CTLTYPE_INT, "mblowat", NULL,
307 sysctl_kern_mbuf, 0, &mblowat, 0,
308 CTL_KERN, KERN_MBUF, MBUF_MBLOWAT, CTL_EOL);
309 sysctl_createv(clog, 0, NULL, NULL,
310 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
311 CTLTYPE_INT, "mcllowat", NULL,
312 sysctl_kern_mbuf, 0, &mcllowat, 0,
313 CTL_KERN, KERN_MBUF, MBUF_MCLLOWAT, CTL_EOL);
314 sysctl_createv(clog, 0, NULL, NULL,
315 CTLFLAG_PERMANENT,
316 CTLTYPE_STRUCT, "stats", NULL,
317 NULL, 0, &mbstat, sizeof(mbstat),
318 CTL_KERN, KERN_MBUF, MBUF_STATS, CTL_EOL);
319 #ifdef MBUFTRACE
320 sysctl_createv(clog, 0, NULL, NULL,
321 CTLFLAG_PERMANENT,
322 CTLTYPE_STRUCT, "mowners", NULL,
323 sysctl_kern_mbuf_mowners, 0, NULL, 0,
324 CTL_KERN, KERN_MBUF, MBUF_MOWNERS, CTL_EOL);
325 #endif /* MBUFTRACE */
326 }
327
328 void *
329 mclpool_alloc(struct pool *pp, int flags)
330 {
331 boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
332
333 return ((void *)uvm_km_alloc_poolpage1(mb_map, NULL, waitok));
334 }
335
336 void
337 mclpool_release(struct pool *pp, void *v)
338 {
339
340 uvm_km_free_poolpage1(mb_map, (vaddr_t)v);
341 }
342
343 /*ARGSUSED*/
344 static int
345 mb_ctor(void *arg, void *object, int flags)
346 {
347 struct mbuf *m = object;
348
349 #ifdef POOL_VTOPHYS
350 m->m_paddr = POOL_VTOPHYS(m);
351 #else
352 m->m_paddr = M_PADDR_INVALID;
353 #endif
354 return (0);
355 }
356
357 void
358 m_reclaim(void *arg, int flags)
359 {
360 struct domain *dp;
361 const struct protosw *pr;
362 struct ifnet *ifp;
363 int s = splvm();
364
365 for (dp = domains; dp; dp = dp->dom_next)
366 for (pr = dp->dom_protosw;
367 pr < dp->dom_protoswNPROTOSW; pr++)
368 if (pr->pr_drain)
369 (*pr->pr_drain)();
370 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
371 if (ifp->if_drain)
372 (*ifp->if_drain)(ifp);
373 splx(s);
374 mbstat.m_drain++;
375 }
376
377 /*
378 * Space allocation routines.
379 * These are also available as macros
380 * for critical paths.
381 */
382 struct mbuf *
383 m_get(int nowait, int type)
384 {
385 struct mbuf *m;
386
387 MGET(m, nowait, type);
388 return (m);
389 }
390
391 struct mbuf *
392 m_gethdr(int nowait, int type)
393 {
394 struct mbuf *m;
395
396 MGETHDR(m, nowait, type);
397 return (m);
398 }
399
400 struct mbuf *
401 m_getclr(int nowait, int type)
402 {
403 struct mbuf *m;
404
405 MGET(m, nowait, type);
406 if (m == 0)
407 return (NULL);
408 memset(mtod(m, caddr_t), 0, MLEN);
409 return (m);
410 }
411
412 void
413 m_clget(struct mbuf *m, int nowait)
414 {
415
416 MCLGET(m, nowait);
417 }
418
419 struct mbuf *
420 m_free(struct mbuf *m)
421 {
422 struct mbuf *n;
423
424 MFREE(m, n);
425 return (n);
426 }
427
428 void
429 m_freem(struct mbuf *m)
430 {
431 struct mbuf *n;
432
433 if (m == NULL)
434 return;
435 do {
436 MFREE(m, n);
437 m = n;
438 } while (m);
439 }
440
441 #ifdef MBUFTRACE
442 void
443 m_claim(struct mbuf *m, struct mowner *mo)
444 {
445
446 for (; m != NULL; m = m->m_next)
447 MCLAIM(m, mo);
448 }
449 #endif
450
451 /*
452 * Mbuffer utility routines.
453 */
454
455 /*
456 * Lesser-used path for M_PREPEND:
457 * allocate new mbuf to prepend to chain,
458 * copy junk along.
459 */
460 struct mbuf *
461 m_prepend(struct mbuf *m, int len, int how)
462 {
463 struct mbuf *mn;
464
465 MGET(mn, how, m->m_type);
466 if (mn == (struct mbuf *)NULL) {
467 m_freem(m);
468 return ((struct mbuf *)NULL);
469 }
470 if (m->m_flags & M_PKTHDR) {
471 M_COPY_PKTHDR(mn, m);
472 m_tag_delete_chain(m, NULL);
473 m->m_flags &= ~M_PKTHDR;
474 } else {
475 MCLAIM(mn, m->m_owner);
476 }
477 mn->m_next = m;
478 m = mn;
479 if (len < MHLEN)
480 MH_ALIGN(m, len);
481 m->m_len = len;
482 return (m);
483 }
484
485 /*
486 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
487 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
488 * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
489 */
490 int MCFail;
491
492 struct mbuf *
493 m_copym(struct mbuf *m, int off0, int len, int wait)
494 {
495
496 return m_copym0(m, off0, len, wait, 0); /* shallow copy on M_EXT */
497 }
498
499 struct mbuf *
500 m_dup(struct mbuf *m, int off0, int len, int wait)
501 {
502
503 return m_copym0(m, off0, len, wait, 1); /* deep copy */
504 }
505
506 static struct mbuf *
507 m_copym0(struct mbuf *m, int off0, int len, int wait, int deep)
508 {
509 struct mbuf *n, **np;
510 int off = off0;
511 struct mbuf *top;
512 int copyhdr = 0;
513
514 if (off < 0 || len < 0)
515 panic("m_copym: off %d, len %d", off, len);
516 if (off == 0 && m->m_flags & M_PKTHDR)
517 copyhdr = 1;
518 while (off > 0) {
519 if (m == 0)
520 panic("m_copym: m == 0");
521 if (off < m->m_len)
522 break;
523 off -= m->m_len;
524 m = m->m_next;
525 }
526 np = ⊤
527 top = 0;
528 while (len > 0) {
529 if (m == 0) {
530 if (len != M_COPYALL)
531 panic("m_copym: m == 0 and not COPYALL");
532 break;
533 }
534 MGET(n, wait, m->m_type);
535 *np = n;
536 if (n == 0)
537 goto nospace;
538 MCLAIM(n, m->m_owner);
539 if (copyhdr) {
540 M_COPY_PKTHDR(n, m);
541 if (len == M_COPYALL)
542 n->m_pkthdr.len -= off0;
543 else
544 n->m_pkthdr.len = len;
545 copyhdr = 0;
546 }
547 n->m_len = min(len, m->m_len - off);
548 if (m->m_flags & M_EXT) {
549 if (!deep) {
550 n->m_data = m->m_data + off;
551 n->m_ext = m->m_ext;
552 MCLADDREFERENCE(m, n);
553 } else {
554 /*
555 * we are unsure about the way m was allocated.
556 * copy into multiple MCLBYTES cluster mbufs.
557 */
558 MCLGET(n, wait);
559 n->m_len = 0;
560 n->m_len = M_TRAILINGSPACE(n);
561 n->m_len = min(n->m_len, len);
562 n->m_len = min(n->m_len, m->m_len - off);
563 memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + off,
564 (unsigned)n->m_len);
565 }
566 } else
567 memcpy(mtod(n, caddr_t), mtod(m, caddr_t)+off,
568 (unsigned)n->m_len);
569 if (len != M_COPYALL)
570 len -= n->m_len;
571 off += n->m_len;
572 #ifdef DIAGNOSTIC
573 if (off > m->m_len)
574 panic("m_copym0 overrun");
575 #endif
576 if (off == m->m_len) {
577 m = m->m_next;
578 off = 0;
579 }
580 np = &n->m_next;
581 }
582 if (top == 0)
583 MCFail++;
584 return (top);
585 nospace:
586 m_freem(top);
587 MCFail++;
588 return (NULL);
589 }
590
591 /*
592 * Copy an entire packet, including header (which must be present).
593 * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
594 */
595 struct mbuf *
596 m_copypacket(struct mbuf *m, int how)
597 {
598 struct mbuf *top, *n, *o;
599
600 MGET(n, how, m->m_type);
601 top = n;
602 if (!n)
603 goto nospace;
604
605 MCLAIM(n, m->m_owner);
606 M_COPY_PKTHDR(n, m);
607 n->m_len = m->m_len;
608 if (m->m_flags & M_EXT) {
609 n->m_data = m->m_data;
610 n->m_ext = m->m_ext;
611 MCLADDREFERENCE(m, n);
612 } else {
613 memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
614 }
615
616 m = m->m_next;
617 while (m) {
618 MGET(o, how, m->m_type);
619 if (!o)
620 goto nospace;
621
622 MCLAIM(o, m->m_owner);
623 n->m_next = o;
624 n = n->m_next;
625
626 n->m_len = m->m_len;
627 if (m->m_flags & M_EXT) {
628 n->m_data = m->m_data;
629 n->m_ext = m->m_ext;
630 MCLADDREFERENCE(m, n);
631 } else {
632 memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
633 }
634
635 m = m->m_next;
636 }
637 return top;
638 nospace:
639 m_freem(top);
640 MCFail++;
641 return NULL;
642 }
643
644 /*
645 * Copy data from an mbuf chain starting "off" bytes from the beginning,
646 * continuing for "len" bytes, into the indicated buffer.
647 */
648 void
649 m_copydata(struct mbuf *m, int off, int len, caddr_t cp)
650 {
651 unsigned count;
652
653 if (off < 0 || len < 0)
654 panic("m_copydata");
655 while (off > 0) {
656 if (m == 0)
657 panic("m_copydata");
658 if (off < m->m_len)
659 break;
660 off -= m->m_len;
661 m = m->m_next;
662 }
663 while (len > 0) {
664 if (m == 0)
665 panic("m_copydata");
666 count = min(m->m_len - off, len);
667 memcpy(cp, mtod(m, caddr_t) + off, count);
668 len -= count;
669 cp += count;
670 off = 0;
671 m = m->m_next;
672 }
673 }
674
675 /*
676 * Concatenate mbuf chain n to m.
677 * n might be copied into m (when n->m_len is small), therefore data portion of
678 * n could be copied into an mbuf of different mbuf type.
679 * Therefore both chains should be of the same type (e.g. MT_DATA).
680 * Any m_pkthdr is not updated.
681 */
682 void
683 m_cat(struct mbuf *m, struct mbuf *n)
684 {
685
686 while (m->m_next)
687 m = m->m_next;
688 while (n) {
689 if (M_READONLY(m) || n->m_len > M_TRAILINGSPACE(m)) {
690 /* just join the two chains */
691 m->m_next = n;
692 return;
693 }
694 KASSERT(n->m_len == 0 || m->m_type == n->m_type);
695 /* splat the data from one into the other */
696 memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
697 (u_int)n->m_len);
698 m->m_len += n->m_len;
699 n = m_free(n);
700 }
701 }
702
703 void
704 m_adj(struct mbuf *mp, int req_len)
705 {
706 int len = req_len;
707 struct mbuf *m;
708 int count;
709
710 if ((m = mp) == NULL)
711 return;
712 if (len >= 0) {
713 /*
714 * Trim from head.
715 */
716 while (m != NULL && len > 0) {
717 if (m->m_len <= len) {
718 len -= m->m_len;
719 m->m_len = 0;
720 m = m->m_next;
721 } else {
722 m->m_len -= len;
723 m->m_data += len;
724 len = 0;
725 }
726 }
727 m = mp;
728 if (mp->m_flags & M_PKTHDR)
729 m->m_pkthdr.len -= (req_len - len);
730 } else {
731 /*
732 * Trim from tail. Scan the mbuf chain,
733 * calculating its length and finding the last mbuf.
734 * If the adjustment only affects this mbuf, then just
735 * adjust and return. Otherwise, rescan and truncate
736 * after the remaining size.
737 */
738 len = -len;
739 count = 0;
740 for (;;) {
741 count += m->m_len;
742 if (m->m_next == (struct mbuf *)0)
743 break;
744 m = m->m_next;
745 }
746 if (m->m_len >= len) {
747 m->m_len -= len;
748 if (mp->m_flags & M_PKTHDR)
749 mp->m_pkthdr.len -= len;
750 return;
751 }
752 count -= len;
753 if (count < 0)
754 count = 0;
755 /*
756 * Correct length for chain is "count".
757 * Find the mbuf with last data, adjust its length,
758 * and toss data from remaining mbufs on chain.
759 */
760 m = mp;
761 if (m->m_flags & M_PKTHDR)
762 m->m_pkthdr.len = count;
763 for (; m; m = m->m_next) {
764 if (m->m_len >= count) {
765 m->m_len = count;
766 break;
767 }
768 count -= m->m_len;
769 }
770 while (m->m_next)
771 (m = m->m_next) ->m_len = 0;
772 }
773 }
774
775 /*
776 * Rearange an mbuf chain so that len bytes are contiguous
777 * and in the data area of an mbuf (so that mtod and dtom
778 * will work for a structure of size len). Returns the resulting
779 * mbuf chain on success, frees it and returns null on failure.
780 * If there is room, it will add up to max_protohdr-len extra bytes to the
781 * contiguous region in an attempt to avoid being called next time.
782 */
783 int MPFail;
784
785 struct mbuf *
786 m_pullup(struct mbuf *n, int len)
787 {
788 struct mbuf *m;
789 int count;
790 int space;
791
792 /*
793 * If first mbuf has no cluster, and has room for len bytes
794 * without shifting current data, pullup into it,
795 * otherwise allocate a new mbuf to prepend to the chain.
796 */
797 if ((n->m_flags & M_EXT) == 0 &&
798 n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
799 if (n->m_len >= len)
800 return (n);
801 m = n;
802 n = n->m_next;
803 len -= m->m_len;
804 } else {
805 if (len > MHLEN)
806 goto bad;
807 MGET(m, M_DONTWAIT, n->m_type);
808 if (m == 0)
809 goto bad;
810 MCLAIM(m, n->m_owner);
811 m->m_len = 0;
812 if (n->m_flags & M_PKTHDR) {
813 M_COPY_PKTHDR(m, n);
814 m_tag_delete_chain(n, NULL);
815 n->m_flags &= ~M_PKTHDR;
816 }
817 }
818 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
819 do {
820 count = min(min(max(len, max_protohdr), space), n->m_len);
821 memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
822 (unsigned)count);
823 len -= count;
824 m->m_len += count;
825 n->m_len -= count;
826 space -= count;
827 if (n->m_len)
828 n->m_data += count;
829 else
830 n = m_free(n);
831 } while (len > 0 && n);
832 if (len > 0) {
833 (void) m_free(m);
834 goto bad;
835 }
836 m->m_next = n;
837 return (m);
838 bad:
839 m_freem(n);
840 MPFail++;
841 return (NULL);
842 }
843
844 /*
845 * Like m_pullup(), except a new mbuf is always allocated, and we allow
846 * the amount of empty space before the data in the new mbuf to be specified
847 * (in the event that the caller expects to prepend later).
848 */
849 int MSFail;
850
851 struct mbuf *
852 m_copyup(struct mbuf *n, int len, int dstoff)
853 {
854 struct mbuf *m;
855 int count, space;
856
857 if (len > (MHLEN - dstoff))
858 goto bad;
859 MGET(m, M_DONTWAIT, n->m_type);
860 if (m == NULL)
861 goto bad;
862 MCLAIM(m, n->m_owner);
863 m->m_len = 0;
864 if (n->m_flags & M_PKTHDR) {
865 M_COPY_PKTHDR(m, n);
866 m_tag_delete_chain(m, NULL);
867 n->m_flags &= ~M_PKTHDR;
868 }
869 m->m_data += dstoff;
870 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
871 do {
872 count = min(min(max(len, max_protohdr), space), n->m_len);
873 memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
874 (unsigned)count);
875 len -= count;
876 m->m_len += count;
877 n->m_len -= count;
878 space -= count;
879 if (n->m_len)
880 n->m_data += count;
881 else
882 n = m_free(n);
883 } while (len > 0 && n);
884 if (len > 0) {
885 (void) m_free(m);
886 goto bad;
887 }
888 m->m_next = n;
889 return (m);
890 bad:
891 m_freem(n);
892 MSFail++;
893 return (NULL);
894 }
895
896 /*
897 * Partition an mbuf chain in two pieces, returning the tail --
898 * all but the first len0 bytes. In case of failure, it returns NULL and
899 * attempts to restore the chain to its original state.
900 */
901 struct mbuf *
902 m_split(struct mbuf *m0, int len0, int wait)
903 {
904 struct mbuf *m, *n;
905 unsigned len = len0, remain, len_save;
906
907 for (m = m0; m && len > m->m_len; m = m->m_next)
908 len -= m->m_len;
909 if (m == 0)
910 return (NULL);
911 remain = m->m_len - len;
912 if (m0->m_flags & M_PKTHDR) {
913 MGETHDR(n, wait, m0->m_type);
914 if (n == 0)
915 return (NULL);
916 MCLAIM(m, m0->m_owner);
917 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
918 n->m_pkthdr.len = m0->m_pkthdr.len - len0;
919 len_save = m0->m_pkthdr.len;
920 m0->m_pkthdr.len = len0;
921 if (m->m_flags & M_EXT)
922 goto extpacket;
923 if (remain > MHLEN) {
924 /* m can't be the lead packet */
925 MH_ALIGN(n, 0);
926 n->m_next = m_split(m, len, wait);
927 if (n->m_next == 0) {
928 (void) m_free(n);
929 m0->m_pkthdr.len = len_save;
930 return (NULL);
931 } else
932 return (n);
933 } else
934 MH_ALIGN(n, remain);
935 } else if (remain == 0) {
936 n = m->m_next;
937 m->m_next = 0;
938 return (n);
939 } else {
940 MGET(n, wait, m->m_type);
941 if (n == 0)
942 return (NULL);
943 MCLAIM(n, m->m_owner);
944 M_ALIGN(n, remain);
945 }
946 extpacket:
947 if (m->m_flags & M_EXT) {
948 n->m_ext = m->m_ext;
949 MCLADDREFERENCE(m, n);
950 n->m_data = m->m_data + len;
951 } else {
952 memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + len, remain);
953 }
954 n->m_len = remain;
955 m->m_len = len;
956 n->m_next = m->m_next;
957 m->m_next = 0;
958 return (n);
959 }
960 /*
961 * Routine to copy from device local memory into mbufs.
962 */
963 struct mbuf *
964 m_devget(char *buf, int totlen, int off0, struct ifnet *ifp,
965 void (*copy)(const void *from, void *to, size_t len))
966 {
967 struct mbuf *m;
968 struct mbuf *top = 0, **mp = ⊤
969 int off = off0, len;
970 char *cp;
971 char *epkt;
972
973 cp = buf;
974 epkt = cp + totlen;
975 if (off) {
976 /*
977 * If 'off' is non-zero, packet is trailer-encapsulated,
978 * so we have to skip the type and length fields.
979 */
980 cp += off + 2 * sizeof(u_int16_t);
981 totlen -= 2 * sizeof(u_int16_t);
982 }
983 MGETHDR(m, M_DONTWAIT, MT_DATA);
984 if (m == 0)
985 return (NULL);
986 m->m_pkthdr.rcvif = ifp;
987 m->m_pkthdr.len = totlen;
988 m->m_len = MHLEN;
989
990 while (totlen > 0) {
991 if (top) {
992 MGET(m, M_DONTWAIT, MT_DATA);
993 if (m == 0) {
994 m_freem(top);
995 return (NULL);
996 }
997 m->m_len = MLEN;
998 }
999 len = min(totlen, epkt - cp);
1000 if (len >= MINCLSIZE) {
1001 MCLGET(m, M_DONTWAIT);
1002 if ((m->m_flags & M_EXT) == 0) {
1003 m_free(m);
1004 m_freem(top);
1005 return (NULL);
1006 }
1007 m->m_len = len = min(len, MCLBYTES);
1008 } else {
1009 /*
1010 * Place initial small packet/header at end of mbuf.
1011 */
1012 if (len < m->m_len) {
1013 if (top == 0 && len + max_linkhdr <= m->m_len)
1014 m->m_data += max_linkhdr;
1015 m->m_len = len;
1016 } else
1017 len = m->m_len;
1018 }
1019 if (copy)
1020 copy(cp, mtod(m, caddr_t), (size_t)len);
1021 else
1022 memcpy(mtod(m, caddr_t), cp, (size_t)len);
1023 cp += len;
1024 *mp = m;
1025 mp = &m->m_next;
1026 totlen -= len;
1027 if (cp == epkt)
1028 cp = buf;
1029 }
1030 return (top);
1031 }
1032
1033 /*
1034 * Copy data from a buffer back into the indicated mbuf chain,
1035 * starting "off" bytes from the beginning, extending the mbuf
1036 * chain if necessary.
1037 */
1038 void
1039 m_copyback(struct mbuf *m0, int off, int len, caddr_t cp)
1040 {
1041 int mlen;
1042 struct mbuf *m = m0, *n;
1043 int totlen = 0;
1044
1045 if (m0 == 0)
1046 return;
1047 while (off > (mlen = m->m_len)) {
1048 off -= mlen;
1049 totlen += mlen;
1050 if (m->m_next == 0) {
1051 n = m_getclr(M_DONTWAIT, m->m_type);
1052 if (n == 0)
1053 goto out;
1054 n->m_len = min(MLEN, len + off);
1055 m->m_next = n;
1056 }
1057 m = m->m_next;
1058 }
1059 while (len > 0) {
1060 mlen = min (m->m_len - off, len);
1061 memcpy(mtod(m, caddr_t) + off, cp, (unsigned)mlen);
1062 cp += mlen;
1063 len -= mlen;
1064 mlen += off;
1065 off = 0;
1066 totlen += mlen;
1067 if (len == 0)
1068 break;
1069 if (m->m_next == 0) {
1070 n = m_get(M_DONTWAIT, m->m_type);
1071 if (n == 0)
1072 break;
1073 n->m_len = min(MLEN, len);
1074 m->m_next = n;
1075 }
1076 m = m->m_next;
1077 }
1078 out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
1079 m->m_pkthdr.len = totlen;
1080 }
1081
1082 /*
1083 * Apply function f to the data in an mbuf chain starting "off" bytes from the
1084 * beginning, continuing for "len" bytes.
1085 */
1086 int
1087 m_apply(struct mbuf *m, int off, int len,
1088 int (*f)(void *, caddr_t, unsigned int), void *arg)
1089 {
1090 unsigned int count;
1091 int rval;
1092
1093 KASSERT(len >= 0);
1094 KASSERT(off >= 0);
1095
1096 while (off > 0) {
1097 KASSERT(m != NULL);
1098 if (off < m->m_len)
1099 break;
1100 off -= m->m_len;
1101 m = m->m_next;
1102 }
1103 while (len > 0) {
1104 KASSERT(m != NULL);
1105 count = min(m->m_len - off, len);
1106
1107 rval = (*f)(arg, mtod(m, caddr_t) + off, count);
1108 if (rval)
1109 return (rval);
1110
1111 len -= count;
1112 off = 0;
1113 m = m->m_next;
1114 }
1115
1116 return (0);
1117 }
1118
1119 /*
1120 * Return a pointer to mbuf/offset of location in mbuf chain.
1121 */
1122 struct mbuf *
1123 m_getptr(struct mbuf *m, int loc, int *off)
1124 {
1125
1126 while (loc >= 0) {
1127 /* Normal end of search */
1128 if (m->m_len > loc) {
1129 *off = loc;
1130 return (m);
1131 } else {
1132 loc -= m->m_len;
1133
1134 if (m->m_next == NULL) {
1135 if (loc == 0) {
1136 /* Point at the end of valid data */
1137 *off = m->m_len;
1138 return (m);
1139 } else
1140 return (NULL);
1141 } else
1142 m = m->m_next;
1143 }
1144 }
1145
1146 return (NULL);
1147 }
1148