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