Skip to contentSkip to conversation

Migration Stream v2

PATCH In review Andrew Cooper 6 participants 79 messages Sep 10, 2014 – Sep 19, 2014

lore
Share & tools
lore
#0 lore raw

v7 cover Migration Stream v2

Hello,

Presented here for review is v7 of the Migration Stream v2 work, which contains a v1 for xl/libxl support, including on-the-fly conversion of legacy streams.

There are some TODOs and questions in the final section of the series, but the earlier sections are thoroughly tested and considered complete, having undergone extensive testing as part of XenServer.

A git version is available from the saverestore2-v7 branch of:
git://xenbits.xen.org/people/andrewcoop/xen.git
http://xenbits.xen.org/git-http/people/andrewcoop/xen.git

In summary,

Patches 1 to 5 are misc bugfixes/improvements
Patches 6 and 7 are specification documents for the new stream Patch 8 contains python infrastructure for conversion and verification Patches 8 to 19 form the libxc changes
Patches 20 to 29 form the libxl and xl changes

~Andrew

#1 lore raw

PATCH v7 1/29 tools/libxl: Fix stray blank line from debug logging

LOG() automatically adds a newline.

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
---
 tools/libxl/libxl_create.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/libxl/libxl_create.c +1 -1
index b36c719..a5e185e 100644
@@ -883,7 +883,7 @@ static void initiate_domain_create(libxl__egc *egc,
     }
 
     if (restore_fd >= 0) {
-        LOG(DEBUG, "restoring, not running bootloader\n");
+        LOG(DEBUG, "restoring, not running bootloader");
         domcreate_bootloader_done(egc, &dcs->bl, 0);
     } else  {
         LOG(DEBUG, "running bootloader");
Signature · 1 line
1.7.10.4
#3 lore raw

PATCH v7 2/29 tools/[lib]xl: Correct use of init/dispose for libxl_domain_restore_params

In reply to Andrew Cooper
CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
---
 tools/libxl/libxl.h      |    9 +++++++--
 tools/libxl/xl_cmdimpl.c |    6 ++++++
 2 files changed, 13 insertions(+), 2 deletions(-)
tools/libxl/libxl.h +7 -2
index dab3a67..5136d02 100644
@@ -847,10 +847,15 @@ int static inline libxl_domain_create_restore_0x040200(
     LIBXL_EXTERNAL_CALLERS_ONLY
 {
     libxl_domain_restore_params params;
-    params.checkpointed_stream = 0;
+    int ret;
 
-    return libxl_domain_create_restore(
+    libxl_domain_restore_params_init(&params);
+
+    ret = libxl_domain_create_restore(
         ctx, d_config, domid, restore_fd, &params, ao_how, aop_console_how);
+
+    libxl_domain_restore_params_dispose(&params);
+    return ret;
 }
 
 #define libxl_domain_create_restore libxl_domain_create_restore_0x040200
tools/libxl/xl_cmdimpl.c +6 -0
index 8a38077..26492fc 100644
@@ -2246,11 +2246,17 @@ start:
 
     if ( restoring ) {
         libxl_domain_restore_params params;
+
+        libxl_domain_restore_params_init(&params);
+
         params.checkpointed_stream = dom_info->checkpointed_stream;
         ret = libxl_domain_create_restore(ctx, &d_config,
                                           &domid, restore_fd,
                                           &params,
                                           0, autoconnect_console_how);
+
+        libxl_domain_restore_params_dispose(&params);
+
         /*
          * On subsequent reboot etc we should create the domain, not
          * restore/migrate-receive it again.
Signature · 1 line
1.7.10.4
#5 lore raw

PATCH v7 3/29 tools/libxc: Implement writev_exact() in the same style as write_exact()

In reply to Andrew Cooper

This implementation of writev_exact() will cope with an iovcnt greater than IOV_MAX because glibc will actually let this work anyway, and it is very useful not to have to work about this in the caller of writev_exact(). The caller is still required to ensure that the sum of iov_len's doesn't overflow a ssize_t.

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>

---
v3:
* Re-add adjustment for partial writes.
* Split min/max adjustment into separate patch.

v2:
* Remove adjustment for partial writes of a specific iov[] entry.

---
 tools/libxc/xc_private.c |   60 ++++++++++++++++++++++++++++++++++++++++++++++
 tools/libxc/xc_private.h |    2 ++
 2 files changed, 62 insertions(+)
tools/libxc/xc_private.c +60 -0
index 1c214dd..0941b06 100644
@@ -858,6 +858,66 @@ int write_exact(int fd, const void *data, size_t size)
     return 0;
 }
 
+int writev_exact(int fd, const struct iovec *iov, int iovcnt)
+{
+    struct iovec *local_iov = NULL;
+    int rc = 0, iov_idx = 0, saved_errno = 0;
+    ssize_t len;
+
+    while ( iov_idx < iovcnt )
+    {
+        /* Skip over iov[] entries with 0 length. */
+        while ( iov[iov_idx].iov_len == 0 )
+            if ( ++iov_idx == iovcnt )
+                goto out;
+
+        len = writev(fd, &iov[iov_idx], min(iovcnt - iov_idx, IOV_MAX));
+        saved_errno = errno;
+
+        if ( (len == -1) && (errno == EINTR) )
+            continue;
+        if ( len <= 0 )
+        {
+            rc = -1;
+            goto out;
+        }
+
+        /* Check iov[] to see whether we had a partial or complete write. */
+        while ( len > 0 && (iov_idx < iovcnt) )
+        {
+            if ( len >= iov[iov_idx].iov_len )
+                len -= iov[iov_idx++].iov_len;
+            else
+            {
+                /* Partial write of iov[iov_idx]. Copy iov so we can adjust
+                 * element iov_idx and resubmit the rest. */
+                if ( !local_iov )
+                {
+                    local_iov = malloc(iovcnt * sizeof(*iov));
+                    if ( !local_iov )
+                    {
+                        saved_errno = ENOMEM;
+                        goto out;
+                    }
+
+                    iov = memcpy(local_iov, iov, iovcnt * sizeof(*iov));
+                }
+
+                local_iov[iov_idx].iov_base += len;
+                local_iov[iov_idx].iov_len  -= len;
+                break;
+            }
+        }
+    }
+
+    saved_errno = 0;
+
+ out:
+    free(local_iov);
+    errno = saved_errno;
+    return rc;
+}
+
 int xc_ffs8(uint8_t x)
 {
     int i;
tools/libxc/xc_private.h +2 -0
index c50a7c9..97e4a56 100644
@@ -28,6 +28,7 @@
 #include <sys/stat.h>
 #include <stdlib.h>
 #include <sys/ioctl.h>
+#include <sys/uio.h>
 
 #include "xenctrl.h"
 #include "xenctrlosdep.h"
@@ -343,6 +344,7 @@ int xc_flush_mmu_updates(xc_interface *xch, struct xc_mmu *mmu);
 /* Return 0 on success; -1 on error setting errno. */
 int read_exact(int fd, void *data, size_t size); /* EOF => -1, errno=0 */
 int write_exact(int fd, const void *data, size_t size);
+int writev_exact(int fd, const struct iovec *iov, int iovcnt);
 
 int xc_ffs8(uint8_t x);
 int xc_ffs16(uint16_t x);
Signature · 1 line
1.7.10.4
Acked-by on v7 3/29
#6 lore raw

Re: [PATCH 03/29] tools/libxc: Implement writev_exact() in the same style as write_exact()

Quoted 7 lines · Andrew Cooper

This implementation of writev_exact() will cope with an iovcnt greater than IOV_MAX because glibc will actually let this work anyway, and it is very useful not to have to work about this in the caller of writev_exact(). The caller is still required to ensure that the sum of iov_len's doesn't overflow a ssize_t.

#7 lore raw

Re: [PATCH 03/29] tools/libxc: Implement writev_exact() in the same style as write_exact()

In reply to Andrew Cooper

tools/libxc/xc_private.h line 31

Quoted 9 lines · Andrew Cooper
tools/libxc/xc_private.h +1 -0
index c50a7c9..97e4a56 100644
@@ -28,6 +28,7 @@
 #include <sys/stat.h>
 #include <stdlib.h>
 #include <sys/ioctl.h>
+#include <sys/uio.h>

Trying to apply this resulted in (lots of):
compilation terminated.
In file included from xc_flask.c:19:0:
xc_private.h:31:21: fatal error: sys/uio.h: No such file or directory compilation terminated.

Quoted 1 line

From the stubdom build. I suppose given the environment it would be

reasonably easy to stub out a version using a loop and regular write?

Ian.

#8 lore raw

Re: [PATCH 03/29] tools/libxc: Implement writev_exact() in the same style as write_exact()

Quoted 16 lines · Ian Campbell

tools/libxc/xc_private.h line 31

Quoted 9 lines · Andrew Cooper
tools/libxc/xc_private.h +1 -0
index c50a7c9..97e4a56 100644
@@ -28,6 +28,7 @@
 #include <sys/stat.h>
 #include <stdlib.h>
 #include <sys/ioctl.h>
+#include <sys/uio.h>

Trying to apply this resulted in (lots of):
compilation terminated.
In file included from xc_flask.c:19:0:
xc_private.h:31:21: fatal error: sys/uio.h: No such file or directory compilation terminated.

Quoted 6 lines

From the stubdom build. I suppose given the environment it would be reasonably easy to stub out a version using a loop and regular write?

Ian.

Ah - I had forgotten about stubdoms. I will fix that up.

~Andrew

#9 lore raw

PATCH v7 4/29 libxc/bitops: Add or() to the available bitmap operations

In reply to Andrew Cooper
CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
---
 tools/libxc/xc_bitops.h |    8 ++++++++
 1 file changed, 8 insertions(+)
tools/libxc/xc_bitops.h +8 -0
index d8e0c16..dfce3b8 100644
@@ -60,4 +60,12 @@ static inline int test_and_set_bit(int nr, unsigned long *addr)
     return oldbit;
 }
 
+static inline void bitmap_or(unsigned long *dst, const unsigned long *other,
+                             int nr_bits)
+{
+    int i, nr_longs = (bitmap_size(nr_bits) / sizeof(unsigned long));
+    for ( i = 0; i < nr_longs; ++i )
+        dst[i] |= other[i];
+}
+
 #endif  /* XC_BITOPS_H */
Signature · 1 line
1.7.10.4
Acked-by on v7 4/29
#10 lore raw

Re: [PATCH 04/29] libxc/bitops: Add or() to the available bitmap operations

Quoted 1 line · Andrew Cooper

There's a bit of a trap here for callers wrt needing to make sure they are the same size (or at least both larger than nr_bits), but I think we can live with that.

Quoted 11 lines
CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
---
 tools/libxc/xc_bitops.h |    8 ++++++++
 1 file changed, 8 insertions(+)
tools/libxc/xc_bitops.h +0 -0
index d8e0c16..dfce3b8 100644

tools/libxc/xc_bitops.h static inline int test_and_set_bit(int nr, unsigned long *addr) · line 66

Quoted 7 lines
tools/libxc/xc_bitops.h +4 -0
@@ -60,4 +60,12 @@ static inline int test_and_set_bit(int nr, unsigned long *addr)
     return oldbit;
 }
 
+static inline void bitmap_or(unsigned long *dst, const unsigned long *other,
+                             int nr_bits)
+{
+    int i, nr_longs = (bitmap_size(nr_bits) / sizeof(unsigned long));

Ah, this doesn't round down because bitnmap_size already rounds up. Good.

#11 lore raw

PATCH v7 5/29 libxc/progress: Repurpose the current progress reporting infrastructure

In reply to Andrew Cooper

Not everything which needs reporting as progress comes with a range. Allow reporting "0 of 0" for a single progress statement.

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
---
 tools/libxc/xc_domain_restore.c |    2 +-
 tools/libxc/xc_domain_save.c    |    2 +-
 tools/libxc/xc_private.c        |   22 ++++++++++++++--------
 tools/libxc/xc_private.h        |    4 ++--
 tools/libxc/xtl_core.c          |    9 +++++----
 5 files changed, 23 insertions(+), 16 deletions(-)
tools/libxc/xc_domain_restore.c +1 -1
index b9a56d5..b411126 100644
@@ -1610,7 +1610,7 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
         goto out;
     }
 
-    xc_report_progress_start(xch, "Reloading memory pages", dinfo->p2m_size);
+    xc_report_progress_set(xch, "Reloading memory pages");
 
     /*
      * Now simply read each saved frame into its new machine frame.
tools/libxc/xc_domain_save.c +1 -1
index 254fdb3..02544f8 100644
@@ -1127,7 +1127,7 @@ int xc_domain_save(xc_interface *xch, int io_fd, uint32_t dom, uint32_t max_iter
                  "Saving memory: iter %d (last sent %u skipped %u)",
                  iter, sent_this_iter, skip_this_iter);
 
-        xc_report_progress_start(xch, reportbuf, dinfo->p2m_size);
+        xc_report_progress_set(xch, reportbuf);
 
         iter++;
         sent_this_iter = 0;
tools/libxc/xc_private.c +14 -8
index 0941b06..45537af 100644
@@ -388,18 +388,24 @@ void xc_osdep_log(xc_interface *xch, xentoollog_level level, int code, const cha
     va_end(args);
 }
 
-void xc_report_progress_start(xc_interface *xch, const char *doing,
-                              unsigned long total) {
+const char *xc_report_progress_set(xc_interface *xch, const char *doing)
+{
+    const char *old = xch->currently_progress_reporting;
+
     xch->currently_progress_reporting = doing;
-    xtl_progress(xch->error_handler, "xc", xch->currently_progress_reporting,
-                 0, total);
+    return old;
+}
+
+void xc_report_progress_single(xc_interface *xch, const char *doing)
+{
+    xtl_progress(xch->error_handler, "xc", doing, 0, 0);
 }
 
 void xc_report_progress_step(xc_interface *xch,
-                             unsigned long done, unsigned long total) {
-    assert(xch->currently_progress_reporting);
-    xtl_progress(xch->error_handler, "xc", xch->currently_progress_reporting,
-                 done, total);
+                             unsigned long done, unsigned long total)
+{
+    xtl_progress(xch->error_handler, "xc",
+                 xch->currently_progress_reporting ?: "???", done, total);
 }
 
 int xc_get_pfn_type_batch(xc_interface *xch, uint32_t dom,
tools/libxc/xc_private.h +2 -2
index 97e4a56..22021e9 100644
@@ -119,8 +119,8 @@ void xc_report(xc_interface *xch, xentoollog_logger *lg, xentoollog_level,
                int code, const char *fmt, ...)
      __attribute__((format(printf,5,6)));
 
-void xc_report_progress_start(xc_interface *xch, const char *doing,
-                              unsigned long total);
+const char *xc_report_progress_set(xc_interface *xch, const char *doing);
+void xc_report_progress_single(xc_interface *xch, const char *doing);
 void xc_report_progress_step(xc_interface *xch,
                              unsigned long done, unsigned long total);
 
tools/libxc/xtl_core.c +5 -4
index 326b97e..73add92 100644
@@ -66,13 +66,14 @@ void xtl_log(struct xentoollog_logger *logger,
 void xtl_progress(struct xentoollog_logger *logger,
                   const char *context, const char *doing_what,
                   unsigned long done, unsigned long total) {
-    int percent;
+    int percent = 0;
 
     if (!logger->progress) return;
 
-    percent = (total < LONG_MAX/100)
-        ? (done * 100) / total
-        : done / ((total + 99) / 100);
+    if ( total )
+        percent = (total < LONG_MAX/100)
+            ? (done * 100) / total
+            : done / ((total + 99) / 100);
 
     logger->progress(logger, context, doing_what, percent, done, total);
 }
Signature · 1 line
1.7.10.4
#12 lore raw

Re: [PATCH 05/29] libxc/progress: Repurpose the current progress reporting infrastructure

Quoted 1 line · Andrew Cooper

Not everything which needs reporting as progress comes with a range.

Please can you expand on your reasoning wrt the places where you are removing a usage of start with a size. Especially since you aren't changing any subsequent progress_step call to a you new singleton variant.

Quoted 1 line

Allow reporting "0 of 0" for a single progress statement.

Can we not arrange to suppress this entirely? Perhaps by turning total=0 into percent=-1 and having the lower level code omit that bit of the message in that case? If doing that you should update xentoollog.h to make this clear.

It appears you are also doing more than just this as well, by changing start to set for some reason. Even if you want to change the parameters it's not clear that the new name is in any way an improvement.

Thirdly you appear to also be arranging to make it allowable to call progress_step without having previously called progress_start/set. Why is this needed?

Quoted 112 lines
CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
---
 tools/libxc/xc_domain_restore.c |    2 +-
 tools/libxc/xc_domain_save.c    |    2 +-
 tools/libxc/xc_private.c        |   22 ++++++++++++++--------
 tools/libxc/xc_private.h        |    4 ++--
 tools/libxc/xtl_core.c          |    9 +++++----
 5 files changed, 23 insertions(+), 16 deletions(-)
tools/libxc/xc_domain_restore.c +1 -1
index b9a56d5..b411126 100644
@@ -1610,7 +1610,7 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
         goto out;
     }
 
-    xc_report_progress_start(xch, "Reloading memory pages", dinfo->p2m_size);
+    xc_report_progress_set(xch, "Reloading memory pages");
 
     /*
      * Now simply read each saved frame into its new machine frame.
tools/libxc/xc_domain_save.c +1 -1
index 254fdb3..02544f8 100644
@@ -1127,7 +1127,7 @@ int xc_domain_save(xc_interface *xch, int io_fd, uint32_t dom, uint32_t max_iter
                  "Saving memory: iter %d (last sent %u skipped %u)",
                  iter, sent_this_iter, skip_this_iter);
 
-        xc_report_progress_start(xch, reportbuf, dinfo->p2m_size);
+        xc_report_progress_set(xch, reportbuf);
 
         iter++;
         sent_this_iter = 0;
tools/libxc/xc_private.c +14 -8
index 0941b06..45537af 100644
@@ -388,18 +388,24 @@ void xc_osdep_log(xc_interface *xch, xentoollog_level level, int code, const cha
     va_end(args);
 }
 
-void xc_report_progress_start(xc_interface *xch, const char *doing,
-                              unsigned long total) {
+const char *xc_report_progress_set(xc_interface *xch, const char *doing)
+{
+    const char *old = xch->currently_progress_reporting;
+
     xch->currently_progress_reporting = doing;
-    xtl_progress(xch->error_handler, "xc", xch->currently_progress_reporting,
-                 0, total);
+    return old;
+}
+
+void xc_report_progress_single(xc_interface *xch, const char *doing)
+{
+    xtl_progress(xch->error_handler, "xc", doing, 0, 0);
 }
 
 void xc_report_progress_step(xc_interface *xch,
-                             unsigned long done, unsigned long total) {
-    assert(xch->currently_progress_reporting);
-    xtl_progress(xch->error_handler, "xc", xch->currently_progress_reporting,
-                 done, total);
+                             unsigned long done, unsigned long total)
+{
+    xtl_progress(xch->error_handler, "xc",
+                 xch->currently_progress_reporting ?: "???", done, total);
 }
 
 int xc_get_pfn_type_batch(xc_interface *xch, uint32_t dom,
tools/libxc/xc_private.h +2 -2
index 97e4a56..22021e9 100644
@@ -119,8 +119,8 @@ void xc_report(xc_interface *xch, xentoollog_logger *lg, xentoollog_level,
                int code, const char *fmt, ...)
      __attribute__((format(printf,5,6)));
 
-void xc_report_progress_start(xc_interface *xch, const char *doing,
-                              unsigned long total);
+const char *xc_report_progress_set(xc_interface *xch, const char *doing);
+void xc_report_progress_single(xc_interface *xch, const char *doing);
 void xc_report_progress_step(xc_interface *xch,
                              unsigned long done, unsigned long total);
 
tools/libxc/xtl_core.c +5 -4
index 326b97e..73add92 100644
@@ -66,13 +66,14 @@ void xtl_log(struct xentoollog_logger *logger,
 void xtl_progress(struct xentoollog_logger *logger,
                   const char *context, const char *doing_what,
                   unsigned long done, unsigned long total) {
-    int percent;
+    int percent = 0;
 
     if (!logger->progress) return;
 
-    percent = (total < LONG_MAX/100)
-        ? (done * 100) / total
-        : done / ((total + 99) / 100);
+    if ( total )
+        percent = (total < LONG_MAX/100)
+            ? (done * 100) / total
+            : done / ((total + 99) / 100);
 
     logger->progress(logger, context, doing_what, percent, done, total);
 }
#13 lore raw

Re: [PATCH 05/29] libxc/progress: Repurpose the current progress reporting infrastructure

Quoted 4 lines · Ian Campbell
Quoted 1 line · Andrew Cooper

Not everything which needs reporting as progress comes with a range.

Please can you expand on your reasoning wrt the places where you are removing a usage of start with a size.

I suppose more correctly, "I wish to introduce new uses of the progress functionality, which involves reporting progress without a range".

Quoted 3 lines

Especially since you aren't
changing any subsequent progress_step call to a you new singleton variant.

xc_domain_{save,restore}() are the only generators of progress, and they are/were being completely replaced, which would make xc_domain_{save,restore}2() the only generators of progress.

Quoted 6 lines
Quoted 2 lines

Allow reporting "0 of 0" for a single progress statement.

Can we not arrange to suppress this entirely? Perhaps by turning total=0 into percent=-1 and having the lower level code omit that bit of the message in that case? If doing that you should update xentoollog.h to make this clear.

The text string is generated by the provider of the progress callback, and I wanted a way which didn't alter this callback.

Changing to use percent=-1 for this would also work, but produce less meaningful messages by an existing implementation expecting the old behaviour.

Quoted 8 lines

It appears you are also doing more than just this as well, by changing start to set for some reason. Even if you want to change the parameters it's not clear that the new name is in any way an improvement.

Thirdly you appear to also be arranging to make it allowable to call progress_step without having previously called progress_start/set. Why is this needed?

It logically separates setting the string describing the current step, and providing the progress numbers.

This is IMO a failure in the previous design, and the distinction is used by the v2 code so the common functions to shunt pages don't need to be handed a string from their caller to identify the exact current step (which has already been latched in xch anyway).

~Andrew

#14 lore raw

Re: [PATCH 05/29] libxc/progress: Repurpose the current progress reporting infrastructure

Quoted 40 lines · Andrew Cooper
Quoted 4 lines · Ian Campbell
Quoted 1 line · Andrew Cooper

Not everything which needs reporting as progress comes with a range.

Please can you expand on your reasoning wrt the places where you are removing a usage of start with a size.

I suppose more correctly, "I wish to introduce new uses of the progress functionality, which involves reporting progress without a range".

Quoted 3 lines

Especially since you aren't
changing any subsequent progress_step call to a you new singleton variant.

xc_domain_{save,restore}() are the only generators of progress, and they are/were being completely replaced, which would make xc_domain_{save,restore}2() the only generators of progress.

Quoted 6 lines
Quoted 2 lines

Allow reporting "0 of 0" for a single progress statement.

Can we not arrange to suppress this entirely? Perhaps by turning total=0 into percent=-1 and having the lower level code omit that bit of the message in that case? If doing that you should update xentoollog.h to make this clear.

The text string is generated by the provider of the progress callback, and I wanted a way which didn't alter this callback.

Changing to use percent=-1 for this would also work, but produce less meaningful messages by an existing implementation expecting the old behaviour.

Quoted 8 lines

It appears you are also doing more than just this as well, by changing start to set for some reason. Even if you want to change the parameters it's not clear that the new name is in any way an improvement.

Thirdly you appear to also be arranging to make it allowable to call progress_step without having previously called progress_start/set. Why is this needed?

Quoted 7 lines

It logically separates setting the string describing the current step, and providing the progress numbers.

This is IMO a failure in the previous design, and the distinction is used by the v2 code so the common functions to shunt pages don't need to be handed a string from their caller to identify the exact current step (which has already been latched in xch anyway).

This may well all be reasonable, in which case please explain it in the commit log.

Quoted 3 lines

~Andrew

#15 lore raw

PATCH v7 6/29 docs: libxc migration stream specification

In reply to Andrew Cooper

Add the specification for a new migration stream format. The document includes all the details but to summarize:

The existing (legacy) format is dependant on the word size of the toolstack. This prevents domains from migrating from hosts running 32-bit toolstacks to hosts running 64-bit toolstacks (and vice-versa).

The legacy format lacks any version information making it difficult to extend in compatible way.

The new format has a header (the image header) with version information, a domain header with basic information of the domain and a stream of records for the image data.

The format will be used for future domain types (such as on ARM).

The specification is pandoc format (an extended markdown format) and the documentation build system is extended to support pandoc format documents.

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>

---
v7:
* Remove SAVING_CPU.
* More clear statements regarding record order and interleaving.
v6:
* Add pandoc -> txt/html conversion.
* Spelling fixes.
* Introduce SAVING_CPU record.

---
 docs/Makefile                            |   11 +-
 docs/specs/libxc-migration-stream.pandoc |  672 ++++++++++++++++++++++++++++++
 2 files changed, 682 insertions(+), 1 deletion(-)
 create mode 100644 docs/specs/libxc-migration-stream.pandoc
docs/Makefile +10 -1
index 2c0903b..777998d 100644
@@ -14,21 +14,26 @@ MAN5SRC-y := $(wildcard man/xl*.pod.5)
 
 MARKDOWNSRC-y := $(wildcard misc/*.markdown)
 
+PANDOCSRC-y += $(wildcard specs/*.pandoc)
+
 TXTSRC-y := $(wildcard misc/*.txt)
 
 
 DOC_MAN1 := $(patsubst man/%.pod.1,man1/%.1,$(MAN1SRC-y))
 DOC_MAN5 := $(patsubst man/%.pod.5,man5/%.5,$(MAN5SRC-y))
 DOC_HTML := $(patsubst %.markdown,html/%.html,$(MARKDOWNSRC-y)) \
+            $(patsubst %.pandoc,html/%.html,$(PANDOCSRC-y)) \
             $(patsubst man/%.pod.1,html/man/%.1.html,$(MAN1SRC-y)) \
             $(patsubst man/%.pod.5,html/man/%.5.html,$(MAN5SRC-y)) \
             $(patsubst %.txt,html/%.txt,$(TXTSRC-y)) \
             $(patsubst %,html/hypercall/%/index.html,$(DOC_ARCHES))
 DOC_TXT  := $(patsubst %.txt,txt/%.txt,$(TXTSRC-y)) \
             $(patsubst %.markdown,txt/%.txt,$(MARKDOWNSRC-y)) \
+            $(patsubst %.pandoc,txt/%.txt,$(PANDOCSRC-y)) \
             $(patsubst man/%.pod.1,txt/man/%.1.txt,$(MAN1SRC-y)) \
             $(patsubst man/%.pod.5,txt/man/%.5.txt,$(MAN5SRC-y))
-DOC_PDF  := $(patsubst %.markdown,pdf/%.pdf,$(MARKDOWNSRC-y))
+DOC_PDF  := $(patsubst %.markdown,pdf/%.pdf,$(MARKDOWNSRC-y)) \
+            $(patsubst %.pandoc,pdf/%.pdf,$(PANDOCSRC-y))
 
 .PHONY: all
 all: build
@@ -191,6 +196,10 @@ pdf/%.pdf: %.markdown
 	$(INSTALL_DIR) $(@D)
 	pandoc -N --toc --standalone $< --output $@
 
+pdf/%.pdf txt/%.txt html/%.html: %.pandoc
+	$(INSTALL_DIR) $(@D)
+	pandoc --number-sections --toc --standalone $< --output $@
+
 ifeq (,$(findstring clean,$(MAKECMDGOALS)))
 $(XEN_ROOT)/config/Docs.mk:
 	$(error You have to run ./configure before building docs)
docs/specs/libxc-migration-stream.pandoc new file +672 -0

672 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#16 lore raw

PATCH v7 7/29 docs: libxl migration stream specification

In reply to Andrew Cooper
---
 docs/specs/libxl-migration-stream.pandoc |  223 ++++++++++++++++++++++++++++++
 1 file changed, 223 insertions(+)
 create mode 100644 docs/specs/libxl-migration-stream.pandoc
docs/specs/libxl-migration-stream.pandoc new file +223 -0
new file mode 100644
index 0000000..88b9fca
@@ -0,0 +1,223 @@
+% LibXenLight Domain Image Format
+% Andrew Cooper <<[email protected]>>
+% Draft A
+
+Introduction
+============
+
+For the purposes of this document, `xl` is used as a representation of any
+implementer of the `libxl` API.  `xl` should be considered completely
+interchangeable with alternates, such as `libvirt` or `xenopsd-xl`.
+
+Purpose
+-------
+
+The _domain image format_ is the context of a running domain used for
+snapshots of a domain or for transferring domains between hosts during
+migration.
+
+There are a number of problems with the domain image format used in Xen 4.4
+and earlier (the _legacy format_)
+
+* There is no `libxl` context information.  `xl` is required to send certain
+  pieces of `libxl` context itself.
+
+* The contents of the stream is passed directly through `libxl` to `libxc`.
+  The legacy `libxc` format contained some information which belonged at the
+  `libxl` level, resulting in awkward layer violation to return the
+  information back to `libxl`.
+
+* The legacy `libxc` format was inextensible, causing inextensibility in the
+  legacy `libxl` handling.
+
+This design addresses the above points, allowing for a completely
+self-contained, extensible stream with each layer responsibile for its own
+appropriate information.
+
+
+Not Yet Included
+----------------
+
+The following features are not yet fully specified and will be
+included in a future draft.
+
+* Remus
+
+* ARM
+
+
+Overview
+========
+
+The image format consists of a _Header_, followed by 1 or more _Records_.
+Each record consists of a type and length field, followed by any type-specific
+data.
+
+\clearpage
+
+Header
+======
+
+The header identifies the stream as a `libxl` stream, including the version of
+this specification that it complies with.
+
+All fields in this header shall be in _big-endian_ byte order, regardless of
+the setting of the endianness bit.
+
+     0     1     2     3     4     5     6     7 octet
+    +-------------------------------------------------+
+    | ident                                           |
+    +-----------------------+-------------------------+
+    | version               | options                 |
+    +-----------------------+-------------------------+
+
+--------------------------------------------------------------------
+Field       Description
+----------- --------------------------------------------------------
+ident       0x4c6962786c466d74 ("LibxlFmt" in ASCII).
+
+version     0x00000002.  The version of this specification.
+
+options     bit 0: Endianness.    0 = little-endian, 1 = big-endian.
+
+            bit 1: Legacy Format. If set, this stream was created by
+                                  the legacy conversion tool.
+
+            bits 2-31: Reserved.
+--------------------------------------------------------------------
+
+The endianness shall be 0 (little-endian) for images generated on an
+i386, x86_64, or arm host.
+
+\clearpage
+
+
+Records
+=======
+
+A record has a record header, type specific data and a trailing footer.  If
+`length` is not a multiple of 8, the body is padded with zeroes to align the
+end of the record on an 8 octet boundary.
+
+     0     1     2     3     4     5     6     7 octet
+    +-----------------------+-------------------------+
+    | type                  | body_length             |
+    +-----------+-----------+-------------------------+
+    | body...                                         |
+    ...
+    |           | padding (0 to 7 octets)             |
+    +-----------+-------------------------------------+
+
+--------------------------------------------------------------------
+Field        Description
+-----------  -------------------------------------------------------
+type         0x00000000: END
+
+             0x00000001: DOMAIN_JSON
+
+             0x00000002: LIBXC_CONTEXT
+
+             0x00000003: XENSTORE_DATA
+
+             0x00000004: EMULATOR_CONTEXT
+
+             0x00000005 - 0x7FFFFFFF: Reserved for future _mandatory_
+             records.
+
+             0x80000000 - 0xFFFFFFFF: Reserved for future _optional_
+             records.
+
+body_length  Length in octets of the record body.
+
+body         Content of the record.
+
+padding      0 to 7 octets of zeros to pad the whole record to a multiple
+             of 8 octets.
+--------------------------------------------------------------------
+
+\clearpage
+
+END
+----
+
+A end record marks the end of the image, and shall be the final record
+in the stream.
+
+     0     1     2     3     4     5     6     7 octet
+    +-------------------------------------------------+
+
+The end record contains no fields; its body_length is 0.
+
+DOMAIN\_JSON
+-----------
+
+The JSON string containing a description of the entire domain.
+
+     0     1     2     3     4     5     6     7 octet
+    +-------------------------------------------------+
+    | domain json                                     |
+    ...
+    +-------------------------------------------------+
+
+This record is expected to be present for non-legacy streams.  Legacy streams
+did not contain any such information, and the conversion script will not be
+able to make it appear.  `xl` shall be responsible for appropriately handling
+the out-of-band context for legacy streams.
+
+LIBXC\_CONTEXT
+--------------
+
+A libxc context record is a marker, indicating that the stream should be
+handed to `xc_domain_restore()`.  `libxc` shall be resonsible for reading its
+own image format from the stream.
+
+     0     1     2     3     4     5     6     7 octet
+    +-------------------------------------------------+
+
+The libxc context record contains no fields; its body_length is 0[^1].
+
+
+[^1]: The sending side cannot calculate ahead of time how much data `libxc`
+might write into the stream, especially for live migration where the quantity
+of data is partially proportional to the elapsed time.
+
+XENSTORE\_DATA
+-------------
+
+A record containing xenstore key/value pairs of data.
+
+     0     1     2     3     4     5     6     7 octet
+    +-------------------------------------------------+
+    | xenstore key/value pairs                        |
+    ...
+    +-------------------------------------------------+
+
+EMULATOR\_CONTEXT
+----------------
+
+A context blob for a specific emulator associated with the domain.
+
+     0     1     2     3     4     5     6     7 octet
+    +------------------------+------------------------+
+    | emulator_id            | index                  |
+    +------------------------+------------------------+
+    | emulator_ctx                                    |
+    ...
+    +-------------------------------------------------+
+
+--------------------------------------------------------------------
+Field            Description
+------------     ---------------------------------------------------
+emulator_id      0x00000000: Unknown (In the case of a legacy stream)
+
+                 0x00000001: Qemu Traditional
+
+                 0x00000002: Qemu Upstream
+
+                 0x00000003 - 0xFFFFFFFF: Reserved for future emulators.
+
+index            Index of this emulator for the domain, if multiple
+                 emulators are in use.
+
+emulator_ctx     Emulator context blob.
+--------------------------------------------------------------------
Signature · 1 line
1.7.10.4
#17 lore raw

Re: [PATCH 07/29] docs: libxl migration stream specification

On Wed, 2014-09-10 at 18:10 +0100, Andrew Cooper wrote:

Please run a spell checker over this ("responsibile" was the first one I spotted, "resonsible" was the second)

Quoted 2 lines

+Purpose
+-------

I think this (and the equivalent section in the libxc level doc) should be moved to the commit log. It's useful right now as a motivator for the change but in a years time it will just be some random fluff in a doc that everyone has to page past to get to the interesting bits.

Quoted 42 lines
docs/specs/libxl-migration-stream.pandoc new file +42 -0
+This design addresses the above points, allowing for a completely
+self-contained, extensible stream with each layer responsibile for its own
+appropriate information.
+
+
+Not Yet Included
+----------------
+
+The following features are not yet fully specified and will be
+included in a future draft.
+
+* Remus
+
+* ARM
+
+
+Overview
+========
+
+The image format consists of a _Header_, followed by 1 or more _Records_.
+Each record consists of a type and length field, followed by any type-specific
+data.
+
+\clearpage
+
+Header
+======
+
+The header identifies the stream as a `libxl` stream, including the version of
+this specification that it complies with.
+
+All fields in this header shall be in _big-endian_ byte order, regardless of
+the setting of the endianness bit.
+
+     0     1     2     3     4     5     6     7 octet
+    +-------------------------------------------------+
+    | ident                                           |
+    +-----------------------+-------------------------+
+    | version               | options                 |
+    +-----------------------+-------------------------+
+
+--------------------------------------------------------------------

docs/specs/libxl-migration-stream.pandoc line 84

Quoted 10 lines
docs/specs/libxl-migration-stream.pandoc new file +10 -0
+Field       Description
+----------- --------------------------------------------------------
+ident       0x4c6962786c466d74 ("LibxlFmt" in ASCII).
+
+version     0x00000002.  The version of this specification.
+
+options     bit 0: Endianness.    0 = little-endian, 1 = big-endian.
+
+            bit 1: Legacy Format. If set, this stream was created by
+                                  the legacy conversion tool.

Are such streams otherwise distinguishable from a stream which was created directly? Should anything care about this?

I fear this is going to be used to paper over shortcomings in the conversion tool somehow, but I suppose I'll see later in the series.

Quoted 6 lines
docs/specs/libxl-migration-stream.pandoc new file +6 -0
+LIBXC\_CONTEXT
+--------------
+
+A libxc context record is a marker, indicating that the stream should be
+handed to `xc_domain_restore()`.  `libxc` shall be resonsible for reading its
+own image format from the stream.

docs/specs/libxl-migration-stream.pandoc line 182

Quoted 10 lines
docs/specs/libxl-migration-stream.pandoc new file +10 -0
+
+     0     1     2     3     4     5     6     7 octet
+    +-------------------------------------------------+
+
+The libxc context record contains no fields; its body_length is 0[^1].
+
+
+[^1]: The sending side cannot calculate ahead of time how much data `libxc`
+might write into the stream, especially for live migration where the quantity
+of data is partially proportional to the elapsed time.

I think this deserves to be in the main text and not a footnote (assuming that's what ^1 is). I think it should probably be expanded to explain how a toolstack can actually treat this, which I assume is to assume that xc_domain_restore will consume exactly its own business and then return.

Something somewhere also ought to say what libxc will have done on error, which is presumably to have left the stream in some indeterminate state and almost certainly not at the next libxl record boundary.

docs/specs/libxl-migration-stream.pandoc line 187

Quoted 5 lines
docs/specs/libxl-migration-stream.pandoc new file +5 -0
+
+XENSTORE\_DATA
+-------------
+
+A record containing xenstore key/value pairs of data.

In what format?

docs/specs/libxl-migration-stream.pandoc line 194

Quoted 6 lines
docs/specs/libxl-migration-stream.pandoc new file +6 -0
+     0     1     2     3     4     5     6     7 octet
+    +-------------------------------------------------+
+    | xenstore key/value pairs                        |
+    ...
+    +-------------------------------------------------+
+

Ian.

#18 lore raw

Re: [PATCH 07/29] docs: libxl migration stream specification

Quoted 11 lines · Ian Campbell

On Wed, 2014-09-10 at 18:10 +0100, Andrew Cooper wrote:

Please run a spell checker over this ("responsibile" was the first one I spotted, "resonsible" was the second)

Quoted 2 lines

+Purpose
+-------

I think this (and the equivalent section in the libxc level doc) should be moved to the commit log. It's useful right now as a motivator for the change but in a years time it will just be some random fluff in a doc that everyone has to page past to get to the interesting bits.

Ok.

Quoted 56 lines
Quoted 43 lines
docs/specs/libxl-migration-stream.pandoc new file +42 -0
+This design addresses the above points, allowing for a completely
+self-contained, extensible stream with each layer responsibile for its own
+appropriate information.
+
+
+Not Yet Included
+----------------
+
+The following features are not yet fully specified and will be
+included in a future draft.
+
+* Remus
+
+* ARM
+
+
+Overview
+========
+
+The image format consists of a _Header_, followed by 1 or more _Records_.
+Each record consists of a type and length field, followed by any type-specific
+data.
+
+\clearpage
+
+Header
+======
+
+The header identifies the stream as a `libxl` stream, including the version of
+this specification that it complies with.
+
+All fields in this header shall be in _big-endian_ byte order, regardless of
+the setting of the endianness bit.
+
+     0     1     2     3     4     5     6     7 octet
+    +-------------------------------------------------+
+    | ident                                           |
+    +-----------------------+-------------------------+
+    | version               | options                 |
+    +-----------------------+-------------------------+
+
+--------------------------------------------------------------------

docs/specs/libxl-migration-stream.pandoc line 84

Quoted 10 lines
docs/specs/libxl-migration-stream.pandoc new file +10 -0
+Field       Description
+----------- --------------------------------------------------------
+ident       0x4c6962786c466d74 ("LibxlFmt" in ASCII).
+
+version     0x00000002.  The version of this specification.
+
+options     bit 0: Endianness.    0 = little-endian, 1 = big-endian.
+
+            bit 1: Legacy Format. If set, this stream was created by
+                                  the legacy conversion tool.

Are such streams otherwise distinguishable from a stream which was created directly? Should anything care about this?

Quoted 2 lines

I fear this is going to be used to paper over shortcomings in the conversion tool somehow, but I suppose I'll see later in the series.

My concern here was regarding the d_config. A legacy converted stream cannot possibly contain a domain_json blob, so must have a d_config passed by the caller. Admittedly, this did pre-date realising that libxl currently allows the caller to blindly overwrite the config anyway, and this needs to continue for compatibility reasons.

However, knowing that a stream has been converted is a key debugging detail, even if this flag serves no other purpose from libxl's point of view.

Quoted 11 lines
Quoted 11 lines
docs/specs/libxl-migration-stream.pandoc new file +10 -0
+LIBXC\_CONTEXT
+--------------
+
+A libxc context record is a marker, indicating that the stream should be
+handed to `xc_domain_restore()`.  `libxc` shall be resonsible for reading its
+own image format from the stream.
+
+     0     1     2     3     4     5     6     7 octet
+    +-------------------------------------------------+
+
Quoted 8 lines

docs/specs/libxl-migration-stream.pandoc line 182

Quoted 6 lines
docs/specs/libxl-migration-stream.pandoc new file +6 -0
+The libxc context record contains no fields; its body_length is 0[^1].
+
+
+[^1]: The sending side cannot calculate ahead of time how much data `libxc`
+might write into the stream, especially for live migration where the quantity
+of data is partially proportional to the elapsed time.

I think this deserves to be in the main text and not a footnote (assuming that's what ^1 is).

^1 is indeed a footnote.

Quoted 4 lines

I think it should probably be expanded to
explain how a toolstack can actually treat this, which I assume is to assume that xc_domain_restore will consume exactly its own business and then return.

Ok

Quoted 4 lines

Something somewhere also ought to say what libxc will have done on error, which is presumably to have left the stream in some indeterminate state and almost certainly not at the next libxl record boundary.

Correct - I shall discuss this.

Quoted 7 lines

docs/specs/libxl-migration-stream.pandoc line 187

Quoted 6 lines
docs/specs/libxl-migration-stream.pandoc new file +5 -0
+
+XENSTORE\_DATA
+-------------
+
+A record containing xenstore key/value pairs of data.

In what format?

Ah, yes. "Whatever libxl currently does", although I guess I need to expand on that.

Quoted 9 lines

docs/specs/libxl-migration-stream.pandoc line 194

Quoted 7 lines
docs/specs/libxl-migration-stream.pandoc new file +6 -0
+     0     1     2     3     4     5     6     7 octet
+    +-------------------------------------------------+
+    | xenstore key/value pairs                        |
+    ...
+    +-------------------------------------------------+
+

Ian.

#19 lore raw

Re: [PATCH 07/29] docs: libxl migration stream specification

Quoted 14 lines · Andrew Cooper
Quoted 7 lines

docs/specs/libxl-migration-stream.pandoc line 84

Quoted 2 lines
docs/specs/libxl-migration-stream.pandoc new file +2 -0
+            bit 1: Legacy Format. If set, this stream was created by
+                                  the legacy conversion tool.

Are such streams otherwise distinguishable from a stream which was created directly? Should anything care about this?

I fear this is going to be used to paper over shortcomings in the conversion tool somehow, but I suppose I'll see later in the series.

My concern here was regarding the d_config. A legacy converted stream cannot possibly contain a domain_json blob, so must have a d_config passed by the caller. Admittedly, this did pre-date realising that libxl currently allows the caller to blindly overwrite the config anyway, and this needs to continue for compatibility reasons.

Quoted 3 lines

However, knowing that a stream has been converted is a key debugging detail, even if this flag serves no other purpose from libxl's point of view.

Would it be worth saying so e.g. "This flag is for debugging purposes only, toolstacks should not modify their behaviour based on this flag"?

Or could you do away with this bit and use version==1 to indicate this (since version==1 doesn't actually get used by a real legacy stream)

#20 lore raw

Re: [PATCH 07/29] docs: libxl migration stream specification

Quoted 14 lines · Ian Campbell
Quoted 13 lines · Andrew Cooper
Quoted 7 lines

docs/specs/libxl-migration-stream.pandoc line 84

Quoted 2 lines
docs/specs/libxl-migration-stream.pandoc new file +2 -0
+            bit 1: Legacy Format. If set, this stream was created by
+                                  the legacy conversion tool.

Are such streams otherwise distinguishable from a stream which was created directly? Should anything care about this?

I fear this is going to be used to paper over shortcomings in the conversion tool somehow, but I suppose I'll see later in the series.

My concern here was regarding the d_config. A legacy converted stream cannot possibly contain a domain_json blob, so must have a d_config passed by the caller. Admittedly, this did pre-date realising that libxl currently allows the caller to blindly overwrite the config anyway, and this needs to continue for compatibility reasons.

Quoted 5 lines
Quoted 3 lines

However, knowing that a stream has been converted is a key debugging detail, even if this flag serves no other purpose from libxl's point of view.

Would it be worth saying so e.g. "This flag is for debugging purposes only, toolstacks should not modify their behaviour based on this flag"?

If it turns out that way, then absolutely.

Quoted 5 lines

Or could you do away with this bit and use version==1 to indicate this (since version==1 doesn't actually get used by a real legacy stream)

I don't like this, concepually. The stream version is 2, as that states that "this stream conforms to v2 of the spec", which describes the framing etc. (The problem needing to be solved is that there was no older libxl stream where there should have been.)

~Andrew

#21 lore raw

PATCH v7 8/29 tools/python: Infrastructure relating to migration v2 streams

In reply to Andrew Cooper

* libxc.py and libxl.py contain stream structure and constants, as well as verification functions as per the specifications.

* tests.py contains some basic unit tests, usable by the existing unittest infrastructure.

* convert-legacy-stream.py will take a legacy migration stream as an input, and produce a v2 stream as an output, including reframing a legacy stream for libxl to consume.

* verify-stream-v2.py will verify a stream according to the v2 specification(s), including a libxc stream contained inside a libxl stream.

The files from xen/migration lives as part of the regular xen library, while convert-legacy-stream and verify-stream-v2 are installed as standalone scripts into PRIVATE_BINDIR.

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>

---
v7:
* Change to xen/migration namespace
* Libxl stream support
v6:
* Move to be part of tools/python and installed in proper locations

---
 tools/python/Makefile                         |    5 +
 tools/python/scripts/convert-legacy-stream.py |  671 +++++++++++++++++++++++++
 tools/python/scripts/verify-stream-v2.py      |  173 +++++++
 tools/python/setup.py                         |    1 +
 tools/python/xen/migration/libxc.py           |  434 ++++++++++++++++
 tools/python/xen/migration/libxl.py           |  190 +++++++
 tools/python/xen/migration/tests.py           |   55 ++
 tools/python/xen/migration/verify.py          |   37 ++
 8 files changed, 1566 insertions(+)
 create mode 100755 tools/python/scripts/convert-legacy-stream.py
 create mode 100755 tools/python/scripts/verify-stream-v2.py
 create mode 100644 tools/python/xen/migration/__init__.py
 create mode 100644 tools/python/xen/migration/libxc.py
 create mode 100644 tools/python/xen/migration/libxl.py
 create mode 100644 tools/python/xen/migration/tests.py
 create mode 100644 tools/python/xen/migration/verify.py
tools/python/Makefile +5 -0
index c914332..f1038a1 100644
@@ -20,9 +20,14 @@ build: genpath genwrap.py $(XEN_ROOT)/tools/libxl/libxl_types.idl \
 
 .PHONY: install
 install:
+	$(INSTALL_DIR) $(DESTDIR)$(PRIVATE_BINDIR)
+
 	CC="$(CC)" CFLAGS="$(CFLAGS) $(APPEND_LDFLAGS)" $(PYTHON) setup.py install \
 		$(PYTHON_PREFIX_ARG) --root="$(DESTDIR)" --force
 
+	$(INSTALL_PROG) scripts/convert-legacy-stream.py $(DESTDIR)$(PRIVATE_BINDIR)/convert-legacy-stream
+	$(INSTALL_PROG) scripts/verify-stream-v2.py $(DESTDIR)$(PRIVATE_BINDIR)/verify-stream-v2
+
 .PHONY: test
 test:
 	export LD_LIBRARY_PATH=$$(readlink -f ../libxc):$$(readlink -f ../xenstore); $(PYTHON) test.py -b -u
tools/python/scripts/convert-legacy-stream.py new file +671 -0

671 lines not on this page · raw message

tools/python/scripts/verify-stream-v2.py new file +173 -0

173 lines not on this page · raw message

tools/python/setup.py +1 -0
index 17ebb4a..74822f4 100644
@@ -43,6 +43,7 @@ setup(name            = 'xen',
       version         = '3.0',
       description     = 'Xen',
       packages        = ['xen',
+                         'xen.migration',
                          'xen.lowlevel',
                         ],
       ext_package = "xen.lowlevel",
tools/python/xen/migration/__init__.py new file +0 -0
new file mode 100644
index 0000000..e69de29
tools/python/xen/migration/libxc.py new file +434 -0

434 lines not on this page · raw message

tools/python/xen/migration/libxl.py new file +190 -0

190 lines not on this page · raw message

tools/python/xen/migration/tests.py new file +55 -0

55 lines not on this page · raw message

tools/python/xen/migration/verify.py new file +37 -0
new file mode 100644
index 0000000..7a42dbf
@@ -0,0 +1,37 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+"""
+Common verification infrastructure for v2 streams
+"""
+
+from struct import calcsize, unpack
+
+class StreamError(StandardError):
+    """Error with the stream"""
+    pass
+
+class RecordError(StandardError):
+    """Error with a record in the stream"""
+    pass
+
+
+class VerifyBase(object):
+
+    def __init__(self, info, read):
+
+        self.info = info
+        self.read = read
+
+    def rdexact(self, nr_bytes):
+        """Read exactly nr_bytes from the stream"""
+        _ = self.read(nr_bytes)
+        if len(_) != nr_bytes:
+            raise IOError("Stream truncated")
+        return _
+
+    def unpack_exact(self, fmt):
+        """Unpack a struct format string from the stream"""
+        sz = calcsize(fmt)
+        return unpack(fmt, self.rdexact(sz))
+
Signature · 1 line
1.7.10.4
#22 lore raw

PATCH v7 9/29 tools/libxc: save/restore v2 framework

In reply to Andrew Cooper

For testing purposes, the environmental variable "XG_MIGRATION_V2" allows the two save/restore codepaths to coexist, and have a runtime switch.

It is indended that once this series is less RFC, the v2 framework will completely replace v1.

---
 tools/libxc/Makefile              |    1 +
 tools/libxc/saverestore/common.h  |   15 +++++++++++++++
 tools/libxc/saverestore/restore.c |   23 +++++++++++++++++++++++
 tools/libxc/saverestore/save.c    |   19 +++++++++++++++++++
 tools/libxc/xc_domain_restore.c   |    8 ++++++++
 tools/libxc/xc_domain_save.c      |    6 ++++++
 tools/libxc/xenguest.h            |   13 +++++++++++++
 7 files changed, 85 insertions(+)
 create mode 100644 tools/libxc/saverestore/common.h
 create mode 100644 tools/libxc/saverestore/restore.c
 create mode 100644 tools/libxc/saverestore/save.c
tools/libxc/Makefile +1 -0
index 3b04027..564e805 100644
@@ -45,6 +45,7 @@ GUEST_SRCS-y :=
 GUEST_SRCS-y += xg_private.c xc_suspend.c
 ifeq ($(CONFIG_MIGRATE),y)
 GUEST_SRCS-y += xc_domain_restore.c xc_domain_save.c
+GUEST_SRCS-y += $(wildcard saverestore/*.c)
 GUEST_SRCS-y += xc_offline_page.c xc_compression.c
 else
 GUEST_SRCS-y += xc_nomigrate.c
tools/libxc/saverestore/common.h new file +15 -0
new file mode 100644
index 0000000..f1aff44
@@ -0,0 +1,15 @@
+#ifndef __COMMON__H
+#define __COMMON__H
+
+#include "../xg_private.h"
+
+#endif
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
tools/libxc/saverestore/restore.c new file +23 -0
new file mode 100644
index 0000000..6624baa
@@ -0,0 +1,23 @@
+#include "common.h"
+
+int xc_domain_restore2(xc_interface *xch, int io_fd, uint32_t dom,
+                       unsigned int store_evtchn, unsigned long *store_mfn,
+                       domid_t store_domid, unsigned int console_evtchn,
+                       unsigned long *console_mfn, domid_t console_domid,
+                       unsigned int hvm, unsigned int pae, int superpages,
+                       int checkpointed_stream,
+                       struct restore_callbacks *callbacks)
+{
+    IPRINTF("In experimental %s", __func__);
+    return -1;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
tools/libxc/saverestore/save.c new file +19 -0
new file mode 100644
index 0000000..f6ad734
@@ -0,0 +1,19 @@
+#include "common.h"
+
+int xc_domain_save2(xc_interface *xch, int io_fd, uint32_t dom, uint32_t max_iters,
+                    uint32_t max_factor, uint32_t flags,
+                    struct save_callbacks* callbacks, int hvm)
+{
+    IPRINTF("In experimental %s", __func__);
+    return -1;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
tools/libxc/xc_domain_restore.c +8 -0
index b411126..cec2dfc 100644
@@ -1490,6 +1490,14 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
     struct restore_ctx *ctx = &_ctx;
     struct domain_info_context *dinfo = &ctx->dinfo;
 
+    if ( getenv("XG_MIGRATION_V2") )
+    {
+        return xc_domain_restore2(
+            xch, io_fd, dom, store_evtchn, store_mfn,
+            store_domid, console_evtchn, console_mfn, console_domid,
+            hvm,  pae,  superpages, checkpointed_stream, callbacks);
+    }
+
     DPRINTF("%s: starting restore of new domid %u", __func__, dom);
 
     pagebuf_init(&pagebuf);
tools/libxc/xc_domain_save.c +6 -0
index 02544f8..a23ed68 100644
@@ -894,6 +894,12 @@ int xc_domain_save(xc_interface *xch, int io_fd, uint32_t dom, uint32_t max_iter
 
     int completed = 0;
 
+    if ( getenv("XG_MIGRATION_V2") )
+    {
+        return xc_domain_save2(xch, io_fd, dom, max_iters,
+                               max_factor, flags, callbacks, hvm);
+    }
+
     DPRINTF("%s: starting save of domid %u", __func__, dom);
 
     if ( hvm && !callbacks->switch_qemu_logdirty )
tools/libxc/xenguest.h +13 -0
index 40bbac8..55755cf 100644
@@ -88,6 +88,10 @@ int xc_domain_save(xc_interface *xch, int io_fd, uint32_t dom, uint32_t max_iter
                    uint32_t max_factor, uint32_t flags /* XCFLAGS_xxx */,
                    struct save_callbacks* callbacks, int hvm);
 
