[PATCH RESEND 05/20] virpci: introduce virPCIDeviceExists()

Daniel Henrique Barboza posted 20 patches 5 years ago
[PATCH RESEND 05/20] virpci: introduce virPCIDeviceExists()
Posted by Daniel Henrique Barboza 5 years ago
We're going to add logic to handle the case where a previously
existing PCI device does not longer exist in the host.

The logic was copied from virPCIDeviceNew(), which verifies if a
PCI device exists in the host, returning NULL and throwing an
error if it doesn't. The NULL is used for other errors as well
(product/vendor id read errors, dev id overflow), meaning that we
can't re-use virPCIDeviceNew() for the purpose of detecting
if the device exists.

Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
---
 src/libvirt_private.syms |  1 +
 src/util/virpci.c        | 10 ++++++++++
 src/util/virpci.h        |  1 +
 3 files changed, 12 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 55284577b0..36635e6de7 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2819,6 +2819,7 @@ virPCIDeviceAddressIsValid;
 virPCIDeviceAddressParse;
 virPCIDeviceCopy;
 virPCIDeviceDetach;
+virPCIDeviceExists;
 virPCIDeviceFileIterate;
 virPCIDeviceFree;
 virPCIDeviceGetAddress;
diff --git a/src/util/virpci.c b/src/util/virpci.c
index 5a91553b5f..7143380348 100644
--- a/src/util/virpci.c
+++ b/src/util/virpci.c
@@ -1448,6 +1448,16 @@ virPCIDeviceAddressAsString(const virPCIDeviceAddress *addr)
     return str;
 }
 
+bool
+virPCIDeviceExists(const virPCIDeviceAddress *addr)
+{
+    g_autofree char *devName = virPCIDeviceAddressAsString(addr);
+    g_autofree char *devPath = g_strdup_printf(PCI_SYSFS "devices/%s/config",
+                                               devName);
+
+    return virFileExists(devPath);
+}
+
 virPCIDevicePtr
 virPCIDeviceNew(const virPCIDeviceAddress *address)
 {
diff --git a/src/util/virpci.h b/src/util/virpci.h
index d4451848c1..a9c597a428 100644
--- a/src/util/virpci.h
+++ b/src/util/virpci.h
@@ -201,6 +201,7 @@ int virPCIDeviceAddressGetIOMMUGroupAddresses(virPCIDeviceAddressPtr devAddr,
                                               size_t *nIommuGroupDevices);
 int virPCIDeviceAddressGetIOMMUGroupNum(virPCIDeviceAddressPtr addr);
 char *virPCIDeviceAddressGetIOMMUGroupDev(const virPCIDeviceAddress *devAddr);
+bool virPCIDeviceExists(const virPCIDeviceAddress *addr);
 char *virPCIDeviceGetIOMMUGroupDev(virPCIDevicePtr dev);
 
 int virPCIDeviceIsAssignable(virPCIDevicePtr dev,
-- 
2.26.2

Re: [PATCH RESEND 05/20] virpci: introduce virPCIDeviceExists()
Posted by Laine Stump 5 years ago
On 1/18/21 2:53 PM, Daniel Henrique Barboza wrote:
> We're going to add logic to handle the case where a previously
> existing PCI device does not longer exist in the host.
>
> The logic was copied from virPCIDeviceNew(), which verifies if a
> PCI device exists in the host, returning NULL and throwing an
> error if it doesn't. The NULL is used for other errors as well
> (product/vendor id read errors, dev id overflow), meaning that we
> can't re-use virPCIDeviceNew() for the purpose of detecting
> if the device exists.
>
> Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
> ---
>   src/libvirt_private.syms |  1 +
>   src/util/virpci.c        | 10 ++++++++++
>   src/util/virpci.h        |  1 +
>   3 files changed, 12 insertions(+)
>
> diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
> index 55284577b0..36635e6de7 100644
> --- a/src/libvirt_private.syms
> +++ b/src/libvirt_private.syms
> @@ -2819,6 +2819,7 @@ virPCIDeviceAddressIsValid;
>   virPCIDeviceAddressParse;
>   virPCIDeviceCopy;
>   virPCIDeviceDetach;
> +virPCIDeviceExists;
>   virPCIDeviceFileIterate;
>   virPCIDeviceFree;
>   virPCIDeviceGetAddress;
> diff --git a/src/util/virpci.c b/src/util/virpci.c
> index 5a91553b5f..7143380348 100644
> --- a/src/util/virpci.c
> +++ b/src/util/virpci.c
> @@ -1448,6 +1448,16 @@ virPCIDeviceAddressAsString(const virPCIDeviceAddress *addr)
>       return str;
>   }
>   
> +bool
> +virPCIDeviceExists(const virPCIDeviceAddress *addr)
> +{
> +    g_autofree char *devName = virPCIDeviceAddressAsString(addr);
> +    g_autofree char *devPath = g_strdup_printf(PCI_SYSFS "devices/%s/config",
> +                                               devName);


This same string is used at the top of virPCIDeviceNew(). Might be good 
to have a short (static) utility function virPCIDeviceConfigFileName() 
function.


But, as with most of my comments, that's for later.


Reviewed-by: Laine Stump <laine@redhat.com>


> +
> +    return virFileExists(devPath);
> +}
> +
>   virPCIDevicePtr
>   virPCIDeviceNew(const virPCIDeviceAddress *address)
>   {
> diff --git a/src/util/virpci.h b/src/util/virpci.h
> index d4451848c1..a9c597a428 100644
> --- a/src/util/virpci.h
> +++ b/src/util/virpci.h
> @@ -201,6 +201,7 @@ int virPCIDeviceAddressGetIOMMUGroupAddresses(virPCIDeviceAddressPtr devAddr,
>                                                 size_t *nIommuGroupDevices);
>   int virPCIDeviceAddressGetIOMMUGroupNum(virPCIDeviceAddressPtr addr);
>   char *virPCIDeviceAddressGetIOMMUGroupDev(const virPCIDeviceAddress *devAddr);
> +bool virPCIDeviceExists(const virPCIDeviceAddress *addr);
>   char *virPCIDeviceGetIOMMUGroupDev(virPCIDevicePtr dev);
>   
>   int virPCIDeviceIsAssignable(virPCIDevicePtr dev,