From: Olaf Hering <olaf@aepfle.de>
Subject: libxl: fix migration of PV and PVH domUs with and without qemu
References: bsc#1079730, bsc#1098403, bsc#1111025
Upstream: not interested

If a domU has a qemu-xen instance attached, it is required to call qemus
"xen-save-devices-state" method. Without it, the receiving side of a PV or
PVH migration may be unable to lock the image:

xen be: qdisk-51712: xen be: qdisk-51712: error: Failed to get "write" lock
error: Failed to get "write" lock
xen be: qdisk-51712: xen be: qdisk-51712: initialise() failed
initialise() failed

To fix this bug, libxl__domain_suspend_device_model() and
libxl__domain_resume_device_model() have to be called not only for HVM,
but also if the active device_model is QEMU_XEN.

Unfortunately, libxl__domain_build_info_setdefault() hardcodes
b_info->device_model_version to QEMU_XEN if it does not know it any
better. This breaks domUs without a device_model. libxl__qmp_stop() would
wait 10 seconds in qmp_open() for a qemu that will never appear.  During
this long timeframe the domU remains in state paused on the sending side.
As a result network connections may be dropped. Once this bug is fixed as
well, by just removing that assumption, there is no code to actually
initialise b_info->device_model_version.

There is a helper function libxl__need_xenpv_qemu(), which is used in
various places to decide if any device_model has to be spawned. This
function can not be used as is, just to fill b_info->device_model_version,
because store_libxl_entry() was already called earlier. Update this
function to receive a domid to work with, instead of reading xenstore.

Rearrange the code and initialize b_info->device_model_version in
libxl__domain_build_info_setdefault() per DOMAIN_TYPE.

Update initiate_domain_create() to set b_info->device_model_version if it
was not set earlier, using the updated libxl__need_xenpv_qemu().

Use LIBXL_DEVICE_MODEL_VERSION_UNKNOWN for PV and PVH that
have no need for a device_model.

Update existing users of libxl__need_xenpv_qemu() to use
b_info->device_model_version for their check if a device_model is needed.

Signed-off-by: Olaf Hering <olaf@aepfle.de>
---
 tools/libxl/libxl_create.c      | 39 +++++++++++++++++++++++++++++++--------
 tools/libxl/libxl_dm.c          | 40 +++++++++++++++++++++++-----------------
 tools/libxl/libxl_dom_suspend.c |  8 ++++++--
 tools/libxl/libxl_internal.h    |  3 ++-
 tools/libxl/libxl_types.idl     |  1 +
 5 files changed, 63 insertions(+), 28 deletions(-)

--- a/tools/libxl/libxl_create.c
+++ b/tools/libxl/libxl_create.c
@@ -92,9 +92,6 @@ int libxl__domain_build_info_setdefault(
             } else {
                 b_info->device_model_version = libxl__default_device_model(gc);
             }
-        } else {
-            b_info->device_model_version =
-                LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN;
         }
         if (b_info->device_model_version
                 == LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN) {
@@ -969,6 +966,18 @@ static void initiate_domain_create(libxl
     dcs->sdss.dm.guest_domid = 0; /* means we haven't spawned */
 
     /*
+     * libxl__domain_build_info_setdefault sets this only for HVM
+     * set it before store_libxl_entry()
+     */
+    if (d_config->b_info.device_model_version
+        == LIBXL_DEVICE_MODEL_VERSION_UNKNOWN) {
+        ret = libxl__need_xenpv_qemu(gc, d_config);
+        if (ret > 0)
+            d_config->b_info.device_model_version =
+                LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN;
+    }
+
+    /*
      * Set the dm version quite early so that libxl doesn't have to pass the
      * build info around just to know if the domain has a device model or not.
      */
--- a/tools/libxl/libxl_dom_suspend.c
+++ b/tools/libxl/libxl_dom_suspend.c
@@ -377,7 +377,9 @@ static void domain_suspend_common_guest_
     libxl__ev_xswatch_deregister(gc, &dsps->guest_watch);
     libxl__ev_time_deregister(gc, &dsps->guest_timeout);
 
-    if (dsps->type == LIBXL_DOMAIN_TYPE_HVM) {
+    if (dsps->type == LIBXL_DOMAIN_TYPE_HVM ||
+        libxl__device_model_version_running(gc, dsps->domid) ==
+        LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN) {
         rc = libxl__domain_suspend_device_model(gc, dsps);
         if (rc) {
             LOGD(ERROR, dsps->domid,
@@ -460,7 +462,9 @@ int libxl__domain_resume(libxl__gc *gc,
         goto out;
     }
 
-    if (type == LIBXL_DOMAIN_TYPE_HVM) {
+    if (type == LIBXL_DOMAIN_TYPE_HVM ||
+        libxl__device_model_version_running(gc, domid) ==
+        LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN) {
         rc = libxl__domain_resume_device_model(gc, domid);
         if (rc) {
             LOGD(ERROR, domid, "failed to resume device model:%d", rc);