+/* Domain Save v2 */
+int xc_domain_save2(xc_interface *xch, int io_fd, uint32_t dom, uint32_t max_iters,
+                    uint32_t max_factor, uint32_t flags,
+                    struct save_callbacks* callbacks, int hvm);
 
 /* callbacks provided by xc_domain_restore */
 struct restore_callbacks {
@@ -124,6 +128,15 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
                       unsigned int hvm, unsigned int pae, int superpages,
                       int checkpointed_stream,
                       struct restore_callbacks *callbacks);
+
+/* Domain Restore v2 */
+int xc_domain_restore2(xc_interface *xch, int io_fd, uint32_t dom,
+                       unsigned int store_evtchn, unsigned long *store_mfn,
+                       domid_t store_domid, unsigned int console_evtchn,
+                       unsigned long *console_mfn, domid_t console_domid,
+                       unsigned int hvm, unsigned int pae, int superpages,
+                       int checkpointed_stream,
+                       struct restore_callbacks *callbacks);
 /**
  * xc_domain_restore writes a file to disk that contains the device
  * model saved state.
Signature · 1 line
1.7.10.4
#23 lore raw

Re: [PATCH 09/29] [HACK] tools/libxc: save/restore v2 framework

Quoted 5 lines · Andrew Cooper

For testing purposes, the environmental variable "XG_MIGRATION_V2" allows the two save/restore codepaths to coexist, and have a runtime switch.

It is indended that once this series is less RFC, the v2 framework will completely replace v1.

I think we are now at the point where this hack needs to be dropped from the series.

Quoted 168 lines
---
 tools/libxc/Makefile              |    1 +
 tools/libxc/saverestore/common.h  |   15 +++++++++++++++
 tools/libxc/saverestore/restore.c |   23 +++++++++++++++++++++++
 tools/libxc/saverestore/save.c    |   19 +++++++++++++++++++
 tools/libxc/xc_domain_restore.c   |    8 ++++++++
 tools/libxc/xc_domain_save.c      |    6 ++++++
 tools/libxc/xenguest.h            |   13 +++++++++++++
 7 files changed, 85 insertions(+)
 create mode 100644 tools/libxc/saverestore/common.h
 create mode 100644 tools/libxc/saverestore/restore.c
 create mode 100644 tools/libxc/saverestore/save.c
tools/libxc/Makefile +1 -0
index 3b04027..564e805 100644
@@ -45,6 +45,7 @@ GUEST_SRCS-y :=
 GUEST_SRCS-y += xg_private.c xc_suspend.c
 ifeq ($(CONFIG_MIGRATE),y)
 GUEST_SRCS-y += xc_domain_restore.c xc_domain_save.c
+GUEST_SRCS-y += $(wildcard saverestore/*.c)
 GUEST_SRCS-y += xc_offline_page.c xc_compression.c
 else
 GUEST_SRCS-y += xc_nomigrate.c
tools/libxc/saverestore/common.h new file +15 -0
new file mode 100644
index 0000000..f1aff44
@@ -0,0 +1,15 @@
+#ifndef __COMMON__H
+#define __COMMON__H
+
+#include "../xg_private.h"
+
+#endif
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
tools/libxc/saverestore/restore.c new file +23 -0
new file mode 100644
index 0000000..6624baa
@@ -0,0 +1,23 @@
+#include "common.h"
+
+int xc_domain_restore2(xc_interface *xch, int io_fd, uint32_t dom,
+                       unsigned int store_evtchn, unsigned long *store_mfn,
+                       domid_t store_domid, unsigned int console_evtchn,
+                       unsigned long *console_mfn, domid_t console_domid,
+                       unsigned int hvm, unsigned int pae, int superpages,
+                       int checkpointed_stream,
+                       struct restore_callbacks *callbacks)
+{
+    IPRINTF("In experimental %s", __func__);
+    return -1;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
tools/libxc/saverestore/save.c new file +19 -0
new file mode 100644
index 0000000..f6ad734
@@ -0,0 +1,19 @@
+#include "common.h"
+
+int xc_domain_save2(xc_interface *xch, int io_fd, uint32_t dom, uint32_t max_iters,
+                    uint32_t max_factor, uint32_t flags,
+                    struct save_callbacks* callbacks, int hvm)
+{
+    IPRINTF("In experimental %s", __func__);
+    return -1;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
tools/libxc/xc_domain_restore.c +8 -0
index b411126..cec2dfc 100644
@@ -1490,6 +1490,14 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
     struct restore_ctx *ctx = &_ctx;
     struct domain_info_context *dinfo = &ctx->dinfo;
 
+    if ( getenv("XG_MIGRATION_V2") )
+    {
+        return xc_domain_restore2(
+            xch, io_fd, dom, store_evtchn, store_mfn,
+            store_domid, console_evtchn, console_mfn, console_domid,
+            hvm,  pae,  superpages, checkpointed_stream, callbacks);
+    }
+
     DPRINTF("%s: starting restore of new domid %u", __func__, dom);
 
     pagebuf_init(&pagebuf);
tools/libxc/xc_domain_save.c +6 -0
index 02544f8..a23ed68 100644
@@ -894,6 +894,12 @@ int xc_domain_save(xc_interface *xch, int io_fd, uint32_t dom, uint32_t max_iter
 
     int completed = 0;
 
+    if ( getenv("XG_MIGRATION_V2") )
+    {
+        return xc_domain_save2(xch, io_fd, dom, max_iters,
+                               max_factor, flags, callbacks, hvm);
+    }
+
     DPRINTF("%s: starting save of domid %u", __func__, dom);
 
     if ( hvm && !callbacks->switch_qemu_logdirty )
tools/libxc/xenguest.h +13 -0
index 40bbac8..55755cf 100644
@@ -88,6 +88,10 @@ int xc_domain_save(xc_interface *xch, int io_fd, uint32_t dom, uint32_t max_iter
                    uint32_t max_factor, uint32_t flags /* XCFLAGS_xxx */,
                    struct save_callbacks* callbacks, int hvm);
 
