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