xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Andrew Cooper <andrew.cooper3@citrix.com>
To: Xen-devel <xen-devel@lists.xen.org>
Cc: Wei Liu <wei.liu2@citrix.com>,
	Ross Lagerwall <ross.lagerwall@citrix.com>,
	Ian Jackson <Ian.Jackson@eu.citrix.com>,
	Ian Campbell <Ian.Campbell@citrix.com>,
	Andrew Cooper <andrew.cooper3@citrix.com>
Subject: [PATCH VERY RFC 3/5] tools/libxl: Implement libxl_domain_restore() for v2 streams
Date: Wed, 3 Sep 2014 18:14:09 +0100	[thread overview]
Message-ID: <1409764451-13053-4-git-send-email-andrew.cooper3@citrix.com> (raw)
In-Reply-To: <1409764451-13053-1-git-send-email-andrew.cooper3@citrix.com>

TODO:
 * Make it asynchronous
 * Integrate with the json series
 * Support for HVM elements

Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com>
Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
---
 tools/libxl/libxl.c          |   13 +++
 tools/libxl/libxl.h          |   12 +++
 tools/libxl/libxl_create.c   |  234 ++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_internal.h |    5 +
 4 files changed, 264 insertions(+)

diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index 2ae5fca..209d56a 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -878,6 +878,19 @@ int libxl_domain_suspend(libxl_ctx *ctx, uint32_t domid, int fd, int flags,
     return AO_ABORT(rc);
 }
 
