]> xenbits.xensource.com Git - libvirt.git/commitdiff
interface: list all interfaces with flags == 0
authorGuannan Ren <gren@redhat.com>
Tue, 21 May 2013 13:29:38 +0000 (21:29 +0800)
committerGuannan Ren <gren@redhat.com>
Wed, 22 May 2013 01:50:34 +0000 (09:50 +0800)
virConnectListAllInterfaces should support to list all of
interfaces when the value of flags is 0. The behaviour is
consistent with other virConnectListAll* APIs

src/conf/interface_conf.h
src/interface/interface_backend_netcf.c
src/interface/interface_backend_udev.c

index e636c35cc58996b489c7b21fa1ff2dc812569a87..ae938117ec7f16c21b01d2b0de1c0fca37402f36 100644 (file)
@@ -211,4 +211,8 @@ char *virInterfaceDefFormat(const virInterfaceDefPtr def);
 void virInterfaceObjLock(virInterfaceObjPtr obj);
 void virInterfaceObjUnlock(virInterfaceObjPtr obj);
 
+#define VIR_CONNECT_LIST_INTERFACES_FILTERS_ACTIVE   \
+                (VIR_CONNECT_LIST_INTERFACES_ACTIVE | \
+                 VIR_CONNECT_LIST_INTERFACES_INACTIVE)
+
 #endif /* __INTERFACE_CONF_H__ */
index cbba4fd758d75b2927d3113d4f63a017aaf5dbe8..d626017bd20408248adc7572b0c5a27b9b44cb42 100644 (file)
@@ -260,6 +260,7 @@ static int netcfConnectListDefinedInterfaces(virConnectPtr conn, char **const na
 
 }
 
+#define MATCH(FLAG) (flags & (FLAG))
 static int
 netcfConnectListAllInterfaces(virConnectPtr conn,
                               virInterfacePtr **ifaces,
@@ -276,8 +277,7 @@ netcfConnectListAllInterfaces(virConnectPtr conn,
     int ret = -1;
     char **names = NULL;
 
-    virCheckFlags(VIR_CONNECT_LIST_INTERFACES_ACTIVE |
-                  VIR_CONNECT_LIST_INTERFACES_INACTIVE, -1);
+    virCheckFlags(VIR_CONNECT_LIST_INTERFACES_FILTERS_ACTIVE, -1);
 
     interfaceDriverLock(driver);
 
@@ -293,7 +293,6 @@ netcfConnectListAllInterfaces(virConnectPtr conn,
                        _("failed to get number of host interfaces: %s%s%s"),
                        errmsg, details ? " - " : "",
                        details ? details : "");
-        ret = -1;
         goto cleanup;
     }
 
@@ -304,7 +303,6 @@ netcfConnectListAllInterfaces(virConnectPtr conn,
 
     if (VIR_ALLOC_N(names, count) < 0) {
         virReportOOMError();
-        ret = -1;
         goto cleanup;
     }
 
@@ -361,16 +359,19 @@ netcfConnectListAllInterfaces(virConnectPtr conn,
         /* XXX: Filter the result, need to be splitted once new filter flags
          * except active|inactive are supported.
          */
-        if (((status & NETCF_IFACE_ACTIVE) &&
-             (flags & VIR_CONNECT_LIST_INTERFACES_ACTIVE)) ||
-            ((status & NETCF_IFACE_INACTIVE) &&
-             (flags & VIR_CONNECT_LIST_INTERFACES_INACTIVE))) {
-            if (ifaces) {
-                iface_obj = virGetInterface(conn, ncf_if_name(iface),
-                                            ncf_if_mac_string(iface));
-                tmp_iface_objs[niface_objs] = iface_obj;
-            }
-            niface_objs++;
+        if (MATCH(VIR_CONNECT_LIST_INTERFACES_FILTERS_ACTIVE) &&
+            !((MATCH(VIR_CONNECT_LIST_INTERFACES_ACTIVE) &&
+               (status & NETCF_IFACE_ACTIVE)) ||
+              (MATCH(VIR_CONNECT_LIST_INTERFACES_INACTIVE) &&
+               (status & NETCF_IFACE_INACTIVE)))) {
+            ncf_if_free(iface);
+            continue;
+        }
+
+        if (ifaces) {
+            iface_obj = virGetInterface(conn, ncf_if_name(iface),
+                                        ncf_if_mac_string(iface));
+            tmp_iface_objs[niface_objs++] = iface_obj;
         }
 
         ncf_if_free(iface);
index 6e27e83019115092f44151e7650fb80437e9c164..a6c7fde47133e6bf7caed802f7cfe91fb2cb220c 100644 (file)
@@ -282,6 +282,7 @@ udevConnectListDefinedInterfaces(virConnectPtr conn,
                                       VIR_UDEV_IFACE_INACTIVE);
 }
 
+#define MATCH(FLAG) (flags & (FLAG))
 static int
 udevConnectListAllInterfaces(virConnectPtr conn,
                              virInterfacePtr **ifaces,
@@ -299,8 +300,7 @@ udevConnectListAllInterfaces(virConnectPtr conn,
     int status = 0;
     int ret;
 
-    virCheckFlags(VIR_CONNECT_LIST_INTERFACES_ACTIVE |
-                  VIR_CONNECT_LIST_INTERFACES_INACTIVE, -1);
+    virCheckFlags(VIR_CONNECT_LIST_INTERFACES_FILTERS_ACTIVE, -1);
 
     /* Grab a udev reference */
     udev = udev_ref(driverState->udev);
@@ -354,7 +354,6 @@ udevConnectListAllInterfaces(virConnectPtr conn,
         const char *path;
         const char *name;
         const char *macaddr;
-        int add_to_list = 0;
 
         path = udev_list_entry_get_name(dev_entry);
         dev = udev_device_new_from_syspath(udev, path);
@@ -363,18 +362,17 @@ udevConnectListAllInterfaces(virConnectPtr conn,
         status = STREQ(udev_device_get_sysattr_value(dev, "operstate"), "up");
 
         /* Filter the results */
-        if (status && (flags & VIR_CONNECT_LIST_INTERFACES_ACTIVE))
-            add_to_list = 1;
-        else if (!status && (flags & VIR_CONNECT_LIST_INTERFACES_INACTIVE))
-            add_to_list = 1;
+        if (MATCH(VIR_CONNECT_LIST_INTERFACES_FILTERS_ACTIVE) &&
+            !((MATCH(VIR_CONNECT_LIST_INTERFACES_ACTIVE) && status) ||
+              (MATCH(VIR_CONNECT_LIST_INTERFACES_INACTIVE) && !status))) {
+            udev_device_unref(dev);
+            continue;
+        }
 
         /* If we matched a filter, then add it */
-        if (add_to_list) {
-            if (ifaces) {
-                iface_obj = virGetInterface(conn, name, macaddr);
-                ifaces_list[count] = iface_obj;
-            }
-            count++;
+        if (ifaces) {
+            iface_obj = virGetInterface(conn, name, macaddr);
+            ifaces_list[count++] = iface_obj;
         }
         udev_device_unref(dev);
     }
@@ -387,6 +385,7 @@ udevConnectListAllInterfaces(virConnectPtr conn,
     if (ifaces) {
         ignore_value(VIR_REALLOC_N(ifaces_list, count + 1));
         *ifaces = ifaces_list;
+        ifaces_list = NULL;
     }
 
     return count;