summaryrefslogtreecommitdiffstats
path: root/sys/netgraph/ng_iface.c
diff options
context:
space:
mode:
authorglebius <glebius@FreeBSD.org>2005-03-14 19:25:24 +0000
committerglebius <glebius@FreeBSD.org>2005-03-14 19:25:24 +0000
commit46cd9598e0f9991fb8dd534322b9d251ca4c010b (patch)
tree0674db8ecdb1a00a7dcbb79551e94c8fb686bfa9 /sys/netgraph/ng_iface.c
parent66a628923f4a3caa6e225f6b1bf033ddb7bbdf7d (diff)
downloadFreeBSD-src-46cd9598e0f9991fb8dd534322b9d251ca4c010b.zip
FreeBSD-src-46cd9598e0f9991fb8dd534322b9d251ca4c010b.tar.gz
Use subr_unit allocator instead of own functions.
Diffstat (limited to 'sys/netgraph/ng_iface.c')
-rw-r--r--sys/netgraph/ng_iface.c97
1 files changed, 5 insertions, 92 deletions
diff --git a/sys/netgraph/ng_iface.c b/sys/netgraph/ng_iface.c
index c7dee9d..9293ac3 100644
--- a/sys/netgraph/ng_iface.c
+++ b/sys/netgraph/ng_iface.c
@@ -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;
OpenPOWER on IntegriCloud