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