FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_mbuf.c
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: releng/6.1/sys/kern/kern_mbuf.c 158179 2006-04-30 16:44:43Z cvs2svn $");
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
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;
102
103 static void
104 tunable_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 }
111 SYSINIT(tunable_mbinit, SI_SUB_TUNABLES, SI_ORDER_ANY, tunable_mbinit, NULL);
112
113 SYSCTL_DECL(_kern_ipc);
114 SYSCTL_INT(_kern_ipc, OID_AUTO, nmbclusters, CTLFLAG_RW, &nmbclusters, 0,
115 "Maximum number of mbuf clusters allowed");
116 SYSCTL_INT(_kern_ipc, OID_AUTO, nmbjumbop, CTLFLAG_RW, &nmbjumbop, 0,
117 "Maximum number of mbuf page size jumbo clusters allowed");
118 SYSCTL_INT(_kern_ipc, OID_AUTO, nmbjumbo9, CTLFLAG_RW, &nmbjumbo9, 0,
119 "Maximum number of mbuf 9k jumbo clusters allowed");
120 SYSCTL_INT(_kern_ipc, OID_AUTO, nmbjumbo16, CTLFLAG_RW, &nmbjumbo16, 0,
121 "Maximum number of mbuf 16k jumbo clusters allowed");
122 SYSCTL_STRUCT(_kern_ipc, OID_AUTO, mbstat, CTLFLAG_RD, &mbstat, mbstat,
123 "Mbuf general information and statistics");
124
125 /*
126 * Zones from which we allocate.
127 */
128 uma_zone_t zone_mbuf;
129 uma_zone_t zone_clust;
130 uma_zone_t zone_pack;
131 uma_zone_t zone_jumbop;
132 uma_zone_t zone_jumbo9;
133 uma_zone_t zone_jumbo16;
134
135 /*
136 * Local prototypes.
137 */
138 static int mb_ctor_mbuf(void *, int, void *, int);
139 static int mb_ctor_clust(void *, int, void *, int);
140 static int mb_ctor_pack(void *, int, void *, int);
141 static void mb_dtor_mbuf(void *, int, void *);
142 static void mb_dtor_clust(void *, int, void *);
143 static void mb_dtor_pack(void *, int, void *);
144 static int mb_zinit_pack(void *, int, int);
145 static void mb_zfini_pack(void *, int);
146
147 static void mb_reclaim(void *);
148 static void mbuf_init(void *);
149
150 /* Ensure that MSIZE doesn't break dtom() - it must be a power of 2 */
151 CTASSERT((((MSIZE - 1) ^ MSIZE) + 1) >> 1 == MSIZE);
152
153 /*
154 * Initialize FreeBSD Network buffer allocation.
155 */
156 SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbuf_init, NULL)
157 static void
158 mbuf_init(void *dummy)
159 {
160
161 /*
162 * Configure UMA zones for Mbufs, Clusters, and Packets.
163 */
164 zone_mbuf = uma_zcreate(MBUF_MEM_NAME, MSIZE,
165 mb_ctor_mbuf, mb_dtor_mbuf,
166 #ifdef INVARIANTS
167 trash_init, trash_fini,
168 #else
169 NULL, NULL,
170 #endif
171 MSIZE - 1, UMA_ZONE_MAXBUCKET);
172
173 zone_clust = uma_zcreate(MBUF_CLUSTER_MEM_NAME, MCLBYTES,
174 mb_ctor_clust, mb_dtor_clust,
175 #ifdef INVARIANTS
176 trash_init, trash_fini,
177 #else
178 NULL, NULL,
179 #endif
180 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
181
182 if (nmbclusters > 0)
183 uma_zone_set_max(zone_clust, nmbclusters);
184
185 zone_pack = uma_zsecond_create(MBUF_PACKET_MEM_NAME, mb_ctor_pack,
186 mb_dtor_pack, mb_zinit_pack, mb_zfini_pack, zone_mbuf);
187
188 /* Make jumbo frame zone too. Page size, 9k and 16k. */
189 zone_jumbop = uma_zcreate(MBUF_JUMBOP_MEM_NAME, MJUMPAGESIZE,
190 mb_ctor_clust, mb_dtor_clust,
191 #ifdef INVARIANTS
192 trash_init, trash_fini,
193 #else
194 NULL, NULL,
195 #endif
196 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
197 if (nmbjumbop > 0)
198 uma_zone_set_max(zone_jumbop, nmbjumbop);
199
200 zone_jumbo9 = uma_zcreate(MBUF_JUMBO9_MEM_NAME, MJUM9BYTES,
201 mb_ctor_clust, mb_dtor_clust,
202 #ifdef INVARIANTS
203 trash_init, trash_fini,
204 #else
205 NULL, NULL,
206 #endif
207 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
208 if (nmbjumbo9 > 0)
209 uma_zone_set_max(zone_jumbo9, nmbjumbo9);
210
211 zone_jumbo16 = uma_zcreate(MBUF_JUMBO16_MEM_NAME, MJUM16BYTES,
212 mb_ctor_clust, mb_dtor_clust,
213 #ifdef INVARIANTS
214 trash_init, trash_fini,
215 #else
216 NULL, NULL,
217 #endif
218 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
219 if (nmbjumbo16 > 0)
220 uma_zone_set_max(zone_jumbo16, nmbjumbo16);
221
222 /* uma_prealloc() goes here... */
223
224 /*
225 * Hook event handler for low-memory situation, used to
226 * drain protocols and push data back to the caches (UMA
227 * later pushes it back to VM).
228 */
229 EVENTHANDLER_REGISTER(vm_lowmem, mb_reclaim, NULL,
230 EVENTHANDLER_PRI_FIRST);
231
232 /*
233 * [Re]set counters and local statistics knobs.
234 * XXX Some of these should go and be replaced, but UMA stat
235 * gathering needs to be revised.
236 */
237 mbstat.m_mbufs = 0;
238 mbstat.m_mclusts = 0;
239 mbstat.m_drain = 0;
240 mbstat.m_msize = MSIZE;
241 mbstat.m_mclbytes = MCLBYTES;
242 mbstat.m_minclsize = MINCLSIZE;
243 mbstat.m_mlen = MLEN;
244 mbstat.m_mhlen = MHLEN;
245 mbstat.m_numtypes = MT_NTYPES;
246
247 mbstat.m_mcfail = mbstat.m_mpfail = 0;
248 mbstat.sf_iocnt = 0;
249 mbstat.sf_allocwait = mbstat.sf_allocfail = 0;
250 }
251
252 /*
253 * Constructor for Mbuf master zone.
254 *
255 * The 'arg' pointer points to a mb_args structure which
256 * contains call-specific information required to support the
257 * mbuf allocation API. See mbuf.h.
258 */
259 static int
260 mb_ctor_mbuf(void *mem, int size, void *arg, int how)
261 {
262 struct mbuf *m;
263 struct mb_args *args;
264 #ifdef MAC
265 int error;
266 #endif
267 int flags;
268 short type;
269
270 #ifdef INVARIANTS
271 trash_ctor(mem, size, arg, how);
272 #endif
273 m = (struct mbuf *)mem;
274 args = (struct mb_args *)arg;
275 flags = args->flags;
276 type = args->type;
277
278 /*
279 * The mbuf is initialized later. The caller has the
280 * responsibility to set up any MAC labels too.
281 */
282 if (type == MT_NOINIT)
283 return (0);
284
285 m->m_next = NULL;
286 m->m_nextpkt = NULL;
287 m->m_len = 0;
288 m->m_flags = flags;
289 m->m_type = type;
290 if (flags & M_PKTHDR) {
291 m->m_data = m->m_pktdat;
292 m->m_pkthdr.rcvif = NULL;
293 m->m_pkthdr.len = 0;
294 m->m_pkthdr.header = NULL;
295 m->m_pkthdr.csum_flags = 0;
296 m->m_pkthdr.csum_data = 0;
297 SLIST_INIT(&m->m_pkthdr.tags);
298 #ifdef MAC
299 /* If the label init fails, fail the alloc */
300 error = mac_init_mbuf(m, how);
301 if (error)
302 return (error);
303 #endif
304 } else
305 m->m_data = m->m_dat;
306 mbstat.m_mbufs += 1; /* XXX */
307 return (0);
308 }
309
310 /*
311 * The Mbuf master zone destructor.
312 */
313 static void
314 mb_dtor_mbuf(void *mem, int size, void *arg)
315 {
316 struct mbuf *m;
317
318 m = (struct mbuf *)mem;
319 if ((m->m_flags & M_PKTHDR) != 0)
320 m_tag_delete_chain(m, NULL);
321 #ifdef INVARIANTS
322 trash_dtor(mem, size, arg);
323 #endif
324 mbstat.m_mbufs -= 1; /* XXX */
325 }
326
327 /*
328 * The Mbuf Packet zone destructor.
329 */
330 static void
331 mb_dtor_pack(void *mem, int size, void *arg)
332 {
333 struct mbuf *m;
334
335 m = (struct mbuf *)mem;
336 if ((m->m_flags & M_PKTHDR) != 0)
337 m_tag_delete_chain(m, NULL);
338
339 /* Make sure we've got a clean cluster back. */
340 KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__));
341 KASSERT(m->m_ext.ext_buf != NULL, ("%s: ext_buf == NULL", __func__));
342 KASSERT(m->m_ext.ext_free == NULL, ("%s: ext_free != NULL", __func__));
343 KASSERT(m->m_ext.ext_args == NULL, ("%s: ext_args != NULL", __func__));
344 KASSERT(m->m_ext.ext_size == MCLBYTES, ("%s: ext_size != MCLBYTES", __func__));
345 KASSERT(m->m_ext.ext_type == EXT_PACKET, ("%s: ext_type != EXT_PACKET", __func__));
346 #ifdef INVARIANTS
347 trash_dtor(m->m_ext.ext_buf, MCLBYTES, arg);
348 #endif
349 mbstat.m_mbufs -= 1; /* XXX */
350 mbstat.m_mclusts -= 1; /* XXX */
351 }
352
353 /*
354 * The Cluster and Jumbo[PAGESIZE|9|16] zone constructor.
355 *
356 * Here the 'arg' pointer points to the Mbuf which we
357 * are configuring cluster storage for. If 'arg' is
358 * empty we allocate just the cluster without setting
359 * the mbuf to it. See mbuf.h.
360 */
361 static int
362 mb_ctor_clust(void *mem, int size, void *arg, int how)
363 {
364 struct mbuf *m;
365 int type = 0;
366
367 #ifdef INVARIANTS
368 trash_ctor(mem, size, arg, how);
369 #endif
370 m = (struct mbuf *)arg;
371 if (m != NULL) {
372 switch (size) {
373 case MCLBYTES:
374 type = EXT_CLUSTER;
375 break;
376 #if MJUMPAGESIZE != MCLBYTES
377 case MJUMPAGESIZE:
378 type = EXT_JUMBOP;
379 break;
380 #endif
381 case MJUM9BYTES:
382 type = EXT_JUMBO9;
383 break;
384 case MJUM16BYTES:
385 type = EXT_JUMBO16;
386 break;
387 default:
388 panic("unknown cluster size");
389 break;
390 }
391 m->m_ext.ext_buf = (caddr_t)mem;
392 m->m_data = m->m_ext.ext_buf;
393 m->m_flags |= M_EXT;
394 m->m_ext.ext_free = NULL;
395 m->m_ext.ext_args = NULL;
396 m->m_ext.ext_size = size;
397 m->m_ext.ext_type = type;
398 m->m_ext.ref_cnt = NULL; /* Lazy counter assign. */
399 }
400 mbstat.m_mclusts += 1; /* XXX */
401 return (0);
402 }
403
404 /*
405 * The Mbuf Cluster zone destructor.
406 */
407 static void
408 mb_dtor_clust(void *mem, int size, void *arg)
409 {
410 #ifdef INVARIANTS
411 trash_dtor(mem, size, arg);
412 #endif
413 mbstat.m_mclusts -= 1; /* XXX */
414 }
415
416 /*
417 * The Packet secondary zone's init routine, executed on the
418 * object's transition from mbuf keg slab to zone cache.
419 */
420 static int
421 mb_zinit_pack(void *mem, int size, int how)
422 {
423 struct mbuf *m;
424
425 m = (struct mbuf *)mem;
426 if (uma_zalloc_arg(zone_clust, m, how) == NULL ||
427 m->m_ext.ext_buf == NULL)
428 return (ENOMEM);
429 m->m_ext.ext_type = EXT_PACKET; /* Override. */
430 #ifdef INVARIANTS
431 trash_init(m->m_ext.ext_buf, MCLBYTES, how);
432 #endif
433 mbstat.m_mclusts -= 1; /* XXX */
434 return (0);
435 }
436
437 /*
438 * The Packet secondary zone's fini routine, executed on the
439 * object's transition from zone cache to keg slab.
440 */
441 static void
442 mb_zfini_pack(void *mem, int size)
443 {
444 struct mbuf *m;
445
446 m = (struct mbuf *)mem;
447 #ifdef INVARIANTS
448 trash_fini(m->m_ext.ext_buf, MCLBYTES);
449 #endif
450 uma_zfree_arg(zone_clust, m->m_ext.ext_buf, NULL);
451 m->m_ext.ext_buf = NULL;
452 mbstat.m_mclusts += 1; /* XXX */
453 #ifdef INVARIANTS
454 trash_dtor(mem, size, NULL);
455 #endif
456 }
457
458 /*
459 * The "packet" keg constructor.
460 */
461 static int
462 mb_ctor_pack(void *mem, int size, void *arg, int how)
463 {
464 struct mbuf *m;
465 struct mb_args *args;
466 #ifdef MAC
467 int error;
468 #endif
469 int flags;
470 short type;
471
472 m = (struct mbuf *)mem;
473 args = (struct mb_args *)arg;
474 flags = args->flags;
475 type = args->type;
476
477 #ifdef INVARIANTS
478 trash_ctor(m->m_ext.ext_buf, MCLBYTES, arg, how);
479 #endif
480 m->m_next = NULL;
481 m->m_nextpkt = NULL;
482 m->m_data = m->m_ext.ext_buf;
483 m->m_len = 0;
484 m->m_flags = (flags | M_EXT);
485 m->m_type = type;
486 m->m_ext.ref_cnt = NULL; /* Lazy counter assign. */
487
488 if (flags & M_PKTHDR) {
489 m->m_pkthdr.rcvif = NULL;
490 m->m_pkthdr.len = 0;
491 m->m_pkthdr.header = NULL;
492 m->m_pkthdr.csum_flags = 0;
493 m->m_pkthdr.csum_data = 0;
494 SLIST_INIT(&m->m_pkthdr.tags);
495 #ifdef MAC
496 /* If the label init fails, fail the alloc */
497 error = mac_init_mbuf(m, how);
498 if (error)
499 return (error);
500 #endif
501 }
502 /* m_ext is already initialized. */
503
504 mbstat.m_mbufs += 1; /* XXX */
505 mbstat.m_mclusts += 1; /* XXX */
506 return (0);
507 }
508
509 /*
510 * This is the protocol drain routine.
511 *
512 * No locks should be held when this is called. The drain routines have to
513 * presently acquire some locks which raises the possibility of lock order
514 * reversal.
515 */
516 static void
517 mb_reclaim(void *junk)
518 {
519 struct domain *dp;
520 struct protosw *pr;
521
522 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK | WARN_PANIC, NULL,
523 "mb_reclaim()");
524
525 mbstat.m_drain++;
526 for (dp = domains; dp != NULL; dp = dp->dom_next)
527 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
528 if (pr->pr_drain != NULL)
529 (*pr->pr_drain)();
530 }
Cache object: fcc58aa4da9716ad0caac4b421f593d7
|