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

Laine Stump laine at laine.org
Wed Oct 14 04:07:03 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 |    6 ++++++
 src/dutil.c           |   33 +++++++++++++++++++++++++++++++++
 src/dutil.h           |    5 +++++
 3 files changed, 44 insertions(+), 0 deletions(-)

diff --git a/src/drv_initscripts.c b/src/drv_initscripts.c
index 8e36fab..e4f2b37 100644
--- a/src/drv_initscripts.c
+++ b/src/drv_initscripts.c
@@ -759,6 +759,7 @@ char *drv_xml_state(struct netcf_if *nif) {
     xmlDocPtr ncf_xml = NULL;
     xmlNodePtr root;
     xmlAttrPtr prop;
+    const char *type;
     unsigned int ipv4;
     int prefix;
 
@@ -775,6 +776,11 @@ char *drv_xml_state(struct netcf_if *nif) {
     prop = xmlNewProp(root, BAD_CAST "name", BAD_CAST nif->name);
     ERR_NOMEM(prop == NULL, ncf);
 
+    type = if_type(ncf, nif->name);
+    ERR_BAIL(ncf);
+    prop = xmlSetProp(root, BAD_CAST "type", BAD_CAST type);
+    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..ae27801 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))
+            ret = "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))
+            ret = "bond";
+        FREE(path);
+    }
+    if (ret == NULL)
+        ret = "ethernet";
+
+error:        
+    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