+/* Domain Save v2 */
+int xc_domain_save2(xc_interface *xch, int io_fd, uint32_t dom, uint32_t max_iters,
+                    uint32_t max_factor, uint32_t flags,
+                    struct save_callbacks* callbacks, int hvm);
 
 /* callbacks provided by xc_domain_restore */
 struct restore_callbacks {
@@ -124,6 +128,15 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
                       unsigned int hvm, unsigned int pae, int superpages,
                       int checkpointed_stream,
                       struct restore_callbacks *callbacks);
+
+/* Domain Restore v2 */
+int xc_domain_restore2(xc_interface *xch, int io_fd, uint32_t dom,
+                       unsigned int store_evtchn, unsigned long *store_mfn,
+                       domid_t store_domid, unsigned int console_evtchn,
+                       unsigned long *console_mfn, domid_t console_domid,
+                       unsigned int hvm, unsigned int pae, int superpages,
+                       int checkpointed_stream,
+                       struct restore_callbacks *callbacks);
 /**
  * xc_domain_restore writes a file to disk that contains the device
  * model saved state.
#24 lore raw

Re: [PATCH 09/29] [HACK] tools/libxc: save/restore v2 framework

Quoted 8 lines · Ian Campbell
Quoted 5 lines · Andrew Cooper

For testing purposes, the environmental variable "XG_MIGRATION_V2" allows the two save/restore codepaths to coexist, and have a runtime switch.

It is indended that once this series is less RFC, the v2 framework will completely replace v1.

I think we are now at the point where this hack needs to be dropped from the series.

One problem is remus. My plan when dropping this patch was to drop all of xc_domain_{save/restore}.c as well, but without remus migration-v2 support available, this will break existing set-ups.

One option might be to have legacy and v2 sitting properly side-by-side in libxc for the transition period.

~Andrew

Quoted 170 lines
Quoted 169 lines
---
 tools/libxc/Makefile              |    1 +
 tools/libxc/saverestore/common.h  |   15 +++++++++++++++
 tools/libxc/saverestore/restore.c |   23 +++++++++++++++++++++++
 tools/libxc/saverestore/save.c    |   19 +++++++++++++++++++
 tools/libxc/xc_domain_restore.c   |    8 ++++++++
 tools/libxc/xc_domain_save.c      |    6 ++++++
 tools/libxc/xenguest.h            |   13 +++++++++++++
 7 files changed, 85 insertions(+)
 create mode 100644 tools/libxc/saverestore/common.h
 create mode 100644 tools/libxc/saverestore/restore.c
 create mode 100644 tools/libxc/saverestore/save.c
tools/libxc/Makefile +1 -0
index 3b04027..564e805 100644
@@ -45,6 +45,7 @@ GUEST_SRCS-y :=
 GUEST_SRCS-y += xg_private.c xc_suspend.c
 ifeq ($(CONFIG_MIGRATE),y)
 GUEST_SRCS-y += xc_domain_restore.c xc_domain_save.c
+GUEST_SRCS-y += $(wildcard saverestore/*.c)
 GUEST_SRCS-y += xc_offline_page.c xc_compression.c
 else
 GUEST_SRCS-y += xc_nomigrate.c
tools/libxc/saverestore/common.h new file +15 -0
new file mode 100644
index 0000000..f1aff44
@@ -0,0 +1,15 @@
+#ifndef __COMMON__H
+#define __COMMON__H
+
+#include "../xg_private.h"
+
+#endif
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
tools/libxc/saverestore/restore.c new file +23 -0
new file mode 100644
index 0000000..6624baa
@@ -0,0 +1,23 @@
+#include "common.h"
+
+int xc_domain_restore2(xc_interface *xch, int io_fd, uint32_t dom,
+                       unsigned int store_evtchn, unsigned long *store_mfn,
+                       domid_t store_domid, unsigned int console_evtchn,
+                       unsigned long *console_mfn, domid_t console_domid,
+                       unsigned int hvm, unsigned int pae, int superpages,
+                       int checkpointed_stream,
+                       struct restore_callbacks *callbacks)
+{
+    IPRINTF("In experimental %s", __func__);
+    return -1;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
tools/libxc/saverestore/save.c new file +19 -0
new file mode 100644
index 0000000..f6ad734
@@ -0,0 +1,19 @@
+#include "common.h"
+
+int xc_domain_save2(xc_interface *xch, int io_fd, uint32_t dom, uint32_t max_iters,
+                    uint32_t max_factor, uint32_t flags,
+                    struct save_callbacks* callbacks, int hvm)
+{
+    IPRINTF("In experimental %s", __func__);
+    return -1;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
tools/libxc/xc_domain_restore.c +8 -0
index b411126..cec2dfc 100644
@@ -1490,6 +1490,14 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
     struct restore_ctx *ctx = &_ctx;
     struct domain_info_context *dinfo = &ctx->dinfo;
 
+    if ( getenv("XG_MIGRATION_V2") )
+    {
+        return xc_domain_restore2(
+            xch, io_fd, dom, store_evtchn, store_mfn,
+            store_domid, console_evtchn, console_mfn, console_domid,
+            hvm,  pae,  superpages, checkpointed_stream, callbacks);
+    }
+
     DPRINTF("%s: starting restore of new domid %u", __func__, dom);
 
     pagebuf_init(&pagebuf);
tools/libxc/xc_domain_save.c +6 -0
index 02544f8..a23ed68 100644
@@ -894,6 +894,12 @@ int xc_domain_save(xc_interface *xch, int io_fd, uint32_t dom, uint32_t max_iter
 
     int completed = 0;
 
+    if ( getenv("XG_MIGRATION_V2") )
+    {
+        return xc_domain_save2(xch, io_fd, dom, max_iters,
+                               max_factor, flags, callbacks, hvm);
+    }
+
     DPRINTF("%s: starting save of domid %u", __func__, dom);
 
     if ( hvm && !callbacks->switch_qemu_logdirty )
tools/libxc/xenguest.h +13 -0
index 40bbac8..55755cf 100644
@@ -88,6 +88,10 @@ int xc_domain_save(xc_interface *xch, int io_fd, uint32_t dom, uint32_t max_iter
                    uint32_t max_factor, uint32_t flags /* XCFLAGS_xxx */,
                    struct save_callbacks* callbacks, int hvm);
 
+/* Domain Save v2 */
+int xc_domain_save2(xc_interface *xch, int io_fd, uint32_t dom, uint32_t max_iters,
+                    uint32_t max_factor, uint32_t flags,
+                    struct save_callbacks* callbacks, int hvm);
 
 /* callbacks provided by xc_domain_restore */
 struct restore_callbacks {
@@ -124,6 +128,15 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
                       unsigned int hvm, unsigned int pae, int superpages,
                       int checkpointed_stream,
                       struct restore_callbacks *callbacks);
+
+/* Domain Restore v2 */
+int xc_domain_restore2(xc_interface *xch, int io_fd, uint32_t dom,
+                       unsigned int store_evtchn, unsigned long *store_mfn,
+                       domid_t store_domid, unsigned int console_evtchn,
+                       unsigned long *console_mfn, domid_t console_domid,
+                       unsigned int hvm, unsigned int pae, int superpages,
+                       int checkpointed_stream,
+                       struct restore_callbacks *callbacks);
 /**
  * xc_domain_restore writes a file to disk that contains the device
  * model saved state.
#25 lore raw

Re: [PATCH 09/29] [HACK] tools/libxc: save/restore v2 framework

Quoted 5 lines · Andrew Cooper
Quoted 4 lines · Ian Campbell
Quoted 3 lines · Andrew Cooper

For testing purposes, the environmental variable "XG_MIGRATION_V2" allows the two save/restore codepaths to coexist, and have a runtime switch.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

It is indended that once this series is less RFC, the v2 framework will completely replace v1.

I think we are now at the point where this hack needs to be dropped from the series.

One problem is remus. My plan when dropping this patch was to drop all of xc_domain_{save/restore}.c as well, but without remus migration-v2 support available, this will break existing set-ups.

Hrm, how is that going wrt 4.5 freeze?

Quoted 2 lines

One option might be to have legacy and v2 sitting properly side-by-side in libxc for the transition period.

How long do you mean? Until 4.6?

#26 lore raw

Re: [PATCH 09/29] [HACK] tools/libxc: save/restore v2 framework

Quoted 6 lines · Ian Campbell
Quoted 5 lines · Andrew Cooper
Quoted 4 lines · Ian Campbell
Quoted 3 lines · Andrew Cooper

For testing purposes, the environmental variable "XG_MIGRATION_V2" allows the two save/restore codepaths to coexist, and have a runtime switch.

Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

It is indended that once this series is less RFC, the v2 framework will completely replace v1.

I think we are now at the point where this hack needs to be dropped from the series.

One problem is remus. My plan when dropping this patch was to drop all of xc_domain_{save/restore}.c as well, but without remus migration-v2 support available, this will break existing set-ups.

Hrm, how is that going wrt 4.5 freeze?

I haven’t heard seen anything since v5 of this series (for which I did some quick bugfixes and released v6).

I don't know, which probably means not good.

Quoted 4 lines
Quoted 3 lines

One option might be to have legacy and v2 sitting properly side-by-side in libxc for the transition period.

How long do you mean? Until 4.6?

Ideally, very early in the 4.6 dev period.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#27 lore raw

Re: [PATCH 09/29] [HACK] tools/libxc: save/restore v2 framework

Quoted 14 lines · Andrew Cooper
Quoted 13 lines · Ian Campbell
Quoted 12 lines · Andrew Cooper
Quoted 8 lines · Ian Campbell
Quoted 5 lines · Andrew Cooper

For testing purposes, the environmental variable "XG_MIGRATION_V2" allows the two save/restore codepaths to coexist, and have a runtime switch.

It is indended that once this series is less RFC, the v2 framework will completely replace v1.

I think we are now at the point where this hack needs to be dropped from the series.

One problem is remus. My plan when dropping this patch was to drop all of xc_domain_{save/restore}.c as well, but without remus migration-v2 support available, this will break existing set-ups.

Quoted 6 lines
Quoted 1 line

Hrm, how is that going wrt 4.5 freeze?

I haven’t heard seen anything since v5 of this series (for which I did some quick bugfixes and released v6).

I don't know, which probably means not good.

I think there have been remus positins since then, but I've been leaving them to Ian so I don't know either.

Quoted 6 lines
Quoted 4 lines
Quoted 3 lines

One option might be to have legacy and v2 sitting properly side-by-side in libxc for the transition period.

How long do you mean? Until 4.6?

Ideally, very early in the 4.6 dev period.

Well, also for the lifetime of 4.5...

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#28 lore raw

Re: [PATCH 09/29] [HACK] tools/libxc: save/restore v2 framework

In reply to Andrew Cooper
Quoted 6 lines · Andrew Cooper
Quoted 4 lines · Ian Campbell
Quoted 3 lines · Andrew Cooper
Quoted 2 lines · Ian Campbell
Quoted 1 line · Andrew Cooper

For testing purposes, the environmental variable "XG_MIGRATION_V2"

allows the

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

two save/restore codepaths to coexist, and have a runtime switch.

It is indended that once this series is less RFC, the v2 framework

will

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

completely replace v1.

I think we are now at the point where this hack needs to be dropped

from

Quoted 9 lines
Quoted 5 lines
Quoted 4 lines
Quoted 1 line

the series.

One problem is remus. My plan when dropping this patch was to drop all of xc_domain_{save/restore}.c as well, but without remus migration-v2 support available, this will break existing set-ups.

Hrm, how is that going wrt 4.5 freeze?

I haven’t heard seen anything since v5 of this series (for which I did some quick bugfixes and released v6).

FYI, thats not entirely true. Yang did post a set of RFC patches for remus support in migration v2, based on your V6 series (back in July)
http://lists.xenproject.org/archives/html/xen-devel/2014-07/msg01163.html

It would actually be helpful if you could cc me on the patches relevant to Remus,
or if there is anything specific to Remus that needs to be done. There are 100s of
posts on Xen devel every day and its hard to keep track of everything posted to
Xen devel.

And I looking at your patch sets in

http://xenbits.xen.org/gitweb/?p=people/andrewcoop/xen.git;a=shortlog;h=refs/heads/saverestore2-v6.3

I see that there is no support for Remus currently. Nor can I differentiate which parts of the
code fix to these "quick bug fixes" that you mentioned above. From the discussion over the remus rfc
patches, I only recall a bug related to vcpu context caching. But I cannot delineate that specific part from
the patches in the repo. So, if these bug fixes you are referring to are something else, please explain.

Quoted 8 lines

I don't know, which probably means not good.

Quoted 4 lines
Quoted 3 lines

One option might be to have legacy and v2 sitting properly side-by-side in libxc for the transition period.

How long do you mean? Until 4.6?

fwiw, I don't plan to work on remus migration v2 support until the remus netbuffer patches get in.
I have been at this for almost two release cycles. Its frustrating to iterate on feedbacks for patch 4/11
of a series for two months and then get a bunch of first-pass review for patch 6/10 at the eleventh hour
before a feature freeze, while the rest of the series has still not been reviewed at all for the past 3 months.

Quoted 8 lines

Ideally, very early in the 4.6 dev period.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#29 lore raw

Re: [PATCH 09/29] [HACK] tools/libxc: save/restore v2 framework

Quoted 23 lines · Shriram Rajagopalan
Quoted 6 lines · On Sep 11, 2014 4:08 AM, "Andrew Cooper" <[email protected]…
Quoted 4 lines · Ian Campbell
Quoted 3 lines · Andrew Cooper
Quoted 2 lines · Ian Campbell
Quoted 1 line · Andrew Cooper

For testing purposes, the environmental variable

"XG_MIGRATION_V2" allows the

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

two save/restore codepaths to coexist, and have a runtime switch.

It is indended that once this series is less RFC, the v2

framework will

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

completely replace v1.

I think we are now at the point where this hack needs to be

dropped from

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

the series.

One problem is remus. My plan when dropping this patch was to

drop all

Quoted 4 lines
Quoted 3 lines
Quoted 2 lines

of xc_domain_{save/restore}.c as well, but without remus migration-v2 support available, this will break existing set-ups.

Hrm, how is that going wrt 4.5 freeze?

Quoted 8 lines
Quoted 3 lines

I haven’t heard seen anything since v5 of this series (for which I did some quick bugfixes and released v6).

FYI, thats not entirely true. Yang did post a set of RFC patches for remus
support in migration v2, based on your V6 series (back in July)
http://lists.xenproject.org/archives/html/xen-devel/2014-07/msg01163.html

My apologies - it was v6 to v6.1

Quoted 14 lines

It would actually be helpful if you could cc me on the patches relevant to Remus,
or if there is anything specific to Remus that needs to be done. There are 100s of
posts on Xen devel every day and its hard to keep track of everything posted to
Xen devel.

And I looking at your patch sets in
http://xenbits.xen.org/gitweb/?p=people/andrewcoop/xen.git;a=shortlog;h=refs/heads/saverestore2-v6.3

Quoted 8 lines

I see that there is no support for Remus currently. Nor can I differentiate which parts of the
code fix to these "quick bug fixes" that you mentioned above. From the discussion over the remus rfc
patches, I only recall a bug related to vcpu context caching. But I cannot delineate that specific part from
the patches in the repo. So, if these bug fixes you are referring to are something else, please explain.

The bugfixes were referring to the vcpu context caching, but far more bits needed caching than the remus series fixed. The fixes were necessary even in the non-remus case and there were also improvements to receive side state machine to avoid vm corruption caused by an incorrect send order.

I did not integrate the remus specific patches as there were outstanding review concerns/comments.

Quoted 12 lines
Quoted 7 lines

I don't know, which probably means not good.

Quoted 2 lines
Quoted 2 lines

One option might be to have legacy and v2 sitting properly

side-by-side

Quoted 3 lines
Quoted 2 lines
Quoted 1 line

in libxc for the transition period.

How long do you mean? Until 4.6?

Quoted 8 lines

fwiw, I don't plan to work on remus migration v2 support until the remus netbuffer patches get in.
I have been at this for almost two release cycles. Its frustrating to iterate on feedbacks for patch 4/11
of a series for two months and then get a bunch of first-pass review for patch 6/10 at the eleventh hour
before a feature freeze, while the rest of the series has still not been reviewed at all for the past 3 months.

I can appreciate your frustration on this point, and do not envy your position.

The concern I have is that XenServer 6.5 is shipping with migrationv2 as we absolutely need it, given the 32->64bit upgrade. We were hoping to get the new format committed in 4.5 to guarantee stability, but that is looking increasingly unlikely to happen. As a result, it will probably have to go in early in 4.6, with extra care taken to ensure that no incompatible changes are made as a result of further review.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#30 lore raw

Re: [PATCH 09/29] [HACK] tools/libxc: save/restore v2 framework

Quoted 13 lines · Andrew Cooper
Quoted 11 lines · Shriram Rajagopalan
Quoted 6 lines · On Sep 11, 2014 4:08 AM, "Andrew Cooper" <[email protected]…
Quoted 4 lines · Ian Campbell
Quoted 3 lines · Andrew Cooper
Quoted 2 lines · Ian Campbell
Quoted 1 line · Andrew Cooper

For testing purposes, the environmental variable

"XG_MIGRATION_V2" allows the

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

two save/restore codepaths to coexist, and have a runtime switch.

Quoted 7 lines
Quoted 7 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

It is indended that once this series is less RFC, the v2

framework will

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

completely replace v1.

I think we are now at the point where this hack needs to be

dropped from

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

the series.

One problem is remus. My plan when dropping this patch was to

The other is 'tmem'. But 'tmem' has not yet been declared 'baked' so not making it work from a release perspective is OK.

With the 'tmem' maintainer hat on, however I would like to it work without having to do anything :-) Which reminds me I need to follow up on double-checking the migation hasn't bitrotten!

Quoted 3 lines
Quoted 3 lines

drop all

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

of xc_domain_{save/restore}.c as well, but without remus migration-v2 support available, this will break existing set-ups.

And by 'set-ups' you mean Xen 4.5 using the v1 migration tools and then out of tree patches on top of that. In other words, users of the libxc "API" (which we do not gurantee between releases - it is an internal API).

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 1 line

Hrm, how is that going wrt 4.5 freeze?

I haven’t heard seen anything since v5 of this series (for which I did some quick bugfixes and released v6).

Quoted 16 lines
Quoted 4 lines

FYI, thats not entirely true. Yang did post a set of RFC patches for remus
support in migration v2, based on your V6 series (back in July)
http://lists.xenproject.org/archives/html/xen-devel/2014-07/msg01163.html

My apologies - it was v6 to v6.1

Quoted 9 lines

It would actually be helpful if you could cc me on the patches relevant to Remus,
or if there is anything specific to Remus that needs to be done. There are 100s of
posts on Xen devel every day and its hard to keep track of everything posted to
Xen devel.

I've found that putting filters for the right keywords help in that. That is how I can subscribe to lkml without drinking the firehouse.

Quoted 14 lines
Quoted 5 lines

And I looking at your patch sets in
http://xenbits.xen.org/gitweb/?p=people/andrewcoop/xen.git;a=shortlog;h=refs/heads/saverestore2-v6.3

Quoted 8 lines

I see that there is no support for Remus currently. Nor can I differentiate which parts of the
code fix to these "quick bug fixes" that you mentioned above. From the discussion over the remus rfc
patches, I only recall a bug related to vcpu context caching. But I cannot delineate that specific part from
the patches in the repo. So, if these bug fixes you are referring to are something else, please explain.

Quoted 8 lines

The bugfixes were referring to the vcpu context caching, but far more bits needed caching than the remus series fixed. The fixes were necessary even in the non-remus case and there were also improvements to receive side state machine to avoid vm corruption caused by an incorrect send order.

I did not integrate the remus specific patches as there were outstanding review concerns/comments.

<nods> My recollection as well.

Quoted 13 lines
Quoted 13 lines
Quoted 8 lines

I don't know, which probably means not good.

Quoted 2 lines
Quoted 2 lines

One option might be to have legacy and v2 sitting properly

side-by-side

Quoted 3 lines
Quoted 2 lines
Quoted 1 line

in libxc for the transition period.

How long do you mean? Until 4.6?

Quoted 8 lines
Quoted 8 lines

fwiw, I don't plan to work on remus migration v2 support until the remus netbuffer patches get in.
I have been at this for almost two release cycles. Its frustrating to iterate on feedbacks for patch 4/11
of a series for two months and then get a bunch of first-pass review for patch 6/10 at the eleventh hour
before a feature freeze, while the rest of the series has still not been reviewed at all for the past 3 months.

What is the dependency on "full remus" support? Is there a list of all the different patchset that need to be reviewed?

Quoted 10 lines

I can appreciate your frustration on this point, and do not envy your position.

The concern I have is that XenServer 6.5 is shipping with migrationv2 as we absolutely need it, given the 32->64bit upgrade. We were hoping to get the new format committed in 4.5 to guarantee stability, but that is looking increasingly unlikely to happen. As a result, it will probably have to go in early in 4.6, with extra care taken to ensure that no incompatible changes are made as a result of further review.

Could you tell me what are the benefits of having a v1 to v2 runtime switch for developers/users besides the obvious (faster migration, easier to understand code)?

For me it sounded that this would allow the community to also test it and report bugs - which would be invaluable. And better yet there is a env flag to swap between a baseline and new code to ease the testing.

The risks seem quite contained - if something goes awry, folks can use the v1 version - which should have the same amount of bugs that it had in previous releases. And since it is on by default - so only dedicated users would turn v2 on.

From an maintaince perspective, it does add more code but then once feature freeze hits we do not pay attention to features anymore, but rather to bug-fixes.

Hm, Ian's - what are you folks take on it?

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#31 lore raw

Re: [PATCH 09/29] [HACK] tools/libxc: save/restore v2 framework

Quoted 17 lines · Konrad Rzeszutek Wilk
Quoted 16 lines · Andrew Cooper
Quoted 15 lines · Shriram Rajagopalan
Quoted 5 lines · On Sep 11, 2014 4:08 AM, "Andrew Cooper" <[email protected]…
Quoted 4 lines · Ian Campbell
Quoted 3 lines · Andrew Cooper
Quoted 2 lines · Ian Campbell
Quoted 1 line · Andrew Cooper

For testing purposes, the environmental variable

"XG_MIGRATION_V2" allows the

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

two save/restore codepaths to coexist, and have a runtime switch.

It is indended that once this series is less RFC, the v2

framework will

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

completely replace v1.

I think we are now at the point where this hack needs to be

dropped from

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

the series.

One problem is remus. My plan when dropping this patch was to

The other is 'tmem'. But 'tmem' has not yet been declared 'baked' so not making it work from a release perspective is OK.

With the 'tmem' maintainer hat on, however I would like to it work without having to do anything :-) Which reminds me I need to follow up on double-checking the migation hasn't bitrotten!

While reverse engineering the existing protocol is not too difficult, I think the TMEM migration needs redesigning. From memory, there is a huge quantity of metadata which is sent redundantly (tmem pool uuid with every frame). It would also benefit massively from some batching to help reduce the quantity of hypercalls made (5 per frame iirc).

Quoted 68 lines
Quoted 5 lines
Quoted 5 lines

drop all

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

of xc_domain_{save/restore}.c as well, but without remus migration-v2 support available, this will break existing set-ups.

And by 'set-ups' you mean Xen 4.5 using the v1 migration tools and then out of tree patches on top of that. In other words, users of the libxc "API" (which we do not gurantee between releases - it is an internal API).

Quoted 18 lines
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Hrm, how is that going wrt 4.5 freeze?

I haven’t heard seen anything since v5 of this series (for which I did some quick bugfixes and released v6).

FYI, thats not entirely true. Yang did post a set of RFC patches for remus
support in migration v2, based on your V6 series (back in July)
http://lists.xenproject.org/archives/html/xen-devel/2014-07/msg01163.html

My apologies - it was v6 to v6.1

Quoted 8 lines

It would actually be helpful if you could cc me on the patches relevant to Remus,
or if there is anything specific to Remus that needs to be done. There are 100s of
posts on Xen devel every day and its hard to keep track of everything posted to
Xen devel.

I've found that putting filters for the right keywords help in that. That is how I can subscribe to lkml without drinking the firehouse.

Quoted 12 lines
Quoted 4 lines

And I looking at your patch sets in
http://xenbits.xen.org/gitweb/?p=people/andrewcoop/xen.git;a=shortlog;h=refs/heads/saverestore2-v6.3

Quoted 8 lines

I see that there is no support for Remus currently. Nor can I differentiate which parts of the
code fix to these "quick bug fixes" that you mentioned above. From the discussion over the remus rfc
patches, I only recall a bug related to vcpu context caching. But I cannot delineate that specific part from
the patches in the repo. So, if these bug fixes you are referring to are something else, please explain.

Quoted 8 lines

The bugfixes were referring to the vcpu context caching, but far more bits needed caching than the remus series fixed. The fixes were necessary even in the non-remus case and there were also improvements to receive side state machine to avoid vm corruption caused by an incorrect send order.

I did not integrate the remus specific patches as there were outstanding review concerns/comments.

<nods> My recollection as well.

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

I don't know, which probably means not good.

Quoted 1 line
Quoted 1 line

One option might be to have legacy and v2 sitting properly

side-by-side

Quoted 2 lines
Quoted 2 lines
Quoted 1 line

in libxc for the transition period.

How long do you mean? Until 4.6?

Quoted 8 lines
Quoted 8 lines

fwiw, I don't plan to work on remus migration v2 support until the remus netbuffer patches get in.
I have been at this for almost two release cycles. Its frustrating to iterate on feedbacks for patch 4/11
of a series for two months and then get a bunch of first-pass review for patch 6/10 at the eleventh hour
before a feature freeze, while the rest of the series has still not been reviewed at all for the past 3 months.

Quoted 2 lines

What is the dependency on "full remus" support? Is there a list of all the different patchset that need to be reviewed?

As with TMEM, remus support needs redesigning, as it needs coordinated additions to both the libxc and libxl stream formats to support checkpoints without the current layer violations.

Quoted 10 lines
Quoted 10 lines

I can appreciate your frustration on this point, and do not envy your position.

The concern I have is that XenServer 6.5 is shipping with migrationv2 as we absolutely need it, given the 32->64bit upgrade. We were hoping to get the new format committed in 4.5 to guarantee stability, but that is looking increasingly unlikely to happen. As a result, it will probably have to go in early in 4.6, with extra care taken to ensure that no incompatible changes are made as a result of further review.

Quoted 3 lines

Could you tell me what are the benefits of having a v1 to v2 runtime switch for developers/users besides the obvious (faster migration, easier to understand code)?

Users should not notice a difference, other than it being faster.

From a developer point of view,

* It actually has some header information now
* It is independent of the bitness of the toolstack (which is the key reason we needed to do it for XenServers switch from 32 to 64bit dom0)
* The old format (little that it was) was basically inextensible for PV guests (See the PV MSRs thread)
* It has allowed for dropping 2-level PV guest support, as well as other 32bit Xen bits.

Quoted 5 lines

For me it sounded that this would allow the community to also test it and report bugs - which would be invaluable. And better yet there is a env flag to swap between a baseline and new code to ease the testing.

That was only supposed to be development, and removed when committed upstream.

~Andrew

Quoted 11 lines

The risks seem quite contained - if something goes awry, folks can use the v1 version - which should have the same amount of bugs that it had in previous releases. And since it is on by default - so only dedicated users would turn v2 on.

From an maintaince perspective, it does add more code but then once
feature freeze hits we do not pay attention to features anymore, but rather to bug-fixes.

Hm, Ian's - what are you folks take on it?

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#32 lore raw

Re: [PATCH 09/29] [HACK] tools/libxc: save/restore v2 framework

Quoted 28 lines · Andrew Cooper
Quoted 17 lines · Konrad Rzeszutek Wilk
Quoted 16 lines · Andrew Cooper
Quoted 15 lines · Shriram Rajagopalan
Quoted 5 lines · On Sep 11, 2014 4:08 AM, "Andrew Cooper" <[email protected]…
Quoted 4 lines · Ian Campbell
Quoted 3 lines · Andrew Cooper
Quoted 2 lines · Ian Campbell
Quoted 1 line · Andrew Cooper

For testing purposes, the environmental variable

"XG_MIGRATION_V2" allows the

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

two save/restore codepaths to coexist, and have a runtime switch.

It is indended that once this series is less RFC, the v2

framework will

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

completely replace v1.

I think we are now at the point where this hack needs to be

dropped from

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

the series.

One problem is remus. My plan when dropping this patch was to

The other is 'tmem'. But 'tmem' has not yet been declared 'baked' so not making it work from a release perspective is OK.

With the 'tmem' maintainer hat on, however I would like to it work without having to do anything :-) Which reminds me I need to follow up on double-checking the migation hasn't bitrotten!

Quoted 5 lines

While reverse engineering the existing protocol is not too difficult, I think the TMEM migration needs redesigning. From memory, there is a huge quantity of metadata which is sent redundantly (tmem pool uuid with every frame). It would also benefit massively from some batching to help reduce the quantity of hypercalls made (5 per frame iirc).

Ugh.
.. snip..

Quoted 27 lines
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines

before a feature freeze, while the rest of the series has still not been reviewed at all for the past 3 months.

What is the dependency on "full remus" support? Is there a list of all the different patchset that need to be reviewed?

As with TMEM, remus support needs redesigning, as it needs coordinated additions to both the libxc and libxl stream formats to support checkpoints without the current layer violations.

Quoted 10 lines
Quoted 10 lines

I can appreciate your frustration on this point, and do not envy your position.

The concern I have is that XenServer 6.5 is shipping with migrationv2 as we absolutely need it, given the 32->64bit upgrade. We were hoping to get the new format committed in 4.5 to guarantee stability, but that is looking increasingly unlikely to happen. As a result, it will probably have to go in early in 4.6, with extra care taken to ensure that no incompatible changes are made as a result of further review.

Quoted 3 lines

Could you tell me what are the benefits of having a v1 to v2 runtime switch for developers/users besides the obvious (faster migration, easier to understand code)?

Users should not notice a difference, other than it being faster.

From a developer point of view,

Quoted 7 lines

* It actually has some header information now
* It is independent of the bitness of the toolstack (which is the key reason we needed to do it for XenServers switch from 32 to 64bit dom0)
* The old format (little that it was) was basically inextensible for PV guests (See the PV MSRs thread)
* It has allowed for dropping 2-level PV guest support, as well as other 32bit Xen bits.

The disadvantages are that:
- Breaks tmem migration.
- Breaks outside users of libxc (granted we don't specify an API for that, but I am not a huge fan of putting barriers).

Quoted 24 lines
Quoted 6 lines

For me it sounded that this would allow the community to also test it and report bugs - which would be invaluable. And better yet there is a env flag to swap between a baseline and new code to ease the testing.

That was only supposed to be development, and removed when committed upstream.

~Andrew

Quoted 11 lines

The risks seem quite contained - if something goes awry, folks can use the v1 version - which should have the same amount of bugs that it had in previous releases. And since it is on by default - so only dedicated users would turn v2 on.

From an maintaince perspective, it does add more code but then once feature freeze hits we do not pay attention to features anymore, but rather to bug-fixes.

Hm, Ian's - what are you folks take on it?

#33 lore raw

PATCH v7 10/29 tools/libxc: C implementation of stream format

In reply to Andrew Cooper

Provide the C structures matching the binary (wire) format of the new stream format. All header/record fields are naturally aligned and explicit padding fields are used to ensure the correct layout (i.e., there is no need for any non-standard structure packing pragma or attribute).

Provide some helper functions for converting types to string for diagnostic purposes.

---
 tools/libxc/saverestore/common.c        |   72 +++++++++++++++
 tools/libxc/saverestore/common.h        |    8 ++
 tools/libxc/saverestore/stream_format.h |  148 +++++++++++++++++++++++++++++++
 3 files changed, 228 insertions(+)
 create mode 100644 tools/libxc/saverestore/common.c
 create mode 100644 tools/libxc/saverestore/stream_format.h
tools/libxc/saverestore/common.c new file +72 -0
new file mode 100644
index 0000000..d9e47ef
@@ -0,0 +1,72 @@
+#include "common.h"
+
+static const char *dhdr_types[] =
+{
+    [DHDR_TYPE_X86_PV]  = "x86 PV",
+    [DHDR_TYPE_X86_HVM] = "x86 HVM",
+    [DHDR_TYPE_X86_PVH] = "x86 PVH",
+    [DHDR_TYPE_ARM]     = "ARM",
+};
+
+const char *dhdr_type_to_str(uint32_t type)
+{
+    if ( type < ARRAY_SIZE(dhdr_types) && dhdr_types[type] )
+        return dhdr_types[type];
+
+    return "Reserved";
+}
+
+static const char *mandatory_rec_types[] =
+{
+    [REC_TYPE_END]                  = "End",
+    [REC_TYPE_PAGE_DATA]            = "Page data",
+    [REC_TYPE_X86_PV_INFO]          = "x86 PV info",
+    [REC_TYPE_X86_PV_P2M_FRAMES]    = "x86 PV P2M frames",
+    [REC_TYPE_X86_PV_VCPU_BASIC]    = "x86 PV vcpu basic",
+    [REC_TYPE_X86_PV_VCPU_EXTENDED] = "x86 PV vcpu extended",
+    [REC_TYPE_X86_PV_VCPU_XSAVE]    = "x86 PV vcpu xsave",
+    [REC_TYPE_SHARED_INFO]          = "Shared info",
+    [REC_TYPE_TSC_INFO]             = "TSC info",
+    [REC_TYPE_HVM_CONTEXT]          = "HVM context",
+    [REC_TYPE_HVM_PARAMS]           = "HVM params",
+    [REC_TYPE_TOOLSTACK]            = "Toolstack",
+    [REC_TYPE_X86_PV_VCPU_MSRS]     = "x86 PV vcpu msrs",
+    [REC_TYPE_VERIFY]               = "Verify",
+};
+
+const char *rec_type_to_str(uint32_t type)
+{
+    if ( !(type & REC_TYPE_OPTIONAL) )
+    {
+        if ( (type < ARRAY_SIZE(mandatory_rec_types)) &&
+             (mandatory_rec_types[type]) )
+            return mandatory_rec_types[type];
+    }
+
+    return "Reserved";
+}
+
+static void __attribute__((unused)) build_assertions(void)
+{
+    XC_BUILD_BUG_ON(sizeof(struct xc_sr_ihdr) != 24);
+    XC_BUILD_BUG_ON(sizeof(struct xc_sr_dhdr) != 16);
+    XC_BUILD_BUG_ON(sizeof(struct xc_sr_rhdr) != 8);
+
+    XC_BUILD_BUG_ON(sizeof(struct xc_sr_rec_page_data_header)  != 8);
+    XC_BUILD_BUG_ON(sizeof(struct xc_sr_rec_x86_pv_info)       != 8);
+    XC_BUILD_BUG_ON(sizeof(struct xc_sr_rec_x86_pv_p2m_frames) != 8);
+    XC_BUILD_BUG_ON(sizeof(struct xc_sr_rec_x86_pv_vcpu_hdr)   != 8);
+    XC_BUILD_BUG_ON(sizeof(struct xc_sr_rec_tsc_info)          != 24);
+    XC_BUILD_BUG_ON(sizeof(struct xc_sr_rec_hvm_params_entry)  != 16);
+    XC_BUILD_BUG_ON(sizeof(struct xc_sr_rec_hvm_params)        != 8);
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
tools/libxc/saverestore/common.h +8 -0
index f1aff44..cbecf0a 100644
@@ -3,6 +3,14 @@
 
 #include "../xg_private.h"
 
+#include "stream_format.h"
+
+/* String representation of Domain Header types. */
+const char *dhdr_type_to_str(uint32_t type);
+
+/* String representation of Record types. */
+const char *rec_type_to_str(uint32_t type);
+
 #endif
 /*
  * Local variables:
tools/libxc/saverestore/stream_format.h new file +148 -0

148 lines not on this page · raw message

Signature · 1 line
1.7.10.4
Acked-by on v7 10/29
#34 lore raw

Re: [PATCH 10/29] tools/libxc: C implementation of stream format

Quoted 10 lines · Andrew Cooper

Provide the C structures matching the binary (wire) format of the new stream format. All header/record fields are naturally aligned and explicit padding fields are used to ensure the correct layout (i.e., there is no need for any non-standard structure packing pragma or attribute).

Provide some helper functions for converting types to string for diagnostic purposes.

(David V also previously offered a reviewed-by, which isn't present here)

Ian.

#35 lore raw

PATCH v7 11/29 tools/libxc: noarch common code

In reply to Andrew Cooper

Add the context structure used to keep state during the save/restore process.

Define the set of architecture or domain type specific operations with a set of callbacks (save_ops, and restore_ops).

Add common functions for writing records.

---
v6:
* Use writev() instead of write()

---
 tools/libxc/saverestore/common.c |   41 +++++
 tools/libxc/saverestore/common.h |  306 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 347 insertions(+)
tools/libxc/saverestore/common.c +41 -0
index d9e47ef..cacca17 100644
@@ -1,3 +1,5 @@
+#include <assert.h>
+
 #include "common.h"
 
 static const char *dhdr_types[] =
@@ -46,6 +48,45 @@ const char *rec_type_to_str(uint32_t type)
     return "Reserved";
 }
 
+int write_split_record(struct xc_sr_context *ctx, struct xc_sr_record *rec,
+                       void *buf, size_t sz)
+{
+    static const char zeroes[(1u << REC_ALIGN_ORDER) - 1] = { 0 };
+
+    xc_interface *xch = ctx->xch;
+    typeof(rec->length) combined_length = rec->length + sz;
+    size_t record_length = ROUNDUP(combined_length, REC_ALIGN_ORDER);
+    struct iovec parts[] =
+    {
+        { &rec->type, sizeof(rec->type) },
+        { &combined_length, sizeof(combined_length) },
+        { rec->data, rec->length },
+        { buf, sz },
+        { (void*)zeroes, record_length - combined_length },
+    };
+
+    if ( record_length > REC_LENGTH_MAX )
+    {
+        ERROR("Record (0x%08x, %s) length %#x exceeds max (%#x)", rec->type,
+              rec_type_to_str(rec->type), rec->length, REC_LENGTH_MAX);
+        return -1;
+    }
+
+    if ( rec->length )
+        assert(rec->data);
+    if ( sz )
+        assert(buf);
+
+    if ( writev_exact(ctx->fd, parts, ARRAY_SIZE(parts)) )
+        goto err;
+
+    return 0;
+
+ err:
+    PERROR("Unable to write record to stream");
+    return -1;
+}
+
 static void __attribute__((unused)) build_assertions(void)
 {
     XC_BUILD_BUG_ON(sizeof(struct xc_sr_ihdr) != 24);
tools/libxc/saverestore/common.h +306 -0

319 lines not on this page · raw message

Signature · 1 line
1.7.10.4
Acked-by on v7 11/29
#36 lore raw

Re: [PATCH 11/29] tools/libxc: noarch common code

tools/libxc/saverestore/common.h const char *dhdr_type_to_str(uint32_t type); · line 39

Quoted 3 lines · Andrew Cooper
tools/libxc/saverestore/common.h +3 -0
+     * The page of data at the end of 'page' may be a read-only mapping of a
+     * running guest; it must not be modified.  If no transformation is
+     * required, the callee should leave '*pages' unouched.

untouched

tools/libxc/saverestore/common.h const char *dhdr_type_to_str(uint32_t type); · line 45

Quoted 6 lines
tools/libxc/saverestore/common.h +6 -0
+     *
+     * If a transformation is required, the callee should allocate themselves
+     * a local page using malloc() and return it via '*page'.
+     *
+     * The caller shall free() '*page' in all cases.  In the case that the
+     * callee enounceters an error, it should *NOT* free() the memory it

encounters

tools/libxc/saverestore/common.h const char *dhdr_type_to_str(uint32_t type); · line 128

Quoted 6 lines

[...]

tools/libxc/saverestore/common.h +5 -0
+    /**
+     * Process an individual record from the stream.  The caller shall take
+     * care of processing common records (e.g. END, PAGE_DATA).
+     *
+     * @return 0 for success, -1 for failure, or the sentinal value

sentinel

(might I recommend M-x ispell-region)

With the spelling fixed:

#37 lore raw

PATCH v7 12/29 tools/libxc: x86 common code

In reply to Andrew Cooper

Save/restore records common to all x86 domain types (HVM, PV).

This is only the TSC_INFO record.

---
 tools/libxc/saverestore/common_x86.c |   54 ++++++++++++++++++++++++++++++++++
 tools/libxc/saverestore/common_x86.h |   26 ++++++++++++++++
 2 files changed, 80 insertions(+)
 create mode 100644 tools/libxc/saverestore/common_x86.c
 create mode 100644 tools/libxc/saverestore/common_x86.h
tools/libxc/saverestore/common_x86.c new file +54 -0
new file mode 100644
index 0000000..d76c11f
@@ -0,0 +1,54 @@
+#include "common_x86.h"
+
+int write_tsc_info(struct xc_sr_context *ctx)
+{
+    xc_interface *xch = ctx->xch;
+    struct xc_sr_rec_tsc_info tsc = { 0 };
+    struct xc_sr_record rec =
+    {
+        .type = REC_TYPE_TSC_INFO,
+        .length = sizeof(tsc),
+        .data = &tsc
+    };
+
+    if ( xc_domain_get_tsc_info(xch, ctx->domid, &tsc.mode,
+                                &tsc.nsec, &tsc.khz, &tsc.incarnation) < 0 )
+    {
+        PERROR("Unable to obtain TSC information");
+        return -1;
+    }
+
+    return write_record(ctx, &rec);
+}
+
+int handle_tsc_info(struct xc_sr_context *ctx, struct xc_sr_record *rec)
+{
+    xc_interface *xch = ctx->xch;
+    struct xc_sr_rec_tsc_info *tsc = rec->data;
+
+    if ( rec->length != sizeof(*tsc) )
+    {
+        ERROR("TSC_INFO record wrong size: length %u, expected %zu",
+              rec->length, sizeof(*tsc));
+        return -1;
+    }
+
+    if ( xc_domain_set_tsc_info(xch, ctx->domid, tsc->mode,
+                                tsc->nsec, tsc->khz, tsc->incarnation) )
+    {
+        PERROR("Unable to set TSC information");
+        return -1;
+    }
+
+    return 0;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
tools/libxc/saverestore/common_x86.h new file +26 -0
new file mode 100644
index 0000000..5971bc5
@@ -0,0 +1,26 @@
+#ifndef __COMMON_X86__H
+#define __COMMON_X86__H
+
+#include "common.h"
+
+/*
+ * Obtains a domains TSC information from Xen and writes a TSC_INFO record
+ * into the stream.
+ */
+int write_tsc_info(struct xc_sr_context *ctx);
+
+/*
+ * Parses a TSC_INFO record and applies the result to the domain.
+ */
+int handle_tsc_info(struct xc_sr_context *ctx, struct xc_sr_record *rec);
+
+#endif
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
Signature · 1 line
1.7.10.4
#38 lore raw

