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