Deleted Added
full compact
kern_mbuf.c (156428) kern_mbuf.c (157927)
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
9 * notice unmodified, this list of conditions and the following
10 * disclaimer.
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.
14 *
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
25 * SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
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
9 * notice unmodified, this list of conditions and the following
10 * disclaimer.
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.
14 *
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
25 * SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: head/sys/kern/kern_mbuf.c 156428 2006-03-08 14:05:38Z andre $");
29__FBSDID("$FreeBSD: head/sys/kern/kern_mbuf.c 157927 2006-04-21 09:25:40Z ps $");
30
31#include "opt_mac.h"
32#include "opt_param.h"
33
34#include <sys/param.h>
35#include <sys/mac.h>
36#include <sys/malloc.h>
37#include <sys/systm.h>
38#include <sys/mbuf.h>
39#include <sys/domain.h>
40#include <sys/eventhandler.h>
41#include <sys/kernel.h>
42#include <sys/protosw.h>
43#include <sys/smp.h>
44#include <sys/sysctl.h>
45
46#include <vm/vm.h>
47#include <vm/vm_page.h>
48#include <vm/uma.h>
49#include <vm/uma_int.h>
50#include <vm/uma_dbg.h>
51
52/*
53 * In FreeBSD, Mbufs and Mbuf Clusters are allocated from UMA
54 * Zones.
55 *
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.
59 *
60 * Mbufs are allocated from a UMA Master Zone called the Mbuf
61 * Zone.
62 *
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.
66 *
67 * Thus common-case allocations and locking are simplified:
68 *
69 * m_clget() m_getcl()
70 * | |
71 * | .------------>[(Packet Cache)] m_get(), m_gethdr()
72 * | | [ Packet ] |
73 * [(Cluster Cache)] [ Secondary ] [ (Mbuf Cache) ]
74 * [ Cluster Zone ] [ Zone ] [ Mbuf Master Zone ]
75 * | \________ |
76 * [ Cluster Keg ] \ /
77 * | [ Mbuf Keg ]
78 * [ Cluster Slabs ] |
79 * | [ Mbuf Slabs ]
80 * \____________(VM)_________________/
81 *
82 *
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
86 * is executed.
87 *
88 * Caches are per-CPU and are filled from the Master Zone.
89 *
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.
94 *
95 */
96
97int nmbclusters; /* limits number of mbuf clusters */
98int nmbjumbop; /* limits number of page size jumbo clusters */
99int nmbjumbo9; /* limits number of 9k jumbo clusters */
100int nmbjumbo16; /* limits number of 16k jumbo clusters */
101struct mbstat mbstat;
102
103static void
104tunable_mbinit(void *dummy)
105{
106
107 /* This has to be done before VM init. */
108 nmbclusters = 1024 + maxusers * 64;
109 TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters);
110}
111SYSINIT(tunable_mbinit, SI_SUB_TUNABLES, SI_ORDER_ANY, tunable_mbinit, NULL);
112
113SYSCTL_DECL(_kern_ipc);
114/* XXX: These should be tuneables. Can't change UMA limits on the fly. */
30
31#include "opt_mac.h"
32#include "opt_param.h"
33
34#include <sys/param.h>
35#include <sys/mac.h>
36#include <sys/malloc.h>
37#include <sys/systm.h>
38#include <sys/mbuf.h>
39#include <sys/domain.h>
40#include <sys/eventhandler.h>
41#include <sys/kernel.h>
42#include <sys/protosw.h>
43#include <sys/smp.h>
44#include <sys/sysctl.h>
45
46#include <vm/vm.h>
47#include <vm/vm_page.h>
48#include <vm/uma.h>
49#include <vm/uma_int.h>
50#include <vm/uma_dbg.h>
51
52/*
53 * In FreeBSD, Mbufs and Mbuf Clusters are allocated from UMA
54 * Zones.
55 *
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.
59 *
60 * Mbufs are allocated from a UMA Master Zone called the Mbuf
61 * Zone.
62 *
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.
66 *
67 * Thus common-case allocations and locking are simplified:
68 *
69 * m_clget() m_getcl()
70 * | |
71 * | .------------>[(Packet Cache)] m_get(), m_gethdr()
72 * | | [ Packet ] |
73 * [(Cluster Cache)] [ Secondary ] [ (Mbuf Cache) ]
74 * [ Cluster Zone ] [ Zone ] [ Mbuf Master Zone ]
75 * | \________ |
76 * [ Cluster Keg ] \ /
77 * | [ Mbuf Keg ]
78 * [ Cluster Slabs ] |
79 * | [ Mbuf Slabs ]
80 * \____________(VM)_________________/
81 *
82 *
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
86 * is executed.
87 *
88 * Caches are per-CPU and are filled from the Master Zone.
89 *
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.
94 *
95 */
96
97int nmbclusters; /* limits number of mbuf clusters */
98int nmbjumbop; /* limits number of page size jumbo clusters */
99int nmbjumbo9; /* limits number of 9k jumbo clusters */
100int nmbjumbo16; /* limits number of 16k jumbo clusters */
101struct mbstat mbstat;
102
103static void
104tunable_mbinit(void *dummy)
105{
106
107 /* This has to be done before VM init. */
108 nmbclusters = 1024 + maxusers * 64;
109 TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters);
110}
111SYSINIT(tunable_mbinit, SI_SUB_TUNABLES, SI_ORDER_ANY, tunable_mbinit, NULL);
112
113SYSCTL_DECL(_kern_ipc);
114/* XXX: These should be tuneables. Can't change UMA limits on the fly. */
115SYSCTL_INT(_kern_ipc, OID_AUTO, nmbclusters, CTLFLAG_RW, &nmbclusters, 0,
115static int
116sysctl_nmbclusters(SYSCTL_HANDLER_ARGS)
117{
118 int error, newnmbclusters;
119
120 newnmbclusters = nmbclusters;
121 error = sysctl_handle_int(oidp, &newnmbclusters, sizeof(int), req);
122 if (error == 0 && req->newptr) {
123 if (newnmbclusters > nmbclusters) {
124 nmbclusters = newnmbclusters;
125 uma_zone_set_max(zone_clust, nmbclusters);
126 EVENTHANDLER_INVOKE(nmbclusters_change);
127 } else
128 error = EINVAL;
129 }
130 return (error);
131}
132SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbclusters, CTLTYPE_INT|CTLFLAG_RW,
133&nmbclusters, 0, sysctl_nmbclusters, "IU",
116 "Maximum number of mbuf clusters allowed");
117SYSCTL_INT(_kern_ipc, OID_AUTO, nmbjumbop, CTLFLAG_RW, &nmbjumbop, 0,
118 "Maximum number of mbuf page size jumbo clusters allowed");
119SYSCTL_INT(_kern_ipc, OID_AUTO, nmbjumbo9, CTLFLAG_RW, &nmbjumbo9, 0,
120 "Maximum number of mbuf 9k jumbo clusters allowed");
121SYSCTL_INT(_kern_ipc, OID_AUTO, nmbjumbo16, CTLFLAG_RW, &nmbjumbo16, 0,
122 "Maximum number of mbuf 16k jumbo clusters allowed");
123SYSCTL_STRUCT(_kern_ipc, OID_AUTO, mbstat, CTLFLAG_RD, &mbstat, mbstat,
124 "Mbuf general information and statistics");
125
126/*
127 * Zones from which we allocate.
128 */
129uma_zone_t zone_mbuf;
130uma_zone_t zone_clust;
131uma_zone_t zone_pack;
132uma_zone_t zone_jumbop;
133uma_zone_t zone_jumbo9;
134uma_zone_t zone_jumbo16;
135uma_zone_t zone_ext_refcnt;
136uma_zone_t zone_mtag_vlan;
137
138/*
139 * Local prototypes.
140 */
141static int mb_ctor_mbuf(void *, int, void *, int);
142static int mb_ctor_clust(void *, int, void *, int);
143static int mb_ctor_pack(void *, int, void *, int);
144static void mb_dtor_mbuf(void *, int, void *);
145static void mb_dtor_clust(void *, int, void *);
146static void mb_dtor_pack(void *, int, void *);
147static int mb_zinit_pack(void *, int, int);
148static void mb_zfini_pack(void *, int);
149static int mt_zinit_vlan(void *, int, int);
150
151static void mb_reclaim(void *);
152static void mbuf_init(void *);
153
154/* Ensure that MSIZE doesn't break dtom() - it must be a power of 2 */
155CTASSERT((((MSIZE - 1) ^ MSIZE) + 1) >> 1 == MSIZE);
156
157/*
158 * Initialize FreeBSD Network buffer allocation.
159 */
160SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbuf_init, NULL)
161static void
162mbuf_init(void *dummy)
163{
164
165 /*
166 * Configure UMA zones for Mbufs, Clusters, and Packets.
167 */
168 zone_mbuf = uma_zcreate(MBUF_MEM_NAME, MSIZE,
169 mb_ctor_mbuf, mb_dtor_mbuf,
170#ifdef INVARIANTS
171 trash_init, trash_fini,
172#else
173 NULL, NULL,
174#endif
175 MSIZE - 1, UMA_ZONE_MAXBUCKET);
176
177 zone_clust = uma_zcreate(MBUF_CLUSTER_MEM_NAME, MCLBYTES,
178 mb_ctor_clust, mb_dtor_clust,
179#ifdef INVARIANTS
180 trash_init, trash_fini,
181#else
182 NULL, NULL,
183#endif
184 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
185 if (nmbclusters > 0)
186 uma_zone_set_max(zone_clust, nmbclusters);
187
188 zone_pack = uma_zsecond_create(MBUF_PACKET_MEM_NAME, mb_ctor_pack,
189 mb_dtor_pack, mb_zinit_pack, mb_zfini_pack, zone_mbuf);
190
191 /* Make jumbo frame zone too. Page size, 9k and 16k. */
192 zone_jumbop = uma_zcreate(MBUF_JUMBOP_MEM_NAME, MJUMPAGESIZE,
193 mb_ctor_clust, mb_dtor_clust,
194#ifdef INVARIANTS
195 trash_init, trash_fini,
196#else
197 NULL, NULL,
198#endif
199 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
200 if (nmbjumbop > 0)
201 uma_zone_set_max(zone_jumbop, nmbjumbop);
202
203 zone_jumbo9 = uma_zcreate(MBUF_JUMBO9_MEM_NAME, MJUM9BYTES,
204 mb_ctor_clust, mb_dtor_clust,
205#ifdef INVARIANTS
206 trash_init, trash_fini,
207#else
208 NULL, NULL,
209#endif
210 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
211 if (nmbjumbo9 > 0)
212 uma_zone_set_max(zone_jumbo9, nmbjumbo9);
213
214 zone_jumbo16 = uma_zcreate(MBUF_JUMBO16_MEM_NAME, MJUM16BYTES,
215 mb_ctor_clust, mb_dtor_clust,
216#ifdef INVARIANTS
217 trash_init, trash_fini,
218#else
219 NULL, NULL,
220#endif
221 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
222 if (nmbjumbo16 > 0)
223 uma_zone_set_max(zone_jumbo16, nmbjumbo16);
224
225 zone_ext_refcnt = uma_zcreate(MBUF_EXTREFCNT_MEM_NAME, sizeof(u_int),
226 NULL, NULL,
227 NULL, NULL,
228 UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
229
230 zone_mtag_vlan = uma_zcreate("mtag_vlan",
231 sizeof(struct m_tag) + sizeof(u_int),
232 NULL, NULL,
233 mt_zinit_vlan, NULL,
234 UMA_ALIGN_INT, 0);
235
236 /* uma_prealloc() goes here... */
237
238 /*
239 * Hook event handler for low-memory situation, used to
240 * drain protocols and push data back to the caches (UMA
241 * later pushes it back to VM).
242 */
243 EVENTHANDLER_REGISTER(vm_lowmem, mb_reclaim, NULL,
244 EVENTHANDLER_PRI_FIRST);
245
246 /*
247 * [Re]set counters and local statistics knobs.
248 * XXX Some of these should go and be replaced, but UMA stat
249 * gathering needs to be revised.
250 */
251 mbstat.m_mbufs = 0;
252 mbstat.m_mclusts = 0;
253 mbstat.m_drain = 0;
254 mbstat.m_msize = MSIZE;
255 mbstat.m_mclbytes = MCLBYTES;
256 mbstat.m_minclsize = MINCLSIZE;
257 mbstat.m_mlen = MLEN;
258 mbstat.m_mhlen = MHLEN;
259 mbstat.m_numtypes = MT_NTYPES;
260
261 mbstat.m_mcfail = mbstat.m_mpfail = 0;
262 mbstat.sf_iocnt = 0;
263 mbstat.sf_allocwait = mbstat.sf_allocfail = 0;
264}
265
266/*
267 * Constructor for Mbuf master zone.
268 *
269 * The 'arg' pointer points to a mb_args structure which
270 * contains call-specific information required to support the
271 * mbuf allocation API. See mbuf.h.
272 */
273static int
274mb_ctor_mbuf(void *mem, int size, void *arg, int how)
275{
276 struct mbuf *m;
277 struct mb_args *args;
278#ifdef MAC
279 int error;
280#endif
281 int flags;
282 short type;
283
284#ifdef INVARIANTS
285 trash_ctor(mem, size, arg, how);
286#endif
287 m = (struct mbuf *)mem;
288 args = (struct mb_args *)arg;
289 flags = args->flags;
290 type = args->type;
291
292 /*
293 * The mbuf is initialized later. The caller has the
294 * responsibility to set up any MAC labels too.
295 */
296 if (type == MT_NOINIT)
297 return (0);
298
299 m->m_next = NULL;
300 m->m_nextpkt = NULL;
301 m->m_len = 0;
302 m->m_flags = flags;
303 m->m_type = type;
304 if (flags & M_PKTHDR) {
305 m->m_data = m->m_pktdat;
306 m->m_pkthdr.rcvif = NULL;
307 m->m_pkthdr.len = 0;
308 m->m_pkthdr.header = NULL;
309 m->m_pkthdr.csum_flags = 0;
310 m->m_pkthdr.csum_data = 0;
311 SLIST_INIT(&m->m_pkthdr.tags);
312#ifdef MAC
313 /* If the label init fails, fail the alloc */
314 error = mac_init_mbuf(m, how);
315 if (error)
316 return (error);
317#endif
318 } else
319 m->m_data = m->m_dat;
320 return (0);
321}
322
323/*
324 * The Mbuf master zone destructor.
325 */
326static void
327mb_dtor_mbuf(void *mem, int size, void *arg)
328{
329 struct mbuf *m;
330
331 m = (struct mbuf *)mem;
332 if ((m->m_flags & M_PKTHDR) != 0)
333 m_tag_delete_chain(m, NULL);
334 KASSERT((m->m_flags & M_EXT) == 0, ("%s: M_EXT set", __func__));
335#ifdef INVARIANTS
336 trash_dtor(mem, size, arg);
337#endif
338}
339
340/*
341 * The Mbuf Packet zone destructor.
342 */
343static void
344mb_dtor_pack(void *mem, int size, void *arg)
345{
346 struct mbuf *m;
347
348 m = (struct mbuf *)mem;
349 if ((m->m_flags & M_PKTHDR) != 0)
350 m_tag_delete_chain(m, NULL);
351
352 /* Make sure we've got a clean cluster back. */
353 KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__));
354 KASSERT(m->m_ext.ext_buf != NULL, ("%s: ext_buf == NULL", __func__));
355 KASSERT(m->m_ext.ext_free == NULL, ("%s: ext_free != NULL", __func__));
356 KASSERT(m->m_ext.ext_args == NULL, ("%s: ext_args != NULL", __func__));
357 KASSERT(m->m_ext.ext_size == MCLBYTES, ("%s: ext_size != MCLBYTES", __func__));
358 KASSERT(m->m_ext.ext_type == EXT_PACKET, ("%s: ext_type != EXT_PACKET", __func__));
359 KASSERT(*m->m_ext.ref_cnt == 1, ("%s: ref_cnt != 1", __func__));
360#ifdef INVARIANTS
361 trash_dtor(m->m_ext.ext_buf, MCLBYTES, arg);
362#endif
363}
364
365/*
366 * The Cluster and Jumbo[PAGESIZE|9|16] zone constructor.
367 *
368 * Here the 'arg' pointer points to the Mbuf which we
369 * are configuring cluster storage for. If 'arg' is
370 * empty we allocate just the cluster without setting
371 * the mbuf to it. See mbuf.h.
372 */
373static int
374mb_ctor_clust(void *mem, int size, void *arg, int how)
375{
376 struct mbuf *m;
377 u_int *refcnt;
378 int type = 0;
379
380#ifdef INVARIANTS
381 trash_ctor(mem, size, arg, how);
382#endif
383 m = (struct mbuf *)arg;
384 if (m != NULL) {
385 switch (size) {
386 case MCLBYTES:
387 type = EXT_CLUSTER;
388 break;
389#if MJUMPAGESIZE != MCLBYTES
390 case MJUMPAGESIZE:
391 type = EXT_JUMBOP;
392 break;
393#endif
394 case MJUM9BYTES:
395 type = EXT_JUMBO9;
396 break;
397 case MJUM16BYTES:
398 type = EXT_JUMBO16;
399 break;
400 default:
401 panic("unknown cluster size");
402 break;
403 }
404 m->m_ext.ext_buf = (caddr_t)mem;
405 m->m_data = m->m_ext.ext_buf;
406 m->m_flags |= M_EXT;
407 m->m_ext.ext_free = NULL;
408 m->m_ext.ext_args = NULL;
409 m->m_ext.ext_size = size;
410 m->m_ext.ext_type = type;
411 m->m_ext.ref_cnt = uma_find_refcnt(zone_clust, mem);
412 *m->m_ext.ref_cnt = 1;
413 } else {
414 refcnt = uma_find_refcnt(zone_clust, mem);
415 *refcnt = 1;
416 }
417 return (0);
418}
419
420/*
421 * The Mbuf Cluster zone destructor.
422 */
423static void
424mb_dtor_clust(void *mem, int size, void *arg)
425{
426
427 KASSERT(*(uma_find_refcnt(zone_clust, mem)) <= 1,
428 ("%s: refcnt incorrect %u", __func__,
429 *(uma_find_refcnt(zone_clust, mem))) );
430#ifdef INVARIANTS
431 trash_dtor(mem, size, arg);
432#endif
433}
434
435/*
436 * The Packet secondary zone's init routine, executed on the
437 * object's transition from mbuf keg slab to zone cache.
438 */
439static int
440mb_zinit_pack(void *mem, int size, int how)
441{
442 struct mbuf *m;
443
444 m = (struct mbuf *)mem; /* m is virgin. */
445 if (uma_zalloc_arg(zone_clust, m, how) == NULL ||
446 m->m_ext.ext_buf == NULL)
447 return (ENOMEM);
448 m->m_ext.ext_type = EXT_PACKET; /* Override. */
449#ifdef INVARIANTS
450 trash_init(m->m_ext.ext_buf, MCLBYTES, how);
451#endif
452 return (0);
453}
454
455/*
456 * The Packet secondary zone's fini routine, executed on the
457 * object's transition from zone cache to keg slab.
458 */
459static void
460mb_zfini_pack(void *mem, int size)
461{
462 struct mbuf *m;
463
464 m = (struct mbuf *)mem;
465#ifdef INVARIANTS
466 trash_fini(m->m_ext.ext_buf, MCLBYTES);
467#endif
468 uma_zfree_arg(zone_clust, m->m_ext.ext_buf, NULL);
469#ifdef INVARIANTS
470 trash_dtor(mem, size, NULL);
471#endif
472}
473
474/*
475 * The "packet" keg constructor.
476 */
477static int
478mb_ctor_pack(void *mem, int size, void *arg, int how)
479{
480 struct mbuf *m;
481 struct mb_args *args;
482#ifdef MAC
483 int error;
484#endif
485 int flags;
486 short type;
487
488 m = (struct mbuf *)mem;
489 args = (struct mb_args *)arg;
490 flags = args->flags;
491 type = args->type;
492
493#ifdef INVARIANTS
494 trash_ctor(m->m_ext.ext_buf, MCLBYTES, arg, how);
495#endif
496 m->m_next = NULL;
497 m->m_nextpkt = NULL;
498 m->m_data = m->m_ext.ext_buf;
499 m->m_len = 0;
500 m->m_flags = (flags | M_EXT);
501 m->m_type = type;
502
503 if (flags & M_PKTHDR) {
504 m->m_pkthdr.rcvif = NULL;
505 m->m_pkthdr.len = 0;
506 m->m_pkthdr.header = NULL;
507 m->m_pkthdr.csum_flags = 0;
508 m->m_pkthdr.csum_data = 0;
509 SLIST_INIT(&m->m_pkthdr.tags);
510#ifdef MAC
511 /* If the label init fails, fail the alloc */
512 error = mac_init_mbuf(m, how);
513 if (error)
514 return (error);
515#endif
516 }
517 /* m_ext is already initialized. */
518
519 return (0);
520}
521
522static void
523mt_vlan_free(struct m_tag *mtag)
524{
525 uma_zfree(zone_mtag_vlan, mtag);
526}
527
528static int
529mt_zinit_vlan(void *mem, int size, int how)
530{
531 struct m_tag *mtag = (struct m_tag *)mem;
532
533 m_tag_setup(mtag, MTAG_VLAN, MTAG_VLAN_TAG, sizeof(u_int));
534 mtag->m_tag_free = mt_vlan_free;
535
536 return (0);
537}
538
539/*
540 * This is the protocol drain routine.
541 *
542 * No locks should be held when this is called. The drain routines have to
543 * presently acquire some locks which raises the possibility of lock order
544 * reversal.
545 */
546static void
547mb_reclaim(void *junk)
548{
549 struct domain *dp;
550 struct protosw *pr;
551
552 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK | WARN_PANIC, NULL,
553 "mb_reclaim()");
554
555 for (dp = domains; dp != NULL; dp = dp->dom_next)
556 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
557 if (pr->pr_drain != NULL)
558 (*pr->pr_drain)();
559}
134 "Maximum number of mbuf clusters allowed");
135SYSCTL_INT(_kern_ipc, OID_AUTO, nmbjumbop, CTLFLAG_RW, &nmbjumbop, 0,
136 "Maximum number of mbuf page size jumbo clusters allowed");
137SYSCTL_INT(_kern_ipc, OID_AUTO, nmbjumbo9, CTLFLAG_RW, &nmbjumbo9, 0,
138 "Maximum number of mbuf 9k jumbo clusters allowed");
139SYSCTL_INT(_kern_ipc, OID_AUTO, nmbjumbo16, CTLFLAG_RW, &nmbjumbo16, 0,
140 "Maximum number of mbuf 16k jumbo clusters allowed");
141SYSCTL_STRUCT(_kern_ipc, OID_AUTO, mbstat, CTLFLAG_RD, &mbstat, mbstat,
142 "Mbuf general information and statistics");
143
144/*
145 * Zones from which we allocate.
146 */
147uma_zone_t zone_mbuf;
148uma_zone_t zone_clust;
149uma_zone_t zone_pack;
150uma_zone_t zone_jumbop;
151uma_zone_t zone_jumbo9;
152uma_zone_t zone_jumbo16;
153uma_zone_t zone_ext_refcnt;
154uma_zone_t zone_mtag_vlan;
155
156/*
157 * Local prototypes.
158 */
159static int mb_ctor_mbuf(void *, int, void *, int);
160static int mb_ctor_clust(void *, int, void *, int);
161static int mb_ctor_pack(void *, int, void *, int);
162static void mb_dtor_mbuf(void *, int, void *);
163static void mb_dtor_clust(void *, int, void *);
164static void mb_dtor_pack(void *, int, void *);
165static int mb_zinit_pack(void *, int, int);
166static void mb_zfini_pack(void *, int);
167static int mt_zinit_vlan(void *, int, int);
168
169static void mb_reclaim(void *);
170static void mbuf_init(void *);
171
172/* Ensure that MSIZE doesn't break dtom() - it must be a power of 2 */
173CTASSERT((((MSIZE - 1) ^ MSIZE) + 1) >> 1 == MSIZE);
174
175/*
176 * Initialize FreeBSD Network buffer allocation.
177 */
178SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbuf_init, NULL)
179static void
180mbuf_init(void *dummy)
181{
182
183 /*
184 * Configure UMA zones for Mbufs, Clusters, and Packets.
185 */
186 zone_mbuf = uma_zcreate(MBUF_MEM_NAME, MSIZE,
187 mb_ctor_mbuf, mb_dtor_mbuf,
188#ifdef INVARIANTS
189 trash_init, trash_fini,
190#else
191 NULL, NULL,
192#endif
193 MSIZE - 1, UMA_ZONE_MAXBUCKET);
194
195 zone_clust = uma_zcreate(MBUF_CLUSTER_MEM_NAME, MCLBYTES,
196 mb_ctor_clust, mb_dtor_clust,
197#ifdef INVARIANTS
198 trash_init, trash_fini,
199#else
200 NULL, NULL,
201#endif
202 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
203 if (nmbclusters > 0)
204 uma_zone_set_max(zone_clust, nmbclusters);
205
206 zone_pack = uma_zsecond_create(MBUF_PACKET_MEM_NAME, mb_ctor_pack,
207 mb_dtor_pack, mb_zinit_pack, mb_zfini_pack, zone_mbuf);
208
209 /* Make jumbo frame zone too. Page size, 9k and 16k. */
210 zone_jumbop = uma_zcreate(MBUF_JUMBOP_MEM_NAME, MJUMPAGESIZE,
211 mb_ctor_clust, mb_dtor_clust,
212#ifdef INVARIANTS
213 trash_init, trash_fini,
214#else
215 NULL, NULL,
216#endif
217 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
218 if (nmbjumbop > 0)
219 uma_zone_set_max(zone_jumbop, nmbjumbop);
220
221 zone_jumbo9 = uma_zcreate(MBUF_JUMBO9_MEM_NAME, MJUM9BYTES,
222 mb_ctor_clust, mb_dtor_clust,
223#ifdef INVARIANTS
224 trash_init, trash_fini,
225#else
226 NULL, NULL,
227#endif
228 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
229 if (nmbjumbo9 > 0)
230 uma_zone_set_max(zone_jumbo9, nmbjumbo9);
231
232 zone_jumbo16 = uma_zcreate(MBUF_JUMBO16_MEM_NAME, MJUM16BYTES,
233 mb_ctor_clust, mb_dtor_clust,
234#ifdef INVARIANTS
235 trash_init, trash_fini,
236#else
237 NULL, NULL,
238#endif
239 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
240 if (nmbjumbo16 > 0)
241 uma_zone_set_max(zone_jumbo16, nmbjumbo16);
242
243 zone_ext_refcnt = uma_zcreate(MBUF_EXTREFCNT_MEM_NAME, sizeof(u_int),
244 NULL, NULL,
245 NULL, NULL,
246 UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
247
248 zone_mtag_vlan = uma_zcreate("mtag_vlan",
249 sizeof(struct m_tag) + sizeof(u_int),
250 NULL, NULL,
251 mt_zinit_vlan, NULL,
252 UMA_ALIGN_INT, 0);
253
254 /* uma_prealloc() goes here... */
255
256 /*
257 * Hook event handler for low-memory situation, used to
258 * drain protocols and push data back to the caches (UMA
259 * later pushes it back to VM).
260 */
261 EVENTHANDLER_REGISTER(vm_lowmem, mb_reclaim, NULL,
262 EVENTHANDLER_PRI_FIRST);
263
264 /*
265 * [Re]set counters and local statistics knobs.
266 * XXX Some of these should go and be replaced, but UMA stat
267 * gathering needs to be revised.
268 */
269 mbstat.m_mbufs = 0;
270 mbstat.m_mclusts = 0;
271 mbstat.m_drain = 0;
272 mbstat.m_msize = MSIZE;
273 mbstat.m_mclbytes = MCLBYTES;
274 mbstat.m_minclsize = MINCLSIZE;
275 mbstat.m_mlen = MLEN;
276 mbstat.m_mhlen = MHLEN;
277 mbstat.m_numtypes = MT_NTYPES;
278
279 mbstat.m_mcfail = mbstat.m_mpfail = 0;
280 mbstat.sf_iocnt = 0;
281 mbstat.sf_allocwait = mbstat.sf_allocfail = 0;
282}
283
284/*
285 * Constructor for Mbuf master zone.
286 *
287 * The 'arg' pointer points to a mb_args structure which
288 * contains call-specific information required to support the
289 * mbuf allocation API. See mbuf.h.
290 */
291static int
292mb_ctor_mbuf(void *mem, int size, void *arg, int how)
293{
294 struct mbuf *m;
295 struct mb_args *args;
296#ifdef MAC
297 int error;
298#endif
299 int flags;
300 short type;
301
302#ifdef INVARIANTS
303 trash_ctor(mem, size, arg, how);
304#endif
305 m = (struct mbuf *)mem;
306 args = (struct mb_args *)arg;
307 flags = args->flags;
308 type = args->type;
309
310 /*
311 * The mbuf is initialized later. The caller has the
312 * responsibility to set up any MAC labels too.
313 */
314 if (type == MT_NOINIT)
315 return (0);
316
317 m->m_next = NULL;
318 m->m_nextpkt = NULL;
319 m->m_len = 0;
320 m->m_flags = flags;
321 m->m_type = type;
322 if (flags & M_PKTHDR) {
323 m->m_data = m->m_pktdat;
324 m->m_pkthdr.rcvif = NULL;
325 m->m_pkthdr.len = 0;
326 m->m_pkthdr.header = NULL;
327 m->m_pkthdr.csum_flags = 0;
328 m->m_pkthdr.csum_data = 0;
329 SLIST_INIT(&m->m_pkthdr.tags);
330#ifdef MAC
331 /* If the label init fails, fail the alloc */
332 error = mac_init_mbuf(m, how);
333 if (error)
334 return (error);
335#endif
336 } else
337 m->m_data = m->m_dat;
338 return (0);
339}
340
341/*
342 * The Mbuf master zone destructor.
343 */
344static void
345mb_dtor_mbuf(void *mem, int size, void *arg)
346{
347 struct mbuf *m;
348
349 m = (struct mbuf *)mem;
350 if ((m->m_flags & M_PKTHDR) != 0)
351 m_tag_delete_chain(m, NULL);
352 KASSERT((m->m_flags & M_EXT) == 0, ("%s: M_EXT set", __func__));
353#ifdef INVARIANTS
354 trash_dtor(mem, size, arg);
355#endif
356}
357
358/*
359 * The Mbuf Packet zone destructor.
360 */
361static void
362mb_dtor_pack(void *mem, int size, void *arg)
363{
364 struct mbuf *m;
365
366 m = (struct mbuf *)mem;
367 if ((m->m_flags & M_PKTHDR) != 0)
368 m_tag_delete_chain(m, NULL);
369
370 /* Make sure we've got a clean cluster back. */
371 KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__));
372 KASSERT(m->m_ext.ext_buf != NULL, ("%s: ext_buf == NULL", __func__));
373 KASSERT(m->m_ext.ext_free == NULL, ("%s: ext_free != NULL", __func__));
374 KASSERT(m->m_ext.ext_args == NULL, ("%s: ext_args != NULL", __func__));
375 KASSERT(m->m_ext.ext_size == MCLBYTES, ("%s: ext_size != MCLBYTES", __func__));
376 KASSERT(m->m_ext.ext_type == EXT_PACKET, ("%s: ext_type != EXT_PACKET", __func__));
377 KASSERT(*m->m_ext.ref_cnt == 1, ("%s: ref_cnt != 1", __func__));
378#ifdef INVARIANTS
379 trash_dtor(m->m_ext.ext_buf, MCLBYTES, arg);
380#endif
381}
382
383/*
384 * The Cluster and Jumbo[PAGESIZE|9|16] zone constructor.
385 *
386 * Here the 'arg' pointer points to the Mbuf which we
387 * are configuring cluster storage for. If 'arg' is
388 * empty we allocate just the cluster without setting
389 * the mbuf to it. See mbuf.h.
390 */
391static int
392mb_ctor_clust(void *mem, int size, void *arg, int how)
393{
394 struct mbuf *m;
395 u_int *refcnt;
396 int type = 0;
397
398#ifdef INVARIANTS
399 trash_ctor(mem, size, arg, how);
400#endif
401 m = (struct mbuf *)arg;
402 if (m != NULL) {
403 switch (size) {
404 case MCLBYTES:
405 type = EXT_CLUSTER;
406 break;
407#if MJUMPAGESIZE != MCLBYTES
408 case MJUMPAGESIZE:
409 type = EXT_JUMBOP;
410 break;
411#endif
412 case MJUM9BYTES:
413 type = EXT_JUMBO9;
414 break;
415 case MJUM16BYTES:
416 type = EXT_JUMBO16;
417 break;
418 default:
419 panic("unknown cluster size");
420 break;
421 }
422 m->m_ext.ext_buf = (caddr_t)mem;
423 m->m_data = m->m_ext.ext_buf;
424 m->m_flags |= M_EXT;
425 m->m_ext.ext_free = NULL;
426 m->m_ext.ext_args = NULL;
427 m->m_ext.ext_size = size;
428 m->m_ext.ext_type = type;
429 m->m_ext.ref_cnt = uma_find_refcnt(zone_clust, mem);
430 *m->m_ext.ref_cnt = 1;
431 } else {
432 refcnt = uma_find_refcnt(zone_clust, mem);
433 *refcnt = 1;
434 }
435 return (0);
436}
437
438/*
439 * The Mbuf Cluster zone destructor.
440 */
441static void
442mb_dtor_clust(void *mem, int size, void *arg)
443{
444
445 KASSERT(*(uma_find_refcnt(zone_clust, mem)) <= 1,
446 ("%s: refcnt incorrect %u", __func__,
447 *(uma_find_refcnt(zone_clust, mem))) );
448#ifdef INVARIANTS
449 trash_dtor(mem, size, arg);
450#endif
451}
452
453/*
454 * The Packet secondary zone's init routine, executed on the
455 * object's transition from mbuf keg slab to zone cache.
456 */
457static int
458mb_zinit_pack(void *mem, int size, int how)
459{
460 struct mbuf *m;
461
462 m = (struct mbuf *)mem; /* m is virgin. */
463 if (uma_zalloc_arg(zone_clust, m, how) == NULL ||
464 m->m_ext.ext_buf == NULL)
465 return (ENOMEM);
466 m->m_ext.ext_type = EXT_PACKET; /* Override. */
467#ifdef INVARIANTS
468 trash_init(m->m_ext.ext_buf, MCLBYTES, how);
469#endif
470 return (0);
471}
472
473/*
474 * The Packet secondary zone's fini routine, executed on the
475 * object's transition from zone cache to keg slab.
476 */
477static void
478mb_zfini_pack(void *mem, int size)
479{
480 struct mbuf *m;
481
482 m = (struct mbuf *)mem;
483#ifdef INVARIANTS
484 trash_fini(m->m_ext.ext_buf, MCLBYTES);
485#endif
486 uma_zfree_arg(zone_clust, m->m_ext.ext_buf, NULL);
487#ifdef INVARIANTS
488 trash_dtor(mem, size, NULL);
489#endif
490}
491
492/*
493 * The "packet" keg constructor.
494 */
495static int
496mb_ctor_pack(void *mem, int size, void *arg, int how)
497{
498 struct mbuf *m;
499 struct mb_args *args;
500#ifdef MAC
501 int error;
502#endif
503 int flags;
504 short type;
505
506 m = (struct mbuf *)mem;
507 args = (struct mb_args *)arg;
508 flags = args->flags;
509 type = args->type;
510
511#ifdef INVARIANTS
512 trash_ctor(m->m_ext.ext_buf, MCLBYTES, arg, how);
513#endif
514 m->m_next = NULL;
515 m->m_nextpkt = NULL;
516 m->m_data = m->m_ext.ext_buf;
517 m->m_len = 0;
518 m->m_flags = (flags | M_EXT);
519 m->m_type = type;
520
521 if (flags & M_PKTHDR) {
522 m->m_pkthdr.rcvif = NULL;
523 m->m_pkthdr.len = 0;
524 m->m_pkthdr.header = NULL;
525 m->m_pkthdr.csum_flags = 0;
526 m->m_pkthdr.csum_data = 0;
527 SLIST_INIT(&m->m_pkthdr.tags);
528#ifdef MAC
529 /* If the label init fails, fail the alloc */
530 error = mac_init_mbuf(m, how);
531 if (error)
532 return (error);
533#endif
534 }
535 /* m_ext is already initialized. */
536
537 return (0);
538}
539
540static void
541mt_vlan_free(struct m_tag *mtag)
542{
543 uma_zfree(zone_mtag_vlan, mtag);
544}
545
546static int
547mt_zinit_vlan(void *mem, int size, int how)
548{
549 struct m_tag *mtag = (struct m_tag *)mem;
550
551 m_tag_setup(mtag, MTAG_VLAN, MTAG_VLAN_TAG, sizeof(u_int));
552 mtag->m_tag_free = mt_vlan_free;
553
554 return (0);
555}
556
557/*
558 * This is the protocol drain routine.
559 *
560 * No locks should be held when this is called. The drain routines have to
561 * presently acquire some locks which raises the possibility of lock order
562 * reversal.
563 */
564static void
565mb_reclaim(void *junk)
566{
567 struct domain *dp;
568 struct protosw *pr;
569
570 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK | WARN_PANIC, NULL,
571 "mb_reclaim()");
572
573 for (dp = domains; dp != NULL; dp = dp->dom_next)
574 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
575 if (pr->pr_drain != NULL)
576 (*pr->pr_drain)();
577}