Use subr_unit allocator instead of own functions.
This commit is contained in:
parent
fca732dc73
commit
afb4ea8d90
@ -61,10 +61,8 @@
|
||||
#include <sys/systm.h>
|
||||
#include <sys/errno.h>
|
||||
#include <sys/kernel.h>
|
||||
#include <sys/lock.h>
|
||||
#include <sys/malloc.h>
|
||||
#include <sys/mbuf.h>
|
||||
#include <sys/mutex.h>
|
||||
#include <sys/errno.h>
|
||||
#include <sys/random.h>
|
||||
#include <sys/sockio.h>
|
||||
@ -207,15 +205,7 @@ static struct ng_type typestruct = {
|
||||
};
|
||||
NETGRAPH_INIT(iface, &typestruct);
|
||||
|
||||
/* We keep a bitmap indicating which unit numbers are free.
|
||||
One means the unit number is free, zero means it's taken. */
|
||||
static int *ng_iface_units = NULL;
|
||||
static int ng_iface_units_len = 0;
|
||||
static int ng_units_in_use = 0;
|
||||
|
||||
#define UNITS_BITSPERWORD (sizeof(*ng_iface_units) * NBBY)
|
||||
|
||||
static struct mtx ng_iface_mtx;
|
||||
static struct unrhdr *ng_iface_unit;
|
||||
|
||||
/************************************************************************
|
||||
HELPER STUFF
|
||||
@ -279,78 +269,6 @@ get_iffam_from_name(const char *name)
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* Find the first free unit number for a new interface.
|
||||
* Increase the size of the unit bitmap as necessary.
|
||||
*/
|
||||
static __inline int
|
||||
ng_iface_get_unit(int *unit)
|
||||
{
|
||||
int index, bit;
|
||||
|
||||
mtx_lock(&ng_iface_mtx);
|
||||
for (index = 0; index < ng_iface_units_len
|
||||
&& ng_iface_units[index] == 0; index++);
|
||||
if (index == ng_iface_units_len) { /* extend array */
|
||||
int i, *newarray, newlen;
|
||||
|
||||
newlen = (2 * ng_iface_units_len) + 4;
|
||||
MALLOC(newarray, int *, newlen * sizeof(*ng_iface_units),
|
||||
M_NETGRAPH_IFACE, M_NOWAIT);
|
||||
if (newarray == NULL) {
|
||||
mtx_unlock(&ng_iface_mtx);
|
||||
return (ENOMEM);
|
||||
}
|
||||
bcopy(ng_iface_units, newarray,
|
||||
ng_iface_units_len * sizeof(*ng_iface_units));
|
||||
for (i = ng_iface_units_len; i < newlen; i++)
|
||||
newarray[i] = ~0;
|
||||
if (ng_iface_units != NULL)
|
||||
FREE(ng_iface_units, M_NETGRAPH_IFACE);
|
||||
ng_iface_units = newarray;
|
||||
ng_iface_units_len = newlen;
|
||||
}
|
||||
bit = ffs(ng_iface_units[index]) - 1;
|
||||
KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1,
|
||||
("%s: word=%d bit=%d", __func__, ng_iface_units[index], bit));
|
||||
ng_iface_units[index] &= ~(1 << bit);
|
||||
*unit = (index * UNITS_BITSPERWORD) + bit;
|
||||
ng_units_in_use++;
|
||||
mtx_unlock(&ng_iface_mtx);
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Free a no longer needed unit number.
|
||||
*/
|
||||
static __inline void
|
||||
ng_iface_free_unit(int unit)
|
||||
{
|
||||
int index, bit;
|
||||
|
||||
index = unit / UNITS_BITSPERWORD;
|
||||
bit = unit % UNITS_BITSPERWORD;
|
||||
mtx_lock(&ng_iface_mtx);
|
||||
KASSERT(index < ng_iface_units_len,
|
||||
("%s: unit=%d len=%d", __func__, unit, ng_iface_units_len));
|
||||
KASSERT((ng_iface_units[index] & (1 << bit)) == 0,
|
||||
("%s: unit=%d is free", __func__, unit));
|
||||
ng_iface_units[index] |= (1 << bit);
|
||||
/*
|
||||
* XXX We could think about reducing the size of ng_iface_units[]
|
||||
* XXX here if the last portion is all ones
|
||||
* XXX At least free it if no more units.
|
||||
* Needed if we are to eventually be able to unload.
|
||||
*/
|
||||
ng_units_in_use--;
|
||||
if (ng_units_in_use == 0) { /* XXX make SMP safe */
|
||||
FREE(ng_iface_units, M_NETGRAPH_IFACE);
|
||||
ng_iface_units_len = 0;
|
||||
ng_iface_units = NULL;
|
||||
}
|
||||
mtx_unlock(&ng_iface_mtx);
|
||||
}
|
||||
|
||||
/************************************************************************
|
||||
INTERFACE STUFF
|
||||
************************************************************************/
|
||||
@ -547,7 +465,6 @@ ng_iface_constructor(node_p node)
|
||||
{
|
||||
struct ifnet *ifp;
|
||||
priv_p priv;
|
||||
int error = 0;
|
||||
|
||||
/* Allocate node and interface private structures */
|
||||
MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_IFACE, M_NOWAIT|M_ZERO);
|
||||
@ -564,11 +481,7 @@ ng_iface_constructor(node_p node)
|
||||
priv->ifp = ifp;
|
||||
|
||||
/* Get an interface unit number */
|
||||
if ((error = ng_iface_get_unit(&priv->unit)) != 0) {
|
||||
FREE(ifp, M_NETGRAPH_IFACE);
|
||||
FREE(priv, M_NETGRAPH_IFACE);
|
||||
return (error);
|
||||
}
|
||||
priv->unit = alloc_unr(ng_iface_unit);
|
||||
|
||||
/* Link together node and private info */
|
||||
NG_NODE_SET_PRIVATE(node, priv);
|
||||
@ -814,7 +727,7 @@ ng_iface_shutdown(node_p node)
|
||||
if_detach(priv->ifp);
|
||||
FREE(priv->ifp, M_NETGRAPH_IFACE);
|
||||
priv->ifp = NULL;
|
||||
ng_iface_free_unit(priv->unit);
|
||||
free_unr(ng_iface_unit, priv->unit);
|
||||
FREE(priv, M_NETGRAPH_IFACE);
|
||||
NG_NODE_SET_PRIVATE(node, NULL);
|
||||
NG_NODE_UNREF(node);
|
||||
@ -847,10 +760,10 @@ ng_iface_mod_event(module_t mod, int event, void *data)
|
||||
|
||||
switch (event) {
|
||||
case MOD_LOAD:
|
||||
mtx_init(&ng_iface_mtx, "ng_iface", NULL, MTX_DEF);
|
||||
ng_iface_unit = new_unrhdr(0, 0xffff, NULL);
|
||||
break;
|
||||
case MOD_UNLOAD:
|
||||
mtx_destroy(&ng_iface_mtx);
|
||||
delete_unrhdr(ng_iface_unit);
|
||||
break;
|
||||
default:
|
||||
error = EOPNOTSUPP;
|
||||
|
Loading…
Reference in New Issue
Block a user