From patchwork Tue Jul 10 21:09:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cole Robinson X-Patchwork-Id: 141652 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp4347846ljj; Tue, 10 Jul 2018 14:09:39 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdILv42k8FVjnuQ2cGJCpafSCpea3LT62cZLXZhksQhYoDNDNtlhovxHuKuAPwBxG/2AjsE X-Received: by 2002:a37:6d44:: with SMTP id i65-v6mr21501826qkc.142.1531256979821; Tue, 10 Jul 2018 14:09:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531256979; cv=none; d=google.com; s=arc-20160816; b=pQAK0Qc/dBXKMLrIWf4WlAxpjDWobmANVV0UjofCpvvWVhnk0zZ+BhxYdYwkXfcrlv CjY4Tgkwm9xwfFWmbTXj0et2Mwlf2/cuTQT7RP5a4N1qxy+jRicjsYglkBQdA2SG4Q43 lyzjt/kyM1EmJvMtRjBZTkJj+gW6QJwkBHqTSwh6KA0PQtXyysR7M+tUHNTIogs8ScOG sJWL8pl/pAF75d5gNKwu5oZp6L9ALz7BkuNOBHENpSUdrKop0AJ+CwDKmH2BWre07XT8 WQYIDpUDRXLwfNYKSgLmowQKpTVMQ1XO/my23Jx5MUiRlzTGFNcJQEznGRohSJ3zwrrf KS+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:sender:content-transfer-encoding:mime-version :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:cc:message-id:date:to:from:delivered-to :arc-authentication-results; bh=TW3z930tR5xX9x4zxWlK75OQOUP5N+2EkdcjpK6DJAA=; b=HV8A/E4fYpAlpAXTbWjXtesRGKUNAp/DPxi4c54ipSNvA7mKoIcl+oNCrL2tcKSG1O yp6XmOG5STKvpn+COAIzaFEaNbKOgg99MZCsYl1A5MzSsDBf+chYNX9ciThrs3mab1Q+ 2Gf0561IhlvHM/pb4qG+/EWN5eG/C1knvu5mgfXx7NOzk9tXbxQoPierZ4WTZVTBvUA5 /xsewegxE9MU/SfCwkhRnaK+cM2b5acTpwE/whi/dESDdf0eUaTlq9WCKNmCkJSTH3c6 HQdT+tRVmydlcN/GAJMBZvYI8O3p2L1ygBFH4WGvZtzLRwcD9Fqp2RLORDI2Y6Tr3AL9 s4iA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of libvir-list-bounces@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com. [209.132.183.28]) by mx.google.com with ESMTPS id g123-v6si12360968qkd.61.2018.07.10.14.09.39 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 10 Jul 2018 14:09:39 -0700 (PDT) Received-SPF: pass (google.com: domain of libvir-list-bounces@redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; Authentication-Results: mx.google.com; spf=pass (google.com: domain of libvir-list-bounces@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E5FA215553; Tue, 10 Jul 2018 21:09:36 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 817645B686; Tue, 10 Jul 2018 21:09:35 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 703774A460; Tue, 10 Jul 2018 21:09:32 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w6AL9TVZ018784 for ; Tue, 10 Jul 2018 17:09:29 -0400 Received: by smtp.corp.redhat.com (Postfix) id E43841C661; Tue, 10 Jul 2018 21:09:28 +0000 (UTC) Delivered-To: libvirt-list@redhat.com Received: from worklaptop.bos.redhat.com (dhcp-17-157.bos.redhat.com [10.18.17.157]) by smtp.corp.redhat.com (Postfix) with ESMTP id C29FD1C65E; Tue, 10 Jul 2018 21:09:27 +0000 (UTC) From: Cole Robinson To: libvirt-list@redhat.com Date: Tue, 10 Jul 2018 17:09:21 -0400 Message-Id: <954786dc3ef7ce9a857ed4e669739432304cc559.1531256961.git.crobinso@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-loop: libvir-list@redhat.com Cc: aharter@redhat.com Subject: [libvirt] [PATCH] test: Implement virConnectListAllInterfaces X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Tue, 10 Jul 2018 21:09:38 +0000 (UTC) This adds some generic virinterfaceobj code, roughly matching what is used by other stateful drivers like network, storage, etc. Signed-off-by: Cole Robinson --- src/conf/virinterfaceobj.c | 105 +++++++++++++++++++++++++++++++++++++ src/conf/virinterfaceobj.h | 7 +++ src/libvirt_private.syms | 1 + src/test/test_driver.c | 15 ++++++ 4 files changed, 128 insertions(+) -- 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list Reviewed-by: John Ferlan diff --git a/src/conf/virinterfaceobj.c b/src/conf/virinterfaceobj.c index a1d7346eb2..87ce188117 100644 --- a/src/conf/virinterfaceobj.c +++ b/src/conf/virinterfaceobj.c @@ -241,6 +241,111 @@ virInterfaceObjListFindByName(virInterfaceObjListPtr interfaces, return obj; } +#define MATCH(FLAG) (flags & (FLAG)) +static bool +virInterfaceMatch(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 (!virInterfaceMatch(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) diff --git a/src/conf/virinterfaceobj.h b/src/conf/virinterfaceobj.h index 799d38038f..33d2dda05d 100644 --- a/src/conf/virinterfaceobj.h +++ b/src/conf/virinterfaceobj.h @@ -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__ */ diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index e688981c3e..ec5ed0cc81 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -961,6 +961,7 @@ virInterfaceObjGetDef; virInterfaceObjIsActive; virInterfaceObjListAssignDef; virInterfaceObjListClone; +virInterfaceObjListExport; virInterfaceObjListFindByMACString; virInterfaceObjListFindByName; virInterfaceObjListGetNames; diff --git a/src/test/test_driver.c b/src/test/test_driver.c index 5494d51017..951d9c4151 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -3828,6 +3828,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) @@ -6944,6 +6958,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 */