From nobody Fri Nov 29 02:58:12 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1507895428237496.4549712610624; Fri, 13 Oct 2017 04:50:28 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A1C0A796ED; Fri, 13 Oct 2017 11:50:26 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4B4A7612B0; Fri, 13 Oct 2017 11:50:26 +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 EA4D518355C1; Fri, 13 Oct 2017 11:50:24 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v9DBfaCn019385 for ; Fri, 13 Oct 2017 07:41:36 -0400 Received: by smtp.corp.redhat.com (Postfix) id 7534117539; Fri, 13 Oct 2017 11:41:36 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-93.phx2.redhat.com [10.3.116.93]) by smtp.corp.redhat.com (Postfix) with ESMTP id 278D55C8A1 for ; Fri, 13 Oct 2017 11:41:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com A1C0A796ED Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: John Ferlan To: libvir-list@redhat.com Date: Fri, 13 Oct 2017 07:41:26 -0400 Message-Id: <20171013114126.14672-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v2] interfaces: Convert virInterfaceObjList to virObjectRWLockable 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 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Fri, 13 Oct 2017 11:50:27 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Rather than a forward linked list, let's use the ObjectRWLockable object in order to manage the data. Requires numerous changes from List to Object management similar to many other drivers/vir*obj.c modules Signed-off-by: John Ferlan --- v1: https://www.redhat.com/archives/libvir-list/2017-October/msg00387.html Difference to v1 - separate out each of the virHashForEach callback helpers and *Data structures. src/conf/virinterfaceobj.c | 341 ++++++++++++++++++++++++++++++-----------= ---- src/libvirt_private.syms | 1 - src/test/test_driver.c | 6 +- 3 files changed, 229 insertions(+), 119 deletions(-) diff --git a/src/conf/virinterfaceobj.c b/src/conf/virinterfaceobj.c index e993b929d7..4bd371bb1e 100644 --- a/src/conf/virinterfaceobj.c +++ b/src/conf/virinterfaceobj.c @@ -25,6 +25,7 @@ #include "viralloc.h" #include "virerror.h" #include "virinterfaceobj.h" +#include "virhash.h" #include "virlog.h" #include "virstring.h" =20 @@ -40,14 +41,19 @@ struct _virInterfaceObj { }; =20 struct _virInterfaceObjList { - size_t count; - virInterfaceObjPtr *objs; + virObjectRWLockable parent; + + /* name string -> virInterfaceObj mapping + * for O(1), lockless lookup-by-name */ + virHashTable *objsName; }; =20 /* virInterfaceObj manipulation */ =20 static virClassPtr virInterfaceObjClass; +static virClassPtr virInterfaceObjListClass; static void virInterfaceObjDispose(void *obj); +static void virInterfaceObjListDispose(void *obj); =20 static int virInterfaceObjOnceInit(void) @@ -58,6 +64,12 @@ virInterfaceObjOnceInit(void) virInterfaceObjDispose))) return -1; =20 + if (!(virInterfaceObjListClass =3D virClassNew(virClassForObjectRWLock= able(), + "virInterfaceObjList", + sizeof(virInterfaceObjLis= t), + virInterfaceObjListDispos= e))) + return -1; + return 0; } =20 @@ -130,119 +142,189 @@ virInterfaceObjListNew(void) { virInterfaceObjListPtr interfaces; =20 - if (VIR_ALLOC(interfaces) < 0) + if (virInterfaceObjInitialize() < 0) return NULL; + + if (!(interfaces =3D virObjectRWLockableNew(virInterfaceObjListClass))) + return NULL; + + if (!(interfaces->objsName =3D virHashCreate(10, virObjectFreeHashData= ))) { + virObjectUnref(interfaces); + return NULL; + } + return interfaces; } =20 =20 +struct _virInterfaceObjForEachData { + bool wantActive; + const char *matchStr; + bool error; + int nElems; + int maxElems; + char **const elems; +}; + +static int +virInterfaceObjListFindByMACStringCb(void *payload, + const void *name ATTRIBUTE_UNUSED, + void *opaque) +{ + virInterfaceObjPtr obj =3D payload; + struct _virInterfaceObjForEachData *data =3D opaque; + + if (data->error) + return 0; + + if (data->nElems =3D=3D data->maxElems) + return 0; + + virObjectLock(obj); + + if (STRCASEEQ(obj->def->mac, data->matchStr)) { + if (VIR_STRDUP(data->elems[data->nElems], data->matchStr) < 0) { + data->error =3D true; + goto cleanup; + } + data->nElems++; + } + + cleanup: + virObjectUnlock(obj); + return 0; +} + + int virInterfaceObjListFindByMACString(virInterfaceObjListPtr interfaces, const char *mac, char **const matches, int maxmatches) { - size_t i; - int matchct =3D 0; + struct _virInterfaceObjForEachData data =3D { .matchStr =3D mac, + .error =3D false, + .nElems =3D 0, + .maxElems =3D maxmatches, + .elems =3D matches }; =20 - for (i =3D 0; i < interfaces->count; i++) { - virInterfaceObjPtr obj =3D interfaces->objs[i]; - virInterfaceDefPtr def; + virObjectRWLockRead(interfaces); + virHashForEach(interfaces->objsName, virInterfaceObjListFindByMACStrin= gCb, + &data); + virObjectRWUnlock(interfaces); =20 - virObjectLock(obj); - def =3D obj->def; - if (STRCASEEQ(def->mac, mac)) { - if (matchct < maxmatches) { - if (VIR_STRDUP(matches[matchct], def->name) < 0) { - virObjectUnlock(obj); - goto error; - } - matchct++; - } - } - virObjectUnlock(obj); - } - return matchct; + if (data.error) + goto error; + + return data.nElems; =20 error: - while (--matchct >=3D 0) - VIR_FREE(matches[matchct]); + while (--data.nElems >=3D 0) + VIR_FREE(data.elems[data.nElems]); =20 return -1; } =20 =20 +static virInterfaceObjPtr +virInterfaceObjListFindByNameLocked(virInterfaceObjListPtr interfaces, + const char *name) +{ + return virObjectRef(virHashLookup(interfaces->objsName, name)); +} + + virInterfaceObjPtr virInterfaceObjListFindByName(virInterfaceObjListPtr interfaces, const char *name) { - size_t i; - - for (i =3D 0; i < interfaces->count; i++) { - virInterfaceObjPtr obj =3D interfaces->objs[i]; - virInterfaceDefPtr def; - + virInterfaceObjPtr obj; + virObjectRWLockRead(interfaces); + obj =3D virInterfaceObjListFindByNameLocked(interfaces, name); + virObjectRWUnlock(interfaces); + if (obj) virObjectLock(obj); - def =3D obj->def; - if (STREQ(def->name, name)) - return virObjectRef(obj); - virObjectUnlock(obj); - } =20 - return NULL; + return obj; } =20 =20 void -virInterfaceObjListFree(virInterfaceObjListPtr interfaces) +virInterfaceObjListDispose(void *obj) +{ + virInterfaceObjListPtr interfaces =3D obj; + + virHashFree(interfaces->objsName); +} + + +struct _virInterfaceObjListCloneData { + bool error; + virInterfaceObjListPtr dest; +}; + +static int +virInterfaceObjListCloneCb(void *payload, + const void *name ATTRIBUTE_UNUSED, + void *opaque) { - size_t i; + virInterfaceObjPtr srcObj =3D payload; + struct _virInterfaceObjListCloneData *data =3D opaque; + char *xml =3D NULL; + virInterfaceDefPtr backup =3D NULL; + virInterfaceObjPtr obj; + + if (data->error) + return 0; + + virObjectLock(srcObj); + + if (!(xml =3D virInterfaceDefFormat(srcObj->def))) + goto error; + + if (!(backup =3D virInterfaceDefParseString(xml))) + goto error; + VIR_FREE(xml); =20 - for (i =3D 0; i < interfaces->count; i++) - virObjectUnref(interfaces->objs[i]); - VIR_FREE(interfaces->objs); - VIR_FREE(interfaces); + if (!(obj =3D virInterfaceObjListAssignDef(data->dest, backup))) + goto error; + virInterfaceObjEndAPI(&obj); + + virObjectUnlock(srcObj); + return 0; + + error: + data->error =3D true; + VIR_FREE(xml); + virInterfaceDefFree(backup); + virObjectUnlock(srcObj); + return 0; } =20 =20 virInterfaceObjListPtr virInterfaceObjListClone(virInterfaceObjListPtr interfaces) { - size_t i; - unsigned int cnt; - virInterfaceObjListPtr dest; + struct _virInterfaceObjListCloneData data =3D { .error =3D false, + .dest =3D NULL }; =20 if (!interfaces) return NULL; =20 - if (!(dest =3D virInterfaceObjListNew())) + if (!(data.dest =3D virInterfaceObjListNew())) return NULL; =20 - cnt =3D interfaces->count; - for (i =3D 0; i < cnt; i++) { - virInterfaceObjPtr srcobj =3D interfaces->objs[i]; - virInterfaceDefPtr backup; - virInterfaceObjPtr obj; - char *xml =3D virInterfaceDefFormat(srcobj->def); + virObjectRWLockRead(interfaces); + virHashForEach(interfaces->objsName, virInterfaceObjListCloneCb, &data= ); + virObjectRWUnlock(interfaces); =20 - if (!xml) - goto error; + if (data.error) + goto error; =20 - if (!(backup =3D virInterfaceDefParseString(xml))) { - VIR_FREE(xml); - goto error; - } - - VIR_FREE(xml); - if (!(obj =3D virInterfaceObjListAssignDef(dest, backup))) - goto error; - virInterfaceObjEndAPI(&obj); - } - - return dest; + return data.dest; =20 error: - virInterfaceObjListFree(dest); + virObjectUnref(data.dest); return NULL; } =20 @@ -253,9 +335,11 @@ virInterfaceObjListAssignDef(virInterfaceObjListPtr in= terfaces, { virInterfaceObjPtr obj; =20 - if ((obj =3D virInterfaceObjListFindByName(interfaces, def->name))) { + virObjectRWLockWrite(interfaces); + if ((obj =3D virInterfaceObjListFindByNameLocked(interfaces, def->name= ))) { virInterfaceDefFree(obj->def); obj->def =3D def; + virObjectRWUnlock(interfaces); =20 return obj; } @@ -263,13 +347,19 @@ virInterfaceObjListAssignDef(virInterfaceObjListPtr i= nterfaces, if (!(obj =3D virInterfaceObjNew())) return NULL; =20 - if (VIR_APPEND_ELEMENT_COPY(interfaces->objs, - interfaces->count, obj) < 0) { - virInterfaceObjEndAPI(&obj); - return NULL; - } + if (virHashAddEntry(interfaces->objsName, def->name, obj) < 0) + goto error; + virObjectRef(obj); + obj->def =3D def; - return virObjectRef(obj); + virObjectRWUnlock(interfaces); + + return obj; + + error: + virInterfaceObjEndAPI(&obj); + virObjectRWUnlock(interfaces); + return NULL; } =20 =20 @@ -277,20 +367,48 @@ void virInterfaceObjListRemove(virInterfaceObjListPtr interfaces, virInterfaceObjPtr obj) { - size_t i; - + virObjectRef(obj); + virObjectUnlock(obj); + virObjectRWLockWrite(interfaces); + virObjectLock(obj); + virHashRemoveEntry(interfaces->objsName, obj->def->name); virObjectUnlock(obj); - for (i =3D 0; i < interfaces->count; i++) { - virObjectLock(interfaces->objs[i]); - if (interfaces->objs[i] =3D=3D obj) { - virObjectUnlock(interfaces->objs[i]); - virObjectUnref(interfaces->objs[i]); - - VIR_DELETE_ELEMENT(interfaces->objs, i, interfaces->count); - break; + virObjectUnref(obj); + virObjectRWUnlock(interfaces); +} + + +static int +virInterfaceObjListForEachCb(void *payload, + const void *name ATTRIBUTE_UNUSED, + void *opaque) +{ + virInterfaceObjPtr obj =3D payload; + struct _virInterfaceObjForEachData *data =3D opaque; + + if (data->error) + return 0; + + if (data->maxElems >=3D 0 && data->nElems =3D=3D data->maxElems) + return 0; + + virObjectLock(obj); + + if (data->wantActive !=3D virInterfaceObjIsActive(obj)) + goto cleanup; + + if (data->elems) { + if (VIR_STRDUP(data->elems[data->nElems], obj->def->name) < 0) { + data->error =3D true; + goto cleanup; } - virObjectUnlock(interfaces->objs[i]); } + + data->nElems++; + + cleanup: + virObjectUnlock(obj); + return 0; } =20 =20 @@ -298,18 +416,17 @@ int virInterfaceObjListNumOfInterfaces(virInterfaceObjListPtr interfaces, bool wantActive) { - size_t i; - int ninterfaces =3D 0; + struct _virInterfaceObjForEachData data =3D { .wantActive =3D wantActi= ve, + .error =3D false, + .nElems =3D 0, + .maxElems =3D -1, + .elems =3D NULL }; =20 - for (i =3D 0; (i < interfaces->count); i++) { - virInterfaceObjPtr obj =3D interfaces->objs[i]; - virObjectLock(obj); - if (wantActive =3D=3D virInterfaceObjIsActive(obj)) - ninterfaces++; - virObjectUnlock(obj); - } + virObjectRWLockRead(interfaces); + virHashForEach(interfaces->objsName, virInterfaceObjListForEachCb, &da= ta); + virObjectRWUnlock(interfaces); =20 - return ninterfaces; + return data.nElems; } =20 =20 @@ -319,30 +436,24 @@ virInterfaceObjListGetNames(virInterfaceObjListPtr in= terfaces, char **const names, int maxnames) { - int nnames =3D 0; - size_t i; + struct _virInterfaceObjForEachData data =3D { .wantActive =3D wantActi= ve, + .error =3D false, + .nElems =3D 0, + .maxElems =3D maxnames, + .elems =3D names }; =20 - for (i =3D 0; i < interfaces->count && nnames < maxnames; i++) { - virInterfaceObjPtr obj =3D interfaces->objs[i]; - virInterfaceDefPtr def; + virObjectRWLockRead(interfaces); + virHashForEach(interfaces->objsName, virInterfaceObjListForEachCb, &da= ta); + virObjectRWUnlock(interfaces); =20 - virObjectLock(obj); - def =3D obj->def; - if (wantActive =3D=3D virInterfaceObjIsActive(obj)) { - if (VIR_STRDUP(names[nnames], def->name) < 0) { - virObjectUnlock(obj); - goto failure; - } - nnames++; - } - virObjectUnlock(obj); - } + if (data.error) + goto error; =20 - return nnames; + return data.nElems; =20 - failure: - while (--nnames >=3D 0) - VIR_FREE(names[nnames]); + error: + while (--data.nElems >=3D 0) + VIR_FREE(data.elems[data.nElems]); =20 return -1; } diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 26c5ddb405..9133199895 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -932,7 +932,6 @@ virInterfaceObjListAssignDef; virInterfaceObjListClone; virInterfaceObjListFindByMACString; virInterfaceObjListFindByName; -virInterfaceObjListFree; virInterfaceObjListGetNames; virInterfaceObjListNew; virInterfaceObjListNumOfInterfaces; diff --git a/src/test/test_driver.c b/src/test/test_driver.c index 1c48347994..14bba7494c 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -154,7 +154,7 @@ testDriverFree(testDriverPtr driver) virObjectUnref(driver->domains); virNodeDeviceObjListFree(driver->devs); virObjectUnref(driver->networks); - virInterfaceObjListFree(driver->ifaces); + virObjectUnref(driver->ifaces); virStoragePoolObjListFree(&driver->pools); virObjectUnref(driver->eventState); virMutexUnlock(&driver->lock); @@ -3880,7 +3880,7 @@ testInterfaceChangeCommit(virConnectPtr conn, goto cleanup; } =20 - virInterfaceObjListFree(privconn->backupIfaces); + virObjectUnref(privconn->backupIfaces); privconn->transaction_running =3D false; =20 ret =3D 0; @@ -3910,7 +3910,7 @@ testInterfaceChangeRollback(virConnectPtr conn, goto cleanup; } =20 - virInterfaceObjListFree(privconn->ifaces); + virObjectUnref(privconn->ifaces); privconn->ifaces =3D privconn->backupIfaces; privconn->backupIfaces =3D NULL; =20 --=20 2.13.6 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list