[netcf-devel] [PATCH] Detect and report live interface type.

Laine Stump laine at laine.org
Tue Oct 13 17:27:57 UTC 2009


Poke around in sysfs and procfs to determine the type of an interface,
and set the "type" attribute in the xml appropriately. If no clue is
found, default to "ethernet", which is the current behavior elsewhere
in netcf as well.
---
 src/drv_initscripts.c |    3 +++
 src/dutil.c           |   33 +++++++++++++++++++++++++++++++++
 src/dutil.h           |    5 +++++
 3 files changed, 41 insertions(+), 0 deletions(-)

diff --git a/src/drv_initscripts.c b/src/drv_initscripts.c
index 8e36fab..ead5b9f 100644
--- a/src/drv_initscripts.c
+++ b/src/drv_initscripts.c
@@ -775,6 +775,9 @@ char *drv_xml_state(struct netcf_if *nif) {
     prop = xmlNewProp(root, BAD_CAST "name", BAD_CAST nif->name);
     ERR_NOMEM(prop == NULL, ncf);
 
+    prop = xmlSetProp(root, BAD_CAST "type", BAD_CAST if_type(ncf, nif->name));
+    ERR_NOMEM(prop == NULL, ncf);
+
     /* get the current IP address and prefix, and add both to the
      * document.
      */
diff --git a/src/dutil.c b/src/dutil.c
index 817ec6d..9c8e575 100644
--- a/src/dutil.c
+++ b/src/dutil.c
@@ -499,6 +499,39 @@ int if_ipv4_prefix(struct netcf *ncf, const char *intf) {
     return prefix;
 }
 
+const char *if_type(struct netcf *ncf, const char *intf) {
+    char *path;
+    struct stat stats;
+    const char *ret = NULL;
+
+    xasprintf(&path, "/proc/net/vlan/%s", intf);
+    ERR_NOMEM(path == NULL, ncf);
+    if ((stat (path, &stats) == 0) && S_ISREG (stats.st_mode)) {
+        ret = "vlan";
+    }
+    FREE(path);
+
+    if (ret == NULL) {
+        xasprintf(&path, "/sys/class/net/%s/bridge", intf);
+        ERR_NOMEM(path == NULL, ncf);
+        if (stat (path, &stats) == 0 && S_ISDIR (stats.st_mode))
+            return "bridge";
+        FREE(path);
+    }
+    if (ret == NULL) {
+        xasprintf(&path, "/sys/class/net/%s/bonding", intf);
+        ERR_NOMEM(path == NULL, ncf);
+        if (stat (path, &stats) == 0 && S_ISDIR (stats.st_mode))
+            return "bond";
+        FREE(path);
+    }
+
+error:        
+    if (ret == NULL)
+        ret = "ethernet";
+    return ret;
+}
+
 /* Create a new netcf if instance for interface NAME */
 struct netcf_if *make_netcf_if(struct netcf *ncf, char *name) {
     int r;
diff --git a/src/dutil.h b/src/dutil.h
index 0a9715f..74d0023 100644
--- a/src/dutil.h
+++ b/src/dutil.h
@@ -123,6 +123,11 @@ unsigned int if_ipv4_netmask(struct netcf *ncf, const char *intf);
 
 int if_ipv4_prefix(struct netcf *ncf, const char *intf);
 
+/* return the type of the interface - "ethernet" (physical device),
+ * "bridge", "bond", or "vlan"
+ */
+const char *if_type(struct netcf *ncf, const char *intf);
+
 /* Create a new netcf if instance for interface NAME */
 struct netcf_if *make_netcf_if(struct netcf *ncf, char *name);
 
-- 
1.6.2.5



More information about the netcf-devel mailing list