[PATCH v2 07/23] src: add constants for guest info 'if.' parameters

Daniel P. Berrangé posted 23 patches 22 hours ago
[PATCH v2 07/23] src: add constants for guest info 'if.' parameters
Posted by Daniel P. Berrangé 22 hours ago
Contrary to most APIs returning typed parameters, there are no constants
defined for the guest info data keys. This is was because many of the
keys needs to be dynamically constructed using one or more array index
values.

It is possible to define constants while still supporting dynamic
array indexes by simply defining the prefixes and suffixes as constants.
The consuming code can then combine the constants with array index
value.

With this approach, it is practical to add constants for the guest info
API keys.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 include/libvirt/libvirt-domain.h | 88 ++++++++++++++++++++++++++++++++
 src/libvirt-domain.c             | 13 ++---
 src/qemu/qemu_driver.c           | 21 +++++---
 3 files changed, 104 insertions(+), 18 deletions(-)

diff --git a/include/libvirt/libvirt-domain.h b/include/libvirt/libvirt-domain.h
index fd7cf01243..38139ed8ec 100644
--- a/include/libvirt/libvirt-domain.h
+++ b/include/libvirt/libvirt-domain.h
@@ -6855,6 +6855,94 @@ int virDomainSetLaunchSecurityState(virDomainPtr domain,
  */
 #define VIR_DOMAIN_GUEST_INFO_DISK_SUFFIX_GUEST_BUS ".guest_bus"
 
+
+/**
+ * VIR_DOMAIN_GUEST_INFO_IF_COUNT:
+ *
+ * The number of interfaces defined on this domain as an unsigned int.
+ *
+ * Since: 11.2.0
+ */
+#define VIR_DOMAIN_GUEST_INFO_IF_COUNT "if.count"
+
+/**
+ * VIR_DOMAIN_GUEST_INFO_IF_PREFIX:
+ *
+ * The parameter name prefix to access each interface entry. Concatenate the
+ * prefix, the entry number formatted as an unsigned integer and one of the
+ * interface suffix parameters to form a complete parameter name.
+ *
+ * Since: 11.2.0
+ */
+#define VIR_DOMAIN_GUEST_INFO_IF_PREFIX "if."
+
+/**
+ * VIR_DOMAIN_GUEST_INFO_IF_SUFFIX_NAME:
+ *
+ * Name in the guest (e.g. ``eth0``) for the interface as a string.
+ *
+ * Since: 11.2.0
+ */
+#define VIR_DOMAIN_GUEST_INFO_IF_SUFFIX_NAME ".name"
+
+/**
+ * VIR_DOMAIN_GUEST_INFO_IF_SUFFIX_HWADDR:
+ *
+ * Hardware address in the guest of the interface as a string.
+ *
+ * Since: 11.2.0
+ */
+#define VIR_DOMAIN_GUEST_INFO_IF_SUFFIX_HWADDR ".hwaddr"
+
+/**
+ * VIR_DOMAIN_GUEST_INFO_IF_SUFFIX_ADDR_COUNT:
+ *
+ * The number of IP addresses of interface as an unsigned int.
+ *
+ * Since: 11.2.0
+ */
+#define VIR_DOMAIN_GUEST_INFO_IF_SUFFIX_ADDR_COUNT ".addr.count"
+
+/**
+ * VIR_DOMAIN_GUEST_INFO_IF_SUFFIX_ADDR_PREFIX:
+ *
+ * The parameter name prefix to access each address entry. Concatenate the
+ * interface prefix, the interface entry number formatted as an unsigned
+ * integer, the address prefix, the address entry number formatted as an
+ * unsigned integer and one of the address suffix parameters to form a
+ * complete parameter name.
+ *
+ * Since: 11.2.0
+ */
+#define VIR_DOMAIN_GUEST_INFO_IF_SUFFIX_ADDR_PREFIX ".addr."
+
+/**
+ * VIR_DOMAIN_GUEST_INFO_IF_SUFFIX_ADDR_SUFFIX_TYPE:
+ *
+ * The IP address type (e.g. ipv4) as a string.
+ *
+ * Since: 11.2.0
+ */
+#define VIR_DOMAIN_GUEST_INFO_IF_SUFFIX_ADDR_SUFFIX_TYPE ".type"
+
+/**
+ * VIR_DOMAIN_GUEST_INFO_IF_SUFFIX_ADDR_SUFFIX_ADDR:
+ *
+ * The IP address as a string.
+ *
+ * Since: 11.2.0
+ */
+#define VIR_DOMAIN_GUEST_INFO_IF_SUFFIX_ADDR_SUFFIX_ADDR ".addr"
+
+/**
+ * VIR_DOMAIN_GUEST_INFO_IF_SUFFIX_ADDR_SUFFIX_PREFIX:
+ *
+ * The prefix of IP address as an unsigned int.
+ *
+ * Since: 11.2.0
+ */
+#define VIR_DOMAIN_GUEST_INFO_IF_SUFFIX_ADDR_SUFFIX_PREFIX ".prefix"
+
 /**
  * virDomainGuestInfoTypes:
  *
diff --git a/src/libvirt-domain.c b/src/libvirt-domain.c
index 87ea7a28ad..4e91e7bd20 100644
--- a/src/libvirt-domain.c
+++ b/src/libvirt-domain.c
@@ -13233,16 +13233,9 @@ virDomainSetVcpu(virDomainPtr domain,
  *  keys.
  *
  * VIR_DOMAIN_GUEST_INFO_INTERFACES:
- *  Returns information about the interfaces within the domain. The typed
- *  parameter keys are in this format:
- *
- *      "if.count" - the number of interfaces defined on this domain
- *      "if.<num>.name" - name in the guest (e.g. ``eth0``) for interface <num>
- *      "if.<num>.hwaddr" - hardware address in the guest for interface <num>
- *      "if.<num>.addr.count - the number of IP addresses of interface <num>
- *      "if.<num>.addr.<num1>.type" - the IP address type of addr <num1> (e.g. ipv4)
- *      "if.<num>.addr.<num1>.addr" - the IP address of addr <num1>
- *      "if.<num>.addr.<num1>.prefix" - the prefix of IP address of addr <num1>
+ *  Returns information about the interfaces within the domain.
+ *  The VIR_DOMAIN_GUEST_INFO_IF_* constants define the known typed parameter
+ *  keys.
  *
  * VIR_DOMAIN_GUEST_INFO_LOAD:
  *  Returns load (the number of processes in the runqueue or waiting for disk
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 8f7e232abd..248787784e 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -19325,30 +19325,35 @@ virDomainInterfaceFormatParams(virDomainInterfacePtr *ifaces,
 {
     size_t i;
 
-    virTypedParamListAddUInt(list, nifaces, "if.count");
+    virTypedParamListAddUInt(list, nifaces, VIR_DOMAIN_GUEST_INFO_IF_COUNT);
 
     for (i = 0; i < nifaces; i++) {
         size_t j;
 
-        virTypedParamListAddString(list, ifaces[i]->name, "if.%zu.name", i);
-        virTypedParamListAddString(list, ifaces[i]->hwaddr, "if.%zu.hwaddr", i);
-        virTypedParamListAddUInt(list, ifaces[i]->naddrs, "if.%zu.addr.count", i);
+        virTypedParamListAddString(list, ifaces[i]->name,
+                                   VIR_DOMAIN_GUEST_INFO_IF_PREFIX "%zu" VIR_DOMAIN_GUEST_INFO_IF_SUFFIX_NAME, i);
+        virTypedParamListAddString(list, ifaces[i]->hwaddr,
+                                   VIR_DOMAIN_GUEST_INFO_IF_PREFIX "%zu" VIR_DOMAIN_GUEST_INFO_IF_SUFFIX_HWADDR, i);
+        virTypedParamListAddUInt(list, ifaces[i]->naddrs,
+                                 VIR_DOMAIN_GUEST_INFO_IF_PREFIX "%zu" VIR_DOMAIN_GUEST_INFO_IF_SUFFIX_ADDR_COUNT, i);
 
         for (j = 0; j < ifaces[i]->naddrs; j++) {
             switch (ifaces[i]->addrs[j].type) {
                 case VIR_IP_ADDR_TYPE_IPV4:
-                    virTypedParamListAddString(list, "ipv4", "if.%zu.addr.%zu.type", i, j);
+                    virTypedParamListAddString(list, "ipv4",
+                                               VIR_DOMAIN_GUEST_INFO_IF_PREFIX "%zu" VIR_DOMAIN_GUEST_INFO_IF_SUFFIX_ADDR_PREFIX "%zu" VIR_DOMAIN_GUEST_INFO_IF_SUFFIX_ADDR_SUFFIX_TYPE, i, j);
                     break;
 
                 case VIR_IP_ADDR_TYPE_IPV6:
-                    virTypedParamListAddString(list, "ipv6", "if.%zu.addr.%zu.type", i, j);
+                    virTypedParamListAddString(list, "ipv6",
+                                               VIR_DOMAIN_GUEST_INFO_IF_PREFIX "%zu" VIR_DOMAIN_GUEST_INFO_IF_SUFFIX_ADDR_PREFIX "%zu" VIR_DOMAIN_GUEST_INFO_IF_SUFFIX_ADDR_SUFFIX_TYPE, i, j);
                     break;
             }
 
             virTypedParamListAddString(list, ifaces[i]->addrs[j].addr,
-                                       "if.%zu.addr.%zu.addr", i, j);
+                                       VIR_DOMAIN_GUEST_INFO_IF_PREFIX "%zu" VIR_DOMAIN_GUEST_INFO_IF_SUFFIX_ADDR_PREFIX "%zu" VIR_DOMAIN_GUEST_INFO_IF_SUFFIX_ADDR_SUFFIX_ADDR, i, j);
             virTypedParamListAddUInt(list, ifaces[i]->addrs[j].prefix,
-                                     "if.%zu.addr.%zu.prefix", i, j);
+                                     VIR_DOMAIN_GUEST_INFO_IF_PREFIX "%zu" VIR_DOMAIN_GUEST_INFO_IF_SUFFIX_ADDR_PREFIX "%zu" VIR_DOMAIN_GUEST_INFO_IF_SUFFIX_ADDR_SUFFIX_PREFIX, i, j);
         }
     }
 }
-- 
2.48.1
Re: [PATCH v2 07/23] src: add constants for guest info 'if.' parameters
Posted by Peter Krempa 20 hours ago
On Tue, Mar 11, 2025 at 14:24:13 +0000, Daniel P. Berrangé wrote:
> Contrary to most APIs returning typed parameters, there are no constants
> defined for the guest info data keys. This is was because many of the
> keys needs to be dynamically constructed using one or more array index
> values.
> 
> It is possible to define constants while still supporting dynamic
> array indexes by simply defining the prefixes and suffixes as constants.
> The consuming code can then combine the constants with array index
> value.
> 
> With this approach, it is practical to add constants for the guest info
> API keys.
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>  include/libvirt/libvirt-domain.h | 88 ++++++++++++++++++++++++++++++++
>  src/libvirt-domain.c             | 13 ++---
>  src/qemu/qemu_driver.c           | 21 +++++---
>  3 files changed, 104 insertions(+), 18 deletions(-)

Reviewed-by: Peter Krempa <pkrempa@redhat.com>