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