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