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