summaryrefslogtreecommitdiffstats
path: root/sys/net/if.c
diff options
context:
space:
mode:
authorzec <zec@FreeBSD.org>2009-05-05 10:56:12 +0000
committerzec <zec@FreeBSD.org>2009-05-05 10:56:12 +0000
commitd78a1b1a824c4f5eb8cb3583bb5265f73dcc24dd (patch)
tree79a0bccccf2c92504cdf23ad15f7c1813bb3f926 /sys/net/if.c
parent8e4ffe653f6c9ff6da3eed58566ef35e77d530d0 (diff)
downloadFreeBSD-src-d78a1b1a824c4f5eb8cb3583bb5265f73dcc24dd.zip
FreeBSD-src-d78a1b1a824c4f5eb8cb3583bb5265f73dcc24dd.tar.gz
Change the curvnet variable from a global const struct vnet *,
previously always pointing to the default vnet context, to a dynamically changing thread-local one. The currvnet context should be set on entry to networking code via CURVNET_SET() macros, and reverted to previous state via CURVNET_RESTORE(). Recursions on curvnet are permitted, though strongly discuouraged. This change should have no functional impact on nooptions VIMAGE kernel builds, where CURVNET_* macros expand to whitespace. The curthread->td_vnet (aka curvnet) variable's purpose is to be an indicator of the vnet context in which the current network-related operation takes place, in case we cannot deduce the current vnet context from any other source, such as by looking at mbuf's m->m_pkthdr.rcvif->if_vnet, sockets's so->so_vnet etc. Moreover, so far curvnet has turned out to be an invaluable consistency checking aid: it helps to catch cases when sockets, ifnets or any other vnet-aware structures may have leaked from one vnet to another. The exact placement of the CURVNET_SET() / CURVNET_RESTORE() macros was a result of an empirical iterative process, whith an aim to reduce recursions on CURVNET_SET() to a minimum, while still reducing the scope of CURVNET_SET() to networking only operations - the alternative would be calling CURVNET_SET() on each system call entry. In general, curvnet has to be set in three typicall cases: when processing socket-related requests from userspace or from within the kernel; when processing inbound traffic flowing from device drivers to upper layers of the networking stack, and when executing timer-driven networking functions. This change also introduces a DDB subcommand to show the list of all vnet instances. Approved by: julian (mentor)
Diffstat (limited to 'sys/net/if.c')
-rw-r--r--sys/net/if.c42
1 files changed, 28 insertions, 14 deletions
diff --git a/sys/net/if.c b/sys/net/if.c
index 92bf8a6..a67f31b 100644
--- a/sys/net/if.c
+++ b/sys/net/if.c
@@ -53,6 +53,7 @@
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/refcount.h>
+#include <sys/module.h>
#include <sys/rwlock.h>
#include <sys/sockio.h>
#include <sys/syslog.h>
@@ -126,7 +127,6 @@ static void if_attachdomain(void *);
static void if_attachdomain1(struct ifnet *);
static int ifconf(u_long, caddr_t);
static void if_freemulti(struct ifmultiaddr *);
-static void if_grow(void);
static void if_init(void *);
static void if_check(void *);
static void if_route(struct ifnet *, int flag, int fam);
@@ -202,7 +202,7 @@ MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
-static struct ifnet *
+struct ifnet *
ifnet_byindex_locked(u_short idx)
{
INIT_VNET_NET(curvnet);
@@ -239,7 +239,7 @@ ifnet_byindex_ref(u_short idx)
return (ifp);
}
-static void
+void
ifnet_setbyindex(u_short idx, struct ifnet *ifp)
{
INIT_VNET_NET(curvnet);
@@ -445,7 +445,7 @@ vnet_net_iattach(const void *unused __unused)
return (0);
}
-static void
+void
if_grow(void)
{
INIT_VNET_NET(curvnet);
@@ -696,11 +696,13 @@ if_attach(struct ifnet *ifp)
mac_ifnet_create(ifp);
#endif
- ifdev_setbyindex(ifp->if_index, make_dev(&net_cdevsw,
- ifp->if_index, UID_ROOT, GID_WHEEL, 0600, "%s/%s",
- net_cdevsw.d_name, ifp->if_xname));
- make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d",
- net_cdevsw.d_name, ifp->if_index);
+ if (IS_DEFAULT_VNET(curvnet)) {
+ ifdev_setbyindex(ifp->if_index, make_dev(&net_cdevsw,
+ ifp->if_index, UID_ROOT, GID_WHEEL, 0600, "%s/%s",
+ net_cdevsw.d_name, ifp->if_xname));
+ make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d",
+ net_cdevsw.d_name, ifp->if_index);
+ }
ifq_attach(&ifp->if_snd, ifp);
@@ -742,13 +744,17 @@ if_attach(struct ifnet *ifp)
IFNET_WLOCK();
TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
+#ifdef VIMAGE
+ curvnet->ifccnt++;
+#endif
IFNET_WUNLOCK();
if (domain_init_status >= 2)
if_attachdomain1(ifp);
EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
- devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
+ if (IS_DEFAULT_VNET(curvnet))
+ devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
/* Announce the interface. */
rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
@@ -895,6 +901,10 @@ if_detach(struct ifnet *ifp)
found = 1;
break;
}
+#ifdef VIMAGE
+ if (found)
+ curvnet->ifccnt--;
+#endif
IFNET_WUNLOCK();
if (!found)
return;
@@ -943,7 +953,8 @@ if_detach(struct ifnet *ifp)
* Clean up all addresses.
*/
ifp->if_addr = NULL;
- destroy_dev(ifdev_byindex(ifp->if_index));
+ if (IS_DEFAULT_VNET(curvnet))
+ destroy_dev(ifdev_byindex(ifp->if_index));
ifdev_setbyindex(ifp->if_index, NULL);
/* We can now free link ifaddr. */
@@ -972,7 +983,8 @@ if_detach(struct ifnet *ifp)
/* Announce that the interface is gone. */
rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
- devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
+ if (IS_DEFAULT_VNET(curvnet))
+ devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
if_delgroups(ifp);
IF_AFDATA_LOCK(ifp);
@@ -1701,8 +1713,10 @@ do_link_state_change(void *arg, int pending)
(*lagg_linkstate_p)(ifp, link_state);
}
- devctl_notify("IFNET", ifp->if_xname,
- (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
+ if (IS_DEFAULT_VNET(curvnet))
+ devctl_notify("IFNET", ifp->if_xname,
+ (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
+ NULL);
if (pending > 1)
if_printf(ifp, "%d link states coalesced\n", pending);
if (log_link_state_change)
OpenPOWER on IntegriCloud