2 * Copyright (c) 2004, 2005,
3 * Bosko Milekic <bmilekic@FreeBSD.org>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include "opt_param.h"
34 #include <sys/param.h>
36 #include <sys/malloc.h>
37 #include <sys/systm.h>
39 #include <sys/domain.h>
40 #include <sys/eventhandler.h>
41 #include <sys/kernel.h>
42 #include <sys/protosw.h>
44 #include <sys/sysctl.h>
47 #include <vm/vm_page.h>
49 #include <vm/uma_int.h>
50 #include <vm/uma_dbg.h>
53 * In FreeBSD, Mbufs and Mbuf Clusters are allocated from UMA
56 * Mbuf Clusters (2K, contiguous) are allocated from the Cluster
57 * Zone. The Zone can be capped at kern.ipc.nmbclusters, if the
58 * administrator so desires.
60 * Mbufs are allocated from a UMA Master Zone called the Mbuf
63 * Additionally, FreeBSD provides a Packet Zone, which it
64 * configures as a Secondary Zone to the Mbuf Master Zone,
65 * thus sharing backend Slab kegs with the Mbuf Master Zone.
67 * Thus common-case allocations and locking are simplified:
71 * | .------------>[(Packet Cache)] m_get(), m_gethdr()
73 * [(Cluster Cache)] [ Secondary ] [ (Mbuf Cache) ]
74 * [ Cluster Zone ] [ Zone ] [ Mbuf Master Zone ]
80 * \____________(VM)_________________/
83 * Whenever an object is allocated with uma_zalloc() out of
84 * one of the Zones its _ctor_ function is executed. The same
85 * for any deallocation through uma_zfree() the _dtor_ function
88 * Caches are per-CPU and are filled from the Master Zone.
90 * Whenever an object is allocated from the underlying global
91 * memory pool it gets pre-initialized with the _zinit_ functions.
92 * When the Keg's are overfull objects get decomissioned with
93 * _zfini_ functions and free'd back to the global memory pool.
97 int nmbclusters; /* limits number of mbuf clusters */
98 int nmbjumbop; /* limits number of page size jumbo clusters */
99 int nmbjumbo9; /* limits number of 9k jumbo clusters */
100 int nmbjumbo16; /* limits number of 16k jumbo clusters */
101 struct mbstat mbstat;
104 tunable_mbinit(void *dummy)
107 /* This has to be done before VM init. */
108 nmbclusters = 1024 + maxusers * 64;
109 TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters);
111 SYSINIT(tunable_mbinit, SI_SUB_TUNABLES, SI_ORDER_ANY, tunable_mbinit, NULL);
113 /* XXX: These should be tuneables. Can't change UMA limits on the fly. */
115 sysctl_nmbclusters(SYSCTL_HANDLER_ARGS)
117 int error, newnmbclusters;
119 newnmbclusters = nmbclusters;
120 error = sysctl_handle_int(oidp, &newnmbclusters, sizeof(int), req);
121 if (error == 0 && req->newptr) {
122 if (newnmbclusters > nmbclusters) {
123 nmbclusters = newnmbclusters;
124 uma_zone_set_max(zone_clust, nmbclusters);
125 EVENTHANDLER_INVOKE(nmbclusters_change);
131 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbclusters, CTLTYPE_INT|CTLFLAG_RW,
132 &nmbclusters, 0, sysctl_nmbclusters, "IU",
133 "Maximum number of mbuf clusters allowed");
134 SYSCTL_INT(_kern_ipc, OID_AUTO, nmbjumbop, CTLFLAG_RW, &nmbjumbop, 0,
135 "Maximum number of mbuf page size jumbo clusters allowed");
136 SYSCTL_INT(_kern_ipc, OID_AUTO, nmbjumbo9, CTLFLAG_RW, &nmbjumbo9, 0,
137 "Maximum number of mbuf 9k jumbo clusters allowed");
138 SYSCTL_INT(_kern_ipc, OID_AUTO, nmbjumbo16, CTLFLAG_RW, &nmbjumbo16, 0,
139 "Maximum number of mbuf 16k jumbo clusters allowed");
140 SYSCTL_STRUCT(_kern_ipc, OID_AUTO, mbstat, CTLFLAG_RD, &mbstat, mbstat,
141 "Mbuf general information and statistics");
144 * Zones from which we allocate.
146 uma_zone_t zone_mbuf;
147 uma_zone_t zone_clust;
148 uma_zone_t zone_pack;
149 uma_zone_t zone_jumbop;
150 uma_zone_t zone_jumbo9;
151 uma_zone_t zone_jumbo16;
152 uma_zone_t zone_ext_refcnt;
153 uma_zone_t zone_mtag_vlan;
158 static int mb_ctor_mbuf(void *, int, void *, int);
159 static int mb_ctor_clust(void *, int, void *, int);
160 static int mb_ctor_pack(void *, int, void *, int);
161 static void mb_dtor_mbuf(void *, int, void *);
162 static void mb_dtor_clust(void *, int, void *);
163 static void mb_dtor_pack(void *, int, void *);
164 static int mb_zinit_pack(void *, int, int);
165 static void mb_zfini_pack(void *, int);
166 static int mt_zinit_vlan(void *, int, int);
168 static void mb_reclaim(void *);
169 static void mbuf_init(void *);
171 /* Ensure that MSIZE doesn't break dtom() - it must be a power of 2 */
172 CTASSERT((((MSIZE - 1) ^ MSIZE) + 1) >> 1 == MSIZE);
175 * Initialize FreeBSD Network buffer allocation.
177 SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbuf_init, NULL)
179 mbuf_init(void *dummy)
183 * Configure UMA zones for Mbufs, Clusters, and Packets.
185 zone_mbuf = uma_zcreate(MBUF_MEM_NAME, MSIZE,
186 mb_ctor_mbuf, mb_dtor_mbuf,
188 trash_init, trash_fini,
192 MSIZE - 1, UMA_ZONE_MAXBUCKET);
194 zone_clust = uma_zcreate(MBUF_CLUSTER_MEM_NAME, MCLBYTES,
195 mb_ctor_clust, mb_dtor_clust,
197 trash_init, trash_fini,
201 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
203 uma_zone_set_max(zone_clust, nmbclusters);
205 zone_pack = uma_zsecond_create(MBUF_PACKET_MEM_NAME, mb_ctor_pack,
206 mb_dtor_pack, mb_zinit_pack, mb_zfini_pack, zone_mbuf);
208 /* Make jumbo frame zone too. Page size, 9k and 16k. */
209 zone_jumbop = uma_zcreate(MBUF_JUMBOP_MEM_NAME, MJUMPAGESIZE,
210 mb_ctor_clust, mb_dtor_clust,
212 trash_init, trash_fini,
216 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
218 uma_zone_set_max(zone_jumbop, nmbjumbop);
220 zone_jumbo9 = uma_zcreate(MBUF_JUMBO9_MEM_NAME, MJUM9BYTES,
221 mb_ctor_clust, mb_dtor_clust,
223 trash_init, trash_fini,
227 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
229 uma_zone_set_max(zone_jumbo9, nmbjumbo9);
231 zone_jumbo16 = uma_zcreate(MBUF_JUMBO16_MEM_NAME, MJUM16BYTES,
232 mb_ctor_clust, mb_dtor_clust,
234 trash_init, trash_fini,
238 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
240 uma_zone_set_max(zone_jumbo16, nmbjumbo16);
242 zone_ext_refcnt = uma_zcreate(MBUF_EXTREFCNT_MEM_NAME, sizeof(u_int),
245 UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
247 zone_mtag_vlan = uma_zcreate("mtag_vlan",
248 sizeof(struct m_tag) + sizeof(u_int),
253 /* uma_prealloc() goes here... */
256 * Hook event handler for low-memory situation, used to
257 * drain protocols and push data back to the caches (UMA
258 * later pushes it back to VM).
260 EVENTHANDLER_REGISTER(vm_lowmem, mb_reclaim, NULL,
261 EVENTHANDLER_PRI_FIRST);
264 * [Re]set counters and local statistics knobs.
265 * XXX Some of these should go and be replaced, but UMA stat
266 * gathering needs to be revised.
269 mbstat.m_mclusts = 0;
271 mbstat.m_msize = MSIZE;
272 mbstat.m_mclbytes = MCLBYTES;
273 mbstat.m_minclsize = MINCLSIZE;
274 mbstat.m_mlen = MLEN;
275 mbstat.m_mhlen = MHLEN;
276 mbstat.m_numtypes = MT_NTYPES;
278 mbstat.m_mcfail = mbstat.m_mpfail = 0;
280 mbstat.sf_allocwait = mbstat.sf_allocfail = 0;
284 * Constructor for Mbuf master zone.
286 * The 'arg' pointer points to a mb_args structure which
287 * contains call-specific information required to support the
288 * mbuf allocation API. See mbuf.h.
291 mb_ctor_mbuf(void *mem, int size, void *arg, int how)
294 struct mb_args *args;
302 trash_ctor(mem, size, arg, how);
304 m = (struct mbuf *)mem;
305 args = (struct mb_args *)arg;
310 * The mbuf is initialized later. The caller has the
311 * responsibility to set up any MAC labels too.
313 if (type == MT_NOINIT)
321 if (flags & M_PKTHDR) {
322 m->m_data = m->m_pktdat;
323 m->m_pkthdr.rcvif = NULL;
325 m->m_pkthdr.header = NULL;
326 m->m_pkthdr.csum_flags = 0;
327 m->m_pkthdr.csum_data = 0;
328 SLIST_INIT(&m->m_pkthdr.tags);
330 /* If the label init fails, fail the alloc */
331 error = mac_init_mbuf(m, how);
336 m->m_data = m->m_dat;
341 * The Mbuf master zone destructor.
344 mb_dtor_mbuf(void *mem, int size, void *arg)
348 m = (struct mbuf *)mem;
349 if ((m->m_flags & M_PKTHDR) != 0)
350 m_tag_delete_chain(m, NULL);
351 KASSERT((m->m_flags & M_EXT) == 0, ("%s: M_EXT set", __func__));
353 trash_dtor(mem, size, arg);
358 * The Mbuf Packet zone destructor.
361 mb_dtor_pack(void *mem, int size, void *arg)
365 m = (struct mbuf *)mem;
366 if ((m->m_flags & M_PKTHDR) != 0)
367 m_tag_delete_chain(m, NULL);
369 /* Make sure we've got a clean cluster back. */
370 KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__));
371 KASSERT(m->m_ext.ext_buf != NULL, ("%s: ext_buf == NULL", __func__));
372 KASSERT(m->m_ext.ext_free == NULL, ("%s: ext_free != NULL", __func__));
373 KASSERT(m->m_ext.ext_args == NULL, ("%s: ext_args != NULL", __func__));
374 KASSERT(m->m_ext.ext_size == MCLBYTES, ("%s: ext_size != MCLBYTES", __func__));
375 KASSERT(m->m_ext.ext_type == EXT_PACKET, ("%s: ext_type != EXT_PACKET", __func__));
376 KASSERT(*m->m_ext.ref_cnt == 1, ("%s: ref_cnt != 1", __func__));
378 trash_dtor(m->m_ext.ext_buf, MCLBYTES, arg);
383 * The Cluster and Jumbo[PAGESIZE|9|16] zone constructor.
385 * Here the 'arg' pointer points to the Mbuf which we
386 * are configuring cluster storage for. If 'arg' is
387 * empty we allocate just the cluster without setting
388 * the mbuf to it. See mbuf.h.
391 mb_ctor_clust(void *mem, int size, void *arg, int how)
398 trash_ctor(mem, size, arg, how);
400 m = (struct mbuf *)arg;
406 #if MJUMPAGESIZE != MCLBYTES
418 panic("unknown cluster size");
421 m->m_ext.ext_buf = (caddr_t)mem;
422 m->m_data = m->m_ext.ext_buf;
424 m->m_ext.ext_free = NULL;
425 m->m_ext.ext_args = NULL;
426 m->m_ext.ext_size = size;
427 m->m_ext.ext_type = type;
428 m->m_ext.ref_cnt = uma_find_refcnt(zone_clust, mem);
429 *m->m_ext.ref_cnt = 1;
431 refcnt = uma_find_refcnt(zone_clust, mem);
438 * The Mbuf Cluster zone destructor.
441 mb_dtor_clust(void *mem, int size, void *arg)
444 KASSERT(*(uma_find_refcnt(zone_clust, mem)) <= 1,
445 ("%s: refcnt incorrect %u", __func__,
446 *(uma_find_refcnt(zone_clust, mem))) );
448 trash_dtor(mem, size, arg);
453 * The Packet secondary zone's init routine, executed on the
454 * object's transition from mbuf keg slab to zone cache.
457 mb_zinit_pack(void *mem, int size, int how)
461 m = (struct mbuf *)mem; /* m is virgin. */
462 if (uma_zalloc_arg(zone_clust, m, how) == NULL ||
463 m->m_ext.ext_buf == NULL)
465 m->m_ext.ext_type = EXT_PACKET; /* Override. */
467 trash_init(m->m_ext.ext_buf, MCLBYTES, how);
473 * The Packet secondary zone's fini routine, executed on the
474 * object's transition from zone cache to keg slab.
477 mb_zfini_pack(void *mem, int size)
481 m = (struct mbuf *)mem;
483 trash_fini(m->m_ext.ext_buf, MCLBYTES);
485 uma_zfree_arg(zone_clust, m->m_ext.ext_buf, NULL);
487 trash_dtor(mem, size, NULL);
492 * The "packet" keg constructor.
495 mb_ctor_pack(void *mem, int size, void *arg, int how)
498 struct mb_args *args;
505 m = (struct mbuf *)mem;
506 args = (struct mb_args *)arg;
511 trash_ctor(m->m_ext.ext_buf, MCLBYTES, arg, how);
515 m->m_data = m->m_ext.ext_buf;
517 m->m_flags = (flags | M_EXT);
520 if (flags & M_PKTHDR) {
521 m->m_pkthdr.rcvif = NULL;
523 m->m_pkthdr.header = NULL;
524 m->m_pkthdr.csum_flags = 0;
525 m->m_pkthdr.csum_data = 0;
526 SLIST_INIT(&m->m_pkthdr.tags);
528 /* If the label init fails, fail the alloc */
529 error = mac_init_mbuf(m, how);
534 /* m_ext is already initialized. */
540 mt_vlan_free(struct m_tag *mtag)
542 uma_zfree(zone_mtag_vlan, mtag);
546 mt_zinit_vlan(void *mem, int size, int how)
548 struct m_tag *mtag = (struct m_tag *)mem;
550 m_tag_setup(mtag, MTAG_VLAN, MTAG_VLAN_TAG, sizeof(u_int));
551 mtag->m_tag_free = mt_vlan_free;
557 * This is the protocol drain routine.
559 * No locks should be held when this is called. The drain routines have to
560 * presently acquire some locks which raises the possibility of lock order
564 mb_reclaim(void *junk)
569 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK | WARN_PANIC, NULL,
572 for (dp = domains; dp != NULL; dp = dp->dom_next)
573 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
574 if (pr->pr_drain != NULL)