1/*- 2 * Copyright (c) 2004, 2005, 3 * Bosko Milekic <bmilekic@FreeBSD.org>. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions and the following 10 * disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28#include <sys/cdefs.h>
| 1/*- 2 * Copyright (c) 2004, 2005, 3 * Bosko Milekic <bmilekic@FreeBSD.org>. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions and the following 10 * disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28#include <sys/cdefs.h>
|
29__FBSDID("$FreeBSD: head/sys/kern/kern_mbuf.c 193511 2009-06-05 14:55:22Z rwatson $");
| 29__FBSDID("$FreeBSD: head/sys/kern/kern_mbuf.c 194454 2009-06-18 17:59:04Z alc $");
|
30 31#include "opt_param.h" 32 33#include <sys/param.h> 34#include <sys/malloc.h> 35#include <sys/systm.h> 36#include <sys/mbuf.h> 37#include <sys/domain.h> 38#include <sys/eventhandler.h> 39#include <sys/kernel.h> 40#include <sys/protosw.h> 41#include <sys/smp.h> 42#include <sys/sysctl.h> 43 44#include <security/mac/mac_framework.h> 45 46#include <vm/vm.h>
| 30 31#include "opt_param.h" 32 33#include <sys/param.h> 34#include <sys/malloc.h> 35#include <sys/systm.h> 36#include <sys/mbuf.h> 37#include <sys/domain.h> 38#include <sys/eventhandler.h> 39#include <sys/kernel.h> 40#include <sys/protosw.h> 41#include <sys/smp.h> 42#include <sys/sysctl.h> 43 44#include <security/mac/mac_framework.h> 45 46#include <vm/vm.h>
|
| 47#include <vm/vm_extern.h> 48#include <vm/vm_kern.h>
|
47#include <vm/vm_page.h> 48#include <vm/uma.h> 49#include <vm/uma_int.h> 50#include <vm/uma_dbg.h> 51 52/* 53 * In FreeBSD, Mbufs and Mbuf Clusters are allocated from UMA 54 * Zones. 55 * 56 * Mbuf Clusters (2K, contiguous) are allocated from the Cluster 57 * Zone. The Zone can be capped at kern.ipc.nmbclusters, if the 58 * administrator so desires. 59 * 60 * Mbufs are allocated from a UMA Master Zone called the Mbuf 61 * Zone. 62 * 63 * Additionally, FreeBSD provides a Packet Zone, which it 64 * configures as a Secondary Zone to the Mbuf Master Zone, 65 * thus sharing backend Slab kegs with the Mbuf Master Zone. 66 * 67 * Thus common-case allocations and locking are simplified: 68 * 69 * m_clget() m_getcl() 70 * | | 71 * | .------------>[(Packet Cache)] m_get(), m_gethdr() 72 * | | [ Packet ] | 73 * [(Cluster Cache)] [ Secondary ] [ (Mbuf Cache) ] 74 * [ Cluster Zone ] [ Zone ] [ Mbuf Master Zone ] 75 * | \________ | 76 * [ Cluster Keg ] \ / 77 * | [ Mbuf Keg ] 78 * [ Cluster Slabs ] | 79 * | [ Mbuf Slabs ] 80 * \____________(VM)_________________/ 81 * 82 * 83 * Whenever an object is allocated with uma_zalloc() out of 84 * one of the Zones its _ctor_ function is executed. The same 85 * for any deallocation through uma_zfree() the _dtor_ function 86 * is executed. 87 * 88 * Caches are per-CPU and are filled from the Master Zone. 89 * 90 * Whenever an object is allocated from the underlying global 91 * memory pool it gets pre-initialized with the _zinit_ functions. 92 * When the Keg's are overfull objects get decomissioned with 93 * _zfini_ functions and free'd back to the global memory pool. 94 * 95 */ 96 97int nmbclusters; /* limits number of mbuf clusters */ 98int nmbjumbop; /* limits number of page size jumbo clusters */ 99int nmbjumbo9; /* limits number of 9k jumbo clusters */ 100int nmbjumbo16; /* limits number of 16k jumbo clusters */ 101struct mbstat mbstat; 102 103/* 104 * tunable_mbinit() has to be run before init_maxsockets() thus 105 * the SYSINIT order below is SI_ORDER_MIDDLE while init_maxsockets() 106 * runs at SI_ORDER_ANY. 107 */ 108static void 109tunable_mbinit(void *dummy) 110{ 111 TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters); 112 113 /* This has to be done before VM init. */ 114 if (nmbclusters == 0) 115 nmbclusters = 1024 + maxusers * 64; 116 nmbjumbop = nmbclusters / 2; 117 nmbjumbo9 = nmbjumbop / 2; 118 nmbjumbo16 = nmbjumbo9 / 2; 119} 120SYSINIT(tunable_mbinit, SI_SUB_TUNABLES, SI_ORDER_MIDDLE, tunable_mbinit, NULL); 121 122static int 123sysctl_nmbclusters(SYSCTL_HANDLER_ARGS) 124{ 125 int error, newnmbclusters; 126 127 newnmbclusters = nmbclusters; 128 error = sysctl_handle_int(oidp, &newnmbclusters, 0, req); 129 if (error == 0 && req->newptr) { 130 if (newnmbclusters > nmbclusters) { 131 nmbclusters = newnmbclusters; 132 uma_zone_set_max(zone_clust, nmbclusters); 133 EVENTHANDLER_INVOKE(nmbclusters_change); 134 } else 135 error = EINVAL; 136 } 137 return (error); 138} 139SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbclusters, CTLTYPE_INT|CTLFLAG_RW, 140&nmbclusters, 0, sysctl_nmbclusters, "IU", 141 "Maximum number of mbuf clusters allowed"); 142 143static int 144sysctl_nmbjumbop(SYSCTL_HANDLER_ARGS) 145{ 146 int error, newnmbjumbop; 147 148 newnmbjumbop = nmbjumbop; 149 error = sysctl_handle_int(oidp, &newnmbjumbop, 0, req); 150 if (error == 0 && req->newptr) { 151 if (newnmbjumbop> nmbjumbop) { 152 nmbjumbop = newnmbjumbop; 153 uma_zone_set_max(zone_jumbop, nmbjumbop); 154 } else 155 error = EINVAL; 156 } 157 return (error); 158} 159SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbop, CTLTYPE_INT|CTLFLAG_RW, 160&nmbjumbop, 0, sysctl_nmbjumbop, "IU", 161 "Maximum number of mbuf page size jumbo clusters allowed"); 162 163 164static int 165sysctl_nmbjumbo9(SYSCTL_HANDLER_ARGS) 166{ 167 int error, newnmbjumbo9; 168 169 newnmbjumbo9 = nmbjumbo9; 170 error = sysctl_handle_int(oidp, &newnmbjumbo9, 0, req); 171 if (error == 0 && req->newptr) { 172 if (newnmbjumbo9> nmbjumbo9) { 173 nmbjumbo9 = newnmbjumbo9; 174 uma_zone_set_max(zone_jumbo9, nmbjumbo9); 175 } else 176 error = EINVAL; 177 } 178 return (error); 179} 180SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbo9, CTLTYPE_INT|CTLFLAG_RW, 181&nmbjumbo9, 0, sysctl_nmbjumbo9, "IU", 182 "Maximum number of mbuf 9k jumbo clusters allowed"); 183 184static int 185sysctl_nmbjumbo16(SYSCTL_HANDLER_ARGS) 186{ 187 int error, newnmbjumbo16; 188 189 newnmbjumbo16 = nmbjumbo16; 190 error = sysctl_handle_int(oidp, &newnmbjumbo16, 0, req); 191 if (error == 0 && req->newptr) { 192 if (newnmbjumbo16> nmbjumbo16) { 193 nmbjumbo16 = newnmbjumbo16; 194 uma_zone_set_max(zone_jumbo16, nmbjumbo16); 195 } else 196 error = EINVAL; 197 } 198 return (error); 199} 200SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbo16, CTLTYPE_INT|CTLFLAG_RW, 201&nmbjumbo16, 0, sysctl_nmbjumbo16, "IU", 202 "Maximum number of mbuf 16k jumbo clusters allowed"); 203 204 205 206SYSCTL_STRUCT(_kern_ipc, OID_AUTO, mbstat, CTLFLAG_RD, &mbstat, mbstat, 207 "Mbuf general information and statistics"); 208 209/* 210 * Zones from which we allocate. 211 */ 212uma_zone_t zone_mbuf; 213uma_zone_t zone_clust; 214uma_zone_t zone_pack; 215uma_zone_t zone_jumbop; 216uma_zone_t zone_jumbo9; 217uma_zone_t zone_jumbo16; 218uma_zone_t zone_ext_refcnt; 219 220/* 221 * Local prototypes. 222 */ 223static int mb_ctor_mbuf(void *, int, void *, int); 224static int mb_ctor_clust(void *, int, void *, int); 225static int mb_ctor_pack(void *, int, void *, int); 226static void mb_dtor_mbuf(void *, int, void *); 227static void mb_dtor_clust(void *, int, void *); 228static void mb_dtor_pack(void *, int, void *); 229static int mb_zinit_pack(void *, int, int); 230static void mb_zfini_pack(void *, int); 231 232static void mb_reclaim(void *); 233static void mbuf_init(void *); 234static void *mbuf_jumbo_alloc(uma_zone_t, int, u_int8_t *, int);
| 49#include <vm/vm_page.h> 50#include <vm/uma.h> 51#include <vm/uma_int.h> 52#include <vm/uma_dbg.h> 53 54/* 55 * In FreeBSD, Mbufs and Mbuf Clusters are allocated from UMA 56 * Zones. 57 * 58 * Mbuf Clusters (2K, contiguous) are allocated from the Cluster 59 * Zone. The Zone can be capped at kern.ipc.nmbclusters, if the 60 * administrator so desires. 61 * 62 * Mbufs are allocated from a UMA Master Zone called the Mbuf 63 * Zone. 64 * 65 * Additionally, FreeBSD provides a Packet Zone, which it 66 * configures as a Secondary Zone to the Mbuf Master Zone, 67 * thus sharing backend Slab kegs with the Mbuf Master Zone. 68 * 69 * Thus common-case allocations and locking are simplified: 70 * 71 * m_clget() m_getcl() 72 * | | 73 * | .------------>[(Packet Cache)] m_get(), m_gethdr() 74 * | | [ Packet ] | 75 * [(Cluster Cache)] [ Secondary ] [ (Mbuf Cache) ] 76 * [ Cluster Zone ] [ Zone ] [ Mbuf Master Zone ] 77 * | \________ | 78 * [ Cluster Keg ] \ / 79 * | [ Mbuf Keg ] 80 * [ Cluster Slabs ] | 81 * | [ Mbuf Slabs ] 82 * \____________(VM)_________________/ 83 * 84 * 85 * Whenever an object is allocated with uma_zalloc() out of 86 * one of the Zones its _ctor_ function is executed. The same 87 * for any deallocation through uma_zfree() the _dtor_ function 88 * is executed. 89 * 90 * Caches are per-CPU and are filled from the Master Zone. 91 * 92 * Whenever an object is allocated from the underlying global 93 * memory pool it gets pre-initialized with the _zinit_ functions. 94 * When the Keg's are overfull objects get decomissioned with 95 * _zfini_ functions and free'd back to the global memory pool. 96 * 97 */ 98 99int nmbclusters; /* limits number of mbuf clusters */ 100int nmbjumbop; /* limits number of page size jumbo clusters */ 101int nmbjumbo9; /* limits number of 9k jumbo clusters */ 102int nmbjumbo16; /* limits number of 16k jumbo clusters */ 103struct mbstat mbstat; 104 105/* 106 * tunable_mbinit() has to be run before init_maxsockets() thus 107 * the SYSINIT order below is SI_ORDER_MIDDLE while init_maxsockets() 108 * runs at SI_ORDER_ANY. 109 */ 110static void 111tunable_mbinit(void *dummy) 112{ 113 TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters); 114 115 /* This has to be done before VM init. */ 116 if (nmbclusters == 0) 117 nmbclusters = 1024 + maxusers * 64; 118 nmbjumbop = nmbclusters / 2; 119 nmbjumbo9 = nmbjumbop / 2; 120 nmbjumbo16 = nmbjumbo9 / 2; 121} 122SYSINIT(tunable_mbinit, SI_SUB_TUNABLES, SI_ORDER_MIDDLE, tunable_mbinit, NULL); 123 124static int 125sysctl_nmbclusters(SYSCTL_HANDLER_ARGS) 126{ 127 int error, newnmbclusters; 128 129 newnmbclusters = nmbclusters; 130 error = sysctl_handle_int(oidp, &newnmbclusters, 0, req); 131 if (error == 0 && req->newptr) { 132 if (newnmbclusters > nmbclusters) { 133 nmbclusters = newnmbclusters; 134 uma_zone_set_max(zone_clust, nmbclusters); 135 EVENTHANDLER_INVOKE(nmbclusters_change); 136 } else 137 error = EINVAL; 138 } 139 return (error); 140} 141SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbclusters, CTLTYPE_INT|CTLFLAG_RW, 142&nmbclusters, 0, sysctl_nmbclusters, "IU", 143 "Maximum number of mbuf clusters allowed"); 144 145static int 146sysctl_nmbjumbop(SYSCTL_HANDLER_ARGS) 147{ 148 int error, newnmbjumbop; 149 150 newnmbjumbop = nmbjumbop; 151 error = sysctl_handle_int(oidp, &newnmbjumbop, 0, req); 152 if (error == 0 && req->newptr) { 153 if (newnmbjumbop> nmbjumbop) { 154 nmbjumbop = newnmbjumbop; 155 uma_zone_set_max(zone_jumbop, nmbjumbop); 156 } else 157 error = EINVAL; 158 } 159 return (error); 160} 161SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbop, CTLTYPE_INT|CTLFLAG_RW, 162&nmbjumbop, 0, sysctl_nmbjumbop, "IU", 163 "Maximum number of mbuf page size jumbo clusters allowed"); 164 165 166static int 167sysctl_nmbjumbo9(SYSCTL_HANDLER_ARGS) 168{ 169 int error, newnmbjumbo9; 170 171 newnmbjumbo9 = nmbjumbo9; 172 error = sysctl_handle_int(oidp, &newnmbjumbo9, 0, req); 173 if (error == 0 && req->newptr) { 174 if (newnmbjumbo9> nmbjumbo9) { 175 nmbjumbo9 = newnmbjumbo9; 176 uma_zone_set_max(zone_jumbo9, nmbjumbo9); 177 } else 178 error = EINVAL; 179 } 180 return (error); 181} 182SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbo9, CTLTYPE_INT|CTLFLAG_RW, 183&nmbjumbo9, 0, sysctl_nmbjumbo9, "IU", 184 "Maximum number of mbuf 9k jumbo clusters allowed"); 185 186static int 187sysctl_nmbjumbo16(SYSCTL_HANDLER_ARGS) 188{ 189 int error, newnmbjumbo16; 190 191 newnmbjumbo16 = nmbjumbo16; 192 error = sysctl_handle_int(oidp, &newnmbjumbo16, 0, req); 193 if (error == 0 && req->newptr) { 194 if (newnmbjumbo16> nmbjumbo16) { 195 nmbjumbo16 = newnmbjumbo16; 196 uma_zone_set_max(zone_jumbo16, nmbjumbo16); 197 } else 198 error = EINVAL; 199 } 200 return (error); 201} 202SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbo16, CTLTYPE_INT|CTLFLAG_RW, 203&nmbjumbo16, 0, sysctl_nmbjumbo16, "IU", 204 "Maximum number of mbuf 16k jumbo clusters allowed"); 205 206 207 208SYSCTL_STRUCT(_kern_ipc, OID_AUTO, mbstat, CTLFLAG_RD, &mbstat, mbstat, 209 "Mbuf general information and statistics"); 210 211/* 212 * Zones from which we allocate. 213 */ 214uma_zone_t zone_mbuf; 215uma_zone_t zone_clust; 216uma_zone_t zone_pack; 217uma_zone_t zone_jumbop; 218uma_zone_t zone_jumbo9; 219uma_zone_t zone_jumbo16; 220uma_zone_t zone_ext_refcnt; 221 222/* 223 * Local prototypes. 224 */ 225static int mb_ctor_mbuf(void *, int, void *, int); 226static int mb_ctor_clust(void *, int, void *, int); 227static int mb_ctor_pack(void *, int, void *, int); 228static void mb_dtor_mbuf(void *, int, void *); 229static void mb_dtor_clust(void *, int, void *); 230static void mb_dtor_pack(void *, int, void *); 231static int mb_zinit_pack(void *, int, int); 232static void mb_zfini_pack(void *, int); 233 234static void mb_reclaim(void *); 235static void mbuf_init(void *); 236static void *mbuf_jumbo_alloc(uma_zone_t, int, u_int8_t *, int);
|
235static void mbuf_jumbo_free(void *, int, u_int8_t);
| |
236
| 237
|
237static MALLOC_DEFINE(M_JUMBOFRAME, "jumboframes", "mbuf jumbo frame buffers"); 238
| |
239/* Ensure that MSIZE doesn't break dtom() - it must be a power of 2 */ 240CTASSERT((((MSIZE - 1) ^ MSIZE) + 1) >> 1 == MSIZE); 241 242/* 243 * Initialize FreeBSD Network buffer allocation. 244 */ 245SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbuf_init, NULL); 246static void 247mbuf_init(void *dummy) 248{ 249 250 /* 251 * Configure UMA zones for Mbufs, Clusters, and Packets. 252 */ 253 zone_mbuf = uma_zcreate(MBUF_MEM_NAME, MSIZE, 254 mb_ctor_mbuf, mb_dtor_mbuf, 255#ifdef INVARIANTS 256 trash_init, trash_fini, 257#else 258 NULL, NULL, 259#endif 260 MSIZE - 1, UMA_ZONE_MAXBUCKET); 261 262 zone_clust = uma_zcreate(MBUF_CLUSTER_MEM_NAME, MCLBYTES, 263 mb_ctor_clust, mb_dtor_clust, 264#ifdef INVARIANTS 265 trash_init, trash_fini, 266#else 267 NULL, NULL, 268#endif 269 UMA_ALIGN_PTR, UMA_ZONE_REFCNT); 270 if (nmbclusters > 0) 271 uma_zone_set_max(zone_clust, nmbclusters); 272 273 zone_pack = uma_zsecond_create(MBUF_PACKET_MEM_NAME, mb_ctor_pack, 274 mb_dtor_pack, mb_zinit_pack, mb_zfini_pack, zone_mbuf); 275 276 /* Make jumbo frame zone too. Page size, 9k and 16k. */ 277 zone_jumbop = uma_zcreate(MBUF_JUMBOP_MEM_NAME, MJUMPAGESIZE, 278 mb_ctor_clust, mb_dtor_clust, 279#ifdef INVARIANTS 280 trash_init, trash_fini, 281#else 282 NULL, NULL, 283#endif 284 UMA_ALIGN_PTR, UMA_ZONE_REFCNT); 285 if (nmbjumbop > 0) 286 uma_zone_set_max(zone_jumbop, nmbjumbop); 287 288 zone_jumbo9 = uma_zcreate(MBUF_JUMBO9_MEM_NAME, MJUM9BYTES, 289 mb_ctor_clust, mb_dtor_clust, 290#ifdef INVARIANTS 291 trash_init, trash_fini, 292#else 293 NULL, NULL, 294#endif 295 UMA_ALIGN_PTR, UMA_ZONE_REFCNT); 296 if (nmbjumbo9 > 0) 297 uma_zone_set_max(zone_jumbo9, nmbjumbo9); 298 uma_zone_set_allocf(zone_jumbo9, mbuf_jumbo_alloc);
| 238/* Ensure that MSIZE doesn't break dtom() - it must be a power of 2 */ 239CTASSERT((((MSIZE - 1) ^ MSIZE) + 1) >> 1 == MSIZE); 240 241/* 242 * Initialize FreeBSD Network buffer allocation. 243 */ 244SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbuf_init, NULL); 245static void 246mbuf_init(void *dummy) 247{ 248 249 /* 250 * Configure UMA zones for Mbufs, Clusters, and Packets. 251 */ 252 zone_mbuf = uma_zcreate(MBUF_MEM_NAME, MSIZE, 253 mb_ctor_mbuf, mb_dtor_mbuf, 254#ifdef INVARIANTS 255 trash_init, trash_fini, 256#else 257 NULL, NULL, 258#endif 259 MSIZE - 1, UMA_ZONE_MAXBUCKET); 260 261 zone_clust = uma_zcreate(MBUF_CLUSTER_MEM_NAME, MCLBYTES, 262 mb_ctor_clust, mb_dtor_clust, 263#ifdef INVARIANTS 264 trash_init, trash_fini, 265#else 266 NULL, NULL, 267#endif 268 UMA_ALIGN_PTR, UMA_ZONE_REFCNT); 269 if (nmbclusters > 0) 270 uma_zone_set_max(zone_clust, nmbclusters); 271 272 zone_pack = uma_zsecond_create(MBUF_PACKET_MEM_NAME, mb_ctor_pack, 273 mb_dtor_pack, mb_zinit_pack, mb_zfini_pack, zone_mbuf); 274 275 /* Make jumbo frame zone too. Page size, 9k and 16k. */ 276 zone_jumbop = uma_zcreate(MBUF_JUMBOP_MEM_NAME, MJUMPAGESIZE, 277 mb_ctor_clust, mb_dtor_clust, 278#ifdef INVARIANTS 279 trash_init, trash_fini, 280#else 281 NULL, NULL, 282#endif 283 UMA_ALIGN_PTR, UMA_ZONE_REFCNT); 284 if (nmbjumbop > 0) 285 uma_zone_set_max(zone_jumbop, nmbjumbop); 286 287 zone_jumbo9 = uma_zcreate(MBUF_JUMBO9_MEM_NAME, MJUM9BYTES, 288 mb_ctor_clust, mb_dtor_clust, 289#ifdef INVARIANTS 290 trash_init, trash_fini, 291#else 292 NULL, NULL, 293#endif 294 UMA_ALIGN_PTR, UMA_ZONE_REFCNT); 295 if (nmbjumbo9 > 0) 296 uma_zone_set_max(zone_jumbo9, nmbjumbo9); 297 uma_zone_set_allocf(zone_jumbo9, mbuf_jumbo_alloc);
|
299 uma_zone_set_freef(zone_jumbo9, mbuf_jumbo_free);
| |
300 301 zone_jumbo16 = uma_zcreate(MBUF_JUMBO16_MEM_NAME, MJUM16BYTES, 302 mb_ctor_clust, mb_dtor_clust, 303#ifdef INVARIANTS 304 trash_init, trash_fini, 305#else 306 NULL, NULL, 307#endif 308 UMA_ALIGN_PTR, UMA_ZONE_REFCNT); 309 if (nmbjumbo16 > 0) 310 uma_zone_set_max(zone_jumbo16, nmbjumbo16); 311 uma_zone_set_allocf(zone_jumbo16, mbuf_jumbo_alloc);
| 298 299 zone_jumbo16 = uma_zcreate(MBUF_JUMBO16_MEM_NAME, MJUM16BYTES, 300 mb_ctor_clust, mb_dtor_clust, 301#ifdef INVARIANTS 302 trash_init, trash_fini, 303#else 304 NULL, NULL, 305#endif 306 UMA_ALIGN_PTR, UMA_ZONE_REFCNT); 307 if (nmbjumbo16 > 0) 308 uma_zone_set_max(zone_jumbo16, nmbjumbo16); 309 uma_zone_set_allocf(zone_jumbo16, mbuf_jumbo_alloc);
|
312 uma_zone_set_freef(zone_jumbo16, mbuf_jumbo_free);
| |
313 314 zone_ext_refcnt = uma_zcreate(MBUF_EXTREFCNT_MEM_NAME, sizeof(u_int), 315 NULL, NULL, 316 NULL, NULL, 317 UMA_ALIGN_PTR, UMA_ZONE_ZINIT); 318 319 /* uma_prealloc() goes here... */ 320 321 /* 322 * Hook event handler for low-memory situation, used to 323 * drain protocols and push data back to the caches (UMA 324 * later pushes it back to VM). 325 */ 326 EVENTHANDLER_REGISTER(vm_lowmem, mb_reclaim, NULL, 327 EVENTHANDLER_PRI_FIRST); 328 329 /* 330 * [Re]set counters and local statistics knobs. 331 * XXX Some of these should go and be replaced, but UMA stat 332 * gathering needs to be revised. 333 */ 334 mbstat.m_mbufs = 0; 335 mbstat.m_mclusts = 0; 336 mbstat.m_drain = 0; 337 mbstat.m_msize = MSIZE; 338 mbstat.m_mclbytes = MCLBYTES; 339 mbstat.m_minclsize = MINCLSIZE; 340 mbstat.m_mlen = MLEN; 341 mbstat.m_mhlen = MHLEN; 342 mbstat.m_numtypes = MT_NTYPES; 343 344 mbstat.m_mcfail = mbstat.m_mpfail = 0; 345 mbstat.sf_iocnt = 0; 346 mbstat.sf_allocwait = mbstat.sf_allocfail = 0; 347} 348 349/* 350 * UMA backend page allocator for the jumbo frame zones. 351 * 352 * Allocates kernel virtual memory that is backed by contiguous physical 353 * pages. 354 */ 355static void * 356mbuf_jumbo_alloc(uma_zone_t zone, int bytes, u_int8_t *flags, int wait) 357{ 358 359 /* Inform UMA that this allocator uses kernel_map/object. */ 360 *flags = UMA_SLAB_KERNEL;
| 310 311 zone_ext_refcnt = uma_zcreate(MBUF_EXTREFCNT_MEM_NAME, sizeof(u_int), 312 NULL, NULL, 313 NULL, NULL, 314 UMA_ALIGN_PTR, UMA_ZONE_ZINIT); 315 316 /* uma_prealloc() goes here... */ 317 318 /* 319 * Hook event handler for low-memory situation, used to 320 * drain protocols and push data back to the caches (UMA 321 * later pushes it back to VM). 322 */ 323 EVENTHANDLER_REGISTER(vm_lowmem, mb_reclaim, NULL, 324 EVENTHANDLER_PRI_FIRST); 325 326 /* 327 * [Re]set counters and local statistics knobs. 328 * XXX Some of these should go and be replaced, but UMA stat 329 * gathering needs to be revised. 330 */ 331 mbstat.m_mbufs = 0; 332 mbstat.m_mclusts = 0; 333 mbstat.m_drain = 0; 334 mbstat.m_msize = MSIZE; 335 mbstat.m_mclbytes = MCLBYTES; 336 mbstat.m_minclsize = MINCLSIZE; 337 mbstat.m_mlen = MLEN; 338 mbstat.m_mhlen = MHLEN; 339 mbstat.m_numtypes = MT_NTYPES; 340 341 mbstat.m_mcfail = mbstat.m_mpfail = 0; 342 mbstat.sf_iocnt = 0; 343 mbstat.sf_allocwait = mbstat.sf_allocfail = 0; 344} 345 346/* 347 * UMA backend page allocator for the jumbo frame zones. 348 * 349 * Allocates kernel virtual memory that is backed by contiguous physical 350 * pages. 351 */ 352static void * 353mbuf_jumbo_alloc(uma_zone_t zone, int bytes, u_int8_t *flags, int wait) 354{ 355 356 /* Inform UMA that this allocator uses kernel_map/object. */ 357 *flags = UMA_SLAB_KERNEL;
|
361 return (contigmalloc(bytes, M_JUMBOFRAME, wait, (vm_paddr_t)0, 362 ~(vm_paddr_t)0, 1, 0));
| 358 return ((void *)kmem_alloc_contig(kernel_map, bytes, wait, 359 (vm_paddr_t)0, ~(vm_paddr_t)0, 1, 0));
|
363} 364 365/*
| 360} 361 362/*
|
366 * UMA backend page deallocator for the jumbo frame zones. 367 */ 368static void 369mbuf_jumbo_free(void *mem, int size, u_int8_t flags) 370{ 371 372 contigfree(mem, size, M_JUMBOFRAME); 373} 374 375/*
| |
376 * Constructor for Mbuf master zone. 377 * 378 * The 'arg' pointer points to a mb_args structure which 379 * contains call-specific information required to support the 380 * mbuf allocation API. See mbuf.h. 381 */ 382static int 383mb_ctor_mbuf(void *mem, int size, void *arg, int how) 384{ 385 struct mbuf *m; 386 struct mb_args *args; 387#ifdef MAC 388 int error; 389#endif 390 int flags; 391 short type; 392 393#ifdef INVARIANTS 394 trash_ctor(mem, size, arg, how); 395#endif 396 m = (struct mbuf *)mem; 397 args = (struct mb_args *)arg; 398 flags = args->flags; 399 type = args->type; 400 401 /* 402 * The mbuf is initialized later. The caller has the 403 * responsibility to set up any MAC labels too. 404 */ 405 if (type == MT_NOINIT) 406 return (0); 407 408 m->m_next = NULL; 409 m->m_nextpkt = NULL; 410 m->m_len = 0; 411 m->m_flags = flags; 412 m->m_type = type; 413 if (flags & M_PKTHDR) { 414 m->m_data = m->m_pktdat; 415 m->m_pkthdr.rcvif = NULL; 416 m->m_pkthdr.header = NULL; 417 m->m_pkthdr.len = 0; 418 m->m_pkthdr.csum_flags = 0; 419 m->m_pkthdr.csum_data = 0; 420 m->m_pkthdr.tso_segsz = 0; 421 m->m_pkthdr.ether_vtag = 0; 422 m->m_pkthdr.flowid = 0; 423 SLIST_INIT(&m->m_pkthdr.tags); 424#ifdef MAC 425 /* If the label init fails, fail the alloc */ 426 error = mac_mbuf_init(m, how); 427 if (error) 428 return (error); 429#endif 430 } else 431 m->m_data = m->m_dat; 432 return (0); 433} 434 435/* 436 * The Mbuf master zone destructor. 437 */ 438static void 439mb_dtor_mbuf(void *mem, int size, void *arg) 440{ 441 struct mbuf *m; 442 unsigned long flags; 443 444 m = (struct mbuf *)mem; 445 flags = (unsigned long)arg; 446 447 if ((flags & MB_NOTAGS) == 0 && (m->m_flags & M_PKTHDR) != 0) 448 m_tag_delete_chain(m, NULL); 449 KASSERT((m->m_flags & M_EXT) == 0, ("%s: M_EXT set", __func__)); 450 KASSERT((m->m_flags & M_NOFREE) == 0, ("%s: M_NOFREE set", __func__)); 451#ifdef INVARIANTS 452 trash_dtor(mem, size, arg); 453#endif 454} 455 456/* 457 * The Mbuf Packet zone destructor. 458 */ 459static void 460mb_dtor_pack(void *mem, int size, void *arg) 461{ 462 struct mbuf *m; 463 464 m = (struct mbuf *)mem; 465 if ((m->m_flags & M_PKTHDR) != 0) 466 m_tag_delete_chain(m, NULL); 467 468 /* Make sure we've got a clean cluster back. */ 469 KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__)); 470 KASSERT(m->m_ext.ext_buf != NULL, ("%s: ext_buf == NULL", __func__)); 471 KASSERT(m->m_ext.ext_free == NULL, ("%s: ext_free != NULL", __func__)); 472 KASSERT(m->m_ext.ext_arg1 == NULL, ("%s: ext_arg1 != NULL", __func__)); 473 KASSERT(m->m_ext.ext_arg2 == NULL, ("%s: ext_arg2 != NULL", __func__)); 474 KASSERT(m->m_ext.ext_size == MCLBYTES, ("%s: ext_size != MCLBYTES", __func__)); 475 KASSERT(m->m_ext.ext_type == EXT_PACKET, ("%s: ext_type != EXT_PACKET", __func__)); 476 KASSERT(*m->m_ext.ref_cnt == 1, ("%s: ref_cnt != 1", __func__)); 477#ifdef INVARIANTS 478 trash_dtor(m->m_ext.ext_buf, MCLBYTES, arg); 479#endif 480 /* 481 * If there are processes blocked on zone_clust, waiting for pages 482 * to be freed up, * cause them to be woken up by draining the 483 * packet zone. We are exposed to a race here * (in the check for 484 * the UMA_ZFLAG_FULL) where we might miss the flag set, but that 485 * is deliberate. We don't want to acquire the zone lock for every 486 * mbuf free. 487 */ 488 if (uma_zone_exhausted_nolock(zone_clust)) 489 zone_drain(zone_pack); 490} 491 492/* 493 * The Cluster and Jumbo[PAGESIZE|9|16] zone constructor. 494 * 495 * Here the 'arg' pointer points to the Mbuf which we 496 * are configuring cluster storage for. If 'arg' is 497 * empty we allocate just the cluster without setting 498 * the mbuf to it. See mbuf.h. 499 */ 500static int 501mb_ctor_clust(void *mem, int size, void *arg, int how) 502{ 503 struct mbuf *m; 504 u_int *refcnt; 505 int type; 506 uma_zone_t zone; 507 508#ifdef INVARIANTS 509 trash_ctor(mem, size, arg, how); 510#endif 511 switch (size) { 512 case MCLBYTES: 513 type = EXT_CLUSTER; 514 zone = zone_clust; 515 break; 516#if MJUMPAGESIZE != MCLBYTES 517 case MJUMPAGESIZE: 518 type = EXT_JUMBOP; 519 zone = zone_jumbop; 520 break; 521#endif 522 case MJUM9BYTES: 523 type = EXT_JUMBO9; 524 zone = zone_jumbo9; 525 break; 526 case MJUM16BYTES: 527 type = EXT_JUMBO16; 528 zone = zone_jumbo16; 529 break; 530 default: 531 panic("unknown cluster size"); 532 break; 533 } 534 535 m = (struct mbuf *)arg; 536 refcnt = uma_find_refcnt(zone, mem); 537 *refcnt = 1; 538 if (m != NULL) { 539 m->m_ext.ext_buf = (caddr_t)mem; 540 m->m_data = m->m_ext.ext_buf; 541 m->m_flags |= M_EXT; 542 m->m_ext.ext_free = NULL; 543 m->m_ext.ext_arg1 = NULL; 544 m->m_ext.ext_arg2 = NULL; 545 m->m_ext.ext_size = size; 546 m->m_ext.ext_type = type; 547 m->m_ext.ref_cnt = refcnt; 548 } 549 550 return (0); 551} 552 553/* 554 * The Mbuf Cluster zone destructor. 555 */ 556static void 557mb_dtor_clust(void *mem, int size, void *arg) 558{ 559#ifdef INVARIANTS 560 uma_zone_t zone; 561 562 zone = m_getzone(size); 563 KASSERT(*(uma_find_refcnt(zone, mem)) <= 1, 564 ("%s: refcnt incorrect %u", __func__, 565 *(uma_find_refcnt(zone, mem))) ); 566 567 trash_dtor(mem, size, arg); 568#endif 569} 570 571/* 572 * The Packet secondary zone's init routine, executed on the 573 * object's transition from mbuf keg slab to zone cache. 574 */ 575static int 576mb_zinit_pack(void *mem, int size, int how) 577{ 578 struct mbuf *m; 579 580 m = (struct mbuf *)mem; /* m is virgin. */ 581 if (uma_zalloc_arg(zone_clust, m, how) == NULL || 582 m->m_ext.ext_buf == NULL) 583 return (ENOMEM); 584 m->m_ext.ext_type = EXT_PACKET; /* Override. */ 585#ifdef INVARIANTS 586 trash_init(m->m_ext.ext_buf, MCLBYTES, how); 587#endif 588 return (0); 589} 590 591/* 592 * The Packet secondary zone's fini routine, executed on the 593 * object's transition from zone cache to keg slab. 594 */ 595static void 596mb_zfini_pack(void *mem, int size) 597{ 598 struct mbuf *m; 599 600 m = (struct mbuf *)mem; 601#ifdef INVARIANTS 602 trash_fini(m->m_ext.ext_buf, MCLBYTES); 603#endif 604 uma_zfree_arg(zone_clust, m->m_ext.ext_buf, NULL); 605#ifdef INVARIANTS 606 trash_dtor(mem, size, NULL); 607#endif 608} 609 610/* 611 * The "packet" keg constructor. 612 */ 613static int 614mb_ctor_pack(void *mem, int size, void *arg, int how) 615{ 616 struct mbuf *m; 617 struct mb_args *args; 618#ifdef MAC 619 int error; 620#endif 621 int flags; 622 short type; 623 624 m = (struct mbuf *)mem; 625 args = (struct mb_args *)arg; 626 flags = args->flags; 627 type = args->type; 628 629#ifdef INVARIANTS 630 trash_ctor(m->m_ext.ext_buf, MCLBYTES, arg, how); 631#endif 632 m->m_next = NULL; 633 m->m_nextpkt = NULL; 634 m->m_data = m->m_ext.ext_buf; 635 m->m_len = 0; 636 m->m_flags = (flags | M_EXT); 637 m->m_type = type; 638 639 if (flags & M_PKTHDR) { 640 m->m_pkthdr.rcvif = NULL; 641 m->m_pkthdr.len = 0; 642 m->m_pkthdr.header = NULL; 643 m->m_pkthdr.csum_flags = 0; 644 m->m_pkthdr.csum_data = 0; 645 m->m_pkthdr.tso_segsz = 0; 646 m->m_pkthdr.ether_vtag = 0; 647 m->m_pkthdr.flowid = 0; 648 SLIST_INIT(&m->m_pkthdr.tags); 649#ifdef MAC 650 /* If the label init fails, fail the alloc */ 651 error = mac_mbuf_init(m, how); 652 if (error) 653 return (error); 654#endif 655 } 656 /* m_ext is already initialized. */ 657 658 return (0); 659} 660 661/* 662 * This is the protocol drain routine. 663 * 664 * No locks should be held when this is called. The drain routines have to 665 * presently acquire some locks which raises the possibility of lock order 666 * reversal. 667 */ 668static void 669mb_reclaim(void *junk) 670{ 671 struct domain *dp; 672 struct protosw *pr; 673 674 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK | WARN_PANIC, NULL, 675 "mb_reclaim()"); 676 677 for (dp = domains; dp != NULL; dp = dp->dom_next) 678 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 679 if (pr->pr_drain != NULL) 680 (*pr->pr_drain)(); 681}
| 363 * Constructor for Mbuf master zone. 364 * 365 * The 'arg' pointer points to a mb_args structure which 366 * contains call-specific information required to support the 367 * mbuf allocation API. See mbuf.h. 368 */ 369static int 370mb_ctor_mbuf(void *mem, int size, void *arg, int how) 371{ 372 struct mbuf *m; 373 struct mb_args *args; 374#ifdef MAC 375 int error; 376#endif 377 int flags; 378 short type; 379 380#ifdef INVARIANTS 381 trash_ctor(mem, size, arg, how); 382#endif 383 m = (struct mbuf *)mem; 384 args = (struct mb_args *)arg; 385 flags = args->flags; 386 type = args->type; 387 388 /* 389 * The mbuf is initialized later. The caller has the 390 * responsibility to set up any MAC labels too. 391 */ 392 if (type == MT_NOINIT) 393 return (0); 394 395 m->m_next = NULL; 396 m->m_nextpkt = NULL; 397 m->m_len = 0; 398 m->m_flags = flags; 399 m->m_type = type; 400 if (flags & M_PKTHDR) { 401 m->m_data = m->m_pktdat; 402 m->m_pkthdr.rcvif = NULL; 403 m->m_pkthdr.header = NULL; 404 m->m_pkthdr.len = 0; 405 m->m_pkthdr.csum_flags = 0; 406 m->m_pkthdr.csum_data = 0; 407 m->m_pkthdr.tso_segsz = 0; 408 m->m_pkthdr.ether_vtag = 0; 409 m->m_pkthdr.flowid = 0; 410 SLIST_INIT(&m->m_pkthdr.tags); 411#ifdef MAC 412 /* If the label init fails, fail the alloc */ 413 error = mac_mbuf_init(m, how); 414 if (error) 415 return (error); 416#endif 417 } else 418 m->m_data = m->m_dat; 419 return (0); 420} 421 422/* 423 * The Mbuf master zone destructor. 424 */ 425static void 426mb_dtor_mbuf(void *mem, int size, void *arg) 427{ 428 struct mbuf *m; 429 unsigned long flags; 430 431 m = (struct mbuf *)mem; 432 flags = (unsigned long)arg; 433 434 if ((flags & MB_NOTAGS) == 0 && (m->m_flags & M_PKTHDR) != 0) 435 m_tag_delete_chain(m, NULL); 436 KASSERT((m->m_flags & M_EXT) == 0, ("%s: M_EXT set", __func__)); 437 KASSERT((m->m_flags & M_NOFREE) == 0, ("%s: M_NOFREE set", __func__)); 438#ifdef INVARIANTS 439 trash_dtor(mem, size, arg); 440#endif 441} 442 443/* 444 * The Mbuf Packet zone destructor. 445 */ 446static void 447mb_dtor_pack(void *mem, int size, void *arg) 448{ 449 struct mbuf *m; 450 451 m = (struct mbuf *)mem; 452 if ((m->m_flags & M_PKTHDR) != 0) 453 m_tag_delete_chain(m, NULL); 454 455 /* Make sure we've got a clean cluster back. */ 456 KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__)); 457 KASSERT(m->m_ext.ext_buf != NULL, ("%s: ext_buf == NULL", __func__)); 458 KASSERT(m->m_ext.ext_free == NULL, ("%s: ext_free != NULL", __func__)); 459 KASSERT(m->m_ext.ext_arg1 == NULL, ("%s: ext_arg1 != NULL", __func__)); 460 KASSERT(m->m_ext.ext_arg2 == NULL, ("%s: ext_arg2 != NULL", __func__)); 461 KASSERT(m->m_ext.ext_size == MCLBYTES, ("%s: ext_size != MCLBYTES", __func__)); 462 KASSERT(m->m_ext.ext_type == EXT_PACKET, ("%s: ext_type != EXT_PACKET", __func__)); 463 KASSERT(*m->m_ext.ref_cnt == 1, ("%s: ref_cnt != 1", __func__)); 464#ifdef INVARIANTS 465 trash_dtor(m->m_ext.ext_buf, MCLBYTES, arg); 466#endif 467 /* 468 * If there are processes blocked on zone_clust, waiting for pages 469 * to be freed up, * cause them to be woken up by draining the 470 * packet zone. We are exposed to a race here * (in the check for 471 * the UMA_ZFLAG_FULL) where we might miss the flag set, but that 472 * is deliberate. We don't want to acquire the zone lock for every 473 * mbuf free. 474 */ 475 if (uma_zone_exhausted_nolock(zone_clust)) 476 zone_drain(zone_pack); 477} 478 479/* 480 * The Cluster and Jumbo[PAGESIZE|9|16] zone constructor. 481 * 482 * Here the 'arg' pointer points to the Mbuf which we 483 * are configuring cluster storage for. If 'arg' is 484 * empty we allocate just the cluster without setting 485 * the mbuf to it. See mbuf.h. 486 */ 487static int 488mb_ctor_clust(void *mem, int size, void *arg, int how) 489{ 490 struct mbuf *m; 491 u_int *refcnt; 492 int type; 493 uma_zone_t zone; 494 495#ifdef INVARIANTS 496 trash_ctor(mem, size, arg, how); 497#endif 498 switch (size) { 499 case MCLBYTES: 500 type = EXT_CLUSTER; 501 zone = zone_clust; 502 break; 503#if MJUMPAGESIZE != MCLBYTES 504 case MJUMPAGESIZE: 505 type = EXT_JUMBOP; 506 zone = zone_jumbop; 507 break; 508#endif 509 case MJUM9BYTES: 510 type = EXT_JUMBO9; 511 zone = zone_jumbo9; 512 break; 513 case MJUM16BYTES: 514 type = EXT_JUMBO16; 515 zone = zone_jumbo16; 516 break; 517 default: 518 panic("unknown cluster size"); 519 break; 520 } 521 522 m = (struct mbuf *)arg; 523 refcnt = uma_find_refcnt(zone, mem); 524 *refcnt = 1; 525 if (m != NULL) { 526 m->m_ext.ext_buf = (caddr_t)mem; 527 m->m_data = m->m_ext.ext_buf; 528 m->m_flags |= M_EXT; 529 m->m_ext.ext_free = NULL; 530 m->m_ext.ext_arg1 = NULL; 531 m->m_ext.ext_arg2 = NULL; 532 m->m_ext.ext_size = size; 533 m->m_ext.ext_type = type; 534 m->m_ext.ref_cnt = refcnt; 535 } 536 537 return (0); 538} 539 540/* 541 * The Mbuf Cluster zone destructor. 542 */ 543static void 544mb_dtor_clust(void *mem, int size, void *arg) 545{ 546#ifdef INVARIANTS 547 uma_zone_t zone; 548 549 zone = m_getzone(size); 550 KASSERT(*(uma_find_refcnt(zone, mem)) <= 1, 551 ("%s: refcnt incorrect %u", __func__, 552 *(uma_find_refcnt(zone, mem))) ); 553 554 trash_dtor(mem, size, arg); 555#endif 556} 557 558/* 559 * The Packet secondary zone's init routine, executed on the 560 * object's transition from mbuf keg slab to zone cache. 561 */ 562static int 563mb_zinit_pack(void *mem, int size, int how) 564{ 565 struct mbuf *m; 566 567 m = (struct mbuf *)mem; /* m is virgin. */ 568 if (uma_zalloc_arg(zone_clust, m, how) == NULL || 569 m->m_ext.ext_buf == NULL) 570 return (ENOMEM); 571 m->m_ext.ext_type = EXT_PACKET; /* Override. */ 572#ifdef INVARIANTS 573 trash_init(m->m_ext.ext_buf, MCLBYTES, how); 574#endif 575 return (0); 576} 577 578/* 579 * The Packet secondary zone's fini routine, executed on the 580 * object's transition from zone cache to keg slab. 581 */ 582static void 583mb_zfini_pack(void *mem, int size) 584{ 585 struct mbuf *m; 586 587 m = (struct mbuf *)mem; 588#ifdef INVARIANTS 589 trash_fini(m->m_ext.ext_buf, MCLBYTES); 590#endif 591 uma_zfree_arg(zone_clust, m->m_ext.ext_buf, NULL); 592#ifdef INVARIANTS 593 trash_dtor(mem, size, NULL); 594#endif 595} 596 597/* 598 * The "packet" keg constructor. 599 */ 600static int 601mb_ctor_pack(void *mem, int size, void *arg, int how) 602{ 603 struct mbuf *m; 604 struct mb_args *args; 605#ifdef MAC 606 int error; 607#endif 608 int flags; 609 short type; 610 611 m = (struct mbuf *)mem; 612 args = (struct mb_args *)arg; 613 flags = args->flags; 614 type = args->type; 615 616#ifdef INVARIANTS 617 trash_ctor(m->m_ext.ext_buf, MCLBYTES, arg, how); 618#endif 619 m->m_next = NULL; 620 m->m_nextpkt = NULL; 621 m->m_data = m->m_ext.ext_buf; 622 m->m_len = 0; 623 m->m_flags = (flags | M_EXT); 624 m->m_type = type; 625 626 if (flags & M_PKTHDR) { 627 m->m_pkthdr.rcvif = NULL; 628 m->m_pkthdr.len = 0; 629 m->m_pkthdr.header = NULL; 630 m->m_pkthdr.csum_flags = 0; 631 m->m_pkthdr.csum_data = 0; 632 m->m_pkthdr.tso_segsz = 0; 633 m->m_pkthdr.ether_vtag = 0; 634 m->m_pkthdr.flowid = 0; 635 SLIST_INIT(&m->m_pkthdr.tags); 636#ifdef MAC 637 /* If the label init fails, fail the alloc */ 638 error = mac_mbuf_init(m, how); 639 if (error) 640 return (error); 641#endif 642 } 643 /* m_ext is already initialized. */ 644 645 return (0); 646} 647 648/* 649 * This is the protocol drain routine. 650 * 651 * No locks should be held when this is called. The drain routines have to 652 * presently acquire some locks which raises the possibility of lock order 653 * reversal. 654 */ 655static void 656mb_reclaim(void *junk) 657{ 658 struct domain *dp; 659 struct protosw *pr; 660 661 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK | WARN_PANIC, NULL, 662 "mb_reclaim()"); 663 664 for (dp = domains; dp != NULL; dp = dp->dom_next) 665 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 666 if (pr->pr_drain != NULL) 667 (*pr->pr_drain)(); 668}
|