PATCH v7 13/29 tools/libxc: x86 PV common code

In reply to Andrew Cooper

Add functions common to save and restore of x86 PV guests. This includes functions for dealing with the P2M and M2P and the VCPU context.

---
 tools/libxc/saverestore/common_x86_pv.c |  198 +++++++++++++++++++++++++++++++
 tools/libxc/saverestore/common_x86_pv.h |  102 ++++++++++++++++
 2 files changed, 300 insertions(+)
 create mode 100644 tools/libxc/saverestore/common_x86_pv.c
 create mode 100644 tools/libxc/saverestore/common_x86_pv.h
tools/libxc/saverestore/common_x86_pv.c new file +198 -0
new file mode 100644
index 0000000..d82d7e1
@@ -0,0 +1,198 @@
+#include <assert.h>
+
+#include "common_x86_pv.h"
+
+xen_pfn_t mfn_to_pfn(struct xc_sr_context *ctx, xen_pfn_t mfn)
+{
+    assert(mfn <= ctx->x86_pv.max_mfn);
+    return ctx->x86_pv.m2p[mfn];
+}
+
+bool mfn_in_pseudophysmap(struct xc_sr_context *ctx, xen_pfn_t mfn)
+{
+    return ( (mfn <= ctx->x86_pv.max_mfn) &&
+             (mfn_to_pfn(ctx, mfn) <= ctx->x86_pv.max_pfn) &&
+             (xc_pfn_to_mfn(mfn_to_pfn(ctx, mfn), ctx->x86_pv.p2m,
+                            ctx->x86_pv.width) == mfn) );
+}
+
+void dump_bad_pseudophysmap_entry(struct xc_sr_context *ctx, xen_pfn_t mfn)
+{
+    xc_interface *xch = ctx->xch;
+    xen_pfn_t pfn = ~0UL;
+
+    ERROR("mfn %#lx, max %#lx", mfn, ctx->x86_pv.max_mfn);
+
+    if ( (mfn != ~0UL) && (mfn <= ctx->x86_pv.max_mfn) )
+    {
+        pfn = ctx->x86_pv.m2p[mfn];
+        ERROR("  m2p[%#lx] = %#lx, max_pfn %#lx",
+              mfn, pfn, ctx->x86_pv.max_pfn);
+    }
+
+    if ( (pfn != ~0UL) && (pfn <= ctx->x86_pv.max_pfn) )
+        ERROR("  p2m[%#lx] = %#lx",
+              pfn, xc_pfn_to_mfn(pfn, ctx->x86_pv.p2m, ctx->x86_pv.width));
+}
+
+xen_pfn_t cr3_to_mfn(struct xc_sr_context *ctx, uint64_t cr3)
+{
+    if ( ctx->x86_pv.width == 8 )
+        return cr3 >> 12;
+    else
+        return (((uint32_t)cr3 >> 12) | ((uint32_t)cr3 << 20));
+}
+
+uint64_t mfn_to_cr3(struct xc_sr_context *ctx, xen_pfn_t mfn)
+{
+    if ( ctx->x86_pv.width == 8 )
+        return ((uint64_t)mfn) << 12;
+    else
+        return (((uint32_t)mfn << 12) | ((uint32_t)mfn >> 20));
+}
+
+int x86_pv_domain_info(struct xc_sr_context *ctx)
+{
+    xc_interface *xch = ctx->xch;
+    unsigned int guest_width, guest_levels, fpp;
+    int max_pfn;
+
+    /* Get the domain width */
+    if ( xc_domain_get_guest_width(xch, ctx->domid, &guest_width) )
+    {
+        PERROR("Unable to determine dom%d's width", ctx->domid);
+        return -1;
+    }
+
+    if ( guest_width == 4 )
+        guest_levels = 3;
+    else if ( guest_width == 8 )
+        guest_levels = 4;
+    else
+    {
+        ERROR("Invalid guest width %d.  Expected 32 or 64", guest_width * 8);
+        return -1;
+    }
+    ctx->x86_pv.width = guest_width;
+    ctx->x86_pv.levels = guest_levels;
+    fpp = PAGE_SIZE / ctx->x86_pv.width;
+
+    DPRINTF("%d bits, %d levels", guest_width * 8, guest_levels);
+
+    /* Get the domain's size */
+    max_pfn = xc_domain_maximum_gpfn(xch, ctx->domid);
+    if ( max_pfn < 0 )
+    {
+        PERROR("Unable to obtain guests max pfn");
+        return -1;
+    }
+
+    if ( max_pfn > 0 )
+    {
+        ctx->x86_pv.max_pfn = max_pfn;
+        ctx->x86_pv.p2m_frames = (ctx->x86_pv.max_pfn + fpp) / fpp;
+
+        DPRINTF("max_pfn %#x, p2m_frames %d", max_pfn, ctx->x86_pv.p2m_frames);
+    }
+
+    return 0;
+}
+
+int x86_pv_map_m2p(struct xc_sr_context *ctx)
+{
+    xc_interface *xch = ctx->xch;
+    long max_page = xc_maximum_ram_page(xch);
+    unsigned long m2p_chunks, m2p_size;
+    privcmd_mmap_entry_t *entries = NULL;
+    xen_pfn_t *extents_start = NULL;
+    int rc = -1, i;
+
+    if ( max_page < 0 )
+    {
+        PERROR("Failed to get maximum ram page");
+        goto err;
+    }
+
+    ctx->x86_pv.max_mfn = max_page;
+    m2p_size   = M2P_SIZE(ctx->x86_pv.max_mfn);
+    m2p_chunks = M2P_CHUNKS(ctx->x86_pv.max_mfn);
+
+    extents_start = malloc(m2p_chunks * sizeof(xen_pfn_t));
+    if ( !extents_start )
+    {
+        ERROR("Unable to allocate %lu bytes for m2p mfns",
+              m2p_chunks * sizeof(xen_pfn_t));
+        goto err;
+    }
+
+    if ( xc_machphys_mfn_list(xch, m2p_chunks, extents_start) )
+    {
+        PERROR("Failed to get m2p mfn list");
+        goto err;
+    }
+
+    entries = malloc(m2p_chunks * sizeof(privcmd_mmap_entry_t));
+    if ( !entries )
+    {
+        ERROR("Unable to allocate %lu bytes for m2p mapping mfns",
+              m2p_chunks * sizeof(privcmd_mmap_entry_t));
+        goto err;
+    }
+
+    for ( i = 0; i < m2p_chunks; ++i )
+        entries[i].mfn = extents_start[i];
+
+    ctx->x86_pv.m2p = xc_map_foreign_ranges(
+        xch, DOMID_XEN, m2p_size, PROT_READ,
+        M2P_CHUNK_SIZE, entries, m2p_chunks);
+
+    if ( !ctx->x86_pv.m2p )
+    {
+        PERROR("Failed to mmap m2p ranges");
+        goto err;
+    }
+
+    ctx->x86_pv.nr_m2p_frames = (M2P_CHUNK_SIZE >> PAGE_SHIFT) * m2p_chunks;
+
+#ifdef __i386__
+    /* 32 bit toolstacks automatically get the compat m2p */
+    ctx->x86_pv.compat_m2p_mfn0 = entries[0].mfn;
+#else
+    /* 64 bit toolstacks need to ask Xen specially for it */
+    {
+        struct xen_machphys_mfn_list xmml = {
+            .max_extents = 1,
+            .extent_start = { &ctx->x86_pv.compat_m2p_mfn0 }
+        };
+
+        rc = do_memory_op(xch, XENMEM_machphys_compat_mfn_list,
+                          &xmml, sizeof(xmml));
+        if ( rc || xmml.nr_extents != 1 )
+        {
+            PERROR("Failed to get compat mfn list from Xen");
+            rc = -1;
+            goto err;
+        }
+    }
+#endif
+
+    /* All Done */
+    rc = 0;
+    DPRINTF("max_mfn %#lx", ctx->x86_pv.max_mfn);
+
+err:
+    free(entries);
+    free(extents_start);
+
+    return rc;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
tools/libxc/saverestore/common_x86_pv.h new file +102 -0

102 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#39 lore raw

PATCH v7 14/29 tools/libxc: x86 PV save code

In reply to Andrew Cooper

Save the x86 PV specific parts of a domain. This is the X86_PV_INFO record, the P2M_FRAMES, the X86_PV_SHARED_INFO, the three different VCPU context records, and the MSR records.

The normalise_page callback used by the common code when writing the PAGE_DATA records, converts MFNs in page tables to PFNs.

---
v6:
* L1's don't have PSE. Bit 7 of an L1 is a PAT bit.
* Type-change failures are not fatal if live.

---
 tools/libxc/saverestore/common.h      |    1 +
 tools/libxc/saverestore/save_x86_pv.c |  850 +++++++++++++++++++++++++++++++++
 2 files changed, 851 insertions(+)
 create mode 100644 tools/libxc/saverestore/save_x86_pv.c
tools/libxc/saverestore/common.h +1 -0
index 0ded85d..dbb7f8d 100644
@@ -282,6 +282,7 @@ struct xc_sr_context
     };
 };
 
