1099a0e58SBosko Milekic /*- 28a36da99SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 38a36da99SPedro F. Giffuni * 48076cb52SBosko Milekic * Copyright (c) 2004, 2005, 58076cb52SBosko Milekic * Bosko Milekic <bmilekic@FreeBSD.org>. All rights reserved. 6099a0e58SBosko Milekic * 7099a0e58SBosko Milekic * Redistribution and use in source and binary forms, with or without 8099a0e58SBosko Milekic * modification, are permitted provided that the following conditions 9099a0e58SBosko Milekic * are met: 10099a0e58SBosko Milekic * 1. Redistributions of source code must retain the above copyright 11099a0e58SBosko Milekic * notice unmodified, this list of conditions and the following 12099a0e58SBosko Milekic * disclaimer. 13099a0e58SBosko Milekic * 2. Redistributions in binary form must reproduce the above copyright 14099a0e58SBosko Milekic * notice, this list of conditions and the following disclaimer in the 15099a0e58SBosko Milekic * documentation and/or other materials provided with the distribution. 16099a0e58SBosko Milekic * 17099a0e58SBosko Milekic * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18099a0e58SBosko Milekic * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19099a0e58SBosko Milekic * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20099a0e58SBosko Milekic * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21099a0e58SBosko Milekic * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22099a0e58SBosko Milekic * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23099a0e58SBosko Milekic * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24099a0e58SBosko Milekic * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25099a0e58SBosko Milekic * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26099a0e58SBosko Milekic * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27099a0e58SBosko Milekic * SUCH DAMAGE. 28099a0e58SBosko Milekic */ 29099a0e58SBosko Milekic 30099a0e58SBosko Milekic #include <sys/cdefs.h> 31099a0e58SBosko Milekic __FBSDID("$FreeBSD$"); 32099a0e58SBosko Milekic 33099a0e58SBosko Milekic #include "opt_param.h" 34099a0e58SBosko Milekic 35099a0e58SBosko Milekic #include <sys/param.h> 36*3937ee75SConrad Meyer #include <sys/conf.h> 37099a0e58SBosko Milekic #include <sys/malloc.h> 38099a0e58SBosko Milekic #include <sys/systm.h> 39099a0e58SBosko Milekic #include <sys/mbuf.h> 40099a0e58SBosko Milekic #include <sys/domain.h> 41099a0e58SBosko Milekic #include <sys/eventhandler.h> 42099a0e58SBosko Milekic #include <sys/kernel.h> 435475ca5aSMark Johnston #include <sys/limits.h> 4454503a13SJonathan T. Looney #include <sys/lock.h> 4554503a13SJonathan T. Looney #include <sys/mutex.h> 46099a0e58SBosko Milekic #include <sys/protosw.h> 47099a0e58SBosko Milekic #include <sys/smp.h> 48099a0e58SBosko Milekic #include <sys/sysctl.h> 49099a0e58SBosko Milekic 50099a0e58SBosko Milekic #include <vm/vm.h> 51c45c0034SAlan Cox #include <vm/vm_extern.h> 52c45c0034SAlan Cox #include <vm/vm_kern.h> 53099a0e58SBosko Milekic #include <vm/vm_page.h> 5437140716SAndre Oppermann #include <vm/vm_map.h> 55099a0e58SBosko Milekic #include <vm/uma.h> 56121f0509SMike Silbersack #include <vm/uma_dbg.h> 57099a0e58SBosko Milekic 58099a0e58SBosko Milekic /* 59099a0e58SBosko Milekic * In FreeBSD, Mbufs and Mbuf Clusters are allocated from UMA 60099a0e58SBosko Milekic * Zones. 61099a0e58SBosko Milekic * 62099a0e58SBosko Milekic * Mbuf Clusters (2K, contiguous) are allocated from the Cluster 63099a0e58SBosko Milekic * Zone. The Zone can be capped at kern.ipc.nmbclusters, if the 64099a0e58SBosko Milekic * administrator so desires. 65099a0e58SBosko Milekic * 66099a0e58SBosko Milekic * Mbufs are allocated from a UMA Master Zone called the Mbuf 67099a0e58SBosko Milekic * Zone. 68099a0e58SBosko Milekic * 69099a0e58SBosko Milekic * Additionally, FreeBSD provides a Packet Zone, which it 70099a0e58SBosko Milekic * configures as a Secondary Zone to the Mbuf Master Zone, 71099a0e58SBosko Milekic * thus sharing backend Slab kegs with the Mbuf Master Zone. 72099a0e58SBosko Milekic * 73099a0e58SBosko Milekic * Thus common-case allocations and locking are simplified: 74099a0e58SBosko Milekic * 75099a0e58SBosko Milekic * m_clget() m_getcl() 76099a0e58SBosko Milekic * | | 77099a0e58SBosko Milekic * | .------------>[(Packet Cache)] m_get(), m_gethdr() 78099a0e58SBosko Milekic * | | [ Packet ] | 79099a0e58SBosko Milekic * [(Cluster Cache)] [ Secondary ] [ (Mbuf Cache) ] 80099a0e58SBosko Milekic * [ Cluster Zone ] [ Zone ] [ Mbuf Master Zone ] 81099a0e58SBosko Milekic * | \________ | 82099a0e58SBosko Milekic * [ Cluster Keg ] \ / 83099a0e58SBosko Milekic * | [ Mbuf Keg ] 84099a0e58SBosko Milekic * [ Cluster Slabs ] | 85099a0e58SBosko Milekic * | [ Mbuf Slabs ] 86099a0e58SBosko Milekic * \____________(VM)_________________/ 8756a4e45aSAndre Oppermann * 8856a4e45aSAndre Oppermann * 89fcf90618SGleb Smirnoff * Whenever an object is allocated with uma_zalloc() out of 9056a4e45aSAndre Oppermann * one of the Zones its _ctor_ function is executed. The same 91fcf90618SGleb Smirnoff * for any deallocation through uma_zfree() the _dtor_ function 9256a4e45aSAndre Oppermann * is executed. 9356a4e45aSAndre Oppermann * 9456a4e45aSAndre Oppermann * Caches are per-CPU and are filled from the Master Zone. 9556a4e45aSAndre Oppermann * 96fcf90618SGleb Smirnoff * Whenever an object is allocated from the underlying global 9756a4e45aSAndre Oppermann * memory pool it gets pre-initialized with the _zinit_ functions. 98e3043798SPedro F. Giffuni * When the Keg's are overfull objects get decommissioned with 9956a4e45aSAndre Oppermann * _zfini_ functions and free'd back to the global memory pool. 10056a4e45aSAndre Oppermann * 101099a0e58SBosko Milekic */ 102099a0e58SBosko Milekic 103ead46972SAndre Oppermann int nmbufs; /* limits number of mbufs */ 10456a4e45aSAndre Oppermann int nmbclusters; /* limits number of mbuf clusters */ 105ec63cb90SAndre Oppermann int nmbjumbop; /* limits number of page size jumbo clusters */ 10656a4e45aSAndre Oppermann int nmbjumbo9; /* limits number of 9k jumbo clusters */ 10756a4e45aSAndre Oppermann int nmbjumbo16; /* limits number of 16k jumbo clusters */ 108099a0e58SBosko Milekic 109e0c00addSAndre Oppermann static quad_t maxmbufmem; /* overall real memory limit for all mbufs */ 110e0c00addSAndre Oppermann 111af3b2549SHans Petter Selasky SYSCTL_QUAD(_kern_ipc, OID_AUTO, maxmbufmem, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &maxmbufmem, 0, 112b6f49c23SHiren Panchasara "Maximum real memory allocatable to various mbuf types"); 113e0c00addSAndre Oppermann 11462938659SBjoern A. Zeeb /* 11537140716SAndre Oppermann * tunable_mbinit() has to be run before any mbuf allocations are done. 11662938659SBjoern A. Zeeb */ 117099a0e58SBosko Milekic static void 118099a0e58SBosko Milekic tunable_mbinit(void *dummy) 119099a0e58SBosko Milekic { 120e0c00addSAndre Oppermann quad_t realmem; 12137140716SAndre Oppermann 12237140716SAndre Oppermann /* 12337140716SAndre Oppermann * The default limit for all mbuf related memory is 1/2 of all 12437140716SAndre Oppermann * available kernel memory (physical or kmem). 12537140716SAndre Oppermann * At most it can be 3/4 of available kernel memory. 12637140716SAndre Oppermann */ 1275df87b21SJeff Roberson realmem = qmin((quad_t)physmem * PAGE_SIZE, vm_kmem_size); 12837140716SAndre Oppermann maxmbufmem = realmem / 2; 129e0c00addSAndre Oppermann TUNABLE_QUAD_FETCH("kern.ipc.maxmbufmem", &maxmbufmem); 13037140716SAndre Oppermann if (maxmbufmem > realmem / 4 * 3) 13137140716SAndre Oppermann maxmbufmem = realmem / 4 * 3; 132099a0e58SBosko Milekic 133812302c3SNavdeep Parhar TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters); 134416a434cSAndre Oppermann if (nmbclusters == 0) 135416a434cSAndre Oppermann nmbclusters = maxmbufmem / MCLBYTES / 4; 136812302c3SNavdeep Parhar 137812302c3SNavdeep Parhar TUNABLE_INT_FETCH("kern.ipc.nmbjumbop", &nmbjumbop); 138812302c3SNavdeep Parhar if (nmbjumbop == 0) 139416a434cSAndre Oppermann nmbjumbop = maxmbufmem / MJUMPAGESIZE / 4; 140812302c3SNavdeep Parhar 141812302c3SNavdeep Parhar TUNABLE_INT_FETCH("kern.ipc.nmbjumbo9", &nmbjumbo9); 142812302c3SNavdeep Parhar if (nmbjumbo9 == 0) 143416a434cSAndre Oppermann nmbjumbo9 = maxmbufmem / MJUM9BYTES / 6; 144812302c3SNavdeep Parhar 145812302c3SNavdeep Parhar TUNABLE_INT_FETCH("kern.ipc.nmbjumbo16", &nmbjumbo16); 146812302c3SNavdeep Parhar if (nmbjumbo16 == 0) 147416a434cSAndre Oppermann nmbjumbo16 = maxmbufmem / MJUM16BYTES / 6; 148416a434cSAndre Oppermann 149416a434cSAndre Oppermann /* 150416a434cSAndre Oppermann * We need at least as many mbufs as we have clusters of 151416a434cSAndre Oppermann * the various types added together. 152416a434cSAndre Oppermann */ 153416a434cSAndre Oppermann TUNABLE_INT_FETCH("kern.ipc.nmbufs", &nmbufs); 154416a434cSAndre Oppermann if (nmbufs < nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) 155416a434cSAndre Oppermann nmbufs = lmax(maxmbufmem / MSIZE / 5, 156416a434cSAndre Oppermann nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16); 157099a0e58SBosko Milekic } 15837140716SAndre Oppermann SYSINIT(tunable_mbinit, SI_SUB_KMEM, SI_ORDER_MIDDLE, tunable_mbinit, NULL); 159099a0e58SBosko Milekic 1604f590175SPaul Saab static int 1614f590175SPaul Saab sysctl_nmbclusters(SYSCTL_HANDLER_ARGS) 1624f590175SPaul Saab { 1634f590175SPaul Saab int error, newnmbclusters; 1644f590175SPaul Saab 1654f590175SPaul Saab newnmbclusters = nmbclusters; 166041b706bSDavid Malone error = sysctl_handle_int(oidp, &newnmbclusters, 0, req); 167d251e700SJohn Baldwin if (error == 0 && req->newptr && newnmbclusters != nmbclusters) { 168ead46972SAndre Oppermann if (newnmbclusters > nmbclusters && 169ead46972SAndre Oppermann nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) { 1704f590175SPaul Saab nmbclusters = newnmbclusters; 171bc4a1b8cSAndre Oppermann nmbclusters = uma_zone_set_max(zone_clust, nmbclusters); 1724f590175SPaul Saab EVENTHANDLER_INVOKE(nmbclusters_change); 1734f590175SPaul Saab } else 1744f590175SPaul Saab error = EINVAL; 1754f590175SPaul Saab } 1764f590175SPaul Saab return (error); 1774f590175SPaul Saab } 1784f590175SPaul Saab SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbclusters, CTLTYPE_INT|CTLFLAG_RW, 1794f590175SPaul Saab &nmbclusters, 0, sysctl_nmbclusters, "IU", 180099a0e58SBosko Milekic "Maximum number of mbuf clusters allowed"); 181cf70a46bSRandall Stewart 182cf70a46bSRandall Stewart static int 183cf70a46bSRandall Stewart sysctl_nmbjumbop(SYSCTL_HANDLER_ARGS) 184cf70a46bSRandall Stewart { 185cf70a46bSRandall Stewart int error, newnmbjumbop; 186cf70a46bSRandall Stewart 187cf70a46bSRandall Stewart newnmbjumbop = nmbjumbop; 188cf70a46bSRandall Stewart error = sysctl_handle_int(oidp, &newnmbjumbop, 0, req); 189d251e700SJohn Baldwin if (error == 0 && req->newptr && newnmbjumbop != nmbjumbop) { 190ead46972SAndre Oppermann if (newnmbjumbop > nmbjumbop && 191ead46972SAndre Oppermann nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) { 192cf70a46bSRandall Stewart nmbjumbop = newnmbjumbop; 193bc4a1b8cSAndre Oppermann nmbjumbop = uma_zone_set_max(zone_jumbop, nmbjumbop); 194cf70a46bSRandall Stewart } else 195cf70a46bSRandall Stewart error = EINVAL; 196cf70a46bSRandall Stewart } 197cf70a46bSRandall Stewart return (error); 198cf70a46bSRandall Stewart } 199cf70a46bSRandall Stewart SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbop, CTLTYPE_INT|CTLFLAG_RW, 200cf70a46bSRandall Stewart &nmbjumbop, 0, sysctl_nmbjumbop, "IU", 201ec63cb90SAndre Oppermann "Maximum number of mbuf page size jumbo clusters allowed"); 202cf70a46bSRandall Stewart 203cf70a46bSRandall Stewart static int 204cf70a46bSRandall Stewart sysctl_nmbjumbo9(SYSCTL_HANDLER_ARGS) 205cf70a46bSRandall Stewart { 206cf70a46bSRandall Stewart int error, newnmbjumbo9; 207cf70a46bSRandall Stewart 208cf70a46bSRandall Stewart newnmbjumbo9 = nmbjumbo9; 209cf70a46bSRandall Stewart error = sysctl_handle_int(oidp, &newnmbjumbo9, 0, req); 210d251e700SJohn Baldwin if (error == 0 && req->newptr && newnmbjumbo9 != nmbjumbo9) { 211ead46972SAndre Oppermann if (newnmbjumbo9 > nmbjumbo9 && 212ead46972SAndre Oppermann nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) { 213cf70a46bSRandall Stewart nmbjumbo9 = newnmbjumbo9; 214bc4a1b8cSAndre Oppermann nmbjumbo9 = uma_zone_set_max(zone_jumbo9, nmbjumbo9); 215cf70a46bSRandall Stewart } else 216cf70a46bSRandall Stewart error = EINVAL; 217cf70a46bSRandall Stewart } 218cf70a46bSRandall Stewart return (error); 219cf70a46bSRandall Stewart } 220cf70a46bSRandall Stewart SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbo9, CTLTYPE_INT|CTLFLAG_RW, 221cf70a46bSRandall Stewart &nmbjumbo9, 0, sysctl_nmbjumbo9, "IU", 22256a4e45aSAndre Oppermann "Maximum number of mbuf 9k jumbo clusters allowed"); 223cf70a46bSRandall Stewart 224cf70a46bSRandall Stewart static int 225cf70a46bSRandall Stewart sysctl_nmbjumbo16(SYSCTL_HANDLER_ARGS) 226cf70a46bSRandall Stewart { 227cf70a46bSRandall Stewart int error, newnmbjumbo16; 228cf70a46bSRandall Stewart 229cf70a46bSRandall Stewart newnmbjumbo16 = nmbjumbo16; 230cf70a46bSRandall Stewart error = sysctl_handle_int(oidp, &newnmbjumbo16, 0, req); 231d251e700SJohn Baldwin if (error == 0 && req->newptr && newnmbjumbo16 != nmbjumbo16) { 232ead46972SAndre Oppermann if (newnmbjumbo16 > nmbjumbo16 && 233ead46972SAndre Oppermann nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) { 234cf70a46bSRandall Stewart nmbjumbo16 = newnmbjumbo16; 235bc4a1b8cSAndre Oppermann nmbjumbo16 = uma_zone_set_max(zone_jumbo16, nmbjumbo16); 236cf70a46bSRandall Stewart } else 237cf70a46bSRandall Stewart error = EINVAL; 238cf70a46bSRandall Stewart } 239cf70a46bSRandall Stewart return (error); 240cf70a46bSRandall Stewart } 241cf70a46bSRandall Stewart SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbo16, CTLTYPE_INT|CTLFLAG_RW, 242cf70a46bSRandall Stewart &nmbjumbo16, 0, sysctl_nmbjumbo16, "IU", 24356a4e45aSAndre Oppermann "Maximum number of mbuf 16k jumbo clusters allowed"); 244cf70a46bSRandall Stewart 245ead46972SAndre Oppermann static int 246ead46972SAndre Oppermann sysctl_nmbufs(SYSCTL_HANDLER_ARGS) 247ead46972SAndre Oppermann { 248ead46972SAndre Oppermann int error, newnmbufs; 249ead46972SAndre Oppermann 250ead46972SAndre Oppermann newnmbufs = nmbufs; 251ead46972SAndre Oppermann error = sysctl_handle_int(oidp, &newnmbufs, 0, req); 252d251e700SJohn Baldwin if (error == 0 && req->newptr && newnmbufs != nmbufs) { 253ead46972SAndre Oppermann if (newnmbufs > nmbufs) { 254ead46972SAndre Oppermann nmbufs = newnmbufs; 255bc4a1b8cSAndre Oppermann nmbufs = uma_zone_set_max(zone_mbuf, nmbufs); 256ead46972SAndre Oppermann EVENTHANDLER_INVOKE(nmbufs_change); 257ead46972SAndre Oppermann } else 258ead46972SAndre Oppermann error = EINVAL; 259ead46972SAndre Oppermann } 260ead46972SAndre Oppermann return (error); 261ead46972SAndre Oppermann } 262e0c00addSAndre Oppermann SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbufs, CTLTYPE_INT|CTLFLAG_RW, 263ead46972SAndre Oppermann &nmbufs, 0, sysctl_nmbufs, "IU", 264ead46972SAndre Oppermann "Maximum number of mbufs allowed"); 265cf70a46bSRandall Stewart 266099a0e58SBosko Milekic /* 267099a0e58SBosko Milekic * Zones from which we allocate. 268099a0e58SBosko Milekic */ 269099a0e58SBosko Milekic uma_zone_t zone_mbuf; 270099a0e58SBosko Milekic uma_zone_t zone_clust; 271099a0e58SBosko Milekic uma_zone_t zone_pack; 272ec63cb90SAndre Oppermann uma_zone_t zone_jumbop; 27356a4e45aSAndre Oppermann uma_zone_t zone_jumbo9; 27456a4e45aSAndre Oppermann uma_zone_t zone_jumbo16; 275099a0e58SBosko Milekic 276099a0e58SBosko Milekic /* 277099a0e58SBosko Milekic * Local prototypes. 278099a0e58SBosko Milekic */ 279b23f72e9SBrian Feldman static int mb_ctor_mbuf(void *, int, void *, int); 280b23f72e9SBrian Feldman static int mb_ctor_clust(void *, int, void *, int); 281b23f72e9SBrian Feldman static int mb_ctor_pack(void *, int, void *, int); 282099a0e58SBosko Milekic static void mb_dtor_mbuf(void *, int, void *); 28356a4e45aSAndre Oppermann static void mb_dtor_pack(void *, int, void *); 28456a4e45aSAndre Oppermann static int mb_zinit_pack(void *, int, int); 28556a4e45aSAndre Oppermann static void mb_zfini_pack(void *, int); 286e60b2fcbSGleb Smirnoff static void mb_reclaim(uma_zone_t, int); 287ab3185d1SJeff Roberson static void *mbuf_jumbo_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 288099a0e58SBosko Milekic 28937140716SAndre Oppermann /* Ensure that MSIZE is a power of 2. */ 290a04946cfSBrian Somers CTASSERT((((MSIZE - 1) ^ MSIZE) + 1) >> 1 == MSIZE); 291a04946cfSBrian Somers 292099a0e58SBosko Milekic /* 293099a0e58SBosko Milekic * Initialize FreeBSD Network buffer allocation. 294099a0e58SBosko Milekic */ 295099a0e58SBosko Milekic static void 296099a0e58SBosko Milekic mbuf_init(void *dummy) 297099a0e58SBosko Milekic { 298099a0e58SBosko Milekic 299099a0e58SBosko Milekic /* 300099a0e58SBosko Milekic * Configure UMA zones for Mbufs, Clusters, and Packets. 301099a0e58SBosko Milekic */ 30256a4e45aSAndre Oppermann zone_mbuf = uma_zcreate(MBUF_MEM_NAME, MSIZE, 30356a4e45aSAndre Oppermann mb_ctor_mbuf, mb_dtor_mbuf, 304121f0509SMike Silbersack #ifdef INVARIANTS 30556a4e45aSAndre Oppermann trash_init, trash_fini, 306121f0509SMike Silbersack #else 30756a4e45aSAndre Oppermann NULL, NULL, 308121f0509SMike Silbersack #endif 30956a4e45aSAndre Oppermann MSIZE - 1, UMA_ZONE_MAXBUCKET); 31045fe0bf7SPawel Jakub Dawidek if (nmbufs > 0) 31145fe0bf7SPawel Jakub Dawidek nmbufs = uma_zone_set_max(zone_mbuf, nmbufs); 3126e0b6746SPawel Jakub Dawidek uma_zone_set_warning(zone_mbuf, "kern.ipc.nmbufs limit reached"); 313e60b2fcbSGleb Smirnoff uma_zone_set_maxaction(zone_mbuf, mb_reclaim); 31456a4e45aSAndre Oppermann 31568352adfSRobert Watson zone_clust = uma_zcreate(MBUF_CLUSTER_MEM_NAME, MCLBYTES, 31656a5f52eSGleb Smirnoff mb_ctor_clust, 317121f0509SMike Silbersack #ifdef INVARIANTS 31856a5f52eSGleb Smirnoff trash_dtor, trash_init, trash_fini, 319121f0509SMike Silbersack #else 32056a5f52eSGleb Smirnoff NULL, NULL, NULL, 321121f0509SMike Silbersack #endif 32256a5f52eSGleb Smirnoff UMA_ALIGN_PTR, 0); 32345fe0bf7SPawel Jakub Dawidek if (nmbclusters > 0) 32445fe0bf7SPawel Jakub Dawidek nmbclusters = uma_zone_set_max(zone_clust, nmbclusters); 3256e0b6746SPawel Jakub Dawidek uma_zone_set_warning(zone_clust, "kern.ipc.nmbclusters limit reached"); 326e60b2fcbSGleb Smirnoff uma_zone_set_maxaction(zone_clust, mb_reclaim); 327099a0e58SBosko Milekic 32856a4e45aSAndre Oppermann zone_pack = uma_zsecond_create(MBUF_PACKET_MEM_NAME, mb_ctor_pack, 32956a4e45aSAndre Oppermann mb_dtor_pack, mb_zinit_pack, mb_zfini_pack, zone_mbuf); 33056a4e45aSAndre Oppermann 331fcf90618SGleb Smirnoff /* Make jumbo frame zone too. Page size, 9k and 16k. */ 332ec63cb90SAndre Oppermann zone_jumbop = uma_zcreate(MBUF_JUMBOP_MEM_NAME, MJUMPAGESIZE, 33356a5f52eSGleb Smirnoff mb_ctor_clust, 334d5269a63SAndre Oppermann #ifdef INVARIANTS 33556a5f52eSGleb Smirnoff trash_dtor, trash_init, trash_fini, 336d5269a63SAndre Oppermann #else 33756a5f52eSGleb Smirnoff NULL, NULL, NULL, 338d5269a63SAndre Oppermann #endif 33956a5f52eSGleb Smirnoff UMA_ALIGN_PTR, 0); 34045fe0bf7SPawel Jakub Dawidek if (nmbjumbop > 0) 34145fe0bf7SPawel Jakub Dawidek nmbjumbop = uma_zone_set_max(zone_jumbop, nmbjumbop); 3426e0b6746SPawel Jakub Dawidek uma_zone_set_warning(zone_jumbop, "kern.ipc.nmbjumbop limit reached"); 343e60b2fcbSGleb Smirnoff uma_zone_set_maxaction(zone_jumbop, mb_reclaim); 344d5269a63SAndre Oppermann 34556a4e45aSAndre Oppermann zone_jumbo9 = uma_zcreate(MBUF_JUMBO9_MEM_NAME, MJUM9BYTES, 34656a5f52eSGleb Smirnoff mb_ctor_clust, 34756a4e45aSAndre Oppermann #ifdef INVARIANTS 34856a5f52eSGleb Smirnoff trash_dtor, trash_init, trash_fini, 34956a4e45aSAndre Oppermann #else 35056a5f52eSGleb Smirnoff NULL, NULL, NULL, 35156a4e45aSAndre Oppermann #endif 35256a5f52eSGleb Smirnoff UMA_ALIGN_PTR, 0); 353ba63339aSAlan Cox uma_zone_set_allocf(zone_jumbo9, mbuf_jumbo_alloc); 35445fe0bf7SPawel Jakub Dawidek if (nmbjumbo9 > 0) 35545fe0bf7SPawel Jakub Dawidek nmbjumbo9 = uma_zone_set_max(zone_jumbo9, nmbjumbo9); 3566e0b6746SPawel Jakub Dawidek uma_zone_set_warning(zone_jumbo9, "kern.ipc.nmbjumbo9 limit reached"); 357e60b2fcbSGleb Smirnoff uma_zone_set_maxaction(zone_jumbo9, mb_reclaim); 35856a4e45aSAndre Oppermann 35956a4e45aSAndre Oppermann zone_jumbo16 = uma_zcreate(MBUF_JUMBO16_MEM_NAME, MJUM16BYTES, 36056a5f52eSGleb Smirnoff mb_ctor_clust, 36156a4e45aSAndre Oppermann #ifdef INVARIANTS 36256a5f52eSGleb Smirnoff trash_dtor, trash_init, trash_fini, 36356a4e45aSAndre Oppermann #else 36456a5f52eSGleb Smirnoff NULL, NULL, NULL, 36556a4e45aSAndre Oppermann #endif 36656a5f52eSGleb Smirnoff UMA_ALIGN_PTR, 0); 367ba63339aSAlan Cox uma_zone_set_allocf(zone_jumbo16, mbuf_jumbo_alloc); 36845fe0bf7SPawel Jakub Dawidek if (nmbjumbo16 > 0) 36945fe0bf7SPawel Jakub Dawidek nmbjumbo16 = uma_zone_set_max(zone_jumbo16, nmbjumbo16); 3706e0b6746SPawel Jakub Dawidek uma_zone_set_warning(zone_jumbo16, "kern.ipc.nmbjumbo16 limit reached"); 371e60b2fcbSGleb Smirnoff uma_zone_set_maxaction(zone_jumbo16, mb_reclaim); 37256a4e45aSAndre Oppermann 373099a0e58SBosko Milekic /* 374099a0e58SBosko Milekic * Hook event handler for low-memory situation, used to 375099a0e58SBosko Milekic * drain protocols and push data back to the caches (UMA 376099a0e58SBosko Milekic * later pushes it back to VM). 377099a0e58SBosko Milekic */ 378099a0e58SBosko Milekic EVENTHANDLER_REGISTER(vm_lowmem, mb_reclaim, NULL, 379099a0e58SBosko Milekic EVENTHANDLER_PRI_FIRST); 380099a0e58SBosko Milekic } 38137140716SAndre Oppermann SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbuf_init, NULL); 382099a0e58SBosko Milekic 3835475ca5aSMark Johnston #ifdef NETDUMP 3845475ca5aSMark Johnston /* 3855475ca5aSMark Johnston * netdump makes use of a pre-allocated pool of mbufs and clusters. When 3865475ca5aSMark Johnston * netdump is configured, we initialize a set of UMA cache zones which return 3875475ca5aSMark Johnston * items from this pool. At panic-time, the regular UMA zone pointers are 3885475ca5aSMark Johnston * overwritten with those of the cache zones so that drivers may allocate and 3895475ca5aSMark Johnston * free mbufs and clusters without attempting to allocate physical memory. 3905475ca5aSMark Johnston * 3915475ca5aSMark Johnston * We keep mbufs and clusters in a pair of mbuf queues. In particular, for 3925475ca5aSMark Johnston * the purpose of caching clusters, we treat them as mbufs. 3935475ca5aSMark Johnston */ 3945475ca5aSMark Johnston static struct mbufq nd_mbufq = 3955475ca5aSMark Johnston { STAILQ_HEAD_INITIALIZER(nd_mbufq.mq_head), 0, INT_MAX }; 3965475ca5aSMark Johnston static struct mbufq nd_clustq = 3975475ca5aSMark Johnston { STAILQ_HEAD_INITIALIZER(nd_clustq.mq_head), 0, INT_MAX }; 3985475ca5aSMark Johnston 3995475ca5aSMark Johnston static int nd_clsize; 4005475ca5aSMark Johnston static uma_zone_t nd_zone_mbuf; 4015475ca5aSMark Johnston static uma_zone_t nd_zone_clust; 4025475ca5aSMark Johnston static uma_zone_t nd_zone_pack; 4035475ca5aSMark Johnston 4045475ca5aSMark Johnston static int 4055475ca5aSMark Johnston nd_buf_import(void *arg, void **store, int count, int domain __unused, 4065475ca5aSMark Johnston int flags) 4075475ca5aSMark Johnston { 4085475ca5aSMark Johnston struct mbufq *q; 4095475ca5aSMark Johnston struct mbuf *m; 4105475ca5aSMark Johnston int i; 4115475ca5aSMark Johnston 412*3937ee75SConrad Meyer KASSERT(!dumping, ("%s: ran out of pre-allocated mbufs", __func__)); 413*3937ee75SConrad Meyer 4145475ca5aSMark Johnston q = arg; 4155475ca5aSMark Johnston 4165475ca5aSMark Johnston for (i = 0; i < count; i++) { 4175475ca5aSMark Johnston m = mbufq_dequeue(q); 4185475ca5aSMark Johnston if (m == NULL) 4195475ca5aSMark Johnston break; 4205475ca5aSMark Johnston trash_init(m, q == &nd_mbufq ? MSIZE : nd_clsize, flags); 4215475ca5aSMark Johnston store[i] = m; 4225475ca5aSMark Johnston } 4235475ca5aSMark Johnston return (i); 4245475ca5aSMark Johnston } 4255475ca5aSMark Johnston 4265475ca5aSMark Johnston static void 4275475ca5aSMark Johnston nd_buf_release(void *arg, void **store, int count) 4285475ca5aSMark Johnston { 4295475ca5aSMark Johnston struct mbufq *q; 4305475ca5aSMark Johnston struct mbuf *m; 4315475ca5aSMark Johnston int i; 4325475ca5aSMark Johnston 4335475ca5aSMark Johnston q = arg; 4345475ca5aSMark Johnston 4355475ca5aSMark Johnston for (i = 0; i < count; i++) { 4365475ca5aSMark Johnston m = store[i]; 4375475ca5aSMark Johnston (void)mbufq_enqueue(q, m); 4385475ca5aSMark Johnston } 4395475ca5aSMark Johnston } 4405475ca5aSMark Johnston 4415475ca5aSMark Johnston static int 4425475ca5aSMark Johnston nd_pack_import(void *arg __unused, void **store, int count, int domain __unused, 4435475ca5aSMark Johnston int flags __unused) 4445475ca5aSMark Johnston { 4455475ca5aSMark Johnston struct mbuf *m; 4465475ca5aSMark Johnston void *clust; 4475475ca5aSMark Johnston int i; 4485475ca5aSMark Johnston 449*3937ee75SConrad Meyer KASSERT(!dumping, ("%s: ran out of pre-allocated mbufs", __func__)); 450*3937ee75SConrad Meyer 4515475ca5aSMark Johnston for (i = 0; i < count; i++) { 4525475ca5aSMark Johnston m = m_get(MT_DATA, M_NOWAIT); 4535475ca5aSMark Johnston if (m == NULL) 4545475ca5aSMark Johnston break; 4555475ca5aSMark Johnston clust = uma_zalloc(nd_zone_clust, M_NOWAIT); 4565475ca5aSMark Johnston if (clust == NULL) { 4575475ca5aSMark Johnston m_free(m); 4585475ca5aSMark Johnston break; 4595475ca5aSMark Johnston } 4605475ca5aSMark Johnston mb_ctor_clust(clust, nd_clsize, m, 0); 4615475ca5aSMark Johnston store[i] = m; 4625475ca5aSMark Johnston } 4635475ca5aSMark Johnston return (i); 4645475ca5aSMark Johnston } 4655475ca5aSMark Johnston 4665475ca5aSMark Johnston static void 4675475ca5aSMark Johnston nd_pack_release(void *arg __unused, void **store, int count) 4685475ca5aSMark Johnston { 4695475ca5aSMark Johnston struct mbuf *m; 4705475ca5aSMark Johnston void *clust; 4715475ca5aSMark Johnston int i; 4725475ca5aSMark Johnston 4735475ca5aSMark Johnston for (i = 0; i < count; i++) { 4745475ca5aSMark Johnston m = store[i]; 4755475ca5aSMark Johnston clust = m->m_ext.ext_buf; 4765475ca5aSMark Johnston uma_zfree(nd_zone_clust, clust); 4775475ca5aSMark Johnston uma_zfree(nd_zone_mbuf, m); 4785475ca5aSMark Johnston } 4795475ca5aSMark Johnston } 4805475ca5aSMark Johnston 4815475ca5aSMark Johnston /* 4825475ca5aSMark Johnston * Free the pre-allocated mbufs and clusters reserved for netdump, and destroy 4835475ca5aSMark Johnston * the corresponding UMA cache zones. 4845475ca5aSMark Johnston */ 4855475ca5aSMark Johnston void 4865475ca5aSMark Johnston netdump_mbuf_drain(void) 4875475ca5aSMark Johnston { 4885475ca5aSMark Johnston struct mbuf *m; 4895475ca5aSMark Johnston void *item; 4905475ca5aSMark Johnston 4915475ca5aSMark Johnston if (nd_zone_mbuf != NULL) { 4925475ca5aSMark Johnston uma_zdestroy(nd_zone_mbuf); 4935475ca5aSMark Johnston nd_zone_mbuf = NULL; 4945475ca5aSMark Johnston } 4955475ca5aSMark Johnston if (nd_zone_clust != NULL) { 4965475ca5aSMark Johnston uma_zdestroy(nd_zone_clust); 4975475ca5aSMark Johnston nd_zone_clust = NULL; 4985475ca5aSMark Johnston } 4995475ca5aSMark Johnston if (nd_zone_pack != NULL) { 5005475ca5aSMark Johnston uma_zdestroy(nd_zone_pack); 5015475ca5aSMark Johnston nd_zone_pack = NULL; 5025475ca5aSMark Johnston } 5035475ca5aSMark Johnston 5045475ca5aSMark Johnston while ((m = mbufq_dequeue(&nd_mbufq)) != NULL) 5055475ca5aSMark Johnston m_free(m); 5065475ca5aSMark Johnston while ((item = mbufq_dequeue(&nd_clustq)) != NULL) 5075475ca5aSMark Johnston uma_zfree(m_getzone(nd_clsize), item); 5085475ca5aSMark Johnston } 5095475ca5aSMark Johnston 5105475ca5aSMark Johnston /* 5115475ca5aSMark Johnston * Callback invoked immediately prior to starting a netdump. 5125475ca5aSMark Johnston */ 5135475ca5aSMark Johnston void 5145475ca5aSMark Johnston netdump_mbuf_dump(void) 5155475ca5aSMark Johnston { 5165475ca5aSMark Johnston 5175475ca5aSMark Johnston /* 5185475ca5aSMark Johnston * All cluster zones return buffers of the size requested by the 5195475ca5aSMark Johnston * drivers. It's up to the driver to reinitialize the zones if the 5205475ca5aSMark Johnston * MTU of a netdump-enabled interface changes. 5215475ca5aSMark Johnston */ 5225475ca5aSMark Johnston printf("netdump: overwriting mbuf zone pointers\n"); 5235475ca5aSMark Johnston zone_mbuf = nd_zone_mbuf; 5245475ca5aSMark Johnston zone_clust = nd_zone_clust; 5255475ca5aSMark Johnston zone_pack = nd_zone_pack; 5265475ca5aSMark Johnston zone_jumbop = nd_zone_clust; 5275475ca5aSMark Johnston zone_jumbo9 = nd_zone_clust; 5285475ca5aSMark Johnston zone_jumbo16 = nd_zone_clust; 5295475ca5aSMark Johnston } 5305475ca5aSMark Johnston 5315475ca5aSMark Johnston /* 5325475ca5aSMark Johnston * Reinitialize the netdump mbuf+cluster pool and cache zones. 5335475ca5aSMark Johnston */ 5345475ca5aSMark Johnston void 5355475ca5aSMark Johnston netdump_mbuf_reinit(int nmbuf, int nclust, int clsize) 5365475ca5aSMark Johnston { 5375475ca5aSMark Johnston struct mbuf *m; 5385475ca5aSMark Johnston void *item; 5395475ca5aSMark Johnston 5405475ca5aSMark Johnston netdump_mbuf_drain(); 5415475ca5aSMark Johnston 5425475ca5aSMark Johnston nd_clsize = clsize; 5435475ca5aSMark Johnston 5445475ca5aSMark Johnston nd_zone_mbuf = uma_zcache_create("netdump_" MBUF_MEM_NAME, 5455475ca5aSMark Johnston MSIZE, mb_ctor_mbuf, mb_dtor_mbuf, 5465475ca5aSMark Johnston #ifdef INVARIANTS 5475475ca5aSMark Johnston trash_init, trash_fini, 5485475ca5aSMark Johnston #else 5495475ca5aSMark Johnston NULL, NULL, 5505475ca5aSMark Johnston #endif 5515475ca5aSMark Johnston nd_buf_import, nd_buf_release, 5525475ca5aSMark Johnston &nd_mbufq, UMA_ZONE_NOBUCKET); 5535475ca5aSMark Johnston 5545475ca5aSMark Johnston nd_zone_clust = uma_zcache_create("netdump_" MBUF_CLUSTER_MEM_NAME, 5555475ca5aSMark Johnston clsize, mb_ctor_clust, 5565475ca5aSMark Johnston #ifdef INVARIANTS 5575475ca5aSMark Johnston trash_dtor, trash_init, trash_fini, 5585475ca5aSMark Johnston #else 5595475ca5aSMark Johnston NULL, NULL, NULL, 5605475ca5aSMark Johnston #endif 5615475ca5aSMark Johnston nd_buf_import, nd_buf_release, 5625475ca5aSMark Johnston &nd_clustq, UMA_ZONE_NOBUCKET); 5635475ca5aSMark Johnston 5645475ca5aSMark Johnston nd_zone_pack = uma_zcache_create("netdump_" MBUF_PACKET_MEM_NAME, 5655475ca5aSMark Johnston MCLBYTES, mb_ctor_pack, mb_dtor_pack, NULL, NULL, 5665475ca5aSMark Johnston nd_pack_import, nd_pack_release, 5675475ca5aSMark Johnston NULL, UMA_ZONE_NOBUCKET); 5685475ca5aSMark Johnston 5695475ca5aSMark Johnston while (nmbuf-- > 0) { 5705475ca5aSMark Johnston m = m_get(MT_DATA, M_WAITOK); 5715475ca5aSMark Johnston uma_zfree(nd_zone_mbuf, m); 5725475ca5aSMark Johnston } 5735475ca5aSMark Johnston while (nclust-- > 0) { 5745475ca5aSMark Johnston item = uma_zalloc(m_getzone(nd_clsize), M_WAITOK); 5755475ca5aSMark Johnston uma_zfree(nd_zone_clust, item); 5765475ca5aSMark Johnston } 5775475ca5aSMark Johnston } 5785475ca5aSMark Johnston #endif /* NETDUMP */ 5795475ca5aSMark Johnston 580099a0e58SBosko Milekic /* 581ba63339aSAlan Cox * UMA backend page allocator for the jumbo frame zones. 582ba63339aSAlan Cox * 583ba63339aSAlan Cox * Allocates kernel virtual memory that is backed by contiguous physical 584ba63339aSAlan Cox * pages. 585ba63339aSAlan Cox */ 586ba63339aSAlan Cox static void * 587ab3185d1SJeff Roberson mbuf_jumbo_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags, 588ab3185d1SJeff Roberson int wait) 589ba63339aSAlan Cox { 590ba63339aSAlan Cox 5917630c265SAlan Cox /* Inform UMA that this allocator uses kernel_map/object. */ 5927630c265SAlan Cox *flags = UMA_SLAB_KERNEL; 593ab3185d1SJeff Roberson return ((void *)kmem_alloc_contig_domain(domain, bytes, wait, 5943153e878SAlan Cox (vm_paddr_t)0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT)); 595ba63339aSAlan Cox } 596ba63339aSAlan Cox 597ba63339aSAlan Cox /* 598099a0e58SBosko Milekic * Constructor for Mbuf master zone. 599099a0e58SBosko Milekic * 600099a0e58SBosko Milekic * The 'arg' pointer points to a mb_args structure which 601099a0e58SBosko Milekic * contains call-specific information required to support the 60256a4e45aSAndre Oppermann * mbuf allocation API. See mbuf.h. 603099a0e58SBosko Milekic */ 604b23f72e9SBrian Feldman static int 605b23f72e9SBrian Feldman mb_ctor_mbuf(void *mem, int size, void *arg, int how) 606099a0e58SBosko Milekic { 607099a0e58SBosko Milekic struct mbuf *m; 608099a0e58SBosko Milekic struct mb_args *args; 609b23f72e9SBrian Feldman int error; 610099a0e58SBosko Milekic int flags; 611099a0e58SBosko Milekic short type; 612099a0e58SBosko Milekic 613121f0509SMike Silbersack #ifdef INVARIANTS 614121f0509SMike Silbersack trash_ctor(mem, size, arg, how); 615121f0509SMike Silbersack #endif 616099a0e58SBosko Milekic args = (struct mb_args *)arg; 617099a0e58SBosko Milekic type = args->type; 618099a0e58SBosko Milekic 61956a4e45aSAndre Oppermann /* 62056a4e45aSAndre Oppermann * The mbuf is initialized later. The caller has the 621fcf90618SGleb Smirnoff * responsibility to set up any MAC labels too. 62256a4e45aSAndre Oppermann */ 62356a4e45aSAndre Oppermann if (type == MT_NOINIT) 62456a4e45aSAndre Oppermann return (0); 62556a4e45aSAndre Oppermann 626afb295ccSAndre Oppermann m = (struct mbuf *)mem; 627afb295ccSAndre Oppermann flags = args->flags; 628fddd4f62SNavdeep Parhar MPASS((flags & M_NOFREE) == 0); 629afb295ccSAndre Oppermann 630b4b12e52SGleb Smirnoff error = m_init(m, how, type, flags); 631afb295ccSAndre Oppermann 632b23f72e9SBrian Feldman return (error); 633099a0e58SBosko Milekic } 634099a0e58SBosko Milekic 635099a0e58SBosko Milekic /* 63656a4e45aSAndre Oppermann * The Mbuf master zone destructor. 637099a0e58SBosko Milekic */ 638099a0e58SBosko Milekic static void 639099a0e58SBosko Milekic mb_dtor_mbuf(void *mem, int size, void *arg) 640099a0e58SBosko Milekic { 641099a0e58SBosko Milekic struct mbuf *m; 642629b9e08SKip Macy unsigned long flags; 643099a0e58SBosko Milekic 644099a0e58SBosko Milekic m = (struct mbuf *)mem; 645629b9e08SKip Macy flags = (unsigned long)arg; 646629b9e08SKip Macy 647a9fa76f2SNavdeep Parhar KASSERT((m->m_flags & M_NOFREE) == 0, ("%s: M_NOFREE set", __func__)); 6484c7070dbSScott Long if (!(flags & MB_DTOR_SKIP) && (m->m_flags & M_PKTHDR) && !SLIST_EMPTY(&m->m_pkthdr.tags)) 649099a0e58SBosko Milekic m_tag_delete_chain(m, NULL); 650121f0509SMike Silbersack #ifdef INVARIANTS 651121f0509SMike Silbersack trash_dtor(mem, size, arg); 652121f0509SMike Silbersack #endif 653099a0e58SBosko Milekic } 654099a0e58SBosko Milekic 65556a4e45aSAndre Oppermann /* 65656a4e45aSAndre Oppermann * The Mbuf Packet zone destructor. 65756a4e45aSAndre Oppermann */ 658099a0e58SBosko Milekic static void 659099a0e58SBosko Milekic mb_dtor_pack(void *mem, int size, void *arg) 660099a0e58SBosko Milekic { 661099a0e58SBosko Milekic struct mbuf *m; 662099a0e58SBosko Milekic 663099a0e58SBosko Milekic m = (struct mbuf *)mem; 664099a0e58SBosko Milekic if ((m->m_flags & M_PKTHDR) != 0) 665099a0e58SBosko Milekic m_tag_delete_chain(m, NULL); 66656a4e45aSAndre Oppermann 66756a4e45aSAndre Oppermann /* Make sure we've got a clean cluster back. */ 66856a4e45aSAndre Oppermann KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__)); 66956a4e45aSAndre Oppermann KASSERT(m->m_ext.ext_buf != NULL, ("%s: ext_buf == NULL", __func__)); 67056a4e45aSAndre Oppermann KASSERT(m->m_ext.ext_free == NULL, ("%s: ext_free != NULL", __func__)); 671cf827063SPoul-Henning Kamp KASSERT(m->m_ext.ext_arg1 == NULL, ("%s: ext_arg1 != NULL", __func__)); 672cf827063SPoul-Henning Kamp KASSERT(m->m_ext.ext_arg2 == NULL, ("%s: ext_arg2 != NULL", __func__)); 67356a4e45aSAndre Oppermann KASSERT(m->m_ext.ext_size == MCLBYTES, ("%s: ext_size != MCLBYTES", __func__)); 67449d46b61SGleb Smirnoff KASSERT(m->m_ext.ext_type == EXT_PACKET, ("%s: ext_type != EXT_PACKET", __func__)); 675121f0509SMike Silbersack #ifdef INVARIANTS 676121f0509SMike Silbersack trash_dtor(m->m_ext.ext_buf, MCLBYTES, arg); 677121f0509SMike Silbersack #endif 6786c125b8dSMohan Srinivasan /* 679ef44c8d2SDavid E. O'Brien * If there are processes blocked on zone_clust, waiting for pages 680ef44c8d2SDavid E. O'Brien * to be freed up, * cause them to be woken up by draining the 681ef44c8d2SDavid E. O'Brien * packet zone. We are exposed to a race here * (in the check for 682ef44c8d2SDavid E. O'Brien * the UMA_ZFLAG_FULL) where we might miss the flag set, but that 683ef44c8d2SDavid E. O'Brien * is deliberate. We don't want to acquire the zone lock for every 684ef44c8d2SDavid E. O'Brien * mbuf free. 6856c125b8dSMohan Srinivasan */ 6866c125b8dSMohan Srinivasan if (uma_zone_exhausted_nolock(zone_clust)) 6876c125b8dSMohan Srinivasan zone_drain(zone_pack); 688099a0e58SBosko Milekic } 689099a0e58SBosko Milekic 690099a0e58SBosko Milekic /* 691ec63cb90SAndre Oppermann * The Cluster and Jumbo[PAGESIZE|9|16] zone constructor. 692099a0e58SBosko Milekic * 693099a0e58SBosko Milekic * Here the 'arg' pointer points to the Mbuf which we 69456a4e45aSAndre Oppermann * are configuring cluster storage for. If 'arg' is 69556a4e45aSAndre Oppermann * empty we allocate just the cluster without setting 69656a4e45aSAndre Oppermann * the mbuf to it. See mbuf.h. 697099a0e58SBosko Milekic */ 698b23f72e9SBrian Feldman static int 699b23f72e9SBrian Feldman mb_ctor_clust(void *mem, int size, void *arg, int how) 700099a0e58SBosko Milekic { 701099a0e58SBosko Milekic struct mbuf *m; 702099a0e58SBosko Milekic 703121f0509SMike Silbersack #ifdef INVARIANTS 704121f0509SMike Silbersack trash_ctor(mem, size, arg, how); 705121f0509SMike Silbersack #endif 7060f4d9d04SKip Macy m = (struct mbuf *)arg; 7070f4d9d04SKip Macy if (m != NULL) { 708e8fd18f3SGleb Smirnoff m->m_ext.ext_buf = (char *)mem; 709099a0e58SBosko Milekic m->m_data = m->m_ext.ext_buf; 710099a0e58SBosko Milekic m->m_flags |= M_EXT; 711099a0e58SBosko Milekic m->m_ext.ext_free = NULL; 712cf827063SPoul-Henning Kamp m->m_ext.ext_arg1 = NULL; 713cf827063SPoul-Henning Kamp m->m_ext.ext_arg2 = NULL; 71456a4e45aSAndre Oppermann m->m_ext.ext_size = size; 71556a5f52eSGleb Smirnoff m->m_ext.ext_type = m_gettype(size); 71656a5f52eSGleb Smirnoff m->m_ext.ext_flags = EXT_FLAG_EMBREF; 71756a5f52eSGleb Smirnoff m->m_ext.ext_count = 1; 71856a4e45aSAndre Oppermann } 7190f4d9d04SKip Macy 720b23f72e9SBrian Feldman return (0); 721099a0e58SBosko Milekic } 722099a0e58SBosko Milekic 72356a4e45aSAndre Oppermann /* 724099a0e58SBosko Milekic * The Packet secondary zone's init routine, executed on the 72556a4e45aSAndre Oppermann * object's transition from mbuf keg slab to zone cache. 726099a0e58SBosko Milekic */ 727b23f72e9SBrian Feldman static int 72856a4e45aSAndre Oppermann mb_zinit_pack(void *mem, int size, int how) 729099a0e58SBosko Milekic { 730099a0e58SBosko Milekic struct mbuf *m; 731099a0e58SBosko Milekic 73256a4e45aSAndre Oppermann m = (struct mbuf *)mem; /* m is virgin. */ 733a7bd90efSAndre Oppermann if (uma_zalloc_arg(zone_clust, m, how) == NULL || 734a7bd90efSAndre Oppermann m->m_ext.ext_buf == NULL) 735b23f72e9SBrian Feldman return (ENOMEM); 736cd5bb63bSAndre Oppermann m->m_ext.ext_type = EXT_PACKET; /* Override. */ 737121f0509SMike Silbersack #ifdef INVARIANTS 738121f0509SMike Silbersack trash_init(m->m_ext.ext_buf, MCLBYTES, how); 739121f0509SMike Silbersack #endif 740b23f72e9SBrian Feldman return (0); 741099a0e58SBosko Milekic } 742099a0e58SBosko Milekic 743099a0e58SBosko Milekic /* 744099a0e58SBosko Milekic * The Packet secondary zone's fini routine, executed on the 745099a0e58SBosko Milekic * object's transition from zone cache to keg slab. 746099a0e58SBosko Milekic */ 747099a0e58SBosko Milekic static void 74856a4e45aSAndre Oppermann mb_zfini_pack(void *mem, int size) 749099a0e58SBosko Milekic { 750099a0e58SBosko Milekic struct mbuf *m; 751099a0e58SBosko Milekic 752099a0e58SBosko Milekic m = (struct mbuf *)mem; 753121f0509SMike Silbersack #ifdef INVARIANTS 754121f0509SMike Silbersack trash_fini(m->m_ext.ext_buf, MCLBYTES); 755121f0509SMike Silbersack #endif 756099a0e58SBosko Milekic uma_zfree_arg(zone_clust, m->m_ext.ext_buf, NULL); 757a7b844d2SMike Silbersack #ifdef INVARIANTS 758a7b844d2SMike Silbersack trash_dtor(mem, size, NULL); 759a7b844d2SMike Silbersack #endif 760099a0e58SBosko Milekic } 761099a0e58SBosko Milekic 762099a0e58SBosko Milekic /* 763099a0e58SBosko Milekic * The "packet" keg constructor. 764099a0e58SBosko Milekic */ 765b23f72e9SBrian Feldman static int 766b23f72e9SBrian Feldman mb_ctor_pack(void *mem, int size, void *arg, int how) 767099a0e58SBosko Milekic { 768099a0e58SBosko Milekic struct mbuf *m; 769099a0e58SBosko Milekic struct mb_args *args; 770ce28636bSAndre Oppermann int error, flags; 771099a0e58SBosko Milekic short type; 772099a0e58SBosko Milekic 773099a0e58SBosko Milekic m = (struct mbuf *)mem; 774099a0e58SBosko Milekic args = (struct mb_args *)arg; 775099a0e58SBosko Milekic flags = args->flags; 776099a0e58SBosko Milekic type = args->type; 777fddd4f62SNavdeep Parhar MPASS((flags & M_NOFREE) == 0); 778099a0e58SBosko Milekic 779121f0509SMike Silbersack #ifdef INVARIANTS 780121f0509SMike Silbersack trash_ctor(m->m_ext.ext_buf, MCLBYTES, arg, how); 781121f0509SMike Silbersack #endif 782099a0e58SBosko Milekic 783b4b12e52SGleb Smirnoff error = m_init(m, how, type, flags); 784afb295ccSAndre Oppermann 78556a4e45aSAndre Oppermann /* m_ext is already initialized. */ 786afb295ccSAndre Oppermann m->m_data = m->m_ext.ext_buf; 787afb295ccSAndre Oppermann m->m_flags = (flags | M_EXT); 78856a4e45aSAndre Oppermann 789afb295ccSAndre Oppermann return (error); 790099a0e58SBosko Milekic } 791099a0e58SBosko Milekic 792099a0e58SBosko Milekic /* 793e60b2fcbSGleb Smirnoff * This is the protocol drain routine. Called by UMA whenever any of the 794e60b2fcbSGleb Smirnoff * mbuf zones is closed to its limit. 795099a0e58SBosko Milekic * 796099a0e58SBosko Milekic * No locks should be held when this is called. The drain routines have to 797099a0e58SBosko Milekic * presently acquire some locks which raises the possibility of lock order 798099a0e58SBosko Milekic * reversal. 799099a0e58SBosko Milekic */ 800099a0e58SBosko Milekic static void 801e60b2fcbSGleb Smirnoff mb_reclaim(uma_zone_t zone __unused, int pending __unused) 802099a0e58SBosko Milekic { 803099a0e58SBosko Milekic struct domain *dp; 804099a0e58SBosko Milekic struct protosw *pr; 805099a0e58SBosko Milekic 806e60b2fcbSGleb Smirnoff WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK | WARN_PANIC, NULL, __func__); 807099a0e58SBosko Milekic 808099a0e58SBosko Milekic for (dp = domains; dp != NULL; dp = dp->dom_next) 809099a0e58SBosko Milekic for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 810099a0e58SBosko Milekic if (pr->pr_drain != NULL) 811099a0e58SBosko Milekic (*pr->pr_drain)(); 812099a0e58SBosko Milekic } 8135e4bc63bSGleb Smirnoff 8145e4bc63bSGleb Smirnoff /* 8155e4bc63bSGleb Smirnoff * Clean up after mbufs with M_EXT storage attached to them if the 8165e4bc63bSGleb Smirnoff * reference count hits 1. 8175e4bc63bSGleb Smirnoff */ 8185e4bc63bSGleb Smirnoff void 8195e4bc63bSGleb Smirnoff mb_free_ext(struct mbuf *m) 8205e4bc63bSGleb Smirnoff { 82156a5f52eSGleb Smirnoff volatile u_int *refcnt; 82256a5f52eSGleb Smirnoff struct mbuf *mref; 8235e4bc63bSGleb Smirnoff int freembuf; 8245e4bc63bSGleb Smirnoff 8255e4bc63bSGleb Smirnoff KASSERT(m->m_flags & M_EXT, ("%s: M_EXT not set on %p", __func__, m)); 8265e4bc63bSGleb Smirnoff 82756a5f52eSGleb Smirnoff /* See if this is the mbuf that holds the embedded refcount. */ 82856a5f52eSGleb Smirnoff if (m->m_ext.ext_flags & EXT_FLAG_EMBREF) { 82956a5f52eSGleb Smirnoff refcnt = &m->m_ext.ext_count; 83056a5f52eSGleb Smirnoff mref = m; 83156a5f52eSGleb Smirnoff } else { 83256a5f52eSGleb Smirnoff KASSERT(m->m_ext.ext_cnt != NULL, 83356a5f52eSGleb Smirnoff ("%s: no refcounting pointer on %p", __func__, m)); 83456a5f52eSGleb Smirnoff refcnt = m->m_ext.ext_cnt; 83556a5f52eSGleb Smirnoff mref = __containerof(refcnt, struct mbuf, m_ext.ext_count); 83656a5f52eSGleb Smirnoff } 83756a5f52eSGleb Smirnoff 8385e4bc63bSGleb Smirnoff /* 83956a5f52eSGleb Smirnoff * Check if the header is embedded in the cluster. It is 84056a5f52eSGleb Smirnoff * important that we can't touch any of the mbuf fields 84156a5f52eSGleb Smirnoff * after we have freed the external storage, since mbuf 84217cd649fSGleb Smirnoff * could have been embedded in it. For now, the mbufs 84317cd649fSGleb Smirnoff * embedded into the cluster are always of type EXT_EXTREF, 84417cd649fSGleb Smirnoff * and for this type we won't free the mref. 8455e4bc63bSGleb Smirnoff */ 84617cd649fSGleb Smirnoff if (m->m_flags & M_NOFREE) { 84717cd649fSGleb Smirnoff freembuf = 0; 84817cd649fSGleb Smirnoff KASSERT(m->m_ext.ext_type == EXT_EXTREF, 84917cd649fSGleb Smirnoff ("%s: no-free mbuf %p has wrong type", __func__, m)); 85017cd649fSGleb Smirnoff } else 85117cd649fSGleb Smirnoff freembuf = 1; 8525e4bc63bSGleb Smirnoff 85356a5f52eSGleb Smirnoff /* Free attached storage if this mbuf is the only reference to it. */ 85456a5f52eSGleb Smirnoff if (*refcnt == 1 || atomic_fetchadd_int(refcnt, -1) == 1) { 8555e4bc63bSGleb Smirnoff switch (m->m_ext.ext_type) { 85656a5f52eSGleb Smirnoff case EXT_PACKET: 85756a5f52eSGleb Smirnoff /* The packet zone is special. */ 85856a5f52eSGleb Smirnoff if (*refcnt == 0) 85956a5f52eSGleb Smirnoff *refcnt = 1; 86056a5f52eSGleb Smirnoff uma_zfree(zone_pack, mref); 8615e4bc63bSGleb Smirnoff break; 8625e4bc63bSGleb Smirnoff case EXT_CLUSTER: 8635e4bc63bSGleb Smirnoff uma_zfree(zone_clust, m->m_ext.ext_buf); 86456a5f52eSGleb Smirnoff uma_zfree(zone_mbuf, mref); 8655e4bc63bSGleb Smirnoff break; 8665e4bc63bSGleb Smirnoff case EXT_JUMBOP: 8675e4bc63bSGleb Smirnoff uma_zfree(zone_jumbop, m->m_ext.ext_buf); 86856a5f52eSGleb Smirnoff uma_zfree(zone_mbuf, mref); 8695e4bc63bSGleb Smirnoff break; 8705e4bc63bSGleb Smirnoff case EXT_JUMBO9: 8715e4bc63bSGleb Smirnoff uma_zfree(zone_jumbo9, m->m_ext.ext_buf); 87256a5f52eSGleb Smirnoff uma_zfree(zone_mbuf, mref); 8735e4bc63bSGleb Smirnoff break; 8745e4bc63bSGleb Smirnoff case EXT_JUMBO16: 8755e4bc63bSGleb Smirnoff uma_zfree(zone_jumbo16, m->m_ext.ext_buf); 87656a5f52eSGleb Smirnoff uma_zfree(zone_mbuf, mref); 87756a5f52eSGleb Smirnoff break; 87856a5f52eSGleb Smirnoff case EXT_SFBUF: 8795e4bc63bSGleb Smirnoff case EXT_NET_DRV: 8805e4bc63bSGleb Smirnoff case EXT_MOD_TYPE: 8815e4bc63bSGleb Smirnoff case EXT_DISPOSABLE: 88207e87a1dSGleb Smirnoff KASSERT(mref->m_ext.ext_free != NULL, 8830ea37a86SGleb Smirnoff ("%s: ext_free not set", __func__)); 88407e87a1dSGleb Smirnoff mref->m_ext.ext_free(mref); 88556a5f52eSGleb Smirnoff uma_zfree(zone_mbuf, mref); 8860ea37a86SGleb Smirnoff break; 8875e4bc63bSGleb Smirnoff case EXT_EXTREF: 8885e4bc63bSGleb Smirnoff KASSERT(m->m_ext.ext_free != NULL, 8895e4bc63bSGleb Smirnoff ("%s: ext_free not set", __func__)); 890e8fd18f3SGleb Smirnoff m->m_ext.ext_free(m); 8915e4bc63bSGleb Smirnoff break; 8925e4bc63bSGleb Smirnoff default: 8935e4bc63bSGleb Smirnoff KASSERT(m->m_ext.ext_type == 0, 8945e4bc63bSGleb Smirnoff ("%s: unknown ext_type", __func__)); 8955e4bc63bSGleb Smirnoff } 8965e4bc63bSGleb Smirnoff } 8975e4bc63bSGleb Smirnoff 89856a5f52eSGleb Smirnoff if (freembuf && m != mref) 8995e4bc63bSGleb Smirnoff uma_zfree(zone_mbuf, m); 9005e4bc63bSGleb Smirnoff } 9015e4bc63bSGleb Smirnoff 9025e4bc63bSGleb Smirnoff /* 9035e4bc63bSGleb Smirnoff * Official mbuf(9) allocation KPI for stack and drivers: 9045e4bc63bSGleb Smirnoff * 9055e4bc63bSGleb Smirnoff * m_get() - a single mbuf without any attachments, sys/mbuf.h. 9065e4bc63bSGleb Smirnoff * m_gethdr() - a single mbuf initialized as M_PKTHDR, sys/mbuf.h. 9075e4bc63bSGleb Smirnoff * m_getcl() - an mbuf + 2k cluster, sys/mbuf.h. 9085e4bc63bSGleb Smirnoff * m_clget() - attach cluster to already allocated mbuf. 9095e4bc63bSGleb Smirnoff * m_cljget() - attach jumbo cluster to already allocated mbuf. 9105e4bc63bSGleb Smirnoff * m_get2() - allocate minimum mbuf that would fit size argument. 9115e4bc63bSGleb Smirnoff * m_getm2() - allocate a chain of mbufs/clusters. 9125e4bc63bSGleb Smirnoff * m_extadd() - attach external cluster to mbuf. 9135e4bc63bSGleb Smirnoff * 9145e4bc63bSGleb Smirnoff * m_free() - free single mbuf with its tags and ext, sys/mbuf.h. 9155e4bc63bSGleb Smirnoff * m_freem() - free chain of mbufs. 9165e4bc63bSGleb Smirnoff */ 9175e4bc63bSGleb Smirnoff 9185e4bc63bSGleb Smirnoff int 9195e4bc63bSGleb Smirnoff m_clget(struct mbuf *m, int how) 9205e4bc63bSGleb Smirnoff { 9215e4bc63bSGleb Smirnoff 9225e4bc63bSGleb Smirnoff KASSERT((m->m_flags & M_EXT) == 0, ("%s: mbuf %p has M_EXT", 9235e4bc63bSGleb Smirnoff __func__, m)); 9245e4bc63bSGleb Smirnoff m->m_ext.ext_buf = (char *)NULL; 9255e4bc63bSGleb Smirnoff uma_zalloc_arg(zone_clust, m, how); 9265e4bc63bSGleb Smirnoff /* 9275e4bc63bSGleb Smirnoff * On a cluster allocation failure, drain the packet zone and retry, 9285e4bc63bSGleb Smirnoff * we might be able to loosen a few clusters up on the drain. 9295e4bc63bSGleb Smirnoff */ 9305e4bc63bSGleb Smirnoff if ((how & M_NOWAIT) && (m->m_ext.ext_buf == NULL)) { 9315e4bc63bSGleb Smirnoff zone_drain(zone_pack); 9325e4bc63bSGleb Smirnoff uma_zalloc_arg(zone_clust, m, how); 9335e4bc63bSGleb Smirnoff } 934480f4e94SGeorge V. Neville-Neil MBUF_PROBE2(m__clget, m, how); 9355e4bc63bSGleb Smirnoff return (m->m_flags & M_EXT); 9365e4bc63bSGleb Smirnoff } 9375e4bc63bSGleb Smirnoff 9385e4bc63bSGleb Smirnoff /* 9395e4bc63bSGleb Smirnoff * m_cljget() is different from m_clget() as it can allocate clusters without 9405e4bc63bSGleb Smirnoff * attaching them to an mbuf. In that case the return value is the pointer 9415e4bc63bSGleb Smirnoff * to the cluster of the requested size. If an mbuf was specified, it gets 9425e4bc63bSGleb Smirnoff * the cluster attached to it and the return value can be safely ignored. 9435e4bc63bSGleb Smirnoff * For size it takes MCLBYTES, MJUMPAGESIZE, MJUM9BYTES, MJUM16BYTES. 9445e4bc63bSGleb Smirnoff */ 9455e4bc63bSGleb Smirnoff void * 9465e4bc63bSGleb Smirnoff m_cljget(struct mbuf *m, int how, int size) 9475e4bc63bSGleb Smirnoff { 9485e4bc63bSGleb Smirnoff uma_zone_t zone; 949480f4e94SGeorge V. Neville-Neil void *retval; 9505e4bc63bSGleb Smirnoff 9515e4bc63bSGleb Smirnoff if (m != NULL) { 9525e4bc63bSGleb Smirnoff KASSERT((m->m_flags & M_EXT) == 0, ("%s: mbuf %p has M_EXT", 9535e4bc63bSGleb Smirnoff __func__, m)); 9545e4bc63bSGleb Smirnoff m->m_ext.ext_buf = NULL; 9555e4bc63bSGleb Smirnoff } 9565e4bc63bSGleb Smirnoff 9575e4bc63bSGleb Smirnoff zone = m_getzone(size); 958480f4e94SGeorge V. Neville-Neil retval = uma_zalloc_arg(zone, m, how); 959480f4e94SGeorge V. Neville-Neil 960480f4e94SGeorge V. Neville-Neil MBUF_PROBE4(m__cljget, m, how, size, retval); 961480f4e94SGeorge V. Neville-Neil 962480f4e94SGeorge V. Neville-Neil return (retval); 9635e4bc63bSGleb Smirnoff } 9645e4bc63bSGleb Smirnoff 9655e4bc63bSGleb Smirnoff /* 9665e4bc63bSGleb Smirnoff * m_get2() allocates minimum mbuf that would fit "size" argument. 9675e4bc63bSGleb Smirnoff */ 9685e4bc63bSGleb Smirnoff struct mbuf * 9695e4bc63bSGleb Smirnoff m_get2(int size, int how, short type, int flags) 9705e4bc63bSGleb Smirnoff { 9715e4bc63bSGleb Smirnoff struct mb_args args; 9725e4bc63bSGleb Smirnoff struct mbuf *m, *n; 9735e4bc63bSGleb Smirnoff 9745e4bc63bSGleb Smirnoff args.flags = flags; 9755e4bc63bSGleb Smirnoff args.type = type; 9765e4bc63bSGleb Smirnoff 9775e4bc63bSGleb Smirnoff if (size <= MHLEN || (size <= MLEN && (flags & M_PKTHDR) == 0)) 9785e4bc63bSGleb Smirnoff return (uma_zalloc_arg(zone_mbuf, &args, how)); 9795e4bc63bSGleb Smirnoff if (size <= MCLBYTES) 9805e4bc63bSGleb Smirnoff return (uma_zalloc_arg(zone_pack, &args, how)); 9815e4bc63bSGleb Smirnoff 9825e4bc63bSGleb Smirnoff if (size > MJUMPAGESIZE) 9835e4bc63bSGleb Smirnoff return (NULL); 9845e4bc63bSGleb Smirnoff 9855e4bc63bSGleb Smirnoff m = uma_zalloc_arg(zone_mbuf, &args, how); 9865e4bc63bSGleb Smirnoff if (m == NULL) 9875e4bc63bSGleb Smirnoff return (NULL); 9885e4bc63bSGleb Smirnoff 9895e4bc63bSGleb Smirnoff n = uma_zalloc_arg(zone_jumbop, m, how); 9905e4bc63bSGleb Smirnoff if (n == NULL) { 9915e4bc63bSGleb Smirnoff uma_zfree(zone_mbuf, m); 9925e4bc63bSGleb Smirnoff return (NULL); 9935e4bc63bSGleb Smirnoff } 9945e4bc63bSGleb Smirnoff 9955e4bc63bSGleb Smirnoff return (m); 9965e4bc63bSGleb Smirnoff } 9975e4bc63bSGleb Smirnoff 9985e4bc63bSGleb Smirnoff /* 9995e4bc63bSGleb Smirnoff * m_getjcl() returns an mbuf with a cluster of the specified size attached. 10005e4bc63bSGleb Smirnoff * For size it takes MCLBYTES, MJUMPAGESIZE, MJUM9BYTES, MJUM16BYTES. 10015e4bc63bSGleb Smirnoff */ 10025e4bc63bSGleb Smirnoff struct mbuf * 10035e4bc63bSGleb Smirnoff m_getjcl(int how, short type, int flags, int size) 10045e4bc63bSGleb Smirnoff { 10055e4bc63bSGleb Smirnoff struct mb_args args; 10065e4bc63bSGleb Smirnoff struct mbuf *m, *n; 10075e4bc63bSGleb Smirnoff uma_zone_t zone; 10085e4bc63bSGleb Smirnoff 10095e4bc63bSGleb Smirnoff if (size == MCLBYTES) 10105e4bc63bSGleb Smirnoff return m_getcl(how, type, flags); 10115e4bc63bSGleb Smirnoff 10125e4bc63bSGleb Smirnoff args.flags = flags; 10135e4bc63bSGleb Smirnoff args.type = type; 10145e4bc63bSGleb Smirnoff 10155e4bc63bSGleb Smirnoff m = uma_zalloc_arg(zone_mbuf, &args, how); 10165e4bc63bSGleb Smirnoff if (m == NULL) 10175e4bc63bSGleb Smirnoff return (NULL); 10185e4bc63bSGleb Smirnoff 10195e4bc63bSGleb Smirnoff zone = m_getzone(size); 10205e4bc63bSGleb Smirnoff n = uma_zalloc_arg(zone, m, how); 10215e4bc63bSGleb Smirnoff if (n == NULL) { 10225e4bc63bSGleb Smirnoff uma_zfree(zone_mbuf, m); 10235e4bc63bSGleb Smirnoff return (NULL); 10245e4bc63bSGleb Smirnoff } 10255e4bc63bSGleb Smirnoff return (m); 10265e4bc63bSGleb Smirnoff } 10275e4bc63bSGleb Smirnoff 10285e4bc63bSGleb Smirnoff /* 10295e4bc63bSGleb Smirnoff * Allocate a given length worth of mbufs and/or clusters (whatever fits 10305e4bc63bSGleb Smirnoff * best) and return a pointer to the top of the allocated chain. If an 10315e4bc63bSGleb Smirnoff * existing mbuf chain is provided, then we will append the new chain 10325e4bc63bSGleb Smirnoff * to the existing one but still return the top of the newly allocated 10335e4bc63bSGleb Smirnoff * chain. 10345e4bc63bSGleb Smirnoff */ 10355e4bc63bSGleb Smirnoff struct mbuf * 10365e4bc63bSGleb Smirnoff m_getm2(struct mbuf *m, int len, int how, short type, int flags) 10375e4bc63bSGleb Smirnoff { 10385e4bc63bSGleb Smirnoff struct mbuf *mb, *nm = NULL, *mtail = NULL; 10395e4bc63bSGleb Smirnoff 10405e4bc63bSGleb Smirnoff KASSERT(len >= 0, ("%s: len is < 0", __func__)); 10415e4bc63bSGleb Smirnoff 10425e4bc63bSGleb Smirnoff /* Validate flags. */ 10435e4bc63bSGleb Smirnoff flags &= (M_PKTHDR | M_EOR); 10445e4bc63bSGleb Smirnoff 10455e4bc63bSGleb Smirnoff /* Packet header mbuf must be first in chain. */ 10465e4bc63bSGleb Smirnoff if ((flags & M_PKTHDR) && m != NULL) 10475e4bc63bSGleb Smirnoff flags &= ~M_PKTHDR; 10485e4bc63bSGleb Smirnoff 10495e4bc63bSGleb Smirnoff /* Loop and append maximum sized mbufs to the chain tail. */ 10505e4bc63bSGleb Smirnoff while (len > 0) { 10515e4bc63bSGleb Smirnoff if (len > MCLBYTES) 10525e4bc63bSGleb Smirnoff mb = m_getjcl(how, type, (flags & M_PKTHDR), 10535e4bc63bSGleb Smirnoff MJUMPAGESIZE); 10545e4bc63bSGleb Smirnoff else if (len >= MINCLSIZE) 10555e4bc63bSGleb Smirnoff mb = m_getcl(how, type, (flags & M_PKTHDR)); 10565e4bc63bSGleb Smirnoff else if (flags & M_PKTHDR) 10575e4bc63bSGleb Smirnoff mb = m_gethdr(how, type); 10585e4bc63bSGleb Smirnoff else 10595e4bc63bSGleb Smirnoff mb = m_get(how, type); 10605e4bc63bSGleb Smirnoff 10615e4bc63bSGleb Smirnoff /* Fail the whole operation if one mbuf can't be allocated. */ 10625e4bc63bSGleb Smirnoff if (mb == NULL) { 10635e4bc63bSGleb Smirnoff if (nm != NULL) 10645e4bc63bSGleb Smirnoff m_freem(nm); 10655e4bc63bSGleb Smirnoff return (NULL); 10665e4bc63bSGleb Smirnoff } 10675e4bc63bSGleb Smirnoff 10685e4bc63bSGleb Smirnoff /* Book keeping. */ 10695e4bc63bSGleb Smirnoff len -= M_SIZE(mb); 10705e4bc63bSGleb Smirnoff if (mtail != NULL) 10715e4bc63bSGleb Smirnoff mtail->m_next = mb; 10725e4bc63bSGleb Smirnoff else 10735e4bc63bSGleb Smirnoff nm = mb; 10745e4bc63bSGleb Smirnoff mtail = mb; 10755e4bc63bSGleb Smirnoff flags &= ~M_PKTHDR; /* Only valid on the first mbuf. */ 10765e4bc63bSGleb Smirnoff } 10775e4bc63bSGleb Smirnoff if (flags & M_EOR) 10785e4bc63bSGleb Smirnoff mtail->m_flags |= M_EOR; /* Only valid on the last mbuf. */ 10795e4bc63bSGleb Smirnoff 10805e4bc63bSGleb Smirnoff /* If mbuf was supplied, append new chain to the end of it. */ 10815e4bc63bSGleb Smirnoff if (m != NULL) { 10825e4bc63bSGleb Smirnoff for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next) 10835e4bc63bSGleb Smirnoff ; 10845e4bc63bSGleb Smirnoff mtail->m_next = nm; 10855e4bc63bSGleb Smirnoff mtail->m_flags &= ~M_EOR; 10865e4bc63bSGleb Smirnoff } else 10875e4bc63bSGleb Smirnoff m = nm; 10885e4bc63bSGleb Smirnoff 10895e4bc63bSGleb Smirnoff return (m); 10905e4bc63bSGleb Smirnoff } 10915e4bc63bSGleb Smirnoff 10925e4bc63bSGleb Smirnoff /*- 10935e4bc63bSGleb Smirnoff * Configure a provided mbuf to refer to the provided external storage 109456a5f52eSGleb Smirnoff * buffer and setup a reference count for said buffer. 10955e4bc63bSGleb Smirnoff * 10965e4bc63bSGleb Smirnoff * Arguments: 10975e4bc63bSGleb Smirnoff * mb The existing mbuf to which to attach the provided buffer. 10985e4bc63bSGleb Smirnoff * buf The address of the provided external storage buffer. 10995e4bc63bSGleb Smirnoff * size The size of the provided buffer. 11005e4bc63bSGleb Smirnoff * freef A pointer to a routine that is responsible for freeing the 11015e4bc63bSGleb Smirnoff * provided external storage buffer. 11025e4bc63bSGleb Smirnoff * args A pointer to an argument structure (of any type) to be passed 11035e4bc63bSGleb Smirnoff * to the provided freef routine (may be NULL). 11045e4bc63bSGleb Smirnoff * flags Any other flags to be passed to the provided mbuf. 11055e4bc63bSGleb Smirnoff * type The type that the external storage buffer should be 11065e4bc63bSGleb Smirnoff * labeled with. 11075e4bc63bSGleb Smirnoff * 11085e4bc63bSGleb Smirnoff * Returns: 11095e4bc63bSGleb Smirnoff * Nothing. 11105e4bc63bSGleb Smirnoff */ 111156a5f52eSGleb Smirnoff void 1112e8fd18f3SGleb Smirnoff m_extadd(struct mbuf *mb, char *buf, u_int size, m_ext_free_t freef, 1113e8fd18f3SGleb Smirnoff void *arg1, void *arg2, int flags, int type) 11145e4bc63bSGleb Smirnoff { 111556a5f52eSGleb Smirnoff 11165e4bc63bSGleb Smirnoff KASSERT(type != EXT_CLUSTER, ("%s: EXT_CLUSTER not allowed", __func__)); 11175e4bc63bSGleb Smirnoff 11185e4bc63bSGleb Smirnoff mb->m_flags |= (M_EXT | flags); 11195e4bc63bSGleb Smirnoff mb->m_ext.ext_buf = buf; 11205e4bc63bSGleb Smirnoff mb->m_data = mb->m_ext.ext_buf; 11215e4bc63bSGleb Smirnoff mb->m_ext.ext_size = size; 11225e4bc63bSGleb Smirnoff mb->m_ext.ext_free = freef; 11235e4bc63bSGleb Smirnoff mb->m_ext.ext_arg1 = arg1; 11245e4bc63bSGleb Smirnoff mb->m_ext.ext_arg2 = arg2; 11255e4bc63bSGleb Smirnoff mb->m_ext.ext_type = type; 11265e4bc63bSGleb Smirnoff 112756a5f52eSGleb Smirnoff if (type != EXT_EXTREF) { 112856a5f52eSGleb Smirnoff mb->m_ext.ext_count = 1; 112956a5f52eSGleb Smirnoff mb->m_ext.ext_flags = EXT_FLAG_EMBREF; 113056a5f52eSGleb Smirnoff } else 113156a5f52eSGleb Smirnoff mb->m_ext.ext_flags = 0; 11325e4bc63bSGleb Smirnoff } 11335e4bc63bSGleb Smirnoff 11345e4bc63bSGleb Smirnoff /* 11355e4bc63bSGleb Smirnoff * Free an entire chain of mbufs and associated external buffers, if 11365e4bc63bSGleb Smirnoff * applicable. 11375e4bc63bSGleb Smirnoff */ 11385e4bc63bSGleb Smirnoff void 11395e4bc63bSGleb Smirnoff m_freem(struct mbuf *mb) 11405e4bc63bSGleb Smirnoff { 11415e4bc63bSGleb Smirnoff 1142c8f59118SGleb Smirnoff MBUF_PROBE1(m__freem, mb); 11435e4bc63bSGleb Smirnoff while (mb != NULL) 11445e4bc63bSGleb Smirnoff mb = m_free(mb); 11455e4bc63bSGleb Smirnoff } 1146