[libvirt] [PATCH 09/10] device: cleanup input device code

Pavel Hrdina phrdina at redhat.com
Thu Jan 7 11:32:41 UTC 2016


The current code was a little bit odd.  At first we've removed all
possible implicit input devices from domain definition to add them later
back if there was any graphics device defined while parsing XML
description.  That's not all, while formating domain definition to XML
description we at first ignore any input devices with bus different to
USB and VIRTIO and few lines later we add implicit input devices to XML.

This seems to me as a lot of code for nothing.  This patch may look
to be more complicated than original approach, but this is a preferred
way to modify/add driver specific staff only in those drivers and not
deal with them in common parsing/formating functions.

The update is to add those implicit input devices into config XML to
follow the real HW configuration visible by guest OS.

There was also inconsistence between our behavior and QEMU's in the way,
that in QEMU there is no way how to disable those implicit input devices
for x86 architecture and they are available always, even without graphics
device.  This applies also to XEN hypervisor.  VZ driver already does its
part by putting correct implicit devices into live XML.

Signed-off-by: Pavel Hrdina <phrdina at redhat.com>
---
 src/Makefile.am            |  4 +--
 src/conf/domain_conf.c     | 77 ++--------------------------------------------
 src/libxl/libxl_domain.c   |  5 +++
 src/qemu/qemu_domain.c     | 22 +++++++++++++
 src/xen/xen_driver.c       |  5 +++
 src/xenapi/xenapi_driver.c |  5 +++
 src/xenconfig/xen_common.c | 22 +++++++++++++
 src/xenconfig/xen_common.h |  2 ++
 8 files changed, 66 insertions(+), 76 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am
index af22fc1..84ddb13 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1210,7 +1210,7 @@ libvirt_driver_xen_impl_la_CFLAGS =				\
 		-I$(srcdir)/xenconfig				\
 		$(AM_CFLAGS)
 libvirt_driver_xen_impl_la_LDFLAGS = $(AM_LDFLAGS)
-libvirt_driver_xen_impl_la_LIBADD = $(XEN_LIBS)
+libvirt_driver_xen_impl_la_LIBADD = $(XEN_LIBS) libvirt_xenconfig.la
 libvirt_driver_xen_impl_la_SOURCES = $(XEN_DRIVER_SOURCES)
 endif WITH_XEN
 
@@ -1271,7 +1271,7 @@ if WITH_XENAPI
 noinst_LTLIBRARIES += libvirt_driver_xenapi.la
 libvirt_la_BUILT_LIBADD += libvirt_driver_xenapi.la
 libvirt_driver_xenapi_la_CFLAGS = $(LIBXENSERVER_CFLAGS) $(CURL_CFLAGS) \
-		-I$(srcdir)/conf $(AM_CFLAGS)
+		-I$(srcdir)/conf -I$(srcdir)/xenconfig $(AM_CFLAGS)
 libvirt_driver_xenapi_la_LDFLAGS = $(AM_LDFLAGS)
 libvirt_driver_xenapi_la_LIBADD = $(LIBXENSERVER_LIBS) $(CURL_LIBS)
 libvirt_driver_xenapi_la_SOURCES = $(XENAPI_DRIVER_SOURCES)
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 9d47846..d4ceb32 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -15930,27 +15930,6 @@ virDomainDefParseXML(xmlDocPtr xml,
             goto error;
         }
 
-        /* With QEMU / KVM / Xen graphics, mouse + PS/2 is implicit
-         * with graphics, so don't store it.
-         * XXX will this be true for other virt types ? */
-        if ((def->os.type == VIR_DOMAIN_OSTYPE_HVM &&
-             input->bus == VIR_DOMAIN_INPUT_BUS_PS2 &&
-             (input->type == VIR_DOMAIN_INPUT_TYPE_MOUSE ||
-              input->type == VIR_DOMAIN_INPUT_TYPE_KBD)) ||
-            (def->os.type == VIR_DOMAIN_OSTYPE_XEN &&
-             input->bus == VIR_DOMAIN_INPUT_BUS_XEN &&
-             (input->type == VIR_DOMAIN_INPUT_TYPE_MOUSE ||
-              input->type == VIR_DOMAIN_INPUT_TYPE_KBD)) ||
-            (def->os.type == VIR_DOMAIN_OSTYPE_EXE &&
-             (def->virtType == VIR_DOMAIN_VIRT_VZ ||
-              def->virtType == VIR_DOMAIN_VIRT_PARALLELS)  &&
-             input->bus == VIR_DOMAIN_INPUT_BUS_PARALLELS &&
-             (input->type == VIR_DOMAIN_INPUT_TYPE_MOUSE ||
-              input->type == VIR_DOMAIN_INPUT_TYPE_KBD))) {
-            virDomainInputDefFree(input);
-            continue;
-        }
-
         def->inputs[def->ninputs++] = input;
     }
     VIR_FREE(nodes);
@@ -15971,29 +15950,6 @@ virDomainDefParseXML(xmlDocPtr xml,
     }
     VIR_FREE(nodes);
 
