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