xref: /dflybsd-src/sys/kern/uipc_mbuf.c (revision 93bffecadc0caefc46f12b736eab0e62c2b6f42e)
1 /*
2  * (MPSAFE)
3  *
4  * Copyright (c) 2004 Jeffrey M. Hsu.  All rights reserved.
5  * Copyright (c) 2004 The DragonFly Project.  All rights reserved.
6  *
7  * This code is derived from software contributed to The DragonFly Project
8  * by Jeffrey M. Hsu.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of The DragonFly Project nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific, prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 /*
37  * Copyright (c) 1982, 1986, 1988, 1991, 1993
38  *	The Regents of the University of California.  All rights reserved.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  * 3. All advertising materials mentioning features or use of this software
49  *    must display the following acknowledgement:
50  *	This product includes software developed by the University of
51  *	California, Berkeley and its contributors.
52  * 4. Neither the name of the University nor the names of its contributors
53  *    may be used to endorse or promote products derived from this software
54  *    without specific prior written permission.
55  *
56  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66  * SUCH DAMAGE.
67  *
68  * @(#)uipc_mbuf.c	8.2 (Berkeley) 1/4/94
69  * $FreeBSD: src/sys/kern/uipc_mbuf.c,v 1.51.2.24 2003/04/15 06:59:29 silby Exp $
70  * $DragonFly: src/sys/kern/uipc_mbuf.c,v 1.70 2008/11/20 14:21:01 sephe Exp $
71  */
72 
73 #include "opt_param.h"
74 #include "opt_mbuf_stress_test.h"
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/malloc.h>
78 #include <sys/mbuf.h>
79 #include <sys/kernel.h>
80 #include <sys/sysctl.h>
81 #include <sys/domain.h>
82 #include <sys/objcache.h>
83 #include <sys/tree.h>
84 #include <sys/protosw.h>
85 #include <sys/uio.h>
86 #include <sys/thread.h>
87 #include <sys/globaldata.h>
88 
89 #include <sys/thread2.h>
90 #include <sys/spinlock2.h>
91 
92 #include <machine/atomic.h>
93 #include <machine/limits.h>
94 
95 #include <vm/vm.h>
96 #include <vm/vm_kern.h>
97 #include <vm/vm_extern.h>
98 
99 #ifdef INVARIANTS
100 #include <machine/cpu.h>
101 #endif
102 
103 /*
104  * mbuf cluster meta-data
105  */
106 struct mbcluster {
107 	int32_t	mcl_refs;
108 	void	*mcl_data;
109 };
110 
111 /*
112  * mbuf tracking for debugging purposes
113  */
114 #ifdef MBUF_DEBUG
115 
116 static MALLOC_DEFINE(M_MTRACK, "mtrack", "mtrack");
117 
118 struct mbctrack;
119 RB_HEAD(mbuf_rb_tree, mbtrack);
120 RB_PROTOTYPE2(mbuf_rb_tree, mbtrack, rb_node, mbtrack_cmp, struct mbuf *);
121 
122 struct mbtrack {
123 	RB_ENTRY(mbtrack) rb_node;
124 	int trackid;
125 	struct mbuf *m;
126 };
127 
128 static int
129 mbtrack_cmp(struct mbtrack *mb1, struct mbtrack *mb2)
130 {
131 	if (mb1->m < mb2->m)
132 		return(-1);
133 	if (mb1->m > mb2->m)
134 		return(1);
135 	return(0);
136 }
137 
138 RB_GENERATE2(mbuf_rb_tree, mbtrack, rb_node, mbtrack_cmp, struct mbuf *, m);
139 
140 struct mbuf_rb_tree	mbuf_track_root;
141 static struct spinlock	mbuf_track_spin = SPINLOCK_INITIALIZER(mbuf_track_spin);
142 
143 static void
144 mbuftrack(struct mbuf *m)
145 {
146 	struct mbtrack *mbt;
147 
148 	mbt = kmalloc(sizeof(*mbt), M_MTRACK, M_INTWAIT|M_ZERO);
149 	spin_lock(&mbuf_track_spin);
150 	mbt->m = m;
151 	if (mbuf_rb_tree_RB_INSERT(&mbuf_track_root, mbt)) {
152 		spin_unlock(&mbuf_track_spin);
153 		panic("mbuftrack: mbuf %p already being tracked\n", m);
154 	}
155 	spin_unlock(&mbuf_track_spin);
156 }
157 
158 static void
159 mbufuntrack(struct mbuf *m)
160 {
161 	struct mbtrack *mbt;
162 
163 	spin_lock(&mbuf_track_spin);
164 	mbt = mbuf_rb_tree_RB_LOOKUP(&mbuf_track_root, m);
165 	if (mbt == NULL) {
166 		spin_unlock(&mbuf_track_spin);
167 		panic("mbufuntrack: mbuf %p was not tracked\n", m);
168 	} else {
169 		mbuf_rb_tree_RB_REMOVE(&mbuf_track_root, mbt);
170 		spin_unlock(&mbuf_track_spin);
171 		kfree(mbt, M_MTRACK);
172 	}
173 }
174 
175 void
176 mbuftrackid(struct mbuf *m, int trackid)
177 {
178 	struct mbtrack *mbt;
179 	struct mbuf *n;
180 
181 	spin_lock(&mbuf_track_spin);
182 	while (m) {
183 		n = m->m_nextpkt;
184 		while (m) {
185 			mbt = mbuf_rb_tree_RB_LOOKUP(&mbuf_track_root, m);
186 			if (mbt == NULL) {
187 				spin_unlock(&mbuf_track_spin);
188 				panic("mbuftrackid: mbuf %p not tracked", m);
189 			}
190 			mbt->trackid = trackid;
191 			m = m->m_next;
192 		}
193 		m = n;
194 	}
195 	spin_unlock(&mbuf_track_spin);
196 }
197 
198 static int
199 mbuftrack_callback(struct mbtrack *mbt, void *arg)
200 {
201 	struct sysctl_req *req = arg;
202 	char buf[64];
203 	int error;
204 
205 	ksnprintf(buf, sizeof(buf), "mbuf %p track %d\n", mbt->m, mbt->trackid);
206 
207 	spin_unlock(&mbuf_track_spin);
208 	error = SYSCTL_OUT(req, buf, strlen(buf));
209 	spin_lock(&mbuf_track_spin);
210 	if (error)
211 		return(-error);
212 	return(0);
213 }
214 
215 static int
216 mbuftrack_show(SYSCTL_HANDLER_ARGS)
217 {
218 	int error;
219 
220 	spin_lock(&mbuf_track_spin);
221 	error = mbuf_rb_tree_RB_SCAN(&mbuf_track_root, NULL,
222 				     mbuftrack_callback, req);
223 	spin_unlock(&mbuf_track_spin);
224 	return (-error);
225 }
226 SYSCTL_PROC(_kern_ipc, OID_AUTO, showmbufs, CTLFLAG_RD|CTLTYPE_STRING,
227 	    0, 0, mbuftrack_show, "A", "Show all in-use mbufs");
228 
229 #else
230 
231 #define mbuftrack(m)
232 #define mbufuntrack(m)
233 
234 #endif
235 
236 static void mbinit(void *);
237 SYSINIT(mbuf, SI_BOOT2_MACHDEP, SI_ORDER_FIRST, mbinit, NULL)
238 
239 static u_long	mbtypes[SMP_MAXCPU][MT_NTYPES];
240 
241 static struct mbstat mbstat[SMP_MAXCPU];
242 int	max_linkhdr;
243 int	max_protohdr;
244 int	max_hdr;
245 int	max_datalen;
246 int	m_defragpackets;
247 int	m_defragbytes;
248 int	m_defraguseless;
249 int	m_defragfailure;
250 #ifdef MBUF_STRESS_TEST
251 int	m_defragrandomfailures;
252 #endif
253 
254 struct objcache *mbuf_cache, *mbufphdr_cache;
255 struct objcache *mclmeta_cache;
256 struct objcache *mbufcluster_cache, *mbufphdrcluster_cache;
257 
258 int	nmbclusters;
259 int	nmbufs;
260 
261 SYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RW,
262 	   &max_linkhdr, 0, "");
263 SYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RW,
264 	   &max_protohdr, 0, "");
265 SYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RW, &max_hdr, 0, "");
266 SYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RW,
267 	   &max_datalen, 0, "");
268 SYSCTL_INT(_kern_ipc, OID_AUTO, mbuf_wait, CTLFLAG_RW,
269 	   &mbuf_wait, 0, "");
270 static int do_mbstat(SYSCTL_HANDLER_ARGS);
271 
272 SYSCTL_PROC(_kern_ipc, KIPC_MBSTAT, mbstat, CTLTYPE_STRUCT|CTLFLAG_RD,
273 	0, 0, do_mbstat, "S,mbstat", "");
274 
275 static int do_mbtypes(SYSCTL_HANDLER_ARGS);
276 
277 SYSCTL_PROC(_kern_ipc, OID_AUTO, mbtypes, CTLTYPE_ULONG|CTLFLAG_RD,
278 	0, 0, do_mbtypes, "LU", "");
279 
280 static int
281 do_mbstat(SYSCTL_HANDLER_ARGS)
282 {
283 	struct mbstat mbstat_total;
284 	struct mbstat *mbstat_totalp;
285 	int i;
286 
287 	bzero(&mbstat_total, sizeof(mbstat_total));
288 	mbstat_totalp = &mbstat_total;
289 
290 	for (i = 0; i < ncpus; i++)
291 	{
292 		mbstat_total.m_mbufs += mbstat[i].m_mbufs;
293 		mbstat_total.m_clusters += mbstat[i].m_clusters;
294 		mbstat_total.m_spare += mbstat[i].m_spare;
295 		mbstat_total.m_clfree += mbstat[i].m_clfree;
296 		mbstat_total.m_drops += mbstat[i].m_drops;
297 		mbstat_total.m_wait += mbstat[i].m_wait;
298 		mbstat_total.m_drain += mbstat[i].m_drain;
299 		mbstat_total.m_mcfail += mbstat[i].m_mcfail;
300 		mbstat_total.m_mpfail += mbstat[i].m_mpfail;
301 
302 	}
303 	/*
304 	 * The following fields are not cumulative fields so just
305 	 * get their values once.
306 	 */
307 	mbstat_total.m_msize = mbstat[0].m_msize;
308 	mbstat_total.m_mclbytes = mbstat[0].m_mclbytes;
309 	mbstat_total.m_minclsize = mbstat[0].m_minclsize;
310 	mbstat_total.m_mlen = mbstat[0].m_mlen;
311 	mbstat_total.m_mhlen = mbstat[0].m_mhlen;
312 
313 	return(sysctl_handle_opaque(oidp, mbstat_totalp, sizeof(mbstat_total), req));
314 }
315 
316 static int
317 do_mbtypes(SYSCTL_HANDLER_ARGS)
318 {
319 	u_long totals[MT_NTYPES];
320 	int i, j;
321 
322 	for (i = 0; i < MT_NTYPES; i++)
323 		totals[i] = 0;
324 
325 	for (i = 0; i < ncpus; i++)
326 	{
327 		for (j = 0; j < MT_NTYPES; j++)
328 			totals[j] += mbtypes[i][j];
329 	}
330 
331 	return(sysctl_handle_opaque(oidp, totals, sizeof(totals), req));
332 }
333 
334 /*
335  * These are read-only because we do not currently have any code
336  * to adjust the objcache limits after the fact.  The variables
337  * may only be set as boot-time tunables.
338  */
339 SYSCTL_INT(_kern_ipc, KIPC_NMBCLUSTERS, nmbclusters, CTLFLAG_RD,
340 	   &nmbclusters, 0, "Maximum number of mbuf clusters available");
341 SYSCTL_INT(_kern_ipc, OID_AUTO, nmbufs, CTLFLAG_RD, &nmbufs, 0,
342 	   "Maximum number of mbufs available");
343 
344 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragpackets, CTLFLAG_RD,
345 	   &m_defragpackets, 0, "");
346 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragbytes, CTLFLAG_RD,
347 	   &m_defragbytes, 0, "");
348 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defraguseless, CTLFLAG_RD,
349 	   &m_defraguseless, 0, "");
350 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragfailure, CTLFLAG_RD,
351 	   &m_defragfailure, 0, "");
352 #ifdef MBUF_STRESS_TEST
353 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragrandomfailures, CTLFLAG_RW,
354 	   &m_defragrandomfailures, 0, "");
355 #endif
356 
357 static MALLOC_DEFINE(M_MBUF, "mbuf", "mbuf");
358 static MALLOC_DEFINE(M_MBUFCL, "mbufcl", "mbufcl");
359 static MALLOC_DEFINE(M_MCLMETA, "mclmeta", "mclmeta");
360 
361 static void m_reclaim (void);
362 static void m_mclref(void *arg);
363 static void m_mclfree(void *arg);
364 
365 #ifndef NMBCLUSTERS
366 #define NMBCLUSTERS	(512 + maxusers * 16)
367 #endif
368 #ifndef NMBUFS
369 #define NMBUFS		(nmbclusters * 2)
370 #endif
371 
372 /*
373  * Perform sanity checks of tunables declared above.
374  */
375 static void
376 tunable_mbinit(void *dummy)
377 {
378 	/*
379 	 * This has to be done before VM init.
380 	 */
381 	nmbclusters = NMBCLUSTERS;
382 	TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters);
383 	nmbufs = NMBUFS;
384 	TUNABLE_INT_FETCH("kern.ipc.nmbufs", &nmbufs);
385 	/* Sanity checks */
386 	if (nmbufs < nmbclusters * 2)
387 		nmbufs = nmbclusters * 2;
388 }
389 SYSINIT(tunable_mbinit, SI_BOOT1_TUNABLES, SI_ORDER_ANY,
390 	tunable_mbinit, NULL);
391 
392 /* "number of clusters of pages" */
393 #define NCL_INIT	1
394 
395 #define NMB_INIT	16
396 
397 /*
398  * The mbuf object cache only guarantees that m_next and m_nextpkt are
399  * NULL and that m_data points to the beginning of the data area.  In
400  * particular, m_len and m_pkthdr.len are uninitialized.  It is the
401  * responsibility of the caller to initialize those fields before use.
402  */
403 
404 static boolean_t __inline
405 mbuf_ctor(void *obj, void *private, int ocflags)
406 {
407 	struct mbuf *m = obj;
408 
409 	m->m_next = NULL;
410 	m->m_nextpkt = NULL;
411 	m->m_data = m->m_dat;
412 	m->m_flags = 0;
413 
414 	return (TRUE);
415 }
416 
417 /*
418  * Initialize the mbuf and the packet header fields.
419  */
420 static boolean_t
421 mbufphdr_ctor(void *obj, void *private, int ocflags)
422 {
423 	struct mbuf *m = obj;
424 
425 	m->m_next = NULL;
426 	m->m_nextpkt = NULL;
427 	m->m_data = m->m_pktdat;
428 	m->m_flags = M_PKTHDR | M_PHCACHE;
429 
430 	m->m_pkthdr.rcvif = NULL;	/* eliminate XXX JH */
431 	SLIST_INIT(&m->m_pkthdr.tags);
432 	m->m_pkthdr.csum_flags = 0;	/* eliminate XXX JH */
433 	m->m_pkthdr.fw_flags = 0;	/* eliminate XXX JH */
434 
435 	return (TRUE);
436 }
437 
438 /*
439  * A mbcluster object consists of 2K (MCLBYTES) cluster and a refcount.
440  */
441 static boolean_t
442 mclmeta_ctor(void *obj, void *private, int ocflags)
443 {
444 	struct mbcluster *cl = obj;
445 	void *buf;
446 
447 	if (ocflags & M_NOWAIT)
448 		buf = kmalloc(MCLBYTES, M_MBUFCL, M_NOWAIT | M_ZERO);
449 	else
450 		buf = kmalloc(MCLBYTES, M_MBUFCL, M_INTWAIT | M_ZERO);
451 	if (buf == NULL)
452 		return (FALSE);
453 	cl->mcl_refs = 0;
454 	cl->mcl_data = buf;
455 	return (TRUE);
456 }
457 
458 static void
459 mclmeta_dtor(void *obj, void *private)
460 {
461 	struct mbcluster *mcl = obj;
462 
463 	KKASSERT(mcl->mcl_refs == 0);
464 	kfree(mcl->mcl_data, M_MBUFCL);
465 }
466 
467 static void
468 linkcluster(struct mbuf *m, struct mbcluster *cl)
469 {
470 	/*
471 	 * Add the cluster to the mbuf.  The caller will detect that the
472 	 * mbuf now has an attached cluster.
473 	 */
474 	m->m_ext.ext_arg = cl;
475 	m->m_ext.ext_buf = cl->mcl_data;
476 	m->m_ext.ext_ref = m_mclref;
477 	m->m_ext.ext_free = m_mclfree;
478 	m->m_ext.ext_size = MCLBYTES;
479 	atomic_add_int(&cl->mcl_refs, 1);
480 
481 	m->m_data = m->m_ext.ext_buf;
482 	m->m_flags |= M_EXT | M_EXT_CLUSTER;
483 }
484 
485 static boolean_t
486 mbufphdrcluster_ctor(void *obj, void *private, int ocflags)
487 {
488 	struct mbuf *m = obj;
489 	struct mbcluster *cl;
490 
491 	mbufphdr_ctor(obj, private, ocflags);
492 	cl = objcache_get(mclmeta_cache, ocflags);
493 	if (cl == NULL) {
494 		++mbstat[mycpu->gd_cpuid].m_drops;
495 		return (FALSE);
496 	}
497 	m->m_flags |= M_CLCACHE;
498 	linkcluster(m, cl);
499 	return (TRUE);
500 }
501 
502 static boolean_t
503 mbufcluster_ctor(void *obj, void *private, int ocflags)
504 {
505 	struct mbuf *m = obj;
506 	struct mbcluster *cl;
507 
508 	mbuf_ctor(obj, private, ocflags);
509 	cl = objcache_get(mclmeta_cache, ocflags);
510 	if (cl == NULL) {
511 		++mbstat[mycpu->gd_cpuid].m_drops;
512 		return (FALSE);
513 	}
514 	m->m_flags |= M_CLCACHE;
515 	linkcluster(m, cl);
516 	return (TRUE);
517 }
518 
519 /*
520  * Used for both the cluster and cluster PHDR caches.
521  *
522  * The mbuf may have lost its cluster due to sharing, deal
523  * with the situation by checking M_EXT.
524  */
525 static void
526 mbufcluster_dtor(void *obj, void *private)
527 {
528 	struct mbuf *m = obj;
529 	struct mbcluster *mcl;
530 
531 	if (m->m_flags & M_EXT) {
532 		KKASSERT((m->m_flags & M_EXT_CLUSTER) != 0);
533 		mcl = m->m_ext.ext_arg;
534 		KKASSERT(mcl->mcl_refs == 1);
535 		mcl->mcl_refs = 0;
536 		objcache_put(mclmeta_cache, mcl);
537 	}
538 }
539 
540 struct objcache_malloc_args mbuf_malloc_args = { MSIZE, M_MBUF };
541 struct objcache_malloc_args mclmeta_malloc_args =
542 	{ sizeof(struct mbcluster), M_MCLMETA };
543 
544 /* ARGSUSED*/
545 static void
546 mbinit(void *dummy)
547 {
548 	int mb_limit, cl_limit;
549 	int limit;
550 	int i;
551 
552 	/*
553 	 * Initialize statistics
554 	 */
555 	for (i = 0; i < ncpus; i++) {
556 		atomic_set_long_nonlocked(&mbstat[i].m_msize, MSIZE);
557 		atomic_set_long_nonlocked(&mbstat[i].m_mclbytes, MCLBYTES);
558 		atomic_set_long_nonlocked(&mbstat[i].m_minclsize, MINCLSIZE);
559 		atomic_set_long_nonlocked(&mbstat[i].m_mlen, MLEN);
560 		atomic_set_long_nonlocked(&mbstat[i].m_mhlen, MHLEN);
561 	}
562 
563 	/*
564 	 * Create objtect caches and save cluster limits, which will
565 	 * be used to adjust backing kmalloc pools' limit later.
566 	 */
567 
568 	mb_limit = cl_limit = 0;
569 
570 	limit = nmbufs;
571 	mbuf_cache = objcache_create("mbuf", &limit, 0,
572 	    mbuf_ctor, NULL, NULL,
573 	    objcache_malloc_alloc, objcache_malloc_free, &mbuf_malloc_args);
574 	mb_limit += limit;
575 
576 	limit = nmbufs;
577 	mbufphdr_cache = objcache_create("mbuf pkt hdr", &limit, 64,
578 	    mbufphdr_ctor, NULL, NULL,
579 	    objcache_malloc_alloc, objcache_malloc_free, &mbuf_malloc_args);
580 	mb_limit += limit;
581 
582 	cl_limit = nmbclusters;
583 	mclmeta_cache = objcache_create("cluster mbuf", &cl_limit, 0,
584 	    mclmeta_ctor, mclmeta_dtor, NULL,
585 	    objcache_malloc_alloc, objcache_malloc_free, &mclmeta_malloc_args);
586 
587 	limit = nmbclusters;
588 	mbufcluster_cache = objcache_create("mbuf + cluster", &limit, 0,
589 	    mbufcluster_ctor, mbufcluster_dtor, NULL,
590 	    objcache_malloc_alloc, objcache_malloc_free, &mbuf_malloc_args);
591 	mb_limit += limit;
592 
593 	limit = nmbclusters;
594 	mbufphdrcluster_cache = objcache_create("mbuf pkt hdr + cluster",
595 	    &limit, 64, mbufphdrcluster_ctor, mbufcluster_dtor, NULL,
596 	    objcache_malloc_alloc, objcache_malloc_free, &mbuf_malloc_args);
597 	mb_limit += limit;
598 
599 	/*
600 	 * Adjust backing kmalloc pools' limit
601 	 *
602 	 * NOTE: We raise the limit by another 1/8 to take the effect
603 	 * of loosememuse into account.
604 	 */
605 	cl_limit += cl_limit / 8;
606 	kmalloc_raise_limit(mclmeta_malloc_args.mtype,
607 			    mclmeta_malloc_args.objsize * cl_limit);
608 	kmalloc_raise_limit(M_MBUFCL, MCLBYTES * cl_limit);
609 
610 	mb_limit += mb_limit / 8;
611 	kmalloc_raise_limit(mbuf_malloc_args.mtype,
612 			    mbuf_malloc_args.objsize * mb_limit);
613 }
614 
615 /*
616  * Return the number of references to this mbuf's data.  0 is returned
617  * if the mbuf is not M_EXT, a reference count is returned if it is
618  * M_EXT | M_EXT_CLUSTER, and 99 is returned if it is a special M_EXT.
619  */
620 int
621 m_sharecount(struct mbuf *m)
622 {
623 	switch (m->m_flags & (M_EXT | M_EXT_CLUSTER)) {
624 	case 0:
625 		return (0);
626 	case M_EXT:
627 		return (99);
628 	case M_EXT | M_EXT_CLUSTER:
629 		return (((struct mbcluster *)m->m_ext.ext_arg)->mcl_refs);
630 	}
631 	/* NOTREACHED */
632 	return (0);		/* to shut up compiler */
633 }
634 
635 /*
636  * change mbuf to new type
637  */
638 void
639 m_chtype(struct mbuf *m, int type)
640 {
641 	struct globaldata *gd = mycpu;
642 
643 	atomic_add_long_nonlocked(&mbtypes[gd->gd_cpuid][type], 1);
644 	atomic_subtract_long_nonlocked(&mbtypes[gd->gd_cpuid][m->m_type], 1);
645 	atomic_set_short_nonlocked(&m->m_type, type);
646 }
647 
648 static void
649 m_reclaim(void)
650 {
651 	struct domain *dp;
652 	struct protosw *pr;
653 
654 	kprintf("Debug: m_reclaim() called\n");
655 
656 	SLIST_FOREACH(dp, &domains, dom_next) {
657 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
658 			if (pr->pr_drain)
659 				(*pr->pr_drain)();
660 		}
661 	}
662 	atomic_add_long_nonlocked(&mbstat[mycpu->gd_cpuid].m_drain, 1);
663 }
664 
665 static void __inline
666 updatestats(struct mbuf *m, int type)
667 {
668 	struct globaldata *gd = mycpu;
669 	m->m_type = type;
670 
671 	mbuftrack(m);
672 
673 	atomic_add_long_nonlocked(&mbtypes[gd->gd_cpuid][type], 1);
674 	atomic_add_long_nonlocked(&mbstat[mycpu->gd_cpuid].m_mbufs, 1);
675 
676 }
677 
678 /*
679  * Allocate an mbuf.
680  */
681 struct mbuf *
682 m_get(int how, int type)
683 {
684 	struct mbuf *m;
685 	int ntries = 0;
686 	int ocf = MBTOM(how);
687 
688 retryonce:
689 
690 	m = objcache_get(mbuf_cache, ocf);
691 
692 	if (m == NULL) {
693 		if ((how & MB_TRYWAIT) && ntries++ == 0) {
694 			struct objcache *reclaimlist[] = {
695 				mbufphdr_cache,
696 				mbufcluster_cache,
697 				mbufphdrcluster_cache
698 			};
699 			const int nreclaims = __arysize(reclaimlist);
700 
701 			if (!objcache_reclaimlist(reclaimlist, nreclaims, ocf))
702 				m_reclaim();
703 			goto retryonce;
704 		}
705 		++mbstat[mycpu->gd_cpuid].m_drops;
706 		return (NULL);
707 	}
708 
709 	updatestats(m, type);
710 	return (m);
711 }
712 
713 struct mbuf *
714 m_gethdr(int how, int type)
715 {
716 	struct mbuf *m;
717 	int ocf = MBTOM(how);
718 	int ntries = 0;
719 
720 retryonce:
721 
722 	m = objcache_get(mbufphdr_cache, ocf);
723 
724 	if (m == NULL) {
725 		if ((how & MB_TRYWAIT) && ntries++ == 0) {
726 			struct objcache *reclaimlist[] = {
727 				mbuf_cache,
728 				mbufcluster_cache, mbufphdrcluster_cache
729 			};
730 			const int nreclaims = __arysize(reclaimlist);
731 
732 			if (!objcache_reclaimlist(reclaimlist, nreclaims, ocf))
733 				m_reclaim();
734 			goto retryonce;
735 		}
736 		++mbstat[mycpu->gd_cpuid].m_drops;
737 		return (NULL);
738 	}
739 
740 	updatestats(m, type);
741 	return (m);
742 }
743 
744 /*
745  * Get a mbuf (not a mbuf cluster!) and zero it.
746  * Deprecated.
747  */
748 struct mbuf *
749 m_getclr(int how, int type)
750 {
751 	struct mbuf *m;
752 
753 	m = m_get(how, type);
754 	if (m != NULL)
755 		bzero(m->m_data, MLEN);
756 	return (m);
757 }
758 
759 /*
760  * Returns an mbuf with an attached cluster.
761  * Because many network drivers use this kind of buffers a lot, it is
762  * convenient to keep a small pool of free buffers of this kind.
763  * Even a small size such as 10 gives about 10% improvement in the
764  * forwarding rate in a bridge or router.
765  */
766 struct mbuf *
767 m_getcl(int how, short type, int flags)
768 {
769 	struct mbuf *m;
770 	int ocflags = MBTOM(how);
771 	int ntries = 0;
772 
773 retryonce:
774 
775 	if (flags & M_PKTHDR)
776 		m = objcache_get(mbufphdrcluster_cache, ocflags);
777 	else
778 		m = objcache_get(mbufcluster_cache, ocflags);
779 
780 	if (m == NULL) {
781 		if ((how & MB_TRYWAIT) && ntries++ == 0) {
782 			struct objcache *reclaimlist[1];
783 
784 			if (flags & M_PKTHDR)
785 				reclaimlist[0] = mbufcluster_cache;
786 			else
787 				reclaimlist[0] = mbufphdrcluster_cache;
788 			if (!objcache_reclaimlist(reclaimlist, 1, ocflags))
789 				m_reclaim();
790 			goto retryonce;
791 		}
792 		++mbstat[mycpu->gd_cpuid].m_drops;
793 		return (NULL);
794 	}
795 
796 	m->m_type = type;
797 
798 	mbuftrack(m);
799 
800 	atomic_add_long_nonlocked(&mbtypes[mycpu->gd_cpuid][type], 1);
801 	atomic_add_long_nonlocked(&mbstat[mycpu->gd_cpuid].m_clusters, 1);
802 	return (m);
803 }
804 
805 /*
806  * Allocate chain of requested length.
807  */
808 struct mbuf *
809 m_getc(int len, int how, int type)
810 {
811 	struct mbuf *n, *nfirst = NULL, **ntail = &nfirst;
812 	int nsize;
813 
814 	while (len > 0) {
815 		n = m_getl(len, how, type, 0, &nsize);
816 		if (n == NULL)
817 			goto failed;
818 		n->m_len = 0;
819 		*ntail = n;
820 		ntail = &n->m_next;
821 		len -= nsize;
822 	}
823 	return (nfirst);
824 
825 failed:
826 	m_freem(nfirst);
827 	return (NULL);
828 }
829 
830 /*
831  * Allocate len-worth of mbufs and/or mbuf clusters (whatever fits best)
832  * and return a pointer to the head of the allocated chain. If m0 is
833  * non-null, then we assume that it is a single mbuf or an mbuf chain to
834  * which we want len bytes worth of mbufs and/or clusters attached, and so
835  * if we succeed in allocating it, we will just return a pointer to m0.
836  *
837  * If we happen to fail at any point during the allocation, we will free
838  * up everything we have already allocated and return NULL.
839  *
840  * Deprecated.  Use m_getc() and m_cat() instead.
841  */
842 struct mbuf *
843 m_getm(struct mbuf *m0, int len, int type, int how)
844 {
845 	struct mbuf *nfirst;
846 
847 	nfirst = m_getc(len, how, type);
848 
849 	if (m0 != NULL) {
850 		m_last(m0)->m_next = nfirst;
851 		return (m0);
852 	}
853 
854 	return (nfirst);
855 }
856 
857 /*
858  * Adds a cluster to a normal mbuf, M_EXT is set on success.
859  * Deprecated.  Use m_getcl() instead.
860  */
861 void
862 m_mclget(struct mbuf *m, int how)
863 {
864 	struct mbcluster *mcl;
865 
866 	KKASSERT((m->m_flags & M_EXT) == 0);
867 	mcl = objcache_get(mclmeta_cache, MBTOM(how));
868 	if (mcl != NULL) {
869 		linkcluster(m, mcl);
870 		atomic_add_long_nonlocked(&mbstat[mycpu->gd_cpuid].m_clusters,
871 					  1);
872 	} else {
873 		++mbstat[mycpu->gd_cpuid].m_drops;
874 	}
875 }
876 
877 /*
878  * Updates to mbcluster must be MPSAFE.  Only an entity which already has
879  * a reference to the cluster can ref it, so we are in no danger of
880  * racing an add with a subtract.  But the operation must still be atomic
881  * since multiple entities may have a reference on the cluster.
882  *
883  * m_mclfree() is almost the same but it must contend with two entities
884  * freeing the cluster at the same time.
885  */
886 static void
887 m_mclref(void *arg)
888 {
889 	struct mbcluster *mcl = arg;
890 
891 	atomic_add_int(&mcl->mcl_refs, 1);
892 }
893 
894 /*
895  * When dereferencing a cluster we have to deal with a N->0 race, where
896  * N entities free their references simultaniously.  To do this we use
897  * atomic_fetchadd_int().
898  */
899 static void
900 m_mclfree(void *arg)
901 {
902 	struct mbcluster *mcl = arg;
903 
904 	if (atomic_fetchadd_int(&mcl->mcl_refs, -1) == 1)
905 		objcache_put(mclmeta_cache, mcl);
906 }
907 
908 /*
909  * Free a single mbuf and any associated external storage.  The successor,
910  * if any, is returned.
911  *
912  * We do need to check non-first mbuf for m_aux, since some of existing
913  * code does not call M_PREPEND properly.
914  * (example: call to bpf_mtap from drivers)
915  */
916 struct mbuf *
917 m_free(struct mbuf *m)
918 {
919 	struct mbuf *n;
920 	struct globaldata *gd = mycpu;
921 
922 	KASSERT(m->m_type != MT_FREE, ("freeing free mbuf %p", m));
923 	atomic_subtract_long_nonlocked(&mbtypes[gd->gd_cpuid][m->m_type], 1);
924 
925 	n = m->m_next;
926 
927 	/*
928 	 * Make sure the mbuf is in constructed state before returning it
929 	 * to the objcache.
930 	 */
931 	m->m_next = NULL;
932 	mbufuntrack(m);
933 #ifdef notyet
934 	KKASSERT(m->m_nextpkt == NULL);
935 #else
936 	if (m->m_nextpkt != NULL) {
937 		static int afewtimes = 10;
938 
939 		if (afewtimes-- > 0) {
940 			kprintf("mfree: m->m_nextpkt != NULL\n");
941 			print_backtrace(-1);
942 		}
943 		m->m_nextpkt = NULL;
944 	}
945 #endif
946 	if (m->m_flags & M_PKTHDR) {
947 		m_tag_delete_chain(m);		/* eliminate XXX JH */
948 	}
949 
950 	m->m_flags &= (M_EXT | M_EXT_CLUSTER | M_CLCACHE | M_PHCACHE);
951 
952 	/*
953 	 * Clean the M_PKTHDR state so we can return the mbuf to its original
954 	 * cache.  This is based on the PHCACHE flag which tells us whether
955 	 * the mbuf was originally allocated out of a packet-header cache
956 	 * or a non-packet-header cache.
957 	 */
958 	if (m->m_flags & M_PHCACHE) {
959 		m->m_flags |= M_PKTHDR;
960 		m->m_pkthdr.rcvif = NULL;	/* eliminate XXX JH */
961 		m->m_pkthdr.csum_flags = 0;	/* eliminate XXX JH */
962 		m->m_pkthdr.fw_flags = 0;	/* eliminate XXX JH */
963 		SLIST_INIT(&m->m_pkthdr.tags);
964 	}
965 
966 	/*
967 	 * Handle remaining flags combinations.  M_CLCACHE tells us whether
968 	 * the mbuf was originally allocated from a cluster cache or not,
969 	 * and is totally separate from whether the mbuf is currently
970 	 * associated with a cluster.
971 	 */
972 	switch(m->m_flags & (M_CLCACHE | M_EXT | M_EXT_CLUSTER)) {
973 	case M_CLCACHE | M_EXT | M_EXT_CLUSTER:
974 		/*
975 		 * mbuf+cluster cache case.  The mbuf was allocated from the
976 		 * combined mbuf_cluster cache and can be returned to the
977 		 * cache if the cluster hasn't been shared.
978 		 */
979 		if (m_sharecount(m) == 1) {
980 			/*
981 			 * The cluster has not been shared, we can just
982 			 * reset the data pointer and return the mbuf
983 			 * to the cluster cache.  Note that the reference
984 			 * count is left intact (it is still associated with
985 			 * an mbuf).
986 			 */
987 			m->m_data = m->m_ext.ext_buf;
988 			if (m->m_flags & M_PHCACHE)
989 				objcache_put(mbufphdrcluster_cache, m);
990 			else
991 				objcache_put(mbufcluster_cache, m);
992 			atomic_subtract_long_nonlocked(&mbstat[mycpu->gd_cpuid].m_clusters, 1);
993 		} else {
994 			/*
995 			 * Hell.  Someone else has a ref on this cluster,
996 			 * we have to disconnect it which means we can't
997 			 * put it back into the mbufcluster_cache, we
998 			 * have to destroy the mbuf.
999 			 *
1000 			 * Other mbuf references to the cluster will typically
1001 			 * be M_EXT | M_EXT_CLUSTER but without M_CLCACHE.
1002 			 *
1003 			 * XXX we could try to connect another cluster to
1004 			 * it.
1005 			 */
1006 			m->m_ext.ext_free(m->m_ext.ext_arg);
1007 			m->m_flags &= ~(M_EXT | M_EXT_CLUSTER);
1008 			if (m->m_flags & M_PHCACHE)
1009 				objcache_dtor(mbufphdrcluster_cache, m);
1010 			else
1011 				objcache_dtor(mbufcluster_cache, m);
1012 		}
1013 		break;
1014 	case M_EXT | M_EXT_CLUSTER:
1015 		/*
1016 		 * Normal cluster associated with an mbuf that was allocated
1017 		 * from the normal mbuf pool rather then the cluster pool.
1018 		 * The cluster has to be independantly disassociated from the
1019 		 * mbuf.
1020 		 */
1021 		if (m_sharecount(m) == 1)
1022 			atomic_subtract_long_nonlocked(&mbstat[mycpu->gd_cpuid].m_clusters, 1);
1023 		/* fall through */
1024 	case M_EXT:
1025 		/*
1026 		 * Normal cluster association case, disconnect the cluster from
1027 		 * the mbuf.  The cluster may or may not be custom.
1028 		 */
1029 		m->m_ext.ext_free(m->m_ext.ext_arg);
1030 		m->m_flags &= ~(M_EXT | M_EXT_CLUSTER);
1031 		/* fall through */
1032 	case 0:
1033 		/*
1034 		 * return the mbuf to the mbuf cache.
1035 		 */
1036 		if (m->m_flags & M_PHCACHE) {
1037 			m->m_data = m->m_pktdat;
1038 			objcache_put(mbufphdr_cache, m);
1039 		} else {
1040 			m->m_data = m->m_dat;
1041 			objcache_put(mbuf_cache, m);
1042 		}
1043 		atomic_subtract_long_nonlocked(&mbstat[mycpu->gd_cpuid].m_mbufs, 1);
1044 		break;
1045 	default:
1046 		if (!panicstr)
1047 			panic("bad mbuf flags %p %08x\n", m, m->m_flags);
1048 		break;
1049 	}
1050 	return (n);
1051 }
1052 
1053 void
1054 m_freem(struct mbuf *m)
1055 {
1056 	while (m)
1057 		m = m_free(m);
1058 }
1059 
1060 /*
1061  * mbuf utility routines
1062  */
1063 
1064 /*
1065  * Lesser-used path for M_PREPEND: allocate new mbuf to prepend to chain and
1066  * copy junk along.
1067  */
1068 struct mbuf *
1069 m_prepend(struct mbuf *m, int len, int how)
1070 {
1071 	struct mbuf *mn;
1072 
1073 	if (m->m_flags & M_PKTHDR)
1074 	    mn = m_gethdr(how, m->m_type);
1075 	else
1076 	    mn = m_get(how, m->m_type);
1077 	if (mn == NULL) {
1078 		m_freem(m);
1079 		return (NULL);
1080 	}
1081 	if (m->m_flags & M_PKTHDR)
1082 		M_MOVE_PKTHDR(mn, m);
1083 	mn->m_next = m;
1084 	m = mn;
1085 	if (len < MHLEN)
1086 		MH_ALIGN(m, len);
1087 	m->m_len = len;
1088 	return (m);
1089 }
1090 
1091 /*
1092  * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
1093  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
1094  * The wait parameter is a choice of MB_WAIT/MB_DONTWAIT from caller.
1095  * Note that the copy is read-only, because clusters are not copied,
1096  * only their reference counts are incremented.
1097  */
1098 struct mbuf *
1099 m_copym(const struct mbuf *m, int off0, int len, int wait)
1100 {
1101 	struct mbuf *n, **np;
1102 	int off = off0;
1103 	struct mbuf *top;
1104 	int copyhdr = 0;
1105 
1106 	KASSERT(off >= 0, ("m_copym, negative off %d", off));
1107 	KASSERT(len >= 0, ("m_copym, negative len %d", len));
1108 	if (off == 0 && (m->m_flags & M_PKTHDR))
1109 		copyhdr = 1;
1110 	while (off > 0) {
1111 		KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain"));
1112 		if (off < m->m_len)
1113 			break;
1114 		off -= m->m_len;
1115 		m = m->m_next;
1116 	}
1117 	np = &top;
1118 	top = NULL;
1119 	while (len > 0) {
1120 		if (m == NULL) {
1121 			KASSERT(len == M_COPYALL,
1122 			    ("m_copym, length > size of mbuf chain"));
1123 			break;
1124 		}
1125 		/*
1126 		 * Because we are sharing any cluster attachment below,
1127 		 * be sure to get an mbuf that does not have a cluster
1128 		 * associated with it.
1129 		 */
1130 		if (copyhdr)
1131 			n = m_gethdr(wait, m->m_type);
1132 		else
1133 			n = m_get(wait, m->m_type);
1134 		*np = n;
1135 		if (n == NULL)
1136 			goto nospace;
1137 		if (copyhdr) {
1138 			if (!m_dup_pkthdr(n, m, wait))
1139 				goto nospace;
1140 			if (len == M_COPYALL)
1141 				n->m_pkthdr.len -= off0;
1142 			else
1143 				n->m_pkthdr.len = len;
1144 			copyhdr = 0;
1145 		}
1146 		n->m_len = min(len, m->m_len - off);
1147 		if (m->m_flags & M_EXT) {
1148 			KKASSERT((n->m_flags & M_EXT) == 0);
1149 			n->m_data = m->m_data + off;
1150 			m->m_ext.ext_ref(m->m_ext.ext_arg);
1151 			n->m_ext = m->m_ext;
1152 			n->m_flags |= m->m_flags & (M_EXT | M_EXT_CLUSTER);
1153 		} else {
1154 			bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
1155 			    (unsigned)n->m_len);
1156 		}
1157 		if (len != M_COPYALL)
1158 			len -= n->m_len;
1159 		off = 0;
1160 		m = m->m_next;
1161 		np = &n->m_next;
1162 	}
1163 	if (top == NULL)
1164 		atomic_add_long_nonlocked(&mbstat[mycpu->gd_cpuid].m_mcfail, 1);
1165 	return (top);
1166 nospace:
1167 	m_freem(top);
1168 	atomic_add_long_nonlocked(&mbstat[mycpu->gd_cpuid].m_mcfail, 1);
1169 	return (NULL);
1170 }
1171 
1172 /*
1173  * Copy an entire packet, including header (which must be present).
1174  * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
1175  * Note that the copy is read-only, because clusters are not copied,
1176  * only their reference counts are incremented.
1177  * Preserve alignment of the first mbuf so if the creator has left
1178  * some room at the beginning (e.g. for inserting protocol headers)
1179  * the copies also have the room available.
1180  */
1181 struct mbuf *
1182 m_copypacket(struct mbuf *m, int how)
1183 {
1184 	struct mbuf *top, *n, *o;
1185 
1186 	n = m_gethdr(how, m->m_type);
1187 	top = n;
1188 	if (!n)
1189 		goto nospace;
1190 
1191 	if (!m_dup_pkthdr(n, m, how))
1192 		goto nospace;
1193 	n->m_len = m->m_len;
1194 	if (m->m_flags & M_EXT) {
1195 		KKASSERT((n->m_flags & M_EXT) == 0);
1196 		n->m_data = m->m_data;
1197 		m->m_ext.ext_ref(m->m_ext.ext_arg);
1198 		n->m_ext = m->m_ext;
1199 		n->m_flags |= m->m_flags & (M_EXT | M_EXT_CLUSTER);
1200 	} else {
1201 		n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat );
1202 		bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
1203 	}
1204 
1205 	m = m->m_next;
1206 	while (m) {
1207 		o = m_get(how, m->m_type);
1208 		if (!o)
1209 			goto nospace;
1210 
1211 		n->m_next = o;
1212 		n = n->m_next;
1213 
1214 		n->m_len = m->m_len;
1215 		if (m->m_flags & M_EXT) {
1216 			KKASSERT((n->m_flags & M_EXT) == 0);
1217 			n->m_data = m->m_data;
1218 			m->m_ext.ext_ref(m->m_ext.ext_arg);
1219 			n->m_ext = m->m_ext;
1220 			n->m_flags |= m->m_flags & (M_EXT | M_EXT_CLUSTER);
1221 		} else {
1222 			bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
1223 		}
1224 
1225 		m = m->m_next;
1226 	}
1227 	return top;
1228 nospace:
1229 	m_freem(top);
1230 	atomic_add_long_nonlocked(&mbstat[mycpu->gd_cpuid].m_mcfail, 1);
1231 	return (NULL);
1232 }
1233 
1234 /*
1235  * Copy data from an mbuf chain starting "off" bytes from the beginning,
1236  * continuing for "len" bytes, into the indicated buffer.
1237  */
1238 void
1239 m_copydata(const struct mbuf *m, int off, int len, caddr_t cp)
1240 {
1241 	unsigned count;
1242 
1243 	KASSERT(off >= 0, ("m_copydata, negative off %d", off));
1244 	KASSERT(len >= 0, ("m_copydata, negative len %d", len));
1245 	while (off > 0) {
1246 		KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain"));
1247 		if (off < m->m_len)
1248 			break;
1249 		off -= m->m_len;
1250 		m = m->m_next;
1251 	}
1252 	while (len > 0) {
1253 		KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain"));
1254 		count = min(m->m_len - off, len);
1255 		bcopy(mtod(m, caddr_t) + off, cp, count);
1256 		len -= count;
1257 		cp += count;
1258 		off = 0;
1259 		m = m->m_next;
1260 	}
1261 }
1262 
1263 /*
1264  * Copy a packet header mbuf chain into a completely new chain, including
1265  * copying any mbuf clusters.  Use this instead of m_copypacket() when
1266  * you need a writable copy of an mbuf chain.
1267  */
1268 struct mbuf *
1269 m_dup(struct mbuf *m, int how)
1270 {
1271 	struct mbuf **p, *top = NULL;
1272 	int remain, moff, nsize;
1273 
1274 	/* Sanity check */
1275 	if (m == NULL)
1276 		return (NULL);
1277 	KASSERT((m->m_flags & M_PKTHDR) != 0, ("%s: !PKTHDR", __func__));
1278 
1279 	/* While there's more data, get a new mbuf, tack it on, and fill it */
1280 	remain = m->m_pkthdr.len;
1281 	moff = 0;
1282 	p = &top;
1283 	while (remain > 0 || top == NULL) {	/* allow m->m_pkthdr.len == 0 */
1284 		struct mbuf *n;
1285 
1286 		/* Get the next new mbuf */
1287 		n = m_getl(remain, how, m->m_type, top == NULL ? M_PKTHDR : 0,
1288 			   &nsize);
1289 		if (n == NULL)
1290 			goto nospace;
1291 		if (top == NULL)
1292 			if (!m_dup_pkthdr(n, m, how))
1293 				goto nospace0;
1294 
1295 		/* Link it into the new chain */
1296 		*p = n;
1297 		p = &n->m_next;
1298 
1299 		/* Copy data from original mbuf(s) into new mbuf */
1300 		n->m_len = 0;
1301 		while (n->m_len < nsize && m != NULL) {
1302 			int chunk = min(nsize - n->m_len, m->m_len - moff);
1303 
1304 			bcopy(m->m_data + moff, n->m_data + n->m_len, chunk);
1305 			moff += chunk;
1306 			n->m_len += chunk;
1307 			remain -= chunk;
1308 			if (moff == m->m_len) {
1309 				m = m->m_next;
1310 				moff = 0;
1311 			}
1312 		}
1313 
1314 		/* Check correct total mbuf length */
1315 		KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL),
1316 			("%s: bogus m_pkthdr.len", __func__));
1317 	}
1318 	return (top);
1319 
1320 nospace:
1321 	m_freem(top);
1322 nospace0:
1323 	atomic_add_long_nonlocked(&mbstat[mycpu->gd_cpuid].m_mcfail, 1);
1324 	return (NULL);
1325 }
1326 
1327 /*
1328  * Copy the non-packet mbuf data chain into a new set of mbufs, including
1329  * copying any mbuf clusters.  This is typically used to realign a data
1330  * chain by nfs_realign().
1331  *
1332  * The original chain is left intact.  how should be MB_WAIT or MB_DONTWAIT
1333  * and NULL can be returned if MB_DONTWAIT is passed.
1334  *
1335  * Be careful to use cluster mbufs, a large mbuf chain converted to non
1336  * cluster mbufs can exhaust our supply of mbufs.
1337  */
1338 struct mbuf *
1339 m_dup_data(struct mbuf *m, int how)
1340 {
1341 	struct mbuf **p, *n, *top = NULL;
1342 	int mlen, moff, chunk, gsize, nsize;
1343 
1344 	/*
1345 	 * Degenerate case
1346 	 */
1347 	if (m == NULL)
1348 		return (NULL);
1349 
1350 	/*
1351 	 * Optimize the mbuf allocation but do not get too carried away.
1352 	 */
1353 	if (m->m_next || m->m_len > MLEN)
1354 		gsize = MCLBYTES;
1355 	else
1356 		gsize = MLEN;
1357 
1358 	/* Chain control */
1359 	p = &top;
1360 	n = NULL;
1361 	nsize = 0;
1362 
1363 	/*
1364 	 * Scan the mbuf chain until nothing is left, the new mbuf chain
1365 	 * will be allocated on the fly as needed.
1366 	 */
1367 	while (m) {
1368 		mlen = m->m_len;
1369 		moff = 0;
1370 
1371 		while (mlen) {
1372 			KKASSERT(m->m_type == MT_DATA);
1373 			if (n == NULL) {
1374 				n = m_getl(gsize, how, MT_DATA, 0, &nsize);
1375 				n->m_len = 0;
1376 				if (n == NULL)
1377 					goto nospace;
1378 				*p = n;
1379 				p = &n->m_next;
1380 			}
1381 			chunk = imin(mlen, nsize);
1382 			bcopy(m->m_data + moff, n->m_data + n->m_len, chunk);
1383 			mlen -= chunk;
1384 			moff += chunk;
1385 			n->m_len += chunk;
1386 			nsize -= chunk;
1387 			if (nsize == 0)
1388 				n = NULL;
1389 		}
1390 		m = m->m_next;
1391 	}
1392 	*p = NULL;
1393 	return(top);
1394 nospace:
1395 	*p = NULL;
1396 	m_freem(top);
1397 	atomic_add_long_nonlocked(&mbstat[mycpu->gd_cpuid].m_mcfail, 1);
1398 	return (NULL);
1399 }
1400 
1401 /*
1402  * Concatenate mbuf chain n to m.
1403  * Both chains must be of the same type (e.g. MT_DATA).
1404  * Any m_pkthdr is not updated.
1405  */
1406 void
1407 m_cat(struct mbuf *m, struct mbuf *n)
1408 {
1409 	m = m_last(m);
1410 	while (n) {
1411 		if (m->m_flags & M_EXT ||
1412 		    m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
1413 			/* just join the two chains */
1414 			m->m_next = n;
1415 			return;
1416 		}
1417 		/* splat the data from one into the other */
1418 		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
1419 		    (u_int)n->m_len);
1420 		m->m_len += n->m_len;
1421 		n = m_free(n);
1422 	}
1423 }
1424 
1425 void
1426 m_adj(struct mbuf *mp, int req_len)
1427 {
1428 	int len = req_len;
1429 	struct mbuf *m;
1430 	int count;
1431 
1432 	if ((m = mp) == NULL)
1433 		return;
1434 	if (len >= 0) {
1435 		/*
1436 		 * Trim from head.
1437 		 */
1438 		while (m != NULL && len > 0) {
1439 			if (m->m_len <= len) {
1440 				len -= m->m_len;
1441 				m->m_len = 0;
1442 				m = m->m_next;
1443 			} else {
1444 				m->m_len -= len;
1445 				m->m_data += len;
1446 				len = 0;
1447 			}
1448 		}
1449 		m = mp;
1450 		if (mp->m_flags & M_PKTHDR)
1451 			m->m_pkthdr.len -= (req_len - len);
1452 	} else {
1453 		/*
1454 		 * Trim from tail.  Scan the mbuf chain,
1455 		 * calculating its length and finding the last mbuf.
1456 		 * If the adjustment only affects this mbuf, then just
1457 		 * adjust and return.  Otherwise, rescan and truncate
1458 		 * after the remaining size.
1459 		 */
1460 		len = -len;
1461 		count = 0;
1462 		for (;;) {
1463 			count += m->m_len;
1464 			if (m->m_next == NULL)
1465 				break;
1466 			m = m->m_next;
1467 		}
1468 		if (m->m_len >= len) {
1469 			m->m_len -= len;
1470 			if (mp->m_flags & M_PKTHDR)
1471 				mp->m_pkthdr.len -= len;
1472 			return;
1473 		}
1474 		count -= len;
1475 		if (count < 0)
1476 			count = 0;
1477 		/*
1478 		 * Correct length for chain is "count".
1479 		 * Find the mbuf with last data, adjust its length,
1480 		 * and toss data from remaining mbufs on chain.
1481 		 */
1482 		m = mp;
1483 		if (m->m_flags & M_PKTHDR)
1484 			m->m_pkthdr.len = count;
1485 		for (; m; m = m->m_next) {
1486 			if (m->m_len >= count) {
1487 				m->m_len = count;
1488 				break;
1489 			}
1490 			count -= m->m_len;
1491 		}
1492 		while (m->m_next)
1493 			(m = m->m_next) ->m_len = 0;
1494 	}
1495 }
1496 
1497 /*
1498  * Set the m_data pointer of a newly-allocated mbuf
1499  * to place an object of the specified size at the
1500  * end of the mbuf, longword aligned.
1501  */
1502 void
1503 m_align(struct mbuf *m, int len)
1504 {
1505 	int adjust;
1506 
1507 	if (m->m_flags & M_EXT)
1508 		adjust = m->m_ext.ext_size - len;
1509 	else if (m->m_flags & M_PKTHDR)
1510 		adjust = MHLEN - len;
1511 	else
1512 		adjust = MLEN - len;
1513 	m->m_data += adjust &~ (sizeof(long)-1);
1514 }
1515 
1516 /*
1517  * Rearrange an mbuf chain so that len bytes are contiguous
1518  * and in the data area of an mbuf (so that mtod will work for a structure
1519  * of size len).  Returns the resulting mbuf chain on success, frees it and
1520  * returns null on failure.  If there is room, it will add up to
1521  * max_protohdr-len extra bytes to the contiguous region in an attempt to
1522  * avoid being called next time.
1523  */
1524 struct mbuf *
1525 m_pullup(struct mbuf *n, int len)
1526 {
1527 	struct mbuf *m;
1528 	int count;
1529 	int space;
1530 
1531 	/*
1532 	 * If first mbuf has no cluster, and has room for len bytes
1533 	 * without shifting current data, pullup into it,
1534 	 * otherwise allocate a new mbuf to prepend to the chain.
1535 	 */
1536 	if (!(n->m_flags & M_EXT) &&
1537 	    n->m_data + len < &n->m_dat[MLEN] &&
1538 	    n->m_next) {
1539 		if (n->m_len >= len)
1540 			return (n);
1541 		m = n;
1542 		n = n->m_next;
1543 		len -= m->m_len;
1544 	} else {
1545 		if (len > MHLEN)
1546 			goto bad;
1547 		if (n->m_flags & M_PKTHDR)
1548 			m = m_gethdr(MB_DONTWAIT, n->m_type);
1549 		else
1550 			m = m_get(MB_DONTWAIT, n->m_type);
1551 		if (m == NULL)
1552 			goto bad;
1553 		m->m_len = 0;
1554 		if (n->m_flags & M_PKTHDR)
1555 			M_MOVE_PKTHDR(m, n);
1556 	}
1557 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
1558 	do {
1559 		count = min(min(max(len, max_protohdr), space), n->m_len);
1560 		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
1561 		  (unsigned)count);
1562 		len -= count;
1563 		m->m_len += count;
1564 		n->m_len -= count;
1565 		space -= count;
1566 		if (n->m_len)
1567 			n->m_data += count;
1568 		else
1569 			n = m_free(n);
1570 	} while (len > 0 && n);
1571 	if (len > 0) {
1572 		m_free(m);
1573 		goto bad;
1574 	}
1575 	m->m_next = n;
1576 	return (m);
1577 bad:
1578 	m_freem(n);
1579 	atomic_add_long_nonlocked(&mbstat[mycpu->gd_cpuid].m_mcfail, 1);
1580 	return (NULL);
1581 }
1582 
1583 /*
1584  * Partition an mbuf chain in two pieces, returning the tail --
1585  * all but the first len0 bytes.  In case of failure, it returns NULL and
1586  * attempts to restore the chain to its original state.
1587  *
1588  * Note that the resulting mbufs might be read-only, because the new
1589  * mbuf can end up sharing an mbuf cluster with the original mbuf if
1590  * the "breaking point" happens to lie within a cluster mbuf. Use the
1591  * M_WRITABLE() macro to check for this case.
1592  */
1593 struct mbuf *
1594 m_split(struct mbuf *m0, int len0, int wait)
1595 {
1596 	struct mbuf *m, *n;
1597 	unsigned len = len0, remain;
1598 
1599 	for (m = m0; m && len > m->m_len; m = m->m_next)
1600 		len -= m->m_len;
1601 	if (m == NULL)
1602 		return (NULL);
1603 	remain = m->m_len - len;
1604 	if (m0->m_flags & M_PKTHDR) {
1605 		n = m_gethdr(wait, m0->m_type);
1606 		if (n == NULL)
1607 			return (NULL);
1608 		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
1609 		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
1610 		m0->m_pkthdr.len = len0;
1611 		if (m->m_flags & M_EXT)
1612 			goto extpacket;
1613 		if (remain > MHLEN) {
1614 			/* m can't be the lead packet */
1615 			MH_ALIGN(n, 0);
1616 			n->m_next = m_split(m, len, wait);
1617 			if (n->m_next == NULL) {
1618 				m_free(n);
1619 				return (NULL);
1620 			} else {
1621 				n->m_len = 0;
1622 				return (n);
1623 			}
1624 		} else
1625 			MH_ALIGN(n, remain);
1626 	} else if (remain == 0) {
1627 		n = m->m_next;
1628 		m->m_next = 0;
1629 		return (n);
1630 	} else {
1631 		n = m_get(wait, m->m_type);
1632 		if (n == NULL)
1633 			return (NULL);
1634 		M_ALIGN(n, remain);
1635 	}
1636 extpacket:
1637 	if (m->m_flags & M_EXT) {
1638 		KKASSERT((n->m_flags & M_EXT) == 0);
1639 		n->m_data = m->m_data + len;
1640 		m->m_ext.ext_ref(m->m_ext.ext_arg);
1641 		n->m_ext = m->m_ext;
1642 		n->m_flags |= m->m_flags & (M_EXT | M_EXT_CLUSTER);
1643 	} else {
1644 		bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
1645 	}
1646 	n->m_len = remain;
1647 	m->m_len = len;
1648 	n->m_next = m->m_next;
1649 	m->m_next = 0;
1650 	return (n);
1651 }
1652 
1653 /*
1654  * Routine to copy from device local memory into mbufs.
1655  * Note: "offset" is ill-defined and always called as 0, so ignore it.
1656  */
1657 struct mbuf *
1658 m_devget(char *buf, int len, int offset, struct ifnet *ifp,
1659     void (*copy)(volatile const void *from, volatile void *to, size_t length))
1660 {
1661 	struct mbuf *m, *mfirst = NULL, **mtail;
1662 	int nsize, flags;
1663 
1664 	if (copy == NULL)
1665 		copy = bcopy;
1666 	mtail = &mfirst;
1667 	flags = M_PKTHDR;
1668 
1669 	while (len > 0) {
1670 		m = m_getl(len, MB_DONTWAIT, MT_DATA, flags, &nsize);
1671 		if (m == NULL) {
1672 			m_freem(mfirst);
1673 			return (NULL);
1674 		}
1675 		m->m_len = min(len, nsize);
1676 
1677 		if (flags & M_PKTHDR) {
1678 			if (len + max_linkhdr <= nsize)
1679 				m->m_data += max_linkhdr;
1680 			m->m_pkthdr.rcvif = ifp;
1681 			m->m_pkthdr.len = len;
1682 			flags = 0;
1683 		}
1684 
1685 		copy(buf, m->m_data, (unsigned)m->m_len);
1686 		buf += m->m_len;
1687 		len -= m->m_len;
1688 		*mtail = m;
1689 		mtail = &m->m_next;
1690 	}
1691 
1692 	return (mfirst);
1693 }
1694 
1695 /*
1696  * Routine to pad mbuf to the specified length 'padto'.
1697  */
1698 int
1699 m_devpad(struct mbuf *m, int padto)
1700 {
1701 	struct mbuf *last = NULL;
1702 	int padlen;
1703 
1704 	if (padto <= m->m_pkthdr.len)
1705 		return 0;
1706 
1707 	padlen = padto - m->m_pkthdr.len;
1708 
1709 	/* if there's only the packet-header and we can pad there, use it. */
1710 	if (m->m_pkthdr.len == m->m_len && M_TRAILINGSPACE(m) >= padlen) {
1711 		last = m;
1712 	} else {
1713 		/*
1714 		 * Walk packet chain to find last mbuf. We will either
1715 		 * pad there, or append a new mbuf and pad it
1716 		 */
1717 		for (last = m; last->m_next != NULL; last = last->m_next)
1718 			; /* EMPTY */
1719 
1720 		/* `last' now points to last in chain. */
1721 		if (M_TRAILINGSPACE(last) < padlen) {
1722 			struct mbuf *n;
1723 
1724 			/* Allocate new empty mbuf, pad it.  Compact later. */
1725 			MGET(n, MB_DONTWAIT, MT_DATA);
1726 			if (n == NULL)
1727 				return ENOBUFS;
1728 			n->m_len = 0;
1729 			last->m_next = n;
1730 			last = n;
1731 		}
1732 	}
1733 	KKASSERT(M_TRAILINGSPACE(last) >= padlen);
1734 	KKASSERT(M_WRITABLE(last));
1735 
1736 	/* Now zero the pad area */
1737 	bzero(mtod(last, char *) + last->m_len, padlen);
1738 	last->m_len += padlen;
1739 	m->m_pkthdr.len += padlen;
1740 	return 0;
1741 }
1742 
1743 /*
1744  * Copy data from a buffer back into the indicated mbuf chain,
1745  * starting "off" bytes from the beginning, extending the mbuf
1746  * chain if necessary.
1747  */
1748 void
1749 m_copyback(struct mbuf *m0, int off, int len, caddr_t cp)
1750 {
1751 	int mlen;
1752 	struct mbuf *m = m0, *n;
1753 	int totlen = 0;
1754 
1755 	if (m0 == NULL)
1756 		return;
1757 	while (off > (mlen = m->m_len)) {
1758 		off -= mlen;
1759 		totlen += mlen;
1760 		if (m->m_next == NULL) {
1761 			n = m_getclr(MB_DONTWAIT, m->m_type);
1762 			if (n == NULL)
1763 				goto out;
1764 			n->m_len = min(MLEN, len + off);
1765 			m->m_next = n;
1766 		}
1767 		m = m->m_next;
1768 	}
1769 	while (len > 0) {
1770 		mlen = min (m->m_len - off, len);
1771 		bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen);
1772 		cp += mlen;
1773 		len -= mlen;
1774 		mlen += off;
1775 		off = 0;
1776 		totlen += mlen;
1777 		if (len == 0)
1778 			break;
1779 		if (m->m_next == NULL) {
1780 			n = m_get(MB_DONTWAIT, m->m_type);
1781 			if (n == NULL)
1782 				break;
1783 			n->m_len = min(MLEN, len);
1784 			m->m_next = n;
1785 		}
1786 		m = m->m_next;
1787 	}
1788 out:	if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
1789 		m->m_pkthdr.len = totlen;
1790 }
1791 
1792 /*
1793  * Append the specified data to the indicated mbuf chain,
1794  * Extend the mbuf chain if the new data does not fit in
1795  * existing space.
1796  *
1797  * Return 1 if able to complete the job; otherwise 0.
1798  */
1799 int
1800 m_append(struct mbuf *m0, int len, c_caddr_t cp)
1801 {
1802 	struct mbuf *m, *n;
1803 	int remainder, space;
1804 
1805 	for (m = m0; m->m_next != NULL; m = m->m_next)
1806 		;
1807 	remainder = len;
1808 	space = M_TRAILINGSPACE(m);
1809 	if (space > 0) {
1810 		/*
1811 		 * Copy into available space.
1812 		 */
1813 		if (space > remainder)
1814 			space = remainder;
1815 		bcopy(cp, mtod(m, caddr_t) + m->m_len, space);
1816 		m->m_len += space;
1817 		cp += space, remainder -= space;
1818 	}
1819 	while (remainder > 0) {
1820 		/*
1821 		 * Allocate a new mbuf; could check space
1822 		 * and allocate a cluster instead.
1823 		 */
1824 		n = m_get(MB_DONTWAIT, m->m_type);
1825 		if (n == NULL)
1826 			break;
1827 		n->m_len = min(MLEN, remainder);
1828 		bcopy(cp, mtod(n, caddr_t), n->m_len);
1829 		cp += n->m_len, remainder -= n->m_len;
1830 		m->m_next = n;
1831 		m = n;
1832 	}
1833 	if (m0->m_flags & M_PKTHDR)
1834 		m0->m_pkthdr.len += len - remainder;
1835 	return (remainder == 0);
1836 }
1837 
1838 /*
1839  * Apply function f to the data in an mbuf chain starting "off" bytes from
1840  * the beginning, continuing for "len" bytes.
1841  */
1842 int
1843 m_apply(struct mbuf *m, int off, int len,
1844     int (*f)(void *, void *, u_int), void *arg)
1845 {
1846 	u_int count;
1847 	int rval;
1848 
1849 	KASSERT(off >= 0, ("m_apply, negative off %d", off));
1850 	KASSERT(len >= 0, ("m_apply, negative len %d", len));
1851 	while (off > 0) {
1852 		KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain"));
1853 		if (off < m->m_len)
1854 			break;
1855 		off -= m->m_len;
1856 		m = m->m_next;
1857 	}
1858 	while (len > 0) {
1859 		KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain"));
1860 		count = min(m->m_len - off, len);
1861 		rval = (*f)(arg, mtod(m, caddr_t) + off, count);
1862 		if (rval)
1863 			return (rval);
1864 		len -= count;
1865 		off = 0;
1866 		m = m->m_next;
1867 	}
1868 	return (0);
1869 }
1870 
1871 /*
1872  * Return a pointer to mbuf/offset of location in mbuf chain.
1873  */
1874 struct mbuf *
1875 m_getptr(struct mbuf *m, int loc, int *off)
1876 {
1877 
1878 	while (loc >= 0) {
1879 		/* Normal end of search. */
1880 		if (m->m_len > loc) {
1881 			*off = loc;
1882 			return (m);
1883 		} else {
1884 			loc -= m->m_len;
1885 			if (m->m_next == NULL) {
1886 				if (loc == 0) {
1887 					/* Point at the end of valid data. */
1888 					*off = m->m_len;
1889 					return (m);
1890 				}
1891 				return (NULL);
1892 			}
1893 			m = m->m_next;
1894 		}
1895 	}
1896 	return (NULL);
1897 }
1898 
1899 void
1900 m_print(const struct mbuf *m)
1901 {
1902 	int len;
1903 	const struct mbuf *m2;
1904 
1905 	len = m->m_pkthdr.len;
1906 	m2 = m;
1907 	while (len) {
1908 		kprintf("%p %*D\n", m2, m2->m_len, (u_char *)m2->m_data, "-");
1909 		len -= m2->m_len;
1910 		m2 = m2->m_next;
1911 	}
1912 	return;
1913 }
1914 
1915 /*
1916  * "Move" mbuf pkthdr from "from" to "to".
1917  * "from" must have M_PKTHDR set, and "to" must be empty.
1918  */
1919 void
1920 m_move_pkthdr(struct mbuf *to, struct mbuf *from)
1921 {
1922 	KASSERT((to->m_flags & M_PKTHDR), ("m_move_pkthdr: not packet header"));
1923 
1924 	to->m_flags |= from->m_flags & M_COPYFLAGS;
1925 	to->m_pkthdr = from->m_pkthdr;		/* especially tags */
1926 	SLIST_INIT(&from->m_pkthdr.tags);	/* purge tags from src */
1927 }
1928 
1929 /*
1930  * Duplicate "from"'s mbuf pkthdr in "to".
1931  * "from" must have M_PKTHDR set, and "to" must be empty.
1932  * In particular, this does a deep copy of the packet tags.
1933  */
1934 int
1935 m_dup_pkthdr(struct mbuf *to, const struct mbuf *from, int how)
1936 {
1937 	KASSERT((to->m_flags & M_PKTHDR), ("m_dup_pkthdr: not packet header"));
1938 
1939 	to->m_flags = (from->m_flags & M_COPYFLAGS) |
1940 		      (to->m_flags & ~M_COPYFLAGS);
1941 	to->m_pkthdr = from->m_pkthdr;
1942 	SLIST_INIT(&to->m_pkthdr.tags);
1943 	return (m_tag_copy_chain(to, from, how));
1944 }
1945 
1946 /*
1947  * Defragment a mbuf chain, returning the shortest possible
1948  * chain of mbufs and clusters.  If allocation fails and
1949  * this cannot be completed, NULL will be returned, but
1950  * the passed in chain will be unchanged.  Upon success,
1951  * the original chain will be freed, and the new chain
1952  * will be returned.
1953  *
1954  * If a non-packet header is passed in, the original
1955  * mbuf (chain?) will be returned unharmed.
1956  *
1957  * m_defrag_nofree doesn't free the passed in mbuf.
1958  */
1959 struct mbuf *
1960 m_defrag(struct mbuf *m0, int how)
1961 {
1962 	struct mbuf *m_new;
1963 
1964 	if ((m_new = m_defrag_nofree(m0, how)) == NULL)
1965 		return (NULL);
1966 	if (m_new != m0)
1967 		m_freem(m0);
1968 	return (m_new);
1969 }
1970 
1971 struct mbuf *
1972 m_defrag_nofree(struct mbuf *m0, int how)
1973 {
1974 	struct mbuf	*m_new = NULL, *m_final = NULL;
1975 	int		progress = 0, length, nsize;
1976 
1977 	if (!(m0->m_flags & M_PKTHDR))
1978 		return (m0);
1979 
1980 #ifdef MBUF_STRESS_TEST
1981 	if (m_defragrandomfailures) {
1982 		int temp = karc4random() & 0xff;
1983 		if (temp == 0xba)
1984 			goto nospace;
1985 	}
1986 #endif
1987 
1988 	m_final = m_getl(m0->m_pkthdr.len, how, MT_DATA, M_PKTHDR, &nsize);
1989 	if (m_final == NULL)
1990 		goto nospace;
1991 	m_final->m_len = 0;	/* in case m0->m_pkthdr.len is zero */
1992 
1993 	if (m_dup_pkthdr(m_final, m0, how) == 0)
1994 		goto nospace;
1995 
1996 	m_new = m_final;
1997 
1998 	while (progress < m0->m_pkthdr.len) {
1999 		length = m0->m_pkthdr.len - progress;
2000 		if (length > MCLBYTES)
2001 			length = MCLBYTES;
2002 
2003 		if (m_new == NULL) {
2004 			m_new = m_getl(length, how, MT_DATA, 0, &nsize);
2005 			if (m_new == NULL)
2006 				goto nospace;
2007 		}
2008 
2009 		m_copydata(m0, progress, length, mtod(m_new, caddr_t));
2010 		progress += length;
2011 		m_new->m_len = length;
2012 		if (m_new != m_final)
2013 			m_cat(m_final, m_new);
2014 		m_new = NULL;
2015 	}
2016 	if (m0->m_next == NULL)
2017 		m_defraguseless++;
2018 	m_defragpackets++;
2019 	m_defragbytes += m_final->m_pkthdr.len;
2020 	return (m_final);
2021 nospace:
2022 	m_defragfailure++;
2023 	if (m_new)
2024 		m_free(m_new);
2025 	m_freem(m_final);
2026 	return (NULL);
2027 }
2028 
2029 /*
2030  * Move data from uio into mbufs.
2031  */
2032 struct mbuf *
2033 m_uiomove(struct uio *uio)
2034 {
2035 	struct mbuf *m;			/* current working mbuf */
2036 	struct mbuf *head = NULL;	/* result mbuf chain */
2037 	struct mbuf **mp = &head;
2038 	int flags = M_PKTHDR;
2039 	int nsize;
2040 	int error;
2041 	int resid;
2042 
2043 	do {
2044 		if (uio->uio_resid > INT_MAX)
2045 			resid = INT_MAX;
2046 		else
2047 			resid = (int)uio->uio_resid;
2048 		m = m_getl(resid, MB_WAIT, MT_DATA, flags, &nsize);
2049 		if (flags) {
2050 			m->m_pkthdr.len = 0;
2051 			/* Leave room for protocol headers. */
2052 			if (resid < MHLEN)
2053 				MH_ALIGN(m, resid);
2054 			flags = 0;
2055 		}
2056 		m->m_len = imin(nsize, resid);
2057 		error = uiomove(mtod(m, caddr_t), m->m_len, uio);
2058 		if (error) {
2059 			m_free(m);
2060 			goto failed;
2061 		}
2062 		*mp = m;
2063 		mp = &m->m_next;
2064 		head->m_pkthdr.len += m->m_len;
2065 	} while (uio->uio_resid > 0);
2066 
2067 	return (head);
2068 
2069 failed:
2070 	m_freem(head);
2071 	return (NULL);
2072 }
2073 
2074 struct mbuf *
2075 m_last(struct mbuf *m)
2076 {
2077 	while (m->m_next)
2078 		m = m->m_next;
2079 	return (m);
2080 }
2081 
2082 /*
2083  * Return the number of bytes in an mbuf chain.
2084  * If lastm is not NULL, also return the last mbuf.
2085  */
2086 u_int
2087 m_lengthm(struct mbuf *m, struct mbuf **lastm)
2088 {
2089 	u_int len = 0;
2090 	struct mbuf *prev = m;
2091 
2092 	while (m) {
2093 		len += m->m_len;
2094 		prev = m;
2095 		m = m->m_next;
2096 	}
2097 	if (lastm != NULL)
2098 		*lastm = prev;
2099 	return (len);
2100 }
2101 
2102 /*
2103  * Like m_lengthm(), except also keep track of mbuf usage.
2104  */
2105 u_int
2106 m_countm(struct mbuf *m, struct mbuf **lastm, u_int *pmbcnt)
2107 {
2108 	u_int len = 0, mbcnt = 0;
2109 	struct mbuf *prev = m;
2110 
2111 	while (m) {
2112 		len += m->m_len;
2113 		mbcnt += MSIZE;
2114 		if (m->m_flags & M_EXT)
2115 			mbcnt += m->m_ext.ext_size;
2116 		prev = m;
2117 		m = m->m_next;
2118 	}
2119 	if (lastm != NULL)
2120 		*lastm = prev;
2121 	*pmbcnt = mbcnt;
2122 	return (len);
2123 }
2124