-    /* If graphics are enabled, there's an implicit PS2 mouse */
-    if (def->ngraphics > 0 &&
-        (ARCH_IS_X86(def->os.arch) || def->os.arch == VIR_ARCH_NONE)) {
-        int input_bus = VIR_DOMAIN_INPUT_BUS_XEN;
-
-        if (def->os.type == VIR_DOMAIN_OSTYPE_HVM)
-            input_bus = VIR_DOMAIN_INPUT_BUS_PS2;
-        if (def->os.type == VIR_DOMAIN_OSTYPE_EXE &&
-            (def->virtType == VIR_DOMAIN_VIRT_VZ ||
-             def->virtType == VIR_DOMAIN_VIRT_PARALLELS))
-            input_bus = VIR_DOMAIN_INPUT_BUS_PARALLELS;
-
-        if (virDomainDefMaybeAddInput(def,
-                                      VIR_DOMAIN_INPUT_TYPE_MOUSE,
-                                      input_bus) < 0)
-            goto error;
-
-        if (virDomainDefMaybeAddInput(def,
-                                      VIR_DOMAIN_INPUT_TYPE_KBD,
-                                      input_bus) < 0)
-            goto error;
-    }
-
     /* analysis of the sound devices */
     if ((n = virXPathNodeSet("./devices/sound", ctxt, &nodes)) < 0)
         goto error;
@@ -22235,11 +22191,10 @@ virDomainDefFormatInternal(virDomainDefPtr def,
         if (virDomainChrDefFormat(buf, def->channels[n], flags) < 0)
             goto error;
 
-    for (n = 0; n < def->ninputs; n++)
-        if ((def->inputs[n]->bus == VIR_DOMAIN_INPUT_BUS_USB ||
-             def->inputs[n]->bus == VIR_DOMAIN_INPUT_BUS_VIRTIO) &&
-            virDomainInputDefFormat(buf, def->inputs[n], flags) < 0)
+    for (n = 0; n < def->ninputs; n++) {
+        if (virDomainInputDefFormat(buf, def->inputs[n], flags) < 0)
             goto error;
+    }
 
     if (def->tpm) {
         if (virDomainTPMDefFormat(buf, def->tpm, flags) < 0)
@@ -22247,32 +22202,6 @@ virDomainDefFormatInternal(virDomainDefPtr def,
     }
 
     if (def->ngraphics > 0) {
-        /* If graphics is enabled, add the implicit mouse/keyboard */
-        if ((ARCH_IS_X86(def->os.arch)) || def->os.arch == VIR_ARCH_NONE) {
-            virDomainInputDef autoInput = {
-                .type = VIR_DOMAIN_INPUT_TYPE_MOUSE,
-                .info = { .alias = NULL },
-            };
-
-            if (def->os.type == VIR_DOMAIN_OSTYPE_HVM)
-                autoInput.bus = VIR_DOMAIN_INPUT_BUS_PS2;
-            else if (def->os.type == VIR_DOMAIN_OSTYPE_EXE &&
-                     (def->virtType == VIR_DOMAIN_VIRT_VZ ||
-                      def->virtType == VIR_DOMAIN_VIRT_PARALLELS))
-                autoInput.bus = VIR_DOMAIN_INPUT_BUS_PARALLELS;
-            else
-               autoInput.bus = VIR_DOMAIN_INPUT_BUS_XEN;
-
-            if (virDomainInputDefFormat(buf, &autoInput, flags) < 0)
-                goto error;
-
-            if (!(flags & VIR_DOMAIN_DEF_FORMAT_MIGRATABLE)) {
-                autoInput.type = VIR_DOMAIN_INPUT_TYPE_KBD;
-                if (virDomainInputDefFormat(buf, &autoInput, flags) < 0)
-                    goto error;
-            }
-        }
-
         for (n = 0; n < def->ngraphics; n++)
             if (virDomainGraphicsDefFormat(buf, def->graphics[n], flags) < 0)
                 goto error;
diff --git a/src/libxl/libxl_domain.c b/src/libxl/libxl_domain.c
index 5b01db8..20fa590 100644
--- a/src/libxl/libxl_domain.c
+++ b/src/libxl/libxl_domain.c
@@ -35,6 +35,7 @@
 #include "virstring.h"
 #include "virtime.h"
 #include "locking/domain_lock.h"
+#include "xenconfig/xen_common.h"
 
 #define VIR_FROM_THIS VIR_FROM_LIBXL
 
@@ -394,6 +395,10 @@ libxlDomainDefPostParse(virDomainDefPtr def,
         def->consoles[0] = chrdef;
     }
 
+    /* add implicit input devices */
+    if (xenDomainDefAddImplicitInputDevice(def) < 0)
+        return -1;
+
     /* memory hotplug tunables are not supported by this driver */
     if (virDomainDefCheckUnsupportedMemoryHotplug(def) < 0)
         return -1;
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 73fc79d..a1d9ea5 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -1027,6 +1027,25 @@ virDomainXMLNamespace virQEMUDriverDomainXMLNamespace = {
 
 
 static int
+qemuDomainDefAddImplicitInputDevice(virDomainDef *def)
+{
+    if (ARCH_IS_X86(def->os.arch)) {
+        if (virDomainDefMaybeAddInput(def,
+                                      VIR_DOMAIN_INPUT_TYPE_MOUSE,
+                                      VIR_DOMAIN_INPUT_BUS_PS2) < 0)
+            return -1;
+
+        if (virDomainDefMaybeAddInput(def,
+                                      VIR_DOMAIN_INPUT_TYPE_KBD,
+                                      VIR_DOMAIN_INPUT_BUS_PS2) < 0)
+            return -1;
+    }
+
+    return 0;
+}
+
+
+static int
 qemuDomainDefPostParse(virDomainDefPtr def,
                        virCapsPtr caps,
                        void *opaque)
@@ -1054,6 +1073,9 @@ qemuDomainDefPostParse(virDomainDefPtr def,
         !(def->emulator = virDomainDefGetDefaultEmulator(def, caps)))
         return ret;
 
+    /* add implicit input devices */
+    if (qemuDomainDefAddImplicitInputDevice(def) < 0)
+        return ret;
 
     qemuCaps = virQEMUCapsCacheLookup(driver->qemuCapsCache, def->emulator);
 
diff --git a/src/xen/xen_driver.c b/src/xen/xen_driver.c
index 95f0e42..a987e82 100644
--- a/src/xen/xen_driver.c
+++ b/src/xen/xen_driver.c
@@ -46,6 +46,7 @@
 
 #include "xen_sxpr.h"
 #include "xen_xm.h"
+#include "xen_common.h"
 #include "xen_hypervisor.h"
 #include "xend_internal.h"
 #include "xs_internal.h"
@@ -380,6 +381,10 @@ xenDomainDefPostParse(virDomainDefPtr def,
         def->memballoon = memballoon;
     }
 
+    /* add implicit input device */
+    if (xenDomainDefAddImplicitInputDevice(def) <0)
+        return -1;
+
     /* memory hotplug tunables are not supported by this driver */
     if (virDomainDefCheckUnsupportedMemoryHotplug(def) < 0)
         return -1;
diff --git a/src/xenapi/xenapi_driver.c b/src/xenapi/xenapi_driver.c
index e4e9936..0453cac 100644
--- a/src/xenapi/xenapi_driver.c
+++ b/src/xenapi/xenapi_driver.c
@@ -39,6 +39,7 @@
 #include "xenapi_driver_private.h"
 #include "xenapi_utils.h"
 #include "virstring.h"
+#include "xen_common.h"
 
 #define VIR_FROM_THIS VIR_FROM_XENAPI
 
@@ -77,6 +78,10 @@ xenapiDomainDefPostParse(virDomainDefPtr def,
                          virCapsPtr caps ATTRIBUTE_UNUSED,
                          void *opaque ATTRIBUTE_UNUSED)
 {
+    /* add implicit input device */
+    if (xenDomainDefAddImplicitInputDevice(def) < 0)
+        return -1;
+
     /* memory hotplug tunables are not supported by this driver */
     if (virDomainDefCheckUnsupportedMemoryHotplug(def) < 0)
         return -1;
diff --git a/src/xenconfig/xen_common.c b/src/xenconfig/xen_common.c
index 54f5791..fa5777b 100644
--- a/src/xenconfig/xen_common.c
+++ b/src/xenconfig/xen_common.c
@@ -1799,3 +1799,25 @@ xenFormatConfigCommon(virConfPtr conf,
 
     return 0;
 }
+
+
+int
+xenDomainDefAddImplicitInputDevice(virDomainDefPtr def)
+{
+    virDomainInputBus implicitInputBus = VIR_DOMAIN_INPUT_BUS_XEN;
+
+    if (def->os.type == VIR_DOMAIN_OSTYPE_HVM)
+        implicitInputBus = VIR_DOMAIN_INPUT_BUS_PS2;
+
+    if (virDomainDefMaybeAddInput(def,
+                                  VIR_DOMAIN_INPUT_TYPE_MOUSE,
+                                  implicitInputBus) < 0)
+        return -1;
+
+    if (virDomainDefMaybeAddInput(def,
+                                  VIR_DOMAIN_INPUT_TYPE_KBD,
+                                  implicitInputBus) < 0)
+        return -1;
+
+    return 0;
+}
diff --git a/src/xenconfig/xen_common.h b/src/xenconfig/xen_common.h
index 3d9b03c..9ddf210 100644
--- a/src/xenconfig/xen_common.h
+++ b/src/xenconfig/xen_common.h
@@ -61,4 +61,6 @@ int xenFormatConfigCommon(virConfPtr conf,
                           virConnectPtr conn);
 
 
+int xenDomainDefAddImplicitInputDevice(virDomainDefPtr def);
+
 #endif /* __VIR_XEN_COMMON_H__ */
-- 
2.7.0




More information about the libvir-list mailing list