Home | History | Annotate | Line # | Download | only in kern
uipc_mbuf.c revision 1.155
      1 /*	$NetBSD: uipc_mbuf.c,v 1.155 2013/11/14 09:21:30 skrll 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.155 2013/11/14 09:21:30 skrll 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, "kern", NULL,
    393 		       NULL, 0, NULL, 0,
    394 		       CTL_KERN, CTL_EOL);
    395 	sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
    396 		       CTLFLAG_PERMANENT,
    397 		       CTLTYPE_NODE, "mbuf",
    398 		       SYSCTL_DESCR("mbuf control variables"),
    399 		       NULL, 0, NULL, 0,
    400 		       CTL_KERN, KERN_MBUF, CTL_EOL);
    401 
    402 	sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
    403 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    404 		       CTLTYPE_INT, "msize",
    405 		       SYSCTL_DESCR("mbuf base size"),
    406 		       NULL, msize, NULL, 0,
    407 		       CTL_KERN, KERN_MBUF, MBUF_MSIZE, CTL_EOL);
    408 	sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
    409 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    410 		       CTLTYPE_INT, "mclbytes",
    411 		       SYSCTL_DESCR("mbuf cluster size"),
    412 		       NULL, mclbytes, NULL, 0,
    413 		       CTL_KERN, KERN_MBUF, MBUF_MCLBYTES, CTL_EOL);
    414 	sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
    415 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    416 		       CTLTYPE_INT, "nmbclusters",
    417 		       SYSCTL_DESCR("Limit on the number of mbuf clusters"),
    418 		       sysctl_kern_mbuf, 0, &nmbclusters, 0,
    419 		       CTL_KERN, KERN_MBUF, MBUF_NMBCLUSTERS, CTL_EOL);
    420 	sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
    421 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    422 		       CTLTYPE_INT, "mblowat",
    423 		       SYSCTL_DESCR("mbuf low water mark"),
    424 		       sysctl_kern_mbuf, 0, &mblowat, 0,
    425 		       CTL_KERN, KERN_MBUF, MBUF_MBLOWAT, CTL_EOL);
    426 	sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
    427 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    428 		       CTLTYPE_INT, "mcllowat",
    429 		       SYSCTL_DESCR("mbuf cluster low water mark"),
    430 		       sysctl_kern_mbuf, 0, &mcllowat, 0,
    431 		       CTL_KERN, KERN_MBUF, MBUF_MCLLOWAT, CTL_EOL);
    432 	sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
    433 		       CTLFLAG_PERMANENT,
    434 		       CTLTYPE_STRUCT, "stats",
    435 		       SYSCTL_DESCR("mbuf allocation statistics"),
    436 		       sysctl_kern_mbuf_stats, 0, NULL, 0,
    437 		       CTL_KERN, KERN_MBUF, MBUF_STATS, CTL_EOL);
    438 #ifdef MBUFTRACE
    439 	sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
    440 		       CTLFLAG_PERMANENT,
    441 		       CTLTYPE_STRUCT, "mowners",
    442 		       SYSCTL_DESCR("Information about mbuf owners"),
    443 		       sysctl_kern_mbuf_mowners, 0, NULL, 0,
    444 		       CTL_KERN, KERN_MBUF, MBUF_MOWNERS, CTL_EOL);
    445 #endif /* MBUFTRACE */
    446 }
    447 
    448 static int
    449 mb_ctor(void *arg, void *object, int flags)
    450 {
    451 	struct mbuf *m = object;
    452 
    453 #ifdef POOL_VTOPHYS
    454 	m->m_paddr = POOL_VTOPHYS(m);
    455 #else
    456 	m->m_paddr = M_PADDR_INVALID;
    457 #endif
    458 	return (0);
    459 }
    460 
    461 /*
    462  * Add mbuf to the end of a chain
    463  */
    464 struct mbuf *
    465 m_add(struct mbuf *c, struct mbuf *m) {
    466 	struct mbuf *n;
    467 
    468 	if (c == NULL)
    469 		return m;
    470 
    471 	for (n = c; n->m_next != NULL; n = n->m_next)
    472 		continue;
    473 	n->m_next = m;
    474 	return c;
    475 }
    476 
    477 /*
    478  * Set the m_data pointer of a newly-allocated mbuf
    479  * to place an object of the specified size at the
    480  * end of the mbuf, longword aligned.
    481  */
    482 void
    483 m_align(struct mbuf *m, int len)
    484 {
    485        int adjust;
    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 	for (m = m0; m->m_next != NULL; m = m->m_next)
    511 		continue;
    512 	remainder = len;
    513 	space = M_TRAILINGSPACE(m);
    514 	if (space > 0) {
    515 		/*
    516 		 * Copy into available space.
    517 		 */
    518 		if (space > remainder)
    519 			space = remainder;
    520 		memmove(mtod(m, char *) + m->m_len, cp, space);
    521 		m->m_len += space;
    522 		cp = cp + space, remainder -= space;
    523 	}
    524 	while (remainder > 0) {
    525 		/*
    526 		 * Allocate a new mbuf; could check space
    527 		 * and allocate a cluster instead.
    528 		 */
    529 		n = m_get(M_DONTWAIT, m->m_type);
    530 		if (n == NULL)
    531 			break;
    532 		n->m_len = min(MLEN, remainder);
    533 		memmove(mtod(n, void *), cp, n->m_len);
    534 		cp += n->m_len, remainder -= n->m_len;
    535 		m->m_next = n;
    536 		m = n;
    537 	}
    538 	if (m0->m_flags & M_PKTHDR)
    539 		m0->m_pkthdr.len += len - remainder;
    540 	return (remainder == 0);
    541 }
    542 
    543 void
    544 m_reclaim(void *arg, int flags)
    545 {
    546 	struct domain *dp;
    547 	const struct protosw *pr;
    548 	struct ifnet *ifp;
    549 	int s;
    550 
    551 	KERNEL_LOCK(1, NULL);
    552 	s = splvm();
    553 	DOMAIN_FOREACH(dp) {
    554 		for (pr = dp->dom_protosw;
    555 		     pr < dp->dom_protoswNPROTOSW; pr++)
    556 			if (pr->pr_drain)
    557 				(*pr->pr_drain)();
    558 	}
    559 	IFNET_FOREACH(ifp) {
    560 		if (ifp->if_drain)
    561 			(*ifp->if_drain)(ifp);
    562 	}
    563 	splx(s);
    564 	mbstat.m_drain++;
    565 	KERNEL_UNLOCK_ONE(NULL);
    566 }
    567 
    568 /*
    569  * Space allocation routines.
    570  * These are also available as macros
    571  * for critical paths.
    572  */
    573 struct mbuf *
    574 m_get(int nowait, int type)
    575 {
    576 	struct mbuf *m;
    577 
    578 	KASSERT(type != MT_FREE);
    579 
    580 	m = pool_cache_get(mb_cache,
    581 	    nowait == M_WAIT ? PR_WAITOK|PR_LIMITFAIL : 0);
    582 	if (m == NULL)
    583 		return NULL;
    584 
    585 	mbstat_type_add(type, 1);
    586 	mowner_init(m, type);
    587 	m->m_ext_ref = m;
    588 	m->m_type = type;
    589 	m->m_len = 0;
    590 	m->m_next = NULL;
    591 	m->m_nextpkt = NULL;
    592 	m->m_data = m->m_dat;
    593 	m->m_flags = 0;
    594 
    595 	return m;
    596 }
    597 
    598 struct mbuf *
    599 m_gethdr(int nowait, int type)
    600 {
    601 	struct mbuf *m;
    602 
    603 	m = m_get(nowait, type);
    604 	if (m == NULL)
    605 		return NULL;
    606 
    607 	m->m_data = m->m_pktdat;
    608 	m->m_flags = M_PKTHDR;
    609 	m->m_pkthdr.rcvif = NULL;
    610 	m->m_pkthdr.len = 0;
    611 	m->m_pkthdr.csum_flags = 0;
    612 	m->m_pkthdr.csum_data = 0;
    613 	SLIST_INIT(&m->m_pkthdr.tags);
    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 == 0)
    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 struct mbuf *
    638 m_free(struct mbuf *m)
    639 {
    640 	struct mbuf *n;
    641 
    642 	MFREE(m, n);
    643 	return (n);
    644 }
    645 
    646 void
    647 m_freem(struct mbuf *m)
    648 {
    649 	struct mbuf *n;
    650 
    651 	if (m == NULL)
    652 		return;
    653 	do {
    654 		MFREE(m, n);
    655 		m = n;
    656 	} while (m);
    657 }
    658 
    659 #ifdef MBUFTRACE
    660 /*
    661  * Walk a chain of mbufs, claiming ownership of each mbuf in the chain.
    662  */
    663 void
    664 m_claimm(struct mbuf *m, struct mowner *mo)
    665 {
    666 
    667 	for (; m != NULL; m = m->m_next)
    668 		MCLAIM(m, mo);
    669 }
    670 #endif
    671 
    672 /*
    673  * Mbuffer utility routines.
    674  */
    675 
    676 /*
    677  * Lesser-used path for M_PREPEND:
    678  * allocate new mbuf to prepend to chain,
    679  * copy junk along.
    680  */
    681 struct mbuf *
    682 m_prepend(struct mbuf *m, int len, int how)
    683 {
    684 	struct mbuf *mn;
    685 
    686 	mn = m_get(how, m->m_type);
    687 	if (mn == NULL) {
    688 		m_freem(m);
    689 		return (NULL);
    690 	}
    691 	if (m->m_flags & M_PKTHDR) {
    692 		M_MOVE_PKTHDR(mn, m);
    693 	} else {
    694 		MCLAIM(mn, m->m_owner);
    695 	}
    696 	mn->m_next = m;
    697 	m = mn;
    698 	if (len < MHLEN)
    699 		MH_ALIGN(m, len);
    700 	m->m_len = len;
    701 	return (m);
    702 }
    703 
    704 /*
    705  * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
    706  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
    707  * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
    708  */
    709 int MCFail;
    710 
    711 struct mbuf *
    712 m_copym(struct mbuf *m, int off0, int len, int wait)
    713 {
    714 
    715 	return m_copym0(m, off0, len, wait, 0);	/* shallow copy on M_EXT */
    716 }
    717 
    718 struct mbuf *
    719 m_dup(struct mbuf *m, int off0, int len, int wait)
    720 {
    721 
    722 	return m_copym0(m, off0, len, wait, 1);	/* deep copy */
    723 }
    724 
    725 static inline int
    726 m_copylen(int len, int copylen) {
    727     return len == M_COPYALL ? copylen : min(len, copylen);
    728 }
    729 
    730 static struct mbuf *
    731 m_copym0(struct mbuf *m, int off0, int len, int wait, int deep)
    732 {
    733 	struct mbuf *n, **np;
    734 	int off = off0;
    735 	struct mbuf *top;
    736 	int copyhdr = 0;
    737 
    738 	if (off < 0 || (len != M_COPYALL && len < 0))
    739 		panic("m_copym: off %d, len %d", off, len);
    740 	if (off == 0 && m->m_flags & M_PKTHDR)
    741 		copyhdr = 1;
    742 	while (off > 0) {
    743 		if (m == 0)
    744 			panic("m_copym: m == 0, off %d", off);
    745 		if (off < m->m_len)
    746 			break;
    747 		off -= m->m_len;
    748 		m = m->m_next;
    749 	}
    750 	np = &top;
    751 	top = 0;
    752 	while (len == M_COPYALL || len > 0) {
    753 		if (m == 0) {
    754 			if (len != M_COPYALL)
    755 				panic("m_copym: m == 0, len %d [!COPYALL]",
    756 				    len);
    757 			break;
    758 		}
    759 		n = m_get(wait, m->m_type);
    760 		*np = n;
    761 		if (n == 0)
    762 			goto nospace;
    763 		MCLAIM(n, m->m_owner);
    764 		if (copyhdr) {
    765 			M_COPY_PKTHDR(n, m);
    766 			if (len == M_COPYALL)
    767 				n->m_pkthdr.len -= off0;
    768 			else
    769 				n->m_pkthdr.len = len;
    770 			copyhdr = 0;
    771 		}
    772 		n->m_len = m_copylen(len, m->m_len - off);
    773 		if (m->m_flags & M_EXT) {
    774 			if (!deep) {
    775 				n->m_data = m->m_data + off;
    776 				MCLADDREFERENCE(m, n);
    777 			} else {
    778 				/*
    779 				 * we are unsure about the way m was allocated.
    780 				 * copy into multiple MCLBYTES cluster mbufs.
    781 				 */
    782 				MCLGET(n, wait);
    783 				n->m_len = M_TRAILINGSPACE(n);
    784 				n->m_len = m_copylen(len, n->m_len);
    785 				n->m_len = min(n->m_len, m->m_len - off);
    786 				memcpy(mtod(n, void *), mtod(m, char *) + off,
    787 				    (unsigned)n->m_len);
    788 			}
    789 		} else
    790 			memcpy(mtod(n, void *), mtod(m, char *) + off,
    791 			    (unsigned)n->m_len);
    792 		if (len != M_COPYALL)
    793 			len -= n->m_len;
    794 		off += n->m_len;
    795 #ifdef DIAGNOSTIC
    796 		if (off > m->m_len)
    797 			panic("m_copym0 overrun %d %d", off, m->m_len);
    798 #endif
    799 		if (off == m->m_len) {
    800 			m = m->m_next;
    801 			off = 0;
    802 		}
    803 		np = &n->m_next;
    804 	}
    805 	if (top == 0)
    806 		MCFail++;
    807 	return (top);
    808 nospace:
    809 	m_freem(top);
    810 	MCFail++;
    811 	return (NULL);
    812 }
    813 
    814 /*
    815  * Copy an entire packet, including header (which must be present).
    816  * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
    817  */
    818 struct mbuf *
    819 m_copypacket(struct mbuf *m, int how)
    820 {
    821 	struct mbuf *top, *n, *o;
    822 
    823 	n = m_get(how, m->m_type);
    824 	top = n;
    825 	if (!n)
    826 		goto nospace;
    827 
    828 	MCLAIM(n, m->m_owner);
    829 	M_COPY_PKTHDR(n, m);
    830 	n->m_len = m->m_len;
    831 	if (m->m_flags & M_EXT) {
    832 		n->m_data = m->m_data;
    833 		MCLADDREFERENCE(m, n);
    834 	} else {
    835 		memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
    836 	}
    837 
    838 	m = m->m_next;
    839 	while (m) {
    840 		o = m_get(how, m->m_type);
    841 		if (!o)
    842 			goto nospace;
    843 
    844 		MCLAIM(o, m->m_owner);
    845 		n->m_next = o;
    846 		n = n->m_next;
    847 
    848 		n->m_len = m->m_len;
    849 		if (m->m_flags & M_EXT) {
    850 			n->m_data = m->m_data;
    851 			MCLADDREFERENCE(m, n);
    852 		} else {
    853 			memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
    854 		}
    855 
    856 		m = m->m_next;
    857 	}
    858 	return top;
    859 nospace:
    860 	m_freem(top);
    861 	MCFail++;
    862 	return NULL;
    863 }
    864 
    865 /*
    866  * Copy data from an mbuf chain starting "off" bytes from the beginning,
    867  * continuing for "len" bytes, into the indicated buffer.
    868  */
    869 void
    870 m_copydata(struct mbuf *m, int off, int len, void *vp)
    871 {
    872 	unsigned	count;
    873 	void *		cp = vp;
    874 	struct mbuf	*m0 = m;
    875 	int		len0 = len;
    876 	int		off0 = off;
    877 	void		*vp0 = vp;
    878 
    879 	if (off < 0 || len < 0)
    880 		panic("m_copydata: off %d, len %d", off, len);
    881 	while (off > 0) {
    882 		if (m == NULL)
    883 			panic("m_copydata(%p,%d,%d,%p): m=NULL, off=%d (%d)",
    884 			    m0, len0, off0, vp0, off, off0 - off);
    885 		if (off < m->m_len)
    886 			break;
    887 		off -= m->m_len;
    888 		m = m->m_next;
    889 	}
    890 	while (len > 0) {
    891 		if (m == NULL)
    892 			panic("m_copydata(%p,%d,%d,%p): "
    893 			    "m=NULL, off=%d (%d), len=%d (%d)",
    894 			    m0, len0, off0, vp0,
    895 			    off, off0 - off, len, len0 - len);
    896 		count = min(m->m_len - off, len);
    897 		memcpy(cp, mtod(m, char *) + off, count);
    898 		len -= count;
    899 		cp = (char *)cp + count;
    900 		off = 0;
    901 		m = m->m_next;
    902 	}
    903 }
    904 
    905 /*
    906  * Concatenate mbuf chain n to m.
    907  * n might be copied into m (when n->m_len is small), therefore data portion of
    908  * n could be copied into an mbuf of different mbuf type.
    909  * Any m_pkthdr is not updated.
    910  */
    911 void
    912 m_cat(struct mbuf *m, struct mbuf *n)
    913 {
    914 
    915 	while (m->m_next)
    916 		m = m->m_next;
    917 	while (n) {
    918 		if (M_READONLY(m) || n->m_len > M_TRAILINGSPACE(m)) {
    919 			/* just join the two chains */
    920 			m->m_next = n;
    921 			return;
    922 		}
    923 		/* splat the data from one into the other */
    924 		memcpy(mtod(m, char *) + m->m_len, mtod(n, void *),
    925 		    (u_int)n->m_len);
    926 		m->m_len += n->m_len;
    927 		n = m_free(n);
    928 	}
    929 }
    930 
    931 void
    932 m_adj(struct mbuf *mp, int req_len)
    933 {
    934 	int len = req_len;
    935 	struct mbuf *m;
    936 	int count;
    937 
    938 	if ((m = mp) == NULL)
    939 		return;
    940 	if (len >= 0) {
    941 		/*
    942 		 * Trim from head.
    943 		 */
    944 		while (m != NULL && len > 0) {
    945 			if (m->m_len <= len) {
    946 				len -= m->m_len;
    947 				m->m_len = 0;
    948 				m = m->m_next;
    949 			} else {
    950 				m->m_len -= len;
    951 				m->m_data += len;
    952 				len = 0;
    953 			}
    954 		}
    955 		m = mp;
    956 		if (mp->m_flags & M_PKTHDR)
    957 			m->m_pkthdr.len -= (req_len - len);
    958 	} else {
    959 		/*
    960 		 * Trim from tail.  Scan the mbuf chain,
    961 		 * calculating its length and finding the last mbuf.
    962 		 * If the adjustment only affects this mbuf, then just
    963 		 * adjust and return.  Otherwise, rescan and truncate
    964 		 * after the remaining size.
    965 		 */
    966 		len = -len;
    967 		count = 0;
    968 		for (;;) {
    969 			count += m->m_len;
    970 			if (m->m_next == (struct mbuf *)0)
    971 				break;
    972 			m = m->m_next;
    973 		}
    974 		if (m->m_len >= len) {
    975 			m->m_len -= len;
    976 			if (mp->m_flags & M_PKTHDR)
    977 				mp->m_pkthdr.len -= len;
    978 			return;
    979 		}
    980 		count -= len;
    981 		if (count < 0)
    982 			count = 0;
    983 		/*
    984 		 * Correct length for chain is "count".
    985 		 * Find the mbuf with last data, adjust its length,
    986 		 * and toss data from remaining mbufs on chain.
    987 		 */
    988 		m = mp;
    989 		if (m->m_flags & M_PKTHDR)
    990 			m->m_pkthdr.len = count;
    991 		for (; m; m = m->m_next) {
    992 			if (m->m_len >= count) {
    993 				m->m_len = count;
    994 				break;
    995 			}
    996 			count -= m->m_len;
    997 		}
    998 		if (m)
    999 			while (m->m_next)
   1000 				(m = m->m_next)->m_len = 0;
   1001 	}
   1002 }
   1003 
   1004 /*
   1005  * m_ensure_contig: rearrange an mbuf chain that given length of bytes
   1006  * would be contiguous and in the data area of an mbuf (therefore, mtod()
   1007  * would work for a structure of given length).
   1008  *
   1009  * => On success, returns true and the resulting mbuf chain; false otherwise.
   1010  * => The mbuf chain may change, but is always preserved valid.
   1011  */
   1012 bool
   1013 m_ensure_contig(struct mbuf **m0, int len)
   1014 {
   1015 	struct mbuf *n = *m0, *m;
   1016 	size_t count, space;
   1017 
   1018 	/*
   1019 	 * If first mbuf has no cluster, and has room for len bytes
   1020 	 * without shifting current data, pullup into it,
   1021 	 * otherwise allocate a new mbuf to prepend to the chain.
   1022 	 */
   1023 	if ((n->m_flags & M_EXT) == 0 &&
   1024 	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
   1025 		if (n->m_len >= len) {
   1026 			return true;
   1027 		}
   1028 		m = n;
   1029 		n = n->m_next;
   1030 		len -= m->m_len;
   1031 	} else {
   1032 		if (len > MHLEN) {
   1033 			return false;
   1034 		}
   1035 		m = m_get(M_DONTWAIT, n->m_type);
   1036 		if (m == NULL) {
   1037 			return false;
   1038 		}
   1039 		MCLAIM(m, n->m_owner);
   1040 		if (n->m_flags & M_PKTHDR) {
   1041 			M_MOVE_PKTHDR(m, n);
   1042 		}
   1043 	}
   1044 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
   1045 	do {
   1046 		count = MIN(MIN(MAX(len, max_protohdr), space), n->m_len);
   1047 		memcpy(mtod(m, char *) + m->m_len, mtod(n, void *),
   1048 		  (unsigned)count);
   1049 		len -= count;
   1050 		m->m_len += count;
   1051 		n->m_len -= count;
   1052 		space -= count;
   1053 		if (n->m_len)
   1054 			n->m_data += count;
   1055 		else
   1056 			n = m_free(n);
   1057 	} while (len > 0 && n);
   1058 
   1059 	m->m_next = n;
   1060 	*m0 = m;
   1061 
   1062 	return len <= 0;
   1063 }
   1064 
   1065 /*
   1066  * m_pullup: same as m_ensure_contig(), but destroys mbuf chain on error.
   1067  */
   1068 int MPFail;
   1069 
   1070 struct mbuf *
   1071 m_pullup(struct mbuf *n, int len)
   1072 {
   1073 	struct mbuf *m = n;
   1074 
   1075 	if (!m_ensure_contig(&m, len)) {
   1076 		KASSERT(m != NULL);
   1077 		m_freem(m);
   1078 		MPFail++;
   1079 		m = NULL;
   1080 	}
   1081 	return m;
   1082 }
   1083 
   1084 /*
   1085  * Like m_pullup(), except a new mbuf is always allocated, and we allow
   1086  * the amount of empty space before the data in the new mbuf to be specified
   1087  * (in the event that the caller expects to prepend later).
   1088  */
   1089 int MSFail;
   1090 
   1091 struct mbuf *
   1092 m_copyup(struct mbuf *n, int len, int dstoff)
   1093 {
   1094 	struct mbuf *m;
   1095 	int count, space;
   1096 
   1097 	if (len > (MHLEN - dstoff))
   1098 		goto bad;
   1099 	m = m_get(M_DONTWAIT, n->m_type);
   1100 	if (m == NULL)
   1101 		goto bad;
   1102 	MCLAIM(m, n->m_owner);
   1103 	if (n->m_flags & M_PKTHDR) {
   1104 		M_MOVE_PKTHDR(m, n);
   1105 	}
   1106 	m->m_data += dstoff;
   1107 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
   1108 	do {
   1109 		count = min(min(max(len, max_protohdr), space), n->m_len);
   1110 		memcpy(mtod(m, char *) + m->m_len, mtod(n, void *),
   1111 		    (unsigned)count);
   1112 		len -= count;
   1113 		m->m_len += count;
   1114 		n->m_len -= count;
   1115 		space -= count;
   1116 		if (n->m_len)
   1117 			n->m_data += count;
   1118 		else
   1119 			n = m_free(n);
   1120 	} while (len > 0 && n);
   1121 	if (len > 0) {
   1122 		(void) m_free(m);
   1123 		goto bad;
   1124 	}
   1125 	m->m_next = n;
   1126 	return (m);
   1127  bad:
   1128 	m_freem(n);
   1129 	MSFail++;
   1130 	return (NULL);
   1131 }
   1132 
   1133 /*
   1134  * Partition an mbuf chain in two pieces, returning the tail --
   1135  * all but the first len0 bytes.  In case of failure, it returns NULL and
   1136  * attempts to restore the chain to its original state.
   1137  */
   1138 struct mbuf *
   1139 m_split(struct mbuf *m0, int len0, int wait)
   1140 {
   1141 
   1142 	return m_split0(m0, len0, wait, 1);
   1143 }
   1144 
   1145 static struct mbuf *
   1146 m_split0(struct mbuf *m0, int len0, int wait, int copyhdr)
   1147 {
   1148 	struct mbuf *m, *n;
   1149 	unsigned len = len0, remain, len_save;
   1150 
   1151 	for (m = m0; m && len > m->m_len; m = m->m_next)
   1152 		len -= m->m_len;
   1153 	if (m == 0)
   1154 		return (NULL);
   1155 	remain = m->m_len - len;
   1156 	if (copyhdr && (m0->m_flags & M_PKTHDR)) {
   1157 		n = m_gethdr(wait, m0->m_type);
   1158 		if (n == NULL)
   1159 			return NULL;
   1160 		MCLAIM(n, m0->m_owner);
   1161 		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
   1162 		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
   1163 		len_save = m0->m_pkthdr.len;
   1164 		m0->m_pkthdr.len = len0;
   1165 		if (m->m_flags & M_EXT)
   1166 			goto extpacket;
   1167 		if (remain > MHLEN) {
   1168 			/* m can't be the lead packet */
   1169 			MH_ALIGN(n, 0);
   1170 			n->m_len = 0;
   1171 			n->m_next = m_split(m, len, wait);
   1172 			if (n->m_next == 0) {
   1173 				(void) m_free(n);
   1174 				m0->m_pkthdr.len = len_save;
   1175 				return (NULL);
   1176 			} else
   1177 				return (n);
   1178 		} else
   1179 			MH_ALIGN(n, remain);
   1180 	} else if (remain == 0) {
   1181 		n = m->m_next;
   1182 		m->m_next = 0;
   1183 		return (n);
   1184 	} else {
   1185 		n = m_get(wait, m->m_type);
   1186 		if (n == 0)
   1187 			return (NULL);
   1188 		MCLAIM(n, m->m_owner);
   1189 		M_ALIGN(n, remain);
   1190 	}
   1191 extpacket:
   1192 	if (m->m_flags & M_EXT) {
   1193 		n->m_data = m->m_data + len;
   1194 		MCLADDREFERENCE(m, n);
   1195 	} else {
   1196 		memcpy(mtod(n, void *), mtod(m, char *) + len, remain);
   1197 	}
   1198 	n->m_len = remain;
   1199 	m->m_len = len;
   1200 	n->m_next = m->m_next;
   1201 	m->m_next = 0;
   1202 	return (n);
   1203 }
   1204 /*
   1205  * Routine to copy from device local memory into mbufs.
   1206  */
   1207 struct mbuf *
   1208 m_devget(char *buf, int totlen, int off0, struct ifnet *ifp,
   1209     void (*copy)(const void *from, void *to, size_t len))
   1210 {
   1211 	struct mbuf *m;
   1212 	struct mbuf *top = 0, **mp = &top;
   1213 	int off = off0, len;
   1214 	char *cp;
   1215 	char *epkt;
   1216 
   1217 	cp = buf;
   1218 	epkt = cp + totlen;
   1219 	if (off) {
   1220 		/*
   1221 		 * If 'off' is non-zero, packet is trailer-encapsulated,
   1222 		 * so we have to skip the type and length fields.
   1223 		 */
   1224 		cp += off + 2 * sizeof(uint16_t);
   1225 		totlen -= 2 * sizeof(uint16_t);
   1226 	}
   1227 	m = m_gethdr(M_DONTWAIT, MT_DATA);
   1228 	if (m == NULL)
   1229 		return NULL;
   1230 	m->m_pkthdr.rcvif = ifp;
   1231 	m->m_pkthdr.len = totlen;
   1232 	m->m_len = MHLEN;
   1233 
   1234 	while (totlen > 0) {
   1235 		if (top) {
   1236 			m = m_get(M_DONTWAIT, MT_DATA);
   1237 			if (m == 0) {
   1238 				m_freem(top);
   1239 				return (NULL);
   1240 			}
   1241 			m->m_len = MLEN;
   1242 		}
   1243 		len = min(totlen, epkt - cp);
   1244 		if (len >= MINCLSIZE) {
   1245 			MCLGET(m, M_DONTWAIT);
   1246 			if ((m->m_flags & M_EXT) == 0) {
   1247 				m_free(m);
   1248 				m_freem(top);
   1249 				return (NULL);
   1250 			}
   1251 			m->m_len = len = min(len, MCLBYTES);
   1252 		} else {
   1253 			/*
   1254 			 * Place initial small packet/header at end of mbuf.
   1255 			 */
   1256 			if (len < m->m_len) {
   1257 				if (top == 0 && len + max_linkhdr <= m->m_len)
   1258 					m->m_data += max_linkhdr;
   1259 				m->m_len = len;
   1260 			} else
   1261 				len = m->m_len;
   1262 		}
   1263 		if (copy)
   1264 			copy(cp, mtod(m, void *), (size_t)len);
   1265 		else
   1266 			memcpy(mtod(m, void *), cp, (size_t)len);
   1267 		cp += len;
   1268 		*mp = m;
   1269 		mp = &m->m_next;
   1270 		totlen -= len;
   1271 		if (cp == epkt)
   1272 			cp = buf;
   1273 	}
   1274 	return (top);
   1275 }
   1276 
   1277 /*
   1278  * Copy data from a buffer back into the indicated mbuf chain,
   1279  * starting "off" bytes from the beginning, extending the mbuf
   1280  * chain if necessary.
   1281  */
   1282 void
   1283 m_copyback(struct mbuf *m0, int off, int len, const void *cp)
   1284 {
   1285 #if defined(DEBUG)
   1286 	struct mbuf *origm = m0;
   1287 	int error;
   1288 #endif /* defined(DEBUG) */
   1289 
   1290 	if (m0 == NULL)
   1291 		return;
   1292 
   1293 #if defined(DEBUG)
   1294 	error =
   1295 #endif /* defined(DEBUG) */
   1296 	m_copyback0(&m0, off, len, cp,
   1297 	    M_COPYBACK0_COPYBACK|M_COPYBACK0_EXTEND, M_DONTWAIT);
   1298 
   1299 #if defined(DEBUG)
   1300 	if (error != 0 || (m0 != NULL && origm != m0))
   1301 		panic("m_copyback");
   1302 #endif /* defined(DEBUG) */
   1303 }
   1304 
   1305 struct mbuf *
   1306 m_copyback_cow(struct mbuf *m0, int off, int len, const void *cp, int how)
   1307 {
   1308 	int error;
   1309 
   1310 	/* don't support chain expansion */
   1311 	KDASSERT(off + len <= m_length(m0));
   1312 
   1313 	error = m_copyback0(&m0, off, len, cp,
   1314 	    M_COPYBACK0_COPYBACK|M_COPYBACK0_COW, how);
   1315 	if (error) {
   1316 		/*
   1317 		 * no way to recover from partial success.
   1318 		 * just free the chain.
   1319 		 */
   1320 		m_freem(m0);
   1321 		return NULL;
   1322 	}
   1323 	return m0;
   1324 }
   1325 
   1326 /*
   1327  * m_makewritable: ensure the specified range writable.
   1328  */
   1329 int
   1330 m_makewritable(struct mbuf **mp, int off, int len, int how)
   1331 {
   1332 	int error;
   1333 #if defined(DEBUG)
   1334 	struct mbuf *n;
   1335 	int origlen, reslen;
   1336 
   1337 	origlen = m_length(*mp);
   1338 #endif /* defined(DEBUG) */
   1339 
   1340 #if 0 /* M_COPYALL is large enough */
   1341 	if (len == M_COPYALL)
   1342 		len = m_length(*mp) - off; /* XXX */
   1343 #endif
   1344 
   1345 	error = m_copyback0(mp, off, len, NULL,
   1346 	    M_COPYBACK0_PRESERVE|M_COPYBACK0_COW, how);
   1347 
   1348 #if defined(DEBUG)
   1349 	reslen = 0;
   1350 	for (n = *mp; n; n = n->m_next)
   1351 		reslen += n->m_len;
   1352 	if (origlen != reslen)
   1353 		panic("m_makewritable: length changed");
   1354 	if (((*mp)->m_flags & M_PKTHDR) != 0 && reslen != (*mp)->m_pkthdr.len)
   1355 		panic("m_makewritable: inconsist");
   1356 #endif /* defined(DEBUG) */
   1357 
   1358 	return error;
   1359 }
   1360 
   1361 /*
   1362  * Copy the mbuf chain to a new mbuf chain that is as short as possible.
   1363  * Return the new mbuf chain on success, NULL on failure.  On success,
   1364  * free the old mbuf chain.
   1365  */
   1366 struct mbuf *
   1367 m_defrag(struct mbuf *mold, int flags)
   1368 {
   1369 	struct mbuf *m0, *mn, *n;
   1370 	size_t sz = mold->m_pkthdr.len;
   1371 
   1372 #ifdef DIAGNOSTIC
   1373 	if ((mold->m_flags & M_PKTHDR) == 0)
   1374 		panic("m_defrag: not a mbuf chain header");
   1375 #endif
   1376 
   1377 	m0 = m_gethdr(flags, MT_DATA);
   1378 	if (m0 == NULL)
   1379 		return NULL;
   1380 	M_COPY_PKTHDR(m0, mold);
   1381 	mn = m0;
   1382 
   1383 	do {
   1384 		if (sz > MHLEN) {
   1385 			MCLGET(mn, M_DONTWAIT);
   1386 			if ((mn->m_flags & M_EXT) == 0) {
   1387 				m_freem(m0);
   1388 				return NULL;
   1389 			}
   1390 		}
   1391 
   1392 		mn->m_len = MIN(sz, MCLBYTES);
   1393 
   1394 		m_copydata(mold, mold->m_pkthdr.len - sz, mn->m_len,
   1395 		     mtod(mn, void *));
   1396 
   1397 		sz -= mn->m_len;
   1398 
   1399 		if (sz > 0) {
   1400 			/* need more mbufs */
   1401 			n = m_get(M_NOWAIT, MT_DATA);
   1402 			if (n == NULL) {
   1403 				m_freem(m0);
   1404 				return NULL;
   1405 			}
   1406 
   1407 			mn->m_next = n;
   1408 			mn = n;
   1409 		}
   1410 	} while (sz > 0);
   1411 
   1412 	m_freem(mold);
   1413 
   1414 	return m0;
   1415 }
   1416 
   1417 int
   1418 m_copyback0(struct mbuf **mp0, int off, int len, const void *vp, int flags,
   1419     int how)
   1420 {
   1421 	int mlen;
   1422 	struct mbuf *m, *n;
   1423 	struct mbuf **mp;
   1424 	int totlen = 0;
   1425 	const char *cp = vp;
   1426 
   1427 	KASSERT(mp0 != NULL);
   1428 	KASSERT(*mp0 != NULL);
   1429 	KASSERT((flags & M_COPYBACK0_PRESERVE) == 0 || cp == NULL);
   1430 	KASSERT((flags & M_COPYBACK0_COPYBACK) == 0 || cp != NULL);
   1431 
   1432 	/*
   1433 	 * we don't bother to update "totlen" in the case of M_COPYBACK0_COW,
   1434 	 * assuming that M_COPYBACK0_EXTEND and M_COPYBACK0_COW are exclusive.
   1435 	 */
   1436 
   1437 	KASSERT((~flags & (M_COPYBACK0_EXTEND|M_COPYBACK0_COW)) != 0);
   1438 
   1439 	mp = mp0;
   1440 	m = *mp;
   1441 	while (off > (mlen = m->m_len)) {
   1442 		off -= mlen;
   1443 		totlen += mlen;
   1444 		if (m->m_next == NULL) {
   1445 			int tspace;
   1446 extend:
   1447 			if ((flags & M_COPYBACK0_EXTEND) == 0)
   1448 				goto out;
   1449 
   1450 			/*
   1451 			 * try to make some space at the end of "m".
   1452 			 */
   1453 
   1454 			mlen = m->m_len;
   1455 			if (off + len >= MINCLSIZE &&
   1456 			    (m->m_flags & M_EXT) == 0 && m->m_len == 0) {
   1457 				MCLGET(m, how);
   1458 			}
   1459 			tspace = M_TRAILINGSPACE(m);
   1460 			if (tspace > 0) {
   1461 				tspace = min(tspace, off + len);
   1462 				KASSERT(tspace > 0);
   1463 				memset(mtod(m, char *) + m->m_len, 0,
   1464 				    min(off, tspace));
   1465 				m->m_len += tspace;
   1466 				off += mlen;
   1467 				totlen -= mlen;
   1468 				continue;
   1469 			}
   1470 
   1471 			/*
   1472 			 * need to allocate an mbuf.
   1473 			 */
   1474 
   1475 			if (off + len >= MINCLSIZE) {
   1476 				n = m_getcl(how, m->m_type, 0);
   1477 			} else {
   1478 				n = m_get(how, m->m_type);
   1479 			}
   1480 			if (n == NULL) {
   1481 				goto out;
   1482 			}
   1483 			n->m_len = min(M_TRAILINGSPACE(n), off + len);
   1484 			memset(mtod(n, char *), 0, min(n->m_len, off));
   1485 			m->m_next = n;
   1486 		}
   1487 		mp = &m->m_next;
   1488 		m = m->m_next;
   1489 	}
   1490 	while (len > 0) {
   1491 		mlen = m->m_len - off;
   1492 		if (mlen != 0 && M_READONLY(m)) {
   1493 			char *datap;
   1494 			int eatlen;
   1495 
   1496 			/*
   1497 			 * this mbuf is read-only.
   1498 			 * allocate a new writable mbuf and try again.
   1499 			 */
   1500 
   1501 #if defined(DIAGNOSTIC)
   1502 			if ((flags & M_COPYBACK0_COW) == 0)
   1503 				panic("m_copyback0: read-only");
   1504 #endif /* defined(DIAGNOSTIC) */
   1505 
   1506 			/*
   1507 			 * if we're going to write into the middle of
   1508 			 * a mbuf, split it first.
   1509 			 */
   1510 			if (off > 0) {
   1511 				n = m_split0(m, off, how, 0);
   1512 				if (n == NULL)
   1513 					goto enobufs;
   1514 				m->m_next = n;
   1515 				mp = &m->m_next;
   1516 				m = n;
   1517 				off = 0;
   1518 				continue;
   1519 			}
   1520 
   1521 			/*
   1522 			 * XXX TODO coalesce into the trailingspace of
   1523 			 * the previous mbuf when possible.
   1524 			 */
   1525 
   1526 			/*
   1527 			 * allocate a new mbuf.  copy packet header if needed.
   1528 			 */
   1529 			n = m_get(how, m->m_type);
   1530 			if (n == NULL)
   1531 				goto enobufs;
   1532 			MCLAIM(n, m->m_owner);
   1533 			if (off == 0 && (m->m_flags & M_PKTHDR) != 0) {
   1534 				M_MOVE_PKTHDR(n, m);
   1535 				n->m_len = MHLEN;
   1536 			} else {
   1537 				if (len >= MINCLSIZE)
   1538 					MCLGET(n, M_DONTWAIT);
   1539 				n->m_len =
   1540 				    (n->m_flags & M_EXT) ? MCLBYTES : MLEN;
   1541 			}
   1542 			if (n->m_len > len)
   1543 				n->m_len = len;
   1544 
   1545 			/*
   1546 			 * free the region which has been overwritten.
   1547 			 * copying data from old mbufs if requested.
   1548 			 */
   1549 			if (flags & M_COPYBACK0_PRESERVE)
   1550 				datap = mtod(n, char *);
   1551 			else
   1552 				datap = NULL;
   1553 			eatlen = n->m_len;
   1554 			while (m != NULL && M_READONLY(m) &&
   1555 			    n->m_type == m->m_type && eatlen > 0) {
   1556 				mlen = min(eatlen, m->m_len);
   1557 				if (datap) {
   1558 					m_copydata(m, 0, mlen, datap);
   1559 					datap += mlen;
   1560 				}
   1561 				m->m_data += mlen;
   1562 				m->m_len -= mlen;
   1563 				eatlen -= mlen;
   1564 				if (m->m_len == 0)
   1565 					*mp = m = m_free(m);
   1566 			}
   1567 			if (eatlen > 0)
   1568 				n->m_len -= eatlen;
   1569 			n->m_next = m;
   1570 			*mp = m = n;
   1571 			continue;
   1572 		}
   1573 		mlen = min(mlen, len);
   1574 		if (flags & M_COPYBACK0_COPYBACK) {
   1575 			memcpy(mtod(m, char *) + off, cp, (unsigned)mlen);
   1576 			cp += mlen;
   1577 		}
   1578 		len -= mlen;
   1579 		mlen += off;
   1580 		off = 0;
   1581 		totlen += mlen;
   1582 		if (len == 0)
   1583 			break;
   1584 		if (m->m_next == NULL) {
   1585 			goto extend;
   1586 		}
   1587 		mp = &m->m_next;
   1588 		m = m->m_next;
   1589 	}
   1590 out:	if (((m = *mp0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) {
   1591 		KASSERT((flags & M_COPYBACK0_EXTEND) != 0);
   1592 		m->m_pkthdr.len = totlen;
   1593 	}
   1594 
   1595 	return 0;
   1596 
   1597 enobufs:
   1598 	return ENOBUFS;
   1599 }
   1600 
   1601 void
   1602 m_move_pkthdr(struct mbuf *to, struct mbuf *from)
   1603 {
   1604 
   1605 	KASSERT((to->m_flags & M_EXT) == 0);
   1606 	KASSERT((to->m_flags & M_PKTHDR) == 0 || m_tag_first(to) == NULL);
   1607 	KASSERT((from->m_flags & M_PKTHDR) != 0);
   1608 
   1609 	to->m_pkthdr = from->m_pkthdr;
   1610 	to->m_flags = from->m_flags & M_COPYFLAGS;
   1611 	to->m_data = to->m_pktdat;
   1612 
   1613 	from->m_flags &= ~M_PKTHDR;
   1614 }
   1615 
   1616 /*
   1617  * Apply function f to the data in an mbuf chain starting "off" bytes from the
   1618  * beginning, continuing for "len" bytes.
   1619  */
   1620 int
   1621 m_apply(struct mbuf *m, int off, int len,
   1622     int (*f)(void *, void *, unsigned int), void *arg)
   1623 {
   1624 	unsigned int count;
   1625 	int rval;
   1626 
   1627 	KASSERT(len >= 0);
   1628 	KASSERT(off >= 0);
   1629 
   1630 	while (off > 0) {
   1631 		KASSERT(m != NULL);
   1632 		if (off < m->m_len)
   1633 			break;
   1634 		off -= m->m_len;
   1635 		m = m->m_next;
   1636 	}
   1637 	while (len > 0) {
   1638 		KASSERT(m != NULL);
   1639 		count = min(m->m_len - off, len);
   1640 
   1641 		rval = (*f)(arg, mtod(m, char *) + off, count);
   1642 		if (rval)
   1643 			return (rval);
   1644 
   1645 		len -= count;
   1646 		off = 0;
   1647 		m = m->m_next;
   1648 	}
   1649 
   1650 	return (0);
   1651 }
   1652 
   1653 /*
   1654  * Return a pointer to mbuf/offset of location in mbuf chain.
   1655  */
   1656 struct mbuf *
   1657 m_getptr(struct mbuf *m, int loc, int *off)
   1658 {
   1659 
   1660 	while (loc >= 0) {
   1661 		/* Normal end of search */
   1662 		if (m->m_len > loc) {
   1663 	    		*off = loc;
   1664 	    		return (m);
   1665 		} else {
   1666 	    		loc -= m->m_len;
   1667 
   1668 	    		if (m->m_next == NULL) {
   1669 				if (loc == 0) {
   1670  					/* Point at the end of valid data */
   1671 		    			*off = m->m_len;
   1672 		    			return (m);
   1673 				} else
   1674 		  			return (NULL);
   1675 	    		} else
   1676 	      			m = m->m_next;
   1677 		}
   1678     	}
   1679 
   1680 	return (NULL);
   1681 }
   1682 
   1683 /*
   1684  * m_ext_free: release a reference to the mbuf external storage.
   1685  *
   1686  * => free the mbuf m itsself as well.
   1687  */
   1688 
   1689 void
   1690 m_ext_free(struct mbuf *m)
   1691 {
   1692 	bool embedded = MEXT_ISEMBEDDED(m);
   1693 	bool dofree = true;
   1694 	u_int refcnt;
   1695 
   1696 	KASSERT((m->m_flags & M_EXT) != 0);
   1697 	KASSERT(MEXT_ISEMBEDDED(m->m_ext_ref));
   1698 	KASSERT((m->m_ext_ref->m_flags & M_EXT) != 0);
   1699 	KASSERT((m->m_flags & M_EXT_CLUSTER) ==
   1700 	    (m->m_ext_ref->m_flags & M_EXT_CLUSTER));
   1701 
   1702 	if (__predict_true(m->m_ext.ext_refcnt == 1)) {
   1703 		refcnt = m->m_ext.ext_refcnt = 0;
   1704 	} else {
   1705 		refcnt = atomic_dec_uint_nv(&m->m_ext.ext_refcnt);
   1706 	}
   1707 	if (refcnt > 0) {
   1708 		if (embedded) {
   1709 			/*
   1710 			 * other mbuf's m_ext_ref still points to us.
   1711 			 */
   1712 			dofree = false;
   1713 		} else {
   1714 			m->m_ext_ref = m;
   1715 		}
   1716 	} else {
   1717 		/*
   1718 		 * dropping the last reference
   1719 		 */
   1720 		if (!embedded) {
   1721 			m->m_ext.ext_refcnt++; /* XXX */
   1722 			m_ext_free(m->m_ext_ref);
   1723 			m->m_ext_ref = m;
   1724 		} else if ((m->m_flags & M_EXT_CLUSTER) != 0) {
   1725 			pool_cache_put_paddr((struct pool_cache *)
   1726 			    m->m_ext.ext_arg,
   1727 			    m->m_ext.ext_buf, m->m_ext.ext_paddr);
   1728 		} else if (m->m_ext.ext_free) {
   1729 			(*m->m_ext.ext_free)(m,
   1730 			    m->m_ext.ext_buf, m->m_ext.ext_size,
   1731 			    m->m_ext.ext_arg);
   1732 			/*
   1733 			 * 'm' is already freed by the ext_free callback.
   1734 			 */
   1735 			dofree = false;
   1736 		} else {
   1737 			free(m->m_ext.ext_buf, m->m_ext.ext_type);
   1738 		}
   1739 	}
   1740 	if (dofree) {
   1741 		m->m_type = MT_FREE;
   1742 		pool_cache_put(mb_cache, m);
   1743 	}
   1744 }
   1745 
   1746 #if defined(DDB)
   1747 void
   1748 m_print(const struct mbuf *m, const char *modif, void (*pr)(const char *, ...))
   1749 {
   1750 	char ch;
   1751 	bool opt_c = false;
   1752 	char buf[512];
   1753 
   1754 	while ((ch = *(modif++)) != '\0') {
   1755 		switch (ch) {
   1756 		case 'c':
   1757 			opt_c = true;
   1758 			break;
   1759 		}
   1760 	}
   1761 
   1762 nextchain:
   1763 	(*pr)("MBUF %p\n", m);
   1764 	snprintb(buf, sizeof(buf), M_FLAGS_BITS, (u_int)m->m_flags);
   1765 	(*pr)("  data=%p, len=%d, type=%d, flags=%s\n",
   1766 	    m->m_data, m->m_len, m->m_type, buf);
   1767 	(*pr)("  owner=%p, next=%p, nextpkt=%p\n", m->m_owner, m->m_next,
   1768 	    m->m_nextpkt);
   1769 	(*pr)("  leadingspace=%u, trailingspace=%u, readonly=%u\n",
   1770 	    (int)M_LEADINGSPACE(m), (int)M_TRAILINGSPACE(m),
   1771 	    (int)M_READONLY(m));
   1772 	if ((m->m_flags & M_PKTHDR) != 0) {
   1773 		snprintb(buf, sizeof(buf), M_CSUM_BITS, m->m_pkthdr.csum_flags);
   1774 		(*pr)("  pktlen=%d, rcvif=%p, csum_flags=0x%s, csum_data=0x%"
   1775 		    PRIx32 ", segsz=%u\n",
   1776 		    m->m_pkthdr.len, m->m_pkthdr.rcvif,
   1777 		    buf, m->m_pkthdr.csum_data, m->m_pkthdr.segsz);
   1778 	}
   1779 	if ((m->m_flags & M_EXT)) {
   1780 		(*pr)("  ext_refcnt=%u, ext_buf=%p, ext_size=%zd, "
   1781 		    "ext_free=%p, ext_arg=%p\n",
   1782 		    m->m_ext.ext_refcnt,
   1783 		    m->m_ext.ext_buf, m->m_ext.ext_size,
   1784 		    m->m_ext.ext_free, m->m_ext.ext_arg);
   1785 	}
   1786 	if ((~m->m_flags & (M_EXT|M_EXT_PAGES)) == 0) {
   1787 		vaddr_t sva = (vaddr_t)m->m_ext.ext_buf;
   1788 		vaddr_t eva = sva + m->m_ext.ext_size;
   1789 		int n = (round_page(eva) - trunc_page(sva)) >> PAGE_SHIFT;
   1790 		int i;
   1791 
   1792 		(*pr)("  pages:");
   1793 		for (i = 0; i < n; i ++) {
   1794 			(*pr)(" %p", m->m_ext.ext_pgs[i]);
   1795 		}
   1796 		(*pr)("\n");
   1797 	}
   1798 
   1799 	if (opt_c) {
   1800 		m = m->m_next;
   1801 		if (m != NULL) {
   1802 			goto nextchain;
   1803 		}
   1804 	}
   1805 }
   1806 #endif /* defined(DDB) */
   1807 
   1808 void
   1809 mbstat_type_add(int type, int diff)
   1810 {
   1811 	struct mbstat_cpu *mb;
   1812 	int s;
   1813 
   1814 	s = splvm();
   1815 	mb = percpu_getref(mbstat_percpu);
   1816 	mb->m_mtypes[type] += diff;
   1817 	percpu_putref(mbstat_percpu);
   1818 	splx(s);
   1819 }
   1820 
   1821 #if defined(MBUFTRACE)
   1822 void
   1823 mowner_attach(struct mowner *mo)
   1824 {
   1825 
   1826 	KASSERT(mo->mo_counters == NULL);
   1827 	mo->mo_counters = percpu_alloc(sizeof(struct mowner_counter));
   1828 
   1829 	/* XXX lock */
   1830 	LIST_INSERT_HEAD(&mowners, mo, mo_link);
   1831 }
   1832 
   1833 void
   1834 mowner_detach(struct mowner *mo)
   1835 {
   1836 
   1837 	KASSERT(mo->mo_counters != NULL);
   1838 
   1839 	/* XXX lock */
   1840 	LIST_REMOVE(mo, mo_link);
   1841 
   1842 	percpu_free(mo->mo_counters, sizeof(struct mowner_counter));
   1843 	mo->mo_counters = NULL;
   1844 }
   1845 
   1846 void
   1847 mowner_init(struct mbuf *m, int type)
   1848 {
   1849 	struct mowner_counter *mc;
   1850 	struct mowner *mo;
   1851 	int s;
   1852 
   1853 	m->m_owner = mo = &unknown_mowners[type];
   1854 	s = splvm();
   1855 	mc = percpu_getref(mo->mo_counters);
   1856 	mc->mc_counter[MOWNER_COUNTER_CLAIMS]++;
   1857 	percpu_putref(mo->mo_counters);
   1858 	splx(s);
   1859 }
   1860 
   1861 void
   1862 mowner_ref(struct mbuf *m, int flags)
   1863 {
   1864 	struct mowner *mo = m->m_owner;
   1865 	struct mowner_counter *mc;
   1866 	int s;
   1867 
   1868 	s = splvm();
   1869 	mc = percpu_getref(mo->mo_counters);
   1870 	if ((flags & M_EXT) != 0)
   1871 		mc->mc_counter[MOWNER_COUNTER_EXT_CLAIMS]++;
   1872 	if ((flags & M_CLUSTER) != 0)
   1873 		mc->mc_counter[MOWNER_COUNTER_CLUSTER_CLAIMS]++;
   1874 	percpu_putref(mo->mo_counters);
   1875 	splx(s);
   1876 }
   1877 
   1878 void
   1879 mowner_revoke(struct mbuf *m, bool all, int flags)
   1880 {
   1881 	struct mowner *mo = m->m_owner;
   1882 	struct mowner_counter *mc;
   1883 	int s;
   1884 
   1885 	s = splvm();
   1886 	mc = percpu_getref(mo->mo_counters);
   1887 	if ((flags & M_EXT) != 0)
   1888 		mc->mc_counter[MOWNER_COUNTER_EXT_RELEASES]++;
   1889 	if ((flags & M_CLUSTER) != 0)
   1890 		mc->mc_counter[MOWNER_COUNTER_CLUSTER_RELEASES]++;
   1891 	if (all)
   1892 		mc->mc_counter[MOWNER_COUNTER_RELEASES]++;
   1893 	percpu_putref(mo->mo_counters);
   1894 	splx(s);
   1895 	if (all)
   1896 		m->m_owner = &revoked_mowner;
   1897 }
   1898 
   1899 static void
   1900 mowner_claim(struct mbuf *m, struct mowner *mo)
   1901 {
   1902 	struct mowner_counter *mc;
   1903 	int flags = m->m_flags;
   1904 	int s;
   1905 
   1906 	s = splvm();
   1907 	mc = percpu_getref(mo->mo_counters);
   1908 	mc->mc_counter[MOWNER_COUNTER_CLAIMS]++;
   1909 	if ((flags & M_EXT) != 0)
   1910 		mc->mc_counter[MOWNER_COUNTER_EXT_CLAIMS]++;
   1911 	if ((flags & M_CLUSTER) != 0)
   1912 		mc->mc_counter[MOWNER_COUNTER_CLUSTER_CLAIMS]++;
   1913 	percpu_putref(mo->mo_counters);
   1914 	splx(s);
   1915 	m->m_owner = mo;
   1916 }
   1917 
   1918 void
   1919 m_claim(struct mbuf *m, struct mowner *mo)
   1920 {
   1921 
   1922 	if (m->m_owner == mo || mo == NULL)
   1923 		return;
   1924 
   1925 	mowner_revoke(m, true, m->m_flags);
   1926 	mowner_claim(m, mo);
   1927 }
   1928 #endif /* defined(MBUFTRACE) */
   1929