uipc_mbuf.c revision 1.100.2.6 1 /* $NetBSD: uipc_mbuf.c,v 1.100.2.6 2006/06/21 15:09:39 yamt 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.100.2.6 2006/06/21 15:09:39 yamt Exp $");
73
74 #include "opt_mbuftrace.h"
75 #include "opt_ddb.h"
76
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/proc.h>
80 #include <sys/malloc.h>
81 #define MBTYPES
82 #include <sys/mbuf.h>
83 #include <sys/kernel.h>
84 #include <sys/syslog.h>
85 #include <sys/domain.h>
86 #include <sys/protosw.h>
87 #include <sys/pool.h>
88 #include <sys/socket.h>
89 #include <sys/sysctl.h>
90
91 #include <net/if.h>
92
93 #include <uvm/uvm.h>
94
95
96 struct pool mbpool; /* mbuf pool */
97 struct pool mclpool; /* mbuf cluster pool */
98
99 struct pool_cache mbpool_cache;
100 struct pool_cache mclpool_cache;
101
102 struct mbstat mbstat;
103 int max_linkhdr;
104 int max_protohdr;
105 int max_hdr;
106 int max_datalen;
107
108 static int mb_ctor(void *, void *, int);
109
110 static void *mclpool_alloc(struct pool *, int);
111 static void mclpool_release(struct pool *, void *);
112
113 static struct pool_allocator mclpool_allocator = {
114 mclpool_alloc, mclpool_release, 0,
115 };
116
117 static struct mbuf *m_copym0(struct mbuf *, int, int, int, int);
118 static struct mbuf *m_split0(struct mbuf *, int, int, int);
119 static int m_copyback0(struct mbuf **, int, int, const void *, int, int);
120
121 /* flags for m_copyback0 */
122 #define M_COPYBACK0_COPYBACK 0x0001 /* copyback from cp */
123 #define M_COPYBACK0_PRESERVE 0x0002 /* preserve original data */
124 #define M_COPYBACK0_COW 0x0004 /* do copy-on-write */
125 #define M_COPYBACK0_EXTEND 0x0008 /* extend chain */
126
127 static const char mclpool_warnmsg[] =
128 "WARNING: mclpool limit reached; increase NMBCLUSTERS";
129
130 MALLOC_DEFINE(M_MBUF, "mbuf", "mbuf");
131
132 #ifdef MBUFTRACE
133 struct mownerhead mowners = LIST_HEAD_INITIALIZER(mowners);
134 struct mowner unknown_mowners[] = {
135 { "unknown", "free" },
136 { "unknown", "data" },
137 { "unknown", "header" },
138 { "unknown", "soname" },
139 { "unknown", "soopts" },
140 { "unknown", "ftable" },
141 { "unknown", "control" },
142 { "unknown", "oobdata" },
143 };
144 struct mowner revoked_mowner = { "revoked", "" };
145 #endif
146
147 /*
148 * Initialize the mbuf allocator.
149 */
150 void
151 mbinit(void)
152 {
153
154 KASSERT(sizeof(struct _m_ext) <= MHLEN);
155 KASSERT(sizeof(struct mbuf) == MSIZE);
156
157 mclpool_allocator.pa_backingmap = mb_map;
158 pool_init(&mbpool, msize, 0, 0, 0, "mbpl", NULL);
159 pool_init(&mclpool, mclbytes, 0, 0, 0, "mclpl", &mclpool_allocator);
160
161 pool_set_drain_hook(&mbpool, m_reclaim, NULL);
162 pool_set_drain_hook(&mclpool, m_reclaim, NULL);
163
164 pool_cache_init(&mbpool_cache, &mbpool, mb_ctor, NULL, NULL);
165 pool_cache_init(&mclpool_cache, &mclpool, NULL, NULL, NULL);
166
167 /*
168 * Set the hard limit on the mclpool to the number of
169 * mbuf clusters the kernel is to support. Log the limit
170 * reached message max once a minute.
171 */
172 pool_sethardlimit(&mclpool, nmbclusters, mclpool_warnmsg, 60);
173
174 /*
175 * Set a low water mark for both mbufs and clusters. This should
176 * help ensure that they can be allocated in a memory starvation
177 * situation. This is important for e.g. diskless systems which
178 * must allocate mbufs in order for the pagedaemon to clean pages.
179 */
180 pool_setlowat(&mbpool, mblowat);
181 pool_setlowat(&mclpool, mcllowat);
182
183 #ifdef MBUFTRACE
184 {
185 /*
186 * Attach the unknown mowners.
187 */
188 int i;
189 MOWNER_ATTACH(&revoked_mowner);
190 for (i = sizeof(unknown_mowners)/sizeof(unknown_mowners[0]);
191 i-- > 0; )
192 MOWNER_ATTACH(&unknown_mowners[i]);
193 }
194 #endif
195 }
196
197 /*
198 * sysctl helper routine for the kern.mbuf subtree. nmbclusters may
199 * or may not be writable, and mblowat and mcllowat need range
200 * checking and pool tweaking after being reset.
201 */
202 static int
203 sysctl_kern_mbuf(SYSCTLFN_ARGS)
204 {
205 int error, newval;
206 struct sysctlnode node;
207
208 node = *rnode;
209 node.sysctl_data = &newval;
210 switch (rnode->sysctl_num) {
211 case MBUF_NMBCLUSTERS:
212 if (mb_map != NULL) {
213 node.sysctl_flags &= ~CTLFLAG_READWRITE;
214 node.sysctl_flags |= CTLFLAG_READONLY;
215 }
216 /* FALLTHROUGH */
217 case MBUF_MBLOWAT:
218 case MBUF_MCLLOWAT:
219 newval = *(int*)rnode->sysctl_data;
220 break;
221 default:
222 return (EOPNOTSUPP);
223 }
224
225 error = sysctl_lookup(SYSCTLFN_CALL(&node));
226 if (error || newp == NULL)
227 return (error);
228 if (newval < 0)
229 return (EINVAL);
230
231 switch (node.sysctl_num) {
232 case MBUF_NMBCLUSTERS:
233 if (newval < nmbclusters)
234 return (EINVAL);
235 nmbclusters = newval;
236 pool_sethardlimit(&mclpool, nmbclusters, mclpool_warnmsg, 60);
237 break;
238 case MBUF_MBLOWAT:
239 mblowat = newval;
240 pool_setlowat(&mbpool, mblowat);
241 break;
242 case MBUF_MCLLOWAT:
243 mcllowat = newval;
244 pool_setlowat(&mclpool, mcllowat);
245 break;
246 }
247
248 return (0);
249 }
250
251 #ifdef MBUFTRACE
252 static int
253 sysctl_kern_mbuf_mowners(SYSCTLFN_ARGS)
254 {
255 struct mowner *mo;
256 size_t len = 0;
257 int error = 0;
258
259 if (namelen != 0)
260 return (EINVAL);
261 if (newp != NULL)
262 return (EPERM);
263
264 LIST_FOREACH(mo, &mowners, mo_link) {
265 if (oldp != NULL) {
266 if (*oldlenp - len < sizeof(*mo)) {
267 error = ENOMEM;
268 break;
269 }
270 error = copyout(mo, (caddr_t) oldp + len,
271 sizeof(*mo));
272 if (error)
273 break;
274 }
275 len += sizeof(*mo);
276 }
277
278 if (error == 0)
279 *oldlenp = len;
280
281 return (error);
282 }
283 #endif /* MBUFTRACE */
284
285 SYSCTL_SETUP(sysctl_kern_mbuf_setup, "sysctl kern.mbuf subtree setup")
286 {
287
288 sysctl_createv(clog, 0, NULL, NULL,
289 CTLFLAG_PERMANENT,
290 CTLTYPE_NODE, "kern", NULL,
291 NULL, 0, NULL, 0,
292 CTL_KERN, CTL_EOL);
293 sysctl_createv(clog, 0, NULL, NULL,
294 CTLFLAG_PERMANENT,
295 CTLTYPE_NODE, "mbuf",
296 SYSCTL_DESCR("mbuf control variables"),
297 NULL, 0, NULL, 0,
298 CTL_KERN, KERN_MBUF, CTL_EOL);
299
300 sysctl_createv(clog, 0, NULL, NULL,
301 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
302 CTLTYPE_INT, "msize",
303 SYSCTL_DESCR("mbuf base size"),
304 NULL, msize, NULL, 0,
305 CTL_KERN, KERN_MBUF, MBUF_MSIZE, CTL_EOL);
306 sysctl_createv(clog, 0, NULL, NULL,
307 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
308 CTLTYPE_INT, "mclbytes",
309 SYSCTL_DESCR("mbuf cluster size"),
310 NULL, mclbytes, NULL, 0,
311 CTL_KERN, KERN_MBUF, MBUF_MCLBYTES, CTL_EOL);
312 sysctl_createv(clog, 0, NULL, NULL,
313 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
314 CTLTYPE_INT, "nmbclusters",
315 SYSCTL_DESCR("Limit on the number of mbuf clusters"),
316 sysctl_kern_mbuf, 0, &nmbclusters, 0,
317 CTL_KERN, KERN_MBUF, MBUF_NMBCLUSTERS, CTL_EOL);
318 sysctl_createv(clog, 0, NULL, NULL,
319 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
320 CTLTYPE_INT, "mblowat",
321 SYSCTL_DESCR("mbuf low water mark"),
322 sysctl_kern_mbuf, 0, &mblowat, 0,
323 CTL_KERN, KERN_MBUF, MBUF_MBLOWAT, CTL_EOL);
324 sysctl_createv(clog, 0, NULL, NULL,
325 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
326 CTLTYPE_INT, "mcllowat",
327 SYSCTL_DESCR("mbuf cluster low water mark"),
328 sysctl_kern_mbuf, 0, &mcllowat, 0,
329 CTL_KERN, KERN_MBUF, MBUF_MCLLOWAT, CTL_EOL);
330 sysctl_createv(clog, 0, NULL, NULL,
331 CTLFLAG_PERMANENT,
332 CTLTYPE_STRUCT, "stats",
333 SYSCTL_DESCR("mbuf allocation statistics"),
334 NULL, 0, &mbstat, sizeof(mbstat),
335 CTL_KERN, KERN_MBUF, MBUF_STATS, CTL_EOL);
336 #ifdef MBUFTRACE
337 sysctl_createv(clog, 0, NULL, NULL,
338 CTLFLAG_PERMANENT,
339 CTLTYPE_STRUCT, "mowners",
340 SYSCTL_DESCR("Information about mbuf owners"),
341 sysctl_kern_mbuf_mowners, 0, NULL, 0,
342 CTL_KERN, KERN_MBUF, MBUF_MOWNERS, CTL_EOL);
343 #endif /* MBUFTRACE */
344 }
345
346 static void *
347 mclpool_alloc(struct pool *pp, int flags)
348 {
349 boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
350
351 return ((void *)uvm_km_alloc_poolpage(mb_map, waitok));
352 }
353
354 static void
355 mclpool_release(struct pool *pp, void *v)
356 {
357
358 uvm_km_free_poolpage(mb_map, (vaddr_t)v);
359 }
360
361 /*ARGSUSED*/
362 static int
363 mb_ctor(void *arg, void *object, int flags)
364 {
365 struct mbuf *m = object;
366
367 #ifdef POOL_VTOPHYS
368 m->m_paddr = POOL_VTOPHYS(m);
369 #else
370 m->m_paddr = M_PADDR_INVALID;
371 #endif
372 return (0);
373 }
374
375 void
376 m_reclaim(void *arg, int flags)
377 {
378 struct domain *dp;
379 const struct protosw *pr;
380 struct ifnet *ifp;
381 int s = splvm();
382
383 DOMAIN_FOREACH(dp) {
384 for (pr = dp->dom_protosw;
385 pr < dp->dom_protoswNPROTOSW; pr++)
386 if (pr->pr_drain)
387 (*pr->pr_drain)();
388 }
389 IFNET_FOREACH(ifp) {
390 if (ifp->if_drain)
391 (*ifp->if_drain)(ifp);
392 }
393 splx(s);
394 mbstat.m_drain++;
395 }
396
397 /*
398 * Space allocation routines.
399 * These are also available as macros
400 * for critical paths.
401 */
402 struct mbuf *
403 m_get(int nowait, int type)
404 {
405 struct mbuf *m;
406
407 MGET(m, nowait, type);
408 return (m);
409 }
410
411 struct mbuf *
412 m_gethdr(int nowait, int type)
413 {
414 struct mbuf *m;
415
416 MGETHDR(m, nowait, type);
417 return (m);
418 }
419
420 struct mbuf *
421 m_getclr(int nowait, int type)
422 {
423 struct mbuf *m;
424
425 MGET(m, nowait, type);
426 if (m == 0)
427 return (NULL);
428 memset(mtod(m, caddr_t), 0, MLEN);
429 return (m);
430 }
431
432 void
433 m_clget(struct mbuf *m, int nowait)
434 {
435
436 MCLGET(m, nowait);
437 }
438
439 struct mbuf *
440 m_free(struct mbuf *m)
441 {
442 struct mbuf *n;
443
444 MFREE(m, n);
445 return (n);
446 }
447
448 void
449 m_freem(struct mbuf *m)
450 {
451 struct mbuf *n;
452
453 if (m == NULL)
454 return;
455 do {
456 MFREE(m, n);
457 m = n;
458 } while (m);
459 }
460
461 #ifdef MBUFTRACE
462 /*
463 * Walk a chain of mbufs, claiming ownership of each mbuf in the chain.
464 */
465 void
466 m_claimm(struct mbuf *m, struct mowner *mo)
467 {
468
469 for (; m != NULL; m = m->m_next)
470 MCLAIM(m, mo);
471 }
472 #endif
473
474 /*
475 * Mbuffer utility routines.
476 */
477
478 /*
479 * Lesser-used path for M_PREPEND:
480 * allocate new mbuf to prepend to chain,
481 * copy junk along.
482 */
483 struct mbuf *
484 m_prepend(struct mbuf *m, int len, int how)
485 {
486 struct mbuf *mn;
487
488 MGET(mn, how, m->m_type);
489 if (mn == (struct mbuf *)NULL) {
490 m_freem(m);
491 return ((struct mbuf *)NULL);
492 }
493 if (m->m_flags & M_PKTHDR) {
494 M_MOVE_PKTHDR(mn, m);
495 } else {
496 MCLAIM(mn, m->m_owner);
497 }
498 mn->m_next = m;
499 m = mn;
500 if (len < MHLEN)
501 MH_ALIGN(m, len);
502 m->m_len = len;
503 return (m);
504 }
505
506 /*
507 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
508 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
509 * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
510 */
511 int MCFail;
512
513 struct mbuf *
514 m_copym(struct mbuf *m, int off0, int len, int wait)
515 {
516
517 return m_copym0(m, off0, len, wait, 0); /* shallow copy on M_EXT */
518 }
519
520 struct mbuf *
521 m_dup(struct mbuf *m, int off0, int len, int wait)
522 {
523
524 return m_copym0(m, off0, len, wait, 1); /* deep copy */
525 }
526
527 static struct mbuf *
528 m_copym0(struct mbuf *m, int off0, int len, int wait, int deep)
529 {
530 struct mbuf *n, **np;
531 int off = off0;
532 struct mbuf *top;
533 int copyhdr = 0;
534
535 if (off < 0 || len < 0)
536 panic("m_copym: off %d, len %d", off, len);
537 if (off == 0 && m->m_flags & M_PKTHDR)
538 copyhdr = 1;
539 while (off > 0) {
540 if (m == 0)
541 panic("m_copym: m == 0, off %d", off);
542 if (off < m->m_len)
543 break;
544 off -= m->m_len;
545 m = m->m_next;
546 }
547 np = ⊤
548 top = 0;
549 while (len > 0) {
550 if (m == 0) {
551 if (len != M_COPYALL)
552 panic("m_copym: m == 0, len %d [!COPYALL]",
553 len);
554 break;
555 }
556 MGET(n, wait, m->m_type);
557 *np = n;
558 if (n == 0)
559 goto nospace;
560 MCLAIM(n, m->m_owner);
561 if (copyhdr) {
562 M_COPY_PKTHDR(n, m);
563 if (len == M_COPYALL)
564 n->m_pkthdr.len -= off0;
565 else
566 n->m_pkthdr.len = len;
567 copyhdr = 0;
568 }
569 n->m_len = min(len, m->m_len - off);
570 if (m->m_flags & M_EXT) {
571 if (!deep) {
572 n->m_data = m->m_data + off;
573 MCLADDREFERENCE(m, n);
574 } else {
575 /*
576 * we are unsure about the way m was allocated.
577 * copy into multiple MCLBYTES cluster mbufs.
578 */
579 MCLGET(n, wait);
580 n->m_len = 0;
581 n->m_len = M_TRAILINGSPACE(n);
582 n->m_len = min(n->m_len, len);
583 n->m_len = min(n->m_len, m->m_len - off);
584 memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + off,
585 (unsigned)n->m_len);
586 }
587 } else
588 memcpy(mtod(n, caddr_t), mtod(m, caddr_t)+off,
589 (unsigned)n->m_len);
590 if (len != M_COPYALL)
591 len -= n->m_len;
592 off += n->m_len;
593 #ifdef DIAGNOSTIC
594 if (off > m->m_len)
595 panic("m_copym0 overrun");
596 #endif
597 if (off == m->m_len) {
598 m = m->m_next;
599 off = 0;
600 }
601 np = &n->m_next;
602 }
603 if (top == 0)
604 MCFail++;
605 return (top);
606 nospace:
607 m_freem(top);
608 MCFail++;
609 return (NULL);
610 }
611
612 /*
613 * Copy an entire packet, including header (which must be present).
614 * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
615 */
616 struct mbuf *
617 m_copypacket(struct mbuf *m, int how)
618 {
619 struct mbuf *top, *n, *o;
620
621 MGET(n, how, m->m_type);
622 top = n;
623 if (!n)
624 goto nospace;
625
626 MCLAIM(n, m->m_owner);
627 M_COPY_PKTHDR(n, m);
628 n->m_len = m->m_len;
629 if (m->m_flags & M_EXT) {
630 n->m_data = m->m_data;
631 MCLADDREFERENCE(m, n);
632 } else {
633 memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
634 }
635
636 m = m->m_next;
637 while (m) {
638 MGET(o, how, m->m_type);
639 if (!o)
640 goto nospace;
641
642 MCLAIM(o, m->m_owner);
643 n->m_next = o;
644 n = n->m_next;
645
646 n->m_len = m->m_len;
647 if (m->m_flags & M_EXT) {
648 n->m_data = m->m_data;
649 MCLADDREFERENCE(m, n);
650 } else {
651 memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
652 }
653
654 m = m->m_next;
655 }
656 return top;
657 nospace:
658 m_freem(top);
659 MCFail++;
660 return NULL;
661 }
662
663 /*
664 * Copy data from an mbuf chain starting "off" bytes from the beginning,
665 * continuing for "len" bytes, into the indicated buffer.
666 */
667 void
668 m_copydata(struct mbuf *m, int off, int len, void *vp)
669 {
670 unsigned count;
671 caddr_t cp = vp;
672
673 if (off < 0 || len < 0)
674 panic("m_copydata: off %d, len %d", off, len);
675 while (off > 0) {
676 if (m == NULL)
677 panic("m_copydata: m == NULL, off %d", off);
678 if (off < m->m_len)
679 break;
680 off -= m->m_len;
681 m = m->m_next;
682 }
683 while (len > 0) {
684 if (m == NULL)
685 panic("m_copydata: m == NULL, len %d", len);
686 count = min(m->m_len - off, len);
687 memcpy(cp, mtod(m, caddr_t) + off, count);
688 len -= count;
689 cp += count;
690 off = 0;
691 m = m->m_next;
692 }
693 }
694
695 /*
696 * Concatenate mbuf chain n to m.
697 * n might be copied into m (when n->m_len is small), therefore data portion of
698 * n could be copied into an mbuf of different mbuf type.
699 * Any m_pkthdr is not updated.
700 */
701 void
702 m_cat(struct mbuf *m, struct mbuf *n)
703 {
704
705 while (m->m_next)
706 m = m->m_next;
707 while (n) {
708 if (M_READONLY(m) || n->m_len > M_TRAILINGSPACE(m)) {
709 /* just join the two chains */
710 m->m_next = n;
711 return;
712 }
713 /* splat the data from one into the other */
714 memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
715 (u_int)n->m_len);
716 m->m_len += n->m_len;
717 n = m_free(n);
718 }
719 }
720
721 void
722 m_adj(struct mbuf *mp, int req_len)
723 {
724 int len = req_len;
725 struct mbuf *m;
726 int count;
727
728 if ((m = mp) == NULL)
729 return;
730 if (len >= 0) {
731 /*
732 * Trim from head.
733 */
734 while (m != NULL && len > 0) {
735 if (m->m_len <= len) {
736 len -= m->m_len;
737 m->m_len = 0;
738 m = m->m_next;
739 } else {
740 m->m_len -= len;
741 m->m_data += len;
742 len = 0;
743 }
744 }
745 m = mp;
746 if (mp->m_flags & M_PKTHDR)
747 m->m_pkthdr.len -= (req_len - len);
748 } else {
749 /*
750 * Trim from tail. Scan the mbuf chain,
751 * calculating its length and finding the last mbuf.
752 * If the adjustment only affects this mbuf, then just
753 * adjust and return. Otherwise, rescan and truncate
754 * after the remaining size.
755 */
756 len = -len;
757 count = 0;
758 for (;;) {
759 count += m->m_len;
760 if (m->m_next == (struct mbuf *)0)
761 break;
762 m = m->m_next;
763 }
764 if (m->m_len >= len) {
765 m->m_len -= len;
766 if (mp->m_flags & M_PKTHDR)
767 mp->m_pkthdr.len -= len;
768 return;
769 }
770 count -= len;
771 if (count < 0)
772 count = 0;
773 /*
774 * Correct length for chain is "count".
775 * Find the mbuf with last data, adjust its length,
776 * and toss data from remaining mbufs on chain.
777 */
778 m = mp;
779 if (m->m_flags & M_PKTHDR)
780 m->m_pkthdr.len = count;
781 for (; m; m = m->m_next) {
782 if (m->m_len >= count) {
783 m->m_len = count;
784 break;
785 }
786 count -= m->m_len;
787 }
788 if (m)
789 while (m->m_next)
790 (m = m->m_next)->m_len = 0;
791 }
792 }
793
794 /*
795 * Rearrange an mbuf chain so that len bytes are contiguous
796 * and in the data area of an mbuf (so that mtod and dtom
797 * will work for a structure of size len). Returns the resulting
798 * mbuf chain on success, frees it and returns null on failure.
799 * If there is room, it will add up to max_protohdr-len extra bytes to the
800 * contiguous region in an attempt to avoid being called next time.
801 */
802 int MPFail;
803
804 struct mbuf *
805 m_pullup(struct mbuf *n, int len)
806 {
807 struct mbuf *m;
808 int count;
809 int space;
810
811 /*
812 * If first mbuf has no cluster, and has room for len bytes
813 * without shifting current data, pullup into it,
814 * otherwise allocate a new mbuf to prepend to the chain.
815 */
816 if ((n->m_flags & M_EXT) == 0 &&
817 n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
818 if (n->m_len >= len)
819 return (n);
820 m = n;
821 n = n->m_next;
822 len -= m->m_len;
823 } else {
824 if (len > MHLEN)
825 goto bad;
826 MGET(m, M_DONTWAIT, n->m_type);
827 if (m == 0)
828 goto bad;
829 MCLAIM(m, n->m_owner);
830 m->m_len = 0;
831 if (n->m_flags & M_PKTHDR) {
832 M_MOVE_PKTHDR(m, n);
833 }
834 }
835 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
836 do {
837 count = min(min(max(len, max_protohdr), space), n->m_len);
838 memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
839 (unsigned)count);
840 len -= count;
841 m->m_len += count;
842 n->m_len -= count;
843 space -= count;
844 if (n->m_len)
845 n->m_data += count;
846 else
847 n = m_free(n);
848 } while (len > 0 && n);
849 if (len > 0) {
850 (void) m_free(m);
851 goto bad;
852 }
853 m->m_next = n;
854 return (m);
855 bad:
856 m_freem(n);
857 MPFail++;
858 return (NULL);
859 }
860
861 /*
862 * Like m_pullup(), except a new mbuf is always allocated, and we allow
863 * the amount of empty space before the data in the new mbuf to be specified
864 * (in the event that the caller expects to prepend later).
865 */
866 int MSFail;
867
868 struct mbuf *
869 m_copyup(struct mbuf *n, int len, int dstoff)
870 {
871 struct mbuf *m;
872 int count, space;
873
874 if (len > (MHLEN - dstoff))
875 goto bad;
876 MGET(m, M_DONTWAIT, n->m_type);
877 if (m == NULL)
878 goto bad;
879 MCLAIM(m, n->m_owner);
880 m->m_len = 0;
881 if (n->m_flags & M_PKTHDR) {
882 M_MOVE_PKTHDR(m, n);
883 }
884 m->m_data += dstoff;
885 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
886 do {
887 count = min(min(max(len, max_protohdr), space), n->m_len);
888 memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
889 (unsigned)count);
890 len -= count;
891 m->m_len += count;
892 n->m_len -= count;
893 space -= count;
894 if (n->m_len)
895 n->m_data += count;
896 else
897 n = m_free(n);
898 } while (len > 0 && n);
899 if (len > 0) {
900 (void) m_free(m);
901 goto bad;
902 }
903 m->m_next = n;
904 return (m);
905 bad:
906 m_freem(n);
907 MSFail++;
908 return (NULL);
909 }
910
911 /*
912 * Partition an mbuf chain in two pieces, returning the tail --
913 * all but the first len0 bytes. In case of failure, it returns NULL and
914 * attempts to restore the chain to its original state.
915 */
916 struct mbuf *
917 m_split(struct mbuf *m0, int len0, int wait)
918 {
919
920 return m_split0(m0, len0, wait, 1);
921 }
922
923 static struct mbuf *
924 m_split0(struct mbuf *m0, int len0, int wait, int copyhdr)
925 {
926 struct mbuf *m, *n;
927 unsigned len = len0, remain, len_save;
928
929 for (m = m0; m && len > m->m_len; m = m->m_next)
930 len -= m->m_len;
931 if (m == 0)
932 return (NULL);
933 remain = m->m_len - len;
934 if (copyhdr && (m0->m_flags & M_PKTHDR)) {
935 MGETHDR(n, wait, m0->m_type);
936 if (n == 0)
937 return (NULL);
938 MCLAIM(m, m0->m_owner);
939 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
940 n->m_pkthdr.len = m0->m_pkthdr.len - len0;
941 len_save = m0->m_pkthdr.len;
942 m0->m_pkthdr.len = len0;
943 if (m->m_flags & M_EXT)
944 goto extpacket;
945 if (remain > MHLEN) {
946 /* m can't be the lead packet */
947 MH_ALIGN(n, 0);
948 n->m_next = m_split(m, len, wait);
949 if (n->m_next == 0) {
950 (void) m_free(n);
951 m0->m_pkthdr.len = len_save;
952 return (NULL);
953 } else
954 return (n);
955 } else
956 MH_ALIGN(n, remain);
957 } else if (remain == 0) {
958 n = m->m_next;
959 m->m_next = 0;
960 return (n);
961 } else {
962 MGET(n, wait, m->m_type);
963 if (n == 0)
964 return (NULL);
965 MCLAIM(n, m->m_owner);
966 M_ALIGN(n, remain);
967 }
968 extpacket:
969 if (m->m_flags & M_EXT) {
970 n->m_data = m->m_data + len;
971 MCLADDREFERENCE(m, n);
972 } else {
973 memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + len, remain);
974 }
975 n->m_len = remain;
976 m->m_len = len;
977 n->m_next = m->m_next;
978 m->m_next = 0;
979 return (n);
980 }
981 /*
982 * Routine to copy from device local memory into mbufs.
983 */
984 struct mbuf *
985 m_devget(char *buf, int totlen, int off0, struct ifnet *ifp,
986 void (*copy)(const void *from, void *to, size_t len))
987 {
988 struct mbuf *m;
989 struct mbuf *top = 0, **mp = ⊤
990 int off = off0, len;
991 char *cp;
992 char *epkt;
993
994 cp = buf;
995 epkt = cp + totlen;
996 if (off) {
997 /*
998 * If 'off' is non-zero, packet is trailer-encapsulated,
999 * so we have to skip the type and length fields.
1000 */
1001 cp += off + 2 * sizeof(uint16_t);
1002 totlen -= 2 * sizeof(uint16_t);
1003 }
1004 MGETHDR(m, M_DONTWAIT, MT_DATA);
1005 if (m == 0)
1006 return (NULL);
1007 m->m_pkthdr.rcvif = ifp;
1008 m->m_pkthdr.len = totlen;
1009 m->m_len = MHLEN;
1010
1011 while (totlen > 0) {
1012 if (top) {
1013 MGET(m, M_DONTWAIT, MT_DATA);
1014 if (m == 0) {
1015 m_freem(top);
1016 return (NULL);
1017 }
1018 m->m_len = MLEN;
1019 }
1020 len = min(totlen, epkt - cp);
1021 if (len >= MINCLSIZE) {
1022 MCLGET(m, M_DONTWAIT);
1023 if ((m->m_flags & M_EXT) == 0) {
1024 m_free(m);
1025 m_freem(top);
1026 return (NULL);
1027 }
1028 m->m_len = len = min(len, MCLBYTES);
1029 } else {
1030 /*
1031 * Place initial small packet/header at end of mbuf.
1032 */
1033 if (len < m->m_len) {
1034 if (top == 0 && len + max_linkhdr <= m->m_len)
1035 m->m_data += max_linkhdr;
1036 m->m_len = len;
1037 } else
1038 len = m->m_len;
1039 }
1040 if (copy)
1041 copy(cp, mtod(m, caddr_t), (size_t)len);
1042 else
1043 memcpy(mtod(m, caddr_t), cp, (size_t)len);
1044 cp += len;
1045 *mp = m;
1046 mp = &m->m_next;
1047 totlen -= len;
1048 if (cp == epkt)
1049 cp = buf;
1050 }
1051 return (top);
1052 }
1053
1054 /*
1055 * Copy data from a buffer back into the indicated mbuf chain,
1056 * starting "off" bytes from the beginning, extending the mbuf
1057 * chain if necessary.
1058 */
1059 void
1060 m_copyback(struct mbuf *m0, int off, int len, const void *cp)
1061 {
1062 #if defined(DEBUG)
1063 struct mbuf *origm = m0;
1064 int error;
1065 #endif /* defined(DEBUG) */
1066
1067 if (m0 == NULL)
1068 return;
1069
1070 #if defined(DEBUG)
1071 error =
1072 #endif /* defined(DEBUG) */
1073 m_copyback0(&m0, off, len, cp,
1074 M_COPYBACK0_COPYBACK|M_COPYBACK0_EXTEND, M_DONTWAIT);
1075
1076 #if defined(DEBUG)
1077 if (error != 0 || (m0 != NULL && origm != m0))
1078 panic("m_copyback");
1079 #endif /* defined(DEBUG) */
1080 }
1081
1082 struct mbuf *
1083 m_copyback_cow(struct mbuf *m0, int off, int len, const void *cp, int how)
1084 {
1085 int error;
1086
1087 /* don't support chain expansion */
1088 KDASSERT(off + len <= m_length(m0));
1089
1090 error = m_copyback0(&m0, off, len, cp,
1091 M_COPYBACK0_COPYBACK|M_COPYBACK0_COW, how);
1092 if (error) {
1093 /*
1094 * no way to recover from partial success.
1095 * just free the chain.
1096 */
1097 m_freem(m0);
1098 return NULL;
1099 }
1100 return m0;
1101 }
1102
1103 /*
1104 * m_makewritable: ensure the specified range writable.
1105 */
1106 int
1107 m_makewritable(struct mbuf **mp, int off, int len, int how)
1108 {
1109 int error;
1110 #if defined(DEBUG)
1111 struct mbuf *n;
1112 int origlen, reslen;
1113
1114 origlen = m_length(*mp);
1115 #endif /* defined(DEBUG) */
1116
1117 #if 0 /* M_COPYALL is large enough */
1118 if (len == M_COPYALL)
1119 len = m_length(*mp) - off; /* XXX */
1120 #endif
1121
1122 error = m_copyback0(mp, off, len, NULL,
1123 M_COPYBACK0_PRESERVE|M_COPYBACK0_COW, how);
1124
1125 #if defined(DEBUG)
1126 reslen = 0;
1127 for (n = *mp; n; n = n->m_next)
1128 reslen += n->m_len;
1129 if (origlen != reslen)
1130 panic("m_makewritable: length changed");
1131 if (((*mp)->m_flags & M_PKTHDR) != 0 && reslen != (*mp)->m_pkthdr.len)
1132 panic("m_makewritable: inconsist");
1133 #endif /* defined(DEBUG) */
1134
1135 return error;
1136 }
1137
1138 int
1139 m_copyback0(struct mbuf **mp0, int off, int len, const void *vp, int flags,
1140 int how)
1141 {
1142 int mlen;
1143 struct mbuf *m, *n;
1144 struct mbuf **mp;
1145 int totlen = 0;
1146 const char *cp = vp;
1147
1148 KASSERT(mp0 != NULL);
1149 KASSERT(*mp0 != NULL);
1150 KASSERT((flags & M_COPYBACK0_PRESERVE) == 0 || cp == NULL);
1151 KASSERT((flags & M_COPYBACK0_COPYBACK) == 0 || cp != NULL);
1152
1153 /*
1154 * we don't bother to update "totlen" in the case of M_COPYBACK0_COW,
1155 * assuming that M_COPYBACK0_EXTEND and M_COPYBACK0_COW are exclusive.
1156 */
1157
1158 KASSERT((~flags & (M_COPYBACK0_EXTEND|M_COPYBACK0_COW)) != 0);
1159
1160 mp = mp0;
1161 m = *mp;
1162 while (off > (mlen = m->m_len)) {
1163 off -= mlen;
1164 totlen += mlen;
1165 if (m->m_next == NULL) {
1166 int tspace;
1167 extend:
1168 if ((flags & M_COPYBACK0_EXTEND) == 0)
1169 goto out;
1170
1171 /*
1172 * try to make some space at the end of "m".
1173 */
1174
1175 mlen = m->m_len;
1176 if (off + len >= MINCLSIZE &&
1177 (m->m_flags & M_EXT) == 0 && m->m_len == 0) {
1178 MCLGET(m, how);
1179 }
1180 tspace = M_TRAILINGSPACE(m);
1181 if (tspace > 0) {
1182 tspace = min(tspace, off + len);
1183 KASSERT(tspace > 0);
1184 memset(mtod(m, char *) + m->m_len, 0,
1185 min(off, tspace));
1186 m->m_len += tspace;
1187 off += mlen;
1188 totlen -= mlen;
1189 continue;
1190 }
1191
1192 /*
1193 * need to allocate an mbuf.
1194 */
1195
1196 if (off + len >= MINCLSIZE) {
1197 n = m_getcl(how, m->m_type, 0);
1198 } else {
1199 n = m_get(how, m->m_type);
1200 }
1201 if (n == NULL) {
1202 goto out;
1203 }
1204 n->m_len = 0;
1205 n->m_len = min(M_TRAILINGSPACE(n), off + len);
1206 memset(mtod(n, char *), 0, min(n->m_len, off));
1207 m->m_next = n;
1208 }
1209 mp = &m->m_next;
1210 m = m->m_next;
1211 }
1212 while (len > 0) {
1213 mlen = m->m_len - off;
1214 if (mlen != 0 && M_READONLY(m)) {
1215 char *datap;
1216 int eatlen;
1217
1218 /*
1219 * this mbuf is read-only.
1220 * allocate a new writable mbuf and try again.
1221 */
1222
1223 #if defined(DIAGNOSTIC)
1224 if ((flags & M_COPYBACK0_COW) == 0)
1225 panic("m_copyback0: read-only");
1226 #endif /* defined(DIAGNOSTIC) */
1227
1228 /*
1229 * if we're going to write into the middle of
1230 * a mbuf, split it first.
1231 */
1232 if (off > 0 && len < mlen) {
1233 n = m_split0(m, off, how, 0);
1234 if (n == NULL)
1235 goto enobufs;
1236 m->m_next = n;
1237 mp = &m->m_next;
1238 m = n;
1239 off = 0;
1240 continue;
1241 }
1242
1243 /*
1244 * XXX TODO coalesce into the trailingspace of
1245 * the previous mbuf when possible.
1246 */
1247
1248 /*
1249 * allocate a new mbuf. copy packet header if needed.
1250 */
1251 MGET(n, how, m->m_type);
1252 if (n == NULL)
1253 goto enobufs;
1254 MCLAIM(n, m->m_owner);
1255 if (off == 0 && (m->m_flags & M_PKTHDR) != 0) {
1256 M_MOVE_PKTHDR(n, m);
1257 n->m_len = MHLEN;
1258 } else {
1259 if (len >= MINCLSIZE)
1260 MCLGET(n, M_DONTWAIT);
1261 n->m_len =
1262 (n->m_flags & M_EXT) ? MCLBYTES : MLEN;
1263 }
1264 if (n->m_len > len)
1265 n->m_len = len;
1266
1267 /*
1268 * free the region which has been overwritten.
1269 * copying data from old mbufs if requested.
1270 */
1271 if (flags & M_COPYBACK0_PRESERVE)
1272 datap = mtod(n, char *);
1273 else
1274 datap = NULL;
1275 eatlen = n->m_len;
1276 KDASSERT(off == 0 || eatlen >= mlen);
1277 if (off > 0) {
1278 KDASSERT(len >= mlen);
1279 m->m_len = off;
1280 m->m_next = n;
1281 if (datap) {
1282 m_copydata(m, off, mlen, datap);
1283 datap += mlen;
1284 }
1285 eatlen -= mlen;
1286 mp = &m->m_next;
1287 m = m->m_next;
1288 }
1289 while (m != NULL && M_READONLY(m) &&
1290 n->m_type == m->m_type && eatlen > 0) {
1291 mlen = min(eatlen, m->m_len);
1292 if (datap) {
1293 m_copydata(m, 0, mlen, datap);
1294 datap += mlen;
1295 }
1296 m->m_data += mlen;
1297 m->m_len -= mlen;
1298 eatlen -= mlen;
1299 if (m->m_len == 0)
1300 *mp = m = m_free(m);
1301 }
1302 if (eatlen > 0)
1303 n->m_len -= eatlen;
1304 n->m_next = m;
1305 *mp = m = n;
1306 continue;
1307 }
1308 mlen = min(mlen, len);
1309 if (flags & M_COPYBACK0_COPYBACK) {
1310 memcpy(mtod(m, caddr_t) + off, cp, (unsigned)mlen);
1311 cp += mlen;
1312 }
1313 len -= mlen;
1314 mlen += off;
1315 off = 0;
1316 totlen += mlen;
1317 if (len == 0)
1318 break;
1319 if (m->m_next == NULL) {
1320 goto extend;
1321 }
1322 mp = &m->m_next;
1323 m = m->m_next;
1324 }
1325 out: if (((m = *mp0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) {
1326 KASSERT((flags & M_COPYBACK0_EXTEND) != 0);
1327 m->m_pkthdr.len = totlen;
1328 }
1329
1330 return 0;
1331
1332 enobufs:
1333 return ENOBUFS;
1334 }
1335
1336 void
1337 m_move_pkthdr(struct mbuf *to, struct mbuf *from)
1338 {
1339
1340 KASSERT((to->m_flags & M_EXT) == 0);
1341 KASSERT((to->m_flags & M_PKTHDR) == 0 || m_tag_first(to) == NULL);
1342 KASSERT((from->m_flags & M_PKTHDR) != 0);
1343
1344 to->m_pkthdr = from->m_pkthdr;
1345 to->m_flags = from->m_flags & M_COPYFLAGS;
1346 to->m_data = to->m_pktdat;
1347
1348 from->m_flags &= ~M_PKTHDR;
1349 }
1350
1351 /*
1352 * Apply function f to the data in an mbuf chain starting "off" bytes from the
1353 * beginning, continuing for "len" bytes.
1354 */
1355 int
1356 m_apply(struct mbuf *m, int off, int len,
1357 int (*f)(void *, caddr_t, unsigned int), void *arg)
1358 {
1359 unsigned int count;
1360 int rval;
1361
1362 KASSERT(len >= 0);
1363 KASSERT(off >= 0);
1364
1365 while (off > 0) {
1366 KASSERT(m != NULL);
1367 if (off < m->m_len)
1368 break;
1369 off -= m->m_len;
1370 m = m->m_next;
1371 }
1372 while (len > 0) {
1373 KASSERT(m != NULL);
1374 count = min(m->m_len - off, len);
1375
1376 rval = (*f)(arg, mtod(m, caddr_t) + off, count);
1377 if (rval)
1378 return (rval);
1379
1380 len -= count;
1381 off = 0;
1382 m = m->m_next;
1383 }
1384
1385 return (0);
1386 }
1387
1388 /*
1389 * Return a pointer to mbuf/offset of location in mbuf chain.
1390 */
1391 struct mbuf *
1392 m_getptr(struct mbuf *m, int loc, int *off)
1393 {
1394
1395 while (loc >= 0) {
1396 /* Normal end of search */
1397 if (m->m_len > loc) {
1398 *off = loc;
1399 return (m);
1400 } else {
1401 loc -= m->m_len;
1402
1403 if (m->m_next == NULL) {
1404 if (loc == 0) {
1405 /* Point at the end of valid data */
1406 *off = m->m_len;
1407 return (m);
1408 } else
1409 return (NULL);
1410 } else
1411 m = m->m_next;
1412 }
1413 }
1414
1415 return (NULL);
1416 }
1417
1418 /*
1419 * m_ext_free: release a reference to the mbuf external storage.
1420 *
1421 * => free the mbuf m itsself as well.
1422 * => called at splvm.
1423 */
1424
1425 void
1426 m_ext_free(struct mbuf *m)
1427 {
1428 boolean_t embedded = MEXT_ISEMBEDDED(m);
1429 boolean_t dofree = TRUE;
1430
1431 KASSERT((m->m_flags & M_EXT) != 0);
1432 KASSERT(MEXT_ISEMBEDDED(m->m_ext_ref));
1433 KASSERT((m->m_ext_ref->m_flags & M_EXT) != 0);
1434 KASSERT((m->m_flags & M_EXT_CLUSTER) ==
1435 (m->m_ext_ref->m_flags & M_EXT_CLUSTER));
1436
1437 MEXT_LOCK(m);
1438 if (MCLISREFERENCED(m)) {
1439 _MCLDEREFERENCE(m);
1440 MEXT_UNLOCK(m);
1441 if (embedded) {
1442 dofree = FALSE;
1443 } else {
1444 m->m_ext_ref = m;
1445 }
1446 } else {
1447 MEXT_UNLOCK(m);
1448 /*
1449 * dropping the last reference
1450 */
1451 if (!embedded) {
1452 m_ext_free(m->m_ext_ref);
1453 m->m_ext_ref = m;
1454 } else if ((m->m_flags & M_EXT_CLUSTER) != 0) {
1455 pool_cache_put_paddr((struct pool_cache *)
1456 m->m_ext.ext_arg,
1457 m->m_ext.ext_buf, m->m_ext.ext_paddr);
1458 } else if (m->m_ext.ext_free) {
1459 (*m->m_ext.ext_free)(m,
1460 m->m_ext.ext_buf, m->m_ext.ext_size,
1461 m->m_ext.ext_arg);
1462 /*
1463 * 'm' is already freed by the ext_free callback.
1464 */
1465 dofree = FALSE;
1466 } else {
1467 free(m->m_ext.ext_buf, m->m_ext.ext_type);
1468 }
1469 }
1470 if (dofree) {
1471 pool_cache_put(&mbpool_cache, m);
1472 }
1473 }
1474
1475 #if defined(__HAVE_LAZY_MBUF) || defined(DEBUG)
1476 caddr_t
1477 m_mapin(struct mbuf *m)
1478 {
1479 #if defined(__HAVE_LAZY_MBUF)
1480 int s;
1481
1482 KASSERT((~m->m_flags & (M_EXT|M_EXT_PAGES|M_EXT_LAZY)) == 0);
1483
1484 s = splvm();
1485 MEXT_LOCK(m);
1486 if (m->m_ext.ext_flags & M_EXT_LAZY) {
1487 vaddr_t buf = (vaddr_t)m->m_ext.ext_buf;
1488 vsize_t size = (vsize_t)m->m_ext.ext_size;
1489 vaddr_t va, sva, eva;
1490 int i;
1491
1492 sva = trunc_page(buf);
1493 eva = round_page(buf + size);
1494
1495 for (i = 0, va = sva; va < eva; i++, va += PAGE_SIZE) {
1496 pmap_kenter_pa(va, VM_PAGE_TO_PHYS(m->m_ext.ext_pgs[i]),
1497 VM_PROT_READ);
1498 }
1499 pmap_update(pmap_kernel());
1500 m->m_ext.ext_flags &= ~M_EXT_LAZY;
1501 }
1502 MEXT_UNLOCK(m);
1503 splx(s);
1504
1505 m->m_flags &= ~M_EXT_LAZY;
1506 return m->m_data;
1507 #else /* defined(__HAVE_LAZY_MBUF) */
1508 panic("m_mapin");
1509 #endif /* defined(__HAVE_LAZY_MBUF) */
1510 }
1511 #endif /* defined(__HAVE_LAZY_MBUF) || defined(DEBUG) */
1512
1513 #if defined(DDB)
1514 void
1515 m_print(const struct mbuf *m, const char *modif, void (*pr)(const char *, ...))
1516 {
1517 char ch;
1518 boolean_t opt_c = FALSE;
1519 char buf[512];
1520
1521 while ((ch = *(modif++)) != '\0') {
1522 switch (ch) {
1523 case 'c':
1524 opt_c = TRUE;
1525 break;
1526 }
1527 }
1528
1529 nextchain:
1530 (*pr)("MBUF %p\n", m);
1531 bitmask_snprintf(m->m_flags, M_FLAGS_BITS, buf, sizeof(buf));
1532 (*pr)(" data=%p, len=%d, type=%d, flags=0x%s\n",
1533 m->m_data, m->m_len, m->m_type, buf);
1534 (*pr)(" owner=%p, next=%p, nextpkt=%p\n", m->m_owner, m->m_next,
1535 m->m_nextpkt);
1536 (*pr)(" leadingspace=%u, trailingspace=%u, readonly=%u\n",
1537 (int)M_LEADINGSPACE(m), (int)M_TRAILINGSPACE(m),
1538 (int)M_READONLY(m));
1539 if ((m->m_flags & M_PKTHDR) != 0) {
1540 bitmask_snprintf(m->m_pkthdr.csum_flags, M_CSUM_BITS, buf,
1541 sizeof(buf));
1542 (*pr)(" pktlen=%d, rcvif=%p, csum_flags=0x%s, csum_data=0x%"
1543 PRIx32 ", segsz=%u\n",
1544 m->m_pkthdr.len, m->m_pkthdr.rcvif,
1545 buf, m->m_pkthdr.csum_data, m->m_pkthdr.segsz);
1546 }
1547 if ((m->m_flags & M_EXT)) {
1548 (*pr)(" shared=%u, ext_buf=%p, ext_size=%zd, "
1549 "ext_free=%p, ext_arg=%p\n",
1550 (int)MCLISREFERENCED(m),
1551 m->m_ext.ext_buf, m->m_ext.ext_size,
1552 m->m_ext.ext_free, m->m_ext.ext_arg);
1553 }
1554 if ((~m->m_flags & (M_EXT|M_EXT_PAGES)) == 0) {
1555 vaddr_t sva = (vaddr_t)m->m_ext.ext_buf;
1556 vaddr_t eva = sva + m->m_ext.ext_size;
1557 int n = (round_page(eva) - trunc_page(sva)) >> PAGE_SHIFT;
1558 int i;
1559
1560 (*pr)(" pages:");
1561 for (i = 0; i < n; i ++) {
1562 (*pr)(" %p", m->m_ext.ext_pgs[i]);
1563 }
1564 (*pr)("\n");
1565 }
1566
1567 if (opt_c) {
1568 m = m->m_next;
1569 if (m != NULL) {
1570 goto nextchain;
1571 }
1572 }
1573 }
1574 #endif /* defined(DDB) */
1575