Home | History | Annotate | Line # | Download | only in kern
uipc_mbuf.c revision 1.78
      1 /*	$NetBSD: uipc_mbuf.c,v 1.78 2004/03/09 06:37:59 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1982, 1986, 1988, 1991, 1993
     42  *	The Regents of the University of California.  All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. Neither the name of the University nor the names of its contributors
     53  *    may be used to endorse or promote products derived from this software
     54  *    without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     57  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     59  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     61  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     62  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     64  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     65  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     66  * SUCH DAMAGE.
     67  *
     68  *	@(#)uipc_mbuf.c	8.4 (Berkeley) 2/14/95
     69  */
     70 
     71 #include <sys/cdefs.h>
     72 __KERNEL_RCSID(0, "$NetBSD: uipc_mbuf.c,v 1.78 2004/03/09 06:37:59 yamt Exp $");
     73 
     74 #include "opt_mbuftrace.h"
     75 
     76 #include <sys/param.h>
     77 #include <sys/systm.h>
     78 #include <sys/proc.h>
     79 #include <sys/malloc.h>
     80 #define MBTYPES
     81 #include <sys/mbuf.h>
     82 #include <sys/kernel.h>
     83 #include <sys/syslog.h>
     84 #include <sys/domain.h>
     85 #include <sys/protosw.h>
     86 #include <sys/pool.h>
     87 #include <sys/socket.h>
     88 #include <sys/sysctl.h>
     89 
     90 #include <net/if.h>
     91 
     92 #include <uvm/uvm.h>
     93 
     94 
     95 struct	pool mbpool;		/* mbuf pool */
     96 struct	pool mclpool;		/* mbuf cluster pool */
     97 
     98 struct pool_cache mbpool_cache;
     99 struct pool_cache mclpool_cache;
    100 
    101 struct mbstat mbstat;
    102 int	max_linkhdr;
    103 int	max_protohdr;
    104 int	max_hdr;
    105 int	max_datalen;
    106 
    107 static int mb_ctor(void *, void *, int);
    108 
    109 void	*mclpool_alloc(struct pool *, int);
    110 void	mclpool_release(struct pool *, void *);
    111 
    112 struct pool_allocator mclpool_allocator = {
    113 	mclpool_alloc, mclpool_release, 0,
    114 };
    115 
    116 static struct mbuf *m_copym0 __P((struct mbuf *, int, int, int, int));
    117 
    118 const char mclpool_warnmsg[] =
    119     "WARNING: mclpool limit reached; increase NMBCLUSTERS";
    120 
    121 MALLOC_DEFINE(M_MBUF, "mbuf", "mbuf");
    122 
    123 #ifdef MBUFTRACE
    124 struct mownerhead mowners = LIST_HEAD_INITIALIZER(mowners);
    125 struct mowner unknown_mowners[] = {
    126 	{ "unknown", "free" },
    127 	{ "unknown", "data" },
    128 	{ "unknown", "header" },
    129 	{ "unknown", "soname" },
    130 	{ "unknown", "soopts" },
    131 	{ "unknown", "ftable" },
    132 	{ "unknown", "control" },
    133 	{ "unknown", "oobdata" },
    134 };
    135 struct mowner revoked_mowner = { "revoked", "" };
    136 #endif
    137 
    138 /*
    139  * Initialize the mbuf allocator.
    140  */
    141 void
    142 mbinit(void)
    143 {
    144 
    145 	KASSERT(sizeof(struct _m_ext) <= MHLEN);
    146 	KASSERT(sizeof(struct mbuf) == MSIZE);
    147 
    148 	pool_init(&mbpool, msize, 0, 0, 0, "mbpl", NULL);
    149 	pool_init(&mclpool, mclbytes, 0, 0, 0, "mclpl", &mclpool_allocator);
    150 
    151 	pool_set_drain_hook(&mbpool, m_reclaim, NULL);
    152 	pool_set_drain_hook(&mclpool, m_reclaim, NULL);
    153 
    154 	pool_cache_init(&mbpool_cache, &mbpool, mb_ctor, NULL, NULL);
    155 	pool_cache_init(&mclpool_cache, &mclpool, NULL, NULL, NULL);
    156 
    157 	/*
    158 	 * Set the hard limit on the mclpool to the number of
    159 	 * mbuf clusters the kernel is to support.  Log the limit
    160 	 * reached message max once a minute.
    161 	 */
    162 	pool_sethardlimit(&mclpool, nmbclusters, mclpool_warnmsg, 60);
    163 
    164 	/*
    165 	 * Set a low water mark for both mbufs and clusters.  This should
    166 	 * help ensure that they can be allocated in a memory starvation
    167 	 * situation.  This is important for e.g. diskless systems which
    168 	 * must allocate mbufs in order for the pagedaemon to clean pages.
    169 	 */
    170 	pool_setlowat(&mbpool, mblowat);
    171 	pool_setlowat(&mclpool, mcllowat);
    172 
    173 #ifdef MBUFTRACE
    174 	{
    175 		/*
    176 		 * Attach the unknown mowners.
    177 		 */
    178 		int i;
    179 		MOWNER_ATTACH(&revoked_mowner);
    180 		for (i = sizeof(unknown_mowners)/sizeof(unknown_mowners[0]);
    181 		     i-- > 0; )
    182 			MOWNER_ATTACH(&unknown_mowners[i]);
    183 	}
    184 #endif
    185 }
    186 
    187 /*
    188  * sysctl helper routine for the kern.mbuf subtree.  nmbclusters may
    189  * or may not be writable, and mblowat and mcllowat need range
    190  * checking and pool tweaking after being reset.
    191  */
    192 static int
    193 sysctl_kern_mbuf(SYSCTLFN_ARGS)
    194 {
    195 	int error, newval;
    196 	struct sysctlnode node;
    197 
    198 	node = *rnode;
    199 	node.sysctl_data = &newval;
    200 	switch (rnode->sysctl_num) {
    201 	case MBUF_NMBCLUSTERS:
    202 		if (mb_map != NULL) {
    203 			node.sysctl_flags &= ~SYSCTL_READWRITE;
    204 			node.sysctl_flags |= SYSCTL_READONLY;
    205 		}
    206 		/* FALLTHROUGH */
    207 	case MBUF_MBLOWAT:
    208 	case MBUF_MCLLOWAT:
    209 		newval = *(int*)rnode->sysctl_data;
    210 		break;
    211 	default:
    212 		return (EOPNOTSUPP);
    213 	}
    214 
    215 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    216 	if (error || newp == NULL)
    217 		return (error);
    218 	if (newval < 0)
    219 		return (EINVAL);
    220 
    221 	switch (node.sysctl_num) {
    222 	case MBUF_NMBCLUSTERS:
    223 		if (newval < nmbclusters)
    224 			return (EINVAL);
    225 		nmbclusters = newval;
    226 		pool_sethardlimit(&mclpool, nmbclusters, mclpool_warnmsg, 60);
    227 		break;
    228 	case MBUF_MBLOWAT:
    229 		mblowat = newval;
    230 		pool_setlowat(&mbpool, mblowat);
    231 		break;
    232 	case MBUF_MCLLOWAT:
    233 		mcllowat = newval;
    234 		pool_setlowat(&mclpool, mcllowat);
    235 		break;
    236 	}
    237 
    238 	return (0);
    239 }
    240 
    241 #ifdef MBUFTRACE
    242 static int
    243 sysctl_kern_mbuf_mowners(SYSCTLFN_ARGS)
    244 {
    245 	struct mowner *mo;
    246 	size_t len = 0;
    247 	int error = 0;
    248 
    249 	if (namelen != 0)
    250 		return (EINVAL);
    251 	if (newp != NULL)
    252 		return (EPERM);
    253 
    254 	LIST_FOREACH(mo, &mowners, mo_link) {
    255 		if (oldp != NULL) {
    256 			if (*oldlenp - len < sizeof(*mo)) {
    257 				error = ENOMEM;
    258 				break;
    259 			}
    260 			error = copyout(mo, (caddr_t) oldp + len,
    261 					sizeof(*mo));
    262 			if (error)
    263 				break;
    264 		}
    265 		len += sizeof(*mo);
    266 	}
    267 
    268 	if (error == 0)
    269 		*oldlenp = len;
    270 
    271 	return (error);
    272 }
    273 #endif /* MBUFTRACE */
    274 
    275 SYSCTL_SETUP(sysctl_kern_mbuf_setup, "sysctl kern.mbuf subtree setup")
    276 {
    277 
    278 	sysctl_createv(SYSCTL_PERMANENT,
    279 		       CTLTYPE_NODE, "kern", NULL,
    280 		       NULL, 0, NULL, 0,
    281 		       CTL_KERN, CTL_EOL);
    282 	sysctl_createv(SYSCTL_PERMANENT,
    283 		       CTLTYPE_NODE, "mbuf", NULL,
    284 		       NULL, 0, NULL, 0,
    285 		       CTL_KERN, KERN_MBUF, CTL_EOL);
    286 
    287 	sysctl_createv(SYSCTL_PERMANENT|SYSCTL_IMMEDIATE,
    288 		       CTLTYPE_INT, "msize", NULL,
    289 		       NULL, msize, NULL, 0,
    290 		       CTL_KERN, KERN_MBUF, MBUF_MSIZE, CTL_EOL);
    291 	sysctl_createv(SYSCTL_PERMANENT|SYSCTL_IMMEDIATE,
    292 		       CTLTYPE_INT, "mclbytes", NULL,
    293 		       NULL, mclbytes, NULL, 0,
    294 		       CTL_KERN, KERN_MBUF, MBUF_MCLBYTES, CTL_EOL);
    295 	sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
    296 		       CTLTYPE_INT, "nmbclusters", NULL,
    297 		       sysctl_kern_mbuf, 0, &nmbclusters, 0,
    298 		       CTL_KERN, KERN_MBUF, MBUF_NMBCLUSTERS, CTL_EOL);
    299 	sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
    300 		       CTLTYPE_INT, "mblowat", NULL,
    301 		       sysctl_kern_mbuf, 0, &mblowat, 0,
    302 		       CTL_KERN, KERN_MBUF, MBUF_MBLOWAT, CTL_EOL);
    303 	sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
    304 		       CTLTYPE_INT, "mcllowat", NULL,
    305 		       sysctl_kern_mbuf, 0, &mcllowat, 0,
    306 		       CTL_KERN, KERN_MBUF, MBUF_MCLLOWAT, CTL_EOL);
    307 	sysctl_createv(SYSCTL_PERMANENT,
    308 		       CTLTYPE_STRUCT, "stats", NULL,
    309 		       NULL, 0, &mbstat, sizeof(mbstat),
    310 		       CTL_KERN, KERN_MBUF, MBUF_STATS, CTL_EOL);
    311 #ifdef MBUFTRACE
    312 	sysctl_createv(SYSCTL_PERMANENT,
    313 		       CTLTYPE_STRUCT, "mowners", NULL,
    314 		       sysctl_kern_mbuf_mowners, 0, NULL, 0,
    315 		       CTL_KERN, KERN_MBUF, MBUF_MOWNERS, CTL_EOL);
    316 #endif /* MBUFTRACE */
    317 }
    318 
    319 void *
    320 mclpool_alloc(struct pool *pp, int flags)
    321 {
    322 	boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
    323 
    324 	return ((void *)uvm_km_alloc_poolpage1(mb_map, NULL, waitok));
    325 }
    326 
    327 void
    328 mclpool_release(struct pool *pp, void *v)
    329 {
    330 
    331 	uvm_km_free_poolpage1(mb_map, (vaddr_t)v);
    332 }
    333 
    334 /*ARGSUSED*/
    335 static int
    336 mb_ctor(void *arg, void *object, int flags)
    337 {
    338 	struct mbuf *m = object;
    339 
    340 #ifdef POOL_VTOPHYS
    341 	m->m_paddr = POOL_VTOPHYS(m);
    342 #else
    343 	m->m_paddr = M_PADDR_INVALID;
    344 #endif
    345 	return (0);
    346 }
    347 
    348 void
    349 m_reclaim(void *arg, int flags)
    350 {
    351 	struct domain *dp;
    352 	struct protosw *pr;
    353 	struct ifnet *ifp;
    354 	int s = splvm();
    355 
    356 	for (dp = domains; dp; dp = dp->dom_next)
    357 		for (pr = dp->dom_protosw;
    358 		     pr < dp->dom_protoswNPROTOSW; pr++)
    359 			if (pr->pr_drain)
    360 				(*pr->pr_drain)();
    361 	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
    362 		if (ifp->if_drain)
    363 			(*ifp->if_drain)(ifp);
    364 	splx(s);
    365 	mbstat.m_drain++;
    366 }
    367 
    368 /*
    369  * Space allocation routines.
    370  * These are also available as macros
    371  * for critical paths.
    372  */
    373 struct mbuf *
    374 m_get(int nowait, int type)
    375 {
    376 	struct mbuf *m;
    377 
    378 	MGET(m, nowait, type);
    379 	return (m);
    380 }
    381 
    382 struct mbuf *
    383 m_gethdr(int nowait, int type)
    384 {
    385 	struct mbuf *m;
    386 
    387 	MGETHDR(m, nowait, type);
    388 	return (m);
    389 }
    390 
    391 struct mbuf *
    392 m_getclr(int nowait, int type)
    393 {
    394 	struct mbuf *m;
    395 
    396 	MGET(m, nowait, type);
    397 	if (m == 0)
    398 		return (NULL);
    399 	memset(mtod(m, caddr_t), 0, MLEN);
    400 	return (m);
    401 }
    402 
    403 void
    404 m_clget(struct mbuf *m, int nowait)
    405 {
    406 
    407 	MCLGET(m, nowait);
    408 }
    409 
    410 struct mbuf *
    411 m_free(struct mbuf *m)
    412 {
    413 	struct mbuf *n;
    414 
    415 	MFREE(m, n);
    416 	return (n);
    417 }
    418 
    419 void
    420 m_freem(struct mbuf *m)
    421 {
    422 	struct mbuf *n;
    423 
    424 	if (m == NULL)
    425 		return;
    426 	do {
    427 		MFREE(m, n);
    428 		m = n;
    429 	} while (m);
    430 }
    431 
    432 #ifdef MBUFTRACE
    433 void
    434 m_claim(struct mbuf *m, struct mowner *mo)
    435 {
    436 
    437 	for (; m != NULL; m = m->m_next)
    438 		MCLAIM(m, mo);
    439 }
    440 #endif
    441 
    442 /*
    443  * Mbuffer utility routines.
    444  */
    445 
    446 /*
    447  * Lesser-used path for M_PREPEND:
    448  * allocate new mbuf to prepend to chain,
    449  * copy junk along.
    450  */
    451 struct mbuf *
    452 m_prepend(struct mbuf *m, int len, int how)
    453 {
    454 	struct mbuf *mn;
    455 
    456 	MGET(mn, how, m->m_type);
    457 	if (mn == (struct mbuf *)NULL) {
    458 		m_freem(m);
    459 		return ((struct mbuf *)NULL);
    460 	}
    461 	if (m->m_flags & M_PKTHDR) {
    462 		M_COPY_PKTHDR(mn, m);
    463 		m_tag_delete_chain(m, NULL);
    464 		m->m_flags &= ~M_PKTHDR;
    465 	} else {
    466 		MCLAIM(mn, m->m_owner);
    467 	}
    468 	mn->m_next = m;
    469 	m = mn;
    470 	if (len < MHLEN)
    471 		MH_ALIGN(m, len);
    472 	m->m_len = len;
    473 	return (m);
    474 }
    475 
    476 /*
    477  * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
    478  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
    479  * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
    480  */
    481 int MCFail;
    482 
    483 struct mbuf *
    484 m_copym(struct mbuf *m, int off0, int len, int wait)
    485 {
    486 
    487 	return m_copym0(m, off0, len, wait, 0);	/* shallow copy on M_EXT */
    488 }
    489 
    490 struct mbuf *
    491 m_dup(struct mbuf *m, int off0, int len, int wait)
    492 {
    493 
    494 	return m_copym0(m, off0, len, wait, 1);	/* deep copy */
    495 }
    496 
    497 static struct mbuf *
    498 m_copym0(struct mbuf *m, int off0, int len, int wait, int deep)
    499 {
    500 	struct mbuf *n, **np;
    501 	int off = off0;
    502 	struct mbuf *top;
    503 	int copyhdr = 0;
    504 
    505 	if (off < 0 || len < 0)
    506 		panic("m_copym: off %d, len %d", off, len);
    507 	if (off == 0 && m->m_flags & M_PKTHDR)
    508 		copyhdr = 1;
    509 	while (off > 0) {
    510 		if (m == 0)
    511 			panic("m_copym: m == 0");
    512 		if (off < m->m_len)
    513 			break;
    514 		off -= m->m_len;
    515 		m = m->m_next;
    516 	}
    517 	np = &top;
    518 	top = 0;
    519 	while (len > 0) {
    520 		if (m == 0) {
    521 			if (len != M_COPYALL)
    522 				panic("m_copym: m == 0 and not COPYALL");
    523 			break;
    524 		}
    525 		MGET(n, wait, m->m_type);
    526 		*np = n;
    527 		if (n == 0)
    528 			goto nospace;
    529 		MCLAIM(n, m->m_owner);
    530 		if (copyhdr) {
    531 			M_COPY_PKTHDR(n, m);
    532 			if (len == M_COPYALL)
    533 				n->m_pkthdr.len -= off0;
    534 			else
    535 				n->m_pkthdr.len = len;
    536 			copyhdr = 0;
    537 		}
    538 		n->m_len = min(len, m->m_len - off);
    539 		if (m->m_flags & M_EXT) {
    540 			if (!deep) {
    541 				n->m_data = m->m_data + off;
    542 				n->m_ext = m->m_ext;
    543 				MCLADDREFERENCE(m, n);
    544 			} else {
    545 				/*
    546 				 * we are unsure about the way m was allocated.
    547 				 * copy into multiple MCLBYTES cluster mbufs.
    548 				 */
    549 				MCLGET(n, wait);
    550 				n->m_len = 0;
    551 				n->m_len = M_TRAILINGSPACE(n);
    552 				n->m_len = min(n->m_len, len);
    553 				n->m_len = min(n->m_len, m->m_len - off);
    554 				memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + off,
    555 				    (unsigned)n->m_len);
    556 			}
    557 		} else
    558 			memcpy(mtod(n, caddr_t), mtod(m, caddr_t)+off,
    559 			    (unsigned)n->m_len);
    560 		if (len != M_COPYALL)
    561 			len -= n->m_len;
    562 		off += n->m_len;
    563 #ifdef DIAGNOSTIC
    564 		if (off > m->m_len)
    565 			panic("m_copym0 overrun");
    566 #endif
    567 		if (off == m->m_len) {
    568 			m = m->m_next;
    569 			off = 0;
    570 		}
    571 		np = &n->m_next;
    572 	}
    573 	if (top == 0)
    574 		MCFail++;
    575 	return (top);
    576 nospace:
    577 	m_freem(top);
    578 	MCFail++;
    579 	return (NULL);
    580 }
    581 
    582 /*
    583  * Copy an entire packet, including header (which must be present).
    584  * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
    585  */
    586 struct mbuf *
    587 m_copypacket(struct mbuf *m, int how)
    588 {
    589 	struct mbuf *top, *n, *o;
    590 
    591 	MGET(n, how, m->m_type);
    592 	top = n;
    593 	if (!n)
    594 		goto nospace;
    595 
    596 	MCLAIM(n, m->m_owner);
    597 	M_COPY_PKTHDR(n, m);
    598 	n->m_len = m->m_len;
    599 	if (m->m_flags & M_EXT) {
    600 		n->m_data = m->m_data;
    601 		n->m_ext = m->m_ext;
    602 		MCLADDREFERENCE(m, n);
    603 	} else {
    604 		memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
    605 	}
    606 
    607 	m = m->m_next;
    608 	while (m) {
    609 		MGET(o, how, m->m_type);
    610 		if (!o)
    611 			goto nospace;
    612 
    613 		MCLAIM(o, m->m_owner);
    614 		n->m_next = o;
    615 		n = n->m_next;
    616 
    617 		n->m_len = m->m_len;
    618 		if (m->m_flags & M_EXT) {
    619 			n->m_data = m->m_data;
    620 			n->m_ext = m->m_ext;
    621 			MCLADDREFERENCE(m, n);
    622 		} else {
    623 			memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
    624 		}
    625 
    626 		m = m->m_next;
    627 	}
    628 	return top;
    629 nospace:
    630 	m_freem(top);
    631 	MCFail++;
    632 	return NULL;
    633 }
    634 
    635 /*
    636  * Copy data from an mbuf chain starting "off" bytes from the beginning,
    637  * continuing for "len" bytes, into the indicated buffer.
    638  */
    639 void
    640 m_copydata(struct mbuf *m, int off, int len, caddr_t cp)
    641 {
    642 	unsigned count;
    643 
    644 	if (off < 0 || len < 0)
    645 		panic("m_copydata");
    646 	while (off > 0) {
    647 		if (m == 0)
    648 			panic("m_copydata");
    649 		if (off < m->m_len)
    650 			break;
    651 		off -= m->m_len;
    652 		m = m->m_next;
    653 	}
    654 	while (len > 0) {
    655 		if (m == 0)
    656 			panic("m_copydata");
    657 		count = min(m->m_len - off, len);
    658 		memcpy(cp, mtod(m, caddr_t) + off, count);
    659 		len -= count;
    660 		cp += count;
    661 		off = 0;
    662 		m = m->m_next;
    663 	}
    664 }
    665 
    666 /*
    667  * Concatenate mbuf chain n to m.
    668  * n might be copied into m (when n->m_len is small), therefore data portion of
    669  * n could be copied into an mbuf of different mbuf type.
    670  * Therefore both chains should be of the same type (e.g. MT_DATA).
    671  * Any m_pkthdr is not updated.
    672  */
    673 void
    674 m_cat(struct mbuf *m, struct mbuf *n)
    675 {
    676 
    677 	while (m->m_next)
    678 		m = m->m_next;
    679 	while (n) {
    680 		if (M_READONLY(m) || n->m_len > M_TRAILINGSPACE(m)) {
    681 			/* just join the two chains */
    682 			m->m_next = n;
    683 			return;
    684 		}
    685 		KASSERT(n->m_len == 0 || m->m_type == n->m_type);
    686 		/* splat the data from one into the other */
    687 		memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
    688 		    (u_int)n->m_len);
    689 		m->m_len += n->m_len;
    690 		n = m_free(n);
    691 	}
    692 }
    693 
    694 void
    695 m_adj(struct mbuf *mp, int req_len)
    696 {
    697 	int len = req_len;
    698 	struct mbuf *m;
    699 	int count;
    700 
    701 	if ((m = mp) == NULL)
    702 		return;
    703 	if (len >= 0) {
    704 		/*
    705 		 * Trim from head.
    706 		 */
    707 		while (m != NULL && len > 0) {
    708 			if (m->m_len <= len) {
    709 				len -= m->m_len;
    710 				m->m_len = 0;
    711 				m = m->m_next;
    712 			} else {
    713 				m->m_len -= len;
    714 				m->m_data += len;
    715 				len = 0;
    716 			}
    717 		}
    718 		m = mp;
    719 		if (mp->m_flags & M_PKTHDR)
    720 			m->m_pkthdr.len -= (req_len - len);
    721 	} else {
    722 		/*
    723 		 * Trim from tail.  Scan the mbuf chain,
    724 		 * calculating its length and finding the last mbuf.
    725 		 * If the adjustment only affects this mbuf, then just
    726 		 * adjust and return.  Otherwise, rescan and truncate
    727 		 * after the remaining size.
    728 		 */
    729 		len = -len;
    730 		count = 0;
    731 		for (;;) {
    732 			count += m->m_len;
    733 			if (m->m_next == (struct mbuf *)0)
    734 				break;
    735 			m = m->m_next;
    736 		}
    737 		if (m->m_len >= len) {
    738 			m->m_len -= len;
    739 			if (mp->m_flags & M_PKTHDR)
    740 				mp->m_pkthdr.len -= len;
    741 			return;
    742 		}
    743 		count -= len;
    744 		if (count < 0)
    745 			count = 0;
    746 		/*
    747 		 * Correct length for chain is "count".
    748 		 * Find the mbuf with last data, adjust its length,
    749 		 * and toss data from remaining mbufs on chain.
    750 		 */
    751 		m = mp;
    752 		if (m->m_flags & M_PKTHDR)
    753 			m->m_pkthdr.len = count;
    754 		for (; m; m = m->m_next) {
    755 			if (m->m_len >= count) {
    756 				m->m_len = count;
    757 				break;
    758 			}
    759 			count -= m->m_len;
    760 		}
    761 		while (m->m_next)
    762 			(m = m->m_next) ->m_len = 0;
    763 	}
    764 }
    765 
    766 /*
    767  * Rearange an mbuf chain so that len bytes are contiguous
    768  * and in the data area of an mbuf (so that mtod and dtom
    769  * will work for a structure of size len).  Returns the resulting
    770  * mbuf chain on success, frees it and returns null on failure.
    771  * If there is room, it will add up to max_protohdr-len extra bytes to the
    772  * contiguous region in an attempt to avoid being called next time.
    773  */
    774 int MPFail;
    775 
    776 struct mbuf *
    777 m_pullup(struct mbuf *n, int len)
    778 {
    779 	struct mbuf *m;
    780 	int count;
    781 	int space;
    782 
    783 	/*
    784 	 * If first mbuf has no cluster, and has room for len bytes
    785 	 * without shifting current data, pullup into it,
    786 	 * otherwise allocate a new mbuf to prepend to the chain.
    787 	 */
    788 	if ((n->m_flags & M_EXT) == 0 &&
    789 	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
    790 		if (n->m_len >= len)
    791 			return (n);
    792 		m = n;
    793 		n = n->m_next;
    794 		len -= m->m_len;
    795 	} else {
    796 		if (len > MHLEN)
    797 			goto bad;
    798 		MGET(m, M_DONTWAIT, n->m_type);
    799 		if (m == 0)
    800 			goto bad;
    801 		MCLAIM(m, n->m_owner);
    802 		m->m_len = 0;
    803 		if (n->m_flags & M_PKTHDR) {
    804 			M_COPY_PKTHDR(m, n);
    805 			m_tag_delete_chain(n, NULL);
    806 			n->m_flags &= ~M_PKTHDR;
    807 		}
    808 	}
    809 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
    810 	do {
    811 		count = min(min(max(len, max_protohdr), space), n->m_len);
    812 		memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
    813 		  (unsigned)count);
    814 		len -= count;
    815 		m->m_len += count;
    816 		n->m_len -= count;
    817 		space -= count;
    818 		if (n->m_len)
    819 			n->m_data += count;
    820 		else
    821 			n = m_free(n);
    822 	} while (len > 0 && n);
    823 	if (len > 0) {
    824 		(void) m_free(m);
    825 		goto bad;
    826 	}
    827 	m->m_next = n;
    828 	return (m);
    829 bad:
    830 	m_freem(n);
    831 	MPFail++;
    832 	return (NULL);
    833 }
    834 
    835 /*
    836  * Like m_pullup(), except a new mbuf is always allocated, and we allow
    837  * the amount of empty space before the data in the new mbuf to be specified
    838  * (in the event that the caller expects to prepend later).
    839  */
    840 int MSFail;
    841 
    842 struct mbuf *
    843 m_copyup(struct mbuf *n, int len, int dstoff)
    844 {
    845 	struct mbuf *m;
    846 	int count, space;
    847 
    848 	if (len > (MHLEN - dstoff))
    849 		goto bad;
    850 	MGET(m, M_DONTWAIT, n->m_type);
    851 	if (m == NULL)
    852 		goto bad;
    853 	MCLAIM(m, n->m_owner);
    854 	m->m_len = 0;
    855 	if (n->m_flags & M_PKTHDR) {
    856 		M_COPY_PKTHDR(m, n);
    857 		m_tag_delete_chain(m, NULL);
    858 		n->m_flags &= ~M_PKTHDR;
    859 	}
    860 	m->m_data += dstoff;
    861 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
    862 	do {
    863 		count = min(min(max(len, max_protohdr), space), n->m_len);
    864 		memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
    865 		    (unsigned)count);
    866 		len -= count;
    867 		m->m_len += count;
    868 		n->m_len -= count;
    869 		space -= count;
    870 		if (n->m_len)
    871 			n->m_data += count;
    872 		else
    873 			n = m_free(n);
    874 	} while (len > 0 && n);
    875 	if (len > 0) {
    876 		(void) m_free(m);
    877 		goto bad;
    878 	}
    879 	m->m_next = n;
    880 	return (m);
    881  bad:
    882 	m_freem(n);
    883 	MSFail++;
    884 	return (NULL);
    885 }
    886 
    887 /*
    888  * Partition an mbuf chain in two pieces, returning the tail --
    889  * all but the first len0 bytes.  In case of failure, it returns NULL and
    890  * attempts to restore the chain to its original state.
    891  */
    892 struct mbuf *
    893 m_split(struct mbuf *m0, int len0, int wait)
    894 {
    895 	struct mbuf *m, *n;
    896 	unsigned len = len0, remain, len_save;
    897 
    898 	for (m = m0; m && len > m->m_len; m = m->m_next)
    899 		len -= m->m_len;
    900 	if (m == 0)
    901 		return (NULL);
    902 	remain = m->m_len - len;
    903 	if (m0->m_flags & M_PKTHDR) {
    904 		MGETHDR(n, wait, m0->m_type);
    905 		if (n == 0)
    906 			return (NULL);
    907 		MCLAIM(m, m0->m_owner);
    908 		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
    909 		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
    910 		len_save = m0->m_pkthdr.len;
    911 		m0->m_pkthdr.len = len0;
    912 		if (m->m_flags & M_EXT)
    913 			goto extpacket;
    914 		if (remain > MHLEN) {
    915 			/* m can't be the lead packet */
    916 			MH_ALIGN(n, 0);
    917 			n->m_next = m_split(m, len, wait);
    918 			if (n->m_next == 0) {
    919 				(void) m_free(n);
    920 				m0->m_pkthdr.len = len_save;
    921 				return (NULL);
    922 			} else
    923 				return (n);
    924 		} else
    925 			MH_ALIGN(n, remain);
    926 	} else if (remain == 0) {
    927 		n = m->m_next;
    928 		m->m_next = 0;
    929 		return (n);
    930 	} else {
    931 		MGET(n, wait, m->m_type);
    932 		if (n == 0)
    933 			return (NULL);
    934 		MCLAIM(n, m->m_owner);
    935 		M_ALIGN(n, remain);
    936 	}
    937 extpacket:
    938 	if (m->m_flags & M_EXT) {
    939 		n->m_ext = m->m_ext;
    940 		MCLADDREFERENCE(m, n);
    941 		n->m_data = m->m_data + len;
    942 	} else {
    943 		memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + len, remain);
    944 	}
    945 	n->m_len = remain;
    946 	m->m_len = len;
    947 	n->m_next = m->m_next;
    948 	m->m_next = 0;
    949 	return (n);
    950 }
    951 /*
    952  * Routine to copy from device local memory into mbufs.
    953  */
    954 struct mbuf *
    955 m_devget(char *buf, int totlen, int off0, struct ifnet *ifp,
    956     void (*copy)(const void *from, void *to, size_t len))
    957 {
    958 	struct mbuf *m;
    959 	struct mbuf *top = 0, **mp = &top;
    960 	int off = off0, len;
    961 	char *cp;
    962 	char *epkt;
    963 
    964 	cp = buf;
    965 	epkt = cp + totlen;
    966 	if (off) {
    967 		/*
    968 		 * If 'off' is non-zero, packet is trailer-encapsulated,
    969 		 * so we have to skip the type and length fields.
    970 		 */
    971 		cp += off + 2 * sizeof(u_int16_t);
    972 		totlen -= 2 * sizeof(u_int16_t);
    973 	}
    974 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    975 	if (m == 0)
    976 		return (NULL);
    977 	m->m_pkthdr.rcvif = ifp;
    978 	m->m_pkthdr.len = totlen;
    979 	m->m_len = MHLEN;
    980 
    981 	while (totlen > 0) {
    982 		if (top) {
    983 			MGET(m, M_DONTWAIT, MT_DATA);
    984 			if (m == 0) {
    985 				m_freem(top);
    986 				return (NULL);
    987 			}
    988 			m->m_len = MLEN;
    989 		}
    990 		len = min(totlen, epkt - cp);
    991 		if (len >= MINCLSIZE) {
    992 			MCLGET(m, M_DONTWAIT);
    993 			if ((m->m_flags & M_EXT) == 0) {
    994 				m_free(m);
    995 				m_freem(top);
    996 				return (NULL);
    997 			}
    998 			m->m_len = len = min(len, MCLBYTES);
    999 		} else {
   1000 			/*
   1001 			 * Place initial small packet/header at end of mbuf.
   1002 			 */
   1003 			if (len < m->m_len) {
   1004 				if (top == 0 && len + max_linkhdr <= m->m_len)
   1005 					m->m_data += max_linkhdr;
   1006 				m->m_len = len;
   1007 			} else
   1008 				len = m->m_len;
   1009 		}
   1010 		if (copy)
   1011 			copy(cp, mtod(m, caddr_t), (size_t)len);
   1012 		else
   1013 			memcpy(mtod(m, caddr_t), cp, (size_t)len);
   1014 		cp += len;
   1015 		*mp = m;
   1016 		mp = &m->m_next;
   1017 		totlen -= len;
   1018 		if (cp == epkt)
   1019 			cp = buf;
   1020 	}
   1021 	return (top);
   1022 }
   1023 
   1024 /*
   1025  * Copy data from a buffer back into the indicated mbuf chain,
   1026  * starting "off" bytes from the beginning, extending the mbuf
   1027  * chain if necessary.
   1028  */
   1029 void
   1030 m_copyback(struct mbuf *m0, int off, int len, caddr_t cp)
   1031 {
   1032 	int mlen;
   1033 	struct mbuf *m = m0, *n;
   1034 	int totlen = 0;
   1035 
   1036 	if (m0 == 0)
   1037 		return;
   1038 	while (off > (mlen = m->m_len)) {
   1039 		off -= mlen;
   1040 		totlen += mlen;
   1041 		if (m->m_next == 0) {
   1042 			n = m_getclr(M_DONTWAIT, m->m_type);
   1043 			if (n == 0)
   1044 				goto out;
   1045 			n->m_len = min(MLEN, len + off);
   1046 			m->m_next = n;
   1047 		}
   1048 		m = m->m_next;
   1049 	}
   1050 	while (len > 0) {
   1051 		mlen = min (m->m_len - off, len);
   1052 		memcpy(mtod(m, caddr_t) + off, cp, (unsigned)mlen);
   1053 		cp += mlen;
   1054 		len -= mlen;
   1055 		mlen += off;
   1056 		off = 0;
   1057 		totlen += mlen;
   1058 		if (len == 0)
   1059 			break;
   1060 		if (m->m_next == 0) {
   1061 			n = m_get(M_DONTWAIT, m->m_type);
   1062 			if (n == 0)
   1063 				break;
   1064 			n->m_len = min(MLEN, len);
   1065 			m->m_next = n;
   1066 		}
   1067 		m = m->m_next;
   1068 	}
   1069 out:	if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
   1070 		m->m_pkthdr.len = totlen;
   1071 }
   1072 
   1073 /*
   1074  * Apply function f to the data in an mbuf chain starting "off" bytes from the
   1075  * beginning, continuing for "len" bytes.
   1076  */
   1077 int
   1078 m_apply(struct mbuf *m, int off, int len,
   1079     int (*f)(void *, caddr_t, unsigned int), void *arg)
   1080 {
   1081 	unsigned int count;
   1082 	int rval;
   1083 
   1084 	KASSERT(len >= 0);
   1085 	KASSERT(off >= 0);
   1086 
   1087 	while (off > 0) {
   1088 		KASSERT(m != NULL);
   1089 		if (off < m->m_len)
   1090 			break;
   1091 		off -= m->m_len;
   1092 		m = m->m_next;
   1093 	}
   1094 	while (len > 0) {
   1095 		KASSERT(m != NULL);
   1096 		count = min(m->m_len - off, len);
   1097 
   1098 		rval = (*f)(arg, mtod(m, caddr_t) + off, count);
   1099 		if (rval)
   1100 			return (rval);
   1101 
   1102 		len -= count;
   1103 		off = 0;
   1104 		m = m->m_next;
   1105 	}
   1106 
   1107 	return (0);
   1108 }
   1109 
   1110 /*
   1111  * Return a pointer to mbuf/offset of location in mbuf chain.
   1112  */
   1113 struct mbuf *
   1114 m_getptr(struct mbuf *m, int loc, int *off)
   1115 {
   1116 
   1117 	while (loc >= 0) {
   1118 		/* Normal end of search */
   1119 		if (m->m_len > loc) {
   1120 	    		*off = loc;
   1121 	    		return (m);
   1122 		} else {
   1123 	    		loc -= m->m_len;
   1124 
   1125 	    		if (m->m_next == NULL) {
   1126 				if (loc == 0) {
   1127  					/* Point at the end of valid data */
   1128 		    			*off = m->m_len;
   1129 		    			return (m);
   1130 				} else
   1131 		  			return (NULL);
   1132 	    		} else
   1133 	      			m = m->m_next;
   1134 		}
   1135     	}
   1136 
   1137 	return (NULL);
   1138 }
   1139