uipc_mbuf.c revision 1.75 1 /* $NetBSD: uipc_mbuf.c,v 1.75 2003/12/04 19:38:24 atatat 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.75 2003/12/04 19:38:24 atatat 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(&mclpool, mblowat);
231 break;
232 case MBUF_MCLLOWAT:
233 mblowat = newval;
234 pool_setlowat(&mclpool, mblowat);
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->m_flags & M_EXT ||
683 m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
684 /* just join the two chains */
685 m->m_next = n;
686 return;
687 }
688 /* splat the data from one into the other */
689 memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
690 (u_int)n->m_len);
691 m->m_len += n->m_len;
692 n = m_free(n);
693 }
694 }
695
696 void
697 m_adj(struct mbuf *mp, int req_len)
698 {
699 int len = req_len;
700 struct mbuf *m;
701 int count;
702
703 if ((m = mp) == NULL)
704 return;
705 if (len >= 0) {
706 /*
707 * Trim from head.
708 */
709 while (m != NULL && len > 0) {
710 if (m->m_len <= len) {
711 len -= m->m_len;
712 m->m_len = 0;
713 m = m->m_next;
714 } else {
715 m->m_len -= len;
716 m->m_data += len;
717 len = 0;
718 }
719 }
720 m = mp;
721 if (mp->m_flags & M_PKTHDR)
722 m->m_pkthdr.len -= (req_len - len);
723 } else {
724 /*
725 * Trim from tail. Scan the mbuf chain,
726 * calculating its length and finding the last mbuf.
727 * If the adjustment only affects this mbuf, then just
728 * adjust and return. Otherwise, rescan and truncate
729 * after the remaining size.
730 */
731 len = -len;
732 count = 0;
733 for (;;) {
734 count += m->m_len;
735 if (m->m_next == (struct mbuf *)0)
736 break;
737 m = m->m_next;
738 }
739 if (m->m_len >= len) {
740 m->m_len -= len;
741 if (mp->m_flags & M_PKTHDR)
742 mp->m_pkthdr.len -= len;
743 return;
744 }
745 count -= len;
746 if (count < 0)
747 count = 0;
748 /*
749 * Correct length for chain is "count".
750 * Find the mbuf with last data, adjust its length,
751 * and toss data from remaining mbufs on chain.
752 */
753 m = mp;
754 if (m->m_flags & M_PKTHDR)
755 m->m_pkthdr.len = count;
756 for (; m; m = m->m_next) {
757 if (m->m_len >= count) {
758 m->m_len = count;
759 break;
760 }
761 count -= m->m_len;
762 }
763 while (m->m_next)
764 (m = m->m_next) ->m_len = 0;
765 }
766 }
767
768 /*
769 * Rearange an mbuf chain so that len bytes are contiguous
770 * and in the data area of an mbuf (so that mtod and dtom
771 * will work for a structure of size len). Returns the resulting
772 * mbuf chain on success, frees it and returns null on failure.
773 * If there is room, it will add up to max_protohdr-len extra bytes to the
774 * contiguous region in an attempt to avoid being called next time.
775 */
776 int MPFail;
777
778 struct mbuf *
779 m_pullup(struct mbuf *n, int len)
780 {
781 struct mbuf *m;
782 int count;
783 int space;
784
785 /*
786 * If first mbuf has no cluster, and has room for len bytes
787 * without shifting current data, pullup into it,
788 * otherwise allocate a new mbuf to prepend to the chain.
789 */
790 if ((n->m_flags & M_EXT) == 0 &&
791 n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
792 if (n->m_len >= len)
793 return (n);
794 m = n;
795 n = n->m_next;
796 len -= m->m_len;
797 } else {
798 if (len > MHLEN)
799 goto bad;
800 MGET(m, M_DONTWAIT, n->m_type);
801 if (m == 0)
802 goto bad;
803 MCLAIM(m, n->m_owner);
804 m->m_len = 0;
805 if (n->m_flags & M_PKTHDR) {
806 M_COPY_PKTHDR(m, n);
807 m_tag_delete_chain(n, NULL);
808 n->m_flags &= ~M_PKTHDR;
809 }
810 }
811 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
812 do {
813 count = min(min(max(len, max_protohdr), space), n->m_len);
814 memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
815 (unsigned)count);
816 len -= count;
817 m->m_len += count;
818 n->m_len -= count;
819 space -= count;
820 if (n->m_len)
821 n->m_data += count;
822 else
823 n = m_free(n);
824 } while (len > 0 && n);
825 if (len > 0) {
826 (void) m_free(m);
827 goto bad;
828 }
829 m->m_next = n;
830 return (m);
831 bad:
832 m_freem(n);
833 MPFail++;
834 return (NULL);
835 }
836
837 /*
838 * Like m_pullup(), except a new mbuf is always allocated, and we allow
839 * the amount of empty space before the data in the new mbuf to be specified
840 * (in the event that the caller expects to prepend later).
841 */
842 int MSFail;
843
844 struct mbuf *
845 m_copyup(struct mbuf *n, int len, int dstoff)
846 {
847 struct mbuf *m;
848 int count, space;
849
850 if (len > (MHLEN - dstoff))
851 goto bad;
852 MGET(m, M_DONTWAIT, n->m_type);
853 if (m == NULL)
854 goto bad;
855 MCLAIM(m, n->m_owner);
856 m->m_len = 0;
857 if (n->m_flags & M_PKTHDR) {
858 M_COPY_PKTHDR(m, n);
859 m_tag_delete_chain(m, NULL);
860 n->m_flags &= ~M_PKTHDR;
861 }
862 m->m_data += dstoff;
863 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
864 do {
865 count = min(min(max(len, max_protohdr), space), n->m_len);
866 memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
867 (unsigned)count);
868 len -= count;
869 m->m_len += count;
870 n->m_len -= count;
871 space -= count;
872 if (n->m_len)
873 n->m_data += count;
874 else
875 n = m_free(n);
876 } while (len > 0 && n);
877 if (len > 0) {
878 (void) m_free(m);
879 goto bad;
880 }
881 m->m_next = n;
882 return (m);
883 bad:
884 m_freem(n);
885 MSFail++;
886 return (NULL);
887 }
888
889 /*
890 * Partition an mbuf chain in two pieces, returning the tail --
891 * all but the first len0 bytes. In case of failure, it returns NULL and
892 * attempts to restore the chain to its original state.
893 */
894 struct mbuf *
895 m_split(struct mbuf *m0, int len0, int wait)
896 {
897 struct mbuf *m, *n;
898 unsigned len = len0, remain, len_save;
899
900 for (m = m0; m && len > m->m_len; m = m->m_next)
901 len -= m->m_len;
902 if (m == 0)
903 return (NULL);
904 remain = m->m_len - len;
905 if (m0->m_flags & M_PKTHDR) {
906 MGETHDR(n, wait, m0->m_type);
907 if (n == 0)
908 return (NULL);
909 MCLAIM(m, m0->m_owner);
910 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
911 n->m_pkthdr.len = m0->m_pkthdr.len - len0;
912 len_save = m0->m_pkthdr.len;
913 m0->m_pkthdr.len = len0;
914 if (m->m_flags & M_EXT)
915 goto extpacket;
916 if (remain > MHLEN) {
917 /* m can't be the lead packet */
918 MH_ALIGN(n, 0);
919 n->m_next = m_split(m, len, wait);
920 if (n->m_next == 0) {
921 (void) m_free(n);
922 m0->m_pkthdr.len = len_save;
923 return (NULL);
924 } else
925 return (n);
926 } else
927 MH_ALIGN(n, remain);
928 } else if (remain == 0) {
929 n = m->m_next;
930 m->m_next = 0;
931 return (n);
932 } else {
933 MGET(n, wait, m->m_type);
934 if (n == 0)
935 return (NULL);
936 MCLAIM(n, m->m_owner);
937 M_ALIGN(n, remain);
938 }
939 extpacket:
940 if (m->m_flags & M_EXT) {
941 n->m_ext = m->m_ext;
942 MCLADDREFERENCE(m, n);
943 n->m_data = m->m_data + len;
944 } else {
945 memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + len, remain);
946 }
947 n->m_len = remain;
948 m->m_len = len;
949 n->m_next = m->m_next;
950 m->m_next = 0;
951 return (n);
952 }
953 /*
954 * Routine to copy from device local memory into mbufs.
955 */
956 struct mbuf *
957 m_devget(char *buf, int totlen, int off0, struct ifnet *ifp,
958 void (*copy)(const void *from, void *to, size_t len))
959 {
960 struct mbuf *m;
961 struct mbuf *top = 0, **mp = ⊤
962 int off = off0, len;
963 char *cp;
964 char *epkt;
965
966 cp = buf;
967 epkt = cp + totlen;
968 if (off) {
969 /*
970 * If 'off' is non-zero, packet is trailer-encapsulated,
971 * so we have to skip the type and length fields.
972 */
973 cp += off + 2 * sizeof(u_int16_t);
974 totlen -= 2 * sizeof(u_int16_t);
975 }
976 MGETHDR(m, M_DONTWAIT, MT_DATA);
977 if (m == 0)
978 return (NULL);
979 m->m_pkthdr.rcvif = ifp;
980 m->m_pkthdr.len = totlen;
981 m->m_len = MHLEN;
982
983 while (totlen > 0) {
984 if (top) {
985 MGET(m, M_DONTWAIT, MT_DATA);
986 if (m == 0) {
987 m_freem(top);
988 return (NULL);
989 }
990 m->m_len = MLEN;
991 }
992 len = min(totlen, epkt - cp);
993 if (len >= MINCLSIZE) {
994 MCLGET(m, M_DONTWAIT);
995 if ((m->m_flags & M_EXT) == 0) {
996 m_free(m);
997 m_freem(top);
998 return (NULL);
999 }
1000 m->m_len = len = min(len, MCLBYTES);
1001 } else {
1002 /*
1003 * Place initial small packet/header at end of mbuf.
1004 */
1005 if (len < m->m_len) {
1006 if (top == 0 && len + max_linkhdr <= m->m_len)
1007 m->m_data += max_linkhdr;
1008 m->m_len = len;
1009 } else
1010 len = m->m_len;
1011 }
1012 if (copy)
1013 copy(cp, mtod(m, caddr_t), (size_t)len);
1014 else
1015 memcpy(mtod(m, caddr_t), cp, (size_t)len);
1016 cp += len;
1017 *mp = m;
1018 mp = &m->m_next;
1019 totlen -= len;
1020 if (cp == epkt)
1021 cp = buf;
1022 }
1023 return (top);
1024 }
1025
1026 /*
1027 * Copy data from a buffer back into the indicated mbuf chain,
1028 * starting "off" bytes from the beginning, extending the mbuf
1029 * chain if necessary.
1030 */
1031 void
1032 m_copyback(struct mbuf *m0, int off, int len, caddr_t cp)
1033 {
1034 int mlen;
1035 struct mbuf *m = m0, *n;
1036 int totlen = 0;
1037
1038 if (m0 == 0)
1039 return;
1040 while (off > (mlen = m->m_len)) {
1041 off -= mlen;
1042 totlen += mlen;
1043 if (m->m_next == 0) {
1044 n = m_getclr(M_DONTWAIT, m->m_type);
1045 if (n == 0)
1046 goto out;
1047 n->m_len = min(MLEN, len + off);
1048 m->m_next = n;
1049 }
1050 m = m->m_next;
1051 }
1052 while (len > 0) {
1053 mlen = min (m->m_len - off, len);
1054 memcpy(mtod(m, caddr_t) + off, cp, (unsigned)mlen);
1055 cp += mlen;
1056 len -= mlen;
1057 mlen += off;
1058 off = 0;
1059 totlen += mlen;
1060 if (len == 0)
1061 break;
1062 if (m->m_next == 0) {
1063 n = m_get(M_DONTWAIT, m->m_type);
1064 if (n == 0)
1065 break;
1066 n->m_len = min(MLEN, len);
1067 m->m_next = n;
1068 }
1069 m = m->m_next;
1070 }
1071 out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
1072 m->m_pkthdr.len = totlen;
1073 }
1074
1075 /*
1076 * Apply function f to the data in an mbuf chain starting "off" bytes from the
1077 * beginning, continuing for "len" bytes.
1078 */
1079 int
1080 m_apply(struct mbuf *m, int off, int len,
1081 int (*f)(void *, caddr_t, unsigned int), void *arg)
1082 {
1083 unsigned int count;
1084 int rval;
1085
1086 KASSERT(len >= 0);
1087 KASSERT(off >= 0);
1088
1089 while (off > 0) {
1090 KASSERT(m != NULL);
1091 if (off < m->m_len)
1092 break;
1093 off -= m->m_len;
1094 m = m->m_next;
1095 }
1096 while (len > 0) {
1097 KASSERT(m != NULL);
1098 count = min(m->m_len - off, len);
1099
1100 rval = (*f)(arg, mtod(m, caddr_t) + off, count);
1101 if (rval)
1102 return (rval);
1103
1104 len -= count;
1105 off = 0;
1106 m = m->m_next;
1107 }
1108
1109 return (0);
1110 }
1111
1112 /*
1113 * Return a pointer to mbuf/offset of location in mbuf chain.
1114 */
1115 struct mbuf *
1116 m_getptr(struct mbuf *m, int loc, int *off)
1117 {
1118
1119 while (loc >= 0) {
1120 /* Normal end of search */
1121 if (m->m_len > loc) {
1122 *off = loc;
1123 return (m);
1124 } else {
1125 loc -= m->m_len;
1126
1127 if (m->m_next == NULL) {
1128 if (loc == 0) {
1129 /* Point at the end of valid data */
1130 *off = m->m_len;
1131 return (m);
1132 } else
1133 return (NULL);
1134 } else
1135 m = m->m_next;
1136 }
1137 }
1138
1139 return (NULL);
1140 }
1141