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