[PATCHv2] ch_domain: Add handler for virDomainDeviceDefValidateCallback

William Douglas william.douglas at intel.com
Fri Jun 18 21:25:59 UTC 2021


Instead of trying to match devices passed in based on the monitor
detecting the number of devices that were used in the domain
definition, use the deviceValidateCallback to evaluate if
unsupported devices are used.

This allows the compiler to detect when new device types are added
that need to be checked.

Signed-off-by: William Douglas <william.douglas at intel.com>
---
The only change from the previous version was to switch to use the XML
validation callback based on the great explanations from Michal and Peter.
---
 src/ch/ch_domain.c  | 120 +++++++++++++++++++++++++++++++++++++++++++
 src/ch/ch_monitor.c | 122 --------------------------------------------
 2 files changed, 120 insertions(+), 122 deletions(-)

diff --git a/src/ch/ch_domain.c b/src/ch/ch_domain.c
index f9a6f3f31d..3495ee22ff 100644
--- a/src/ch/ch_domain.c
+++ b/src/ch/ch_domain.c
@@ -197,7 +197,127 @@ virCHDomainDefPostParse(virDomainDef *def,
     return 0;
 }
 
+static int
+chValidateDomainDeviceDef(const virDomainDeviceDef *dev,
+                          const virDomainDef *def G_GNUC_UNUSED,
+                          void *opaque G_GNUC_UNUSED,
+                          void *parseOpaque G_GNUC_UNUSED)
+{
+    int ret = -1;
+
+    switch ((virDomainDeviceType)dev->type) {
+    case VIR_DOMAIN_DEVICE_DISK:
+        ret = 0;
+        break;
+    case VIR_DOMAIN_DEVICE_LEASE:
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("Cloud-Hypervisor doesn't support lease"));
+        break;
+    case VIR_DOMAIN_DEVICE_FS:
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("Cloud-Hypervisor doesn't support fs"));
+        break;
+    case VIR_DOMAIN_DEVICE_NET:
+        ret = 0;
+        break;
+    case VIR_DOMAIN_DEVICE_INPUT:
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("Cloud-Hypervisor doesn't support input"));
+        break;
+    case VIR_DOMAIN_DEVICE_SOUND:
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("Cloud-Hypervisor doesn't support sound"));
+        break;
+    case VIR_DOMAIN_DEVICE_VIDEO:
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("Cloud-Hypervisor doesn't support video"));
+        break;
+    case VIR_DOMAIN_DEVICE_HOSTDEV:
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("Cloud-Hypervisor doesn't support hostdev"));
+        break;
+    case VIR_DOMAIN_DEVICE_WATCHDOG:
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("Cloud-Hypervisor doesn't support watchdog"));
+        break;
+    case VIR_DOMAIN_DEVICE_CONTROLLER:
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("Cloud-Hypervisor doesn't support controller"));
+        break;
+    case VIR_DOMAIN_DEVICE_GRAPHICS:
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("Cloud-Hypervisor doesn't support graphics"));
+        break;
+    case VIR_DOMAIN_DEVICE_HUB:
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("Cloud-Hypervisor doesn't support hub"));
+        break;
+    case VIR_DOMAIN_DEVICE_REDIRDEV:
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("Cloud-Hypervisor doesn't support redirdev"));
+        break;
+    case VIR_DOMAIN_DEVICE_SMARTCARD:
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("Cloud-Hypervisor doesn't support smartcard"));
+        break;
+    case VIR_DOMAIN_DEVICE_CHR:
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("Cloud-Hypervisor doesn't support chr"));
+        break;
+    case VIR_DOMAIN_DEVICE_MEMBALLOON:
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("Cloud-Hypervisor doesn't support memballoon"));
+        break;
+    case VIR_DOMAIN_DEVICE_NVRAM:
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("Cloud-Hypervisor doesn't support nvram"));
+        break;
+    case VIR_DOMAIN_DEVICE_RNG:
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("Cloud-Hypervisor doesn't support rng"));
+        break;
+    case VIR_DOMAIN_DEVICE_SHMEM:
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("Cloud-Hypervisor doesn't support shmem"));
+        break;
+    case VIR_DOMAIN_DEVICE_TPM:
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("Cloud-Hypervisor doesn't support tpm"));
+        break;
+    case VIR_DOMAIN_DEVICE_PANIC:
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("Cloud-Hypervisor doesn't support panic"));
+        break;
+    case VIR_DOMAIN_DEVICE_MEMORY:
+        ret = 0;
+        break;
+    case VIR_DOMAIN_DEVICE_IOMMU:
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("Cloud-Hypervisor doesn't support iommu"));
+        break;
+    case VIR_DOMAIN_DEVICE_VSOCK:
+        ret = 0;
+        break;
+    case VIR_DOMAIN_DEVICE_AUDIO:
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("Cloud-Hypervisor doesn't support audio"));
+        break;
+    case VIR_DOMAIN_DEVICE_NONE:
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("unexpected VIR_DOMAIN_DEVICE_NONE"));
+        break;
+
+    case VIR_DOMAIN_DEVICE_LAST:
+    default:
+        virReportEnumRangeError(virDomainDeviceType, dev->type);
+        break;
+    }
+
+    return ret;
+}
+
 virDomainDefParserConfig virCHDriverDomainDefParserConfig = {
     .domainPostParseBasicCallback = virCHDomainDefPostParseBasic,
     .domainPostParseCallback = virCHDomainDefPostParse,
+    .deviceValidateCallback = chValidateDomainDeviceDef,
 };
