[Midnightbsd-cvs] src [8906] trunk/sys/netgraph/ng_ether.c: track interface renaming.

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Mon Sep 26 18:42:02 EDT 2016


Revision: 8906
          http://svnweb.midnightbsd.org/src/?rev=8906
Author:   laffer1
Date:     2016-09-26 18:42:02 -0400 (Mon, 26 Sep 2016)
Log Message:
-----------
track interface renaming.

Modified Paths:
--------------
    trunk/sys/netgraph/ng_ether.c

Modified: trunk/sys/netgraph/ng_ether.c
===================================================================
--- trunk/sys/netgraph/ng_ether.c	2016-09-26 22:40:10 UTC (rev 8905)
+++ trunk/sys/netgraph/ng_ether.c	2016-09-26 22:42:02 UTC (rev 8906)
@@ -118,6 +118,8 @@
 static ng_disconnect_t	ng_ether_disconnect;
 static int		ng_ether_mod_event(module_t mod, int event, void *data);
 
+static eventhandler_tag	ng_ether_ifnet_arrival_cookie;
+
 /* List of commands and how to convert arguments to/from ASCII */
 static const struct ng_cmdlist ng_ether_cmdlist[] = {
 	{
@@ -215,6 +217,24 @@
 NETGRAPH_INIT(ether, &ng_ether_typestruct);
 
 /******************************************************************
+		    UTILITY FUNCTIONS
+******************************************************************/
+static void
+ng_ether_sanitize_ifname(const char *ifname, char *name)
+{
+	int i;
+
+	for (i = 0; i < IFNAMSIZ; i++) {
+		if (ifname[i] == '.' || ifname[i] == ':')
+			name[i] = '_';
+		else
+			name[i] = ifname[i];
+		if (name[i] == '\0')
+			break;
+	}
+}
+
+/******************************************************************
 		    ETHERNET FUNCTION HOOKS
 ******************************************************************/
 
@@ -287,6 +307,7 @@
 static void
 ng_ether_attach(struct ifnet *ifp)
 {
+	char name[IFNAMSIZ];
 	priv_p priv;
 	node_p node;
 
@@ -324,10 +345,9 @@
 	priv->hwassist = ifp->if_hwassist;
 
 	/* Try to give the node the same name as the interface */
-	if (ng_name_node(node, ifp->if_xname) != 0) {
-		log(LOG_WARNING, "%s: can't name node %s\n",
-		    __func__, ifp->if_xname);
-	}
+	ng_ether_sanitize_ifname(ifp->if_xname, name);
+	if (ng_name_node(node, name) != 0)
+		log(LOG_WARNING, "%s: can't name node %s\n", __func__, name);
 }
 
 /*
@@ -383,6 +403,38 @@
 	}
 }
 
+/*
+ * Interface arrival notification handler.
+ * The notification is produced in two cases:
+ *  o a new interface arrives
+ *  o an existing interface got renamed
+ * Currently the first case is handled by ng_ether_attach via special
+ * hook ng_ether_attach_p.
+ */
+static void
+ng_ether_ifnet_arrival_event(void *arg __unused, struct ifnet *ifp)
+{
+	char name[IFNAMSIZ];
+	node_p node;
+
+	/* Only ethernet interfaces are of interest. */
+	if (ifp->if_type != IFT_ETHER
+	    && ifp->if_type != IFT_L2VLAN)
+		return;
+
+	/*
+	 * Just return if it's a new interface without an ng_ether companion.
+	 */
+	node = IFP2NG(ifp);
+	if (node == NULL)
+		return;
+
+	/* Try to give the node the same name as the new interface name */
+	ng_ether_sanitize_ifname(ifp->if_xname, name);
+	if (ng_name_node(node, name) != 0)
+		log(LOG_WARNING, "%s: can't re-name node %s\n", __func__, name);
+}
+
 /******************************************************************
 		    NETGRAPH NODE METHODS
 ******************************************************************/
@@ -778,6 +830,9 @@
 		ng_ether_input_orphan_p = ng_ether_input_orphan;
 		ng_ether_link_state_p = ng_ether_link_state;
 
+		ng_ether_ifnet_arrival_cookie =
+		    EVENTHANDLER_REGISTER(ifnet_arrival_event,
+		    ng_ether_ifnet_arrival_event, NULL, EVENTHANDLER_PRI_ANY);
 		break;
 
 	case MOD_UNLOAD:
@@ -790,6 +845,9 @@
 		 * is MOD_UNLOAD, so there's no need to detach any nodes.
 		 */
 
+		EVENTHANDLER_DEREGISTER(ifnet_arrival_event,
+		    ng_ether_ifnet_arrival_cookie);
+
 		/* Unregister function hooks */
 		ng_ether_attach_p = NULL;
 		ng_ether_detach_p = NULL;



More information about the Midnightbsd-cvs mailing list