[PATCH v2] conf: Report alias name of the detached device in error

Kristina Hanicova posted 1 patch 2 years, 11 months ago
Test syntax-check failed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/51a288360bb499ea379e77785f9d3851d14bc4c4.1621519824.git.khanicov@redhat.com
src/conf/domain_conf.c | 67 ++++++++++++++++--------------------------
1 file changed, 25 insertions(+), 42 deletions(-)
[PATCH v2] conf: Report alias name of the detached device in error
Posted by Kristina Hanicova 2 years, 11 months ago
This is v2 from:
https://listman.redhat.com/archives/libvir-list/2021-May/msg00481.html

I have reworked the code a bit to have only one error report
instead of multiple ones with different combinations of possible
matching items. Suggested by Laine.

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1942367

Signed-off-by: Kristina Hanicova <khanicov@redhat.com>
---
 src/conf/domain_conf.c | 67 ++++++++++++++++--------------------------
 1 file changed, 25 insertions(+), 42 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index b3ed3a9c5a..9631e40815 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -15645,6 +15645,12 @@ virDomainNetFindIdx(virDomainDef *def, virDomainNetDef *net)
                                                           VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI);
     bool CCWAddrSpecified = virDomainDeviceAddressIsValid(&net->info,
                                                           VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCW);
+    g_autofree char *addr = NULL;
+    const char *macAddr = _("(<null>)");
+    const char *alias = _("(<null>)");
+
+    if (MACAddrSpecified)
+        macAddr = virMacAddrFormat(&net->mac, mac);
 
     for (i = 0; i < def->nnets; i++) {
         if (MACAddrSpecified &&
@@ -15676,7 +15682,7 @@ virDomainNetFindIdx(virDomainDef *def, virDomainNetDef *net)
             if (MACAddrSpecified) {
                 virReportError(VIR_ERR_OPERATION_FAILED,
                                _("multiple devices matching MAC address %s found"),
-                               virMacAddrFormat(&net->mac, mac));
+                               macAddr);
             } else {
                 virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                                _("multiple matching devices found"));
@@ -15688,47 +15694,24 @@ virDomainNetFindIdx(virDomainDef *def, virDomainNetDef *net)
         matchidx = i;
     }
 
-    if (matchidx < 0) {
-        if (MACAddrSpecified && PCIAddrSpecified) {
-            virReportError(VIR_ERR_DEVICE_MISSING,
-                           _("no device matching MAC address %s found on "
-                             VIR_PCI_DEVICE_ADDRESS_FMT),
-                           virMacAddrFormat(&net->mac, mac),
-                           net->info.addr.pci.domain,
-                           net->info.addr.pci.bus,
-                           net->info.addr.pci.slot,
-                           net->info.addr.pci.function);
-        } else if (MACAddrSpecified && CCWAddrSpecified) {
-            virReportError(VIR_ERR_DEVICE_MISSING,
-                           _("no device matching MAC address %s found on "
-                             VIR_CCW_DEVICE_ADDRESS_FMT),
-                           virMacAddrFormat(&net->mac, mac),
-                           net->info.addr.ccw.cssid,
-                           net->info.addr.ccw.ssid,
-                           net->info.addr.ccw.devno);
-        } else if (PCIAddrSpecified) {
-            virReportError(VIR_ERR_DEVICE_MISSING,
-                           _("no device found on " VIR_PCI_DEVICE_ADDRESS_FMT),
-                           net->info.addr.pci.domain,
-                           net->info.addr.pci.bus,
-                           net->info.addr.pci.slot,
-                           net->info.addr.pci.function);
-        } else if (CCWAddrSpecified) {
-            virReportError(VIR_ERR_DEVICE_MISSING,
-                           _("no device found on " VIR_CCW_DEVICE_ADDRESS_FMT),
-                           net->info.addr.ccw.cssid,
-                           net->info.addr.ccw.ssid,
-                           net->info.addr.ccw.devno);
-        } else if (MACAddrSpecified) {
-            virReportError(VIR_ERR_DEVICE_MISSING,
-                           _("no device matching MAC address %s found"),
-                           virMacAddrFormat(&net->mac, mac));
-        } else {
-            virReportError(VIR_ERR_DEVICE_MISSING, "%s",
-                           _("no matching device found"));
-        }
-    }
-    return matchidx;
+    if (matchidx >= 0)
+        return matchidx;
+
+    if (net->info.alias)
+        alias = net->info.alias;
+
+    if (CCWAddrSpecified)
+        addr = virDomainCCWAddressAsString(&net->info.addr.ccw);
+    else if (PCIAddrSpecified)
+        addr = virPCIDeviceAddressAsString(&net->info.addr.pci);
+    else
+        addr = g_strdup(_("(<null>)"));
+
+    virReportError(VIR_ERR_DEVICE_MISSING,
+                   _("no device found at address '%s' matching MAC address"
+                     " '%s' and alias '%s'"),
+                   addr, macAddr, alias);
+    return -1;
 }
 
 bool
-- 
2.31.1

Re: [PATCH v2] conf: Report alias name of the detached device in error
Posted by Michal Prívozník 2 years, 11 months ago
On 5/20/21 4:13 PM, Kristina Hanicova wrote:
> This is v2 from:
> https://listman.redhat.com/archives/libvir-list/2021-May/msg00481.html

This is something we usually put [1]

> 
> I have reworked the code a bit to have only one error report
> instead of multiple ones with different combinations of possible
> matching items. Suggested by Laine.
> 
> Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1942367
> 
> Signed-off-by: Kristina Hanicova <khanicov@redhat.com>
> ---

