[PATCH 15/17] virDomainInputDefParseXML: Switch to virXMLPropEnumDefault()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx>
---
 src/conf/domain_audit.c          |  2 +-
 src/conf/domain_conf.c           | 86 +++++++++++++-------------------
 src/conf/domain_conf.h           |  6 +--
 src/conf/domain_validate.c       |  2 +-
 src/libxl/libxl_conf.c           |  4 ++
 src/libxl/xen_xl.c               |  3 ++
 src/libxl/xen_xm.c               |  3 ++
 src/qemu/qemu_cgroup.c           | 12 +++++
 src/qemu/qemu_command.c          | 10 ++--
 src/qemu/qemu_domain_address.c   |  4 +-
 src/qemu/qemu_hotplug.c          |  4 +-
 src/qemu/qemu_validate.c         |  4 +-
 src/security/security_apparmor.c |  2 +-
 src/security/security_dac.c      |  4 +-
 src/security/security_selinux.c  |  4 +-
 15 files changed, 80 insertions(+), 70 deletions(-)

diff --git a/src/conf/domain_audit.c b/src/conf/domain_audit.c
index 17a01c51ba..9ce14de80b 100644
--- a/src/conf/domain_audit.c
+++ b/src/conf/domain_audit.c
@@ -949,7 +949,7 @@ virDomainAuditInput(virDomainObj *vm,
     if (!(vmname = virAuditEncode("vm", vm->def->name)))
         return;
 
-    switch ((virDomainInputType) input->type) {
+    switch (input->type) {
     case VIR_DOMAIN_INPUT_TYPE_MOUSE:
     case VIR_DOMAIN_INPUT_TYPE_TABLET:
     case VIR_DOMAIN_INPUT_TYPE_KBD:
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 678b9265c7..0cb8de8b1f 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -1932,7 +1932,7 @@ void virDomainGraphicsDefFree(virDomainGraphicsDef *def)
 
 const char *virDomainInputDefGetPath(virDomainInputDef *input)
 {
-    switch ((virDomainInputType) input->type) {
+    switch (input->type) {
     case VIR_DOMAIN_INPUT_TYPE_MOUSE:
     case VIR_DOMAIN_INPUT_TYPE_TABLET:
     case VIR_DOMAIN_INPUT_TYPE_KBD:
@@ -11785,70 +11785,54 @@ virDomainInputDefParseXML(virDomainXMLOption *xmlopt,
 {
     VIR_XPATH_NODE_AUTORESTORE(ctxt)
     virDomainInputDef *def;
-    g_autofree char *type = NULL;
-    g_autofree char *bus = NULL;
-    g_autofree char *model = NULL;
+    virDomainInputBus bus = VIR_DOMAIN_INPUT_BUS_PS2;
     xmlNodePtr source = NULL;
 
     def = g_new0(virDomainInputDef, 1);
 
     ctxt->node = node;
 
-    type = virXMLPropString(node, "type");
-    bus = virXMLPropString(node, "bus");
-    model = virXMLPropString(node, "model");
-
-    if (!type) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       "%s", _("missing input device type"));
-        goto error;
-    }
-
-    if ((def->type = virDomainInputTypeFromString(type)) < 0) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                       _("unknown input device type '%s'"), type);
+    if (virXMLPropEnum(node, "type",
+                       virDomainInputTypeFromString,
+                       VIR_XML_PROP_REQUIRED,
+                       &def->type) < 0)
         goto error;
-    }
 
-    if (model &&
-        ((def->model = virDomainInputModelTypeFromString(model)) < 0 ||
-         def->model == VIR_DOMAIN_INPUT_MODEL_DEFAULT)) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                       _("unknown input model '%s'"), model);
+    if (virXMLPropEnum(node, "model",
+                       virDomainInputModelTypeFromString,
+                       VIR_XML_PROP_NONZERO,
+                       &def->model) < 0)
         goto error;
-    }
-
-    if (bus) {
-        if ((def->bus = virDomainInputBusTypeFromString(bus)) < 0) {
-            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                           _("unknown input bus type '%s'"), bus);
-            goto error;
-        }
 
-    } else {
-        if (dom->os.type == VIR_DOMAIN_OSTYPE_HVM) {
-            if ((def->type == VIR_DOMAIN_INPUT_TYPE_MOUSE ||
-                def->type == VIR_DOMAIN_INPUT_TYPE_KBD) &&
-                (ARCH_IS_X86(dom->os.arch) || dom->os.arch == VIR_ARCH_NONE)) {
-                def->bus = VIR_DOMAIN_INPUT_BUS_PS2;
-            } else if (ARCH_IS_S390(dom->os.arch) ||
-                       def->type == VIR_DOMAIN_INPUT_TYPE_PASSTHROUGH) {
-                def->bus = VIR_DOMAIN_INPUT_BUS_VIRTIO;
-            } else if (def->type == VIR_DOMAIN_INPUT_TYPE_EVDEV) {
-                def->bus = VIR_DOMAIN_INPUT_BUS_NONE;
-            } else {
-                def->bus = VIR_DOMAIN_INPUT_BUS_USB;
-            }
-        } else if (dom->os.type == VIR_DOMAIN_OSTYPE_XEN ||
-                   dom->os.type == VIR_DOMAIN_OSTYPE_XENPVH) {
-            def->bus = VIR_DOMAIN_INPUT_BUS_XEN;
+    if (dom->os.type == VIR_DOMAIN_OSTYPE_HVM) {
+        if ((def->type == VIR_DOMAIN_INPUT_TYPE_MOUSE ||
+             def->type == VIR_DOMAIN_INPUT_TYPE_KBD) &&
+            (ARCH_IS_X86(dom->os.arch) || dom->os.arch == VIR_ARCH_NONE)) {
+            bus = VIR_DOMAIN_INPUT_BUS_PS2;
+        } else if (ARCH_IS_S390(dom->os.arch) ||
+                   def->type == VIR_DOMAIN_INPUT_TYPE_PASSTHROUGH) {
+            bus = VIR_DOMAIN_INPUT_BUS_VIRTIO;
+        } else if (def->type == VIR_DOMAIN_INPUT_TYPE_EVDEV) {
+            bus = VIR_DOMAIN_INPUT_BUS_NONE;
         } else {
-            if ((dom->virtType == VIR_DOMAIN_VIRT_VZ ||
-                 dom->virtType == VIR_DOMAIN_VIRT_PARALLELS))
-                def->bus = VIR_DOMAIN_INPUT_BUS_PARALLELS;
+            bus = VIR_DOMAIN_INPUT_BUS_USB;
         }
+    } else if (dom->os.type == VIR_DOMAIN_OSTYPE_XEN ||
+               dom->os.type == VIR_DOMAIN_OSTYPE_XENPVH) {
+        bus = VIR_DOMAIN_INPUT_BUS_XEN;
+    } else {
+        if ((dom->virtType == VIR_DOMAIN_VIRT_VZ ||
+             dom->virtType == VIR_DOMAIN_VIRT_PARALLELS))
+            bus = VIR_DOMAIN_INPUT_BUS_PARALLELS;
     }
 
+    if (virXMLPropEnumDefault(node, "bus",
+                              virDomainInputBusTypeFromString,
+                              VIR_XML_PROP_NONE,
+                              &def->bus, bus) < 0)
+        goto error;
+
+
     if (virDomainDeviceInfoParseXML(xmlopt, node, ctxt, &def->info, flags) < 0)
         goto error;
 
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index 9a0b962ec8..836f518f9c 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -1477,9 +1477,9 @@ typedef enum {
 } virDomainInputSourceGrabToggle;
 
 struct _virDomainInputDef {
-    int type;
-    int bus;
-    int model; /* virDomainInputModel */
+    virDomainInputType type;
+    virDomainInputBus bus;
+    virDomainInputModel model;
     struct {
         char *evdev;
         virDomainInputSourceGrab grab;
diff --git a/src/conf/domain_validate.c b/src/conf/domain_validate.c
index 9e64a96eb9..fd04add5d2 100644
--- a/src/conf/domain_validate.c
+++ b/src/conf/domain_validate.c
@@ -2177,7 +2177,7 @@ virDomainInputDefValidate(const virDomainInputDef *input,
         }
     }
 
-    switch ((virDomainInputType) input->type) {
+    switch (input->type) {
         case VIR_DOMAIN_INPUT_TYPE_MOUSE:
         case VIR_DOMAIN_INPUT_TYPE_TABLET:
         case VIR_DOMAIN_INPUT_TYPE_KBD:
diff --git a/src/libxl/libxl_conf.c b/src/libxl/libxl_conf.c
index 7a733cf30d..e3924e8b43 100644
--- a/src/libxl/libxl_conf.c
+++ b/src/libxl/libxl_conf.c
@@ -722,6 +722,10 @@ libxlMakeDomBuildInfo(virDomainDef *def,
                     VIR_FREE(*usbdevice);
                     *usbdevice = g_strdup("tablet");
                     break;
+                case VIR_DOMAIN_INPUT_TYPE_KBD:
+                case VIR_DOMAIN_INPUT_TYPE_PASSTHROUGH:
+                case VIR_DOMAIN_INPUT_TYPE_EVDEV:
+                case VIR_DOMAIN_INPUT_TYPE_LAST:
                 default:
                     virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                             _("Unknown input device type"));
diff --git a/src/libxl/xen_xl.c b/src/libxl/xen_xl.c
index c0edceeac7..36d6e1156d 100644
--- a/src/libxl/xen_xl.c
+++ b/src/libxl/xen_xl.c
@@ -1759,6 +1759,9 @@ xenFormatXLInputDevs(virConf *conf, virDomainDef *def)
                     case VIR_DOMAIN_INPUT_TYPE_KBD:
                         devtype = "keyboard";
                         break;
+                    case VIR_DOMAIN_INPUT_TYPE_PASSTHROUGH:
+                    case VIR_DOMAIN_INPUT_TYPE_EVDEV:
+                    case VIR_DOMAIN_INPUT_TYPE_LAST:
                     default:
                         continue;
                 }
diff --git a/src/libxl/xen_xm.c b/src/libxl/xen_xm.c
index a962da9cad..31d2d2e6a1 100644
--- a/src/libxl/xen_xm.c
+++ b/src/libxl/xen_xm.c
@@ -521,6 +521,9 @@ xenFormatXMInputDevs(virConf *conf, virDomainDef *def)
                     case VIR_DOMAIN_INPUT_TYPE_KBD:
                         devtype = "keyboard";
                         break;
+                    case VIR_DOMAIN_INPUT_TYPE_PASSTHROUGH:
+                    case VIR_DOMAIN_INPUT_TYPE_EVDEV:
+                    case VIR_DOMAIN_INPUT_TYPE_LAST:
                     default:
                         continue;
                 }
diff --git a/src/qemu/qemu_cgroup.c b/src/qemu/qemu_cgroup.c
index aa0c927578..7c24e187c9 100644
--- a/src/qemu/qemu_cgroup.c
+++ b/src/qemu/qemu_cgroup.c
@@ -384,6 +384,12 @@ qemuSetupInputCgroup(virDomainObj *vm,
         return qemuCgroupAllowDevicePath(vm, dev->source.evdev,
                                          VIR_CGROUP_DEVICE_RW, false);
         break;
+
+    case VIR_DOMAIN_INPUT_TYPE_MOUSE:
+    case VIR_DOMAIN_INPUT_TYPE_TABLET:
+    case VIR_DOMAIN_INPUT_TYPE_KBD:
+    case VIR_DOMAIN_INPUT_TYPE_LAST:
+        break;
     }
 
     return ret;
@@ -405,6 +411,12 @@ qemuTeardownInputCgroup(virDomainObj *vm,
         return qemuCgroupDenyDevicePath(vm, dev->source.evdev,
                                         VIR_CGROUP_DEVICE_RWM, false);
         break;
+
+    case VIR_DOMAIN_INPUT_TYPE_MOUSE:
+    case VIR_DOMAIN_INPUT_TYPE_TABLET:
+    case VIR_DOMAIN_INPUT_TYPE_KBD:
+    case VIR_DOMAIN_INPUT_TYPE_LAST:
+        break;
     }
 
     return 0;
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index 72bca45dc4..5cfcdc3699 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -872,7 +872,7 @@ qemuBuildVirtioDevGetConfigDev(const virDomainDeviceDef *device,
         case VIR_DOMAIN_DEVICE_INPUT:
             *virtioOptions = device->data.input->virtio;
 
-            switch ((virDomainInputType) device->data.input->type) {
+            switch (device->data.input->type) {
             case VIR_DOMAIN_INPUT_TYPE_MOUSE:
                 *baseName = "virtio-mouse";
                 break;
@@ -4515,7 +4515,7 @@ qemuBuildInputVirtioDevProps(const virDomainDef *def,
     g_autoptr(virJSONValue) props = NULL;
     const char *evdev = NULL;
 
-    switch ((virDomainInputType)dev->type) {
+    switch (dev->type) {
     case VIR_DOMAIN_INPUT_TYPE_MOUSE:
     case VIR_DOMAIN_INPUT_TYPE_TABLET:
     case VIR_DOMAIN_INPUT_TYPE_KBD:
@@ -4564,6 +4564,10 @@ qemuBuildInputUSBDevProps(const virDomainDef *def,
     case VIR_DOMAIN_INPUT_TYPE_KBD:
         driver = "usb-kbd";
         break;
+    case VIR_DOMAIN_INPUT_TYPE_PASSTHROUGH:
+    case VIR_DOMAIN_INPUT_TYPE_EVDEV:
+    case VIR_DOMAIN_INPUT_TYPE_LAST:
+        break;
     }
 
     if (virJSONValueObjectAdd(&props,
@@ -4628,7 +4632,7 @@ qemuBuildInputCommandLine(virCommand *cmd,
         } else {
             g_autoptr(virJSONValue) props = NULL;
 
-            switch ((virDomainInputBus) input->bus) {
+            switch (input->bus) {
             case VIR_DOMAIN_INPUT_BUS_USB:
                 if (!(props = qemuBuildInputUSBDevProps(def, input)))
                     return -1;
diff --git a/src/qemu/qemu_domain_address.c b/src/qemu/qemu_domain_address.c
index 753733d1b9..3b1139d975 100644
--- a/src/qemu/qemu_domain_address.c
+++ b/src/qemu/qemu_domain_address.c
@@ -960,9 +960,9 @@ qemuDomainDeviceCalculatePCIConnectFlags(virDomainDeviceDef *dev,
         return pciFlags;
 
     case VIR_DOMAIN_DEVICE_INPUT:
-        switch ((virDomainInputBus) dev->data.input->bus) {
+        switch (dev->data.input->bus) {
         case VIR_DOMAIN_INPUT_BUS_VIRTIO:
-            switch ((virDomainInputModel) dev->data.input->model) {
+            switch (dev->data.input->model) {
             case VIR_DOMAIN_INPUT_MODEL_VIRTIO_TRANSITIONAL:
                 /* Transitional devices only work in conventional PCI slots */
                 return pciFlags;
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index 274f47c25d..bc38ee0a11 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -3236,7 +3236,7 @@ qemuDomainAttachInputDevice(virQEMUDriver *driver,
 
     qemuAssignDeviceInputAlias(vm->def, input, -1);
 
-    switch ((virDomainInputBus) input->bus) {
+    switch (input->bus) {
     case VIR_DOMAIN_INPUT_BUS_USB:
         if (virDomainUSBAddressEnsure(priv->usbaddrs, &input->info) < 0)
             return -1;
@@ -5978,7 +5978,7 @@ qemuDomainDetachPrepInput(virDomainObj *vm,
     }
     *detach = input = vm->def->inputs[idx];
 
-    switch ((virDomainInputBus) input->bus) {
+    switch (input->bus) {
     case VIR_DOMAIN_INPUT_BUS_PS2:
     case VIR_DOMAIN_INPUT_BUS_XEN:
     case VIR_DOMAIN_INPUT_BUS_PARALLELS:
diff --git a/src/qemu/qemu_validate.c b/src/qemu/qemu_validate.c
index d4f0ea03e8..65b5fe7bc6 100644
--- a/src/qemu/qemu_validate.c
+++ b/src/qemu/qemu_validate.c
@@ -4734,7 +4734,7 @@ qemuValidateDomainDeviceDefInput(const virDomainInputDef *input,
         return 0;
 
     /* model=virtio-(non-)transitional is unsupported */
-    switch ((virDomainInputModel)input->model) {
+    switch (input->model) {
     case VIR_DOMAIN_INPUT_MODEL_VIRTIO_TRANSITIONAL:
     case VIR_DOMAIN_INPUT_MODEL_VIRTIO_NON_TRANSITIONAL:
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
@@ -4752,7 +4752,7 @@ qemuValidateDomainDeviceDefInput(const virDomainInputDef *input,
         return -1;
     }
 
-    switch ((virDomainInputType)input->type) {
+    switch (input->type) {
     case VIR_DOMAIN_INPUT_TYPE_MOUSE:
         baseName = "virtio-mouse";
         cap = QEMU_CAPS_VIRTIO_MOUSE;
diff --git a/src/security/security_apparmor.c b/src/security/security_apparmor.c
index 957c1a6357..e74045f563 100644
--- a/src/security/security_apparmor.c
+++ b/src/security/security_apparmor.c
@@ -712,7 +712,7 @@ AppArmorSetInputLabel(virSecurityManager *mgr,
     if (input == NULL)
         return 0;
 
-    switch ((virDomainInputType)input->type) {
+    switch (input->type) {
     case VIR_DOMAIN_INPUT_TYPE_PASSTHROUGH:
     case VIR_DOMAIN_INPUT_TYPE_EVDEV:
         if (input->source.evdev == NULL) {
diff --git a/src/security/security_dac.c b/src/security/security_dac.c
index 183f291cc6..e2ff861fd5 100644
--- a/src/security/security_dac.c
+++ b/src/security/security_dac.c
@@ -1788,7 +1788,7 @@ virSecurityDACSetInputLabel(virSecurityManager *mgr,
     if (seclabel && !seclabel->relabel)
         return 0;
 
-    switch ((virDomainInputType)input->type) {
+    switch (input->type) {
     case VIR_DOMAIN_INPUT_TYPE_PASSTHROUGH:
     case VIR_DOMAIN_INPUT_TYPE_EVDEV:
         if (virSecurityDACGetIds(seclabel, priv, &user, &group, NULL, NULL) < 0)
@@ -1817,7 +1817,7 @@ virSecurityDACRestoreInputLabel(virSecurityManager *mgr,
 {
     int ret = -1;
 
-    switch ((virDomainInputType)input->type) {
+    switch (input->type) {
     case VIR_DOMAIN_INPUT_TYPE_PASSTHROUGH:
     case VIR_DOMAIN_INPUT_TYPE_EVDEV:
         ret = virSecurityDACRestoreFileLabel(mgr, input->source.evdev);
diff --git a/src/security/security_selinux.c b/src/security/security_selinux.c
index 099306b522..6cdbd963e7 100644
--- a/src/security/security_selinux.c
+++ b/src/security/security_selinux.c
@@ -1512,7 +1512,7 @@ virSecuritySELinuxSetInputLabel(virSecurityManager *mgr,
     if (seclabel == NULL)
         return 0;
 
-    switch ((virDomainInputType)input->type) {
+    switch (input->type) {
     case VIR_DOMAIN_INPUT_TYPE_PASSTHROUGH:
     case VIR_DOMAIN_INPUT_TYPE_EVDEV:
         if (virSecuritySELinuxSetFilecon(mgr, input->source.evdev,
@@ -1543,7 +1543,7 @@ virSecuritySELinuxRestoreInputLabel(virSecurityManager *mgr,
     if (seclabel == NULL)
         return 0;
 
-    switch ((virDomainInputType)input->type) {
+    switch (input->type) {
     case VIR_DOMAIN_INPUT_TYPE_PASSTHROUGH:
     case VIR_DOMAIN_INPUT_TYPE_EVDEV:
         rc = virSecuritySELinuxRestoreFileLabel(mgr, input->source.evdev, true);
-- 
2.35.1




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux