uipc_mbuf.c revision 22671
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
3421673Sjkh * $FreeBSD: head/sys/kern/uipc_mbuf.c 22671 1997-02-13 19:41:40Z wollman $
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);
12115689Swollman	p = (caddr_t)kmem_malloc(mb_map, nbytes, nowait ? M_NOWAIT : M_WAITOK);
12215689Swollman	/*
12315689Swollman	 * Either the map is now full, or this is nowait and there
12415689Swollman	 * are no pages left.
12515689Swollman	 */
12615689Swollman	if (p == NULL)
12715689Swollman		return (0);
12815689Swollman
12915689Swollman	nmb = nbytes / MSIZE;
13015689Swollman	for (i = 0; i < nmb; i++) {
13115689Swollman		((struct mbuf *)p)->m_next = mmbfree;
13215689Swollman		mmbfree = (struct mbuf *)p;
13315689Swollman		p += MSIZE;
13415689Swollman	}
13515689Swollman	mbstat.m_mbufs += nmb;
13615689Swollman	return (1);
13715689Swollman}
13815689Swollman
13922671Swollman#if MCLBYTES > PAGE_SIZE
14022671Swollmanint i_want_my_mcl;
14122671Swollman
14222671Swollmanvoid
14322671Swollmankproc_mclalloc(void)
14422671Swollman{
14522671Swollman	int status;
14622671Swollman
14722671Swollman	while (1) {
14822671Swollman		tsleep(&i_want_my_mcl, PVM, "mclalloc", 0);
14922671Swollman
15022671Swollman		for (; i_want_my_mcl; i_want_my_mcl--) {
15122671Swollman			if (m_clalloc(1, 0) == 0)
15222671Swollman				printf("m_clalloc failed even in process context!\n");
15322671Swollman		}
15422671Swollman	}
15522671Swollman}
15622671Swollman
15722671Swollmanstatic struct proc *mclallocproc;
15822671Swollmanstatic struct kproc_desc mclalloc_kp = {
15922671Swollman	"mclalloc",
16022671Swollman	kproc_mclalloc,
16122671Swollman	&mclallocproc
16222671Swollman};
16322671SwollmanSYSINIT_KT(mclallocproc, SI_SUB_KTHREAD_UPDATE, SI_ORDER_ANY, kproc_start,
16422671Swollman	   &mclalloc_kp);
16522671Swollman#endif
16622671Swollman
16715689Swollman/*
1681541Srgrimes * Allocate some number of mbuf clusters
1691541Srgrimes * and place on cluster free list.
1701541Srgrimes * Must be called at splimp.
1711541Srgrimes */
1721541Srgrimes/* ARGSUSED */
1731549Srgrimesint
1741541Srgrimesm_clalloc(ncl, nowait)
1751541Srgrimes	register int ncl;
1761541Srgrimes	int nowait;
1771541Srgrimes{
1781541Srgrimes	register caddr_t p;
1791541Srgrimes	register int i;
1801541Srgrimes	int npg;
1811541Srgrimes
1827066Sdg	/*
1837066Sdg	 * Once we run out of map space, it will be impossible
1847066Sdg	 * to get any more (nothing is ever freed back to the
1857066Sdg	 * map).
1867066Sdg	 */
18721737Sdg	if (mb_map_full)
1887066Sdg		return (0);
1897066Sdg
19022671Swollman#if MCLBYTES > PAGE_SIZE
19122671Swollman	if (nowait) {
19222671Swollman		i_want_my_mcl += ncl;
19322671Swollman		wakeup(&i_want_my_mcl);
19422671Swollman		p = 0;
19522671Swollman	} else {
19622671Swollman		p = contigmalloc1(MCLBYTES * ncl, M_DEVBUF, M_WAITOK, 0ul,
19722671Swollman				  ~0ul, PAGE_SIZE, 0, mb_map);
19822671Swollman	}
19922671Swollman#else
20015543Sphk	npg = ncl;
20121737Sdg	p = (caddr_t)kmem_malloc(mb_map, ctob(npg),
2026191Sbde				 nowait ? M_NOWAIT : M_WAITOK);
20322671Swollman	ncl = ncl * PAGE_SIZE / MCLBYTES;
20422671Swollman#endif
2057066Sdg	/*
2067066Sdg	 * Either the map is now full, or this is nowait and there
2077066Sdg	 * are no pages left.
2087066Sdg	 */
2097066Sdg	if (p == NULL)
2101541Srgrimes		return (0);
2117066Sdg
2121541Srgrimes	for (i = 0; i < ncl; i++) {
2131541Srgrimes		((union mcluster *)p)->mcl_next = mclfree;
2141541Srgrimes		mclfree = (union mcluster *)p;
2151541Srgrimes		p += MCLBYTES;
2161541Srgrimes		mbstat.m_clfree++;
2171541Srgrimes	}
2181541Srgrimes	mbstat.m_clusters += ncl;
2191541Srgrimes	return (1);
2201541Srgrimes}
2211541Srgrimes
2221541Srgrimes/*
2231541Srgrimes * When MGET failes, ask protocols to free space when short of memory,
2241541Srgrimes * then re-attempt to allocate an mbuf.
2251541Srgrimes */
2261541Srgrimesstruct mbuf *
2271541Srgrimesm_retry(i, t)
2281541Srgrimes	int i, t;
2291541Srgrimes{
2301541Srgrimes	register struct mbuf *m;
2311541Srgrimes
2321541Srgrimes	m_reclaim();
2331541Srgrimes#define m_retry(i, t)	(struct mbuf *)0
2341541Srgrimes	MGET(m, i, t);
2351541Srgrimes#undef m_retry
2366669Sdg	if (m != NULL)
2376669Sdg		mbstat.m_wait++;
2386669Sdg	else
2396669Sdg		mbstat.m_drops++;
2401541Srgrimes	return (m);
2411541Srgrimes}
2421541Srgrimes
2431541Srgrimes/*
2441541Srgrimes * As above; retry an MGETHDR.
2451541Srgrimes */
2461541Srgrimesstruct mbuf *
2471541Srgrimesm_retryhdr(i, t)
2481541Srgrimes	int i, t;
2491541Srgrimes{
2501541Srgrimes	register struct mbuf *m;
2511541Srgrimes
2521541Srgrimes	m_reclaim();
2531541Srgrimes#define m_retryhdr(i, t) (struct mbuf *)0
2541541Srgrimes	MGETHDR(m, i, t);
2551541Srgrimes#undef m_retryhdr
2566669Sdg	if (m != NULL)
2576669Sdg		mbstat.m_wait++;
2586669Sdg	else
2596669Sdg		mbstat.m_drops++;
2601541Srgrimes	return (m);
2611541Srgrimes}
2621541Srgrimes
26312819Sphkstatic void
2641541Srgrimesm_reclaim()
2651541Srgrimes{
2661541Srgrimes	register struct domain *dp;
2671541Srgrimes	register struct protosw *pr;
2681541Srgrimes	int s = splimp();
2691541Srgrimes
2701541Srgrimes	for (dp = domains; dp; dp = dp->dom_next)
2711541Srgrimes		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
2721541Srgrimes			if (pr->pr_drain)
2731541Srgrimes				(*pr->pr_drain)();
2741541Srgrimes	splx(s);
2751541Srgrimes	mbstat.m_drain++;
2761541Srgrimes}
2771541Srgrimes
2781541Srgrimes/*
2791541Srgrimes * Space allocation routines.
2801541Srgrimes * These are also available as macros
2811541Srgrimes * for critical paths.
2821541Srgrimes */
2831541Srgrimesstruct mbuf *
2841541Srgrimesm_get(nowait, type)
2851541Srgrimes	int nowait, type;
2861541Srgrimes{
2871541Srgrimes	register struct mbuf *m;
2881541Srgrimes
2891541Srgrimes	MGET(m, nowait, type);
2901541Srgrimes	return (m);
2911541Srgrimes}
2921541Srgrimes
2931541Srgrimesstruct mbuf *
2941541Srgrimesm_gethdr(nowait, type)
2951541Srgrimes	int nowait, type;
2961541Srgrimes{
2971541Srgrimes	register struct mbuf *m;
2981541Srgrimes
2991541Srgrimes	MGETHDR(m, nowait, type);
3001541Srgrimes	return (m);
3011541Srgrimes}
3021541Srgrimes
3031541Srgrimesstruct mbuf *
3041541Srgrimesm_getclr(nowait, type)
3051541Srgrimes	int nowait, type;
3061541Srgrimes{
3071541Srgrimes	register struct mbuf *m;
3081541Srgrimes
3091541Srgrimes	MGET(m, nowait, type);
3101541Srgrimes	if (m == 0)
3111541Srgrimes		return (0);
3121541Srgrimes	bzero(mtod(m, caddr_t), MLEN);
3131541Srgrimes	return (m);
3141541Srgrimes}
3151541Srgrimes
3161541Srgrimesstruct mbuf *
3171541Srgrimesm_free(m)
3181541Srgrimes	struct mbuf *m;
3191541Srgrimes{
3201541Srgrimes	register struct mbuf *n;
3211541Srgrimes
3221541Srgrimes	MFREE(m, n);
3231541Srgrimes	return (n);
3241541Srgrimes}
3251541Srgrimes
3261541Srgrimesvoid
3271541Srgrimesm_freem(m)
3281541Srgrimes	register struct mbuf *m;
3291541Srgrimes{
3301541Srgrimes	register struct mbuf *n;
3311541Srgrimes
3321541Srgrimes	if (m == NULL)
3331541Srgrimes		return;
3341541Srgrimes	do {
3351541Srgrimes		MFREE(m, n);
3363308Sphk		m = n;
3373308Sphk	} while (m);
3381541Srgrimes}
3391541Srgrimes
3401541Srgrimes/*
3411541Srgrimes * Mbuffer utility routines.
3421541Srgrimes */
3431541Srgrimes
3441541Srgrimes/*
3451541Srgrimes * Lesser-used path for M_PREPEND:
3461541Srgrimes * allocate new mbuf to prepend to chain,
3471541Srgrimes * copy junk along.
3481541Srgrimes */
3491541Srgrimesstruct mbuf *
3501541Srgrimesm_prepend(m, len, how)
3511541Srgrimes	register struct mbuf *m;
3521541Srgrimes	int len, how;
3531541Srgrimes{
3541541Srgrimes	struct mbuf *mn;
3551541Srgrimes
3561541Srgrimes	MGET(mn, how, m->m_type);
3571541Srgrimes	if (mn == (struct mbuf *)NULL) {
3581541Srgrimes		m_freem(m);
3591541Srgrimes		return ((struct mbuf *)NULL);
3601541Srgrimes	}
3611541Srgrimes	if (m->m_flags & M_PKTHDR) {
3621541Srgrimes		M_COPY_PKTHDR(mn, m);
3631541Srgrimes		m->m_flags &= ~M_PKTHDR;
3641541Srgrimes	}
3651541Srgrimes	mn->m_next = m;
3661541Srgrimes	m = mn;
3671541Srgrimes	if (len < MHLEN)
3681541Srgrimes		MH_ALIGN(m, len);
3691541Srgrimes	m->m_len = len;
3701541Srgrimes	return (m);
3711541Srgrimes}
3721541Srgrimes
3731541Srgrimes/*
3741541Srgrimes * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
3751541Srgrimes * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
3761541Srgrimes * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
3771541Srgrimes */
37812819Sphkstatic int MCFail;
3791541Srgrimes
3801541Srgrimesstruct mbuf *
3811541Srgrimesm_copym(m, off0, len, wait)
3821541Srgrimes	register struct mbuf *m;
3831541Srgrimes	int off0, wait;
3841541Srgrimes	register int len;
3851541Srgrimes{
3861541Srgrimes	register struct mbuf *n, **np;
3871541Srgrimes	register int off = off0;
3881541Srgrimes	struct mbuf *top;
3891541Srgrimes	int copyhdr = 0;
3901541Srgrimes
3911541Srgrimes	if (off < 0 || len < 0)
3921541Srgrimes		panic("m_copym");
3931541Srgrimes	if (off == 0 && m->m_flags & M_PKTHDR)
3941541Srgrimes		copyhdr = 1;
3951541Srgrimes	while (off > 0) {
3961541Srgrimes		if (m == 0)
3971541Srgrimes			panic("m_copym");
3981541Srgrimes		if (off < m->m_len)
3991541Srgrimes			break;
4001541Srgrimes		off -= m->m_len;
4011541Srgrimes		m = m->m_next;
4021541Srgrimes	}
4031541Srgrimes	np = &top;
4041541Srgrimes	top = 0;
4051541Srgrimes	while (len > 0) {
4061541Srgrimes		if (m == 0) {
4071541Srgrimes			if (len != M_COPYALL)
4081541Srgrimes				panic("m_copym");
4091541Srgrimes			break;
4101541Srgrimes		}
4111541Srgrimes		MGET(n, wait, m->m_type);
4121541Srgrimes		*np = n;
4131541Srgrimes		if (n == 0)
4141541Srgrimes			goto nospace;
4151541Srgrimes		if (copyhdr) {
4161541Srgrimes			M_COPY_PKTHDR(n, m);
4171541Srgrimes			if (len == M_COPYALL)
4181541Srgrimes				n->m_pkthdr.len -= off0;
4191541Srgrimes			else
4201541Srgrimes				n->m_pkthdr.len = len;
4211541Srgrimes			copyhdr = 0;
4221541Srgrimes		}
4231541Srgrimes		n->m_len = min(len, m->m_len - off);
4241541Srgrimes		if (m->m_flags & M_EXT) {
4251541Srgrimes			n->m_data = m->m_data + off;
42617663Sjulian			if(!m->m_ext.ext_ref)
42717663Sjulian				mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
42817663Sjulian			else
42917663Sjulian				(*(m->m_ext.ext_ref))(m->m_ext.ext_buf,
43017663Sjulian							m->m_ext.ext_size);
4311541Srgrimes			n->m_ext = m->m_ext;
4321541Srgrimes			n->m_flags |= M_EXT;
4331541Srgrimes		} else
4341541Srgrimes			bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
4351541Srgrimes			    (unsigned)n->m_len);
4361541Srgrimes		if (len != M_COPYALL)
4371541Srgrimes			len -= n->m_len;
4381541Srgrimes		off = 0;
4391541Srgrimes		m = m->m_next;
4401541Srgrimes		np = &n->m_next;
4411541Srgrimes	}
4421541Srgrimes	if (top == 0)
4431541Srgrimes		MCFail++;
4441541Srgrimes	return (top);
4451541Srgrimesnospace:
4461541Srgrimes	m_freem(top);
4471541Srgrimes	MCFail++;
4481541Srgrimes	return (0);
4491541Srgrimes}
4501541Srgrimes
4511541Srgrimes/*
45215689Swollman * Copy an entire packet, including header (which must be present).
45315689Swollman * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
45415689Swollman */
45515689Swollmanstruct mbuf *
45615689Swollmanm_copypacket(m, how)
45715689Swollman	struct mbuf *m;
45815689Swollman	int how;
45915689Swollman{
46015689Swollman	struct mbuf *top, *n, *o;
46115689Swollman
46215689Swollman	MGET(n, how, m->m_type);
46315689Swollman	top = n;
46415689Swollman	if (!n)
46515689Swollman		goto nospace;
46615689Swollman
46715689Swollman	M_COPY_PKTHDR(n, m);
46815689Swollman	n->m_len = m->m_len;
46915689Swollman	if (m->m_flags & M_EXT) {
47015689Swollman		n->m_data = m->m_data;
47115689Swollman		mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
47215689Swollman		n->m_ext = m->m_ext;
47315689Swollman		n->m_flags |= M_EXT;
47415689Swollman	} else {
47515689Swollman		bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
47615689Swollman	}
47715689Swollman
47815689Swollman	m = m->m_next;
47915689Swollman	while (m) {
48015689Swollman		MGET(o, how, m->m_type);
48115689Swollman		if (!o)
48215689Swollman			goto nospace;
48315689Swollman
48415689Swollman		n->m_next = o;
48515689Swollman		n = n->m_next;
48615689Swollman
48715689Swollman		n->m_len = m->m_len;
48815689Swollman		if (m->m_flags & M_EXT) {
48915689Swollman			n->m_data = m->m_data;
49015689Swollman			mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
49115689Swollman			n->m_ext = m->m_ext;
49215689Swollman			n->m_flags |= M_EXT;
49315689Swollman		} else {
49415689Swollman			bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
49515689Swollman		}
49615689Swollman
49715689Swollman		m = m->m_next;
49815689Swollman	}
49915689Swollman	return top;
50015689Swollmannospace:
50115689Swollman	m_freem(top);
50215689Swollman	MCFail++;
50315689Swollman	return 0;
50415689Swollman}
50515689Swollman
50615689Swollman/*
5071541Srgrimes * Copy data from an mbuf chain starting "off" bytes from the beginning,
5081541Srgrimes * continuing for "len" bytes, into the indicated buffer.
5091541Srgrimes */
5101549Srgrimesvoid
5111541Srgrimesm_copydata(m, off, len, cp)
5121541Srgrimes	register struct mbuf *m;
5131541Srgrimes	register int off;
5141541Srgrimes	register int len;
5151541Srgrimes	caddr_t cp;
5161541Srgrimes{
5171541Srgrimes	register unsigned count;
5181541Srgrimes
5191541Srgrimes	if (off < 0 || len < 0)
5201541Srgrimes		panic("m_copydata");
5211541Srgrimes	while (off > 0) {
5221541Srgrimes		if (m == 0)
5231541Srgrimes			panic("m_copydata");
5241541Srgrimes		if (off < m->m_len)
5251541Srgrimes			break;
5261541Srgrimes		off -= m->m_len;
5271541Srgrimes		m = m->m_next;
5281541Srgrimes	}
5291541Srgrimes	while (len > 0) {
5301541Srgrimes		if (m == 0)
5311541Srgrimes			panic("m_copydata");
5321541Srgrimes		count = min(m->m_len - off, len);
5331541Srgrimes		bcopy(mtod(m, caddr_t) + off, cp, count);
5341541Srgrimes		len -= count;
5351541Srgrimes		cp += count;
5361541Srgrimes		off = 0;
5371541Srgrimes		m = m->m_next;
5381541Srgrimes	}
5391541Srgrimes}
5401541Srgrimes
5411541Srgrimes/*
5421541Srgrimes * Concatenate mbuf chain n to m.
5431541Srgrimes * Both chains must be of the same type (e.g. MT_DATA).
5441541Srgrimes * Any m_pkthdr is not updated.
5451541Srgrimes */
5461549Srgrimesvoid
5471541Srgrimesm_cat(m, n)
5481541Srgrimes	register struct mbuf *m, *n;
5491541Srgrimes{
5501541Srgrimes	while (m->m_next)
5511541Srgrimes		m = m->m_next;
5521541Srgrimes	while (n) {
5531541Srgrimes		if (m->m_flags & M_EXT ||
5541541Srgrimes		    m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
5551541Srgrimes			/* just join the two chains */
5561541Srgrimes			m->m_next = n;
5571541Srgrimes			return;
5581541Srgrimes		}
5591541Srgrimes		/* splat the data from one into the other */
5601541Srgrimes		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
5611541Srgrimes		    (u_int)n->m_len);
5621541Srgrimes		m->m_len += n->m_len;
5631541Srgrimes		n = m_free(n);
5641541Srgrimes	}
5651541Srgrimes}
5661541Srgrimes
5671549Srgrimesvoid
5681541Srgrimesm_adj(mp, req_len)
5691541Srgrimes	struct mbuf *mp;
5701541Srgrimes	int req_len;
5711541Srgrimes{
5721541Srgrimes	register int len = req_len;
5731541Srgrimes	register struct mbuf *m;
5741541Srgrimes	register count;
5751541Srgrimes
5761541Srgrimes	if ((m = mp) == NULL)
5771541Srgrimes		return;
5781541Srgrimes	if (len >= 0) {
5791541Srgrimes		/*
5801541Srgrimes		 * Trim from head.
5811541Srgrimes		 */
5821541Srgrimes		while (m != NULL && len > 0) {
5831541Srgrimes			if (m->m_len <= len) {
5841541Srgrimes				len -= m->m_len;
5851541Srgrimes				m->m_len = 0;
5861541Srgrimes				m = m->m_next;
5871541Srgrimes			} else {
5881541Srgrimes				m->m_len -= len;
5891541Srgrimes				m->m_data += len;
5901541Srgrimes				len = 0;
5911541Srgrimes			}
5921541Srgrimes		}
5931541Srgrimes		m = mp;
5941541Srgrimes		if (mp->m_flags & M_PKTHDR)
5951541Srgrimes			m->m_pkthdr.len -= (req_len - len);
5961541Srgrimes	} else {
5971541Srgrimes		/*
5981541Srgrimes		 * Trim from tail.  Scan the mbuf chain,
5991541Srgrimes		 * calculating its length and finding the last mbuf.
6001541Srgrimes		 * If the adjustment only affects this mbuf, then just
6011541Srgrimes		 * adjust and return.  Otherwise, rescan and truncate
6021541Srgrimes		 * after the remaining size.
6031541Srgrimes		 */
6041541Srgrimes		len = -len;
6051541Srgrimes		count = 0;
6061541Srgrimes		for (;;) {
6071541Srgrimes			count += m->m_len;
6081541Srgrimes			if (m->m_next == (struct mbuf *)0)
6091541Srgrimes				break;
6101541Srgrimes			m = m->m_next;
6111541Srgrimes		}
6121541Srgrimes		if (m->m_len >= len) {
6131541Srgrimes			m->m_len -= len;
6141541Srgrimes			if (mp->m_flags & M_PKTHDR)
6151541Srgrimes				mp->m_pkthdr.len -= len;
6161541Srgrimes			return;
6171541Srgrimes		}
6181541Srgrimes		count -= len;
6191541Srgrimes		if (count < 0)
6201541Srgrimes			count = 0;
6211541Srgrimes		/*
6221541Srgrimes		 * Correct length for chain is "count".
6231541Srgrimes		 * Find the mbuf with last data, adjust its length,
6241541Srgrimes		 * and toss data from remaining mbufs on chain.
6251541Srgrimes		 */
6261541Srgrimes		m = mp;
6271541Srgrimes		if (m->m_flags & M_PKTHDR)
6281541Srgrimes			m->m_pkthdr.len = count;
6291541Srgrimes		for (; m; m = m->m_next) {
6301541Srgrimes			if (m->m_len >= count) {
6311541Srgrimes				m->m_len = count;
6321541Srgrimes				break;
6331541Srgrimes			}
6341541Srgrimes			count -= m->m_len;
6351541Srgrimes		}
6363308Sphk		while (m->m_next)
6373308Sphk			(m = m->m_next) ->m_len = 0;
6381541Srgrimes	}
6391541Srgrimes}
6401541Srgrimes
6411541Srgrimes/*
6421541Srgrimes * Rearange an mbuf chain so that len bytes are contiguous
6431541Srgrimes * and in the data area of an mbuf (so that mtod and dtom
6441541Srgrimes * will work for a structure of size len).  Returns the resulting
6451541Srgrimes * mbuf chain on success, frees it and returns null on failure.
6461541Srgrimes * If there is room, it will add up to max_protohdr-len extra bytes to the
6471541Srgrimes * contiguous region in an attempt to avoid being called next time.
6481541Srgrimes */
64912819Sphkstatic int MPFail;
6501541Srgrimes
6511541Srgrimesstruct mbuf *
6521541Srgrimesm_pullup(n, len)
6531541Srgrimes	register struct mbuf *n;
6541541Srgrimes	int len;
6551541Srgrimes{
6561541Srgrimes	register struct mbuf *m;
6571541Srgrimes	register int count;
6581541Srgrimes	int space;
6591541Srgrimes
6601541Srgrimes	/*
6611541Srgrimes	 * If first mbuf has no cluster, and has room for len bytes
6621541Srgrimes	 * without shifting current data, pullup into it,
6631541Srgrimes	 * otherwise allocate a new mbuf to prepend to the chain.
6641541Srgrimes	 */
6651541Srgrimes	if ((n->m_flags & M_EXT) == 0 &&
6661541Srgrimes	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
6671541Srgrimes		if (n->m_len >= len)
6681541Srgrimes			return (n);
6691541Srgrimes		m = n;
6701541Srgrimes		n = n->m_next;
6711541Srgrimes		len -= m->m_len;
6721541Srgrimes	} else {
6731541Srgrimes		if (len > MHLEN)
6741541Srgrimes			goto bad;
6751541Srgrimes		MGET(m, M_DONTWAIT, n->m_type);
6761541Srgrimes		if (m == 0)
6771541Srgrimes			goto bad;
6781541Srgrimes		m->m_len = 0;
6791541Srgrimes		if (n->m_flags & M_PKTHDR) {
6801541Srgrimes			M_COPY_PKTHDR(m, n);
6811541Srgrimes			n->m_flags &= ~M_PKTHDR;
6821541Srgrimes		}
6831541Srgrimes	}
6841541Srgrimes	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
6851541Srgrimes	do {
6861541Srgrimes		count = min(min(max(len, max_protohdr), space), n->m_len);
6871541Srgrimes		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
6881541Srgrimes		  (unsigned)count);
6891541Srgrimes		len -= count;
6901541Srgrimes		m->m_len += count;
6911541Srgrimes		n->m_len -= count;
6921541Srgrimes		space -= count;
6931541Srgrimes		if (n->m_len)
6941541Srgrimes			n->m_data += count;
6951541Srgrimes		else
6961541Srgrimes			n = m_free(n);
6971541Srgrimes	} while (len > 0 && n);
6981541Srgrimes	if (len > 0) {
6991541Srgrimes		(void) m_free(m);
7001541Srgrimes		goto bad;
7011541Srgrimes	}
7021541Srgrimes	m->m_next = n;
7031541Srgrimes	return (m);
7041541Srgrimesbad:
7051541Srgrimes	m_freem(n);
7061541Srgrimes	MPFail++;
7071541Srgrimes	return (0);
7081541Srgrimes}
7091541Srgrimes
7101541Srgrimes/*
7111541Srgrimes * Partition an mbuf chain in two pieces, returning the tail --
7121541Srgrimes * all but the first len0 bytes.  In case of failure, it returns NULL and
7131541Srgrimes * attempts to restore the chain to its original state.
7141541Srgrimes */
7151541Srgrimesstruct mbuf *
7161541Srgrimesm_split(m0, len0, wait)
7171541Srgrimes	register struct mbuf *m0;
7181541Srgrimes	int len0, wait;
7191541Srgrimes{
7201541Srgrimes	register struct mbuf *m, *n;
7211541Srgrimes	unsigned len = len0, remain;
7221541Srgrimes
7231541Srgrimes	for (m = m0; m && len > m->m_len; m = m->m_next)
7241541Srgrimes		len -= m->m_len;
7251541Srgrimes	if (m == 0)
7261541Srgrimes		return (0);
7271541Srgrimes	remain = m->m_len - len;
7281541Srgrimes	if (m0->m_flags & M_PKTHDR) {
7291541Srgrimes		MGETHDR(n, wait, m0->m_type);
7301541Srgrimes		if (n == 0)
7311541Srgrimes			return (0);
7321541Srgrimes		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
7331541Srgrimes		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
7341541Srgrimes		m0->m_pkthdr.len = len0;
7351541Srgrimes		if (m->m_flags & M_EXT)
7361541Srgrimes			goto extpacket;
7371541Srgrimes		if (remain > MHLEN) {
7381541Srgrimes			/* m can't be the lead packet */
7391541Srgrimes			MH_ALIGN(n, 0);
7401541Srgrimes			n->m_next = m_split(m, len, wait);
7411541Srgrimes			if (n->m_next == 0) {
7421541Srgrimes				(void) m_free(n);
7431541Srgrimes				return (0);
7441541Srgrimes			} else
7451541Srgrimes				return (n);
7461541Srgrimes		} else
7471541Srgrimes			MH_ALIGN(n, remain);
7481541Srgrimes	} else if (remain == 0) {
7491541Srgrimes		n = m->m_next;
7501541Srgrimes		m->m_next = 0;
7511541Srgrimes		return (n);
7521541Srgrimes	} else {
7531541Srgrimes		MGET(n, wait, m->m_type);
7541541Srgrimes		if (n == 0)
7551541Srgrimes			return (0);
7561541Srgrimes		M_ALIGN(n, remain);
7571541Srgrimes	}
7581541Srgrimesextpacket:
7591541Srgrimes	if (m->m_flags & M_EXT) {
7601541Srgrimes		n->m_flags |= M_EXT;
7611541Srgrimes		n->m_ext = m->m_ext;
76217663Sjulian		if(!m->m_ext.ext_ref)
76317663Sjulian			mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
76417663Sjulian		else
76517663Sjulian			(*(m->m_ext.ext_ref))(m->m_ext.ext_buf,
76617663Sjulian						m->m_ext.ext_size);
7671541Srgrimes		m->m_ext.ext_size = 0; /* For Accounting XXXXXX danger */
7681541Srgrimes		n->m_data = m->m_data + len;
7691541Srgrimes	} else {
7701541Srgrimes		bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
7711541Srgrimes	}
7721541Srgrimes	n->m_len = remain;
7731541Srgrimes	m->m_len = len;
7741541Srgrimes	n->m_next = m->m_next;
7751541Srgrimes	m->m_next = 0;
7761541Srgrimes	return (n);
7771541Srgrimes}
7781541Srgrimes/*
7791541Srgrimes * Routine to copy from device local memory into mbufs.
7801541Srgrimes */
7811541Srgrimesstruct mbuf *
7821541Srgrimesm_devget(buf, totlen, off0, ifp, copy)
7831541Srgrimes	char *buf;
7841541Srgrimes	int totlen, off0;
7851541Srgrimes	struct ifnet *ifp;
78612577Sbde	void (*copy) __P((char *from, caddr_t to, u_int len));
7871541Srgrimes{
7881541Srgrimes	register struct mbuf *m;
7891541Srgrimes	struct mbuf *top = 0, **mp = &top;
7901541Srgrimes	register int off = off0, len;
7911541Srgrimes	register char *cp;
7921541Srgrimes	char *epkt;
7931541Srgrimes
7941541Srgrimes	cp = buf;
7951541Srgrimes	epkt = cp + totlen;
7961541Srgrimes	if (off) {
7971541Srgrimes		cp += off + 2 * sizeof(u_short);
7981541Srgrimes		totlen -= 2 * sizeof(u_short);
7991541Srgrimes	}
8001541Srgrimes	MGETHDR(m, M_DONTWAIT, MT_DATA);
8011541Srgrimes	if (m == 0)
8021541Srgrimes		return (0);
8031541Srgrimes	m->m_pkthdr.rcvif = ifp;
8041541Srgrimes	m->m_pkthdr.len = totlen;
8051541Srgrimes	m->m_len = MHLEN;
8061541Srgrimes
8071541Srgrimes	while (totlen > 0) {
8081541Srgrimes		if (top) {
8091541Srgrimes			MGET(m, M_DONTWAIT, MT_DATA);
8101541Srgrimes			if (m == 0) {
8111541Srgrimes				m_freem(top);
8121541Srgrimes				return (0);
8131541Srgrimes			}
8141541Srgrimes			m->m_len = MLEN;
8151541Srgrimes		}
8161541Srgrimes		len = min(totlen, epkt - cp);
8171541Srgrimes		if (len >= MINCLSIZE) {
8181541Srgrimes			MCLGET(m, M_DONTWAIT);
8191541Srgrimes			if (m->m_flags & M_EXT)
8201541Srgrimes				m->m_len = len = min(len, MCLBYTES);
8211541Srgrimes			else
8221541Srgrimes				len = m->m_len;
8231541Srgrimes		} else {
8241541Srgrimes			/*
8251541Srgrimes			 * Place initial small packet/header at end of mbuf.
8261541Srgrimes			 */
8271541Srgrimes			if (len < m->m_len) {
8281541Srgrimes				if (top == 0 && len + max_linkhdr <= m->m_len)
8291541Srgrimes					m->m_data += max_linkhdr;
8301541Srgrimes				m->m_len = len;
8311541Srgrimes			} else
8321541Srgrimes				len = m->m_len;
8331541Srgrimes		}
8341541Srgrimes		if (copy)
8351541Srgrimes			copy(cp, mtod(m, caddr_t), (unsigned)len);
8361541Srgrimes		else
8371541Srgrimes			bcopy(cp, mtod(m, caddr_t), (unsigned)len);
8381541Srgrimes		cp += len;
8391541Srgrimes		*mp = m;
8401541Srgrimes		mp = &m->m_next;
8411541Srgrimes		totlen -= len;
8421541Srgrimes		if (cp == epkt)
8431541Srgrimes			cp = buf;
8441541Srgrimes	}
8451541Srgrimes	return (top);
8461541Srgrimes}
8473352Sphk
8483352Sphk/*
8493352Sphk * Copy data from a buffer back into the indicated mbuf chain,
8503352Sphk * starting "off" bytes from the beginning, extending the mbuf
8513352Sphk * chain if necessary.
8523352Sphk */
8533352Sphkvoid
8543352Sphkm_copyback(m0, off, len, cp)
8553352Sphk	struct	mbuf *m0;
8563352Sphk	register int off;
8573352Sphk	register int len;
8583352Sphk	caddr_t cp;
8593352Sphk{
8603352Sphk	register int mlen;
8613352Sphk	register struct mbuf *m = m0, *n;
8623352Sphk	int totlen = 0;
8633352Sphk
8643352Sphk	if (m0 == 0)
8653352Sphk		return;
8663352Sphk	while (off > (mlen = m->m_len)) {
8673352Sphk		off -= mlen;
8683352Sphk		totlen += mlen;
8693352Sphk		if (m->m_next == 0) {
8703352Sphk			n = m_getclr(M_DONTWAIT, m->m_type);
8713352Sphk			if (n == 0)
8723352Sphk				goto out;
8733352Sphk			n->m_len = min(MLEN, len + off);
8743352Sphk			m->m_next = n;
8753352Sphk		}
8763352Sphk		m = m->m_next;
8773352Sphk	}
8783352Sphk	while (len > 0) {
8793352Sphk		mlen = min (m->m_len - off, len);
8803352Sphk		bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen);
8813352Sphk		cp += mlen;
8823352Sphk		len -= mlen;
8833352Sphk		mlen += off;
8843352Sphk		off = 0;
8853352Sphk		totlen += mlen;
8863352Sphk		if (len == 0)
8873352Sphk			break;
8883352Sphk		if (m->m_next == 0) {
8893352Sphk			n = m_get(M_DONTWAIT, m->m_type);
8903352Sphk			if (n == 0)
8913352Sphk				break;
8923352Sphk			n->m_len = min(MLEN, len);
8933352Sphk			m->m_next = n;
8943352Sphk		}
8953352Sphk		m = m->m_next;
8963352Sphk	}
8973352Sphkout:	if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
8983352Sphk		m->m_pkthdr.len = totlen;
8993352Sphk}
900