kern_mbuf.c revision 129947
1/*- 2 * Copyright (c) 2004 3 * Bosko Milekic <bmilekic@FreeBSD.org>. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice unmodified, this list of conditions and the following 11 * disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the author nor the names of contributors may be 16 * used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32#include <sys/cdefs.h> 33__FBSDID("$FreeBSD: head/sys/kern/kern_mbuf.c 129947 2004-06-01 16:17:10Z bmilekic $"); 34 35#include "opt_mac.h" 36#include "opt_param.h" 37 38#include <sys/param.h> 39#include <sys/mac.h> 40#include <sys/malloc.h> 41#include <sys/systm.h> 42#include <sys/mbuf.h> 43#include <sys/domain.h> 44#include <sys/eventhandler.h> 45#include <sys/kernel.h> 46#include <sys/protosw.h> 47#include <sys/smp.h> 48#include <sys/sysctl.h> 49 50#include <vm/vm.h> 51#include <vm/vm_page.h> 52#include <vm/uma.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 85int nmbclusters; 86struct mbstat mbstat; 87 88static void 89tunable_mbinit(void *dummy) 90{ 91 92 /* This has to be done before VM init. */ 93 nmbclusters = 1024 + maxusers * 64; 94 TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters); 95} 96SYSINIT(tunable_mbinit, SI_SUB_TUNABLES, SI_ORDER_ANY, tunable_mbinit, NULL); 97 98SYSCTL_DECL(_kern_ipc); 99SYSCTL_INT(_kern_ipc, OID_AUTO, nmbclusters, CTLFLAG_RW, &nmbclusters, 0, 100 "Maximum number of mbuf clusters allowed"); 101SYSCTL_STRUCT(_kern_ipc, OID_AUTO, mbstat, CTLFLAG_RD, &mbstat, mbstat, 102 "Mbuf general information and statistics"); 103 104/* 105 * Zones from which we allocate. 106 */ 107uma_zone_t zone_mbuf; 108uma_zone_t zone_clust; 109uma_zone_t zone_pack; 110 111/* 112 * Local prototypes. 113 */ 114static void mb_ctor_mbuf(void *, int, void *); 115static void mb_ctor_clust(void *, int, void *); 116static void mb_ctor_pack(void *, int, void *); 117static void mb_dtor_mbuf(void *, int, void *); 118static void mb_dtor_clust(void *, int, void *); /* XXX */ 119static void mb_dtor_pack(void *, int, void *); /* XXX */ 120static void mb_init_pack(void *, int); 121static void mb_fini_pack(void *, int); 122 123static void mb_reclaim(void *); 124static void mbuf_init(void *); 125 126/* 127 * Initialize FreeBSD Network buffer allocation. 128 */ 129SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbuf_init, NULL) 130static void 131mbuf_init(void *dummy) 132{ 133 134 /* 135 * Configure UMA zones for Mbufs, Clusters, and Packets. 136 */ 137 zone_mbuf = uma_zcreate("Mbuf", MSIZE, mb_ctor_mbuf, mb_dtor_mbuf, 138 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_MAXBUCKET); 139 zone_clust = uma_zcreate("MbufClust", MCLBYTES, mb_ctor_clust, 140 mb_dtor_clust, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_REFCNT); 141 if (nmbclusters > 0) 142 uma_zone_set_max(zone_clust, nmbclusters); 143 zone_pack = uma_zsecond_create("Packet", mb_ctor_pack, mb_dtor_pack, 144 mb_init_pack, mb_fini_pack, zone_mbuf); 145 146 /* uma_prealloc() goes here */ 147 148 /* 149 * Hook event handler for low-memory situation, used to 150 * drain protocols and push data back to the caches (UMA 151 * later pushes it back to VM). 152 */ 153 EVENTHANDLER_REGISTER(vm_lowmem, mb_reclaim, NULL, 154 EVENTHANDLER_PRI_FIRST); 155 156 /* 157 * [Re]set counters and local statistics knobs. 158 * XXX Some of these should go and be replaced, but UMA stat 159 * gathering needs to be revised. 160 */ 161 mbstat.m_mbufs = 0; 162 mbstat.m_mclusts = 0; 163 mbstat.m_drain = 0; 164 mbstat.m_msize = MSIZE; 165 mbstat.m_mclbytes = MCLBYTES; 166 mbstat.m_minclsize = MINCLSIZE; 167 mbstat.m_mlen = MLEN; 168 mbstat.m_mhlen = MHLEN; 169 mbstat.m_numtypes = MT_NTYPES; 170 171 mbstat.m_mcfail = mbstat.m_mpfail = 0; 172 mbstat.sf_iocnt = 0; 173 mbstat.sf_allocwait = mbstat.sf_allocfail = 0; 174} 175 176/* 177 * Constructor for Mbuf master zone. 178 * 179 * The 'arg' pointer points to a mb_args structure which 180 * contains call-specific information required to support the 181 * mbuf allocation API. 182 */ 183static void 184mb_ctor_mbuf(void *mem, int size, void *arg) 185{ 186 struct mbuf *m; 187 struct mb_args *args; 188 int flags; 189 int how; 190 short type; 191 192 m = (struct mbuf *)mem; 193 args = (struct mb_args *)arg; 194 flags = args->flags; 195 how = args->how; 196 type = args->type; 197 198 m->m_type = type; 199 m->m_next = NULL; 200 m->m_nextpkt = NULL; 201 m->m_flags = flags; 202 if (flags & M_PKTHDR) { 203 m->m_data = m->m_pktdat; 204 m->m_pkthdr.rcvif = NULL; 205 m->m_pkthdr.csum_flags = 0; 206 SLIST_INIT(&m->m_pkthdr.tags); 207#ifdef MAC 208 /* If the label init fails, fail the alloc */ 209 if (mac_init_mbuf(m, how) != 0) { 210 m_free(m); 211/* XXX*/ panic("mb_ctor_mbuf(): can't deal with failure!"); 212/* return 0; */ 213 } 214#endif 215 } else 216 m->m_data = m->m_dat; 217 mbstat.m_mbufs += 1; /* XXX */ 218/* return 1; 219*/ 220} 221 222/* 223 * The Mbuf master zone and Packet secondary zone destructor. 224 */ 225static void 226mb_dtor_mbuf(void *mem, int size, void *arg) 227{ 228 struct mbuf *m; 229 230 m = (struct mbuf *)mem; 231 if ((m->m_flags & M_PKTHDR) != 0) 232 m_tag_delete_chain(m, NULL); 233 mbstat.m_mbufs -= 1; /* XXX */ 234} 235 236/* XXX Only because of stats */ 237static void 238mb_dtor_pack(void *mem, int size, void *arg) 239{ 240 struct mbuf *m; 241 242 m = (struct mbuf *)mem; 243 if ((m->m_flags & M_PKTHDR) != 0) 244 m_tag_delete_chain(m, NULL); 245 mbstat.m_mbufs -= 1; /* XXX */ 246 mbstat.m_mclusts -= 1; /* XXX */ 247} 248 249/* 250 * The Cluster zone constructor. 251 * 252 * Here the 'arg' pointer points to the Mbuf which we 253 * are configuring cluster storage for. 254 */ 255static void 256mb_ctor_clust(void *mem, int size, void *arg) 257{ 258 struct mbuf *m; 259 260 m = (struct mbuf *)arg; 261 m->m_ext.ext_buf = (caddr_t)mem; 262 m->m_data = m->m_ext.ext_buf; 263 m->m_flags |= M_EXT; 264 m->m_ext.ext_free = NULL; 265 m->m_ext.ext_args = NULL; 266 m->m_ext.ext_size = MCLBYTES; 267 m->m_ext.ext_type = EXT_CLUSTER; 268 m->m_ext.ref_cnt = (u_int *)uma_find_refcnt(zone_clust, 269 m->m_ext.ext_buf); 270 *(m->m_ext.ref_cnt) = 1; 271 mbstat.m_mclusts += 1; /* XXX */ 272/* return 1; 273*/ 274} 275 276/* XXX */ 277static void 278mb_dtor_clust(void *mem, int size, void *arg) 279{ 280 mbstat.m_mclusts -= 1; /* XXX */ 281} 282 283/* 284 * The Packet secondary zone's init routine, executed on the 285 * object's transition from keg slab to zone cache. 286 */ 287static void 288mb_init_pack(void *mem, int size) 289{ 290 struct mbuf *m; 291 292 m = (struct mbuf *)mem; 293 m->m_ext.ext_buf = NULL; 294 uma_zalloc_arg(zone_clust, m, M_NOWAIT); 295 if (m->m_ext.ext_buf == NULL) /* XXX */ 296 panic("mb_init_pack(): Can't deal with failure yet."); 297 mbstat.m_mclusts -= 1; /* XXX */ 298} 299 300/* 301 * The Packet secondary zone's fini routine, executed on the 302 * object's transition from zone cache to keg slab. 303 */ 304static void 305mb_fini_pack(void *mem, int size) 306{ 307 struct mbuf *m; 308 309 m = (struct mbuf *)mem; 310 uma_zfree_arg(zone_clust, m->m_ext.ext_buf, NULL); 311 m->m_ext.ext_buf = NULL; 312 mbstat.m_mclusts += 1; /* XXX */ 313} 314 315/* 316 * The "packet" keg constructor. 317 */ 318static void 319mb_ctor_pack(void *mem, int size, void *arg) 320{ 321 struct mbuf *m; 322 struct mb_args *args; 323 int flags, how; 324 short type; 325 326 m = (struct mbuf *)mem; 327 args = (struct mb_args *)arg; 328 flags = args->flags; 329 type = args->type; 330 how = args->how; 331 332 m->m_type = type; 333 m->m_next = NULL; 334 m->m_nextpkt = NULL; 335 m->m_data = m->m_ext.ext_buf; 336 m->m_flags = flags|M_EXT; 337 m->m_ext.ext_free = NULL; 338 m->m_ext.ext_args = NULL; 339 m->m_ext.ext_size = MCLBYTES; 340 m->m_ext.ext_type = EXT_PACKET; 341 *(m->m_ext.ref_cnt) = 1; 342 343 if (flags & M_PKTHDR) { 344 m->m_pkthdr.rcvif = NULL; 345 m->m_pkthdr.csum_flags = 0; 346 SLIST_INIT(&m->m_pkthdr.tags); 347#ifdef MAC 348 /* If the label init fails, fail the alloc */ 349 if (mac_init_mbuf(m, how) != 0) { 350 m_free(m); 351/* XXX*/ panic("mb_ctor_pack(): can't deal with failure!"); 352/* return 0; */ 353 } 354#endif 355 } 356 mbstat.m_mbufs += 1; /* XXX */ 357 mbstat.m_mclusts += 1; /* XXX */ 358/* return 1; 359*/ 360} 361 362/* 363 * This is the protocol drain routine. 364 * 365 * No locks should be held when this is called. The drain routines have to 366 * presently acquire some locks which raises the possibility of lock order 367 * reversal. 368 */ 369static void 370mb_reclaim(void *junk) 371{ 372 struct domain *dp; 373 struct protosw *pr; 374 375 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK | WARN_PANIC, NULL, 376 "mb_reclaim()"); 377 378 mbstat.m_drain++; 379 for (dp = domains; dp != NULL; dp = dp->dom_next) 380 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 381 if (pr->pr_drain != NULL) 382 (*pr->pr_drain)(); 383} 384