Home | History | Annotate | Line # | Download | only in kern
uipc_mbuf.c revision 1.74
      1 /*	$NetBSD: uipc_mbuf.c,v 1.74 2003/10/03 20:56:11 itojun 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.74 2003/10/03 20:56:11 itojun 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 int
    188 sysctl_dombuf(int *name, u_int namelen, void *oldp, size_t *oldlenp,
    189     void *newp, size_t newlen)
    190 {
    191 	int error, newval;
    192 
    193 	/* All sysctl names at this level are terminal. */
    194 	if (namelen != 1)
    195 		return (ENOTDIR);		/* overloaded */
    196 
    197 	switch (name[0]) {
    198 	case MBUF_MSIZE:
    199 		return (sysctl_rdint(oldp, oldlenp, newp, msize));
    200 	case MBUF_MCLBYTES:
    201 		return (sysctl_rdint(oldp, oldlenp, newp, mclbytes));
    202 	case MBUF_NMBCLUSTERS:
    203 		/*
    204 		 * If we have direct-mapped pool pages, we can adjust this
    205 		 * number on the fly.  If not, we're limited by the size
    206 		 * of mb_map, and cannot change this value.
    207 		 *
    208 		 * Note: we only allow the value to be increased, never
    209 		 * decreased.
    210 		 */
    211 		if (mb_map == NULL) {
    212 			newval = nmbclusters;
    213 			error = sysctl_int(oldp, oldlenp, newp, newlen,
    214 			    &newval);
    215 			if (error != 0)
    216 				return (error);
    217 			if (newp != NULL) {
    218 				if (newval >= nmbclusters) {
    219 					nmbclusters = newval;
    220 					pool_sethardlimit(&mclpool,
    221 					    nmbclusters, mclpool_warnmsg, 60);
    222 				} else
    223 					error = EINVAL;
    224 			}
    225 			return (error);
    226 		} else
    227 			return (sysctl_rdint(oldp, oldlenp, newp, nmbclusters));
    228 	case MBUF_MBLOWAT:
    229 	case MBUF_MCLLOWAT:
    230 		/* New value must be >= 0. */
    231 		newval = (name[0] == MBUF_MBLOWAT) ? mblowat : mcllowat;
    232 		error = sysctl_int(oldp, oldlenp, newp, newlen, &newval);
    233 		if (error != 0)
    234 			return (error);
    235 		if (newp != NULL) {
    236 			if (newval >= 0) {
    237 				if (name[0] == MBUF_MBLOWAT) {
    238 					mblowat = newval;
    239 					pool_setlowat(&mbpool, newval);
    240 				} else {
    241 					mcllowat = newval;
    242 					pool_setlowat(&mclpool, newval);
    243 				}
    244 			} else
    245 				error = EINVAL;
    246 		}
    247 		return (error);
    248 	case MBUF_STATS:
    249 		return (sysctl_rdstruct(oldp, oldlenp, newp,
    250 		    &mbstat, sizeof(mbstat)));
    251 #ifdef MBUFTRACE
    252 	case MBUF_MOWNERS: {
    253 		struct mowner *mo;
    254 		size_t len = 0;
    255 		if (newp != NULL)
    256 			return (EPERM);
    257 		error = 0;
    258 		LIST_FOREACH(mo, &mowners, mo_link) {
    259 			if (oldp != NULL) {
    260 				if (*oldlenp - len < sizeof(*mo)) {
    261 					error = ENOMEM;
    262 					break;
    263 				}
    264 				error = copyout(mo, (caddr_t) oldp + len,
    265 					sizeof(*mo));
    266 				if (error)
    267 					break;
    268 			}
    269 			len += sizeof(*mo);
    270 		}
    271 		*oldlenp = len;
    272 		return (error);
    273 	}
    274 #endif
    275 	default:
    276 		return (EOPNOTSUPP);
    277 	}
    278 	/* NOTREACHED */
    279 }
    280 
    281 void *
    282 mclpool_alloc(struct pool *pp, int flags)
    283 {
    284 	boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
    285 
    286 	return ((void *)uvm_km_alloc_poolpage1(mb_map, NULL, waitok));
    287 }
    288 
    289 void
    290 mclpool_release(struct pool *pp, void *v)
    291 {
    292 
    293 	uvm_km_free_poolpage1(mb_map, (vaddr_t)v);
    294 }
    295 
    296 /*ARGSUSED*/
    297 static int
    298 mb_ctor(void *arg, void *object, int flags)
    299 {
    300 	struct mbuf *m = object;
    301 
    302 #ifdef POOL_VTOPHYS
    303 	m->m_paddr = POOL_VTOPHYS(m);
    304 #else
    305 	m->m_paddr = M_PADDR_INVALID;
    306 #endif
    307 	return (0);
    308 }
    309 
    310 void
    311 m_reclaim(void *arg, int flags)
    312 {
    313 	struct domain *dp;
    314 	struct protosw *pr;
    315 	struct ifnet *ifp;
    316 	int s = splvm();
    317 
    318 	for (dp = domains; dp; dp = dp->dom_next)
    319 		for (pr = dp->dom_protosw;
    320 		     pr < dp->dom_protoswNPROTOSW; pr++)
    321 			if (pr->pr_drain)
    322 				(*pr->pr_drain)();
    323 	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
    324 		if (ifp->if_drain)
    325 			(*ifp->if_drain)(ifp);
    326 	splx(s);
    327 	mbstat.m_drain++;
    328 }
    329 
    330 /*
    331  * Space allocation routines.
    332  * These are also available as macros
    333  * for critical paths.
    334  */
    335 struct mbuf *
    336 m_get(int nowait, int type)
    337 {
    338 	struct mbuf *m;
    339 
    340 	MGET(m, nowait, type);
    341 	return (m);
    342 }
    343 
    344 struct mbuf *
    345 m_gethdr(int nowait, int type)
    346 {
    347 	struct mbuf *m;
    348 
    349 	MGETHDR(m, nowait, type);
    350 	return (m);
    351 }
    352 
    353 struct mbuf *
    354 m_getclr(int nowait, int type)
    355 {
    356 	struct mbuf *m;
    357 
    358 	MGET(m, nowait, type);
    359 	if (m == 0)
    360 		return (NULL);
    361 	memset(mtod(m, caddr_t), 0, MLEN);
    362 	return (m);
    363 }
    364 
    365 void
    366 m_clget(struct mbuf *m, int nowait)
    367 {
    368 
    369 	MCLGET(m, nowait);
    370 }
    371 
    372 struct mbuf *
    373 m_free(struct mbuf *m)
    374 {
    375 	struct mbuf *n;
    376 
    377 	MFREE(m, n);
    378 	return (n);
    379 }
    380 
    381 void
    382 m_freem(struct mbuf *m)
    383 {
    384 	struct mbuf *n;
    385 
    386 	if (m == NULL)
    387 		return;
    388 	do {
    389 		MFREE(m, n);
    390 		m = n;
    391 	} while (m);
    392 }
    393 
    394 #ifdef MBUFTRACE
    395 void
    396 m_claim(struct mbuf *m, struct mowner *mo)
    397 {
    398 
    399 	for (; m != NULL; m = m->m_next)
    400 		MCLAIM(m, mo);
    401 }
    402 #endif
    403 
    404 /*
    405  * Mbuffer utility routines.
    406  */
    407 
    408 /*
    409  * Lesser-used path for M_PREPEND:
    410  * allocate new mbuf to prepend to chain,
    411  * copy junk along.
    412  */
    413 struct mbuf *
    414 m_prepend(struct mbuf *m, int len, int how)
    415 {
    416 	struct mbuf *mn;
    417 
    418 	MGET(mn, how, m->m_type);
    419 	if (mn == (struct mbuf *)NULL) {
    420 		m_freem(m);
    421 		return ((struct mbuf *)NULL);
    422 	}
    423 	if (m->m_flags & M_PKTHDR) {
    424 		M_COPY_PKTHDR(mn, m);
    425 		m_tag_delete_chain(m, NULL);
    426 		m->m_flags &= ~M_PKTHDR;
    427 	} else {
    428 		MCLAIM(mn, m->m_owner);
    429 	}
    430 	mn->m_next = m;
    431 	m = mn;
    432 	if (len < MHLEN)
    433 		MH_ALIGN(m, len);
    434 	m->m_len = len;
    435 	return (m);
    436 }
    437 
    438 /*
    439  * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
    440  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
    441  * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
    442  */
    443 int MCFail;
    444 
    445 struct mbuf *
    446 m_copym(struct mbuf *m, int off0, int len, int wait)
    447 {
    448 
    449 	return m_copym0(m, off0, len, wait, 0);	/* shallow copy on M_EXT */
    450 }
    451 
    452 struct mbuf *
    453 m_dup(struct mbuf *m, int off0, int len, int wait)
    454 {
    455 
    456 	return m_copym0(m, off0, len, wait, 1);	/* deep copy */
    457 }
    458 
    459 static struct mbuf *
    460 m_copym0(struct mbuf *m, int off0, int len, int wait, int deep)
    461 {
    462 	struct mbuf *n, **np;
    463 	int off = off0;
    464 	struct mbuf *top;
    465 	int copyhdr = 0;
    466 
    467 	if (off < 0 || len < 0)
    468 		panic("m_copym: off %d, len %d", off, len);
    469 	if (off == 0 && m->m_flags & M_PKTHDR)
    470 		copyhdr = 1;
    471 	while (off > 0) {
    472 		if (m == 0)
    473 			panic("m_copym: m == 0");
    474 		if (off < m->m_len)
    475 			break;
    476 		off -= m->m_len;
    477 		m = m->m_next;
    478 	}
    479 	np = &top;
    480 	top = 0;
    481 	while (len > 0) {
    482 		if (m == 0) {
    483 			if (len != M_COPYALL)
    484 				panic("m_copym: m == 0 and not COPYALL");
    485 			break;
    486 		}
    487 		MGET(n, wait, m->m_type);
    488 		*np = n;
    489 		if (n == 0)
    490 			goto nospace;
    491 		MCLAIM(n, m->m_owner);
    492 		if (copyhdr) {
    493 			M_COPY_PKTHDR(n, m);
    494 			if (len == M_COPYALL)
    495 				n->m_pkthdr.len -= off0;
    496 			else
    497 				n->m_pkthdr.len = len;
    498 			copyhdr = 0;
    499 		}
    500 		n->m_len = min(len, m->m_len - off);
    501 		if (m->m_flags & M_EXT) {
    502 			if (!deep) {
    503 				n->m_data = m->m_data + off;
    504 				n->m_ext = m->m_ext;
    505 				MCLADDREFERENCE(m, n);
    506 			} else {
    507 				/*
    508 				 * we are unsure about the way m was allocated.
    509 				 * copy into multiple MCLBYTES cluster mbufs.
    510 				 */
    511 				MCLGET(n, wait);
    512 				n->m_len = 0;
    513 				n->m_len = M_TRAILINGSPACE(n);
    514 				n->m_len = min(n->m_len, len);
    515 				n->m_len = min(n->m_len, m->m_len - off);
    516 				memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + off,
    517 				    (unsigned)n->m_len);
    518 			}
    519 		} else
    520 			memcpy(mtod(n, caddr_t), mtod(m, caddr_t)+off,
    521 			    (unsigned)n->m_len);
    522 		if (len != M_COPYALL)
    523 			len -= n->m_len;
    524 		off += n->m_len;
    525 #ifdef DIAGNOSTIC
    526 		if (off > m->m_len)
    527 			panic("m_copym0 overrun");
    528 #endif
    529 		if (off == m->m_len) {
    530 			m = m->m_next;
    531 			off = 0;
    532 		}
    533 		np = &n->m_next;
    534 	}
    535 	if (top == 0)
    536 		MCFail++;
    537 	return (top);
    538 nospace:
    539 	m_freem(top);
    540 	MCFail++;
    541 	return (NULL);
    542 }
    543 
    544 /*
    545  * Copy an entire packet, including header (which must be present).
    546  * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
    547  */
    548 struct mbuf *
    549 m_copypacket(struct mbuf *m, int how)
    550 {
    551 	struct mbuf *top, *n, *o;
    552 
    553 	MGET(n, how, m->m_type);
    554 	top = n;
    555 	if (!n)
    556 		goto nospace;
    557 
    558 	MCLAIM(n, m->m_owner);
    559 	M_COPY_PKTHDR(n, m);
    560 	n->m_len = m->m_len;
    561 	if (m->m_flags & M_EXT) {
    562 		n->m_data = m->m_data;
    563 		n->m_ext = m->m_ext;
    564 		MCLADDREFERENCE(m, n);
    565 	} else {
    566 		memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
    567 	}
    568 
    569 	m = m->m_next;
    570 	while (m) {
    571 		MGET(o, how, m->m_type);
    572 		if (!o)
    573 			goto nospace;
    574 
    575 		MCLAIM(o, m->m_owner);
    576 		n->m_next = o;
    577 		n = n->m_next;
    578 
    579 		n->m_len = m->m_len;
    580 		if (m->m_flags & M_EXT) {
    581 			n->m_data = m->m_data;
    582 			n->m_ext = m->m_ext;
    583 			MCLADDREFERENCE(m, n);
    584 		} else {
    585 			memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
    586 		}
    587 
    588 		m = m->m_next;
    589 	}
    590 	return top;
    591 nospace:
    592 	m_freem(top);
    593 	MCFail++;
    594 	return NULL;
    595 }
    596 
    597 /*
    598  * Copy data from an mbuf chain starting "off" bytes from the beginning,
    599  * continuing for "len" bytes, into the indicated buffer.
    600  */
    601 void
    602 m_copydata(struct mbuf *m, int off, int len, caddr_t cp)
    603 {
    604 	unsigned count;
    605 
    606 	if (off < 0 || len < 0)
    607 		panic("m_copydata");
    608 	while (off > 0) {
    609 		if (m == 0)
    610 			panic("m_copydata");
    611 		if (off < m->m_len)
    612 			break;
    613 		off -= m->m_len;
    614 		m = m->m_next;
    615 	}
    616 	while (len > 0) {
    617 		if (m == 0)
    618 			panic("m_copydata");
    619 		count = min(m->m_len - off, len);
    620 		memcpy(cp, mtod(m, caddr_t) + off, count);
    621 		len -= count;
    622 		cp += count;
    623 		off = 0;
    624 		m = m->m_next;
    625 	}
    626 }
    627 
    628 /*
    629  * Concatenate mbuf chain n to m.
    630  * n might be copied into m (when n->m_len is small), therefore data portion of
    631  * n could be copied into an mbuf of different mbuf type.
    632  * Therefore both chains should be of the same type (e.g. MT_DATA).
    633  * Any m_pkthdr is not updated.
    634  */
    635 void
    636 m_cat(struct mbuf *m, struct mbuf *n)
    637 {
    638 
    639 	KASSERT(n == NULL || m->m_type == n->m_type);
    640 
    641 	while (m->m_next)
    642 		m = m->m_next;
    643 	while (n) {
    644 		if (m->m_flags & M_EXT ||
    645 		    m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
    646 			/* just join the two chains */
    647 			m->m_next = n;
    648 			return;
    649 		}
    650 		/* splat the data from one into the other */
    651 		memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
    652 		    (u_int)n->m_len);
    653 		m->m_len += n->m_len;
    654 		n = m_free(n);
    655 	}
    656 }
    657 
    658 void
    659 m_adj(struct mbuf *mp, int req_len)
    660 {
    661 	int len = req_len;
    662 	struct mbuf *m;
    663 	int count;
    664 
    665 	if ((m = mp) == NULL)
    666 		return;
    667 	if (len >= 0) {
    668 		/*
    669 		 * Trim from head.
    670 		 */
    671 		while (m != NULL && len > 0) {
    672 			if (m->m_len <= len) {
    673 				len -= m->m_len;
    674 				m->m_len = 0;
    675 				m = m->m_next;
    676 			} else {
    677 				m->m_len -= len;
    678 				m->m_data += len;
    679 				len = 0;
    680 			}
    681 		}
    682 		m = mp;
    683 		if (mp->m_flags & M_PKTHDR)
    684 			m->m_pkthdr.len -= (req_len - len);
    685 	} else {
    686 		/*
    687 		 * Trim from tail.  Scan the mbuf chain,
    688 		 * calculating its length and finding the last mbuf.
    689 		 * If the adjustment only affects this mbuf, then just
    690 		 * adjust and return.  Otherwise, rescan and truncate
    691 		 * after the remaining size.
    692 		 */
    693 		len = -len;
    694 		count = 0;
    695 		for (;;) {
    696 			count += m->m_len;
    697 			if (m->m_next == (struct mbuf *)0)
    698 				break;
    699 			m = m->m_next;
    700 		}
    701 		if (m->m_len >= len) {
    702 			m->m_len -= len;
    703 			if (mp->m_flags & M_PKTHDR)
    704 				mp->m_pkthdr.len -= len;
    705 			return;
    706 		}
    707 		count -= len;
    708 		if (count < 0)
    709 			count = 0;
    710 		/*
    711 		 * Correct length for chain is "count".
    712 		 * Find the mbuf with last data, adjust its length,
    713 		 * and toss data from remaining mbufs on chain.
    714 		 */
    715 		m = mp;
    716 		if (m->m_flags & M_PKTHDR)
    717 			m->m_pkthdr.len = count;
    718 		for (; m; m = m->m_next) {
    719 			if (m->m_len >= count) {
    720 				m->m_len = count;
    721 				break;
    722 			}
    723 			count -= m->m_len;
    724 		}
    725 		while (m->m_next)
    726 			(m = m->m_next) ->m_len = 0;
    727 	}
    728 }
    729 
    730 /*
    731  * Rearange an mbuf chain so that len bytes are contiguous
    732  * and in the data area of an mbuf (so that mtod and dtom
    733  * will work for a structure of size len).  Returns the resulting
    734  * mbuf chain on success, frees it and returns null on failure.
    735  * If there is room, it will add up to max_protohdr-len extra bytes to the
    736  * contiguous region in an attempt to avoid being called next time.
    737  */
    738 int MPFail;
    739 
    740 struct mbuf *
    741 m_pullup(struct mbuf *n, int len)
    742 {
    743 	struct mbuf *m;
    744 	int count;
    745 	int space;
    746 
    747 	/*
    748 	 * If first mbuf has no cluster, and has room for len bytes
    749 	 * without shifting current data, pullup into it,
    750 	 * otherwise allocate a new mbuf to prepend to the chain.
    751 	 */
    752 	if ((n->m_flags & M_EXT) == 0 &&
    753 	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
    754 		if (n->m_len >= len)
    755 			return (n);
    756 		m = n;
    757 		n = n->m_next;
    758 		len -= m->m_len;
    759 	} else {
    760 		if (len > MHLEN)
    761 			goto bad;
    762 		MGET(m, M_DONTWAIT, n->m_type);
    763 		if (m == 0)
    764 			goto bad;
    765 		MCLAIM(m, n->m_owner);
    766 		m->m_len = 0;
    767 		if (n->m_flags & M_PKTHDR) {
    768 			M_COPY_PKTHDR(m, n);
    769 			m_tag_delete_chain(n, NULL);
    770 			n->m_flags &= ~M_PKTHDR;
    771 		}
    772 	}
    773 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
    774 	do {
    775 		count = min(min(max(len, max_protohdr), space), n->m_len);
    776 		memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
    777 		  (unsigned)count);
    778 		len -= count;
    779 		m->m_len += count;
    780 		n->m_len -= count;
    781 		space -= count;
    782 		if (n->m_len)
    783 			n->m_data += count;
    784 		else
    785 			n = m_free(n);
    786 	} while (len > 0 && n);
    787 	if (len > 0) {
    788 		(void) m_free(m);
    789 		goto bad;
    790 	}
    791 	m->m_next = n;
    792 	return (m);
    793 bad:
    794 	m_freem(n);
    795 	MPFail++;
    796 	return (NULL);
    797 }
    798 
    799 /*
    800  * Like m_pullup(), except a new mbuf is always allocated, and we allow
    801  * the amount of empty space before the data in the new mbuf to be specified
    802  * (in the event that the caller expects to prepend later).
    803  */
    804 int MSFail;
    805 
    806 struct mbuf *
    807 m_copyup(struct mbuf *n, int len, int dstoff)
    808 {
    809 	struct mbuf *m;
    810 	int count, space;
    811 
    812 	if (len > (MHLEN - dstoff))
    813 		goto bad;
    814 	MGET(m, M_DONTWAIT, n->m_type);
    815 	if (m == NULL)
    816 		goto bad;
    817 	MCLAIM(m, n->m_owner);
    818 	m->m_len = 0;
    819 	if (n->m_flags & M_PKTHDR) {
    820 		M_COPY_PKTHDR(m, n);
    821 		m_tag_delete_chain(m, NULL);
    822 		n->m_flags &= ~M_PKTHDR;
    823 	}
    824 	m->m_data += dstoff;
    825 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
    826 	do {
    827 		count = min(min(max(len, max_protohdr), space), n->m_len);
    828 		memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
    829 		    (unsigned)count);
    830 		len -= count;
    831 		m->m_len += count;
    832 		n->m_len -= count;
    833 		space -= count;
    834 		if (n->m_len)
    835 			n->m_data += count;
    836 		else
    837 			n = m_free(n);
    838 	} while (len > 0 && n);
    839 	if (len > 0) {
    840 		(void) m_free(m);
    841 		goto bad;
    842 	}
    843 	m->m_next = n;
    844 	return (m);
    845  bad:
    846 	m_freem(n);
    847 	MSFail++;
    848 	return (NULL);
    849 }
    850 
    851 /*
    852  * Partition an mbuf chain in two pieces, returning the tail --
    853  * all but the first len0 bytes.  In case of failure, it returns NULL and
    854  * attempts to restore the chain to its original state.
    855  */
    856 struct mbuf *
    857 m_split(struct mbuf *m0, int len0, int wait)
    858 {
    859 	struct mbuf *m, *n;
    860 	unsigned len = len0, remain, len_save;
    861 
    862 	for (m = m0; m && len > m->m_len; m = m->m_next)
    863 		len -= m->m_len;
    864 	if (m == 0)
    865 		return (NULL);
    866 	remain = m->m_len - len;
    867 	if (m0->m_flags & M_PKTHDR) {
    868 		MGETHDR(n, wait, m0->m_type);
    869 		if (n == 0)
    870 			return (NULL);
    871 		MCLAIM(m, m0->m_owner);
    872 		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
    873 		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
    874 		len_save = m0->m_pkthdr.len;
    875 		m0->m_pkthdr.len = len0;
    876 		if (m->m_flags & M_EXT)
    877 			goto extpacket;
    878 		if (remain > MHLEN) {
    879 			/* m can't be the lead packet */
    880 			MH_ALIGN(n, 0);
    881 			n->m_next = m_split(m, len, wait);
    882 			if (n->m_next == 0) {
    883 				(void) m_free(n);
    884 				m0->m_pkthdr.len = len_save;
    885 				return (NULL);
    886 			} else
    887 				return (n);
    888 		} else
    889 			MH_ALIGN(n, remain);
    890 	} else if (remain == 0) {
    891 		n = m->m_next;
    892 		m->m_next = 0;
    893 		return (n);
    894 	} else {
    895 		MGET(n, wait, m->m_type);
    896 		if (n == 0)
    897 			return (NULL);
    898 		MCLAIM(n, m->m_owner);
    899 		M_ALIGN(n, remain);
    900 	}
    901 extpacket:
    902 	if (m->m_flags & M_EXT) {
    903 		n->m_ext = m->m_ext;
    904 		MCLADDREFERENCE(m, n);
    905 		n->m_data = m->m_data + len;
    906 	} else {
    907 		memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + len, remain);
    908 	}
    909 	n->m_len = remain;
    910 	m->m_len = len;
    911 	n->m_next = m->m_next;
    912 	m->m_next = 0;
    913 	return (n);
    914 }
    915 /*
    916  * Routine to copy from device local memory into mbufs.
    917  */
    918 struct mbuf *
    919 m_devget(char *buf, int totlen, int off0, struct ifnet *ifp,
    920     void (*copy)(const void *from, void *to, size_t len))
    921 {
    922 	struct mbuf *m;
    923 	struct mbuf *top = 0, **mp = &top;
    924 	int off = off0, len;
    925 	char *cp;
    926 	char *epkt;
    927 
    928 	cp = buf;
    929 	epkt = cp + totlen;
    930 	if (off) {
    931 		/*
    932 		 * If 'off' is non-zero, packet is trailer-encapsulated,
    933 		 * so we have to skip the type and length fields.
    934 		 */
    935 		cp += off + 2 * sizeof(u_int16_t);
    936 		totlen -= 2 * sizeof(u_int16_t);
    937 	}
    938 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    939 	if (m == 0)
    940 		return (NULL);
    941 	m->m_pkthdr.rcvif = ifp;
    942 	m->m_pkthdr.len = totlen;
    943 	m->m_len = MHLEN;
    944 
    945 	while (totlen > 0) {
    946 		if (top) {
    947 			MGET(m, M_DONTWAIT, MT_DATA);
    948 			if (m == 0) {
    949 				m_freem(top);
    950 				return (NULL);
    951 			}
    952 			m->m_len = MLEN;
    953 		}
    954 		len = min(totlen, epkt - cp);
    955 		if (len >= MINCLSIZE) {
    956 			MCLGET(m, M_DONTWAIT);
    957 			if ((m->m_flags & M_EXT) == 0) {
    958 				m_free(m);
    959 				m_freem(top);
    960 				return (NULL);
    961 			}
    962 			m->m_len = len = min(len, MCLBYTES);
    963 		} else {
    964 			/*
    965 			 * Place initial small packet/header at end of mbuf.
    966 			 */
    967 			if (len < m->m_len) {
    968 				if (top == 0 && len + max_linkhdr <= m->m_len)
    969 					m->m_data += max_linkhdr;
    970 				m->m_len = len;
    971 			} else
    972 				len = m->m_len;
    973 		}
    974 		if (copy)
    975 			copy(cp, mtod(m, caddr_t), (size_t)len);
    976 		else
    977 			memcpy(mtod(m, caddr_t), cp, (size_t)len);
    978 		cp += len;
    979 		*mp = m;
    980 		mp = &m->m_next;
    981 		totlen -= len;
    982 		if (cp == epkt)
    983 			cp = buf;
    984 	}
    985 	return (top);
    986 }
    987 
    988 /*
    989  * Copy data from a buffer back into the indicated mbuf chain,
    990  * starting "off" bytes from the beginning, extending the mbuf
    991  * chain if necessary.
    992  */
    993 void
    994 m_copyback(struct mbuf *m0, int off, int len, caddr_t cp)
    995 {
    996 	int mlen;
    997 	struct mbuf *m = m0, *n;
    998 	int totlen = 0;
    999 
   1000 	if (m0 == 0)
   1001 		return;
   1002 	while (off > (mlen = m->m_len)) {
   1003 		off -= mlen;
   1004 		totlen += mlen;
   1005 		if (m->m_next == 0) {
   1006 			n = m_getclr(M_DONTWAIT, m->m_type);
   1007 			if (n == 0)
   1008 				goto out;
   1009 			n->m_len = min(MLEN, len + off);
   1010 			m->m_next = n;
   1011 		}
   1012 		m = m->m_next;
   1013 	}
   1014 	while (len > 0) {
   1015 		mlen = min (m->m_len - off, len);
   1016 		memcpy(mtod(m, caddr_t) + off, cp, (unsigned)mlen);
   1017 		cp += mlen;
   1018 		len -= mlen;
   1019 		mlen += off;
   1020 		off = 0;
   1021 		totlen += mlen;
   1022 		if (len == 0)
   1023 			break;
   1024 		if (m->m_next == 0) {
   1025 			n = m_get(M_DONTWAIT, m->m_type);
   1026 			if (n == 0)
   1027 				break;
   1028 			n->m_len = min(MLEN, len);
   1029 			m->m_next = n;
   1030 		}
   1031 		m = m->m_next;
   1032 	}
   1033 out:	if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
   1034 		m->m_pkthdr.len = totlen;
   1035 }
   1036 
   1037 /*
   1038  * Apply function f to the data in an mbuf chain starting "off" bytes from the
   1039  * beginning, continuing for "len" bytes.
   1040  */
   1041 int
   1042 m_apply(struct mbuf *m, int off, int len,
   1043     int (*f)(void *, caddr_t, unsigned int), void *arg)
   1044 {
   1045 	unsigned int count;
   1046 	int rval;
   1047 
   1048 	KASSERT(len >= 0);
   1049 	KASSERT(off >= 0);
   1050 
   1051 	while (off > 0) {
   1052 		KASSERT(m != NULL);
   1053 		if (off < m->m_len)
   1054 			break;
   1055 		off -= m->m_len;
   1056 		m = m->m_next;
   1057 	}
   1058 	while (len > 0) {
   1059 		KASSERT(m != NULL);
   1060 		count = min(m->m_len - off, len);
   1061 
   1062 		rval = (*f)(arg, mtod(m, caddr_t) + off, count);
   1063 		if (rval)
   1064 			return (rval);
   1065 
   1066 		len -= count;
   1067 		off = 0;
   1068 		m = m->m_next;
   1069 	}
   1070 
   1071 	return (0);
   1072 }
   1073 
   1074 /*
   1075  * Return a pointer to mbuf/offset of location in mbuf chain.
   1076  */
   1077 struct mbuf *
   1078 m_getptr(struct mbuf *m, int loc, int *off)
   1079 {
   1080 
   1081 	while (loc >= 0) {
   1082 		/* Normal end of search */
   1083 		if (m->m_len > loc) {
   1084 	    		*off = loc;
   1085 	    		return (m);
   1086 		} else {
   1087 	    		loc -= m->m_len;
   1088 
   1089 	    		if (m->m_next == NULL) {
   1090 				if (loc == 0) {
   1091  					/* Point at the end of valid data */
   1092 		    			*off = m->m_len;
   1093 		    			return (m);
   1094 				} else
   1095 		  			return (NULL);
   1096 	    		} else
   1097 	      			m = m->m_next;
   1098 		}
   1099     	}
   1100 
   1101 	return (NULL);
   1102 }
   1103