+extern struct xc_sr_save_ops save_ops_x86_pv;
 
 struct xc_sr_record
 {
tools/libxc/saverestore/save_x86_pv.c new file +850 -0

850 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#40 lore raw

PATCH v7 15/29 tools/libxc: x86 PV restore code

In reply to Andrew Cooper

Restore the x86 PV specific parts. The X86_PV_INFO, the P2M_FRAMES, SHARED_INFO, and VCPU context records.

The localise_page callback is called from the common PAGE_DATA code to convert PFNs in page tables to MFNs.

Page tables are pinned and the guest's P2M is updated when the stream is complete.

---
v7:
* Fix possible integer overflow in expand_p2m()
* Batch population of frames from normalisation. Reduces the number of populate physmap hypercalls by several orders of magnitude.
v6:
* Batch pagetable pinning.
* Fix fencepost error validating basic vcpu state.

---
 tools/libxc/saverestore/common.h         |    2 +
 tools/libxc/saverestore/restore_x86_pv.c | 1150 ++++++++++++++++++++++++++++++
 2 files changed, 1152 insertions(+)
 create mode 100644 tools/libxc/saverestore/restore_x86_pv.c
tools/libxc/saverestore/common.h +2 -0
index dbb7f8d..b72711c 100644
@@ -284,6 +284,8 @@ struct xc_sr_context
 
 extern struct xc_sr_save_ops save_ops_x86_pv;
 
+extern struct xc_sr_restore_ops restore_ops_x86_pv;
+
 struct xc_sr_record
 {
     uint32_t type;
tools/libxc/saverestore/restore_x86_pv.c new file +1150 -0

1,150 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#41 lore raw

PATCH v7 16/29 tools/libxc: x86 HVM save code

In reply to Andrew Cooper

Save the x86 HVM specific parts of the domain. This is considerably simpler than an x86 PV domain. Only the HVM_CONTEXT and HVM_PARAMS records are needed.

There is no need for any page normalisation.

---
 tools/libxc/saverestore/common.h       |    1 +
 tools/libxc/saverestore/save_x86_hvm.c |  259 ++++++++++++++++++++++++++++++++
 2 files changed, 260 insertions(+)
 create mode 100644 tools/libxc/saverestore/save_x86_hvm.c
tools/libxc/saverestore/common.h +1 -0
index b72711c..de7063e 100644
@@ -283,6 +283,7 @@ struct xc_sr_context
 };
 
 extern struct xc_sr_save_ops save_ops_x86_pv;
+extern struct xc_sr_save_ops save_ops_x86_hvm;
 
 extern struct xc_sr_restore_ops restore_ops_x86_pv;
 
tools/libxc/saverestore/save_x86_hvm.c new file +259 -0

259 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#42 lore raw

PATCH v7 17/29 tools/libxc: x86 HVM restore code

In reply to Andrew Cooper

Restore the x86 HVM specific parts of a domain. This is the HVM_CONTEXT and HVM_PARAMS records.

There is no need for any page localisation.

This also includes writing the trailing qemu save record to a file because this is what libxc currently does. This is intended to be moved into libxl proper in the future.

---
 tools/libxc/saverestore/common.h          |    1 +
 tools/libxc/saverestore/restore_x86_hvm.c |  355 +++++++++++++++++++++++++++++
 2 files changed, 356 insertions(+)
 create mode 100644 tools/libxc/saverestore/restore_x86_hvm.c
tools/libxc/saverestore/common.h +1 -0
index de7063e..193dd0e 100644
@@ -286,6 +286,7 @@ extern struct xc_sr_save_ops save_ops_x86_pv;
 extern struct xc_sr_save_ops save_ops_x86_hvm;
 
 extern struct xc_sr_restore_ops restore_ops_x86_pv;
+extern struct xc_sr_restore_ops restore_ops_x86_hvm;
 
 struct xc_sr_record
 {
tools/libxc/saverestore/restore_x86_hvm.c new file +355 -0

355 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#43 lore raw

PATCH v7 18/29 tools/libxc: noarch save code

In reply to Andrew Cooper

Save a domain, calling domain type specific function at the appropriate points. This implements the xc_domain_save2() API function which is equivalent to the existing xc_domain_save().

This writes the image and domain headers, and writes all the PAGE_DATA records using a "live" process.

---
v6:
* Use writev() instead of write() for PAGE_DATA.
* Introduce separate non-live suspend which avoids playing with logdirty.
* Fix a bug which would clobber the penultimate logdirty bitmap.

---
 tools/libxc/saverestore/save.c |  737 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 736 insertions(+), 1 deletion(-)
tools/libxc/saverestore/save.c +736 -1
index f6ad734..1517759 100644
@@ -1,11 +1,746 @@
+#include <assert.h>
+#include <arpa/inet.h>
+
 #include "common.h"
 
+/*
+ * Writes an Image header and Domain header into the stream.
+ */
+static int write_headers(struct xc_sr_context *ctx, uint16_t guest_type)
+{
+    xc_interface *xch = ctx->xch;
+    int32_t xen_version = xc_version(xch, XENVER_version, NULL);
+    struct xc_sr_ihdr ihdr =
+        {
+            .marker  = IHDR_MARKER,
+            .id      = htonl(IHDR_ID),
+            .version = htonl(IHDR_VERSION),
+            .options = htons(IHDR_OPT_LITTLE_ENDIAN),
+        };
+    struct xc_sr_dhdr dhdr =
+        {
+            .type       = guest_type,
+            .page_shift = XC_PAGE_SHIFT,
+            .xen_major  = (xen_version >> 16) & 0xffff,
+            .xen_minor  = (xen_version)       & 0xffff,
+        };
+
+    if ( xen_version < 0 )
+    {
+        PERROR("Unable to obtain Xen Version");
+        return -1;
+    }
+
+    if ( write_exact(ctx->fd, &ihdr, sizeof(ihdr)) )
+    {
+        PERROR("Unable to write Image Header to stream");
+        return -1;
+    }
+
+    if ( write_exact(ctx->fd, &dhdr, sizeof(dhdr)) )
+    {
+        PERROR("Unable to write Domain Header to stream");
+        return -1;
+    }
+
+    return 0;
+}
+
+/*
+ * Writes an END record into the stream.
+ */
+static int write_end_record(struct xc_sr_context *ctx)
+{
+    struct xc_sr_record end = { REC_TYPE_END, 0, NULL };
+
+    return write_record(ctx, &end);
+}
+
+/*
+ * Writes a batch of memory as a PAGE_DATA record into the stream.  The batch
+ * is constructed in ctx->save.batch_pfns.
+ *
+ * This function:
+ * - gets the types for each pfn in the batch.
+ * - for each pfn with real data:
+ *   - maps and attempts to localise the pages.
+ * - construct and writes a PAGE_DATA record into the stream.
+ */
+static int write_batch(struct xc_sr_context *ctx)
+{
+    xc_interface *xch = ctx->xch;
+    xen_pfn_t *mfns = NULL, *types = NULL;
+    void *guest_mapping = NULL;
+    void **guest_data = NULL;
+    void **local_pages = NULL;
+    int *errors = NULL, rc = -1;
+    unsigned i, p, nr_pages = 0;
+    unsigned nr_pfns = ctx->save.nr_batch_pfns;
+    void *page, *orig_page;
+    uint64_t *rec_pfns = NULL;
+    struct iovec *iov = NULL; int iovcnt = 0;
+    struct xc_sr_rec_page_data_header hdr = { 0 };
+    struct xc_sr_record rec =
+    {
+        .type = REC_TYPE_PAGE_DATA,
+    };
+
+    assert(nr_pfns != 0);
+
+    /* Mfns of the batch pfns. */
+    mfns = malloc(nr_pfns * sizeof(*mfns));
+    /* Types of the batch pfns. */
+    types = malloc(nr_pfns * sizeof(*types));
+    /* Errors from attempting to map the mfns. */
+    errors = malloc(nr_pfns * sizeof(*errors));
+    /* Pointers to page data to send.  Either mapped mfns or local allocations. */
+    guest_data = calloc(nr_pfns, sizeof(*guest_data));
+    /* Pointers to locally allocated pages.  Need freeing. */
+    local_pages = calloc(nr_pfns, sizeof(*local_pages));
+    /* iovec[] for writev(). */
+    iov = malloc((nr_pfns + 4) * sizeof(*iov));
+
+    if ( !mfns || !types || !errors || !guest_data || !local_pages || !iov )
+    {
+        ERROR("Unable to allocate arrays for a batch of %u pages",
+              nr_pfns);
+        goto err;
+    }
+
+    for ( i = 0; i < nr_pfns; ++i )
+    {
+        types[i] = mfns[i] = ctx->save.ops.pfn_to_gfn(ctx, ctx->save.batch_pfns[i]);
+
+        /* Likely a ballooned page. */
+        if ( mfns[i] == INVALID_MFN )
+        {
+            set_bit(ctx->save.batch_pfns[i], ctx->save.deferred_pages);
+            ++ctx->save.nr_deferred_pages;
+        }
+    }
+
+    rc = xc_get_pfn_type_batch(xch, ctx->domid, nr_pfns, types);
+    if ( rc )
+    {
+        PERROR("Failed to get types for pfn batch");
+        goto err;
+    }
+    rc = -1;
+
+    for ( i = 0; i < nr_pfns; ++i )
+    {
+        switch ( types[i] )
+        {
+        case XEN_DOMCTL_PFINFO_BROKEN:
+        case XEN_DOMCTL_PFINFO_XALLOC:
+        case XEN_DOMCTL_PFINFO_XTAB:
+            continue;
+        }
+
+        mfns[nr_pages++] = mfns[i];
+    }
+
+    if ( nr_pages > 0 )
+    {
+        guest_mapping = xc_map_foreign_bulk(
+            xch, ctx->domid, PROT_READ, mfns, errors, nr_pages);
+        if ( !guest_mapping )
+        {
+            PERROR("Failed to map guest pages");
+            goto err;
+        }
+    }
+
+    for ( i = 0, p = 0; i < nr_pfns; ++i )
+    {
+        switch ( types[i] )
+        {
+        case XEN_DOMCTL_PFINFO_BROKEN:
+        case XEN_DOMCTL_PFINFO_XALLOC:
+        case XEN_DOMCTL_PFINFO_XTAB:
+            continue;
+        }
+
+        if ( errors[p] )
+        {
+            ERROR("Mapping of pfn %#lx (mfn %#lx) failed %d",
+                  ctx->save.batch_pfns[i], mfns[p], errors[p]);
+            goto err;
+        }
+
+        orig_page = page = guest_mapping + (p * PAGE_SIZE);
+        rc = ctx->save.ops.normalise_page(ctx, types[i], &page);
+        if ( rc )
+        {
+            if ( rc == -1 && errno == EAGAIN )
+            {
+                set_bit(ctx->save.batch_pfns[i], ctx->save.deferred_pages);
+                ++ctx->save.nr_deferred_pages;
+                types[i] = XEN_DOMCTL_PFINFO_XTAB;
+                --nr_pages;
+            }
+            else
+                goto err;
+        }
+        else
+            guest_data[i] = page;
+
+        if ( page != orig_page )
+            local_pages[i] = page;
+        rc = -1;
+
+        ++p;
+    }
+
+    rec_pfns = malloc(nr_pfns * sizeof(*rec_pfns));
+    if ( !rec_pfns )
+    {
+        ERROR("Unable to allocate %zu bytes of memory for page data pfn list",
+              nr_pfns * sizeof(*rec_pfns));
+        goto err;
+    }
+
+    hdr.count = nr_pfns;
+
+    rec.length = sizeof(hdr);
+    rec.length += nr_pfns * sizeof(*rec_pfns);
+    rec.length += nr_pages * PAGE_SIZE;
+
+    for ( i = 0; i < nr_pfns; ++i )
+        rec_pfns[i] = ((uint64_t)(types[i]) << 32) | ctx->save.batch_pfns[i];
+
+    iov[0].iov_base = &rec.type;
+    iov[0].iov_len = sizeof(rec.type);
+
+    iov[1].iov_base = &rec.length;
+    iov[1].iov_len = sizeof(rec.length);
+
+    iov[2].iov_base = &hdr;
+    iov[2].iov_len = sizeof(hdr);
+
+    iov[3].iov_base = rec_pfns;
+    iov[3].iov_len = nr_pfns * sizeof(*rec_pfns);
+
+    for ( i = 0, iovcnt = 4; i < nr_pfns; ++i )
+    {
+        if ( guest_data[i] )
+        {
+            iov[iovcnt].iov_base = guest_data[i];
+            iov[iovcnt].iov_len = PAGE_SIZE;
+            iovcnt++;
+            --nr_pages;
+        }
+    }
+
+    if ( writev_exact(ctx->fd, iov, iovcnt) )
+    {
+        PERROR("Failed to write page data to stream");
+        goto err;
+    }
+
+    /* Sanity check we have sent all the pages we expected to. */
+    assert(nr_pages == 0);
+    rc = ctx->save.nr_batch_pfns = 0;
+
+ err:
+    free(rec_pfns);
+    if ( guest_mapping )
+        munmap(guest_mapping, nr_pages * PAGE_SIZE);
+    for ( i = 0; local_pages && i < nr_pfns; ++i )
+            free(local_pages[i]);
+    free(iov);
+    free(local_pages);
+    free(guest_data);
+    free(errors);
+    free(types);
+    free(mfns);
+
+    return rc;
+}
+
+/*
+ * Flush a batch of pfns into the stream.
+ */
+static int flush_batch(struct xc_sr_context *ctx)
+{
+    int rc = 0;
+
+    if ( ctx->save.nr_batch_pfns == 0 )
+        return rc;
+
+    rc = write_batch(ctx);
+
+    if ( !rc )
+    {
+        VALGRIND_MAKE_MEM_UNDEFINED(ctx->save.batch_pfns,
+                                    MAX_BATCH_SIZE * sizeof(*ctx->save.batch_pfns));
+    }
+
+    return rc;
+}
+
+/*
+ * Add a single pfn to the batch, flushing the batch if full.
+ */
+static int add_to_batch(struct xc_sr_context *ctx, xen_pfn_t pfn)
+{
+    int rc = 0;
+
+    if ( ctx->save.nr_batch_pfns == MAX_BATCH_SIZE )
+        rc = flush_batch(ctx);
+
+    if ( rc == 0 )
+        ctx->save.batch_pfns[ctx->save.nr_batch_pfns++] = pfn;
+
+    return rc;
+}
+
+/*
+ * Pause/suspend the domain, and refresh ctx->dominfo if required.
+ */
+static int suspend_domain(struct xc_sr_context *ctx)
+{
+    xc_interface *xch = ctx->xch;
+
+    /* TODO: Properly specify the return value from this callback.  All
+     * implementations currently appear to return 1 for success, whereas
+     * the legacy code checks for != 0. */
+    int cb_rc = ctx->save.callbacks->suspend(ctx->save.callbacks->data);
+
+    if ( cb_rc == 0 )
+    {
+        ERROR("save callback suspend() failed: %d", cb_rc);
+        return -1;
+    }
+
+    /* Refresh domain information. */
+    if ( (xc_domain_getinfo(xch, ctx->domid, 1, &ctx->dominfo) != 1) ||
+         (ctx->dominfo.domid != ctx->domid) )
+    {
+        PERROR("Unable to refresh domain information");
+        return -1;
+    }
+
+    /* Confirm the domain has actually been paused. */
+    if ( !ctx->dominfo.shutdown ||
+         (ctx->dominfo.shutdown_reason != SHUTDOWN_suspend) )
+    {
+        ERROR("Domain has not been suspended: shutdown %d, reason %d",
+              ctx->dominfo.shutdown, ctx->dominfo.shutdown_reason);
+        return -1;
+    }
+
+    xc_report_progress_single(xch, "Domain now suspended");
+
+    return 0;
+}
+
+/*
+ * Send all pages in the guests p2m.  Used as the first iteration of the live
+ * migration loop, and for a non-live save.
+ */
+static int send_all_pages(struct xc_sr_context *ctx)
+{
+    xc_interface *xch = ctx->xch;
+    xen_pfn_t p;
+    int rc;
+
+    for ( p = 0; p < ctx->save.p2m_size; ++p )
+    {
+        rc = add_to_batch(ctx, p);
+        if ( rc )
+            return rc;
+
+        /* Update progress every 4MB worth of memory sent. */
+        if ( (p & ((1U << (22 - 12)) - 1)) == 0 )
+            xc_report_progress_step(xch, p, ctx->save.p2m_size);
+    }
+
+    rc = flush_batch(ctx);
+    if ( rc )
+        return rc;
+
+    xc_report_progress_step(xch, ctx->save.p2m_size,
+                            ctx->save.p2m_size);
+    return 0;
+}
+
+/*
+ * Send a subset of pages in the guests p2m, according to the provided bitmap.
+ * Used for each subsequent iteration of the live migration loop.
+ *
+ * Bitmap is bounded by p2m_size.
+ */
+static int send_some_pages(struct xc_sr_context *ctx,
+                           unsigned long *bitmap,
+                           unsigned long entries)
+{
+    xc_interface *xch = ctx->xch;
+    xen_pfn_t p;
+    unsigned long written;
+    int rc;
+
+    for ( p = 0, written = 0; p < ctx->save.p2m_size; ++p )
+    {
+        if ( !test_bit(p, bitmap) )
+            continue;
+
+        rc = add_to_batch(ctx, p);
+        if ( rc )
+            return rc;
+
+        /* Update progress every 4MB worth of memory sent. */
+        if ( (written & ((1U << (22 - 12)) - 1)) == 0 )
+            xc_report_progress_step(xch, written, entries);
+
+        ++written;
+    }
+
+    rc = flush_batch(ctx);
+    if ( rc )
+        return rc;
+
+    if ( written > entries )
+        DPRINTF("Bitmap contained more entries than expected...");
+
+    xc_report_progress_step(xch, entries, entries);
+    return 0;
+}
+
+static int update_progress_string(struct xc_sr_context *ctx,
+                                  char **str, unsigned iter)
+{
+    xc_interface *xch = ctx->xch;
+
+    free(*str);
+    if ( asprintf(str, "Memory iteration %u of %u",
+                  iter, ctx->save.max_iterations) == -1 )
+    {
+        ERROR("Unable to allocate progress string");
+        return -1;
+    }
+
+    xc_report_progress_set(xch, *str);
+    return 0;
+}
+
+/*
+ * Send all domain memory.  This is the heart of the live migration loop.
+ */
+static int send_domain_memory_live(struct xc_sr_context *ctx)
+{
+    xc_interface *xch = ctx->xch;
+    DECLARE_HYPERCALL_BUFFER(unsigned long, to_send);
+    xc_shadow_op_stats_t stats = { 0, ctx->save.p2m_size };
+    char *progress_str = NULL;
+    unsigned x;
+    int rc = -1;
+
+    to_send = xc_hypercall_buffer_alloc_pages(
+        xch, to_send, NRPAGES(bitmap_size(ctx->save.p2m_size)));
+
+    ctx->save.batch_pfns = malloc(MAX_BATCH_SIZE * sizeof(*ctx->save.batch_pfns));
+    ctx->save.deferred_pages = calloc(1, bitmap_size(ctx->save.p2m_size));
+
+    if ( !ctx->save.batch_pfns || !to_send || !ctx->save.deferred_pages )
+    {
+        ERROR("Unable to allocate memory for to_{send,fix}/batch bitmaps");
+        goto out;
+    }
+
+    /* This juggling is required if logdirty is enabled for VRAM tracking. */
+    if ( xc_shadow_control(xch, ctx->domid,
+                           XEN_DOMCTL_SHADOW_OP_ENABLE_LOGDIRTY,
+                           NULL, 0, NULL, 0, NULL) < 0 )
+    {
+        int frc = xc_shadow_control(xch, ctx->domid, XEN_DOMCTL_SHADOW_OP_OFF,
+                                    NULL, 0, NULL, 0, NULL);
+        if ( frc >= 0 )
+            frc = xc_shadow_control(xch, ctx->domid,
+                                    XEN_DOMCTL_SHADOW_OP_ENABLE_LOGDIRTY,
+                                    NULL, 0, NULL, 0, NULL);
+        if ( frc < 0 )
+        {
+            PERROR("Failed to enable logdirty: rc %d", frc);
+            goto out;
+        }
+    }
+
+    rc = update_progress_string(ctx, &progress_str, 0);
+    if ( rc )
+        goto out;
+
+    rc = send_all_pages(ctx);
+    if ( rc )
+        goto out;
+
+    for ( x = 1;
+          ((x < ctx->save.max_iterations) &&
+           (stats.dirty_count > ctx->save.dirty_threshold)); ++x )
+    {
+        if ( xc_shadow_control(
+                 xch, ctx->domid, XEN_DOMCTL_SHADOW_OP_CLEAN,
+                 HYPERCALL_BUFFER(to_send), ctx->save.p2m_size,
+                 NULL, 0, &stats) != ctx->save.p2m_size )
+        {
+            PERROR("Failed to retrieve logdirty bitmap");
+            rc = -1;
+            goto out;
+        }
+
+        if ( stats.dirty_count == 0 )
+            break;
+
+        rc = update_progress_string(ctx, &progress_str, x);
+        if ( rc )
+            goto out;
+
+        rc = send_some_pages(ctx, to_send, stats.dirty_count);
+        if ( rc )
+            goto out;
+    }
+
+    rc = suspend_domain(ctx);
+    if ( rc )
+        goto out;
+
+    if ( xc_shadow_control(
+             xch, ctx->domid, XEN_DOMCTL_SHADOW_OP_CLEAN,
+             HYPERCALL_BUFFER(to_send), ctx->save.p2m_size,
+             NULL, 0, &stats) != ctx->save.p2m_size )
+    {
+        PERROR("Failed to retrieve logdirty bitmap");
+        rc = -1;
+        goto out;
+    }
+
+    rc = update_progress_string(ctx, &progress_str, ctx->save.max_iterations);
+    if ( rc )
+        goto out;
+
+    bitmap_or(to_send, ctx->save.deferred_pages, ctx->save.p2m_size);
+
+    rc = send_some_pages(ctx, to_send,
+                         stats.dirty_count + ctx->save.nr_deferred_pages);
+    if ( rc )
+        goto out;
+
+    if ( ctx->save.debug )
+    {
+        struct xc_sr_record rec =
+        {
+            .type = REC_TYPE_VERIFY,
+            .length = 0,
+        };
+
+        DPRINTF("Enabling verify mode");
+
+        rc = write_record(ctx, &rec);
+        if ( rc )
+            goto out;
+
+        xc_report_progress_set(xch, "Memory verify");
+        rc = send_all_pages(ctx);
+        if ( rc )
+            goto out;
+
+        if ( xc_shadow_control(
+                 xch, ctx->domid, XEN_DOMCTL_SHADOW_OP_PEEK,
+                 HYPERCALL_BUFFER(to_send), ctx->save.p2m_size,
+                 NULL, 0, &stats) != ctx->save.p2m_size )
+        {
+            PERROR("Failed to retrieve logdirty bitmap");
+            rc = -1;
+            goto out;
+        }
+
+        DPRINTF("  Further stats: faults %u, dirty %u",
+                stats.fault_count, stats.dirty_count);
+    }
+
+  out:
+    xc_report_progress_set(xch, NULL);
+    free(progress_str);
+    xc_hypercall_buffer_free_pages(xch, to_send,
+                                   NRPAGES(bitmap_size(ctx->save.p2m_size)));
+    free(ctx->save.deferred_pages);
+    free(ctx->save.batch_pfns);
+    return rc;
+}
+
+/*
+ * Send all domain memory, pausing the domain first.  Generally used for
+ * suspend-to-file.
+ */
+static int send_domain_memory_nonlive(struct xc_sr_context *ctx)
+{
+    xc_interface *xch = ctx->xch;
+    int rc = -1;
+
+    ctx->save.batch_pfns = malloc(MAX_BATCH_SIZE *
+                                  sizeof(*ctx->save.batch_pfns));
+    ctx->save.deferred_pages = calloc(1, bitmap_size(ctx->save.p2m_size));
+
+    if ( !ctx->save.batch_pfns || !ctx->save.deferred_pages )
+    {
+        PERROR("Failed to allocate memory for nonlive tracking structures");
+        errno = ENOMEM;
+        goto err;
+    }
+
+    rc = suspend_domain(ctx);
+    if ( rc )
+        goto err;
+
+    xc_report_progress_set(xch, "Memory");
+
+    rc = send_all_pages(ctx);
+    if ( rc )
+        goto err;
+
+ err:
+    free(ctx->save.deferred_pages);
+    free(ctx->save.batch_pfns);
+
+    return rc;
+}
+
+/*
+ * Save a domain.
+ */
+static int save(struct xc_sr_context *ctx, uint16_t guest_type)
+{
+    xc_interface *xch = ctx->xch;
+    int rc, saved_rc = 0, saved_errno = 0;
+
+    IPRINTF("Saving domain %d, type %s",
+            ctx->domid, dhdr_type_to_str(guest_type));
+
+    rc = ctx->save.ops.setup(ctx);
+    if ( rc )
+        goto err;
+
+    xc_report_progress_single(xch, "Start of stream");
+
+    rc = write_headers(ctx, guest_type);
+    if ( rc )
+        goto err;
+
+    rc = ctx->save.ops.start_of_stream(ctx);
+    if ( rc )
+        goto err;
+
+    if ( ctx->save.live )
+        rc = send_domain_memory_live(ctx);
+    else
+        rc = send_domain_memory_nonlive(ctx);
+
+    if ( rc )
+        goto err;
+
+    if ( !ctx->dominfo.shutdown ||
+         (ctx->dominfo.shutdown_reason != SHUTDOWN_suspend) )
+    {
+        ERROR("Domain has not been suspended");
+        rc = -1;
+        goto err;
+    }
+
+    xc_report_progress_single(xch, "End of stream");
+
+    rc = ctx->save.ops.end_of_stream(ctx);
+    if ( rc )
+        goto err;
+
+    rc = write_end_record(ctx);
+    if ( rc )
+        goto err;
+
+    xc_report_progress_single(xch, "Complete");
+    goto done;
+
+ err:
+    saved_errno = errno;
+    saved_rc = rc;
+    PERROR("Save failed");
+
+ done:
+    xc_shadow_control(xch, ctx->domid, XEN_DOMCTL_SHADOW_OP_OFF,
+                      NULL, 0, NULL, 0, NULL);
+
+    rc = ctx->save.ops.cleanup(ctx);
+    if ( rc )
+        PERROR("Failed to clean up");
+
+    if ( saved_rc )
+    {
+        rc = saved_rc;
+        errno = saved_errno;
+    }
+
+    return rc;
+};
+
 int xc_domain_save2(xc_interface *xch, int io_fd, uint32_t dom, uint32_t max_iters,
                     uint32_t max_factor, uint32_t flags,
                     struct save_callbacks* callbacks, int hvm)
 {
+    struct xc_sr_context ctx =
+        {
+            .xch = xch,
+            .fd = io_fd,
+        };
+
+    /* GCC 4.4 (of CentOS 6.x vintage) can' t initialise anonymous unions :( */
+    ctx.save.callbacks = callbacks;
+    ctx.save.live  = !!(flags & XCFLAGS_LIVE);
+    ctx.save.debug = !!(flags & XCFLAGS_DEBUG);
+
+    /*
+     * TODO: Find some time to better tweak the live migration algorithm.
+     *
+     * These parameters are better than the legacy algorithm especially for
+     * busy guests.
+     */
+    ctx.save.max_iterations = 5;
+    ctx.save.dirty_threshold = 50;
+
     IPRINTF("In experimental %s", __func__);
-    return -1;
+    DPRINTF("fd %d, dom %u, max_iters %u, max_factor %u, flags %u, hvm %d",
+            io_fd, dom, max_iters, max_factor, flags, hvm);
+
+    if ( xc_domain_getinfo(xch, dom, 1, &ctx.dominfo) != 1 )
+    {
+        PERROR("Failed to get domain info");
+        return -1;
+    }
+
+    if ( ctx.dominfo.domid != dom )
+    {
+        ERROR("Domain %u does not exist", dom);
+        return -1;
+    }
+
+    ctx.domid = dom;
+
+    ctx.save.p2m_size = xc_domain_maximum_gpfn(xch, dom) + 1;
+    if ( ctx.save.p2m_size > ~XEN_DOMCTL_PFINFO_LTAB_MASK )
+    {
+        errno = E2BIG;
+        ERROR("Cannot save this big a guest");
+        return -1;
+    }
+
+    if ( ctx.dominfo.hvm )
+    {
+        ctx.save.ops = save_ops_x86_hvm;
+        return save(&ctx, DHDR_TYPE_X86_HVM);
+    }
+    else
+    {
+        ctx.save.ops = save_ops_x86_pv;
+        return save(&ctx, DHDR_TYPE_X86_PV);
+    }
 }
 
 /*
Signature · 1 line
1.7.10.4
#44 lore raw

PATCH v7 19/29 tools/libxc: noarch restore code

In reply to Andrew Cooper

Restore a domain from the new format. This reads and validates the domain and image header and loads the guest memory from the PAGE_DATA records, populating the p2m as it does so.

This provides the xc_domain_restore2() function as an alternative to the existing xc_domain_restore().

---
v7:
* Support population of pfns with no typedata.
v6:
* Fix error path with rc = 0.
* Fix undefined memory issue with creating the populated_pfns array.

---
 tools/libxc/saverestore/common.h  |    6 +
 tools/libxc/saverestore/restore.c |  623 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 628 insertions(+), 1 deletion(-)
tools/libxc/saverestore/common.h +6 -0
index 193dd0e..35e5011 100644
@@ -322,6 +322,12 @@ static inline int write_record(struct xc_sr_context *ctx,
     return write_split_record(ctx, rec, NULL, 0);
 }
 
+/* TODO - find a better way of hiding this.  It should be private to
+ * restore.c, but is needed by x86_pv_localise_page()
+ */
+int populate_pfns(struct xc_sr_context *ctx, unsigned count,
+                  const xen_pfn_t *original_pfns, const uint32_t *types);
+
 #endif
 /*
  * Local variables:
tools/libxc/saverestore/restore.c +622 -1

635 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#45 lore raw

PATCH v7 20/29 tools/libxl: Update datacopier to support sending data only

In reply to Andrew Cooper

From: Wen Congyang <[email protected]>

datacopier is to read some data and write it out. If we have some data to send it over network, we cannot use datacopier. Update it to support this case.

---
 tools/libxl/libxl_aoutils.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/libxl/libxl_aoutils.c +5 -3
index b10d2e1..3e0c0ae 100644
@@ -309,9 +309,11 @@ int libxl__datacopier_start(libxl__datacopier_state *dc)
 
     libxl__datacopier_init(dc);
 
-    rc = libxl__ev_fd_register(gc, &dc->toread, datacopier_readable,
-                               dc->readfd, POLLIN);
-    if (rc) goto out;
+    if (dc->readfd >= 0) {
+        rc = libxl__ev_fd_register(gc, &dc->toread, datacopier_readable,
+                                   dc->readfd, POLLIN);
+        if (rc) goto out;
+    }
 
     rc = libxl__ev_fd_register(gc, &dc->towrite, datacopier_writable,
                                dc->writefd, POLLOUT);
Signature · 1 line
1.7.10.4
#46 lore raw

Re: [PATCH 20/29] tools/libxl: Update datacopier to support sending data only

Quoted 5 lines · Andrew Cooper

From: Wen Congyang <[email protected]>

datacopier is to read some data and write it out. If we have some data to send it over network, we cannot use datacopier. Update it to support this case.

I'm afraid I'm not sure what this changelog is saying. If we have data to send over the network then why can't we use datacopier? How does making this conditional on readfd's validity help that case?

Quoted 25 lines
---
 tools/libxl/libxl_aoutils.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/libxl/libxl_aoutils.c +5 -3
index b10d2e1..3e0c0ae 100644
@@ -309,9 +309,11 @@ int libxl__datacopier_start(libxl__datacopier_state *dc)
 
     libxl__datacopier_init(dc);
 
-    rc = libxl__ev_fd_register(gc, &dc->toread, datacopier_readable,
-                               dc->readfd, POLLIN);
-    if (rc) goto out;
+    if (dc->readfd >= 0) {
+        rc = libxl__ev_fd_register(gc, &dc->toread, datacopier_readable,
+                                   dc->readfd, POLLIN);
+        if (rc) goto out;
+    }
 
     rc = libxl__ev_fd_register(gc, &dc->towrite, datacopier_writable,
                                dc->writefd, POLLOUT);
#47 lore raw

Re: [PATCH 20/29] tools/libxl: Update datacopier to support sending data only

Quoted 9 lines · Ian Campbell
Quoted 5 lines · Andrew Cooper

From: Wen Congyang <[email protected]>

datacopier is to read some data and write it out. If we have some data to send it over network, we cannot use datacopier. Update it to support this case.

I'm afraid I'm not sure what this changelog is saying. If we have data to send over the network then why can't we use datacopier? How does making this conditional on readfd's validity help that case?

datacopier was originally strictly copying data from infd to outfd, until EOF.

These patches around here in the series extend datacopier to include "please write this local buffer to outfd", "please read $N bytes from infd to this local buffer", and "please copy exactly $N bytes from infd to outfd", all as asynchronous operations.

~Andrew

#48 lore raw

Re: [PATCH 20/29] tools/libxl: Update datacopier to support sending data only

Quoted 11 lines · Andrew Cooper
Quoted 9 lines · Ian Campbell
Quoted 5 lines · Andrew Cooper

From: Wen Congyang <[email protected]>

datacopier is to read some data and write it out. If we have some data to send it over network, we cannot use datacopier. Update it to support this case.

I'm afraid I'm not sure what this changelog is saying. If we have data to send over the network then why can't we use datacopier? How does making this conditional on readfd's validity help that case?

Quoted 7 lines

datacopier was originally strictly copying data from infd to outfd, until EOF.

These patches around here in the series extend datacopier to include "please write this local buffer to outfd", "please read $N bytes from infd to this local buffer", and "please copy exactly $N bytes from infd to outfd", all as asynchronous operations.

Thanks, that all needs to be clearly explained in the commit log.

I'm going to leave it to Ian to decide if this is an acceptable repurposing of the datacopier infrastructure.

Ian.

#49 lore raw

Re: [PATCH 20/29] tools/libxl: Update datacopier to support sending data only

Quoted 18 lines · Ian Campbell
Quoted 10 lines · Andrew Cooper
Quoted 9 lines · Ian Campbell
Quoted 5 lines · Andrew Cooper

From: Wen Congyang <[email protected]>

datacopier is to read some data and write it out. If we have some data to send it over network, we cannot use datacopier. Update it to support this case.

I'm afraid I'm not sure what this changelog is saying. If we have data to send over the network then why can't we use datacopier? How does making this conditional on readfd's validity help that case?

Quoted 7 lines

datacopier was originally strictly copying data from infd to outfd, until EOF.

These patches around here in the series extend datacopier to include "please write this local buffer to outfd", "please read $N bytes from infd to this local buffer", and "please copy exactly $N bytes from infd to outfd", all as asynchronous operations.

Quoted 7 lines

Thanks, that all needs to be clearly explained in the commit log.

I'm going to leave it to Ian to decide if this is an acceptable repurposing of the datacopier infrastructure.

Ian.

Ian explicitly requested modification of datacopier in preference to adding similar infrastructure with a different name, when I talked to him in person.

~Andrew

#50 lore raw

Re: [PATCH 20/29] tools/libxl: Update datacopier to support sending data only

Quoted 24 lines · Andrew Cooper
Quoted 23 lines · Ian Campbell
Quoted 10 lines · Andrew Cooper
Quoted 9 lines · Ian Campbell
Quoted 5 lines · Andrew Cooper

From: Wen Congyang <[email protected]>

datacopier is to read some data and write it out. If we have some data to send it over network, we cannot use datacopier. Update it to support this case.

I'm afraid I'm not sure what this changelog is saying. If we have data to send over the network then why can't we use datacopier? How does making this conditional on readfd's validity help that case?

Quoted 7 lines

datacopier was originally strictly copying data from infd to outfd, until EOF.

These patches around here in the series extend datacopier to include "please write this local buffer to outfd", "please read $N bytes from infd to this local buffer", and "please copy exactly $N bytes from infd to outfd", all as asynchronous operations.

Thanks, that all needs to be clearly explained in the commit log.

I'm going to leave it to Ian to decide if this is an acceptable repurposing of the datacopier infrastructure.

Quoted 6 lines
Quoted 2 lines

Ian.

Ian explicitly requested modification of datacopier in preference to adding similar infrastructure with a different name, when I talked to him in person.

OK.

Ian.

#51 lore raw

PATCH v7 21/29 tools/libxl: Allow adding larger amounts of prefixdata to datacopier

In reply to Andrew Cooper

From: Ross Lagerwall <[email protected]>

Previously, adding more than 1000 bytes of data would cause a segfault. Now, the maximum amount of data that can be added is limited by maxsz.

---
 tools/libxl/libxl_aoutils.c |   14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
tools/libxl/libxl_aoutils.c +9 -5
index 3e0c0ae..caba637 100644
@@ -160,6 +160,8 @@ void libxl__datacopier_prefixdata(libxl__egc *egc, libxl__datacopier_state *dc,
 {
     EGC_GC;
     libxl__datacopier_buf *buf;
+    const uint8_t *ptr;
+
     /*
      * It is safe for this to be called immediately after _start, as
      * is documented in the public comment.  _start's caller must have
@@ -170,12 +172,14 @@ void libxl__datacopier_prefixdata(libxl__egc *egc, libxl__datacopier_state *dc,
 
     assert(len < dc->maxsz - dc->used);
 
-    buf = libxl__zalloc(NOGC, sizeof(*buf));
-    buf->used = len;
-    memcpy(buf->buf, data, len);
+    for (ptr = data; len; len -= buf->used, ptr += buf->used) {
+        buf = libxl__zalloc(NOGC, sizeof(*buf));
+        buf->used = len < sizeof(buf->buf) ? len : sizeof(buf->buf);
+        memcpy(buf->buf, ptr, buf->used);
 
-    dc->used += len;
-    LIBXL_TAILQ_INSERT_TAIL(&dc->bufs, buf, entry);
+        dc->used += buf->used;
+        LIBXL_TAILQ_INSERT_TAIL(&dc->bufs, buf, entry);
+    }
 }
 
 static int datacopier_pollhup_handled(libxl__egc *egc,
Signature · 1 line
1.7.10.4
#52 lore raw

Re: [PATCH 21/29] tools/libxl: Allow adding larger amounts of prefixdata to datacopier

Quoted 3 lines · Andrew Cooper

From: Ross Lagerwall <[email protected]>

Previously, adding more than 1000 bytes of data would cause a segfault.

Segfault is a symptom, not a cause, why does it happen?

Quoted 40 lines

Now, the maximum amount of data that can be added is limited by maxsz.

---
 tools/libxl/libxl_aoutils.c |   14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
tools/libxl/libxl_aoutils.c +9 -5
index 3e0c0ae..caba637 100644
@@ -160,6 +160,8 @@ void libxl__datacopier_prefixdata(libxl__egc *egc, libxl__datacopier_state *dc,
 {
     EGC_GC;
     libxl__datacopier_buf *buf;
+    const uint8_t *ptr;
+
     /*
      * It is safe for this to be called immediately after _start, as
      * is documented in the public comment.  _start's caller must have
@@ -170,12 +172,14 @@ void libxl__datacopier_prefixdata(libxl__egc *egc, libxl__datacopier_state *dc,
 
     assert(len < dc->maxsz - dc->used);
 
-    buf = libxl__zalloc(NOGC, sizeof(*buf));
-    buf->used = len;
-    memcpy(buf->buf, data, len);
+    for (ptr = data; len; len -= buf->used, ptr += buf->used) {
+        buf = libxl__zalloc(NOGC, sizeof(*buf));
+        buf->used = len < sizeof(buf->buf) ? len : sizeof(buf->buf);
+        memcpy(buf->buf, ptr, buf->used);
 
-    dc->used += len;
-    LIBXL_TAILQ_INSERT_TAIL(&dc->bufs, buf, entry);
+        dc->used += buf->used;
+        LIBXL_TAILQ_INSERT_TAIL(&dc->bufs, buf, entry);
+    }
 }
 
 static int datacopier_pollhup_handled(libxl__egc *egc,
#53 lore raw

Re: [PATCH 21/29] tools/libxl: Allow adding larger amounts of prefixdata to datacopier

Quoted 6 lines · Ian Campbell
Quoted 3 lines · Andrew Cooper

From: Ross Lagerwall <[email protected]>

Previously, adding more than 1000 bytes of data would cause a segfault.

Segfault is a symptom, not a cause, why does it happen?

struct libxl__datacopier_buf contains a fixed size 1000 byte statically allocated buffer so adding > 1000 bytes of data would cause it to overrun the buffer and overwrite other memory.

Regards

Signature · 1 line
Ross Lagerwall
#54 lore raw

Re: [PATCH 21/29] tools/libxl: Allow adding larger amounts of prefixdata to datacopier

Quoted 11 lines · Ross Lagerwall
Quoted 6 lines · Ian Campbell
Quoted 3 lines · Andrew Cooper

From: Ross Lagerwall <[email protected]>

Previously, adding more than 1000 bytes of data would cause a segfault.

Segfault is a symptom, not a cause, why does it happen?

struct libxl__datacopier_buf contains a fixed size 1000 byte statically allocated buffer so adding > 1000 bytes of data would cause it to overrun the buffer and overwrite other memory.

Yes, this should be the main point of the commit log though.

Ian.

#55 lore raw

PATCH v7 22/29 tools/libxl: Allow limiting amount copied by datacopier

In reply to Andrew Cooper

From: Ross Lagerwall <[email protected]>

Add a parameter, maxread, to limit the amount of data read from the source fd of a datacopier.

---
 tools/libxl/libxl_aoutils.c    |    9 +++++++--
 tools/libxl/libxl_bootloader.c |    2 ++
 tools/libxl/libxl_dom.c        |    1 +
 tools/libxl/libxl_internal.h   |    1 +
 4 files changed, 11 insertions(+), 2 deletions(-)
tools/libxl/libxl_aoutils.c +7 -2
index caba637..6502325 100644
@@ -145,7 +145,7 @@ static void datacopier_check_state(libxl__egc *egc, libxl__datacopier_state *dc)
                 return;
             }
         }
-    } else if (!libxl__ev_fd_isregistered(&dc->toread)) {
+    } else if (!libxl__ev_fd_isregistered(&dc->toread) || dc->maxread == 0) {
         /* we have had eof */
         datacopier_callback(egc, dc, 0, 0);
         return;
@@ -233,7 +233,8 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev,
         }
         int r = read(ev->fd,
                      buf->buf + buf->used,
-                     sizeof(buf->buf) - buf->used);
+                     (sizeof(buf->buf) - buf->used) < dc->maxread ?
+                         (sizeof(buf->buf) - buf->used) : dc->maxread);
         if (r < 0) {
             if (errno == EINTR) continue;
             if (errno == EWOULDBLOCK) break;
@@ -258,7 +259,11 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev,
         }
         buf->used += r;
         dc->used += r;
