The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_mbuf.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: b96a076be4a43bc112781ab694a3a5c2


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.