2 * Copyright (c) 2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
35 * Copyright (c) 1982, 1986, 1988, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94
67 * $FreeBSD: src/sys/kern/uipc_mbuf.c,v 1.51.2.24 2003/04/15 06:59:29 silby Exp $
68 * $DragonFly: src/sys/kern/uipc_mbuf.c,v 1.62 2007/05/13 22:56:59 dillon Exp $
71 #include "opt_param.h"
73 #include "opt_mbuf_stress_test.h"
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/malloc.h>
78 #include <sys/kernel.h>
79 #include <sys/sysctl.h>
80 #include <sys/domain.h>
81 #include <sys/objcache.h>
82 #include <sys/protosw.h>
84 #include <sys/thread.h>
85 #include <sys/globaldata.h>
86 #include <sys/serialize.h>
87 #include <sys/thread2.h>
90 #include <vm/vm_kern.h>
91 #include <vm/vm_extern.h>
94 #include <machine/cpu.h>
98 * mbuf cluster meta-data
103 struct lwkt_serialize mcl_serializer
;
106 static void mbinit(void *);
107 SYSINIT(mbuf
, SI_BOOT2_MACHDEP
, SI_ORDER_FIRST
, mbinit
, NULL
)
109 static u_long mbtypes
[MT_NTYPES
];
111 struct mbstat mbstat
;
120 #ifdef MBUF_STRESS_TEST
121 int m_defragrandomfailures
;
124 struct objcache
*mbuf_cache
, *mbufphdr_cache
;
125 struct objcache
*mclmeta_cache
;
126 struct objcache
*mbufcluster_cache
, *mbufphdrcluster_cache
;
131 SYSCTL_INT(_kern_ipc
, KIPC_MAX_LINKHDR
, max_linkhdr
, CTLFLAG_RW
,
132 &max_linkhdr
, 0, "");
133 SYSCTL_INT(_kern_ipc
, KIPC_MAX_PROTOHDR
, max_protohdr
, CTLFLAG_RW
,
134 &max_protohdr
, 0, "");
135 SYSCTL_INT(_kern_ipc
, KIPC_MAX_HDR
, max_hdr
, CTLFLAG_RW
, &max_hdr
, 0, "");
136 SYSCTL_INT(_kern_ipc
, KIPC_MAX_DATALEN
, max_datalen
, CTLFLAG_RW
,
137 &max_datalen
, 0, "");
138 SYSCTL_INT(_kern_ipc
, OID_AUTO
, mbuf_wait
, CTLFLAG_RW
,
140 SYSCTL_STRUCT(_kern_ipc
, KIPC_MBSTAT
, mbstat
, CTLFLAG_RW
, &mbstat
, mbstat
, "");
141 SYSCTL_OPAQUE(_kern_ipc
, OID_AUTO
, mbtypes
, CTLFLAG_RD
, mbtypes
,
142 sizeof(mbtypes
), "LU", "");
145 * These are read-only because we do not currently have any code
146 * to adjust the objcache limits after the fact. The variables
147 * may only be set as boot-time tunables.
149 SYSCTL_INT(_kern_ipc
, KIPC_NMBCLUSTERS
, nmbclusters
, CTLFLAG_RD
,
150 &nmbclusters
, 0, "Maximum number of mbuf clusters available");
151 SYSCTL_INT(_kern_ipc
, OID_AUTO
, nmbufs
, CTLFLAG_RD
, &nmbufs
, 0,
152 "Maximum number of mbufs available");
154 SYSCTL_INT(_kern_ipc
, OID_AUTO
, m_defragpackets
, CTLFLAG_RD
,
155 &m_defragpackets
, 0, "");
156 SYSCTL_INT(_kern_ipc
, OID_AUTO
, m_defragbytes
, CTLFLAG_RD
,
157 &m_defragbytes
, 0, "");
158 SYSCTL_INT(_kern_ipc
, OID_AUTO
, m_defraguseless
, CTLFLAG_RD
,
159 &m_defraguseless
, 0, "");
160 SYSCTL_INT(_kern_ipc
, OID_AUTO
, m_defragfailure
, CTLFLAG_RD
,
161 &m_defragfailure
, 0, "");
162 #ifdef MBUF_STRESS_TEST
163 SYSCTL_INT(_kern_ipc
, OID_AUTO
, m_defragrandomfailures
, CTLFLAG_RW
,
164 &m_defragrandomfailures
, 0, "");
167 static MALLOC_DEFINE(M_MBUF
, "mbuf", "mbuf");
168 static MALLOC_DEFINE(M_MBUFCL
, "mbufcl", "mbufcl");
169 static MALLOC_DEFINE(M_MCLMETA
, "mclmeta", "mclmeta");
171 static void m_reclaim (void);
172 static void m_mclref(void *arg
);
173 static void m_mclfree(void *arg
);
176 #define NMBCLUSTERS (512 + maxusers * 16)
179 #define NMBUFS (nmbclusters * 2)
183 * Perform sanity checks of tunables declared above.
186 tunable_mbinit(void *dummy
)
189 * This has to be done before VM init.
191 nmbclusters
= NMBCLUSTERS
;
192 TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters
);
194 TUNABLE_INT_FETCH("kern.ipc.nmbufs", &nmbufs
);
196 if (nmbufs
< nmbclusters
* 2)
197 nmbufs
= nmbclusters
* 2;
199 SYSINIT(tunable_mbinit
, SI_BOOT1_TUNABLES
, SI_ORDER_ANY
,
200 tunable_mbinit
, NULL
);
202 /* "number of clusters of pages" */
208 * The mbuf object cache only guarantees that m_next and m_nextpkt are
209 * NULL and that m_data points to the beginning of the data area. In
210 * particular, m_len and m_pkthdr.len are uninitialized. It is the
211 * responsibility of the caller to initialize those fields before use.
214 static boolean_t __inline
215 mbuf_ctor(void *obj
, void *private, int ocflags
)
217 struct mbuf
*m
= obj
;
221 m
->m_data
= m
->m_dat
;
228 * Initialize the mbuf and the packet header fields.
231 mbufphdr_ctor(void *obj
, void *private, int ocflags
)
233 struct mbuf
*m
= obj
;
237 m
->m_data
= m
->m_pktdat
;
238 m
->m_flags
= M_PKTHDR
| M_PHCACHE
;
240 m
->m_pkthdr
.rcvif
= NULL
; /* eliminate XXX JH */
241 SLIST_INIT(&m
->m_pkthdr
.tags
);
242 m
->m_pkthdr
.csum_flags
= 0; /* eliminate XXX JH */
243 m
->m_pkthdr
.fw_flags
= 0; /* eliminate XXX JH */
249 * A mbcluster object consists of 2K (MCLBYTES) cluster and a refcount.
252 mclmeta_ctor(void *obj
, void *private, int ocflags
)
254 struct mbcluster
*cl
= obj
;
257 if (ocflags
& M_NOWAIT
)
258 buf
= kmalloc(MCLBYTES
, M_MBUFCL
, M_NOWAIT
| M_ZERO
);
260 buf
= kmalloc(MCLBYTES
, M_MBUFCL
, M_INTWAIT
| M_ZERO
);
265 lwkt_serialize_init(&cl
->mcl_serializer
);
270 mclmeta_dtor(void *obj
, void *private)
272 struct mbcluster
*mcl
= obj
;
274 KKASSERT(mcl
->mcl_refs
== 0);
275 kfree(mcl
->mcl_data
, M_MBUFCL
);
279 linkcluster(struct mbuf
*m
, struct mbcluster
*cl
)
282 * Add the cluster to the mbuf. The caller will detect that the
283 * mbuf now has an attached cluster.
285 m
->m_ext
.ext_arg
= cl
;
286 m
->m_ext
.ext_buf
= cl
->mcl_data
;
287 m
->m_ext
.ext_ref
= m_mclref
;
288 m
->m_ext
.ext_free
= m_mclfree
;
289 m
->m_ext
.ext_size
= MCLBYTES
;
290 atomic_add_int(&cl
->mcl_refs
, 1);
292 m
->m_data
= m
->m_ext
.ext_buf
;
293 m
->m_flags
|= M_EXT
| M_EXT_CLUSTER
;
297 mbufphdrcluster_ctor(void *obj
, void *private, int ocflags
)
299 struct mbuf
*m
= obj
;
300 struct mbcluster
*cl
;
302 mbufphdr_ctor(obj
, private, ocflags
);
303 cl
= objcache_get(mclmeta_cache
, ocflags
);
306 m
->m_flags
|= M_CLCACHE
;
312 mbufcluster_ctor(void *obj
, void *private, int ocflags
)
314 struct mbuf
*m
= obj
;
315 struct mbcluster
*cl
;
317 mbuf_ctor(obj
, private, ocflags
);
318 cl
= objcache_get(mclmeta_cache
, ocflags
);
321 m
->m_flags
|= M_CLCACHE
;
327 * Used for both the cluster and cluster PHDR caches.
329 * The mbuf may have lost its cluster due to sharing, deal
330 * with the situation by checking M_EXT.
333 mbufcluster_dtor(void *obj
, void *private)
335 struct mbuf
*m
= obj
;
336 struct mbcluster
*mcl
;
338 if (m
->m_flags
& M_EXT
) {
339 KKASSERT((m
->m_flags
& M_EXT_CLUSTER
) != 0);
340 mcl
= m
->m_ext
.ext_arg
;
341 KKASSERT(mcl
->mcl_refs
== 1);
343 objcache_put(mclmeta_cache
, mcl
);
347 struct objcache_malloc_args mbuf_malloc_args
= { MSIZE
, M_MBUF
};
348 struct objcache_malloc_args mclmeta_malloc_args
=
349 { sizeof(struct mbcluster
), M_MCLMETA
};
355 mbstat
.m_msize
= MSIZE
;
356 mbstat
.m_mclbytes
= MCLBYTES
;
357 mbstat
.m_minclsize
= MINCLSIZE
;
358 mbstat
.m_mlen
= MLEN
;
359 mbstat
.m_mhlen
= MHLEN
;
361 mbuf_cache
= objcache_create("mbuf", nmbufs
, 0,
362 mbuf_ctor
, NULL
, NULL
,
363 objcache_malloc_alloc
, objcache_malloc_free
, &mbuf_malloc_args
);
364 mbufphdr_cache
= objcache_create("mbuf pkt hdr", nmbufs
, 64,
365 mbufphdr_ctor
, NULL
, NULL
,
366 objcache_malloc_alloc
, objcache_malloc_free
, &mbuf_malloc_args
);
367 mclmeta_cache
= objcache_create("cluster mbuf", nmbclusters
, 0,
368 mclmeta_ctor
, mclmeta_dtor
, NULL
,
369 objcache_malloc_alloc
, objcache_malloc_free
, &mclmeta_malloc_args
);
370 mbufcluster_cache
= objcache_create("mbuf + cluster", nmbclusters
, 0,
371 mbufcluster_ctor
, mbufcluster_dtor
, NULL
,
372 objcache_malloc_alloc
, objcache_malloc_free
, &mbuf_malloc_args
);
373 mbufphdrcluster_cache
= objcache_create("mbuf pkt hdr + cluster",
374 nmbclusters
, 64, mbufphdrcluster_ctor
, mbufcluster_dtor
, NULL
,
375 objcache_malloc_alloc
, objcache_malloc_free
, &mbuf_malloc_args
);
380 * Return the number of references to this mbuf's data. 0 is returned
381 * if the mbuf is not M_EXT, a reference count is returned if it is
382 * M_EXT | M_EXT_CLUSTER, and 99 is returned if it is a special M_EXT.
385 m_sharecount(struct mbuf
*m
)
387 switch (m
->m_flags
& (M_EXT
| M_EXT_CLUSTER
)) {
392 case M_EXT
| M_EXT_CLUSTER
:
393 return (((struct mbcluster
*)m
->m_ext
.ext_arg
)->mcl_refs
);
396 return (0); /* to shut up compiler */
400 * change mbuf to new type
403 m_chtype(struct mbuf
*m
, int type
)
407 --mbtypes
[m
->m_type
];
419 SLIST_FOREACH(dp
, &domains
, dom_next
) {
420 for (pr
= dp
->dom_protosw
; pr
< dp
->dom_protoswNPROTOSW
; pr
++) {
430 updatestats(struct mbuf
*m
, int type
)
444 m_get(int how
, int type
)
448 int ocf
= MBTOM(how
);
452 m
= objcache_get(mbuf_cache
, ocf
);
455 if ((how
& MB_TRYWAIT
) && ntries
++ == 0) {
456 struct objcache
*reclaimlist
[] = {
458 mbufcluster_cache
, mbufphdrcluster_cache
460 const int nreclaims
= __arysize(reclaimlist
);
462 if (!objcache_reclaimlist(reclaimlist
, nreclaims
, ocf
))
469 updatestats(m
, type
);
474 m_gethdr(int how
, int type
)
477 int ocf
= MBTOM(how
);
482 m
= objcache_get(mbufphdr_cache
, ocf
);
485 if ((how
& MB_TRYWAIT
) && ntries
++ == 0) {
486 struct objcache
*reclaimlist
[] = {
488 mbufcluster_cache
, mbufphdrcluster_cache
490 const int nreclaims
= __arysize(reclaimlist
);
492 if (!objcache_reclaimlist(reclaimlist
, nreclaims
, ocf
))
499 updatestats(m
, type
);
504 * Get a mbuf (not a mbuf cluster!) and zero it.
508 m_getclr(int how
, int type
)
512 m
= m_get(how
, type
);
514 bzero(m
->m_data
, MLEN
);
519 * Returns an mbuf with an attached cluster.
520 * Because many network drivers use this kind of buffers a lot, it is
521 * convenient to keep a small pool of free buffers of this kind.
522 * Even a small size such as 10 gives about 10% improvement in the
523 * forwarding rate in a bridge or router.
526 m_getcl(int how
, short type
, int flags
)
529 int ocflags
= MBTOM(how
);
534 if (flags
& M_PKTHDR
)
535 m
= objcache_get(mbufphdrcluster_cache
, ocflags
);
537 m
= objcache_get(mbufcluster_cache
, ocflags
);
540 if ((how
& MB_TRYWAIT
) && ntries
++ == 0) {
541 struct objcache
*reclaimlist
[1];
543 if (flags
& M_PKTHDR
)
544 reclaimlist
[0] = mbufcluster_cache
;
546 reclaimlist
[0] = mbufphdrcluster_cache
;
547 if (!objcache_reclaimlist(reclaimlist
, 1, ocflags
))
564 * Allocate chain of requested length.
567 m_getc(int len
, int how
, int type
)
569 struct mbuf
*n
, *nfirst
= NULL
, **ntail
= &nfirst
;
573 n
= m_getl(len
, how
, type
, 0, &nsize
);
589 * Allocate len-worth of mbufs and/or mbuf clusters (whatever fits best)
590 * and return a pointer to the head of the allocated chain. If m0 is
591 * non-null, then we assume that it is a single mbuf or an mbuf chain to
592 * which we want len bytes worth of mbufs and/or clusters attached, and so
593 * if we succeed in allocating it, we will just return a pointer to m0.
595 * If we happen to fail at any point during the allocation, we will free
596 * up everything we have already allocated and return NULL.
598 * Deprecated. Use m_getc() and m_cat() instead.
601 m_getm(struct mbuf
*m0
, int len
, int type
, int how
)
605 nfirst
= m_getc(len
, how
, type
);
608 m_last(m0
)->m_next
= nfirst
;
616 * Adds a cluster to a normal mbuf, M_EXT is set on success.
617 * Deprecated. Use m_getcl() instead.
620 m_mclget(struct mbuf
*m
, int how
)
622 struct mbcluster
*mcl
;
624 KKASSERT((m
->m_flags
& M_EXT
) == 0);
625 mcl
= objcache_get(mclmeta_cache
, MBTOM(how
));
630 /* leave the m_mbufs count intact for original mbuf */
636 * Updates to mbcluster must be MPSAFE. Only an entity which already has
637 * a reference to the cluster can ref it, so we are in no danger of
638 * racing an add with a subtract. But the operation must still be atomic
639 * since multiple entities may have a reference on the cluster.
641 * m_mclfree() is almost the same but it must contend with two entities
642 * freeing the cluster at the same time. If there is only one reference
643 * count we are the only entity referencing the cluster and no further
644 * locking is required. Otherwise we must protect against a race to 0
645 * with the serializer.
650 struct mbcluster
*mcl
= arg
;
652 atomic_add_int(&mcl
->mcl_refs
, 1);
658 struct mbcluster
*mcl
= arg
;
660 if (mcl
->mcl_refs
== 1) {
662 objcache_put(mclmeta_cache
, mcl
);
664 lwkt_serialize_enter(&mcl
->mcl_serializer
);
665 if (mcl
->mcl_refs
> 1) {
666 atomic_subtract_int(&mcl
->mcl_refs
, 1);
667 lwkt_serialize_exit(&mcl
->mcl_serializer
);
669 lwkt_serialize_exit(&mcl
->mcl_serializer
);
670 KKASSERT(mcl
->mcl_refs
== 1);
672 objcache_put(mclmeta_cache
, mcl
);
677 extern void db_print_backtrace(void);
680 * Free a single mbuf and any associated external storage. The successor,
681 * if any, is returned.
683 * We do need to check non-first mbuf for m_aux, since some of existing
684 * code does not call M_PREPEND properly.
685 * (example: call to bpf_mtap from drivers)
688 m_free(struct mbuf
*m
)
692 KASSERT(m
->m_type
!= MT_FREE
, ("freeing free mbuf %p", m
));
693 --mbtypes
[m
->m_type
];
698 * Make sure the mbuf is in constructed state before returning it
703 KKASSERT(m
->m_nextpkt
== NULL
);
705 if (m
->m_nextpkt
!= NULL
) {
707 static int afewtimes
= 10;
709 if (afewtimes
-- > 0) {
710 kprintf("mfree: m->m_nextpkt != NULL\n");
711 db_print_backtrace();
717 if (m
->m_flags
& M_PKTHDR
) {
718 m_tag_delete_chain(m
); /* eliminate XXX JH */
721 m
->m_flags
&= (M_EXT
| M_EXT_CLUSTER
| M_CLCACHE
| M_PHCACHE
);
724 * Clean the M_PKTHDR state so we can return the mbuf to its original
725 * cache. This is based on the PHCACHE flag which tells us whether
726 * the mbuf was originally allocated out of a packet-header cache
727 * or a non-packet-header cache.
729 if (m
->m_flags
& M_PHCACHE
) {
730 m
->m_flags
|= M_PKTHDR
;
731 m
->m_pkthdr
.rcvif
= NULL
; /* eliminate XXX JH */
732 m
->m_pkthdr
.csum_flags
= 0; /* eliminate XXX JH */
733 m
->m_pkthdr
.fw_flags
= 0; /* eliminate XXX JH */
734 SLIST_INIT(&m
->m_pkthdr
.tags
);
738 * Handle remaining flags combinations. M_CLCACHE tells us whether
739 * the mbuf was originally allocated from a cluster cache or not,
740 * and is totally separate from whether the mbuf is currently
741 * associated with a cluster.
744 switch(m
->m_flags
& (M_CLCACHE
| M_EXT
| M_EXT_CLUSTER
)) {
745 case M_CLCACHE
| M_EXT
| M_EXT_CLUSTER
:
747 * mbuf+cluster cache case. The mbuf was allocated from the
748 * combined mbuf_cluster cache and can be returned to the
749 * cache if the cluster hasn't been shared.
751 if (m_sharecount(m
) == 1) {
753 * The cluster has not been shared, we can just
754 * reset the data pointer and return the mbuf
755 * to the cluster cache. Note that the reference
756 * count is left intact (it is still associated with
759 m
->m_data
= m
->m_ext
.ext_buf
;
760 if (m
->m_flags
& M_PHCACHE
)
761 objcache_put(mbufphdrcluster_cache
, m
);
763 objcache_put(mbufcluster_cache
, m
);
767 * Hell. Someone else has a ref on this cluster,
768 * we have to disconnect it which means we can't
769 * put it back into the mbufcluster_cache, we
770 * have to destroy the mbuf.
772 * Other mbuf references to the cluster will typically
773 * be M_EXT | M_EXT_CLUSTER but without M_CLCACHE.
775 * XXX we could try to connect another cluster to
778 m
->m_ext
.ext_free(m
->m_ext
.ext_arg
);
779 m
->m_flags
&= ~(M_EXT
| M_EXT_CLUSTER
);
780 if (m
->m_flags
& M_PHCACHE
)
781 objcache_dtor(mbufphdrcluster_cache
, m
);
783 objcache_dtor(mbufcluster_cache
, m
);
786 case M_EXT
| M_EXT_CLUSTER
:
788 * Normal cluster associated with an mbuf that was allocated
789 * from the normal mbuf pool rather then the cluster pool.
790 * The cluster has to be independantly disassociated from the
793 if (m_sharecount(m
) == 1)
798 * Normal cluster association case, disconnect the cluster from
799 * the mbuf. The cluster may or may not be custom.
801 m
->m_ext
.ext_free(m
->m_ext
.ext_arg
);
802 m
->m_flags
&= ~(M_EXT
| M_EXT_CLUSTER
);
806 * return the mbuf to the mbuf cache.
808 if (m
->m_flags
& M_PHCACHE
) {
809 m
->m_data
= m
->m_pktdat
;
810 objcache_put(mbufphdr_cache
, m
);
812 m
->m_data
= m
->m_dat
;
813 objcache_put(mbuf_cache
, m
);
819 panic("bad mbuf flags %p %08x\n", m
, m
->m_flags
);
827 m_freem(struct mbuf
*m
)
836 * mbuf utility routines
840 * Lesser-used path for M_PREPEND: allocate new mbuf to prepend to chain and
844 m_prepend(struct mbuf
*m
, int len
, int how
)
848 if (m
->m_flags
& M_PKTHDR
)
849 mn
= m_gethdr(how
, m
->m_type
);
851 mn
= m_get(how
, m
->m_type
);
856 if (m
->m_flags
& M_PKTHDR
)
857 M_MOVE_PKTHDR(mn
, m
);
867 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
868 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
869 * The wait parameter is a choice of MB_WAIT/MB_DONTWAIT from caller.
870 * Note that the copy is read-only, because clusters are not copied,
871 * only their reference counts are incremented.
874 m_copym(const struct mbuf
*m
, int off0
, int len
, int wait
)
876 struct mbuf
*n
, **np
;
881 KASSERT(off
>= 0, ("m_copym, negative off %d", off
));
882 KASSERT(len
>= 0, ("m_copym, negative len %d", len
));
883 if (off
== 0 && m
->m_flags
& M_PKTHDR
)
886 KASSERT(m
!= NULL
, ("m_copym, offset > size of mbuf chain"));
896 KASSERT(len
== M_COPYALL
,
897 ("m_copym, length > size of mbuf chain"));
901 * Because we are sharing any cluster attachment below,
902 * be sure to get an mbuf that does not have a cluster
903 * associated with it.
906 n
= m_gethdr(wait
, m
->m_type
);
908 n
= m_get(wait
, m
->m_type
);
913 if (!m_dup_pkthdr(n
, m
, wait
))
915 if (len
== M_COPYALL
)
916 n
->m_pkthdr
.len
-= off0
;
918 n
->m_pkthdr
.len
= len
;
921 n
->m_len
= min(len
, m
->m_len
- off
);
922 if (m
->m_flags
& M_EXT
) {
923 KKASSERT((n
->m_flags
& M_EXT
) == 0);
924 n
->m_data
= m
->m_data
+ off
;
925 m
->m_ext
.ext_ref(m
->m_ext
.ext_arg
);
927 n
->m_flags
|= m
->m_flags
& (M_EXT
| M_EXT_CLUSTER
);
929 bcopy(mtod(m
, caddr_t
)+off
, mtod(n
, caddr_t
),
932 if (len
!= M_COPYALL
)
948 * Copy an entire packet, including header (which must be present).
949 * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
950 * Note that the copy is read-only, because clusters are not copied,
951 * only their reference counts are incremented.
952 * Preserve alignment of the first mbuf so if the creator has left
953 * some room at the beginning (e.g. for inserting protocol headers)
954 * the copies also have the room available.
957 m_copypacket(struct mbuf
*m
, int how
)
959 struct mbuf
*top
, *n
, *o
;
961 n
= m_gethdr(how
, m
->m_type
);
966 if (!m_dup_pkthdr(n
, m
, how
))
969 if (m
->m_flags
& M_EXT
) {
970 KKASSERT((n
->m_flags
& M_EXT
) == 0);
971 n
->m_data
= m
->m_data
;
972 m
->m_ext
.ext_ref(m
->m_ext
.ext_arg
);
974 n
->m_flags
|= m
->m_flags
& (M_EXT
| M_EXT_CLUSTER
);
976 n
->m_data
= n
->m_pktdat
+ (m
->m_data
- m
->m_pktdat
);
977 bcopy(mtod(m
, char *), mtod(n
, char *), n
->m_len
);
982 o
= m_get(how
, m
->m_type
);
990 if (m
->m_flags
& M_EXT
) {
991 KKASSERT((n
->m_flags
& M_EXT
) == 0);
992 n
->m_data
= m
->m_data
;
993 m
->m_ext
.ext_ref(m
->m_ext
.ext_arg
);
995 n
->m_flags
|= m
->m_flags
& (M_EXT
| M_EXT_CLUSTER
);
997 bcopy(mtod(m
, char *), mtod(n
, char *), n
->m_len
);
1010 * Copy data from an mbuf chain starting "off" bytes from the beginning,
1011 * continuing for "len" bytes, into the indicated buffer.
1014 m_copydata(const struct mbuf
*m
, int off
, int len
, caddr_t cp
)
1018 KASSERT(off
>= 0, ("m_copydata, negative off %d", off
));
1019 KASSERT(len
>= 0, ("m_copydata, negative len %d", len
));
1021 KASSERT(m
!= NULL
, ("m_copydata, offset > size of mbuf chain"));
1028 KASSERT(m
!= NULL
, ("m_copydata, length > size of mbuf chain"));
1029 count
= min(m
->m_len
- off
, len
);
1030 bcopy(mtod(m
, caddr_t
) + off
, cp
, count
);
1039 * Copy a packet header mbuf chain into a completely new chain, including
1040 * copying any mbuf clusters. Use this instead of m_copypacket() when
1041 * you need a writable copy of an mbuf chain.
1044 m_dup(struct mbuf
*m
, int how
)
1046 struct mbuf
**p
, *top
= NULL
;
1047 int remain
, moff
, nsize
;
1052 KASSERT((m
->m_flags
& M_PKTHDR
) != 0, ("%s: !PKTHDR", __func__
));
1054 /* While there's more data, get a new mbuf, tack it on, and fill it */
1055 remain
= m
->m_pkthdr
.len
;
1058 while (remain
> 0 || top
== NULL
) { /* allow m->m_pkthdr.len == 0 */
1061 /* Get the next new mbuf */
1062 n
= m_getl(remain
, how
, m
->m_type
, top
== NULL
? M_PKTHDR
: 0,
1067 if (!m_dup_pkthdr(n
, m
, how
))
1070 /* Link it into the new chain */
1074 /* Copy data from original mbuf(s) into new mbuf */
1076 while (n
->m_len
< nsize
&& m
!= NULL
) {
1077 int chunk
= min(nsize
- n
->m_len
, m
->m_len
- moff
);
1079 bcopy(m
->m_data
+ moff
, n
->m_data
+ n
->m_len
, chunk
);
1083 if (moff
== m
->m_len
) {
1089 /* Check correct total mbuf length */
1090 KASSERT((remain
> 0 && m
!= NULL
) || (remain
== 0 && m
== NULL
),
1091 ("%s: bogus m_pkthdr.len", __func__
));
1103 * Concatenate mbuf chain n to m.
1104 * Both chains must be of the same type (e.g. MT_DATA).
1105 * Any m_pkthdr is not updated.
1108 m_cat(struct mbuf
*m
, struct mbuf
*n
)
1112 if (m
->m_flags
& M_EXT
||
1113 m
->m_data
+ m
->m_len
+ n
->m_len
>= &m
->m_dat
[MLEN
]) {
1114 /* just join the two chains */
1118 /* splat the data from one into the other */
1119 bcopy(mtod(n
, caddr_t
), mtod(m
, caddr_t
) + m
->m_len
,
1121 m
->m_len
+= n
->m_len
;
1127 m_adj(struct mbuf
*mp
, int req_len
)
1133 if ((m
= mp
) == NULL
)
1139 while (m
!= NULL
&& len
> 0) {
1140 if (m
->m_len
<= len
) {
1151 if (mp
->m_flags
& M_PKTHDR
)
1152 m
->m_pkthdr
.len
-= (req_len
- len
);
1155 * Trim from tail. Scan the mbuf chain,
1156 * calculating its length and finding the last mbuf.
1157 * If the adjustment only affects this mbuf, then just
1158 * adjust and return. Otherwise, rescan and truncate
1159 * after the remaining size.
1165 if (m
->m_next
== (struct mbuf
*)0)
1169 if (m
->m_len
>= len
) {
1171 if (mp
->m_flags
& M_PKTHDR
)
1172 mp
->m_pkthdr
.len
-= len
;
1179 * Correct length for chain is "count".
1180 * Find the mbuf with last data, adjust its length,
1181 * and toss data from remaining mbufs on chain.
1184 if (m
->m_flags
& M_PKTHDR
)
1185 m
->m_pkthdr
.len
= count
;
1186 for (; m
; m
= m
->m_next
) {
1187 if (m
->m_len
>= count
) {
1194 (m
= m
->m_next
) ->m_len
= 0;
1199 * Rearrange an mbuf chain so that len bytes are contiguous
1200 * and in the data area of an mbuf (so that mtod will work for a structure
1201 * of size len). Returns the resulting mbuf chain on success, frees it and
1202 * returns null on failure. If there is room, it will add up to
1203 * max_protohdr-len extra bytes to the contiguous region in an attempt to
1204 * avoid being called next time.
1207 m_pullup(struct mbuf
*n
, int len
)
1214 * If first mbuf has no cluster, and has room for len bytes
1215 * without shifting current data, pullup into it,
1216 * otherwise allocate a new mbuf to prepend to the chain.
1218 if (!(n
->m_flags
& M_EXT
) &&
1219 n
->m_data
+ len
< &n
->m_dat
[MLEN
] &&
1221 if (n
->m_len
>= len
)
1229 if (n
->m_flags
& M_PKTHDR
)
1230 m
= m_gethdr(MB_DONTWAIT
, n
->m_type
);
1232 m
= m_get(MB_DONTWAIT
, n
->m_type
);
1236 if (n
->m_flags
& M_PKTHDR
)
1237 M_MOVE_PKTHDR(m
, n
);
1239 space
= &m
->m_dat
[MLEN
] - (m
->m_data
+ m
->m_len
);
1241 count
= min(min(max(len
, max_protohdr
), space
), n
->m_len
);
1242 bcopy(mtod(n
, caddr_t
), mtod(m
, caddr_t
) + m
->m_len
,
1252 } while (len
> 0 && n
);
1266 * Partition an mbuf chain in two pieces, returning the tail --
1267 * all but the first len0 bytes. In case of failure, it returns NULL and
1268 * attempts to restore the chain to its original state.
1270 * Note that the resulting mbufs might be read-only, because the new
1271 * mbuf can end up sharing an mbuf cluster with the original mbuf if
1272 * the "breaking point" happens to lie within a cluster mbuf. Use the
1273 * M_WRITABLE() macro to check for this case.
1276 m_split(struct mbuf
*m0
, int len0
, int wait
)
1279 unsigned len
= len0
, remain
;
1281 for (m
= m0
; m
&& len
> m
->m_len
; m
= m
->m_next
)
1285 remain
= m
->m_len
- len
;
1286 if (m0
->m_flags
& M_PKTHDR
) {
1287 n
= m_gethdr(wait
, m0
->m_type
);
1290 n
->m_pkthdr
.rcvif
= m0
->m_pkthdr
.rcvif
;
1291 n
->m_pkthdr
.len
= m0
->m_pkthdr
.len
- len0
;
1292 m0
->m_pkthdr
.len
= len0
;
1293 if (m
->m_flags
& M_EXT
)
1295 if (remain
> MHLEN
) {
1296 /* m can't be the lead packet */
1298 n
->m_next
= m_split(m
, len
, wait
);
1299 if (n
->m_next
== NULL
) {
1307 MH_ALIGN(n
, remain
);
1308 } else if (remain
== 0) {
1313 n
= m_get(wait
, m
->m_type
);
1319 if (m
->m_flags
& M_EXT
) {
1320 KKASSERT((n
->m_flags
& M_EXT
) == 0);
1321 n
->m_data
= m
->m_data
+ len
;
1322 m
->m_ext
.ext_ref(m
->m_ext
.ext_arg
);
1323 n
->m_ext
= m
->m_ext
;
1324 n
->m_flags
|= m
->m_flags
& (M_EXT
| M_EXT_CLUSTER
);
1326 bcopy(mtod(m
, caddr_t
) + len
, mtod(n
, caddr_t
), remain
);
1330 n
->m_next
= m
->m_next
;
1336 * Routine to copy from device local memory into mbufs.
1337 * Note: "offset" is ill-defined and always called as 0, so ignore it.
1340 m_devget(char *buf
, int len
, int offset
, struct ifnet
*ifp
,
1341 void (*copy
)(volatile const void *from
, volatile void *to
, size_t length
))
1343 struct mbuf
*m
, *mfirst
= NULL
, **mtail
;
1352 m
= m_getl(len
, MB_DONTWAIT
, MT_DATA
, flags
, &nsize
);
1357 m
->m_len
= min(len
, nsize
);
1359 if (flags
& M_PKTHDR
) {
1360 if (len
+ max_linkhdr
<= nsize
)
1361 m
->m_data
+= max_linkhdr
;
1362 m
->m_pkthdr
.rcvif
= ifp
;
1363 m
->m_pkthdr
.len
= len
;
1367 copy(buf
, m
->m_data
, (unsigned)m
->m_len
);
1378 * Copy data from a buffer back into the indicated mbuf chain,
1379 * starting "off" bytes from the beginning, extending the mbuf
1380 * chain if necessary.
1383 m_copyback(struct mbuf
*m0
, int off
, int len
, caddr_t cp
)
1386 struct mbuf
*m
= m0
, *n
;
1391 while (off
> (mlen
= m
->m_len
)) {
1394 if (m
->m_next
== NULL
) {
1395 n
= m_getclr(MB_DONTWAIT
, m
->m_type
);
1398 n
->m_len
= min(MLEN
, len
+ off
);
1404 mlen
= min (m
->m_len
- off
, len
);
1405 bcopy(cp
, off
+ mtod(m
, caddr_t
), (unsigned)mlen
);
1413 if (m
->m_next
== NULL
) {
1414 n
= m_get(MB_DONTWAIT
, m
->m_type
);
1417 n
->m_len
= min(MLEN
, len
);
1422 out
: if (((m
= m0
)->m_flags
& M_PKTHDR
) && (m
->m_pkthdr
.len
< totlen
))
1423 m
->m_pkthdr
.len
= totlen
;
1427 m_print(const struct mbuf
*m
)
1430 const struct mbuf
*m2
;
1432 len
= m
->m_pkthdr
.len
;
1435 kprintf("%p %*D\n", m2
, m2
->m_len
, (u_char
*)m2
->m_data
, "-");
1443 * "Move" mbuf pkthdr from "from" to "to".
1444 * "from" must have M_PKTHDR set, and "to" must be empty.
1447 m_move_pkthdr(struct mbuf
*to
, struct mbuf
*from
)
1449 KASSERT((to
->m_flags
& M_PKTHDR
), ("m_move_pkthdr: not packet header"));
1451 to
->m_flags
|= from
->m_flags
& M_COPYFLAGS
;
1452 to
->m_pkthdr
= from
->m_pkthdr
; /* especially tags */
1453 SLIST_INIT(&from
->m_pkthdr
.tags
); /* purge tags from src */
1457 * Duplicate "from"'s mbuf pkthdr in "to".
1458 * "from" must have M_PKTHDR set, and "to" must be empty.
1459 * In particular, this does a deep copy of the packet tags.
1462 m_dup_pkthdr(struct mbuf
*to
, const struct mbuf
*from
, int how
)
1464 KASSERT((to
->m_flags
& M_PKTHDR
), ("m_dup_pkthdr: not packet header"));
1466 to
->m_flags
= (from
->m_flags
& M_COPYFLAGS
) |
1467 (to
->m_flags
& ~M_COPYFLAGS
);
1468 to
->m_pkthdr
= from
->m_pkthdr
;
1469 SLIST_INIT(&to
->m_pkthdr
.tags
);
1470 return (m_tag_copy_chain(to
, from
, how
));
1474 * Defragment a mbuf chain, returning the shortest possible
1475 * chain of mbufs and clusters. If allocation fails and
1476 * this cannot be completed, NULL will be returned, but
1477 * the passed in chain will be unchanged. Upon success,
1478 * the original chain will be freed, and the new chain
1481 * If a non-packet header is passed in, the original
1482 * mbuf (chain?) will be returned unharmed.
1484 * m_defrag_nofree doesn't free the passed in mbuf.
1487 m_defrag(struct mbuf
*m0
, int how
)
1491 if ((m_new
= m_defrag_nofree(m0
, how
)) == NULL
)
1499 m_defrag_nofree(struct mbuf
*m0
, int how
)
1501 struct mbuf
*m_new
= NULL
, *m_final
= NULL
;
1502 int progress
= 0, length
, nsize
;
1504 if (!(m0
->m_flags
& M_PKTHDR
))
1507 #ifdef MBUF_STRESS_TEST
1508 if (m_defragrandomfailures
) {
1509 int temp
= karc4random() & 0xff;
1515 m_final
= m_getl(m0
->m_pkthdr
.len
, how
, MT_DATA
, M_PKTHDR
, &nsize
);
1516 if (m_final
== NULL
)
1518 m_final
->m_len
= 0; /* in case m0->m_pkthdr.len is zero */
1520 if (m_dup_pkthdr(m_final
, m0
, how
) == NULL
)
1525 while (progress
< m0
->m_pkthdr
.len
) {
1526 length
= m0
->m_pkthdr
.len
- progress
;
1527 if (length
> MCLBYTES
)
1530 if (m_new
== NULL
) {
1531 m_new
= m_getl(length
, how
, MT_DATA
, 0, &nsize
);
1536 m_copydata(m0
, progress
, length
, mtod(m_new
, caddr_t
));
1538 m_new
->m_len
= length
;
1539 if (m_new
!= m_final
)
1540 m_cat(m_final
, m_new
);
1543 if (m0
->m_next
== NULL
)
1546 m_defragbytes
+= m_final
->m_pkthdr
.len
;
1557 * Move data from uio into mbufs.
1560 m_uiomove(struct uio
*uio
)
1562 struct mbuf
*m
; /* current working mbuf */
1563 struct mbuf
*head
= NULL
; /* result mbuf chain */
1564 struct mbuf
**mp
= &head
;
1565 int resid
= uio
->uio_resid
, nsize
, flags
= M_PKTHDR
, error
;
1568 m
= m_getl(resid
, MB_WAIT
, MT_DATA
, flags
, &nsize
);
1570 m
->m_pkthdr
.len
= 0;
1571 /* Leave room for protocol headers. */
1576 m
->m_len
= min(nsize
, resid
);
1577 error
= uiomove(mtod(m
, caddr_t
), m
->m_len
, uio
);
1584 head
->m_pkthdr
.len
+= m
->m_len
;
1586 } while (resid
> 0);
1596 m_last(struct mbuf
*m
)
1604 * Return the number of bytes in an mbuf chain.
1605 * If lastm is not NULL, also return the last mbuf.
1608 m_lengthm(struct mbuf
*m
, struct mbuf
**lastm
)
1611 struct mbuf
*prev
= m
;
1624 * Like m_lengthm(), except also keep track of mbuf usage.
1627 m_countm(struct mbuf
*m
, struct mbuf
**lastm
, u_int
*pmbcnt
)
1629 u_int len
= 0, mbcnt
= 0;
1630 struct mbuf
*prev
= m
;
1635 if (m
->m_flags
& M_EXT
)
1636 mbcnt
+= m
->m_ext
.ext_size
;