uipc_mbuf.c revision 45615
1/*
2 * Copyright (c) 1982, 1986, 1988, 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *	@(#)uipc_mbuf.c	8.2 (Berkeley) 1/4/94
34 *	$Id: uipc_mbuf.c,v 1.38 1999/02/16 10:49:49 dfr Exp $
35 */
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/malloc.h>
40#include <sys/mbuf.h>
41#include <sys/kernel.h>
42#include <sys/sysctl.h>
43#include <sys/domain.h>
44#include <sys/protosw.h>
45
46#include <vm/vm.h>
47#include <vm/vm_kern.h>
48#include <vm/vm_extern.h>
49
50static void mbinit __P((void *));
51SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbinit, NULL)
52
53struct mbuf *mbutl;
54char	*mclrefcnt;
55struct mbstat mbstat;
56struct mbuf *mmbfree;
57union mcluster *mclfree;
58int	max_linkhdr;
59int	max_protohdr;
60int	max_hdr;
61int	max_datalen;
62
63SYSCTL_DECL(_kern_ipc);
64SYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RW,
65	   &max_linkhdr, 0, "");
66SYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RW,
67	   &max_protohdr, 0, "");
68SYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RW, &max_hdr, 0, "");
69SYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RW,
70	   &max_datalen, 0, "");
71SYSCTL_STRUCT(_kern_ipc, KIPC_MBSTAT, mbstat, CTLFLAG_RW, &mbstat, mbstat, "");
72
73static void	m_reclaim __P((void));
74
75/* "number of clusters of pages" */
76#define NCL_INIT	1
77
78#define NMB_INIT	16
79
80/* ARGSUSED*/
81static void
82mbinit(dummy)
83	void *dummy;
84{
85	int s;
86
87	mmbfree = NULL; mclfree = NULL;
88	mbstat.m_msize = MSIZE;
89	mbstat.m_mclbytes = MCLBYTES;
90	mbstat.m_minclsize = MINCLSIZE;
91	mbstat.m_mlen = MLEN;
92	mbstat.m_mhlen = MHLEN;
93
94	s = splimp();
95	if (m_mballoc(NMB_INIT, M_DONTWAIT) == 0)
96		goto bad;
97#if MCLBYTES <= PAGE_SIZE
98	if (m_clalloc(NCL_INIT, M_DONTWAIT) == 0)
99		goto bad;
100#else
101	/* It's OK to call contigmalloc in this context. */
102	if (m_clalloc(16, M_WAIT) == 0)
103		goto bad;
104#endif
105	splx(s);
106	return;
107bad:
108	panic("mbinit");
109}
110
111/*
112 * Allocate at least nmb mbufs and place on mbuf free list.
113 * Must be called at splimp.
114 */
115/* ARGSUSED */
116int
117m_mballoc(nmb, how)
118	register int nmb;
119	int how;
120{
121	register caddr_t p;
122	register int i;
123	int nbytes;
124
125	/* Once we run out of map space, it will be impossible to get
126	 * any more (nothing is ever freed back to the map) (XXX which
127	 * is dumb). (however you are not dead as m_reclaim might
128	 * still be able to free a substantial amount of space).
129	 */
130	if (mb_map_full)
131		return (0);
132
133	nbytes = round_page(nmb * MSIZE);
134	p = (caddr_t)kmem_malloc(mb_map, nbytes, M_NOWAIT);
135	if (p == 0 && how == M_WAIT) {
136		mbstat.m_wait++;
137		p = (caddr_t)kmem_malloc(mb_map, nbytes, M_WAITOK);
138	}
139
140	/*
141	 * Either the map is now full, or `how' is M_NOWAIT and there
142	 * are no pages left.
143	 */
144	if (p == NULL)
145		return (0);
146
147	nmb = nbytes / MSIZE;
148	for (i = 0; i < nmb; i++) {
149		((struct mbuf *)p)->m_next = mmbfree;
150		mmbfree = (struct mbuf *)p;
151		p += MSIZE;
152	}
153	mbstat.m_mbufs += nmb;
154	return (1);
155}
156
157#if MCLBYTES > PAGE_SIZE
158static int i_want_my_mcl;
159
160static void
161kproc_mclalloc(void)
162{
163	int status;
164
165	while (1) {
166		tsleep(&i_want_my_mcl, PVM, "mclalloc", 0);
167
168		for (; i_want_my_mcl; i_want_my_mcl--) {
169			if (m_clalloc(1, M_WAIT) == 0)
170				printf("m_clalloc failed even in process context!\n");
171		}
172	}
173}
174
175static struct proc *mclallocproc;
176static struct kproc_desc mclalloc_kp = {
177	"mclalloc",
178	kproc_mclalloc,
179	&mclallocproc
180};
181SYSINIT_KT(mclallocproc, SI_SUB_KTHREAD_UPDATE, SI_ORDER_ANY, kproc_start,
182	   &mclalloc_kp);
183#endif
184
185/*
186 * Allocate some number of mbuf clusters
187 * and place on cluster free list.
188 * Must be called at splimp.
189 */
190/* ARGSUSED */
191int
192m_clalloc(ncl, how)
193	register int ncl;
194	int how;
195{
196	register caddr_t p;
197	register int i;
198	int npg;
199
200	/*
201	 * Once we run out of map space, it will be impossible
202	 * to get any more (nothing is ever freed back to the
203	 * map).
204	 */
205	if (mb_map_full) {
206		mbstat.m_drops++;
207		return (0);
208	}
209
210#if MCLBYTES > PAGE_SIZE
211	if (how != M_WAIT) {
212		i_want_my_mcl += ncl;
213		wakeup(&i_want_my_mcl);
214		mbstat.m_wait++;
215		p = 0;
216	} else {
217		p = contigmalloc1(MCLBYTES * ncl, M_DEVBUF, M_WAITOK, 0ul,
218				  ~0ul, PAGE_SIZE, 0, mb_map);
219	}
220#else
221	npg = ncl;
222	p = (caddr_t)kmem_malloc(mb_map, ctob(npg),
223				 how != M_WAIT ? M_NOWAIT : M_WAITOK);
224	ncl = ncl * PAGE_SIZE / MCLBYTES;
225#endif
226	/*
227	 * Either the map is now full, or `how' is M_NOWAIT and there
228	 * are no pages left.
229	 */
230	if (p == NULL) {
231		mbstat.m_drops++;
232		return (0);
233	}
234
235	for (i = 0; i < ncl; i++) {
236		((union mcluster *)p)->mcl_next = mclfree;
237		mclfree = (union mcluster *)p;
238		p += MCLBYTES;
239		mbstat.m_clfree++;
240	}
241	mbstat.m_clusters += ncl;
242	return (1);
243}
244
245/*
246 * When MGET fails, ask protocols to free space when short of memory,
247 * then re-attempt to allocate an mbuf.
248 */
249struct mbuf *
250m_retry(i, t)
251	int i, t;
252{
253	register struct mbuf *m;
254
255	/*
256	 * Must only do the reclaim if not in an interrupt context.
257	 */
258	if (i == M_WAIT)
259		m_reclaim();
260#define m_retry(i, t)	(struct mbuf *)0
261	MGET(m, i, t);
262#undef m_retry
263	if (m != NULL) {
264		mbstat.m_wait++;
265	} else {
266		if (i == M_DONTWAIT)
267			mbstat.m_drops++;
268		else
269			panic("Out of mbuf clusters");
270	}
271	return (m);
272}
273
274/*
275 * As above; retry an MGETHDR.
276 */
277struct mbuf *
278m_retryhdr(i, t)
279	int i, t;
280{
281	register struct mbuf *m;
282
283	/*
284	 * Must only do the reclaim if not in an interrupt context.
285	 */
286	if (i == M_WAIT)
287		m_reclaim();
288#define m_retryhdr(i, t) (struct mbuf *)0
289	MGETHDR(m, i, t);
290#undef m_retryhdr
291	if (m != NULL) {
292		mbstat.m_wait++;
293	} else {
294		if (i == M_DONTWAIT)
295			mbstat.m_drops++;
296		else
297			panic("Out of mbuf clusters");
298	}
299	return (m);
300}
301
302static void
303m_reclaim()
304{
305	register struct domain *dp;
306	register struct protosw *pr;
307	int s = splimp();
308
309	for (dp = domains; dp; dp = dp->dom_next)
310		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
311			if (pr->pr_drain)
312				(*pr->pr_drain)();
313	splx(s);
314	mbstat.m_drain++;
315}
316
317/*
318 * Space allocation routines.
319 * These are also available as macros
320 * for critical paths.
321 */
322struct mbuf *
323m_get(how, type)
324	int how, type;
325{
326	register struct mbuf *m;
327
328	MGET(m, how, type);
329	return (m);
330}
331
332struct mbuf *
333m_gethdr(how, type)
334	int how, type;
335{
336	register struct mbuf *m;
337
338	MGETHDR(m, how, type);
339	return (m);
340}
341
342struct mbuf *
343m_getclr(how, type)
344	int how, type;
345{
346	register struct mbuf *m;
347
348	MGET(m, how, type);
349	if (m == 0)
350		return (0);
351	bzero(mtod(m, caddr_t), MLEN);
352	return (m);
353}
354
355struct mbuf *
356m_free(m)
357	struct mbuf *m;
358{
359	register struct mbuf *n;
360
361	MFREE(m, n);
362	return (n);
363}
364
365void
366m_freem(m)
367	register struct mbuf *m;
368{
369	register struct mbuf *n;
370
371	if (m == NULL)
372		return;
373	do {
374		MFREE(m, n);
375		m = n;
376	} while (m);
377}
378
379/*
380 * Mbuffer utility routines.
381 */
382
383/*
384 * Lesser-used path for M_PREPEND:
385 * allocate new mbuf to prepend to chain,
386 * copy junk along.
387 */
388struct mbuf *
389m_prepend(m, len, how)
390	register struct mbuf *m;
391	int len, how;
392{
393	struct mbuf *mn;
394
395	MGET(mn, how, m->m_type);
396	if (mn == (struct mbuf *)NULL) {
397		m_freem(m);
398		return ((struct mbuf *)NULL);
399	}
400	if (m->m_flags & M_PKTHDR) {
401		M_COPY_PKTHDR(mn, m);
402		m->m_flags &= ~M_PKTHDR;
403	}
404	mn->m_next = m;
405	m = mn;
406	if (len < MHLEN)
407		MH_ALIGN(m, len);
408	m->m_len = len;
409	return (m);
410}
411
412/*
413 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
414 * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
415 * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
416 */
417#define MCFail (mbstat.m_mcfail)
418
419struct mbuf *
420m_copym(m, off0, len, wait)
421	register struct mbuf *m;
422	int off0, wait;
423	register int len;
424{
425	register struct mbuf *n, **np;
426	register int off = off0;
427	struct mbuf *top;
428	int copyhdr = 0;
429
430	if (off < 0 || len < 0)
431		panic("m_copym");
432	if (off == 0 && m->m_flags & M_PKTHDR)
433		copyhdr = 1;
434	while (off > 0) {
435		if (m == 0)
436			panic("m_copym");
437		if (off < m->m_len)
438			break;
439		off -= m->m_len;
440		m = m->m_next;
441	}
442	np = &top;
443	top = 0;
444	while (len > 0) {
445		if (m == 0) {
446			if (len != M_COPYALL)
447				panic("m_copym");
448			break;
449		}
450		MGET(n, wait, m->m_type);
451		*np = n;
452		if (n == 0)
453			goto nospace;
454		if (copyhdr) {
455			M_COPY_PKTHDR(n, m);
456			if (len == M_COPYALL)
457				n->m_pkthdr.len -= off0;
458			else
459				n->m_pkthdr.len = len;
460			copyhdr = 0;
461		}
462		n->m_len = min(len, m->m_len - off);
463		if (m->m_flags & M_EXT) {
464			n->m_data = m->m_data + off;
465			if(!m->m_ext.ext_ref)
466				mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
467			else
468				(*(m->m_ext.ext_ref))(m->m_ext.ext_buf,
469							m->m_ext.ext_size);
470			n->m_ext = m->m_ext;
471			n->m_flags |= M_EXT;
472		} else
473			bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
474			    (unsigned)n->m_len);
475		if (len != M_COPYALL)
476			len -= n->m_len;
477		off = 0;
478		m = m->m_next;
479		np = &n->m_next;
480	}
481	if (top == 0)
482		MCFail++;
483	return (top);
484nospace:
485	m_freem(top);
486	MCFail++;
487	return (0);
488}
489
490/*
491 * Copy an entire packet, including header (which must be present).
492 * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
493 */
494struct mbuf *
495m_copypacket(m, how)
496	struct mbuf *m;
497	int how;
498{
499	struct mbuf *top, *n, *o;
500
501	MGET(n, how, m->m_type);
502	top = n;
503	if (!n)
504		goto nospace;
505
506	M_COPY_PKTHDR(n, m);
507	n->m_len = m->m_len;
508	if (m->m_flags & M_EXT) {
509		n->m_data = m->m_data;
510		if(!m->m_ext.ext_ref)
511			mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
512		else
513			(*(m->m_ext.ext_ref))(m->m_ext.ext_buf,
514						m->m_ext.ext_size);
515		n->m_ext = m->m_ext;
516		n->m_flags |= M_EXT;
517	} else {
518		bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
519	}
520
521	m = m->m_next;
522	while (m) {
523		MGET(o, how, m->m_type);
524		if (!o)
525			goto nospace;
526
527		n->m_next = o;
528		n = n->m_next;
529
530		n->m_len = m->m_len;
531		if (m->m_flags & M_EXT) {
532			n->m_data = m->m_data;
533			if(!m->m_ext.ext_ref)
534				mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
535			else
536				(*(m->m_ext.ext_ref))(m->m_ext.ext_buf,
537							m->m_ext.ext_size);
538			n->m_ext = m->m_ext;
539			n->m_flags |= M_EXT;
540		} else {
541			bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
542		}
543
544		m = m->m_next;
545	}
546	return top;
547nospace:
548	m_freem(top);
549	MCFail++;
550	return 0;
551}
552
553/*
554 * Copy data from an mbuf chain starting "off" bytes from the beginning,
555 * continuing for "len" bytes, into the indicated buffer.
556 */
557void
558m_copydata(m, off, len, cp)
559	register struct mbuf *m;
560	register int off;
561	register int len;
562	caddr_t cp;
563{
564	register unsigned count;
565
566	if (off < 0 || len < 0)
567		panic("m_copydata");
568	while (off > 0) {
569		if (m == 0)
570			panic("m_copydata");
571		if (off < m->m_len)
572			break;
573		off -= m->m_len;
574		m = m->m_next;
575	}
576	while (len > 0) {
577		if (m == 0)
578			panic("m_copydata");
579		count = min(m->m_len - off, len);
580		bcopy(mtod(m, caddr_t) + off, cp, count);
581		len -= count;
582		cp += count;
583		off = 0;
584		m = m->m_next;
585	}
586}
587
588/*
589 * Concatenate mbuf chain n to m.
590 * Both chains must be of the same type (e.g. MT_DATA).
591 * Any m_pkthdr is not updated.
592 */
593void
594m_cat(m, n)
595	register struct mbuf *m, *n;
596{
597	while (m->m_next)
598		m = m->m_next;
599	while (n) {
600		if (m->m_flags & M_EXT ||
601		    m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
602			/* just join the two chains */
603			m->m_next = n;
604			return;
605		}
606		/* splat the data from one into the other */
607		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
608		    (u_int)n->m_len);
609		m->m_len += n->m_len;
610		n = m_free(n);
611	}
612}
613
614void
615m_adj(mp, req_len)
616	struct mbuf *mp;
617	int req_len;
618{
619	register int len = req_len;
620	register struct mbuf *m;
621	register int count;
622
623	if ((m = mp) == NULL)
624		return;
625	if (len >= 0) {
626		/*
627		 * Trim from head.
628		 */
629		while (m != NULL && len > 0) {
630			if (m->m_len <= len) {
631				len -= m->m_len;
632				m->m_len = 0;
633				m = m->m_next;
634			} else {
635				m->m_len -= len;
636				m->m_data += len;
637				len = 0;
638			}
639		}
640		m = mp;
641		if (mp->m_flags & M_PKTHDR)
642			m->m_pkthdr.len -= (req_len - len);
643	} else {
644		/*
645		 * Trim from tail.  Scan the mbuf chain,
646		 * calculating its length and finding the last mbuf.
647		 * If the adjustment only affects this mbuf, then just
648		 * adjust and return.  Otherwise, rescan and truncate
649		 * after the remaining size.
650		 */
651		len = -len;
652		count = 0;
653		for (;;) {
654			count += m->m_len;
655			if (m->m_next == (struct mbuf *)0)
656				break;
657			m = m->m_next;
658		}
659		if (m->m_len >= len) {
660			m->m_len -= len;
661			if (mp->m_flags & M_PKTHDR)
662				mp->m_pkthdr.len -= len;
663			return;
664		}
665		count -= len;
666		if (count < 0)
667			count = 0;
668		/*
669		 * Correct length for chain is "count".
670		 * Find the mbuf with last data, adjust its length,
671		 * and toss data from remaining mbufs on chain.
672		 */
673		m = mp;
674		if (m->m_flags & M_PKTHDR)
675			m->m_pkthdr.len = count;
676		for (; m; m = m->m_next) {
677			if (m->m_len >= count) {
678				m->m_len = count;
679				break;
680			}
681			count -= m->m_len;
682		}
683		while (m->m_next)
684			(m = m->m_next) ->m_len = 0;
685	}
686}
687
688/*
689 * Rearange an mbuf chain so that len bytes are contiguous
690 * and in the data area of an mbuf (so that mtod and dtom
691 * will work for a structure of size len).  Returns the resulting
692 * mbuf chain on success, frees it and returns null on failure.
693 * If there is room, it will add up to max_protohdr-len extra bytes to the
694 * contiguous region in an attempt to avoid being called next time.
695 */
696#define MPFail (mbstat.m_mpfail)
697
698struct mbuf *
699m_pullup(n, len)
700	register struct mbuf *n;
701	int len;
702{
703	register struct mbuf *m;
704	register int count;
705	int space;
706
707	/*
708	 * If first mbuf has no cluster, and has room for len bytes
709	 * without shifting current data, pullup into it,
710	 * otherwise allocate a new mbuf to prepend to the chain.
711	 */
712	if ((n->m_flags & M_EXT) == 0 &&
713	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
714		if (n->m_len >= len)
715			return (n);
716		m = n;
717		n = n->m_next;
718		len -= m->m_len;
719	} else {
720		if (len > MHLEN)
721			goto bad;
722		MGET(m, M_DONTWAIT, n->m_type);
723		if (m == 0)
724			goto bad;
725		m->m_len = 0;
726		if (n->m_flags & M_PKTHDR) {
727			M_COPY_PKTHDR(m, n);
728			n->m_flags &= ~M_PKTHDR;
729		}
730	}
731	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
732	do {
733		count = min(min(max(len, max_protohdr), space), n->m_len);
734		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
735		  (unsigned)count);
736		len -= count;
737		m->m_len += count;
738		n->m_len -= count;
739		space -= count;
740		if (n->m_len)
741			n->m_data += count;
742		else
743			n = m_free(n);
744	} while (len > 0 && n);
745	if (len > 0) {
746		(void) m_free(m);
747		goto bad;
748	}
749	m->m_next = n;
750	return (m);
751bad:
752	m_freem(n);
753	MPFail++;
754	return (0);
755}
756
757/*
758 * Partition an mbuf chain in two pieces, returning the tail --
759 * all but the first len0 bytes.  In case of failure, it returns NULL and
760 * attempts to restore the chain to its original state.
761 */
762struct mbuf *
763m_split(m0, len0, wait)
764	register struct mbuf *m0;
765	int len0, wait;
766{
767	register struct mbuf *m, *n;
768	unsigned len = len0, remain;
769
770	for (m = m0; m && len > m->m_len; m = m->m_next)
771		len -= m->m_len;
772	if (m == 0)
773		return (0);
774	remain = m->m_len - len;
775	if (m0->m_flags & M_PKTHDR) {
776		MGETHDR(n, wait, m0->m_type);
777		if (n == 0)
778			return (0);
779		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
780		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
781		m0->m_pkthdr.len = len0;
782		if (m->m_flags & M_EXT)
783			goto extpacket;
784		if (remain > MHLEN) {
785			/* m can't be the lead packet */
786			MH_ALIGN(n, 0);
787			n->m_next = m_split(m, len, wait);
788			if (n->m_next == 0) {
789				(void) m_free(n);
790				return (0);
791			} else
792				return (n);
793		} else
794			MH_ALIGN(n, remain);
795	} else if (remain == 0) {
796		n = m->m_next;
797		m->m_next = 0;
798		return (n);
799	} else {
800		MGET(n, wait, m->m_type);
801		if (n == 0)
802			return (0);
803		M_ALIGN(n, remain);
804	}
805extpacket:
806	if (m->m_flags & M_EXT) {
807		n->m_flags |= M_EXT;
808		n->m_ext = m->m_ext;
809		if(!m->m_ext.ext_ref)
810			mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
811		else
812			(*(m->m_ext.ext_ref))(m->m_ext.ext_buf,
813						m->m_ext.ext_size);
814		m->m_ext.ext_size = 0; /* For Accounting XXXXXX danger */
815		n->m_data = m->m_data + len;
816	} else {
817		bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
818	}
819	n->m_len = remain;
820	m->m_len = len;
821	n->m_next = m->m_next;
822	m->m_next = 0;
823	return (n);
824}
825/*
826 * Routine to copy from device local memory into mbufs.
827 */
828struct mbuf *
829m_devget(buf, totlen, off0, ifp, copy)
830	char *buf;
831	int totlen, off0;
832	struct ifnet *ifp;
833	void (*copy) __P((char *from, caddr_t to, u_int len));
834{
835	register struct mbuf *m;
836	struct mbuf *top = 0, **mp = &top;
837	register int off = off0, len;
838	register char *cp;
839	char *epkt;
840
841	cp = buf;
842	epkt = cp + totlen;
843	if (off) {
844		cp += off + 2 * sizeof(u_short);
845		totlen -= 2 * sizeof(u_short);
846	}
847	MGETHDR(m, M_DONTWAIT, MT_DATA);
848	if (m == 0)
849		return (0);
850	m->m_pkthdr.rcvif = ifp;
851	m->m_pkthdr.len = totlen;
852	m->m_len = MHLEN;
853
854	while (totlen > 0) {
855		if (top) {
856			MGET(m, M_DONTWAIT, MT_DATA);
857			if (m == 0) {
858				m_freem(top);
859				return (0);
860			}
861			m->m_len = MLEN;
862		}
863		len = min(totlen, epkt - cp);
864		if (len >= MINCLSIZE) {
865			MCLGET(m, M_DONTWAIT);
866			if (m->m_flags & M_EXT)
867				m->m_len = len = min(len, MCLBYTES);
868			else
869				len = m->m_len;
870		} else {
871			/*
872			 * Place initial small packet/header at end of mbuf.
873			 */
874			if (len < m->m_len) {
875				if (top == 0 && len + max_linkhdr <= m->m_len)
876					m->m_data += max_linkhdr;
877				m->m_len = len;
878			} else
879				len = m->m_len;
880		}
881		if (copy)
882			copy(cp, mtod(m, caddr_t), (unsigned)len);
883		else
884			bcopy(cp, mtod(m, caddr_t), (unsigned)len);
885		cp += len;
886		*mp = m;
887		mp = &m->m_next;
888		totlen -= len;
889		if (cp == epkt)
890			cp = buf;
891	}
892	return (top);
893}
894
895/*
896 * Copy data from a buffer back into the indicated mbuf chain,
897 * starting "off" bytes from the beginning, extending the mbuf
898 * chain if necessary.
899 */
900void
901m_copyback(m0, off, len, cp)
902	struct	mbuf *m0;
903	register int off;
904	register int len;
905	caddr_t cp;
906{
907	register int mlen;
908	register struct mbuf *m = m0, *n;
909	int totlen = 0;
910
911	if (m0 == 0)
912		return;
913	while (off > (mlen = m->m_len)) {
914		off -= mlen;
915		totlen += mlen;
916		if (m->m_next == 0) {
917			n = m_getclr(M_DONTWAIT, m->m_type);
918			if (n == 0)
919				goto out;
920			n->m_len = min(MLEN, len + off);
921			m->m_next = n;
922		}
923		m = m->m_next;
924	}
925	while (len > 0) {
926		mlen = min (m->m_len - off, len);
927		bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen);
928		cp += mlen;
929		len -= mlen;
930		mlen += off;
931		off = 0;
932		totlen += mlen;
933		if (len == 0)
934			break;
935		if (m->m_next == 0) {
936			n = m_get(M_DONTWAIT, m->m_type);
937			if (n == 0)
938				break;
939			n->m_len = min(MLEN, len);
940			m->m_next = n;
941		}
942		m = m->m_next;
943	}
944out:	if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
945		m->m_pkthdr.len = totlen;
946}
947