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