[PATCH 22/48] list: Add helpers to list network objects

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



src/conf/network_conf.c: Add virNetworkMatch to filter the networks;
and virNetworkList to iterate over all the networks with the filter.

src/conf/network_conf.h: Declare virNetworkList and define the macros
for filters.

src/libvirt_private.syms: Export virNetworkList.
---
 src/conf/network_conf.c  |   91 ++++++++++++++++++++++++++++++++++++++++++++++
 src/conf/network_conf.h  |   22 +++++++++++
 src/libvirt_private.syms |    1 +
 3 files changed, 114 insertions(+), 0 deletions(-)

diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index eb92d93..5dfc9c1 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -1922,3 +1922,94 @@ void virNetworkObjUnlock(virNetworkObjPtr obj)
 {
     virMutexUnlock(&obj->lock);
 }
+
+#define MATCH(FLAG) (flags & (FLAG))
+static bool
+virNetworkMatch (virNetworkObjPtr netobj,
+                 unsigned int flags)
+{
+    /* filter by active state */
+    if (MATCH(VIR_CONNECT_LIST_NETWORKS_FILTERS_ACTIVE) &&
+        !((MATCH(VIR_CONNECT_LIST_NETWORKS_ACTIVE) &&
+           virNetworkObjIsActive(netobj)) ||
+          (MATCH(VIR_CONNECT_LIST_NETWORKS_INACTIVE) &&
+           !virNetworkObjIsActive(netobj))))
+        return false;
+
+    /* filter by persistence */
+    if (MATCH(VIR_CONNECT_LIST_NETWORKS_FILTERS_PERSISTENT) &&
+        !((MATCH(VIR_CONNECT_LIST_NETWORKS_PERSISTENT) &&
+           netobj->persistent) ||
+          (MATCH(VIR_CONNECT_LIST_NETWORKS_TRANSIENT) &&
+           !netobj->persistent)))
+        return false;
+
+    /* filter by autostart option */
+    if (MATCH(VIR_CONNECT_LIST_NETWORKS_FILTERS_AUTOSTART) &&
+        !((MATCH(VIR_CONNECT_LIST_NETWORKS_AUTOSTART) &&
+           netobj->autostart) ||
+          (MATCH(VIR_CONNECT_LIST_NETWORKS_NO_AUTOSTART) &&
+           !netobj->autostart)))
+        return false;
+
+    return true;
+}
+#undef MATCH
+
+int
+virNetworkList(virConnectPtr conn,
+               virNetworkObjList netobjs,
+               virNetworkPtr **nets,
+               unsigned int flags)
+{
+    virNetworkPtr *tmp_nets = NULL;
+    virNetworkPtr net = NULL;
+    int nnets = 0;
+    int ret = -1;
+    int i;
+
+    if (nets) {
+        if (VIR_ALLOC_N(tmp_nets, netobjs.count + 1) < 0) {
+            virReportOOMError();
+            goto cleanup;
+        }
+    }
+
+    for (i = 0; i < netobjs.count; i++) {
+        virNetworkObjPtr netobj = netobjs.objs[i];
+        virNetworkObjLock(netobj);
+        if (virNetworkMatch(netobj, flags)) {
+            if (nets) {
+                if (!(net = virGetNetwork(conn,
+                                          netobj->def->name,
+                                          netobj->def->uuid))) {
+                    virNetworkObjUnlock(netobj);
+                    goto cleanup;
+                }
+                tmp_nets[nnets] = net;
+            }
+            nnets++;
+        }
+        virNetworkObjUnlock(netobj);
+    }
+
+    if (tmp_nets) {
+        /* trim the array to the final size */
+        ignore_value(VIR_REALLOC_N(tmp_nets, nnets + 1));
+        *nets = tmp_nets;
+        tmp_nets = NULL;
+    }
+
+    ret = nnets;
+
+cleanup:
+    if (tmp_nets) {
+        for (i = 0; i < nnets; i++) {
+            if (tmp_nets[i])
+                virNetworkFree(tmp_nets[i]);
+        }
+    }
+
+    VIR_FREE(tmp_nets);
+    return ret;
+}
diff --git a/src/conf/network_conf.h b/src/conf/network_conf.h
index 1c640a9..fae7e9b 100644
--- a/src/conf/network_conf.h
+++ b/src/conf/network_conf.h
@@ -289,4 +289,26 @@ int virNetworkObjIsDuplicate(virNetworkObjListPtr doms,
 void virNetworkObjLock(virNetworkObjPtr obj);
 void virNetworkObjUnlock(virNetworkObjPtr obj);
 
+# define VIR_CONNECT_LIST_NETWORKS_FILTERS_ACTIVE   \
+                (VIR_CONNECT_LIST_NETWORKS_ACTIVE | \
+                 VIR_CONNECT_LIST_NETWORKS_INACTIVE)
+
+# define VIR_CONNECT_LIST_NETWORKS_FILTERS_PERSISTENT   \
+                (VIR_CONNECT_LIST_NETWORKS_PERSISTENT | \
+                 VIR_CONNECT_LIST_NETWORKS_TRANSIENT)
+
+# define VIR_CONNECT_LIST_NETWORKS_FILTERS_AUTOSTART    \
+                (VIR_CONNECT_LIST_NETWORKS_AUTOSTART |  \
+                 VIR_CONNECT_LIST_NETWORKS_NO_AUTOSTART)
+
+# define VIR_CONNECT_LIST_NETWORKS_FILTERS_ALL                  \
+                (VIR_CONNECT_LIST_NETWORKS_FILTERS_ACTIVE     | \
+                 VIR_CONNECT_LIST_NETWORKS_FILTERS_PERSISTENT | \
+                 VIR_CONNECT_LIST_NETWORKS_FILTERS_AUTOSTART)
+
+int virNetworkList(virConnectPtr conn,
+                   virNetworkObjList netobjs,
+                   virNetworkPtr **nets,
+                   unsigned int flags);
+
 #endif /* __NETWORK_CONF_H__ */
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 44602c3..4bb6171 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -804,6 +804,7 @@ virNetworkFindByName;
 virNetworkFindByUUID;
 virNetworkIpDefNetmask;
 virNetworkIpDefPrefix;
+virNetworkList;
 virNetworkLoadAllConfigs;
 virNetworkObjIsDuplicate;
 virNetworkObjListFree;
-- 
1.7.7.3

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]