uipc_mbuf.c revision 22899
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
3422899Swollman *	$Id$
351541Srgrimes */
361541Srgrimes
371541Srgrimes#include <sys/param.h>
381541Srgrimes#include <sys/systm.h>
391541Srgrimes#include <sys/proc.h>
401541Srgrimes#include <sys/malloc.h>
411541Srgrimes#define MBTYPES
421541Srgrimes#include <sys/mbuf.h>
431541Srgrimes#include <sys/kernel.h>
441541Srgrimes#include <sys/syslog.h>
451541Srgrimes#include <sys/domain.h>
461541Srgrimes#include <sys/protosw.h>
471541Srgrimes
481541Srgrimes#include <vm/vm.h>
4912662Sdg#include <vm/vm_param.h>
509759Sbde#include <vm/vm_kern.h>
5112662Sdg#include <vm/vm_extern.h>
521541Srgrimes
5310653Sdgstatic void mbinit __P((void *));
5410358SjulianSYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbinit, NULL)
5510358Sjulian
569759Sbdestruct mbuf *mbutl;
571541Srgrimeschar	*mclrefcnt;
589759Sbdestruct mbstat mbstat;
5915689Swollmanstruct mbuf *mmbfree;
609759Sbdeunion mcluster *mclfree;
619759Sbdeint	max_linkhdr;
629759Sbdeint	max_protohdr;
639759Sbdeint	max_hdr;
649759Sbdeint	max_datalen;
651541Srgrimes
6612819Sphkstatic void	m_reclaim __P((void));
6712819Sphk
6815736Sphk/* "number of clusters of pages" */
6915736Sphk#define NCL_INIT	1
7015736Sphk
7115744Sphk#define NMB_INIT	16
7215744Sphk
7310358Sjulian/* ARGSUSED*/
7410358Sjulianstatic void
7512569Sbdembinit(dummy)
7612569Sbde	void *dummy;
771541Srgrimes{
781541Srgrimes	int s;
791541Srgrimes
8015689Swollman	mmbfree = NULL; mclfree = NULL;
811541Srgrimes	s = splimp();
8215689Swollman	if (m_mballoc(NMB_INIT, M_DONTWAIT) == 0)
8315689Swollman		goto bad;
8422671Swollman#if MCLBYTES <= PAGE_SIZE
851541Srgrimes	if (m_clalloc(NCL_INIT, M_DONTWAIT) == 0)
861541Srgrimes		goto bad;
8722671Swollman#else
8822671Swollman	/* It's OK to call contigmalloc in this context. */
8922671Swollman	if (m_clalloc(16, 0) == 0)
9022671Swollman		goto bad;
9122671Swollman#endif
921541Srgrimes	splx(s);
931541Srgrimes	return;
941541Srgrimesbad:
951541Srgrimes	panic("mbinit");
961541Srgrimes}
971541Srgrimes
981541Srgrimes/*
9915689Swollman * Allocate at least nmb mbufs and place on mbuf free list.
10015689Swollman * Must be called at splimp.
10115689Swollman */
10215689Swollman/* ARGSUSED */
10315689Swollmanint
10415689Swollmanm_mballoc(nmb, nowait)
10515689Swollman	register int nmb;
10615689Swollman	int nowait;
10715689Swollman{
10815689Swollman	register caddr_t p;
10915689Swollman	register int i;
11015689Swollman	int nbytes;
11115689Swollman
11215689Swollman	/* Once we run out of map space, it will be impossible to get
11315689Swollman	 * any more (nothing is ever freed back to the map) (XXX which
11415689Swollman	 * is dumb). (however you are not dead as m_reclaim might
11515689Swollman	 * still be able to free a substantial amount of space).
11615689Swollman	 */
11715689Swollman	if (mb_map_full)
11815689Swollman		return (0);
11915689Swollman
12015689Swollman	nbytes = round_page(nmb * MSIZE);
12122899Swollman	p = (caddr_t)kmem_malloc(mb_map, nbytes, M_NOWAIT);
12222899Swollman	if (p == 0 && !nowait) {
12322899Swollman		mbstat.m_wait++;
12422899Swollman		p = (caddr_t)kmem_malloc(mb_map, nbytes, M_WAITOK);
12522899Swollman	}
12622899Swollman
12715689Swollman	/*
12815689Swollman	 * Either the map is now full, or this is nowait and there
12915689Swollman	 * are no pages left.
13015689Swollman	 */
13115689Swollman	if (p == NULL)
13215689Swollman		return (0);
13315689Swollman
13415689Swollman	nmb = nbytes / MSIZE;
13515689Swollman	for (i = 0; i < nmb; i++) {
13615689Swollman		((struct mbuf *)p)->m_next = mmbfree;
13715689Swollman		mmbfree = (struct mbuf *)p;
13815689Swollman		p += MSIZE;
13915689Swollman	}
14015689Swollman	mbstat.m_mbufs += nmb;
14115689Swollman	return (1);
14215689Swollman}
14315689Swollman
14422671Swollman#if MCLBYTES > PAGE_SIZE
14522899Swollmanstatic int i_want_my_mcl;
14622671Swollman
14722899Swollmanstatic void
14822671Swollmankproc_mclalloc(void)
14922671Swollman{
15022671Swollman	int status;
15122671Swollman
15222671Swollman	while (1) {
15322671Swollman		tsleep(&i_want_my_mcl, PVM, "mclalloc", 0);
15422671Swollman
15522671Swollman		for (; i_want_my_mcl; i_want_my_mcl--) {
15622671Swollman			if (m_clalloc(1, 0) == 0)
15722671Swollman				printf("m_clalloc failed even in process context!\n");
15822671Swollman		}
15922671Swollman	}
16022671Swollman}
16122671Swollman
16222671Swollmanstatic struct proc *mclallocproc;
16322671Swollmanstatic struct kproc_desc mclalloc_kp = {
16422671Swollman	"mclalloc",
16522671Swollman	kproc_mclalloc,
16622671Swollman	&mclallocproc
16722671Swollman};
16822671SwollmanSYSINIT_KT(mclallocproc, SI_SUB_KTHREAD_UPDATE, SI_ORDER_ANY, kproc_start,
16922671Swollman	   &mclalloc_kp);
17022671Swollman#endif
17122671Swollman
17215689Swollman/*
1731541Srgrimes * Allocate some number of mbuf clusters
1741541Srgrimes * and place on cluster free list.
1751541Srgrimes * Must be called at splimp.
1761541Srgrimes */
1771541Srgrimes/* ARGSUSED */
1781549Srgrimesint
1791541Srgrimesm_clalloc(ncl, nowait)
1801541Srgrimes	register int ncl;
1811541Srgrimes	int nowait;
1821541Srgrimes{
1831541Srgrimes	register caddr_t p;
1841541Srgrimes	register int i;
1851541Srgrimes	int npg;
1861541Srgrimes
1877066Sdg	/*
1887066Sdg	 * Once we run out of map space, it will be impossible
1897066Sdg	 * to get any more (nothing is ever freed back to the
1907066Sdg	 * map).
1917066Sdg	 */
19222899Swollman	if (mb_map_full) {
19322899Swollman		mbstat.m_drops++;
1947066Sdg		return (0);
19522899Swollman	}
1967066Sdg
19722671Swollman#if MCLBYTES > PAGE_SIZE
19822671Swollman	if (nowait) {
19922671Swollman		i_want_my_mcl += ncl;
20022671Swollman		wakeup(&i_want_my_mcl);
20122899Swollman		mbstat.m_wait++;
20222671Swollman		p = 0;
20322671Swollman	} else {
20422671Swollman		p = contigmalloc1(MCLBYTES * ncl, M_DEVBUF, M_WAITOK, 0ul,
20522671Swollman				  ~0ul, PAGE_SIZE, 0, mb_map);
20622671Swollman	}
20722671Swollman#else
20815543Sphk	npg = ncl;
20921737Sdg	p = (caddr_t)kmem_malloc(mb_map, ctob(npg),
2106191Sbde				 nowait ? M_NOWAIT : M_WAITOK);
21122671Swollman	ncl = ncl * PAGE_SIZE / MCLBYTES;
21222671Swollman#endif
2137066Sdg	/*
2147066Sdg	 * Either the map is now full, or this is nowait and there
2157066Sdg	 * are no pages left.
2167066Sdg	 */
21722899Swollman	if (p == NULL) {
21822899Swollman		mbstat.m_drops++;
2191541Srgrimes		return (0);
22022899Swollman	}
2217066Sdg
2221541Srgrimes	for (i = 0; i < ncl; i++) {
2231541Srgrimes		((union mcluster *)p)->mcl_next = mclfree;
2241541Srgrimes		mclfree = (union mcluster *)p;
2251541Srgrimes		p += MCLBYTES;
2261541Srgrimes		mbstat.m_clfree++;
2271541Srgrimes	}
2281541Srgrimes	mbstat.m_clusters += ncl;
2291541Srgrimes	return (1);
2301541Srgrimes}
2311541Srgrimes
2321541Srgrimes/*
2331541Srgrimes * When MGET failes, ask protocols to free space when short of memory,
2341541Srgrimes * then re-attempt to allocate an mbuf.
2351541Srgrimes */
2361541Srgrimesstruct mbuf *
2371541Srgrimesm_retry(i, t)
2381541Srgrimes	int i, t;
2391541Srgrimes{
2401541Srgrimes	register struct mbuf *m;
2411541Srgrimes
2421541Srgrimes	m_reclaim();
2431541Srgrimes#define m_retry(i, t)	(struct mbuf *)0
2441541Srgrimes	MGET(m, i, t);
2451541Srgrimes#undef m_retry
2466669Sdg	if (m != NULL)
2476669Sdg		mbstat.m_wait++;
2486669Sdg	else
2496669Sdg		mbstat.m_drops++;
2501541Srgrimes	return (m);
2511541Srgrimes}
2521541Srgrimes
2531541Srgrimes/*
2541541Srgrimes * As above; retry an MGETHDR.
2551541Srgrimes */
2561541Srgrimesstruct mbuf *
2571541Srgrimesm_retryhdr(i, t)
2581541Srgrimes	int i, t;
2591541Srgrimes{
2601541Srgrimes	register struct mbuf *m;
2611541Srgrimes
2621541Srgrimes	m_reclaim();
2631541Srgrimes#define m_retryhdr(i, t) (struct mbuf *)0
2641541Srgrimes	MGETHDR(m, i, t);
2651541Srgrimes#undef m_retryhdr
2666669Sdg	if (m != NULL)
2676669Sdg		mbstat.m_wait++;
2686669Sdg	else
2696669Sdg		mbstat.m_drops++;
2701541Srgrimes	return (m);
2711541Srgrimes}
2721541Srgrimes
27312819Sphkstatic void
2741541Srgrimesm_reclaim()
2751541Srgrimes{
2761541Srgrimes	register struct domain *dp;
2771541Srgrimes	register struct protosw *pr;
2781541Srgrimes	int s = splimp();
2791541Srgrimes
2801541Srgrimes	for (dp = domains; dp; dp = dp->dom_next)
2811541Srgrimes		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
2821541Srgrimes			if (pr->pr_drain)
2831541Srgrimes				(*pr->pr_drain)();
2841541Srgrimes	splx(s);
2851541Srgrimes	mbstat.m_drain++;
2861541Srgrimes}
2871541Srgrimes
2881541Srgrimes/*
2891541Srgrimes * Space allocation routines.
2901541Srgrimes * These are also available as macros
2911541Srgrimes * for critical paths.
2921541Srgrimes */
2931541Srgrimesstruct mbuf *
2941541Srgrimesm_get(nowait, type)
2951541Srgrimes	int nowait, type;
2961541Srgrimes{
2971541Srgrimes	register struct mbuf *m;
2981541Srgrimes
2991541Srgrimes	MGET(m, nowait, type);
3001541Srgrimes	return (m);
3011541Srgrimes}
3021541Srgrimes
3031541Srgrimesstruct mbuf *
3041541Srgrimesm_gethdr(nowait, type)
3051541Srgrimes	int nowait, type;
3061541Srgrimes{
3071541Srgrimes	register struct mbuf *m;
3081541Srgrimes
3091541Srgrimes	MGETHDR(m, nowait, type);
3101541Srgrimes	return (m);
3111541Srgrimes}
3121541Srgrimes
3131541Srgrimesstruct mbuf *
3141541Srgrimesm_getclr(nowait, type)
3151541Srgrimes	int nowait, type;
3161541Srgrimes{
3171541Srgrimes	register struct mbuf *m;
3181541Srgrimes
3191541Srgrimes	MGET(m, nowait, type);
3201541Srgrimes	if (m == 0)
3211541Srgrimes		return (0);
3221541Srgrimes	bzero(mtod(m, caddr_t), MLEN);
3231541Srgrimes	return (m);
3241541Srgrimes}
3251541Srgrimes
3261541Srgrimesstruct mbuf *
3271541Srgrimesm_free(m)
3281541Srgrimes	struct mbuf *m;
3291541Srgrimes{
3301541Srgrimes	register struct mbuf *n;
3311541Srgrimes
3321541Srgrimes	MFREE(m, n);
3331541Srgrimes	return (n);
3341541Srgrimes}
3351541Srgrimes
3361541Srgrimesvoid
3371541Srgrimesm_freem(m)
3381541Srgrimes	register struct mbuf *m;
3391541Srgrimes{
3401541Srgrimes	register struct mbuf *n;
3411541Srgrimes
3421541Srgrimes	if (m == NULL)
3431541Srgrimes		return;
3441541Srgrimes	do {
3451541Srgrimes		MFREE(m, n);
3463308Sphk		m = n;
3473308Sphk	} while (m);
3481541Srgrimes}
3491541Srgrimes
3501541Srgrimes/*
3511541Srgrimes * Mbuffer utility routines.
3521541Srgrimes */
3531541Srgrimes
3541541Srgrimes/*
3551541Srgrimes * Lesser-used path for M_PREPEND:
3561541Srgrimes * allocate new mbuf to prepend to chain,
3571541Srgrimes * copy junk along.
3581541Srgrimes */
3591541Srgrimesstruct mbuf *
3601541Srgrimesm_prepend(m, len, how)
3611541Srgrimes	register struct mbuf *m;
3621541Srgrimes	int len, how;
3631541Srgrimes{
3641541Srgrimes	struct mbuf *mn;
3651541Srgrimes
3661541Srgrimes	MGET(mn, how, m->m_type);
3671541Srgrimes	if (mn == (struct mbuf *)NULL) {
3681541Srgrimes		m_freem(m);
3691541Srgrimes		return ((struct mbuf *)NULL);
3701541Srgrimes	}
3711541Srgrimes	if (m->m_flags & M_PKTHDR) {
3721541Srgrimes		M_COPY_PKTHDR(mn, m);
3731541Srgrimes		m->m_flags &= ~M_PKTHDR;
3741541Srgrimes	}
3751541Srgrimes	mn->m_next = m;
3761541Srgrimes	m = mn;
3771541Srgrimes	if (len < MHLEN)
3781541Srgrimes		MH_ALIGN(m, len);
3791541Srgrimes	m->m_len = len;
3801541Srgrimes	return (m);
3811541Srgrimes}
3821541Srgrimes
3831541Srgrimes/*
3841541Srgrimes * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
3851541Srgrimes * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
3861541Srgrimes * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
3871541Srgrimes */
38812819Sphkstatic int MCFail;
3891541Srgrimes
3901541Srgrimesstruct mbuf *
3911541Srgrimesm_copym(m, off0, len, wait)
3921541Srgrimes	register struct mbuf *m;
3931541Srgrimes	int off0, wait;
3941541Srgrimes	register int len;
3951541Srgrimes{
3961541Srgrimes	register struct mbuf *n, **np;
3971541Srgrimes	register int off = off0;
3981541Srgrimes	struct mbuf *top;
3991541Srgrimes	int copyhdr = 0;
4001541Srgrimes
4011541Srgrimes	if (off < 0 || len < 0)
4021541Srgrimes		panic("m_copym");
4031541Srgrimes	if (off == 0 && m->m_flags & M_PKTHDR)
4041541Srgrimes		copyhdr = 1;
4051541Srgrimes	while (off > 0) {
4061541Srgrimes		if (m == 0)
4071541Srgrimes			panic("m_copym");
4081541Srgrimes		if (off < m->m_len)
4091541Srgrimes			break;
4101541Srgrimes		off -= m->m_len;
4111541Srgrimes		m = m->m_next;
4121541Srgrimes	}
4131541Srgrimes	np = &top;
4141541Srgrimes	top = 0;
4151541Srgrimes	while (len > 0) {
4161541Srgrimes		if (m == 0) {
4171541Srgrimes			if (len != M_COPYALL)
4181541Srgrimes				panic("m_copym");
4191541Srgrimes			break;
4201541Srgrimes		}
4211541Srgrimes		MGET(n, wait, m->m_type);
4221541Srgrimes		*np = n;
4231541Srgrimes		if (n == 0)
4241541Srgrimes			goto nospace;
4251541Srgrimes		if (copyhdr) {
4261541Srgrimes			M_COPY_PKTHDR(n, m);
4271541Srgrimes			if (len == M_COPYALL)
4281541Srgrimes				n->m_pkthdr.len -= off0;
4291541Srgrimes			else
4301541Srgrimes				n->m_pkthdr.len = len;
4311541Srgrimes			copyhdr = 0;
4321541Srgrimes		}
4331541Srgrimes		n->m_len = min(len, m->m_len - off);
4341541Srgrimes		if (m->m_flags & M_EXT) {
4351541Srgrimes			n->m_data = m->m_data + off;
43617663Sjulian			if(!m->m_ext.ext_ref)
43717663Sjulian				mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
43817663Sjulian			else
43917663Sjulian				(*(m->m_ext.ext_ref))(m->m_ext.ext_buf,
44017663Sjulian							m->m_ext.ext_size);
4411541Srgrimes			n->m_ext = m->m_ext;
4421541Srgrimes			n->m_flags |= M_EXT;
4431541Srgrimes		} else
4441541Srgrimes			bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
4451541Srgrimes			    (unsigned)n->m_len);
4461541Srgrimes		if (len != M_COPYALL)
4471541Srgrimes			len -= n->m_len;
4481541Srgrimes		off = 0;
4491541Srgrimes		m = m->m_next;
4501541Srgrimes		np = &n->m_next;
4511541Srgrimes	}
4521541Srgrimes	if (top == 0)
4531541Srgrimes		MCFail++;
4541541Srgrimes	return (top);
4551541Srgrimesnospace:
4561541Srgrimes	m_freem(top);
4571541Srgrimes	MCFail++;
4581541Srgrimes	return (0);
4591541Srgrimes}
4601541Srgrimes
4611541Srgrimes/*
46215689Swollman * Copy an entire packet, including header (which must be present).
46315689Swollman * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
46415689Swollman */
46515689Swollmanstruct mbuf *
46615689Swollmanm_copypacket(m, how)
46715689Swollman	struct mbuf *m;
46815689Swollman	int how;
46915689Swollman{
47015689Swollman	struct mbuf *top, *n, *o;
47115689Swollman
47215689Swollman	MGET(n, how, m->m_type);
47315689Swollman	top = n;
47415689Swollman	if (!n)
47515689Swollman		goto nospace;
47615689Swollman
47715689Swollman	M_COPY_PKTHDR(n, m);
47815689Swollman	n->m_len = m->m_len;
47915689Swollman	if (m->m_flags & M_EXT) {
48015689Swollman		n->m_data = m->m_data;
48115689Swollman		mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
48215689Swollman		n->m_ext = m->m_ext;
48315689Swollman		n->m_flags |= M_EXT;
48415689Swollman	} else {
48515689Swollman		bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
48615689Swollman	}
48715689Swollman
48815689Swollman	m = m->m_next;
48915689Swollman	while (m) {
49015689Swollman		MGET(o, how, m->m_type);
49115689Swollman		if (!o)
49215689Swollman			goto nospace;
49315689Swollman
49415689Swollman		n->m_next = o;
49515689Swollman		n = n->m_next;
49615689Swollman
49715689Swollman		n->m_len = m->m_len;
49815689Swollman		if (m->m_flags & M_EXT) {
49915689Swollman			n->m_data = m->m_data;
50015689Swollman			mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
50115689Swollman			n->m_ext = m->m_ext;
50215689Swollman			n->m_flags |= M_EXT;
50315689Swollman		} else {
50415689Swollman			bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
50515689Swollman		}
50615689Swollman
50715689Swollman		m = m->m_next;
50815689Swollman	}
50915689Swollman	return top;
51015689Swollmannospace:
51115689Swollman	m_freem(top);
51215689Swollman	MCFail++;
51315689Swollman	return 0;
51415689Swollman}
51515689Swollman
51615689Swollman/*
5171541Srgrimes * Copy data from an mbuf chain starting "off" bytes from the beginning,
5181541Srgrimes * continuing for "len" bytes, into the indicated buffer.
5191541Srgrimes */
5201549Srgrimesvoid
5211541Srgrimesm_copydata(m, off, len, cp)
5221541Srgrimes	register struct mbuf *m;
5231541Srgrimes	register int off;
5241541Srgrimes	register int len;
5251541Srgrimes	caddr_t cp;
5261541Srgrimes{
5271541Srgrimes	register unsigned count;
5281541Srgrimes
5291541Srgrimes	if (off < 0 || len < 0)
5301541Srgrimes		panic("m_copydata");
5311541Srgrimes	while (off > 0) {
5321541Srgrimes		if (m == 0)
5331541Srgrimes			panic("m_copydata");
5341541Srgrimes		if (off < m->m_len)
5351541Srgrimes			break;
5361541Srgrimes		off -= m->m_len;
5371541Srgrimes		m = m->m_next;
5381541Srgrimes	}
5391541Srgrimes	while (len > 0) {
5401541Srgrimes		if (m == 0)
5411541Srgrimes			panic("m_copydata");
5421541Srgrimes		count = min(m->m_len - off, len);
5431541Srgrimes		bcopy(mtod(m, caddr_t) + off, cp, count);
5441541Srgrimes		len -= count;
5451541Srgrimes		cp += count;
5461541Srgrimes		off = 0;
5471541Srgrimes		m = m->m_next;
5481541Srgrimes	}
5491541Srgrimes}
5501541Srgrimes
5511541Srgrimes/*
5521541Srgrimes * Concatenate mbuf chain n to m.
5531541Srgrimes * Both chains must be of the same type (e.g. MT_DATA).
5541541Srgrimes * Any m_pkthdr is not updated.
5551541Srgrimes */
5561549Srgrimesvoid
5571541Srgrimesm_cat(m, n)
5581541Srgrimes	register struct mbuf *m, *n;
5591541Srgrimes{
5601541Srgrimes	while (m->m_next)
5611541Srgrimes		m = m->m_next;
5621541Srgrimes	while (n) {
5631541Srgrimes		if (m->m_flags & M_EXT ||
5641541Srgrimes		    m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
5651541Srgrimes			/* just join the two chains */
5661541Srgrimes			m->m_next = n;
5671541Srgrimes			return;
5681541Srgrimes		}
5691541Srgrimes		/* splat the data from one into the other */
5701541Srgrimes		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
5711541Srgrimes		    (u_int)n->m_len);
5721541Srgrimes		m->m_len += n->m_len;
5731541Srgrimes		n = m_free(n);
5741541Srgrimes	}
5751541Srgrimes}
5761541Srgrimes
5771549Srgrimesvoid
5781541Srgrimesm_adj(mp, req_len)
5791541Srgrimes	struct mbuf *mp;
5801541Srgrimes	int req_len;
5811541Srgrimes{
5821541Srgrimes	register int len = req_len;
5831541Srgrimes	register struct mbuf *m;
5841541Srgrimes	register count;
5851541Srgrimes
5861541Srgrimes	if ((m = mp) == NULL)
5871541Srgrimes		return;
5881541Srgrimes	if (len >= 0) {
5891541Srgrimes		/*
5901541Srgrimes		 * Trim from head.
5911541Srgrimes		 */
5921541Srgrimes		while (m != NULL && len > 0) {
5931541Srgrimes			if (m->m_len <= len) {
5941541Srgrimes				len -= m->m_len;
5951541Srgrimes				m->m_len = 0;
5961541Srgrimes				m = m->m_next;
5971541Srgrimes			} else {
5981541Srgrimes				m->m_len -= len;
5991541Srgrimes				m->m_data += len;
6001541Srgrimes				len = 0;
6011541Srgrimes			}
6021541Srgrimes		}
6031541Srgrimes		m = mp;
6041541Srgrimes		if (mp->m_flags & M_PKTHDR)
6051541Srgrimes			m->m_pkthdr.len -= (req_len - len);
6061541Srgrimes	} else {
6071541Srgrimes		/*
6081541Srgrimes		 * Trim from tail.  Scan the mbuf chain,
6091541Srgrimes		 * calculating its length and finding the last mbuf.
6101541Srgrimes		 * If the adjustment only affects this mbuf, then just
6111541Srgrimes		 * adjust and return.  Otherwise, rescan and truncate
6121541Srgrimes		 * after the remaining size.
6131541Srgrimes		 */
6141541Srgrimes		len = -len;
6151541Srgrimes		count = 0;
6161541Srgrimes		for (;;) {
6171541Srgrimes			count += m->m_len;
6181541Srgrimes			if (m->m_next == (struct mbuf *)0)
6191541Srgrimes				break;
6201541Srgrimes			m = m->m_next;
6211541Srgrimes		}
6221541Srgrimes		if (m->m_len >= len) {
6231541Srgrimes			m->m_len -= len;
6241541Srgrimes			if (mp->m_flags & M_PKTHDR)
6251541Srgrimes				mp->m_pkthdr.len -= len;
6261541Srgrimes			return;
6271541Srgrimes		}
6281541Srgrimes		count -= len;
6291541Srgrimes		if (count < 0)
6301541Srgrimes			count = 0;
6311541Srgrimes		/*
6321541Srgrimes		 * Correct length for chain is "count".
6331541Srgrimes		 * Find the mbuf with last data, adjust its length,
6341541Srgrimes		 * and toss data from remaining mbufs on chain.
6351541Srgrimes		 */
6361541Srgrimes		m = mp;
6371541Srgrimes		if (m->m_flags & M_PKTHDR)
6381541Srgrimes			m->m_pkthdr.len = count;
6391541Srgrimes		for (; m; m = m->m_next) {
6401541Srgrimes			if (m->m_len >= count) {
6411541Srgrimes				m->m_len = count;
6421541Srgrimes				break;
6431541Srgrimes			}
6441541Srgrimes			count -= m->m_len;
6451541Srgrimes		}
6463308Sphk		while (m->m_next)
6473308Sphk			(m = m->m_next) ->m_len = 0;
6481541Srgrimes	}
6491541Srgrimes}
6501541Srgrimes
6511541Srgrimes/*
6521541Srgrimes * Rearange an mbuf chain so that len bytes are contiguous
6531541Srgrimes * and in the data area of an mbuf (so that mtod and dtom
6541541Srgrimes * will work for a structure of size len).  Returns the resulting
6551541Srgrimes * mbuf chain on success, frees it and returns null on failure.
6561541Srgrimes * If there is room, it will add up to max_protohdr-len extra bytes to the
6571541Srgrimes * contiguous region in an attempt to avoid being called next time.
6581541Srgrimes */
65912819Sphkstatic int MPFail;
6601541Srgrimes
6611541Srgrimesstruct mbuf *
6621541Srgrimesm_pullup(n, len)
6631541Srgrimes	register struct mbuf *n;
6641541Srgrimes	int len;
6651541Srgrimes{
6661541Srgrimes	register struct mbuf *m;
6671541Srgrimes	register int count;
6681541Srgrimes	int space;
6691541Srgrimes
6701541Srgrimes	/*
6711541Srgrimes	 * If first mbuf has no cluster, and has room for len bytes
6721541Srgrimes	 * without shifting current data, pullup into it,
6731541Srgrimes	 * otherwise allocate a new mbuf to prepend to the chain.
6741541Srgrimes	 */
6751541Srgrimes	if ((n->m_flags & M_EXT) == 0 &&
6761541Srgrimes	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
6771541Srgrimes		if (n->m_len >= len)
6781541Srgrimes			return (n);
6791541Srgrimes		m = n;
6801541Srgrimes		n = n->m_next;
6811541Srgrimes		len -= m->m_len;
6821541Srgrimes	} else {
6831541Srgrimes		if (len > MHLEN)
6841541Srgrimes			goto bad;
6851541Srgrimes		MGET(m, M_DONTWAIT, n->m_type);
6861541Srgrimes		if (m == 0)
6871541Srgrimes			goto bad;
6881541Srgrimes		m->m_len = 0;
6891541Srgrimes		if (n->m_flags & M_PKTHDR) {
6901541Srgrimes			M_COPY_PKTHDR(m, n);
6911541Srgrimes			n->m_flags &= ~M_PKTHDR;
6921541Srgrimes		}
6931541Srgrimes	}
6941541Srgrimes	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
6951541Srgrimes	do {
6961541Srgrimes		count = min(min(max(len, max_protohdr), space), n->m_len);
6971541Srgrimes		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
6981541Srgrimes		  (unsigned)count);
6991541Srgrimes		len -= count;
7001541Srgrimes		m->m_len += count;
7011541Srgrimes		n->m_len -= count;
7021541Srgrimes		space -= count;
7031541Srgrimes		if (n->m_len)
7041541Srgrimes			n->m_data += count;
7051541Srgrimes		else
7061541Srgrimes			n = m_free(n);
7071541Srgrimes	} while (len > 0 && n);
7081541Srgrimes	if (len > 0) {
7091541Srgrimes		(void) m_free(m);
7101541Srgrimes		goto bad;
7111541Srgrimes	}
7121541Srgrimes	m->m_next = n;
7131541Srgrimes	return (m);
7141541Srgrimesbad:
7151541Srgrimes	m_freem(n);
7161541Srgrimes	MPFail++;
7171541Srgrimes	return (0);
7181541Srgrimes}
7191541Srgrimes
7201541Srgrimes/*
7211541Srgrimes * Partition an mbuf chain in two pieces, returning the tail --
7221541Srgrimes * all but the first len0 bytes.  In case of failure, it returns NULL and
7231541Srgrimes * attempts to restore the chain to its original state.
7241541Srgrimes */
7251541Srgrimesstruct mbuf *
7261541Srgrimesm_split(m0, len0, wait)
7271541Srgrimes	register struct mbuf *m0;
7281541Srgrimes	int len0, wait;
7291541Srgrimes{
7301541Srgrimes	register struct mbuf *m, *n;
7311541Srgrimes	unsigned len = len0, remain;
7321541Srgrimes
7331541Srgrimes	for (m = m0; m && len > m->m_len; m = m->m_next)
7341541Srgrimes		len -= m->m_len;
7351541Srgrimes	if (m == 0)
7361541Srgrimes		return (0);
7371541Srgrimes	remain = m->m_len - len;
7381541Srgrimes	if (m0->m_flags & M_PKTHDR) {
7391541Srgrimes		MGETHDR(n, wait, m0->m_type);
7401541Srgrimes		if (n == 0)
7411541Srgrimes			return (0);
7421541Srgrimes		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
7431541Srgrimes		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
7441541Srgrimes		m0->m_pkthdr.len = len0;
7451541Srgrimes		if (m->m_flags & M_EXT)
7461541Srgrimes			goto extpacket;
7471541Srgrimes		if (remain > MHLEN) {
7481541Srgrimes			/* m can't be the lead packet */
7491541Srgrimes			MH_ALIGN(n, 0);
7501541Srgrimes			n->m_next = m_split(m, len, wait);
7511541Srgrimes			if (n->m_next == 0) {
7521541Srgrimes				(void) m_free(n);
7531541Srgrimes				return (0);
7541541Srgrimes			} else
7551541Srgrimes				return (n);
7561541Srgrimes		} else
7571541Srgrimes			MH_ALIGN(n, remain);
7581541Srgrimes	} else if (remain == 0) {
7591541Srgrimes		n = m->m_next;
7601541Srgrimes		m->m_next = 0;
7611541Srgrimes		return (n);
7621541Srgrimes	} else {
7631541Srgrimes		MGET(n, wait, m->m_type);
7641541Srgrimes		if (n == 0)
7651541Srgrimes			return (0);
7661541Srgrimes		M_ALIGN(n, remain);
7671541Srgrimes	}
7681541Srgrimesextpacket:
7691541Srgrimes	if (m->m_flags & M_EXT) {
7701541Srgrimes		n->m_flags |= M_EXT;
7711541Srgrimes		n->m_ext = m->m_ext;
77217663Sjulian		if(!m->m_ext.ext_ref)
77317663Sjulian			mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
77417663Sjulian		else
77517663Sjulian			(*(m->m_ext.ext_ref))(m->m_ext.ext_buf,
77617663Sjulian						m->m_ext.ext_size);
7771541Srgrimes		m->m_ext.ext_size = 0; /* For Accounting XXXXXX danger */
7781541Srgrimes		n->m_data = m->m_data + len;
7791541Srgrimes	} else {
7801541Srgrimes		bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
7811541Srgrimes	}
7821541Srgrimes	n->m_len = remain;
7831541Srgrimes	m->m_len = len;
7841541Srgrimes	n->m_next = m->m_next;
7851541Srgrimes	m->m_next = 0;
7861541Srgrimes	return (n);
7871541Srgrimes}
7881541Srgrimes/*
7891541Srgrimes * Routine to copy from device local memory into mbufs.
7901541Srgrimes */
7911541Srgrimesstruct mbuf *
7921541Srgrimesm_devget(buf, totlen, off0, ifp, copy)
7931541Srgrimes	char *buf;
7941541Srgrimes	int totlen, off0;
7951541Srgrimes	struct ifnet *ifp;
79612577Sbde	void (*copy) __P((char *from, caddr_t to, u_int len));
7971541Srgrimes{
7981541Srgrimes	register struct mbuf *m;
7991541Srgrimes	struct mbuf *top = 0, **mp = &top;
8001541Srgrimes	register int off = off0, len;
8011541Srgrimes	register char *cp;
8021541Srgrimes	char *epkt;
8031541Srgrimes
8041541Srgrimes	cp = buf;
8051541Srgrimes	epkt = cp + totlen;
8061541Srgrimes	if (off) {
8071541Srgrimes		cp += off + 2 * sizeof(u_short);
8081541Srgrimes		totlen -= 2 * sizeof(u_short);
8091541Srgrimes	}
8101541Srgrimes	MGETHDR(m, M_DONTWAIT, MT_DATA);
8111541Srgrimes	if (m == 0)
8121541Srgrimes		return (0);
8131541Srgrimes	m->m_pkthdr.rcvif = ifp;
8141541Srgrimes	m->m_pkthdr.len = totlen;
8151541Srgrimes	m->m_len = MHLEN;
8161541Srgrimes
8171541Srgrimes	while (totlen > 0) {
8181541Srgrimes		if (top) {
8191541Srgrimes			MGET(m, M_DONTWAIT, MT_DATA);
8201541Srgrimes			if (m == 0) {
8211541Srgrimes				m_freem(top);
8221541Srgrimes				return (0);
8231541Srgrimes			}
8241541Srgrimes			m->m_len = MLEN;
8251541Srgrimes		}
8261541Srgrimes		len = min(totlen, epkt - cp);
8271541Srgrimes		if (len >= MINCLSIZE) {
8281541Srgrimes			MCLGET(m, M_DONTWAIT);
8291541Srgrimes			if (m->m_flags & M_EXT)
8301541Srgrimes				m->m_len = len = min(len, MCLBYTES);
8311541Srgrimes			else
8321541Srgrimes				len = m->m_len;
8331541Srgrimes		} else {
8341541Srgrimes			/*
8351541Srgrimes			 * Place initial small packet/header at end of mbuf.
8361541Srgrimes			 */
8371541Srgrimes			if (len < m->m_len) {
8381541Srgrimes				if (top == 0 && len + max_linkhdr <= m->m_len)
8391541Srgrimes					m->m_data += max_linkhdr;
8401541Srgrimes				m->m_len = len;
8411541Srgrimes			} else
8421541Srgrimes				len = m->m_len;
8431541Srgrimes		}
8441541Srgrimes		if (copy)
8451541Srgrimes			copy(cp, mtod(m, caddr_t), (unsigned)len);
8461541Srgrimes		else
8471541Srgrimes			bcopy(cp, mtod(m, caddr_t), (unsigned)len);
8481541Srgrimes		cp += len;
8491541Srgrimes		*mp = m;
8501541Srgrimes		mp = &m->m_next;
8511541Srgrimes		totlen -= len;
8521541Srgrimes		if (cp == epkt)
8531541Srgrimes			cp = buf;
8541541Srgrimes	}
8551541Srgrimes	return (top);
8561541Srgrimes}
8573352Sphk
8583352Sphk/*
8593352Sphk * Copy data from a buffer back into the indicated mbuf chain,
8603352Sphk * starting "off" bytes from the beginning, extending the mbuf
8613352Sphk * chain if necessary.
8623352Sphk */
8633352Sphkvoid
8643352Sphkm_copyback(m0, off, len, cp)
8653352Sphk	struct	mbuf *m0;
8663352Sphk	register int off;
8673352Sphk	register int len;
8683352Sphk	caddr_t cp;
8693352Sphk{
8703352Sphk	register int mlen;
8713352Sphk	register struct mbuf *m = m0, *n;
8723352Sphk	int totlen = 0;
8733352Sphk
8743352Sphk	if (m0 == 0)
8753352Sphk		return;
8763352Sphk	while (off > (mlen = m->m_len)) {
8773352Sphk		off -= mlen;
8783352Sphk		totlen += mlen;
8793352Sphk		if (m->m_next == 0) {
8803352Sphk			n = m_getclr(M_DONTWAIT, m->m_type);
8813352Sphk			if (n == 0)
8823352Sphk				goto out;
8833352Sphk			n->m_len = min(MLEN, len + off);
8843352Sphk			m->m_next = n;
8853352Sphk		}
8863352Sphk		m = m->m_next;
8873352Sphk	}
8883352Sphk	while (len > 0) {
8893352Sphk		mlen = min (m->m_len - off, len);
8903352Sphk		bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen);
8913352Sphk		cp += mlen;
8923352Sphk		len -= mlen;
8933352Sphk		mlen += off;
8943352Sphk		off = 0;
8953352Sphk		totlen += mlen;
8963352Sphk		if (len == 0)
8973352Sphk			break;
8983352Sphk		if (m->m_next == 0) {
8993352Sphk			n = m_get(M_DONTWAIT, m->m_type);
9003352Sphk			if (n == 0)
9013352Sphk				break;
9023352Sphk			n->m_len = min(MLEN, len);
9033352Sphk			m->m_next = n;
9043352Sphk		}
9053352Sphk		m = m->m_next;
9063352Sphk	}
9073352Sphkout:	if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
9083352Sphk		m->m_pkthdr.len = totlen;
9093352Sphk}
910