uipc_mbuf.c revision 54002
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
3450477Speter * $FreeBSD: head/sys/kern/uipc_mbuf.c 54002 1999-12-01 22:31:32Z archie $
351541Srgrimes */
361541Srgrimes
3748579Smsmith#include "opt_param.h"
381541Srgrimes#include <sys/param.h>
391541Srgrimes#include <sys/systm.h>
4032036Sbde#include <sys/malloc.h>
411541Srgrimes#include <sys/mbuf.h>
421541Srgrimes#include <sys/kernel.h>
4323081Swollman#include <sys/sysctl.h>
441541Srgrimes#include <sys/domain.h>
451541Srgrimes#include <sys/protosw.h>
461541Srgrimes
471541Srgrimes#include <vm/vm.h>
489759Sbde#include <vm/vm_kern.h>
4912662Sdg#include <vm/vm_extern.h>
501541Srgrimes
5110653Sdgstatic void mbinit __P((void *));
5210358SjulianSYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbinit, NULL)
5310358Sjulian
549759Sbdestruct mbuf *mbutl;
551541Srgrimeschar	*mclrefcnt;
569759Sbdestruct mbstat mbstat;
5715689Swollmanstruct mbuf *mmbfree;
589759Sbdeunion mcluster *mclfree;
599759Sbdeint	max_linkhdr;
609759Sbdeint	max_protohdr;
619759Sbdeint	max_hdr;
629759Sbdeint	max_datalen;
6348579Smsmithint	nmbclusters;
6448579Smsmithint	nmbufs;
651541Srgrimes
6644078SdfrSYSCTL_DECL(_kern_ipc);
6723081SwollmanSYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RW,
6823081Swollman	   &max_linkhdr, 0, "");
6923081SwollmanSYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RW,
7023081Swollman	   &max_protohdr, 0, "");
7123081SwollmanSYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RW, &max_hdr, 0, "");
7223081SwollmanSYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RW,
7323081Swollman	   &max_datalen, 0, "");
7423081SwollmanSYSCTL_STRUCT(_kern_ipc, KIPC_MBSTAT, mbstat, CTLFLAG_RW, &mbstat, mbstat, "");
7548579SmsmithSYSCTL_INT(_kern_ipc, KIPC_NMBCLUSTERS, nmbclusters, CTLFLAG_RD,
7648579Smsmith	   &nmbclusters, 0, "Maximum number of mbuf clusters avaliable");
7748579Smsmith#ifndef NMBCLUSTERS
7848579Smsmith#define NMBCLUSTERS	(512 + MAXUSERS * 16)
7948579Smsmith#endif
8048579SmsmithTUNABLE_INT_DECL("kern.ipc.nmbclusters", NMBCLUSTERS, nmbclusters);
8148579SmsmithTUNABLE_INT_DECL("kern.ipc.nmbufs", NMBCLUSTERS * 4, nmbufs);	/* XXX fixup? */
8223081Swollman
8312819Sphkstatic void	m_reclaim __P((void));
8412819Sphk
8515736Sphk/* "number of clusters of pages" */
8615736Sphk#define NCL_INIT	1
8715736Sphk
8815744Sphk#define NMB_INIT	16
8915744Sphk
9010358Sjulian/* ARGSUSED*/
9110358Sjulianstatic void
9212569Sbdembinit(dummy)
9312569Sbde	void *dummy;
941541Srgrimes{
951541Srgrimes	int s;
961541Srgrimes
9715689Swollman	mmbfree = NULL; mclfree = NULL;
9823081Swollman	mbstat.m_msize = MSIZE;
9923081Swollman	mbstat.m_mclbytes = MCLBYTES;
10023081Swollman	mbstat.m_minclsize = MINCLSIZE;
10123081Swollman	mbstat.m_mlen = MLEN;
10223081Swollman	mbstat.m_mhlen = MHLEN;
10323081Swollman
1041541Srgrimes	s = splimp();
10515689Swollman	if (m_mballoc(NMB_INIT, M_DONTWAIT) == 0)
10615689Swollman		goto bad;
10722671Swollman#if MCLBYTES <= PAGE_SIZE
1081541Srgrimes	if (m_clalloc(NCL_INIT, M_DONTWAIT) == 0)
1091541Srgrimes		goto bad;
11022671Swollman#else
11122671Swollman	/* It's OK to call contigmalloc in this context. */
11232036Sbde	if (m_clalloc(16, M_WAIT) == 0)
11322671Swollman		goto bad;
11422671Swollman#endif
1151541Srgrimes	splx(s);
1161541Srgrimes	return;
1171541Srgrimesbad:
1181541Srgrimes	panic("mbinit");
1191541Srgrimes}
1201541Srgrimes
1211541Srgrimes/*
12215689Swollman * Allocate at least nmb mbufs and place on mbuf free list.
12315689Swollman * Must be called at splimp.
12415689Swollman */
12515689Swollman/* ARGSUSED */
12615689Swollmanint
12732036Sbdem_mballoc(nmb, how)
12815689Swollman	register int nmb;
12932036Sbde	int how;
13015689Swollman{
13115689Swollman	register caddr_t p;
13215689Swollman	register int i;
13315689Swollman	int nbytes;
13415689Swollman
13515689Swollman	/* Once we run out of map space, it will be impossible to get
13615689Swollman	 * any more (nothing is ever freed back to the map) (XXX which
13715689Swollman	 * is dumb). (however you are not dead as m_reclaim might
13815689Swollman	 * still be able to free a substantial amount of space).
13915689Swollman	 */
14015689Swollman	if (mb_map_full)
14115689Swollman		return (0);
14215689Swollman
14315689Swollman	nbytes = round_page(nmb * MSIZE);
14422899Swollman	p = (caddr_t)kmem_malloc(mb_map, nbytes, M_NOWAIT);
14532036Sbde	if (p == 0 && how == M_WAIT) {
14622899Swollman		mbstat.m_wait++;
14722899Swollman		p = (caddr_t)kmem_malloc(mb_map, nbytes, M_WAITOK);
14822899Swollman	}
14922899Swollman
15015689Swollman	/*
15132036Sbde	 * Either the map is now full, or `how' is M_NOWAIT and there
15215689Swollman	 * are no pages left.
15315689Swollman	 */
15415689Swollman	if (p == NULL)
15515689Swollman		return (0);
15615689Swollman
15715689Swollman	nmb = nbytes / MSIZE;
15815689Swollman	for (i = 0; i < nmb; i++) {
15915689Swollman		((struct mbuf *)p)->m_next = mmbfree;
16015689Swollman		mmbfree = (struct mbuf *)p;
16115689Swollman		p += MSIZE;
16215689Swollman	}
16315689Swollman	mbstat.m_mbufs += nmb;
16415689Swollman	return (1);
16515689Swollman}
16615689Swollman
16722671Swollman#if MCLBYTES > PAGE_SIZE
16822899Swollmanstatic int i_want_my_mcl;
16922671Swollman
17022899Swollmanstatic void
17122671Swollmankproc_mclalloc(void)
17222671Swollman{
17322671Swollman	int status;
17422671Swollman
17522671Swollman	while (1) {
17622671Swollman		tsleep(&i_want_my_mcl, PVM, "mclalloc", 0);
17722671Swollman
17822671Swollman		for (; i_want_my_mcl; i_want_my_mcl--) {
17932036Sbde			if (m_clalloc(1, M_WAIT) == 0)
18022671Swollman				printf("m_clalloc failed even in process context!\n");
18122671Swollman		}
18222671Swollman	}
18322671Swollman}
18422671Swollman
18522671Swollmanstatic struct proc *mclallocproc;
18622671Swollmanstatic struct kproc_desc mclalloc_kp = {
18722671Swollman	"mclalloc",
18822671Swollman	kproc_mclalloc,
18922671Swollman	&mclallocproc
19022671Swollman};
19148391SpeterSYSINIT(mclallocproc, SI_SUB_KTHREAD_UPDATE, SI_ORDER_ANY, kproc_start,
19222671Swollman	   &mclalloc_kp);
19322671Swollman#endif
19422671Swollman
19515689Swollman/*
1961541Srgrimes * Allocate some number of mbuf clusters
1971541Srgrimes * and place on cluster free list.
1981541Srgrimes * Must be called at splimp.
1991541Srgrimes */
2001541Srgrimes/* ARGSUSED */
2011549Srgrimesint
20232036Sbdem_clalloc(ncl, how)
2031541Srgrimes	register int ncl;
20432036Sbde	int how;
2051541Srgrimes{
2061541Srgrimes	register caddr_t p;
2071541Srgrimes	register int i;
2081541Srgrimes	int npg;
2091541Srgrimes
2107066Sdg	/*
2117066Sdg	 * Once we run out of map space, it will be impossible
2127066Sdg	 * to get any more (nothing is ever freed back to the
2137066Sdg	 * map).
2147066Sdg	 */
21522899Swollman	if (mb_map_full) {
21622899Swollman		mbstat.m_drops++;
2177066Sdg		return (0);
21822899Swollman	}
2197066Sdg
22022671Swollman#if MCLBYTES > PAGE_SIZE
22132036Sbde	if (how != M_WAIT) {
22222671Swollman		i_want_my_mcl += ncl;
22322671Swollman		wakeup(&i_want_my_mcl);
22422899Swollman		mbstat.m_wait++;
22522671Swollman		p = 0;
22622671Swollman	} else {
22722671Swollman		p = contigmalloc1(MCLBYTES * ncl, M_DEVBUF, M_WAITOK, 0ul,
22822671Swollman				  ~0ul, PAGE_SIZE, 0, mb_map);
22922671Swollman	}
23022671Swollman#else
23115543Sphk	npg = ncl;
23221737Sdg	p = (caddr_t)kmem_malloc(mb_map, ctob(npg),
23332036Sbde				 how != M_WAIT ? M_NOWAIT : M_WAITOK);
23422671Swollman	ncl = ncl * PAGE_SIZE / MCLBYTES;
23522671Swollman#endif
2367066Sdg	/*
23732036Sbde	 * Either the map is now full, or `how' is M_NOWAIT and there
2387066Sdg	 * are no pages left.
2397066Sdg	 */
24022899Swollman	if (p == NULL) {
24122899Swollman		mbstat.m_drops++;
2421541Srgrimes		return (0);
24322899Swollman	}
2447066Sdg
2451541Srgrimes	for (i = 0; i < ncl; i++) {
2461541Srgrimes		((union mcluster *)p)->mcl_next = mclfree;
2471541Srgrimes		mclfree = (union mcluster *)p;
2481541Srgrimes		p += MCLBYTES;
2491541Srgrimes		mbstat.m_clfree++;
2501541Srgrimes	}
2511541Srgrimes	mbstat.m_clusters += ncl;
2521541Srgrimes	return (1);
2531541Srgrimes}
2541541Srgrimes
2551541Srgrimes/*
25645615Sdes * When MGET fails, ask protocols to free space when short of memory,
2571541Srgrimes * then re-attempt to allocate an mbuf.
2581541Srgrimes */
2591541Srgrimesstruct mbuf *
2601541Srgrimesm_retry(i, t)
2611541Srgrimes	int i, t;
2621541Srgrimes{
2631541Srgrimes	register struct mbuf *m;
2641541Srgrimes
26537878Sdg	/*
26637878Sdg	 * Must only do the reclaim if not in an interrupt context.
26737878Sdg	 */
26837878Sdg	if (i == M_WAIT)
26937878Sdg		m_reclaim();
2701541Srgrimes#define m_retry(i, t)	(struct mbuf *)0
2711541Srgrimes	MGET(m, i, t);
2721541Srgrimes#undef m_retry
27336675Sdg	if (m != NULL) {
2746669Sdg		mbstat.m_wait++;
27536675Sdg	} else {
27636675Sdg		if (i == M_DONTWAIT)
27736675Sdg			mbstat.m_drops++;
27836675Sdg		else
27936675Sdg			panic("Out of mbuf clusters");
28036675Sdg	}
2811541Srgrimes	return (m);
2821541Srgrimes}
2831541Srgrimes
2841541Srgrimes/*
2851541Srgrimes * As above; retry an MGETHDR.
2861541Srgrimes */
2871541Srgrimesstruct mbuf *
2881541Srgrimesm_retryhdr(i, t)
2891541Srgrimes	int i, t;
2901541Srgrimes{
2911541Srgrimes	register struct mbuf *m;
2921541Srgrimes
29337878Sdg	/*
29437878Sdg	 * Must only do the reclaim if not in an interrupt context.
29537878Sdg	 */
29637878Sdg	if (i == M_WAIT)
29737878Sdg		m_reclaim();
2981541Srgrimes#define m_retryhdr(i, t) (struct mbuf *)0
2991541Srgrimes	MGETHDR(m, i, t);
3001541Srgrimes#undef m_retryhdr
30136675Sdg	if (m != NULL) {
3026669Sdg		mbstat.m_wait++;
30336675Sdg	} else {
30436675Sdg		if (i == M_DONTWAIT)
30536675Sdg			mbstat.m_drops++;
30636675Sdg		else
30736675Sdg			panic("Out of mbuf clusters");
30836675Sdg	}
3091541Srgrimes	return (m);
3101541Srgrimes}
3111541Srgrimes
31212819Sphkstatic void
3131541Srgrimesm_reclaim()
3141541Srgrimes{
3151541Srgrimes	register struct domain *dp;
3161541Srgrimes	register struct protosw *pr;
3171541Srgrimes	int s = splimp();
3181541Srgrimes
3191541Srgrimes	for (dp = domains; dp; dp = dp->dom_next)
3201541Srgrimes		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
3211541Srgrimes			if (pr->pr_drain)
3221541Srgrimes				(*pr->pr_drain)();
3231541Srgrimes	splx(s);
3241541Srgrimes	mbstat.m_drain++;
3251541Srgrimes}
3261541Srgrimes
3271541Srgrimes/*
3281541Srgrimes * Space allocation routines.
3291541Srgrimes * These are also available as macros
3301541Srgrimes * for critical paths.
3311541Srgrimes */
3321541Srgrimesstruct mbuf *
33332036Sbdem_get(how, type)
33432036Sbde	int how, type;
3351541Srgrimes{
3361541Srgrimes	register struct mbuf *m;
3371541Srgrimes
33832036Sbde	MGET(m, how, type);
3391541Srgrimes	return (m);
3401541Srgrimes}
3411541Srgrimes
3421541Srgrimesstruct mbuf *
34332036Sbdem_gethdr(how, type)
34432036Sbde	int how, type;
3451541Srgrimes{
3461541Srgrimes	register struct mbuf *m;
3471541Srgrimes
34832036Sbde	MGETHDR(m, how, type);
3491541Srgrimes	return (m);
3501541Srgrimes}
3511541Srgrimes
3521541Srgrimesstruct mbuf *
35332036Sbdem_getclr(how, type)
35432036Sbde	int how, type;
3551541Srgrimes{
3561541Srgrimes	register struct mbuf *m;
3571541Srgrimes
35832036Sbde	MGET(m, how, type);
3591541Srgrimes	if (m == 0)
3601541Srgrimes		return (0);
3611541Srgrimes	bzero(mtod(m, caddr_t), MLEN);
3621541Srgrimes	return (m);
3631541Srgrimes}
3641541Srgrimes
3651541Srgrimesstruct mbuf *
3661541Srgrimesm_free(m)
3671541Srgrimes	struct mbuf *m;
3681541Srgrimes{
3691541Srgrimes	register struct mbuf *n;
3701541Srgrimes
3711541Srgrimes	MFREE(m, n);
3721541Srgrimes	return (n);
3731541Srgrimes}
3741541Srgrimes
3751541Srgrimesvoid
3761541Srgrimesm_freem(m)
3771541Srgrimes	register struct mbuf *m;
3781541Srgrimes{
3791541Srgrimes	register struct mbuf *n;
3801541Srgrimes
3811541Srgrimes	if (m == NULL)
3821541Srgrimes		return;
3831541Srgrimes	do {
3841541Srgrimes		MFREE(m, n);
3853308Sphk		m = n;
3863308Sphk	} while (m);
3871541Srgrimes}
3881541Srgrimes
3891541Srgrimes/*
3901541Srgrimes * Mbuffer utility routines.
3911541Srgrimes */
3921541Srgrimes
3931541Srgrimes/*
3941541Srgrimes * Lesser-used path for M_PREPEND:
3951541Srgrimes * allocate new mbuf to prepend to chain,
3961541Srgrimes * copy junk along.
3971541Srgrimes */
3981541Srgrimesstruct mbuf *
3991541Srgrimesm_prepend(m, len, how)
4001541Srgrimes	register struct mbuf *m;
4011541Srgrimes	int len, how;
4021541Srgrimes{
4031541Srgrimes	struct mbuf *mn;
4041541Srgrimes
4051541Srgrimes	MGET(mn, how, m->m_type);
4061541Srgrimes	if (mn == (struct mbuf *)NULL) {
4071541Srgrimes		m_freem(m);
4081541Srgrimes		return ((struct mbuf *)NULL);
4091541Srgrimes	}
4101541Srgrimes	if (m->m_flags & M_PKTHDR) {
4111541Srgrimes		M_COPY_PKTHDR(mn, m);
4121541Srgrimes		m->m_flags &= ~M_PKTHDR;
4131541Srgrimes	}
4141541Srgrimes	mn->m_next = m;
4151541Srgrimes	m = mn;
4161541Srgrimes	if (len < MHLEN)
4171541Srgrimes		MH_ALIGN(m, len);
4181541Srgrimes	m->m_len = len;
4191541Srgrimes	return (m);
4201541Srgrimes}
4211541Srgrimes
4221541Srgrimes/*
4231541Srgrimes * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
4241541Srgrimes * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
4251541Srgrimes * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
42654002Sarchie * Note that the copy is read-only, because clusters are not copied,
42754002Sarchie * only their reference counts are incremented.
4281541Srgrimes */
42923081Swollman#define MCFail (mbstat.m_mcfail)
4301541Srgrimes
4311541Srgrimesstruct mbuf *
4321541Srgrimesm_copym(m, off0, len, wait)
4331541Srgrimes	register struct mbuf *m;
4341541Srgrimes	int off0, wait;
4351541Srgrimes	register int len;
4361541Srgrimes{
4371541Srgrimes	register struct mbuf *n, **np;
4381541Srgrimes	register int off = off0;
4391541Srgrimes	struct mbuf *top;
4401541Srgrimes	int copyhdr = 0;
4411541Srgrimes
44252201Salfred	KASSERT(off >= 0, ("m_copym, negative off %d", off));
44352201Salfred	KASSERT(len >= 0, ("m_copym, negative len %d", len));
4441541Srgrimes	if (off == 0 && m->m_flags & M_PKTHDR)
4451541Srgrimes		copyhdr = 1;
4461541Srgrimes	while (off > 0) {
44752201Salfred		KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain"));
4481541Srgrimes		if (off < m->m_len)
4491541Srgrimes			break;
4501541Srgrimes		off -= m->m_len;
4511541Srgrimes		m = m->m_next;
4521541Srgrimes	}
4531541Srgrimes	np = &top;
4541541Srgrimes	top = 0;
4551541Srgrimes	while (len > 0) {
4561541Srgrimes		if (m == 0) {
45752201Salfred			KASSERT(len == M_COPYALL,
45852201Salfred			    ("m_copym, length > size of mbuf chain"));
4591541Srgrimes			break;
4601541Srgrimes		}
4611541Srgrimes		MGET(n, wait, m->m_type);
4621541Srgrimes		*np = n;
4631541Srgrimes		if (n == 0)
4641541Srgrimes			goto nospace;
4651541Srgrimes		if (copyhdr) {
4661541Srgrimes			M_COPY_PKTHDR(n, m);
4671541Srgrimes			if (len == M_COPYALL)
4681541Srgrimes				n->m_pkthdr.len -= off0;
4691541Srgrimes			else
4701541Srgrimes				n->m_pkthdr.len = len;
4711541Srgrimes			copyhdr = 0;
4721541Srgrimes		}
4731541Srgrimes		n->m_len = min(len, m->m_len - off);
4741541Srgrimes		if (m->m_flags & M_EXT) {
4751541Srgrimes			n->m_data = m->m_data + off;
47617663Sjulian			if(!m->m_ext.ext_ref)
47717663Sjulian				mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
47817663Sjulian			else
47917663Sjulian				(*(m->m_ext.ext_ref))(m->m_ext.ext_buf,
48017663Sjulian							m->m_ext.ext_size);
4811541Srgrimes			n->m_ext = m->m_ext;
4821541Srgrimes			n->m_flags |= M_EXT;
4831541Srgrimes		} else
4841541Srgrimes			bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
4851541Srgrimes			    (unsigned)n->m_len);
4861541Srgrimes		if (len != M_COPYALL)
4871541Srgrimes			len -= n->m_len;
4881541Srgrimes		off = 0;
4891541Srgrimes		m = m->m_next;
4901541Srgrimes		np = &n->m_next;
4911541Srgrimes	}
4921541Srgrimes	if (top == 0)
4931541Srgrimes		MCFail++;
4941541Srgrimes	return (top);
4951541Srgrimesnospace:
4961541Srgrimes	m_freem(top);
4971541Srgrimes	MCFail++;
4981541Srgrimes	return (0);
4991541Srgrimes}
5001541Srgrimes
5011541Srgrimes/*
50215689Swollman * Copy an entire packet, including header (which must be present).
50315689Swollman * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
50454002Sarchie * Note that the copy is read-only, because clusters are not copied,
50554002Sarchie * only their reference counts are incremented.
50615689Swollman */
50715689Swollmanstruct mbuf *
50815689Swollmanm_copypacket(m, how)
50915689Swollman	struct mbuf *m;
51015689Swollman	int how;
51115689Swollman{
51215689Swollman	struct mbuf *top, *n, *o;
51315689Swollman
51415689Swollman	MGET(n, how, m->m_type);
51515689Swollman	top = n;
51615689Swollman	if (!n)
51715689Swollman		goto nospace;
51815689Swollman
51915689Swollman	M_COPY_PKTHDR(n, m);
52015689Swollman	n->m_len = m->m_len;
52115689Swollman	if (m->m_flags & M_EXT) {
52215689Swollman		n->m_data = m->m_data;
52337350Sphk		if(!m->m_ext.ext_ref)
52437350Sphk			mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
52537350Sphk		else
52637350Sphk			(*(m->m_ext.ext_ref))(m->m_ext.ext_buf,
52737350Sphk						m->m_ext.ext_size);
52815689Swollman		n->m_ext = m->m_ext;
52915689Swollman		n->m_flags |= M_EXT;
53015689Swollman	} else {
53115689Swollman		bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
53215689Swollman	}
53315689Swollman
53415689Swollman	m = m->m_next;
53515689Swollman	while (m) {
53615689Swollman		MGET(o, how, m->m_type);
53715689Swollman		if (!o)
53815689Swollman			goto nospace;
53915689Swollman
54015689Swollman		n->m_next = o;
54115689Swollman		n = n->m_next;
54215689Swollman
54315689Swollman		n->m_len = m->m_len;
54415689Swollman		if (m->m_flags & M_EXT) {
54515689Swollman			n->m_data = m->m_data;
54637350Sphk			if(!m->m_ext.ext_ref)
54737350Sphk				mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
54837350Sphk			else
54937350Sphk				(*(m->m_ext.ext_ref))(m->m_ext.ext_buf,
55037350Sphk							m->m_ext.ext_size);
55115689Swollman			n->m_ext = m->m_ext;
55215689Swollman			n->m_flags |= M_EXT;
55315689Swollman		} else {
55415689Swollman			bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
55515689Swollman		}
55615689Swollman
55715689Swollman		m = m->m_next;
55815689Swollman	}
55915689Swollman	return top;
56015689Swollmannospace:
56115689Swollman	m_freem(top);
56215689Swollman	MCFail++;
56315689Swollman	return 0;
56415689Swollman}
56515689Swollman
56615689Swollman/*
5671541Srgrimes * Copy data from an mbuf chain starting "off" bytes from the beginning,
5681541Srgrimes * continuing for "len" bytes, into the indicated buffer.
5691541Srgrimes */
5701549Srgrimesvoid
5711541Srgrimesm_copydata(m, off, len, cp)
5721541Srgrimes	register struct mbuf *m;
5731541Srgrimes	register int off;
5741541Srgrimes	register int len;
5751541Srgrimes	caddr_t cp;
5761541Srgrimes{
5771541Srgrimes	register unsigned count;
5781541Srgrimes
57952201Salfred	KASSERT(off >= 0, ("m_copydata, negative off %d", off));
58052201Salfred	KASSERT(len >= 0, ("m_copydata, negative len %d", len));
5811541Srgrimes	while (off > 0) {
58252201Salfred		KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain"));
5831541Srgrimes		if (off < m->m_len)
5841541Srgrimes			break;
5851541Srgrimes		off -= m->m_len;
5861541Srgrimes		m = m->m_next;
5871541Srgrimes	}
5881541Srgrimes	while (len > 0) {
58952201Salfred		KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain"));
5901541Srgrimes		count = min(m->m_len - off, len);
5911541Srgrimes		bcopy(mtod(m, caddr_t) + off, cp, count);
5921541Srgrimes		len -= count;
5931541Srgrimes		cp += count;
5941541Srgrimes		off = 0;
5951541Srgrimes		m = m->m_next;
5961541Srgrimes	}
5971541Srgrimes}
5981541Srgrimes
5991541Srgrimes/*
60054002Sarchie * Copy a packet header mbuf chain into a completely new chain, including
60154002Sarchie * copying any mbuf clusters.  Use this instead of m_copypacket() when
60254002Sarchie * you need a writable copy of an mbuf chain.
60354002Sarchie */
60454002Sarchiestruct mbuf *
60554002Sarchiem_dup(m, how)
60654002Sarchie	struct mbuf *m;
60754002Sarchie	int how;
60854002Sarchie{
60954002Sarchie	struct mbuf **p, *top = NULL;
61054002Sarchie	int remain, moff, nsize;
61154002Sarchie
61254002Sarchie	/* Sanity check */
61354002Sarchie	if (m == NULL)
61454002Sarchie		return (0);
61554002Sarchie	KASSERT((m->m_flags & M_PKTHDR) != 0, ("%s: !PKTHDR", __FUNCTION__));
61654002Sarchie
61754002Sarchie	/* While there's more data, get a new mbuf, tack it on, and fill it */
61854002Sarchie	remain = m->m_pkthdr.len;
61954002Sarchie	moff = 0;
62054002Sarchie	p = &top;
62154002Sarchie	while (remain > 0 || top == NULL) {	/* allow m->m_pkthdr.len == 0 */
62254002Sarchie		struct mbuf *n;
62354002Sarchie
62454002Sarchie		/* Get the next new mbuf */
62554002Sarchie		MGET(n, how, m->m_type);
62654002Sarchie		if (n == NULL)
62754002Sarchie			goto nospace;
62854002Sarchie		if (top == NULL) {		/* first one, must be PKTHDR */
62954002Sarchie			M_COPY_PKTHDR(n, m);
63054002Sarchie			nsize = MHLEN;
63154002Sarchie		} else				/* not the first one */
63254002Sarchie			nsize = MLEN;
63354002Sarchie		if (remain >= MINCLSIZE) {
63454002Sarchie			MCLGET(n, how);
63554002Sarchie			if ((n->m_flags & M_EXT) == 0) {
63654002Sarchie				(void)m_free(n);
63754002Sarchie				goto nospace;
63854002Sarchie			}
63954002Sarchie			nsize = MCLBYTES;
64054002Sarchie		}
64154002Sarchie		n->m_len = 0;
64254002Sarchie
64354002Sarchie		/* Link it into the new chain */
64454002Sarchie		*p = n;
64554002Sarchie		p = &n->m_next;
64654002Sarchie
64754002Sarchie		/* Copy data from original mbuf(s) into new mbuf */
64854002Sarchie		while (n->m_len < nsize && m != NULL) {
64954002Sarchie			int chunk = min(nsize - n->m_len, m->m_len - moff);
65054002Sarchie
65154002Sarchie			bcopy(m->m_data + moff, n->m_data + n->m_len, chunk);
65254002Sarchie			moff += chunk;
65354002Sarchie			n->m_len += chunk;
65454002Sarchie			remain -= chunk;
65554002Sarchie			if (moff == m->m_len) {
65654002Sarchie				m = m->m_next;
65754002Sarchie				moff = 0;
65854002Sarchie			}
65954002Sarchie		}
66054002Sarchie
66154002Sarchie		/* Check correct total mbuf length */
66254002Sarchie		KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL),
66354002Sarchie		    	("%s: bogus m_pkthdr.len", __FUNCTION__));
66454002Sarchie	}
66554002Sarchie	return (top);
66654002Sarchie
66754002Sarchienospace:
66854002Sarchie	m_freem(top);
66954002Sarchie	MCFail++;
67054002Sarchie	return (0);
67154002Sarchie}
67254002Sarchie
67354002Sarchie/*
6741541Srgrimes * Concatenate mbuf chain n to m.
6751541Srgrimes * Both chains must be of the same type (e.g. MT_DATA).
6761541Srgrimes * Any m_pkthdr is not updated.
6771541Srgrimes */
6781549Srgrimesvoid
6791541Srgrimesm_cat(m, n)
6801541Srgrimes	register struct mbuf *m, *n;
6811541Srgrimes{
6821541Srgrimes	while (m->m_next)
6831541Srgrimes		m = m->m_next;
6841541Srgrimes	while (n) {
6851541Srgrimes		if (m->m_flags & M_EXT ||
6861541Srgrimes		    m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
6871541Srgrimes			/* just join the two chains */
6881541Srgrimes			m->m_next = n;
6891541Srgrimes			return;
6901541Srgrimes		}
6911541Srgrimes		/* splat the data from one into the other */
6921541Srgrimes		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
6931541Srgrimes		    (u_int)n->m_len);
6941541Srgrimes		m->m_len += n->m_len;
6951541Srgrimes		n = m_free(n);
6961541Srgrimes	}
6971541Srgrimes}
6981541Srgrimes
6991549Srgrimesvoid
7001541Srgrimesm_adj(mp, req_len)
7011541Srgrimes	struct mbuf *mp;
7021541Srgrimes	int req_len;
7031541Srgrimes{
7041541Srgrimes	register int len = req_len;
7051541Srgrimes	register struct mbuf *m;
70633678Sbde	register int count;
7071541Srgrimes
7081541Srgrimes	if ((m = mp) == NULL)
7091541Srgrimes		return;
7101541Srgrimes	if (len >= 0) {
7111541Srgrimes		/*
7121541Srgrimes		 * Trim from head.
7131541Srgrimes		 */
7141541Srgrimes		while (m != NULL && len > 0) {
7151541Srgrimes			if (m->m_len <= len) {
7161541Srgrimes				len -= m->m_len;
7171541Srgrimes				m->m_len = 0;
7181541Srgrimes				m = m->m_next;
7191541Srgrimes			} else {
7201541Srgrimes				m->m_len -= len;
7211541Srgrimes				m->m_data += len;
7221541Srgrimes				len = 0;
7231541Srgrimes			}
7241541Srgrimes		}
7251541Srgrimes		m = mp;
7261541Srgrimes		if (mp->m_flags & M_PKTHDR)
7271541Srgrimes			m->m_pkthdr.len -= (req_len - len);
7281541Srgrimes	} else {
7291541Srgrimes		/*
7301541Srgrimes		 * Trim from tail.  Scan the mbuf chain,
7311541Srgrimes		 * calculating its length and finding the last mbuf.
7321541Srgrimes		 * If the adjustment only affects this mbuf, then just
7331541Srgrimes		 * adjust and return.  Otherwise, rescan and truncate
7341541Srgrimes		 * after the remaining size.
7351541Srgrimes		 */
7361541Srgrimes		len = -len;
7371541Srgrimes		count = 0;
7381541Srgrimes		for (;;) {
7391541Srgrimes			count += m->m_len;
7401541Srgrimes			if (m->m_next == (struct mbuf *)0)
7411541Srgrimes				break;
7421541Srgrimes			m = m->m_next;
7431541Srgrimes		}
7441541Srgrimes		if (m->m_len >= len) {
7451541Srgrimes			m->m_len -= len;
7461541Srgrimes			if (mp->m_flags & M_PKTHDR)
7471541Srgrimes				mp->m_pkthdr.len -= len;
7481541Srgrimes			return;
7491541Srgrimes		}
7501541Srgrimes		count -= len;
7511541Srgrimes		if (count < 0)
7521541Srgrimes			count = 0;
7531541Srgrimes		/*
7541541Srgrimes		 * Correct length for chain is "count".
7551541Srgrimes		 * Find the mbuf with last data, adjust its length,
7561541Srgrimes		 * and toss data from remaining mbufs on chain.
7571541Srgrimes		 */
7581541Srgrimes		m = mp;
7591541Srgrimes		if (m->m_flags & M_PKTHDR)
7601541Srgrimes			m->m_pkthdr.len = count;
7611541Srgrimes		for (; m; m = m->m_next) {
7621541Srgrimes			if (m->m_len >= count) {
7631541Srgrimes				m->m_len = count;
7641541Srgrimes				break;
7651541Srgrimes			}
7661541Srgrimes			count -= m->m_len;
7671541Srgrimes		}
7683308Sphk		while (m->m_next)
7693308Sphk			(m = m->m_next) ->m_len = 0;
7701541Srgrimes	}
7711541Srgrimes}
7721541Srgrimes
7731541Srgrimes/*
7741541Srgrimes * Rearange an mbuf chain so that len bytes are contiguous
7751541Srgrimes * and in the data area of an mbuf (so that mtod and dtom
7761541Srgrimes * will work for a structure of size len).  Returns the resulting
7771541Srgrimes * mbuf chain on success, frees it and returns null on failure.
7781541Srgrimes * If there is room, it will add up to max_protohdr-len extra bytes to the
7791541Srgrimes * contiguous region in an attempt to avoid being called next time.
7801541Srgrimes */
78123081Swollman#define MPFail (mbstat.m_mpfail)
7821541Srgrimes
7831541Srgrimesstruct mbuf *
7841541Srgrimesm_pullup(n, len)
7851541Srgrimes	register struct mbuf *n;
7861541Srgrimes	int len;
7871541Srgrimes{
7881541Srgrimes	register struct mbuf *m;
7891541Srgrimes	register int count;
7901541Srgrimes	int space;
7911541Srgrimes
7921541Srgrimes	/*
7931541Srgrimes	 * If first mbuf has no cluster, and has room for len bytes
7941541Srgrimes	 * without shifting current data, pullup into it,
7951541Srgrimes	 * otherwise allocate a new mbuf to prepend to the chain.
7961541Srgrimes	 */
7971541Srgrimes	if ((n->m_flags & M_EXT) == 0 &&
7981541Srgrimes	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
7991541Srgrimes		if (n->m_len >= len)
8001541Srgrimes			return (n);
8011541Srgrimes		m = n;
8021541Srgrimes		n = n->m_next;
8031541Srgrimes		len -= m->m_len;
8041541Srgrimes	} else {
8051541Srgrimes		if (len > MHLEN)
8061541Srgrimes			goto bad;
8071541Srgrimes		MGET(m, M_DONTWAIT, n->m_type);
8081541Srgrimes		if (m == 0)
8091541Srgrimes			goto bad;
8101541Srgrimes		m->m_len = 0;
8111541Srgrimes		if (n->m_flags & M_PKTHDR) {
8121541Srgrimes			M_COPY_PKTHDR(m, n);
8131541Srgrimes			n->m_flags &= ~M_PKTHDR;
8141541Srgrimes		}
8151541Srgrimes	}
8161541Srgrimes	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
8171541Srgrimes	do {
8181541Srgrimes		count = min(min(max(len, max_protohdr), space), n->m_len);
8191541Srgrimes		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
8201541Srgrimes		  (unsigned)count);
8211541Srgrimes		len -= count;
8221541Srgrimes		m->m_len += count;
8231541Srgrimes		n->m_len -= count;
8241541Srgrimes		space -= count;
8251541Srgrimes		if (n->m_len)
8261541Srgrimes			n->m_data += count;
8271541Srgrimes		else
8281541Srgrimes			n = m_free(n);
8291541Srgrimes	} while (len > 0 && n);
8301541Srgrimes	if (len > 0) {
8311541Srgrimes		(void) m_free(m);
8321541Srgrimes		goto bad;
8331541Srgrimes	}
8341541Srgrimes	m->m_next = n;
8351541Srgrimes	return (m);
8361541Srgrimesbad:
8371541Srgrimes	m_freem(n);
8381541Srgrimes	MPFail++;
8391541Srgrimes	return (0);
8401541Srgrimes}
8411541Srgrimes
8421541Srgrimes/*
8431541Srgrimes * Partition an mbuf chain in two pieces, returning the tail --
8441541Srgrimes * all but the first len0 bytes.  In case of failure, it returns NULL and
8451541Srgrimes * attempts to restore the chain to its original state.
8461541Srgrimes */
8471541Srgrimesstruct mbuf *
8481541Srgrimesm_split(m0, len0, wait)
8491541Srgrimes	register struct mbuf *m0;
8501541Srgrimes	int len0, wait;
8511541Srgrimes{
8521541Srgrimes	register struct mbuf *m, *n;
8531541Srgrimes	unsigned len = len0, remain;
8541541Srgrimes
8551541Srgrimes	for (m = m0; m && len > m->m_len; m = m->m_next)
8561541Srgrimes		len -= m->m_len;
8571541Srgrimes	if (m == 0)
8581541Srgrimes		return (0);
8591541Srgrimes	remain = m->m_len - len;
8601541Srgrimes	if (m0->m_flags & M_PKTHDR) {
8611541Srgrimes		MGETHDR(n, wait, m0->m_type);
8621541Srgrimes		if (n == 0)
8631541Srgrimes			return (0);
8641541Srgrimes		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
8651541Srgrimes		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
8661541Srgrimes		m0->m_pkthdr.len = len0;
8671541Srgrimes		if (m->m_flags & M_EXT)
8681541Srgrimes			goto extpacket;
8691541Srgrimes		if (remain > MHLEN) {
8701541Srgrimes			/* m can't be the lead packet */
8711541Srgrimes			MH_ALIGN(n, 0);
8721541Srgrimes			n->m_next = m_split(m, len, wait);
8731541Srgrimes			if (n->m_next == 0) {
8741541Srgrimes				(void) m_free(n);
8751541Srgrimes				return (0);
8761541Srgrimes			} else
8771541Srgrimes				return (n);
8781541Srgrimes		} else
8791541Srgrimes			MH_ALIGN(n, remain);
8801541Srgrimes	} else if (remain == 0) {
8811541Srgrimes		n = m->m_next;
8821541Srgrimes		m->m_next = 0;
8831541Srgrimes		return (n);
8841541Srgrimes	} else {
8851541Srgrimes		MGET(n, wait, m->m_type);
8861541Srgrimes		if (n == 0)
8871541Srgrimes			return (0);
8881541Srgrimes		M_ALIGN(n, remain);
8891541Srgrimes	}
8901541Srgrimesextpacket:
8911541Srgrimes	if (m->m_flags & M_EXT) {
8921541Srgrimes		n->m_flags |= M_EXT;
8931541Srgrimes		n->m_ext = m->m_ext;
89417663Sjulian		if(!m->m_ext.ext_ref)
89517663Sjulian			mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
89617663Sjulian		else
89717663Sjulian			(*(m->m_ext.ext_ref))(m->m_ext.ext_buf,
89817663Sjulian						m->m_ext.ext_size);
8991541Srgrimes		m->m_ext.ext_size = 0; /* For Accounting XXXXXX danger */
9001541Srgrimes		n->m_data = m->m_data + len;
9011541Srgrimes	} else {
9021541Srgrimes		bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
9031541Srgrimes	}
9041541Srgrimes	n->m_len = remain;
9051541Srgrimes	m->m_len = len;
9061541Srgrimes	n->m_next = m->m_next;
9071541Srgrimes	m->m_next = 0;
9081541Srgrimes	return (n);
9091541Srgrimes}
9101541Srgrimes/*
9111541Srgrimes * Routine to copy from device local memory into mbufs.
9121541Srgrimes */
9131541Srgrimesstruct mbuf *
9141541Srgrimesm_devget(buf, totlen, off0, ifp, copy)
9151541Srgrimes	char *buf;
9161541Srgrimes	int totlen, off0;
9171541Srgrimes	struct ifnet *ifp;
91812577Sbde	void (*copy) __P((char *from, caddr_t to, u_int len));
9191541Srgrimes{
9201541Srgrimes	register struct mbuf *m;
9211541Srgrimes	struct mbuf *top = 0, **mp = &top;
9221541Srgrimes	register int off = off0, len;
9231541Srgrimes	register char *cp;
9241541Srgrimes	char *epkt;
9251541Srgrimes
9261541Srgrimes	cp = buf;
9271541Srgrimes	epkt = cp + totlen;
9281541Srgrimes	if (off) {
9291541Srgrimes		cp += off + 2 * sizeof(u_short);
9301541Srgrimes		totlen -= 2 * sizeof(u_short);
9311541Srgrimes	}
9321541Srgrimes	MGETHDR(m, M_DONTWAIT, MT_DATA);
9331541Srgrimes	if (m == 0)
9341541Srgrimes		return (0);
9351541Srgrimes	m->m_pkthdr.rcvif = ifp;
9361541Srgrimes	m->m_pkthdr.len = totlen;
9371541Srgrimes	m->m_len = MHLEN;
9381541Srgrimes
9391541Srgrimes	while (totlen > 0) {
9401541Srgrimes		if (top) {
9411541Srgrimes			MGET(m, M_DONTWAIT, MT_DATA);
9421541Srgrimes			if (m == 0) {
9431541Srgrimes				m_freem(top);
9441541Srgrimes				return (0);
9451541Srgrimes			}
9461541Srgrimes			m->m_len = MLEN;
9471541Srgrimes		}
9481541Srgrimes		len = min(totlen, epkt - cp);
9491541Srgrimes		if (len >= MINCLSIZE) {
9501541Srgrimes			MCLGET(m, M_DONTWAIT);
9511541Srgrimes			if (m->m_flags & M_EXT)
9521541Srgrimes				m->m_len = len = min(len, MCLBYTES);
9531541Srgrimes			else
9541541Srgrimes				len = m->m_len;
9551541Srgrimes		} else {
9561541Srgrimes			/*
9571541Srgrimes			 * Place initial small packet/header at end of mbuf.
9581541Srgrimes			 */
9591541Srgrimes			if (len < m->m_len) {
9601541Srgrimes				if (top == 0 && len + max_linkhdr <= m->m_len)
9611541Srgrimes					m->m_data += max_linkhdr;
9621541Srgrimes				m->m_len = len;
9631541Srgrimes			} else
9641541Srgrimes				len = m->m_len;
9651541Srgrimes		}
9661541Srgrimes		if (copy)
9671541Srgrimes			copy(cp, mtod(m, caddr_t), (unsigned)len);
9681541Srgrimes		else
9691541Srgrimes			bcopy(cp, mtod(m, caddr_t), (unsigned)len);
9701541Srgrimes		cp += len;
9711541Srgrimes		*mp = m;
9721541Srgrimes		mp = &m->m_next;
9731541Srgrimes		totlen -= len;
9741541Srgrimes		if (cp == epkt)
9751541Srgrimes			cp = buf;
9761541Srgrimes	}
9771541Srgrimes	return (top);
9781541Srgrimes}
9793352Sphk
9803352Sphk/*
9813352Sphk * Copy data from a buffer back into the indicated mbuf chain,
9823352Sphk * starting "off" bytes from the beginning, extending the mbuf
9833352Sphk * chain if necessary.
9843352Sphk */
9853352Sphkvoid
9863352Sphkm_copyback(m0, off, len, cp)
9873352Sphk	struct	mbuf *m0;
9883352Sphk	register int off;
9893352Sphk	register int len;
9903352Sphk	caddr_t cp;
9913352Sphk{
9923352Sphk	register int mlen;
9933352Sphk	register struct mbuf *m = m0, *n;
9943352Sphk	int totlen = 0;
9953352Sphk
9963352Sphk	if (m0 == 0)
9973352Sphk		return;
9983352Sphk	while (off > (mlen = m->m_len)) {
9993352Sphk		off -= mlen;
10003352Sphk		totlen += mlen;
10013352Sphk		if (m->m_next == 0) {
10023352Sphk			n = m_getclr(M_DONTWAIT, m->m_type);
10033352Sphk			if (n == 0)
10043352Sphk				goto out;
10053352Sphk			n->m_len = min(MLEN, len + off);
10063352Sphk			m->m_next = n;
10073352Sphk		}
10083352Sphk		m = m->m_next;
10093352Sphk	}
10103352Sphk	while (len > 0) {
10113352Sphk		mlen = min (m->m_len - off, len);
10123352Sphk		bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen);
10133352Sphk		cp += mlen;
10143352Sphk		len -= mlen;
10153352Sphk		mlen += off;
10163352Sphk		off = 0;
10173352Sphk		totlen += mlen;
10183352Sphk		if (len == 0)
10193352Sphk			break;
10203352Sphk		if (m->m_next == 0) {
10213352Sphk			n = m_get(M_DONTWAIT, m->m_type);
10223352Sphk			if (n == 0)
10233352Sphk				break;
10243352Sphk			n->m_len = min(MLEN, len);
10253352Sphk			m->m_next = n;
10263352Sphk		}
10273352Sphk		m = m->m_next;
10283352Sphk	}
10293352Sphkout:	if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
10303352Sphk		m->m_pkthdr.len = totlen;
10313352Sphk}
103252756Sphk
103352756Sphkvoid
103452756Sphkm_print(const struct mbuf *m)
103552756Sphk{
103652756Sphk	int len;
103753332Speter	const struct mbuf *m2;
103852756Sphk
103952756Sphk	len = m->m_pkthdr.len;
104052756Sphk	m2 = m;
104152756Sphk	while (len) {
104252756Sphk		printf("%p %*D\n", m2, m2->m_len, (u_char *)m2->m_data, "-");
104352756Sphk		len -= m2->m_len;
104452756Sphk		m2 = m2->m_next;
104552756Sphk	}
104652756Sphk	return;
104752756Sphk}
1048