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 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, nmbjumbop, CTLFLAG_RW, &nmbjumbop, 0, 118 "Maximum number of mbuf page size 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_jumbop; 133 uma_zone_t zone_jumbo9; 134 uma_zone_t zone_jumbo16; 135 uma_zone_t zone_ext_refcnt; 136 uma_zone_t zone_mtag_vlan; 137 138 /* 139 * Local prototypes. 140 */ 141 static int mb_ctor_mbuf(void *, int, void *, int); 142 static int mb_ctor_clust(void *, int, void *, int); 143 static int mb_ctor_pack(void *, int, void *, int); 144 static void mb_dtor_mbuf(void *, int, void *); 145 static void mb_dtor_clust(void *, int, void *); 146 static void mb_dtor_pack(void *, int, void *); 147 static int mb_zinit_pack(void *, int, int); 148 static void mb_zfini_pack(void *, int); 149 static int mt_zinit_vlan(void *, int, int); 150 151 static void mb_reclaim(void *); 152 static void mbuf_init(void *); 153 154 /* Ensure that MSIZE doesn't break dtom() - it must be a power of 2 */ 155 CTASSERT((((MSIZE - 1) ^ MSIZE) + 1) >> 1 == MSIZE); 156 157 /* 158 * Initialize FreeBSD Network buffer allocation. 159 */ 160 SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbuf_init, NULL) 161 static void 162 mbuf_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 */ 273 static int 274 mb_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 */ 326 static void 327 mb_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 */ 343 static void 344 mb_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 */ 373 static int 374 mb_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 */ 423 static void 424 mb_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 */ 439 static int 440 mb_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 */ 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 #ifdef INVARIANTS 470 trash_dtor(mem, size, NULL); 471 #endif 472 } 473 474 /* 475 * The "packet" keg constructor. 476 */ 477 static int 478 mb_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 522 static void 523 mt_vlan_free(struct m_tag *mtag) 524 { 525 uma_zfree(zone_mtag_vlan, mtag); 526 } 527 528 static int 529 mt_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 */ 546 static void 547 mb_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 } 560