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" 34b2e60773SJohn Baldwin #include "opt_kern_tls.h" 35099a0e58SBosko Milekic 36099a0e58SBosko Milekic #include <sys/param.h> 373937ee75SConrad Meyer #include <sys/conf.h> 389978bd99SMark Johnston #include <sys/domainset.h> 39099a0e58SBosko Milekic #include <sys/malloc.h> 40099a0e58SBosko Milekic #include <sys/systm.h> 41099a0e58SBosko Milekic #include <sys/mbuf.h> 42099a0e58SBosko Milekic #include <sys/domain.h> 43099a0e58SBosko Milekic #include <sys/eventhandler.h> 44099a0e58SBosko Milekic #include <sys/kernel.h> 45b2e60773SJohn Baldwin #include <sys/ktls.h> 465475ca5aSMark Johnston #include <sys/limits.h> 4754503a13SJonathan T. Looney #include <sys/lock.h> 4854503a13SJonathan T. Looney #include <sys/mutex.h> 49099a0e58SBosko Milekic #include <sys/protosw.h> 50b2e60773SJohn Baldwin #include <sys/refcount.h> 5182334850SJohn Baldwin #include <sys/sf_buf.h> 52099a0e58SBosko Milekic #include <sys/smp.h> 53fb3bc596SJohn Baldwin #include <sys/socket.h> 54099a0e58SBosko Milekic #include <sys/sysctl.h> 55099a0e58SBosko Milekic 56fb3bc596SJohn Baldwin #include <net/if.h> 57fb3bc596SJohn Baldwin #include <net/if_var.h> 58fb3bc596SJohn Baldwin 59099a0e58SBosko Milekic #include <vm/vm.h> 60c45c0034SAlan Cox #include <vm/vm_extern.h> 61c45c0034SAlan Cox #include <vm/vm_kern.h> 62099a0e58SBosko Milekic #include <vm/vm_page.h> 6337140716SAndre Oppermann #include <vm/vm_map.h> 64099a0e58SBosko Milekic #include <vm/uma.h> 65121f0509SMike Silbersack #include <vm/uma_dbg.h> 66099a0e58SBosko Milekic 67099a0e58SBosko Milekic /* 68099a0e58SBosko Milekic * In FreeBSD, Mbufs and Mbuf Clusters are allocated from UMA 69099a0e58SBosko Milekic * Zones. 70099a0e58SBosko Milekic * 71099a0e58SBosko Milekic * Mbuf Clusters (2K, contiguous) are allocated from the Cluster 72099a0e58SBosko Milekic * Zone. The Zone can be capped at kern.ipc.nmbclusters, if the 73099a0e58SBosko Milekic * administrator so desires. 74099a0e58SBosko Milekic * 75099a0e58SBosko Milekic * Mbufs are allocated from a UMA Master Zone called the Mbuf 76099a0e58SBosko Milekic * Zone. 77099a0e58SBosko Milekic * 78099a0e58SBosko Milekic * Additionally, FreeBSD provides a Packet Zone, which it 79099a0e58SBosko Milekic * configures as a Secondary Zone to the Mbuf Master Zone, 80099a0e58SBosko Milekic * thus sharing backend Slab kegs with the Mbuf Master Zone. 81099a0e58SBosko Milekic * 82099a0e58SBosko Milekic * Thus common-case allocations and locking are simplified: 83099a0e58SBosko Milekic * 84099a0e58SBosko Milekic * m_clget() m_getcl() 85099a0e58SBosko Milekic * | | 86099a0e58SBosko Milekic * | .------------>[(Packet Cache)] m_get(), m_gethdr() 87099a0e58SBosko Milekic * | | [ Packet ] | 88099a0e58SBosko Milekic * [(Cluster Cache)] [ Secondary ] [ (Mbuf Cache) ] 89099a0e58SBosko Milekic * [ Cluster Zone ] [ Zone ] [ Mbuf Master Zone ] 90099a0e58SBosko Milekic * | \________ | 91099a0e58SBosko Milekic * [ Cluster Keg ] \ / 92099a0e58SBosko Milekic * | [ Mbuf Keg ] 93099a0e58SBosko Milekic * [ Cluster Slabs ] | 94099a0e58SBosko Milekic * | [ Mbuf Slabs ] 95099a0e58SBosko Milekic * \____________(VM)_________________/ 9656a4e45aSAndre Oppermann * 9756a4e45aSAndre Oppermann * 98fcf90618SGleb Smirnoff * Whenever an object is allocated with uma_zalloc() out of 9956a4e45aSAndre Oppermann * one of the Zones its _ctor_ function is executed. The same 100fcf90618SGleb Smirnoff * for any deallocation through uma_zfree() the _dtor_ function 10156a4e45aSAndre Oppermann * is executed. 10256a4e45aSAndre Oppermann * 10356a4e45aSAndre Oppermann * Caches are per-CPU and are filled from the Master Zone. 10456a4e45aSAndre Oppermann * 105fcf90618SGleb Smirnoff * Whenever an object is allocated from the underlying global 10656a4e45aSAndre Oppermann * memory pool it gets pre-initialized with the _zinit_ functions. 107e3043798SPedro F. Giffuni * When the Keg's are overfull objects get decommissioned with 10856a4e45aSAndre Oppermann * _zfini_ functions and free'd back to the global memory pool. 10956a4e45aSAndre Oppermann * 110099a0e58SBosko Milekic */ 111099a0e58SBosko Milekic 112ead46972SAndre Oppermann int nmbufs; /* limits number of mbufs */ 11356a4e45aSAndre Oppermann int nmbclusters; /* limits number of mbuf clusters */ 114ec63cb90SAndre Oppermann int nmbjumbop; /* limits number of page size jumbo clusters */ 11556a4e45aSAndre Oppermann int nmbjumbo9; /* limits number of 9k jumbo clusters */ 11656a4e45aSAndre Oppermann int nmbjumbo16; /* limits number of 16k jumbo clusters */ 117099a0e58SBosko Milekic 118b2e60773SJohn Baldwin bool mb_use_ext_pgs; /* use EXT_PGS mbufs for sendfile & TLS */ 119cec06a3eSJohn Baldwin SYSCTL_BOOL(_kern_ipc, OID_AUTO, mb_use_ext_pgs, CTLFLAG_RWTUN, 120cec06a3eSJohn Baldwin &mb_use_ext_pgs, 0, 121b2e60773SJohn Baldwin "Use unmapped mbufs for sendfile(2) and TLS offload"); 122cec06a3eSJohn Baldwin 123e0c00addSAndre Oppermann static quad_t maxmbufmem; /* overall real memory limit for all mbufs */ 124e0c00addSAndre Oppermann 125af3b2549SHans Petter Selasky SYSCTL_QUAD(_kern_ipc, OID_AUTO, maxmbufmem, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &maxmbufmem, 0, 126b6f49c23SHiren Panchasara "Maximum real memory allocatable to various mbuf types"); 127e0c00addSAndre Oppermann 128fb3bc596SJohn Baldwin static counter_u64_t snd_tag_count; 129fb3bc596SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc, OID_AUTO, num_snd_tags, CTLFLAG_RW, 130fb3bc596SJohn Baldwin &snd_tag_count, "# of active mbuf send tags"); 131fb3bc596SJohn Baldwin 13262938659SBjoern A. Zeeb /* 13337140716SAndre Oppermann * tunable_mbinit() has to be run before any mbuf allocations are done. 13462938659SBjoern A. Zeeb */ 135099a0e58SBosko Milekic static void 136099a0e58SBosko Milekic tunable_mbinit(void *dummy) 137099a0e58SBosko Milekic { 138e0c00addSAndre Oppermann quad_t realmem; 13937140716SAndre Oppermann 14037140716SAndre Oppermann /* 14137140716SAndre Oppermann * The default limit for all mbuf related memory is 1/2 of all 14237140716SAndre Oppermann * available kernel memory (physical or kmem). 14337140716SAndre Oppermann * At most it can be 3/4 of available kernel memory. 14437140716SAndre Oppermann */ 1455df87b21SJeff Roberson realmem = qmin((quad_t)physmem * PAGE_SIZE, vm_kmem_size); 14637140716SAndre Oppermann maxmbufmem = realmem / 2; 147e0c00addSAndre Oppermann TUNABLE_QUAD_FETCH("kern.ipc.maxmbufmem", &maxmbufmem); 14837140716SAndre Oppermann if (maxmbufmem > realmem / 4 * 3) 14937140716SAndre Oppermann maxmbufmem = realmem / 4 * 3; 150099a0e58SBosko Milekic 151812302c3SNavdeep Parhar TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters); 152416a434cSAndre Oppermann if (nmbclusters == 0) 153416a434cSAndre Oppermann nmbclusters = maxmbufmem / MCLBYTES / 4; 154812302c3SNavdeep Parhar 155812302c3SNavdeep Parhar TUNABLE_INT_FETCH("kern.ipc.nmbjumbop", &nmbjumbop); 156812302c3SNavdeep Parhar if (nmbjumbop == 0) 157416a434cSAndre Oppermann nmbjumbop = maxmbufmem / MJUMPAGESIZE / 4; 158812302c3SNavdeep Parhar 159812302c3SNavdeep Parhar TUNABLE_INT_FETCH("kern.ipc.nmbjumbo9", &nmbjumbo9); 160812302c3SNavdeep Parhar if (nmbjumbo9 == 0) 161416a434cSAndre Oppermann nmbjumbo9 = maxmbufmem / MJUM9BYTES / 6; 162812302c3SNavdeep Parhar 163812302c3SNavdeep Parhar TUNABLE_INT_FETCH("kern.ipc.nmbjumbo16", &nmbjumbo16); 164812302c3SNavdeep Parhar if (nmbjumbo16 == 0) 165416a434cSAndre Oppermann nmbjumbo16 = maxmbufmem / MJUM16BYTES / 6; 166416a434cSAndre Oppermann 167416a434cSAndre Oppermann /* 168416a434cSAndre Oppermann * We need at least as many mbufs as we have clusters of 169416a434cSAndre Oppermann * the various types added together. 170416a434cSAndre Oppermann */ 171416a434cSAndre Oppermann TUNABLE_INT_FETCH("kern.ipc.nmbufs", &nmbufs); 172416a434cSAndre Oppermann if (nmbufs < nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) 173416a434cSAndre Oppermann nmbufs = lmax(maxmbufmem / MSIZE / 5, 174416a434cSAndre Oppermann nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16); 175099a0e58SBosko Milekic } 17637140716SAndre Oppermann SYSINIT(tunable_mbinit, SI_SUB_KMEM, SI_ORDER_MIDDLE, tunable_mbinit, NULL); 177099a0e58SBosko Milekic 1784f590175SPaul Saab static int 1794f590175SPaul Saab sysctl_nmbclusters(SYSCTL_HANDLER_ARGS) 1804f590175SPaul Saab { 1814f590175SPaul Saab int error, newnmbclusters; 1824f590175SPaul Saab 1834f590175SPaul Saab newnmbclusters = nmbclusters; 184041b706bSDavid Malone error = sysctl_handle_int(oidp, &newnmbclusters, 0, req); 185d251e700SJohn Baldwin if (error == 0 && req->newptr && newnmbclusters != nmbclusters) { 186ead46972SAndre Oppermann if (newnmbclusters > nmbclusters && 187ead46972SAndre Oppermann nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) { 1884f590175SPaul Saab nmbclusters = newnmbclusters; 189bc4a1b8cSAndre Oppermann nmbclusters = uma_zone_set_max(zone_clust, nmbclusters); 1904f590175SPaul Saab EVENTHANDLER_INVOKE(nmbclusters_change); 1914f590175SPaul Saab } else 1924f590175SPaul Saab error = EINVAL; 1934f590175SPaul Saab } 1944f590175SPaul Saab return (error); 1954f590175SPaul Saab } 1964f590175SPaul Saab SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbclusters, CTLTYPE_INT|CTLFLAG_RW, 1974f590175SPaul Saab &nmbclusters, 0, sysctl_nmbclusters, "IU", 198099a0e58SBosko Milekic "Maximum number of mbuf clusters allowed"); 199cf70a46bSRandall Stewart 200cf70a46bSRandall Stewart static int 201cf70a46bSRandall Stewart sysctl_nmbjumbop(SYSCTL_HANDLER_ARGS) 202cf70a46bSRandall Stewart { 203cf70a46bSRandall Stewart int error, newnmbjumbop; 204cf70a46bSRandall Stewart 205cf70a46bSRandall Stewart newnmbjumbop = nmbjumbop; 206cf70a46bSRandall Stewart error = sysctl_handle_int(oidp, &newnmbjumbop, 0, req); 207d251e700SJohn Baldwin if (error == 0 && req->newptr && newnmbjumbop != nmbjumbop) { 208ead46972SAndre Oppermann if (newnmbjumbop > nmbjumbop && 209ead46972SAndre Oppermann nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) { 210cf70a46bSRandall Stewart nmbjumbop = newnmbjumbop; 211bc4a1b8cSAndre Oppermann nmbjumbop = uma_zone_set_max(zone_jumbop, nmbjumbop); 212cf70a46bSRandall Stewart } else 213cf70a46bSRandall Stewart error = EINVAL; 214cf70a46bSRandall Stewart } 215cf70a46bSRandall Stewart return (error); 216cf70a46bSRandall Stewart } 217cf70a46bSRandall Stewart SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbop, CTLTYPE_INT|CTLFLAG_RW, 218cf70a46bSRandall Stewart &nmbjumbop, 0, sysctl_nmbjumbop, "IU", 219ec63cb90SAndre Oppermann "Maximum number of mbuf page size jumbo clusters allowed"); 220cf70a46bSRandall Stewart 221cf70a46bSRandall Stewart static int 222cf70a46bSRandall Stewart sysctl_nmbjumbo9(SYSCTL_HANDLER_ARGS) 223cf70a46bSRandall Stewart { 224cf70a46bSRandall Stewart int error, newnmbjumbo9; 225cf70a46bSRandall Stewart 226cf70a46bSRandall Stewart newnmbjumbo9 = nmbjumbo9; 227cf70a46bSRandall Stewart error = sysctl_handle_int(oidp, &newnmbjumbo9, 0, req); 228d251e700SJohn Baldwin if (error == 0 && req->newptr && newnmbjumbo9 != nmbjumbo9) { 229ead46972SAndre Oppermann if (newnmbjumbo9 > nmbjumbo9 && 230ead46972SAndre Oppermann nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) { 231cf70a46bSRandall Stewart nmbjumbo9 = newnmbjumbo9; 232bc4a1b8cSAndre Oppermann nmbjumbo9 = uma_zone_set_max(zone_jumbo9, nmbjumbo9); 233cf70a46bSRandall Stewart } else 234cf70a46bSRandall Stewart error = EINVAL; 235cf70a46bSRandall Stewart } 236cf70a46bSRandall Stewart return (error); 237cf70a46bSRandall Stewart } 238cf70a46bSRandall Stewart SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbo9, CTLTYPE_INT|CTLFLAG_RW, 239cf70a46bSRandall Stewart &nmbjumbo9, 0, sysctl_nmbjumbo9, "IU", 24056a4e45aSAndre Oppermann "Maximum number of mbuf 9k jumbo clusters allowed"); 241cf70a46bSRandall Stewart 242cf70a46bSRandall Stewart static int 243cf70a46bSRandall Stewart sysctl_nmbjumbo16(SYSCTL_HANDLER_ARGS) 244cf70a46bSRandall Stewart { 245cf70a46bSRandall Stewart int error, newnmbjumbo16; 246cf70a46bSRandall Stewart 247cf70a46bSRandall Stewart newnmbjumbo16 = nmbjumbo16; 248cf70a46bSRandall Stewart error = sysctl_handle_int(oidp, &newnmbjumbo16, 0, req); 249d251e700SJohn Baldwin if (error == 0 && req->newptr && newnmbjumbo16 != nmbjumbo16) { 250ead46972SAndre Oppermann if (newnmbjumbo16 > nmbjumbo16 && 251ead46972SAndre Oppermann nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) { 252cf70a46bSRandall Stewart nmbjumbo16 = newnmbjumbo16; 253bc4a1b8cSAndre Oppermann nmbjumbo16 = uma_zone_set_max(zone_jumbo16, nmbjumbo16); 254cf70a46bSRandall Stewart } else 255cf70a46bSRandall Stewart error = EINVAL; 256cf70a46bSRandall Stewart } 257cf70a46bSRandall Stewart return (error); 258cf70a46bSRandall Stewart } 259cf70a46bSRandall Stewart SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbo16, CTLTYPE_INT|CTLFLAG_RW, 260cf70a46bSRandall Stewart &nmbjumbo16, 0, sysctl_nmbjumbo16, "IU", 26156a4e45aSAndre Oppermann "Maximum number of mbuf 16k jumbo clusters allowed"); 262cf70a46bSRandall Stewart 263ead46972SAndre Oppermann static int 264ead46972SAndre Oppermann sysctl_nmbufs(SYSCTL_HANDLER_ARGS) 265ead46972SAndre Oppermann { 266ead46972SAndre Oppermann int error, newnmbufs; 267ead46972SAndre Oppermann 268ead46972SAndre Oppermann newnmbufs = nmbufs; 269ead46972SAndre Oppermann error = sysctl_handle_int(oidp, &newnmbufs, 0, req); 270d251e700SJohn Baldwin if (error == 0 && req->newptr && newnmbufs != nmbufs) { 271ead46972SAndre Oppermann if (newnmbufs > nmbufs) { 272ead46972SAndre Oppermann nmbufs = newnmbufs; 273bc4a1b8cSAndre Oppermann nmbufs = uma_zone_set_max(zone_mbuf, nmbufs); 274ead46972SAndre Oppermann EVENTHANDLER_INVOKE(nmbufs_change); 275ead46972SAndre Oppermann } else 276ead46972SAndre Oppermann error = EINVAL; 277ead46972SAndre Oppermann } 278ead46972SAndre Oppermann return (error); 279ead46972SAndre Oppermann } 280e0c00addSAndre Oppermann SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbufs, CTLTYPE_INT|CTLFLAG_RW, 281ead46972SAndre Oppermann &nmbufs, 0, sysctl_nmbufs, "IU", 282ead46972SAndre Oppermann "Maximum number of mbufs allowed"); 283cf70a46bSRandall Stewart 284099a0e58SBosko Milekic /* 285099a0e58SBosko Milekic * Zones from which we allocate. 286099a0e58SBosko Milekic */ 287099a0e58SBosko Milekic uma_zone_t zone_mbuf; 288099a0e58SBosko Milekic uma_zone_t zone_clust; 289099a0e58SBosko Milekic uma_zone_t zone_pack; 290ec63cb90SAndre Oppermann uma_zone_t zone_jumbop; 29156a4e45aSAndre Oppermann uma_zone_t zone_jumbo9; 29256a4e45aSAndre Oppermann uma_zone_t zone_jumbo16; 29382334850SJohn Baldwin uma_zone_t zone_extpgs; 294099a0e58SBosko Milekic 295099a0e58SBosko Milekic /* 296099a0e58SBosko Milekic * Local prototypes. 297099a0e58SBosko Milekic */ 298b23f72e9SBrian Feldman static int mb_ctor_mbuf(void *, int, void *, int); 299b23f72e9SBrian Feldman static int mb_ctor_clust(void *, int, void *, int); 300b23f72e9SBrian Feldman static int mb_ctor_pack(void *, int, void *, int); 301099a0e58SBosko Milekic static void mb_dtor_mbuf(void *, int, void *); 30256a4e45aSAndre Oppermann static void mb_dtor_pack(void *, int, void *); 30356a4e45aSAndre Oppermann static int mb_zinit_pack(void *, int, int); 30456a4e45aSAndre Oppermann static void mb_zfini_pack(void *, int); 305e60b2fcbSGleb Smirnoff static void mb_reclaim(uma_zone_t, int); 306ab3185d1SJeff Roberson static void *mbuf_jumbo_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 307099a0e58SBosko Milekic 30837140716SAndre Oppermann /* Ensure that MSIZE is a power of 2. */ 309a04946cfSBrian Somers CTASSERT((((MSIZE - 1) ^ MSIZE) + 1) >> 1 == MSIZE); 310a04946cfSBrian Somers 31182334850SJohn Baldwin _Static_assert(sizeof(struct mbuf_ext_pgs) == 256, 31282334850SJohn Baldwin "mbuf_ext_pgs size mismatch"); 31382334850SJohn Baldwin 314099a0e58SBosko Milekic /* 315099a0e58SBosko Milekic * Initialize FreeBSD Network buffer allocation. 316099a0e58SBosko Milekic */ 317099a0e58SBosko Milekic static void 318099a0e58SBosko Milekic mbuf_init(void *dummy) 319099a0e58SBosko Milekic { 320099a0e58SBosko Milekic 321099a0e58SBosko Milekic /* 322099a0e58SBosko Milekic * Configure UMA zones for Mbufs, Clusters, and Packets. 323099a0e58SBosko Milekic */ 32456a4e45aSAndre Oppermann zone_mbuf = uma_zcreate(MBUF_MEM_NAME, MSIZE, 325*30be9685SRyan Libby mb_ctor_mbuf, mb_dtor_mbuf, NULL, NULL, 32656a4e45aSAndre Oppermann MSIZE - 1, UMA_ZONE_MAXBUCKET); 32745fe0bf7SPawel Jakub Dawidek if (nmbufs > 0) 32845fe0bf7SPawel Jakub Dawidek nmbufs = uma_zone_set_max(zone_mbuf, nmbufs); 3296e0b6746SPawel Jakub Dawidek uma_zone_set_warning(zone_mbuf, "kern.ipc.nmbufs limit reached"); 330e60b2fcbSGleb Smirnoff uma_zone_set_maxaction(zone_mbuf, mb_reclaim); 33156a4e45aSAndre Oppermann 33268352adfSRobert Watson zone_clust = uma_zcreate(MBUF_CLUSTER_MEM_NAME, MCLBYTES, 333*30be9685SRyan Libby mb_ctor_clust, NULL, NULL, NULL, 33456a5f52eSGleb Smirnoff UMA_ALIGN_PTR, 0); 33545fe0bf7SPawel Jakub Dawidek if (nmbclusters > 0) 33645fe0bf7SPawel Jakub Dawidek nmbclusters = uma_zone_set_max(zone_clust, nmbclusters); 3376e0b6746SPawel Jakub Dawidek uma_zone_set_warning(zone_clust, "kern.ipc.nmbclusters limit reached"); 338e60b2fcbSGleb Smirnoff uma_zone_set_maxaction(zone_clust, mb_reclaim); 339099a0e58SBosko Milekic 34056a4e45aSAndre Oppermann zone_pack = uma_zsecond_create(MBUF_PACKET_MEM_NAME, mb_ctor_pack, 34156a4e45aSAndre Oppermann mb_dtor_pack, mb_zinit_pack, mb_zfini_pack, zone_mbuf); 34256a4e45aSAndre Oppermann 343fcf90618SGleb Smirnoff /* Make jumbo frame zone too. Page size, 9k and 16k. */ 344ec63cb90SAndre Oppermann zone_jumbop = uma_zcreate(MBUF_JUMBOP_MEM_NAME, MJUMPAGESIZE, 345*30be9685SRyan Libby mb_ctor_clust, NULL, NULL, NULL, 34656a5f52eSGleb Smirnoff UMA_ALIGN_PTR, 0); 34745fe0bf7SPawel Jakub Dawidek if (nmbjumbop > 0) 34845fe0bf7SPawel Jakub Dawidek nmbjumbop = uma_zone_set_max(zone_jumbop, nmbjumbop); 3496e0b6746SPawel Jakub Dawidek uma_zone_set_warning(zone_jumbop, "kern.ipc.nmbjumbop limit reached"); 350e60b2fcbSGleb Smirnoff uma_zone_set_maxaction(zone_jumbop, mb_reclaim); 351d5269a63SAndre Oppermann 35256a4e45aSAndre Oppermann zone_jumbo9 = uma_zcreate(MBUF_JUMBO9_MEM_NAME, MJUM9BYTES, 353*30be9685SRyan Libby mb_ctor_clust, NULL, NULL, NULL, 35456a5f52eSGleb Smirnoff UMA_ALIGN_PTR, 0); 355ba63339aSAlan Cox uma_zone_set_allocf(zone_jumbo9, mbuf_jumbo_alloc); 35645fe0bf7SPawel Jakub Dawidek if (nmbjumbo9 > 0) 35745fe0bf7SPawel Jakub Dawidek nmbjumbo9 = uma_zone_set_max(zone_jumbo9, nmbjumbo9); 3586e0b6746SPawel Jakub Dawidek uma_zone_set_warning(zone_jumbo9, "kern.ipc.nmbjumbo9 limit reached"); 359e60b2fcbSGleb Smirnoff uma_zone_set_maxaction(zone_jumbo9, mb_reclaim); 36056a4e45aSAndre Oppermann 36156a4e45aSAndre Oppermann zone_jumbo16 = uma_zcreate(MBUF_JUMBO16_MEM_NAME, MJUM16BYTES, 362*30be9685SRyan Libby mb_ctor_clust, NULL, NULL, NULL, 36356a5f52eSGleb Smirnoff UMA_ALIGN_PTR, 0); 364ba63339aSAlan Cox uma_zone_set_allocf(zone_jumbo16, mbuf_jumbo_alloc); 36545fe0bf7SPawel Jakub Dawidek if (nmbjumbo16 > 0) 36645fe0bf7SPawel Jakub Dawidek nmbjumbo16 = uma_zone_set_max(zone_jumbo16, nmbjumbo16); 3676e0b6746SPawel Jakub Dawidek uma_zone_set_warning(zone_jumbo16, "kern.ipc.nmbjumbo16 limit reached"); 368e60b2fcbSGleb Smirnoff uma_zone_set_maxaction(zone_jumbo16, mb_reclaim); 36956a4e45aSAndre Oppermann 37082334850SJohn Baldwin zone_extpgs = uma_zcreate(MBUF_EXTPGS_MEM_NAME, 37182334850SJohn Baldwin sizeof(struct mbuf_ext_pgs), 37282334850SJohn Baldwin NULL, NULL, NULL, NULL, 37382334850SJohn Baldwin UMA_ALIGN_CACHE, 0); 37482334850SJohn Baldwin 375099a0e58SBosko Milekic /* 376099a0e58SBosko Milekic * Hook event handler for low-memory situation, used to 377099a0e58SBosko Milekic * drain protocols and push data back to the caches (UMA 378099a0e58SBosko Milekic * later pushes it back to VM). 379099a0e58SBosko Milekic */ 380099a0e58SBosko Milekic EVENTHANDLER_REGISTER(vm_lowmem, mb_reclaim, NULL, 381099a0e58SBosko Milekic EVENTHANDLER_PRI_FIRST); 382fb3bc596SJohn Baldwin 383fb3bc596SJohn Baldwin snd_tag_count = counter_u64_alloc(M_WAITOK); 384099a0e58SBosko Milekic } 38537140716SAndre Oppermann SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbuf_init, NULL); 386099a0e58SBosko Milekic 3877790c8c1SConrad Meyer #ifdef DEBUGNET 3885475ca5aSMark Johnston /* 3897790c8c1SConrad Meyer * debugnet makes use of a pre-allocated pool of mbufs and clusters. When 3907790c8c1SConrad Meyer * debugnet is configured, we initialize a set of UMA cache zones which return 3915475ca5aSMark Johnston * items from this pool. At panic-time, the regular UMA zone pointers are 3925475ca5aSMark Johnston * overwritten with those of the cache zones so that drivers may allocate and 3935475ca5aSMark Johnston * free mbufs and clusters without attempting to allocate physical memory. 3945475ca5aSMark Johnston * 3955475ca5aSMark Johnston * We keep mbufs and clusters in a pair of mbuf queues. In particular, for 3965475ca5aSMark Johnston * the purpose of caching clusters, we treat them as mbufs. 3975475ca5aSMark Johnston */ 3987790c8c1SConrad Meyer static struct mbufq dn_mbufq = 3997790c8c1SConrad Meyer { STAILQ_HEAD_INITIALIZER(dn_mbufq.mq_head), 0, INT_MAX }; 4007790c8c1SConrad Meyer static struct mbufq dn_clustq = 4017790c8c1SConrad Meyer { STAILQ_HEAD_INITIALIZER(dn_clustq.mq_head), 0, INT_MAX }; 4025475ca5aSMark Johnston 4037790c8c1SConrad Meyer static int dn_clsize; 4047790c8c1SConrad Meyer static uma_zone_t dn_zone_mbuf; 4057790c8c1SConrad Meyer static uma_zone_t dn_zone_clust; 4067790c8c1SConrad Meyer static uma_zone_t dn_zone_pack; 4077790c8c1SConrad Meyer 4087790c8c1SConrad Meyer static struct debugnet_saved_zones { 4097790c8c1SConrad Meyer uma_zone_t dsz_mbuf; 4107790c8c1SConrad Meyer uma_zone_t dsz_clust; 4117790c8c1SConrad Meyer uma_zone_t dsz_pack; 4127790c8c1SConrad Meyer uma_zone_t dsz_jumbop; 4137790c8c1SConrad Meyer uma_zone_t dsz_jumbo9; 4147790c8c1SConrad Meyer uma_zone_t dsz_jumbo16; 4157790c8c1SConrad Meyer bool dsz_debugnet_zones_enabled; 4167790c8c1SConrad Meyer } dn_saved_zones; 4175475ca5aSMark Johnston 4185475ca5aSMark Johnston static int 4197790c8c1SConrad Meyer dn_buf_import(void *arg, void **store, int count, int domain __unused, 4205475ca5aSMark Johnston int flags) 4215475ca5aSMark Johnston { 4225475ca5aSMark Johnston struct mbufq *q; 4235475ca5aSMark Johnston struct mbuf *m; 4245475ca5aSMark Johnston int i; 4255475ca5aSMark Johnston 4265475ca5aSMark Johnston q = arg; 4275475ca5aSMark Johnston 4285475ca5aSMark Johnston for (i = 0; i < count; i++) { 4295475ca5aSMark Johnston m = mbufq_dequeue(q); 4305475ca5aSMark Johnston if (m == NULL) 4315475ca5aSMark Johnston break; 4327790c8c1SConrad Meyer trash_init(m, q == &dn_mbufq ? MSIZE : dn_clsize, flags); 4335475ca5aSMark Johnston store[i] = m; 4345475ca5aSMark Johnston } 4350d1467b1SConrad Meyer KASSERT((flags & M_WAITOK) == 0 || i == count, 4360d1467b1SConrad Meyer ("%s: ran out of pre-allocated mbufs", __func__)); 4375475ca5aSMark Johnston return (i); 4385475ca5aSMark Johnston } 4395475ca5aSMark Johnston 4405475ca5aSMark Johnston static void 4417790c8c1SConrad Meyer dn_buf_release(void *arg, void **store, int count) 4425475ca5aSMark Johnston { 4435475ca5aSMark Johnston struct mbufq *q; 4445475ca5aSMark Johnston struct mbuf *m; 4455475ca5aSMark Johnston int i; 4465475ca5aSMark Johnston 4475475ca5aSMark Johnston q = arg; 4485475ca5aSMark Johnston 4495475ca5aSMark Johnston for (i = 0; i < count; i++) { 4505475ca5aSMark Johnston m = store[i]; 4515475ca5aSMark Johnston (void)mbufq_enqueue(q, m); 4525475ca5aSMark Johnston } 4535475ca5aSMark Johnston } 4545475ca5aSMark Johnston 4555475ca5aSMark Johnston static int 4567790c8c1SConrad Meyer dn_pack_import(void *arg __unused, void **store, int count, int domain __unused, 4575475ca5aSMark Johnston int flags __unused) 4585475ca5aSMark Johnston { 4595475ca5aSMark Johnston struct mbuf *m; 4605475ca5aSMark Johnston void *clust; 4615475ca5aSMark Johnston int i; 4625475ca5aSMark Johnston 4635475ca5aSMark Johnston for (i = 0; i < count; i++) { 4645475ca5aSMark Johnston m = m_get(MT_DATA, M_NOWAIT); 4655475ca5aSMark Johnston if (m == NULL) 4665475ca5aSMark Johnston break; 4677790c8c1SConrad Meyer clust = uma_zalloc(dn_zone_clust, M_NOWAIT); 4685475ca5aSMark Johnston if (clust == NULL) { 4695475ca5aSMark Johnston m_free(m); 4705475ca5aSMark Johnston break; 4715475ca5aSMark Johnston } 4727790c8c1SConrad Meyer mb_ctor_clust(clust, dn_clsize, m, 0); 4735475ca5aSMark Johnston store[i] = m; 4745475ca5aSMark Johnston } 4750d1467b1SConrad Meyer KASSERT((flags & M_WAITOK) == 0 || i == count, 4760d1467b1SConrad Meyer ("%s: ran out of pre-allocated mbufs", __func__)); 4775475ca5aSMark Johnston return (i); 4785475ca5aSMark Johnston } 4795475ca5aSMark Johnston 4805475ca5aSMark Johnston static void 4817790c8c1SConrad Meyer dn_pack_release(void *arg __unused, void **store, int count) 4825475ca5aSMark Johnston { 4835475ca5aSMark Johnston struct mbuf *m; 4845475ca5aSMark Johnston void *clust; 4855475ca5aSMark Johnston int i; 4865475ca5aSMark Johnston 4875475ca5aSMark Johnston for (i = 0; i < count; i++) { 4885475ca5aSMark Johnston m = store[i]; 4895475ca5aSMark Johnston clust = m->m_ext.ext_buf; 4907790c8c1SConrad Meyer uma_zfree(dn_zone_clust, clust); 4917790c8c1SConrad Meyer uma_zfree(dn_zone_mbuf, m); 4925475ca5aSMark Johnston } 4935475ca5aSMark Johnston } 4945475ca5aSMark Johnston 4955475ca5aSMark Johnston /* 4967790c8c1SConrad Meyer * Free the pre-allocated mbufs and clusters reserved for debugnet, and destroy 4975475ca5aSMark Johnston * the corresponding UMA cache zones. 4985475ca5aSMark Johnston */ 4995475ca5aSMark Johnston void 5007790c8c1SConrad Meyer debugnet_mbuf_drain(void) 5015475ca5aSMark Johnston { 5025475ca5aSMark Johnston struct mbuf *m; 5035475ca5aSMark Johnston void *item; 5045475ca5aSMark Johnston 5057790c8c1SConrad Meyer if (dn_zone_mbuf != NULL) { 5067790c8c1SConrad Meyer uma_zdestroy(dn_zone_mbuf); 5077790c8c1SConrad Meyer dn_zone_mbuf = NULL; 5085475ca5aSMark Johnston } 5097790c8c1SConrad Meyer if (dn_zone_clust != NULL) { 5107790c8c1SConrad Meyer uma_zdestroy(dn_zone_clust); 5117790c8c1SConrad Meyer dn_zone_clust = NULL; 5125475ca5aSMark Johnston } 5137790c8c1SConrad Meyer if (dn_zone_pack != NULL) { 5147790c8c1SConrad Meyer uma_zdestroy(dn_zone_pack); 5157790c8c1SConrad Meyer dn_zone_pack = NULL; 5165475ca5aSMark Johnston } 5175475ca5aSMark Johnston 5187790c8c1SConrad Meyer while ((m = mbufq_dequeue(&dn_mbufq)) != NULL) 5195475ca5aSMark Johnston m_free(m); 5207790c8c1SConrad Meyer while ((item = mbufq_dequeue(&dn_clustq)) != NULL) 5217790c8c1SConrad Meyer uma_zfree(m_getzone(dn_clsize), item); 5225475ca5aSMark Johnston } 5235475ca5aSMark Johnston 5245475ca5aSMark Johnston /* 5257790c8c1SConrad Meyer * Callback invoked immediately prior to starting a debugnet connection. 5265475ca5aSMark Johnston */ 5275475ca5aSMark Johnston void 5287790c8c1SConrad Meyer debugnet_mbuf_start(void) 5295475ca5aSMark Johnston { 5305475ca5aSMark Johnston 5317790c8c1SConrad Meyer MPASS(!dn_saved_zones.dsz_debugnet_zones_enabled); 5327790c8c1SConrad Meyer 5337790c8c1SConrad Meyer /* Save the old zone pointers to restore when debugnet is closed. */ 5347790c8c1SConrad Meyer dn_saved_zones = (struct debugnet_saved_zones) { 5357790c8c1SConrad Meyer .dsz_debugnet_zones_enabled = true, 5367790c8c1SConrad Meyer .dsz_mbuf = zone_mbuf, 5377790c8c1SConrad Meyer .dsz_clust = zone_clust, 5387790c8c1SConrad Meyer .dsz_pack = zone_pack, 5397790c8c1SConrad Meyer .dsz_jumbop = zone_jumbop, 5407790c8c1SConrad Meyer .dsz_jumbo9 = zone_jumbo9, 5417790c8c1SConrad Meyer .dsz_jumbo16 = zone_jumbo16, 5427790c8c1SConrad Meyer }; 5437790c8c1SConrad Meyer 5445475ca5aSMark Johnston /* 5455475ca5aSMark Johnston * All cluster zones return buffers of the size requested by the 5465475ca5aSMark Johnston * drivers. It's up to the driver to reinitialize the zones if the 5477790c8c1SConrad Meyer * MTU of a debugnet-enabled interface changes. 5485475ca5aSMark Johnston */ 5497790c8c1SConrad Meyer printf("debugnet: overwriting mbuf zone pointers\n"); 5507790c8c1SConrad Meyer zone_mbuf = dn_zone_mbuf; 5517790c8c1SConrad Meyer zone_clust = dn_zone_clust; 5527790c8c1SConrad Meyer zone_pack = dn_zone_pack; 5537790c8c1SConrad Meyer zone_jumbop = dn_zone_clust; 5547790c8c1SConrad Meyer zone_jumbo9 = dn_zone_clust; 5557790c8c1SConrad Meyer zone_jumbo16 = dn_zone_clust; 5565475ca5aSMark Johnston } 5575475ca5aSMark Johnston 5585475ca5aSMark Johnston /* 5597790c8c1SConrad Meyer * Callback invoked when a debugnet connection is closed/finished. 5605475ca5aSMark Johnston */ 5615475ca5aSMark Johnston void 5627790c8c1SConrad Meyer debugnet_mbuf_finish(void) 5637790c8c1SConrad Meyer { 5647790c8c1SConrad Meyer 5657790c8c1SConrad Meyer MPASS(dn_saved_zones.dsz_debugnet_zones_enabled); 5667790c8c1SConrad Meyer 5677790c8c1SConrad Meyer printf("debugnet: restoring mbuf zone pointers\n"); 5687790c8c1SConrad Meyer zone_mbuf = dn_saved_zones.dsz_mbuf; 5697790c8c1SConrad Meyer zone_clust = dn_saved_zones.dsz_clust; 5707790c8c1SConrad Meyer zone_pack = dn_saved_zones.dsz_pack; 5717790c8c1SConrad Meyer zone_jumbop = dn_saved_zones.dsz_jumbop; 5727790c8c1SConrad Meyer zone_jumbo9 = dn_saved_zones.dsz_jumbo9; 5737790c8c1SConrad Meyer zone_jumbo16 = dn_saved_zones.dsz_jumbo16; 5747790c8c1SConrad Meyer 5757790c8c1SConrad Meyer memset(&dn_saved_zones, 0, sizeof(dn_saved_zones)); 5767790c8c1SConrad Meyer } 5777790c8c1SConrad Meyer 5787790c8c1SConrad Meyer /* 5797790c8c1SConrad Meyer * Reinitialize the debugnet mbuf+cluster pool and cache zones. 5807790c8c1SConrad Meyer */ 5817790c8c1SConrad Meyer void 5827790c8c1SConrad Meyer debugnet_mbuf_reinit(int nmbuf, int nclust, int clsize) 5835475ca5aSMark Johnston { 5845475ca5aSMark Johnston struct mbuf *m; 5855475ca5aSMark Johnston void *item; 5865475ca5aSMark Johnston 5877790c8c1SConrad Meyer debugnet_mbuf_drain(); 5885475ca5aSMark Johnston 5897790c8c1SConrad Meyer dn_clsize = clsize; 5905475ca5aSMark Johnston 5917790c8c1SConrad Meyer dn_zone_mbuf = uma_zcache_create("debugnet_" MBUF_MEM_NAME, 592*30be9685SRyan Libby MSIZE, mb_ctor_mbuf, mb_dtor_mbuf, NULL, NULL, 5937790c8c1SConrad Meyer dn_buf_import, dn_buf_release, 5947790c8c1SConrad Meyer &dn_mbufq, UMA_ZONE_NOBUCKET); 5955475ca5aSMark Johnston 5967790c8c1SConrad Meyer dn_zone_clust = uma_zcache_create("debugnet_" MBUF_CLUSTER_MEM_NAME, 597*30be9685SRyan Libby clsize, mb_ctor_clust, NULL, NULL, NULL, 5987790c8c1SConrad Meyer dn_buf_import, dn_buf_release, 5997790c8c1SConrad Meyer &dn_clustq, UMA_ZONE_NOBUCKET); 6005475ca5aSMark Johnston 6017790c8c1SConrad Meyer dn_zone_pack = uma_zcache_create("debugnet_" MBUF_PACKET_MEM_NAME, 6025475ca5aSMark Johnston MCLBYTES, mb_ctor_pack, mb_dtor_pack, NULL, NULL, 6037790c8c1SConrad Meyer dn_pack_import, dn_pack_release, 6045475ca5aSMark Johnston NULL, UMA_ZONE_NOBUCKET); 6055475ca5aSMark Johnston 6065475ca5aSMark Johnston while (nmbuf-- > 0) { 6075475ca5aSMark Johnston m = m_get(MT_DATA, M_WAITOK); 6087790c8c1SConrad Meyer uma_zfree(dn_zone_mbuf, m); 6095475ca5aSMark Johnston } 6105475ca5aSMark Johnston while (nclust-- > 0) { 6117790c8c1SConrad Meyer item = uma_zalloc(m_getzone(dn_clsize), M_WAITOK); 6127790c8c1SConrad Meyer uma_zfree(dn_zone_clust, item); 6135475ca5aSMark Johnston } 6145475ca5aSMark Johnston } 6157790c8c1SConrad Meyer #endif /* DEBUGNET */ 6165475ca5aSMark Johnston 617099a0e58SBosko Milekic /* 618ba63339aSAlan Cox * UMA backend page allocator for the jumbo frame zones. 619ba63339aSAlan Cox * 620ba63339aSAlan Cox * Allocates kernel virtual memory that is backed by contiguous physical 621ba63339aSAlan Cox * pages. 622ba63339aSAlan Cox */ 623ba63339aSAlan Cox static void * 624ab3185d1SJeff Roberson mbuf_jumbo_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags, 625ab3185d1SJeff Roberson int wait) 626ba63339aSAlan Cox { 627ba63339aSAlan Cox 6287630c265SAlan Cox /* Inform UMA that this allocator uses kernel_map/object. */ 6297630c265SAlan Cox *flags = UMA_SLAB_KERNEL; 6309978bd99SMark Johnston return ((void *)kmem_alloc_contig_domainset(DOMAINSET_FIXED(domain), 6319978bd99SMark Johnston bytes, wait, (vm_paddr_t)0, ~(vm_paddr_t)0, 1, 0, 6329978bd99SMark Johnston VM_MEMATTR_DEFAULT)); 633ba63339aSAlan Cox } 634ba63339aSAlan Cox 635ba63339aSAlan Cox /* 636099a0e58SBosko Milekic * Constructor for Mbuf master zone. 637099a0e58SBosko Milekic * 638099a0e58SBosko Milekic * The 'arg' pointer points to a mb_args structure which 639099a0e58SBosko Milekic * contains call-specific information required to support the 64056a4e45aSAndre Oppermann * mbuf allocation API. See mbuf.h. 641099a0e58SBosko Milekic */ 642b23f72e9SBrian Feldman static int 643b23f72e9SBrian Feldman mb_ctor_mbuf(void *mem, int size, void *arg, int how) 644099a0e58SBosko Milekic { 645099a0e58SBosko Milekic struct mbuf *m; 646099a0e58SBosko Milekic struct mb_args *args; 647b23f72e9SBrian Feldman int error; 648099a0e58SBosko Milekic int flags; 649099a0e58SBosko Milekic short type; 650099a0e58SBosko Milekic 651099a0e58SBosko Milekic args = (struct mb_args *)arg; 652099a0e58SBosko Milekic type = args->type; 653099a0e58SBosko Milekic 65456a4e45aSAndre Oppermann /* 65556a4e45aSAndre Oppermann * The mbuf is initialized later. The caller has the 656fcf90618SGleb Smirnoff * responsibility to set up any MAC labels too. 65756a4e45aSAndre Oppermann */ 65856a4e45aSAndre Oppermann if (type == MT_NOINIT) 65956a4e45aSAndre Oppermann return (0); 66056a4e45aSAndre Oppermann 661afb295ccSAndre Oppermann m = (struct mbuf *)mem; 662afb295ccSAndre Oppermann flags = args->flags; 663fddd4f62SNavdeep Parhar MPASS((flags & M_NOFREE) == 0); 664afb295ccSAndre Oppermann 665b4b12e52SGleb Smirnoff error = m_init(m, how, type, flags); 666afb295ccSAndre Oppermann 667b23f72e9SBrian Feldman return (error); 668099a0e58SBosko Milekic } 669099a0e58SBosko Milekic 670099a0e58SBosko Milekic /* 67156a4e45aSAndre Oppermann * The Mbuf master zone destructor. 672099a0e58SBosko Milekic */ 673099a0e58SBosko Milekic static void 674099a0e58SBosko Milekic mb_dtor_mbuf(void *mem, int size, void *arg) 675099a0e58SBosko Milekic { 676099a0e58SBosko Milekic struct mbuf *m; 677629b9e08SKip Macy unsigned long flags; 678099a0e58SBosko Milekic 679099a0e58SBosko Milekic m = (struct mbuf *)mem; 680629b9e08SKip Macy flags = (unsigned long)arg; 681629b9e08SKip Macy 682a9fa76f2SNavdeep Parhar KASSERT((m->m_flags & M_NOFREE) == 0, ("%s: M_NOFREE set", __func__)); 6834c7070dbSScott Long if (!(flags & MB_DTOR_SKIP) && (m->m_flags & M_PKTHDR) && !SLIST_EMPTY(&m->m_pkthdr.tags)) 684099a0e58SBosko Milekic m_tag_delete_chain(m, NULL); 685099a0e58SBosko Milekic } 686099a0e58SBosko Milekic 68756a4e45aSAndre Oppermann /* 68856a4e45aSAndre Oppermann * The Mbuf Packet zone destructor. 68956a4e45aSAndre Oppermann */ 690099a0e58SBosko Milekic static void 691099a0e58SBosko Milekic mb_dtor_pack(void *mem, int size, void *arg) 692099a0e58SBosko Milekic { 693099a0e58SBosko Milekic struct mbuf *m; 694099a0e58SBosko Milekic 695099a0e58SBosko Milekic m = (struct mbuf *)mem; 696099a0e58SBosko Milekic if ((m->m_flags & M_PKTHDR) != 0) 697099a0e58SBosko Milekic m_tag_delete_chain(m, NULL); 69856a4e45aSAndre Oppermann 69956a4e45aSAndre Oppermann /* Make sure we've got a clean cluster back. */ 70056a4e45aSAndre Oppermann KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__)); 70156a4e45aSAndre Oppermann KASSERT(m->m_ext.ext_buf != NULL, ("%s: ext_buf == NULL", __func__)); 70256a4e45aSAndre Oppermann KASSERT(m->m_ext.ext_free == NULL, ("%s: ext_free != NULL", __func__)); 703cf827063SPoul-Henning Kamp KASSERT(m->m_ext.ext_arg1 == NULL, ("%s: ext_arg1 != NULL", __func__)); 704cf827063SPoul-Henning Kamp KASSERT(m->m_ext.ext_arg2 == NULL, ("%s: ext_arg2 != NULL", __func__)); 70556a4e45aSAndre Oppermann KASSERT(m->m_ext.ext_size == MCLBYTES, ("%s: ext_size != MCLBYTES", __func__)); 70649d46b61SGleb Smirnoff KASSERT(m->m_ext.ext_type == EXT_PACKET, ("%s: ext_type != EXT_PACKET", __func__)); 707121f0509SMike Silbersack #ifdef INVARIANTS 708121f0509SMike Silbersack trash_dtor(m->m_ext.ext_buf, MCLBYTES, arg); 709121f0509SMike Silbersack #endif 7106c125b8dSMohan Srinivasan /* 711ef44c8d2SDavid E. O'Brien * If there are processes blocked on zone_clust, waiting for pages 71208cfa56eSMark Johnston * to be freed up, cause them to be woken up by draining the 71308cfa56eSMark Johnston * packet zone. We are exposed to a race here (in the check for 714ef44c8d2SDavid E. O'Brien * the UMA_ZFLAG_FULL) where we might miss the flag set, but that 715ef44c8d2SDavid E. O'Brien * is deliberate. We don't want to acquire the zone lock for every 716ef44c8d2SDavid E. O'Brien * mbuf free. 7176c125b8dSMohan Srinivasan */ 7186c125b8dSMohan Srinivasan if (uma_zone_exhausted_nolock(zone_clust)) 71908cfa56eSMark Johnston uma_zone_reclaim(zone_pack, UMA_RECLAIM_DRAIN); 720099a0e58SBosko Milekic } 721099a0e58SBosko Milekic 722099a0e58SBosko Milekic /* 723ec63cb90SAndre Oppermann * The Cluster and Jumbo[PAGESIZE|9|16] zone constructor. 724099a0e58SBosko Milekic * 725099a0e58SBosko Milekic * Here the 'arg' pointer points to the Mbuf which we 72656a4e45aSAndre Oppermann * are configuring cluster storage for. If 'arg' is 72756a4e45aSAndre Oppermann * empty we allocate just the cluster without setting 72856a4e45aSAndre Oppermann * the mbuf to it. See mbuf.h. 729099a0e58SBosko Milekic */ 730b23f72e9SBrian Feldman static int 731b23f72e9SBrian Feldman mb_ctor_clust(void *mem, int size, void *arg, int how) 732099a0e58SBosko Milekic { 733099a0e58SBosko Milekic struct mbuf *m; 734099a0e58SBosko Milekic 7350f4d9d04SKip Macy m = (struct mbuf *)arg; 7360f4d9d04SKip Macy if (m != NULL) { 737e8fd18f3SGleb Smirnoff m->m_ext.ext_buf = (char *)mem; 738099a0e58SBosko Milekic m->m_data = m->m_ext.ext_buf; 739099a0e58SBosko Milekic m->m_flags |= M_EXT; 740099a0e58SBosko Milekic m->m_ext.ext_free = NULL; 741cf827063SPoul-Henning Kamp m->m_ext.ext_arg1 = NULL; 742cf827063SPoul-Henning Kamp m->m_ext.ext_arg2 = NULL; 74356a4e45aSAndre Oppermann m->m_ext.ext_size = size; 74456a5f52eSGleb Smirnoff m->m_ext.ext_type = m_gettype(size); 74556a5f52eSGleb Smirnoff m->m_ext.ext_flags = EXT_FLAG_EMBREF; 74656a5f52eSGleb Smirnoff m->m_ext.ext_count = 1; 74756a4e45aSAndre Oppermann } 7480f4d9d04SKip Macy 749b23f72e9SBrian Feldman return (0); 750099a0e58SBosko Milekic } 751099a0e58SBosko Milekic 75256a4e45aSAndre Oppermann /* 753099a0e58SBosko Milekic * The Packet secondary zone's init routine, executed on the 75456a4e45aSAndre Oppermann * object's transition from mbuf keg slab to zone cache. 755099a0e58SBosko Milekic */ 756b23f72e9SBrian Feldman static int 75756a4e45aSAndre Oppermann mb_zinit_pack(void *mem, int size, int how) 758099a0e58SBosko Milekic { 759099a0e58SBosko Milekic struct mbuf *m; 760099a0e58SBosko Milekic 76156a4e45aSAndre Oppermann m = (struct mbuf *)mem; /* m is virgin. */ 762a7bd90efSAndre Oppermann if (uma_zalloc_arg(zone_clust, m, how) == NULL || 763a7bd90efSAndre Oppermann m->m_ext.ext_buf == NULL) 764b23f72e9SBrian Feldman return (ENOMEM); 765cd5bb63bSAndre Oppermann m->m_ext.ext_type = EXT_PACKET; /* Override. */ 766121f0509SMike Silbersack #ifdef INVARIANTS 767121f0509SMike Silbersack trash_init(m->m_ext.ext_buf, MCLBYTES, how); 768121f0509SMike Silbersack #endif 769b23f72e9SBrian Feldman return (0); 770099a0e58SBosko Milekic } 771099a0e58SBosko Milekic 772099a0e58SBosko Milekic /* 773099a0e58SBosko Milekic * The Packet secondary zone's fini routine, executed on the 774099a0e58SBosko Milekic * object's transition from zone cache to keg slab. 775099a0e58SBosko Milekic */ 776099a0e58SBosko Milekic static void 77756a4e45aSAndre Oppermann mb_zfini_pack(void *mem, int size) 778099a0e58SBosko Milekic { 779099a0e58SBosko Milekic struct mbuf *m; 780099a0e58SBosko Milekic 781099a0e58SBosko Milekic m = (struct mbuf *)mem; 782121f0509SMike Silbersack #ifdef INVARIANTS 783121f0509SMike Silbersack trash_fini(m->m_ext.ext_buf, MCLBYTES); 784121f0509SMike Silbersack #endif 785099a0e58SBosko Milekic uma_zfree_arg(zone_clust, m->m_ext.ext_buf, NULL); 786a7b844d2SMike Silbersack #ifdef INVARIANTS 787a7b844d2SMike Silbersack trash_dtor(mem, size, NULL); 788a7b844d2SMike Silbersack #endif 789099a0e58SBosko Milekic } 790099a0e58SBosko Milekic 791099a0e58SBosko Milekic /* 792099a0e58SBosko Milekic * The "packet" keg constructor. 793099a0e58SBosko Milekic */ 794b23f72e9SBrian Feldman static int 795b23f72e9SBrian Feldman mb_ctor_pack(void *mem, int size, void *arg, int how) 796099a0e58SBosko Milekic { 797099a0e58SBosko Milekic struct mbuf *m; 798099a0e58SBosko Milekic struct mb_args *args; 799ce28636bSAndre Oppermann int error, flags; 800099a0e58SBosko Milekic short type; 801099a0e58SBosko Milekic 802099a0e58SBosko Milekic m = (struct mbuf *)mem; 803099a0e58SBosko Milekic args = (struct mb_args *)arg; 804099a0e58SBosko Milekic flags = args->flags; 805099a0e58SBosko Milekic type = args->type; 806fddd4f62SNavdeep Parhar MPASS((flags & M_NOFREE) == 0); 807099a0e58SBosko Milekic 808121f0509SMike Silbersack #ifdef INVARIANTS 809121f0509SMike Silbersack trash_ctor(m->m_ext.ext_buf, MCLBYTES, arg, how); 810121f0509SMike Silbersack #endif 811099a0e58SBosko Milekic 812b4b12e52SGleb Smirnoff error = m_init(m, how, type, flags); 813afb295ccSAndre Oppermann 81456a4e45aSAndre Oppermann /* m_ext is already initialized. */ 815afb295ccSAndre Oppermann m->m_data = m->m_ext.ext_buf; 816afb295ccSAndre Oppermann m->m_flags = (flags | M_EXT); 81756a4e45aSAndre Oppermann 818afb295ccSAndre Oppermann return (error); 819099a0e58SBosko Milekic } 820099a0e58SBosko Milekic 821099a0e58SBosko Milekic /* 822e60b2fcbSGleb Smirnoff * This is the protocol drain routine. Called by UMA whenever any of the 823e60b2fcbSGleb Smirnoff * mbuf zones is closed to its limit. 824099a0e58SBosko Milekic * 825099a0e58SBosko Milekic * No locks should be held when this is called. The drain routines have to 826099a0e58SBosko Milekic * presently acquire some locks which raises the possibility of lock order 827099a0e58SBosko Milekic * reversal. 828099a0e58SBosko Milekic */ 829099a0e58SBosko Milekic static void 830e60b2fcbSGleb Smirnoff mb_reclaim(uma_zone_t zone __unused, int pending __unused) 831099a0e58SBosko Milekic { 832099a0e58SBosko Milekic struct domain *dp; 833099a0e58SBosko Milekic struct protosw *pr; 834099a0e58SBosko Milekic 835e60b2fcbSGleb Smirnoff WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK | WARN_PANIC, NULL, __func__); 836099a0e58SBosko Milekic 837099a0e58SBosko Milekic for (dp = domains; dp != NULL; dp = dp->dom_next) 838099a0e58SBosko Milekic for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 839099a0e58SBosko Milekic if (pr->pr_drain != NULL) 840099a0e58SBosko Milekic (*pr->pr_drain)(); 841099a0e58SBosko Milekic } 8425e4bc63bSGleb Smirnoff 8435e4bc63bSGleb Smirnoff /* 84482334850SJohn Baldwin * Free "count" units of I/O from an mbuf chain. They could be held 84582334850SJohn Baldwin * in EXT_PGS or just as a normal mbuf. This code is intended to be 84682334850SJohn Baldwin * called in an error path (I/O error, closed connection, etc). 84782334850SJohn Baldwin */ 84882334850SJohn Baldwin void 84982334850SJohn Baldwin mb_free_notready(struct mbuf *m, int count) 85082334850SJohn Baldwin { 85182334850SJohn Baldwin int i; 85282334850SJohn Baldwin 85382334850SJohn Baldwin for (i = 0; i < count && m != NULL; i++) { 85482334850SJohn Baldwin if ((m->m_flags & M_EXT) != 0 && 85582334850SJohn Baldwin m->m_ext.ext_type == EXT_PGS) { 85682334850SJohn Baldwin m->m_ext.ext_pgs->nrdy--; 85782334850SJohn Baldwin if (m->m_ext.ext_pgs->nrdy != 0) 85882334850SJohn Baldwin continue; 85982334850SJohn Baldwin } 86082334850SJohn Baldwin m = m_free(m); 86182334850SJohn Baldwin } 86282334850SJohn Baldwin KASSERT(i == count, ("Removed only %d items from %p", i, m)); 86382334850SJohn Baldwin } 86482334850SJohn Baldwin 86582334850SJohn Baldwin /* 86682334850SJohn Baldwin * Compress an unmapped mbuf into a simple mbuf when it holds a small 86782334850SJohn Baldwin * amount of data. This is used as a DOS defense to avoid having 86882334850SJohn Baldwin * small packets tie up wired pages, an ext_pgs structure, and an 86982334850SJohn Baldwin * mbuf. Since this converts the existing mbuf in place, it can only 87082334850SJohn Baldwin * be used if there are no other references to 'm'. 87182334850SJohn Baldwin */ 87282334850SJohn Baldwin int 87382334850SJohn Baldwin mb_unmapped_compress(struct mbuf *m) 87482334850SJohn Baldwin { 87582334850SJohn Baldwin volatile u_int *refcnt; 87682334850SJohn Baldwin struct mbuf m_temp; 87782334850SJohn Baldwin 87882334850SJohn Baldwin /* 87982334850SJohn Baldwin * Assert that 'm' does not have a packet header. If 'm' had 88082334850SJohn Baldwin * a packet header, it would only be able to hold MHLEN bytes 88182334850SJohn Baldwin * and m_data would have to be initialized differently. 88282334850SJohn Baldwin */ 88382334850SJohn Baldwin KASSERT((m->m_flags & M_PKTHDR) == 0 && (m->m_flags & M_EXT) && 88482334850SJohn Baldwin m->m_ext.ext_type == EXT_PGS, 88582334850SJohn Baldwin ("%s: m %p !M_EXT or !EXT_PGS or M_PKTHDR", __func__, m)); 88682334850SJohn Baldwin KASSERT(m->m_len <= MLEN, ("m_len too large %p", m)); 88782334850SJohn Baldwin 88882334850SJohn Baldwin if (m->m_ext.ext_flags & EXT_FLAG_EMBREF) { 88982334850SJohn Baldwin refcnt = &m->m_ext.ext_count; 89082334850SJohn Baldwin } else { 89182334850SJohn Baldwin KASSERT(m->m_ext.ext_cnt != NULL, 89282334850SJohn Baldwin ("%s: no refcounting pointer on %p", __func__, m)); 89382334850SJohn Baldwin refcnt = m->m_ext.ext_cnt; 89482334850SJohn Baldwin } 89582334850SJohn Baldwin 89682334850SJohn Baldwin if (*refcnt != 1) 89782334850SJohn Baldwin return (EBUSY); 89882334850SJohn Baldwin 89982334850SJohn Baldwin /* 90082334850SJohn Baldwin * Copy mbuf header and m_ext portion of 'm' to 'm_temp' to 90182334850SJohn Baldwin * create a "fake" EXT_PGS mbuf that can be used with 90282334850SJohn Baldwin * m_copydata() as well as the ext_free callback. 90382334850SJohn Baldwin */ 90482334850SJohn Baldwin memcpy(&m_temp, m, offsetof(struct mbuf, m_ext) + sizeof (m->m_ext)); 90582334850SJohn Baldwin m_temp.m_next = NULL; 90682334850SJohn Baldwin m_temp.m_nextpkt = NULL; 90782334850SJohn Baldwin 90882334850SJohn Baldwin /* Turn 'm' into a "normal" mbuf. */ 90982334850SJohn Baldwin m->m_flags &= ~(M_EXT | M_RDONLY | M_NOMAP); 91082334850SJohn Baldwin m->m_data = m->m_dat; 91182334850SJohn Baldwin 91282334850SJohn Baldwin /* Copy data from template's ext_pgs. */ 91382334850SJohn Baldwin m_copydata(&m_temp, 0, m_temp.m_len, mtod(m, caddr_t)); 91482334850SJohn Baldwin 91582334850SJohn Baldwin /* Free the backing pages. */ 91682334850SJohn Baldwin m_temp.m_ext.ext_free(&m_temp); 91782334850SJohn Baldwin 91882334850SJohn Baldwin /* Finally, free the ext_pgs struct. */ 91982334850SJohn Baldwin uma_zfree(zone_extpgs, m_temp.m_ext.ext_pgs); 92082334850SJohn Baldwin return (0); 92182334850SJohn Baldwin } 92282334850SJohn Baldwin 92382334850SJohn Baldwin /* 92482334850SJohn Baldwin * These next few routines are used to permit downgrading an unmapped 92582334850SJohn Baldwin * mbuf to a chain of mapped mbufs. This is used when an interface 92682334850SJohn Baldwin * doesn't supported unmapped mbufs or if checksums need to be 92782334850SJohn Baldwin * computed in software. 92882334850SJohn Baldwin * 92982334850SJohn Baldwin * Each unmapped mbuf is converted to a chain of mbufs. First, any 93082334850SJohn Baldwin * TLS header data is stored in a regular mbuf. Second, each page of 93182334850SJohn Baldwin * unmapped data is stored in an mbuf with an EXT_SFBUF external 93282334850SJohn Baldwin * cluster. These mbufs use an sf_buf to provide a valid KVA for the 93382334850SJohn Baldwin * associated physical page. They also hold a reference on the 93482334850SJohn Baldwin * original EXT_PGS mbuf to ensure the physical page doesn't go away. 93582334850SJohn Baldwin * Finally, any TLS trailer data is stored in a regular mbuf. 93682334850SJohn Baldwin * 93782334850SJohn Baldwin * mb_unmapped_free_mext() is the ext_free handler for the EXT_SFBUF 93882334850SJohn Baldwin * mbufs. It frees the associated sf_buf and releases its reference 93982334850SJohn Baldwin * on the original EXT_PGS mbuf. 94082334850SJohn Baldwin * 94182334850SJohn Baldwin * _mb_unmapped_to_ext() is a helper function that converts a single 94282334850SJohn Baldwin * unmapped mbuf into a chain of mbufs. 94382334850SJohn Baldwin * 94482334850SJohn Baldwin * mb_unmapped_to_ext() is the public function that walks an mbuf 94582334850SJohn Baldwin * chain converting any unmapped mbufs to mapped mbufs. It returns 94682334850SJohn Baldwin * the new chain of unmapped mbufs on success. On failure it frees 94782334850SJohn Baldwin * the original mbuf chain and returns NULL. 94882334850SJohn Baldwin */ 94982334850SJohn Baldwin static void 95082334850SJohn Baldwin mb_unmapped_free_mext(struct mbuf *m) 95182334850SJohn Baldwin { 95282334850SJohn Baldwin struct sf_buf *sf; 95382334850SJohn Baldwin struct mbuf *old_m; 95482334850SJohn Baldwin 95582334850SJohn Baldwin sf = m->m_ext.ext_arg1; 95682334850SJohn Baldwin sf_buf_free(sf); 95782334850SJohn Baldwin 95882334850SJohn Baldwin /* Drop the reference on the backing EXT_PGS mbuf. */ 95982334850SJohn Baldwin old_m = m->m_ext.ext_arg2; 96082334850SJohn Baldwin mb_free_ext(old_m); 96182334850SJohn Baldwin } 96282334850SJohn Baldwin 96382334850SJohn Baldwin static struct mbuf * 96482334850SJohn Baldwin _mb_unmapped_to_ext(struct mbuf *m) 96582334850SJohn Baldwin { 96682334850SJohn Baldwin struct mbuf_ext_pgs *ext_pgs; 96782334850SJohn Baldwin struct mbuf *m_new, *top, *prev, *mref; 96882334850SJohn Baldwin struct sf_buf *sf; 96982334850SJohn Baldwin vm_page_t pg; 97082334850SJohn Baldwin int i, len, off, pglen, pgoff, seglen, segoff; 97182334850SJohn Baldwin volatile u_int *refcnt; 97282334850SJohn Baldwin u_int ref_inc = 0; 97382334850SJohn Baldwin 97482334850SJohn Baldwin MBUF_EXT_PGS_ASSERT(m); 97582334850SJohn Baldwin ext_pgs = m->m_ext.ext_pgs; 97682334850SJohn Baldwin len = m->m_len; 97782334850SJohn Baldwin KASSERT(ext_pgs->tls == NULL, ("%s: can't convert TLS mbuf %p", 97882334850SJohn Baldwin __func__, m)); 97982334850SJohn Baldwin 98082334850SJohn Baldwin /* See if this is the mbuf that holds the embedded refcount. */ 98182334850SJohn Baldwin if (m->m_ext.ext_flags & EXT_FLAG_EMBREF) { 98282334850SJohn Baldwin refcnt = &m->m_ext.ext_count; 98382334850SJohn Baldwin mref = m; 98482334850SJohn Baldwin } else { 98582334850SJohn Baldwin KASSERT(m->m_ext.ext_cnt != NULL, 98682334850SJohn Baldwin ("%s: no refcounting pointer on %p", __func__, m)); 98782334850SJohn Baldwin refcnt = m->m_ext.ext_cnt; 98882334850SJohn Baldwin mref = __containerof(refcnt, struct mbuf, m_ext.ext_count); 98982334850SJohn Baldwin } 99082334850SJohn Baldwin 99182334850SJohn Baldwin /* Skip over any data removed from the front. */ 99282334850SJohn Baldwin off = mtod(m, vm_offset_t); 99382334850SJohn Baldwin 99482334850SJohn Baldwin top = NULL; 99582334850SJohn Baldwin if (ext_pgs->hdr_len != 0) { 99682334850SJohn Baldwin if (off >= ext_pgs->hdr_len) { 99782334850SJohn Baldwin off -= ext_pgs->hdr_len; 99882334850SJohn Baldwin } else { 99982334850SJohn Baldwin seglen = ext_pgs->hdr_len - off; 100082334850SJohn Baldwin segoff = off; 100182334850SJohn Baldwin seglen = min(seglen, len); 100282334850SJohn Baldwin off = 0; 100382334850SJohn Baldwin len -= seglen; 100482334850SJohn Baldwin m_new = m_get(M_NOWAIT, MT_DATA); 100582334850SJohn Baldwin if (m_new == NULL) 100682334850SJohn Baldwin goto fail; 100782334850SJohn Baldwin m_new->m_len = seglen; 100882334850SJohn Baldwin prev = top = m_new; 100982334850SJohn Baldwin memcpy(mtod(m_new, void *), &ext_pgs->hdr[segoff], 101082334850SJohn Baldwin seglen); 101182334850SJohn Baldwin } 101282334850SJohn Baldwin } 101382334850SJohn Baldwin pgoff = ext_pgs->first_pg_off; 101482334850SJohn Baldwin for (i = 0; i < ext_pgs->npgs && len > 0; i++) { 101582334850SJohn Baldwin pglen = mbuf_ext_pg_len(ext_pgs, i, pgoff); 101682334850SJohn Baldwin if (off >= pglen) { 101782334850SJohn Baldwin off -= pglen; 101882334850SJohn Baldwin pgoff = 0; 101982334850SJohn Baldwin continue; 102082334850SJohn Baldwin } 102182334850SJohn Baldwin seglen = pglen - off; 102282334850SJohn Baldwin segoff = pgoff + off; 102382334850SJohn Baldwin off = 0; 102482334850SJohn Baldwin seglen = min(seglen, len); 102582334850SJohn Baldwin len -= seglen; 102682334850SJohn Baldwin 102782334850SJohn Baldwin pg = PHYS_TO_VM_PAGE(ext_pgs->pa[i]); 102882334850SJohn Baldwin m_new = m_get(M_NOWAIT, MT_DATA); 102982334850SJohn Baldwin if (m_new == NULL) 103082334850SJohn Baldwin goto fail; 103182334850SJohn Baldwin if (top == NULL) { 103282334850SJohn Baldwin top = prev = m_new; 103382334850SJohn Baldwin } else { 103482334850SJohn Baldwin prev->m_next = m_new; 103582334850SJohn Baldwin prev = m_new; 103682334850SJohn Baldwin } 103782334850SJohn Baldwin sf = sf_buf_alloc(pg, SFB_NOWAIT); 103882334850SJohn Baldwin if (sf == NULL) 103982334850SJohn Baldwin goto fail; 104082334850SJohn Baldwin 104182334850SJohn Baldwin ref_inc++; 104282334850SJohn Baldwin m_extadd(m_new, (char *)sf_buf_kva(sf), PAGE_SIZE, 104382334850SJohn Baldwin mb_unmapped_free_mext, sf, mref, M_RDONLY, EXT_SFBUF); 104482334850SJohn Baldwin m_new->m_data += segoff; 104582334850SJohn Baldwin m_new->m_len = seglen; 104682334850SJohn Baldwin 104782334850SJohn Baldwin pgoff = 0; 104882334850SJohn Baldwin }; 104982334850SJohn Baldwin if (len != 0) { 105082334850SJohn Baldwin KASSERT((off + len) <= ext_pgs->trail_len, 105182334850SJohn Baldwin ("off + len > trail (%d + %d > %d)", off, len, 105282334850SJohn Baldwin ext_pgs->trail_len)); 105382334850SJohn Baldwin m_new = m_get(M_NOWAIT, MT_DATA); 105482334850SJohn Baldwin if (m_new == NULL) 105582334850SJohn Baldwin goto fail; 105682334850SJohn Baldwin if (top == NULL) 105782334850SJohn Baldwin top = m_new; 105882334850SJohn Baldwin else 105982334850SJohn Baldwin prev->m_next = m_new; 106082334850SJohn Baldwin m_new->m_len = len; 106182334850SJohn Baldwin memcpy(mtod(m_new, void *), &ext_pgs->trail[off], len); 106282334850SJohn Baldwin } 106382334850SJohn Baldwin 106482334850SJohn Baldwin if (ref_inc != 0) { 106582334850SJohn Baldwin /* 106682334850SJohn Baldwin * Obtain an additional reference on the old mbuf for 106782334850SJohn Baldwin * each created EXT_SFBUF mbuf. They will be dropped 106882334850SJohn Baldwin * in mb_unmapped_free_mext(). 106982334850SJohn Baldwin */ 107082334850SJohn Baldwin if (*refcnt == 1) 107182334850SJohn Baldwin *refcnt += ref_inc; 107282334850SJohn Baldwin else 107382334850SJohn Baldwin atomic_add_int(refcnt, ref_inc); 107482334850SJohn Baldwin } 107582334850SJohn Baldwin m_free(m); 107682334850SJohn Baldwin return (top); 107782334850SJohn Baldwin 107882334850SJohn Baldwin fail: 107982334850SJohn Baldwin if (ref_inc != 0) { 108082334850SJohn Baldwin /* 108182334850SJohn Baldwin * Obtain an additional reference on the old mbuf for 108282334850SJohn Baldwin * each created EXT_SFBUF mbuf. They will be 108382334850SJohn Baldwin * immediately dropped when these mbufs are freed 108482334850SJohn Baldwin * below. 108582334850SJohn Baldwin */ 108682334850SJohn Baldwin if (*refcnt == 1) 108782334850SJohn Baldwin *refcnt += ref_inc; 108882334850SJohn Baldwin else 108982334850SJohn Baldwin atomic_add_int(refcnt, ref_inc); 109082334850SJohn Baldwin } 109182334850SJohn Baldwin m_free(m); 109282334850SJohn Baldwin m_freem(top); 109382334850SJohn Baldwin return (NULL); 109482334850SJohn Baldwin } 109582334850SJohn Baldwin 109682334850SJohn Baldwin struct mbuf * 109782334850SJohn Baldwin mb_unmapped_to_ext(struct mbuf *top) 109882334850SJohn Baldwin { 109982334850SJohn Baldwin struct mbuf *m, *next, *prev = NULL; 110082334850SJohn Baldwin 110182334850SJohn Baldwin prev = NULL; 110282334850SJohn Baldwin for (m = top; m != NULL; m = next) { 110382334850SJohn Baldwin /* m might be freed, so cache the next pointer. */ 110482334850SJohn Baldwin next = m->m_next; 110582334850SJohn Baldwin if (m->m_flags & M_NOMAP) { 110682334850SJohn Baldwin if (prev != NULL) { 110782334850SJohn Baldwin /* 110882334850SJohn Baldwin * Remove 'm' from the new chain so 110982334850SJohn Baldwin * that the 'top' chain terminates 111082334850SJohn Baldwin * before 'm' in case 'top' is freed 111182334850SJohn Baldwin * due to an error. 111282334850SJohn Baldwin */ 111382334850SJohn Baldwin prev->m_next = NULL; 111482334850SJohn Baldwin } 111582334850SJohn Baldwin m = _mb_unmapped_to_ext(m); 111682334850SJohn Baldwin if (m == NULL) { 111782334850SJohn Baldwin m_freem(top); 111882334850SJohn Baldwin m_freem(next); 111982334850SJohn Baldwin return (NULL); 112082334850SJohn Baldwin } 112182334850SJohn Baldwin if (prev == NULL) { 112282334850SJohn Baldwin top = m; 112382334850SJohn Baldwin } else { 112482334850SJohn Baldwin prev->m_next = m; 112582334850SJohn Baldwin } 112682334850SJohn Baldwin 112782334850SJohn Baldwin /* 112882334850SJohn Baldwin * Replaced one mbuf with a chain, so we must 112982334850SJohn Baldwin * find the end of chain. 113082334850SJohn Baldwin */ 113182334850SJohn Baldwin prev = m_last(m); 113282334850SJohn Baldwin } else { 113382334850SJohn Baldwin if (prev != NULL) { 113482334850SJohn Baldwin prev->m_next = m; 113582334850SJohn Baldwin } 113682334850SJohn Baldwin prev = m; 113782334850SJohn Baldwin } 113882334850SJohn Baldwin } 113982334850SJohn Baldwin return (top); 114082334850SJohn Baldwin } 114182334850SJohn Baldwin 114282334850SJohn Baldwin /* 114382334850SJohn Baldwin * Allocate an empty EXT_PGS mbuf. The ext_free routine is 114482334850SJohn Baldwin * responsible for freeing any pages backing this mbuf when it is 114582334850SJohn Baldwin * freed. 114682334850SJohn Baldwin */ 114782334850SJohn Baldwin struct mbuf * 114882334850SJohn Baldwin mb_alloc_ext_pgs(int how, bool pkthdr, m_ext_free_t ext_free) 114982334850SJohn Baldwin { 115082334850SJohn Baldwin struct mbuf *m; 115182334850SJohn Baldwin struct mbuf_ext_pgs *ext_pgs; 115282334850SJohn Baldwin 115382334850SJohn Baldwin if (pkthdr) 115482334850SJohn Baldwin m = m_gethdr(how, MT_DATA); 115582334850SJohn Baldwin else 115682334850SJohn Baldwin m = m_get(how, MT_DATA); 115782334850SJohn Baldwin if (m == NULL) 115882334850SJohn Baldwin return (NULL); 115982334850SJohn Baldwin 116082334850SJohn Baldwin ext_pgs = uma_zalloc(zone_extpgs, how); 116182334850SJohn Baldwin if (ext_pgs == NULL) { 116282334850SJohn Baldwin m_free(m); 116382334850SJohn Baldwin return (NULL); 116482334850SJohn Baldwin } 116582334850SJohn Baldwin ext_pgs->npgs = 0; 116682334850SJohn Baldwin ext_pgs->nrdy = 0; 116782334850SJohn Baldwin ext_pgs->first_pg_off = 0; 116882334850SJohn Baldwin ext_pgs->last_pg_len = 0; 1169b2dba663SAndrew Gallatin ext_pgs->flags = 0; 117082334850SJohn Baldwin ext_pgs->hdr_len = 0; 117182334850SJohn Baldwin ext_pgs->trail_len = 0; 117282334850SJohn Baldwin ext_pgs->tls = NULL; 117382334850SJohn Baldwin ext_pgs->so = NULL; 117482334850SJohn Baldwin m->m_data = NULL; 117582334850SJohn Baldwin m->m_flags |= (M_EXT | M_RDONLY | M_NOMAP); 117682334850SJohn Baldwin m->m_ext.ext_type = EXT_PGS; 117782334850SJohn Baldwin m->m_ext.ext_flags = EXT_FLAG_EMBREF; 117882334850SJohn Baldwin m->m_ext.ext_count = 1; 117982334850SJohn Baldwin m->m_ext.ext_pgs = ext_pgs; 118082334850SJohn Baldwin m->m_ext.ext_size = 0; 118182334850SJohn Baldwin m->m_ext.ext_free = ext_free; 118282334850SJohn Baldwin return (m); 118382334850SJohn Baldwin } 118482334850SJohn Baldwin 118582334850SJohn Baldwin #ifdef INVARIANT_SUPPORT 118682334850SJohn Baldwin void 118782334850SJohn Baldwin mb_ext_pgs_check(struct mbuf_ext_pgs *ext_pgs) 118882334850SJohn Baldwin { 118982334850SJohn Baldwin 119082334850SJohn Baldwin /* 119182334850SJohn Baldwin * NB: This expects a non-empty buffer (npgs > 0 and 119282334850SJohn Baldwin * last_pg_len > 0). 119382334850SJohn Baldwin */ 119482334850SJohn Baldwin KASSERT(ext_pgs->npgs > 0, 119582334850SJohn Baldwin ("ext_pgs with no valid pages: %p", ext_pgs)); 119682334850SJohn Baldwin KASSERT(ext_pgs->npgs <= nitems(ext_pgs->pa), 119782334850SJohn Baldwin ("ext_pgs with too many pages: %p", ext_pgs)); 119882334850SJohn Baldwin KASSERT(ext_pgs->nrdy <= ext_pgs->npgs, 119982334850SJohn Baldwin ("ext_pgs with too many ready pages: %p", ext_pgs)); 120082334850SJohn Baldwin KASSERT(ext_pgs->first_pg_off < PAGE_SIZE, 120182334850SJohn Baldwin ("ext_pgs with too large page offset: %p", ext_pgs)); 120282334850SJohn Baldwin KASSERT(ext_pgs->last_pg_len > 0, 120382334850SJohn Baldwin ("ext_pgs with zero last page length: %p", ext_pgs)); 120482334850SJohn Baldwin KASSERT(ext_pgs->last_pg_len <= PAGE_SIZE, 120582334850SJohn Baldwin ("ext_pgs with too large last page length: %p", ext_pgs)); 120682334850SJohn Baldwin if (ext_pgs->npgs == 1) { 120782334850SJohn Baldwin KASSERT(ext_pgs->first_pg_off + ext_pgs->last_pg_len <= 120882334850SJohn Baldwin PAGE_SIZE, ("ext_pgs with single page too large: %p", 120982334850SJohn Baldwin ext_pgs)); 121082334850SJohn Baldwin } 121182334850SJohn Baldwin KASSERT(ext_pgs->hdr_len <= sizeof(ext_pgs->hdr), 121282334850SJohn Baldwin ("ext_pgs with too large header length: %p", ext_pgs)); 121382334850SJohn Baldwin KASSERT(ext_pgs->trail_len <= sizeof(ext_pgs->trail), 121482334850SJohn Baldwin ("ext_pgs with too large header length: %p", ext_pgs)); 121582334850SJohn Baldwin } 121682334850SJohn Baldwin #endif 121782334850SJohn Baldwin 121882334850SJohn Baldwin /* 12195e4bc63bSGleb Smirnoff * Clean up after mbufs with M_EXT storage attached to them if the 12205e4bc63bSGleb Smirnoff * reference count hits 1. 12215e4bc63bSGleb Smirnoff */ 12225e4bc63bSGleb Smirnoff void 12235e4bc63bSGleb Smirnoff mb_free_ext(struct mbuf *m) 12245e4bc63bSGleb Smirnoff { 122556a5f52eSGleb Smirnoff volatile u_int *refcnt; 122656a5f52eSGleb Smirnoff struct mbuf *mref; 12275e4bc63bSGleb Smirnoff int freembuf; 12285e4bc63bSGleb Smirnoff 12295e4bc63bSGleb Smirnoff KASSERT(m->m_flags & M_EXT, ("%s: M_EXT not set on %p", __func__, m)); 12305e4bc63bSGleb Smirnoff 123156a5f52eSGleb Smirnoff /* See if this is the mbuf that holds the embedded refcount. */ 123256a5f52eSGleb Smirnoff if (m->m_ext.ext_flags & EXT_FLAG_EMBREF) { 123356a5f52eSGleb Smirnoff refcnt = &m->m_ext.ext_count; 123456a5f52eSGleb Smirnoff mref = m; 123556a5f52eSGleb Smirnoff } else { 123656a5f52eSGleb Smirnoff KASSERT(m->m_ext.ext_cnt != NULL, 123756a5f52eSGleb Smirnoff ("%s: no refcounting pointer on %p", __func__, m)); 123856a5f52eSGleb Smirnoff refcnt = m->m_ext.ext_cnt; 123956a5f52eSGleb Smirnoff mref = __containerof(refcnt, struct mbuf, m_ext.ext_count); 124056a5f52eSGleb Smirnoff } 124156a5f52eSGleb Smirnoff 12425e4bc63bSGleb Smirnoff /* 124356a5f52eSGleb Smirnoff * Check if the header is embedded in the cluster. It is 124456a5f52eSGleb Smirnoff * important that we can't touch any of the mbuf fields 124556a5f52eSGleb Smirnoff * after we have freed the external storage, since mbuf 124617cd649fSGleb Smirnoff * could have been embedded in it. For now, the mbufs 124717cd649fSGleb Smirnoff * embedded into the cluster are always of type EXT_EXTREF, 124817cd649fSGleb Smirnoff * and for this type we won't free the mref. 12495e4bc63bSGleb Smirnoff */ 125017cd649fSGleb Smirnoff if (m->m_flags & M_NOFREE) { 125117cd649fSGleb Smirnoff freembuf = 0; 1252eec189c7SGleb Smirnoff KASSERT(m->m_ext.ext_type == EXT_EXTREF || 1253eec189c7SGleb Smirnoff m->m_ext.ext_type == EXT_RXRING, 125417cd649fSGleb Smirnoff ("%s: no-free mbuf %p has wrong type", __func__, m)); 125517cd649fSGleb Smirnoff } else 125617cd649fSGleb Smirnoff freembuf = 1; 12575e4bc63bSGleb Smirnoff 125856a5f52eSGleb Smirnoff /* Free attached storage if this mbuf is the only reference to it. */ 125956a5f52eSGleb Smirnoff if (*refcnt == 1 || atomic_fetchadd_int(refcnt, -1) == 1) { 12605e4bc63bSGleb Smirnoff switch (m->m_ext.ext_type) { 126156a5f52eSGleb Smirnoff case EXT_PACKET: 126256a5f52eSGleb Smirnoff /* The packet zone is special. */ 126356a5f52eSGleb Smirnoff if (*refcnt == 0) 126456a5f52eSGleb Smirnoff *refcnt = 1; 126556a5f52eSGleb Smirnoff uma_zfree(zone_pack, mref); 12665e4bc63bSGleb Smirnoff break; 12675e4bc63bSGleb Smirnoff case EXT_CLUSTER: 12685e4bc63bSGleb Smirnoff uma_zfree(zone_clust, m->m_ext.ext_buf); 126956a5f52eSGleb Smirnoff uma_zfree(zone_mbuf, mref); 12705e4bc63bSGleb Smirnoff break; 12715e4bc63bSGleb Smirnoff case EXT_JUMBOP: 12725e4bc63bSGleb Smirnoff uma_zfree(zone_jumbop, m->m_ext.ext_buf); 127356a5f52eSGleb Smirnoff uma_zfree(zone_mbuf, mref); 12745e4bc63bSGleb Smirnoff break; 12755e4bc63bSGleb Smirnoff case EXT_JUMBO9: 12765e4bc63bSGleb Smirnoff uma_zfree(zone_jumbo9, m->m_ext.ext_buf); 127756a5f52eSGleb Smirnoff uma_zfree(zone_mbuf, mref); 12785e4bc63bSGleb Smirnoff break; 12795e4bc63bSGleb Smirnoff case EXT_JUMBO16: 12805e4bc63bSGleb Smirnoff uma_zfree(zone_jumbo16, m->m_ext.ext_buf); 128156a5f52eSGleb Smirnoff uma_zfree(zone_mbuf, mref); 128256a5f52eSGleb Smirnoff break; 1283b2e60773SJohn Baldwin case EXT_PGS: { 1284b2e60773SJohn Baldwin #ifdef KERN_TLS 1285b2e60773SJohn Baldwin struct mbuf_ext_pgs *pgs; 1286b2e60773SJohn Baldwin struct ktls_session *tls; 1287b2e60773SJohn Baldwin #endif 1288b2e60773SJohn Baldwin 1289afa60c06SJohn Baldwin KASSERT(mref->m_ext.ext_free != NULL, 1290afa60c06SJohn Baldwin ("%s: ext_free not set", __func__)); 1291afa60c06SJohn Baldwin mref->m_ext.ext_free(mref); 1292b2e60773SJohn Baldwin #ifdef KERN_TLS 1293b2e60773SJohn Baldwin pgs = mref->m_ext.ext_pgs; 1294b2e60773SJohn Baldwin tls = pgs->tls; 1295b2e60773SJohn Baldwin if (tls != NULL && 1296b2e60773SJohn Baldwin !refcount_release_if_not_last(&tls->refcount)) 1297b2e60773SJohn Baldwin ktls_enqueue_to_free(pgs); 1298b2e60773SJohn Baldwin else 1299b2e60773SJohn Baldwin #endif 130082334850SJohn Baldwin uma_zfree(zone_extpgs, mref->m_ext.ext_pgs); 130182334850SJohn Baldwin uma_zfree(zone_mbuf, mref); 130282334850SJohn Baldwin break; 1303b2e60773SJohn Baldwin } 130456a5f52eSGleb Smirnoff case EXT_SFBUF: 13055e4bc63bSGleb Smirnoff case EXT_NET_DRV: 13065e4bc63bSGleb Smirnoff case EXT_MOD_TYPE: 13075e4bc63bSGleb Smirnoff case EXT_DISPOSABLE: 130807e87a1dSGleb Smirnoff KASSERT(mref->m_ext.ext_free != NULL, 13090ea37a86SGleb Smirnoff ("%s: ext_free not set", __func__)); 131007e87a1dSGleb Smirnoff mref->m_ext.ext_free(mref); 131156a5f52eSGleb Smirnoff uma_zfree(zone_mbuf, mref); 13120ea37a86SGleb Smirnoff break; 13135e4bc63bSGleb Smirnoff case EXT_EXTREF: 13145e4bc63bSGleb Smirnoff KASSERT(m->m_ext.ext_free != NULL, 13155e4bc63bSGleb Smirnoff ("%s: ext_free not set", __func__)); 1316e8fd18f3SGleb Smirnoff m->m_ext.ext_free(m); 13175e4bc63bSGleb Smirnoff break; 1318eec189c7SGleb Smirnoff case EXT_RXRING: 1319eec189c7SGleb Smirnoff KASSERT(m->m_ext.ext_free == NULL, 1320eec189c7SGleb Smirnoff ("%s: ext_free is set", __func__)); 1321eec189c7SGleb Smirnoff break; 13225e4bc63bSGleb Smirnoff default: 13235e4bc63bSGleb Smirnoff KASSERT(m->m_ext.ext_type == 0, 13245e4bc63bSGleb Smirnoff ("%s: unknown ext_type", __func__)); 13255e4bc63bSGleb Smirnoff } 13265e4bc63bSGleb Smirnoff } 13275e4bc63bSGleb Smirnoff 132856a5f52eSGleb Smirnoff if (freembuf && m != mref) 13295e4bc63bSGleb Smirnoff uma_zfree(zone_mbuf, m); 13305e4bc63bSGleb Smirnoff } 13315e4bc63bSGleb Smirnoff 13325e4bc63bSGleb Smirnoff /* 13335e4bc63bSGleb Smirnoff * Official mbuf(9) allocation KPI for stack and drivers: 13345e4bc63bSGleb Smirnoff * 13355e4bc63bSGleb Smirnoff * m_get() - a single mbuf without any attachments, sys/mbuf.h. 13365e4bc63bSGleb Smirnoff * m_gethdr() - a single mbuf initialized as M_PKTHDR, sys/mbuf.h. 13375e4bc63bSGleb Smirnoff * m_getcl() - an mbuf + 2k cluster, sys/mbuf.h. 13385e4bc63bSGleb Smirnoff * m_clget() - attach cluster to already allocated mbuf. 13395e4bc63bSGleb Smirnoff * m_cljget() - attach jumbo cluster to already allocated mbuf. 13405e4bc63bSGleb Smirnoff * m_get2() - allocate minimum mbuf that would fit size argument. 13415e4bc63bSGleb Smirnoff * m_getm2() - allocate a chain of mbufs/clusters. 13425e4bc63bSGleb Smirnoff * m_extadd() - attach external cluster to mbuf. 13435e4bc63bSGleb Smirnoff * 13445e4bc63bSGleb Smirnoff * m_free() - free single mbuf with its tags and ext, sys/mbuf.h. 13455e4bc63bSGleb Smirnoff * m_freem() - free chain of mbufs. 13465e4bc63bSGleb Smirnoff */ 13475e4bc63bSGleb Smirnoff 13485e4bc63bSGleb Smirnoff int 13495e4bc63bSGleb Smirnoff m_clget(struct mbuf *m, int how) 13505e4bc63bSGleb Smirnoff { 13515e4bc63bSGleb Smirnoff 13525e4bc63bSGleb Smirnoff KASSERT((m->m_flags & M_EXT) == 0, ("%s: mbuf %p has M_EXT", 13535e4bc63bSGleb Smirnoff __func__, m)); 13545e4bc63bSGleb Smirnoff m->m_ext.ext_buf = (char *)NULL; 13555e4bc63bSGleb Smirnoff uma_zalloc_arg(zone_clust, m, how); 13565e4bc63bSGleb Smirnoff /* 13575e4bc63bSGleb Smirnoff * On a cluster allocation failure, drain the packet zone and retry, 13585e4bc63bSGleb Smirnoff * we might be able to loosen a few clusters up on the drain. 13595e4bc63bSGleb Smirnoff */ 13605e4bc63bSGleb Smirnoff if ((how & M_NOWAIT) && (m->m_ext.ext_buf == NULL)) { 136108cfa56eSMark Johnston uma_zone_reclaim(zone_pack, UMA_RECLAIM_DRAIN); 13625e4bc63bSGleb Smirnoff uma_zalloc_arg(zone_clust, m, how); 13635e4bc63bSGleb Smirnoff } 1364480f4e94SGeorge V. Neville-Neil MBUF_PROBE2(m__clget, m, how); 13655e4bc63bSGleb Smirnoff return (m->m_flags & M_EXT); 13665e4bc63bSGleb Smirnoff } 13675e4bc63bSGleb Smirnoff 13685e4bc63bSGleb Smirnoff /* 13695e4bc63bSGleb Smirnoff * m_cljget() is different from m_clget() as it can allocate clusters without 13705e4bc63bSGleb Smirnoff * attaching them to an mbuf. In that case the return value is the pointer 13715e4bc63bSGleb Smirnoff * to the cluster of the requested size. If an mbuf was specified, it gets 13725e4bc63bSGleb Smirnoff * the cluster attached to it and the return value can be safely ignored. 13735e4bc63bSGleb Smirnoff * For size it takes MCLBYTES, MJUMPAGESIZE, MJUM9BYTES, MJUM16BYTES. 13745e4bc63bSGleb Smirnoff */ 13755e4bc63bSGleb Smirnoff void * 13765e4bc63bSGleb Smirnoff m_cljget(struct mbuf *m, int how, int size) 13775e4bc63bSGleb Smirnoff { 13785e4bc63bSGleb Smirnoff uma_zone_t zone; 1379480f4e94SGeorge V. Neville-Neil void *retval; 13805e4bc63bSGleb Smirnoff 13815e4bc63bSGleb Smirnoff if (m != NULL) { 13825e4bc63bSGleb Smirnoff KASSERT((m->m_flags & M_EXT) == 0, ("%s: mbuf %p has M_EXT", 13835e4bc63bSGleb Smirnoff __func__, m)); 13845e4bc63bSGleb Smirnoff m->m_ext.ext_buf = NULL; 13855e4bc63bSGleb Smirnoff } 13865e4bc63bSGleb Smirnoff 13875e4bc63bSGleb Smirnoff zone = m_getzone(size); 1388480f4e94SGeorge V. Neville-Neil retval = uma_zalloc_arg(zone, m, how); 1389480f4e94SGeorge V. Neville-Neil 1390480f4e94SGeorge V. Neville-Neil MBUF_PROBE4(m__cljget, m, how, size, retval); 1391480f4e94SGeorge V. Neville-Neil 1392480f4e94SGeorge V. Neville-Neil return (retval); 13935e4bc63bSGleb Smirnoff } 13945e4bc63bSGleb Smirnoff 13955e4bc63bSGleb Smirnoff /* 13965e4bc63bSGleb Smirnoff * m_get2() allocates minimum mbuf that would fit "size" argument. 13975e4bc63bSGleb Smirnoff */ 13985e4bc63bSGleb Smirnoff struct mbuf * 13995e4bc63bSGleb Smirnoff m_get2(int size, int how, short type, int flags) 14005e4bc63bSGleb Smirnoff { 14015e4bc63bSGleb Smirnoff struct mb_args args; 14025e4bc63bSGleb Smirnoff struct mbuf *m, *n; 14035e4bc63bSGleb Smirnoff 14045e4bc63bSGleb Smirnoff args.flags = flags; 14055e4bc63bSGleb Smirnoff args.type = type; 14065e4bc63bSGleb Smirnoff 14075e4bc63bSGleb Smirnoff if (size <= MHLEN || (size <= MLEN && (flags & M_PKTHDR) == 0)) 14085e4bc63bSGleb Smirnoff return (uma_zalloc_arg(zone_mbuf, &args, how)); 14095e4bc63bSGleb Smirnoff if (size <= MCLBYTES) 14105e4bc63bSGleb Smirnoff return (uma_zalloc_arg(zone_pack, &args, how)); 14115e4bc63bSGleb Smirnoff 14125e4bc63bSGleb Smirnoff if (size > MJUMPAGESIZE) 14135e4bc63bSGleb Smirnoff return (NULL); 14145e4bc63bSGleb Smirnoff 14155e4bc63bSGleb Smirnoff m = uma_zalloc_arg(zone_mbuf, &args, how); 14165e4bc63bSGleb Smirnoff if (m == NULL) 14175e4bc63bSGleb Smirnoff return (NULL); 14185e4bc63bSGleb Smirnoff 14195e4bc63bSGleb Smirnoff n = uma_zalloc_arg(zone_jumbop, m, how); 14205e4bc63bSGleb Smirnoff if (n == NULL) { 14215e4bc63bSGleb Smirnoff uma_zfree(zone_mbuf, m); 14225e4bc63bSGleb Smirnoff return (NULL); 14235e4bc63bSGleb Smirnoff } 14245e4bc63bSGleb Smirnoff 14255e4bc63bSGleb Smirnoff return (m); 14265e4bc63bSGleb Smirnoff } 14275e4bc63bSGleb Smirnoff 14285e4bc63bSGleb Smirnoff /* 14295e4bc63bSGleb Smirnoff * m_getjcl() returns an mbuf with a cluster of the specified size attached. 14305e4bc63bSGleb Smirnoff * For size it takes MCLBYTES, MJUMPAGESIZE, MJUM9BYTES, MJUM16BYTES. 14315e4bc63bSGleb Smirnoff */ 14325e4bc63bSGleb Smirnoff struct mbuf * 14335e4bc63bSGleb Smirnoff m_getjcl(int how, short type, int flags, int size) 14345e4bc63bSGleb Smirnoff { 14355e4bc63bSGleb Smirnoff struct mb_args args; 14365e4bc63bSGleb Smirnoff struct mbuf *m, *n; 14375e4bc63bSGleb Smirnoff uma_zone_t zone; 14385e4bc63bSGleb Smirnoff 14395e4bc63bSGleb Smirnoff if (size == MCLBYTES) 14405e4bc63bSGleb Smirnoff return m_getcl(how, type, flags); 14415e4bc63bSGleb Smirnoff 14425e4bc63bSGleb Smirnoff args.flags = flags; 14435e4bc63bSGleb Smirnoff args.type = type; 14445e4bc63bSGleb Smirnoff 14455e4bc63bSGleb Smirnoff m = uma_zalloc_arg(zone_mbuf, &args, how); 14465e4bc63bSGleb Smirnoff if (m == NULL) 14475e4bc63bSGleb Smirnoff return (NULL); 14485e4bc63bSGleb Smirnoff 14495e4bc63bSGleb Smirnoff zone = m_getzone(size); 14505e4bc63bSGleb Smirnoff n = uma_zalloc_arg(zone, m, how); 14515e4bc63bSGleb Smirnoff if (n == NULL) { 14525e4bc63bSGleb Smirnoff uma_zfree(zone_mbuf, m); 14535e4bc63bSGleb Smirnoff return (NULL); 14545e4bc63bSGleb Smirnoff } 14555e4bc63bSGleb Smirnoff return (m); 14565e4bc63bSGleb Smirnoff } 14575e4bc63bSGleb Smirnoff 14585e4bc63bSGleb Smirnoff /* 14595e4bc63bSGleb Smirnoff * Allocate a given length worth of mbufs and/or clusters (whatever fits 14605e4bc63bSGleb Smirnoff * best) and return a pointer to the top of the allocated chain. If an 14615e4bc63bSGleb Smirnoff * existing mbuf chain is provided, then we will append the new chain 146258c43838SAndriy Voskoboinyk * to the existing one and return a pointer to the provided mbuf. 14635e4bc63bSGleb Smirnoff */ 14645e4bc63bSGleb Smirnoff struct mbuf * 14655e4bc63bSGleb Smirnoff m_getm2(struct mbuf *m, int len, int how, short type, int flags) 14665e4bc63bSGleb Smirnoff { 14675e4bc63bSGleb Smirnoff struct mbuf *mb, *nm = NULL, *mtail = NULL; 14685e4bc63bSGleb Smirnoff 14695e4bc63bSGleb Smirnoff KASSERT(len >= 0, ("%s: len is < 0", __func__)); 14705e4bc63bSGleb Smirnoff 14715e4bc63bSGleb Smirnoff /* Validate flags. */ 14725e4bc63bSGleb Smirnoff flags &= (M_PKTHDR | M_EOR); 14735e4bc63bSGleb Smirnoff 14745e4bc63bSGleb Smirnoff /* Packet header mbuf must be first in chain. */ 14755e4bc63bSGleb Smirnoff if ((flags & M_PKTHDR) && m != NULL) 14765e4bc63bSGleb Smirnoff flags &= ~M_PKTHDR; 14775e4bc63bSGleb Smirnoff 14785e4bc63bSGleb Smirnoff /* Loop and append maximum sized mbufs to the chain tail. */ 14795e4bc63bSGleb Smirnoff while (len > 0) { 14805e4bc63bSGleb Smirnoff if (len > MCLBYTES) 14815e4bc63bSGleb Smirnoff mb = m_getjcl(how, type, (flags & M_PKTHDR), 14825e4bc63bSGleb Smirnoff MJUMPAGESIZE); 14835e4bc63bSGleb Smirnoff else if (len >= MINCLSIZE) 14845e4bc63bSGleb Smirnoff mb = m_getcl(how, type, (flags & M_PKTHDR)); 14855e4bc63bSGleb Smirnoff else if (flags & M_PKTHDR) 14865e4bc63bSGleb Smirnoff mb = m_gethdr(how, type); 14875e4bc63bSGleb Smirnoff else 14885e4bc63bSGleb Smirnoff mb = m_get(how, type); 14895e4bc63bSGleb Smirnoff 14905e4bc63bSGleb Smirnoff /* Fail the whole operation if one mbuf can't be allocated. */ 14915e4bc63bSGleb Smirnoff if (mb == NULL) { 14925e4bc63bSGleb Smirnoff if (nm != NULL) 14935e4bc63bSGleb Smirnoff m_freem(nm); 14945e4bc63bSGleb Smirnoff return (NULL); 14955e4bc63bSGleb Smirnoff } 14965e4bc63bSGleb Smirnoff 14975e4bc63bSGleb Smirnoff /* Book keeping. */ 14985e4bc63bSGleb Smirnoff len -= M_SIZE(mb); 14995e4bc63bSGleb Smirnoff if (mtail != NULL) 15005e4bc63bSGleb Smirnoff mtail->m_next = mb; 15015e4bc63bSGleb Smirnoff else 15025e4bc63bSGleb Smirnoff nm = mb; 15035e4bc63bSGleb Smirnoff mtail = mb; 15045e4bc63bSGleb Smirnoff flags &= ~M_PKTHDR; /* Only valid on the first mbuf. */ 15055e4bc63bSGleb Smirnoff } 15065e4bc63bSGleb Smirnoff if (flags & M_EOR) 15075e4bc63bSGleb Smirnoff mtail->m_flags |= M_EOR; /* Only valid on the last mbuf. */ 15085e4bc63bSGleb Smirnoff 15095e4bc63bSGleb Smirnoff /* If mbuf was supplied, append new chain to the end of it. */ 15105e4bc63bSGleb Smirnoff if (m != NULL) { 15115e4bc63bSGleb Smirnoff for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next) 15125e4bc63bSGleb Smirnoff ; 15135e4bc63bSGleb Smirnoff mtail->m_next = nm; 15145e4bc63bSGleb Smirnoff mtail->m_flags &= ~M_EOR; 15155e4bc63bSGleb Smirnoff } else 15165e4bc63bSGleb Smirnoff m = nm; 15175e4bc63bSGleb Smirnoff 15185e4bc63bSGleb Smirnoff return (m); 15195e4bc63bSGleb Smirnoff } 15205e4bc63bSGleb Smirnoff 15215e4bc63bSGleb Smirnoff /*- 15225e4bc63bSGleb Smirnoff * Configure a provided mbuf to refer to the provided external storage 152356a5f52eSGleb Smirnoff * buffer and setup a reference count for said buffer. 15245e4bc63bSGleb Smirnoff * 15255e4bc63bSGleb Smirnoff * Arguments: 15265e4bc63bSGleb Smirnoff * mb The existing mbuf to which to attach the provided buffer. 15275e4bc63bSGleb Smirnoff * buf The address of the provided external storage buffer. 15285e4bc63bSGleb Smirnoff * size The size of the provided buffer. 15295e4bc63bSGleb Smirnoff * freef A pointer to a routine that is responsible for freeing the 15305e4bc63bSGleb Smirnoff * provided external storage buffer. 15315e4bc63bSGleb Smirnoff * args A pointer to an argument structure (of any type) to be passed 15325e4bc63bSGleb Smirnoff * to the provided freef routine (may be NULL). 15335e4bc63bSGleb Smirnoff * flags Any other flags to be passed to the provided mbuf. 15345e4bc63bSGleb Smirnoff * type The type that the external storage buffer should be 15355e4bc63bSGleb Smirnoff * labeled with. 15365e4bc63bSGleb Smirnoff * 15375e4bc63bSGleb Smirnoff * Returns: 15385e4bc63bSGleb Smirnoff * Nothing. 15395e4bc63bSGleb Smirnoff */ 154056a5f52eSGleb Smirnoff void 1541e8fd18f3SGleb Smirnoff m_extadd(struct mbuf *mb, char *buf, u_int size, m_ext_free_t freef, 1542e8fd18f3SGleb Smirnoff void *arg1, void *arg2, int flags, int type) 15435e4bc63bSGleb Smirnoff { 154456a5f52eSGleb Smirnoff 15455e4bc63bSGleb Smirnoff KASSERT(type != EXT_CLUSTER, ("%s: EXT_CLUSTER not allowed", __func__)); 15465e4bc63bSGleb Smirnoff 15475e4bc63bSGleb Smirnoff mb->m_flags |= (M_EXT | flags); 15485e4bc63bSGleb Smirnoff mb->m_ext.ext_buf = buf; 15495e4bc63bSGleb Smirnoff mb->m_data = mb->m_ext.ext_buf; 15505e4bc63bSGleb Smirnoff mb->m_ext.ext_size = size; 15515e4bc63bSGleb Smirnoff mb->m_ext.ext_free = freef; 15525e4bc63bSGleb Smirnoff mb->m_ext.ext_arg1 = arg1; 15535e4bc63bSGleb Smirnoff mb->m_ext.ext_arg2 = arg2; 15545e4bc63bSGleb Smirnoff mb->m_ext.ext_type = type; 15555e4bc63bSGleb Smirnoff 155656a5f52eSGleb Smirnoff if (type != EXT_EXTREF) { 155756a5f52eSGleb Smirnoff mb->m_ext.ext_count = 1; 155856a5f52eSGleb Smirnoff mb->m_ext.ext_flags = EXT_FLAG_EMBREF; 155956a5f52eSGleb Smirnoff } else 156056a5f52eSGleb Smirnoff mb->m_ext.ext_flags = 0; 15615e4bc63bSGleb Smirnoff } 15625e4bc63bSGleb Smirnoff 15635e4bc63bSGleb Smirnoff /* 15645e4bc63bSGleb Smirnoff * Free an entire chain of mbufs and associated external buffers, if 15655e4bc63bSGleb Smirnoff * applicable. 15665e4bc63bSGleb Smirnoff */ 15675e4bc63bSGleb Smirnoff void 15685e4bc63bSGleb Smirnoff m_freem(struct mbuf *mb) 15695e4bc63bSGleb Smirnoff { 15705e4bc63bSGleb Smirnoff 1571c8f59118SGleb Smirnoff MBUF_PROBE1(m__freem, mb); 15725e4bc63bSGleb Smirnoff while (mb != NULL) 15735e4bc63bSGleb Smirnoff mb = m_free(mb); 15745e4bc63bSGleb Smirnoff } 1575fb3bc596SJohn Baldwin 1576fb3bc596SJohn Baldwin void 1577fb3bc596SJohn Baldwin m_snd_tag_init(struct m_snd_tag *mst, struct ifnet *ifp) 1578fb3bc596SJohn Baldwin { 1579fb3bc596SJohn Baldwin 1580fb3bc596SJohn Baldwin if_ref(ifp); 1581fb3bc596SJohn Baldwin mst->ifp = ifp; 1582fb3bc596SJohn Baldwin refcount_init(&mst->refcount, 1); 1583fb3bc596SJohn Baldwin counter_u64_add(snd_tag_count, 1); 1584fb3bc596SJohn Baldwin } 1585fb3bc596SJohn Baldwin 1586fb3bc596SJohn Baldwin void 1587fb3bc596SJohn Baldwin m_snd_tag_destroy(struct m_snd_tag *mst) 1588fb3bc596SJohn Baldwin { 1589fb3bc596SJohn Baldwin struct ifnet *ifp; 1590fb3bc596SJohn Baldwin 1591fb3bc596SJohn Baldwin ifp = mst->ifp; 1592fb3bc596SJohn Baldwin ifp->if_snd_tag_free(mst); 1593fb3bc596SJohn Baldwin if_rele(ifp); 1594fb3bc596SJohn Baldwin counter_u64_add(snd_tag_count, -1); 1595fb3bc596SJohn Baldwin } 1596