+        dc->maxread -= r;
         assert(buf->used <= sizeof(buf->buf));
+        assert(dc->maxread >= 0);
+        if (dc->maxread == 0)
+            break;
     }
     datacopier_check_state(egc, dc);
 }
tools/libxl/libxl_bootloader.c +2 -0
index 79947d4..1503101 100644
@@ -516,6 +516,7 @@ static void bootloader_gotptys(libxl__egc *egc, libxl__openpty_state *op)
 
     bl->keystrokes.ao = ao;
     bl->keystrokes.maxsz = BOOTLOADER_BUF_OUT;
+    bl->keystrokes.maxread = INT_MAX;
     bl->keystrokes.copywhat =
         GCSPRINTF("bootloader input for domain %"PRIu32, bl->domid);
     bl->keystrokes.callback =         bootloader_keystrokes_copyfail;
@@ -527,6 +528,7 @@ static void bootloader_gotptys(libxl__egc *egc, libxl__openpty_state *op)
 
     bl->display.ao = ao;
     bl->display.maxsz = BOOTLOADER_BUF_IN;
+    bl->display.maxread = INT_MAX;
     bl->display.copywhat =
         GCSPRINTF("bootloader output for domain %"PRIu32, bl->domid);
     bl->display.callback =         bootloader_display_copyfail;
tools/libxl/libxl_dom.c +1 -0
index 0dfdb08..2f74341 100644
@@ -1717,6 +1717,7 @@ void libxl__domain_save_device_model(libxl__egc *egc,
     dc->readfd = -1;
     dc->writefd = fd;
     dc->maxsz = INT_MAX;
+    dc->maxread = INT_MAX;
     dc->copywhat = GCSPRINTF("qemu save file for domain %"PRIu32, dss->domid);
     dc->writewhat = "save/migration stream";
     dc->callback = save_device_model_datacopier_done;
tools/libxl/libxl_internal.h +1 -0
index 03e9978..d93a6ee 100644
@@ -2425,6 +2425,7 @@ struct libxl__datacopier_state {
     libxl__ao *ao;
     int readfd, writefd;
     ssize_t maxsz;
+    ssize_t maxread;
     const char *copywhat, *readwhat, *writewhat; /* for error msgs */
     FILE *log; /* gets a copy of everything */
     libxl__datacopier_callback *callback;
Signature · 1 line
1.7.10.4
#56 lore raw

Re: [PATCH 22/29] tools/libxl: Allow limiting amount copied by datacopier

Quoted 4 lines · Andrew Cooper

From: Ross Lagerwall <[email protected]>

Add a parameter, maxread, to limit the amount of data read from the source fd of a datacopier.

Why is this useful?

Quoted 88 lines
---
 tools/libxl/libxl_aoutils.c    |    9 +++++++--
 tools/libxl/libxl_bootloader.c |    2 ++
 tools/libxl/libxl_dom.c        |    1 +
 tools/libxl/libxl_internal.h   |    1 +
 4 files changed, 11 insertions(+), 2 deletions(-)
tools/libxl/libxl_aoutils.c +7 -2
index caba637..6502325 100644
@@ -145,7 +145,7 @@ static void datacopier_check_state(libxl__egc *egc, libxl__datacopier_state *dc)
                 return;
             }
         }
-    } else if (!libxl__ev_fd_isregistered(&dc->toread)) {
+    } else if (!libxl__ev_fd_isregistered(&dc->toread) || dc->maxread == 0) {
         /* we have had eof */
         datacopier_callback(egc, dc, 0, 0);
         return;
@@ -233,7 +233,8 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev,
         }
         int r = read(ev->fd,
                      buf->buf + buf->used,
-                     sizeof(buf->buf) - buf->used);
+                     (sizeof(buf->buf) - buf->used) < dc->maxread ?
+                         (sizeof(buf->buf) - buf->used) : dc->maxread);
         if (r < 0) {
             if (errno == EINTR) continue;
             if (errno == EWOULDBLOCK) break;
@@ -258,7 +259,11 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev,
         }
         buf->used += r;
         dc->used += r;
+        dc->maxread -= r;
         assert(buf->used <= sizeof(buf->buf));
+        assert(dc->maxread >= 0);
+        if (dc->maxread == 0)
+            break;
     }
     datacopier_check_state(egc, dc);
 }
tools/libxl/libxl_bootloader.c +2 -0
index 79947d4..1503101 100644
@@ -516,6 +516,7 @@ static void bootloader_gotptys(libxl__egc *egc, libxl__openpty_state *op)
 
     bl->keystrokes.ao = ao;
     bl->keystrokes.maxsz = BOOTLOADER_BUF_OUT;
+    bl->keystrokes.maxread = INT_MAX;
     bl->keystrokes.copywhat =
         GCSPRINTF("bootloader input for domain %"PRIu32, bl->domid);
     bl->keystrokes.callback =         bootloader_keystrokes_copyfail;
@@ -527,6 +528,7 @@ static void bootloader_gotptys(libxl__egc *egc, libxl__openpty_state *op)
 
     bl->display.ao = ao;
     bl->display.maxsz = BOOTLOADER_BUF_IN;
+    bl->display.maxread = INT_MAX;
     bl->display.copywhat =
         GCSPRINTF("bootloader output for domain %"PRIu32, bl->domid);
     bl->display.callback =         bootloader_display_copyfail;
tools/libxl/libxl_dom.c +1 -0
index 0dfdb08..2f74341 100644
@@ -1717,6 +1717,7 @@ void libxl__domain_save_device_model(libxl__egc *egc,
     dc->readfd = -1;
     dc->writefd = fd;
     dc->maxsz = INT_MAX;
+    dc->maxread = INT_MAX;
     dc->copywhat = GCSPRINTF("qemu save file for domain %"PRIu32, dss->domid);
     dc->writewhat = "save/migration stream";
     dc->callback = save_device_model_datacopier_done;
tools/libxl/libxl_internal.h +1 -0
index 03e9978..d93a6ee 100644
@@ -2425,6 +2425,7 @@ struct libxl__datacopier_state {
     libxl__ao *ao;
     int readfd, writefd;
     ssize_t maxsz;
+    ssize_t maxread;
     const char *copywhat, *readwhat, *writewhat; /* for error msgs */
     FILE *log; /* gets a copy of everything */
     libxl__datacopier_callback *callback;
#57 lore raw

Re: [PATCH 22/29] tools/libxl: Allow limiting amount copied by datacopier

Quoted 8 lines · Ian Campbell
Quoted 4 lines · Andrew Cooper

From: Ross Lagerwall <[email protected]>

Add a parameter, maxread, to limit the amount of data read from the source fd of a datacopier.

Why is this useful?

It is useful for splicing _part_ of a stream from one fd to another rather than splicing the whole stream. i.e. it is used in this series for writing out the emulator context part of the libxl stream to a qemu save file.

Signature · 1 line
Ross Lagerwall
#58 lore raw

Re: [PATCH 22/29] tools/libxl: Allow limiting amount copied by datacopier

Quoted 7 lines · Ross Lagerwall
Quoted 6 lines · Ian Campbell
Quoted 4 lines · Andrew Cooper

From: Ross Lagerwall <[email protected]>

Add a parameter, maxread, to limit the amount of data read from the source fd of a datacopier.

Quoted 7 lines
Quoted 2 lines

Why is this useful?

It is useful for splicing _part_ of a stream from one fd to another rather than splicing the whole stream. i.e. it is used in this series for writing out the emulator context part of the libxl stream to a qemu save file.

Thanks, again this should be part of the ocmmit message.

#59 lore raw

Re: [PATCH 22/29] tools/libxl: Allow limiting amount copied by datacopier

In reply to Andrew Cooper
Quoted 37 lines · Andrew Cooper

From: Ross Lagerwall <[email protected]>

Add a parameter, maxread, to limit the amount of data read from the source fd of a datacopier.

---
 tools/libxl/libxl_aoutils.c    |    9 +++++++--
 tools/libxl/libxl_bootloader.c |    2 ++
 tools/libxl/libxl_dom.c        |    1 +
 tools/libxl/libxl_internal.h   |    1 +
 4 files changed, 11 insertions(+), 2 deletions(-)
tools/libxl/libxl_aoutils.c +3 -2
index caba637..6502325 100644
@@ -145,7 +145,7 @@ static void datacopier_check_state(libxl__egc *egc, libxl__datacopier_state *dc)
                 return;
             }
         }
-    } else if (!libxl__ev_fd_isregistered(&dc->toread)) {
+    } else if (!libxl__ev_fd_isregistered(&dc->toread) || dc->maxread == 0) {
         /* we have had eof */
         datacopier_callback(egc, dc, 0, 0);
         return;
@@ -233,7 +233,8 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev,
         }
         int r = read(ev->fd,
                      buf->buf + buf->used,
-                     sizeof(buf->buf) - buf->used);
+                     (sizeof(buf->buf) - buf->used) < dc->maxread ?
+                         (sizeof(buf->buf) - buf->used) : dc->maxread);
         if (r < 0) {
             if (errno == EINTR) continue;
             if (errno == EWOULDBLOCK) break;

tools/libxl/libxl_aoutils.c static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev, · line 266

Quoted 8 lines
tools/libxl/libxl_aoutils.c +4 -0
@@ -258,7 +259,11 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev,
         }
         buf->used += r;
         dc->used += r;
+        dc->maxread -= r;
         assert(buf->used <= sizeof(buf->buf));
+        assert(dc->maxread >= 0);
+        if (dc->maxread == 0)
+            break;

We can call libxl__ev_fd_deregister() here, and no need to touch datacopier_check_state(). Otherwise, datacopier_readable() may be called again, and read() returns 0. And then libxl__ev_fd_deregister() is called.

Thanks
Wen Congyang

Quoted 48 lines
tools/libxl/libxl_aoutils.c +0 -0
     }
     datacopier_check_state(egc, dc);
 }
tools/libxl/libxl_bootloader.c +2 -0
index 79947d4..1503101 100644
@@ -516,6 +516,7 @@ static void bootloader_gotptys(libxl__egc *egc, libxl__openpty_state *op)
 
     bl->keystrokes.ao = ao;
     bl->keystrokes.maxsz = BOOTLOADER_BUF_OUT;
+    bl->keystrokes.maxread = INT_MAX;
     bl->keystrokes.copywhat =
         GCSPRINTF("bootloader input for domain %"PRIu32, bl->domid);
     bl->keystrokes.callback =         bootloader_keystrokes_copyfail;
@@ -527,6 +528,7 @@ static void bootloader_gotptys(libxl__egc *egc, libxl__openpty_state *op)
 
     bl->display.ao = ao;
     bl->display.maxsz = BOOTLOADER_BUF_IN;
+    bl->display.maxread = INT_MAX;
     bl->display.copywhat =
         GCSPRINTF("bootloader output for domain %"PRIu32, bl->domid);
     bl->display.callback =         bootloader_display_copyfail;
tools/libxl/libxl_dom.c +1 -0
index 0dfdb08..2f74341 100644
@@ -1717,6 +1717,7 @@ void libxl__domain_save_device_model(libxl__egc *egc,
     dc->readfd = -1;
     dc->writefd = fd;
     dc->maxsz = INT_MAX;
+    dc->maxread = INT_MAX;
     dc->copywhat = GCSPRINTF("qemu save file for domain %"PRIu32, dss->domid);
     dc->writewhat = "save/migration stream";
     dc->callback = save_device_model_datacopier_done;
tools/libxl/libxl_internal.h +1 -0
index 03e9978..d93a6ee 100644
@@ -2425,6 +2425,7 @@ struct libxl__datacopier_state {
     libxl__ao *ao;
     int readfd, writefd;
     ssize_t maxsz;
+    ssize_t maxread;
     const char *copywhat, *readwhat, *writewhat; /* for error msgs */
     FILE *log; /* gets a copy of everything */
     libxl__datacopier_callback *callback;
#60 lore raw

Re: [PATCH 22/29] tools/libxl: Allow limiting amount copied by datacopier

Quoted 42 lines · Wen Congyang
Quoted 41 lines · Andrew Cooper

From: Ross Lagerwall <[email protected]>

Add a parameter, maxread, to limit the amount of data read from the source fd of a datacopier.

---
  tools/libxl/libxl_aoutils.c    |    9 +++++++--
  tools/libxl/libxl_bootloader.c |    2 ++
  tools/libxl/libxl_dom.c        |    1 +
  tools/libxl/libxl_internal.h   |    1 +
  4 files changed, 11 insertions(+), 2 deletions(-)
tools/libxl/libxl_aoutils.c +4 -2
index caba637..6502325 100644
@@ -145,7 +145,7 @@ static void datacopier_check_state(libxl__egc *egc, libxl__datacopier_state *dc)
                  return;
              }
          }
-    } else if (!libxl__ev_fd_isregistered(&dc->toread)) {
+    } else if (!libxl__ev_fd_isregistered(&dc->toread) || dc->maxread == 0) {
          /* we have had eof */
          datacopier_callback(egc, dc, 0, 0);
          return;
@@ -233,7 +233,8 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev,
          }
          int r = read(ev->fd,
                       buf->buf + buf->used,
-                     sizeof(buf->buf) - buf->used);
+                     (sizeof(buf->buf) - buf->used) < dc->maxread ?
+                         (sizeof(buf->buf) - buf->used) : dc->maxread);
          if (r < 0) {
              if (errno == EINTR) continue;
              if (errno == EWOULDBLOCK) break;
@@ -258,7 +259,11 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev,
          }
          buf->used += r;
          dc->used += r;
+        dc->maxread -= r;
Quoted 9 lines

tools/libxl/libxl_aoutils.c static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev, · line 266

Quoted 4 lines
tools/libxl/libxl_aoutils.c +3 -0
          assert(buf->used <= sizeof(buf->buf));
+        assert(dc->maxread >= 0);
+        if (dc->maxread == 0)
+            break;

We can call libxl__ev_fd_deregister() here, and no need to touch datacopier_check_state(). Otherwise, datacopier_readable() may be called again, and read() returns 0. And then libxl__ev_fd_deregister() is called.

Well datacopier_check_state() is called immediately after the break and that would deregister the fd so datacopier_readable() would not be called again. What you suggest is definitely better though.

Thanks
--
Ross Lagerwall

#61 lore raw

PATCH v7 23/29 tools/libxl: Extend datacopier to support reading into a buffer

In reply to Andrew Cooper

From: Ross Lagerwall <[email protected]>

Implement a read-only mode for libxl__datacopier. The mode is invoked when readbuf is set and writefd is < 0. On success, the callback passes the number of bytes read.

---
 tools/libxl/libxl_aoutils.c  |   59 +++++++++++++++++++++++++-----------------
 tools/libxl/libxl_internal.h |    4 ++-
 2 files changed, 38 insertions(+), 25 deletions(-)
tools/libxl/libxl_aoutils.c +35 -24
index 6502325..9183716 100644
@@ -134,7 +134,7 @@ static void datacopier_check_state(libxl__egc *egc, libxl__datacopier_state *dc)
     STATE_AO_GC(dc->ao);
     int rc;
     
-    if (dc->used) {
+    if (dc->used && !dc->readbuf) {
         if (!libxl__ev_fd_isregistered(&dc->towrite)) {
             rc = libxl__ev_fd_register(gc, &dc->towrite, datacopier_writable,
                                        dc->writefd, POLLOUT);
@@ -147,7 +147,7 @@ static void datacopier_check_state(libxl__egc *egc, libxl__datacopier_state *dc)
         }
     } else if (!libxl__ev_fd_isregistered(&dc->toread) || dc->maxread == 0) {
         /* we have had eof */
-        datacopier_callback(egc, dc, 0, 0);
+        datacopier_callback(egc, dc, 0, dc->readbuf ? dc->used : 0);
         return;
     } else {
         /* nothing buffered, but still reading */
@@ -215,26 +215,31 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev,
     }
     assert(revents & POLLIN);
     for (;;) {
-        while (dc->used >= dc->maxsz) {
-            libxl__datacopier_buf *rm = LIBXL_TAILQ_FIRST(&dc->bufs);
-            dc->used -= rm->used;
-            assert(dc->used >= 0);
-            LIBXL_TAILQ_REMOVE(&dc->bufs, rm, entry);
-            free(rm);
-        }
+        libxl__datacopier_buf *buf = NULL;
+        int r;
+
+        if (dc->readbuf) {
+            r = read(ev->fd, dc->readbuf + dc->used, dc->maxread);
+        } else {
+            while (dc->used >= dc->maxsz) {
+                libxl__datacopier_buf *rm = LIBXL_TAILQ_FIRST(&dc->bufs);
+                dc->used -= rm->used;
+                assert(dc->used >= 0);
+                LIBXL_TAILQ_REMOVE(&dc->bufs, rm, entry);
+                free(rm);
+            }
 
-        libxl__datacopier_buf *buf =
-            LIBXL_TAILQ_LAST(&dc->bufs, libxl__datacopier_bufs);
-        if (!buf || buf->used >= sizeof(buf->buf)) {
-            buf = malloc(sizeof(*buf));
-            if (!buf) libxl__alloc_failed(CTX, __func__, 1, sizeof(*buf));
-            buf->used = 0;
-            LIBXL_TAILQ_INSERT_TAIL(&dc->bufs, buf, entry);
-        }
-        int r = read(ev->fd,
-                     buf->buf + buf->used,
+            buf = LIBXL_TAILQ_LAST(&dc->bufs, libxl__datacopier_bufs);
+            if (!buf || buf->used >= sizeof(buf->buf)) {
+                buf = malloc(sizeof(*buf));
+                if (!buf) libxl__alloc_failed(CTX, __func__, 1, sizeof(*buf));
+                buf->used = 0;
+                LIBXL_TAILQ_INSERT_TAIL(&dc->bufs, buf, entry);
+            }
+            r = read(ev->fd, buf->buf + buf->used,
                      (sizeof(buf->buf) - buf->used) < dc->maxread ?
                          (sizeof(buf->buf) - buf->used) : dc->maxread);
+        }
         if (r < 0) {
             if (errno == EINTR) continue;
             if (errno == EWOULDBLOCK) break;
@@ -257,10 +262,12 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev,
                 return;
             }
         }
-        buf->used += r;
+        if (!dc->readbuf) {
+            buf->used += r;
+            assert(buf->used <= sizeof(buf->buf));
+        }
         dc->used += r;
         dc->maxread -= r;
-        assert(buf->used <= sizeof(buf->buf));
         assert(dc->maxread >= 0);
         if (dc->maxread == 0)
             break;
@@ -324,9 +331,13 @@ int libxl__datacopier_start(libxl__datacopier_state *dc)
         if (rc) goto out;
     }
 
-    rc = libxl__ev_fd_register(gc, &dc->towrite, datacopier_writable,
-                               dc->writefd, POLLOUT);
-    if (rc) goto out;
+    if (dc->writefd >= 0) {
+        rc = libxl__ev_fd_register(gc, &dc->towrite, datacopier_writable,
+                                   dc->writefd, POLLOUT);
+        if (rc) goto out;
+    }
+
+    assert(dc->readfd >= 0 || dc->writefd >= 0);
 
     return 0;
 
tools/libxl/libxl_internal.h +3 -1
index d93a6ee..056843a 100644
@@ -2404,7 +2404,8 @@ typedef struct libxl__datacopier_buf libxl__datacopier_buf;
 
 /* onwrite==1 means failure happened when writing, logged, errnoval is valid
  * onwrite==0 means failure happened when reading
- *     errnoval==0 means we got eof and all data was written
+ *     errnoval>=0 means we got eof and all data was written or number of bytes
+ *                 written when in read mode
  *     errnoval!=0 means we had a read error, logged
  * onwrite==-1 means some other internal failure, errnoval not valid, logged
  * If we get POLLHUP, we call callback_pollhup(..., onwrite, -1);
@@ -2433,6 +2434,7 @@ struct libxl__datacopier_state {
     /* remaining fields are private to datacopier */
     libxl__ev_fd toread, towrite;
     ssize_t used;
+    void *readbuf;
     LIBXL_TAILQ_HEAD(libxl__datacopier_bufs, libxl__datacopier_buf) bufs;
 };
 
Signature · 1 line
1.7.10.4
#62 lore raw

Re: [PATCH 23/29] tools/libxl: Extend datacopier to support reading into a buffer

Quoted 3 lines · Andrew Cooper

From: Ross Lagerwall <[email protected]>

Implement a read-only mode for libxl__datacopier.

Read-only in the sense that it only reads, or in the sense that one may only read from it?

What is the purpose of this mode?

Where does the data go?

Quoted 135 lines

The mode is invoked
when readbuf is set and writefd is < 0. On success, the callback passes the number of bytes read.

---
 tools/libxl/libxl_aoutils.c  |   59 +++++++++++++++++++++++++-----------------
 tools/libxl/libxl_internal.h |    4 ++-
 2 files changed, 38 insertions(+), 25 deletions(-)
tools/libxl/libxl_aoutils.c +35 -24
index 6502325..9183716 100644
@@ -134,7 +134,7 @@ static void datacopier_check_state(libxl__egc *egc, libxl__datacopier_state *dc)
     STATE_AO_GC(dc->ao);
     int rc;
 
-    if (dc->used) {
+    if (dc->used && !dc->readbuf) {
         if (!libxl__ev_fd_isregistered(&dc->towrite)) {
             rc = libxl__ev_fd_register(gc, &dc->towrite, datacopier_writable,
                                        dc->writefd, POLLOUT);
@@ -147,7 +147,7 @@ static void datacopier_check_state(libxl__egc *egc, libxl__datacopier_state *dc)
         }
     } else if (!libxl__ev_fd_isregistered(&dc->toread) || dc->maxread == 0) {
         /* we have had eof */
-        datacopier_callback(egc, dc, 0, 0);
+        datacopier_callback(egc, dc, 0, dc->readbuf ? dc->used : 0);
         return;
     } else {
         /* nothing buffered, but still reading */
@@ -215,26 +215,31 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev,
     }
     assert(revents & POLLIN);
     for (;;) {
-        while (dc->used >= dc->maxsz) {
-            libxl__datacopier_buf *rm = LIBXL_TAILQ_FIRST(&dc->bufs);
-            dc->used -= rm->used;
-            assert(dc->used >= 0);
-            LIBXL_TAILQ_REMOVE(&dc->bufs, rm, entry);
-            free(rm);
-        }
+        libxl__datacopier_buf *buf = NULL;
+        int r;
+
+        if (dc->readbuf) {
+            r = read(ev->fd, dc->readbuf + dc->used, dc->maxread);
+        } else {
+            while (dc->used >= dc->maxsz) {
+                libxl__datacopier_buf *rm = LIBXL_TAILQ_FIRST(&dc->bufs);
+                dc->used -= rm->used;
+                assert(dc->used >= 0);
+                LIBXL_TAILQ_REMOVE(&dc->bufs, rm, entry);
+                free(rm);
+            }
 
-        libxl__datacopier_buf *buf =
-            LIBXL_TAILQ_LAST(&dc->bufs, libxl__datacopier_bufs);
-        if (!buf || buf->used >= sizeof(buf->buf)) {
-            buf = malloc(sizeof(*buf));
-            if (!buf) libxl__alloc_failed(CTX, __func__, 1, sizeof(*buf));
-            buf->used = 0;
-            LIBXL_TAILQ_INSERT_TAIL(&dc->bufs, buf, entry);
-        }
-        int r = read(ev->fd,
-                     buf->buf + buf->used,
+            buf = LIBXL_TAILQ_LAST(&dc->bufs, libxl__datacopier_bufs);
+            if (!buf || buf->used >= sizeof(buf->buf)) {
+                buf = malloc(sizeof(*buf));
+                if (!buf) libxl__alloc_failed(CTX, __func__, 1, sizeof(*buf));
+                buf->used = 0;
+                LIBXL_TAILQ_INSERT_TAIL(&dc->bufs, buf, entry);
+            }
+            r = read(ev->fd, buf->buf + buf->used,
                      (sizeof(buf->buf) - buf->used) < dc->maxread ?
                          (sizeof(buf->buf) - buf->used) : dc->maxread);
+        }
         if (r < 0) {
             if (errno == EINTR) continue;
             if (errno == EWOULDBLOCK) break;
@@ -257,10 +262,12 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev,
                 return;
             }
         }
-        buf->used += r;
+        if (!dc->readbuf) {
+            buf->used += r;
+            assert(buf->used <= sizeof(buf->buf));
+        }
         dc->used += r;
         dc->maxread -= r;
-        assert(buf->used <= sizeof(buf->buf));
         assert(dc->maxread >= 0);
         if (dc->maxread == 0)
             break;
@@ -324,9 +331,13 @@ int libxl__datacopier_start(libxl__datacopier_state *dc)
         if (rc) goto out;
     }
 
-    rc = libxl__ev_fd_register(gc, &dc->towrite, datacopier_writable,
-                               dc->writefd, POLLOUT);
-    if (rc) goto out;
+    if (dc->writefd >= 0) {
+        rc = libxl__ev_fd_register(gc, &dc->towrite, datacopier_writable,
+                                   dc->writefd, POLLOUT);
+        if (rc) goto out;
+    }
+
+    assert(dc->readfd >= 0 || dc->writefd >= 0);
 
     return 0;
 
tools/libxl/libxl_internal.h +3 -1
index d93a6ee..056843a 100644
@@ -2404,7 +2404,8 @@ typedef struct libxl__datacopier_buf libxl__datacopier_buf;
 
 /* onwrite==1 means failure happened when writing, logged, errnoval is valid
  * onwrite==0 means failure happened when reading
- *     errnoval==0 means we got eof and all data was written
+ *     errnoval>=0 means we got eof and all data was written or number of bytes
+ *                 written when in read mode
  *     errnoval!=0 means we had a read error, logged
  * onwrite==-1 means some other internal failure, errnoval not valid, logged
  * If we get POLLHUP, we call callback_pollhup(..., onwrite, -1);
@@ -2433,6 +2434,7 @@ struct libxl__datacopier_state {
     /* remaining fields are private to datacopier */
     libxl__ev_fd toread, towrite;
     ssize_t used;
+    void *readbuf;
     LIBXL_TAILQ_HEAD(libxl__datacopier_bufs, libxl__datacopier_buf) bufs;
 };
 
