]> xenbits.xensource.com Git - libvirt.git/commitdiff
virDomainInputDefParseXML: Move validation into validator
authorMichal Privoznik <mprivozn@redhat.com>
Sun, 23 Jan 2022 11:54:27 +0000 (12:54 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Wed, 26 Jan 2022 09:49:03 +0000 (10:49 +0100)
There is some code that validates whether parsed @bus <input/>
makes sense (e.g. some hypervisors have their own type of bus).
But this code should not live in the parser, but validator
rather. That way, we can also validate that the value we compute
(if user didn't provide any) is valid.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/conf/domain_conf.c
src/conf/domain_validate.c

index f5b15cff33c41b1747049cd971f8302b4710b57e..87576d48048de39cceb23426fc4ac439cebd33f2 100644 (file)
@@ -11857,62 +11857,6 @@ virDomainInputDefParseXML(virDomainXMLOption *xmlopt,
             goto error;
         }
 
-        if (dom->os.type == VIR_DOMAIN_OSTYPE_HVM) {
-            if (def->bus == VIR_DOMAIN_INPUT_BUS_PS2 &&
-                def->type != VIR_DOMAIN_INPUT_TYPE_MOUSE &&
-                def->type != VIR_DOMAIN_INPUT_TYPE_KBD) {
-                virReportError(VIR_ERR_INTERNAL_ERROR,
-                               _("ps2 bus does not support %s input device"),
-                               type);
-                goto error;
-            }
-            if (def->bus == VIR_DOMAIN_INPUT_BUS_XEN) {
-                virReportError(VIR_ERR_INTERNAL_ERROR,
-                               _("unsupported input bus %s"),
-                               bus);
-                goto error;
-            }
-        } else if (dom->os.type == VIR_DOMAIN_OSTYPE_XEN ||
-                   dom->os.type == VIR_DOMAIN_OSTYPE_XENPVH) {
-            if (def->bus != VIR_DOMAIN_INPUT_BUS_XEN) {
-                virReportError(VIR_ERR_INTERNAL_ERROR,
-                               _("unsupported input bus %s"),
-                               bus);
-                goto error;
-            }
-            if (def->type != VIR_DOMAIN_INPUT_TYPE_MOUSE &&
-                def->type != VIR_DOMAIN_INPUT_TYPE_KBD) {
-                virReportError(VIR_ERR_INTERNAL_ERROR,
-                               _("xen bus does not support %s input device"),
-                               type);
-                goto error;
-            }
-        } else {
-            if (dom->virtType == VIR_DOMAIN_VIRT_VZ ||
-                dom->virtType == VIR_DOMAIN_VIRT_PARALLELS) {
-                if (def->bus != VIR_DOMAIN_INPUT_BUS_PARALLELS) {
-                    virReportError(VIR_ERR_INTERNAL_ERROR,
-                                   _("parallels containers don't support "
-                                     "input bus %s"),
-                                   bus);
-                    goto error;
-                }
-
-                if (def->type != VIR_DOMAIN_INPUT_TYPE_MOUSE &&
-                    def->type != VIR_DOMAIN_INPUT_TYPE_KBD) {
-                    virReportError(VIR_ERR_INTERNAL_ERROR,
-                                   _("parallels bus does not support "
-                                     "%s input device"),
-                                   type);
-                    goto error;
-                }
-            } else {
-                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                               _("Input devices are not supported by this "
-                                 "virtualization driver."));
-                goto error;
-            }
-        }
     } else {
         if (dom->os.type == VIR_DOMAIN_OSTYPE_HVM) {
             if ((def->type == VIR_DOMAIN_INPUT_TYPE_MOUSE ||
index e9baf1d41a85d394aad2fe63d935dce333eab774..e443a17b0e8397792bcba490bec8ca469b695513 100644 (file)
@@ -2112,8 +2112,71 @@ virDomainVsockDefValidate(const virDomainVsockDef *vsock)
 
 
 static int
-virDomainInputDefValidate(const virDomainInputDef *input)
+virDomainInputDefValidate(const virDomainInputDef *input,
+                          const virDomainDef *def)
 {
+    switch (def->os.type) {
+    case VIR_DOMAIN_OSTYPE_HVM:
+        if (input->bus == VIR_DOMAIN_INPUT_BUS_PS2 &&
+            input->type != VIR_DOMAIN_INPUT_TYPE_MOUSE &&
+            input->type != VIR_DOMAIN_INPUT_TYPE_KBD) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("ps2 bus does not support %s input device"),
+                           virDomainInputTypeToString(input->type));
+            return -1;
+        }
+        if (input->bus == VIR_DOMAIN_INPUT_BUS_XEN) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("unsupported input bus %s"),
+                           virDomainInputBusTypeToString(input->bus));
+            return -1;
+        }
+        break;
+
+    case VIR_DOMAIN_OSTYPE_XEN:
+    case VIR_DOMAIN_OSTYPE_XENPVH:
+        if (input->bus != VIR_DOMAIN_INPUT_BUS_XEN) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("unsupported input bus %s"),
+                           virDomainInputBusTypeToString(input->bus));
+            return -1;
+        }
+        if (input->type != VIR_DOMAIN_INPUT_TYPE_MOUSE &&
+            input->type != VIR_DOMAIN_INPUT_TYPE_KBD) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("xen bus does not support %s input device"),
+                           virDomainInputTypeToString(input->type));
+            return -1;
+        }
+        break;
+
+    default:
+        if (def->virtType == VIR_DOMAIN_VIRT_VZ ||
+            def->virtType == VIR_DOMAIN_VIRT_PARALLELS) {
+            if (input->bus != VIR_DOMAIN_INPUT_BUS_PARALLELS) {
+                virReportError(VIR_ERR_INTERNAL_ERROR,
+                               _("parallels containers don't support "
+                                 "input bus %s"),
+                               virDomainInputBusTypeToString(input->bus));
+                return -1;
+            }
+
+            if (input->type != VIR_DOMAIN_INPUT_TYPE_MOUSE &&
+                input->type != VIR_DOMAIN_INPUT_TYPE_KBD) {
+                virReportError(VIR_ERR_INTERNAL_ERROR,
+                               _("parallels bus does not support "
+                                 "%s input device"),
+                               virDomainInputTypeToString(input->type));
+                return -1;
+            }
+        } else {
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                           _("Input devices are not supported by this "
+                             "virtualization driver."));
+            return -1;
+        }
+    }
+
     switch ((virDomainInputType) input->type) {
         case VIR_DOMAIN_INPUT_TYPE_MOUSE:
         case VIR_DOMAIN_INPUT_TYPE_TABLET:
@@ -2296,7 +2359,7 @@ virDomainDeviceDefValidateInternal(const virDomainDeviceDef *dev,
         return virDomainVsockDefValidate(dev->data.vsock);
 
     case VIR_DOMAIN_DEVICE_INPUT:
-        return virDomainInputDefValidate(dev->data.input);
+        return virDomainInputDefValidate(dev->data.input, def);
 
     case VIR_DOMAIN_DEVICE_SHMEM:
         return virDomainShmemDefValidate(dev->data.shmem);