+/*
+ * Restore a domain from an fd using the LibXenLight Domain Image Format v2.
+ * Returns 0 on success, non-zero otherwise.
+ */
+int libxl_domain_restore(libxl_ctx *ctx,
+                         libxl_domain_config *d_config, /* TMP until json is sorted */
+                         int restore_fd, uint32_t *domid,
+                         const libxl_asyncop_how *ao_how,
+                         const libxl_asyncprogress_how *aop_console_how)
+{
+    return libxl__domain_restore(ctx, restore_fd, domid, d_config);
+}
+
 int libxl_domain_pause(libxl_ctx *ctx, uint32_t domid)
 {
     int ret;
diff --git a/tools/libxl/libxl.h b/tools/libxl/libxl.h
index 460207b..fb14a91 100644
--- a/tools/libxl/libxl.h
+++ b/tools/libxl/libxl.h
@@ -857,6 +857,18 @@ int static inline libxl_domain_create_restore_0x040200(
 
 #endif
 
+/* Restore from a v2 stream
+ *
+ * TODO - Make this function properly asynchronous, and remove d_config
+ * parameter.
+ */
+int libxl_domain_restore(libxl_ctx *ctx,
+                         libxl_domain_config *d_config, /* TMP until json is sorted */
+                         int restore_fd, uint32_t *domid,
+                         const libxl_asyncop_how *ao_how,
+                         const libxl_asyncprogress_how *aop_console_how)
+                         LIBXL_EXTERNAL_CALLERS_ONLY;
+
   /* A progress report will be made via ao_console_how, of type
    * domain_create_console_available, when the domain's primary
    * console is available and can be connected to.
diff --git a/tools/libxl/libxl_create.c b/tools/libxl/libxl_create.c
index ee328e9..4f3a493 100644
--- a/tools/libxl/libxl_create.c
+++ b/tools/libxl/libxl_create.c
@@ -19,6 +19,7 @@
 
 #include "libxl_internal.h"
 #include "libxl_arch.h"
+#include "libxl_saverestore.h"
 
 #include <xc_dom.h>
 #include <xenguest.h>
@@ -1487,6 +1488,239 @@ int libxl_domain_create_restore(libxl_ctx *ctx, libxl_domain_config *d_config,
 }
 
 /*
+ * Helper for stub handlers.  This should go away when the stub handlers are
+ * implemented.
+ */
+static int skip_data(int fd, int len)
+{
+    unsigned char buf[4096];
+    int ret = 0;
+    ssize_t n;
+
+    while (len > 0) {
+        n = read(fd, buf, len < sizeof(buf) ? len : sizeof(buf));
+        if (n <= 0) {
+            if (errno == EAGAIN)
+                continue;
+            ret = ERROR_FAIL;
+            goto out;
+        }
+
+        len -= n;
+    }
+
+out:
+    return ret;
+}
+
+/*
+ * Process a domain JSON string read from fd.
+ * Returns 0 on success, non-zero otherwise.
+ */
+static int restore_domain_json(libxl_ctx *ctx, int fd, uint32_t len)
+{
+    int ret = 0;
+
+    ret = skip_data(fd, len);
+
+    return ret;
+}
+
+/*
+ * Process a libxc_context blob by passing it off to libxc.
+ * Returns 0 on success, non-zero otherwise.
+ */
+static int restore_libxc_context(libxl_ctx *ctx, int fd,
+                                 libxl_domain_config *d_config, uint32_t *domid)
+{
+    return do_domain_create(ctx, d_config, domid, fd, 0, NULL, NULL);
+}
+
+/*
+ * Process a set of xenstore key-value pairs.
+ * Returns 0 on success, non-zero otherwise.
+ */
+static int restore_xenstore_data(libxl_ctx *ctx, int fd, uint32_t len)
+{
+    int ret = 0;
+
+    ret = skip_data(fd, len);
+
+    return ret;
+}
+
+/*
+ * Process a qemu traditional emulator context.
+ * Returns 0 on success, non-zero otherwise.
+ */
+static int restore_qemu_traditional(libxl_ctx *ctx, int fd, uint32_t len)
+{
+    return 0;
+}
+
+/*
+ * Process a qemu upstream emulator context.
+ * Returns 0 on success, non-zero otherwise.
+ */
+static int restore_qemu_upstream(libxl_ctx *ctx, int fd, uint32_t len)
+{
+    return 0;
+}
+
+/*
+ * Process an emulator context record.
+ * Returns 0 on success, non-zero otherwise.
+ */
+static int restore_emulator_context(libxl_ctx *ctx, int fd, uint32_t len)
+{
+    struct restore_emulator_hdr hdr;
+    int ret = 0;
+
+    if (libxl_read_exactly(ctx, fd, &hdr, sizeof(hdr),
+                           "restore stream", "emulator context header")) {
+        ret = ERROR_FAIL;
+        goto out;
+    }
+    hdr.id = be32toh(hdr.id);
+    hdr.index = be32toh(hdr.index);
+    len -= sizeof(hdr);
+
+    switch (hdr.id) {
+        break;
+    case EMULATOR_QEMU_TRADITIONAL:
+        LIBXL__LOG(ctx, LIBXL__LOG_DEBUG,
+                   "Emulator: QEMU traditional, index %u", hdr.index);
+        ret = restore_qemu_traditional(ctx, fd, len);
+        break;
+    case EMULATOR_QEMU_UPSTREAM:
+        LIBXL__LOG(ctx, LIBXL__LOG_DEBUG,
+                   "Emulator: QEMU upstream, index %u", hdr.index);
+        ret = restore_qemu_upstream(ctx, fd, len);
+        break;
+    case EMULATOR_UNKNOWN:
+    default:
+        LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
+                   "Emulator: Unknown id 0x%08x, index %u", hdr.id, hdr.index);
+        ret = ERROR_FAIL;
+        break;
+    }
+
+    if (ret)
+        goto out;
+
+    ret = skip_data(fd, len);
+
+out:
+    return ret;
+}
+
+int libxl__domain_restore(libxl_ctx *ctx, int restore_fd, uint32_t *domid,
+                          libxl_domain_config *d_config)
+{
+    GC_INIT(ctx);
+    struct restore_hdr hdr;
+    struct restore_rec_hdr rechdr;
+    int ret = 0;
+
+    if (libxl_read_exactly(ctx, restore_fd, &hdr, sizeof(hdr),
+                           "restore stream", "stream header")) {
+        ret = ERROR_FAIL;
+        goto out;
+    }
+    hdr.ident = be64toh(hdr.ident);
+    hdr.version = be32toh(hdr.version);
+    hdr.options = be32toh(hdr.options);
+
+    if (hdr.ident != RESTORE_STREAM_IDENT) {
+        LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
+                   "Invalid ident: Got 0x%016"PRIx64, hdr.ident);
+        ret = ERROR_FAIL;
+        goto out;
+    }
+    if (hdr.version != RESTORE_STREAM_VERSION) {
+        LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
+                   "Invalid Version: Expected %u, Got %u",
+                   hdr.version, RESTORE_STREAM_VERSION);
+        ret = ERROR_FAIL;
+        goto out;
+    }
+    if (hdr.options & RESTORE_OPT_BIG_ENDIAN) {
+        LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
+                   "Unable to handle big endian streams");
+        ret = ERROR_FAIL;
+        goto out;
+    }
+    if (hdr.options & RESTORE_OPT_LEGACY) {
+        LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Unable to restore legacy streams");
+        ret = ERROR_FAIL;
+        goto out;
+    }
+
+    do {
+        if (libxl_read_exactly(ctx, restore_fd, &rechdr, sizeof(rechdr),
+                               "restore stream", "record header")) {
+            ret = ERROR_FAIL;
+            goto out;
+        }
+
+        switch (rechdr.type) {
+        case REC_TYPE_END:
+            LIBXL__LOG(ctx, LIBXL__LOG_DEBUG,
+                       "Record: END, length %u", rechdr.length);
+            if (rechdr.length != 0) {
+                LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
+                           "Encountered END record with non-zero length");
+                ret = ERROR_FAIL;
+            }
+            break;
+        case REC_TYPE_DOMAIN_JSON:
+            LIBXL__LOG(ctx, LIBXL__LOG_DEBUG,
+                       "Record: DOMAIN_JSON, length %u", rechdr.length);
+            ret = restore_domain_json(ctx, restore_fd, rechdr.length);
+            break;
+        case REC_TYPE_LIBXC_CONTEXT:
+            LIBXL__LOG(ctx, LIBXL__LOG_DEBUG,
+                       "Record: LIBXC_CONTEXT, length %u", rechdr.length);
+            ret = restore_libxc_context(ctx, restore_fd, d_config, domid);
+            break;
+        case REC_TYPE_XENSTORE_DATA:
+            LIBXL__LOG(ctx, LIBXL__LOG_DEBUG,
+                       "Record: XENSTORE_DATA, length %u", rechdr.length);
+            ret = restore_xenstore_data(ctx, restore_fd, rechdr.length);
+            break;
+        case REC_TYPE_EMULATOR_CONTEXT:
+            LIBXL__LOG(ctx, LIBXL__LOG_DEBUG,
+                       "Record: EMULATOR_CONTEXT, length %u", rechdr.length);
+            ret = restore_emulator_context(ctx, restore_fd, rechdr.length);
+            break;
+        default:
+            LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
+                       "Unknown record type: Got 0x%08x", rechdr.type);
+            ret = ERROR_FAIL;
+            break;
+        }
+
+        /* Skip until an 8 byte boundary for the next record. */
+        if (!ret) {
+            unsigned char pad[8];
+            ssize_t len = ROUNDUP(rechdr.length, REC_ALIGN_ORDER) - rechdr.length;
+
+            assert(len >= 0 && len < 8);
+
+            if (libxl_read_exactly(ctx, restore_fd, pad, len,
+                                   "restore stream", "padding")) {
+                ret = ERROR_FAIL;
+                goto out;
+            }
+        }
+    } while (!ret && rechdr.type != REC_TYPE_END);
+
+out:
+    GC_FREE;
+    return ret;
+}
+
+/*
  * Local variables:
  * mode: C
  * c-basic-offset: 4
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index 47fbf45..4128373 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -2876,6 +2876,11 @@ _hidden void libxl__domain_save_device_model(libxl__egc *egc,
 
 _hidden const char *libxl__device_model_savefile(libxl__gc *gc, uint32_t domid);
 
+/*----- Domain restore functions -----*/
+/* TODO - make this asychronous */
+_hidden int libxl__domain_restore(libxl_ctx *ctx, int fd, uint32_t *domid,
+                                  libxl_domain_config *d_config /* TMP until json is sorted */
+    );
 
 /*
  * Convenience macros.
-- 
1.7.10.4

  parent reply	other threads:[~2014-09-03 17:14 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-03 17:14 [PATCH VERY RFC 0/5] Libxl migration v2 support Andrew Cooper
2014-09-03 17:14 ` [PATCH VERY RFC 1/5] tools/libxl: Add support for writing a set of buffers asynchronously Andrew Cooper
2014-09-04  1:28   ` Wen Congyang
2014-09-03 17:14 ` [PATCH VERY RFC 2/5] tools/libxl: Stream v2 format Andrew Cooper
2014-09-03 17:14 ` Andrew Cooper [this message]
2014-09-03 17:14 ` [PATCH VERY RFC 4/5] tools/xl: Restore v2 streams using new libxl_domain_restore() interface Andrew Cooper
2014-09-03 17:14 ` [PATCH VERY RFC 5/5] tools/[lib]xl: Alter libxl_domain_suspend() to write a v2 stream Andrew Cooper

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1409764451-13053-4-git-send-email-andrew.cooper3@citrix.com \
    --to=andrew.cooper3@citrix.com \
    --cc=Ian.Campbell@citrix.com \
    --cc=Ian.Jackson@eu.citrix.com \
    --cc=ross.lagerwall@citrix.com \
    --cc=wei.liu2@citrix.com \
    --cc=xen-devel@lists.xen.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).