summaryrefslogtreecommitdiffstats
path: root/sys/net
diff options
context:
space:
mode:
authorhsu <hsu@FreeBSD.org>2002-12-24 03:03:39 +0000
committerhsu <hsu@FreeBSD.org>2002-12-24 03:03:39 +0000
commit32436a25c0510e4371c6f02353b447c09ba9db0d (patch)
tree5e53dfd4f16f3a5c2080f2ce73aed007b1be63a1 /sys/net
parent018c02046059a2588bdd154c9136e4467aafa156 (diff)
downloadFreeBSD-src-32436a25c0510e4371c6f02353b447c09ba9db0d.zip
FreeBSD-src-32436a25c0510e4371c6f02353b447c09ba9db0d.tar.gz
SMP locking for radix nodes.
Diffstat (limited to 'sys/net')
-rw-r--r--sys/net/if.c2
-rw-r--r--sys/net/radix.c1
-rw-r--r--sys/net/radix.h11
-rw-r--r--sys/net/route.c37
-rw-r--r--sys/net/rtsock.c32
5 files changed, 66 insertions, 17 deletions
diff --git a/sys/net/if.c b/sys/net/if.c
index 42f2590..1f58605 100644
--- a/sys/net/if.c
+++ b/sys/net/if.c
@@ -533,7 +533,9 @@ if_detach(ifp)
for (i = 1; i <= AF_MAX; i++) {
if ((rnh = rt_tables[i]) == NULL)
continue;
+ RADIX_NODE_HEAD_LOCK(rnh);
(void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
+ RADIX_NODE_HEAD_UNLOCK(rnh);
}
/* Announce that the interface is gone. */
diff --git a/sys/net/radix.c b/sys/net/radix.c
index e1742f3..f6cdadb 100644
--- a/sys/net/radix.c
+++ b/sys/net/radix.c
@@ -1024,6 +1024,7 @@ rn_inithead(head, off)
if (rnh == 0)
return (0);
Bzero(rnh, sizeof (*rnh));
+ RADIX_NODE_HEAD_LOCK_INIT(rnh);
*head = rnh;
t = rn_newpair(rn_zeros, off, rnh->rnh_nodes);
ttt = rnh->rnh_nodes + 2;
diff --git a/sys/net/radix.h b/sys/net/radix.h
index 3a65585..d9937ca 100644
--- a/sys/net/radix.h
+++ b/sys/net/radix.h
@@ -37,6 +37,9 @@
#ifndef _RADIX_H_
#define _RADIX_H_
+#include <sys/lock.h>
+#include <sys/mutex.h>
+
#ifdef MALLOC_DECLARE
MALLOC_DECLARE(M_RTABLE);
#endif
@@ -138,6 +141,7 @@ struct radix_node_head {
void (*rnh_close) /* do something when the last ref drops */
(struct radix_node *rn, struct radix_node_head *head);
struct radix_node rnh_nodes[3]; /* empty tree for common case */
+ struct mtx rnh_mtx;
};
#ifndef _KERNEL
@@ -152,6 +156,13 @@ struct radix_node_head {
#define Bzero(p, n) bzero((caddr_t)(p), (unsigned)(n));
#define R_Malloc(p, t, n) (p = (t) malloc((unsigned long)(n), M_RTABLE, M_NOWAIT))
#define Free(p) free((caddr_t)p, M_RTABLE);
+
+
+#define RADIX_NODE_HEAD_LOCK_INIT(rnh) \
+ mtx_init(&(rnh)->rnh_mtx, "radix node head", NULL, MTX_DEF | MTX_RECURSE)
+#define RADIX_NODE_HEAD_LOCK(rnh) mtx_lock(&(rnh)->rnh_mtx)
+#define RADIX_NODE_HEAD_UNLOCK(rnh) mtx_unlock(&(rnh)->rnh_mtx)
+#define RADIX_NODE_HEAD_DESTROY(rnh) mtx_destroy(&(rnh)->rnh_mtx)
#endif /* _KERNEL */
void rn_init(void);
diff --git a/sys/net/route.c b/sys/net/route.c
index 1727d5b..260f375 100644
--- a/sys/net/route.c
+++ b/sys/net/route.c
@@ -132,7 +132,12 @@ rtalloc1(dst, report, ignflags)
/*
* Look up the address in the table for that Address Family
*/
- if (rnh && (rn = rnh->rnh_matchaddr((caddr_t)dst, rnh)) &&
+ if (rnh == NULL) {
+ rtstat.rts_unreach++;
+ goto miss2;
+ }
+ RADIX_NODE_HEAD_LOCK(rnh);
+ if ((rn = rnh->rnh_matchaddr((caddr_t)dst, rnh)) &&
((rn->rn_flags & RNF_ROOT) == 0)) {
/*
* If we find it and it's not the root node, then
@@ -178,6 +183,7 @@ rtalloc1(dst, report, ignflags)
rt_missmsg(RTM_ADD, &info, rt->rt_flags, 0);
} else
rt->rt_refcnt++;
+ RADIX_NODE_HEAD_UNLOCK(rnh);
} else {
/*
* Either we hit the root or couldn't find any match,
@@ -185,7 +191,9 @@ rtalloc1(dst, report, ignflags)
* "caint get there frm here"
*/
rtstat.rts_unreach++;
- miss: if (report) {
+ miss:
+ RADIX_NODE_HEAD_UNLOCK(rnh);
+ miss2: if (report) {
/*
* If required, report the failure to the supervising
* Authorities.
@@ -211,8 +219,7 @@ rtfree(rt)
/*
* find the tree for that address family
*/
- register struct radix_node_head *rnh =
- rt_tables[rt_key(rt)->sa_family];
+ struct radix_node_head *rnh = rt_tables[rt_key(rt)->sa_family];
if (rt == 0 || rnh == 0)
panic("rtfree");
@@ -222,7 +229,7 @@ rtfree(rt)
* and there is a close function defined, call the close function
*/
rt->rt_refcnt--;
- if(rnh->rnh_close && rt->rt_refcnt == 0) {
+ if (rnh->rnh_close && rt->rt_refcnt == 0) {
rnh->rnh_close((struct radix_node *)rt, rnh);
}
@@ -270,7 +277,8 @@ rtfree(rt)
}
}
-#define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
+/* compare two sockaddr structures */
+#define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
/*
* Force a routing table entry to the specified
@@ -306,7 +314,7 @@ rtredirect(dst, gateway, netmask, flags, src, rtp)
* going down recently.
*/
if (!(flags & RTF_DONE) && rt &&
- (!equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
+ (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
error = EINVAL;
else if (ifa_ifwithaddr(gateway))
error = EHOSTUNREACH;
@@ -538,6 +546,7 @@ rtrequest1(req, info, ret_nrt)
*/
if ((rnh = rt_tables[dst->sa_family]) == 0)
senderr(EAFNOSUPPORT);
+ RADIX_NODE_HEAD_LOCK(rnh);
/*
* If we are adding a host route then we don't want to put
* a netmask in the tree, nor do we want to clone it.
@@ -763,6 +772,7 @@ rtrequest1(req, info, ret_nrt)
error = EOPNOTSUPP;
}
bad:
+ RADIX_NODE_HEAD_UNLOCK(rnh);
splx(s);
return (error);
#undef dst
@@ -1002,8 +1012,10 @@ rt_setgate(rt0, dst, gate)
struct rtfc_arg arg;
arg.rnh = rnh;
arg.rt0 = rt;
+ RADIX_NODE_HEAD_LOCK(rnh);
rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
rt_fixchange, &arg);
+ RADIX_NODE_HEAD_UNLOCK(rnh);
}
return 0;
@@ -1079,11 +1091,16 @@ rtinit(ifa, cmd, flags)
* Look up an rtentry that is in the routing tree and
* contains the correct info.
*/
- if ((rnh = rt_tables[dst->sa_family]) == NULL ||
- (rn = rnh->rnh_lookup(dst, netmask, rnh)) == NULL ||
+ if ((rnh = rt_tables[dst->sa_family]) == NULL)
+ goto bad;
+ RADIX_NODE_HEAD_LOCK(rnh);
+ error = ((rn = rnh->rnh_lookup(dst, netmask, rnh)) == NULL ||
(rn->rn_flags & RNF_ROOT) ||
((struct rtentry *)rn)->rt_ifa != ifa ||
- !equal(SA(rn->rn_key), dst)) {
+ !sa_equal(SA(rn->rn_key), dst));
+ RADIX_NODE_HEAD_UNLOCK(rnh);
+ if (error) {
+bad:
if (m)
(void) m_free(m);
return (flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
diff --git a/sys/net/rtsock.c b/sys/net/rtsock.c
index 40f2ee1..0c441ef 100644
--- a/sys/net/rtsock.c
+++ b/sys/net/rtsock.c
@@ -367,11 +367,15 @@ route_output(m, so)
case RTM_LOCK:
if ((rnh = rt_tables[dst->sa_family]) == 0) {
senderr(EAFNOSUPPORT);
- } else if ((rt = (struct rtentry *)
- rnh->rnh_lookup(dst, netmask, rnh)) != NULL)
+ }
+ RADIX_NODE_HEAD_LOCK(rnh);
+ rt = (struct rtentry *) rnh->rnh_lookup(dst, netmask, rnh);
+ RADIX_NODE_HEAD_UNLOCK(rnh);
+ if (rt != NULL)
rt->rt_refcnt++;
else
senderr(ESRCH);
+
switch(rtm->rtm_type) {
case RTM_GET:
@@ -1024,11 +1028,25 @@ sysctl_rtsock(SYSCTL_HANDLER_ARGS)
case NET_RT_DUMP:
case NET_RT_FLAGS:
- for (i = 1; i <= AF_MAX; i++)
- if ((rnh = rt_tables[i]) && (af == 0 || af == i) &&
- (error = rnh->rnh_walktree(rnh,
- sysctl_dumpentry, &w)))
- break;
+ if (af != 0) {
+ if ((rnh = rt_tables[af]) != NULL) {
+ RADIX_NODE_HEAD_LOCK(rnh);
+ error = rnh->rnh_walktree(rnh,
+ sysctl_dumpentry, &w);
+ RADIX_NODE_HEAD_UNLOCK(rnh);
+ } else
+ error = EAFNOSUPPORT;
+ } else {
+ for (i = 1; i <= AF_MAX; i++)
+ if ((rnh = rt_tables[i]) != NULL) {
+ RADIX_NODE_HEAD_LOCK(rnh);
+ error = rnh->rnh_walktree(rnh,
+ sysctl_dumpentry, &w);
+ RADIX_NODE_HEAD_UNLOCK(rnh);
+ if (error)
+ break;
+ }
+ }
break;
case NET_RT_IFLIST:
OpenPOWER on IntegriCloud