debuggers.hg

changeset 22371:4ac03710fc32

libxl: Fix migration of HVM guests

In the default "QemuDeviceModelRecord"-style HVM tail of the migration
protocol the size of the qemu save record is unknown at the receiver
and therefore it must read until EOF. This is not compatible with the
xl migration protocol which contains a post-migration handshake and
therefore cannot close the socket on the sending end.

What is required is an explicit length field for the save record,
which the "RemusDeviceModelState"-style HVM tail includes.

Rather than overload the "RemusDeviceModelState" name for a non-Remus
use case (on off chance that they need to diverge for some reason in
the future) introduce a third style called "DeviceModelRecord0002"
which is identical to current "RemusDeviceModelState"-style.

Hopefully the inclusion of a number here will allow easier extension
in the future without needing to come up with increasingly less
helpful names!

Also propagate errors from xc_domain_save and
libxl__domain_suspend_common to callers.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
author Ian Campbell <ian.campbell@citrix.com>
date Thu Oct 28 12:19:43 2010 +0100 (2010-10-28)
parents 902fdaaa1fe6
children dc66c13a29f9
files tools/libxc/xc_domain_restore.c tools/libxc/xg_save_restore.h tools/libxl/libxl.c tools/libxl/libxl_dom.c tools/libxl/libxl_internal.h
line diff
     1.1 --- a/tools/libxc/xc_domain_restore.c	Thu Oct 28 12:16:48 2010 +0100
     1.2 +++ b/tools/libxc/xc_domain_restore.c	Thu Oct 28 12:19:43 2010 +0100
     1.3 @@ -494,13 +494,18 @@ static int buffer_tail_hvm(xc_interface 
     1.4          return -1;
     1.5      }
     1.6  
     1.7 -    /* The normal live-migration QEMU record has no length information.
     1.8 +    /* The legacy live-migration QEMU record has no length information.
     1.9       * Short of reimplementing the QEMU parser, we're forced to just read
    1.10 -     * until EOF. Remus gets around this by sending a different signature
    1.11 -     * which includes a length prefix */
    1.12 +     * until EOF.
    1.13 +     *
    1.14 +     * Gets around this by sending a different signatures for the new
    1.15 +     * live-migration QEMU record and Remus which includes a length
    1.16 +     * prefix
    1.17 +     */
    1.18      if ( !memcmp(qemusig, "QemuDeviceModelRecord", sizeof(qemusig)) )
    1.19          return compat_buffer_qemu(xch, ctx, fd, buf);
    1.20 -    else if ( !memcmp(qemusig, "RemusDeviceModelState", sizeof(qemusig)) )
    1.21 +    else if ( !memcmp(qemusig, "DeviceModelRecord0002", sizeof(qemusig)) ||
    1.22 +              !memcmp(qemusig, "RemusDeviceModelState", sizeof(qemusig)) )
    1.23          return buffer_qemu(xch, ctx, fd, buf);
    1.24  
    1.25      qemusig[20] = '\0';
     2.1 --- a/tools/libxc/xg_save_restore.h	Thu Oct 28 12:16:48 2010 +0100
     2.2 +++ b/tools/libxc/xg_save_restore.h	Thu Oct 28 12:19:43 2010 +0100
     2.3 @@ -104,8 +104,9 @@
     2.4   *  Qemu context:
     2.5   *     char[21]         : Signature:
     2.6   *       "QemuDeviceModelRecord" : Read Qemu save data until EOF
     2.7 - *       "RemusDeviceModelState" : uint32_t length field followed by that many
     2.8 + *       "DeviceModelRecord0002" : uint32_t length field followed by that many
     2.9   *                                 bytes of Qemu save data
    2.10 + *       "RemusDeviceModelState" : Currently the same as "DeviceModelRecord0002".
    2.11   *
    2.12   * PV TAIL:
    2.13   *
     3.1 --- a/tools/libxl/libxl.c	Thu Oct 28 12:16:48 2010 +0100
     3.2 +++ b/tools/libxl/libxl.c	Thu Oct 28 12:19:43 2010 +0100
     3.3 @@ -686,8 +686,8 @@ int libxl_domain_suspend(libxl_ctx *ctx,
     3.4      int debug = info != NULL && info->flags & XL_SUSPEND_DEBUG;
     3.5      int rc = 0;
     3.6  
     3.7 -    libxl__domain_suspend_common(ctx, domid, fd, hvm, live, debug);
     3.8 -    if (hvm)
     3.9 +    rc = libxl__domain_suspend_common(ctx, domid, fd, hvm, live, debug);
    3.10 +    if (!rc && hvm)
    3.11          rc = libxl__domain_save_device_model(ctx, domid, fd);
    3.12      return rc;
    3.13  }
     4.1 --- a/tools/libxl/libxl_dom.c	Thu Oct 28 12:16:48 2010 +0100
     4.2 +++ b/tools/libxl/libxl_dom.c	Thu Oct 28 12:19:43 2010 +0100
     4.3 @@ -442,14 +442,17 @@ int libxl__domain_suspend_common(libxl_c
     4.4      callbacks.switch_qemu_logdirty = libxl__domain_suspend_common_switch_qemu_logdirty;
     4.5      callbacks.data = &si;
     4.6  
     4.7 -    xc_domain_save(ctx->xch, fd, domid, 0, 0, flags, &callbacks, hvm);
     4.8 +    rc = xc_domain_save(ctx->xch, fd, domid, 0, 0, flags, &callbacks, hvm);
     4.9 +    if ( rc ) {
    4.10 +        LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "saving domain");
    4.11 +        rc = ERROR_FAIL;
    4.12 +    }
    4.13  
    4.14      if (si.suspend_eventchn > 0)
    4.15          xc_suspend_evtchn_release(ctx->xch, si.xce, domid, si.suspend_eventchn);
    4.16      if (si.xce > 0)
    4.17          xc_evtchn_close(si.xce);
    4.18  
    4.19 -    rc = 0;
    4.20  out:
    4.21      libxl__free_all(&gc);
    4.22      return rc;
    4.23 @@ -461,15 +464,32 @@ int libxl__domain_save_device_model(libx
    4.24      int fd2, c;
    4.25      char buf[1024];
    4.26      char *filename = libxl__sprintf(&gc, "/var/lib/xen/qemu-save.%d", domid);
    4.27 +    struct stat st;
    4.28 +    uint32_t qemu_state_len;
    4.29  
    4.30      LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "Saving device model state to %s", filename);
    4.31      libxl__xs_write(&gc, XBT_NULL, libxl__sprintf(&gc, "/local/domain/0/device-model/%d/command", domid), "save");
    4.32      libxl__wait_for_device_model(ctx, domid, "paused", NULL, NULL);
    4.33  
    4.34 +    if (stat(filename, &st) < 0)
    4.35 +    {
    4.36 +        LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Unable to stat qemu save file\n");
    4.37 +        return ERROR_FAIL;
    4.38 +    }
    4.39 +
    4.40 +    qemu_state_len = st.st_size;
    4.41 +    LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "Qemu state is %d bytes\n", qemu_state_len);
    4.42 +
    4.43      c = libxl_write_exactly(ctx, fd, QEMU_SIGNATURE, strlen(QEMU_SIGNATURE),
    4.44                              "saved-state file", "qemu signature");
    4.45      if (c)
    4.46          return c;
    4.47 +
    4.48 +    c = libxl_write_exactly(ctx, fd, &qemu_state_len, sizeof(qemu_state_len),
    4.49 +                            "saved-state file", "saved-state length");
    4.50 +    if (c)
    4.51 +        return c;
    4.52 +
    4.53      fd2 = open(filename, O_RDONLY);
    4.54      while ((c = read(fd2, buf, sizeof(buf))) != 0) {
    4.55          if (c < 0) {
     5.1 --- a/tools/libxl/libxl_internal.h	Thu Oct 28 12:16:48 2010 +0100
     5.2 +++ b/tools/libxl/libxl_internal.h	Thu Oct 28 12:19:43 2010 +0100
     5.3 @@ -44,7 +44,7 @@
     5.4  #define LIBXL_PV_EXTRA_MEMORY 1024
     5.5  #define LIBXL_HVM_EXTRA_MEMORY 2048
     5.6  #define LIBXL_MIN_DOM0_MEM (128*1024)
     5.7 -#define QEMU_SIGNATURE "QemuDeviceModelRecord"
     5.8 +#define QEMU_SIGNATURE "DeviceModelRecord0002"
     5.9  
    5.10  #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
    5.11