xref: /freebsd/sys/kern/uipc_mbuf.c (revision c8191bc69e371d47c087ae124ff863de342a479b)
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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)uipc_mbuf.c	8.2 (Berkeley) 1/4/94
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_param.h"
36 #include "opt_mbuf_stress_test.h"
37 #include "opt_mbuf_profiling.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/limits.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/mbuf.h>
46 #include <sys/sysctl.h>
47 #include <sys/domain.h>
48 #include <sys/protosw.h>
49 #include <sys/uio.h>
50 
51 int	max_linkhdr;
52 int	max_protohdr;
53 int	max_hdr;
54 int	max_datalen;
55 #ifdef MBUF_STRESS_TEST
56 int	m_defragpackets;
57 int	m_defragbytes;
58 int	m_defraguseless;
59 int	m_defragfailure;
60 int	m_defragrandomfailures;
61 #endif
62 
63 /*
64  * sysctl(8) exported objects
65  */
66 SYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RD,
67 	   &max_linkhdr, 0, "Size of largest link layer header");
68 SYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RD,
69 	   &max_protohdr, 0, "Size of largest protocol layer header");
70 SYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RD,
71 	   &max_hdr, 0, "Size of largest link plus protocol header");
72 SYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RD,
73 	   &max_datalen, 0, "Minimum space left in mbuf after max_hdr");
74 #ifdef MBUF_STRESS_TEST
75 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragpackets, CTLFLAG_RD,
76 	   &m_defragpackets, 0, "");
77 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragbytes, CTLFLAG_RD,
78 	   &m_defragbytes, 0, "");
79 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defraguseless, CTLFLAG_RD,
80 	   &m_defraguseless, 0, "");
81 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragfailure, CTLFLAG_RD,
82 	   &m_defragfailure, 0, "");
83 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragrandomfailures, CTLFLAG_RW,
84 	   &m_defragrandomfailures, 0, "");
85 #endif
86 
87 /*
88  * Ensure the correct size of various mbuf parameters.  It could be off due
89  * to compiler-induced padding and alignment artifacts.
90  */
91 CTASSERT(sizeof(struct mbuf) == MSIZE);
92 CTASSERT(MSIZE - offsetof(struct mbuf, m_dat) == MLEN);
93 CTASSERT(MSIZE - offsetof(struct mbuf, m_pktdat) == MHLEN);
94 
95 /*
96  * m_get2() allocates minimum mbuf that would fit "size" argument.
97  */
98 struct mbuf *
99 m_get2(int size, int how, short type, int flags)
100 {
101 	struct mb_args args;
102 	struct mbuf *m, *n;
103 
104 	args.flags = flags;
105 	args.type = type;
106 
107 	if (size <= MHLEN || (size <= MLEN && (flags & M_PKTHDR) == 0))
108 		return (uma_zalloc_arg(zone_mbuf, &args, how));
109 	if (size <= MCLBYTES)
110 		return (uma_zalloc_arg(zone_pack, &args, how));
111 
112 	if (size > MJUMPAGESIZE)
113 		return (NULL);
114 
115 	m = uma_zalloc_arg(zone_mbuf, &args, how);
116 	if (m == NULL)
117 		return (NULL);
118 
119 	n = uma_zalloc_arg(zone_jumbop, m, how);
120 	if (n == NULL) {
121 		uma_zfree(zone_mbuf, m);
122 		return (NULL);
123 	}
124 
125 	return (m);
126 }
127 
128 /*
129  * m_getjcl() returns an mbuf with a cluster of the specified size attached.
130  * For size it takes MCLBYTES, MJUMPAGESIZE, MJUM9BYTES, MJUM16BYTES.
131  */
132 struct mbuf *
133 m_getjcl(int how, short type, int flags, int size)
134 {
135 	struct mb_args args;
136 	struct mbuf *m, *n;
137 	uma_zone_t zone;
138 
139 	if (size == MCLBYTES)
140 		return m_getcl(how, type, flags);
141 
142 	args.flags = flags;
143 	args.type = type;
144 
145 	m = uma_zalloc_arg(zone_mbuf, &args, how);
146 	if (m == NULL)
147 		return (NULL);
148 
149 	zone = m_getzone(size);
150 	n = uma_zalloc_arg(zone, m, how);
151 	if (n == NULL) {
152 		uma_zfree(zone_mbuf, m);
153 		return (NULL);
154 	}
155 	return (m);
156 }
157 
158 /*
159  * Allocate a given length worth of mbufs and/or clusters (whatever fits
160  * best) and return a pointer to the top of the allocated chain.  If an
161  * existing mbuf chain is provided, then we will append the new chain
162  * to the existing one but still return the top of the newly allocated
163  * chain.
164  */
165 struct mbuf *
166 m_getm2(struct mbuf *m, int len, int how, short type, int flags)
167 {
168 	struct mbuf *mb, *nm = NULL, *mtail = NULL;
169 
170 	KASSERT(len >= 0, ("%s: len is < 0", __func__));
171 
172 	/* Validate flags. */
173 	flags &= (M_PKTHDR | M_EOR);
174 
175 	/* Packet header mbuf must be first in chain. */
176 	if ((flags & M_PKTHDR) && m != NULL)
177 		flags &= ~M_PKTHDR;
178 
179 	/* Loop and append maximum sized mbufs to the chain tail. */
180 	while (len > 0) {
181 		if (len > MCLBYTES)
182 			mb = m_getjcl(how, type, (flags & M_PKTHDR),
183 			    MJUMPAGESIZE);
184 		else if (len >= MINCLSIZE)
185 			mb = m_getcl(how, type, (flags & M_PKTHDR));
186 		else if (flags & M_PKTHDR)
187 			mb = m_gethdr(how, type);
188 		else
189 			mb = m_get(how, type);
190 
191 		/* Fail the whole operation if one mbuf can't be allocated. */
192 		if (mb == NULL) {
193 			if (nm != NULL)
194 				m_freem(nm);
195 			return (NULL);
196 		}
197 
198 		/* Book keeping. */
199 		len -= (mb->m_flags & M_EXT) ? mb->m_ext.ext_size :
200 			((mb->m_flags & M_PKTHDR) ? MHLEN : MLEN);
201 		if (mtail != NULL)
202 			mtail->m_next = mb;
203 		else
204 			nm = mb;
205 		mtail = mb;
206 		flags &= ~M_PKTHDR;	/* Only valid on the first mbuf. */
207 	}
208 	if (flags & M_EOR)
209 		mtail->m_flags |= M_EOR;  /* Only valid on the last mbuf. */
210 
211 	/* If mbuf was supplied, append new chain to the end of it. */
212 	if (m != NULL) {
213 		for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next)
214 			;
215 		mtail->m_next = nm;
216 		mtail->m_flags &= ~M_EOR;
217 	} else
218 		m = nm;
219 
220 	return (m);
221 }
222 
223 /*
224  * Free an entire chain of mbufs and associated external buffers, if
225  * applicable.
226  */
227 void
228 m_freem(struct mbuf *mb)
229 {
230 
231 	while (mb != NULL)
232 		mb = m_free(mb);
233 }
234 
235 /*-
236  * Configure a provided mbuf to refer to the provided external storage
237  * buffer and setup a reference count for said buffer.  If the setting
238  * up of the reference count fails, the M_EXT bit will not be set.  If
239  * successfull, the M_EXT bit is set in the mbuf's flags.
240  *
241  * Arguments:
242  *    mb     The existing mbuf to which to attach the provided buffer.
243  *    buf    The address of the provided external storage buffer.
244  *    size   The size of the provided buffer.
245  *    freef  A pointer to a routine that is responsible for freeing the
246  *           provided external storage buffer.
247  *    args   A pointer to an argument structure (of any type) to be passed
248  *           to the provided freef routine (may be NULL).
249  *    flags  Any other flags to be passed to the provided mbuf.
250  *    type   The type that the external storage buffer should be
251  *           labeled with.
252  *
253  * Returns:
254  *    Nothing.
255  */
256 int
257 m_extadd(struct mbuf *mb, caddr_t buf, u_int size,
258     int (*freef)(struct mbuf *, void *, void *), void *arg1, void *arg2,
259     int flags, int type, int wait)
260 {
261 	KASSERT(type != EXT_CLUSTER, ("%s: EXT_CLUSTER not allowed", __func__));
262 
263 	if (type != EXT_EXTREF)
264 		mb->m_ext.ref_cnt = uma_zalloc(zone_ext_refcnt, wait);
265 
266 	if (mb->m_ext.ref_cnt == NULL)
267 		return (ENOMEM);
268 
269 	*(mb->m_ext.ref_cnt) = 1;
270 	mb->m_flags |= (M_EXT | flags);
271 	mb->m_ext.ext_buf = buf;
272 	mb->m_data = mb->m_ext.ext_buf;
273 	mb->m_ext.ext_size = size;
274 	mb->m_ext.ext_free = freef;
275 	mb->m_ext.ext_arg1 = arg1;
276 	mb->m_ext.ext_arg2 = arg2;
277 	mb->m_ext.ext_type = type;
278 	mb->m_ext.ext_flags = 0;
279 
280 	return (0);
281 }
282 
283 /*
284  * Non-directly-exported function to clean up after mbufs with M_EXT
285  * storage attached to them if the reference count hits 1.
286  */
287 void
288 mb_free_ext(struct mbuf *m)
289 {
290 	int skipmbuf;
291 
292 	KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__));
293 	KASSERT(m->m_ext.ref_cnt != NULL, ("%s: ref_cnt not set", __func__));
294 
295 	/*
296 	 * check if the header is embedded in the cluster
297 	 */
298 	skipmbuf = (m->m_flags & M_NOFREE);
299 
300 	/* Free attached storage if this mbuf is the only reference to it. */
301 	if (*(m->m_ext.ref_cnt) == 1 ||
302 	    atomic_fetchadd_int(m->m_ext.ref_cnt, -1) == 1) {
303 		switch (m->m_ext.ext_type) {
304 		case EXT_PACKET:	/* The packet zone is special. */
305 			if (*(m->m_ext.ref_cnt) == 0)
306 				*(m->m_ext.ref_cnt) = 1;
307 			uma_zfree(zone_pack, m);
308 			return;		/* Job done. */
309 		case EXT_CLUSTER:
310 			uma_zfree(zone_clust, m->m_ext.ext_buf);
311 			break;
312 		case EXT_JUMBOP:
313 			uma_zfree(zone_jumbop, m->m_ext.ext_buf);
314 			break;
315 		case EXT_JUMBO9:
316 			uma_zfree(zone_jumbo9, m->m_ext.ext_buf);
317 			break;
318 		case EXT_JUMBO16:
319 			uma_zfree(zone_jumbo16, m->m_ext.ext_buf);
320 			break;
321 		case EXT_SFBUF:
322 		case EXT_NET_DRV:
323 		case EXT_MOD_TYPE:
324 		case EXT_DISPOSABLE:
325 			*(m->m_ext.ref_cnt) = 0;
326 			uma_zfree(zone_ext_refcnt, __DEVOLATILE(u_int *,
327 				m->m_ext.ref_cnt));
328 			/* FALLTHROUGH */
329 		case EXT_EXTREF:
330 			KASSERT(m->m_ext.ext_free != NULL,
331 				("%s: ext_free not set", __func__));
332 			(void)(*(m->m_ext.ext_free))(m, m->m_ext.ext_arg1,
333 			    m->m_ext.ext_arg2);
334 			break;
335 		default:
336 			KASSERT(m->m_ext.ext_type == 0,
337 				("%s: unknown ext_type", __func__));
338 		}
339 	}
340 	if (skipmbuf)
341 		return;
342 
343 	/*
344 	 * Free this mbuf back to the mbuf zone with all m_ext
345 	 * information purged.
346 	 */
347 	m->m_ext.ext_buf = NULL;
348 	m->m_ext.ext_free = NULL;
349 	m->m_ext.ext_arg1 = NULL;
350 	m->m_ext.ext_arg2 = NULL;
351 	m->m_ext.ref_cnt = NULL;
352 	m->m_ext.ext_size = 0;
353 	m->m_ext.ext_type = 0;
354 	m->m_ext.ext_flags = 0;
355 	m->m_flags &= ~M_EXT;
356 	uma_zfree(zone_mbuf, m);
357 }
358 
359 /*
360  * Attach the cluster from *m to *n, set up m_ext in *n
361  * and bump the refcount of the cluster.
362  */
363 static void
364 mb_dupcl(struct mbuf *n, struct mbuf *m)
365 {
366 	KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__));
367 	KASSERT(m->m_ext.ref_cnt != NULL, ("%s: ref_cnt not set", __func__));
368 	KASSERT((n->m_flags & M_EXT) == 0, ("%s: M_EXT set", __func__));
369 
370 	if (*(m->m_ext.ref_cnt) == 1)
371 		*(m->m_ext.ref_cnt) += 1;
372 	else
373 		atomic_add_int(m->m_ext.ref_cnt, 1);
374 	n->m_ext.ext_buf = m->m_ext.ext_buf;
375 	n->m_ext.ext_free = m->m_ext.ext_free;
376 	n->m_ext.ext_arg1 = m->m_ext.ext_arg1;
377 	n->m_ext.ext_arg2 = m->m_ext.ext_arg2;
378 	n->m_ext.ext_size = m->m_ext.ext_size;
379 	n->m_ext.ref_cnt = m->m_ext.ref_cnt;
380 	n->m_ext.ext_type = m->m_ext.ext_type;
381 	n->m_ext.ext_flags = m->m_ext.ext_flags;
382 	n->m_flags |= M_EXT;
383 	n->m_flags |= m->m_flags & M_RDONLY;
384 }
385 
386 /*
387  * Clean up mbuf (chain) from any tags and packet headers.
388  * If "all" is set then the first mbuf in the chain will be
389  * cleaned too.
390  */
391 void
392 m_demote(struct mbuf *m0, int all)
393 {
394 	struct mbuf *m;
395 
396 	for (m = all ? m0 : m0->m_next; m != NULL; m = m->m_next) {
397 		if (m->m_flags & M_PKTHDR) {
398 			m_tag_delete_chain(m, NULL);
399 			m->m_flags &= ~M_PKTHDR;
400 			bzero(&m->m_pkthdr, sizeof(struct pkthdr));
401 		}
402 		if (m != m0 && m->m_nextpkt != NULL) {
403 			KASSERT(m->m_nextpkt == NULL,
404 			    ("%s: m_nextpkt not NULL", __func__));
405 			m_freem(m->m_nextpkt);
406 			m->m_nextpkt = NULL;
407 		}
408 		m->m_flags = m->m_flags & (M_EXT|M_RDONLY|M_NOFREE);
409 	}
410 }
411 
412 /*
413  * Sanity checks on mbuf (chain) for use in KASSERT() and general
414  * debugging.
415  * Returns 0 or panics when bad and 1 on all tests passed.
416  * Sanitize, 0 to run M_SANITY_ACTION, 1 to garble things so they
417  * blow up later.
418  */
419 int
420 m_sanity(struct mbuf *m0, int sanitize)
421 {
422 	struct mbuf *m;
423 	caddr_t a, b;
424 	int pktlen = 0;
425 
426 #ifdef INVARIANTS
427 #define	M_SANITY_ACTION(s)	panic("mbuf %p: " s, m)
428 #else
429 #define	M_SANITY_ACTION(s)	printf("mbuf %p: " s, m)
430 #endif
431 
432 	for (m = m0; m != NULL; m = m->m_next) {
433 		/*
434 		 * Basic pointer checks.  If any of these fails then some
435 		 * unrelated kernel memory before or after us is trashed.
436 		 * No way to recover from that.
437 		 */
438 		a = ((m->m_flags & M_EXT) ? m->m_ext.ext_buf :
439 			((m->m_flags & M_PKTHDR) ? (caddr_t)(&m->m_pktdat) :
440 			 (caddr_t)(&m->m_dat)) );
441 		b = (caddr_t)(a + (m->m_flags & M_EXT ? m->m_ext.ext_size :
442 			((m->m_flags & M_PKTHDR) ? MHLEN : MLEN)));
443 		if ((caddr_t)m->m_data < a)
444 			M_SANITY_ACTION("m_data outside mbuf data range left");
445 		if ((caddr_t)m->m_data > b)
446 			M_SANITY_ACTION("m_data outside mbuf data range right");
447 		if ((caddr_t)m->m_data + m->m_len > b)
448 			M_SANITY_ACTION("m_data + m_len exeeds mbuf space");
449 
450 		/* m->m_nextpkt may only be set on first mbuf in chain. */
451 		if (m != m0 && m->m_nextpkt != NULL) {
452 			if (sanitize) {
453 				m_freem(m->m_nextpkt);
454 				m->m_nextpkt = (struct mbuf *)0xDEADC0DE;
455 			} else
456 				M_SANITY_ACTION("m->m_nextpkt on in-chain mbuf");
457 		}
458 
459 		/* packet length (not mbuf length!) calculation */
460 		if (m0->m_flags & M_PKTHDR)
461 			pktlen += m->m_len;
462 
463 		/* m_tags may only be attached to first mbuf in chain. */
464 		if (m != m0 && m->m_flags & M_PKTHDR &&
465 		    !SLIST_EMPTY(&m->m_pkthdr.tags)) {
466 			if (sanitize) {
467 				m_tag_delete_chain(m, NULL);
468 				/* put in 0xDEADC0DE perhaps? */
469 			} else
470 				M_SANITY_ACTION("m_tags on in-chain mbuf");
471 		}
472 
473 		/* M_PKTHDR may only be set on first mbuf in chain */
474 		if (m != m0 && m->m_flags & M_PKTHDR) {
475 			if (sanitize) {
476 				bzero(&m->m_pkthdr, sizeof(m->m_pkthdr));
477 				m->m_flags &= ~M_PKTHDR;
478 				/* put in 0xDEADCODE and leave hdr flag in */
479 			} else
480 				M_SANITY_ACTION("M_PKTHDR on in-chain mbuf");
481 		}
482 	}
483 	m = m0;
484 	if (pktlen && pktlen != m->m_pkthdr.len) {
485 		if (sanitize)
486 			m->m_pkthdr.len = 0;
487 		else
488 			M_SANITY_ACTION("m_pkthdr.len != mbuf chain length");
489 	}
490 	return 1;
491 
492 #undef	M_SANITY_ACTION
493 }
494 
495 
496 /*
497  * "Move" mbuf pkthdr from "from" to "to".
498  * "from" must have M_PKTHDR set, and "to" must be empty.
499  */
500 void
501 m_move_pkthdr(struct mbuf *to, struct mbuf *from)
502 {
503 
504 #if 0
505 	/* see below for why these are not enabled */
506 	M_ASSERTPKTHDR(to);
507 	/* Note: with MAC, this may not be a good assertion. */
508 	KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags),
509 	    ("m_move_pkthdr: to has tags"));
510 #endif
511 #ifdef MAC
512 	/*
513 	 * XXXMAC: It could be this should also occur for non-MAC?
514 	 */
515 	if (to->m_flags & M_PKTHDR)
516 		m_tag_delete_chain(to, NULL);
517 #endif
518 	to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT);
519 	if ((to->m_flags & M_EXT) == 0)
520 		to->m_data = to->m_pktdat;
521 	to->m_pkthdr = from->m_pkthdr;		/* especially tags */
522 	SLIST_INIT(&from->m_pkthdr.tags);	/* purge tags from src */
523 	from->m_flags &= ~M_PKTHDR;
524 }
525 
526 /*
527  * Duplicate "from"'s mbuf pkthdr in "to".
528  * "from" must have M_PKTHDR set, and "to" must be empty.
529  * In particular, this does a deep copy of the packet tags.
530  */
531 int
532 m_dup_pkthdr(struct mbuf *to, struct mbuf *from, int how)
533 {
534 
535 #if 0
536 	/*
537 	 * The mbuf allocator only initializes the pkthdr
538 	 * when the mbuf is allocated with m_gethdr(). Many users
539 	 * (e.g. m_copy*, m_prepend) use m_get() and then
540 	 * smash the pkthdr as needed causing these
541 	 * assertions to trip.  For now just disable them.
542 	 */
543 	M_ASSERTPKTHDR(to);
544 	/* Note: with MAC, this may not be a good assertion. */
545 	KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), ("m_dup_pkthdr: to has tags"));
546 #endif
547 	MBUF_CHECKSLEEP(how);
548 #ifdef MAC
549 	if (to->m_flags & M_PKTHDR)
550 		m_tag_delete_chain(to, NULL);
551 #endif
552 	to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT);
553 	if ((to->m_flags & M_EXT) == 0)
554 		to->m_data = to->m_pktdat;
555 	to->m_pkthdr = from->m_pkthdr;
556 	SLIST_INIT(&to->m_pkthdr.tags);
557 	return (m_tag_copy_chain(to, from, MBTOM(how)));
558 }
559 
560 /*
561  * Lesser-used path for M_PREPEND:
562  * allocate new mbuf to prepend to chain,
563  * copy junk along.
564  */
565 struct mbuf *
566 m_prepend(struct mbuf *m, int len, int how)
567 {
568 	struct mbuf *mn;
569 
570 	if (m->m_flags & M_PKTHDR)
571 		mn = m_gethdr(how, m->m_type);
572 	else
573 		mn = m_get(how, m->m_type);
574 	if (mn == NULL) {
575 		m_freem(m);
576 		return (NULL);
577 	}
578 	if (m->m_flags & M_PKTHDR)
579 		m_move_pkthdr(mn, m);
580 	mn->m_next = m;
581 	m = mn;
582 	if(m->m_flags & M_PKTHDR) {
583 		if (len < MHLEN)
584 			MH_ALIGN(m, len);
585 	} else {
586 		if (len < MLEN)
587 			M_ALIGN(m, len);
588 	}
589 	m->m_len = len;
590 	return (m);
591 }
592 
593 /*
594  * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
595  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
596  * The wait parameter is a choice of M_WAITOK/M_NOWAIT from caller.
597  * Note that the copy is read-only, because clusters are not copied,
598  * only their reference counts are incremented.
599  */
600 struct mbuf *
601 m_copym(struct mbuf *m, int off0, int len, int wait)
602 {
603 	struct mbuf *n, **np;
604 	int off = off0;
605 	struct mbuf *top;
606 	int copyhdr = 0;
607 
608 	KASSERT(off >= 0, ("m_copym, negative off %d", off));
609 	KASSERT(len >= 0, ("m_copym, negative len %d", len));
610 	MBUF_CHECKSLEEP(wait);
611 	if (off == 0 && m->m_flags & M_PKTHDR)
612 		copyhdr = 1;
613 	while (off > 0) {
614 		KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain"));
615 		if (off < m->m_len)
616 			break;
617 		off -= m->m_len;
618 		m = m->m_next;
619 	}
620 	np = &top;
621 	top = 0;
622 	while (len > 0) {
623 		if (m == NULL) {
624 			KASSERT(len == M_COPYALL,
625 			    ("m_copym, length > size of mbuf chain"));
626 			break;
627 		}
628 		if (copyhdr)
629 			n = m_gethdr(wait, m->m_type);
630 		else
631 			n = m_get(wait, m->m_type);
632 		*np = n;
633 		if (n == NULL)
634 			goto nospace;
635 		if (copyhdr) {
636 			if (!m_dup_pkthdr(n, m, wait))
637 				goto nospace;
638 			if (len == M_COPYALL)
639 				n->m_pkthdr.len -= off0;
640 			else
641 				n->m_pkthdr.len = len;
642 			copyhdr = 0;
643 		}
644 		n->m_len = min(len, m->m_len - off);
645 		if (m->m_flags & M_EXT) {
646 			n->m_data = m->m_data + off;
647 			mb_dupcl(n, m);
648 		} else
649 			bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
650 			    (u_int)n->m_len);
651 		if (len != M_COPYALL)
652 			len -= n->m_len;
653 		off = 0;
654 		m = m->m_next;
655 		np = &n->m_next;
656 	}
657 
658 	return (top);
659 nospace:
660 	m_freem(top);
661 	return (NULL);
662 }
663 
664 /*
665  * Returns mbuf chain with new head for the prepending case.
666  * Copies from mbuf (chain) n from off for len to mbuf (chain) m
667  * either prepending or appending the data.
668  * The resulting mbuf (chain) m is fully writeable.
669  * m is destination (is made writeable)
670  * n is source, off is offset in source, len is len from offset
671  * dir, 0 append, 1 prepend
672  * how, wait or nowait
673  */
674 
675 static int
676 m_bcopyxxx(void *s, void *t, u_int len)
677 {
678 	bcopy(s, t, (size_t)len);
679 	return 0;
680 }
681 
682 struct mbuf *
683 m_copymdata(struct mbuf *m, struct mbuf *n, int off, int len,
684     int prep, int how)
685 {
686 	struct mbuf *mm, *x, *z, *prev = NULL;
687 	caddr_t p;
688 	int i, nlen = 0;
689 	caddr_t buf[MLEN];
690 
691 	KASSERT(m != NULL && n != NULL, ("m_copymdata, no target or source"));
692 	KASSERT(off >= 0, ("m_copymdata, negative off %d", off));
693 	KASSERT(len >= 0, ("m_copymdata, negative len %d", len));
694 	KASSERT(prep == 0 || prep == 1, ("m_copymdata, unknown direction %d", prep));
695 
696 	mm = m;
697 	if (!prep) {
698 		while(mm->m_next) {
699 			prev = mm;
700 			mm = mm->m_next;
701 		}
702 	}
703 	for (z = n; z != NULL; z = z->m_next)
704 		nlen += z->m_len;
705 	if (len == M_COPYALL)
706 		len = nlen - off;
707 	if (off + len > nlen || len < 1)
708 		return NULL;
709 
710 	if (!M_WRITABLE(mm)) {
711 		/* XXX: Use proper m_xxx function instead. */
712 		x = m_getcl(how, MT_DATA, mm->m_flags);
713 		if (x == NULL)
714 			return NULL;
715 		bcopy(mm->m_ext.ext_buf, x->m_ext.ext_buf, x->m_ext.ext_size);
716 		p = x->m_ext.ext_buf + (mm->m_data - mm->m_ext.ext_buf);
717 		x->m_data = p;
718 		mm->m_next = NULL;
719 		if (mm != m)
720 			prev->m_next = x;
721 		m_free(mm);
722 		mm = x;
723 	}
724 
725 	/*
726 	 * Append/prepend the data.  Allocating mbufs as necessary.
727 	 */
728 	/* Shortcut if enough free space in first/last mbuf. */
729 	if (!prep && M_TRAILINGSPACE(mm) >= len) {
730 		m_apply(n, off, len, m_bcopyxxx, mtod(mm, caddr_t) +
731 			 mm->m_len);
732 		mm->m_len += len;
733 		mm->m_pkthdr.len += len;
734 		return m;
735 	}
736 	if (prep && M_LEADINGSPACE(mm) >= len) {
737 		mm->m_data = mtod(mm, caddr_t) - len;
738 		m_apply(n, off, len, m_bcopyxxx, mtod(mm, caddr_t));
739 		mm->m_len += len;
740 		mm->m_pkthdr.len += len;
741 		return mm;
742 	}
743 
744 	/* Expand first/last mbuf to cluster if possible. */
745 	if (!prep && !(mm->m_flags & M_EXT) && len > M_TRAILINGSPACE(mm)) {
746 		bcopy(mm->m_data, &buf, mm->m_len);
747 		m_clget(mm, how);
748 		if (!(mm->m_flags & M_EXT))
749 			return NULL;
750 		bcopy(&buf, mm->m_ext.ext_buf, mm->m_len);
751 		mm->m_data = mm->m_ext.ext_buf;
752 	}
753 	if (prep && !(mm->m_flags & M_EXT) && len > M_LEADINGSPACE(mm)) {
754 		bcopy(mm->m_data, &buf, mm->m_len);
755 		m_clget(mm, how);
756 		if (!(mm->m_flags & M_EXT))
757 			return NULL;
758 		bcopy(&buf, (caddr_t *)mm->m_ext.ext_buf +
759 		       mm->m_ext.ext_size - mm->m_len, mm->m_len);
760 		mm->m_data = (caddr_t)mm->m_ext.ext_buf +
761 			      mm->m_ext.ext_size - mm->m_len;
762 	}
763 
764 	/* Append/prepend as many mbuf (clusters) as necessary to fit len. */
765 	if (!prep && len > M_TRAILINGSPACE(mm)) {
766 		if (!m_getm(mm, len - M_TRAILINGSPACE(mm), how, MT_DATA))
767 			return NULL;
768 	}
769 	if (prep && len > M_LEADINGSPACE(mm)) {
770 		if (!(z = m_getm(NULL, len - M_LEADINGSPACE(mm), how, MT_DATA)))
771 			return NULL;
772 		i = 0;
773 		for (x = z; x != NULL; x = x->m_next) {
774 			i += x->m_flags & M_EXT ? x->m_ext.ext_size :
775 			      (x->m_flags & M_PKTHDR ? MHLEN : MLEN);
776 			if (!x->m_next)
777 				break;
778 		}
779 		z->m_data += i - len;
780 		m_move_pkthdr(mm, z);
781 		x->m_next = mm;
782 		mm = z;
783 	}
784 
785 	/* Seek to start position in source mbuf. Optimization for long chains. */
786 	while (off > 0) {
787 		if (off < n->m_len)
788 			break;
789 		off -= n->m_len;
790 		n = n->m_next;
791 	}
792 
793 	/* Copy data into target mbuf. */
794 	z = mm;
795 	while (len > 0) {
796 		KASSERT(z != NULL, ("m_copymdata, falling off target edge"));
797 		i = M_TRAILINGSPACE(z);
798 		m_apply(n, off, i, m_bcopyxxx, mtod(z, caddr_t) + z->m_len);
799 		z->m_len += i;
800 		/* fixup pkthdr.len if necessary */
801 		if ((prep ? mm : m)->m_flags & M_PKTHDR)
802 			(prep ? mm : m)->m_pkthdr.len += i;
803 		off += i;
804 		len -= i;
805 		z = z->m_next;
806 	}
807 	return (prep ? mm : m);
808 }
809 
810 /*
811  * Copy an entire packet, including header (which must be present).
812  * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
813  * Note that the copy is read-only, because clusters are not copied,
814  * only their reference counts are incremented.
815  * Preserve alignment of the first mbuf so if the creator has left
816  * some room at the beginning (e.g. for inserting protocol headers)
817  * the copies still have the room available.
818  */
819 struct mbuf *
820 m_copypacket(struct mbuf *m, int how)
821 {
822 	struct mbuf *top, *n, *o;
823 
824 	MBUF_CHECKSLEEP(how);
825 	n = m_get(how, m->m_type);
826 	top = n;
827 	if (n == NULL)
828 		goto nospace;
829 
830 	if (!m_dup_pkthdr(n, m, how))
831 		goto nospace;
832 	n->m_len = m->m_len;
833 	if (m->m_flags & M_EXT) {
834 		n->m_data = m->m_data;
835 		mb_dupcl(n, m);
836 	} else {
837 		n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat );
838 		bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
839 	}
840 
841 	m = m->m_next;
842 	while (m) {
843 		o = m_get(how, m->m_type);
844 		if (o == NULL)
845 			goto nospace;
846 
847 		n->m_next = o;
848 		n = n->m_next;
849 
850 		n->m_len = m->m_len;
851 		if (m->m_flags & M_EXT) {
852 			n->m_data = m->m_data;
853 			mb_dupcl(n, m);
854 		} else {
855 			bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
856 		}
857 
858 		m = m->m_next;
859 	}
860 	return top;
861 nospace:
862 	m_freem(top);
863 	return (NULL);
864 }
865 
866 /*
867  * Copy data from an mbuf chain starting "off" bytes from the beginning,
868  * continuing for "len" bytes, into the indicated buffer.
869  */
870 void
871 m_copydata(const struct mbuf *m, int off, int len, caddr_t cp)
872 {
873 	u_int count;
874 
875 	KASSERT(off >= 0, ("m_copydata, negative off %d", off));
876 	KASSERT(len >= 0, ("m_copydata, negative len %d", len));
877 	while (off > 0) {
878 		KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain"));
879 		if (off < m->m_len)
880 			break;
881 		off -= m->m_len;
882 		m = m->m_next;
883 	}
884 	while (len > 0) {
885 		KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain"));
886 		count = min(m->m_len - off, len);
887 		bcopy(mtod(m, caddr_t) + off, cp, count);
888 		len -= count;
889 		cp += count;
890 		off = 0;
891 		m = m->m_next;
892 	}
893 }
894 
895 /*
896  * Copy a packet header mbuf chain into a completely new chain, including
897  * copying any mbuf clusters.  Use this instead of m_copypacket() when
898  * you need a writable copy of an mbuf chain.
899  */
900 struct mbuf *
901 m_dup(struct mbuf *m, int how)
902 {
903 	struct mbuf **p, *top = NULL;
904 	int remain, moff, nsize;
905 
906 	MBUF_CHECKSLEEP(how);
907 	/* Sanity check */
908 	if (m == NULL)
909 		return (NULL);
910 	M_ASSERTPKTHDR(m);
911 
912 	/* While there's more data, get a new mbuf, tack it on, and fill it */
913 	remain = m->m_pkthdr.len;
914 	moff = 0;
915 	p = &top;
916 	while (remain > 0 || top == NULL) {	/* allow m->m_pkthdr.len == 0 */
917 		struct mbuf *n;
918 
919 		/* Get the next new mbuf */
920 		if (remain >= MINCLSIZE) {
921 			n = m_getcl(how, m->m_type, 0);
922 			nsize = MCLBYTES;
923 		} else {
924 			n = m_get(how, m->m_type);
925 			nsize = MLEN;
926 		}
927 		if (n == NULL)
928 			goto nospace;
929 
930 		if (top == NULL) {		/* First one, must be PKTHDR */
931 			if (!m_dup_pkthdr(n, m, how)) {
932 				m_free(n);
933 				goto nospace;
934 			}
935 			if ((n->m_flags & M_EXT) == 0)
936 				nsize = MHLEN;
937 		}
938 		n->m_len = 0;
939 
940 		/* Link it into the new chain */
941 		*p = n;
942 		p = &n->m_next;
943 
944 		/* Copy data from original mbuf(s) into new mbuf */
945 		while (n->m_len < nsize && m != NULL) {
946 			int chunk = min(nsize - n->m_len, m->m_len - moff);
947 
948 			bcopy(m->m_data + moff, n->m_data + n->m_len, chunk);
949 			moff += chunk;
950 			n->m_len += chunk;
951 			remain -= chunk;
952 			if (moff == m->m_len) {
953 				m = m->m_next;
954 				moff = 0;
955 			}
956 		}
957 
958 		/* Check correct total mbuf length */
959 		KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL),
960 		    	("%s: bogus m_pkthdr.len", __func__));
961 	}
962 	return (top);
963 
964 nospace:
965 	m_freem(top);
966 	return (NULL);
967 }
968 
969 /*
970  * Concatenate mbuf chain n to m.
971  * Both chains must be of the same type (e.g. MT_DATA).
972  * Any m_pkthdr is not updated.
973  */
974 void
975 m_cat(struct mbuf *m, struct mbuf *n)
976 {
977 	while (m->m_next)
978 		m = m->m_next;
979 	while (n) {
980 		if (!M_WRITABLE(m) ||
981 		    M_TRAILINGSPACE(m) < n->m_len) {
982 			/* just join the two chains */
983 			m->m_next = n;
984 			return;
985 		}
986 		/* splat the data from one into the other */
987 		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
988 		    (u_int)n->m_len);
989 		m->m_len += n->m_len;
990 		n = m_free(n);
991 	}
992 }
993 
994 void
995 m_adj(struct mbuf *mp, int req_len)
996 {
997 	int len = req_len;
998 	struct mbuf *m;
999 	int count;
1000 
1001 	if ((m = mp) == NULL)
1002 		return;
1003 	if (len >= 0) {
1004 		/*
1005 		 * Trim from head.
1006 		 */
1007 		while (m != NULL && len > 0) {
1008 			if (m->m_len <= len) {
1009 				len -= m->m_len;
1010 				m->m_len = 0;
1011 				m = m->m_next;
1012 			} else {
1013 				m->m_len -= len;
1014 				m->m_data += len;
1015 				len = 0;
1016 			}
1017 		}
1018 		if (mp->m_flags & M_PKTHDR)
1019 			mp->m_pkthdr.len -= (req_len - len);
1020 	} else {
1021 		/*
1022 		 * Trim from tail.  Scan the mbuf chain,
1023 		 * calculating its length and finding the last mbuf.
1024 		 * If the adjustment only affects this mbuf, then just
1025 		 * adjust and return.  Otherwise, rescan and truncate
1026 		 * after the remaining size.
1027 		 */
1028 		len = -len;
1029 		count = 0;
1030 		for (;;) {
1031 			count += m->m_len;
1032 			if (m->m_next == (struct mbuf *)0)
1033 				break;
1034 			m = m->m_next;
1035 		}
1036 		if (m->m_len >= len) {
1037 			m->m_len -= len;
1038 			if (mp->m_flags & M_PKTHDR)
1039 				mp->m_pkthdr.len -= len;
1040 			return;
1041 		}
1042 		count -= len;
1043 		if (count < 0)
1044 			count = 0;
1045 		/*
1046 		 * Correct length for chain is "count".
1047 		 * Find the mbuf with last data, adjust its length,
1048 		 * and toss data from remaining mbufs on chain.
1049 		 */
1050 		m = mp;
1051 		if (m->m_flags & M_PKTHDR)
1052 			m->m_pkthdr.len = count;
1053 		for (; m; m = m->m_next) {
1054 			if (m->m_len >= count) {
1055 				m->m_len = count;
1056 				if (m->m_next != NULL) {
1057 					m_freem(m->m_next);
1058 					m->m_next = NULL;
1059 				}
1060 				break;
1061 			}
1062 			count -= m->m_len;
1063 		}
1064 	}
1065 }
1066 
1067 /*
1068  * Rearange an mbuf chain so that len bytes are contiguous
1069  * and in the data area of an mbuf (so that mtod will work
1070  * for a structure of size len).  Returns the resulting
1071  * mbuf chain on success, frees it and returns null on failure.
1072  * If there is room, it will add up to max_protohdr-len extra bytes to the
1073  * contiguous region in an attempt to avoid being called next time.
1074  */
1075 struct mbuf *
1076 m_pullup(struct mbuf *n, int len)
1077 {
1078 	struct mbuf *m;
1079 	int count;
1080 	int space;
1081 
1082 	/*
1083 	 * If first mbuf has no cluster, and has room for len bytes
1084 	 * without shifting current data, pullup into it,
1085 	 * otherwise allocate a new mbuf to prepend to the chain.
1086 	 */
1087 	if ((n->m_flags & M_EXT) == 0 &&
1088 	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
1089 		if (n->m_len >= len)
1090 			return (n);
1091 		m = n;
1092 		n = n->m_next;
1093 		len -= m->m_len;
1094 	} else {
1095 		if (len > MHLEN)
1096 			goto bad;
1097 		m = m_get(M_NOWAIT, n->m_type);
1098 		if (m == NULL)
1099 			goto bad;
1100 		if (n->m_flags & M_PKTHDR)
1101 			m_move_pkthdr(m, n);
1102 	}
1103 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
1104 	do {
1105 		count = min(min(max(len, max_protohdr), space), n->m_len);
1106 		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
1107 		  (u_int)count);
1108 		len -= count;
1109 		m->m_len += count;
1110 		n->m_len -= count;
1111 		space -= count;
1112 		if (n->m_len)
1113 			n->m_data += count;
1114 		else
1115 			n = m_free(n);
1116 	} while (len > 0 && n);
1117 	if (len > 0) {
1118 		(void) m_free(m);
1119 		goto bad;
1120 	}
1121 	m->m_next = n;
1122 	return (m);
1123 bad:
1124 	m_freem(n);
1125 	return (NULL);
1126 }
1127 
1128 /*
1129  * Like m_pullup(), except a new mbuf is always allocated, and we allow
1130  * the amount of empty space before the data in the new mbuf to be specified
1131  * (in the event that the caller expects to prepend later).
1132  */
1133 int MSFail;
1134 
1135 struct mbuf *
1136 m_copyup(struct mbuf *n, int len, int dstoff)
1137 {
1138 	struct mbuf *m;
1139 	int count, space;
1140 
1141 	if (len > (MHLEN - dstoff))
1142 		goto bad;
1143 	m = m_get(M_NOWAIT, n->m_type);
1144 	if (m == NULL)
1145 		goto bad;
1146 	if (n->m_flags & M_PKTHDR)
1147 		m_move_pkthdr(m, n);
1148 	m->m_data += dstoff;
1149 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
1150 	do {
1151 		count = min(min(max(len, max_protohdr), space), n->m_len);
1152 		memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
1153 		    (unsigned)count);
1154 		len -= count;
1155 		m->m_len += count;
1156 		n->m_len -= count;
1157 		space -= count;
1158 		if (n->m_len)
1159 			n->m_data += count;
1160 		else
1161 			n = m_free(n);
1162 	} while (len > 0 && n);
1163 	if (len > 0) {
1164 		(void) m_free(m);
1165 		goto bad;
1166 	}
1167 	m->m_next = n;
1168 	return (m);
1169  bad:
1170 	m_freem(n);
1171 	MSFail++;
1172 	return (NULL);
1173 }
1174 
1175 /*
1176  * Partition an mbuf chain in two pieces, returning the tail --
1177  * all but the first len0 bytes.  In case of failure, it returns NULL and
1178  * attempts to restore the chain to its original state.
1179  *
1180  * Note that the resulting mbufs might be read-only, because the new
1181  * mbuf can end up sharing an mbuf cluster with the original mbuf if
1182  * the "breaking point" happens to lie within a cluster mbuf. Use the
1183  * M_WRITABLE() macro to check for this case.
1184  */
1185 struct mbuf *
1186 m_split(struct mbuf *m0, int len0, int wait)
1187 {
1188 	struct mbuf *m, *n;
1189 	u_int len = len0, remain;
1190 
1191 	MBUF_CHECKSLEEP(wait);
1192 	for (m = m0; m && len > m->m_len; m = m->m_next)
1193 		len -= m->m_len;
1194 	if (m == NULL)
1195 		return (NULL);
1196 	remain = m->m_len - len;
1197 	if (m0->m_flags & M_PKTHDR && remain == 0) {
1198 		n = m_gethdr(wait, m0->m_type);
1199 			return (NULL);
1200 		n->m_next = m->m_next;
1201 		m->m_next = NULL;
1202 		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
1203 		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
1204 		m0->m_pkthdr.len = len0;
1205 		return (n);
1206 	} else if (m0->m_flags & M_PKTHDR) {
1207 		n = m_gethdr(wait, m0->m_type);
1208 		if (n == NULL)
1209 			return (NULL);
1210 		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
1211 		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
1212 		m0->m_pkthdr.len = len0;
1213 		if (m->m_flags & M_EXT)
1214 			goto extpacket;
1215 		if (remain > MHLEN) {
1216 			/* m can't be the lead packet */
1217 			MH_ALIGN(n, 0);
1218 			n->m_next = m_split(m, len, wait);
1219 			if (n->m_next == NULL) {
1220 				(void) m_free(n);
1221 				return (NULL);
1222 			} else {
1223 				n->m_len = 0;
1224 				return (n);
1225 			}
1226 		} else
1227 			MH_ALIGN(n, remain);
1228 	} else if (remain == 0) {
1229 		n = m->m_next;
1230 		m->m_next = NULL;
1231 		return (n);
1232 	} else {
1233 		n = m_get(wait, m->m_type);
1234 		if (n == NULL)
1235 			return (NULL);
1236 		M_ALIGN(n, remain);
1237 	}
1238 extpacket:
1239 	if (m->m_flags & M_EXT) {
1240 		n->m_data = m->m_data + len;
1241 		mb_dupcl(n, m);
1242 	} else {
1243 		bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
1244 	}
1245 	n->m_len = remain;
1246 	m->m_len = len;
1247 	n->m_next = m->m_next;
1248 	m->m_next = NULL;
1249 	return (n);
1250 }
1251 /*
1252  * Routine to copy from device local memory into mbufs.
1253  * Note that `off' argument is offset into first mbuf of target chain from
1254  * which to begin copying the data to.
1255  */
1256 struct mbuf *
1257 m_devget(char *buf, int totlen, int off, struct ifnet *ifp,
1258     void (*copy)(char *from, caddr_t to, u_int len))
1259 {
1260 	struct mbuf *m;
1261 	struct mbuf *top = NULL, **mp = &top;
1262 	int len;
1263 
1264 	if (off < 0 || off > MHLEN)
1265 		return (NULL);
1266 
1267 	while (totlen > 0) {
1268 		if (top == NULL) {	/* First one, must be PKTHDR */
1269 			if (totlen + off >= MINCLSIZE) {
1270 				m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1271 				len = MCLBYTES;
1272 			} else {
1273 				m = m_gethdr(M_NOWAIT, MT_DATA);
1274 				len = MHLEN;
1275 
1276 				/* Place initial small packet/header at end of mbuf */
1277 				if (m && totlen + off + max_linkhdr <= MLEN) {
1278 					m->m_data += max_linkhdr;
1279 					len -= max_linkhdr;
1280 				}
1281 			}
1282 			if (m == NULL)
1283 				return NULL;
1284 			m->m_pkthdr.rcvif = ifp;
1285 			m->m_pkthdr.len = totlen;
1286 		} else {
1287 			if (totlen + off >= MINCLSIZE) {
1288 				m = m_getcl(M_NOWAIT, MT_DATA, 0);
1289 				len = MCLBYTES;
1290 			} else {
1291 				m = m_get(M_NOWAIT, MT_DATA);
1292 				len = MLEN;
1293 			}
1294 			if (m == NULL) {
1295 				m_freem(top);
1296 				return NULL;
1297 			}
1298 		}
1299 		if (off) {
1300 			m->m_data += off;
1301 			len -= off;
1302 			off = 0;
1303 		}
1304 		m->m_len = len = min(totlen, len);
1305 		if (copy)
1306 			copy(buf, mtod(m, caddr_t), (u_int)len);
1307 		else
1308 			bcopy(buf, mtod(m, caddr_t), (u_int)len);
1309 		buf += len;
1310 		*mp = m;
1311 		mp = &m->m_next;
1312 		totlen -= len;
1313 	}
1314 	return (top);
1315 }
1316 
1317 /*
1318  * Copy data from a buffer back into the indicated mbuf chain,
1319  * starting "off" bytes from the beginning, extending the mbuf
1320  * chain if necessary.
1321  */
1322 void
1323 m_copyback(struct mbuf *m0, int off, int len, c_caddr_t cp)
1324 {
1325 	int mlen;
1326 	struct mbuf *m = m0, *n;
1327 	int totlen = 0;
1328 
1329 	if (m0 == NULL)
1330 		return;
1331 	while (off > (mlen = m->m_len)) {
1332 		off -= mlen;
1333 		totlen += mlen;
1334 		if (m->m_next == NULL) {
1335 			n = m_get(M_NOWAIT, m->m_type);
1336 			if (n == NULL)
1337 				goto out;
1338 			bzero(mtod(n, caddr_t), MLEN);
1339 			n->m_len = min(MLEN, len + off);
1340 			m->m_next = n;
1341 		}
1342 		m = m->m_next;
1343 	}
1344 	while (len > 0) {
1345 		if (m->m_next == NULL && (len > m->m_len - off)) {
1346 			m->m_len += min(len - (m->m_len - off),
1347 			    M_TRAILINGSPACE(m));
1348 		}
1349 		mlen = min (m->m_len - off, len);
1350 		bcopy(cp, off + mtod(m, caddr_t), (u_int)mlen);
1351 		cp += mlen;
1352 		len -= mlen;
1353 		mlen += off;
1354 		off = 0;
1355 		totlen += mlen;
1356 		if (len == 0)
1357 			break;
1358 		if (m->m_next == NULL) {
1359 			n = m_get(M_NOWAIT, m->m_type);
1360 			if (n == NULL)
1361 				break;
1362 			n->m_len = min(MLEN, len);
1363 			m->m_next = n;
1364 		}
1365 		m = m->m_next;
1366 	}
1367 out:	if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
1368 		m->m_pkthdr.len = totlen;
1369 }
1370 
1371 /*
1372  * Append the specified data to the indicated mbuf chain,
1373  * Extend the mbuf chain if the new data does not fit in
1374  * existing space.
1375  *
1376  * Return 1 if able to complete the job; otherwise 0.
1377  */
1378 int
1379 m_append(struct mbuf *m0, int len, c_caddr_t cp)
1380 {
1381 	struct mbuf *m, *n;
1382 	int remainder, space;
1383 
1384 	for (m = m0; m->m_next != NULL; m = m->m_next)
1385 		;
1386 	remainder = len;
1387 	space = M_TRAILINGSPACE(m);
1388 	if (space > 0) {
1389 		/*
1390 		 * Copy into available space.
1391 		 */
1392 		if (space > remainder)
1393 			space = remainder;
1394 		bcopy(cp, mtod(m, caddr_t) + m->m_len, space);
1395 		m->m_len += space;
1396 		cp += space, remainder -= space;
1397 	}
1398 	while (remainder > 0) {
1399 		/*
1400 		 * Allocate a new mbuf; could check space
1401 		 * and allocate a cluster instead.
1402 		 */
1403 		n = m_get(M_NOWAIT, m->m_type);
1404 		if (n == NULL)
1405 			break;
1406 		n->m_len = min(MLEN, remainder);
1407 		bcopy(cp, mtod(n, caddr_t), n->m_len);
1408 		cp += n->m_len, remainder -= n->m_len;
1409 		m->m_next = n;
1410 		m = n;
1411 	}
1412 	if (m0->m_flags & M_PKTHDR)
1413 		m0->m_pkthdr.len += len - remainder;
1414 	return (remainder == 0);
1415 }
1416 
1417 /*
1418  * Apply function f to the data in an mbuf chain starting "off" bytes from
1419  * the beginning, continuing for "len" bytes.
1420  */
1421 int
1422 m_apply(struct mbuf *m, int off, int len,
1423     int (*f)(void *, void *, u_int), void *arg)
1424 {
1425 	u_int count;
1426 	int rval;
1427 
1428 	KASSERT(off >= 0, ("m_apply, negative off %d", off));
1429 	KASSERT(len >= 0, ("m_apply, negative len %d", len));
1430 	while (off > 0) {
1431 		KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain"));
1432 		if (off < m->m_len)
1433 			break;
1434 		off -= m->m_len;
1435 		m = m->m_next;
1436 	}
1437 	while (len > 0) {
1438 		KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain"));
1439 		count = min(m->m_len - off, len);
1440 		rval = (*f)(arg, mtod(m, caddr_t) + off, count);
1441 		if (rval)
1442 			return (rval);
1443 		len -= count;
1444 		off = 0;
1445 		m = m->m_next;
1446 	}
1447 	return (0);
1448 }
1449 
1450 /*
1451  * Return a pointer to mbuf/offset of location in mbuf chain.
1452  */
1453 struct mbuf *
1454 m_getptr(struct mbuf *m, int loc, int *off)
1455 {
1456 
1457 	while (loc >= 0) {
1458 		/* Normal end of search. */
1459 		if (m->m_len > loc) {
1460 			*off = loc;
1461 			return (m);
1462 		} else {
1463 			loc -= m->m_len;
1464 			if (m->m_next == NULL) {
1465 				if (loc == 0) {
1466 					/* Point at the end of valid data. */
1467 					*off = m->m_len;
1468 					return (m);
1469 				}
1470 				return (NULL);
1471 			}
1472 			m = m->m_next;
1473 		}
1474 	}
1475 	return (NULL);
1476 }
1477 
1478 void
1479 m_print(const struct mbuf *m, int maxlen)
1480 {
1481 	int len;
1482 	int pdata;
1483 	const struct mbuf *m2;
1484 
1485 	if (m == NULL) {
1486 		printf("mbuf: %p\n", m);
1487 		return;
1488 	}
1489 
1490 	if (m->m_flags & M_PKTHDR)
1491 		len = m->m_pkthdr.len;
1492 	else
1493 		len = -1;
1494 	m2 = m;
1495 	while (m2 != NULL && (len == -1 || len)) {
1496 		pdata = m2->m_len;
1497 		if (maxlen != -1 && pdata > maxlen)
1498 			pdata = maxlen;
1499 		printf("mbuf: %p len: %d, next: %p, %b%s", m2, m2->m_len,
1500 		    m2->m_next, m2->m_flags, "\20\20freelist\17skipfw"
1501 		    "\11proto5\10proto4\7proto3\6proto2\5proto1\4rdonly"
1502 		    "\3eor\2pkthdr\1ext", pdata ? "" : "\n");
1503 		if (pdata)
1504 			printf(", %*D\n", pdata, (u_char *)m2->m_data, "-");
1505 		if (len != -1)
1506 			len -= m2->m_len;
1507 		m2 = m2->m_next;
1508 	}
1509 	if (len > 0)
1510 		printf("%d bytes unaccounted for.\n", len);
1511 	return;
1512 }
1513 
1514 u_int
1515 m_fixhdr(struct mbuf *m0)
1516 {
1517 	u_int len;
1518 
1519 	len = m_length(m0, NULL);
1520 	m0->m_pkthdr.len = len;
1521 	return (len);
1522 }
1523 
1524 u_int
1525 m_length(struct mbuf *m0, struct mbuf **last)
1526 {
1527 	struct mbuf *m;
1528 	u_int len;
1529 
1530 	len = 0;
1531 	for (m = m0; m != NULL; m = m->m_next) {
1532 		len += m->m_len;
1533 		if (m->m_next == NULL)
1534 			break;
1535 	}
1536 	if (last != NULL)
1537 		*last = m;
1538 	return (len);
1539 }
1540 
1541 /*
1542  * Defragment a mbuf chain, returning the shortest possible
1543  * chain of mbufs and clusters.  If allocation fails and
1544  * this cannot be completed, NULL will be returned, but
1545  * the passed in chain will be unchanged.  Upon success,
1546  * the original chain will be freed, and the new chain
1547  * will be returned.
1548  *
1549  * If a non-packet header is passed in, the original
1550  * mbuf (chain?) will be returned unharmed.
1551  */
1552 struct mbuf *
1553 m_defrag(struct mbuf *m0, int how)
1554 {
1555 	struct mbuf *m_new = NULL, *m_final = NULL;
1556 	int progress = 0, length;
1557 
1558 	MBUF_CHECKSLEEP(how);
1559 	if (!(m0->m_flags & M_PKTHDR))
1560 		return (m0);
1561 
1562 	m_fixhdr(m0); /* Needed sanity check */
1563 
1564 #ifdef MBUF_STRESS_TEST
1565 	if (m_defragrandomfailures) {
1566 		int temp = arc4random() & 0xff;
1567 		if (temp == 0xba)
1568 			goto nospace;
1569 	}
1570 #endif
1571 
1572 	if (m0->m_pkthdr.len > MHLEN)
1573 		m_final = m_getcl(how, MT_DATA, M_PKTHDR);
1574 	else
1575 		m_final = m_gethdr(how, MT_DATA);
1576 
1577 	if (m_final == NULL)
1578 		goto nospace;
1579 
1580 	if (m_dup_pkthdr(m_final, m0, how) == 0)
1581 		goto nospace;
1582 
1583 	m_new = m_final;
1584 
1585 	while (progress < m0->m_pkthdr.len) {
1586 		length = m0->m_pkthdr.len - progress;
1587 		if (length > MCLBYTES)
1588 			length = MCLBYTES;
1589 
1590 		if (m_new == NULL) {
1591 			if (length > MLEN)
1592 				m_new = m_getcl(how, MT_DATA, 0);
1593 			else
1594 				m_new = m_get(how, MT_DATA);
1595 			if (m_new == NULL)
1596 				goto nospace;
1597 		}
1598 
1599 		m_copydata(m0, progress, length, mtod(m_new, caddr_t));
1600 		progress += length;
1601 		m_new->m_len = length;
1602 		if (m_new != m_final)
1603 			m_cat(m_final, m_new);
1604 		m_new = NULL;
1605 	}
1606 #ifdef MBUF_STRESS_TEST
1607 	if (m0->m_next == NULL)
1608 		m_defraguseless++;
1609 #endif
1610 	m_freem(m0);
1611 	m0 = m_final;
1612 #ifdef MBUF_STRESS_TEST
1613 	m_defragpackets++;
1614 	m_defragbytes += m0->m_pkthdr.len;
1615 #endif
1616 	return (m0);
1617 nospace:
1618 #ifdef MBUF_STRESS_TEST
1619 	m_defragfailure++;
1620 #endif
1621 	if (m_final)
1622 		m_freem(m_final);
1623 	return (NULL);
1624 }
1625 
1626 /*
1627  * Defragment an mbuf chain, returning at most maxfrags separate
1628  * mbufs+clusters.  If this is not possible NULL is returned and
1629  * the original mbuf chain is left in it's present (potentially
1630  * modified) state.  We use two techniques: collapsing consecutive
1631  * mbufs and replacing consecutive mbufs by a cluster.
1632  *
1633  * NB: this should really be named m_defrag but that name is taken
1634  */
1635 struct mbuf *
1636 m_collapse(struct mbuf *m0, int how, int maxfrags)
1637 {
1638 	struct mbuf *m, *n, *n2, **prev;
1639 	u_int curfrags;
1640 
1641 	/*
1642 	 * Calculate the current number of frags.
1643 	 */
1644 	curfrags = 0;
1645 	for (m = m0; m != NULL; m = m->m_next)
1646 		curfrags++;
1647 	/*
1648 	 * First, try to collapse mbufs.  Note that we always collapse
1649 	 * towards the front so we don't need to deal with moving the
1650 	 * pkthdr.  This may be suboptimal if the first mbuf has much
1651 	 * less data than the following.
1652 	 */
1653 	m = m0;
1654 again:
1655 	for (;;) {
1656 		n = m->m_next;
1657 		if (n == NULL)
1658 			break;
1659 		if (M_WRITABLE(m) &&
1660 		    n->m_len < M_TRAILINGSPACE(m)) {
1661 			bcopy(mtod(n, void *), mtod(m, char *) + m->m_len,
1662 				n->m_len);
1663 			m->m_len += n->m_len;
1664 			m->m_next = n->m_next;
1665 			m_free(n);
1666 			if (--curfrags <= maxfrags)
1667 				return m0;
1668 		} else
1669 			m = n;
1670 	}
1671 	KASSERT(maxfrags > 1,
1672 		("maxfrags %u, but normal collapse failed", maxfrags));
1673 	/*
1674 	 * Collapse consecutive mbufs to a cluster.
1675 	 */
1676 	prev = &m0->m_next;		/* NB: not the first mbuf */
1677 	while ((n = *prev) != NULL) {
1678 		if ((n2 = n->m_next) != NULL &&
1679 		    n->m_len + n2->m_len < MCLBYTES) {
1680 			m = m_getcl(how, MT_DATA, 0);
1681 			if (m == NULL)
1682 				goto bad;
1683 			bcopy(mtod(n, void *), mtod(m, void *), n->m_len);
1684 			bcopy(mtod(n2, void *), mtod(m, char *) + n->m_len,
1685 				n2->m_len);
1686 			m->m_len = n->m_len + n2->m_len;
1687 			m->m_next = n2->m_next;
1688 			*prev = m;
1689 			m_free(n);
1690 			m_free(n2);
1691 			if (--curfrags <= maxfrags)	/* +1 cl -2 mbufs */
1692 				return m0;
1693 			/*
1694 			 * Still not there, try the normal collapse
1695 			 * again before we allocate another cluster.
1696 			 */
1697 			goto again;
1698 		}
1699 		prev = &n->m_next;
1700 	}
1701 	/*
1702 	 * No place where we can collapse to a cluster; punt.
1703 	 * This can occur if, for example, you request 2 frags
1704 	 * but the packet requires that both be clusters (we
1705 	 * never reallocate the first mbuf to avoid moving the
1706 	 * packet header).
1707 	 */
1708 bad:
1709 	return NULL;
1710 }
1711 
1712 #ifdef MBUF_STRESS_TEST
1713 
1714 /*
1715  * Fragment an mbuf chain.  There's no reason you'd ever want to do
1716  * this in normal usage, but it's great for stress testing various
1717  * mbuf consumers.
1718  *
1719  * If fragmentation is not possible, the original chain will be
1720  * returned.
1721  *
1722  * Possible length values:
1723  * 0	 no fragmentation will occur
1724  * > 0	each fragment will be of the specified length
1725  * -1	each fragment will be the same random value in length
1726  * -2	each fragment's length will be entirely random
1727  * (Random values range from 1 to 256)
1728  */
1729 struct mbuf *
1730 m_fragment(struct mbuf *m0, int how, int length)
1731 {
1732 	struct mbuf *m_new = NULL, *m_final = NULL;
1733 	int progress = 0;
1734 
1735 	if (!(m0->m_flags & M_PKTHDR))
1736 		return (m0);
1737 
1738 	if ((length == 0) || (length < -2))
1739 		return (m0);
1740 
1741 	m_fixhdr(m0); /* Needed sanity check */
1742 
1743 	m_final = m_getcl(how, MT_DATA, M_PKTHDR);
1744 
1745 	if (m_final == NULL)
1746 		goto nospace;
1747 
1748 	if (m_dup_pkthdr(m_final, m0, how) == 0)
1749 		goto nospace;
1750 
1751 	m_new = m_final;
1752 
1753 	if (length == -1)
1754 		length = 1 + (arc4random() & 255);
1755 
1756 	while (progress < m0->m_pkthdr.len) {
1757 		int fraglen;
1758 
1759 		if (length > 0)
1760 			fraglen = length;
1761 		else
1762 			fraglen = 1 + (arc4random() & 255);
1763 		if (fraglen > m0->m_pkthdr.len - progress)
1764 			fraglen = m0->m_pkthdr.len - progress;
1765 
1766 		if (fraglen > MCLBYTES)
1767 			fraglen = MCLBYTES;
1768 
1769 		if (m_new == NULL) {
1770 			m_new = m_getcl(how, MT_DATA, 0);
1771 			if (m_new == NULL)
1772 				goto nospace;
1773 		}
1774 
1775 		m_copydata(m0, progress, fraglen, mtod(m_new, caddr_t));
1776 		progress += fraglen;
1777 		m_new->m_len = fraglen;
1778 		if (m_new != m_final)
1779 			m_cat(m_final, m_new);
1780 		m_new = NULL;
1781 	}
1782 	m_freem(m0);
1783 	m0 = m_final;
1784 	return (m0);
1785 nospace:
1786 	if (m_final)
1787 		m_freem(m_final);
1788 	/* Return the original chain on failure */
1789 	return (m0);
1790 }
1791 
1792 #endif
1793 
1794 /*
1795  * Copy the contents of uio into a properly sized mbuf chain.
1796  */
1797 struct mbuf *
1798 m_uiotombuf(struct uio *uio, int how, int len, int align, int flags)
1799 {
1800 	struct mbuf *m, *mb;
1801 	int error, length;
1802 	ssize_t total;
1803 	int progress = 0;
1804 
1805 	/*
1806 	 * len can be zero or an arbitrary large value bound by
1807 	 * the total data supplied by the uio.
1808 	 */
1809 	if (len > 0)
1810 		total = min(uio->uio_resid, len);
1811 	else
1812 		total = uio->uio_resid;
1813 
1814 	/*
1815 	 * The smallest unit returned by m_getm2() is a single mbuf
1816 	 * with pkthdr.  We can't align past it.
1817 	 */
1818 	if (align >= MHLEN)
1819 		return (NULL);
1820 
1821 	/*
1822 	 * Give us the full allocation or nothing.
1823 	 * If len is zero return the smallest empty mbuf.
1824 	 */
1825 	m = m_getm2(NULL, max(total + align, 1), how, MT_DATA, flags);
1826 	if (m == NULL)
1827 		return (NULL);
1828 	m->m_data += align;
1829 
1830 	/* Fill all mbufs with uio data and update header information. */
1831 	for (mb = m; mb != NULL; mb = mb->m_next) {
1832 		length = min(M_TRAILINGSPACE(mb), total - progress);
1833 
1834 		error = uiomove(mtod(mb, void *), length, uio);
1835 		if (error) {
1836 			m_freem(m);
1837 			return (NULL);
1838 		}
1839 
1840 		mb->m_len = length;
1841 		progress += length;
1842 		if (flags & M_PKTHDR)
1843 			m->m_pkthdr.len += length;
1844 	}
1845 	KASSERT(progress == total, ("%s: progress != total", __func__));
1846 
1847 	return (m);
1848 }
1849 
1850 /*
1851  * Copy an mbuf chain into a uio limited by len if set.
1852  */
1853 int
1854 m_mbuftouio(struct uio *uio, struct mbuf *m, int len)
1855 {
1856 	int error, length, total;
1857 	int progress = 0;
1858 
1859 	if (len > 0)
1860 		total = min(uio->uio_resid, len);
1861 	else
1862 		total = uio->uio_resid;
1863 
1864 	/* Fill the uio with data from the mbufs. */
1865 	for (; m != NULL; m = m->m_next) {
1866 		length = min(m->m_len, total - progress);
1867 
1868 		error = uiomove(mtod(m, void *), length, uio);
1869 		if (error)
1870 			return (error);
1871 
1872 		progress += length;
1873 	}
1874 
1875 	return (0);
1876 }
1877 
1878 /*
1879  * Set the m_data pointer of a newly-allocated mbuf
1880  * to place an object of the specified size at the
1881  * end of the mbuf, longword aligned.
1882  */
1883 void
1884 m_align(struct mbuf *m, int len)
1885 {
1886 #ifdef INVARIANTS
1887 	const char *msg = "%s: not a virgin mbuf";
1888 #endif
1889 	int adjust;
1890 
1891 	if (m->m_flags & M_EXT) {
1892 		KASSERT(m->m_data == m->m_ext.ext_buf, (msg, __func__));
1893 		adjust = m->m_ext.ext_size - len;
1894 	} else if (m->m_flags & M_PKTHDR) {
1895 		KASSERT(m->m_data == m->m_pktdat, (msg, __func__));
1896 		adjust = MHLEN - len;
1897 	} else {
1898 		KASSERT(m->m_data == m->m_dat, (msg, __func__));
1899 		adjust = MLEN - len;
1900 	}
1901 
1902 	m->m_data += adjust &~ (sizeof(long)-1);
1903 }
1904 
1905 /*
1906  * Create a writable copy of the mbuf chain.  While doing this
1907  * we compact the chain with a goal of producing a chain with
1908  * at most two mbufs.  The second mbuf in this chain is likely
1909  * to be a cluster.  The primary purpose of this work is to create
1910  * a writable packet for encryption, compression, etc.  The
1911  * secondary goal is to linearize the data so the data can be
1912  * passed to crypto hardware in the most efficient manner possible.
1913  */
1914 struct mbuf *
1915 m_unshare(struct mbuf *m0, int how)
1916 {
1917 	struct mbuf *m, *mprev;
1918 	struct mbuf *n, *mfirst, *mlast;
1919 	int len, off;
1920 
1921 	mprev = NULL;
1922 	for (m = m0; m != NULL; m = mprev->m_next) {
1923 		/*
1924 		 * Regular mbufs are ignored unless there's a cluster
1925 		 * in front of it that we can use to coalesce.  We do
1926 		 * the latter mainly so later clusters can be coalesced
1927 		 * also w/o having to handle them specially (i.e. convert
1928 		 * mbuf+cluster -> cluster).  This optimization is heavily
1929 		 * influenced by the assumption that we're running over
1930 		 * Ethernet where MCLBYTES is large enough that the max
1931 		 * packet size will permit lots of coalescing into a
1932 		 * single cluster.  This in turn permits efficient
1933 		 * crypto operations, especially when using hardware.
1934 		 */
1935 		if ((m->m_flags & M_EXT) == 0) {
1936 			if (mprev && (mprev->m_flags & M_EXT) &&
1937 			    m->m_len <= M_TRAILINGSPACE(mprev)) {
1938 				/* XXX: this ignores mbuf types */
1939 				memcpy(mtod(mprev, caddr_t) + mprev->m_len,
1940 				       mtod(m, caddr_t), m->m_len);
1941 				mprev->m_len += m->m_len;
1942 				mprev->m_next = m->m_next;	/* unlink from chain */
1943 				m_free(m);			/* reclaim mbuf */
1944 #if 0
1945 				newipsecstat.ips_mbcoalesced++;
1946 #endif
1947 			} else {
1948 				mprev = m;
1949 			}
1950 			continue;
1951 		}
1952 		/*
1953 		 * Writable mbufs are left alone (for now).
1954 		 */
1955 		if (M_WRITABLE(m)) {
1956 			mprev = m;
1957 			continue;
1958 		}
1959 
1960 		/*
1961 		 * Not writable, replace with a copy or coalesce with
1962 		 * the previous mbuf if possible (since we have to copy
1963 		 * it anyway, we try to reduce the number of mbufs and
1964 		 * clusters so that future work is easier).
1965 		 */
1966 		KASSERT(m->m_flags & M_EXT, ("m_flags 0x%x", m->m_flags));
1967 		/* NB: we only coalesce into a cluster or larger */
1968 		if (mprev != NULL && (mprev->m_flags & M_EXT) &&
1969 		    m->m_len <= M_TRAILINGSPACE(mprev)) {
1970 			/* XXX: this ignores mbuf types */
1971 			memcpy(mtod(mprev, caddr_t) + mprev->m_len,
1972 			       mtod(m, caddr_t), m->m_len);
1973 			mprev->m_len += m->m_len;
1974 			mprev->m_next = m->m_next;	/* unlink from chain */
1975 			m_free(m);			/* reclaim mbuf */
1976 #if 0
1977 			newipsecstat.ips_clcoalesced++;
1978 #endif
1979 			continue;
1980 		}
1981 
1982 		/*
1983 		 * Allocate new space to hold the copy and copy the data.
1984 		 * We deal with jumbo mbufs (i.e. m_len > MCLBYTES) by
1985 		 * splitting them into clusters.  We could just malloc a
1986 		 * buffer and make it external but too many device drivers
1987 		 * don't know how to break up the non-contiguous memory when
1988 		 * doing DMA.
1989 		 */
1990 		n = m_getcl(how, m->m_type, m->m_flags);
1991 		if (n == NULL) {
1992 			m_freem(m0);
1993 			return (NULL);
1994 		}
1995 		len = m->m_len;
1996 		off = 0;
1997 		mfirst = n;
1998 		mlast = NULL;
1999 		for (;;) {
2000 			int cc = min(len, MCLBYTES);
2001 			memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + off, cc);
2002 			n->m_len = cc;
2003 			if (mlast != NULL)
2004 				mlast->m_next = n;
2005 			mlast = n;
2006 #if 0
2007 			newipsecstat.ips_clcopied++;
2008 #endif
2009 
2010 			len -= cc;
2011 			if (len <= 0)
2012 				break;
2013 			off += cc;
2014 
2015 			n = m_getcl(how, m->m_type, m->m_flags);
2016 			if (n == NULL) {
2017 				m_freem(mfirst);
2018 				m_freem(m0);
2019 				return (NULL);
2020 			}
2021 		}
2022 		n->m_next = m->m_next;
2023 		if (mprev == NULL)
2024 			m0 = mfirst;		/* new head of chain */
2025 		else
2026 			mprev->m_next = mfirst;	/* replace old mbuf */
2027 		m_free(m);			/* release old mbuf */
2028 		mprev = mfirst;
2029 	}
2030 	return (m0);
2031 }
2032 
2033 #ifdef MBUF_PROFILING
2034 
2035 #define MP_BUCKETS 32 /* don't just change this as things may overflow.*/
2036 struct mbufprofile {
2037 	uintmax_t wasted[MP_BUCKETS];
2038 	uintmax_t used[MP_BUCKETS];
2039 	uintmax_t segments[MP_BUCKETS];
2040 } mbprof;
2041 
2042 #define MP_MAXDIGITS 21	/* strlen("16,000,000,000,000,000,000") == 21 */
2043 #define MP_NUMLINES 6
2044 #define MP_NUMSPERLINE 16
2045 #define MP_EXTRABYTES 64	/* > strlen("used:\nwasted:\nsegments:\n") */
2046 /* work out max space needed and add a bit of spare space too */
2047 #define MP_MAXLINE ((MP_MAXDIGITS+1) * MP_NUMSPERLINE)
2048 #define MP_BUFSIZE ((MP_MAXLINE * MP_NUMLINES) + 1 + MP_EXTRABYTES)
2049 
2050 char mbprofbuf[MP_BUFSIZE];
2051 
2052 void
2053 m_profile(struct mbuf *m)
2054 {
2055 	int segments = 0;
2056 	int used = 0;
2057 	int wasted = 0;
2058 
2059 	while (m) {
2060 		segments++;
2061 		used += m->m_len;
2062 		if (m->m_flags & M_EXT) {
2063 			wasted += MHLEN - sizeof(m->m_ext) +
2064 			    m->m_ext.ext_size - m->m_len;
2065 		} else {
2066 			if (m->m_flags & M_PKTHDR)
2067 				wasted += MHLEN - m->m_len;
2068 			else
2069 				wasted += MLEN - m->m_len;
2070 		}
2071 		m = m->m_next;
2072 	}
2073 	/* be paranoid.. it helps */
2074 	if (segments > MP_BUCKETS - 1)
2075 		segments = MP_BUCKETS - 1;
2076 	if (used > 100000)
2077 		used = 100000;
2078 	if (wasted > 100000)
2079 		wasted = 100000;
2080 	/* store in the appropriate bucket */
2081 	/* don't bother locking. if it's slightly off, so what? */
2082 	mbprof.segments[segments]++;
2083 	mbprof.used[fls(used)]++;
2084 	mbprof.wasted[fls(wasted)]++;
2085 }
2086 
2087 static void
2088 mbprof_textify(void)
2089 {
2090 	int offset;
2091 	char *c;
2092 	uint64_t *p;
2093 
2094 
2095 	p = &mbprof.wasted[0];
2096 	c = mbprofbuf;
2097 	offset = snprintf(c, MP_MAXLINE + 10,
2098 	    "wasted:\n"
2099 	    "%ju %ju %ju %ju %ju %ju %ju %ju "
2100 	    "%ju %ju %ju %ju %ju %ju %ju %ju\n",
2101 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
2102 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
2103 #ifdef BIG_ARRAY
2104 	p = &mbprof.wasted[16];
2105 	c += offset;
2106 	offset = snprintf(c, MP_MAXLINE,
2107 	    "%ju %ju %ju %ju %ju %ju %ju %ju "
2108 	    "%ju %ju %ju %ju %ju %ju %ju %ju\n",
2109 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
2110 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
2111 #endif
2112 	p = &mbprof.used[0];
2113 	c += offset;
2114 	offset = snprintf(c, MP_MAXLINE + 10,
2115 	    "used:\n"
2116 	    "%ju %ju %ju %ju %ju %ju %ju %ju "
2117 	    "%ju %ju %ju %ju %ju %ju %ju %ju\n",
2118 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
2119 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
2120 #ifdef BIG_ARRAY
2121 	p = &mbprof.used[16];
2122 	c += offset;
2123 	offset = snprintf(c, MP_MAXLINE,
2124 	    "%ju %ju %ju %ju %ju %ju %ju %ju "
2125 	    "%ju %ju %ju %ju %ju %ju %ju %ju\n",
2126 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
2127 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
2128 #endif
2129 	p = &mbprof.segments[0];
2130 	c += offset;
2131 	offset = snprintf(c, MP_MAXLINE + 10,
2132 	    "segments:\n"
2133 	    "%ju %ju %ju %ju %ju %ju %ju %ju "
2134 	    "%ju %ju %ju %ju %ju %ju %ju %ju\n",
2135 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
2136 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
2137 #ifdef BIG_ARRAY
2138 	p = &mbprof.segments[16];
2139 	c += offset;
2140 	offset = snprintf(c, MP_MAXLINE,
2141 	    "%ju %ju %ju %ju %ju %ju %ju %ju "
2142 	    "%ju %ju %ju %ju %ju %ju %ju %jju",
2143 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
2144 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
2145 #endif
2146 }
2147 
2148 static int
2149 mbprof_handler(SYSCTL_HANDLER_ARGS)
2150 {
2151 	int error;
2152 
2153 	mbprof_textify();
2154 	error = SYSCTL_OUT(req, mbprofbuf, strlen(mbprofbuf) + 1);
2155 	return (error);
2156 }
2157 
2158 static int
2159 mbprof_clr_handler(SYSCTL_HANDLER_ARGS)
2160 {
2161 	int clear, error;
2162 
2163 	clear = 0;
2164 	error = sysctl_handle_int(oidp, &clear, 0, req);
2165 	if (error || !req->newptr)
2166 		return (error);
2167 
2168 	if (clear) {
2169 		bzero(&mbprof, sizeof(mbprof));
2170 	}
2171 
2172 	return (error);
2173 }
2174 
2175 
2176 SYSCTL_PROC(_kern_ipc, OID_AUTO, mbufprofile, CTLTYPE_STRING|CTLFLAG_RD,
2177 	    NULL, 0, mbprof_handler, "A", "mbuf profiling statistics");
2178 
2179 SYSCTL_PROC(_kern_ipc, OID_AUTO, mbufprofileclr, CTLTYPE_INT|CTLFLAG_RW,
2180 	    NULL, 0, mbprof_clr_handler, "I", "clear mbuf profiling statistics");
2181 #endif
2182 
2183