From nobody Thu May 15 03:56:39 2025 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 1516872300040369.0572402287455; Thu, 25 Jan 2018 01:25:00 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 39BA2C058ECC; Thu, 25 Jan 2018 09:24:59 +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 18BC06056B; Thu, 25 Jan 2018 09:24:59 +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 D68F23FB18; Thu, 25 Jan 2018 09:24:58 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w0P9OvJB000333 for ; Thu, 25 Jan 2018 04:24:57 -0500 Received: by smtp.corp.redhat.com (Postfix) id 1B0872C316; Thu, 25 Jan 2018 09:24:57 +0000 (UTC) Received: from beluga.usersys.redhat.com (unknown [10.43.2.166]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4CAC065608; Thu, 25 Jan 2018 09:24:47 +0000 (UTC) From: Erik Skultety To: libvir-list@redhat.com Date: Thu, 25 Jan 2018 10:23:54 +0100 Message-Id: In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Cc: Erik Skultety Subject: [libvirt] [PATCH 07/15] nodedev: Introduce virNodeDeviceCapsListExport 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.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Thu, 25 Jan 2018 09:24:59 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Whether asking for a number of capabilities supported by a device or listing them, it's handled essentially by a copy-paste code, so extract the common stuff into this new helper which also updates all capabilities just before touching them. Signed-off-by: Erik Skultety --- src/conf/node_device_conf.c | 73 ++++++++++++++++++++++++++++++++= +++ src/conf/node_device_conf.h | 5 +++ src/libvirt_private.syms | 1 + src/node_device/node_device_driver.c | 75 +++++++++-----------------------= ---- 4 files changed, 97 insertions(+), 57 deletions(-) diff --git a/src/conf/node_device_conf.c b/src/conf/node_device_conf.c index 217673a56..9467bb415 100644 --- a/src/conf/node_device_conf.c +++ b/src/conf/node_device_conf.c @@ -2487,6 +2487,79 @@ virNodeDeviceUpdateCaps(virNodeDeviceDefPtr def) } =20 =20 +/** + * virNodeDeviceCapsListExport: + * @def: node device definition + * @list: pointer to an array to store all supported capabilities by a dev= ice + * + * Takes the definition, scans through all the capabilities that the device + * supports (including the nested caps) and populates a newly allocated li= st + * with them. Caller is responsible for freeing the list. + * If NULL is passed to @list, only the number of caps will be returned. + * + * Returns the number of capabilities the device supports, -1 on error. + */ +int +virNodeDeviceCapsListExport(virNodeDeviceDefPtr def, + virNodeDevCapType **list) +{ + virNodeDevCapsDefPtr caps =3D NULL; + virNodeDevCapType *tmp =3D NULL; + bool want_list =3D !!list; + int ncaps =3D 0; + int ret =3D -1; + +#define MAYBE_ADD_CAP(cap) \ + do { \ + if (want_list) \ + tmp[ncaps] =3D cap; \ + } while (0) + + if (want_list && VIR_ALLOC_N(tmp, VIR_NODE_DEV_CAP_LAST - 1) < 0) + goto cleanup; + + for (caps =3D def->caps; caps; caps =3D caps->next) { + unsigned int flags; + + MAYBE_ADD_CAP(caps->data.type); + ncaps++; + + /* check nested caps for a given type as well */ + if (caps->data.type =3D=3D VIR_NODE_DEV_CAP_SCSI_HOST) { + flags =3D caps->data.scsi_host.flags; + + if (flags & VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST) { + MAYBE_ADD_CAP(VIR_NODE_DEV_CAP_FC_HOST); + ncaps++; + } + + if (flags & VIR_NODE_DEV_CAP_FLAG_HBA_VPORT_OPS) { + MAYBE_ADD_CAP(VIR_NODE_DEV_CAP_VPORTS); + ncaps++; + } + } + + if (caps->data.type =3D=3D VIR_NODE_DEV_CAP_PCI_DEV) { + flags =3D caps->data.pci_dev.flags; + + if (flags & VIR_NODE_DEV_CAP_FLAG_PCI_MDEV) { + MAYBE_ADD_CAP(VIR_NODE_DEV_CAP_MDEV_TYPES); + ncaps++; + } + } + } + +#undef MAYBE_ADD_CAP + + if (want_list) + VIR_STEAL_PTR(*list, tmp); + ret =3D ncaps; + cleanup: + VIR_FREE(tmp); + return ret; +} + + #ifdef __linux__ =20 int diff --git a/src/conf/node_device_conf.h b/src/conf/node_device_conf.h index 7e32f5c05..53cdfdb01 100644 --- a/src/conf/node_device_conf.h +++ b/src/conf/node_device_conf.h @@ -403,4 +403,9 @@ virNodeDeviceGetPCIDynamicCaps(const char *sysfsPath, =20 int virNodeDeviceUpdateCaps(virNodeDeviceDefPtr def); + +int +virNodeDeviceCapsListExport(virNodeDeviceDefPtr def, + virNodeDevCapType **list); + #endif /* __VIR_NODE_DEVICE_CONF_H__ */ diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 6098cf121..1698e6227 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -699,6 +699,7 @@ virNodeDevCapMdevTypeFree; virNodeDevCapsDefFree; virNodeDevCapTypeFromString; virNodeDevCapTypeToString; +virNodeDeviceCapsListExport; virNodeDeviceCreateVport; virNodeDeviceDefFormat; virNodeDeviceDefFree; diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_de= vice_driver.c index 48f45474c..8fb08742b 100644 --- a/src/node_device/node_device_driver.c +++ b/src/node_device/node_device_driver.c @@ -306,8 +306,6 @@ nodeDeviceNumOfCaps(virNodeDevicePtr device) { virNodeDeviceObjPtr obj; virNodeDeviceDefPtr def; - virNodeDevCapsDefPtr caps; - int ncaps =3D 0; int ret =3D -1; =20 if (!(obj =3D nodeDeviceObjFindByName(device->name))) @@ -317,27 +315,7 @@ nodeDeviceNumOfCaps(virNodeDevicePtr device) if (virNodeDeviceNumOfCapsEnsureACL(device->conn, def) < 0) goto cleanup; =20 - for (caps =3D def->caps; caps; caps =3D caps->next) { - ++ncaps; - - if (caps->data.type =3D=3D VIR_NODE_DEV_CAP_SCSI_HOST) { - if (caps->data.scsi_host.flags & - VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST) - ncaps++; - - if (caps->data.scsi_host.flags & - VIR_NODE_DEV_CAP_FLAG_HBA_VPORT_OPS) - ncaps++; - } - if (caps->data.type =3D=3D VIR_NODE_DEV_CAP_PCI_DEV) { - if (caps->data.pci_dev.flags & - VIR_NODE_DEV_CAP_FLAG_PCI_MDEV) - ncaps++; - } - - } - - ret =3D ncaps; + ret =3D virNodeDeviceCapsListExport(def, NULL); =20 cleanup: virNodeDeviceObjEndAPI(&obj); @@ -353,9 +331,10 @@ nodeDeviceListCaps(virNodeDevicePtr device, { virNodeDeviceObjPtr obj; virNodeDeviceDefPtr def; - virNodeDevCapsDefPtr caps; + virNodeDevCapType *list =3D NULL; int ncaps =3D 0; int ret =3D -1; + size_t i; =20 if (!(obj =3D nodeDeviceObjFindByName(device->name))) return -1; @@ -364,46 +343,28 @@ nodeDeviceListCaps(virNodeDevicePtr device, if (virNodeDeviceListCapsEnsureACL(device->conn, def) < 0) goto cleanup; =20 - for (caps =3D def->caps; caps && ncaps < maxnames; caps =3D caps->next= ) { - if (VIR_STRDUP(names[ncaps++], virNodeDevCapTypeToString(caps->dat= a.type)) < 0) + if ((ncaps =3D virNodeDeviceCapsListExport(def, &list)) < 0) + goto cleanup; + + if (ncaps > maxnames) + ncaps =3D maxnames; + + for (i =3D 0; i < ncaps; i++) { + if (VIR_STRDUP(names[i], virNodeDevCapTypeToString(list[i])) < 0) goto cleanup; - - if (caps->data.type =3D=3D VIR_NODE_DEV_CAP_SCSI_HOST) { - if (ncaps < maxnames && - caps->data.scsi_host.flags & - VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST) { - if (VIR_STRDUP(names[ncaps++], - virNodeDevCapTypeToString(VIR_NODE_DEV_CAP_= FC_HOST)) < 0) - goto cleanup; - } - - if (ncaps < maxnames && - caps->data.scsi_host.flags & - VIR_NODE_DEV_CAP_FLAG_HBA_VPORT_OPS) { - if (VIR_STRDUP(names[ncaps++], - virNodeDevCapTypeToString(VIR_NODE_DEV_CAP_= VPORTS)) < 0) - goto cleanup; - } - } - if (caps->data.type =3D=3D VIR_NODE_DEV_CAP_PCI_DEV) { - if (ncaps < maxnames && - caps->data.pci_dev.flags & - VIR_NODE_DEV_CAP_FLAG_PCI_MDEV) { - if (VIR_STRDUP(names[ncaps++], - virNodeDevCapTypeToString(VIR_NODE_DEV_CAP_= MDEV_TYPES)) < 0) - goto cleanup; - } - } } + ret =3D ncaps; =20 cleanup: virNodeDeviceObjEndAPI(&obj); - if (ret =3D=3D -1) { - --ncaps; - while (--ncaps >=3D 0) - VIR_FREE(names[ncaps]); + if (ret < 0) { + size_t j; + for (j =3D 0; j < i; j++) + VIR_FREE(names[j]); } + + VIR_FREE(list); return ret; } =20 --=20 2.13.6 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list