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