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