uipc_mbuf.c revision 32036
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1982, 1986, 1988, 1991, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 3. All advertising materials mentioning features or use of this software
141541Srgrimes *    must display the following acknowledgement:
151541Srgrimes *	This product includes software developed by the University of
161541Srgrimes *	California, Berkeley and its contributors.
171541Srgrimes * 4. Neither the name of the University nor the names of its contributors
181541Srgrimes *    may be used to endorse or promote products derived from this software
191541Srgrimes *    without specific prior written permission.
201541Srgrimes *
211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311541Srgrimes * SUCH DAMAGE.
321541Srgrimes *
331541Srgrimes *	@(#)uipc_mbuf.c	8.2 (Berkeley) 1/4/94
3432036Sbde *	$Id: uipc_mbuf.c,v 1.32 1997/10/28 15:58:22 bde Exp $
351541Srgrimes */
361541Srgrimes
371541Srgrimes#include <sys/param.h>
381541Srgrimes#include <sys/systm.h>
3932036Sbde#include <sys/malloc.h>
401541Srgrimes#include <sys/mbuf.h>
411541Srgrimes#include <sys/kernel.h>
4223081Swollman#include <sys/sysctl.h>
431541Srgrimes#include <sys/domain.h>
441541Srgrimes#include <sys/protosw.h>
451541Srgrimes
461541Srgrimes#include <vm/vm.h>
479759Sbde#include <vm/vm_kern.h>
4812662Sdg#include <vm/vm_extern.h>
491541Srgrimes
5010653Sdgstatic void mbinit __P((void *));
5110358SjulianSYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbinit, NULL)
5210358Sjulian
539759Sbdestruct mbuf *mbutl;
541541Srgrimeschar	*mclrefcnt;
559759Sbdestruct mbstat mbstat;
5615689Swollmanstruct mbuf *mmbfree;
579759Sbdeunion mcluster *mclfree;
589759Sbdeint	max_linkhdr;
599759Sbdeint	max_protohdr;
609759Sbdeint	max_hdr;
619759Sbdeint	max_datalen;
621541Srgrimes
6323081SwollmanSYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RW,
6423081Swollman	   &max_linkhdr, 0, "");
6523081SwollmanSYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RW,
6623081Swollman	   &max_protohdr, 0, "");
6723081SwollmanSYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RW, &max_hdr, 0, "");
6823081SwollmanSYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RW,
6923081Swollman	   &max_datalen, 0, "");
7023081SwollmanSYSCTL_STRUCT(_kern_ipc, KIPC_MBSTAT, mbstat, CTLFLAG_RW, &mbstat, mbstat, "");
7123081Swollman
7212819Sphkstatic void	m_reclaim __P((void));
7312819Sphk
7415736Sphk/* "number of clusters of pages" */
7515736Sphk#define NCL_INIT	1
7615736Sphk
7715744Sphk#define NMB_INIT	16
7815744Sphk
7910358Sjulian/* ARGSUSED*/
8010358Sjulianstatic void
8112569Sbdembinit(dummy)
8212569Sbde	void *dummy;
831541Srgrimes{
841541Srgrimes	int s;
851541Srgrimes
8615689Swollman	mmbfree = NULL; mclfree = NULL;
8723081Swollman	mbstat.m_msize = MSIZE;
8823081Swollman	mbstat.m_mclbytes = MCLBYTES;
8923081Swollman	mbstat.m_minclsize = MINCLSIZE;
9023081Swollman	mbstat.m_mlen = MLEN;
9123081Swollman	mbstat.m_mhlen = MHLEN;
9223081Swollman
931541Srgrimes	s = splimp();
9415689Swollman	if (m_mballoc(NMB_INIT, M_DONTWAIT) == 0)
9515689Swollman		goto bad;
9622671Swollman#if MCLBYTES <= PAGE_SIZE
971541Srgrimes	if (m_clalloc(NCL_INIT, M_DONTWAIT) == 0)
981541Srgrimes		goto bad;
9922671Swollman#else
10022671Swollman	/* It's OK to call contigmalloc in this context. */
10132036Sbde	if (m_clalloc(16, M_WAIT) == 0)
10222671Swollman		goto bad;
10322671Swollman#endif
1041541Srgrimes	splx(s);
1051541Srgrimes	return;
1061541Srgrimesbad:
1071541Srgrimes	panic("mbinit");
1081541Srgrimes}
1091541Srgrimes
1101541Srgrimes/*
11115689Swollman * Allocate at least nmb mbufs and place on mbuf free list.
11215689Swollman * Must be called at splimp.
11315689Swollman */
11415689Swollman/* ARGSUSED */
11515689Swollmanint
11632036Sbdem_mballoc(nmb, how)
11715689Swollman	register int nmb;
11832036Sbde	int how;
11915689Swollman{
12015689Swollman	register caddr_t p;
12115689Swollman	register int i;
12215689Swollman	int nbytes;
12315689Swollman
12415689Swollman	/* Once we run out of map space, it will be impossible to get
12515689Swollman	 * any more (nothing is ever freed back to the map) (XXX which
12615689Swollman	 * is dumb). (however you are not dead as m_reclaim might
12715689Swollman	 * still be able to free a substantial amount of space).
12815689Swollman	 */
12915689Swollman	if (mb_map_full)
13015689Swollman		return (0);
13115689Swollman
13215689Swollman	nbytes = round_page(nmb * MSIZE);
13322899Swollman	p = (caddr_t)kmem_malloc(mb_map, nbytes, M_NOWAIT);
13432036Sbde	if (p == 0 && how == M_WAIT) {
13522899Swollman		mbstat.m_wait++;
13622899Swollman		p = (caddr_t)kmem_malloc(mb_map, nbytes, M_WAITOK);
13722899Swollman	}
13822899Swollman
13915689Swollman	/*
14032036Sbde	 * Either the map is now full, or `how' is M_NOWAIT and there
14115689Swollman	 * are no pages left.
14215689Swollman	 */
14315689Swollman	if (p == NULL)
14415689Swollman		return (0);
14515689Swollman
14615689Swollman	nmb = nbytes / MSIZE;
14715689Swollman	for (i = 0; i < nmb; i++) {
14815689Swollman		((struct mbuf *)p)->m_next = mmbfree;
14915689Swollman		mmbfree = (struct mbuf *)p;
15015689Swollman		p += MSIZE;
15115689Swollman	}
15215689Swollman	mbstat.m_mbufs += nmb;
15315689Swollman	return (1);
15415689Swollman}
15515689Swollman
15622671Swollman#if MCLBYTES > PAGE_SIZE
15722899Swollmanstatic int i_want_my_mcl;
15822671Swollman
15922899Swollmanstatic void
16022671Swollmankproc_mclalloc(void)
16122671Swollman{
16222671Swollman	int status;
16322671Swollman
16422671Swollman	while (1) {
16522671Swollman		tsleep(&i_want_my_mcl, PVM, "mclalloc", 0);
16622671Swollman
16722671Swollman		for (; i_want_my_mcl; i_want_my_mcl--) {
16832036Sbde			if (m_clalloc(1, M_WAIT) == 0)
16922671Swollman				printf("m_clalloc failed even in process context!\n");
17022671Swollman		}
17122671Swollman	}
17222671Swollman}
17322671Swollman
17422671Swollmanstatic struct proc *mclallocproc;
17522671Swollmanstatic struct kproc_desc mclalloc_kp = {
17622671Swollman	"mclalloc",
17722671Swollman	kproc_mclalloc,
17822671Swollman	&mclallocproc
17922671Swollman};
18022671SwollmanSYSINIT_KT(mclallocproc, SI_SUB_KTHREAD_UPDATE, SI_ORDER_ANY, kproc_start,
18122671Swollman	   &mclalloc_kp);
18222671Swollman#endif
18322671Swollman
18415689Swollman/*
1851541Srgrimes * Allocate some number of mbuf clusters
1861541Srgrimes * and place on cluster free list.
1871541Srgrimes * Must be called at splimp.
1881541Srgrimes */
1891541Srgrimes/* ARGSUSED */
1901549Srgrimesint
19132036Sbdem_clalloc(ncl, how)
1921541Srgrimes	register int ncl;
19332036Sbde	int how;
1941541Srgrimes{
1951541Srgrimes	register caddr_t p;
1961541Srgrimes	register int i;
1971541Srgrimes	int npg;
1981541Srgrimes
1997066Sdg	/*
2007066Sdg	 * Once we run out of map space, it will be impossible
2017066Sdg	 * to get any more (nothing is ever freed back to the
2027066Sdg	 * map).
2037066Sdg	 */
20422899Swollman	if (mb_map_full) {
20522899Swollman		mbstat.m_drops++;
2067066Sdg		return (0);
20722899Swollman	}
2087066Sdg
20922671Swollman#if MCLBYTES > PAGE_SIZE
21032036Sbde	if (how != M_WAIT) {
21122671Swollman		i_want_my_mcl += ncl;
21222671Swollman		wakeup(&i_want_my_mcl);
21322899Swollman		mbstat.m_wait++;
21422671Swollman		p = 0;
21522671Swollman	} else {
21622671Swollman		p = contigmalloc1(MCLBYTES * ncl, M_DEVBUF, M_WAITOK, 0ul,
21722671Swollman				  ~0ul, PAGE_SIZE, 0, mb_map);
21822671Swollman	}
21922671Swollman#else
22015543Sphk	npg = ncl;
22121737Sdg	p = (caddr_t)kmem_malloc(mb_map, ctob(npg),
22232036Sbde				 how != M_WAIT ? M_NOWAIT : M_WAITOK);
22322671Swollman	ncl = ncl * PAGE_SIZE / MCLBYTES;
22422671Swollman#endif
2257066Sdg	/*
22632036Sbde	 * Either the map is now full, or `how' is M_NOWAIT and there
2277066Sdg	 * are no pages left.
2287066Sdg	 */
22922899Swollman	if (p == NULL) {
23022899Swollman		mbstat.m_drops++;
2311541Srgrimes		return (0);
23222899Swollman	}
2337066Sdg
2341541Srgrimes	for (i = 0; i < ncl; i++) {
2351541Srgrimes		((union mcluster *)p)->mcl_next = mclfree;
2361541Srgrimes		mclfree = (union mcluster *)p;
2371541Srgrimes		p += MCLBYTES;
2381541Srgrimes		mbstat.m_clfree++;
2391541Srgrimes	}
2401541Srgrimes	mbstat.m_clusters += ncl;
2411541Srgrimes	return (1);
2421541Srgrimes}
2431541Srgrimes
2441541Srgrimes/*
2451541Srgrimes * When MGET failes, ask protocols to free space when short of memory,
2461541Srgrimes * then re-attempt to allocate an mbuf.
2471541Srgrimes */
2481541Srgrimesstruct mbuf *
2491541Srgrimesm_retry(i, t)
2501541Srgrimes	int i, t;
2511541Srgrimes{
2521541Srgrimes	register struct mbuf *m;
2531541Srgrimes
2541541Srgrimes	m_reclaim();
2551541Srgrimes#define m_retry(i, t)	(struct mbuf *)0
2561541Srgrimes	MGET(m, i, t);
2571541Srgrimes#undef m_retry
2586669Sdg	if (m != NULL)
2596669Sdg		mbstat.m_wait++;
2606669Sdg	else
2616669Sdg		mbstat.m_drops++;
2621541Srgrimes	return (m);
2631541Srgrimes}
2641541Srgrimes
2651541Srgrimes/*
2661541Srgrimes * As above; retry an MGETHDR.
2671541Srgrimes */
2681541Srgrimesstruct mbuf *
2691541Srgrimesm_retryhdr(i, t)
2701541Srgrimes	int i, t;
2711541Srgrimes{
2721541Srgrimes	register struct mbuf *m;
2731541Srgrimes
2741541Srgrimes	m_reclaim();
2751541Srgrimes#define m_retryhdr(i, t) (struct mbuf *)0
2761541Srgrimes	MGETHDR(m, i, t);
2771541Srgrimes#undef m_retryhdr
2786669Sdg	if (m != NULL)
2796669Sdg		mbstat.m_wait++;
2806669Sdg	else
2816669Sdg		mbstat.m_drops++;
2821541Srgrimes	return (m);
2831541Srgrimes}
2841541Srgrimes
28512819Sphkstatic void
2861541Srgrimesm_reclaim()
2871541Srgrimes{
2881541Srgrimes	register struct domain *dp;
2891541Srgrimes	register struct protosw *pr;
2901541Srgrimes	int s = splimp();
2911541Srgrimes
2921541Srgrimes	for (dp = domains; dp; dp = dp->dom_next)
2931541Srgrimes		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
2941541Srgrimes			if (pr->pr_drain)
2951541Srgrimes				(*pr->pr_drain)();
2961541Srgrimes	splx(s);
2971541Srgrimes	mbstat.m_drain++;
2981541Srgrimes}
2991541Srgrimes
3001541Srgrimes/*
3011541Srgrimes * Space allocation routines.
3021541Srgrimes * These are also available as macros
3031541Srgrimes * for critical paths.
3041541Srgrimes */
3051541Srgrimesstruct mbuf *
30632036Sbdem_get(how, type)
30732036Sbde	int how, type;
3081541Srgrimes{
3091541Srgrimes	register struct mbuf *m;
3101541Srgrimes
31132036Sbde	MGET(m, how, type);
3121541Srgrimes	return (m);
3131541Srgrimes}
3141541Srgrimes
3151541Srgrimesstruct mbuf *
31632036Sbdem_gethdr(how, type)
31732036Sbde	int how, type;
3181541Srgrimes{
3191541Srgrimes	register struct mbuf *m;
3201541Srgrimes
32132036Sbde	MGETHDR(m, how, type);
3221541Srgrimes	return (m);
3231541Srgrimes}
3241541Srgrimes
3251541Srgrimesstruct mbuf *
32632036Sbdem_getclr(how, type)
32732036Sbde	int how, type;
3281541Srgrimes{
3291541Srgrimes	register struct mbuf *m;
3301541Srgrimes
33132036Sbde	MGET(m, how, type);
3321541Srgrimes	if (m == 0)
3331541Srgrimes		return (0);
3341541Srgrimes	bzero(mtod(m, caddr_t), MLEN);
3351541Srgrimes	return (m);
3361541Srgrimes}
3371541Srgrimes
3381541Srgrimesstruct mbuf *
3391541Srgrimesm_free(m)
3401541Srgrimes	struct mbuf *m;
3411541Srgrimes{
3421541Srgrimes	register struct mbuf *n;
3431541Srgrimes
3441541Srgrimes	MFREE(m, n);
3451541Srgrimes	return (n);
3461541Srgrimes}
3471541Srgrimes
3481541Srgrimesvoid
3491541Srgrimesm_freem(m)
3501541Srgrimes	register struct mbuf *m;
3511541Srgrimes{
3521541Srgrimes	register struct mbuf *n;
3531541Srgrimes
3541541Srgrimes	if (m == NULL)
3551541Srgrimes		return;
3561541Srgrimes	do {
3571541Srgrimes		MFREE(m, n);
3583308Sphk		m = n;
3593308Sphk	} while (m);
3601541Srgrimes}
3611541Srgrimes
3621541Srgrimes/*
3631541Srgrimes * Mbuffer utility routines.
3641541Srgrimes */
3651541Srgrimes
3661541Srgrimes/*
3671541Srgrimes * Lesser-used path for M_PREPEND:
3681541Srgrimes * allocate new mbuf to prepend to chain,
3691541Srgrimes * copy junk along.
3701541Srgrimes */
3711541Srgrimesstruct mbuf *
3721541Srgrimesm_prepend(m, len, how)
3731541Srgrimes	register struct mbuf *m;
3741541Srgrimes	int len, how;
3751541Srgrimes{
3761541Srgrimes	struct mbuf *mn;
3771541Srgrimes
3781541Srgrimes	MGET(mn, how, m->m_type);
3791541Srgrimes	if (mn == (struct mbuf *)NULL) {
3801541Srgrimes		m_freem(m);
3811541Srgrimes		return ((struct mbuf *)NULL);
3821541Srgrimes	}
3831541Srgrimes	if (m->m_flags & M_PKTHDR) {
3841541Srgrimes		M_COPY_PKTHDR(mn, m);
3851541Srgrimes		m->m_flags &= ~M_PKTHDR;
3861541Srgrimes	}
3871541Srgrimes	mn->m_next = m;
3881541Srgrimes	m = mn;
3891541Srgrimes	if (len < MHLEN)
3901541Srgrimes		MH_ALIGN(m, len);
3911541Srgrimes	m->m_len = len;
3921541Srgrimes	return (m);
3931541Srgrimes}
3941541Srgrimes
3951541Srgrimes/*
3961541Srgrimes * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
3971541Srgrimes * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
3981541Srgrimes * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
3991541Srgrimes */
40023081Swollman#define MCFail (mbstat.m_mcfail)
4011541Srgrimes
4021541Srgrimesstruct mbuf *
4031541Srgrimesm_copym(m, off0, len, wait)
4041541Srgrimes	register struct mbuf *m;
4051541Srgrimes	int off0, wait;
4061541Srgrimes	register int len;
4071541Srgrimes{
4081541Srgrimes	register struct mbuf *n, **np;
4091541Srgrimes	register int off = off0;
4101541Srgrimes	struct mbuf *top;
4111541Srgrimes	int copyhdr = 0;
4121541Srgrimes
4131541Srgrimes	if (off < 0 || len < 0)
4141541Srgrimes		panic("m_copym");
4151541Srgrimes	if (off == 0 && m->m_flags & M_PKTHDR)
4161541Srgrimes		copyhdr = 1;
4171541Srgrimes	while (off > 0) {
4181541Srgrimes		if (m == 0)
4191541Srgrimes			panic("m_copym");
4201541Srgrimes		if (off < m->m_len)
4211541Srgrimes			break;
4221541Srgrimes		off -= m->m_len;
4231541Srgrimes		m = m->m_next;
4241541Srgrimes	}
4251541Srgrimes	np = &top;
4261541Srgrimes	top = 0;
4271541Srgrimes	while (len > 0) {
4281541Srgrimes		if (m == 0) {
4291541Srgrimes			if (len != M_COPYALL)
4301541Srgrimes				panic("m_copym");
4311541Srgrimes			break;
4321541Srgrimes		}
4331541Srgrimes		MGET(n, wait, m->m_type);
4341541Srgrimes		*np = n;
4351541Srgrimes		if (n == 0)
4361541Srgrimes			goto nospace;
4371541Srgrimes		if (copyhdr) {
4381541Srgrimes			M_COPY_PKTHDR(n, m);
4391541Srgrimes			if (len == M_COPYALL)
4401541Srgrimes				n->m_pkthdr.len -= off0;
4411541Srgrimes			else
4421541Srgrimes				n->m_pkthdr.len = len;
4431541Srgrimes			copyhdr = 0;
4441541Srgrimes		}
4451541Srgrimes		n->m_len = min(len, m->m_len - off);
4461541Srgrimes		if (m->m_flags & M_EXT) {
4471541Srgrimes			n->m_data = m->m_data + off;
44817663Sjulian			if(!m->m_ext.ext_ref)
44917663Sjulian				mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
45017663Sjulian			else
45117663Sjulian				(*(m->m_ext.ext_ref))(m->m_ext.ext_buf,
45217663Sjulian							m->m_ext.ext_size);
4531541Srgrimes			n->m_ext = m->m_ext;
4541541Srgrimes			n->m_flags |= M_EXT;
4551541Srgrimes		} else
4561541Srgrimes			bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
4571541Srgrimes			    (unsigned)n->m_len);
4581541Srgrimes		if (len != M_COPYALL)
4591541Srgrimes			len -= n->m_len;
4601541Srgrimes		off = 0;
4611541Srgrimes		m = m->m_next;
4621541Srgrimes		np = &n->m_next;
4631541Srgrimes	}
4641541Srgrimes	if (top == 0)
4651541Srgrimes		MCFail++;
4661541Srgrimes	return (top);
4671541Srgrimesnospace:
4681541Srgrimes	m_freem(top);
4691541Srgrimes	MCFail++;
4701541Srgrimes	return (0);
4711541Srgrimes}
4721541Srgrimes
4731541Srgrimes/*
47415689Swollman * Copy an entire packet, including header (which must be present).
47515689Swollman * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
47615689Swollman */
47715689Swollmanstruct mbuf *
47815689Swollmanm_copypacket(m, how)
47915689Swollman	struct mbuf *m;
48015689Swollman	int how;
48115689Swollman{
48215689Swollman	struct mbuf *top, *n, *o;
48315689Swollman
48415689Swollman	MGET(n, how, m->m_type);
48515689Swollman	top = n;
48615689Swollman	if (!n)
48715689Swollman		goto nospace;
48815689Swollman
48915689Swollman	M_COPY_PKTHDR(n, m);
49015689Swollman	n->m_len = m->m_len;
49115689Swollman	if (m->m_flags & M_EXT) {
49215689Swollman		n->m_data = m->m_data;
49315689Swollman		mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
49415689Swollman		n->m_ext = m->m_ext;
49515689Swollman		n->m_flags |= M_EXT;
49615689Swollman	} else {
49715689Swollman		bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
49815689Swollman	}
49915689Swollman
50015689Swollman	m = m->m_next;
50115689Swollman	while (m) {
50215689Swollman		MGET(o, how, m->m_type);
50315689Swollman		if (!o)
50415689Swollman			goto nospace;
50515689Swollman
50615689Swollman		n->m_next = o;
50715689Swollman		n = n->m_next;
50815689Swollman
50915689Swollman		n->m_len = m->m_len;
51015689Swollman		if (m->m_flags & M_EXT) {
51115689Swollman			n->m_data = m->m_data;
51215689Swollman			mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
51315689Swollman			n->m_ext = m->m_ext;
51415689Swollman			n->m_flags |= M_EXT;
51515689Swollman		} else {
51615689Swollman			bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
51715689Swollman		}
51815689Swollman
51915689Swollman		m = m->m_next;
52015689Swollman	}
52115689Swollman	return top;
52215689Swollmannospace:
52315689Swollman	m_freem(top);
52415689Swollman	MCFail++;
52515689Swollman	return 0;
52615689Swollman}
52715689Swollman
52815689Swollman/*
5291541Srgrimes * Copy data from an mbuf chain starting "off" bytes from the beginning,
5301541Srgrimes * continuing for "len" bytes, into the indicated buffer.
5311541Srgrimes */
5321549Srgrimesvoid
5331541Srgrimesm_copydata(m, off, len, cp)
5341541Srgrimes	register struct mbuf *m;
5351541Srgrimes	register int off;
5361541Srgrimes	register int len;
5371541Srgrimes	caddr_t cp;
5381541Srgrimes{
5391541Srgrimes	register unsigned count;
5401541Srgrimes
5411541Srgrimes	if (off < 0 || len < 0)
5421541Srgrimes		panic("m_copydata");
5431541Srgrimes	while (off > 0) {
5441541Srgrimes		if (m == 0)
5451541Srgrimes			panic("m_copydata");
5461541Srgrimes		if (off < m->m_len)
5471541Srgrimes			break;
5481541Srgrimes		off -= m->m_len;
5491541Srgrimes		m = m->m_next;
5501541Srgrimes	}
5511541Srgrimes	while (len > 0) {
5521541Srgrimes		if (m == 0)
5531541Srgrimes			panic("m_copydata");
5541541Srgrimes		count = min(m->m_len - off, len);
5551541Srgrimes		bcopy(mtod(m, caddr_t) + off, cp, count);
5561541Srgrimes		len -= count;
5571541Srgrimes		cp += count;
5581541Srgrimes		off = 0;
5591541Srgrimes		m = m->m_next;
5601541Srgrimes	}
5611541Srgrimes}
5621541Srgrimes
5631541Srgrimes/*
5641541Srgrimes * Concatenate mbuf chain n to m.
5651541Srgrimes * Both chains must be of the same type (e.g. MT_DATA).
5661541Srgrimes * Any m_pkthdr is not updated.
5671541Srgrimes */
5681549Srgrimesvoid
5691541Srgrimesm_cat(m, n)
5701541Srgrimes	register struct mbuf *m, *n;
5711541Srgrimes{
5721541Srgrimes	while (m->m_next)
5731541Srgrimes		m = m->m_next;
5741541Srgrimes	while (n) {
5751541Srgrimes		if (m->m_flags & M_EXT ||
5761541Srgrimes		    m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
5771541Srgrimes			/* just join the two chains */
5781541Srgrimes			m->m_next = n;
5791541Srgrimes			return;
5801541Srgrimes		}
5811541Srgrimes		/* splat the data from one into the other */
5821541Srgrimes		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
5831541Srgrimes		    (u_int)n->m_len);
5841541Srgrimes		m->m_len += n->m_len;
5851541Srgrimes		n = m_free(n);
5861541Srgrimes	}
5871541Srgrimes}
5881541Srgrimes
5891549Srgrimesvoid
5901541Srgrimesm_adj(mp, req_len)
5911541Srgrimes	struct mbuf *mp;
5921541Srgrimes	int req_len;
5931541Srgrimes{
5941541Srgrimes	register int len = req_len;
5951541Srgrimes	register struct mbuf *m;
5961541Srgrimes	register count;
5971541Srgrimes
5981541Srgrimes	if ((m = mp) == NULL)
5991541Srgrimes		return;
6001541Srgrimes	if (len >= 0) {
6011541Srgrimes		/*
6021541Srgrimes		 * Trim from head.
6031541Srgrimes		 */
6041541Srgrimes		while (m != NULL && len > 0) {
6051541Srgrimes			if (m->m_len <= len) {
6061541Srgrimes				len -= m->m_len;
6071541Srgrimes				m->m_len = 0;
6081541Srgrimes				m = m->m_next;
6091541Srgrimes			} else {
6101541Srgrimes				m->m_len -= len;
6111541Srgrimes				m->m_data += len;
6121541Srgrimes				len = 0;
6131541Srgrimes			}
6141541Srgrimes		}
6151541Srgrimes		m = mp;
6161541Srgrimes		if (mp->m_flags & M_PKTHDR)
6171541Srgrimes			m->m_pkthdr.len -= (req_len - len);
6181541Srgrimes	} else {
6191541Srgrimes		/*
6201541Srgrimes		 * Trim from tail.  Scan the mbuf chain,
6211541Srgrimes		 * calculating its length and finding the last mbuf.
6221541Srgrimes		 * If the adjustment only affects this mbuf, then just
6231541Srgrimes		 * adjust and return.  Otherwise, rescan and truncate
6241541Srgrimes		 * after the remaining size.
6251541Srgrimes		 */
6261541Srgrimes		len = -len;
6271541Srgrimes		count = 0;
6281541Srgrimes		for (;;) {
6291541Srgrimes			count += m->m_len;
6301541Srgrimes			if (m->m_next == (struct mbuf *)0)
6311541Srgrimes				break;
6321541Srgrimes			m = m->m_next;
6331541Srgrimes		}
6341541Srgrimes		if (m->m_len >= len) {
6351541Srgrimes			m->m_len -= len;
6361541Srgrimes			if (mp->m_flags & M_PKTHDR)
6371541Srgrimes				mp->m_pkthdr.len -= len;
6381541Srgrimes			return;
6391541Srgrimes		}
6401541Srgrimes		count -= len;
6411541Srgrimes		if (count < 0)
6421541Srgrimes			count = 0;
6431541Srgrimes		/*
6441541Srgrimes		 * Correct length for chain is "count".
6451541Srgrimes		 * Find the mbuf with last data, adjust its length,
6461541Srgrimes		 * and toss data from remaining mbufs on chain.
6471541Srgrimes		 */
6481541Srgrimes		m = mp;
6491541Srgrimes		if (m->m_flags & M_PKTHDR)
6501541Srgrimes			m->m_pkthdr.len = count;
6511541Srgrimes		for (; m; m = m->m_next) {
6521541Srgrimes			if (m->m_len >= count) {
6531541Srgrimes				m->m_len = count;
6541541Srgrimes				break;
6551541Srgrimes			}
6561541Srgrimes			count -= m->m_len;
6571541Srgrimes		}
6583308Sphk		while (m->m_next)
6593308Sphk			(m = m->m_next) ->m_len = 0;
6601541Srgrimes	}
6611541Srgrimes}
6621541Srgrimes
6631541Srgrimes/*
6641541Srgrimes * Rearange an mbuf chain so that len bytes are contiguous
6651541Srgrimes * and in the data area of an mbuf (so that mtod and dtom
6661541Srgrimes * will work for a structure of size len).  Returns the resulting
6671541Srgrimes * mbuf chain on success, frees it and returns null on failure.
6681541Srgrimes * If there is room, it will add up to max_protohdr-len extra bytes to the
6691541Srgrimes * contiguous region in an attempt to avoid being called next time.
6701541Srgrimes */
67123081Swollman#define MPFail (mbstat.m_mpfail)
6721541Srgrimes
6731541Srgrimesstruct mbuf *
6741541Srgrimesm_pullup(n, len)
6751541Srgrimes	register struct mbuf *n;
6761541Srgrimes	int len;
6771541Srgrimes{
6781541Srgrimes	register struct mbuf *m;
6791541Srgrimes	register int count;
6801541Srgrimes	int space;
6811541Srgrimes
6821541Srgrimes	/*
6831541Srgrimes	 * If first mbuf has no cluster, and has room for len bytes
6841541Srgrimes	 * without shifting current data, pullup into it,
6851541Srgrimes	 * otherwise allocate a new mbuf to prepend to the chain.
6861541Srgrimes	 */
6871541Srgrimes	if ((n->m_flags & M_EXT) == 0 &&
6881541Srgrimes	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
6891541Srgrimes		if (n->m_len >= len)
6901541Srgrimes			return (n);
6911541Srgrimes		m = n;
6921541Srgrimes		n = n->m_next;
6931541Srgrimes		len -= m->m_len;
6941541Srgrimes	} else {
6951541Srgrimes		if (len > MHLEN)
6961541Srgrimes			goto bad;
6971541Srgrimes		MGET(m, M_DONTWAIT, n->m_type);
6981541Srgrimes		if (m == 0)
6991541Srgrimes			goto bad;
7001541Srgrimes		m->m_len = 0;
7011541Srgrimes		if (n->m_flags & M_PKTHDR) {
7021541Srgrimes			M_COPY_PKTHDR(m, n);
7031541Srgrimes			n->m_flags &= ~M_PKTHDR;
7041541Srgrimes		}
7051541Srgrimes	}
7061541Srgrimes	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
7071541Srgrimes	do {
7081541Srgrimes		count = min(min(max(len, max_protohdr), space), n->m_len);
7091541Srgrimes		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
7101541Srgrimes		  (unsigned)count);
7111541Srgrimes		len -= count;
7121541Srgrimes		m->m_len += count;
7131541Srgrimes		n->m_len -= count;
7141541Srgrimes		space -= count;
7151541Srgrimes		if (n->m_len)
7161541Srgrimes			n->m_data += count;
7171541Srgrimes		else
7181541Srgrimes			n = m_free(n);
7191541Srgrimes	} while (len > 0 && n);
7201541Srgrimes	if (len > 0) {
7211541Srgrimes		(void) m_free(m);
7221541Srgrimes		goto bad;
7231541Srgrimes	}
7241541Srgrimes	m->m_next = n;
7251541Srgrimes	return (m);
7261541Srgrimesbad:
7271541Srgrimes	m_freem(n);
7281541Srgrimes	MPFail++;
7291541Srgrimes	return (0);
7301541Srgrimes}
7311541Srgrimes
7321541Srgrimes/*
7331541Srgrimes * Partition an mbuf chain in two pieces, returning the tail --
7341541Srgrimes * all but the first len0 bytes.  In case of failure, it returns NULL and
7351541Srgrimes * attempts to restore the chain to its original state.
7361541Srgrimes */
7371541Srgrimesstruct mbuf *
7381541Srgrimesm_split(m0, len0, wait)
7391541Srgrimes	register struct mbuf *m0;
7401541Srgrimes	int len0, wait;
7411541Srgrimes{
7421541Srgrimes	register struct mbuf *m, *n;
7431541Srgrimes	unsigned len = len0, remain;
7441541Srgrimes
7451541Srgrimes	for (m = m0; m && len > m->m_len; m = m->m_next)
7461541Srgrimes		len -= m->m_len;
7471541Srgrimes	if (m == 0)
7481541Srgrimes		return (0);
7491541Srgrimes	remain = m->m_len - len;
7501541Srgrimes	if (m0->m_flags & M_PKTHDR) {
7511541Srgrimes		MGETHDR(n, wait, m0->m_type);
7521541Srgrimes		if (n == 0)
7531541Srgrimes			return (0);
7541541Srgrimes		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
7551541Srgrimes		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
7561541Srgrimes		m0->m_pkthdr.len = len0;
7571541Srgrimes		if (m->m_flags & M_EXT)
7581541Srgrimes			goto extpacket;
7591541Srgrimes		if (remain > MHLEN) {
7601541Srgrimes			/* m can't be the lead packet */
7611541Srgrimes			MH_ALIGN(n, 0);
7621541Srgrimes			n->m_next = m_split(m, len, wait);
7631541Srgrimes			if (n->m_next == 0) {
7641541Srgrimes				(void) m_free(n);
7651541Srgrimes				return (0);
7661541Srgrimes			} else
7671541Srgrimes				return (n);
7681541Srgrimes		} else
7691541Srgrimes			MH_ALIGN(n, remain);
7701541Srgrimes	} else if (remain == 0) {
7711541Srgrimes		n = m->m_next;
7721541Srgrimes		m->m_next = 0;
7731541Srgrimes		return (n);
7741541Srgrimes	} else {
7751541Srgrimes		MGET(n, wait, m->m_type);
7761541Srgrimes		if (n == 0)
7771541Srgrimes			return (0);
7781541Srgrimes		M_ALIGN(n, remain);
7791541Srgrimes	}
7801541Srgrimesextpacket:
7811541Srgrimes	if (m->m_flags & M_EXT) {
7821541Srgrimes		n->m_flags |= M_EXT;
7831541Srgrimes		n->m_ext = m->m_ext;
78417663Sjulian		if(!m->m_ext.ext_ref)
78517663Sjulian			mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
78617663Sjulian		else
78717663Sjulian			(*(m->m_ext.ext_ref))(m->m_ext.ext_buf,
78817663Sjulian						m->m_ext.ext_size);
7891541Srgrimes		m->m_ext.ext_size = 0; /* For Accounting XXXXXX danger */
7901541Srgrimes		n->m_data = m->m_data + len;
7911541Srgrimes	} else {
7921541Srgrimes		bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
7931541Srgrimes	}
7941541Srgrimes	n->m_len = remain;
7951541Srgrimes	m->m_len = len;
7961541Srgrimes	n->m_next = m->m_next;
7971541Srgrimes	m->m_next = 0;
7981541Srgrimes	return (n);
7991541Srgrimes}
8001541Srgrimes/*
8011541Srgrimes * Routine to copy from device local memory into mbufs.
8021541Srgrimes */
8031541Srgrimesstruct mbuf *
8041541Srgrimesm_devget(buf, totlen, off0, ifp, copy)
8051541Srgrimes	char *buf;
8061541Srgrimes	int totlen, off0;
8071541Srgrimes	struct ifnet *ifp;
80812577Sbde	void (*copy) __P((char *from, caddr_t to, u_int len));
8091541Srgrimes{
8101541Srgrimes	register struct mbuf *m;
8111541Srgrimes	struct mbuf *top = 0, **mp = &top;
8121541Srgrimes	register int off = off0, len;
8131541Srgrimes	register char *cp;
8141541Srgrimes	char *epkt;
8151541Srgrimes
8161541Srgrimes	cp = buf;
8171541Srgrimes	epkt = cp + totlen;
8181541Srgrimes	if (off) {
8191541Srgrimes		cp += off + 2 * sizeof(u_short);
8201541Srgrimes		totlen -= 2 * sizeof(u_short);
8211541Srgrimes	}
8221541Srgrimes	MGETHDR(m, M_DONTWAIT, MT_DATA);
8231541Srgrimes	if (m == 0)
8241541Srgrimes		return (0);
8251541Srgrimes	m->m_pkthdr.rcvif = ifp;
8261541Srgrimes	m->m_pkthdr.len = totlen;
8271541Srgrimes	m->m_len = MHLEN;
8281541Srgrimes
8291541Srgrimes	while (totlen > 0) {
8301541Srgrimes		if (top) {
8311541Srgrimes			MGET(m, M_DONTWAIT, MT_DATA);
8321541Srgrimes			if (m == 0) {
8331541Srgrimes				m_freem(top);
8341541Srgrimes				return (0);
8351541Srgrimes			}
8361541Srgrimes			m->m_len = MLEN;
8371541Srgrimes		}
8381541Srgrimes		len = min(totlen, epkt - cp);
8391541Srgrimes		if (len >= MINCLSIZE) {
8401541Srgrimes			MCLGET(m, M_DONTWAIT);
8411541Srgrimes			if (m->m_flags & M_EXT)
8421541Srgrimes				m->m_len = len = min(len, MCLBYTES);
8431541Srgrimes			else
8441541Srgrimes				len = m->m_len;
8451541Srgrimes		} else {
8461541Srgrimes			/*
8471541Srgrimes			 * Place initial small packet/header at end of mbuf.
8481541Srgrimes			 */
8491541Srgrimes			if (len < m->m_len) {
8501541Srgrimes				if (top == 0 && len + max_linkhdr <= m->m_len)
8511541Srgrimes					m->m_data += max_linkhdr;
8521541Srgrimes				m->m_len = len;
8531541Srgrimes			} else
8541541Srgrimes				len = m->m_len;
8551541Srgrimes		}
8561541Srgrimes		if (copy)
8571541Srgrimes			copy(cp, mtod(m, caddr_t), (unsigned)len);
8581541Srgrimes		else
8591541Srgrimes			bcopy(cp, mtod(m, caddr_t), (unsigned)len);
8601541Srgrimes		cp += len;
8611541Srgrimes		*mp = m;
8621541Srgrimes		mp = &m->m_next;
8631541Srgrimes		totlen -= len;
8641541Srgrimes		if (cp == epkt)
8651541Srgrimes			cp = buf;
8661541Srgrimes	}
8671541Srgrimes	return (top);
8681541Srgrimes}
8693352Sphk
8703352Sphk/*
8713352Sphk * Copy data from a buffer back into the indicated mbuf chain,
8723352Sphk * starting "off" bytes from the beginning, extending the mbuf
8733352Sphk * chain if necessary.
8743352Sphk */
8753352Sphkvoid
8763352Sphkm_copyback(m0, off, len, cp)
8773352Sphk	struct	mbuf *m0;
8783352Sphk	register int off;
8793352Sphk	register int len;
8803352Sphk	caddr_t cp;
8813352Sphk{
8823352Sphk	register int mlen;
8833352Sphk	register struct mbuf *m = m0, *n;
8843352Sphk	int totlen = 0;
8853352Sphk
8863352Sphk	if (m0 == 0)
8873352Sphk		return;
8883352Sphk	while (off > (mlen = m->m_len)) {
8893352Sphk		off -= mlen;
8903352Sphk		totlen += mlen;
8913352Sphk		if (m->m_next == 0) {
8923352Sphk			n = m_getclr(M_DONTWAIT, m->m_type);
8933352Sphk			if (n == 0)
8943352Sphk				goto out;
8953352Sphk			n->m_len = min(MLEN, len + off);
8963352Sphk			m->m_next = n;
8973352Sphk		}
8983352Sphk		m = m->m_next;
8993352Sphk	}
9003352Sphk	while (len > 0) {
9013352Sphk		mlen = min (m->m_len - off, len);
9023352Sphk		bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen);
9033352Sphk		cp += mlen;
9043352Sphk		len -= mlen;
9053352Sphk		mlen += off;
9063352Sphk		off = 0;
9073352Sphk		totlen += mlen;
9083352Sphk		if (len == 0)
9093352Sphk			break;
9103352Sphk		if (m->m_next == 0) {
9113352Sphk			n = m_get(M_DONTWAIT, m->m_type);
9123352Sphk			if (n == 0)
9133352Sphk				break;
9143352Sphk			n->m_len = min(MLEN, len);
9153352Sphk			m->m_next = n;
9163352Sphk		}
9173352Sphk		m = m->m_next;
9183352Sphk	}
9193352Sphkout:	if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
9203352Sphk		m->m_pkthdr.len = totlen;
9213352Sphk}
922