2 * Copyright (c) 2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of The DragonFly Project nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific, prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1982, 1986, 1988, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94
67 * $FreeBSD: src/sys/kern/uipc_mbuf.c,v 1.51.2.24 2003/04/15 06:59:29 silby Exp $
68 * $DragonFly: src/sys/kern/uipc_mbuf.c,v 1.70 2008/11/20 14:21:01 sephe Exp $
71 #include "opt_param.h"
72 #include "opt_mbuf_stress_test.h"
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/malloc.h>
77 #include <sys/kernel.h>
78 #include <sys/sysctl.h>
79 #include <sys/domain.h>
80 #include <sys/objcache.h>
82 #include <sys/protosw.h>
84 #include <sys/thread.h>
85 #include <sys/globaldata.h>
86 #include <sys/thread2.h>
88 #include <machine/atomic.h>
91 #include <vm/vm_kern.h>
92 #include <vm/vm_extern.h>
95 #include <machine/cpu.h>
99 * mbuf cluster meta-data
107 * mbuf tracking for debugging purposes
111 static MALLOC_DEFINE(M_MTRACK, "mtrack", "mtrack");
114 RB_HEAD(mbuf_rb_tree, mbtrack);
115 RB_PROTOTYPE2(mbuf_rb_tree, mbtrack, rb_node, mbtrack_cmp, struct mbuf *);
118 RB_ENTRY(mbtrack) rb_node;
124 mbtrack_cmp(struct mbtrack *mb1, struct mbtrack *mb2)
133 RB_GENERATE2(mbuf_rb_tree, mbtrack, rb_node, mbtrack_cmp, struct mbuf *, m);
135 struct mbuf_rb_tree mbuf_track_root;
138 mbuftrack(struct mbuf *m)
143 mbt = kmalloc(sizeof(*mbt), M_MTRACK, M_INTWAIT|M_ZERO);
145 if (mbuf_rb_tree_RB_INSERT(&mbuf_track_root, mbt))
146 panic("mbuftrack: mbuf %p already being tracked\n", m);
151 mbufuntrack(struct mbuf *m)
156 mbt = mbuf_rb_tree_RB_LOOKUP(&mbuf_track_root, m);
158 kprintf("mbufuntrack: mbuf %p was not tracked\n", m);
160 mbuf_rb_tree_RB_REMOVE(&mbuf_track_root, mbt);
161 kfree(mbt, M_MTRACK);
167 mbuftrackid(struct mbuf *m, int trackid)
176 mbt = mbuf_rb_tree_RB_LOOKUP(&mbuf_track_root, m);
178 mbt->trackid = trackid;
187 mbuftrack_callback(struct mbtrack *mbt, void *arg)
189 struct sysctl_req *req = arg;
193 ksnprintf(buf, sizeof(buf), "mbuf %p track %d\n", mbt->m, mbt->trackid);
195 error = SYSCTL_OUT(req, buf, strlen(buf));
202 mbuftrack_show(SYSCTL_HANDLER_ARGS)
207 error = mbuf_rb_tree_RB_SCAN(&mbuf_track_root, NULL,
208 mbuftrack_callback, req);
212 SYSCTL_PROC(_kern_ipc, OID_AUTO, showmbufs, CTLFLAG_RD|CTLTYPE_STRING,
213 0, 0, mbuftrack_show, "A", "Show all in-use mbufs");
218 #define mbufuntrack(m)
222 static void mbinit(void *);
223 SYSINIT(mbuf, SI_BOOT2_MACHDEP, SI_ORDER_FIRST, mbinit, NULL)
225 static u_long mbtypes[SMP_MAXCPU][MT_NTYPES];
227 static struct mbstat mbstat[SMP_MAXCPU];
236 #ifdef MBUF_STRESS_TEST
237 int m_defragrandomfailures;
240 struct objcache *mbuf_cache, *mbufphdr_cache;
241 struct objcache *mclmeta_cache;
242 struct objcache *mbufcluster_cache, *mbufphdrcluster_cache;
247 SYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RW,
248 &max_linkhdr, 0, "");
249 SYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RW,
250 &max_protohdr, 0, "");
251 SYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RW, &max_hdr, 0, "");
252 SYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RW,
253 &max_datalen, 0, "");
254 SYSCTL_INT(_kern_ipc, OID_AUTO, mbuf_wait, CTLFLAG_RW,
256 static int do_mbstat(SYSCTL_HANDLER_ARGS);
258 SYSCTL_PROC(_kern_ipc, KIPC_MBSTAT, mbstat, CTLTYPE_STRUCT|CTLFLAG_RD,
259 0, 0, do_mbstat, "S,mbstat", "");
261 static int do_mbtypes(SYSCTL_HANDLER_ARGS);
263 SYSCTL_PROC(_kern_ipc, OID_AUTO, mbtypes, CTLTYPE_ULONG|CTLFLAG_RD,
264 0, 0, do_mbtypes, "LU", "");
267 do_mbstat(SYSCTL_HANDLER_ARGS)
269 struct mbstat mbstat_total;
270 struct mbstat *mbstat_totalp;
273 bzero(&mbstat_total, sizeof(mbstat_total));
274 mbstat_totalp = &mbstat_total;
276 for (i = 0; i < ncpus; i++)
278 mbstat_total.m_mbufs += mbstat[i].m_mbufs;
279 mbstat_total.m_clusters += mbstat[i].m_clusters;
280 mbstat_total.m_spare += mbstat[i].m_spare;
281 mbstat_total.m_clfree += mbstat[i].m_clfree;
282 mbstat_total.m_drops += mbstat[i].m_drops;
283 mbstat_total.m_wait += mbstat[i].m_wait;
284 mbstat_total.m_drain += mbstat[i].m_drain;
285 mbstat_total.m_mcfail += mbstat[i].m_mcfail;
286 mbstat_total.m_mpfail += mbstat[i].m_mpfail;
290 * The following fields are not cumulative fields so just
291 * get their values once.
293 mbstat_total.m_msize = mbstat[0].m_msize;
294 mbstat_total.m_mclbytes = mbstat[0].m_mclbytes;
295 mbstat_total.m_minclsize = mbstat[0].m_minclsize;
296 mbstat_total.m_mlen = mbstat[0].m_mlen;
297 mbstat_total.m_mhlen = mbstat[0].m_mhlen;
299 return(sysctl_handle_opaque(oidp, mbstat_totalp, sizeof(mbstat_total), req));
303 do_mbtypes(SYSCTL_HANDLER_ARGS)
305 u_long totals[MT_NTYPES];
308 for (i = 0; i < MT_NTYPES; i++)
311 for (i = 0; i < ncpus; i++)
313 for (j = 0; j < MT_NTYPES; j++)
314 totals[j] += mbtypes[i][j];
317 return(sysctl_handle_opaque(oidp, totals, sizeof(totals), req));
321 * These are read-only because we do not currently have any code
322 * to adjust the objcache limits after the fact. The variables
323 * may only be set as boot-time tunables.
325 SYSCTL_INT(_kern_ipc, KIPC_NMBCLUSTERS, nmbclusters, CTLFLAG_RD,
326 &nmbclusters, 0, "Maximum number of mbuf clusters available");
327 SYSCTL_INT(_kern_ipc, OID_AUTO, nmbufs, CTLFLAG_RD, &nmbufs, 0,
328 "Maximum number of mbufs available");
330 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragpackets, CTLFLAG_RD,
331 &m_defragpackets, 0, "");
332 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragbytes, CTLFLAG_RD,
333 &m_defragbytes, 0, "");
334 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defraguseless, CTLFLAG_RD,
335 &m_defraguseless, 0, "");
336 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragfailure, CTLFLAG_RD,
337 &m_defragfailure, 0, "");
338 #ifdef MBUF_STRESS_TEST
339 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragrandomfailures, CTLFLAG_RW,
340 &m_defragrandomfailures, 0, "");
343 static MALLOC_DEFINE(M_MBUF, "mbuf", "mbuf");
344 static MALLOC_DEFINE(M_MBUFCL, "mbufcl", "mbufcl");
345 static MALLOC_DEFINE(M_MCLMETA, "mclmeta", "mclmeta");
347 static void m_reclaim (void);
348 static void m_mclref(void *arg);
349 static void m_mclfree(void *arg);
352 #define NMBCLUSTERS (512 + maxusers * 16)
355 #define NMBUFS (nmbclusters * 2)
359 * Perform sanity checks of tunables declared above.
362 tunable_mbinit(void *dummy)
365 * This has to be done before VM init.
367 nmbclusters = NMBCLUSTERS;
368 TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters);
370 TUNABLE_INT_FETCH("kern.ipc.nmbufs", &nmbufs);
372 if (nmbufs < nmbclusters * 2)
373 nmbufs = nmbclusters * 2;
375 SYSINIT(tunable_mbinit, SI_BOOT1_TUNABLES, SI_ORDER_ANY,
376 tunable_mbinit, NULL);
378 /* "number of clusters of pages" */
384 * The mbuf object cache only guarantees that m_next and m_nextpkt are
385 * NULL and that m_data points to the beginning of the data area. In
386 * particular, m_len and m_pkthdr.len are uninitialized. It is the
387 * responsibility of the caller to initialize those fields before use.
390 static boolean_t __inline
391 mbuf_ctor(void *obj, void *private, int ocflags)
393 struct mbuf *m = obj;
397 m->m_data = m->m_dat;
404 * Initialize the mbuf and the packet header fields.
407 mbufphdr_ctor(void *obj, void *private, int ocflags)
409 struct mbuf *m = obj;
413 m->m_data = m->m_pktdat;
414 m->m_flags = M_PKTHDR | M_PHCACHE;
416 m->m_pkthdr.rcvif = NULL; /* eliminate XXX JH */
417 SLIST_INIT(&m->m_pkthdr.tags);
418 m->m_pkthdr.csum_flags = 0; /* eliminate XXX JH */
419 m->m_pkthdr.fw_flags = 0; /* eliminate XXX JH */
425 * A mbcluster object consists of 2K (MCLBYTES) cluster and a refcount.
428 mclmeta_ctor(void *obj, void *private, int ocflags)
430 struct mbcluster *cl = obj;
433 if (ocflags & M_NOWAIT)
434 buf = kmalloc(MCLBYTES, M_MBUFCL, M_NOWAIT | M_ZERO);
436 buf = kmalloc(MCLBYTES, M_MBUFCL, M_INTWAIT | M_ZERO);
445 mclmeta_dtor(void *obj, void *private)
447 struct mbcluster *mcl = obj;
449 KKASSERT(mcl->mcl_refs == 0);
450 kfree(mcl->mcl_data, M_MBUFCL);
454 linkcluster(struct mbuf *m, struct mbcluster *cl)
457 * Add the cluster to the mbuf. The caller will detect that the
458 * mbuf now has an attached cluster.
460 m->m_ext.ext_arg = cl;
461 m->m_ext.ext_buf = cl->mcl_data;
462 m->m_ext.ext_ref = m_mclref;
463 m->m_ext.ext_free = m_mclfree;
464 m->m_ext.ext_size = MCLBYTES;
465 atomic_add_int(&cl->mcl_refs, 1);
467 m->m_data = m->m_ext.ext_buf;
468 m->m_flags |= M_EXT | M_EXT_CLUSTER;
472 mbufphdrcluster_ctor(void *obj, void *private, int ocflags)
474 struct mbuf *m = obj;
475 struct mbcluster *cl;
477 mbufphdr_ctor(obj, private, ocflags);
478 cl = objcache_get(mclmeta_cache, ocflags);
481 m->m_flags |= M_CLCACHE;
487 mbufcluster_ctor(void *obj, void *private, int ocflags)
489 struct mbuf *m = obj;
490 struct mbcluster *cl;
492 mbuf_ctor(obj, private, ocflags);
493 cl = objcache_get(mclmeta_cache, ocflags);
496 m->m_flags |= M_CLCACHE;
502 * Used for both the cluster and cluster PHDR caches.
504 * The mbuf may have lost its cluster due to sharing, deal
505 * with the situation by checking M_EXT.
508 mbufcluster_dtor(void *obj, void *private)
510 struct mbuf *m = obj;
511 struct mbcluster *mcl;
513 if (m->m_flags & M_EXT) {
514 KKASSERT((m->m_flags & M_EXT_CLUSTER) != 0);
515 mcl = m->m_ext.ext_arg;
516 KKASSERT(mcl->mcl_refs == 1);
518 objcache_put(mclmeta_cache, mcl);
522 struct objcache_malloc_args mbuf_malloc_args = { MSIZE, M_MBUF };
523 struct objcache_malloc_args mclmeta_malloc_args =
524 { sizeof(struct mbcluster), M_MCLMETA };
530 int mb_limit, cl_limit;
535 * Initialize statistics
537 for (i = 0; i < ncpus; i++) {
538 atomic_set_long_nonlocked(&mbstat[i].m_msize, MSIZE);
539 atomic_set_long_nonlocked(&mbstat[i].m_mclbytes, MCLBYTES);
540 atomic_set_long_nonlocked(&mbstat[i].m_minclsize, MINCLSIZE);
541 atomic_set_long_nonlocked(&mbstat[i].m_mlen, MLEN);
542 atomic_set_long_nonlocked(&mbstat[i].m_mhlen, MHLEN);
546 * Create objtect caches and save cluster limits, which will
547 * be used to adjust backing kmalloc pools' limit later.
550 mb_limit = cl_limit = 0;
553 mbuf_cache = objcache_create("mbuf", &limit, 0,
554 mbuf_ctor, NULL, NULL,
555 objcache_malloc_alloc, objcache_malloc_free, &mbuf_malloc_args);
559 mbufphdr_cache = objcache_create("mbuf pkt hdr", &limit, 64,
560 mbufphdr_ctor, NULL, NULL,
561 objcache_malloc_alloc, objcache_malloc_free, &mbuf_malloc_args);
564 cl_limit = nmbclusters;
565 mclmeta_cache = objcache_create("cluster mbuf", &cl_limit, 0,
566 mclmeta_ctor, mclmeta_dtor, NULL,
567 objcache_malloc_alloc, objcache_malloc_free, &mclmeta_malloc_args);
570 mbufcluster_cache = objcache_create("mbuf + cluster", &limit, 0,
571 mbufcluster_ctor, mbufcluster_dtor, NULL,
572 objcache_malloc_alloc, objcache_malloc_free, &mbuf_malloc_args);
576 mbufphdrcluster_cache = objcache_create("mbuf pkt hdr + cluster",
577 &limit, 64, mbufphdrcluster_ctor, mbufcluster_dtor, NULL,
578 objcache_malloc_alloc, objcache_malloc_free, &mbuf_malloc_args);
582 * Adjust backing kmalloc pools' limit
584 * NOTE: We raise the limit by another 1/8 to take the effect
585 * of loosememuse into account.
587 cl_limit += cl_limit / 8;
588 kmalloc_raise_limit(mclmeta_malloc_args.mtype,
589 mclmeta_malloc_args.objsize * cl_limit);
590 kmalloc_raise_limit(M_MBUFCL, MCLBYTES * cl_limit);
592 mb_limit += mb_limit / 8;
593 kmalloc_raise_limit(mbuf_malloc_args.mtype,
594 mbuf_malloc_args.objsize * mb_limit);
598 * Return the number of references to this mbuf's data. 0 is returned
599 * if the mbuf is not M_EXT, a reference count is returned if it is
600 * M_EXT | M_EXT_CLUSTER, and 99 is returned if it is a special M_EXT.
603 m_sharecount(struct mbuf *m)
605 switch (m->m_flags & (M_EXT | M_EXT_CLUSTER)) {
610 case M_EXT | M_EXT_CLUSTER:
611 return (((struct mbcluster *)m->m_ext.ext_arg)->mcl_refs);
614 return (0); /* to shut up compiler */
618 * change mbuf to new type
621 m_chtype(struct mbuf *m, int type)
623 struct globaldata *gd = mycpu;
625 atomic_add_long_nonlocked(&mbtypes[gd->gd_cpuid][type], 1);
626 atomic_subtract_long_nonlocked(&mbtypes[gd->gd_cpuid][m->m_type], 1);
627 atomic_set_short_nonlocked(&m->m_type, type);
637 SLIST_FOREACH(dp, &domains, dom_next) {
638 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
644 atomic_add_long_nonlocked(&mbstat[mycpu->gd_cpuid].m_drain, 1);
648 updatestats(struct mbuf *m, int type)
650 struct globaldata *gd = mycpu;
655 atomic_add_long_nonlocked(&mbtypes[gd->gd_cpuid][type], 1);
656 atomic_add_long_nonlocked(&mbstat[mycpu->gd_cpuid].m_mbufs, 1);
664 m_get(int how, int type)
668 int ocf = MBTOM(how);
672 m = objcache_get(mbuf_cache, ocf);
675 if ((how & MB_TRYWAIT) && ntries++ == 0) {
676 struct objcache *reclaimlist[] = {
678 mbufcluster_cache, mbufphdrcluster_cache
680 const int nreclaims = __arysize(reclaimlist);
682 if (!objcache_reclaimlist(reclaimlist, nreclaims, ocf))
689 updatestats(m, type);
694 m_gethdr(int how, int type)
697 int ocf = MBTOM(how);
702 m = objcache_get(mbufphdr_cache, ocf);
705 if ((how & MB_TRYWAIT) && ntries++ == 0) {
706 struct objcache *reclaimlist[] = {
708 mbufcluster_cache, mbufphdrcluster_cache
710 const int nreclaims = __arysize(reclaimlist);
712 if (!objcache_reclaimlist(reclaimlist, nreclaims, ocf))
719 updatestats(m, type);
724 * Get a mbuf (not a mbuf cluster!) and zero it.
728 m_getclr(int how, int type)
732 m = m_get(how, type);
734 bzero(m->m_data, MLEN);
739 * Returns an mbuf with an attached cluster.
740 * Because many network drivers use this kind of buffers a lot, it is
741 * convenient to keep a small pool of free buffers of this kind.
742 * Even a small size such as 10 gives about 10% improvement in the
743 * forwarding rate in a bridge or router.
746 m_getcl(int how, short type, int flags)
749 int ocflags = MBTOM(how);
754 if (flags & M_PKTHDR)
755 m = objcache_get(mbufphdrcluster_cache, ocflags);
757 m = objcache_get(mbufcluster_cache, ocflags);
760 if ((how & MB_TRYWAIT) && ntries++ == 0) {
761 struct objcache *reclaimlist[1];
763 if (flags & M_PKTHDR)
764 reclaimlist[0] = mbufcluster_cache;
766 reclaimlist[0] = mbufphdrcluster_cache;
767 if (!objcache_reclaimlist(reclaimlist, 1, ocflags))
778 atomic_add_long_nonlocked(&mbtypes[mycpu->gd_cpuid][type], 1);
779 atomic_add_long_nonlocked(&mbstat[mycpu->gd_cpuid].m_clusters, 1);
784 * Allocate chain of requested length.
787 m_getc(int len, int how, int type)
789 struct mbuf *n, *nfirst = NULL, **ntail = &nfirst;
793 n = m_getl(len, how, type, 0, &nsize);
809 * Allocate len-worth of mbufs and/or mbuf clusters (whatever fits best)
810 * and return a pointer to the head of the allocated chain. If m0 is
811 * non-null, then we assume that it is a single mbuf or an mbuf chain to
812 * which we want len bytes worth of mbufs and/or clusters attached, and so
813 * if we succeed in allocating it, we will just return a pointer to m0.
815 * If we happen to fail at any point during the allocation, we will free
816 * up everything we have already allocated and return NULL.
818 * Deprecated. Use m_getc() and m_cat() instead.
821 m_getm(struct mbuf *m0, int len, int type, int how)
825 nfirst = m_getc(len, how, type);
828 m_last(m0)->m_next = nfirst;
836 * Adds a cluster to a normal mbuf, M_EXT is set on success.
837 * Deprecated. Use m_getcl() instead.
840 m_mclget(struct mbuf *m, int how)
842 struct mbcluster *mcl;
844 KKASSERT((m->m_flags & M_EXT) == 0);
845 mcl = objcache_get(mclmeta_cache, MBTOM(how));
848 atomic_add_long_nonlocked(&mbstat[mycpu->gd_cpuid].m_clusters, 1);
853 * Updates to mbcluster must be MPSAFE. Only an entity which already has
854 * a reference to the cluster can ref it, so we are in no danger of
855 * racing an add with a subtract. But the operation must still be atomic
856 * since multiple entities may have a reference on the cluster.
858 * m_mclfree() is almost the same but it must contend with two entities
859 * freeing the cluster at the same time. If there is only one reference
860 * count we are the only entity referencing the cluster and no further
861 * locking is required. Otherwise we must protect against a race to 0
862 * with the serializer.
867 struct mbcluster *mcl = arg;
869 atomic_add_int(&mcl->mcl_refs, 1);
873 * When dereferencing a cluster we have to deal with a N->0 race, where
874 * N entities free their references simultaniously. To do this we use
875 * atomic_fetchadd_int().
880 struct mbcluster *mcl = arg;
882 if (atomic_fetchadd_int(&mcl->mcl_refs, -1) == 1)
883 objcache_put(mclmeta_cache, mcl);
887 * Free a single mbuf and any associated external storage. The successor,
888 * if any, is returned.
890 * We do need to check non-first mbuf for m_aux, since some of existing
891 * code does not call M_PREPEND properly.
892 * (example: call to bpf_mtap from drivers)
895 m_free(struct mbuf *m)
898 struct globaldata *gd = mycpu;
900 KASSERT(m->m_type != MT_FREE, ("freeing free mbuf %p", m));
901 atomic_subtract_long_nonlocked(&mbtypes[gd->gd_cpuid][m->m_type], 1);
906 * Make sure the mbuf is in constructed state before returning it
912 KKASSERT(m->m_nextpkt == NULL);
914 if (m->m_nextpkt != NULL) {
915 static int afewtimes = 10;
917 if (afewtimes-- > 0) {
918 kprintf("mfree: m->m_nextpkt != NULL\n");
924 if (m->m_flags & M_PKTHDR) {
925 m_tag_delete_chain(m); /* eliminate XXX JH */
928 m->m_flags &= (M_EXT | M_EXT_CLUSTER | M_CLCACHE | M_PHCACHE);
931 * Clean the M_PKTHDR state so we can return the mbuf to its original
932 * cache. This is based on the PHCACHE flag which tells us whether
933 * the mbuf was originally allocated out of a packet-header cache
934 * or a non-packet-header cache.
936 if (m->m_flags & M_PHCACHE) {
937 m->m_flags |= M_PKTHDR;
938 m->m_pkthdr.rcvif = NULL; /* eliminate XXX JH */
939 m->m_pkthdr.csum_flags = 0; /* eliminate XXX JH */
940 m->m_pkthdr.fw_flags = 0; /* eliminate XXX JH */
941 SLIST_INIT(&m->m_pkthdr.tags);
945 * Handle remaining flags combinations. M_CLCACHE tells us whether
946 * the mbuf was originally allocated from a cluster cache or not,
947 * and is totally separate from whether the mbuf is currently
948 * associated with a cluster.
951 switch(m->m_flags & (M_CLCACHE | M_EXT | M_EXT_CLUSTER)) {
952 case M_CLCACHE | M_EXT | M_EXT_CLUSTER:
954 * mbuf+cluster cache case. The mbuf was allocated from the
955 * combined mbuf_cluster cache and can be returned to the
956 * cache if the cluster hasn't been shared.
958 if (m_sharecount(m) == 1) {
960 * The cluster has not been shared, we can just
961 * reset the data pointer and return the mbuf
962 * to the cluster cache. Note that the reference
963 * count is left intact (it is still associated with
966 m->m_data = m->m_ext.ext_buf;
967 if (m->m_flags & M_PHCACHE)
968 objcache_put(mbufphdrcluster_cache, m);
970 objcache_put(mbufcluster_cache, m);
971 atomic_subtract_long_nonlocked(&mbstat[mycpu->gd_cpuid].m_clusters, 1);
974 * Hell. Someone else has a ref on this cluster,
975 * we have to disconnect it which means we can't
976 * put it back into the mbufcluster_cache, we
977 * have to destroy the mbuf.
979 * Other mbuf references to the cluster will typically
980 * be M_EXT | M_EXT_CLUSTER but without M_CLCACHE.
982 * XXX we could try to connect another cluster to
985 m->m_ext.ext_free(m->m_ext.ext_arg);
986 m->m_flags &= ~(M_EXT | M_EXT_CLUSTER);
987 if (m->m_flags & M_PHCACHE)
988 objcache_dtor(mbufphdrcluster_cache, m);
990 objcache_dtor(mbufcluster_cache, m);
993 case M_EXT | M_EXT_CLUSTER:
995 * Normal cluster associated with an mbuf that was allocated
996 * from the normal mbuf pool rather then the cluster pool.
997 * The cluster has to be independantly disassociated from the
1000 if (m_sharecount(m) == 1)
1001 atomic_subtract_long_nonlocked(&mbstat[mycpu->gd_cpuid].m_clusters, 1);
1005 * Normal cluster association case, disconnect the cluster from
1006 * the mbuf. The cluster may or may not be custom.
1008 m->m_ext.ext_free(m->m_ext.ext_arg);
1009 m->m_flags &= ~(M_EXT | M_EXT_CLUSTER);
1013 * return the mbuf to the mbuf cache.
1015 if (m->m_flags & M_PHCACHE) {
1016 m->m_data = m->m_pktdat;
1017 objcache_put(mbufphdr_cache, m);
1019 m->m_data = m->m_dat;
1020 objcache_put(mbuf_cache, m);
1022 atomic_subtract_long_nonlocked(&mbstat[mycpu->gd_cpuid].m_mbufs, 1);
1026 panic("bad mbuf flags %p %08x\n", m, m->m_flags);
1034 m_freem(struct mbuf *m)
1043 * mbuf utility routines
1047 * Lesser-used path for M_PREPEND: allocate new mbuf to prepend to chain and
1051 m_prepend(struct mbuf *m, int len, int how)
1055 if (m->m_flags & M_PKTHDR)
1056 mn = m_gethdr(how, m->m_type);
1058 mn = m_get(how, m->m_type);
1063 if (m->m_flags & M_PKTHDR)
1064 M_MOVE_PKTHDR(mn, m);
1074 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
1075 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
1076 * The wait parameter is a choice of MB_WAIT/MB_DONTWAIT from caller.
1077 * Note that the copy is read-only, because clusters are not copied,
1078 * only their reference counts are incremented.
1081 m_copym(const struct mbuf *m, int off0, int len, int wait)
1083 struct mbuf *n, **np;
1088 KASSERT(off >= 0, ("m_copym, negative off %d", off));
1089 KASSERT(len >= 0, ("m_copym, negative len %d", len));
1090 if (off == 0 && m->m_flags & M_PKTHDR)
1093 KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain"));
1103 KASSERT(len == M_COPYALL,
1104 ("m_copym, length > size of mbuf chain"));
1108 * Because we are sharing any cluster attachment below,
1109 * be sure to get an mbuf that does not have a cluster
1110 * associated with it.
1113 n = m_gethdr(wait, m->m_type);
1115 n = m_get(wait, m->m_type);
1120 if (!m_dup_pkthdr(n, m, wait))
1122 if (len == M_COPYALL)
1123 n->m_pkthdr.len -= off0;
1125 n->m_pkthdr.len = len;
1128 n->m_len = min(len, m->m_len - off);
1129 if (m->m_flags & M_EXT) {
1130 KKASSERT((n->m_flags & M_EXT) == 0);
1131 n->m_data = m->m_data + off;
1132 m->m_ext.ext_ref(m->m_ext.ext_arg);
1133 n->m_ext = m->m_ext;
1134 n->m_flags |= m->m_flags & (M_EXT | M_EXT_CLUSTER);
1136 bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
1137 (unsigned)n->m_len);
1139 if (len != M_COPYALL)
1146 atomic_add_long_nonlocked(&mbstat[mycpu->gd_cpuid].m_mcfail, 1);
1150 atomic_add_long_nonlocked(&mbstat[mycpu->gd_cpuid].m_mcfail, 1);
1155 * Copy an entire packet, including header (which must be present).
1156 * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
1157 * Note that the copy is read-only, because clusters are not copied,
1158 * only their reference counts are incremented.
1159 * Preserve alignment of the first mbuf so if the creator has left
1160 * some room at the beginning (e.g. for inserting protocol headers)
1161 * the copies also have the room available.
1164 m_copypacket(struct mbuf *m, int how)
1166 struct mbuf *top, *n, *o;
1168 n = m_gethdr(how, m->m_type);
1173 if (!m_dup_pkthdr(n, m, how))
1175 n->m_len = m->m_len;
1176 if (m->m_flags & M_EXT) {
1177 KKASSERT((n->m_flags & M_EXT) == 0);
1178 n->m_data = m->m_data;
1179 m->m_ext.ext_ref(m->m_ext.ext_arg);
1180 n->m_ext = m->m_ext;
1181 n->m_flags |= m->m_flags & (M_EXT | M_EXT_CLUSTER);
1183 n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat );
1184 bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
1189 o = m_get(how, m->m_type);
1196 n->m_len = m->m_len;
1197 if (m->m_flags & M_EXT) {
1198 KKASSERT((n->m_flags & M_EXT) == 0);
1199 n->m_data = m->m_data;
1200 m->m_ext.ext_ref(m->m_ext.ext_arg);
1201 n->m_ext = m->m_ext;
1202 n->m_flags |= m->m_flags & (M_EXT | M_EXT_CLUSTER);
1204 bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
1212 atomic_add_long_nonlocked(&mbstat[mycpu->gd_cpuid].m_mcfail, 1);
1217 * Copy data from an mbuf chain starting "off" bytes from the beginning,
1218 * continuing for "len" bytes, into the indicated buffer.
1221 m_copydata(const struct mbuf *m, int off, int len, caddr_t cp)
1225 KASSERT(off >= 0, ("m_copydata, negative off %d", off));
1226 KASSERT(len >= 0, ("m_copydata, negative len %d", len));
1228 KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain"));
1235 KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain"));
1236 count = min(m->m_len - off, len);
1237 bcopy(mtod(m, caddr_t) + off, cp, count);
1246 * Copy a packet header mbuf chain into a completely new chain, including
1247 * copying any mbuf clusters. Use this instead of m_copypacket() when
1248 * you need a writable copy of an mbuf chain.
1251 m_dup(struct mbuf *m, int how)
1253 struct mbuf **p, *top = NULL;
1254 int remain, moff, nsize;
1259 KASSERT((m->m_flags & M_PKTHDR) != 0, ("%s: !PKTHDR", __func__));
1261 /* While there's more data, get a new mbuf, tack it on, and fill it */
1262 remain = m->m_pkthdr.len;
1265 while (remain > 0 || top == NULL) { /* allow m->m_pkthdr.len == 0 */
1268 /* Get the next new mbuf */
1269 n = m_getl(remain, how, m->m_type, top == NULL ? M_PKTHDR : 0,
1274 if (!m_dup_pkthdr(n, m, how))
1277 /* Link it into the new chain */
1281 /* Copy data from original mbuf(s) into new mbuf */
1283 while (n->m_len < nsize && m != NULL) {
1284 int chunk = min(nsize - n->m_len, m->m_len - moff);
1286 bcopy(m->m_data + moff, n->m_data + n->m_len, chunk);
1290 if (moff == m->m_len) {
1296 /* Check correct total mbuf length */
1297 KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL),
1298 ("%s: bogus m_pkthdr.len", __func__));
1305 atomic_add_long_nonlocked(&mbstat[mycpu->gd_cpuid].m_mcfail, 1);
1310 * Concatenate mbuf chain n to m.
1311 * Both chains must be of the same type (e.g. MT_DATA).
1312 * Any m_pkthdr is not updated.
1315 m_cat(struct mbuf *m, struct mbuf *n)
1319 if (m->m_flags & M_EXT ||
1320 m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
1321 /* just join the two chains */
1325 /* splat the data from one into the other */
1326 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
1328 m->m_len += n->m_len;
1334 m_adj(struct mbuf *mp, int req_len)
1340 if ((m = mp) == NULL)
1346 while (m != NULL && len > 0) {
1347 if (m->m_len <= len) {
1358 if (mp->m_flags & M_PKTHDR)
1359 m->m_pkthdr.len -= (req_len - len);
1362 * Trim from tail. Scan the mbuf chain,
1363 * calculating its length and finding the last mbuf.
1364 * If the adjustment only affects this mbuf, then just
1365 * adjust and return. Otherwise, rescan and truncate
1366 * after the remaining size.
1372 if (m->m_next == NULL)
1376 if (m->m_len >= len) {
1378 if (mp->m_flags & M_PKTHDR)
1379 mp->m_pkthdr.len -= len;
1386 * Correct length for chain is "count".
1387 * Find the mbuf with last data, adjust its length,
1388 * and toss data from remaining mbufs on chain.
1391 if (m->m_flags & M_PKTHDR)
1392 m->m_pkthdr.len = count;
1393 for (; m; m = m->m_next) {
1394 if (m->m_len >= count) {
1401 (m = m->m_next) ->m_len = 0;
1406 * Rearrange an mbuf chain so that len bytes are contiguous
1407 * and in the data area of an mbuf (so that mtod will work for a structure
1408 * of size len). Returns the resulting mbuf chain on success, frees it and
1409 * returns null on failure. If there is room, it will add up to
1410 * max_protohdr-len extra bytes to the contiguous region in an attempt to
1411 * avoid being called next time.
1414 m_pullup(struct mbuf *n, int len)
1421 * If first mbuf has no cluster, and has room for len bytes
1422 * without shifting current data, pullup into it,
1423 * otherwise allocate a new mbuf to prepend to the chain.
1425 if (!(n->m_flags & M_EXT) &&
1426 n->m_data + len < &n->m_dat[MLEN] &&
1428 if (n->m_len >= len)
1436 if (n->m_flags & M_PKTHDR)
1437 m = m_gethdr(MB_DONTWAIT, n->m_type);
1439 m = m_get(MB_DONTWAIT, n->m_type);
1443 if (n->m_flags & M_PKTHDR)
1444 M_MOVE_PKTHDR(m, n);
1446 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
1448 count = min(min(max(len, max_protohdr), space), n->m_len);
1449 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
1459 } while (len > 0 && n);
1468 atomic_add_long_nonlocked(&mbstat[mycpu->gd_cpuid].m_mcfail, 1);
1473 * Partition an mbuf chain in two pieces, returning the tail --
1474 * all but the first len0 bytes. In case of failure, it returns NULL and
1475 * attempts to restore the chain to its original state.
1477 * Note that the resulting mbufs might be read-only, because the new
1478 * mbuf can end up sharing an mbuf cluster with the original mbuf if
1479 * the "breaking point" happens to lie within a cluster mbuf. Use the
1480 * M_WRITABLE() macro to check for this case.
1483 m_split(struct mbuf *m0, int len0, int wait)
1486 unsigned len = len0, remain;
1488 for (m = m0; m && len > m->m_len; m = m->m_next)
1492 remain = m->m_len - len;
1493 if (m0->m_flags & M_PKTHDR) {
1494 n = m_gethdr(wait, m0->m_type);
1497 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
1498 n->m_pkthdr.len = m0->m_pkthdr.len - len0;
1499 m0->m_pkthdr.len = len0;
1500 if (m->m_flags & M_EXT)
1502 if (remain > MHLEN) {
1503 /* m can't be the lead packet */
1505 n->m_next = m_split(m, len, wait);
1506 if (n->m_next == NULL) {
1514 MH_ALIGN(n, remain);
1515 } else if (remain == 0) {
1520 n = m_get(wait, m->m_type);
1526 if (m->m_flags & M_EXT) {
1527 KKASSERT((n->m_flags & M_EXT) == 0);
1528 n->m_data = m->m_data + len;
1529 m->m_ext.ext_ref(m->m_ext.ext_arg);
1530 n->m_ext = m->m_ext;
1531 n->m_flags |= m->m_flags & (M_EXT | M_EXT_CLUSTER);
1533 bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
1537 n->m_next = m->m_next;
1543 * Routine to copy from device local memory into mbufs.
1544 * Note: "offset" is ill-defined and always called as 0, so ignore it.
1547 m_devget(char *buf, int len, int offset, struct ifnet *ifp,
1548 void (*copy)(volatile const void *from, volatile void *to, size_t length))
1550 struct mbuf *m, *mfirst = NULL, **mtail;
1559 m = m_getl(len, MB_DONTWAIT, MT_DATA, flags, &nsize);
1564 m->m_len = min(len, nsize);
1566 if (flags & M_PKTHDR) {
1567 if (len + max_linkhdr <= nsize)
1568 m->m_data += max_linkhdr;
1569 m->m_pkthdr.rcvif = ifp;
1570 m->m_pkthdr.len = len;
1574 copy(buf, m->m_data, (unsigned)m->m_len);
1585 * Routine to pad mbuf to the specified length 'padto'.
1588 m_devpad(struct mbuf *m, int padto)
1590 struct mbuf *last = NULL;
1593 if (padto <= m->m_pkthdr.len)
1596 padlen = padto - m->m_pkthdr.len;
1598 /* if there's only the packet-header and we can pad there, use it. */
1599 if (m->m_pkthdr.len == m->m_len && M_TRAILINGSPACE(m) >= padlen) {
1603 * Walk packet chain to find last mbuf. We will either
1604 * pad there, or append a new mbuf and pad it
1606 for (last = m; last->m_next != NULL; last = last->m_next)
1609 /* `last' now points to last in chain. */
1610 if (M_TRAILINGSPACE(last) < padlen) {
1613 /* Allocate new empty mbuf, pad it. Compact later. */
1614 MGET(n, MB_DONTWAIT, MT_DATA);
1622 KKASSERT(M_TRAILINGSPACE(last) >= padlen);
1623 KKASSERT(M_WRITABLE(last));
1625 /* Now zero the pad area */
1626 bzero(mtod(last, char *) + last->m_len, padlen);
1627 last->m_len += padlen;
1628 m->m_pkthdr.len += padlen;
1633 * Copy data from a buffer back into the indicated mbuf chain,
1634 * starting "off" bytes from the beginning, extending the mbuf
1635 * chain if necessary.
1638 m_copyback(struct mbuf *m0, int off, int len, caddr_t cp)
1641 struct mbuf *m = m0, *n;
1646 while (off > (mlen = m->m_len)) {
1649 if (m->m_next == NULL) {
1650 n = m_getclr(MB_DONTWAIT, m->m_type);
1653 n->m_len = min(MLEN, len + off);
1659 mlen = min (m->m_len - off, len);
1660 bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen);
1668 if (m->m_next == NULL) {
1669 n = m_get(MB_DONTWAIT, m->m_type);
1672 n->m_len = min(MLEN, len);
1677 out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
1678 m->m_pkthdr.len = totlen;
1682 m_print(const struct mbuf *m)
1685 const struct mbuf *m2;
1687 len = m->m_pkthdr.len;
1690 kprintf("%p %*D\n", m2, m2->m_len, (u_char *)m2->m_data, "-");
1698 * "Move" mbuf pkthdr from "from" to "to".
1699 * "from" must have M_PKTHDR set, and "to" must be empty.
1702 m_move_pkthdr(struct mbuf *to, struct mbuf *from)
1704 KASSERT((to->m_flags & M_PKTHDR), ("m_move_pkthdr: not packet header"));
1706 to->m_flags |= from->m_flags & M_COPYFLAGS;
1707 to->m_pkthdr = from->m_pkthdr; /* especially tags */
1708 SLIST_INIT(&from->m_pkthdr.tags); /* purge tags from src */
1712 * Duplicate "from"'s mbuf pkthdr in "to".
1713 * "from" must have M_PKTHDR set, and "to" must be empty.
1714 * In particular, this does a deep copy of the packet tags.
1717 m_dup_pkthdr(struct mbuf *to, const struct mbuf *from, int how)
1719 KASSERT((to->m_flags & M_PKTHDR), ("m_dup_pkthdr: not packet header"));
1721 to->m_flags = (from->m_flags & M_COPYFLAGS) |
1722 (to->m_flags & ~M_COPYFLAGS);
1723 to->m_pkthdr = from->m_pkthdr;
1724 SLIST_INIT(&to->m_pkthdr.tags);
1725 return (m_tag_copy_chain(to, from, how));
1729 * Defragment a mbuf chain, returning the shortest possible
1730 * chain of mbufs and clusters. If allocation fails and
1731 * this cannot be completed, NULL will be returned, but
1732 * the passed in chain will be unchanged. Upon success,
1733 * the original chain will be freed, and the new chain
1736 * If a non-packet header is passed in, the original
1737 * mbuf (chain?) will be returned unharmed.
1739 * m_defrag_nofree doesn't free the passed in mbuf.
1742 m_defrag(struct mbuf *m0, int how)
1746 if ((m_new = m_defrag_nofree(m0, how)) == NULL)
1754 m_defrag_nofree(struct mbuf *m0, int how)
1756 struct mbuf *m_new = NULL, *m_final = NULL;
1757 int progress = 0, length, nsize;
1759 if (!(m0->m_flags & M_PKTHDR))
1762 #ifdef MBUF_STRESS_TEST
1763 if (m_defragrandomfailures) {
1764 int temp = karc4random() & 0xff;
1770 m_final = m_getl(m0->m_pkthdr.len, how, MT_DATA, M_PKTHDR, &nsize);
1771 if (m_final == NULL)
1773 m_final->m_len = 0; /* in case m0->m_pkthdr.len is zero */
1775 if (m_dup_pkthdr(m_final, m0, how) == 0)
1780 while (progress < m0->m_pkthdr.len) {
1781 length = m0->m_pkthdr.len - progress;
1782 if (length > MCLBYTES)
1785 if (m_new == NULL) {
1786 m_new = m_getl(length, how, MT_DATA, 0, &nsize);
1791 m_copydata(m0, progress, length, mtod(m_new, caddr_t));
1793 m_new->m_len = length;
1794 if (m_new != m_final)
1795 m_cat(m_final, m_new);
1798 if (m0->m_next == NULL)
1801 m_defragbytes += m_final->m_pkthdr.len;
1812 * Move data from uio into mbufs.
1815 m_uiomove(struct uio *uio)
1817 struct mbuf *m; /* current working mbuf */
1818 struct mbuf *head = NULL; /* result mbuf chain */
1819 struct mbuf **mp = &head;
1820 int resid = uio->uio_resid, nsize, flags = M_PKTHDR, error;
1823 m = m_getl(resid, MB_WAIT, MT_DATA, flags, &nsize);
1825 m->m_pkthdr.len = 0;
1826 /* Leave room for protocol headers. */
1831 m->m_len = min(nsize, resid);
1832 error = uiomove(mtod(m, caddr_t), m->m_len, uio);
1839 head->m_pkthdr.len += m->m_len;
1841 } while (resid > 0);
1851 m_last(struct mbuf *m)
1859 * Return the number of bytes in an mbuf chain.
1860 * If lastm is not NULL, also return the last mbuf.
1863 m_lengthm(struct mbuf *m, struct mbuf **lastm)
1866 struct mbuf *prev = m;
1879 * Like m_lengthm(), except also keep track of mbuf usage.
1882 m_countm(struct mbuf *m, struct mbuf **lastm, u_int *pmbcnt)
1884 u_int len = 0, mbcnt = 0;
1885 struct mbuf *prev = m;
1890 if (m->m_flags & M_EXT)
1891 mbcnt += m->m_ext.ext_size;