1: here. Right after these three dashes ^^^ and before diffstat.
Everything that's above the dashes is a commit message, and everything
that's after diff stat is patch, leaving this place for comments. BTW:
this is also the place where git-notes would format notes (were there any).

>  src/conf/domain_conf.c | 67 ++++++++++++++++--------------------------
>  1 file changed, 25 insertions(+), 42 deletions(-)

The patch looks good to me. I'll let Laine to express himself too since
he was the one who suggested this approach.

Michal

Re: [PATCH v2] conf: Report alias name of the detached device in error
Posted by Laine Stump 2 years, 11 months ago
On 5/20/21 10:13 AM, Kristina Hanicova wrote:
> This is v2 from:
> https://listman.redhat.com/archives/libvir-list/2021-May/msg00481.html
> 
> I have reworked the code a bit to have only one error report
> instead of multiple ones with different combinations of possible
> matching items. Suggested by Laine.
> 
> Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1942367
> 
> Signed-off-by: Kristina Hanicova <khanicov@redhat.com>

Looks okay to me.

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

I'll push it shortly.

> ---
>   src/conf/domain_conf.c | 67 ++++++++++++++++--------------------------
>   1 file changed, 25 insertions(+), 42 deletions(-)
> 
> diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
> index b3ed3a9c5a..9631e40815 100644
> --- a/src/conf/domain_conf.c
> +++ b/src/conf/domain_conf.c
> @@ -15645,6 +15645,12 @@ virDomainNetFindIdx(virDomainDef *def, virDomainNetDef *net)
>                                                             VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI);
>       bool CCWAddrSpecified = virDomainDeviceAddressIsValid(&net->info,
>                                                             VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCW);
> +    g_autofree char *addr = NULL;
> +    const char *macAddr = _("(<null>)");
> +    const char *alias = _("(<null>)");
> +
> +    if (MACAddrSpecified)
> +        macAddr = virMacAddrFormat(&net->mac, mac);
>   
>       for (i = 0; i < def->nnets; i++) {
>           if (MACAddrSpecified &&
> @@ -15676,7 +15682,7 @@ virDomainNetFindIdx(virDomainDef *def, virDomainNetDef *net)
>               if (MACAddrSpecified) {
>                   virReportError(VIR_ERR_OPERATION_FAILED,
>                                  _("multiple devices matching MAC address %s found"),
> -                               virMacAddrFormat(&net->mac, mac));
> +                               macAddr);
>               } else {
>                   virReportError(VIR_ERR_OPERATION_FAILED, "%s",
>                                  _("multiple matching devices found"));
> @@ -15688,47 +15694,24 @@ virDomainNetFindIdx(virDomainDef *def, virDomainNetDef *net)
>           matchidx = i;
>       }
>   
> -    if (matchidx < 0) {
> -        if (MACAddrSpecified && PCIAddrSpecified) {
> -            virReportError(VIR_ERR_DEVICE_MISSING,
> -                           _("no device matching MAC address %s found on "
> -                             VIR_PCI_DEVICE_ADDRESS_FMT),
> -                           virMacAddrFormat(&net->mac, mac),
> -                           net->info.addr.pci.domain,
> -                           net->info.addr.pci.bus,
> -                           net->info.addr.pci.slot,
> -                           net->info.addr.pci.function);
> -        } else if (MACAddrSpecified && CCWAddrSpecified) {
> -            virReportError(VIR_ERR_DEVICE_MISSING,
> -                           _("no device matching MAC address %s found on "
> -                             VIR_CCW_DEVICE_ADDRESS_FMT),
> -                           virMacAddrFormat(&net->mac, mac),
> -                           net->info.addr.ccw.cssid,
> -                           net->info.addr.ccw.ssid,
> -                           net->info.addr.ccw.devno);
> -        } else if (PCIAddrSpecified) {
> -            virReportError(VIR_ERR_DEVICE_MISSING,
> -                           _("no device found on " VIR_PCI_DEVICE_ADDRESS_FMT),
> -                           net->info.addr.pci.domain,
> -                           net->info.addr.pci.bus,
> -                           net->info.addr.pci.slot,
> -                           net->info.addr.pci.function);
> -        } else if (CCWAddrSpecified) {
> -            virReportError(VIR_ERR_DEVICE_MISSING,
> -                           _("no device found on " VIR_CCW_DEVICE_ADDRESS_FMT),
> -                           net->info.addr.ccw.cssid,
> -                           net->info.addr.ccw.ssid,
> -                           net->info.addr.ccw.devno);
> -        } else if (MACAddrSpecified) {
> -            virReportError(VIR_ERR_DEVICE_MISSING,
> -                           _("no device matching MAC address %s found"),
> -                           virMacAddrFormat(&net->mac, mac));
> -        } else {
> -            virReportError(VIR_ERR_DEVICE_MISSING, "%s",
> -                           _("no matching device found"));
> -        }
> -    }
> -    return matchidx;
> +    if (matchidx >= 0)
> +        return matchidx;
> +
> +    if (net->info.alias)
> +        alias = net->info.alias;
> +
> +    if (CCWAddrSpecified)
> +        addr = virDomainCCWAddressAsString(&net->info.addr.ccw);
> +    else if (PCIAddrSpecified)
> +        addr = virPCIDeviceAddressAsString(&net->info.addr.pci);
> +    else
> +        addr = g_strdup(_("(<null>)"));
> +
> +    virReportError(VIR_ERR_DEVICE_MISSING,
> +                   _("no device found at address '%s' matching MAC address"
> +                     " '%s' and alias '%s'"),
> +                   addr, macAddr, alias);
> +    return -1;
>   }
>   
>   bool
>