4 * Copyright (c) 2004 Jeffrey M. Hsu. All rights reserved.
5 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
7 * This code is derived from software contributed to The DragonFly Project
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * Copyright (c) 1982, 1986, 1988, 1991, 1993
38 * The Regents of the University of California. All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94
65 * $FreeBSD: src/sys/kern/uipc_mbuf.c,v 1.51.2.24 2003/04/15 06:59:29 silby Exp $
68 #include "opt_param.h"
69 #include "opt_mbuf_stress_test.h"
70 #include <sys/param.h>
71 #include <sys/systm.h>
73 #include <sys/malloc.h>
75 #include <sys/kernel.h>
76 #include <sys/sysctl.h>
77 #include <sys/domain.h>
78 #include <sys/objcache.h>
80 #include <sys/protosw.h>
82 #include <sys/thread.h>
83 #include <sys/globaldata.h>
85 #include <sys/thread2.h>
86 #include <sys/spinlock2.h>
88 #include <machine/atomic.h>
89 #include <machine/limits.h>
92 #include <vm/vm_kern.h>
93 #include <vm/vm_extern.h>
96 #include <machine/cpu.h>
100 * mbuf cluster meta-data
108 * mbuf tracking for debugging purposes
112 static MALLOC_DEFINE(M_MTRACK
, "mtrack", "mtrack");
115 RB_HEAD(mbuf_rb_tree
, mbtrack
);
116 RB_PROTOTYPE2(mbuf_rb_tree
, mbtrack
, rb_node
, mbtrack_cmp
, struct mbuf
*);
119 RB_ENTRY(mbtrack
) rb_node
;
125 mbtrack_cmp(struct mbtrack
*mb1
, struct mbtrack
*mb2
)
134 RB_GENERATE2(mbuf_rb_tree
, mbtrack
, rb_node
, mbtrack_cmp
, struct mbuf
*, m
);
136 struct mbuf_rb_tree mbuf_track_root
;
137 static struct spinlock mbuf_track_spin
= SPINLOCK_INITIALIZER(mbuf_track_spin
, "mbuf_track_spin");
140 mbuftrack(struct mbuf
*m
)
144 mbt
= kmalloc(sizeof(*mbt
), M_MTRACK
, M_INTWAIT
|M_ZERO
);
145 spin_lock(&mbuf_track_spin
);
147 if (mbuf_rb_tree_RB_INSERT(&mbuf_track_root
, mbt
)) {
148 spin_unlock(&mbuf_track_spin
);
149 panic("mbuftrack: mbuf %p already being tracked", m
);
151 spin_unlock(&mbuf_track_spin
);
155 mbufuntrack(struct mbuf
*m
)
159 spin_lock(&mbuf_track_spin
);
160 mbt
= mbuf_rb_tree_RB_LOOKUP(&mbuf_track_root
, m
);
162 spin_unlock(&mbuf_track_spin
);
163 panic("mbufuntrack: mbuf %p was not tracked", m
);
165 mbuf_rb_tree_RB_REMOVE(&mbuf_track_root
, mbt
);
166 spin_unlock(&mbuf_track_spin
);
167 kfree(mbt
, M_MTRACK
);
172 mbuftrackid(struct mbuf
*m
, int trackid
)
177 spin_lock(&mbuf_track_spin
);
181 mbt
= mbuf_rb_tree_RB_LOOKUP(&mbuf_track_root
, m
);
183 spin_unlock(&mbuf_track_spin
);
184 panic("mbuftrackid: mbuf %p not tracked", m
);
186 mbt
->trackid
= trackid
;
191 spin_unlock(&mbuf_track_spin
);
195 mbuftrack_callback(struct mbtrack
*mbt
, void *arg
)
197 struct sysctl_req
*req
= arg
;
201 ksnprintf(buf
, sizeof(buf
), "mbuf %p track %d\n", mbt
->m
, mbt
->trackid
);
203 spin_unlock(&mbuf_track_spin
);
204 error
= SYSCTL_OUT(req
, buf
, strlen(buf
));
205 spin_lock(&mbuf_track_spin
);
212 mbuftrack_show(SYSCTL_HANDLER_ARGS
)
216 spin_lock(&mbuf_track_spin
);
217 error
= mbuf_rb_tree_RB_SCAN(&mbuf_track_root
, NULL
,
218 mbuftrack_callback
, req
);
219 spin_unlock(&mbuf_track_spin
);
222 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, showmbufs
, CTLFLAG_RD
|CTLTYPE_STRING
,
223 0, 0, mbuftrack_show
, "A", "Show all in-use mbufs");
228 #define mbufuntrack(m)
232 static void mbinit(void *);
233 SYSINIT(mbuf
, SI_BOOT2_MACHDEP
, SI_ORDER_FIRST
, mbinit
, NULL
);
235 struct mbtypes_stat
{
236 u_long stats
[MT_NTYPES
];
239 static struct mbtypes_stat mbtypes
[SMP_MAXCPU
];
241 static struct mbstat mbstat
[SMP_MAXCPU
] __cachealign
;
250 #ifdef MBUF_STRESS_TEST
251 int m_defragrandomfailures
;
254 struct objcache
*mbuf_cache
, *mbufphdr_cache
;
255 struct objcache
*mclmeta_cache
, *mjclmeta_cache
;
256 struct objcache
*mbufcluster_cache
, *mbufphdrcluster_cache
;
257 struct objcache
*mbufjcluster_cache
, *mbufphdrjcluster_cache
;
259 struct lock mbupdate_lk
= LOCK_INITIALIZER("mbupdate", 0, LK_CANRECURSE
);
262 static int nmbjclusters
;
265 static int mjclph_cachefrac
;
266 static int mjcl_cachefrac
;
267 static int mclph_cachefrac
;
268 static int mcl_cachefrac
;
270 SYSCTL_INT(_kern_ipc
, KIPC_MAX_LINKHDR
, max_linkhdr
, CTLFLAG_RW
,
271 &max_linkhdr
, 0, "Max size of a link-level header");
272 SYSCTL_INT(_kern_ipc
, KIPC_MAX_PROTOHDR
, max_protohdr
, CTLFLAG_RW
,
273 &max_protohdr
, 0, "Max size of a protocol header");
274 SYSCTL_INT(_kern_ipc
, KIPC_MAX_HDR
, max_hdr
, CTLFLAG_RW
, &max_hdr
, 0,
275 "Max size of link+protocol headers");
276 SYSCTL_INT(_kern_ipc
, KIPC_MAX_DATALEN
, max_datalen
, CTLFLAG_RW
,
277 &max_datalen
, 0, "Max data payload size without headers");
278 SYSCTL_INT(_kern_ipc
, OID_AUTO
, mbuf_wait
, CTLFLAG_RW
,
279 &mbuf_wait
, 0, "Time in ticks to sleep after failed mbuf allocations");
280 static int do_mbstat(SYSCTL_HANDLER_ARGS
);
282 SYSCTL_PROC(_kern_ipc
, KIPC_MBSTAT
, mbstat
, CTLTYPE_STRUCT
|CTLFLAG_RD
,
283 0, 0, do_mbstat
, "S,mbstat", "mbuf usage statistics");
285 static int do_mbtypes(SYSCTL_HANDLER_ARGS
);
287 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, mbtypes
, CTLTYPE_ULONG
|CTLFLAG_RD
,
288 0, 0, do_mbtypes
, "LU", "");
291 do_mbstat(SYSCTL_HANDLER_ARGS
)
293 struct mbstat mbstat_total
;
294 struct mbstat
*mbstat_totalp
;
297 bzero(&mbstat_total
, sizeof(mbstat_total
));
298 mbstat_totalp
= &mbstat_total
;
300 for (i
= 0; i
< ncpus
; i
++)
302 mbstat_total
.m_mbufs
+= mbstat
[i
].m_mbufs
;
303 mbstat_total
.m_clusters
+= mbstat
[i
].m_clusters
;
304 mbstat_total
.m_jclusters
+= mbstat
[i
].m_jclusters
;
305 mbstat_total
.m_clfree
+= mbstat
[i
].m_clfree
;
306 mbstat_total
.m_drops
+= mbstat
[i
].m_drops
;
307 mbstat_total
.m_wait
+= mbstat
[i
].m_wait
;
308 mbstat_total
.m_drain
+= mbstat
[i
].m_drain
;
309 mbstat_total
.m_mcfail
+= mbstat
[i
].m_mcfail
;
310 mbstat_total
.m_mpfail
+= mbstat
[i
].m_mpfail
;
314 * The following fields are not cumulative fields so just
315 * get their values once.
317 mbstat_total
.m_msize
= mbstat
[0].m_msize
;
318 mbstat_total
.m_mclbytes
= mbstat
[0].m_mclbytes
;
319 mbstat_total
.m_minclsize
= mbstat
[0].m_minclsize
;
320 mbstat_total
.m_mlen
= mbstat
[0].m_mlen
;
321 mbstat_total
.m_mhlen
= mbstat
[0].m_mhlen
;
323 return(sysctl_handle_opaque(oidp
, mbstat_totalp
, sizeof(mbstat_total
), req
));
327 do_mbtypes(SYSCTL_HANDLER_ARGS
)
329 u_long totals
[MT_NTYPES
];
332 for (i
= 0; i
< MT_NTYPES
; i
++)
335 for (i
= 0; i
< ncpus
; i
++)
337 for (j
= 0; j
< MT_NTYPES
; j
++)
338 totals
[j
] += mbtypes
[i
].stats
[j
];
341 return(sysctl_handle_opaque(oidp
, totals
, sizeof(totals
), req
));
345 * The variables may be set as boot-time tunables or live. Setting these
346 * values too low can deadlock your network. Network interfaces may also
347 * adjust nmbclusters and/or nmbjclusters to account for preloading the
350 static int sysctl_nmbclusters(SYSCTL_HANDLER_ARGS
);
351 static int sysctl_nmbjclusters(SYSCTL_HANDLER_ARGS
);
352 static int sysctl_nmbufs(SYSCTL_HANDLER_ARGS
);
353 SYSCTL_PROC(_kern_ipc
, KIPC_NMBCLUSTERS
, nmbclusters
, CTLTYPE_INT
| CTLFLAG_RW
,
354 0, 0, sysctl_nmbclusters
, "I",
355 "Maximum number of mbuf clusters available");
356 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, nmbjclusters
, CTLTYPE_INT
| CTLFLAG_RW
,
357 0, 0, sysctl_nmbjclusters
, "I",
358 "Maximum number of mbuf jclusters available");
359 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, nmbufs
, CTLTYPE_INT
| CTLFLAG_RW
,
360 0, 0, sysctl_nmbufs
, "I",
361 "Maximum number of mbufs available");
363 SYSCTL_INT(_kern_ipc
, OID_AUTO
, mjclph_cachefrac
, CTLFLAG_RD
,
364 &mjclph_cachefrac
, 0,
365 "Fraction of cacheable mbuf jclusters w/ pkthdr");
366 SYSCTL_INT(_kern_ipc
, OID_AUTO
, mjcl_cachefrac
, CTLFLAG_RD
,
368 "Fraction of cacheable mbuf jclusters");
369 SYSCTL_INT(_kern_ipc
, OID_AUTO
, mclph_cachefrac
, CTLFLAG_RD
,
371 "Fraction of cacheable mbuf clusters w/ pkthdr");
372 SYSCTL_INT(_kern_ipc
, OID_AUTO
, mcl_cachefrac
, CTLFLAG_RD
,
373 &mcl_cachefrac
, 0, "Fraction of cacheable mbuf clusters");
375 SYSCTL_INT(_kern_ipc
, OID_AUTO
, m_defragpackets
, CTLFLAG_RD
,
376 &m_defragpackets
, 0, "Number of defragment packets");
377 SYSCTL_INT(_kern_ipc
, OID_AUTO
, m_defragbytes
, CTLFLAG_RD
,
378 &m_defragbytes
, 0, "Number of defragment bytes");
379 SYSCTL_INT(_kern_ipc
, OID_AUTO
, m_defraguseless
, CTLFLAG_RD
,
380 &m_defraguseless
, 0, "Number of useless defragment mbuf chain operations");
381 SYSCTL_INT(_kern_ipc
, OID_AUTO
, m_defragfailure
, CTLFLAG_RD
,
382 &m_defragfailure
, 0, "Number of failed defragment mbuf chain operations");
383 #ifdef MBUF_STRESS_TEST
384 SYSCTL_INT(_kern_ipc
, OID_AUTO
, m_defragrandomfailures
, CTLFLAG_RW
,
385 &m_defragrandomfailures
, 0, "");
388 static MALLOC_DEFINE(M_MBUF
, "mbuf", "mbuf");
389 static MALLOC_DEFINE(M_MBUFCL
, "mbufcl", "mbufcl");
390 static MALLOC_DEFINE(M_MCLMETA
, "mclmeta", "mclmeta");
392 static void m_reclaim (void);
393 static void m_mclref(void *arg
);
394 static void m_mclfree(void *arg
);
395 static void m_mjclfree(void *arg
);
397 static void mbupdatelimits(void);
400 * NOTE: Default NMBUFS must take into account a possible DOS attack
401 * using fd passing on unix domain sockets.
404 #define NMBCLUSTERS (512 + maxusers * 16)
406 #ifndef MJCLPH_CACHEFRAC
407 #define MJCLPH_CACHEFRAC 16
409 #ifndef MJCL_CACHEFRAC
410 #define MJCL_CACHEFRAC 4
412 #ifndef MCLPH_CACHEFRAC
413 #define MCLPH_CACHEFRAC 16
415 #ifndef MCL_CACHEFRAC
416 #define MCL_CACHEFRAC 4
419 #define NMBJCLUSTERS (NMBCLUSTERS / 2)
422 #define NMBUFS (nmbclusters * 2 + maxfiles)
425 #define NMBCLUSTERS_MIN (NMBCLUSTERS / 2)
426 #define NMBJCLUSTERS_MIN (NMBJCLUSTERS / 2)
427 #define NMBUFS_MIN ((NMBCLUSTERS * 2 + maxfiles) / 2)
430 * Perform sanity checks of tunables declared above.
433 tunable_mbinit(void *dummy
)
436 * This has to be done before VM init.
438 nmbclusters
= NMBCLUSTERS
;
439 TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters
);
440 mjclph_cachefrac
= MJCLPH_CACHEFRAC
;
441 TUNABLE_INT_FETCH("kern.ipc.mjclph_cachefrac", &mjclph_cachefrac
);
442 mjcl_cachefrac
= MJCL_CACHEFRAC
;
443 TUNABLE_INT_FETCH("kern.ipc.mjcl_cachefrac", &mjcl_cachefrac
);
444 mclph_cachefrac
= MCLPH_CACHEFRAC
;
445 TUNABLE_INT_FETCH("kern.ipc.mclph_cachefrac", &mclph_cachefrac
);
446 mcl_cachefrac
= MCL_CACHEFRAC
;
447 TUNABLE_INT_FETCH("kern.ipc.mcl_cachefrac", &mcl_cachefrac
);
450 * WARNING! each mcl cache feeds two mbuf caches, so the minimum
451 * cachefrac is 2. For safety, use 3.
453 if (mjclph_cachefrac
< 3)
454 mjclph_cachefrac
= 3;
455 if (mjcl_cachefrac
< 3)
457 if (mclph_cachefrac
< 3)
459 if (mcl_cachefrac
< 3)
462 nmbjclusters
= NMBJCLUSTERS
;
463 TUNABLE_INT_FETCH("kern.ipc.nmbjclusters", &nmbjclusters
);
466 TUNABLE_INT_FETCH("kern.ipc.nmbufs", &nmbufs
);
469 if (nmbufs
< nmbclusters
* 2)
470 nmbufs
= nmbclusters
* 2;
472 SYSINIT(tunable_mbinit
, SI_BOOT1_TUNABLES
, SI_ORDER_ANY
,
473 tunable_mbinit
, NULL
);
476 mbinclimit(int *limit
, int inc
, int minlim
)
480 lockmgr(&mbupdate_lk
, LK_EXCLUSIVE
);
482 new_limit
= *limit
+ inc
;
483 if (new_limit
< minlim
)
486 if (*limit
!= new_limit
) {
491 lockmgr(&mbupdate_lk
, LK_RELEASE
);
495 mbsetlimit(int *limit
, int new_limit
, int minlim
)
497 if (new_limit
< minlim
)
500 lockmgr(&mbupdate_lk
, LK_EXCLUSIVE
);
501 mbinclimit(limit
, new_limit
- *limit
, minlim
);
502 lockmgr(&mbupdate_lk
, LK_RELEASE
);
507 sysctl_mblimit(SYSCTL_HANDLER_ARGS
, int *limit
, int minlim
)
512 error
= sysctl_handle_int(oidp
, &value
, 0, req
);
513 if (error
|| req
->newptr
== NULL
)
516 return mbsetlimit(limit
, value
, minlim
);
520 * Sysctl support to update nmbclusters, nmbjclusters, and nmbufs.
523 sysctl_nmbclusters(SYSCTL_HANDLER_ARGS
)
525 return sysctl_mblimit(oidp
, arg1
, arg2
, req
, &nmbclusters
,
530 sysctl_nmbjclusters(SYSCTL_HANDLER_ARGS
)
532 return sysctl_mblimit(oidp
, arg1
, arg2
, req
, &nmbjclusters
,
537 sysctl_nmbufs(SYSCTL_HANDLER_ARGS
)
539 return sysctl_mblimit(oidp
, arg1
, arg2
, req
, &nmbufs
, NMBUFS_MIN
);
543 mcl_inclimit(int inc
)
545 mbinclimit(&nmbclusters
, inc
, NMBCLUSTERS_MIN
);
549 mjcl_inclimit(int inc
)
551 mbinclimit(&nmbjclusters
, inc
, NMBJCLUSTERS_MIN
);
557 mbinclimit(&nmbufs
, inc
, NMBUFS_MIN
);
560 /* "number of clusters of pages" */
566 * The mbuf object cache only guarantees that m_next and m_nextpkt are
567 * NULL and that m_data points to the beginning of the data area. In
568 * particular, m_len and m_pkthdr.len are uninitialized. It is the
569 * responsibility of the caller to initialize those fields before use.
571 static __inline boolean_t
572 mbuf_ctor(void *obj
, void *private, int ocflags
)
574 struct mbuf
*m
= obj
;
578 m
->m_data
= m
->m_dat
;
585 * Initialize the mbuf and the packet header fields.
588 mbufphdr_ctor(void *obj
, void *private, int ocflags
)
590 struct mbuf
*m
= obj
;
594 m
->m_data
= m
->m_pktdat
;
595 m
->m_flags
= M_PKTHDR
| M_PHCACHE
;
597 m
->m_pkthdr
.rcvif
= NULL
; /* eliminate XXX JH */
598 SLIST_INIT(&m
->m_pkthdr
.tags
);
599 m
->m_pkthdr
.csum_flags
= 0; /* eliminate XXX JH */
600 m
->m_pkthdr
.fw_flags
= 0; /* eliminate XXX JH */
606 * A mbcluster object consists of 2K (MCLBYTES) cluster and a refcount.
609 mclmeta_ctor(void *obj
, void *private, int ocflags
)
611 struct mbcluster
*cl
= obj
;
614 if (ocflags
& M_NOWAIT
)
615 buf
= kmalloc(MCLBYTES
, M_MBUFCL
, M_NOWAIT
| M_ZERO
);
617 buf
= kmalloc(MCLBYTES
, M_MBUFCL
, M_INTWAIT
| M_ZERO
);
626 mjclmeta_ctor(void *obj
, void *private, int ocflags
)
628 struct mbcluster
*cl
= obj
;
631 if (ocflags
& M_NOWAIT
)
632 buf
= kmalloc(MJUMPAGESIZE
, M_MBUFCL
, M_NOWAIT
| M_ZERO
);
634 buf
= kmalloc(MJUMPAGESIZE
, M_MBUFCL
, M_INTWAIT
| M_ZERO
);
643 mclmeta_dtor(void *obj
, void *private)
645 struct mbcluster
*mcl
= obj
;
647 KKASSERT(mcl
->mcl_refs
== 0);
648 kfree(mcl
->mcl_data
, M_MBUFCL
);
652 linkjcluster(struct mbuf
*m
, struct mbcluster
*cl
, uint size
)
655 * Add the cluster to the mbuf. The caller will detect that the
656 * mbuf now has an attached cluster.
658 m
->m_ext
.ext_arg
= cl
;
659 m
->m_ext
.ext_buf
= cl
->mcl_data
;
660 m
->m_ext
.ext_ref
= m_mclref
;
661 if (size
!= MCLBYTES
)
662 m
->m_ext
.ext_free
= m_mjclfree
;
664 m
->m_ext
.ext_free
= m_mclfree
;
665 m
->m_ext
.ext_size
= size
;
666 atomic_add_int(&cl
->mcl_refs
, 1);
668 m
->m_data
= m
->m_ext
.ext_buf
;
669 m
->m_flags
|= M_EXT
| M_EXT_CLUSTER
;
673 linkcluster(struct mbuf
*m
, struct mbcluster
*cl
)
675 linkjcluster(m
, cl
, MCLBYTES
);
679 mbufphdrcluster_ctor(void *obj
, void *private, int ocflags
)
681 struct mbuf
*m
= obj
;
682 struct mbcluster
*cl
;
684 mbufphdr_ctor(obj
, private, ocflags
);
685 cl
= objcache_get(mclmeta_cache
, ocflags
);
687 ++mbstat
[mycpu
->gd_cpuid
].m_drops
;
690 m
->m_flags
|= M_CLCACHE
;
696 mbufphdrjcluster_ctor(void *obj
, void *private, int ocflags
)
698 struct mbuf
*m
= obj
;
699 struct mbcluster
*cl
;
701 mbufphdr_ctor(obj
, private, ocflags
);
702 cl
= objcache_get(mjclmeta_cache
, ocflags
);
704 ++mbstat
[mycpu
->gd_cpuid
].m_drops
;
707 m
->m_flags
|= M_CLCACHE
;
708 linkjcluster(m
, cl
, MJUMPAGESIZE
);
713 mbufcluster_ctor(void *obj
, void *private, int ocflags
)
715 struct mbuf
*m
= obj
;
716 struct mbcluster
*cl
;
718 mbuf_ctor(obj
, private, ocflags
);
719 cl
= objcache_get(mclmeta_cache
, ocflags
);
721 ++mbstat
[mycpu
->gd_cpuid
].m_drops
;
724 m
->m_flags
|= M_CLCACHE
;
730 mbufjcluster_ctor(void *obj
, void *private, int ocflags
)
732 struct mbuf
*m
= obj
;
733 struct mbcluster
*cl
;
735 mbuf_ctor(obj
, private, ocflags
);
736 cl
= objcache_get(mjclmeta_cache
, ocflags
);
738 ++mbstat
[mycpu
->gd_cpuid
].m_drops
;
741 m
->m_flags
|= M_CLCACHE
;
742 linkjcluster(m
, cl
, MJUMPAGESIZE
);
747 * Used for both the cluster and cluster PHDR caches.
749 * The mbuf may have lost its cluster due to sharing, deal
750 * with the situation by checking M_EXT.
753 mbufcluster_dtor(void *obj
, void *private)
755 struct mbuf
*m
= obj
;
756 struct mbcluster
*mcl
;
758 if (m
->m_flags
& M_EXT
) {
759 KKASSERT((m
->m_flags
& M_EXT_CLUSTER
) != 0);
760 mcl
= m
->m_ext
.ext_arg
;
761 KKASSERT(mcl
->mcl_refs
== 1);
763 if (m
->m_flags
& M_EXT
&& m
->m_ext
.ext_size
!= MCLBYTES
)
764 objcache_put(mjclmeta_cache
, mcl
);
766 objcache_put(mclmeta_cache
, mcl
);
770 struct objcache_malloc_args mbuf_malloc_args
= { MSIZE
, M_MBUF
};
771 struct objcache_malloc_args mclmeta_malloc_args
=
772 { sizeof(struct mbcluster
), M_MCLMETA
};
778 int mb_limit
, cl_limit
, ncl_limit
, jcl_limit
;
783 * Initialize statistics
785 for (i
= 0; i
< ncpus
; i
++) {
786 mbstat
[i
].m_msize
= MSIZE
;
787 mbstat
[i
].m_mclbytes
= MCLBYTES
;
788 mbstat
[i
].m_mjumpagesize
= MJUMPAGESIZE
;
789 mbstat
[i
].m_minclsize
= MINCLSIZE
;
790 mbstat
[i
].m_mlen
= MLEN
;
791 mbstat
[i
].m_mhlen
= MHLEN
;
795 * Create object caches and save cluster limits, which will
796 * be used to adjust backing kmalloc pools' limit later.
799 mb_limit
= cl_limit
= 0;
802 mbuf_cache
= objcache_create("mbuf",
804 mbuf_ctor
, NULL
, NULL
,
805 objcache_malloc_alloc
, objcache_malloc_free
, &mbuf_malloc_args
);
809 mbufphdr_cache
= objcache_create("mbuf pkt hdr",
811 mbufphdr_ctor
, NULL
, NULL
,
812 objcache_malloc_alloc
, objcache_malloc_free
, &mbuf_malloc_args
);
815 ncl_limit
= nmbclusters
;
816 mclmeta_cache
= objcache_create("cluster mbuf",
817 ncl_limit
, nmbclusters
/ 4,
818 mclmeta_ctor
, mclmeta_dtor
, NULL
,
819 objcache_malloc_alloc
, objcache_malloc_free
, &mclmeta_malloc_args
);
820 cl_limit
+= ncl_limit
;
822 jcl_limit
= nmbjclusters
;
823 mjclmeta_cache
= objcache_create("jcluster mbuf",
824 jcl_limit
, nmbjclusters
/ 4,
825 mjclmeta_ctor
, mclmeta_dtor
, NULL
,
826 objcache_malloc_alloc
, objcache_malloc_free
, &mclmeta_malloc_args
);
827 cl_limit
+= jcl_limit
;
830 mbufcluster_cache
= objcache_create("mbuf + cluster",
831 limit
, nmbclusters
/ mcl_cachefrac
,
832 mbufcluster_ctor
, mbufcluster_dtor
, NULL
,
833 objcache_malloc_alloc
, objcache_malloc_free
, &mbuf_malloc_args
);
837 mbufphdrcluster_cache
= objcache_create("mbuf pkt hdr + cluster",
838 limit
, nmbclusters
/ mclph_cachefrac
,
839 mbufphdrcluster_ctor
, mbufcluster_dtor
, NULL
,
840 objcache_malloc_alloc
, objcache_malloc_free
, &mbuf_malloc_args
);
843 limit
= nmbjclusters
;
844 mbufjcluster_cache
= objcache_create("mbuf + jcluster",
845 limit
, nmbjclusters
/ mjcl_cachefrac
,
846 mbufjcluster_ctor
, mbufcluster_dtor
, NULL
,
847 objcache_malloc_alloc
, objcache_malloc_free
, &mbuf_malloc_args
);
850 limit
= nmbjclusters
;
851 mbufphdrjcluster_cache
= objcache_create("mbuf pkt hdr + jcluster",
852 limit
, nmbjclusters
/ mjclph_cachefrac
,
853 mbufphdrjcluster_ctor
, mbufcluster_dtor
, NULL
,
854 objcache_malloc_alloc
, objcache_malloc_free
, &mbuf_malloc_args
);
858 * Adjust backing kmalloc pools' limit
860 * NOTE: We raise the limit by another 1/8 to take the effect
861 * of loosememuse into account.
863 cl_limit
+= cl_limit
/ 8;
864 kmalloc_raise_limit(mclmeta_malloc_args
.mtype
,
865 mclmeta_malloc_args
.objsize
* (size_t)cl_limit
);
866 kmalloc_raise_limit(M_MBUFCL
,
867 (MCLBYTES
* (size_t)ncl_limit
) +
868 (MJUMPAGESIZE
* (size_t)jcl_limit
));
870 mb_limit
+= mb_limit
/ 8;
871 kmalloc_raise_limit(mbuf_malloc_args
.mtype
,
872 mbuf_malloc_args
.objsize
* (size_t)mb_limit
);
876 * Adjust mbuf limits after changes have been made
878 * Caller must hold mbupdate_lk
883 int mb_limit
, cl_limit
, ncl_limit
, jcl_limit
;
886 KASSERT(lockstatus(&mbupdate_lk
, curthread
) != 0,
887 ("mbupdate_lk is not held"));
890 * Figure out adjustments to object caches after nmbufs, nmbclusters,
891 * or nmbjclusters has been modified.
893 mb_limit
= cl_limit
= 0;
896 objcache_set_cluster_limit(mbuf_cache
, limit
);
900 objcache_set_cluster_limit(mbufphdr_cache
, limit
);
903 ncl_limit
= nmbclusters
;
904 objcache_set_cluster_limit(mclmeta_cache
, ncl_limit
);
905 cl_limit
+= ncl_limit
;
907 jcl_limit
= nmbjclusters
;
908 objcache_set_cluster_limit(mjclmeta_cache
, jcl_limit
);
909 cl_limit
+= jcl_limit
;
912 objcache_set_cluster_limit(mbufcluster_cache
, limit
);
916 objcache_set_cluster_limit(mbufphdrcluster_cache
, limit
);
919 limit
= nmbjclusters
;
920 objcache_set_cluster_limit(mbufjcluster_cache
, limit
);
923 limit
= nmbjclusters
;
924 objcache_set_cluster_limit(mbufphdrjcluster_cache
, limit
);
928 * Adjust backing kmalloc pools' limit
930 * NOTE: We raise the limit by another 1/8 to take the effect
931 * of loosememuse into account.
933 cl_limit
+= cl_limit
/ 8;
934 kmalloc_raise_limit(mclmeta_malloc_args
.mtype
,
935 mclmeta_malloc_args
.objsize
* (size_t)cl_limit
);
936 kmalloc_raise_limit(M_MBUFCL
,
937 (MCLBYTES
* (size_t)ncl_limit
) +
938 (MJUMPAGESIZE
* (size_t)jcl_limit
));
939 mb_limit
+= mb_limit
/ 8;
940 kmalloc_raise_limit(mbuf_malloc_args
.mtype
,
941 mbuf_malloc_args
.objsize
* (size_t)mb_limit
);
945 * Return the number of references to this mbuf's data. 0 is returned
946 * if the mbuf is not M_EXT, a reference count is returned if it is
947 * M_EXT | M_EXT_CLUSTER, and 99 is returned if it is a special M_EXT.
950 m_sharecount(struct mbuf
*m
)
952 switch (m
->m_flags
& (M_EXT
| M_EXT_CLUSTER
)) {
957 case M_EXT
| M_EXT_CLUSTER
:
958 return (((struct mbcluster
*)m
->m_ext
.ext_arg
)->mcl_refs
);
961 return (0); /* to shut up compiler */
965 * change mbuf to new type
968 m_chtype(struct mbuf
*m
, int type
)
970 struct globaldata
*gd
= mycpu
;
972 ++mbtypes
[gd
->gd_cpuid
].stats
[type
];
973 --mbtypes
[gd
->gd_cpuid
].stats
[m
->m_type
];
983 kprintf("Debug: m_reclaim() called\n");
985 SLIST_FOREACH(dp
, &domains
, dom_next
) {
986 for (pr
= dp
->dom_protosw
; pr
< dp
->dom_protoswNPROTOSW
; pr
++) {
991 ++mbstat
[mycpu
->gd_cpuid
].m_drain
;
995 updatestats(struct mbuf
*m
, int type
)
997 struct globaldata
*gd
= mycpu
;
1002 KASSERT(m
->m_next
== NULL
, ("mbuf %p: bad m_next in get", m
));
1003 KASSERT(m
->m_nextpkt
== NULL
, ("mbuf %p: bad m_nextpkt in get", m
));
1006 ++mbtypes
[gd
->gd_cpuid
].stats
[type
];
1007 ++mbstat
[gd
->gd_cpuid
].m_mbufs
;
1015 m_get(int how
, int type
)
1019 int ocf
= MB_OCFLAG(how
);
1023 m
= objcache_get(mbuf_cache
, ocf
);
1026 if ((ocf
& M_WAITOK
) && ntries
++ == 0) {
1027 struct objcache
*reclaimlist
[] = {
1030 mbufphdrcluster_cache
,
1032 mbufphdrjcluster_cache
1034 const int nreclaims
= NELEM(reclaimlist
);
1036 if (!objcache_reclaimlist(reclaimlist
, nreclaims
, ocf
))
1040 ++mbstat
[mycpu
->gd_cpuid
].m_drops
;
1044 KASSERT(m
->m_data
== m
->m_dat
, ("mbuf %p: bad m_data in get", m
));
1048 updatestats(m
, type
);
1053 m_gethdr(int how
, int type
)
1056 int ocf
= MB_OCFLAG(how
);
1061 m
= objcache_get(mbufphdr_cache
, ocf
);
1064 if ((ocf
& M_WAITOK
) && ntries
++ == 0) {
1065 struct objcache
*reclaimlist
[] = {
1067 mbufcluster_cache
, mbufphdrcluster_cache
,
1068 mbufjcluster_cache
, mbufphdrjcluster_cache
1070 const int nreclaims
= NELEM(reclaimlist
);
1072 if (!objcache_reclaimlist(reclaimlist
, nreclaims
, ocf
))
1076 ++mbstat
[mycpu
->gd_cpuid
].m_drops
;
1080 KASSERT(m
->m_data
== m
->m_pktdat
, ("mbuf %p: bad m_data in get", m
));
1083 m
->m_pkthdr
.len
= 0;
1085 updatestats(m
, type
);
1090 * Get a mbuf (not a mbuf cluster!) and zero it.
1094 m_getclr(int how
, int type
)
1098 m
= m_get(how
, type
);
1100 bzero(m
->m_data
, MLEN
);
1104 static struct mbuf
*
1105 m_getcl_cache(int how
, short type
, int flags
, struct objcache
*mbclc
,
1106 struct objcache
*mbphclc
, u_long
*cl_stats
)
1108 struct mbuf
*m
= NULL
;
1109 int ocflags
= MB_OCFLAG(how
);
1114 if (flags
& M_PKTHDR
)
1115 m
= objcache_get(mbphclc
, ocflags
);
1117 m
= objcache_get(mbclc
, ocflags
);
1120 if ((ocflags
& M_WAITOK
) && ntries
++ == 0) {
1121 struct objcache
*reclaimlist
[1];
1123 if (flags
& M_PKTHDR
)
1124 reclaimlist
[0] = mbclc
;
1126 reclaimlist
[0] = mbphclc
;
1127 if (!objcache_reclaimlist(reclaimlist
, 1, ocflags
))
1131 ++mbstat
[mycpu
->gd_cpuid
].m_drops
;
1136 KASSERT(m
->m_data
== m
->m_ext
.ext_buf
,
1137 ("mbuf %p: bad m_data in get", m
));
1141 m
->m_pkthdr
.len
= 0; /* just do it unconditonally */
1145 ++mbtypes
[mycpu
->gd_cpuid
].stats
[type
];
1151 m_getjcl(int how
, short type
, int flags
, size_t size
)
1153 struct objcache
*mbclc
, *mbphclc
;
1158 mbclc
= mbufcluster_cache
;
1159 mbphclc
= mbufphdrcluster_cache
;
1160 cl_stats
= &mbstat
[mycpu
->gd_cpuid
].m_clusters
;
1164 mbclc
= mbufjcluster_cache
;
1165 mbphclc
= mbufphdrjcluster_cache
;
1166 cl_stats
= &mbstat
[mycpu
->gd_cpuid
].m_jclusters
;
1169 return m_getcl_cache(how
, type
, flags
, mbclc
, mbphclc
, cl_stats
);
1173 * Returns an mbuf with an attached cluster.
1174 * Because many network drivers use this kind of buffers a lot, it is
1175 * convenient to keep a small pool of free buffers of this kind.
1176 * Even a small size such as 10 gives about 10% improvement in the
1177 * forwarding rate in a bridge or router.
1180 m_getcl(int how
, short type
, int flags
)
1182 return m_getcl_cache(how
, type
, flags
,
1183 mbufcluster_cache
, mbufphdrcluster_cache
,
1184 &mbstat
[mycpu
->gd_cpuid
].m_clusters
);
1188 * Allocate chain of requested length.
1191 m_getc(int len
, int how
, int type
)
1193 struct mbuf
*n
, *nfirst
= NULL
, **ntail
= &nfirst
;
1197 n
= m_getl(len
, how
, type
, 0, &nsize
);
1213 * Allocate len-worth of mbufs and/or mbuf clusters (whatever fits best)
1214 * and return a pointer to the head of the allocated chain. If m0 is
1215 * non-null, then we assume that it is a single mbuf or an mbuf chain to
1216 * which we want len bytes worth of mbufs and/or clusters attached, and so
1217 * if we succeed in allocating it, we will just return a pointer to m0.
1219 * If we happen to fail at any point during the allocation, we will free
1220 * up everything we have already allocated and return NULL.
1222 * Deprecated. Use m_getc() and m_cat() instead.
1225 m_getm(struct mbuf
*m0
, int len
, int type
, int how
)
1227 struct mbuf
*nfirst
;
1229 nfirst
= m_getc(len
, how
, type
);
1232 m_last(m0
)->m_next
= nfirst
;
1240 * Adds a cluster to a normal mbuf, M_EXT is set on success.
1241 * Deprecated. Use m_getcl() instead.
1244 m_mclget(struct mbuf
*m
, int how
)
1246 struct mbcluster
*mcl
;
1248 KKASSERT((m
->m_flags
& M_EXT
) == 0);
1249 mcl
= objcache_get(mclmeta_cache
, MB_OCFLAG(how
));
1251 linkcluster(m
, mcl
);
1252 ++mbstat
[mycpu
->gd_cpuid
].m_clusters
;
1254 ++mbstat
[mycpu
->gd_cpuid
].m_drops
;
1259 * Updates to mbcluster must be MPSAFE. Only an entity which already has
1260 * a reference to the cluster can ref it, so we are in no danger of
1261 * racing an add with a subtract. But the operation must still be atomic
1262 * since multiple entities may have a reference on the cluster.
1264 * m_mclfree() is almost the same but it must contend with two entities
1265 * freeing the cluster at the same time.
1270 struct mbcluster
*mcl
= arg
;
1272 atomic_add_int(&mcl
->mcl_refs
, 1);
1276 * When dereferencing a cluster we have to deal with a N->0 race, where
1277 * N entities free their references simultaniously. To do this we use
1278 * atomic_fetchadd_int().
1281 m_mclfree(void *arg
)
1283 struct mbcluster
*mcl
= arg
;
1285 if (atomic_fetchadd_int(&mcl
->mcl_refs
, -1) == 1) {
1286 --mbstat
[mycpu
->gd_cpuid
].m_clusters
;
1287 objcache_put(mclmeta_cache
, mcl
);
1292 m_mjclfree(void *arg
)
1294 struct mbcluster
*mcl
= arg
;
1296 if (atomic_fetchadd_int(&mcl
->mcl_refs
, -1) == 1) {
1297 --mbstat
[mycpu
->gd_cpuid
].m_jclusters
;
1298 objcache_put(mjclmeta_cache
, mcl
);
1303 * Free a single mbuf and any associated external storage. The successor,
1304 * if any, is returned.
1306 * We do need to check non-first mbuf for m_aux, since some of existing
1307 * code does not call M_PREPEND properly.
1308 * (example: call to bpf_mtap from drivers)
1314 _m_free(struct mbuf
*m
, const char *func
)
1319 m_free(struct mbuf
*m
)
1324 struct globaldata
*gd
= mycpu
;
1326 KASSERT(m
->m_type
!= MT_FREE
, ("freeing free mbuf %p", m
));
1327 KASSERT(M_TRAILINGSPACE(m
) >= 0, ("overflowed mbuf %p", m
));
1328 --mbtypes
[gd
->gd_cpuid
].stats
[m
->m_type
];
1333 * Make sure the mbuf is in constructed state before returning it
1339 m
->m_hdr
.mh_lastfunc
= func
;
1342 KKASSERT(m
->m_nextpkt
== NULL
);
1344 if (m
->m_nextpkt
!= NULL
) {
1345 static int afewtimes
= 10;
1347 if (afewtimes
-- > 0) {
1348 kprintf("mfree: m->m_nextpkt != NULL\n");
1349 print_backtrace(-1);
1351 m
->m_nextpkt
= NULL
;
1354 if (m
->m_flags
& M_PKTHDR
) {
1355 m_tag_delete_chain(m
); /* eliminate XXX JH */
1358 m
->m_flags
&= (M_EXT
| M_EXT_CLUSTER
| M_CLCACHE
| M_PHCACHE
);
1361 * Clean the M_PKTHDR state so we can return the mbuf to its original
1362 * cache. This is based on the PHCACHE flag which tells us whether
1363 * the mbuf was originally allocated out of a packet-header cache
1364 * or a non-packet-header cache.
1366 if (m
->m_flags
& M_PHCACHE
) {
1367 m
->m_flags
|= M_PKTHDR
;
1368 m
->m_pkthdr
.rcvif
= NULL
; /* eliminate XXX JH */
1369 m
->m_pkthdr
.csum_flags
= 0; /* eliminate XXX JH */
1370 m
->m_pkthdr
.fw_flags
= 0; /* eliminate XXX JH */
1371 SLIST_INIT(&m
->m_pkthdr
.tags
);
1375 * Handle remaining flags combinations. M_CLCACHE tells us whether
1376 * the mbuf was originally allocated from a cluster cache or not,
1377 * and is totally separate from whether the mbuf is currently
1378 * associated with a cluster.
1380 switch(m
->m_flags
& (M_CLCACHE
| M_EXT
| M_EXT_CLUSTER
)) {
1381 case M_CLCACHE
| M_EXT
| M_EXT_CLUSTER
:
1383 * mbuf+cluster cache case. The mbuf was allocated from the
1384 * combined mbuf_cluster cache and can be returned to the
1385 * cache if the cluster hasn't been shared.
1387 if (m_sharecount(m
) == 1) {
1389 * The cluster has not been shared, we can just
1390 * reset the data pointer and return the mbuf
1391 * to the cluster cache. Note that the reference
1392 * count is left intact (it is still associated with
1395 m
->m_data
= m
->m_ext
.ext_buf
;
1396 if (m
->m_flags
& M_EXT
&& m
->m_ext
.ext_size
!= MCLBYTES
) {
1397 if (m
->m_flags
& M_PHCACHE
)
1398 objcache_put(mbufphdrjcluster_cache
, m
);
1400 objcache_put(mbufjcluster_cache
, m
);
1401 --mbstat
[mycpu
->gd_cpuid
].m_jclusters
;
1403 if (m
->m_flags
& M_PHCACHE
)
1404 objcache_put(mbufphdrcluster_cache
, m
);
1406 objcache_put(mbufcluster_cache
, m
);
1407 --mbstat
[mycpu
->gd_cpuid
].m_clusters
;
1411 * Hell. Someone else has a ref on this cluster,
1412 * we have to disconnect it which means we can't
1413 * put it back into the mbufcluster_cache, we
1414 * have to destroy the mbuf.
1416 * Other mbuf references to the cluster will typically
1417 * be M_EXT | M_EXT_CLUSTER but without M_CLCACHE.
1419 * XXX we could try to connect another cluster to
1422 m
->m_ext
.ext_free(m
->m_ext
.ext_arg
);
1423 m
->m_flags
&= ~(M_EXT
| M_EXT_CLUSTER
);
1424 if (m
->m_ext
.ext_size
== MCLBYTES
) {
1425 if (m
->m_flags
& M_PHCACHE
)
1426 objcache_dtor(mbufphdrcluster_cache
, m
);
1428 objcache_dtor(mbufcluster_cache
, m
);
1430 if (m
->m_flags
& M_PHCACHE
)
1431 objcache_dtor(mbufphdrjcluster_cache
, m
);
1433 objcache_dtor(mbufjcluster_cache
, m
);
1437 case M_EXT
| M_EXT_CLUSTER
:
1440 * Normal cluster association case, disconnect the cluster from
1441 * the mbuf. The cluster may or may not be custom.
1443 m
->m_ext
.ext_free(m
->m_ext
.ext_arg
);
1444 m
->m_flags
&= ~(M_EXT
| M_EXT_CLUSTER
);
1448 * return the mbuf to the mbuf cache.
1450 if (m
->m_flags
& M_PHCACHE
) {
1451 m
->m_data
= m
->m_pktdat
;
1452 objcache_put(mbufphdr_cache
, m
);
1454 m
->m_data
= m
->m_dat
;
1455 objcache_put(mbuf_cache
, m
);
1457 --mbstat
[mycpu
->gd_cpuid
].m_mbufs
;
1461 panic("bad mbuf flags %p %08x", m
, m
->m_flags
);
1470 _m_freem(struct mbuf
*m
, const char *func
)
1473 m
= _m_free(m
, func
);
1479 m_freem(struct mbuf
*m
)
1488 m_extadd(struct mbuf
*m
, caddr_t buf
, u_int size
, void (*reff
)(void *),
1489 void (*freef
)(void *), void *arg
)
1491 m
->m_ext
.ext_arg
= arg
;
1492 m
->m_ext
.ext_buf
= buf
;
1493 m
->m_ext
.ext_ref
= reff
;
1494 m
->m_ext
.ext_free
= freef
;
1495 m
->m_ext
.ext_size
= size
;
1498 m
->m_flags
|= M_EXT
;
1502 * mbuf utility routines
1506 * Lesser-used path for M_PREPEND: allocate new mbuf to prepend to chain and
1510 m_prepend(struct mbuf
*m
, int len
, int how
)
1514 if (m
->m_flags
& M_PKTHDR
)
1515 mn
= m_gethdr(how
, m
->m_type
);
1517 mn
= m_get(how
, m
->m_type
);
1522 if (m
->m_flags
& M_PKTHDR
)
1523 M_MOVE_PKTHDR(mn
, m
);
1533 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
1534 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
1535 * The wait parameter is a choice of M_WAITOK/M_NOWAIT from caller.
1536 * Note that the copy is read-only, because clusters are not copied,
1537 * only their reference counts are incremented.
1540 m_copym(const struct mbuf
*m
, int off0
, int len
, int wait
)
1542 struct mbuf
*n
, **np
;
1547 KASSERT(off
>= 0, ("m_copym, negative off %d", off
));
1548 KASSERT(len
>= 0, ("m_copym, negative len %d", len
));
1549 if (off
== 0 && (m
->m_flags
& M_PKTHDR
))
1552 KASSERT(m
!= NULL
, ("m_copym, offset > size of mbuf chain"));
1562 KASSERT(len
== M_COPYALL
,
1563 ("m_copym, length > size of mbuf chain"));
1567 * Because we are sharing any cluster attachment below,
1568 * be sure to get an mbuf that does not have a cluster
1569 * associated with it.
1572 n
= m_gethdr(wait
, m
->m_type
);
1574 n
= m_get(wait
, m
->m_type
);
1579 if (!m_dup_pkthdr(n
, m
, wait
))
1581 if (len
== M_COPYALL
)
1582 n
->m_pkthdr
.len
-= off0
;
1584 n
->m_pkthdr
.len
= len
;
1587 n
->m_len
= min(len
, m
->m_len
- off
);
1588 if (m
->m_flags
& M_EXT
) {
1589 KKASSERT((n
->m_flags
& M_EXT
) == 0);
1590 n
->m_data
= m
->m_data
+ off
;
1591 m
->m_ext
.ext_ref(m
->m_ext
.ext_arg
);
1592 n
->m_ext
= m
->m_ext
;
1593 n
->m_flags
|= m
->m_flags
& (M_EXT
| M_EXT_CLUSTER
);
1595 bcopy(mtod(m
, caddr_t
)+off
, mtod(n
, caddr_t
),
1596 (unsigned)n
->m_len
);
1598 if (len
!= M_COPYALL
)
1605 ++mbstat
[mycpu
->gd_cpuid
].m_mcfail
;
1609 ++mbstat
[mycpu
->gd_cpuid
].m_mcfail
;
1614 * Copy an entire packet, including header (which must be present).
1615 * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
1616 * Note that the copy is read-only, because clusters are not copied,
1617 * only their reference counts are incremented.
1618 * Preserve alignment of the first mbuf so if the creator has left
1619 * some room at the beginning (e.g. for inserting protocol headers)
1620 * the copies also have the room available.
1623 m_copypacket(struct mbuf
*m
, int how
)
1625 struct mbuf
*top
, *n
, *o
;
1627 n
= m_gethdr(how
, m
->m_type
);
1632 if (!m_dup_pkthdr(n
, m
, how
))
1634 n
->m_len
= m
->m_len
;
1635 if (m
->m_flags
& M_EXT
) {
1636 KKASSERT((n
->m_flags
& M_EXT
) == 0);
1637 n
->m_data
= m
->m_data
;
1638 m
->m_ext
.ext_ref(m
->m_ext
.ext_arg
);
1639 n
->m_ext
= m
->m_ext
;
1640 n
->m_flags
|= m
->m_flags
& (M_EXT
| M_EXT_CLUSTER
);
1642 n
->m_data
= n
->m_pktdat
+ (m
->m_data
- m
->m_pktdat
);
1643 bcopy(mtod(m
, char *), mtod(n
, char *), n
->m_len
);
1648 o
= m_get(how
, m
->m_type
);
1655 n
->m_len
= m
->m_len
;
1656 if (m
->m_flags
& M_EXT
) {
1657 KKASSERT((n
->m_flags
& M_EXT
) == 0);
1658 n
->m_data
= m
->m_data
;
1659 m
->m_ext
.ext_ref(m
->m_ext
.ext_arg
);
1660 n
->m_ext
= m
->m_ext
;
1661 n
->m_flags
|= m
->m_flags
& (M_EXT
| M_EXT_CLUSTER
);
1663 bcopy(mtod(m
, char *), mtod(n
, char *), n
->m_len
);
1671 ++mbstat
[mycpu
->gd_cpuid
].m_mcfail
;
1676 * Copy data from an mbuf chain starting "off" bytes from the beginning,
1677 * continuing for "len" bytes, into the indicated buffer.
1680 m_copydata(const struct mbuf
*m
, int off
, int len
, caddr_t cp
)
1684 KASSERT(off
>= 0, ("m_copydata, negative off %d", off
));
1685 KASSERT(len
>= 0, ("m_copydata, negative len %d", len
));
1687 KASSERT(m
!= NULL
, ("m_copydata, offset > size of mbuf chain"));
1694 KASSERT(m
!= NULL
, ("m_copydata, length > size of mbuf chain"));
1695 count
= min(m
->m_len
- off
, len
);
1696 bcopy(mtod(m
, caddr_t
) + off
, cp
, count
);
1705 * Copy a packet header mbuf chain into a completely new chain, including
1706 * copying any mbuf clusters. Use this instead of m_copypacket() when
1707 * you need a writable copy of an mbuf chain.
1710 m_dup(struct mbuf
*m
, int how
)
1712 struct mbuf
**p
, *top
= NULL
;
1713 int remain
, moff
, nsize
;
1718 KASSERT((m
->m_flags
& M_PKTHDR
) != 0, ("%s: !PKTHDR", __func__
));
1720 /* While there's more data, get a new mbuf, tack it on, and fill it */
1721 remain
= m
->m_pkthdr
.len
;
1724 while (remain
> 0 || top
== NULL
) { /* allow m->m_pkthdr.len == 0 */
1727 /* Get the next new mbuf */
1728 n
= m_getl(remain
, how
, m
->m_type
, top
== NULL
? M_PKTHDR
: 0,
1733 if (!m_dup_pkthdr(n
, m
, how
))
1736 /* Link it into the new chain */
1740 /* Copy data from original mbuf(s) into new mbuf */
1742 while (n
->m_len
< nsize
&& m
!= NULL
) {
1743 int chunk
= min(nsize
- n
->m_len
, m
->m_len
- moff
);
1745 bcopy(m
->m_data
+ moff
, n
->m_data
+ n
->m_len
, chunk
);
1749 if (moff
== m
->m_len
) {
1755 /* Check correct total mbuf length */
1756 KASSERT((remain
> 0 && m
!= NULL
) || (remain
== 0 && m
== NULL
),
1757 ("%s: bogus m_pkthdr.len", __func__
));
1764 ++mbstat
[mycpu
->gd_cpuid
].m_mcfail
;
1769 * Copy the non-packet mbuf data chain into a new set of mbufs, including
1770 * copying any mbuf clusters. This is typically used to realign a data
1771 * chain by nfs_realign().
1773 * The original chain is left intact. how should be M_WAITOK or M_NOWAIT
1774 * and NULL can be returned if M_NOWAIT is passed.
1776 * Be careful to use cluster mbufs, a large mbuf chain converted to non
1777 * cluster mbufs can exhaust our supply of mbufs.
1780 m_dup_data(struct mbuf
*m
, int how
)
1782 struct mbuf
**p
, *n
, *top
= NULL
;
1783 int mlen
, moff
, chunk
, gsize
, nsize
;
1792 * Optimize the mbuf allocation but do not get too carried away.
1794 if (m
->m_next
|| m
->m_len
> MLEN
)
1795 if (m
->m_flags
& M_EXT
&& m
->m_ext
.ext_size
== MCLBYTES
)
1798 gsize
= MJUMPAGESIZE
;
1808 * Scan the mbuf chain until nothing is left, the new mbuf chain
1809 * will be allocated on the fly as needed.
1816 KKASSERT(m
->m_type
== MT_DATA
);
1818 n
= m_getl(gsize
, how
, MT_DATA
, 0, &nsize
);
1825 chunk
= imin(mlen
, nsize
);
1826 bcopy(m
->m_data
+ moff
, n
->m_data
+ n
->m_len
, chunk
);
1841 ++mbstat
[mycpu
->gd_cpuid
].m_mcfail
;
1846 * Concatenate mbuf chain n to m.
1847 * Both chains must be of the same type (e.g. MT_DATA).
1848 * Any m_pkthdr is not updated.
1851 m_cat(struct mbuf
*m
, struct mbuf
*n
)
1855 if (m
->m_flags
& M_EXT
||
1856 m
->m_data
+ m
->m_len
+ n
->m_len
>= &m
->m_dat
[MLEN
]) {
1857 /* just join the two chains */
1861 /* splat the data from one into the other */
1862 bcopy(mtod(n
, caddr_t
), mtod(m
, caddr_t
) + m
->m_len
,
1864 m
->m_len
+= n
->m_len
;
1870 m_adj(struct mbuf
*mp
, int req_len
)
1876 if ((m
= mp
) == NULL
)
1882 while (m
!= NULL
&& len
> 0) {
1883 if (m
->m_len
<= len
) {
1894 if (mp
->m_flags
& M_PKTHDR
)
1895 m
->m_pkthdr
.len
-= (req_len
- len
);
1898 * Trim from tail. Scan the mbuf chain,
1899 * calculating its length and finding the last mbuf.
1900 * If the adjustment only affects this mbuf, then just
1901 * adjust and return. Otherwise, rescan and truncate
1902 * after the remaining size.
1908 if (m
->m_next
== NULL
)
1912 if (m
->m_len
>= len
) {
1914 if (mp
->m_flags
& M_PKTHDR
)
1915 mp
->m_pkthdr
.len
-= len
;
1922 * Correct length for chain is "count".
1923 * Find the mbuf with last data, adjust its length,
1924 * and toss data from remaining mbufs on chain.
1927 if (m
->m_flags
& M_PKTHDR
)
1928 m
->m_pkthdr
.len
= count
;
1929 for (; m
; m
= m
->m_next
) {
1930 if (m
->m_len
>= count
) {
1937 (m
= m
->m_next
) ->m_len
= 0;
1942 * Set the m_data pointer of a newly-allocated mbuf
1943 * to place an object of the specified size at the
1944 * end of the mbuf, longword aligned.
1947 m_align(struct mbuf
*m
, int len
)
1951 if (m
->m_flags
& M_EXT
)
1952 adjust
= m
->m_ext
.ext_size
- len
;
1953 else if (m
->m_flags
& M_PKTHDR
)
1954 adjust
= MHLEN
- len
;
1956 adjust
= MLEN
- len
;
1957 m
->m_data
+= adjust
&~ (sizeof(long)-1);
1961 * Create a writable copy of the mbuf chain. While doing this
1962 * we compact the chain with a goal of producing a chain with
1963 * at most two mbufs. The second mbuf in this chain is likely
1964 * to be a cluster. The primary purpose of this work is to create
1965 * a writable packet for encryption, compression, etc. The
1966 * secondary goal is to linearize the data so the data can be
1967 * passed to crypto hardware in the most efficient manner possible.
1970 m_unshare(struct mbuf
*m0
, int how
)
1972 struct mbuf
*m
, *mprev
;
1973 struct mbuf
*n
, *mfirst
, *mlast
;
1977 for (m
= m0
; m
!= NULL
; m
= mprev
->m_next
) {
1979 * Regular mbufs are ignored unless there's a cluster
1980 * in front of it that we can use to coalesce. We do
1981 * the latter mainly so later clusters can be coalesced
1982 * also w/o having to handle them specially (i.e. convert
1983 * mbuf+cluster -> cluster). This optimization is heavily
1984 * influenced by the assumption that we're running over
1985 * Ethernet where MCLBYTES is large enough that the max
1986 * packet size will permit lots of coalescing into a
1987 * single cluster. This in turn permits efficient
1988 * crypto operations, especially when using hardware.
1990 if ((m
->m_flags
& M_EXT
) == 0) {
1991 if (mprev
&& (mprev
->m_flags
& M_EXT
) &&
1992 m
->m_len
<= M_TRAILINGSPACE(mprev
)) {
1993 /* XXX: this ignores mbuf types */
1994 memcpy(mtod(mprev
, caddr_t
) + mprev
->m_len
,
1995 mtod(m
, caddr_t
), m
->m_len
);
1996 mprev
->m_len
+= m
->m_len
;
1997 mprev
->m_next
= m
->m_next
; /* unlink from chain */
1998 m_free(m
); /* reclaim mbuf */
2005 * Writable mbufs are left alone (for now).
2007 if (M_WRITABLE(m
)) {
2013 * Not writable, replace with a copy or coalesce with
2014 * the previous mbuf if possible (since we have to copy
2015 * it anyway, we try to reduce the number of mbufs and
2016 * clusters so that future work is easier).
2018 KASSERT(m
->m_flags
& M_EXT
, ("m_flags 0x%x", m
->m_flags
));
2019 /* NB: we only coalesce into a cluster or larger */
2020 if (mprev
!= NULL
&& (mprev
->m_flags
& M_EXT
) &&
2021 m
->m_len
<= M_TRAILINGSPACE(mprev
)) {
2022 /* XXX: this ignores mbuf types */
2023 memcpy(mtod(mprev
, caddr_t
) + mprev
->m_len
,
2024 mtod(m
, caddr_t
), m
->m_len
);
2025 mprev
->m_len
+= m
->m_len
;
2026 mprev
->m_next
= m
->m_next
; /* unlink from chain */
2027 m_free(m
); /* reclaim mbuf */
2032 * Allocate new space to hold the copy...
2034 /* XXX why can M_PKTHDR be set past the first mbuf? */
2035 if (mprev
== NULL
&& (m
->m_flags
& M_PKTHDR
)) {
2037 * NB: if a packet header is present we must
2038 * allocate the mbuf separately from any cluster
2039 * because M_MOVE_PKTHDR will smash the data
2040 * pointer and drop the M_EXT marker.
2042 MGETHDR(n
, how
, m
->m_type
);
2047 M_MOVE_PKTHDR(n
, m
);
2049 if ((n
->m_flags
& M_EXT
) == 0) {
2055 n
= m_getcl(how
, m
->m_type
, m
->m_flags
);
2062 * ... and copy the data. We deal with jumbo mbufs
2063 * (i.e. m_len > MCLBYTES) by splitting them into
2064 * clusters. We could just malloc a buffer and make
2065 * it external but too many device drivers don't know
2066 * how to break up the non-contiguous memory when
2074 int cc
= min(len
, MCLBYTES
);
2075 memcpy(mtod(n
, caddr_t
), mtod(m
, caddr_t
) + off
, cc
);
2086 n
= m_getcl(how
, m
->m_type
, m
->m_flags
);
2093 n
->m_next
= m
->m_next
;
2095 m0
= mfirst
; /* new head of chain */
2097 mprev
->m_next
= mfirst
; /* replace old mbuf */
2098 m_free(m
); /* release old mbuf */
2105 * Rearrange an mbuf chain so that len bytes are contiguous
2106 * and in the data area of an mbuf (so that mtod will work for a structure
2107 * of size len). Returns the resulting mbuf chain on success, frees it and
2108 * returns null on failure. If there is room, it will add up to
2109 * max_protohdr-len extra bytes to the contiguous region in an attempt to
2110 * avoid being called next time.
2113 m_pullup(struct mbuf
*n
, int len
)
2120 * If first mbuf has no cluster, and has room for len bytes
2121 * without shifting current data, pullup into it,
2122 * otherwise allocate a new mbuf to prepend to the chain.
2124 if (!(n
->m_flags
& M_EXT
) &&
2125 n
->m_data
+ len
< &n
->m_dat
[MLEN
] &&
2127 if (n
->m_len
>= len
)
2135 if (n
->m_flags
& M_PKTHDR
)
2136 m
= m_gethdr(M_NOWAIT
, n
->m_type
);
2138 m
= m_get(M_NOWAIT
, n
->m_type
);
2142 if (n
->m_flags
& M_PKTHDR
)
2143 M_MOVE_PKTHDR(m
, n
);
2145 space
= &m
->m_dat
[MLEN
] - (m
->m_data
+ m
->m_len
);
2147 count
= min(min(max(len
, max_protohdr
), space
), n
->m_len
);
2148 bcopy(mtod(n
, caddr_t
), mtod(m
, caddr_t
) + m
->m_len
,
2158 } while (len
> 0 && n
);
2167 ++mbstat
[mycpu
->gd_cpuid
].m_mcfail
;
2172 * Partition an mbuf chain in two pieces, returning the tail --
2173 * all but the first len0 bytes. In case of failure, it returns NULL and
2174 * attempts to restore the chain to its original state.
2176 * Note that the resulting mbufs might be read-only, because the new
2177 * mbuf can end up sharing an mbuf cluster with the original mbuf if
2178 * the "breaking point" happens to lie within a cluster mbuf. Use the
2179 * M_WRITABLE() macro to check for this case.
2182 m_split(struct mbuf
*m0
, int len0
, int wait
)
2185 unsigned len
= len0
, remain
;
2187 for (m
= m0
; m
&& len
> m
->m_len
; m
= m
->m_next
)
2191 remain
= m
->m_len
- len
;
2192 if (m0
->m_flags
& M_PKTHDR
) {
2193 n
= m_gethdr(wait
, m0
->m_type
);
2196 n
->m_pkthdr
.rcvif
= m0
->m_pkthdr
.rcvif
;
2197 n
->m_pkthdr
.len
= m0
->m_pkthdr
.len
- len0
;
2198 m0
->m_pkthdr
.len
= len0
;
2199 if (m
->m_flags
& M_EXT
)
2201 if (remain
> MHLEN
) {
2202 /* m can't be the lead packet */
2204 n
->m_next
= m_split(m
, len
, wait
);
2205 if (n
->m_next
== NULL
) {
2213 MH_ALIGN(n
, remain
);
2214 } else if (remain
== 0) {
2219 n
= m_get(wait
, m
->m_type
);
2225 if (m
->m_flags
& M_EXT
) {
2226 KKASSERT((n
->m_flags
& M_EXT
) == 0);
2227 n
->m_data
= m
->m_data
+ len
;
2228 m
->m_ext
.ext_ref(m
->m_ext
.ext_arg
);
2229 n
->m_ext
= m
->m_ext
;
2230 n
->m_flags
|= m
->m_flags
& (M_EXT
| M_EXT_CLUSTER
);
2232 bcopy(mtod(m
, caddr_t
) + len
, mtod(n
, caddr_t
), remain
);
2236 n
->m_next
= m
->m_next
;
2242 * Routine to copy from device local memory into mbufs.
2243 * Note: "offset" is ill-defined and always called as 0, so ignore it.
2246 m_devget(char *buf
, int len
, int offset
, struct ifnet
*ifp
)
2248 struct mbuf
*m
, *mfirst
= NULL
, **mtail
;
2255 m
= m_getl(len
, M_NOWAIT
, MT_DATA
, flags
, &nsize
);
2260 m
->m_len
= min(len
, nsize
);
2262 if (flags
& M_PKTHDR
) {
2263 if (len
+ max_linkhdr
<= nsize
)
2264 m
->m_data
+= max_linkhdr
;
2265 m
->m_pkthdr
.rcvif
= ifp
;
2266 m
->m_pkthdr
.len
= len
;
2270 bcopy(buf
, m
->m_data
, (unsigned)m
->m_len
);
2281 * Routine to pad mbuf to the specified length 'padto'.
2284 m_devpad(struct mbuf
*m
, int padto
)
2286 struct mbuf
*last
= NULL
;
2289 if (padto
<= m
->m_pkthdr
.len
)
2292 padlen
= padto
- m
->m_pkthdr
.len
;
2294 /* if there's only the packet-header and we can pad there, use it. */
2295 if (m
->m_pkthdr
.len
== m
->m_len
&& M_TRAILINGSPACE(m
) >= padlen
) {
2299 * Walk packet chain to find last mbuf. We will either
2300 * pad there, or append a new mbuf and pad it
2302 for (last
= m
; last
->m_next
!= NULL
; last
= last
->m_next
)
2305 /* `last' now points to last in chain. */
2306 if (M_TRAILINGSPACE(last
) < padlen
) {
2309 /* Allocate new empty mbuf, pad it. Compact later. */
2310 MGET(n
, M_NOWAIT
, MT_DATA
);
2318 KKASSERT(M_TRAILINGSPACE(last
) >= padlen
);
2319 KKASSERT(M_WRITABLE(last
));
2321 /* Now zero the pad area */
2322 bzero(mtod(last
, char *) + last
->m_len
, padlen
);
2323 last
->m_len
+= padlen
;
2324 m
->m_pkthdr
.len
+= padlen
;
2329 * Copy data from a buffer back into the indicated mbuf chain,
2330 * starting "off" bytes from the beginning, extending the mbuf
2331 * chain if necessary.
2334 m_copyback(struct mbuf
*m0
, int off
, int len
, caddr_t cp
)
2337 struct mbuf
*m
= m0
, *n
;
2342 while (off
> (mlen
= m
->m_len
)) {
2345 if (m
->m_next
== NULL
) {
2346 n
= m_getclr(M_NOWAIT
, m
->m_type
);
2349 n
->m_len
= min(MLEN
, len
+ off
);
2355 mlen
= min (m
->m_len
- off
, len
);
2356 bcopy(cp
, off
+ mtod(m
, caddr_t
), (unsigned)mlen
);
2364 if (m
->m_next
== NULL
) {
2365 n
= m_get(M_NOWAIT
, m
->m_type
);
2368 n
->m_len
= min(MLEN
, len
);
2373 out
: if (((m
= m0
)->m_flags
& M_PKTHDR
) && (m
->m_pkthdr
.len
< totlen
))
2374 m
->m_pkthdr
.len
= totlen
;
2378 * Append the specified data to the indicated mbuf chain,
2379 * Extend the mbuf chain if the new data does not fit in
2382 * Return 1 if able to complete the job; otherwise 0.
2385 m_append(struct mbuf
*m0
, int len
, c_caddr_t cp
)
2388 int remainder
, space
;
2390 for (m
= m0
; m
->m_next
!= NULL
; m
= m
->m_next
)
2393 space
= M_TRAILINGSPACE(m
);
2396 * Copy into available space.
2398 if (space
> remainder
)
2400 bcopy(cp
, mtod(m
, caddr_t
) + m
->m_len
, space
);
2402 cp
+= space
, remainder
-= space
;
2404 while (remainder
> 0) {
2406 * Allocate a new mbuf; could check space
2407 * and allocate a cluster instead.
2409 n
= m_get(M_NOWAIT
, m
->m_type
);
2412 n
->m_len
= min(MLEN
, remainder
);
2413 bcopy(cp
, mtod(n
, caddr_t
), n
->m_len
);
2414 cp
+= n
->m_len
, remainder
-= n
->m_len
;
2418 if (m0
->m_flags
& M_PKTHDR
)
2419 m0
->m_pkthdr
.len
+= len
- remainder
;
2420 return (remainder
== 0);
2424 * Apply function f to the data in an mbuf chain starting "off" bytes from
2425 * the beginning, continuing for "len" bytes.
2428 m_apply(struct mbuf
*m
, int off
, int len
,
2429 int (*f
)(void *, void *, u_int
), void *arg
)
2434 KASSERT(off
>= 0, ("m_apply, negative off %d", off
));
2435 KASSERT(len
>= 0, ("m_apply, negative len %d", len
));
2437 KASSERT(m
!= NULL
, ("m_apply, offset > size of mbuf chain"));
2444 KASSERT(m
!= NULL
, ("m_apply, offset > size of mbuf chain"));
2445 count
= min(m
->m_len
- off
, len
);
2446 rval
= (*f
)(arg
, mtod(m
, caddr_t
) + off
, count
);
2457 * Return a pointer to mbuf/offset of location in mbuf chain.
2460 m_getptr(struct mbuf
*m
, int loc
, int *off
)
2464 /* Normal end of search. */
2465 if (m
->m_len
> loc
) {
2470 if (m
->m_next
== NULL
) {
2472 /* Point at the end of valid data. */
2485 m_print(const struct mbuf
*m
)
2488 const struct mbuf
*m2
;
2491 len
= m
->m_pkthdr
.len
;
2493 hexstr
= kmalloc(HEX_NCPYLEN(len
), M_TEMP
, M_ZERO
| M_WAITOK
);
2495 kprintf("%p %s\n", m2
, hexncpy(m2
->m_data
, m2
->m_len
, hexstr
,
2496 HEX_NCPYLEN(m2
->m_len
), "-"));
2500 kfree(hexstr
, M_TEMP
);
2505 * "Move" mbuf pkthdr from "from" to "to".
2506 * "from" must have M_PKTHDR set, and "to" must be empty.
2509 m_move_pkthdr(struct mbuf
*to
, struct mbuf
*from
)
2511 KASSERT((to
->m_flags
& M_PKTHDR
), ("m_move_pkthdr: not packet header"));
2513 to
->m_flags
|= from
->m_flags
& M_COPYFLAGS
;
2514 to
->m_pkthdr
= from
->m_pkthdr
; /* especially tags */
2515 SLIST_INIT(&from
->m_pkthdr
.tags
); /* purge tags from src */
2519 * Duplicate "from"'s mbuf pkthdr in "to".
2520 * "from" must have M_PKTHDR set, and "to" must be empty.
2521 * In particular, this does a deep copy of the packet tags.
2524 m_dup_pkthdr(struct mbuf
*to
, const struct mbuf
*from
, int how
)
2526 KASSERT((to
->m_flags
& M_PKTHDR
), ("m_dup_pkthdr: not packet header"));
2528 to
->m_flags
= (from
->m_flags
& M_COPYFLAGS
) |
2529 (to
->m_flags
& ~M_COPYFLAGS
);
2530 to
->m_pkthdr
= from
->m_pkthdr
;
2531 SLIST_INIT(&to
->m_pkthdr
.tags
);
2532 return (m_tag_copy_chain(to
, from
, how
));
2536 * Defragment a mbuf chain, returning the shortest possible
2537 * chain of mbufs and clusters. If allocation fails and
2538 * this cannot be completed, NULL will be returned, but
2539 * the passed in chain will be unchanged. Upon success,
2540 * the original chain will be freed, and the new chain
2543 * If a non-packet header is passed in, the original
2544 * mbuf (chain?) will be returned unharmed.
2546 * m_defrag_nofree doesn't free the passed in mbuf.
2549 m_defrag(struct mbuf
*m0
, int how
)
2553 if ((m_new
= m_defrag_nofree(m0
, how
)) == NULL
)
2561 m_defrag_nofree(struct mbuf
*m0
, int how
)
2563 struct mbuf
*m_new
= NULL
, *m_final
= NULL
;
2564 int progress
= 0, length
, nsize
;
2566 if (!(m0
->m_flags
& M_PKTHDR
))
2569 #ifdef MBUF_STRESS_TEST
2570 if (m_defragrandomfailures
) {
2571 int temp
= karc4random() & 0xff;
2577 m_final
= m_getl(m0
->m_pkthdr
.len
, how
, MT_DATA
, M_PKTHDR
, &nsize
);
2578 if (m_final
== NULL
)
2580 m_final
->m_len
= 0; /* in case m0->m_pkthdr.len is zero */
2582 if (m_dup_pkthdr(m_final
, m0
, how
) == 0)
2587 while (progress
< m0
->m_pkthdr
.len
) {
2588 length
= m0
->m_pkthdr
.len
- progress
;
2589 if (length
> MCLBYTES
)
2592 if (m_new
== NULL
) {
2593 m_new
= m_getl(length
, how
, MT_DATA
, 0, &nsize
);
2598 m_copydata(m0
, progress
, length
, mtod(m_new
, caddr_t
));
2600 m_new
->m_len
= length
;
2601 if (m_new
!= m_final
)
2602 m_cat(m_final
, m_new
);
2605 if (m0
->m_next
== NULL
)
2608 m_defragbytes
+= m_final
->m_pkthdr
.len
;
2619 * Move data from uio into mbufs.
2622 m_uiomove(struct uio
*uio
)
2624 struct mbuf
*m
; /* current working mbuf */
2625 struct mbuf
*head
= NULL
; /* result mbuf chain */
2626 struct mbuf
**mp
= &head
;
2627 int flags
= M_PKTHDR
;
2633 if (uio
->uio_resid
> INT_MAX
)
2636 resid
= (int)uio
->uio_resid
;
2637 m
= m_getl(resid
, M_WAITOK
, MT_DATA
, flags
, &nsize
);
2639 m
->m_pkthdr
.len
= 0;
2640 /* Leave room for protocol headers. */
2645 m
->m_len
= imin(nsize
, resid
);
2646 error
= uiomove(mtod(m
, caddr_t
), m
->m_len
, uio
);
2653 head
->m_pkthdr
.len
+= m
->m_len
;
2654 } while (uio
->uio_resid
> 0);
2664 m_last(struct mbuf
*m
)
2672 * Return the number of bytes in an mbuf chain.
2673 * If lastm is not NULL, also return the last mbuf.
2676 m_lengthm(struct mbuf
*m
, struct mbuf
**lastm
)
2679 struct mbuf
*prev
= m
;
2692 * Like m_lengthm(), except also keep track of mbuf usage.
2695 m_countm(struct mbuf
*m
, struct mbuf
**lastm
, u_int
*pmbcnt
)
2697 u_int len
= 0, mbcnt
= 0;
2698 struct mbuf
*prev
= m
;
2703 if (m
->m_flags
& M_EXT
)
2704 mbcnt
+= m
->m_ext
.ext_size
;