#63 lore raw

Re: [PATCH 23/29] tools/libxl: Extend datacopier to support reading into a buffer

Quoted 7 lines · Ian Campbell
Quoted 3 lines · Andrew Cooper

From: Ross Lagerwall <[email protected]>

Implement a read-only mode for libxl__datacopier.

Read-only in the sense that it only reads, or in the sense that one may only read from it?

It only reads rather than reading and writing.

Quoted 5 lines

What is the purpose of this mode?

Where does the data go?

This mode allows data from an fd to be read into memory using libxl's async framework. The data goes into the user-allocated libxl__datacopier_state.readbuf.

Regards

Signature · 1 line
Ross Lagerwall
#64 lore raw

Re: [PATCH 23/29] tools/libxl: Extend datacopier to support reading into a buffer

Quoted 12 lines · Ross Lagerwall
Quoted 7 lines · Ian Campbell
Quoted 3 lines · Andrew Cooper

From: Ross Lagerwall <[email protected]>

Implement a read-only mode for libxl__datacopier.

Read-only in the sense that it only reads, or in the sense that one may only read from it?

It only reads rather than reading and writing.

Quoted 1 line
Quoted 8 lines
Quoted 4 lines

What is the purpose of this mode?

Where does the data go?

This mode allows data from an fd to be read into memory using libxl's async framework. The data goes into the user-allocated libxl__datacopier_state.readbuf.

OK. I think you can probably guess what I'm going to say.

In addition to the commit log I'm sure that the doc comments in the headers must needs some updating for all this new functionality (here and the surrounding patches).

Ian.

#65 lore raw

Re: [PATCH 23/29] tools/libxl: Extend datacopier to support reading into a buffer

In reply to Andrew Cooper
Quoted 102 lines · Andrew Cooper

From: Ross Lagerwall <[email protected]>

Implement a read-only mode for libxl__datacopier. The mode is invoked when readbuf is set and writefd is < 0. On success, the callback passes the number of bytes read.

---
 tools/libxl/libxl_aoutils.c  |   59 +++++++++++++++++++++++++-----------------
 tools/libxl/libxl_internal.h |    4 ++-
 2 files changed, 38 insertions(+), 25 deletions(-)
tools/libxl/libxl_aoutils.c +28 -21
index 6502325..9183716 100644
@@ -134,7 +134,7 @@ static void datacopier_check_state(libxl__egc *egc, libxl__datacopier_state *dc)
     STATE_AO_GC(dc->ao);
     int rc;
 
-    if (dc->used) {
+    if (dc->used && !dc->readbuf) {
         if (!libxl__ev_fd_isregistered(&dc->towrite)) {
             rc = libxl__ev_fd_register(gc, &dc->towrite, datacopier_writable,
                                        dc->writefd, POLLOUT);
@@ -147,7 +147,7 @@ static void datacopier_check_state(libxl__egc *egc, libxl__datacopier_state *dc)
         }
     } else if (!libxl__ev_fd_isregistered(&dc->toread) || dc->maxread == 0) {
         /* we have had eof */
-        datacopier_callback(egc, dc, 0, 0);
+        datacopier_callback(egc, dc, 0, dc->readbuf ? dc->used : 0);
         return;
     } else {
         /* nothing buffered, but still reading */
@@ -215,26 +215,31 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev,
     }
     assert(revents & POLLIN);
     for (;;) {
-        while (dc->used >= dc->maxsz) {
-            libxl__datacopier_buf *rm = LIBXL_TAILQ_FIRST(&dc->bufs);
-            dc->used -= rm->used;
-            assert(dc->used >= 0);
-            LIBXL_TAILQ_REMOVE(&dc->bufs, rm, entry);
-            free(rm);
-        }
+        libxl__datacopier_buf *buf = NULL;
+        int r;
+
+        if (dc->readbuf) {
+            r = read(ev->fd, dc->readbuf + dc->used, dc->maxread);
+        } else {
+            while (dc->used >= dc->maxsz) {
+                libxl__datacopier_buf *rm = LIBXL_TAILQ_FIRST(&dc->bufs);
+                dc->used -= rm->used;
+                assert(dc->used >= 0);
+                LIBXL_TAILQ_REMOVE(&dc->bufs, rm, entry);
+                free(rm);
+            }
 
-        libxl__datacopier_buf *buf =
-            LIBXL_TAILQ_LAST(&dc->bufs, libxl__datacopier_bufs);
-        if (!buf || buf->used >= sizeof(buf->buf)) {
-            buf = malloc(sizeof(*buf));
-            if (!buf) libxl__alloc_failed(CTX, __func__, 1, sizeof(*buf));
-            buf->used = 0;
-            LIBXL_TAILQ_INSERT_TAIL(&dc->bufs, buf, entry);
-        }
-        int r = read(ev->fd,
-                     buf->buf + buf->used,
+            buf = LIBXL_TAILQ_LAST(&dc->bufs, libxl__datacopier_bufs);
+            if (!buf || buf->used >= sizeof(buf->buf)) {
+                buf = malloc(sizeof(*buf));
+                if (!buf) libxl__alloc_failed(CTX, __func__, 1, sizeof(*buf));
+                buf->used = 0;
+                LIBXL_TAILQ_INSERT_TAIL(&dc->bufs, buf, entry);
+            }
+            r = read(ev->fd, buf->buf + buf->used,
                      (sizeof(buf->buf) - buf->used) < dc->maxread ?
                          (sizeof(buf->buf) - buf->used) : dc->maxread);
+        }
         if (r < 0) {
             if (errno == EINTR) continue;
             if (errno == EWOULDBLOCK) break;
@@ -257,10 +262,12 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev,
                 return;
             }
         }
-        buf->used += r;
+        if (!dc->readbuf) {
+            buf->used += r;
+            assert(buf->used <= sizeof(buf->buf));
+        }
         dc->used += r;
         dc->maxread -= r;
-        assert(buf->used <= sizeof(buf->buf));
         assert(dc->maxread >= 0);
         if (dc->maxread == 0)
             break;
@@ -324,9 +331,13 @@ int libxl__datacopier_start(libxl__datacopier_state *dc)
         if (rc) goto out;
     }
 

tools/libxl/libxl_aoutils.c int libxl__datacopier_start(libxl__datacopier_state *dc) · line 340

Quoted 10 lines
tools/libxl/libxl_aoutils.c +7 -3
-    rc = libxl__ev_fd_register(gc, &dc->towrite, datacopier_writable,
-                               dc->writefd, POLLOUT);
-    if (rc) goto out;
+    if (dc->writefd >= 0) {
+        rc = libxl__ev_fd_register(gc, &dc->towrite, datacopier_writable,
+                                   dc->writefd, POLLOUT);
+        if (rc) goto out;
+    }
+
+    assert(dc->readfd >= 0 || dc->writefd >= 0);

If readfd and writefd are valid, and readbuf is not NULL, what it the behavior?

Thanks
Wen Congyang

Quoted 26 lines
tools/libxl/libxl_aoutils.c +0 -0
     return 0;
 
tools/libxl/libxl_internal.h +3 -1
index d93a6ee..056843a 100644
@@ -2404,7 +2404,8 @@ typedef struct libxl__datacopier_buf libxl__datacopier_buf;
 
 /* onwrite==1 means failure happened when writing, logged, errnoval is valid
  * onwrite==0 means failure happened when reading
- *     errnoval==0 means we got eof and all data was written
+ *     errnoval>=0 means we got eof and all data was written or number of bytes
+ *                 written when in read mode
  *     errnoval!=0 means we had a read error, logged
  * onwrite==-1 means some other internal failure, errnoval not valid, logged
  * If we get POLLHUP, we call callback_pollhup(..., onwrite, -1);
@@ -2433,6 +2434,7 @@ struct libxl__datacopier_state {
     /* remaining fields are private to datacopier */
     libxl__ev_fd toread, towrite;
     ssize_t used;
+    void *readbuf;
     LIBXL_TAILQ_HEAD(libxl__datacopier_bufs, libxl__datacopier_buf) bufs;
 };
 
#66 lore raw

Re: [PATCH 23/29] tools/libxl: Extend datacopier to support reading into a buffer

Quoted 103 lines · Wen Congyang
Quoted 102 lines · Andrew Cooper

From: Ross Lagerwall <[email protected]>

Implement a read-only mode for libxl__datacopier. The mode is invoked when readbuf is set and writefd is < 0. On success, the callback passes the number of bytes read.

---
  tools/libxl/libxl_aoutils.c  |   59 +++++++++++++++++++++++++-----------------
  tools/libxl/libxl_internal.h |    4 ++-
  2 files changed, 38 insertions(+), 25 deletions(-)
tools/libxl/libxl_aoutils.c +28 -21
index 6502325..9183716 100644
@@ -134,7 +134,7 @@ static void datacopier_check_state(libxl__egc *egc, libxl__datacopier_state *dc)
      STATE_AO_GC(dc->ao);
      int rc;
 