diff --git a/src/ch/ch_monitor.c b/src/ch/ch_monitor.c
index 87520a2639..1648d05017 100644
--- a/src/ch/ch_monitor.c
+++ b/src/ch/ch_monitor.c
@@ -359,125 +359,6 @@ virCHMonitorBuildNetsJson(virJSONValue *content, virDomainDef *vmdef)
     return -1;
 }
 
-static int
-virCHMonitorDetectUnsupportedDevices(virDomainDef *vmdef)
-{
-    int ret = 0;
-
-    if (vmdef->ngraphics > 0) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                       _("Cloud-Hypervisor doesn't support graphics"));
-        ret = 1;
-    }
-    if (vmdef->ncontrollers > 0) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                       _("Cloud-Hypervisor doesn't support controllers"));
-        ret = 1;
-    }
-    if (vmdef->nfss > 0) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                       _("Cloud-Hypervisor doesn't support fss"));
-        ret = 1;
-    }
-    if (vmdef->ninputs > 0) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                       _("Cloud-Hypervisor doesn't support inputs"));
-        ret = 1;
-    }
-    if (vmdef->nsounds > 0) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                       _("Cloud-Hypervisor doesn't support sounds"));
-        ret = 1;
-    }
-    if (vmdef->naudios > 0) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                       _("Cloud-Hypervisor doesn't support audios"));
-        ret = 1;
-    }
-    if (vmdef->nvideos > 0) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                       _("Cloud-Hypervisor doesn't support videos"));
-        ret = 1;
-    }
-    if (vmdef->nhostdevs > 0) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                       _("Cloud-Hypervisor doesn't support hostdevs"));
-        ret = 1;
-    }
-    if (vmdef->nredirdevs > 0) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                       _("Cloud-Hypervisor doesn't support redirdevs"));
-        ret = 1;
-    }
-    if (vmdef->nsmartcards > 0) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                       _("Cloud-Hypervisor doesn't support smartcards"));
-        ret = 1;
-    }
-    if (vmdef->nserials > 0) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                       _("Cloud-Hypervisor doesn't support serials"));
-        ret = 1;
-    }
-    if (vmdef->nparallels > 0) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                       _("Cloud-Hypervisor doesn't support parallels"));
-        ret = 1;
-    }
-    if (vmdef->nchannels > 0) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                       _("Cloud-Hypervisor doesn't support channels"));
-        ret = 1;
-    }
-    if (vmdef->nconsoles > 0) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                       _("Cloud-Hypervisor doesn't support consoles"));
-        ret = 1;
-    }
-    if (vmdef->nleases > 0) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                       _("Cloud-Hypervisor doesn't support leases"));
-        ret = 1;
-    }
-    if (vmdef->nhubs > 0) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                       _("Cloud-Hypervisor doesn't support hubs"));
-        ret = 1;
-    }
-    if (vmdef->nseclabels > 0) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                       _("Cloud-Hypervisor doesn't support seclabels"));
-        ret = 1;
-    }
-    if (vmdef->nrngs > 0) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                       _("Cloud-Hypervisor doesn't support rngs"));
-        ret = 1;
-    }
-    if (vmdef->nshmems > 0) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                       _("Cloud-Hypervisor doesn't support shmems"));
-        ret = 1;
-    }
-    if (vmdef->nmems > 0) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                       _("Cloud-Hypervisor doesn't support mems"));
-        ret = 1;
-    }
-    if (vmdef->npanics > 0) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                       _("Cloud-Hypervisor doesn't support panics"));
-        ret = 1;
-    }
-    if (vmdef->nsysinfo > 0) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                       _("Cloud-Hypervisor doesn't support sysinfo"));
-        ret = 1;
-    }
-
-    return ret;
-}
-
 static int
 virCHMonitorBuildVMJson(virDomainDef *vmdef, char **jsonstr)
 {
@@ -490,9 +371,6 @@ virCHMonitorBuildVMJson(virDomainDef *vmdef, char **jsonstr)
         goto cleanup;
     }
 
-    if (virCHMonitorDetectUnsupportedDevices(vmdef))
-        goto cleanup;
-
     if (virCHMonitorBuildCPUJson(content, vmdef) < 0)
         goto cleanup;
 
-- 
2.31.1




More information about the libvir-list mailing list