1/*-
2 * Copyright (c) 2004, 2005,
3 * Bosko Milekic <bmilekic@FreeBSD.org>. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
--- 12 unchanged lines hidden (view full) ---
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
25 * SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: head/sys/kern/kern_mbuf.c 245575 2013-01-17 21:28:31Z andre $");
30
31#include "opt_param.h"
32
33#include <sys/param.h>
34#include <sys/malloc.h>
35#include <sys/systm.h>
36#include <sys/mbuf.h>
37#include <sys/domain.h>
--- 4 unchanged lines hidden (view full) ---
42#include <sys/sysctl.h>
43
44#include <security/mac/mac_framework.h>
45
46#include <vm/vm.h>
47#include <vm/vm_extern.h>
48#include <vm/vm_kern.h>
49#include <vm/vm_page.h>
50#include <vm/vm_map.h>
51#include <vm/uma.h>
52#include <vm/uma_int.h>
53#include <vm/uma_dbg.h>
54
55/*
56 * In FreeBSD, Mbufs and Mbuf Clusters are allocated from UMA
57 * Zones.
58 *
--- 41 unchanged lines hidden (view full) ---
100int nmbufs; /* limits number of mbufs */
101int nmbclusters; /* limits number of mbuf clusters */
102int nmbjumbop; /* limits number of page size jumbo clusters */
103int nmbjumbo9; /* limits number of 9k jumbo clusters */
104int nmbjumbo16; /* limits number of 16k jumbo clusters */
105struct mbstat mbstat;
106
107/*
108 * tunable_mbinit() has to be run before any mbuf allocations are done.
109 */
110static void
111tunable_mbinit(void *dummy)
112{
113 quad_t realmem, maxmbufmem;
114
115 /*
116 * The default limit for all mbuf related memory is 1/2 of all
117 * available kernel memory (physical or kmem).
118 * At most it can be 3/4 of available kernel memory.
119 */
120 realmem = qmin((quad_t)physmem * PAGE_SIZE,
121 vm_map_max(kernel_map) - vm_map_min(kernel_map));
122 maxmbufmem = realmem / 2;
123 TUNABLE_QUAD_FETCH("kern.maxmbufmem", &maxmbufmem);
124 if (maxmbufmem > realmem / 4 * 3)
125 maxmbufmem = realmem / 4 * 3;
126
127 TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters);
128 if (nmbclusters == 0)
129 nmbclusters = maxmbufmem / MCLBYTES / 4;
130
131 TUNABLE_INT_FETCH("kern.ipc.nmbjumbop", &nmbjumbop);
132 if (nmbjumbop == 0)
133 nmbjumbop = maxmbufmem / MJUMPAGESIZE / 4;
134
--- 9 unchanged lines hidden (view full) ---
144 * We need at least as many mbufs as we have clusters of
145 * the various types added together.
146 */
147 TUNABLE_INT_FETCH("kern.ipc.nmbufs", &nmbufs);
148 if (nmbufs < nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16)
149 nmbufs = lmax(maxmbufmem / MSIZE / 5,
150 nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16);
151}
152SYSINIT(tunable_mbinit, SI_SUB_KMEM, SI_ORDER_MIDDLE, tunable_mbinit, NULL);
153
154static int
155sysctl_nmbclusters(SYSCTL_HANDLER_ARGS)
156{
157 int error, newnmbclusters;
158
159 newnmbclusters = nmbclusters;
160 error = sysctl_handle_int(oidp, &newnmbclusters, 0, req);
--- 123 unchanged lines hidden (view full) ---
284static int mb_ctor_pack(void *, int, void *, int);
285static void mb_dtor_mbuf(void *, int, void *);
286static void mb_dtor_clust(void *, int, void *);
287static void mb_dtor_pack(void *, int, void *);
288static int mb_zinit_pack(void *, int, int);
289static void mb_zfini_pack(void *, int);
290
291static void mb_reclaim(void *);
292static void *mbuf_jumbo_alloc(uma_zone_t, int, uint8_t *, int);
293
294/* Ensure that MSIZE is a power of 2. */
295CTASSERT((((MSIZE - 1) ^ MSIZE) + 1) >> 1 == MSIZE);
296
297/*
298 * Initialize FreeBSD Network buffer allocation.
299 */
300static void
301mbuf_init(void *dummy)
302{
303
304 /*
305 * Configure UMA zones for Mbufs, Clusters, and Packets.
306 */
307 zone_mbuf = uma_zcreate(MBUF_MEM_NAME, MSIZE,
--- 91 unchanged lines hidden (view full) ---
399 mbstat.m_mlen = MLEN;
400 mbstat.m_mhlen = MHLEN;
401 mbstat.m_numtypes = MT_NTYPES;
402
403 mbstat.m_mcfail = mbstat.m_mpfail = 0;
404 mbstat.sf_iocnt = 0;
405 mbstat.sf_allocwait = mbstat.sf_allocfail = 0;
406}
407SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbuf_init, NULL);
408
409/*
410 * UMA backend page allocator for the jumbo frame zones.
411 *
412 * Allocates kernel virtual memory that is backed by contiguous physical
413 * pages.
414 */
415static void *
--- 342 unchanged lines hidden ---
2 * Copyright (c) 2004, 2005,
3 * Bosko Milekic <bmilekic@FreeBSD.org>. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
--- 12 unchanged lines hidden (view full) ---
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
25 * SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: head/sys/kern/kern_mbuf.c 245575 2013-01-17 21:28:31Z andre $");
30
31#include "opt_param.h"
32
33#include <sys/param.h>
34#include <sys/malloc.h>
35#include <sys/systm.h>
36#include <sys/mbuf.h>
37#include <sys/domain.h>
--- 4 unchanged lines hidden (view full) ---
42#include <sys/sysctl.h>
43
44#include <security/mac/mac_framework.h>
45
46#include <vm/vm.h>
47#include <vm/vm_extern.h>
48#include <vm/vm_kern.h>
49#include <vm/vm_page.h>
50#include <vm/vm_map.h>
51#include <vm/uma.h>
52#include <vm/uma_int.h>
53#include <vm/uma_dbg.h>
54
55/*
56 * In FreeBSD, Mbufs and Mbuf Clusters are allocated from UMA
57 * Zones.
58 *
--- 41 unchanged lines hidden (view full) ---
100int nmbufs; /* limits number of mbufs */
101int nmbclusters; /* limits number of mbuf clusters */
102int nmbjumbop; /* limits number of page size jumbo clusters */
103int nmbjumbo9; /* limits number of 9k jumbo clusters */
104int nmbjumbo16; /* limits number of 16k jumbo clusters */
105struct mbstat mbstat;
106
107/*
108 * tunable_mbinit() has to be run before any mbuf allocations are done.
109 */
110static void
111tunable_mbinit(void *dummy)
112{
113 quad_t realmem, maxmbufmem;
114
115 /*
116 * The default limit for all mbuf related memory is 1/2 of all
117 * available kernel memory (physical or kmem).
118 * At most it can be 3/4 of available kernel memory.
119 */
120 realmem = qmin((quad_t)physmem * PAGE_SIZE,
121 vm_map_max(kernel_map) - vm_map_min(kernel_map));
122 maxmbufmem = realmem / 2;
123 TUNABLE_QUAD_FETCH("kern.maxmbufmem", &maxmbufmem);
124 if (maxmbufmem > realmem / 4 * 3)
125 maxmbufmem = realmem / 4 * 3;
126
127 TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters);
128 if (nmbclusters == 0)
129 nmbclusters = maxmbufmem / MCLBYTES / 4;
130
131 TUNABLE_INT_FETCH("kern.ipc.nmbjumbop", &nmbjumbop);
132 if (nmbjumbop == 0)
133 nmbjumbop = maxmbufmem / MJUMPAGESIZE / 4;
134
--- 9 unchanged lines hidden (view full) ---
144 * We need at least as many mbufs as we have clusters of
145 * the various types added together.
146 */
147 TUNABLE_INT_FETCH("kern.ipc.nmbufs", &nmbufs);
148 if (nmbufs < nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16)
149 nmbufs = lmax(maxmbufmem / MSIZE / 5,
150 nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16);
151}
152SYSINIT(tunable_mbinit, SI_SUB_KMEM, SI_ORDER_MIDDLE, tunable_mbinit, NULL);
153
154static int
155sysctl_nmbclusters(SYSCTL_HANDLER_ARGS)
156{
157 int error, newnmbclusters;
158
159 newnmbclusters = nmbclusters;
160 error = sysctl_handle_int(oidp, &newnmbclusters, 0, req);
--- 123 unchanged lines hidden (view full) ---
284static int mb_ctor_pack(void *, int, void *, int);
285static void mb_dtor_mbuf(void *, int, void *);
286static void mb_dtor_clust(void *, int, void *);
287static void mb_dtor_pack(void *, int, void *);
288static int mb_zinit_pack(void *, int, int);
289static void mb_zfini_pack(void *, int);
290
291static void mb_reclaim(void *);
292static void *mbuf_jumbo_alloc(uma_zone_t, int, uint8_t *, int);
293
294/* Ensure that MSIZE is a power of 2. */
295CTASSERT((((MSIZE - 1) ^ MSIZE) + 1) >> 1 == MSIZE);
296
297/*
298 * Initialize FreeBSD Network buffer allocation.
299 */
300static void
301mbuf_init(void *dummy)
302{
303
304 /*
305 * Configure UMA zones for Mbufs, Clusters, and Packets.
306 */
307 zone_mbuf = uma_zcreate(MBUF_MEM_NAME, MSIZE,
--- 91 unchanged lines hidden (view full) ---
399 mbstat.m_mlen = MLEN;
400 mbstat.m_mhlen = MHLEN;
401 mbstat.m_numtypes = MT_NTYPES;
402
403 mbstat.m_mcfail = mbstat.m_mpfail = 0;
404 mbstat.sf_iocnt = 0;
405 mbstat.sf_allocwait = mbstat.sf_allocfail = 0;
406}
407SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbuf_init, NULL);
408
409/*
410 * UMA backend page allocator for the jumbo frame zones.
411 *
412 * Allocates kernel virtual memory that is backed by contiguous physical
413 * pages.
414 */
415static void *
--- 342 unchanged lines hidden ---