[Libvirt-cim] [PATCH V2] Add default network card existence checking

Xu Wang gesaint at linux.vnet.ibm.com
Wed Aug 7 08:46:39 UTC 2013


The default network card (used as forward device in network pool) name
was set as "eth0" in Virt_SettingDefineCapabilities.c. This patch added
check if there is such a network card exists in the network info list.
If it exists, use it. If not, the default network card would be changed
into the first one in the network devices list.

Signed-off-by: Xu Wang <gesaint at linux.vnet.ibm.com>
---
 libxkutil/misc_util.c                 |   39 +++++++++++++++++++++++++++++++++
 libxkutil/misc_util.h                 |    2 +-
 src/Virt_SettingsDefineCapabilities.c |    6 ++++-
 3 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/libxkutil/misc_util.c b/libxkutil/misc_util.c
index 9e7e0d5..7541aa3 100644
--- a/libxkutil/misc_util.c
+++ b/libxkutil/misc_util.c
@@ -33,6 +33,8 @@
 #include <errno.h>
 #include <libvirt/libvirt.h>
 #include <libvirt/virterror.h>
+#include <net/if.h>
+#include <sys/ioctl.h>
 
 #include "cmpidt.h"
 #include "cmpift.h"
@@ -900,6 +902,43 @@ int virt_set_status(const CMPIBroker *broker,
         return ret;
 }
 
+char *get_avail_net(const char *def_name)
+{
+    int fd;
+    int interfaceNum = 0;
+    struct ifreq buf[16];
+    struct ifconf ifc;
+    char *avail_name = NULL;
+ 
+    if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
+       CU_DEBUG("socket() failed.");
+       goto out;
+    }
+ 
+    ifc.ifc_len = sizeof(buf);
+    ifc.ifc_buf = (caddr_t)buf;
+    if (!ioctl(fd, SIOCGIFCONF, (char *)&ifc)) {
+        interfaceNum = ifc.ifc_len / sizeof(struct ifreq);
+        CU_DEBUG("interface num = %d", interfaceNum);
+        while (interfaceNum-- > 0) {
+              CU_DEBUG("network device name: %s", buf[interfaceNum].ifr_name);
+              if (!strcmp(def_name, buf[interfaceNum].ifr_name)) {
+                  avail_name = strdup(buf[interfaceNum].ifr_name);
+                  goto out;
+              }
+        }
+
+        avail_name = strdup(buf[0].ifr_name);
+    } else {
+         CU_DEBUG("ioctl: %s [%s:%d]\n", strerror(errno), __FILE__, __LINE__);
+         goto out;
+    }
+ 
+out:
+    close(fd);
+    return avail_name;
+}
+
 
 /*
  * Local Variables:
diff --git a/libxkutil/misc_util.h b/libxkutil/misc_util.h
index fd4f191..9489a18 100644
--- a/libxkutil/misc_util.h
+++ b/libxkutil/misc_util.h
@@ -157,7 +157,7 @@ const char *get_mig_ssh_tmp_key(void);
 bool get_disable_kvm(void);
 const char *get_lldptool_query_options(void);
 const char *get_vsi_support_key_string(void);
-
+char *get_avail_net(const char *def_name);
 /*
  * Local Variables:
  * mode: C
diff --git a/src/Virt_SettingsDefineCapabilities.c b/src/Virt_SettingsDefineCapabilities.c
index 78c128c..817980c 100644
--- a/src/Virt_SettingsDefineCapabilities.c
+++ b/src/Virt_SettingsDefineCapabilities.c
@@ -777,6 +777,7 @@ static CMPIStatus set_net_pool_props(const CMPIObjectPath *ref,
         int dev_count;
         int i;
         char *tmp_str = NULL;
+        char *forward_device = NULL;
 
         /* Isolated network pools don't have a forward device */
         if (pool_type == NETPOOL_FORWARD_NONE)
@@ -836,14 +837,17 @@ static CMPIStatus set_net_pool_props(const CMPIObjectPath *ref,
                               (CMPIValue *)&pool_type, CMPI_uint16);
 
                 if (i == 1) {
+                        forward_device = get_avail_net("eth0");
+
                         CMSetProperty(inst, "ForwardDevice",
-                                      (CMPIValue *)"eth0", CMPI_chars);
+                                      (CMPIValue *)forward_device, CMPI_chars);
                 }
 
                 inst_list_add(list, inst);
         }
 
  out:
+        free(forward_device);
         return s;
 }
 
-- 
1.7.1




More information about the Libvirt-cim mailing list