uipc_mbuf.c revision 1.141 1 /* $NetBSD: uipc_mbuf.c,v 1.141 2011/07/27 14:35:34 uebayasi 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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1982, 1986, 1988, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)uipc_mbuf.c 8.4 (Berkeley) 2/14/95
62 */
63
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: uipc_mbuf.c,v 1.141 2011/07/27 14:35:34 uebayasi Exp $");
66
67 #include "opt_mbuftrace.h"
68 #include "opt_nmbclusters.h"
69 #include "opt_ddb.h"
70
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/atomic.h>
74 #include <sys/cpu.h>
75 #include <sys/proc.h>
76 #define MBTYPES
77 #include <sys/mbuf.h>
78 #include <sys/kernel.h>
79 #include <sys/syslog.h>
80 #include <sys/domain.h>
81 #include <sys/protosw.h>
82 #include <sys/percpu.h>
83 #include <sys/pool.h>
84 #include <sys/socket.h>
85 #include <sys/sysctl.h>
86
87 #include <net/if.h>
88
89 pool_cache_t mb_cache; /* mbuf cache */
90 pool_cache_t mcl_cache; /* mbuf cluster cache */
91
92 struct mbstat mbstat;
93 int max_linkhdr;
94 int max_protohdr;
95 int max_hdr;
96 int max_datalen;
97
98 static int mb_ctor(void *, void *, int);
99
100 static void sysctl_kern_mbuf_setup(void);
101
102 static struct sysctllog *mbuf_sysctllog;
103
104 static struct mbuf *m_copym0(struct mbuf *, int, int, int, int);
105 static struct mbuf *m_split0(struct mbuf *, int, int, int);
106 static int m_copyback0(struct mbuf **, int, int, const void *, int, int);
107
108 /* flags for m_copyback0 */
109 #define M_COPYBACK0_COPYBACK 0x0001 /* copyback from cp */
110 #define M_COPYBACK0_PRESERVE 0x0002 /* preserve original data */
111 #define M_COPYBACK0_COW 0x0004 /* do copy-on-write */
112 #define M_COPYBACK0_EXTEND 0x0008 /* extend chain */
113
114 static const char mclpool_warnmsg[] =
115 "WARNING: mclpool limit reached; increase kern.mbuf.nmbclusters";
116
117 MALLOC_DEFINE(M_MBUF, "mbuf", "mbuf");
118
119 static percpu_t *mbstat_percpu;
120
121 #ifdef MBUFTRACE
122 struct mownerhead mowners = LIST_HEAD_INITIALIZER(mowners);
123 struct mowner unknown_mowners[] = {
124 MOWNER_INIT("unknown", "free"),
125 MOWNER_INIT("unknown", "data"),
126 MOWNER_INIT("unknown", "header"),
127 MOWNER_INIT("unknown", "soname"),
128 MOWNER_INIT("unknown", "soopts"),
129 MOWNER_INIT("unknown", "ftable"),
130 MOWNER_INIT("unknown", "control"),
131 MOWNER_INIT("unknown", "oobdata"),
132 };
133 struct mowner revoked_mowner = MOWNER_INIT("revoked", "");
134 #endif
135
136 #define MEXT_ISEMBEDDED(m) ((m)->m_ext_ref == (m))
137
138 #define MCLADDREFERENCE(o, n) \
139 do { \
140 KASSERT(((o)->m_flags & M_EXT) != 0); \
141 KASSERT(((n)->m_flags & M_EXT) == 0); \
142 KASSERT((o)->m_ext.ext_refcnt >= 1); \
143 (n)->m_flags |= ((o)->m_flags & M_EXTCOPYFLAGS); \
144 atomic_inc_uint(&(o)->m_ext.ext_refcnt); \
145 (n)->m_ext_ref = (o)->m_ext_ref; \
146 mowner_ref((n), (n)->m_flags); \
147 MCLREFDEBUGN((n), __FILE__, __LINE__); \
148 } while (/* CONSTCOND */ 0)
149
150 static int
151 nmbclusters_limit(void)
152 {
153 #if defined(PMAP_MAP_POOLPAGE)
154 /* direct mapping, doesn't use space in kmem_map */
155 vsize_t max_size = physmem / 4;
156 #else
157 vsize_t max_size = MIN(physmem / 4, nkmempages / 2);
158 #endif
159
160 max_size = max_size * PAGE_SIZE / MCLBYTES;
161 #ifdef NMBCLUSTERS_MAX
162 max_size = MIN(max_size, NMBCLUSTERS_MAX);
163 #endif
164
165 #ifdef NMBCLUSTERS
166 return MIN(max_size, NMBCLUSTERS);
167 #else
168 return max_size;
169 #endif
170 }
171
172 /*
173 * Initialize the mbuf allocator.
174 */
175 void
176 mbinit(void)
177 {
178
179 CTASSERT(sizeof(struct _m_ext) <= MHLEN);
180 CTASSERT(sizeof(struct mbuf) == MSIZE);
181
182 sysctl_kern_mbuf_setup();
183
184 mb_cache = pool_cache_init(msize, 0, 0, 0, "mbpl",
185 NULL, IPL_VM, mb_ctor, NULL, NULL);
186 KASSERT(mb_cache != NULL);
187
188 mcl_cache = pool_cache_init(mclbytes, 0, 0, 0, "mclpl", NULL,
189 IPL_VM, NULL, NULL, NULL);
190 KASSERT(mcl_cache != NULL);
191
192 pool_cache_set_drain_hook(mb_cache, m_reclaim, NULL);
193 pool_cache_set_drain_hook(mcl_cache, m_reclaim, NULL);
194
195 /*
196 * Set an arbitrary default limit on the number of mbuf clusters.
197 */
198 #ifdef NMBCLUSTERS
199 nmbclusters = nmbclusters_limit();
200 #else
201 nmbclusters = MAX(1024,
202 (vsize_t)physmem * PAGE_SIZE / MCLBYTES / 16);
203 nmbclusters = MIN(nmbclusters, nmbclusters_limit());
204 #endif
205
206 /*
207 * Set the hard limit on the mclpool to the number of
208 * mbuf clusters the kernel is to support. Log the limit
209 * reached message max once a minute.
210 */
211 pool_cache_sethardlimit(mcl_cache, nmbclusters, mclpool_warnmsg, 60);
212
213 mbstat_percpu = percpu_alloc(sizeof(struct mbstat_cpu));
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_cache_setlowat(mb_cache, mblowat);
222 pool_cache_setlowat(mcl_cache, 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.
240 * nmbclusters, 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 case MBUF_MBLOWAT:
254 case MBUF_MCLLOWAT:
255 newval = *(int*)rnode->sysctl_data;
256 break;
257 default:
258 return (EOPNOTSUPP);
259 }
260
261 error = sysctl_lookup(SYSCTLFN_CALL(&node));
262 if (error || newp == NULL)
263 return (error);
264 if (newval < 0)
265 return (EINVAL);
266
267 switch (node.sysctl_num) {
268 case MBUF_NMBCLUSTERS:
269 if (newval < nmbclusters)
270 return (EINVAL);
271 if (newval > nmbclusters_limit())
272 return (EINVAL);
273 nmbclusters = newval;
274 pool_cache_sethardlimit(mcl_cache, nmbclusters,
275 mclpool_warnmsg, 60);
276 break;
277 case MBUF_MBLOWAT:
278 mblowat = newval;
279 pool_cache_setlowat(mb_cache, mblowat);
280 break;
281 case MBUF_MCLLOWAT:
282 mcllowat = newval;
283 pool_cache_setlowat(mcl_cache, mcllowat);
284 break;
285 }
286
287 return (0);
288 }
289
290 #ifdef MBUFTRACE
291 static void
292 mowner_conver_to_user_cb(void *v1, void *v2, struct cpu_info *ci)
293 {
294 struct mowner_counter *mc = v1;
295 struct mowner_user *mo_user = v2;
296 int i;
297
298 for (i = 0; i < MOWNER_COUNTER_NCOUNTERS; i++) {
299 mo_user->mo_counter[i] += mc->mc_counter[i];
300 }
301 }
302
303 static void
304 mowner_convert_to_user(struct mowner *mo, struct mowner_user *mo_user)
305 {
306
307 memset(mo_user, 0, sizeof(*mo_user));
308 CTASSERT(sizeof(mo_user->mo_name) == sizeof(mo->mo_name));
309 CTASSERT(sizeof(mo_user->mo_descr) == sizeof(mo->mo_descr));
310 memcpy(mo_user->mo_name, mo->mo_name, sizeof(mo->mo_name));
311 memcpy(mo_user->mo_descr, mo->mo_descr, sizeof(mo->mo_descr));
312 percpu_foreach(mo->mo_counters, mowner_conver_to_user_cb, mo_user);
313 }
314
315 static int
316 sysctl_kern_mbuf_mowners(SYSCTLFN_ARGS)
317 {
318 struct mowner *mo;
319 size_t len = 0;
320 int error = 0;
321
322 if (namelen != 0)
323 return (EINVAL);
324 if (newp != NULL)
325 return (EPERM);
326
327 LIST_FOREACH(mo, &mowners, mo_link) {
328 struct mowner_user mo_user;
329
330 mowner_convert_to_user(mo, &mo_user);
331
332 if (oldp != NULL) {
333 if (*oldlenp - len < sizeof(mo_user)) {
334 error = ENOMEM;
335 break;
336 }
337 error = copyout(&mo_user, (char *)oldp + len,
338 sizeof(mo_user));
339 if (error)
340 break;
341 }
342 len += sizeof(mo_user);
343 }
344
345 if (error == 0)
346 *oldlenp = len;
347
348 return (error);
349 }
350 #endif /* MBUFTRACE */
351
352 static void
353 mbstat_conver_to_user_cb(void *v1, void *v2, struct cpu_info *ci)
354 {
355 struct mbstat_cpu *mbsc = v1;
356 struct mbstat *mbs = v2;
357 int i;
358
359 for (i = 0; i < __arraycount(mbs->m_mtypes); i++) {
360 mbs->m_mtypes[i] += mbsc->m_mtypes[i];
361 }
362 }
363
364 static void
365 mbstat_convert_to_user(struct mbstat *mbs)
366 {
367
368 memset(mbs, 0, sizeof(*mbs));
369 mbs->m_drain = mbstat.m_drain;
370 percpu_foreach(mbstat_percpu, mbstat_conver_to_user_cb, mbs);
371 }
372
373 static int
374 sysctl_kern_mbuf_stats(SYSCTLFN_ARGS)
375 {
376 struct sysctlnode node;
377 struct mbstat mbs;
378
379 mbstat_convert_to_user(&mbs);
380 node = *rnode;
381 node.sysctl_data = &mbs;
382 node.sysctl_size = sizeof(mbs);
383 return sysctl_lookup(SYSCTLFN_CALL(&node));
384 }
385
386 static void
387 sysctl_kern_mbuf_setup(void)
388 {
389
390 KASSERT(mbuf_sysctllog == NULL);
391 sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
392 CTLFLAG_PERMANENT,
393 CTLTYPE_NODE, "kern", NULL,
394 NULL, 0, NULL, 0,
395 CTL_KERN, CTL_EOL);
396 sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
397 CTLFLAG_PERMANENT,
398 CTLTYPE_NODE, "mbuf",
399 SYSCTL_DESCR("mbuf control variables"),
400 NULL, 0, NULL, 0,
401 CTL_KERN, KERN_MBUF, CTL_EOL);
402
403 sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
404 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
405 CTLTYPE_INT, "msize",
406 SYSCTL_DESCR("mbuf base size"),
407 NULL, msize, NULL, 0,
408 CTL_KERN, KERN_MBUF, MBUF_MSIZE, CTL_EOL);
409 sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
410 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
411 CTLTYPE_INT, "mclbytes",
412 SYSCTL_DESCR("mbuf cluster size"),
413 NULL, mclbytes, NULL, 0,
414 CTL_KERN, KERN_MBUF, MBUF_MCLBYTES, CTL_EOL);
415 sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
416 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
417 CTLTYPE_INT, "nmbclusters",
418 SYSCTL_DESCR("Limit on the number of mbuf clusters"),
419 sysctl_kern_mbuf, 0, &nmbclusters, 0,
420 CTL_KERN, KERN_MBUF, MBUF_NMBCLUSTERS, CTL_EOL);
421 sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
422 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
423 CTLTYPE_INT, "mblowat",
424 SYSCTL_DESCR("mbuf low water mark"),
425 sysctl_kern_mbuf, 0, &mblowat, 0,
426 CTL_KERN, KERN_MBUF, MBUF_MBLOWAT, CTL_EOL);
427 sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
428 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
429 CTLTYPE_INT, "mcllowat",
430 SYSCTL_DESCR("mbuf cluster low water mark"),
431 sysctl_kern_mbuf, 0, &mcllowat, 0,
432 CTL_KERN, KERN_MBUF, MBUF_MCLLOWAT, CTL_EOL);
433 sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
434 CTLFLAG_PERMANENT,
435 CTLTYPE_STRUCT, "stats",
436 SYSCTL_DESCR("mbuf allocation statistics"),
437 sysctl_kern_mbuf_stats, 0, NULL, 0,
438 CTL_KERN, KERN_MBUF, MBUF_STATS, CTL_EOL);
439 #ifdef MBUFTRACE
440 sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
441 CTLFLAG_PERMANENT,
442 CTLTYPE_STRUCT, "mowners",
443 SYSCTL_DESCR("Information about mbuf owners"),
444 sysctl_kern_mbuf_mowners, 0, NULL, 0,
445 CTL_KERN, KERN_MBUF, MBUF_MOWNERS, CTL_EOL);
446 #endif /* MBUFTRACE */
447 }
448
449 static int
450 mb_ctor(void *arg, void *object, int flags)
451 {
452 struct mbuf *m = object;
453
454 #ifdef POOL_VTOPHYS
455 m->m_paddr = POOL_VTOPHYS(m);
456 #else
457 m->m_paddr = M_PADDR_INVALID;
458 #endif
459 return (0);
460 }
461
462 void
463 m_reclaim(void *arg, int flags)
464 {
465 struct domain *dp;
466 const struct protosw *pr;
467 struct ifnet *ifp;
468 int s;
469
470 KERNEL_LOCK(1, NULL);
471 s = splvm();
472 DOMAIN_FOREACH(dp) {
473 for (pr = dp->dom_protosw;
474 pr < dp->dom_protoswNPROTOSW; pr++)
475 if (pr->pr_drain)
476 (*pr->pr_drain)();
477 }
478 IFNET_FOREACH(ifp) {
479 if (ifp->if_drain)
480 (*ifp->if_drain)(ifp);
481 }
482 splx(s);
483 mbstat.m_drain++;
484 KERNEL_UNLOCK_ONE(NULL);
485 }
486
487 /*
488 * Space allocation routines.
489 * These are also available as macros
490 * for critical paths.
491 */
492 struct mbuf *
493 m_get(int nowait, int type)
494 {
495 struct mbuf *m;
496
497 m = pool_cache_get(mb_cache,
498 nowait == M_WAIT ? PR_WAITOK|PR_LIMITFAIL : 0);
499 if (m == NULL)
500 return NULL;
501
502 mbstat_type_add(type, 1);
503 mowner_init(m, type);
504 m->m_ext_ref = m;
505 m->m_type = type;
506 m->m_next = NULL;
507 m->m_nextpkt = NULL;
508 m->m_data = m->m_dat;
509 m->m_flags = 0;
510
511 return m;
512 }
513
514 struct mbuf *
515 m_gethdr(int nowait, int type)
516 {
517 struct mbuf *m;
518
519 m = m_get(nowait, type);
520 if (m == NULL)
521 return NULL;
522
523 m->m_data = m->m_pktdat;
524 m->m_flags = M_PKTHDR;
525 m->m_pkthdr.rcvif = NULL;
526 m->m_pkthdr.csum_flags = 0;
527 m->m_pkthdr.csum_data = 0;
528 SLIST_INIT(&m->m_pkthdr.tags);
529
530 return m;
531 }
532
533 struct mbuf *
534 m_getclr(int nowait, int type)
535 {
536 struct mbuf *m;
537
538 MGET(m, nowait, type);
539 if (m == 0)
540 return (NULL);
541 memset(mtod(m, void *), 0, MLEN);
542 return (m);
543 }
544
545 void
546 m_clget(struct mbuf *m, int nowait)
547 {
548
549 MCLGET(m, nowait);
550 }
551
552 struct mbuf *
553 m_free(struct mbuf *m)
554 {
555 struct mbuf *n;
556
557 MFREE(m, n);
558 return (n);
559 }
560
561 void
562 m_freem(struct mbuf *m)
563 {
564 struct mbuf *n;
565
566 if (m == NULL)
567 return;
568 do {
569 MFREE(m, n);
570 m = n;
571 } while (m);
572 }
573
574 #ifdef MBUFTRACE
575 /*
576 * Walk a chain of mbufs, claiming ownership of each mbuf in the chain.
577 */
578 void
579 m_claimm(struct mbuf *m, struct mowner *mo)
580 {
581
582 for (; m != NULL; m = m->m_next)
583 MCLAIM(m, mo);
584 }
585 #endif
586
587 /*
588 * Mbuffer utility routines.
589 */
590
591 /*
592 * Lesser-used path for M_PREPEND:
593 * allocate new mbuf to prepend to chain,
594 * copy junk along.
595 */
596 struct mbuf *
597 m_prepend(struct mbuf *m, int len, int how)
598 {
599 struct mbuf *mn;
600
601 MGET(mn, how, m->m_type);
602 if (mn == (struct mbuf *)NULL) {
603 m_freem(m);
604 return ((struct mbuf *)NULL);
605 }
606 if (m->m_flags & M_PKTHDR) {
607 M_MOVE_PKTHDR(mn, m);
608 } else {
609 MCLAIM(mn, m->m_owner);
610 }
611 mn->m_next = m;
612 m = mn;
613 if (len < MHLEN)
614 MH_ALIGN(m, len);
615 m->m_len = len;
616 return (m);
617 }
618
619 /*
620 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
621 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
622 * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
623 */
624 int MCFail;
625
626 struct mbuf *
627 m_copym(struct mbuf *m, int off0, int len, int wait)
628 {
629
630 return m_copym0(m, off0, len, wait, 0); /* shallow copy on M_EXT */
631 }
632
633 struct mbuf *
634 m_dup(struct mbuf *m, int off0, int len, int wait)
635 {
636
637 return m_copym0(m, off0, len, wait, 1); /* deep copy */
638 }
639
640 static struct mbuf *
641 m_copym0(struct mbuf *m, int off0, int len, int wait, int deep)
642 {
643 struct mbuf *n, **np;
644 int off = off0;
645 struct mbuf *top;
646 int copyhdr = 0;
647
648 if (off < 0 || len < 0)
649 panic("m_copym: off %d, len %d", off, len);
650 if (off == 0 && m->m_flags & M_PKTHDR)
651 copyhdr = 1;
652 while (off > 0) {
653 if (m == 0)
654 panic("m_copym: m == 0, off %d", off);
655 if (off < m->m_len)
656 break;
657 off -= m->m_len;
658 m = m->m_next;
659 }
660 np = ⊤
661 top = 0;
662 while (len > 0) {
663 if (m == 0) {
664 if (len != M_COPYALL)
665 panic("m_copym: m == 0, len %d [!COPYALL]",
666 len);
667 break;
668 }
669 MGET(n, wait, m->m_type);
670 *np = n;
671 if (n == 0)
672 goto nospace;
673 MCLAIM(n, m->m_owner);
674 if (copyhdr) {
675 M_COPY_PKTHDR(n, m);
676 if (len == M_COPYALL)
677 n->m_pkthdr.len -= off0;
678 else
679 n->m_pkthdr.len = len;
680 copyhdr = 0;
681 }
682 n->m_len = min(len, m->m_len - off);
683 if (m->m_flags & M_EXT) {
684 if (!deep) {
685 n->m_data = m->m_data + off;
686 MCLADDREFERENCE(m, n);
687 } else {
688 /*
689 * we are unsure about the way m was allocated.
690 * copy into multiple MCLBYTES cluster mbufs.
691 */
692 MCLGET(n, wait);
693 n->m_len = 0;
694 n->m_len = M_TRAILINGSPACE(n);
695 n->m_len = min(n->m_len, len);
696 n->m_len = min(n->m_len, m->m_len - off);
697 memcpy(mtod(n, void *), mtod(m, char *) + off,
698 (unsigned)n->m_len);
699 }
700 } else
701 memcpy(mtod(n, void *), mtod(m, char *) + off,
702 (unsigned)n->m_len);
703 if (len != M_COPYALL)
704 len -= n->m_len;
705 off += n->m_len;
706 #ifdef DIAGNOSTIC
707 if (off > m->m_len)
708 panic("m_copym0 overrun");
709 #endif
710 if (off == m->m_len) {
711 m = m->m_next;
712 off = 0;
713 }
714 np = &n->m_next;
715 }
716 if (top == 0)
717 MCFail++;
718 return (top);
719 nospace:
720 m_freem(top);
721 MCFail++;
722 return (NULL);
723 }
724
725 /*
726 * Copy an entire packet, including header (which must be present).
727 * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
728 */
729 struct mbuf *
730 m_copypacket(struct mbuf *m, int how)
731 {
732 struct mbuf *top, *n, *o;
733
734 MGET(n, how, m->m_type);
735 top = n;
736 if (!n)
737 goto nospace;
738
739 MCLAIM(n, m->m_owner);
740 M_COPY_PKTHDR(n, m);
741 n->m_len = m->m_len;
742 if (m->m_flags & M_EXT) {
743 n->m_data = m->m_data;
744 MCLADDREFERENCE(m, n);
745 } else {
746 memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
747 }
748
749 m = m->m_next;
750 while (m) {
751 MGET(o, how, m->m_type);
752 if (!o)
753 goto nospace;
754
755 MCLAIM(o, m->m_owner);
756 n->m_next = o;
757 n = n->m_next;
758
759 n->m_len = m->m_len;
760 if (m->m_flags & M_EXT) {
761 n->m_data = m->m_data;
762 MCLADDREFERENCE(m, n);
763 } else {
764 memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
765 }
766
767 m = m->m_next;
768 }
769 return top;
770 nospace:
771 m_freem(top);
772 MCFail++;
773 return NULL;
774 }
775
776 /*
777 * Copy data from an mbuf chain starting "off" bytes from the beginning,
778 * continuing for "len" bytes, into the indicated buffer.
779 */
780 void
781 m_copydata(struct mbuf *m, int off, int len, void *vp)
782 {
783 unsigned count;
784 void * cp = vp;
785
786 if (off < 0 || len < 0)
787 panic("m_copydata: off %d, len %d", off, len);
788 while (off > 0) {
789 if (m == NULL)
790 panic("m_copydata: m == NULL, off %d", off);
791 if (off < m->m_len)
792 break;
793 off -= m->m_len;
794 m = m->m_next;
795 }
796 while (len > 0) {
797 if (m == NULL)
798 panic("m_copydata: m == NULL, len %d", len);
799 count = min(m->m_len - off, len);
800 memcpy(cp, mtod(m, char *) + off, count);
801 len -= count;
802 cp = (char *)cp + count;
803 off = 0;
804 m = m->m_next;
805 }
806 }
807
808 /*
809 * Concatenate mbuf chain n to m.
810 * n might be copied into m (when n->m_len is small), therefore data portion of
811 * n could be copied into an mbuf of different mbuf type.
812 * Any m_pkthdr is not updated.
813 */
814 void
815 m_cat(struct mbuf *m, struct mbuf *n)
816 {
817
818 while (m->m_next)
819 m = m->m_next;
820 while (n) {
821 if (M_READONLY(m) || n->m_len > M_TRAILINGSPACE(m)) {
822 /* just join the two chains */
823 m->m_next = n;
824 return;
825 }
826 /* splat the data from one into the other */
827 memcpy(mtod(m, char *) + m->m_len, mtod(n, void *),
828 (u_int)n->m_len);
829 m->m_len += n->m_len;
830 n = m_free(n);
831 }
832 }
833
834 void
835 m_adj(struct mbuf *mp, int req_len)
836 {
837 int len = req_len;
838 struct mbuf *m;
839 int count;
840
841 if ((m = mp) == NULL)
842 return;
843 if (len >= 0) {
844 /*
845 * Trim from head.
846 */
847 while (m != NULL && len > 0) {
848 if (m->m_len <= len) {
849 len -= m->m_len;
850 m->m_len = 0;
851 m = m->m_next;
852 } else {
853 m->m_len -= len;
854 m->m_data += len;
855 len = 0;
856 }
857 }
858 m = mp;
859 if (mp->m_flags & M_PKTHDR)
860 m->m_pkthdr.len -= (req_len - len);
861 } else {
862 /*
863 * Trim from tail. Scan the mbuf chain,
864 * calculating its length and finding the last mbuf.
865 * If the adjustment only affects this mbuf, then just
866 * adjust and return. Otherwise, rescan and truncate
867 * after the remaining size.
868 */
869 len = -len;
870 count = 0;
871 for (;;) {
872 count += m->m_len;
873 if (m->m_next == (struct mbuf *)0)
874 break;
875 m = m->m_next;
876 }
877 if (m->m_len >= len) {
878 m->m_len -= len;
879 if (mp->m_flags & M_PKTHDR)
880 mp->m_pkthdr.len -= len;
881 return;
882 }
883 count -= len;
884 if (count < 0)
885 count = 0;
886 /*
887 * Correct length for chain is "count".
888 * Find the mbuf with last data, adjust its length,
889 * and toss data from remaining mbufs on chain.
890 */
891 m = mp;
892 if (m->m_flags & M_PKTHDR)
893 m->m_pkthdr.len = count;
894 for (; m; m = m->m_next) {
895 if (m->m_len >= count) {
896 m->m_len = count;
897 break;
898 }
899 count -= m->m_len;
900 }
901 if (m)
902 while (m->m_next)
903 (m = m->m_next)->m_len = 0;
904 }
905 }
906
907 /*
908 * Rearrange an mbuf chain so that len bytes are contiguous
909 * and in the data area of an mbuf (so that mtod and dtom
910 * will work for a structure of size len). Returns the resulting
911 * mbuf chain on success, frees it and returns null on failure.
912 * If there is room, it will add up to max_protohdr-len extra bytes to the
913 * contiguous region in an attempt to avoid being called next time.
914 */
915 int MPFail;
916
917 struct mbuf *
918 m_pullup(struct mbuf *n, int len)
919 {
920 struct mbuf *m;
921 int count;
922 int space;
923
924 /*
925 * If first mbuf has no cluster, and has room for len bytes
926 * without shifting current data, pullup into it,
927 * otherwise allocate a new mbuf to prepend to the chain.
928 */
929 if ((n->m_flags & M_EXT) == 0 &&
930 n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
931 if (n->m_len >= len)
932 return (n);
933 m = n;
934 n = n->m_next;
935 len -= m->m_len;
936 } else {
937 if (len > MHLEN)
938 goto bad;
939 MGET(m, M_DONTWAIT, n->m_type);
940 if (m == 0)
941 goto bad;
942 MCLAIM(m, n->m_owner);
943 m->m_len = 0;
944 if (n->m_flags & M_PKTHDR) {
945 M_MOVE_PKTHDR(m, n);
946 }
947 }
948 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
949 do {
950 count = min(min(max(len, max_protohdr), space), n->m_len);
951 memcpy(mtod(m, char *) + m->m_len, mtod(n, void *),
952 (unsigned)count);
953 len -= count;
954 m->m_len += count;
955 n->m_len -= count;
956 space -= count;
957 if (n->m_len)
958 n->m_data += count;
959 else
960 n = m_free(n);
961 } while (len > 0 && n);
962 if (len > 0) {
963 (void) m_free(m);
964 goto bad;
965 }
966 m->m_next = n;
967 return (m);
968 bad:
969 m_freem(n);
970 MPFail++;
971 return (NULL);
972 }
973
974 /*
975 * Like m_pullup(), except a new mbuf is always allocated, and we allow
976 * the amount of empty space before the data in the new mbuf to be specified
977 * (in the event that the caller expects to prepend later).
978 */
979 int MSFail;
980
981 struct mbuf *
982 m_copyup(struct mbuf *n, int len, int dstoff)
983 {
984 struct mbuf *m;
985 int count, space;
986
987 if (len > (MHLEN - dstoff))
988 goto bad;
989 MGET(m, M_DONTWAIT, n->m_type);
990 if (m == NULL)
991 goto bad;
992 MCLAIM(m, n->m_owner);
993 m->m_len = 0;
994 if (n->m_flags & M_PKTHDR) {
995 M_MOVE_PKTHDR(m, n);
996 }
997 m->m_data += dstoff;
998 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
999 do {
1000 count = min(min(max(len, max_protohdr), space), n->m_len);
1001 memcpy(mtod(m, char *) + m->m_len, mtod(n, void *),
1002 (unsigned)count);
1003 len -= count;
1004 m->m_len += count;
1005 n->m_len -= count;
1006 space -= count;
1007 if (n->m_len)
1008 n->m_data += count;
1009 else
1010 n = m_free(n);
1011 } while (len > 0 && n);
1012 if (len > 0) {
1013 (void) m_free(m);
1014 goto bad;
1015 }
1016 m->m_next = n;
1017 return (m);
1018 bad:
1019 m_freem(n);
1020 MSFail++;
1021 return (NULL);
1022 }
1023
1024 /*
1025 * Partition an mbuf chain in two pieces, returning the tail --
1026 * all but the first len0 bytes. In case of failure, it returns NULL and
1027 * attempts to restore the chain to its original state.
1028 */
1029 struct mbuf *
1030 m_split(struct mbuf *m0, int len0, int wait)
1031 {
1032
1033 return m_split0(m0, len0, wait, 1);
1034 }
1035
1036 static struct mbuf *
1037 m_split0(struct mbuf *m0, int len0, int wait, int copyhdr)
1038 {
1039 struct mbuf *m, *n;
1040 unsigned len = len0, remain, len_save;
1041
1042 for (m = m0; m && len > m->m_len; m = m->m_next)
1043 len -= m->m_len;
1044 if (m == 0)
1045 return (NULL);
1046 remain = m->m_len - len;
1047 if (copyhdr && (m0->m_flags & M_PKTHDR)) {
1048 MGETHDR(n, wait, m0->m_type);
1049 if (n == 0)
1050 return (NULL);
1051 MCLAIM(n, m0->m_owner);
1052 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
1053 n->m_pkthdr.len = m0->m_pkthdr.len - len0;
1054 len_save = m0->m_pkthdr.len;
1055 m0->m_pkthdr.len = len0;
1056 if (m->m_flags & M_EXT)
1057 goto extpacket;
1058 if (remain > MHLEN) {
1059 /* m can't be the lead packet */
1060 MH_ALIGN(n, 0);
1061 n->m_len = 0;
1062 n->m_next = m_split(m, len, wait);
1063 if (n->m_next == 0) {
1064 (void) m_free(n);
1065 m0->m_pkthdr.len = len_save;
1066 return (NULL);
1067 } else
1068 return (n);
1069 } else
1070 MH_ALIGN(n, remain);
1071 } else if (remain == 0) {
1072 n = m->m_next;
1073 m->m_next = 0;
1074 return (n);
1075 } else {
1076 MGET(n, wait, m->m_type);
1077 if (n == 0)
1078 return (NULL);
1079 MCLAIM(n, m->m_owner);
1080 M_ALIGN(n, remain);
1081 }
1082 extpacket:
1083 if (m->m_flags & M_EXT) {
1084 n->m_data = m->m_data + len;
1085 MCLADDREFERENCE(m, n);
1086 } else {
1087 memcpy(mtod(n, void *), mtod(m, char *) + len, remain);
1088 }
1089 n->m_len = remain;
1090 m->m_len = len;
1091 n->m_next = m->m_next;
1092 m->m_next = 0;
1093 return (n);
1094 }
1095 /*
1096 * Routine to copy from device local memory into mbufs.
1097 */
1098 struct mbuf *
1099 m_devget(char *buf, int totlen, int off0, struct ifnet *ifp,
1100 void (*copy)(const void *from, void *to, size_t len))
1101 {
1102 struct mbuf *m;
1103 struct mbuf *top = 0, **mp = ⊤
1104 int off = off0, len;
1105 char *cp;
1106 char *epkt;
1107
1108 cp = buf;
1109 epkt = cp + totlen;
1110 if (off) {
1111 /*
1112 * If 'off' is non-zero, packet is trailer-encapsulated,
1113 * so we have to skip the type and length fields.
1114 */
1115 cp += off + 2 * sizeof(uint16_t);
1116 totlen -= 2 * sizeof(uint16_t);
1117 }
1118 MGETHDR(m, M_DONTWAIT, MT_DATA);
1119 if (m == 0)
1120 return (NULL);
1121 m->m_pkthdr.rcvif = ifp;
1122 m->m_pkthdr.len = totlen;
1123 m->m_len = MHLEN;
1124
1125 while (totlen > 0) {
1126 if (top) {
1127 MGET(m, M_DONTWAIT, MT_DATA);
1128 if (m == 0) {
1129 m_freem(top);
1130 return (NULL);
1131 }
1132 m->m_len = MLEN;
1133 }
1134 len = min(totlen, epkt - cp);
1135 if (len >= MINCLSIZE) {
1136 MCLGET(m, M_DONTWAIT);
1137 if ((m->m_flags & M_EXT) == 0) {
1138 m_free(m);
1139 m_freem(top);
1140 return (NULL);
1141 }
1142 m->m_len = len = min(len, MCLBYTES);
1143 } else {
1144 /*
1145 * Place initial small packet/header at end of mbuf.
1146 */
1147 if (len < m->m_len) {
1148 if (top == 0 && len + max_linkhdr <= m->m_len)
1149 m->m_data += max_linkhdr;
1150 m->m_len = len;
1151 } else
1152 len = m->m_len;
1153 }
1154 if (copy)
1155 copy(cp, mtod(m, void *), (size_t)len);
1156 else
1157 memcpy(mtod(m, void *), cp, (size_t)len);
1158 cp += len;
1159 *mp = m;
1160 mp = &m->m_next;
1161 totlen -= len;
1162 if (cp == epkt)
1163 cp = buf;
1164 }
1165 return (top);
1166 }
1167
1168 /*
1169 * Copy data from a buffer back into the indicated mbuf chain,
1170 * starting "off" bytes from the beginning, extending the mbuf
1171 * chain if necessary.
1172 */
1173 void
1174 m_copyback(struct mbuf *m0, int off, int len, const void *cp)
1175 {
1176 #if defined(DEBUG)
1177 struct mbuf *origm = m0;
1178 int error;
1179 #endif /* defined(DEBUG) */
1180
1181 if (m0 == NULL)
1182 return;
1183
1184 #if defined(DEBUG)
1185 error =
1186 #endif /* defined(DEBUG) */
1187 m_copyback0(&m0, off, len, cp,
1188 M_COPYBACK0_COPYBACK|M_COPYBACK0_EXTEND, M_DONTWAIT);
1189
1190 #if defined(DEBUG)
1191 if (error != 0 || (m0 != NULL && origm != m0))
1192 panic("m_copyback");
1193 #endif /* defined(DEBUG) */
1194 }
1195
1196 struct mbuf *
1197 m_copyback_cow(struct mbuf *m0, int off, int len, const void *cp, int how)
1198 {
1199 int error;
1200
1201 /* don't support chain expansion */
1202 KDASSERT(off + len <= m_length(m0));
1203
1204 error = m_copyback0(&m0, off, len, cp,
1205 M_COPYBACK0_COPYBACK|M_COPYBACK0_COW, how);
1206 if (error) {
1207 /*
1208 * no way to recover from partial success.
1209 * just free the chain.
1210 */
1211 m_freem(m0);
1212 return NULL;
1213 }
1214 return m0;
1215 }
1216
1217 /*
1218 * m_makewritable: ensure the specified range writable.
1219 */
1220 int
1221 m_makewritable(struct mbuf **mp, int off, int len, int how)
1222 {
1223 int error;
1224 #if defined(DEBUG)
1225 struct mbuf *n;
1226 int origlen, reslen;
1227
1228 origlen = m_length(*mp);
1229 #endif /* defined(DEBUG) */
1230
1231 #if 0 /* M_COPYALL is large enough */
1232 if (len == M_COPYALL)
1233 len = m_length(*mp) - off; /* XXX */
1234 #endif
1235
1236 error = m_copyback0(mp, off, len, NULL,
1237 M_COPYBACK0_PRESERVE|M_COPYBACK0_COW, how);
1238
1239 #if defined(DEBUG)
1240 reslen = 0;
1241 for (n = *mp; n; n = n->m_next)
1242 reslen += n->m_len;
1243 if (origlen != reslen)
1244 panic("m_makewritable: length changed");
1245 if (((*mp)->m_flags & M_PKTHDR) != 0 && reslen != (*mp)->m_pkthdr.len)
1246 panic("m_makewritable: inconsist");
1247 #endif /* defined(DEBUG) */
1248
1249 return error;
1250 }
1251
1252 int
1253 m_copyback0(struct mbuf **mp0, int off, int len, const void *vp, int flags,
1254 int how)
1255 {
1256 int mlen;
1257 struct mbuf *m, *n;
1258 struct mbuf **mp;
1259 int totlen = 0;
1260 const char *cp = vp;
1261
1262 KASSERT(mp0 != NULL);
1263 KASSERT(*mp0 != NULL);
1264 KASSERT((flags & M_COPYBACK0_PRESERVE) == 0 || cp == NULL);
1265 KASSERT((flags & M_COPYBACK0_COPYBACK) == 0 || cp != NULL);
1266
1267 /*
1268 * we don't bother to update "totlen" in the case of M_COPYBACK0_COW,
1269 * assuming that M_COPYBACK0_EXTEND and M_COPYBACK0_COW are exclusive.
1270 */
1271
1272 KASSERT((~flags & (M_COPYBACK0_EXTEND|M_COPYBACK0_COW)) != 0);
1273
1274 mp = mp0;
1275 m = *mp;
1276 while (off > (mlen = m->m_len)) {
1277 off -= mlen;
1278 totlen += mlen;
1279 if (m->m_next == NULL) {
1280 int tspace;
1281 extend:
1282 if ((flags & M_COPYBACK0_EXTEND) == 0)
1283 goto out;
1284
1285 /*
1286 * try to make some space at the end of "m".
1287 */
1288
1289 mlen = m->m_len;
1290 if (off + len >= MINCLSIZE &&
1291 (m->m_flags & M_EXT) == 0 && m->m_len == 0) {
1292 MCLGET(m, how);
1293 }
1294 tspace = M_TRAILINGSPACE(m);
1295 if (tspace > 0) {
1296 tspace = min(tspace, off + len);
1297 KASSERT(tspace > 0);
1298 memset(mtod(m, char *) + m->m_len, 0,
1299 min(off, tspace));
1300 m->m_len += tspace;
1301 off += mlen;
1302 totlen -= mlen;
1303 continue;
1304 }
1305
1306 /*
1307 * need to allocate an mbuf.
1308 */
1309
1310 if (off + len >= MINCLSIZE) {
1311 n = m_getcl(how, m->m_type, 0);
1312 } else {
1313 n = m_get(how, m->m_type);
1314 }
1315 if (n == NULL) {
1316 goto out;
1317 }
1318 n->m_len = 0;
1319 n->m_len = min(M_TRAILINGSPACE(n), off + len);
1320 memset(mtod(n, char *), 0, min(n->m_len, off));
1321 m->m_next = n;
1322 }
1323 mp = &m->m_next;
1324 m = m->m_next;
1325 }
1326 while (len > 0) {
1327 mlen = m->m_len - off;
1328 if (mlen != 0 && M_READONLY(m)) {
1329 char *datap;
1330 int eatlen;
1331
1332 /*
1333 * this mbuf is read-only.
1334 * allocate a new writable mbuf and try again.
1335 */
1336
1337 #if defined(DIAGNOSTIC)
1338 if ((flags & M_COPYBACK0_COW) == 0)
1339 panic("m_copyback0: read-only");
1340 #endif /* defined(DIAGNOSTIC) */
1341
1342 /*
1343 * if we're going to write into the middle of
1344 * a mbuf, split it first.
1345 */
1346 if (off > 0) {
1347 n = m_split0(m, off, how, 0);
1348 if (n == NULL)
1349 goto enobufs;
1350 m->m_next = n;
1351 mp = &m->m_next;
1352 m = n;
1353 off = 0;
1354 continue;
1355 }
1356
1357 /*
1358 * XXX TODO coalesce into the trailingspace of
1359 * the previous mbuf when possible.
1360 */
1361
1362 /*
1363 * allocate a new mbuf. copy packet header if needed.
1364 */
1365 MGET(n, how, m->m_type);
1366 if (n == NULL)
1367 goto enobufs;
1368 MCLAIM(n, m->m_owner);
1369 if (off == 0 && (m->m_flags & M_PKTHDR) != 0) {
1370 M_MOVE_PKTHDR(n, m);
1371 n->m_len = MHLEN;
1372 } else {
1373 if (len >= MINCLSIZE)
1374 MCLGET(n, M_DONTWAIT);
1375 n->m_len =
1376 (n->m_flags & M_EXT) ? MCLBYTES : MLEN;
1377 }
1378 if (n->m_len > len)
1379 n->m_len = len;
1380
1381 /*
1382 * free the region which has been overwritten.
1383 * copying data from old mbufs if requested.
1384 */
1385 if (flags & M_COPYBACK0_PRESERVE)
1386 datap = mtod(n, char *);
1387 else
1388 datap = NULL;
1389 eatlen = n->m_len;
1390 while (m != NULL && M_READONLY(m) &&
1391 n->m_type == m->m_type && eatlen > 0) {
1392 mlen = min(eatlen, m->m_len);
1393 if (datap) {
1394 m_copydata(m, 0, mlen, datap);
1395 datap += mlen;
1396 }
1397 m->m_data += mlen;
1398 m->m_len -= mlen;
1399 eatlen -= mlen;
1400 if (m->m_len == 0)
1401 *mp = m = m_free(m);
1402 }
1403 if (eatlen > 0)
1404 n->m_len -= eatlen;
1405 n->m_next = m;
1406 *mp = m = n;
1407 continue;
1408 }
1409 mlen = min(mlen, len);
1410 if (flags & M_COPYBACK0_COPYBACK) {
1411 memcpy(mtod(m, char *) + off, cp, (unsigned)mlen);
1412 cp += mlen;
1413 }
1414 len -= mlen;
1415 mlen += off;
1416 off = 0;
1417 totlen += mlen;
1418 if (len == 0)
1419 break;
1420 if (m->m_next == NULL) {
1421 goto extend;
1422 }
1423 mp = &m->m_next;
1424 m = m->m_next;
1425 }
1426 out: if (((m = *mp0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) {
1427 KASSERT((flags & M_COPYBACK0_EXTEND) != 0);
1428 m->m_pkthdr.len = totlen;
1429 }
1430
1431 return 0;
1432
1433 enobufs:
1434 return ENOBUFS;
1435 }
1436
1437 void
1438 m_move_pkthdr(struct mbuf *to, struct mbuf *from)
1439 {
1440
1441 KASSERT((to->m_flags & M_EXT) == 0);
1442 KASSERT((to->m_flags & M_PKTHDR) == 0 || m_tag_first(to) == NULL);
1443 KASSERT((from->m_flags & M_PKTHDR) != 0);
1444
1445 to->m_pkthdr = from->m_pkthdr;
1446 to->m_flags = from->m_flags & M_COPYFLAGS;
1447 to->m_data = to->m_pktdat;
1448
1449 from->m_flags &= ~M_PKTHDR;
1450 }
1451
1452 /*
1453 * Apply function f to the data in an mbuf chain starting "off" bytes from the
1454 * beginning, continuing for "len" bytes.
1455 */
1456 int
1457 m_apply(struct mbuf *m, int off, int len,
1458 int (*f)(void *, void *, unsigned int), void *arg)
1459 {
1460 unsigned int count;
1461 int rval;
1462
1463 KASSERT(len >= 0);
1464 KASSERT(off >= 0);
1465
1466 while (off > 0) {
1467 KASSERT(m != NULL);
1468 if (off < m->m_len)
1469 break;
1470 off -= m->m_len;
1471 m = m->m_next;
1472 }
1473 while (len > 0) {
1474 KASSERT(m != NULL);
1475 count = min(m->m_len - off, len);
1476
1477 rval = (*f)(arg, mtod(m, char *) + off, count);
1478 if (rval)
1479 return (rval);
1480
1481 len -= count;
1482 off = 0;
1483 m = m->m_next;
1484 }
1485
1486 return (0);
1487 }
1488
1489 /*
1490 * Return a pointer to mbuf/offset of location in mbuf chain.
1491 */
1492 struct mbuf *
1493 m_getptr(struct mbuf *m, int loc, int *off)
1494 {
1495
1496 while (loc >= 0) {
1497 /* Normal end of search */
1498 if (m->m_len > loc) {
1499 *off = loc;
1500 return (m);
1501 } else {
1502 loc -= m->m_len;
1503
1504 if (m->m_next == NULL) {
1505 if (loc == 0) {
1506 /* Point at the end of valid data */
1507 *off = m->m_len;
1508 return (m);
1509 } else
1510 return (NULL);
1511 } else
1512 m = m->m_next;
1513 }
1514 }
1515
1516 return (NULL);
1517 }
1518
1519 /*
1520 * m_ext_free: release a reference to the mbuf external storage.
1521 *
1522 * => free the mbuf m itsself as well.
1523 */
1524
1525 void
1526 m_ext_free(struct mbuf *m)
1527 {
1528 bool embedded = MEXT_ISEMBEDDED(m);
1529 bool dofree = true;
1530 u_int refcnt;
1531
1532 KASSERT((m->m_flags & M_EXT) != 0);
1533 KASSERT(MEXT_ISEMBEDDED(m->m_ext_ref));
1534 KASSERT((m->m_ext_ref->m_flags & M_EXT) != 0);
1535 KASSERT((m->m_flags & M_EXT_CLUSTER) ==
1536 (m->m_ext_ref->m_flags & M_EXT_CLUSTER));
1537
1538 if (__predict_true(m->m_ext.ext_refcnt == 1)) {
1539 refcnt = m->m_ext.ext_refcnt = 0;
1540 } else {
1541 refcnt = atomic_dec_uint_nv(&m->m_ext.ext_refcnt);
1542 }
1543 if (refcnt > 0) {
1544 if (embedded) {
1545 /*
1546 * other mbuf's m_ext_ref still points to us.
1547 */
1548 dofree = false;
1549 } else {
1550 m->m_ext_ref = m;
1551 }
1552 } else {
1553 /*
1554 * dropping the last reference
1555 */
1556 if (!embedded) {
1557 m->m_ext.ext_refcnt++; /* XXX */
1558 m_ext_free(m->m_ext_ref);
1559 m->m_ext_ref = m;
1560 } else if ((m->m_flags & M_EXT_CLUSTER) != 0) {
1561 pool_cache_put_paddr((struct pool_cache *)
1562 m->m_ext.ext_arg,
1563 m->m_ext.ext_buf, m->m_ext.ext_paddr);
1564 } else if (m->m_ext.ext_free) {
1565 (*m->m_ext.ext_free)(m,
1566 m->m_ext.ext_buf, m->m_ext.ext_size,
1567 m->m_ext.ext_arg);
1568 /*
1569 * 'm' is already freed by the ext_free callback.
1570 */
1571 dofree = false;
1572 } else {
1573 free(m->m_ext.ext_buf, m->m_ext.ext_type);
1574 }
1575 }
1576 if (dofree) {
1577 pool_cache_put(mb_cache, m);
1578 }
1579 }
1580
1581 #if defined(DDB)
1582 void
1583 m_print(const struct mbuf *m, const char *modif, void (*pr)(const char *, ...))
1584 {
1585 char ch;
1586 bool opt_c = false;
1587 char buf[512];
1588
1589 while ((ch = *(modif++)) != '\0') {
1590 switch (ch) {
1591 case 'c':
1592 opt_c = true;
1593 break;
1594 }
1595 }
1596
1597 nextchain:
1598 (*pr)("MBUF %p\n", m);
1599 snprintb(buf, sizeof(buf), M_FLAGS_BITS, (u_int)m->m_flags);
1600 (*pr)(" data=%p, len=%d, type=%d, flags=%s\n",
1601 m->m_data, m->m_len, m->m_type, buf);
1602 (*pr)(" owner=%p, next=%p, nextpkt=%p\n", m->m_owner, m->m_next,
1603 m->m_nextpkt);
1604 (*pr)(" leadingspace=%u, trailingspace=%u, readonly=%u\n",
1605 (int)M_LEADINGSPACE(m), (int)M_TRAILINGSPACE(m),
1606 (int)M_READONLY(m));
1607 if ((m->m_flags & M_PKTHDR) != 0) {
1608 snprintb(buf, sizeof(buf), M_CSUM_BITS, m->m_pkthdr.csum_flags);
1609 (*pr)(" pktlen=%d, rcvif=%p, csum_flags=0x%s, csum_data=0x%"
1610 PRIx32 ", segsz=%u\n",
1611 m->m_pkthdr.len, m->m_pkthdr.rcvif,
1612 buf, m->m_pkthdr.csum_data, m->m_pkthdr.segsz);
1613 }
1614 if ((m->m_flags & M_EXT)) {
1615 (*pr)(" ext_refcnt=%u, ext_buf=%p, ext_size=%zd, "
1616 "ext_free=%p, ext_arg=%p\n",
1617 m->m_ext.ext_refcnt,
1618 m->m_ext.ext_buf, m->m_ext.ext_size,
1619 m->m_ext.ext_free, m->m_ext.ext_arg);
1620 }
1621 if ((~m->m_flags & (M_EXT|M_EXT_PAGES)) == 0) {
1622 vaddr_t sva = (vaddr_t)m->m_ext.ext_buf;
1623 vaddr_t eva = sva + m->m_ext.ext_size;
1624 int n = (round_page(eva) - trunc_page(sva)) >> PAGE_SHIFT;
1625 int i;
1626
1627 (*pr)(" pages:");
1628 for (i = 0; i < n; i ++) {
1629 (*pr)(" %p", m->m_ext.ext_pgs[i]);
1630 }
1631 (*pr)("\n");
1632 }
1633
1634 if (opt_c) {
1635 m = m->m_next;
1636 if (m != NULL) {
1637 goto nextchain;
1638 }
1639 }
1640 }
1641 #endif /* defined(DDB) */
1642
1643 void
1644 mbstat_type_add(int type, int diff)
1645 {
1646 struct mbstat_cpu *mb;
1647 int s;
1648
1649 s = splvm();
1650 mb = percpu_getref(mbstat_percpu);
1651 mb->m_mtypes[type] += diff;
1652 percpu_putref(mbstat_percpu);
1653 splx(s);
1654 }
1655
1656 #if defined(MBUFTRACE)
1657 void
1658 mowner_attach(struct mowner *mo)
1659 {
1660
1661 KASSERT(mo->mo_counters == NULL);
1662 mo->mo_counters = percpu_alloc(sizeof(struct mowner_counter));
1663
1664 /* XXX lock */
1665 LIST_INSERT_HEAD(&mowners, mo, mo_link);
1666 }
1667
1668 void
1669 mowner_detach(struct mowner *mo)
1670 {
1671
1672 KASSERT(mo->mo_counters != NULL);
1673
1674 /* XXX lock */
1675 LIST_REMOVE(mo, mo_link);
1676
1677 percpu_free(mo->mo_counters, sizeof(struct mowner_counter));
1678 mo->mo_counters = NULL;
1679 }
1680
1681 void
1682 mowner_init(struct mbuf *m, int type)
1683 {
1684 struct mowner_counter *mc;
1685 struct mowner *mo;
1686 int s;
1687
1688 m->m_owner = mo = &unknown_mowners[type];
1689 s = splvm();
1690 mc = percpu_getref(mo->mo_counters);
1691 mc->mc_counter[MOWNER_COUNTER_CLAIMS]++;
1692 percpu_putref(mo->mo_counters);
1693 splx(s);
1694 }
1695
1696 void
1697 mowner_ref(struct mbuf *m, int flags)
1698 {
1699 struct mowner *mo = m->m_owner;
1700 struct mowner_counter *mc;
1701 int s;
1702
1703 s = splvm();
1704 mc = percpu_getref(mo->mo_counters);
1705 if ((flags & M_EXT) != 0)
1706 mc->mc_counter[MOWNER_COUNTER_EXT_CLAIMS]++;
1707 if ((flags & M_CLUSTER) != 0)
1708 mc->mc_counter[MOWNER_COUNTER_CLUSTER_CLAIMS]++;
1709 percpu_putref(mo->mo_counters);
1710 splx(s);
1711 }
1712
1713 void
1714 mowner_revoke(struct mbuf *m, bool all, int flags)
1715 {
1716 struct mowner *mo = m->m_owner;
1717 struct mowner_counter *mc;
1718 int s;
1719
1720 s = splvm();
1721 mc = percpu_getref(mo->mo_counters);
1722 if ((flags & M_EXT) != 0)
1723 mc->mc_counter[MOWNER_COUNTER_EXT_RELEASES]++;
1724 if ((flags & M_CLUSTER) != 0)
1725 mc->mc_counter[MOWNER_COUNTER_CLUSTER_RELEASES]++;
1726 if (all)
1727 mc->mc_counter[MOWNER_COUNTER_RELEASES]++;
1728 percpu_putref(mo->mo_counters);
1729 splx(s);
1730 if (all)
1731 m->m_owner = &revoked_mowner;
1732 }
1733
1734 static void
1735 mowner_claim(struct mbuf *m, struct mowner *mo)
1736 {
1737 struct mowner_counter *mc;
1738 int flags = m->m_flags;
1739 int s;
1740
1741 s = splvm();
1742 mc = percpu_getref(mo->mo_counters);
1743 mc->mc_counter[MOWNER_COUNTER_CLAIMS]++;
1744 if ((flags & M_EXT) != 0)
1745 mc->mc_counter[MOWNER_COUNTER_EXT_CLAIMS]++;
1746 if ((flags & M_CLUSTER) != 0)
1747 mc->mc_counter[MOWNER_COUNTER_CLUSTER_CLAIMS]++;
1748 percpu_putref(mo->mo_counters);
1749 splx(s);
1750 m->m_owner = mo;
1751 }
1752
1753 void
1754 m_claim(struct mbuf *m, struct mowner *mo)
1755 {
1756
1757 if (m->m_owner == mo || mo == NULL)
1758 return;
1759
1760 mowner_revoke(m, true, m->m_flags);
1761 mowner_claim(m, mo);
1762 }
1763 #endif /* defined(MBUFTRACE) */
1764