/*-
* Copyright (c) 2004, 2005,
* Bosko Milekic <bmilekic@FreeBSD.org>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice unmodified, this list of conditions and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef VBOX
__FBSDID("$FreeBSD: src/sys/kern/kern_mbuf.c,v 1.32.2.5.2.1 2009/04/15 03:14:26 kensmith Exp $");
#include "opt_mac.h"
#include "opt_param.h"
#include <sys/eventhandler.h>
#else
# include <slirp.h>
# define IN_BSD
# include "ext.h"
#endif
/*
* In FreeBSD, Mbufs and Mbuf Clusters are allocated from UMA
* Zones.
*
* Mbuf Clusters (2K, contiguous) are allocated from the Cluster
* Zone. The Zone can be capped at kern.ipc.nmbclusters, if the
* administrator so desires.
*
* Mbufs are allocated from a UMA Master Zone called the Mbuf
* Zone.
*
* Additionally, FreeBSD provides a Packet Zone, which it
* configures as a Secondary Zone to the Mbuf Master Zone,
* thus sharing backend Slab kegs with the Mbuf Master Zone.
*
* Thus common-case allocations and locking are simplified:
*
* m_clget() m_getcl()
* | |
* | .------------>[(Packet Cache)] m_get(), m_gethdr()
* | | [ Packet ] |
* [(Cluster Cache)] [ Secondary ] [ (Mbuf Cache) ]
* [ Cluster Zone ] [ Zone ] [ Mbuf Master Zone ]
* | \________ |
* [ Cluster Keg ] \ /
* | [ Mbuf Keg ]
* [ Cluster Slabs ] |
* | [ Mbuf Slabs ]
* \____________(VM)_________________/
*
*
* Whenever an object is allocated with uma_zalloc() out of
* one of the Zones its _ctor_ function is executed. The same
* for any deallocation through uma_zfree() the _dtor_ function
* is executed.
*
* Caches are per-CPU and are filled from the Master Zone.
*
* Whenever an object is allocated from the underlying global
* memory pool it gets pre-initialized with the _zinit_ functions.
* When the Keg's are overfull objects get decomissioned with
* _zfini_ functions and free'd back to the global memory pool.
*
*/
#ifndef VBOX
#endif
/*
* tunable_mbinit() has to be run before init_maxsockets() thus
* the SYSINIT order below is SI_ORDER_MIDDLE while init_maxsockets()
* runs at SI_ORDER_ANY.
*/
static void
{
#ifdef VBOX
#endif
/* This has to be done before VM init. */
}
#ifndef VBOX
/* XXX: These should be tuneables. Can't change UMA limits on the fly. */
static int
{
if (newnmbclusters > nmbclusters) {
} else
}
return (error);
}
"Maximum number of mbuf clusters allowed");
static int
{
if (newnmbjumbop> nmbjumbop) {
} else
}
return (error);
}
"Maximum number of mbuf page size jumbo clusters allowed");
static int
{
if (newnmbjumbo9> nmbjumbo9) {
} else
}
return (error);
}
"Maximum number of mbuf 9k jumbo clusters allowed");
static int
{
if (newnmbjumbo16> nmbjumbo16) {
} else
}
return (error);
}
"Maximum number of mbuf 16k jumbo clusters allowed");
"Mbuf general information and statistics");
/*
* Zones from which we allocate.
*/
/*
* Local prototypes.
*/
static int mb_ctor_mbuf(void *, int, void *, int);
static int mb_ctor_clust(void *, int, void *, int);
static int mb_ctor_pack(void *, int, void *, int);
static void mb_dtor_mbuf(void *, int, void *);
static void mb_dtor_clust(void *, int, void *);
static void mb_dtor_pack(void *, int, void *);
static int mb_zinit_pack(void *, int, int);
static void mb_zfini_pack(void *, int);
#else
/*
* Local prototypes.
*/
static int mb_ctor_mbuf(PNATState, void *, int, void *, int);
static int mb_ctor_clust(PNATState, void *, int, void *, int);
static int mb_ctor_pack(PNATState, void *, int, void *, int);
static void mb_dtor_mbuf(PNATState, void *, int, void *);
static void mb_dtor_clust(PNATState, void *, int, void *);
static void mb_dtor_pack(PNATState, void *, int, void *);
static int mb_zinit_pack(PNATState, void *, int, int);
static void mb_zfini_pack(PNATState, void *, int);
#endif
static void mb_reclaim(void *);
#ifndef VBOX
static void mbuf_init(void *);
static void mbuf_jumbo_free(void *, int, u_int8_t);
#endif
#ifndef VBOX
/* Ensure that MSIZE doesn't break dtom() - it must be a power of 2 */
#else
#endif
/*
* Initialize FreeBSD Network buffer allocation.
*/
#ifndef VBOX
static void
#else
void
#endif
{
/*
* Configure UMA zones for Mbufs, Clusters, and Packets.
*/
#ifndef VBOX
#ifdef INVARIANTS
#else
#endif
#ifdef INVARIANTS
#else
#endif
#else /*!VBOX*/
if (nmbclusters > 0)
#endif /*VBOX*/
if (nmbclusters > 0)
/* Make jumbo frame zone too. Page size, 9k and 16k. */
#ifndef VBOX
#ifdef INVARIANTS
#else
#endif
if (nmbjumbop > 0)
#ifdef INVARIANTS
#else
#endif
if (nmbjumbo9 > 0)
#ifdef INVARIANTS
#else
#endif
if (nmbjumbo16 > 0)
#else /*!VBOX*/
if (nmbjumbop > 0)
if (nmbjumbo9 > 0)
if (nmbjumbo16 > 0)
#endif /*VBOX*/
/* uma_prealloc() goes here... */
/*
* Hook event handler for low-memory situation, used to
* drain protocols and push data back to the caches (UMA
* later pushes it back to VM).
*/
/*
* [Re]set counters and local statistics knobs.
* XXX Some of these should go and be replaced, but UMA stat
* gathering needs to be revised.
*/
}
#ifndef VBOX
/*
* UMA backend page allocator for the jumbo frame zones.
*
* Allocates kernel virtual memory that is backed by contiguous physical
* pages.
*/
static void *
{
/* Inform UMA that this allocator uses kernel_map/object. */
*flags = UMA_SLAB_KERNEL;
~(vm_paddr_t)0, 1, 0));
}
/*
* UMA backend page deallocator for the jumbo frame zones.
*/
static void
{
}
#endif
/*
* Constructor for Mbuf master zone.
*
* The 'arg' pointer points to a mb_args structure which
* contains call-specific information required to support the
* mbuf allocation API. See mbuf.h.
*/
static int
#ifndef VBOX
#else
#endif
{
struct mbuf *m;
#ifdef MAC
int error;
#endif
int flags;
short type;
#ifdef VBOX
#endif
#ifdef INVARIANTS
#endif
/*
* The mbuf is initialized later. The caller has the
* responsibility to set up any MAC labels too.
*/
return (0);
m->m_len = 0;
m->m_pkthdr.csum_flags = 0;
m->m_pkthdr.ether_vtag = 0;
#ifdef MAC
/* If the label init fails, fail the alloc */
if (error)
return (error);
#endif
} else
return (0);
}
/*
* The Mbuf master zone destructor.
*/
static void
#ifndef VBOX
#else
#endif
{
struct mbuf *m;
unsigned long flags;
#ifdef VBOX
#endif
m_tag_delete_chain(m, NULL);
#ifdef INVARIANTS
#endif
}
/*
* The Mbuf Packet zone destructor.
*/
static void
#ifndef VBOX
#else
#endif
{
struct mbuf *m;
m_tag_delete_chain(m, NULL);
/* Make sure we've got a clean cluster back. */
#ifdef INVARIANTS
#endif
/*
* If there are processes blocked on zone_clust, waiting for pages to be freed up,
* cause them to be woken up by draining the packet zone. We are exposed to a race here
* (in the check for the UMA_ZFLAG_FULL) where we might miss the flag set, but that is
* deliberate. We don't want to acquire the zone lock for every mbuf free.
*/
}
/*
* The Cluster and Jumbo[PAGESIZE|9|16] zone constructor.
*
* Here the 'arg' pointer points to the Mbuf which we
* are configuring cluster storage for. If 'arg' is
* empty we allocate just the cluster without setting
* the mbuf to it. See mbuf.h.
*/
static int
#ifndef VBOX
#else
#endif
{
struct mbuf *m;
int type;
#ifdef VBOX
#endif
#ifdef INVARIANTS
#endif
switch (size) {
case MCLBYTES:
type = EXT_CLUSTER;
zone = zone_clust;
break;
#if MJUMPAGESIZE != MCLBYTES
case MJUMPAGESIZE:
type = EXT_JUMBOP;
zone = zone_jumbop;
break;
#endif
case MJUM9BYTES:
type = EXT_JUMBO9;
zone = zone_jumbo9;
break;
case MJUM16BYTES:
type = EXT_JUMBO16;
zone = zone_jumbo16;
break;
default:
panic("unknown cluster size");
break;
}
*refcnt = 1;
if (m != NULL) {
}
return (0);
}
/*
* The Mbuf Cluster zone destructor.
*/
static void
#ifndef VBOX
#else
#endif
{
#ifdef INVARIANTS
#endif
}
/*
* The Packet secondary zone's init routine, executed on the
* object's transition from mbuf keg slab to zone cache.
*/
static int
#ifndef VBOX
#else
#endif
{
struct mbuf *m;
return (ENOMEM);
#ifdef INVARIANTS
#endif
return (0);
}
/*
* The Packet secondary zone's fini routine, executed on the
* object's transition from zone cache to keg slab.
*/
static void
#ifndef VBOX
#else
#endif
{
struct mbuf *m;
#ifdef INVARIANTS
#endif
#ifdef INVARIANTS
#endif
}
/*
* The "packet" keg constructor.
*/
static int
#ifndef VBOX
#else
#endif
{
struct mbuf *m;
#ifdef MAC
int error;
#endif
int flags;
short type;
#ifdef VBOX
#endif
#ifdef INVARIANTS
#endif
m->m_len = 0;
m->m_pkthdr.csum_flags = 0;
m->m_pkthdr.ether_vtag = 0;
#ifdef MAC
/* If the label init fails, fail the alloc */
if (error)
return (error);
#endif
}
/* m_ext is already initialized. */
return (0);
}
/*
* This is the protocol drain routine.
*
* No locks should be held when this is called. The drain routines have to
* presently acquire some locks which raises the possibility of lock order
* reversal.
*/
static void
{
#ifndef VBOX
"mb_reclaim()");
#else
#endif
}