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