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