]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
test: Implement virConnectListAllInterfaces
authorCole Robinson <crobinso@redhat.com>
Tue, 10 Jul 2018 19:01:45 +0000 (15:01 -0400)
committerCole Robinson <crobinso@redhat.com>
Thu, 19 Jul 2018 13:47:01 +0000 (09:47 -0400)
This adds some generic virinterfaceobj code, roughly matching what
is used by other stateful drivers like network, storage, etc.

Reviewed-by: John Ferlan <jferlan@redhat.com>
Signed-off-by: Cole Robinson <crobinso@redhat.com>
src/conf/virinterfaceobj.c
src/conf/virinterfaceobj.h
src/libvirt_private.syms
src/test/test_driver.c

index a1d7346eb2d04d02d47838bc6f1d99c87de011cc..b0c4ca15a174997322be7c968485abc30e0832df 100644 (file)
@@ -242,6 +242,112 @@ virInterfaceObjListFindByName(virInterfaceObjListPtr interfaces,
 }
 
 
+#define MATCH(FLAG) (flags & (FLAG))
+static bool
+virInterfaceObjMatch(virInterfaceObjPtr obj,
+                     unsigned int flags)
+{
+    /* filter by active state */
+    if (MATCH(VIR_CONNECT_LIST_INTERFACES_FILTERS_ACTIVE) &&
+        !((MATCH(VIR_CONNECT_LIST_INTERFACES_ACTIVE) &&
+           virInterfaceObjIsActive(obj)) ||
+          (MATCH(VIR_CONNECT_LIST_INTERFACES_INACTIVE) &&
+           !virInterfaceObjIsActive(obj))))
+        return false;
+
+    return true;
+}
+#undef MATCH
+
+
+struct virInterfaceObjListData {
+    virConnectPtr conn;
+    virInterfacePtr *ifaces;
+    virInterfaceObjListFilter filter;
+    unsigned int flags;
+    int nifaces;
+    bool error;
+};
+
+static int
+virInterfaceObjListPopulate(void *payload,
+                            const void *name ATTRIBUTE_UNUSED,
+                            void *opaque)
+{
+    struct virInterfaceObjListData *data = opaque;
+    virInterfaceObjPtr obj = payload;
+    virInterfacePtr iface = NULL;
+
+    if (data->error)
+        return 0;
+
+    virObjectLock(obj);
+
+    if (data->filter &&
+        !data->filter(data->conn, obj->def))
+        goto cleanup;
+
+    if (!virInterfaceObjMatch(obj, data->flags))
+        goto cleanup;
+
+    if (!data->ifaces) {
+        data->nifaces++;
+        goto cleanup;
+    }
+
+    if (!(iface = virGetInterface(data->conn, obj->def->name, obj->def->mac))) {
+        data->error = true;
+        goto cleanup;
+    }
+
+    data->ifaces[data->nifaces++] = iface;
+
+ cleanup:
+    virObjectUnlock(obj);
+    return 0;
+}
+
+
+int
+virInterfaceObjListExport(virConnectPtr conn,
+                          virInterfaceObjListPtr ifaceobjs,
+                          virInterfacePtr **ifaces,
+                          virInterfaceObjListFilter filter,
+                          unsigned int flags)
+{
+    int ret = -1;
+    struct virInterfaceObjListData data = {
+        .conn = conn, .ifaces = NULL, .filter = filter, .flags = flags,
+        .nifaces = 0, .error = false };
+
+    virObjectRWLockRead(ifaceobjs);
+    if (ifaces && VIR_ALLOC_N(data.ifaces,
+                              virHashSize(ifaceobjs->objsName) + 1) < 0)
+        goto cleanup;
+
+    virHashForEach(ifaceobjs->objsName, virInterfaceObjListPopulate, &data);
+
+    if (data.error)
+        goto cleanup;
+
+    if (data.ifaces) {
+        /* trim the array to the final size */
+        ignore_value(VIR_REALLOC_N(data.ifaces, data.nifaces + 1));
+        *ifaces = data.ifaces;
+        data.ifaces = NULL;
+    }
+
+    ret = data.nifaces;
+ cleanup:
+    virObjectRWUnlock(ifaceobjs);
+    while (data.ifaces && data.nifaces)
+        virObjectUnref(data.ifaces[--data.nifaces]);
+
+    VIR_FREE(data.ifaces);
+    return ret;
+}
+
+
 void
 virInterfaceObjListDispose(void *obj)
 {
index 799d38038f381af34689447541f42898ec80e111..33d2dda05d737587d4073750b15c4ff8b011c692 100644 (file)
@@ -82,4 +82,11 @@ virInterfaceObjListGetNames(virInterfaceObjListPtr interfaces,
                             char **const names,
                             int maxnames);
 
+int
+virInterfaceObjListExport(virConnectPtr conn,
+                          virInterfaceObjListPtr ifaceobjs,
+                          virInterfacePtr **ifaces,
+                          virInterfaceObjListFilter filter,
+                          unsigned int flags);
+
 #endif /* __VIRINTERFACEOBJ_H__ */
index ebf39791096cc4ad6d97aee7e31943e6cf169ae3..0a20eb661b9cf6b142c8469d3d5b8e2396da8904 100644 (file)
@@ -962,6 +962,7 @@ virInterfaceObjGetDef;
 virInterfaceObjIsActive;
 virInterfaceObjListAssignDef;
 virInterfaceObjListClone;
+virInterfaceObjListExport;
 virInterfaceObjListFindByMACString;
 virInterfaceObjListFindByName;
 virInterfaceObjListGetNames;
index 7006422fd53aefe9df20e7b7aae0997dac520536..f3ed667d688ddfb8f0e7c85f90a182a833a42623 100644 (file)
@@ -3829,6 +3829,20 @@ testConnectListDefinedInterfaces(virConnectPtr conn,
 }
 
 
+static int
+testConnectListAllInterfaces(virConnectPtr conn,
+                             virInterfacePtr **ifaces,
+                             unsigned int flags)
+{
+    testDriverPtr privconn = conn->privateData;
+
+    virCheckFlags(VIR_CONNECT_LIST_INTERFACES_FILTERS_ACTIVE, -1);
+
+    return virInterfaceObjListExport(conn, privconn->ifaces, ifaces,
+                                     NULL, flags);
+}
+
+
 static virInterfacePtr
 testInterfaceLookupByName(virConnectPtr conn,
                           const char *name)
@@ -6958,6 +6972,7 @@ static virInterfaceDriver testInterfaceDriver = {
     .connectListInterfaces = testConnectListInterfaces, /* 0.7.0 */
     .connectNumOfDefinedInterfaces = testConnectNumOfDefinedInterfaces, /* 0.7.0 */
     .connectListDefinedInterfaces = testConnectListDefinedInterfaces, /* 0.7.0 */
+    .connectListAllInterfaces = testConnectListAllInterfaces, /* 4.6.0 */
     .interfaceLookupByName = testInterfaceLookupByName, /* 0.7.0 */
     .interfaceLookupByMACString = testInterfaceLookupByMACString, /* 0.7.0 */
     .interfaceGetXMLDesc = testInterfaceGetXMLDesc, /* 0.7.0 */