]> xenbits.xensource.com Git - libvirt.git/commitdiff
conf: Use virXMLPropEnum more when parsing TPM
authorMichal Privoznik <mprivozn@redhat.com>
Mon, 18 Jul 2022 07:00:40 +0000 (09:00 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Mon, 1 Aug 2022 15:35:51 +0000 (17:35 +0200)
When parsing a TPM device plenty of virXMLPropString() +
enum2int() combos are used. These can be replaced with
virXMLPropEnum().

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
src/conf/domain_conf.c
src/conf/domain_conf.h
src/qemu/qemu_command.c
src/qemu/qemu_domain.c

index 62286baf27f90171f3b09bb136ab4e26cbec9fd5..c60c4d3d6cc0e707951e39ee58cd914eadd31069 100644 (file)
@@ -10364,9 +10364,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;
@@ -10375,13 +10372,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_NONZERO,
+                       &def->model) < 0)
         goto error;
-    }
 
     ctxt->node = node;
 
@@ -10400,18 +10395,11 @@ virDomainTPMDefParseXML(virDomainXMLOption *xmlopt,
         goto error;
     }
 
-    if (!(backend = virXMLPropString(backends[0], "type"))) {
-        virReportError(VIR_ERR_XML_ERROR, "%s",
-                       _("missing TPM device backend type"));
-        goto error;
-    }
-
-    if ((def->type = virDomainTPMBackendTypeFromString(backend)) < 0) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                       _("Unknown TPM backend type '%s'"),
-                       backend);
+    if (virXMLPropEnum(backends[0], "type",
+                       virDomainTPMBackendTypeFromString,
+                       VIR_XML_PROP_REQUIRED,
+                       &def->type) < 0)
         goto error;
-    }
 
     switch (def->type) {
     case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
@@ -10424,14 +10412,11 @@ virDomainTPMDefParseXML(virDomainXMLOption *xmlopt,
         def->data.passthrough.source->data.file.path = g_steal_pointer(&path);
         break;
     case VIR_DOMAIN_TPM_TYPE_EMULATOR:
-        version = virXMLPropString(backends[0], "version");
-        if (version &&
-            (def->data.emulator.version = virDomainTPMVersionTypeFromString(version)) <= 0) {
-            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                           _("Unsupported TPM version '%s'"),
-                           version);
+        if (virXMLPropEnum(backends[0], "version",
+                           virDomainTPMVersionTypeFromString,
+                           VIR_XML_PROP_NONZERO,
+                           &def->data.emulator.version) < 0)
             goto error;
-        }
 
         if (!(def->data.emulator.source = virDomainChrSourceDefNew(xmlopt)))
             goto error;
index e2874214d376f1e55c87aa60495357137b75d605..200a75d705c4078ef91f2152aea57de59b529f52 100644 (file)
@@ -1436,15 +1436,15 @@ typedef enum {
 #define VIR_DOMAIN_TPM_DEFAULT_DEVICE "/dev/tpm0"
 
 struct _virDomainTPMDef {
-    int type; /* virDomainTPMBackendType */
+    virDomainTPMModel model;
+    virDomainTPMBackendType type;
     virDomainDeviceInfo info;
-    int model; /* virDomainTPMModel */
     union {
         struct {
             virDomainChrSourceDef *source;
         } passthrough;
         struct {
-            int version; /* virDomainTPMVersion */
+            virDomainTPMVersion version;
             virDomainChrSourceDef *source;
             char *storagepath;
             char *logfile;
index 8699b8822076310e0d2b3640b81a0084165c3341..0771598fb525472efd279f9df21d7c8beb76122f 100644 (file)
@@ -9860,7 +9860,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;
index c70845e746a2559517e709b16666a6fab82db41f..8d96d7b084f6464e9e5fc8f9007ef93312e9dc17 100644 (file)
@@ -11753,7 +11753,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);