[libvirt] [PATCH v2] interface: list all interfaces with flags == 0

Guannan Ren gren at redhat.com
Tue May 21 13:29:38 UTC 2013


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               |  4 ++++
 src/interface/interface_backend_netcf.c | 29 +++++++++++++++--------------
 src/interface/interface_backend_udev.c  | 25 ++++++++++++-------------
 3 files changed, 31 insertions(+), 27 deletions(-)

diff --git a/src/conf/interface_conf.h b/src/conf/interface_conf.h
index e636c35..ae93811 100644
--- a/src/conf/interface_conf.h
+++ b/src/conf/interface_conf.h
@@ -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__ */
diff --git a/src/interface/interface_backend_netcf.c b/src/interface/interface_backend_netcf.c
index cbba4fd..d626017 100644
--- a/src/interface/interface_backend_netcf.c
+++ b/src/interface/interface_backend_netcf.c
@@ -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);
diff --git a/src/interface/interface_backend_udev.c b/src/interface/interface_backend_udev.c
index 1fd7d46..428adc8 100644
--- a/src/interface/interface_backend_udev.c
+++ b/src/interface/interface_backend_udev.c
@@ -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;
-- 
1.8.1.4




More information about the libvir-list mailing list