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 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 /* XXX: These should be tuneables. Can't change UMA limits on the fly. */ 115 static int 116 sysctl_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 } 132 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbclusters, CTLTYPE_INT|CTLFLAG_RW, 133 &nmbclusters, 0, sysctl_nmbclusters, "IU", 134 "Maximum number of mbuf clusters allowed"); 135 SYSCTL_INT(_kern_ipc, OID_AUTO, nmbjumbop, CTLFLAG_RW, &nmbjumbop, 0, 136 "Maximum number of mbuf page size jumbo clusters allowed"); 137 SYSCTL_INT(_kern_ipc, OID_AUTO, nmbjumbo9, CTLFLAG_RW, &nmbjumbo9, 0, 138 "Maximum number of mbuf 9k jumbo clusters allowed"); 139 SYSCTL_INT(_kern_ipc, OID_AUTO, nmbjumbo16, CTLFLAG_RW, &nmbjumbo16, 0, 140 "Maximum number of mbuf 16k jumbo clusters allowed"); 141 SYSCTL_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 */ 147 uma_zone_t zone_mbuf; 148 uma_zone_t zone_clust; 149 uma_zone_t zone_pack; 150 uma_zone_t zone_jumbop; 151 uma_zone_t zone_jumbo9; 152 uma_zone_t zone_jumbo16; 153 uma_zone_t zone_ext_refcnt; 154 uma_zone_t zone_mtag_vlan; 155 156 /* 157 * Local prototypes. 158 */ 159 static int mb_ctor_mbuf(void *, int, void *, int); 160 static int mb_ctor_clust(void *, int, void *, int); 161 static int mb_ctor_pack(void *, int, void *, int); 162 static void mb_dtor_mbuf(void *, int, void *); 163 static void mb_dtor_clust(void *, int, void *); 164 static void mb_dtor_pack(void *, int, void *); 165 static int mb_zinit_pack(void *, int, int); 166 static void mb_zfini_pack(void *, int); 167 static int mt_zinit_vlan(void *, int, int); 168 169 static void mb_reclaim(void *); 170 static void mbuf_init(void *); 171 172 /* Ensure that MSIZE doesn't break dtom() - it must be a power of 2 */ 173 CTASSERT((((MSIZE - 1) ^ MSIZE) + 1) >> 1 == MSIZE); 174 175 /* 176 * Initialize FreeBSD Network buffer allocation. 177 */ 178 SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbuf_init, NULL) 179 static void 180 mbuf_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 */ 291 static int 292 mb_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 */ 344 static void 345 mb_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 */ 361 static void 362 mb_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 */ 391 static int 392 mb_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 */ 441 static void 442 mb_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 */ 457 static int 458 mb_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 */ 477 static void 478 mb_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 */ 495 static int 496 mb_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 540 static void 541 mt_vlan_free(struct m_tag *mtag) 542 { 543 uma_zfree(zone_mtag_vlan, mtag); 544 } 545 546 static int 547 mt_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 */ 564 static void 565 mb_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 } 578