-    if (dc->used) {
+    if (dc->used && !dc->readbuf) {
          if (!libxl__ev_fd_isregistered(&dc->towrite)) {
              rc = libxl__ev_fd_register(gc, &dc->towrite, datacopier_writable,
                                         dc->writefd, POLLOUT);
@@ -147,7 +147,7 @@ static void datacopier_check_state(libxl__egc *egc, libxl__datacopier_state *dc)
          }
      } else if (!libxl__ev_fd_isregistered(&dc->toread) || dc->maxread == 0) {
          /* we have had eof */
-        datacopier_callback(egc, dc, 0, 0);
+        datacopier_callback(egc, dc, 0, dc->readbuf ? dc->used : 0);
          return;
      } else {
          /* nothing buffered, but still reading */
@@ -215,26 +215,31 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev,
      }
      assert(revents & POLLIN);
      for (;;) {
-        while (dc->used >= dc->maxsz) {
-            libxl__datacopier_buf *rm = LIBXL_TAILQ_FIRST(&dc->bufs);
-            dc->used -= rm->used;
-            assert(dc->used >= 0);
-            LIBXL_TAILQ_REMOVE(&dc->bufs, rm, entry);
-            free(rm);
-        }
+        libxl__datacopier_buf *buf = NULL;
+        int r;
+
+        if (dc->readbuf) {
+            r = read(ev->fd, dc->readbuf + dc->used, dc->maxread);
+        } else {
+            while (dc->used >= dc->maxsz) {
+                libxl__datacopier_buf *rm = LIBXL_TAILQ_FIRST(&dc->bufs);
+                dc->used -= rm->used;
+                assert(dc->used >= 0);
+                LIBXL_TAILQ_REMOVE(&dc->bufs, rm, entry);
+                free(rm);
+            }
 
-        libxl__datacopier_buf *buf =
-            LIBXL_TAILQ_LAST(&dc->bufs, libxl__datacopier_bufs);
-        if (!buf || buf->used >= sizeof(buf->buf)) {
-            buf = malloc(sizeof(*buf));
-            if (!buf) libxl__alloc_failed(CTX, __func__, 1, sizeof(*buf));
-            buf->used = 0;
-            LIBXL_TAILQ_INSERT_TAIL(&dc->bufs, buf, entry);
-        }
-        int r = read(ev->fd,
-                     buf->buf + buf->used,
+            buf = LIBXL_TAILQ_LAST(&dc->bufs, libxl__datacopier_bufs);
+            if (!buf || buf->used >= sizeof(buf->buf)) {
+                buf = malloc(sizeof(*buf));
+                if (!buf) libxl__alloc_failed(CTX, __func__, 1, sizeof(*buf));
+                buf->used = 0;
+                LIBXL_TAILQ_INSERT_TAIL(&dc->bufs, buf, entry);
+            }
+            r = read(ev->fd, buf->buf + buf->used,
                       (sizeof(buf->buf) - buf->used) < dc->maxread ?
                           (sizeof(buf->buf) - buf->used) : dc->maxread);
+        }
          if (r < 0) {
              if (errno == EINTR) continue;
              if (errno == EWOULDBLOCK) break;
@@ -257,10 +262,12 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev,
                  return;
              }
          }
-        buf->used += r;
+        if (!dc->readbuf) {
+            buf->used += r;
+            assert(buf->used <= sizeof(buf->buf));
+        }
          dc->used += r;
          dc->maxread -= r;
-        assert(buf->used <= sizeof(buf->buf));
          assert(dc->maxread >= 0);
          if (dc->maxread == 0)
              break;
@@ -324,9 +331,13 @@ int libxl__datacopier_start(libxl__datacopier_state *dc)
          if (rc) goto out;
      }
 
Quoted 13 lines

tools/libxl/libxl_aoutils.c int libxl__datacopier_start(libxl__datacopier_state *dc) · line 340

Quoted 10 lines
tools/libxl/libxl_aoutils.c +7 -3
-    rc = libxl__ev_fd_register(gc, &dc->towrite, datacopier_writable,
-                               dc->writefd, POLLOUT);
-    if (rc) goto out;
+    if (dc->writefd >= 0) {
+        rc = libxl__ev_fd_register(gc, &dc->towrite, datacopier_writable,
+                                   dc->writefd, POLLOUT);
+        if (rc) goto out;
+    }
+
+    assert(dc->readfd >= 0 || dc->writefd >= 0);

If readfd and writefd are valid, and readbuf is not NULL, what it the behavior?

I'd say that's an invalid use of the API. There should probably be an assert to check this.

Regards

Signature · 1 line
Ross Lagerwall
#67 lore raw

PATCH v7 24/29 tools/libxl: Allow suppression of POLLHUP for datacopiers

In reply to Andrew Cooper

If the far end of a pipe has been closed, poll() will set POLLHUP. When reading from a pipe, POLLIN|POLLHUP is a valid state, even when there is still data to be read.

Currently, datacopier will bail because of POLLHUP before discovering that there is valid data to be read.

Add an option to ignore POLLHUP for consumers who would prefer to read to EOF.

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>

---

It might be easier/better to alter the POLLHUP handling, but I am struggling to work out what effect that would have on the bootloader pty handling.

---
 tools/libxl/libxl_aoutils.c  |    2 +-
 tools/libxl/libxl_internal.h |    1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
tools/libxl/libxl_aoutils.c +1 -1
index 9183716..2b39432 100644
@@ -207,7 +207,7 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev,
     if (datacopier_pollhup_handled(egc, dc, revents, 0))
         return;
 
-    if (revents & ~POLLIN) {
+    if (revents & ~(POLLIN | (dc->suppress_pollhup ? POLLHUP : 0))) {
         LOG(ERROR, "unexpected poll event 0x%x (should be POLLIN)"
             " on %s during copy of %s", revents, dc->readwhat, dc->copywhat);
         datacopier_callback(egc, dc, -1, 0);
tools/libxl/libxl_internal.h +1 -0
index 056843a..537b523 100644
@@ -2431,6 +2431,7 @@ struct libxl__datacopier_state {
     FILE *log; /* gets a copy of everything */
     libxl__datacopier_callback *callback;
     libxl__datacopier_callback *callback_pollhup;
+    int suppress_pollhup;
     /* remaining fields are private to datacopier */
     libxl__ev_fd toread, towrite;
     ssize_t used;
Signature · 1 line
1.7.10.4
#68 lore raw

Re: [PATCH 24/29] tools/libxl: Allow suppression of POLLHUP for datacopiers

Quoted 9 lines · Andrew Cooper

If the far end of a pipe has been closed, poll() will set POLLHUP. When reading from a pipe, POLLIN|POLLHUP is a valid state, even when there is still data to be read.

Currently, datacopier will bail because of POLLHUP before discovering that there is valid data to be read.

Add an option to ignore POLLHUP for consumers who would prefer to read to EOF.

Quoted 8 lines
CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>

---

It might be easier/better to alter the POLLHUP handling, but I am struggling to work out what effect that would have on the bootloader pty handling.

I was just about to ask why you weren't doing this. I'll leave the answer to Ian.

Quoted 30 lines
---
 tools/libxl/libxl_aoutils.c  |    2 +-
 tools/libxl/libxl_internal.h |    1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
tools/libxl/libxl_aoutils.c +1 -1
index 9183716..2b39432 100644
@@ -207,7 +207,7 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev,
     if (datacopier_pollhup_handled(egc, dc, revents, 0))
         return;
 
-    if (revents & ~POLLIN) {
+    if (revents & ~(POLLIN | (dc->suppress_pollhup ? POLLHUP : 0))) {
         LOG(ERROR, "unexpected poll event 0x%x (should be POLLIN)"
             " on %s during copy of %s", revents, dc->readwhat, dc->copywhat);
         datacopier_callback(egc, dc, -1, 0);
tools/libxl/libxl_internal.h +1 -0
index 056843a..537b523 100644
@@ -2431,6 +2431,7 @@ struct libxl__datacopier_state {
     FILE *log; /* gets a copy of everything */
     libxl__datacopier_callback *callback;
     libxl__datacopier_callback *callback_pollhup;
+    int suppress_pollhup;
     /* remaining fields are private to datacopier */
     libxl__ev_fd toread, towrite;
     ssize_t used;
#69 lore raw

PATCH v7 25/29 tools/libxl: Stream v2 format

In reply to Andrew Cooper

From: Ross Lagerwall <[email protected]>

TODO: Namespacing

---
 tools/libxl/libxl_saverestore.h |   47 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
 create mode 100644 tools/libxl/libxl_saverestore.h
tools/libxl/libxl_saverestore.h new file +47 -0
new file mode 100644
index 0000000..31a22d6
@@ -0,0 +1,47 @@
+#ifndef LIBXL_SAVERESTORE_H
+#define LIBXL_SAVERESTORE_H
+
+#include <stdint.h>
+
+/* Definitions for LibXenLight Domain Image Format version 2 */
+struct restore_hdr
+{
+    uint64_t ident;
+    uint32_t version;
+    uint32_t options;
+};
+
+struct restore_rec_hdr
+{
+    uint32_t type;
+    uint32_t length;
+};
+
+struct restore_emulator_hdr
+{
+    uint32_t id;
+    uint32_t index;
+};
+
+#define REC_TYPE_END                 0x00000000U
+#define REC_TYPE_DOMAIN_JSON         0x00000001U
+#define REC_TYPE_LIBXC_CONTEXT       0x00000002U
+#define REC_TYPE_XENSTORE_DATA       0x00000003U
+#define REC_TYPE_EMULATOR_CONTEXT    0x00000004U
+
+/* All records must be aligned up to an 8 octet boundary */
+#define REC_ALIGN_ORDER              3U
+
+#define ROUNDUP(_x,_w) (((unsigned long)(_x)+(1UL<<(_w))-1) & ~((1UL<<(_w))-1))
+
+#define EMULATOR_UNKNOWN             0x00000000U
+#define EMULATOR_QEMU_TRADITIONAL    0x00000001U
+#define EMULATOR_QEMU_UPSTREAM       0x00000002U
+
+#define RESTORE_STREAM_IDENT         0x4c6962786c466d74UL
+#define RESTORE_STREAM_VERSION       0x00000002U
+
+#define RESTORE_OPT_BIG_ENDIAN       (1 << 0)
+#define RESTORE_OPT_LEGACY           (1 << 1)
+
+#endif
Signature · 1 line
1.7.10.4
#70 lore raw

Re: [PATCH 25/29] tools/libxl: Stream v2 format

Quoted 1 line · Andrew Cooper

+#define ROUNDUP(_x,_w) (((unsigned long)(_x)+(1UL<<(_w))-1) & ~((1UL<<(_w))-1))

I thought we ended up with a common version of this somewhere, but I can't find it now.

tools/libxl/libxl_saverestore.h line 41

Quoted 6 lines
tools/libxl/libxl_saverestore.h new file +6 -0
+
+#define EMULATOR_UNKNOWN             0x00000000U
+#define EMULATOR_QEMU_TRADITIONAL    0x00000001U
+#define EMULATOR_QEMU_UPSTREAM       0x00000002U
+
+#define RESTORE_STREAM_IDENT         0x4c6962786c466d74UL

Does this compile without warnings for 32-bit?

Ian.

#71 lore raw

PATCH v7 26/29 tools/libxl: Implement libxl__domain_restore() for v2 streams

In reply to Andrew Cooper

TODO:
* Integrate with the json series

---
 tools/libxl/libxl_create.c   |  310 ++++++++++++++++++++++++++++++++++++++++--
 tools/libxl/libxl_dom.c      |    4 +-
 tools/libxl/libxl_internal.h |   10 +-
 3 files changed, 310 insertions(+), 14 deletions(-)
tools/libxl/libxl_create.c +302 -8
index a5e185e..9661f78 100644
@@ -19,6 +19,7 @@
 
 #include "libxl_internal.h"
 #include "libxl_arch.h"
+#include "libxl_saverestore.h"
 
 #include <xc_dom.h>
 #include <xenguest.h>
@@ -934,8 +935,6 @@ static void domcreate_bootloader_done(libxl__egc *egc,
     libxl_domain_build_info *const info = &d_config->b_info;
     const int restore_fd = dcs->restore_fd;
     libxl__domain_build_state *const state = &dcs->build_state;
-    libxl__srm_restore_autogen_callbacks *const callbacks =
-        &dcs->shs.callbacks.restore.a;
 
     if (rc) {
         domcreate_rebuild_done(egc, dcs, rc);
@@ -975,7 +974,6 @@ static void domcreate_bootloader_done(libxl__egc *egc,
         hvm = 1;
         superpages = 1;
         pae = libxl_defbool_val(info->u.hvm.pae);
-        callbacks->toolstack_restore = libxl__toolstack_restore;
         break;
     case LIBXL_DOMAIN_TYPE_PV:
         hvm = 0;
@@ -1105,12 +1103,16 @@ static void domcreate_rebuild_done(libxl__egc *egc,
         goto error_out;
     }
 
-    store_libxl_entry(gc, domid, &d_config->b_info);
+    if (dcs->rebuild_callback) {
+        dcs->rebuild_callback(dcs);
+    } else {
+        store_libxl_entry(gc, domid, &d_config->b_info);
 
-    libxl__multidev_begin(ao, &dcs->multidev);
-    dcs->multidev.callback = domcreate_launch_dm;
-    libxl__add_disks(egc, ao, domid, d_config, &dcs->multidev);
-    libxl__multidev_prepared(egc, &dcs->multidev, 0);
+        libxl__multidev_begin(ao, &dcs->multidev);
+        dcs->multidev.callback = domcreate_launch_dm;
+        libxl__add_disks(egc, ao, domid, d_config, &dcs->multidev);
+        libxl__multidev_prepared(egc, &dcs->multidev, 0);
+    }
 
     return;
 
@@ -1454,6 +1456,11 @@ static void domcreate_destruction_cb(libxl__egc *egc,
 typedef struct {
     libxl__domain_create_state dcs;
     uint32_t *domid_out;
+    libxl__datacopier_state dc;
+    libxl__datacopier_state stream_dc;
+    int expected_len;
+    struct restore_hdr hdr;
+    struct restore_rec_hdr rechdr;
 } libxl__app_domain_create_state;
 
 static void domain_create_cb(libxl__egc *egc,
@@ -1517,6 +1524,293 @@ int libxl_domain_create_restore(libxl_ctx *ctx, libxl_domain_config *d_config,
                             params->checkpointed_stream, ao_how, aop_console_how);
 }
 
+static void read_restore_rec_hdr(libxl__app_domain_create_state *cdcs);
+
+static void read_padding_cb(libxl__egc *egc, libxl__datacopier_state *dc,
+        int onwrite, int errnoval)
+{
+    libxl__app_domain_create_state *cdcs = CONTAINER_OF(dc, *cdcs, stream_dc);
+    STATE_AO_GC(cdcs->dcs.ao);
+    int ret = 0;
+
+    if (onwrite != 0 || errnoval != cdcs->expected_len) {
+        ret = ERROR_FAIL;
+        goto out;
+    }
+
+    read_restore_rec_hdr(cdcs);
+
+out:
+    if (ret)
+        libxl__ao_complete(egc, ao, ret);
+}
+
+static void restore_write_dm_cb(libxl__egc *egc,
+     libxl__datacopier_state *dc, int onwrite, int errnoval)
+{
+    libxl__app_domain_create_state *cdcs = CONTAINER_OF(dc, *cdcs, dc);
+    STATE_AO_GC(cdcs->dcs.ao);
+    int ret = 0, padding;
+    libxl__datacopier_state *stream_dc = &cdcs->stream_dc;
+    struct restore_rec_hdr *rechdr = &cdcs->rechdr;
+
+    if (onwrite || errnoval) {
+        ret = ERROR_FAIL;
+        goto out;
+    }
+
+    padding = ROUNDUP(rechdr->length - sizeof(struct restore_emulator_hdr),
+                  REC_ALIGN_ORDER) - rechdr->length - sizeof(struct restore_emulator_hdr);
+    if (padding > 0) {
+        stream_dc->readwhat = "padding";
+        stream_dc->maxread = padding;
+        cdcs->expected_len = stream_dc->maxread;
+        stream_dc->callback = read_padding_cb;
+        stream_dc->used = 0;
+        stream_dc->readbuf = libxl__malloc(gc, padding);
+        libxl__datacopier_start(stream_dc);
+    } else {
+        read_restore_rec_hdr(cdcs);
+    }
+
+out:
+    if (ret)
+        libxl__ao_complete(egc, ao, ret);
+}
+
+static void read_restore_record_complete(libxl__egc *egc,
+                                         libxl__app_domain_create_state *cdcs,
+                                         void *buf)
+{
+    STATE_AO_GC(cdcs->dcs.ao);
+    int ret = 0;
+
+    /* convenience aliases */
+    libxl_ctx *ctx = CTX;
+    libxl__domain_create_state *dcs = &cdcs->dcs;
+    struct restore_rec_hdr *rechdr = &cdcs->rechdr;
+
+    LIBXL__LOG(ctx, LIBXL__LOG_DEBUG,
+               "Record: 0x%08x, length %u", rechdr->type, rechdr->length);
+    switch (rechdr->type) {
+    case REC_TYPE_END:
+        if (rechdr->length != 0) {
+            LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
+                       "Encountered END record with non-zero length");
+            ret = ERROR_FAIL;
+        } else {
+            /* complete restore */
+            store_libxl_entry(gc, dcs->guest_domid, &dcs->guest_config->b_info);
+
+            libxl__multidev_begin(ao, &dcs->multidev);
+            dcs->multidev.callback = domcreate_launch_dm;
+            libxl__add_disks(egc, ao, dcs->guest_domid, dcs->guest_config, &dcs->multidev);
+            libxl__multidev_prepared(egc, &dcs->multidev, 0);
+        }
+        break;
+    case REC_TYPE_DOMAIN_JSON:
+        /* XXX handle domain JSON */
+        break;
+    case REC_TYPE_LIBXC_CONTEXT:
+        initiate_domain_create(egc, &cdcs->dcs);
+        break;
+    case REC_TYPE_XENSTORE_DATA:
+        ret = libxl__toolstack_restore(cdcs->dcs.guest_domid, buf,
+                                       rechdr->length, &cdcs->dcs);
+        if (!ret)
+            read_restore_rec_hdr(cdcs);
+        break;
+    case REC_TYPE_EMULATOR_CONTEXT: {
+        struct restore_emulator_hdr *emuhdr = buf;
+        char path[256];
+        libxl__datacopier_state *dc = &cdcs->dc;
+
+        if (emuhdr->id == EMULATOR_UNKNOWN) {
+            ret = ERROR_FAIL;
+            goto out;
+        }
+
+        sprintf(path, XC_DEVICE_MODEL_RESTORE_FILE".%u", cdcs->dcs.guest_domid);
+        memset(dc, 0, sizeof(*dc));
+        dc->ao = ao;
+        dc->readwhat = "save/migration stream";
+        dc->copywhat = "emulator context";
+        dc->writewhat = "qemu save file";
+        dc->readfd = cdcs->dcs.restore_fd;
+        dc->writefd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
+        if (dc->writefd == -1) {
+            ret = ERROR_FAIL;
+            goto out;
+        }
+        dc->maxsz = INT_MAX;
+        dc->maxread = rechdr->length - sizeof(*emuhdr);
+        dc->callback = restore_write_dm_cb;
+
+        ret = libxl__datacopier_start(dc);
+        if (ret)
+            goto out;
+        break;
+    }
+    default:
+        ret = ERROR_FAIL;
+        break;
+    }
+
+    free(buf);
+
+out:
+    if (ret)
+        libxl__ao_complete(egc, ao, ret);
+}
+
+static void read_restore_rec_body_cb(libxl__egc *egc,
+     libxl__datacopier_state *dc, int onwrite, int errnoval)
+{
+    libxl__app_domain_create_state *cdcs = CONTAINER_OF(dc, *cdcs, stream_dc);
+    STATE_AO_GC(cdcs->dcs.ao);
+    int ret = 0;
+
+    if (onwrite != 0 || errnoval != cdcs->expected_len) {
+        ret = ERROR_FAIL;
+        free(dc->readbuf);
+        goto out;
+    }
+
+    read_restore_record_complete(egc, cdcs, dc->readbuf);
+
+out:
+    if (ret)
+        libxl__ao_complete(egc, ao, ret);
+}
+
+static void read_restore_rec_hdr_cb(libxl__egc *egc,
+     libxl__datacopier_state *dc, int onwrite, int errnoval)
+{
+    libxl__app_domain_create_state *cdcs = CONTAINER_OF(dc, *cdcs, stream_dc);
+    STATE_AO_GC(cdcs->dcs.ao);
+    int ret = 0;
+
+    /* convenience aliases */
+    struct restore_rec_hdr *rechdr = &cdcs->rechdr;
+
+    if (onwrite != 0 || errnoval != cdcs->expected_len) {
+        ret = ERROR_FAIL;
+        goto out;
+    }
+
+    if (rechdr->length > 0) {
+        dc->readwhat = "read record body";
+        if (rechdr->type == REC_TYPE_EMULATOR_CONTEXT)
+            dc->maxread = sizeof(struct restore_rec_hdr);
+        else
+            dc->maxread = ROUNDUP(rechdr->length, REC_ALIGN_ORDER);
+        cdcs->expected_len = dc->maxread;
+        dc->callback = read_restore_rec_body_cb;
+        dc->used = 0;
+        dc->readbuf = libxl__malloc(NOGC, dc->maxread);
+        libxl__datacopier_start(dc);
+    } else {
+        read_restore_record_complete(egc, cdcs, NULL);
+    }
+
+out:
+    if (ret)
+        libxl__ao_complete(egc, ao, ret);
+}
+
+static void read_restore_rec_hdr(libxl__app_domain_create_state *cdcs)
+{
+    STATE_AO_GC(cdcs->dcs.ao);
+
+    libxl__datacopier_state *dc = &cdcs->stream_dc;
+    dc->readwhat = "read record header";
+    dc->maxread = sizeof(cdcs->rechdr);
+    cdcs->expected_len = dc->maxread;
+    dc->used = 0;
+    dc->callback = read_restore_rec_hdr_cb;
+    dc->readbuf = &cdcs->rechdr;
+    dc->suppress_pollhup = 1;
+    libxl__datacopier_start(dc);
+}
+
+static void read_restore_hdr_cb(libxl__egc *egc,
+     libxl__datacopier_state *dc, int onwrite, int errnoval)
+{
+    libxl__app_domain_create_state *cdcs = CONTAINER_OF(dc, *cdcs, stream_dc);
+    STATE_AO_GC(cdcs->dcs.ao);
+    int ret = 0;
+
+    /* convenience aliases */
+    libxl_ctx *ctx = CTX;
+    struct restore_hdr *hdr = &cdcs->hdr;
+
+    if (onwrite != 0 || errnoval != sizeof(*hdr)) {
+        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;
+    }
+
+    read_restore_rec_hdr(cdcs);
+
+out:
+    if (ret)
+        libxl__ao_complete(egc, ao, ret);
+}
+
+static void restore_rebuild_complete(libxl__domain_create_state *dcs)
+{
+    libxl__app_domain_create_state *cdcs = CONTAINER_OF(dcs, *cdcs, dcs);
+    STATE_AO_GC(cdcs->dcs.ao);
+
+    read_restore_rec_hdr(cdcs);
+}
+
+static void libxl__domain_restore(libxl__egc *egc,
+                                  libxl__app_domain_create_state *cdcs)
+{
+    libxl__datacopier_state *dc = &cdcs->stream_dc;
+
+    memset(dc, 0, sizeof(*dc));
+    dc->readwhat = "read header";
+    dc->copywhat = "";
+    dc->writewhat = "";
+    dc->ao = cdcs->dcs.ao;
+    dc->readfd = cdcs->dcs.restore_fd;
+    dc->maxread = sizeof(cdcs->hdr);
+    cdcs->expected_len = dc->maxread;
+    dc->maxsz = INT_MAX;
+    dc->used = 0;
+    dc->callback = read_restore_hdr_cb;
+    dc->writefd = -1;
+    dc->readbuf = &cdcs->hdr;
+
+    libxl__datacopier_start(dc);
+}
+
+
 /*
  * Local variables:
  * mode: C
tools/libxl/libxl_dom.c +1 -3
index 2f74341..4160695 100644
@@ -780,10 +780,8 @@ static inline char *restore_helper(libxl__gc *gc, uint32_t domid,
 }
 
 int libxl__toolstack_restore(uint32_t domid, const uint8_t *buf,
-                             uint32_t size, void *user)
+                             uint32_t size, libxl__domain_create_state *dcs)
 {
-    libxl__save_helper_state *shs = user;
-    libxl__domain_create_state *dcs = CONTAINER_OF(shs, *dcs, shs);
     STATE_AO_GC(dcs->ao);
     int i, ret;
     const uint8_t *ptr = buf;
tools/libxl/libxl_internal.h +7 -3
index 537b523..3964009 100644
@@ -991,8 +991,11 @@ _hidden int libxl__domain_rename(libxl__gc *gc, uint32_t domid,
                                  const char *old_name, const char *new_name,
                                  xs_transaction_t trans);
 
+typedef struct libxl__domain_create_state libxl__domain_create_state;
+
 _hidden int libxl__toolstack_restore(uint32_t domid, const uint8_t *buf,
-                                     uint32_t size, void *data);
+                                     uint32_t size,
+                                     libxl__domain_create_state *dcs);
 _hidden int libxl__domain_resume_device_model(libxl__gc *gc, uint32_t domid);
 
 _hidden const char *libxl__userdata_path(libxl__gc *gc, uint32_t domid,
@@ -2780,12 +2783,12 @@ _hidden int libxl__destroy_qdisk_backend(libxl__gc *gc, uint32_t domid);
 
 /*----- Domain creation -----*/
 
-typedef struct libxl__domain_create_state libxl__domain_create_state;
-
 typedef void libxl__domain_create_cb(libxl__egc *egc,
                                      libxl__domain_create_state*,
                                      int rc, uint32_t domid);
 
+typedef void libxl__domain_rebuild_cb(libxl__domain_create_state *dcs);
+
 struct libxl__domain_create_state {
     /* filled in by user */
     libxl__ao *ao;
@@ -2806,6 +2809,7 @@ struct libxl__domain_create_state {
     /* necessary if the domain creation failed and we have to destroy it */
     libxl__domain_destroy_state dds;
     libxl__multidev multidev;
+    libxl__domain_rebuild_cb *rebuild_callback;
 };
 
 /*----- Domain suspend (save) functions -----*/
Signature · 1 line
1.7.10.4
#72 lore raw

Re: [PATCH 26/29] tools/libxl: Implement libxl__domain_restore() for v2 streams

Quoted 2 lines · Andrew Cooper

TODO:
* Integrate with the json series

How much will that invalidate the patch wrt my reviewing it now?

Quoted 5 lines
---
 tools/libxl/libxl_create.c   |  310 ++++++++++++++++++++++++++++++++++++++++--

Would it be possible to move most/all of the restore code into a new file? If it doesn't involve exposing too many currently static bits then that might be a good thing to do.

#73 lore raw

Re: [PATCH 26/29] tools/libxl: Implement libxl__domain_restore() for v2 streams

Quoted 4 lines · Ian Campbell
Quoted 2 lines · Andrew Cooper

TODO:
* Integrate with the json series

How much will that invalidate the patch wrt my reviewing it now?

Hopefully not much, but it does complicate the use of d_config. Perhaps we can start with d_config being unconditional, and the json series can make it optional in the case a blob can be found in the stream.

Quoted 11 lines
Quoted 5 lines
---
 tools/libxl/libxl_create.c   |  310 ++++++++++++++++++++++++++++++++++++++++--

Would it be possible to move most/all of the restore code into a new file? If it doesn't involve exposing too many currently static bits then that might be a good thing to do.

Most code should be self contained. libxl_domain_{save,restore}.c sound like a good idea.

~Andrew

#74 lore raw

RFC PATCH v7 27/29 tools/libxl: Support restoring legacy streams

In reply to Andrew Cooper

This WorksForMe in the success case, but the error handling is certainly lacking.

Specifically, the conversion scripts output fd can't be closed until the v2 read loop has exited (cleanly or otherwise), without risking a close()/open() race silently replacing the fd behind the loops back.

However, it can't be closed when the read loop exits, as the conversion script child might still be alive, and would prefer terminating cleaning than failing with a bad FD.

Obviously, having one error handler block for the success/failure of the other side is a no-go, and would still involve a preselecting which was expected to exit first.

Does anyone have any clever ideas of how to asynchronously collect the events "the conversion script has exited", "the save helper has exited" and "the v2 read loop has finished" given the available infrastructure, to kick of a combined cleanup of all 3?

(I also need to fix the conversion script info/error logging, but that is a distinctly more minor problem.)

---
 tools/libxl/Makefile                |    1 +
 tools/libxl/libxl_convert_callout.c |  127 +++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_create.c          |   43 +++++++++---
 tools/libxl/libxl_internal.h        |   16 +++++
 tools/libxl/libxl_types.idl         |    1 +
 5 files changed, 178 insertions(+), 10 deletions(-)
 create mode 100644 tools/libxl/libxl_convert_callout.c
tools/libxl/Makefile +1 -0
index 4bee4af..d7b5036 100644
@@ -80,6 +80,7 @@ LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o libxl_pci.o \
 			libxl_internal.o libxl_utils.o libxl_uuid.o \
 			libxl_json.o libxl_aoutils.o libxl_numa.o \
 			libxl_save_callout.o _libxl_save_msgs_callout.o \
+			libxl_convert_callout.o \
 			libxl_qmp.o libxl_event.o libxl_fork.o $(LIBXL_OBJS-y)
 LIBXL_OBJS += libxl_genid.o
 LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o
tools/libxl/libxl_convert_callout.c new file +127 -0

127 lines not on this page · raw message

tools/libxl/libxl_create.c +33 -10

55 lines not on this page · raw message

tools/libxl/libxl_internal.h +16 -0
index 3964009..c56a167 100644
@@ -2497,6 +2497,19 @@ typedef struct libxl__save_helper_state {
                       * marshalling and xc callback functions */
 } libxl__save_helper_state;
 
+/*----- Legacy conversion helper -----*/
+typedef struct libxl__conversion_helper_state libxl__conversion_helper_state;
+
+struct libxl__conversion_helper_state {
+    /* public */
+    libxl__ao *ao;
+    void (*completion_callback)(libxl__egc *egc,
+                                libxl__conversion_helper_state *chs);
+    /* private */
+    int rc;
+    libxl__ev_child child;
+    libxl__carefd *child_out;
+};
 
 /*----- Domain suspend (save) state structure -----*/
 
@@ -2806,6 +2819,7 @@ struct libxl__domain_create_state {
         /* If we're not doing stubdom, we use only dmss.dm,
          * for the non-stubdom device model. */
     libxl__save_helper_state shs;
+    libxl__conversion_helper_state chs;
     /* necessary if the domain creation failed and we have to destroy it */
     libxl__domain_destroy_state dds;
     libxl__multidev multidev;
@@ -3283,6 +3297,8 @@ static inline void libxl__update_config_vtpm(libxl__gc *gc,
     libxl_uuid_copy(CTX, &dst->uuid, &src->uuid);
 }
 
+int libxl__convert_legacy_stream(libxl__egc *egc,
+                                 libxl__domain_create_state *dcs);
 #endif
 
 /*
tools/libxl/libxl_types.idl +1 -0
index f1fcbc3..fdd3781 100644
@@ -308,6 +308,7 @@ libxl_domain_create_info = Struct("domain_create_info",[
 
 libxl_domain_restore_params = Struct("domain_restore_params", [
     ("checkpointed_stream", integer),
+    ("stream_version", uint32, {'init_val': '1'}),
     ])
 
 libxl_domain_sched_params = Struct("domain_sched_params",[
Signature · 1 line
1.7.10.4
#75 lore raw

Re: [PATCH 27/29] [VERY RFC] tools/libxl: Support restoring legacy streams

Quoted 14 lines · Andrew Cooper

This WorksForMe in the success case, but the error handling is certainly lacking.

Specifically, the conversion scripts output fd can't be closed until the v2 read loop has exited (cleanly or otherwise), without risking a close()/open() race silently replacing the fd behind the loops back.

However, it can't be closed when the read loop exits, as the conversion script child might still be alive, and would prefer terminating cleaning than failing with a bad FD.

Obviously, having one error handler block for the success/failure of the other side is a no-go, and would still involve a preselecting which was expected to exit first.

Quoted 7 lines

Does anyone have any clever ideas of how to asynchronously collect the events "the conversion script has exited", "the save helper has exited" and "the v2 read loop has finished" given the available infrastructure, to kick of a combined cleanup of all 3?

(I also need to fix the conversion script info/error logging, but that is a distinctly more minor problem.)

This is probably one for Ian when he gets back, but a state machine which is cranked in response to the callbacks from the various completion events might be one way to approach this.

#76 lore raw

PATCH v7 28/29 tools/xl: Restore v2 streams using new interface

In reply to Andrew Cooper
---
 tools/libxl/xl_cmdimpl.c |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
tools/libxl/xl_cmdimpl.c +6 -1
index 26492fc..d17e333 100644
@@ -136,6 +136,8 @@ static const char *action_on_shutdown_names[] = {
 
 #define SAVEFILE_BYTEORDER_VALUE ((uint32_t)0x01020304UL)
 
+#define SAVEFILE_MANDATORY_STREAMV2 (1 << 0)
+
 struct domain_create {
     int debug;
     int daemonize;
@@ -2115,7 +2117,7 @@ static uint32_t create_domain(struct domain_create *dom_info)
                 restore_source, hdr.mandatory_flags, hdr.optional_flags,
                 hdr.optional_data_len);
 
-        badflags = hdr.mandatory_flags & ~( 0 /* none understood yet */ );
+        badflags = hdr.mandatory_flags & ~SAVEFILE_MANDATORY_STREAMV2;
         if (badflags) {
             fprintf(stderr, "Savefile has mandatory flag(s) 0x%"PRIx32" "
                     "which are not supported; need newer xl\n",
@@ -2250,6 +2252,9 @@ start:
         libxl_domain_restore_params_init(&params);
 
         params.checkpointed_stream = dom_info->checkpointed_stream;
+        params.stream_version =
+            (hdr.mandatory_flags & SAVEFILE_MANDATORY_STREAMV2) ? 2 : 1;
+
         ret = libxl_domain_create_restore(ctx, &d_config,
                                           &domid, restore_fd,
                                           &params,
Signature · 1 line
1.7.10.4
#77 lore raw

PATCH v7 29/29 tools/[lib]xl: Alter libxl_domain_suspend() to write a v2 stream

In reply to Andrew Cooper

From: Ross Lagerwall <[email protected]>

Note that for now, the xl header and device config blob at the beginning of the stream is still written out since we don't have any domain JSON yet.

---
 tools/libxl/libxl_dom.c      |  265 ++++++++++++++++++++++++++++++++++++------
 tools/libxl/libxl_internal.h |    5 +-
 tools/libxl/xl_cmdimpl.c     |    1 +
 3 files changed, 232 insertions(+), 39 deletions(-)
tools/libxl/libxl_dom.c +229 -36
index 4160695..1544378 100644
@@ -19,6 +19,7 @@
 
 #include "libxl_internal.h"
 #include "libxl_arch.h"
+#include "libxl_saverestore.h"
 
 #include <xc_dom.h>
 #include <xen/hvm/hvm_info_table.h>
@@ -1066,7 +1067,9 @@ int libxl__domain_suspend_device_model(libxl__gc *gc,
     uint32_t const domid = dss->domid;
     const char *const filename = dss->dm_savefile;
 
-    switch (libxl__device_model_version_running(gc, domid)) {
+    dss->dm_version = libxl__device_model_version_running(gc, domid);
+
+    switch (dss->dm_version) {
     case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL: {
         LOG(DEBUG, "Saving device model state to %s", filename);
         libxl__qemu_traditional_cmd(gc, domid, "save");
@@ -1410,10 +1413,9 @@ static inline char *physmap_path(libxl__gc *gc, uint32_t domid,
             domid, phys_offset, node);
 }
 
-int libxl__toolstack_save(uint32_t domid, uint8_t **buf,
-        uint32_t *len, void *dss_void)
+static int libxl__toolstack_save(libxl__domain_suspend_state *dss,
+        uint8_t **buf, uint32_t *len)
 {
-    libxl__domain_suspend_state *dss = dss_void;
     STATE_AO_GC(dss->ao);
     int i = 0;
     char *start_addr = NULL, *size = NULL, *phys_offset = NULL, *name = NULL;
@@ -1423,6 +1425,9 @@ int libxl__toolstack_save(uint32_t domid, uint8_t **buf,
     char **entries = NULL;
     struct libxl__physmap_info *pi;
 
+    /* Convenience aliases */
+    const uint32_t domid = dss->domid;
+
     entries = libxl__xs_directory(gc, 0, GCSPRINTF(
                 "/local/domain/0/device-model/%d/physmap", domid), &num);
     count = num;
@@ -1572,11 +1577,130 @@ static void remus_checkpoint_dm_saved(libxl__egc *egc,
 
 /*----- main code for suspending, in order of execution -----*/
 
+void libxl__save_write_header(libxl__egc *egc,
+                              libxl__domain_suspend_state *dss);
+
 void libxl__domain_suspend(libxl__egc *egc, libxl__domain_suspend_state *dss)
 {
     STATE_AO_GC(dss->ao);
-    int port;
+
+    libxl__save_write_header(egc, dss);
+}
+
+void libxl__save_write_end(libxl__egc *egc,
+                           libxl__domain_suspend_state *dss);
+
+static void domain_save_device_model_cb(libxl__egc *egc,
+                                        libxl__domain_suspend_state *dss,
+                                        int rc)
+{
+    STATE_AO_GC(dss->ao);
+
+    if (rc)
+        domain_suspend_done(egc, dss, rc);
+    else
+        libxl__save_write_end(egc, dss);
+}
+
+static void write_toolstack_done(libxl__egc *egc,
+     libxl__datacopier_state *dc, int onwrite, int errnoval)
+{
+    libxl__domain_suspend_state *dss = CONTAINER_OF(dc, *dss, dc);
+    STATE_AO_GC(dss->ao);
+
+    int rc = ERROR_FAIL;
+
+    /* Convenience aliases */
+    const libxl_domain_type type = dss->type;
+
+    if (onwrite || errnoval)
+        goto out;
+
+    if (type == LIBXL_DOMAIN_TYPE_HVM) {
+        rc = libxl__domain_suspend_device_model(gc, dss);
+        if (rc) goto out;
+
+        libxl__domain_save_device_model(egc, dss, domain_save_device_model_cb);
+        return;
+    }
+
+    libxl__save_write_end(egc, dss);
+
+    return;
+
+out:
+    domain_suspend_done(egc, dss, rc);
+}
+
+void libxl__xc_domain_save_done(libxl__egc *egc, void *dss_void,
+                                int rc, int retval, int errnoval)
+{
+    libxl__domain_suspend_state *dss = dss_void;
+    STATE_AO_GC(dss->ao);
+    struct restore_rec_hdr rechdr;
+    uint8_t *buf;
+    uint32_t len;
+    unsigned char pad[8] = {0};
+
+    if (rc)
+        goto out;
+
+    if (retval) {
+        LOGEV(ERROR, errnoval, "saving domain: %s",
+                         dss->guest_responded ?
+                         "domain responded to suspend request" :
+                         "domain did not respond to suspend request");
+        if ( !dss->guest_responded )
+            rc = ERROR_GUEST_TIMEDOUT;
+        else
+            rc = ERROR_FAIL;
+        goto out;
+    }
+
+    libxl__datacopier_state *dc = &dss->dc;
+    memset(dc, 0, sizeof(*dc));
+    dc->readwhat = "";
+    dc->copywhat = "toolstack data";
+    dc->writewhat = "save/migration stream";
+    dc->ao = ao;
+    dc->readfd = -1;
+    dc->writefd = dss->fd;
+    dc->maxsz = INT_MAX;
+    dc->maxread = INT_MAX;
+    dc->callback = write_toolstack_done;
+
+    rc = libxl__datacopier_start(dc);
+    if (rc) goto out;
+
+    rc = libxl__toolstack_save(dss, &buf, &len);
+    fprintf(stderr, "toolstack_save returned %d, len = %u\n", rc, len);
+    if (rc) goto out;
+
+    rechdr.type = REC_TYPE_XENSTORE_DATA;
+    rechdr.length = len;
+    libxl__datacopier_prefixdata(egc, dc, &rechdr, sizeof(rechdr));
+    libxl__datacopier_prefixdata(egc, dc, buf, len);
+    free(buf);
+
+    len = ROUNDUP(len, REC_ALIGN_ORDER) - len;
+    assert(len >= 0 && len < 8);
+    if (len > 0)
+        libxl__datacopier_prefixdata(egc, dc, pad, len);
+
+    return;
+
+out:
+    domain_suspend_done(egc, dss, rc);
+}
+
+static void write_header_done(libxl__egc *egc,
+     libxl__datacopier_state *dc, int onwrite, int errnoval)
+{
+    libxl__domain_suspend_state *dss = CONTAINER_OF(dc, *dss, dc);
+    STATE_AO_GC(dss->ao);
+
     int rc = ERROR_FAIL;
+    int port;
 
     /* Convenience aliases */
     const uint32_t domid = dss->domid;
@@ -1587,6 +1711,9 @@ void libxl__domain_suspend(libxl__egc *egc, libxl__domain_suspend_state *dss)
     libxl__srm_save_autogen_callbacks *const callbacks =
         &dss->shs.callbacks.save.a;
 
+    if (onwrite || errnoval)
+        goto out;
+
     logdirty_init(&dss->logdirty);
     libxl__xswait_init(&dss->pvcontrol);
     libxl__ev_evtchn_init(&dss->guest_evtchn);
@@ -1643,50 +1770,97 @@ void libxl__domain_suspend(libxl__egc *egc, libxl__domain_suspend_state *dss)
         callbacks->suspend = libxl__domain_suspend_callback;
 
     callbacks->switch_qemu_logdirty = libxl__domain_suspend_common_switch_qemu_logdirty;
-    dss->shs.callbacks.save.toolstack_save = libxl__toolstack_save;
 
     libxl__xc_domain_save(egc, dss);
     return;
 
+out:
+    domain_suspend_done(egc, dss, rc);
+}
+
+void libxl__save_write_header(libxl__egc *egc,
+                              libxl__domain_suspend_state *dss)
+{
+    STATE_AO_GC(dss->ao);
+    struct restore_hdr hdr;
+    struct restore_rec_hdr rechdr;
+    int rc = ERROR_FAIL;
+
+    libxl__datacopier_state *dc = &dss->dc;
+    memset(dc, 0, sizeof(*dc));
+    dc->readwhat = "";
+    dc->copywhat = "suspend header";
+    dc->writewhat = "save/migration stream";
+    dc->ao = ao;
+    dc->readfd = -1;
+    dc->writefd = dss->fd;
+    dc->maxsz = INT_MAX;
+    dc->maxread = INT_MAX;
+    dc->callback = write_header_done;
+
+    rc = libxl__datacopier_start(dc);
+    if (rc) goto out;
+
+    hdr.ident = htobe64(RESTORE_STREAM_IDENT);
+    hdr.version = htobe32(RESTORE_STREAM_VERSION);
+    hdr.options = htobe32(0x0);
+    libxl__datacopier_prefixdata(egc, dc, &hdr, sizeof(hdr));
+
+    /* XXX need to write the domain config here. */
+
+    rechdr.type = REC_TYPE_LIBXC_CONTEXT;
+    rechdr.length = 0;
+    libxl__datacopier_prefixdata(egc, dc, &rechdr, sizeof(rechdr));
+
+    return;
+
  out:
     domain_suspend_done(egc, dss, rc);
 }
 
-void libxl__xc_domain_save_done(libxl__egc *egc, void *dss_void,
-                                int rc, int retval, int errnoval)
+static void write_end_writer_done(libxl__egc *egc,
+     libxl__datacopier_state *dc, int onwrite, int errnoval)
 {
-    libxl__domain_suspend_state *dss = dss_void;
+    libxl__domain_suspend_state *dss = CONTAINER_OF(dc, *dss, dc);
     STATE_AO_GC(dss->ao);
 
-    /* Convenience aliases */
-    const libxl_domain_type type = dss->type;
+    int rc = 0;
 
-    if (rc)
-        goto out;
+    if (onwrite || errnoval)
+        rc = ERROR_FAIL;
 
-    if (retval) {
-        LOGEV(ERROR, errnoval, "saving domain: %s",
-                         dss->guest_responded ?
-                         "domain responded to suspend request" :
-                         "domain did not respond to suspend request");
-        if ( !dss->guest_responded )
-            rc = ERROR_GUEST_TIMEDOUT;
-        else
-            rc = ERROR_FAIL;
-        goto out;
-    }
+    domain_suspend_done(egc, dss, rc);
+}
 
-    if (type == LIBXL_DOMAIN_TYPE_HVM) {
-        rc = libxl__domain_suspend_device_model(gc, dss);
-        if (rc) goto out;
+void libxl__save_write_end(libxl__egc *egc,
+                           libxl__domain_suspend_state *dss)
+{
+    STATE_AO_GC(dss->ao);
+    struct restore_rec_hdr rechdr;
+    int rc = ERROR_FAIL;
 
-        libxl__domain_save_device_model(egc, dss, domain_suspend_done);
-        return;
-    }
+    libxl__datacopier_state *dc = &dss->dc;
+    memset(dc, 0, sizeof(*dc));
+    dc->readwhat = "";
+    dc->copywhat = "suspend footer";
+    dc->writewhat = "save/migration stream";
+    dc->ao = ao;
+    dc->readfd = -1;
+    dc->writefd = dss->fd;
+    dc->maxsz = INT_MAX;
+    dc->maxread = INT_MAX;
+    dc->callback = write_end_writer_done;
 
-    rc = 0;
+    rechdr.type = REC_TYPE_END;
+    rechdr.length = 0;
 
-out:
+    rc = libxl__datacopier_start(dc);
+    if (rc) goto out;
+
+    libxl__datacopier_prefixdata(egc, dc, &rechdr, sizeof(rechdr));
+    return;
+
+ out:
     domain_suspend_done(egc, dss, rc);
 }
 
@@ -1698,6 +1872,8 @@ void libxl__domain_save_device_model(libxl__egc *egc,
                                      libxl__save_device_model_cb *callback)
 {
     STATE_AO_GC(dss->ao);
+    struct restore_rec_hdr rechdr;
+    struct restore_emulator_hdr emuhdr;
     struct stat st;
     uint32_t qemu_state_len;
     int rc;
@@ -1707,8 +1883,9 @@ void libxl__domain_save_device_model(libxl__egc *egc,
     /* Convenience aliases */
     const char *const filename = dss->dm_savefile;
     const int fd = dss->fd;
+    const int dm_version = dss->dm_version;
 
-    libxl__datacopier_state *dc = &dss->save_dm_datacopier;
+    libxl__datacopier_state *dc = &dss->dc;
     memset(dc, 0, sizeof(*dc));
     dc->readwhat = GCSPRINTF("qemu save file %s", filename);
     dc->ao = ao;
@@ -1739,15 +1916,31 @@ void libxl__domain_save_device_model(libxl__egc *egc,
 
     qemu_state_len = st.st_size;
     LOG(DEBUG, "%s is %d bytes", dc->readwhat, qemu_state_len);
+    fprintf(stderr, "device model is %u\n", qemu_state_len);
 
     rc = libxl__datacopier_start(dc);
     if (rc) goto out;
 
+    rechdr.type = REC_TYPE_EMULATOR_CONTEXT;
+    rechdr.length = sizeof(emuhdr) + qemu_state_len;
     libxl__datacopier_prefixdata(egc, dc,
-                                 QEMU_SIGNATURE, strlen(QEMU_SIGNATURE));
+                                 &rechdr, sizeof(rechdr));
 
+    switch (dm_version) {
+    case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL:
+        emuhdr.id = EMULATOR_QEMU_TRADITIONAL;
+        break;
+    case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN:
+        emuhdr.id = EMULATOR_QEMU_UPSTREAM;
+        break;
+    default:
+        emuhdr.id = EMULATOR_UNKNOWN;
+        break;
+    }
+    emuhdr.index = 0;
     libxl__datacopier_prefixdata(egc, dc,
-                                 &qemu_state_len, sizeof(qemu_state_len));
+                                 &emuhdr, sizeof(emuhdr));
+
     return;
 
  out:
@@ -1758,7 +1951,7 @@ static void save_device_model_datacopier_done(libxl__egc *egc,
      libxl__datacopier_state *dc, int onwrite, int errnoval)
 {
     libxl__domain_suspend_state *dss =
-        CONTAINER_OF(dc, *dss, save_dm_datacopier);
+        CONTAINER_OF(dc, *dss, dc);
     STATE_AO_GC(dss->ao);
 
     /* Convenience aliases */
tools/libxl/libxl_internal.h +2 -3
index c56a167..10ab664 100644
@@ -2556,7 +2556,8 @@ struct libxl__domain_suspend_state {
                                  struct libxl__domain_suspend_state*, int ok);
     /* private for libxl__domain_save_device_model */
     libxl__save_device_model_cb *save_dm_callback;
-    libxl__datacopier_state save_dm_datacopier;
+    libxl__datacopier_state dc;
+    int dm_version;
 };
 
 
@@ -2851,8 +2852,6 @@ void libxl__xc_domain_saverestore_async_callback_done(libxl__egc *egc,
 
 _hidden void libxl__domain_suspend_common_switch_qemu_logdirty
                                (int domid, unsigned int enable, void *data);
-_hidden int libxl__toolstack_save(uint32_t domid, uint8_t **buf,
-        uint32_t *len, void *data);
 
 
 /* calls libxl__xc_domain_restore_done when done */
tools/libxl/xl_cmdimpl.c +1 -0
index d17e333..3193352 100644
@@ -3414,6 +3414,7 @@ static void save_domain_core_writeconfig(int fd, const char *source,
     memset(&hdr, 0, sizeof(hdr));
     memcpy(hdr.magic, savefileheader_magic, sizeof(hdr.magic));
     hdr.byteorder = SAVEFILE_BYTEORDER_VALUE;
+    hdr.mandatory_flags = SAVEFILE_MANDATORY_STREAMV2;
 
     optdata_begin= 0;
 
Signature · 1 line
1.7.10.4
#78 lore raw
In reply to Andrew Cooper
Quoted 1 line · Andrew Cooper

Patches 1 to 5 are misc bugfixes/improvements

I just pushed:
89f5ca3 libxc/bitops: Add or() to the available bitmap operations 220c8fe tools/[lib]xl: Correct use of init/dispose for libxl_domain_re 5f7766c tools/libxl: Fix stray blank line from debug logging

The other two had comments.