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