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