[PATCH 13/17] virDomainTPMDefParseXML: Switch to virXMLPropEnumDefault()

Michal Privoznik mprivozn at redhat.com
Mon May 23 13:08:48 UTC 2022


The virDomainTPMDefParseXML() function uses old style of parsing
XML (virXMLPropString + str2enum conversion). Use
virXMLPropEnumDefault() which encapsulates those steps.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 src/conf/domain_conf.c   | 43 ++++++++++++----------------------------
 src/conf/domain_conf.h   |  6 +++---
 src/qemu/qemu_command.c  |  2 +-
 src/qemu/qemu_domain.c   |  2 +-
 src/qemu/qemu_validate.c |  1 +
 5 files changed, 19 insertions(+), 35 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index b5ce80eb76..4044515bdc 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -11677,9 +11677,6 @@ virDomainTPMDefParseXML(virDomainXMLOption *xmlopt,
     int nnodes;
     size_t i;
     g_autofree char *path = NULL;
-    g_autofree char *model = NULL;
-    g_autofree char *backend = NULL;
-    g_autofree char *version = NULL;
     g_autofree char *secretuuid = NULL;
     g_autofree char *persistent_state = NULL;
     g_autofree xmlNodePtr *backends = NULL;
@@ -11688,13 +11685,11 @@ virDomainTPMDefParseXML(virDomainXMLOption *xmlopt,
 
     def = g_new0(virDomainTPMDef, 1);
 
-    model = virXMLPropString(node, "model");
-    if (model != NULL &&
-        (def->model = virDomainTPMModelTypeFromString(model)) < 0) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                       _("Unknown TPM frontend model '%s'"), model);
+    if (virXMLPropEnum(node, "model",
+                       virDomainTPMModelTypeFromString,
+                       VIR_XML_PROP_NONE,
+                       &def->model) < 0)
         goto error;
-    }
 
     ctxt->node = node;
 
@@ -11713,30 +11708,18 @@ virDomainTPMDefParseXML(virDomainXMLOption *xmlopt,
         goto error;
     }
 
-    if (!(backend = virXMLPropString(backends[0], "type"))) {
-        virReportError(VIR_ERR_XML_ERROR, "%s",
-                       _("missing TPM device backend type"));
+    if (virXMLPropEnum(backends[0], "type",
+                       virDomainTPMBackendTypeFromString,
+                       VIR_XML_PROP_REQUIRED,
+                       &def->type) < 0)
         goto error;
-    }
 
-    if ((def->type = virDomainTPMBackendTypeFromString(backend)) < 0) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                       _("Unknown TPM backend type '%s'"),
-                       backend);
+    if (virXMLPropEnumDefault(backends[0], "version",
+                              virDomainTPMVersionTypeFromString,
+                              VIR_XML_PROP_NONE,
+                              &def->version,
+                              VIR_DOMAIN_TPM_VERSION_DEFAULT) < 0)
         goto error;
-    }
-
-    version = virXMLPropString(backends[0], "version");
-    if (!version) {
-        def->version = VIR_DOMAIN_TPM_VERSION_DEFAULT;
-    } else {
-        if ((def->version = virDomainTPMVersionTypeFromString(version)) < 0) {
-            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                           _("Unsupported TPM version '%s'"),
-                           version);
-            goto error;
-        }
-    }
 
     switch (def->type) {
     case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index a81fb09678..13ffdbfe88 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -1423,10 +1423,10 @@ typedef enum {
 #define VIR_DOMAIN_TPM_DEFAULT_DEVICE "/dev/tpm0"
 
 struct _virDomainTPMDef {
-    int type; /* virDomainTPMBackendType */
+    virDomainTPMBackendType type;
     virDomainDeviceInfo info;
-    int model; /* virDomainTPMModel */
-    int version; /* virDomainTPMVersion */
+    virDomainTPMModel model;
+    virDomainTPMVersion version;
     union {
         struct {
             virDomainChrSourceDef *source;
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index 365b7d8292..6f716c63a0 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -9805,7 +9805,7 @@ qemuBuildTPMCommandLine(virCommand *cmd,
     g_autoptr(qemuFDPass) passtpm = NULL;
     g_autoptr(qemuFDPass) passcancel = NULL;
 
-    switch ((virDomainTPMBackendType) tpm->type) {
+    switch (tpm->type) {
     case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH: {
         VIR_AUTOCLOSE fdtpm = -1;
         VIR_AUTOCLOSE fdcancel = -1;
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 3432c83153..d3da566bae 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -11737,7 +11737,7 @@ qemuDomainDeviceBackendChardevForeachOne(virDomainDeviceDef *dev,
         return cb(dev, dev->data.rng->source.chardev, opaque);
 
     case VIR_DOMAIN_DEVICE_TPM:
-        switch ((virDomainTPMBackendType) dev->data.tpm->type) {
+        switch (dev->data.tpm->type) {
         case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
             return cb(dev, dev->data.tpm->data.passthrough.source, opaque);
 
diff --git a/src/qemu/qemu_validate.c b/src/qemu/qemu_validate.c
index 4d6355741e..8842e2f837 100644
--- a/src/qemu/qemu_validate.c
+++ b/src/qemu/qemu_validate.c
@@ -4728,6 +4728,7 @@ qemuValidateDomainDeviceDefTPM(virDomainTPMDef *tpm,
 
         flag = QEMU_CAPS_DEVICE_SPAPR_TPM_PROXY;
         break;
+    case VIR_DOMAIN_TPM_MODEL_DEFAULT:
     case VIR_DOMAIN_TPM_MODEL_LAST:
     default:
         virReportEnumRangeError(virDomainTPMModel, tpm->model);
-- 
2.35.1



More information about the libvir-list mailing list