Skip to contentSkip to conversation

COarse-grain LOck-stepping Virtual Machines for Non-stop Service

RFC PATCH Superseded Wen Congyang 7 participants 64 messages Aug 8, 2014 – Aug 29, 2014

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover COarse-grain LOck-stepping Virtual Machines for Non-stop Service

Virtual machine (VM) replication is a well known technique for providing application-agnostic software-implemented hardware fault tolerance - "non-stop service". Currently, remus provides this function, but it buffers all output packets, and the latency is unacceptable.

In xen summit 2012, We introduce a new VM replication solution: colo (COarse-grain LOck-stepping virtual machine). The presentation is in the following URL:
http://www.slideshare.net/xen_com_mgr/colo-coarsegrain-lockstepping-virtual-machines-for-nonstop-service

Here is the summary of the solution:

Quoted 1 line

From the client's point of view, as long as the client observes identical

responses from the primary and secondary VMs, according to the service semantics, then the secondary vm is a valid replica of the primary vm, and can successfully take over when a hardware failure of the primary vm is detected.

This patchset is RFC, and implements the framework and disk replication of COLO:
1. Both primary vm and secondary vm are running
2. do checkoint
3. disk replication(use blktap2)

This patchset is based on remus-v18, and use migration v1. Only supports hvm guest now.

TODO list:
1. Use migration v2 to implement COLO
3. nic replication
4. support pvm

Patch 1-3 : bugfix
Patch 4-11 : update some APIs which will be used by colo
Patch 12-15: temporarily update remus to reuse remus device codes Patch 16-23: COLO framework related codes
Patch 24 : Hack patch, just for test
Patch 25-34: bugfix for blktap2
Patch 35-38: move some block-remus's codes to block-replication.c. These codes will
be reused by COLO.
Patch 39 : implement block-colo
Patch 40-43: update libxl to support blktap2
Patch 44 : implement disk replication
Patch 45 : hypervisor bugfix. We find this bug before rebasing colo to newest xen.
But we don't trigger this bug now.
Patch 46 : A patch for qemu-xen

Changlog from v1 to v2:
1. rebase to newest remus
2. add disk replication support

Hong Tao (1):
copy the correct page to memory

Lai Jiangshan (1):
colo: dynamic allocate aio_requests to avoid -EBUSY error

Wen Congyang (43):
csum the correct page
don't zero out ioreq page
Refactor domain_suspend_callback_common()
Update libxl__domain_resume() for colo
Update libxl__domain_suspend_common_switch_qemu_logdirty() for colo Introduce a new internal API libxl__domain_unpause() Update libxl__domain_unpause() to support qemu-xen support to resume uncooperative HVM guests
update datecopier to support sending data only
introduce a new API to aync read data from fd
move remus related codes to libxl_remus.c
rename remus device to checkpoint device
adjust the indentation
don't touch remus in checkpoint_device
Update libxl_save_msgs_gen.pl to support return data from xl to xc Allow slave sends data to master
secondary vm suspend/resume/checkpoint code
primary vm suspend/get_dirty_pfn/resume/checkpoint code
xc_domain_save: flush cache before calling callbacks->postcopy() in
colo mode
COLO: xc related codes
send store mfn and console mfn to xl before resuming secondary vm implement the cmdline for COLO
HACK: do checkpoint per 20ms
fix memory leak in block-remus
pass uuid to the callback td_open
return the correct dev path
blktap2: use correct way to get remus_image
don't call client_flush() when switching to unprotected mode
remus: fix bug in tdremus_close()
blktap2: use correct way to get free event id
blktap2: don't return negative event id
blktap2: use correct way to define array.
blktap2: connect to backup asynchronously
switch to unprotected mode before closing
blktap2: move async connect related codes to block-replication.c
blktap2: move ramdisk related codes to block-replication.c
block-colo: implement colo disk replication
pass correct file to qemu if we use blktap2
support blktap remus in xl
support blktap colo in xl:
update libxl__device_disk_from_xs_be() to support blktap device libxl/colo: setup and control disk replication for blktap2 backends x86/hvm: Always set pending event injection when loading VMC[BS]
state.

 docs/man/xl.pod.1                                  |   11 +-
 tools/blktap2/drivers/Makefile                     |    5 +-
 tools/blktap2/drivers/block-aio.c                  |   41 +-
 tools/blktap2/drivers/block-cache.c                |    4 +-
 tools/blktap2/drivers/block-colo.c                 | 1151 ++++++++++++++++++
 tools/blktap2/drivers/block-log.c                  |    4 +-
 tools/blktap2/drivers/block-qcow.c                 |    5 +-
 tools/blktap2/drivers/block-ram.c                  |    5 +-
 tools/blktap2/drivers/block-remus.c                | 1266 +++++---------------
 tools/blktap2/drivers/block-replication.c          | 1116 +++++++++++++++++
 tools/blktap2/drivers/block-replication.h          |  217 ++++
 tools/blktap2/drivers/block-vhd.c                  |    5 +-
 tools/blktap2/drivers/scheduler.c                  |   33 +-
 tools/blktap2/drivers/tapdisk-control.c            |   17 +-
 tools/blktap2/drivers/tapdisk-disktype.c           |   21 +-
 tools/blktap2/drivers/tapdisk-disktype.h           |    3 +-
 tools/blktap2/drivers/tapdisk-interface.c          |   21 +-
 tools/blktap2/drivers/tapdisk-interface.h          |    1 +
 tools/blktap2/drivers/tapdisk-vbd.c                |    9 +
 tools/blktap2/drivers/tapdisk-vbd.h                |    1 +
 tools/blktap2/drivers/tapdisk.h                    |    3 +-
 tools/libxc/xc_domain_restore.c                    |   74 +-
 tools/libxc/xc_domain_save.c                       |   66 +-
 tools/libxc/xc_resume.c                            |   20 +-
 tools/libxc/xenguest.h                             |   40 +
 tools/libxl/Makefile                               |    5 +-
 tools/libxl/libxl.c                                |  148 ++-
 tools/libxl/libxl.h                                |    3 +-
 tools/libxl/libxl_aoutils.c                        |   81 +-
 tools/libxl/libxl_blktap2.c                        |   35 +
 ...xl_remus_device.c => libxl_checkpoint_device.c} |  221 ++--
 tools/libxl/libxl_colo.h                           |   48 +
 tools/libxl/libxl_colo_restore.c                   |  878 ++++++++++++++
 tools/libxl/libxl_colo_save.c                      |  628 ++++++++++
 tools/libxl/libxl_colo_save_disk_blktap2.c         |  216 ++++
 tools/libxl/libxl_create.c                         |  138 ++-
 tools/libxl/libxl_device.c                         |    6 +-
 tools/libxl/libxl_dm.c                             |   20 +-
 tools/libxl/libxl_dom.c                            |  565 ++++-----
 tools/libxl/libxl_internal.h                       |  309 +++--
 tools/libxl/libxl_netbuffer.c                      |  127 +-
 tools/libxl/libxl_noblktap2.c                      |   35 +
 tools/libxl/libxl_nonetbuffer.c                    |   14 +-
 tools/libxl/libxl_qmp.c                            |   10 +
 tools/libxl/libxl_remus.c                          |  335 ++++++
 tools/libxl/libxl_remus.h                          |   27 +
 tools/libxl/libxl_remus_disk_drbd.c                |   67 +-
 tools/libxl/libxl_save_callout.c                   |   37 +-
 tools/libxl/libxl_save_helper.c                    |   17 +
 tools/libxl/libxl_save_msgs_gen.pl                 |   74 +-
 tools/libxl/libxl_types.idl                        |   14 +-
 tools/libxl/libxl_utils.c                          |   23 +
 tools/libxl/libxl_utils.h                          |    1 +
 tools/libxl/libxlu_disk_l.l                        |    2 +
 tools/libxl/xl_cmdimpl.c                           |   54 +-
 tools/libxl/xl_cmdtable.c                          |    3 +-
 xen/arch/x86/hvm/svm/svm.c                         |   16 +-
 xen/arch/x86/hvm/vmx/vmx.c                         |   25 +-
 58 files changed, 6558 insertions(+), 1763 deletions(-)
 create mode 100644 tools/blktap2/drivers/block-colo.c
 create mode 100644 tools/blktap2/drivers/block-replication.c
 create mode 100644 tools/blktap2/drivers/block-replication.h
 rename tools/libxl/{libxl_remus_device.c => libxl_checkpoint_device.c} (41%)
 create mode 100644 tools/libxl/libxl_colo.h
 create mode 100644 tools/libxl/libxl_colo_restore.c
 create mode 100644 tools/libxl/libxl_colo_save.c
 create mode 100644 tools/libxl/libxl_colo_save_disk_blktap2.c
 create mode 100644 tools/libxl/libxl_remus.c
 create mode 100644 tools/libxl/libxl_remus.h
Signature · 1 line
1.9.3
#1 lore raw

RFC PATCH v2 1/45 copy the correct page to memory

From: Hong Tao <[email protected]>

apply_batch() only handles MAX_BATCH_SIZE pages at one time. If there is some bogus/unmapped/allocate-only/broken page, we will skip it. So when we call apply_batch() again, the first page's index is curbatch - invalid_pages. invalid_pages stores the number of bogus/unmapped/allocate-only/broken pages we have found.

In many cases, invalid_pages is 0, so we don't catch this error.

---
 tools/libxc/xc_domain_restore.c | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)
tools/libxc/xc_domain_restore.c +19 -5
index e73e0a2..6c346f9 100644
@@ -1106,7 +1106,7 @@ static int pagebuf_get(xc_interface *xch, struct restore_ctx *ctx,
 static int apply_batch(xc_interface *xch, uint32_t dom, struct restore_ctx *ctx,
                        xen_pfn_t* region_mfn, unsigned long* pfn_type, int pae_extended_cr3,
                        struct xc_mmu* mmu,
-                       pagebuf_t* pagebuf, int curbatch)
+                       pagebuf_t* pagebuf, int curbatch, int *invalid_pages)
 {
     int i, j, curpage, nr_mfns;
     int k, scount;
@@ -1121,6 +1121,12 @@ static int apply_batch(xc_interface *xch, uint32_t dom, struct restore_ctx *ctx,
     struct domain_info_context *dinfo = &ctx->dinfo;
     int* pfn_err = NULL;
     int rc = -1;
+    int local_invalid_pages = 0;
+    /* We have handled curbatch pages before this batch, and there are
+     * *invalid_pages pages that are not in pagebuf->pages. So the first
+     * page for this page is (curbatch - *invalid_pages) page.
+     */
+    int first_page = curbatch - *invalid_pages;
 
     unsigned long mfn, pfn, pagetype;
 
@@ -1293,10 +1299,13 @@ static int apply_batch(xc_interface *xch, uint32_t dom, struct restore_ctx *ctx,
         pfn      = pagebuf->pfn_types[i + curbatch] & ~XEN_DOMCTL_PFINFO_LTAB_MASK;
         pagetype = pagebuf->pfn_types[i + curbatch] &  XEN_DOMCTL_PFINFO_LTAB_MASK;
 
-        if ( pagetype == XEN_DOMCTL_PFINFO_XTAB 
+        if ( pagetype == XEN_DOMCTL_PFINFO_XTAB
              || pagetype == XEN_DOMCTL_PFINFO_XALLOC)
+        {
+            local_invalid_pages++;
             /* a bogus/unmapped/allocate-only page: skip it */
             continue;
+        }
 
         if ( pagetype == XEN_DOMCTL_PFINFO_BROKEN )
         {
@@ -1306,6 +1315,8 @@ static int apply_batch(xc_interface *xch, uint32_t dom, struct restore_ctx *ctx,
                       "dom=%d, pfn=%lx\n", dom, pfn);
                 goto err_mapped;
             }
+
+            local_invalid_pages++;
             continue;
         }
 
@@ -1344,7 +1355,7 @@ static int apply_batch(xc_interface *xch, uint32_t dom, struct restore_ctx *ctx,
             }
         }
         else
-            memcpy(page, pagebuf->pages + (curpage + curbatch) * PAGE_SIZE,
+            memcpy(page, pagebuf->pages + (first_page + curpage) * PAGE_SIZE,
                    PAGE_SIZE);
 
         pagetype &= XEN_DOMCTL_PFINFO_LTABTYPE_MASK;
@@ -1418,6 +1429,7 @@ static int apply_batch(xc_interface *xch, uint32_t dom, struct restore_ctx *ctx,
     } /* end of 'batch' for loop */
 
     rc = nraces;
+    *invalid_pages += local_invalid_pages;
 
   err_mapped:
     munmap(region_base, j*PAGE_SIZE);
@@ -1621,7 +1633,7 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
  loadpages:
     for ( ; ; )
     {
-        int j, curbatch;
+        int j, curbatch, invalid_pages;
 
         xc_report_progress_step(xch, n, dinfo->p2m_size);
 
@@ -1665,11 +1677,13 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
 
         /* break pagebuf into batches */
         curbatch = 0;
+        invalid_pages = 0;
         while ( curbatch < j ) {
             int brc;
 
             brc = apply_batch(xch, dom, ctx, region_mfn, pfn_type,
-                              pae_extended_cr3, mmu, &pagebuf, curbatch);
+                              pae_extended_cr3, mmu, &pagebuf, curbatch,
+                              &invalid_pages);
             if ( brc < 0 )
                 goto out;
 
Signature · 1 line
1.9.3
#2 lore raw

RFC PATCH v2 2/45 csum the correct page

In reply to Wen Congyang

In verify mode, we map the guest memory, and the guest page is region_base + i * PAGE_SIZE. So we should csum page (region_base
+ i * PAGE_SIZE), not (region_base + (i+curbatch) * PAGE_SIZE)

---
 tools/libxc/xc_domain_restore.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/libxc/xc_domain_restore.c +1 -1
index 6c346f9..42abb22 100644
@@ -1405,7 +1405,7 @@ static int apply_batch(xc_interface *xch, uint32_t dom, struct restore_ctx *ctx,
 
                 DPRINTF("************** pfn=%lx type=%lx gotcs=%08lx "
                         "actualcs=%08lx\n", pfn, pagebuf->pfn_types[pfn],
-                        csum_page(region_base + (i + curbatch)*PAGE_SIZE),
+                        csum_page(region_base + i * PAGE_SIZE),
                         csum_page(buf));
 
                 for ( v = 0; v < 4; v++ )
Signature · 1 line
1.9.3
#3 lore raw

RFC PATCH v2 3/45 don't zero out ioreq page

In reply to Wen Congyang

ioreq page may contain some pending I/O requests, and we need to handle the pending I/O req after migration.

TODO:
1. update qemu to handle the pending I/O req

---
 tools/libxc/xc_domain_restore.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
tools/libxc/xc_domain_restore.c +1 -3
index 42abb22..2d6139c 100644
@@ -2301,9 +2301,7 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
     }
 
     /* These comms pages need to be zeroed at the start of day */
-    if ( xc_clear_domain_page(xch, dom, tailbuf.u.hvm.magicpfns[0]) ||
-         xc_clear_domain_page(xch, dom, tailbuf.u.hvm.magicpfns[1]) ||
-         xc_clear_domain_page(xch, dom, tailbuf.u.hvm.magicpfns[2]) )
+    if ( xc_clear_domain_page(xch, dom, tailbuf.u.hvm.magicpfns[2]) )
     {
         PERROR("error zeroing magic pages");
         goto out;
Signature · 1 line
1.9.3
#4 lore raw

RFC PATCH v2 4/45 Refactor domain_suspend_callback_common()

In reply to Wen Congyang

libxl__domain_suspend() is to save the guest. I think we should call it libxl__domain_save(), but I don't rename it.

Secondary vm is running in colo mode. So we will do the following things again and again:
1. suspend both primay vm and secondary vm
2. sync the state
3. resume both primary vm and secondary vm
To suspend secondary vm, we need an independent API to suspend vm.

The core function to suspend vm is domain_suspend_callback_common(). So use a new structure libxl__domain_suspend_state2 to instead of libxl__domain_suspend_state. The dss's members that will be used in domain_suspend_callback_common() are moved to dss2.

We introduce a new API libxl__domain_suspend2() too.

---
 tools/libxl/libxl_dom.c      | 235 ++++++++++++++++++++++++-------------------
 tools/libxl/libxl_internal.h |  39 +++++--
 2 files changed, 159 insertions(+), 115 deletions(-)
tools/libxl/libxl_dom.c +130 -105
index 302062a..1607930 100644
@@ -842,7 +842,7 @@ int libxl__toolstack_restore(uint32_t domid, const uint8_t *buf,
 static void domain_suspend_done(libxl__egc *egc,
                         libxl__domain_suspend_state *dss, int rc);
 static void domain_suspend_callback_common_done(libxl__egc *egc,
-                                libxl__domain_suspend_state *dss, int ok);
+                                libxl__domain_suspend_state2 *dss2, int ok);
 
 /*----- complicated callback, called by xc_domain_save -----*/
 
@@ -1060,16 +1060,17 @@ static void switch_logdirty_done(libxl__egc *egc,
 /*----- callbacks, called by xc_domain_save -----*/
 
 int libxl__domain_suspend_device_model(libxl__gc *gc,
-                                       libxl__domain_suspend_state *dss)
+                                       libxl__domain_suspend_state2 *dss2)
 {
     int ret = 0;
-    uint32_t const domid = dss->domid;
-    const char *const filename = dss->dm_savefile;
+    uint32_t const domid = dss2->domid;
+    const char *const filename = dss2->dm_savefile;
 
     switch (libxl__device_model_version_running(gc, domid)) {
     case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL: {
         LOG(DEBUG, "Saving device model state to %s", filename);
-        libxl__qemu_traditional_cmd(gc, domid, "save");
+        if (dss2->save_dm)
+            libxl__qemu_traditional_cmd(gc, domid, "save");
         libxl__wait_for_device_model_deprecated(gc, domid, "paused", NULL, NULL, NULL);
         break;
     }
@@ -1077,9 +1078,11 @@ int libxl__domain_suspend_device_model(libxl__gc *gc,
         if (libxl__qmp_stop(gc, domid))
             return ERROR_FAIL;
         /* Save DM state into filename */
-        ret = libxl__qmp_save(gc, domid, filename);
-        if (ret)
-            unlink(filename);
+        if (dss2->save_dm) {
+            ret = libxl__qmp_save(gc, domid, filename);
+            if (ret)
+                unlink(filename);
+        }
         break;
     default:
         return ERROR_INVAL;
@@ -1109,9 +1112,9 @@ int libxl__domain_resume_device_model(libxl__gc *gc, uint32_t domid)
 }
 
 static void domain_suspend_common_wait_guest(libxl__egc *egc,
-                                             libxl__domain_suspend_state *dss);
+                                             libxl__domain_suspend_state2 *dss2);
 static void domain_suspend_common_guest_suspended(libxl__egc *egc,
-                                         libxl__domain_suspend_state *dss);
+                                         libxl__domain_suspend_state2 *dss2);
 
 static void domain_suspend_common_pvcontrol_suspending(libxl__egc *egc,
       libxl__xswait_state *xswa, int rc, const char *state);
@@ -1120,14 +1123,14 @@ static void domain_suspend_common_wait_guest_evtchn(libxl__egc *egc,
 static void suspend_common_wait_guest_watch(libxl__egc *egc,
       libxl__ev_xswatch *xsw, const char *watch_path, const char *event_path);
 static void suspend_common_wait_guest_check(libxl__egc *egc,
-        libxl__domain_suspend_state *dss);
+                                            libxl__domain_suspend_state2 *dss2);
 static void suspend_common_wait_guest_timeout(libxl__egc *egc,
       libxl__ev_time *ev, const struct timeval *requested_abs);
 
 static void domain_suspend_common_failed(libxl__egc *egc,
-                                         libxl__domain_suspend_state *dss);
+                                         libxl__domain_suspend_state2 *dss2);
 static void domain_suspend_common_done(libxl__egc *egc,
-                                       libxl__domain_suspend_state *dss,
+                                       libxl__domain_suspend_state2 *dss2,
                                        bool ok);
 
 static bool domain_suspend_pvcontrol_acked(const char *state) {
@@ -1136,36 +1139,36 @@ static bool domain_suspend_pvcontrol_acked(const char *state) {
     return strcmp(state,"suspend");
 }
 
-/* calls dss->callback_common_done when done */
+/* calls dss2->callback_common_done when done */
 static void domain_suspend_callback_common(libxl__egc *egc,
-                                           libxl__domain_suspend_state *dss)
+                                           libxl__domain_suspend_state2 *dss2)
 {
-    STATE_AO_GC(dss->ao);
+    STATE_AO_GC(dss2->ao);
     uint64_t hvm_s_state = 0, hvm_pvdrv = 0;
     int ret, rc;
 
     /* Convenience aliases */
-    const uint32_t domid = dss->domid;
+    const uint32_t domid = dss2->domid;
 
-    if (dss->hvm) {
+    if (dss2->hvm) {
         xc_hvm_param_get(CTX->xch, domid, HVM_PARAM_CALLBACK_IRQ, &hvm_pvdrv);
         xc_hvm_param_get(CTX->xch, domid, HVM_PARAM_ACPI_S_STATE, &hvm_s_state);
     }
 
-    if ((hvm_s_state == 0) && (dss->guest_evtchn.port >= 0)) {
+    if ((hvm_s_state == 0) && (dss2->guest_evtchn.port >= 0)) {
         LOG(DEBUG, "issuing %s suspend request via event channel",
-            dss->hvm ? "PVHVM" : "PV");
-        ret = xc_evtchn_notify(CTX->xce, dss->guest_evtchn.port);
+            dss2->hvm ? "PVHVM" : "PV");
+        ret = xc_evtchn_notify(CTX->xce, dss2->guest_evtchn.port);
         if (ret < 0) {
             LOG(ERROR, "xc_evtchn_notify failed ret=%d", ret);
             goto err;
         }
 
-        dss->guest_evtchn.callback = domain_suspend_common_wait_guest_evtchn;
-        rc = libxl__ev_evtchn_wait(gc, &dss->guest_evtchn);
+        dss2->guest_evtchn.callback = domain_suspend_common_wait_guest_evtchn;
+        rc = libxl__ev_evtchn_wait(gc, &dss2->guest_evtchn);
         if (rc) goto err;
 
-        rc = libxl__ev_time_register_rel(gc, &dss->guest_timeout,
+        rc = libxl__ev_time_register_rel(gc, &dss2->guest_timeout,
                                          suspend_common_wait_guest_timeout,
                                          60*1000);
         if (rc) goto err;
@@ -1173,7 +1176,7 @@ static void domain_suspend_callback_common(libxl__egc *egc,
         return;
     }
 
-    if (dss->hvm && (!hvm_pvdrv || hvm_s_state)) {
+    if (dss2->hvm && (!hvm_pvdrv || hvm_s_state)) {
         LOG(DEBUG, "Calling xc_domain_shutdown on HVM domain");
         ret = xc_domain_shutdown(CTX->xch, domid, SHUTDOWN_suspend);
         if (ret < 0) {
@@ -1181,55 +1184,55 @@ static void domain_suspend_callback_common(libxl__egc *egc,
             goto err;
         }
         /* The guest does not (need to) respond to this sort of request. */
-        dss->guest_responded = 1;
-        domain_suspend_common_wait_guest(egc, dss);
+        dss2->guest_responded = 1;
+        domain_suspend_common_wait_guest(egc, dss2);
         return;
     }
 
     LOG(DEBUG, "issuing %s suspend request via XenBus control node",
-        dss->hvm ? "PVHVM" : "PV");
+        dss2->hvm ? "PVHVM" : "PV");
 
     libxl__domain_pvcontrol_write(gc, XBT_NULL, domid, "suspend");
 
-    dss->pvcontrol.path = libxl__domain_pvcontrol_xspath(gc, domid);
-    if (!dss->pvcontrol.path) goto err;
+    dss2->pvcontrol.path = libxl__domain_pvcontrol_xspath(gc, domid);
+    if (!dss2->pvcontrol.path) goto err;
 
-    dss->pvcontrol.ao = ao;
-    dss->pvcontrol.what = "guest acknowledgement of suspend request";
-    dss->pvcontrol.timeout_ms = 60 * 1000;
-    dss->pvcontrol.callback = domain_suspend_common_pvcontrol_suspending;
-    libxl__xswait_start(gc, &dss->pvcontrol);
+    dss2->pvcontrol.ao = ao;
+    dss2->pvcontrol.what = "guest acknowledgement of suspend request";
+    dss2->pvcontrol.timeout_ms = 60 * 1000;
+    dss2->pvcontrol.callback = domain_suspend_common_pvcontrol_suspending;
+    libxl__xswait_start(gc, &dss2->pvcontrol);
     return;
 
  err:
-    domain_suspend_common_failed(egc, dss);
+    domain_suspend_common_failed(egc, dss2);
 }
 
 static void domain_suspend_common_wait_guest_evtchn(libxl__egc *egc,
-        libxl__ev_evtchn *evev)
+                                                    libxl__ev_evtchn *evev)
 {
-    libxl__domain_suspend_state *dss = CONTAINER_OF(evev, *dss, guest_evtchn);
-    STATE_AO_GC(dss->ao);
+    libxl__domain_suspend_state2 *dss2 = CONTAINER_OF(evev, *dss2, guest_evtchn);
+    STATE_AO_GC(dss2->ao);
     /* If we should be done waiting, suspend_common_wait_guest_check
      * will end up calling domain_suspend_common_guest_suspended or
      * domain_suspend_common_failed, both of which cancel the evtchn
      * wait.  So re-enable it now. */
-    libxl__ev_evtchn_wait(gc, &dss->guest_evtchn);
-    suspend_common_wait_guest_check(egc, dss);
+    libxl__ev_evtchn_wait(gc, &dss2->guest_evtchn);
+    suspend_common_wait_guest_check(egc, dss2);
 }
 
 static void domain_suspend_common_pvcontrol_suspending(libxl__egc *egc,
       libxl__xswait_state *xswa, int rc, const char *state)
 {
-    libxl__domain_suspend_state *dss = CONTAINER_OF(xswa, *dss, pvcontrol);
-    STATE_AO_GC(dss->ao);
+    libxl__domain_suspend_state2 *dss2 = CONTAINER_OF(xswa, *dss2, pvcontrol);
+    STATE_AO_GC(dss2->ao);
     xs_transaction_t t = 0;
 
     if (!rc && !domain_suspend_pvcontrol_acked(state))
         /* keep waiting */
         return;
 
-    libxl__xswait_stop(gc, &dss->pvcontrol);
+    libxl__xswait_stop(gc, &dss2->pvcontrol);
 
     if (rc == ERROR_TIMEDOUT) {
         /*
@@ -1272,56 +1275,56 @@ static void domain_suspend_common_pvcontrol_suspending(libxl__egc *egc,
     LOG(DEBUG, "guest acknowledged suspend request");
 
     libxl__xs_transaction_abort(gc, &t);
-    dss->guest_responded = 1;
-    domain_suspend_common_wait_guest(egc,dss);
+    dss2->guest_responded = 1;
+    domain_suspend_common_wait_guest(egc,dss2);
     return;
 
  err:
     libxl__xs_transaction_abort(gc, &t);
-    domain_suspend_common_failed(egc, dss);
+    domain_suspend_common_failed(egc, dss2);
     return;
 }
 
 static void domain_suspend_common_wait_guest(libxl__egc *egc,
-                                             libxl__domain_suspend_state *dss)
+                                             libxl__domain_suspend_state2 *dss2)
 {
-    STATE_AO_GC(dss->ao);
+    STATE_AO_GC(dss2->ao);
     int rc;
 
     LOG(DEBUG, "wait for the guest to suspend");
 
-    rc = libxl__ev_xswatch_register(gc, &dss->guest_watch,
+    rc = libxl__ev_xswatch_register(gc, &dss2->guest_watch,
                                     suspend_common_wait_guest_watch,
                                     "@releaseDomain");
     if (rc) goto err;
 
-    rc = libxl__ev_time_register_rel(gc, &dss->guest_timeout,
+    rc = libxl__ev_time_register_rel(gc, &dss2->guest_timeout,
                                      suspend_common_wait_guest_timeout,
                                      60*1000);
     if (rc) goto err;
     return;
 
  err:
-    domain_suspend_common_failed(egc, dss);
+    domain_suspend_common_failed(egc, dss2);
 }
 
 static void suspend_common_wait_guest_watch(libxl__egc *egc,
       libxl__ev_xswatch *xsw, const char *watch_path, const char *event_path)
 {
-    libxl__domain_suspend_state *dss = CONTAINER_OF(xsw, *dss, guest_watch);
-    suspend_common_wait_guest_check(egc, dss);
+    libxl__domain_suspend_state2 *dss2 = CONTAINER_OF(xsw, *dss2, guest_watch);
+    suspend_common_wait_guest_check(egc, dss2);
 }
 
 static void suspend_common_wait_guest_check(libxl__egc *egc,
-        libxl__domain_suspend_state *dss)
+                                            libxl__domain_suspend_state2 *dss2)
 {
-    STATE_AO_GC(dss->ao);
+    STATE_AO_GC(dss2->ao);
     xc_domaininfo_t info;
     int ret;
     int shutdown_reason;
 
     /* Convenience aliases */
-    const uint32_t domid = dss->domid;
+    const uint32_t domid = dss2->domid;
 
     ret = xc_domain_getinfolist(CTX->xch, domid, 1, &info);
     if (ret < 0) {
@@ -1348,59 +1351,59 @@ static void suspend_common_wait_guest_check(libxl__egc *egc,
     }
 
     LOG(DEBUG, "guest has suspended");
-    domain_suspend_common_guest_suspended(egc, dss);
+    domain_suspend_common_guest_suspended(egc, dss2);
     return;
 
  err:
-    domain_suspend_common_failed(egc, dss);
+    domain_suspend_common_failed(egc, dss2);
 }
 
 static void suspend_common_wait_guest_timeout(libxl__egc *egc,
       libxl__ev_time *ev, const struct timeval *requested_abs)
 {
-    libxl__domain_suspend_state *dss = CONTAINER_OF(ev, *dss, guest_timeout);
-    STATE_AO_GC(dss->ao);
+    libxl__domain_suspend_state2 *dss2 = CONTAINER_OF(ev, *dss2, guest_timeout);
+    STATE_AO_GC(dss2->ao);
     LOG(ERROR, "guest did not suspend, timed out");
-    domain_suspend_common_failed(egc, dss);
+    domain_suspend_common_failed(egc, dss2);
 }
 
 static void domain_suspend_common_guest_suspended(libxl__egc *egc,
-                                         libxl__domain_suspend_state *dss)
+                                            libxl__domain_suspend_state2 *dss2)
 {
-    STATE_AO_GC(dss->ao);
+    STATE_AO_GC(dss2->ao);
     int ret;
 
-    libxl__ev_evtchn_cancel(gc, &dss->guest_evtchn);
-    libxl__ev_xswatch_deregister(gc, &dss->guest_watch);
-    libxl__ev_time_deregister(gc, &dss->guest_timeout);
+    libxl__ev_evtchn_cancel(gc, &dss2->guest_evtchn);
+    libxl__ev_xswatch_deregister(gc, &dss2->guest_watch);
+    libxl__ev_time_deregister(gc, &dss2->guest_timeout);
 
-    if (dss->hvm) {
-        ret = libxl__domain_suspend_device_model(gc, dss);
+    if (dss2->hvm) {
+        ret = libxl__domain_suspend_device_model(gc, dss2);
         if (ret) {
             LOG(ERROR, "libxl__domain_suspend_device_model failed ret=%d", ret);
-            domain_suspend_common_failed(egc, dss);
+            domain_suspend_common_failed(egc, dss2);
             return;
         }
     }
-    domain_suspend_common_done(egc, dss, 1);
+    domain_suspend_common_done(egc, dss2, 1);
 }
 
 static void domain_suspend_common_failed(libxl__egc *egc,
-                                         libxl__domain_suspend_state *dss)
+                                         libxl__domain_suspend_state2 *dss2)
 {
-    domain_suspend_common_done(egc, dss, 0);
+    domain_suspend_common_done(egc, dss2, 0);
 }
 
 static void domain_suspend_common_done(libxl__egc *egc,
-                                       libxl__domain_suspend_state *dss,
+                                       libxl__domain_suspend_state2 *dss2,
                                        bool ok)
 {
     EGC_GC;
-    assert(!libxl__xswait_inuse(&dss->pvcontrol));
-    libxl__ev_evtchn_cancel(gc, &dss->guest_evtchn);
-    libxl__ev_xswatch_deregister(gc, &dss->guest_watch);
-    libxl__ev_time_deregister(gc, &dss->guest_timeout);
-    dss->callback_common_done(egc, dss, ok);
+    assert(!libxl__xswait_inuse(&dss2->pvcontrol));
+    libxl__ev_evtchn_cancel(gc, &dss2->guest_evtchn);
+    libxl__ev_xswatch_deregister(gc, &dss2->guest_watch);
+    libxl__ev_time_deregister(gc, &dss2->guest_timeout);
+    dss2->callback_common_done(egc, dss2, ok);
 }
 
 static inline char *physmap_path(libxl__gc *gc, uint32_t domid,
@@ -1493,19 +1496,24 @@ static void libxl__domain_suspend_callback(void *data)
     libxl__egc *egc = shs->egc;
     libxl__domain_suspend_state *dss = CONTAINER_OF(shs, *dss, shs);
 
-    dss->callback_common_done = domain_suspend_callback_common_done;
-    domain_suspend_callback_common(egc, dss);
+    /* Convenience aliases */
+    libxl__domain_suspend_state2 *dss2 = &dss->dss2;
+
+    dss2->callback_common_done = domain_suspend_callback_common_done;
+    domain_suspend_callback_common(egc, dss2);
 }
 
 static void domain_suspend_callback_common_done(libxl__egc *egc,
-                                libxl__domain_suspend_state *dss, int ok)
+                                libxl__domain_suspend_state2 *dss2, int ok)
 {
+    libxl__domain_suspend_state *dss = CONTAINER_OF(dss2, *dss, dss2);
+
     libxl__xc_domain_saverestore_async_callback_done(egc, &dss->shs, ok);
 }
 
 /*----- remus callbacks -----*/
 static void remus_domain_suspend_callback_common_done(libxl__egc *egc,
-                                libxl__domain_suspend_state *dss, int ok);
+                                libxl__domain_suspend_state2 *dss2, int ok);
 static void remus_devices_postsuspend_cb(libxl__egc *egc,
                                          libxl__remus_devices_state *rds,
                                          int rc);
@@ -1519,13 +1527,18 @@ static void libxl__remus_domain_suspend_callback(void *data)
     libxl__egc *egc = shs->egc;
     libxl__domain_suspend_state *dss = CONTAINER_OF(shs, *dss, shs);
 
-    dss->callback_common_done = remus_domain_suspend_callback_common_done;
-    domain_suspend_callback_common(egc, dss);
+    /* Convenience aliases */
+    libxl__domain_suspend_state2 *const dss2 = &dss->dss2;
+
+    dss2->callback_common_done = remus_domain_suspend_callback_common_done;
+    domain_suspend_callback_common(egc, dss2);
 }
 
 static void remus_domain_suspend_callback_common_done(libxl__egc *egc,
-                                libxl__domain_suspend_state *dss, int ok)
+                                libxl__domain_suspend_state2 *dss2, int ok)
 {
+    libxl__domain_suspend_state *dss = CONTAINER_OF(dss2, *dss, dss2);
+
     if (!ok)
         goto out;
 
@@ -1687,6 +1700,11 @@ static void remus_next_checkpoint(libxl__egc *egc, libxl__ev_time *ev,
 }
 
 /*----- main code for suspending, in order of execution -----*/
+void libxl__domain_suspend2(libxl__egc *egc,
+                            libxl__domain_suspend_state2 *dss2)
+{
+    domain_suspend_callback_common(egc, dss2);
+}
 
 void libxl__domain_suspend(libxl__egc *egc, libxl__domain_suspend_state *dss)
 {
@@ -1702,20 +1720,23 @@ void libxl__domain_suspend(libxl__egc *egc, libxl__domain_suspend_state *dss)
     const libxl_domain_remus_info *const r_info = dss->remus;
     libxl__srm_save_autogen_callbacks *const callbacks =
         &dss->shs.callbacks.save.a;
+    libxl__domain_suspend_state2 *dss2 = &dss->dss2;
 
     logdirty_init(&dss->logdirty);
-    libxl__xswait_init(&dss->pvcontrol);
-    libxl__ev_evtchn_init(&dss->guest_evtchn);
-    libxl__ev_xswatch_init(&dss->guest_watch);
-    libxl__ev_time_init(&dss->guest_timeout);
+    libxl__xswait_init(&dss2->pvcontrol);
+    libxl__ev_evtchn_init(&dss2->guest_evtchn);
+    libxl__ev_xswatch_init(&dss2->guest_watch);
+    libxl__ev_time_init(&dss2->guest_timeout);
 
     switch (type) {
     case LIBXL_DOMAIN_TYPE_HVM: {
         dss->hvm = 1;
+        dss2->hvm = 1;
         break;
     }
     case LIBXL_DOMAIN_TYPE_PV:
         dss->hvm = 0;
+        dss2->hvm = 0;
         break;
     default:
         abort();
@@ -1725,10 +1746,13 @@ void libxl__domain_suspend(libxl__egc *egc, libxl__domain_suspend_state *dss)
           | (debug ? XCFLAGS_DEBUG : 0)
           | (dss->hvm ? XCFLAGS_HVM : 0);
 
-    dss->guest_evtchn.port = -1;
-    dss->guest_evtchn_lockfd = -1;
-    dss->guest_responded = 0;
-    dss->dm_savefile = libxl__device_model_savefile(gc, domid);
+    dss2->guest_evtchn.port = -1;
+    dss2->guest_evtchn_lockfd = -1;
+    dss2->guest_responded = 0;
+    dss2->dm_savefile = libxl__device_model_savefile(gc, domid);
+    dss2->domid = domid;
+    dss2->ao = ao;
+    dss2->save_dm = 1;
 
     if (r_info != NULL) {
         dss->interval = r_info->interval;
@@ -1739,11 +1763,11 @@ void libxl__domain_suspend(libxl__egc *egc, libxl__domain_suspend_state *dss)
     port = xs_suspend_evtchn_port(dss->domid);
 
     if (port >= 0) {
-        dss->guest_evtchn.port =
+        dss2->guest_evtchn.port =
             xc_suspend_evtchn_init_exclusive(CTX->xch, CTX->xce,
-                                  dss->domid, port, &dss->guest_evtchn_lockfd);
+                                  dss->domid, port, &dss2->guest_evtchn_lockfd);
 
-        if (dss->guest_evtchn.port < 0) {
+        if (dss2->guest_evtchn.port < 0) {
             LOG(WARN, "Suspend event channel initialization failed");
             rc = ERROR_FAIL;
             goto out;
@@ -1782,10 +1806,10 @@ void libxl__xc_domain_save_done(libxl__egc *egc, void *dss_void,
 
     if (retval) {
         LOGEV(ERROR, errnoval, "saving domain: %s",
-                         dss->guest_responded ?
+                         dss->dss2.guest_responded ?
                          "domain responded to suspend request" :
                          "domain did not respond to suspend request");
-        if ( !dss->guest_responded )
+        if ( !dss->dss2.guest_responded )
             rc = ERROR_GUEST_TIMEDOUT;
         else
             rc = ERROR_FAIL;
@@ -1793,7 +1817,7 @@ void libxl__xc_domain_save_done(libxl__egc *egc, void *dss_void,
     }
 
     if (type == LIBXL_DOMAIN_TYPE_HVM) {
-        rc = libxl__domain_suspend_device_model(gc, dss);
+        rc = libxl__domain_suspend_device_model(gc, &dss->dss2);
         if (rc) goto out;
 
         libxl__domain_save_device_model(egc, dss, domain_suspend_done);
@@ -1821,7 +1845,7 @@ void libxl__domain_save_device_model(libxl__egc *egc,
     dss->save_dm_callback = callback;
 
     /* Convenience aliases */
-    const char *const filename = dss->dm_savefile;
+    const char *const filename = dss->dss2.dm_savefile;
     const int fd = dss->fd;
 
     libxl__datacopier_state *dc = &dss->save_dm_datacopier;
@@ -1877,7 +1901,7 @@ static void save_device_model_datacopier_done(libxl__egc *egc,
     STATE_AO_GC(dss->ao);
 
     /* Convenience aliases */
-    const char *const filename = dss->dm_savefile;
+    const char *const filename = dss->dss2.dm_savefile;
     int our_rc = 0;
     int rc;
 
@@ -1908,12 +1932,13 @@ static void domain_suspend_done(libxl__egc *egc,
 
     /* Convenience aliases */
     const uint32_t domid = dss->domid;
+    libxl__domain_suspend_state2 *const dss2 = &dss->dss2;
 
-    libxl__ev_evtchn_cancel(gc, &dss->guest_evtchn);
+    libxl__ev_evtchn_cancel(gc, &dss2->guest_evtchn);
 
-    if (dss->guest_evtchn.port > 0)
+    if (dss2->guest_evtchn.port > 0)
         xc_suspend_evtchn_release(CTX->xch, CTX->xce, domid,
-                           dss->guest_evtchn.port, &dss->guest_evtchn_lockfd);
+                           dss2->guest_evtchn.port, &dss2->guest_evtchn_lockfd);
 
     if (!dss->remus) {
         remus_teardown_done(egc, &dss->rds, rc);
tools/libxl/libxl_internal.h +29 -10
index e631eaf..901cacd 100644
@@ -2670,6 +2670,7 @@ _hidden int libxl__netbuffer_enabled(libxl__gc *gc);
 /*----- Domain suspend (save) state structure -----*/
 
 typedef struct libxl__domain_suspend_state libxl__domain_suspend_state;
+typedef struct libxl__domain_suspend_state2 libxl__domain_suspend_state2;
 
 typedef void libxl__domain_suspend_cb(libxl__egc*,
                                       libxl__domain_suspend_state*, int rc);
@@ -2684,6 +2685,29 @@ typedef struct libxl__logdirty_switch {
     libxl__ev_time timeout;
 } libxl__logdirty_switch;
 
+/*
+ * libxl__domain_suspend_state is for saving guest, not
+ * for suspending guest. We need to an independent API
+ * to suspend guest only.
+ */
+struct libxl__domain_suspend_state2 {
+    /* set by caller of libxl__domain_suspend2 */
+    libxl__ao *ao;
+
+    uint32_t domid;
+    libxl__ev_evtchn guest_evtchn;;
+    int guest_evtchn_lockfd;
+    int hvm;
+    const char *dm_savefile;
+    void (*callback_common_done)(libxl__egc*,
+                                 libxl__domain_suspend_state2*, int ok);
+    int save_dm;
+    int guest_responded;
+    libxl__xswait_state pvcontrol;
+    libxl__ev_xswatch guest_watch;
+    libxl__ev_time guest_timeout;
+};
+
 struct libxl__domain_suspend_state {
     /* set by caller of libxl__domain_suspend */
     libxl__ao *ao;
@@ -2696,22 +2720,14 @@ struct libxl__domain_suspend_state {
     int debug;
     const libxl_domain_remus_info *remus;
     /* private */
-    libxl__ev_evtchn guest_evtchn;
-    int guest_evtchn_lockfd;
+    libxl__domain_suspend_state2 dss2;
     int hvm;
     int xcflags;
-    int guest_responded;
-    libxl__xswait_state pvcontrol;
-    libxl__ev_xswatch guest_watch;
-    libxl__ev_time guest_timeout;
-    const char *dm_savefile;
     libxl__remus_devices_state rds;
     libxl__ev_time checkpoint_timeout; /* used for Remus checkpoint */
     int interval; /* checkpoint interval (for Remus) */
     libxl__save_helper_state shs;
     libxl__logdirty_switch logdirty;
-    void (*callback_common_done)(libxl__egc*,
-                                 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;
@@ -2983,6 +2999,9 @@ struct libxl__domain_create_state {
 
 /*----- Domain suspend (save) functions -----*/
 
+/* calls dss2->callback_common_done when done */
+_hidden void libxl__domain_suspend2(libxl__egc *egc,
+                                    libxl__domain_suspend_state2 *dss2);
 /* calls dss->callback when done */
 _hidden void libxl__domain_suspend(libxl__egc *egc,
                                    libxl__domain_suspend_state *dss);
@@ -3022,7 +3041,7 @@ _hidden void libxl__xc_domain_restore_done(libxl__egc *egc, void *dcs_void,
 
 /* Each time the dm needs to be saved, we must call suspend and then save */
 _hidden int libxl__domain_suspend_device_model(libxl__gc *gc,
-                                           libxl__domain_suspend_state *dss);
+                                           libxl__domain_suspend_state2 *dss2);
 _hidden void libxl__domain_save_device_model(libxl__egc *egc,
                                      libxl__domain_suspend_state *dss,
                                      libxl__save_device_model_cb *callback);
Signature · 1 line
1.9.3
#5 lore raw

RFC PATCH v2 5/45 Update libxl__domain_resume() for colo

In reply to Wen Congyang

Secondary vm is running in colo mode. So we will do the following things again and again:
1. suspend both primay vm and secondary vm
2. sync the state
3. resume both primary vm and secondary vm
We will send qemu's state each time in step2, and slave's qemu should read it each time before resuming secondary vm. libxl__domain_resume() doesn't read qemu's state. Add a new parameter to control whether we need to read qemu's state before resuming.

Note: we should update qemu to support it.

---
 tools/libxl/libxl.c          |  7 ++++---
 tools/libxl/libxl_dom.c      | 24 +++++++++++++++++++++---
 tools/libxl/libxl_internal.h |  8 ++++++--
 tools/libxl/libxl_qmp.c      | 10 ++++++++++
 4 files changed, 41 insertions(+), 8 deletions(-)
tools/libxl/libxl.c +4 -3
index 8182966..b262309 100644
@@ -466,7 +466,8 @@ int libxl_domain_rename(libxl_ctx *ctx, uint32_t domid,
     return rc;
 }
 
-int libxl__domain_resume(libxl__gc *gc, uint32_t domid, int suspend_cancel)
+int libxl__domain_resume(libxl__gc *gc, uint32_t domid,
+                         int suspend_cancel, int read_savefile)
 {
     int rc = 0;
 
@@ -483,7 +484,7 @@ int libxl__domain_resume(libxl__gc *gc, uint32_t domid, int suspend_cancel)
     }
 
     if (type == LIBXL_DOMAIN_TYPE_HVM) {
-        rc = libxl__domain_resume_device_model(gc, domid);
+        rc = libxl__domain_resume_device_model(gc, domid, read_savefile);
         if (rc) {
             LOG(ERROR, "failed to resume device model for domain %u:%d",
                 domid, rc);
@@ -503,7 +504,7 @@ int libxl_domain_resume(libxl_ctx *ctx, uint32_t domid, int suspend_cancel,
                         const libxl_asyncop_how *ao_how)
 {
     AO_CREATE(ctx, domid, ao_how);
-    int rc = libxl__domain_resume(gc, domid, suspend_cancel);
+    int rc = libxl__domain_resume(gc, domid, suspend_cancel, 0);
     libxl__ao_complete(egc, ao, rc);
     return AO_INPROGRESS;
 }
tools/libxl/libxl_dom.c +21 -3
index 1607930..288cbd8 100644
@@ -1091,16 +1091,34 @@ int libxl__domain_suspend_device_model(libxl__gc *gc,
     return ret;
 }
 
-int libxl__domain_resume_device_model(libxl__gc *gc, uint32_t domid)
+int libxl__domain_resume_device_model(libxl__gc *gc, uint32_t domid,
+                                      int read_savefile)
 {
 
     switch (libxl__device_model_version_running(gc, domid)) {
     case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL: {
-        libxl__qemu_traditional_cmd(gc, domid, "continue");
+        if (read_savefile)
+            libxl__qemu_traditional_cmd(gc, domid, "resume");
+        else
+            libxl__qemu_traditional_cmd(gc, domid, "continue");
         libxl__wait_for_device_model_deprecated(gc, domid, "running", NULL, NULL, NULL);
         break;
     }
     case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN:
+        if (read_savefile) {
+            char *state_file;
+            int rc;
+
+            state_file = libxl__sprintf(NOGC,
+                                        XC_DEVICE_MODEL_RESTORE_FILE".%d",
+                                        domid);
+            /* This command only restores the device state */
+            rc = libxl__qmp_restore(gc, domid, state_file);
+            free(state_file);
+            if (rc)
+                return ERROR_FAIL;
+        }
+
         if (libxl__qmp_resume(gc, domid))
             return ERROR_FAIL;
         break;
@@ -1591,7 +1609,7 @@ static void remus_devices_preresume_cb(libxl__egc *egc,
         goto out;
 
     /* Resumes the domain and the device model */
-    if (!libxl__domain_resume(gc, dss->domid, /* Fast Suspend */1))
+    if (!libxl__domain_resume(gc, dss->domid, /* Fast Suspend */1, 0))
         ok = 1;
 
 out:
tools/libxl/libxl_internal.h +6 -2
index 901cacd..437a9cd 100644
@@ -989,12 +989,14 @@ _hidden int libxl__domain_rename(libxl__gc *gc, uint32_t domid,
 
 _hidden int libxl__toolstack_restore(uint32_t domid, const uint8_t *buf,
                                      uint32_t size, void *data);
-_hidden int libxl__domain_resume_device_model(libxl__gc *gc, uint32_t domid);
+_hidden int libxl__domain_resume_device_model(libxl__gc *gc,
+                                              uint32_t domid,
+                                              int read_savefile);
 
 _hidden void libxl__userdata_destroyall(libxl__gc *gc, uint32_t domid);
 
 _hidden int libxl__domain_resume(libxl__gc *gc, uint32_t domid,
-                                 int suspend_cancel);
+                                 int suspend_cancel, int read_savefile);
 
 /* returns 0 or 1, or a libxl error code */
 _hidden int libxl__domain_pvcontrol_available(libxl__gc *gc, uint32_t domid);
@@ -1580,6 +1582,8 @@ _hidden int libxl__qmp_stop(libxl__gc *gc, int domid);
 _hidden int libxl__qmp_resume(libxl__gc *gc, int domid);
 /* Save current QEMU state into fd. */
 _hidden int libxl__qmp_save(libxl__gc *gc, int domid, const char *filename);
+/* Load current QEMU state from fd. */
+_hidden int libxl__qmp_restore(libxl__gc *gc, int domid, const char *filename);
 /* Set dirty bitmap logging status */
 _hidden int libxl__qmp_set_global_dirty_log(libxl__gc *gc, int domid, bool enable);
 _hidden int libxl__qmp_insert_cdrom(libxl__gc *gc, int domid, const libxl_device_disk *disk);
tools/libxl/libxl_qmp.c +10 -0
index c7324e6..e1c2fd1 100644
@@ -871,6 +871,16 @@ int libxl__qmp_save(libxl__gc *gc, int domid, const char *filename)
                            NULL, NULL);
 }
 
+int libxl__qmp_restore(libxl__gc *gc, int domid, const char *state_file)
+{
+    libxl__json_object *args = NULL;
+
+    qmp_parameters_add_string(gc, &args, "filename", (char *)state_file);
+
+    return qmp_run_command(gc, domid, "xen-load-devices-state", args,
+                           NULL, NULL);
+}
+
 static int qmp_change(libxl__gc *gc, libxl__qmp_handler *qmp,
                       char *device, char *target, char *arg)
 {
Signature · 1 line
1.9.3
#6 lore raw

RFC PATCH v2 6/45 Update libxl__domain_suspend_common_switch_qemu_logdirty() for colo

In reply to Wen Congyang

Secondary vm is running in colo mode. So we need to send secondary vm's dirty page information to master. libxl__domain_suspend_common_switch_qemu_logdirty() is to enable qemu logdirty. But it uses domain_suspend_state, and calls libxl__xc_domain_saverestore_async_callback_done() before exits.

Introduce a new API libxl__domain_common_switch_qemu_logdirty(). This API only uses libxl__logdirty_switch, and calls lds->callback before exits.

---
 tools/libxl/libxl_dom.c      | 79 +++++++++++++++++++++++++++-----------------
 tools/libxl/libxl_internal.h | 12 +++++--
 2 files changed, 59 insertions(+), 32 deletions(-)
tools/libxl/libxl_dom.c +49 -30
index 288cbd8..3e22322 100644
@@ -859,7 +859,7 @@ static void switch_logdirty_timeout(libxl__egc *egc, libxl__ev_time *ev,
 static void switch_logdirty_xswatch(libxl__egc *egc, libxl__ev_xswatch*,
                             const char *watch_path, const char *event_path);
 static void switch_logdirty_done(libxl__egc *egc,
-                                 libxl__domain_suspend_state *dss, int ok);
+                                 libxl__logdirty_switch *lds, int ok);
 
 static void logdirty_init(libxl__logdirty_switch *lds)
 {
@@ -870,12 +870,10 @@ static void logdirty_init(libxl__logdirty_switch *lds)
 
 static void domain_suspend_switch_qemu_xen_traditional_logdirty
                                (int domid, unsigned enable,
-                                libxl__save_helper_state *shs)
+                                libxl__logdirty_switch *lds,
+                                libxl__egc *egc)
 {
-    libxl__egc *egc = shs->egc;
-    libxl__domain_suspend_state *dss = CONTAINER_OF(shs, *dss, shs);
-    libxl__logdirty_switch *lds = &dss->logdirty;
-    STATE_AO_GC(dss->ao);
+    STATE_AO_GC(lds->ao);
     int rc;
     xs_transaction_t t = 0;
     const char *got;
@@ -936,64 +934,85 @@ static void domain_suspend_switch_qemu_xen_traditional_logdirty
  out:
     LOG(ERROR,"logdirty switch failed (rc=%d), aborting suspend",rc);
     libxl__xs_transaction_abort(gc, &t);
-    switch_logdirty_done(egc,dss,-1);
+    switch_logdirty_done(egc,lds,-1);
 }
 
 static void domain_suspend_switch_qemu_xen_logdirty
                                (int domid, unsigned enable,
-                                libxl__save_helper_state *shs)
+                                libxl__logdirty_switch *lds,
+                                libxl__egc *egc)
 {
-    libxl__egc *egc = shs->egc;
-    libxl__domain_suspend_state *dss = CONTAINER_OF(shs, *dss, shs);
-    STATE_AO_GC(dss->ao);
+    STATE_AO_GC(lds->ao);
     int rc;
 
     rc = libxl__qmp_set_global_dirty_log(gc, domid, enable);
     if (!rc) {
-        libxl__xc_domain_saverestore_async_callback_done(egc, shs, 0);
+        lds->callback(egc, lds, 0);
     } else {
         LOG(ERROR,"logdirty switch failed (rc=%d), aborting suspend",rc);
-        libxl__xc_domain_saverestore_async_callback_done(egc, shs, -1);
+        lds->callback(egc, lds, -1);
     }
 }
 
+static void libxl__domain_suspend_switch_qemu_logdirty_done
+                                (libxl__egc *egc,
+                                 libxl__logdirty_switch *lds,
+                                 int rc)
+{
+    libxl__domain_suspend_state *dss = CONTAINER_OF(lds, *dss, logdirty);
+
+    libxl__xc_domain_saverestore_async_callback_done(egc, &dss->shs, rc);
+}
+
 void libxl__domain_suspend_common_switch_qemu_logdirty
                                (int domid, unsigned enable, void *user)
 {
     libxl__save_helper_state *shs = user;
     libxl__egc *egc = shs->egc;
     libxl__domain_suspend_state *dss = CONTAINER_OF(shs, *dss, shs);
-    STATE_AO_GC(dss->ao);
+
+    /* convenience aliases */
+    libxl__logdirty_switch *const lds = &dss->logdirty;
+
+    lds->callback = libxl__domain_suspend_switch_qemu_logdirty_done;
+
+    libxl__domain_common_switch_qemu_logdirty(domid, enable, lds, egc);
+}
+
+void libxl__domain_common_switch_qemu_logdirty(int domid, unsigned enable,
+                                               libxl__logdirty_switch *lds,
+                                               libxl__egc *egc)
+{
+    STATE_AO_GC(lds->ao);
 
     switch (libxl__device_model_version_running(gc, domid)) {
     case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL:
-        domain_suspend_switch_qemu_xen_traditional_logdirty(domid, enable, shs);
+        domain_suspend_switch_qemu_xen_traditional_logdirty(domid, enable,
+                                                            lds, egc);
         break;
     case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN:
-        domain_suspend_switch_qemu_xen_logdirty(domid, enable, shs);
+        domain_suspend_switch_qemu_xen_logdirty(domid, enable, lds, egc);
         break;
     default:
         LOG(ERROR,"logdirty switch failed"
             ", no valid device model version found, aborting suspend");
-        libxl__xc_domain_saverestore_async_callback_done(egc, shs, -1);
+        lds->callback(egc, lds, -1);
     }
 }
 static void switch_logdirty_timeout(libxl__egc *egc, libxl__ev_time *ev,
                                     const struct timeval *requested_abs)
 {
-    libxl__domain_suspend_state *dss = CONTAINER_OF(ev, *dss, logdirty.timeout);
-    STATE_AO_GC(dss->ao);
+    libxl__logdirty_switch *lds = CONTAINER_OF(ev, *lds, timeout);
+    STATE_AO_GC(lds->ao);
     LOG(ERROR,"logdirty switch: wait for device model timed out");
-    switch_logdirty_done(egc,dss,-1);
+    switch_logdirty_done(egc,lds,-1);
 }
 
 static void switch_logdirty_xswatch(libxl__egc *egc, libxl__ev_xswatch *watch,
                             const char *watch_path, const char *event_path)
 {
-    libxl__domain_suspend_state *dss =
-        CONTAINER_OF(watch, *dss, logdirty.watch);
-    libxl__logdirty_switch *lds = &dss->logdirty;
-    STATE_AO_GC(dss->ao);
+    libxl__logdirty_switch *lds = CONTAINER_OF(watch, *lds, watch);
+    STATE_AO_GC(lds->ao);
     const char *got;
     xs_transaction_t t = 0;
     int rc;
@@ -1037,24 +1056,23 @@ static void switch_logdirty_xswatch(libxl__egc *egc, libxl__ev_xswatch *watch,
     libxl__xs_transaction_abort(gc, &t);
 
     if (!rc) {
-        switch_logdirty_done(egc,dss,0);
+        switch_logdirty_done(egc,lds,0);
     } else if (rc < 0) {
         LOG(ERROR,"logdirty switch: failed (rc=%d)",rc);
-        switch_logdirty_done(egc,dss,-1);
+        switch_logdirty_done(egc,lds,-1);
     }
 }
 
 static void switch_logdirty_done(libxl__egc *egc,
-                                 libxl__domain_suspend_state *dss,
+                                 libxl__logdirty_switch *lds,
                                  int broke)
 {
-    STATE_AO_GC(dss->ao);
-    libxl__logdirty_switch *lds = &dss->logdirty;
+    STATE_AO_GC(lds->ao);
 
     libxl__ev_xswatch_deregister(gc, &lds->watch);
     libxl__ev_time_deregister(gc, &lds->timeout);
 
-    libxl__xc_domain_saverestore_async_callback_done(egc, &dss->shs, broke);
+    lds->callback(egc, lds, broke);
 }
 
 /*----- callbacks, called by xc_domain_save -----*/
@@ -1741,6 +1759,7 @@ void libxl__domain_suspend(libxl__egc *egc, libxl__domain_suspend_state *dss)
     libxl__domain_suspend_state2 *dss2 = &dss->dss2;
 
     logdirty_init(&dss->logdirty);
+    dss->logdirty.ao = ao;
     libxl__xswait_init(&dss2->pvcontrol);
     libxl__ev_evtchn_init(&dss2->guest_evtchn);
     libxl__ev_xswatch_init(&dss2->guest_watch);
tools/libxl/libxl_internal.h +10 -2
index 437a9cd..ee3561c 100644
@@ -2681,13 +2681,18 @@ typedef void libxl__domain_suspend_cb(libxl__egc*,
 typedef void libxl__save_device_model_cb(libxl__egc*,
                                          libxl__domain_suspend_state*, int rc);
 
-typedef struct libxl__logdirty_switch {
+typedef struct libxl__logdirty_switch libxl__logdirty_switch;
+struct libxl__logdirty_switch {
+    /* set by caller of libxl__domain_common_switch_qemu_logdirty */
+    libxl__ao *ao;
+    void (*callback)(libxl__egc *egc, libxl__logdirty_switch *lds, int rc);
+
     const char *cmd;
     const char *cmd_path;
     const char *ret_path;
     libxl__ev_xswatch watch;
     libxl__ev_time timeout;
-} libxl__logdirty_switch;
+};
 
 /*
  * libxl__domain_suspend_state is for saving guest, not
@@ -3029,6 +3034,9 @@ 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 void libxl__domain_common_switch_qemu_logdirty
+                                (int domid, unsigned int enable,
+                                 libxl__logdirty_switch *lds, libxl__egc *egc);
 _hidden int libxl__toolstack_save(uint32_t domid, uint8_t **buf,
         uint32_t *len, void *data);
 
Signature · 1 line
1.9.3
#7 lore raw

RFC PATCH v2 7/45 Introduce a new internal API libxl__domain_unpause()

In reply to Wen Congyang

The guest is paused after libxl_domain_create_restore(). Secondary vm is running in colo mode. So we need to unpause the guest. The current API libxl_domain_unpause() is not an internal API. Introduce a new API to support it.

---
 tools/libxl/libxl.c          | 21 +++++++++++++++------
 tools/libxl/libxl_internal.h |  1 +
 2 files changed, 16 insertions(+), 6 deletions(-)
tools/libxl/libxl.c +15 -6
index b262309..50213a9 100644
@@ -965,9 +965,8 @@ out:
     return AO_INPROGRESS;
 }
 
-int libxl_domain_unpause(libxl_ctx *ctx, uint32_t domid)
+int libxl__domain_unpause(libxl__gc *gc, uint32_t domid)
 {
-    GC_INIT(ctx);
     char *path;
     char *state;
     int ret, rc = 0;
@@ -987,12 +986,22 @@ int libxl_domain_unpause(libxl_ctx *ctx, uint32_t domid)
                                          NULL, NULL, NULL);
         }
     }
-    ret = xc_domain_unpause(ctx->xch, domid);
-    if (ret<0) {
-        LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "unpausing domain %d", domid);
+
+    ret = xc_domain_unpause(CTX->xch, domid);
+    if (ret < 0) {
+        LOGE(ERROR, "unpausing domain %d", domid);
         rc = ERROR_FAIL;
     }
- out:
+
+out:
+    return rc;
+}
+
+int libxl_domain_unpause(libxl_ctx *ctx, uint32_t domid)
+{
+    GC_INIT(ctx);
+    int rc = libxl__domain_unpause(gc, domid);
+
     GC_FREE;
     return rc;
 }
tools/libxl/libxl_internal.h +1 -0
index ee3561c..c60466c 100644
@@ -997,6 +997,7 @@ _hidden void libxl__userdata_destroyall(libxl__gc *gc, uint32_t domid);
 
 _hidden int libxl__domain_resume(libxl__gc *gc, uint32_t domid,
                                  int suspend_cancel, int read_savefile);
+_hidden int libxl__domain_unpause(libxl__gc *gc, uint32_t domid);
 
 /* returns 0 or 1, or a libxl error code */
 _hidden int libxl__domain_pvcontrol_available(libxl__gc *gc, uint32_t domid);
Signature · 1 line
1.9.3
#8 lore raw

RFC PATCH v2 8/45 Update libxl__domain_unpause() to support qemu-xen

In reply to Wen Congyang

Currently, libxl__domain_unpause() only supports qemu-xen-traditional. Update it to support qemu-xen.

---
 tools/libxl/libxl.c          | 13 +++++--------
 tools/libxl/libxl_dom.c      | 25 +++++++++++++++++++++++++
 tools/libxl/libxl_internal.h |  2 ++
 3 files changed, 32 insertions(+), 8 deletions(-)
tools/libxl/libxl.c +5 -8
index 50213a9..c51fd63 100644
@@ -967,8 +967,6 @@ out:
 
 int libxl__domain_unpause(libxl__gc *gc, uint32_t domid)
 {
-    char *path;
-    char *state;
     int ret, rc = 0;
 
     libxl_domain_type type = libxl__domain_type(gc, domid);
@@ -978,12 +976,11 @@ int libxl__domain_unpause(libxl__gc *gc, uint32_t domid)
     }
 
     if (type == LIBXL_DOMAIN_TYPE_HVM) {
-        path = libxl__sprintf(gc, "/local/domain/0/device-model/%d/state", domid);
-        state = libxl__xs_read(gc, XBT_NULL, path);
-        if (state != NULL && !strcmp(state, "paused")) {
-            libxl__qemu_traditional_cmd(gc, domid, "continue");
-            libxl__wait_for_device_model_deprecated(gc, domid, "running",
-                                         NULL, NULL, NULL);
+        rc = libxl__domain_unpause_device_model(gc, domid);
+        if (rc < 0) {
+            LOG(ERROR, "failed to unpause device model for domain %u:%d",
+                domid, rc);
+            goto out;
         }
     }
 
tools/libxl/libxl_dom.c +25 -0
index 3e22322..55a579b 100644
@@ -2008,6 +2008,31 @@ static void remus_teardown_done(libxl__egc *egc,
     dss->callback(egc, dss, rc);
 }
 
+int libxl__domain_unpause_device_model(libxl__gc *gc, uint32_t domid)
+{
+    char *path;
+    char *state;
+
+    switch (libxl__device_model_version_running(gc, domid)) {
+    case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL:
+        path = libxl__sprintf(gc, "/local/domain/0/device-model/%d/state", domid);
+        state = libxl__xs_read(gc, XBT_NULL, path);
+        if (state != NULL && !strcmp(state, "paused")) {
+            libxl__qemu_traditional_cmd(gc, domid, "continue");
+            libxl__wait_for_device_model_deprecated(gc, domid, "running",
+                                         NULL, NULL, NULL);
+        }
+    case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN:
+        if (libxl__qmp_resume(gc, domid))
+            return ERROR_FAIL;
+        break;
+    default:
+        return ERROR_FAIL;
+    }
+
+    return 0;
+}
+
 /*==================== Miscellaneous ====================*/
 
 char *libxl__uuid2string(libxl__gc *gc, const libxl_uuid uuid)
tools/libxl/libxl_internal.h +2 -0
index c60466c..6bfe5e2 100644
@@ -992,6 +992,8 @@ _hidden int libxl__toolstack_restore(uint32_t domid, const uint8_t *buf,
 _hidden int libxl__domain_resume_device_model(libxl__gc *gc,
                                               uint32_t domid,
                                               int read_savefile);
+_hidden int libxl__domain_unpause_device_model(libxl__gc *gc,
+                                               uint32_t domid);
 
 _hidden void libxl__userdata_destroyall(libxl__gc *gc, uint32_t domid);
 
Signature · 1 line
1.9.3
#9 lore raw

RFC PATCH v2 9/45 support to resume uncooperative HVM guests

In reply to Wen Congyang

For PVHVM, the hypercall return code is 0, and it can be resumed in a new domain context.

For HVM, do nothing.

---
 tools/libxc/xc_resume.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
tools/libxc/xc_resume.c +16 -4
index e67bebd..b862ce3 100644
@@ -109,6 +109,21 @@ static int xc_domain_resume_cooperative(xc_interface *xch, uint32_t domid)
     return do_domctl(xch, &domctl);
 }
 
+static int xc_domain_resume_hvm(xc_interface *xch, uint32_t domid)
+{
+    DECLARE_DOMCTL;
+
+    /*
+     * If it is PVHVM, the hypercall return code is 0, and resume
+     * it in a new domain context.
+     *
+     * If it is a HVM, do nothing.
+     */
+    domctl.cmd = XEN_DOMCTL_resumedomain;
+    domctl.domain = domid;
+    return do_domctl(xch, &domctl);
+}
+
 static int xc_domain_resume_any(xc_interface *xch, uint32_t domid)
 {
     DECLARE_DOMCTL;
@@ -138,10 +153,7 @@ static int xc_domain_resume_any(xc_interface *xch, uint32_t domid)
      */
 #if defined(__i386__) || defined(__x86_64__)
     if ( info.hvm )
-    {
-        ERROR("Cannot resume uncooperative HVM guests");
-        return rc;
-    }
+        return xc_domain_resume_hvm(xch, domid);
 
     if ( xc_domain_get_guest_width(xch, domid, &dinfo->guest_width) != 0 )
     {
Signature · 1 line
1.9.3
#10 lore raw

RFC PATCH v2 10/45 update datecopier to support sending data only

In reply to Wen Congyang

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.9.3
#11 lore raw

RFC PATCH v2 11/45 introduce a new API to aync read data from fd

In reply to Wen Congyang

In colo mode, we will read some data from an fd.
Introduce a new API to avoid redundant codes.

---
 tools/libxl/libxl_aoutils.c  | 73 ++++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_internal.h | 30 ++++++++++++++++++
 2 files changed, 103 insertions(+)
tools/libxl/libxl_aoutils.c +73 -0
index 3e0c0ae..2d36403 100644
@@ -542,3 +542,76 @@ bool libxl__async_exec_inuse(const libxl__async_exec_state *aes)
     assert(time_inuse == child_inuse);
     return child_inuse;
 }
+
+
+/*----- data reader -----*/
+
+static void libxl__datareader_init(libxl__datareader_state *drs)
+{
+    assert(drs->ao);
+    libxl__ev_fd_init(&drs->toread);
+    drs->used = 0;
+}
+
+static void libxl__datareader_kill(libxl__datareader_state *drs)
+{
+    STATE_AO_GC(drs->ao);
+
+    libxl__ev_fd_deregister(gc, &drs->toread);
+}
+
+static void datareader_callback(libxl__egc *egc, libxl__datareader_state *drs,
+                                ssize_t size, int errnoval)
+{
+    libxl__datareader_kill(drs);
+    drs->callback(egc, drs, size, errnoval);
+}
+
+static void datareader_readable(libxl__egc *egc, libxl__ev_fd *ev,
+                                int fd, short events, short revents)
+{
+    libxl__datareader_state *drs = CONTAINER_OF(ev, *drs, toread);
+    STATE_AO_GC(drs->ao);
+    int r;
+
+    if (revents & ~POLLIN) {
+        LOG(ERROR, "unexpected poll event 0x%x (should be POLLIN) on %s",
+            revents, drs->readwhat);
+        datareader_callback(egc, drs, -1, 0);
+        return;
+    }
+
+    assert(revents & POLLIN);
+    while (1) {
+        r = read(ev->fd, drs->buf + drs->used, drs->readsize - drs->used);
+        if (r < 0) {
+            if (errno == EINTR)
+                continue;
+            if (errno == EWOULDBLOCK)
+                break;
+            LOGE(ERROR, "error reading %s",
+                 drs->readwhat);
+            datareader_callback(egc, drs, 0, errno);
+            return;
+        }
+        if (r == 0) {
+            datareader_callback(egc, drs, drs->used, 0);
+            break;
+        }
+
+        drs->used += r;
+    }
+}
+
+int libxl__datareader_start(libxl__datareader_state *drs)
+{
+    int rc;
+    STATE_AO_GC(drs->ao);
+
+    libxl__datareader_init(drs);
+
+    rc = libxl__ev_fd_register(gc, &drs->toread, datareader_readable,
+                               drs->readfd, POLLIN);
+
+    return rc;
+}
tools/libxl/libxl_internal.h +30 -0
index 6bfe5e2..0a615e8 100644
@@ -2092,6 +2092,36 @@ void libxl__async_exec_init(libxl__async_exec_state *aes);
 int libxl__async_exec_start(libxl__gc *gc, libxl__async_exec_state *aes);
 bool libxl__async_exec_inuse(const libxl__async_exec_state *aes);
 
+/*----- datareader: read data from one fd to buffer -----*/
+
+typedef struct libxl__datareader_state libxl__datareader_state;
+
+/*
+ * real_size>=1 means all data was read
+ * real_size==0 means failure happened when reading, errnoval is valid, logged
+ * real_size==-1 means some other internal failure, errnoval not valid, logged
+ * In all cases reader is killed before calling this callback
+ */
+typedef void libxl__datareader_callback(libxl__egc *egc,
+     libxl__datareader_state *drs, ssize_t real_size, int errnoval);
+
+struct libxl__datareader_state {
+    /* caller must fill these in, and they must all remain valid */
+    libxl__ao *ao;
+    int readfd;
+    ssize_t readsize;
+    /* for error msgs */
+    const char *readwhat;
+    libxl__datareader_callback *callback;
+    /* It must contain enough space to store readsize bytes */
+    void *buf;
+    /* remaining fields are private to datareader */
+    libxl__ev_fd toread;
+    ssize_t used;
+};
+
+_hidden int libxl__datareader_start(libxl__datareader_state *drs);
+
 /*----- device addition/removal -----*/
 
 typedef struct libxl__ao_device libxl__ao_device;
Signature · 1 line
1.9.3
#12 lore raw

RFC PATCH v2 12/45 move remus related codes to libxl_remus.c

In reply to Wen Congyang

libxl_domain_remus_start() is external API, and is not moved.

---
 tools/libxl/Makefile      |   2 +-
 tools/libxl/libxl.c       |  57 +--------
 tools/libxl/libxl_dom.c   | 220 +-------------------------------
 tools/libxl/libxl_remus.c | 319 ++++++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_remus.h |  28 ++++
 5 files changed, 353 insertions(+), 273 deletions(-)
 create mode 100644 tools/libxl/libxl_remus.c
 create mode 100644 tools/libxl/libxl_remus.h
tools/libxl/Makefile +1 -1
index ba10ab7..69a6a1d 100644
@@ -56,7 +56,7 @@ else
 LIBXL_OBJS-y += libxl_nonetbuffer.o
 endif
 
-LIBXL_OBJS-y += libxl_remus_device.o libxl_remus_disk_drbd.o
+LIBXL_OBJS-y += libxl_remus.o libxl_remus_device.o libxl_remus_disk_drbd.o
 
 LIBXL_OBJS-$(CONFIG_X86) += libxl_cpuid.o libxl_x86.o
 LIBXL_OBJS-$(CONFIG_ARM) += libxl_nocpuid.o libxl_arm.o
tools/libxl/libxl.c +2 -55

82 lines not on this page · raw message

tools/libxl/libxl_dom.c +3 -217

247 lines not on this page · raw message

tools/libxl/libxl_remus.c new file +319 -0

319 lines not on this page · raw message

tools/libxl/libxl_remus.h new file +28 -0
new file mode 100644
index 0000000..53e5e81
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2014 FUJITSU LIMITED
+ * Author Wen Congyang <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; version 2.1 only. with the special
+ * exception on linking described in file LICENSE.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ */
+
+#ifndef LIBXL_REMUS_H
+#define LIBXL_REMUS_H
+
+void libxl__remus_setup(libxl__egc *egc,
+                        libxl__domain_suspend_state *dss);
+void libxl__remus_teardown(libxl__egc *egc,
+                           libxl__domain_suspend_state *dss,
+                           int rc);
+void libxl__remus_domain_suspend_callback(void *data);
+void libxl__remus_domain_resume_callback(void *data);
+void libxl__remus_domain_checkpoint_callback(void *data);
+
+#endif
Signature · 1 line
1.9.3
#13 lore raw

RFC PATCH v2 13/45 rename remus device to checkpoint device

In reply to Wen Congyang

This patch is auto generated by the following commands:
1. git mv tools/libxl/libxl_remus_device.c tools/libxl/libxl_checkpoint_device.c
2. perl -pi -e 's/libxl_remus_device/libxl_checkpoint_device/g' tools/libxl/Makefile
3. perl -pi -e 's/\blibxl__remus_devices/libxl__checkpoint_devices/g' tools/libxl/*.[ch]
4. perl -pi -e 's/\blibxl__remus_device\b/libxl__checkpoint_device/g' tools/libxl/*.[ch]
5. perl -pi -e 's/\blibxl__remus_device_subkind_ops\b/libxl__checkpoint_device_subkind_ops/g' tools/libxl/*.[ch]
6. perl -pi -e 's/\blibxl__remus_device_kind\b/libxl__checkpoint_device_kind/g' tools/libxl/*.[ch]
7. perl -pi -e 's/\blibxl__remus_callback\b/libxl__checkpoint_callback/g' tools/libxl/*.[ch]
8. perl -pi -e 's/\bremus_device_init\b/checkpoint_device_init/g' tools/libxl/*.[ch]
9. perl -pi -e 's/\bremus_devices_setup\b/checkpoint_devices_setup/g' tools/libxl/*.[ch]
10. perl -pi -e 's/\bdefine_remus_checkpoint_api\b/define_checkpoint_api/g' tools/libxl/*.[ch]
11. perl -pi -e 's/\brds\b/cds/g' tools/libxl/*.[ch]
12. perl -pi -e 's/REMUS_DEVICE/CHECKPOINT_DEVICE/g' tools/libxl/*.[ch] tools/libxl/*.idl
13. perl -pi -e 's/REMUS_DEVOPS/CHECKPOINT_DEVOPS/g' tools/libxl/*.[ch] tools/libxl/*.idl
14. perl -pi -e 's/\bremus\b/checkpoint/g' tools/libxl/libxl_checkpoint_device.[ch]
15. perl -pi -e 's/\bremus device/checkpoint device/g' tools/libxl/libxl_internal.h
16. perl -pi -e 's/\bRemus device/checkpoint device/g' tools/libxl/libxl_internal.h
17. perl -pi -e 's/\bremus abstract/checkpoint abstract/g' tools/libxl/libxl_internal.h
18. perl -pi -e 's/\bremus invocation/checkpoint invocation/g' tools/libxl/libxl_internal.h
19. perl -pi -e 's/\blibxl__remus_device_\(/libxl__checkpoint_device_(/g' tools/libxl/libxl_internal.h

---
 tools/libxl/Makefile                               |   2 +-
 ...xl_remus_device.c => libxl_checkpoint_device.c} | 206 ++++++++++-----------
 tools/libxl/libxl_internal.h                       | 118 ++++++------
 tools/libxl/libxl_netbuffer.c                      | 118 ++++++------
 tools/libxl/libxl_nonetbuffer.c                    |  14 +-
 tools/libxl/libxl_remus.c                          |  80 ++++----
 tools/libxl/libxl_remus_disk_drbd.c                |  62 +++----
 tools/libxl/libxl_types.idl                        |   4 +-
 8 files changed, 302 insertions(+), 302 deletions(-)
 rename tools/libxl/{libxl_remus_device.c => libxl_checkpoint_device.c} (42%)
tools/libxl/Makefile +1 -1
index 69a6a1d..5427461 100644
@@ -56,7 +56,7 @@ else
 LIBXL_OBJS-y += libxl_nonetbuffer.o
 endif
 
-LIBXL_OBJS-y += libxl_remus.o libxl_remus_device.o libxl_remus_disk_drbd.o
+LIBXL_OBJS-y += libxl_remus.o libxl_checkpoint_device.o libxl_remus_disk_drbd.o
 
 LIBXL_OBJS-$(CONFIG_X86) += libxl_cpuid.o libxl_x86.o
 LIBXL_OBJS-$(CONFIG_ARM) += libxl_nocpuid.o libxl_arm.o
tools/libxl/libxl_checkpoint_device.c renamed from tools/libxl/libxl_remus_device.c +103 -103

362 lines not on this page · raw message

tools/libxl/libxl_internal.h +59 -59

227 lines not on this page · raw message

tools/libxl/libxl_netbuffer.c +59 -59

321 lines not on this page · raw message

tools/libxl/libxl_nonetbuffer.c +7 -7
index 28a8326..8380952 100644
@@ -22,26 +22,26 @@ int libxl__netbuffer_enabled(libxl__gc *gc)
     return 0;
 }
 
-static void nic_setup(libxl__remus_device *dev)
+static void nic_setup(libxl__checkpoint_device *dev)
 {
-    STATE_AO_GC(dev->rds->ao);
+    STATE_AO_GC(dev->cds->ao);
 
     dev->aodev.rc = ERROR_FAIL;
-    dev->aodev.callback(dev->rds->egc, &dev->aodev);
+    dev->aodev.callback(dev->cds->egc, &dev->aodev);
 }
 
-static int nic_init(libxl__remus_devices_state *rds)
+static int nic_init(libxl__checkpoint_devices_state *cds)
 {
     return 0;
 }
 
-static void nic_cleanup(libxl__remus_devices_state *rds)
+static void nic_cleanup(libxl__checkpoint_devices_state *cds)
 {
     return;
 }
 
-const libxl__remus_device_subkind_ops remus_device_nic = {
-    .kind = LIBXL__REMUS_DEVICE_NIC,
+const libxl__checkpoint_device_subkind_ops remus_device_nic = {
+    .kind = LIBXL__CHECKPOINT_DEVICE_NIC,
     .init = nic_init,
     .cleanup = nic_cleanup,
     .setup = nic_setup,
tools/libxl/libxl_remus.c +40 -40

203 lines not on this page · raw message

tools/libxl/libxl_remus_disk_drbd.c +31 -31

170 lines not on this page · raw message

tools/libxl/libxl_types.idl +2 -2
index 21ac7f6..1945155 100644
@@ -58,8 +58,8 @@ libxl_error = Enumeration("error", [
     (-12, "OSEVENT_REG_FAIL"),
     (-13, "BUFFERFULL"),
     (-14, "UNKNOWN_CHILD"),
-    (-15, "REMUS_DEVOPS_DOES_NOT_MATCH"),
-    (-16, "REMUS_DEVICE_NOT_SUPPORTED"),
+    (-15, "CHECKPOINT_DEVOPS_DOES_NOT_MATCH"),
+    (-16, "CHECKPOINT_DEVICE_NOT_SUPPORTED"),
     ], value_namespace = "")
 
 libxl_domain_type = Enumeration("domain_type", [
Signature · 1 line
1.9.3
#14 lore raw

RFC PATCH v2 14/45 adjust the indentation

In reply to Wen Congyang
---
 tools/libxl/libxl_checkpoint_device.c | 31 ++++++++++++++++++-------------
 tools/libxl/libxl_internal.h          | 18 ++++++++++--------
 tools/libxl/libxl_remus.c             | 12 ++++++++----
 3 files changed, 36 insertions(+), 25 deletions(-)
tools/libxl/libxl_checkpoint_device.c +18 -13
index 0036858..a57b9e4 100644
@@ -66,9 +66,9 @@ static void devices_teardown_cb(libxl__egc *egc,
 /* checkpoint device setup and teardown */
 
 static libxl__checkpoint_device* checkpoint_device_init(libxl__egc *egc,
-                                              libxl__checkpoint_devices_state *cds,
-                                              libxl__checkpoint_device_kind kind,
-                                              void *libxl_dev)
+                libxl__checkpoint_devices_state *cds,
+                libxl__checkpoint_device_kind kind,
+                void *libxl_dev)
 {
     libxl__checkpoint_device *dev = NULL;
 
@@ -83,9 +83,10 @@ static libxl__checkpoint_device* checkpoint_device_init(libxl__egc *egc,
 }
 
 static void checkpoint_devices_setup(libxl__egc *egc,
-                                libxl__checkpoint_devices_state *cds);
+                                     libxl__checkpoint_devices_state *cds);
 
-void libxl__checkpoint_devices_setup(libxl__egc *egc, libxl__checkpoint_devices_state *cds)
+void libxl__checkpoint_devices_setup(libxl__egc *egc,
+                                     libxl__checkpoint_devices_state *cds)
 {
     int i, rc;
 
@@ -131,7 +132,7 @@ out:
 }
 
 static void checkpoint_devices_setup(libxl__egc *egc,
-                                libxl__checkpoint_devices_state *cds)
+                                     libxl__checkpoint_devices_state *cds)
 {
     int i, rc;
     libxl__checkpoint_device *dev;
@@ -180,14 +181,18 @@ static void devices_setup_cb(libxl__egc *egc,
     for (i = 0; i < cds->num_devices; i++) {
         dev = cds->dev[i];
 
-        if (!dev->aodev.rc || dev->aodev.rc == ERROR_CHECKPOINT_DEVOPS_DOES_NOT_MATCH)
+        if (!dev->aodev.rc ||
+            dev->aodev.rc == ERROR_CHECKPOINT_DEVOPS_DOES_NOT_MATCH)
             continue;
 
         rc = dev->aodev.rc;
         goto out;
     }
 
-    /* if the error is still ERROR_CHECKPOINT_DEVOPS_DOES_NOT_MATCH, begin next iter */
+    /*
+     * if the error is still ERROR_CHECKPOINT_DEVOPS_DOES_NOT_MATCH,
+     * begin next iter
+     */
     if (rc == ERROR_CHECKPOINT_DEVOPS_DOES_NOT_MATCH) {
         checkpoint_devices_setup(egc, cds);
         return;
@@ -198,7 +203,7 @@ out:
 }
 
 void libxl__checkpoint_devices_teardown(libxl__egc *egc,
-                                   libxl__checkpoint_devices_state *cds)
+                                        libxl__checkpoint_devices_state *cds)
 {
     int i;
     libxl__checkpoint_device *dev;
@@ -260,12 +265,12 @@ static void devices_checkpoint_cb(libxl__egc *egc,
 
 /* API implementations */
 
-#define define_checkpoint_api(api)                                \
-void libxl__checkpoint_devices_##api(libxl__egc *egc,                        \
-                                libxl__checkpoint_devices_state *cds)        \
+#define define_checkpoint_api(api)                                      \
+void libxl__checkpoint_devices_##api(libxl__egc *egc,                   \
+                                libxl__checkpoint_devices_state *cds)   \
 {                                                                       \
     int i;                                                              \
-    libxl__checkpoint_device *dev;                                           \
+    libxl__checkpoint_device *dev;                                      \
                                                                         \
     STATE_AO_GC(cds->ao);                                               \
                                                                         \
tools/libxl/libxl_internal.h +10 -8
index b3b726c..766868c 100644
@@ -2541,7 +2541,8 @@ typedef struct libxl__save_helper_state {
  * Each device type needs to implement the interfaces specified in
  * the libxl__checkpoint_device_subkind_ops if it wishes to support Remus.
  *
- * The high-level control flow through the checkpoint device layer is shown below:
+ * The high-level control flow through the checkpoint device layer is shown
+ * below:
  *
  * xl remus
  *  |->  libxl_domain_remus_start
@@ -2610,7 +2611,8 @@ struct libxl__checkpoint_device_subkind_ops {
 };
 
 typedef void libxl__checkpoint_callback(libxl__egc *,
-                                   libxl__checkpoint_devices_state *, int rc);
+                                        libxl__checkpoint_devices_state *,
+                                        int rc);
 
 /*
  * State associated with a checkpoint invocation, including parameters
@@ -2618,7 +2620,7 @@ typedef void libxl__checkpoint_callback(libxl__egc *,
  * save/restore machinery.
  */
 struct libxl__checkpoint_devices_state {
-    /*---- must be set by caller of libxl__checkpoint_device_(setup|teardown) ----*/
+    /*-- must be set by caller of libxl__checkpoint_device_(setup|teardown) --*/
 
     libxl__ao *ao;
     libxl__egc *egc;
@@ -2693,15 +2695,15 @@ struct libxl__checkpoint_device {
 
 /* the following 5 APIs are async ops, call cds->callback when done */
 _hidden void libxl__checkpoint_devices_setup(libxl__egc *egc,
-                                        libxl__checkpoint_devices_state *cds);
+                libxl__checkpoint_devices_state *cds);
 _hidden void libxl__checkpoint_devices_teardown(libxl__egc *egc,
-                                           libxl__checkpoint_devices_state *cds);
+                libxl__checkpoint_devices_state *cds);
 _hidden void libxl__checkpoint_devices_postsuspend(libxl__egc *egc,
-                                              libxl__checkpoint_devices_state *cds);
+                libxl__checkpoint_devices_state *cds);
 _hidden void libxl__checkpoint_devices_preresume(libxl__egc *egc,
-                                            libxl__checkpoint_devices_state *cds);
+                libxl__checkpoint_devices_state *cds);
 _hidden void libxl__checkpoint_devices_commit(libxl__egc *egc,
-                                         libxl__checkpoint_devices_state *cds);
+                libxl__checkpoint_devices_state *cds);
 _hidden int libxl__netbuffer_enabled(libxl__gc *gc);
 
 /*----- Domain suspend (save) state structure -----*/
tools/libxl/libxl_remus.c +8 -4
index a04c05e..ca205a7 100644
@@ -21,9 +21,11 @@
 
 /*----- remus: setup the environment -----*/
 static void libxl__remus_setup_done(libxl__egc *egc,
-                                    libxl__checkpoint_devices_state *cds, int rc);
+                                    libxl__checkpoint_devices_state *cds,
+                                    int rc);
 static void libxl__remus_setup_failed(libxl__egc *egc,
-                                      libxl__checkpoint_devices_state *cds, int rc);
+                                      libxl__checkpoint_devices_state *cds,
+                                      int rc);
 
 void libxl__remus_setup(libxl__egc *egc,
                         libxl__domain_suspend_state *dss)
@@ -58,7 +60,8 @@ out:
 }
 
 static void libxl__remus_setup_done(libxl__egc *egc,
-                                    libxl__checkpoint_devices_state *cds, int rc)
+                                    libxl__checkpoint_devices_state *cds,
+                                    int rc)
 {
     libxl__domain_suspend_state *dss = CONTAINER_OF(cds, *dss, cds);
     STATE_AO_GC(dss->ao);
@@ -220,7 +223,8 @@ out:
 
 /*----- remus: wait a new checkpoint -----*/
 static void remus_checkpoint_dm_saved(libxl__egc *egc,
-                                      libxl__domain_suspend_state *dss, int rc);
+                                      libxl__domain_suspend_state *dss,
+                                      int rc);
 static void remus_devices_commit_cb(libxl__egc *egc,
                                     libxl__checkpoint_devices_state *cds,
                                     int rc);
Signature · 1 line
1.9.3
#15 lore raw

RFC PATCH v2 15/45 don't touch remus in checkpoint_device

In reply to Wen Congyang

Checkpoint device is an abstract layer to do checkpoint. COLO can also use it to do checkpoint. But there is still some codes in checkpoint device which touch remus:
1. remus_ops: we use remus ops directly in checkpoint device. Store it in checkpoint device state.
2. concrete layer's private member: add a new structure remus state, and move them to remus state.

---
 tools/libxl/libxl.c                   |  2 +-
 tools/libxl/libxl_checkpoint_device.c | 14 +++-------
 tools/libxl/libxl_dom.c               |  3 +--
 tools/libxl/libxl_internal.h          | 37 ++++++++++++++++---------
 tools/libxl/libxl_netbuffer.c         | 51 ++++++++++++++++++++---------------
 tools/libxl/libxl_remus.c             | 50 +++++++++++++++++++++-------------
 tools/libxl/libxl_remus.h             |  5 ++--
 tools/libxl/libxl_remus_disk_drbd.c   |  9 ++++---
 8 files changed, 97 insertions(+), 74 deletions(-)
tools/libxl/libxl.c +1 -1
index ff93af3..9a8fd16 100644
@@ -820,7 +820,7 @@ int libxl_domain_remus_start(libxl_ctx *ctx, libxl_domain_remus_info *info,
     assert(info);
 
     /* Point of no return */
-    libxl__remus_setup(egc, dss);
+    libxl__remus_setup(egc, &dss->rs);
     return AO_INPROGRESS;
 
  out:
tools/libxl/libxl_checkpoint_device.c +3 -11
index a57b9e4..f73db0e 100644
@@ -17,14 +17,6 @@
 
 #include "libxl_internal.h"
 
-extern const libxl__checkpoint_device_subkind_ops remus_device_nic;
-extern const libxl__checkpoint_device_subkind_ops remus_device_drbd_disk;
-static const libxl__checkpoint_device_subkind_ops *remus_ops[] = {
-    &remus_device_nic,
-    &remus_device_drbd_disk,
-    NULL,
-};
-
 /*----- helper functions -----*/
 
 static int init_device_subkind(libxl__checkpoint_devices_state *cds)
@@ -32,7 +24,7 @@ static int init_device_subkind(libxl__checkpoint_devices_state *cds)
     int rc;
     const libxl__checkpoint_device_subkind_ops **ops;
 
-    for (ops = remus_ops; *ops; ops++) {
+    for (ops = cds->ops; *ops; ops++) {
         rc = (*ops)->init(cds);
         if (rc)
             goto out;
@@ -48,7 +40,7 @@ static void cleanup_device_subkind(libxl__checkpoint_devices_state *cds)
 {
     const libxl__checkpoint_device_subkind_ops **ops;
 
-    for (ops = remus_ops; *ops; ops++)
+    for (ops = cds->ops; *ops; ops++)
         (*ops)->cleanup(cds);
 }
 
@@ -148,7 +140,7 @@ static void checkpoint_devices_setup(libxl__egc *egc,
 
         /* find avaliable ops */
         do {
-            dev->ops = remus_ops[++dev->ops_index];
+            dev->ops = cds->ops[++dev->ops_index];
             if (!dev->ops) {
                 rc = ERROR_CHECKPOINT_DEVICE_NOT_SUPPORTED;
                 goto out;
tools/libxl/libxl_dom.c +1 -2
index f819846..4e71ec5 100644
@@ -1605,7 +1605,6 @@ void libxl__domain_suspend(libxl__egc *egc, libxl__domain_suspend_state *dss)
     dss2->save_dm = 1;
 
     if (r_info != NULL) {
-        dss->interval = r_info->interval;
         if (r_info->compression)
             dss->xcflags |= XCFLAGS_CHECKPOINT_COMPRESS;
     }
@@ -1787,7 +1786,7 @@ static void domain_suspend_done(libxl__egc *egc,
                            dss2->guest_evtchn.port, &dss2->guest_evtchn_lockfd);
 
     if (dss->remus) {
-        libxl__remus_teardown(egc, dss, rc);
+        libxl__remus_teardown(egc, &dss->rs, rc);
         return;
     }
 
tools/libxl/libxl_internal.h +24 -13
index 766868c..4d37cb4 100644
@@ -2627,6 +2627,8 @@ struct libxl__checkpoint_devices_state {
     uint32_t domid;
     libxl__checkpoint_callback *callback;
     int device_kind_flags;
+    /* The ops must be pointer array, and the last ops must be NULL */
+    const libxl__checkpoint_device_subkind_ops **ops;
 
     /*----- private for abstract layer only -----*/
 
@@ -2645,16 +2647,6 @@ struct libxl__checkpoint_devices_state {
     int num_disks;
 
     libxl__multidev multidev;
-
-    /*----- private for concrete (device-specific) layer only -----*/
-
-    /* private for nic device subkind ops */
-    char *netbufscript;
-    struct nl_sock *nlsock;
-    struct nl_cache *qdisc_cache;
-
-    /* private for drbd disk subkind ops */
-    char *drbd_probe_script;
 };
 
 /*
@@ -2704,6 +2696,27 @@ _hidden void libxl__checkpoint_devices_preresume(libxl__egc *egc,
                 libxl__checkpoint_devices_state *cds);
 _hidden void libxl__checkpoint_devices_commit(libxl__egc *egc,
                 libxl__checkpoint_devices_state *cds);
+
+/*----- Remus related state structure -----*/
+typedef struct libxl__remus_state libxl__remus_state;
+struct libxl__remus_state {
+    /* private */
+    libxl__ev_time checkpoint_timeout; /* used for Remus checkpoint */
+    int interval; /* checkpoint interval */
+
+    /* abstract layer */
+    libxl__checkpoint_devices_state cds;
+
+    /*----- private for concrete (device-specific) layer only -----*/
+    /* private for nic device subkind ops */
+    char *netbufscript;
+    struct nl_sock *nlsock;
+    struct nl_cache *qdisc_cache;
+
+    /* private for drbd disk subkind ops */
+    char *drbd_probe_script;
+};
+
 _hidden int libxl__netbuffer_enabled(libxl__gc *gc);
 
 /*----- Domain suspend (save) state structure -----*/
@@ -2767,9 +2780,7 @@ struct libxl__domain_suspend_state {
     libxl__domain_suspend_state2 dss2;
     int hvm;
     int xcflags;
-    libxl__checkpoint_devices_state cds;
-    libxl__ev_time checkpoint_timeout; /* used for Remus checkpoint */
-    int interval; /* checkpoint interval (for Remus) */
+    libxl__remus_state rs;
     libxl__save_helper_state shs;
     libxl__logdirty_switch logdirty;
     /* private for libxl__domain_save_device_model */
tools/libxl/libxl_netbuffer.c +29 -22

148 lines not on this page · raw message

tools/libxl/libxl_remus.c +31 -19

156 lines not on this page · raw message

tools/libxl/libxl_remus.h +2 -3
index 53e5e81..15bbbe8 100644
@@ -16,10 +16,9 @@
 #ifndef LIBXL_REMUS_H
 #define LIBXL_REMUS_H
 
-void libxl__remus_setup(libxl__egc *egc,
-                        libxl__domain_suspend_state *dss);
+void libxl__remus_setup(libxl__egc *egc, libxl__remus_state *rs);
 void libxl__remus_teardown(libxl__egc *egc,
-                           libxl__domain_suspend_state *dss,
+                           libxl__remus_state *rs,
                            int rc);
 void libxl__remus_domain_suspend_callback(void *data);
 void libxl__remus_domain_resume_callback(void *data);
tools/libxl/libxl_remus_disk_drbd.c +6 -3
index 5928187..89465b6 100644
@@ -60,10 +60,12 @@ out:
 /*----- init() and cleanup() -----*/
 static int drbd_init(libxl__checkpoint_devices_state *cds)
 {
+    libxl__remus_state *rs = CONTAINER_OF(cds, *rs, cds);
+
     STATE_AO_GC(cds->ao);
 
-    cds->drbd_probe_script = GCSPRINTF("%s/block-drbd-probe",
-                                       libxl__xen_script_dir_path());
+    rs->drbd_probe_script = GCSPRINTF("%s/block-drbd-probe",
+                                      libxl__xen_script_dir_path());
 
     return 0;
 }
@@ -96,6 +98,7 @@ static void match_async_exec(libxl__egc *egc, libxl__checkpoint_device *dev)
     int arraysize, nr = 0, rc;
     const libxl_device_disk *disk = dev->backend_dev;
     libxl__async_exec_state *aes = &dev->aodev.aes;
+    libxl__remus_state *rs = CONTAINER_OF(dev->cds, *rs, cds);
     STATE_AO_GC(dev->cds->ao);
 
     /* setup env & args */
@@ -107,7 +110,7 @@ static void match_async_exec(libxl__egc *egc, libxl__checkpoint_device *dev)
     arraysize = 3;
     nr = 0;
     GCNEW_ARRAY(aes->args, arraysize);
-    aes->args[nr++] = dev->cds->drbd_probe_script;
+    aes->args[nr++] = rs->drbd_probe_script;
     aes->args[nr++] = disk->pdev_path;
     aes->args[nr++] = NULL;
     assert(nr <= arraysize);
Signature · 1 line
1.9.3
#16 lore raw

RFC PATCH v2 16/45 Update libxl_save_msgs_gen.pl to support return data from xl to xc

In reply to Wen Congyang

Currently, all callbacks return an integer value or void. We cannot return some data to xc via callback. Update libxl_save_msgs_gen.pl to support this case.

---
 tools/libxl/libxl_internal.h       |  3 ++
 tools/libxl/libxl_save_callout.c   | 31 ++++++++++++++++++
 tools/libxl/libxl_save_helper.c    | 17 ++++++++++
 tools/libxl/libxl_save_msgs_gen.pl | 65 ++++++++++++++++++++++++++++++++++----
 4 files changed, 109 insertions(+), 7 deletions(-)
tools/libxl/libxl_internal.h +3 -0
index 4d37cb4..e3a8947 100644
@@ -3076,6 +3076,9 @@ _hidden void libxl__xc_domain_save_done(libxl__egc*, void *dss_void,
  * When they are ready to indicate completion, they call this. */
 void libxl__xc_domain_saverestore_async_callback_done(libxl__egc *egc,
                            libxl__save_helper_state *shs, int return_value);
+void libxl__xc_domain_saverestore_async_callback_done_with_data(libxl__egc *egc,
+                           libxl__save_helper_state *shs,
+                           const void *data, uint64_t size);
 
 
 _hidden void libxl__domain_suspend_common_switch_qemu_logdirty
tools/libxl/libxl_save_callout.c +31 -0
index 1c9f806..0c09d94 100644
@@ -145,6 +145,15 @@ void libxl__xc_domain_saverestore_async_callback_done(libxl__egc *egc,
     shs->egc = 0;
 }
 
+void libxl__xc_domain_saverestore_async_callback_done_with_data(libxl__egc *egc,
+                           libxl__save_helper_state *shs,
+                           const void *data, uint64_t size)
+{
+    shs->egc = egc;
+    libxl__srm_callout_sendreply_data(data, size, shs);
+    shs->egc = 0;
+}
+
 /*----- helper execution -----*/
 
 static void run_helper(libxl__egc *egc, libxl__save_helper_state *shs,
@@ -370,6 +379,28 @@ void libxl__srm_callout_sendreply(int r, void *user)
         helper_failed(egc, shs, ERROR_FAIL);
 }
 
+void libxl__srm_callout_sendreply_data(const void *data, uint64_t size, void *user)
+{
+    libxl__save_helper_state *shs = user;
+    libxl__egc *egc = shs->egc;
+    STATE_AO_GC(shs->ao);
+    int errnoval;
+
+    errnoval = libxl_write_exactly(CTX, libxl__carefd_fd(shs->pipes[0]),
+                                   &size, sizeof(size), shs->stdin_what,
+                                   "callback return data length");
+    if (errnoval)
+        goto out;
+
+    errnoval = libxl_write_exactly(CTX, libxl__carefd_fd(shs->pipes[0]),
+                                   data, size, shs->stdin_what,
+                                   "callback return data");
+
+out:
+    if (errnoval)
+        helper_failed(egc, shs, ERROR_FAIL);
+}
+
 void libxl__srm_callout_callback_log(uint32_t level, uint32_t errnoval,
                   const char *context, const char *formatted, void *user)
 {
tools/libxl/libxl_save_helper.c +17 -0
index 74826a1..44c5807 100644
@@ -155,6 +155,23 @@ int helper_getreply(void *user)
     return v;
 }
 
+uint8_t *helper_getreply_data(void *user)
+{
+    uint64_t size;
+    int r = read_exactly(0, &size, sizeof(size));
+    uint8_t *data;
+
+    if (r <= 0)
+        exit(-2);
+
+    data = helper_allocbuf(size, user);
+    r = read_exactly(0, data, size);
+    if (r <= 0)
+        exit(-2);
+
+    return data;
+}
+
 /*----- other callbacks -----*/
 
 static int toolstack_save_fd;
tools/libxl/libxl_save_msgs_gen.pl +58 -7

135 lines not on this page · raw message

Signature · 1 line
1.9.3
#17 lore raw

RFC PATCH v2 17/45 Allow slave sends data to master

In reply to Wen Congyang

In colo mode, slave needs to send data to master, but the io_fd only can be written in master, and only can be read in slave. Save recv_fd in domain_suspend_state, and send_fd in domain_create_state.

---
 tools/libxl/libxl.c          |  2 +-
 tools/libxl/libxl.h          |  3 ++-
 tools/libxl/libxl_create.c   | 14 ++++++++++----
 tools/libxl/libxl_internal.h |  2 ++
 tools/libxl/libxl_types.idl  |  7 +++++++
 tools/libxl/xl_cmdimpl.c     |  7 +++++++
 6 files changed, 29 insertions(+), 6 deletions(-)
tools/libxl/libxl.c +1 -1
index 9a8fd16..f35029a 100644
@@ -811,7 +811,7 @@ int libxl_domain_remus_start(libxl_ctx *ctx, libxl_domain_remus_info *info,
     dss->callback = remus_failover_cb;
     dss->domid = domid;
     dss->fd = send_fd;
-    /* TODO do something with recv_fd */
+    dss->recv_fd = recv_fd;
     dss->type = type;
     dss->live = 1;
     dss->debug = 0;
tools/libxl/libxl.h +2 -1
index 0495db7..c9f6ec0 100644
@@ -812,7 +812,8 @@ int static inline libxl_domain_create_restore_0x040200(
     LIBXL_EXTERNAL_CALLERS_ONLY
 {
     libxl_domain_restore_params params;
-    params.checkpointed_stream = 0;
+    params.checkpointed_stream = LIBXL_CHECKPOINTED_STREAM_NONE;
+    params.send_fd = -1;
 
     return libxl_domain_create_restore(
         ctx, d_config, domid, restore_fd, &params, ao_how, aop_console_how);
tools/libxl/libxl_create.c +10 -4
index 9b66294..e29a107 100644
@@ -1398,8 +1398,8 @@ static void domain_create_cb(libxl__egc *egc,
                              int rc, uint32_t domid);
 
 static int do_domain_create(libxl_ctx *ctx, libxl_domain_config *d_config,
-                            uint32_t *domid,
-                            int restore_fd, int checkpointed_stream,
+                            uint32_t *domid, int restore_fd,
+                            int send_fd, int checkpointed_stream,
                             const libxl_asyncop_how *ao_how,
                             const libxl_asyncprogress_how *aop_console_how)
 {
@@ -1410,6 +1410,7 @@ static int do_domain_create(libxl_ctx *ctx, libxl_domain_config *d_config,
     cdcs->dcs.ao = ao;
     cdcs->dcs.guest_config = d_config;
     cdcs->dcs.restore_fd = restore_fd;
+    cdcs->dcs.send_fd = send_fd;
     cdcs->dcs.callback = domain_create_cb;
     cdcs->dcs.checkpointed_stream = checkpointed_stream;
     libxl__ao_progress_gethow(&cdcs->dcs.aop_console_how, aop_console_how);
@@ -1438,7 +1439,7 @@ int libxl_domain_create_new(libxl_ctx *ctx, libxl_domain_config *d_config,
                             const libxl_asyncop_how *ao_how,
                             const libxl_asyncprogress_how *aop_console_how)
 {
-    return do_domain_create(ctx, d_config, domid, -1, 0,
+    return do_domain_create(ctx, d_config, domid, -1, -1, 0,
                             ao_how, aop_console_how);
 }
 
@@ -1448,7 +1449,12 @@ int libxl_domain_create_restore(libxl_ctx *ctx, libxl_domain_config *d_config,
                                 const libxl_asyncop_how *ao_how,
                                 const libxl_asyncprogress_how *aop_console_how)
 {
-    return do_domain_create(ctx, d_config, domid, restore_fd,
+    int send_fd = -1;
+
+    if (params->checkpointed_stream == LIBXL_CHECKPOINTED_STREAM_COLO)
+        send_fd = params->send_fd;
+
+    return do_domain_create(ctx, d_config, domid, restore_fd, send_fd,
                             params->checkpointed_stream, ao_how, aop_console_how);
 }
 
tools/libxl/libxl_internal.h +2 -0
index e3a8947..b04e4b9 100644
@@ -2772,6 +2772,7 @@ struct libxl__domain_suspend_state {
 
     uint32_t domid;
     int fd;
+    int recv_fd;
     libxl_domain_type type;
     int live;
     int debug;
@@ -3036,6 +3037,7 @@ struct libxl__domain_create_state {
     libxl__ao *ao;
     libxl_domain_config *guest_config;
     int restore_fd;
+    int send_fd;
     libxl__domain_create_cb *callback;
     libxl_asyncprogress_how aop_console_how;
     /* private to domain_create */
tools/libxl/libxl_types.idl +7 -0
index 1945155..ea51d1a 100644
@@ -177,6 +177,12 @@ libxl_vendor_device = Enumeration("vendor_device", [
     (0, "NONE"),
     (1, "XENSERVER"),
     ])
+
+libxl_checkpointed_stream = Enumeration("checkpointed_stream", [
+    (0, "NONE"),
+    (1, "REMUS"),
+    (2, "COLO"),
+    ], init_val = 0)
 #
 # Complex libxl types
 #
@@ -303,6 +309,7 @@ libxl_domain_create_info = Struct("domain_create_info",[
 
 libxl_domain_restore_params = Struct("domain_restore_params", [
     ("checkpointed_stream", integer),
+    ("send_fd", integer),
     ])
 
 libxl_domain_sched_params = Struct("domain_sched_params",[
tools/libxl/xl_cmdimpl.c +7 -0
index b859f31..ccb46ab 100644
@@ -151,6 +151,7 @@ struct domain_create {
     const char *extra_config; /* extra config string */
     const char *restore_file;
     int migrate_fd; /* -1 means none */
+    int send_fd; /* -1 means none */
     char **migration_domname_r; /* from malloc */
 };
 
@@ -2033,6 +2034,7 @@ static uint32_t create_domain(struct domain_create *dom_info)
     void *config_data = 0;
     int config_len = 0;
     int restore_fd = -1;
+    int send_fd = -1;
     const libxl_asyncprogress_how *autoconnect_console_how;
     struct save_file_header hdr;
 
@@ -2049,6 +2051,7 @@ static uint32_t create_domain(struct domain_create *dom_info)
         if (migrate_fd >= 0) {
             restore_source = "<incoming migration stream>";
             restore_fd = migrate_fd;
+            send_fd = dom_info->send_fd;
         } else {
             restore_source = restore_file;
             restore_fd = open(restore_file, O_RDONLY);
@@ -2210,6 +2213,7 @@ start:
     if ( restoring ) {
         libxl_domain_restore_params params;
         params.checkpointed_stream = dom_info->checkpointed_stream;
+        params.send_fd = send_fd;
         ret = libxl_domain_create_restore(ctx, &d_config,
                                           &domid, restore_fd,
                                           &params,
@@ -3747,6 +3751,7 @@ static void migrate_receive(int debug, int daemonize, int monitor,
     dom_info.monitor = monitor;
     dom_info.paused = 1;
     dom_info.migrate_fd = recv_fd;
+    dom_info.send_fd = send_fd;
     dom_info.migration_domname_r = &migration_domname;
     dom_info.checkpointed_stream = remus;
 
@@ -3917,6 +3922,7 @@ int main_restore(int argc, char **argv)
     dom_info.config_file = config_file;
     dom_info.restore_file = checkpoint_file;
     dom_info.migrate_fd = -1;
+    dom_info.send_fd = -1;
     dom_info.vnc = vnc;
     dom_info.vncautopass = vncautopass;
     dom_info.console_autoconnect = console_autoconnect;
@@ -4356,6 +4362,7 @@ int main_create(int argc, char **argv)
     dom_info.config_file = filename;
     dom_info.extra_config = extra_config;
     dom_info.migrate_fd = -1;
+    dom_info.send_fd = -1;
     dom_info.vnc = vnc;
     dom_info.vncautopass = vncautopass;
     dom_info.console_autoconnect = console_autoconnect;
Signature · 1 line
1.9.3
#18 lore raw

RFC PATCH v2 18/45 secondary vm suspend/resume/checkpoint code

In reply to Wen Congyang

Secondary vm is running in colo mode. So we will do
the following things again and again:
1. Resume secondary vm
a. Send LIBXL_COLO_SVM_READY to master
b. If it is resumed the first time, call libxl__xc_domain_restore_done()
to build the secondary vm. We should also enable secondary vm's logdirty. Otherwise, call libxl__domain_resume() to resume secondary vm.
c. Send LIBXL_COLO_SVM_RESUMED to master
2. Wait a new checkpoint
a. Read LIBXL_COLO_NEW_CHECKPOINT from master
3. Suspend secondary vm
a. Suspend secondary vm
b. Get secondary vm's dirty page information
c. Send LIBXL_COLO_SVM_SUSPENDED to master
d. Send secondary vm's dirty page information to master(count + pfn list)

---
 tools/libxc/xenguest.h             |  20 +
 tools/libxl/Makefile               |   1 +
 tools/libxl/libxl_colo.h           |  38 ++
 tools/libxl/libxl_colo_restore.c   | 883 +++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_create.c         | 116 ++++-
 tools/libxl/libxl_dom.c            |   2 +-
 tools/libxl/libxl_internal.h       |  22 +
 tools/libxl/libxl_save_callout.c   |   6 +-
 tools/libxl/libxl_save_msgs_gen.pl |   6 +-
 9 files changed, 1087 insertions(+), 7 deletions(-)
 create mode 100644 tools/libxl/libxl_colo.h
 create mode 100644 tools/libxl/libxl_colo_restore.c
tools/libxc/xenguest.h +20 -0
index 40bbac8..d3061c7 100644
@@ -91,6 +91,26 @@ int xc_domain_save(xc_interface *xch, int io_fd, uint32_t dom, uint32_t max_iter
 
 /* callbacks provided by xc_domain_restore */
 struct restore_callbacks {
+    /* Called after a new checkpoint to suspend the guest.
+     */
+    int (*suspend)(void* data);
+
+    /* Called after the secondary vm is ready to resume.
+     * Callback function resumes the guest & the device model,
+     *  returns to xc_domain_restore.
+     */
+    int (*postcopy)(void* data);
+
+    /* callback to wait a new checkpoint
+     *
+     * returns:
+     * 0: terminate checkpointing gracefully
+     * 1: take another checkpoint */
+    int (*checkpoint)(void* data);
+
+    /* Enable qemu-dm logging dirty pages to xen */
+    int (*switch_qemu_logdirty)(int domid, unsigned enable, void *data); /* HVM only */
+
     /* callback to restore toolstack specific data */
     int (*toolstack_restore)(uint32_t domid, const uint8_t *buf,
             uint32_t size, void* data);
tools/libxl/Makefile +1 -0
index 5427461..c026bdd 100644
@@ -57,6 +57,7 @@ LIBXL_OBJS-y += libxl_nonetbuffer.o
 endif
 
 LIBXL_OBJS-y += libxl_remus.o libxl_checkpoint_device.o libxl_remus_disk_drbd.o
+LIBXL_OBJS-y += libxl_colo_restore.o
 
 LIBXL_OBJS-$(CONFIG_X86) += libxl_cpuid.o libxl_x86.o
 LIBXL_OBJS-$(CONFIG_ARM) += libxl_nocpuid.o libxl_arm.o
tools/libxl/libxl_colo.h new file +38 -0
new file mode 100644
index 0000000..91df275
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2014 FUJITSU LIMITED
+ * Author: Wen Congyang <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; version 2.1 only. with the special
+ * exception on linking described in file LICENSE.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ */
+
+#ifndef LIBXL_COLO_H
+#define LIBXL_COLO_H
+
+/*
+ * values to control suspend/resume primary vm and secondary vm
+ * at the same time
+ */
+enum {
+    LIBXL_COLO_NEW_CHECKPOINT = 1,
+    LIBXL_COLO_SVM_SUSPENDED,
+    LIBXL_COLO_SVM_READY,
+    LIBXL_COLO_SVM_RESUMED,
+};
+
+extern void libxl__colo_restore_done(libxl__egc *egc, void *dcs_void,
+                                     int ret, int retval, int errnoval);
+extern void libxl__colo_restore_setup(libxl__egc *egc,
+                                      libxl__colo_restore_state *crs);
+extern void libxl__colo_restore_teardown(libxl__egc *egc,
+                                         libxl__colo_restore_state *crs,
+                                         int rc);
+
+#endif
tools/libxl/libxl_colo_restore.c new file +883 -0

883 lines not on this page · raw message

tools/libxl/libxl_create.c +114 -2

146 lines not on this page · raw message

tools/libxl/libxl_dom.c +1 -1
index 4e71ec5..769952c 100644
@@ -862,7 +862,7 @@ static void switch_logdirty_xswatch(libxl__egc *egc, libxl__ev_xswatch*,
 static void switch_logdirty_done(libxl__egc *egc,
                                  libxl__logdirty_switch *lds, int ok);
 
-static void logdirty_init(libxl__logdirty_switch *lds)
+void logdirty_init(libxl__logdirty_switch *lds)
 {
     lds->cmd_path = 0;
     libxl__ev_xswatch_init(&lds->watch);
tools/libxl/libxl_internal.h +22 -0
index b04e4b9..d2e3176 100644
@@ -2741,6 +2741,7 @@ struct libxl__logdirty_switch {
     libxl__ev_xswatch watch;
     libxl__ev_time timeout;
 };
+_hidden void logdirty_init(libxl__logdirty_switch *lds);
 
 /*
  * libxl__domain_suspend_state is for saving guest, not
@@ -3032,6 +3033,26 @@ typedef void libxl__domain_create_cb(libxl__egc *egc,
                                      libxl__domain_create_state*,
                                      int rc, uint32_t domid);
 
+/* colo related structure */
+typedef struct libxl__colo_restore_state libxl__colo_restore_state;
+typedef void libxl__colo_callback(libxl__egc *,
+                                  libxl__colo_restore_state *, int rc);
+struct libxl__colo_restore_state {
+    /* must set by caller of libxl__colo_(setup|teardown) */
+    libxl__ao *ao;
+    uint32_t domid;
+    int send_fd;
+    int recv_fd;
+    int hvm;
+    int pae;
+    int superpages;
+    libxl__colo_callback *callback;
+
+    /* private, colo restore checkpoint state */
+    libxl__domain_create_cb *saved_cb;
+    void *crcs;
+};
+
 struct libxl__domain_create_state {
     /* filled in by user */
     libxl__ao *ao;
@@ -3044,6 +3065,7 @@ struct libxl__domain_create_state {
     int guest_domid;
     int checkpointed_stream;
     libxl__domain_build_state build_state;
+    libxl__colo_restore_state crs;
     libxl__bootloader_state bl;
     libxl__stub_dm_spawn_state dmss;
         /* If we're not doing stubdom, we use only dmss.dm,
tools/libxl/libxl_save_callout.c +5 -1
index 0c09d94..e251181 100644
@@ -15,6 +15,7 @@
 #include "libxl_osdeps.h"
 
 #include "libxl_internal.h"
+#include "libxl_colo.h"
 
 /* stream_fd is as from the caller (eventually, the application).
  * It may be 0, 1 or 2, in which case we need to dup it elsewhere.
@@ -65,7 +66,10 @@ void libxl__xc_domain_restore(libxl__egc *egc, libxl__domain_create_state *dcs,
     dcs->shs.ao = ao;
     dcs->shs.domid = domid;
     dcs->shs.recv_callback = libxl__srm_callout_received_restore;
-    dcs->shs.completion_callback = libxl__xc_domain_restore_done;
+    if (dcs->checkpointed_stream == LIBXL_CHECKPOINTED_STREAM_COLO)
+        dcs->shs.completion_callback = libxl__colo_restore_done;
+    else
+        dcs->shs.completion_callback = libxl__xc_domain_restore_done;
     dcs->shs.caller_state = dcs;
     dcs->shs.need_results = 1;
     dcs->shs.toolstack_data_file = 0;
tools/libxl/libxl_save_msgs_gen.pl +3 -3
index 41ee000..0239cac 100755
@@ -24,9 +24,9 @@ our @msgs = (
                                                  STRING doing_what),
                                                 'unsigned long', 'done',
                                                 'unsigned long', 'total'] ],
-    [  3, 'scxA',   "suspend", [] ],
-    [  4, 'scxA',   "postcopy", [] ],
-    [  5, 'scxA',   "checkpoint", [] ],
+    [  3, 'srcxA',   "suspend", [] ],
+    [  4, 'srcxA',   "postcopy", [] ],
+    [  5, 'srcxA',   "checkpoint", [] ],
     [  6, 'scxA',   "switch_qemu_logdirty",  [qw(int domid
                                               unsigned enable)] ],
     #                toolstack_save          done entirely `by hand'
Signature · 1 line
1.9.3
#19 lore raw

RFC PATCH v2 19/45 primary vm suspend/get_dirty_pfn/resume/checkpoint code

In reply to Wen Congyang

We will do the following things again and again:
1. Suspend primary vm
a. Suspend primary vm
b. do postsuspend
c. Read LIBXL_COLO_SVM_SUSPENDED to master
d. Read secondary vm's dirty page information to master(count + pfn list)
2. Get dirty pfn list
a. Return secondary vm's dirty pfn list
3. Resume primary vm
a. Read LIBXL_COLO_SVM_READY from slave
b. Do presume
c. Resume primary vm
d. Read LIBXL_COLO_SVM_RESUMED from slave
4. Wait a new checkpoint
a. Wait a new checkpoint(not implemented)
b. Send LIBXL_COLO_NEW_CHECKPOINT to slave

---
 tools/libxc/xenguest.h             |  12 +
 tools/libxl/Makefile               |   2 +-
 tools/libxl/libxl.c                |   6 +-
 tools/libxl/libxl_colo.h           |  10 +
 tools/libxl/libxl_colo_save.c      | 608 +++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_dom.c            |  13 +-
 tools/libxl/libxl_internal.h       |  32 +-
 tools/libxl/libxl_save_msgs_gen.pl |   1 +
 tools/libxl/libxl_types.idl        |   1 +
 9 files changed, 677 insertions(+), 8 deletions(-)
 create mode 100644 tools/libxl/libxl_colo_save.c
tools/libxc/xenguest.h +12 -0
index d3061c7..1aeaad2 100644
@@ -72,6 +72,18 @@ struct save_callbacks {
      */
     int (*toolstack_save)(uint32_t domid, uint8_t **buf, uint32_t *len, void *data);
 
+    /* Called after the guest is suspended.
+     *
+     * returns the list of dirty pfn:
+     *  struct {
+     *      uint64_t count;
+     *      uint64_t pfn[];
+     *  };
+     *
+     *  Note: the caller must free the return value.
+     */
+    uint8_t *(*get_dirty_pfn)(void *data);
+
     /* to be provided as the last argument to each callback function */
     void* data;
 };
tools/libxl/Makefile +1 -1
index c026bdd..1c32ae2 100644
@@ -57,7 +57,7 @@ LIBXL_OBJS-y += libxl_nonetbuffer.o
 endif
 
 LIBXL_OBJS-y += libxl_remus.o libxl_checkpoint_device.o libxl_remus_disk_drbd.o
-LIBXL_OBJS-y += libxl_colo_restore.o
+LIBXL_OBJS-y += libxl_colo_restore.o libxl_colo_save.o
 
 LIBXL_OBJS-$(CONFIG_X86) += libxl_cpuid.o libxl_x86.o
 LIBXL_OBJS-$(CONFIG_ARM) += libxl_nocpuid.o libxl_arm.o
tools/libxl/libxl.c +5 -1
index f35029a..e0817e8 100644
@@ -18,6 +18,7 @@
 
 #include "libxl_internal.h"
 #include "libxl_remus.h"
+#include "libxl_colo.h"
 
 #define PAGE_TO_MEMKB(pages) ((pages) * 4)
 #define BACKEND_STRING_SIZE 5
@@ -820,7 +821,10 @@ int libxl_domain_remus_start(libxl_ctx *ctx, libxl_domain_remus_info *info,
     assert(info);
 
     /* Point of no return */
-    libxl__remus_setup(egc, &dss->rs);
+    if (info->colo)
+        libxl__colo_save_setup(egc, &dss->css);
+    else
+        libxl__remus_setup(egc, &dss->rs);
     return AO_INPROGRESS;
 
  out:
tools/libxl/libxl_colo.h +10 -0
index 91df275..26a2563 100644
@@ -35,4 +35,14 @@ extern void libxl__colo_restore_teardown(libxl__egc *egc,
                                          libxl__colo_restore_state *crs,
                                          int rc);
 
+extern void libxl__colo_save_domain_suspend_callback(void *data);
+extern void libxl__colo_save_domain_resume_callback(void *data);
+extern void libxl__colo_save_domain_checkpoint_callback(void *data);
+extern void libxl__colo_save_get_dirty_pfn_callback(void *data);
+extern void libxl__colo_save_setup(libxl__egc *egc,
+                                   libxl__colo_save_state *css);
+extern void libxl__colo_save_teardown(libxl__egc *egc,
+                                      libxl__colo_save_state *css,
+                                      int rc);
+
 #endif
tools/libxl/libxl_colo_save.c new file +608 -0

608 lines not on this page · raw message

tools/libxl/libxl_dom.c +11 -2
index 769952c..450eb39 100644
@@ -20,6 +20,7 @@
 #include "libxl_internal.h"
 #include "libxl_arch.h"
 #include "libxl_remus.h"
+#include "libxl_colo.h"
 
 #include <xc_dom.h>
 #include <xen/hvm/hvm_info_table.h>
@@ -1624,7 +1625,12 @@ void libxl__domain_suspend(libxl__egc *egc, libxl__domain_suspend_state *dss)
     }
 
     memset(callbacks, 0, sizeof(*callbacks));
-    if (r_info != NULL) {
+    if (r_info != NULL && r_info->colo) {
+        callbacks->suspend = libxl__colo_save_domain_suspend_callback;
+        callbacks->postcopy = libxl__colo_save_domain_resume_callback;
+        callbacks->checkpoint = libxl__colo_save_domain_checkpoint_callback;
+        callbacks->get_dirty_pfn = libxl__colo_save_get_dirty_pfn_callback;
+    } else if (r_info != NULL) {
         callbacks->suspend = libxl__remus_domain_suspend_callback;
         callbacks->postcopy = libxl__remus_domain_resume_callback;
         callbacks->checkpoint = libxl__remus_domain_checkpoint_callback;
@@ -1785,7 +1791,10 @@ static void domain_suspend_done(libxl__egc *egc,
         xc_suspend_evtchn_release(CTX->xch, CTX->xce, domid,
                            dss2->guest_evtchn.port, &dss2->guest_evtchn_lockfd);
 
-    if (dss->remus) {
+    if (dss->remus && dss->remus->colo) {
+        libxl__colo_save_teardown(egc, &dss->css, rc);
+        return;
+    } else if (dss->remus) {
         libxl__remus_teardown(egc, &dss->rs, rc);
         return;
     }
tools/libxl/libxl_internal.h +28 -4
index d2e3176..aebc972 100644
@@ -2521,7 +2521,7 @@ typedef struct libxl__save_helper_state {
 /*
  * The abstract checkpoint device layer exposes a common
  * set of API to [external] libxl for manipulating devices attached to
- * a guest protected by Remus. The device layer also exposes a set of
+ * a guest protected by Remus/COLO. The device layer also exposes a set of
  * [internal] interfaces that every device type must implement.
  *
  * The following API are exposed to libxl:
@@ -2539,7 +2539,7 @@ typedef struct libxl__save_helper_state {
  *  +libxl__checkpoint_devices_commit
  *
  * Each device type needs to implement the interfaces specified in
- * the libxl__checkpoint_device_subkind_ops if it wishes to support Remus.
+ * the libxl__checkpoint_device_subkind_ops if it wishes to support Remus/COLO.
  *
  * The high-level control flow through the checkpoint device layer is shown
  * below:
@@ -2564,7 +2564,7 @@ typedef struct libxl__checkpoint_device_subkind_ops libxl__checkpoint_device_sub
 
 /*
  * Interfaces to be implemented by every device type that wishes to
- * support Remus. Functions must be implemented unless otherwise
+ * support Remus/COLO. Functions must be implemented unless otherwise
  * stated. Many of these functions are asynchronous. They call
  * dev->aodev.callback when done.  The actual implementations may be
  * synchronous and call dev->aodev.callback directly (as the last
@@ -2719,6 +2719,25 @@ struct libxl__remus_state {
 
 _hidden int libxl__netbuffer_enabled(libxl__gc *gc);
 
+/*----- colo related state structure -----*/
+typedef struct libxl__colo_save_state libxl__colo_save_state;
+struct libxl__colo_save_state {
+    libxl__checkpoint_devices_state cds;
+    int send_fd;
+    int recv_fd;
+
+    /* private */
+    libxl__datacopier_state dc;
+    libxl__datareader_state drs;
+    uint8_t section;
+    uint64_t count;
+    uint64_t *buff;
+    /* read section and count, and then store it in temp_buff */
+    uint8_t temp_buff[9];
+    void (*callback)(libxl__egc *, libxl__colo_save_state *);
+    bool svm_running;
+};
+
 /*----- Domain suspend (save) state structure -----*/
 
 typedef struct libxl__domain_suspend_state libxl__domain_suspend_state;
@@ -2782,7 +2801,12 @@ struct libxl__domain_suspend_state {
     libxl__domain_suspend_state2 dss2;
     int hvm;
     int xcflags;
-    libxl__remus_state rs;
+    union {
+        /* for Remus */
+        libxl__remus_state rs;
+        /* for COLO */
+        libxl__colo_save_state css;
+    };
     libxl__save_helper_state shs;
     libxl__logdirty_switch logdirty;
     /* private for libxl__domain_save_device_model */
tools/libxl/libxl_save_msgs_gen.pl +1 -0
index 0239cac..fbb2d67 100755
@@ -36,6 +36,7 @@ our @msgs = (
                                               'unsigned long', 'console_mfn'] ],
     [  9, 'srW',    "complete",              [qw(int retval
                                                  int errnoval)] ],
+    [ 10, 'scxAB',  "get_dirty_pfn", [] ],
 );
 
 #----------------------------------------
tools/libxl/libxl_types.idl +1 -0
index ea51d1a..599f137 100644
@@ -602,6 +602,7 @@ libxl_domain_remus_info = Struct("domain_remus_info",[
     ("netbuf",       bool),
     ("netbufscript", string),
     ("diskbuf",      bool),
+    ("colo",         bool)
     ])
 
 libxl_event_type = Enumeration("event_type", [
Signature · 1 line
1.9.3
#20 lore raw

RFC PATCH v2 20/45 xc_domain_save: flush cache before calling callbacks->postcopy() in colo mode

In reply to Wen Congyang

In colo mode, secondary vm is running. We will use the io_fd to ensure that both primary vm and secondary vm are resumed at the same time. So we should call postcopy later.

---
 tools/libxc/xc_domain_save.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
tools/libxc/xc_domain_save.c +14 -2
index 254fdb3..61caa47 100644
@@ -2078,10 +2078,15 @@ int xc_domain_save(xc_interface *xch, int io_fd, uint32_t dom, uint32_t max_iter
  out_rc:
     completed = 1;
 
-    if ( !rc && callbacks->postcopy )
+    /*
+     * COLO: secondary vm is running. We will use the io_fd to
+     * ensure that both primary vm and secondary vm are resumed
+     * at the same time. So we should call postcopy later.
+     */
+    if ( !rc && callbacks->postcopy && !callbacks->get_dirty_pfn )
         callbacks->postcopy(callbacks->data);
 
-    /* guest has been resumed. Now we can compress data
+    /* Remus: guest has been resumed. Now we can compress data
      * at our own pace.
      */
     if (!rc && compressing)
@@ -2109,6 +2114,13 @@ int xc_domain_save(xc_interface *xch, int io_fd, uint32_t dom, uint32_t max_iter
 
     discard_file_cache(xch, io_fd, 1 /* flush */);
 
+    /*
+     * COLO: send qemu device state and resume both
+     * primary vm and secondary vm now.
+     */
+    if ( !rc && callbacks->postcopy && callbacks->get_dirty_pfn )
+        callbacks->postcopy(callbacks->data);
+
     /* Enable compression now, finally */
     compressing = (flags & XCFLAGS_CHECKPOINT_COMPRESS);
 
Signature · 1 line
1.9.3
#21 lore raw

RFC PATCH v2 21/45 COLO: xc related codes

In reply to Wen Congyang

Save:
1. send XC_SAVE_ID_LAST_CHECKPOINT, so secondary vm can be resumed
2. call callbacks->get_dirty_pfn() after suspend primary vm if we are doing checkpoint.

Restore:
1. call the callbacks resume/checkpoint/suspend if secondary vm's status is the same as primary vm's status.
2. zero out tdata because we will use it zero out pagebuf.tdata.

---
 tools/libxc/xc_domain_restore.c | 44 ++++++++++++++++++++++++++++++++--
 tools/libxc/xc_domain_save.c    | 52 +++++++++++++++++++++++++++++++++++++++--
 2 files changed, 92 insertions(+), 4 deletions(-)
tools/libxc/xc_domain_restore.c +42 -2
index 2d6139c..fe188f4 100644
@@ -1454,7 +1454,7 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
     int nraces = 0;
 
     /* The new domain's shared-info frame number. */
-    unsigned long shared_info_frame;
+    unsigned long shared_info_frame = 0;
     unsigned char shared_info_page[PAGE_SIZE]; /* saved contents from file */
     shared_info_any_t *old_shared_info = 
         (shared_info_any_t *)shared_info_page;
@@ -1504,6 +1504,8 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
 
     DPRINTF("%s: starting restore of new domid %u", __func__, dom);
 
+    n = m = 0;
+
     pagebuf_init(&pagebuf);
     memset(&tailbuf, 0, sizeof(tailbuf));
     tailbuf.ishvm = hvm;
@@ -1629,7 +1631,6 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
      * We uncanonicalise page tables as we go.
      */
 
-    n = m = 0;
  loadpages:
     for ( ; ; )
     {
@@ -1793,6 +1794,7 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
         goto finish;
     }
 
+new_checkpoint:
     // DPRINTF("Buffered checkpoint\n");
 
     if ( pagebuf_get(xch, ctx, &pagebuf, io_fd, dom) ) {
@@ -2292,6 +2294,7 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
             free(tdata.data);
             goto out;
         }
+        memset(&tdata, 0, sizeof(tdata));
     }
 
     /* Dump the QEMU state to a state file for QEMU to load */
@@ -2357,6 +2360,43 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
     rc = 0;
 
  out:
+    if ( !rc && callbacks->checkpoint )
+    {
+#define HANDLE_CALLBACK_RETURN_VALUE(frc)                   \
+    do {                                                    \
+        if ( frc == 0 )                                     \
+        {                                                   \
+            /* Some internal error happens */               \
+            rc = 1;                                         \
+            goto out;                                       \
+        }                                                   \
+        else if ( frc == 2 )                                \
+        {                                                   \
+            /* Reading/writing error, do failover */        \
+            rc = 0;                                         \
+            goto failover;                                  \
+        }                                                   \
+    } while (0)
+        /* COLO */
+
+        /* TODO: call restore_results */
+
+        /* Resume secondary vm */
+        frc = callbacks->postcopy(callbacks->data);
+        HANDLE_CALLBACK_RETURN_VALUE(frc);
+
+        /* wait for new checkpoint */
+        frc = callbacks->checkpoint(callbacks->data);
+        HANDLE_CALLBACK_RETURN_VALUE(frc);
+
+        /* suspend secondary vm */
+        frc = callbacks->suspend(callbacks->data);
+        HANDLE_CALLBACK_RETURN_VALUE(frc);
+
+        goto new_checkpoint;
+    }
+
+failover:
     if ( (rc != 0) && (dom != 0) )
         xc_domain_destroy(xch, dom);
     xc_hypercall_buffer_free(xch, ctxt);
tools/libxc/xc_domain_save.c +50 -2

88 lines not on this page · raw message

Signature · 1 line
1.9.3
#22 lore raw

RFC PATCH v2 22/45 send store mfn and console mfn to xl before resuming secondary vm

In reply to Wen Congyang

We will call libxl__xc_domain_restore_done() to rebuild secondary vm. But we need store mfn and console mfn when rebuilding secondary vm. So make restore_results is a function pointers in callbacks struct and struct {save,restore}_callbacks, and use this callback to send store mfn and console mfn to xl.

---
 tools/libxc/xc_domain_restore.c    | 2 +-
 tools/libxc/xenguest.h             | 8 ++++++++
 tools/libxl/libxl_colo_restore.c   | 5 -----
 tools/libxl/libxl_create.c         | 1 +
 tools/libxl/libxl_save_msgs_gen.pl | 2 +-
 5 files changed, 11 insertions(+), 7 deletions(-)
tools/libxc/xc_domain_restore.c +1 -1
index fe188f4..3700473 100644
@@ -2379,7 +2379,7 @@ new_checkpoint:
     } while (0)
         /* COLO */
 
-        /* TODO: call restore_results */
+        callbacks->restore_results(*store_mfn, *console_mfn, callbacks->data);
 
         /* Resume secondary vm */
         frc = callbacks->postcopy(callbacks->data);
tools/libxc/xenguest.h +8 -0
index 1aeaad2..be8afd4 100644
@@ -123,6 +123,14 @@ struct restore_callbacks {
     /* Enable qemu-dm logging dirty pages to xen */
     int (*switch_qemu_logdirty)(int domid, unsigned enable, void *data); /* HVM only */
 
+    /*
+     * callback to send store mfn and console mfn to xl
+     * if we want to resume vm before xc_domain_save()
+     * exits.
+     */
+    void (*restore_results)(unsigned long store_mfn, unsigned long console_mfn,
+                            void *data);
+
     /* callback to restore toolstack specific data */
     int (*toolstack_restore)(uint32_t domid, const uint8_t *buf,
             uint32_t size, void* data);
tools/libxl/libxl_colo_restore.c +0 -5
index ebbd6b9..aea3feb 100644
@@ -133,11 +133,6 @@ static void colo_resume_vm(libxl__egc *egc,
         return;
     }
 
-    /*
-     * TODO: get store mfn and console mfn
-     *  We should call the callback restore_results in
-     *  xc_domain_restore() before resuming the guest.
-     */
     libxl__xc_domain_restore_done(egc, dcs, 0, 0, 0);
 
     return;
tools/libxl/libxl_create.c +1 -0
index fef9b36..46bd02d 100644
@@ -1063,6 +1063,7 @@ static void domcreate_bootloader_done(libxl__egc *egc,
         rc = ERROR_INVAL;
         goto out;
     }
+    callbacks->restore_results = libxl__srm_callout_callback_restore_results;
 
     if (checkpointed_stream == LIBXL_CHECKPOINTED_STREAM_COLO) {
         crs->ao = ao;
tools/libxl/libxl_save_msgs_gen.pl +1 -1
index fbb2d67..2ecd25d 100755
@@ -32,7 +32,7 @@ our @msgs = (
     #                toolstack_save          done entirely `by hand'
     [  7, 'rcxW',   "toolstack_restore",     [qw(uint32_t domid
                                                 BLOCK tsdata)] ],
-    [  8, 'r',      "restore_results",       ['unsigned long', 'store_mfn',
+    [  8, 'rcx',    "restore_results",       ['unsigned long', 'store_mfn',
                                               'unsigned long', 'console_mfn'] ],
     [  9, 'srW',    "complete",              [qw(int retval
                                                  int errnoval)] ],
Signature · 1 line
1.9.3
#23 lore raw

RFC PATCH v2 23/45 implement the cmdline for COLO

In reply to Wen Congyang

Add a new option -c to the command 'xl remus'. If you want to use COLO HA instead of Remus HA, please use -c option.

Update man pages to reflect the addition of a new option to 'xl remus' command.

Also add a new option -c to the internal command 'xl migrate-receive'.

---
 docs/man/xl.pod.1         | 11 +++++++++--
 tools/libxl/xl_cmdimpl.c  | 47 ++++++++++++++++++++++++++++++++++++++---------
 tools/libxl/xl_cmdtable.c |  3 ++-
 3 files changed, 49 insertions(+), 12 deletions(-)
docs/man/xl.pod.1 +9 -2
index bce4bfe..297cd04 100644
@@ -427,12 +427,15 @@ Print huge (!) amount of debug during the migration process.
 
 =item B<remus> [I<OPTIONS>] I<domain-id> I<host>
 
-Enable Remus HA for domain. By default B<xl> relies on ssh as a transport
-mechanism between the two hosts.
+Enable Remus HA or COLO HA for domain. By default B<xl> relies on ssh as a
+transport mechanism between the two hosts.
 
 N.B: Remus support in xl is still in experimental (proof-of-concept) phase.
      Disk replication support is limited to DRBD disks.
 
+     COLO support in xl is still in experimental (proof-of-concept) phase.
+     There is no support for network or disk at the moment.
+
 B<OPTIONS>
 
 =over 4
@@ -478,6 +481,10 @@ Disable network output buffering. Requires enabling unsafe mode.
 
 Disable disk replication. Requires enabling unsafe mode.
 
+=item B<-c>
+
+Enable COLO HA. It is conflict with B<-i> and B<-b>.
+
 =back
 
 =item B<pause> I<domain-id>
tools/libxl/xl_cmdimpl.c +38 -9

130 lines not on this page · raw message

tools/libxl/xl_cmdtable.c +2 -1
index 6d4596b..22b63db 100644
@@ -498,7 +498,8 @@ struct cmd_spec cmd_table[] = {
       "-b                      Replicate memory checkpoints to /dev/null (blackhole).\n"
       "                        Works only in unsafe mode.\n"
       "-n                      Disable network output buffering. Works only in unsafe mode.\n"
-      "-d                      Disable disk replication. Works only in unsafe mode."
+      "-d                      Disable disk replication. Works only in unsafe mode.\n"
+      "-c                      Enable COLO HA. It is conflict with -i and -b"
     },
 #endif
     { "devd",
Signature · 1 line
1.9.3
#24 lore raw

RFC PATCH v2 24/45 HACK: do checkpoint per 20ms

In reply to Wen Congyang
---
 tools/libxl/libxl_colo_save.c | 19 ++++++++++++++++++-
 tools/libxl/libxl_internal.h  |  3 +++
 2 files changed, 21 insertions(+), 1 deletion(-)
tools/libxl/libxl_colo_save.c +18 -1
index 6675d3d..75d83c8 100644
@@ -55,6 +55,8 @@ void libxl__colo_save_setup(libxl__egc *egc, libxl__colo_save_state *css)
     cds->ao = ao;
     cds->domid = dss->domid;
 
+    libxl__ev_time_init(&css->timeout);
+
     libxl__checkpoint_devices_setup(egc, &css->cds);
 
     return;
@@ -473,6 +475,8 @@ out:
 static void colo_device_commit_cb(libxl__egc *egc,
                                   libxl__checkpoint_devices_state *cds,
                                   int rc);
+static void colo_next_checkpoint(libxl__egc *egc, libxl__ev_time *ev,
+                                  const struct timeval *requested_abs);
 static void colo_start_new_checkpoint(libxl__egc *egc,
                                       libxl__checkpoint_devices_state *cds,
                                       int rc);
@@ -508,13 +512,26 @@ static void colo_device_commit_cb(libxl__egc *egc,
     }
 
     /* TODO: wait a new checkpoint */
-    colo_start_new_checkpoint(egc, cds, 0);
+    rc = libxl__ev_time_register_rel(gc, &css->timeout,
+                                     colo_next_checkpoint,
+                                     20);
+    if (rc)
+        goto out;
+
     return;
 
 out:
     libxl__xc_domain_saverestore_async_callback_done(egc, &dss->shs, 0);
 }
 
+static void colo_next_checkpoint(libxl__egc *egc, libxl__ev_time *ev,
+                                 const struct timeval *requested_abs)
+{
+    libxl__colo_save_state *css = CONTAINER_OF(ev, *css, timeout);
+
+    colo_start_new_checkpoint(egc, &css->cds, 0);
+}
+
 static void colo_start_new_checkpoint(libxl__egc *egc,
                                       libxl__checkpoint_devices_state *cds,
                                       int rc)
tools/libxl/libxl_internal.h +3 -0
index aebc972..6fc26c9 100644
@@ -2736,6 +2736,9 @@ struct libxl__colo_save_state {
     uint8_t temp_buff[9];
     void (*callback)(libxl__egc *, libxl__colo_save_state *);
     bool svm_running;
+
+    /* hack */
+    libxl__ev_time timeout;
 };
 
 /*----- Domain suspend (save) state structure -----*/
Signature · 1 line
1.9.3
#25 lore raw

RFC PATCH v2 25/45 colo: dynamic allocate aio_requests to avoid -EBUSY error

In reply to Wen Congyang

From: Lai Jiangshan <[email protected]>

In normal case, there are at most TAPDISK_DATA_REQUESTS request at the same time. But in remus mode, the write requests are forwarded from the master side, and cached in block-remus. All cached requests will be forwarded to aio driver when syncing PVM and SVM. In this case, The number of requests may be more than TAPDISK_DATA_REQUESTS. So aio driver can't hanlde these requests at the same time, it will cause tapdisk2 exit.

We don't know how many requests will be handled, so dynamic allocate aio_requests to avoid this error.

---
 tools/blktap2/drivers/block-aio.c | 36 +++++++++++++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 3 deletions(-)
tools/blktap2/drivers/block-aio.c +33 -3
index f398da2..10ab20b 100644
@@ -55,9 +55,10 @@ struct tdaio_state {
 	int                  fd;
 	td_driver_t         *driver;
 
+	int                  aio_max_count;
 	int                  aio_free_count;	
 	struct aio_request   aio_requests[MAX_AIO_REQS];
-	struct aio_request  *aio_free_list[MAX_AIO_REQS];
+	struct aio_request   **aio_free_list;
 };
 
 /*Get Image size, secsize*/
@@ -122,6 +123,11 @@ int tdaio_open(td_driver_t *driver, const char *name, td_flag_t flags)
 
 	memset(prv, 0, sizeof(struct tdaio_state));
 
+	prv->aio_free_list = malloc(MAX_AIO_REQS * sizeof(*prv->aio_free_list));
+	if (!prv->aio_free_list)
+		return -ENOMEM;
+
+	prv->aio_max_count = MAX_AIO_REQS;
 	prv->aio_free_count = MAX_AIO_REQS;
 	for (i = 0; i < MAX_AIO_REQS; i++)
 		prv->aio_free_list[i] = &prv->aio_requests[i];
@@ -159,6 +165,28 @@ done:
 	return ret;	
 }
 
+static int tdaio_refill(struct tdaio_state *prv)
+{
+	struct aio_request **new, *new_req;
+	int i, max = prv->aio_max_count + MAX_AIO_REQS;
+
+	new = realloc(prv->aio_free_list, max * sizeof(*prv->aio_free_list));
+	if (!new)
+		return -1;
+	prv->aio_free_list = new;
+
+	new_req = calloc(MAX_AIO_REQS, sizeof(*new_req));
+	if (!new_req)
+		return -1;
+
+	prv->aio_max_count = max;
+	prv->aio_free_count = MAX_AIO_REQS;
+	for (i = 0; i < MAX_AIO_REQS; i++)
+		prv->aio_free_list[i] = &new_req[i];
+
+	return 0;
+}
+
 void tdaio_complete(void *arg, struct tiocb *tiocb, int err)
 {
 	struct aio_request *aio = (struct aio_request *)arg;
@@ -207,8 +235,10 @@ void tdaio_queue_write(td_driver_t *driver, td_request_t treq)
 	size    = treq.secs * driver->info.sector_size;
 	offset  = treq.sec  * (uint64_t)driver->info.sector_size;
 
-	if (prv->aio_free_count == 0)
-		goto fail;
+	if (prv->aio_free_count == 0) {
+		if (tdaio_refill(prv) < 0)
+			goto fail;
+	}
 
 	aio        = prv->aio_free_list[--prv->aio_free_count];
 	aio->treq  = treq;
Signature · 1 line
1.9.3
#26 lore raw

RFC PATCH v2 26/45 fix memory leak in block-remus

In reply to Wen Congyang

Fix the following two memory leak:
1. If s->ramdisk.prev is not NULL, we merge the write requests in s->ramdisk.h into s->ramdisk.prev, and then destroy s->ramdisk.h. But we forget to free hash value when destroying s->ramdisk.h.
2. When write requests is finished, replicated_write_callback() will be called. We forget free the buff in this function.

---
 tools/blktap2/drivers/block-remus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/blktap2/drivers/block-remus.c +1 -1
index 079588d..4ce9dbe 100644
@@ -602,7 +602,7 @@ static int ramdisk_start_flush(td_driver_t *driver)
 		}
 		free(sectors);
 
-		hashtable_destroy (s->ramdisk.h, 0);
+		hashtable_destroy (s->ramdisk.h, 1);
 	} else
 		s->ramdisk.prev = s->ramdisk.h;
 
Signature · 1 line
1.9.3
#27 lore raw

RFC PATCH v2 27/45 pass uuid to the callback td_open

In reply to Wen Congyang

remus's callback td_open needs uuid, but it is hard coded as 0. After commit 4b1af8, the vbd's uuid is the minor of the blktap device, not 0.

---
 tools/blktap2/drivers/block-aio.c         | 3 ++-
 tools/blktap2/drivers/block-cache.c       | 3 ++-
 tools/blktap2/drivers/block-log.c         | 3 ++-
 tools/blktap2/drivers/block-qcow.c        | 3 ++-
 tools/blktap2/drivers/block-ram.c         | 3 ++-
 tools/blktap2/drivers/block-remus.c       | 8 ++------
 tools/blktap2/drivers/block-vhd.c         | 3 ++-
 tools/blktap2/drivers/tapdisk-interface.c | 4 +++-
 tools/blktap2/drivers/tapdisk.h           | 2 +-
 9 files changed, 18 insertions(+), 14 deletions(-)
tools/blktap2/drivers/block-aio.c +2 -1
index 10ab20b..1b560e5 100644
@@ -111,7 +111,8 @@ static int tdaio_get_image_info(int fd, td_disk_info_t *info)
 }
 
 /* Open the disk file and initialize aio state. */
-int tdaio_open(td_driver_t *driver, const char *name, td_flag_t flags)
+int tdaio_open(td_driver_t *driver, const char *name, td_flag_t flags,
+	       td_uuid_t uuid)
 {
 	int i, fd, ret, o_flags;
 	struct tdaio_state *prv;
tools/blktap2/drivers/block-cache.c +2 -1
index 1d2f4eb..cd6ea6a 100644
@@ -517,7 +517,8 @@ block_cache_put_request(block_cache_t *cache, block_cache_request_t *breq)
 }
 
 static int
-block_cache_open(td_driver_t *driver, const char *name, td_flag_t flags)
+block_cache_open(td_driver_t *driver, const char *name, td_flag_t flags,
+		 td_uuid_t uuid)
 {
 	int i, err;
 	radix_tree_t *tree;
tools/blktap2/drivers/block-log.c +2 -1
index 5330cdc..7b33b63 100644
@@ -585,7 +585,8 @@ static void ctl_request(event_id_t id, char mode, void *private)
 
 static int tdlog_close(td_driver_t*);
 
-static int tdlog_open(td_driver_t* driver, const char* name, td_flag_t flags)
+static int tdlog_open(td_driver_t* driver, const char* name, td_flag_t flags,
+		      td_uuid_t uuid)
 {
   struct tdlog_state* s = (struct tdlog_state*)driver->data;
   int rc;
tools/blktap2/drivers/block-qcow.c +2 -1
index b45bcaa..64dfafc 100644
@@ -865,7 +865,8 @@ out:
 }
 
 /* Open the disk file and initialize qcow state. */
-int tdqcow_open (td_driver_t *driver, const char *name, td_flag_t flags)
+int tdqcow_open (td_driver_t *driver, const char *name, td_flag_t flags,
+		 td_uuid_t uuid)
 {
 	int fd, len, i, ret, size, o_flags;
 	td_disk_info_t *bs = &(driver->info);
tools/blktap2/drivers/block-ram.c +2 -1
index a859481..b64a194 100644
@@ -108,7 +108,8 @@ static int get_image_info(int fd, td_disk_info_t *info)
 }
 
 /* Open the disk file and initialize ram state. */
-int tdram_open (td_driver_t *driver, const char *name, td_flag_t flags)
+int tdram_open (td_driver_t *driver, const char *name, td_flag_t flags,
+		td_uuid_t uuid)
 {
 	char *p;
 	uint64_t size;
tools/blktap2/drivers/block-remus.c +2 -6
index 4ce9dbe..504f6b4 100644
@@ -1633,18 +1633,14 @@ static int ctl_register(struct tdremus_state *s)
 /* interface */
 
 static int tdremus_open(td_driver_t *driver, const char *name,
-			td_flag_t flags)
+			td_flag_t flags, td_uuid_t uuid)
 {
 	struct tdremus_state *s = (struct tdremus_state *)driver->data;
 	int rc;
 
 	RPRINTF("opening %s\n", name);
 
-	/* first we need to get the underlying vbd for this driver stack. To do so we
-	 * need to know the vbd's id. Fortunately, for tapdisk2 this is hard-coded as
-	 * 0 (see tapdisk2.c)
-	 */
-	device_vbd = tapdisk_server_get_vbd(0);
+	device_vbd = tapdisk_server_get_vbd(uuid);
 
 	memset(s, 0, sizeof(*s));
 	s->server_fd.fd = -1;
tools/blktap2/drivers/block-vhd.c +2 -1
index 76ea5bd..06e9c89 100644
@@ -675,7 +675,8 @@ __vhd_open(td_driver_t *driver, const char *name, vhd_flag_t flags)
 }
 
 static int
-_vhd_open(td_driver_t *driver, const char *name, td_flag_t flags)
+_vhd_open(td_driver_t *driver, const char *name, td_flag_t flags,
+	  td_uuid_t uuid)
 {
 	vhd_flag_t vhd_flags = 0;
 
tools/blktap2/drivers/tapdisk-interface.c +3 -1
index 2e51883..36b5393 100644
@@ -63,6 +63,7 @@ __td_open(td_image_t *image, td_disk_info_t *info)
 {
 	int err;
 	td_driver_t *driver;
+	td_vbd_t *vbd = image->private;
 
 	driver = image->driver;
 	if (!driver) {
@@ -78,7 +79,8 @@ __td_open(td_image_t *image, td_disk_info_t *info)
 	}
 
 	if (!td_flag_test(driver->state, TD_DRIVER_OPEN)) {
-		err = driver->ops->td_open(driver, image->name, image->flags);
+		err = driver->ops->td_open(driver, image->name, image->flags,
+					   vbd->uuid);
 		if (err) {
 			if (!image->driver)
 				tapdisk_driver_free(driver);
tools/blktap2/drivers/tapdisk.h +1 -1
index 66d508e..459eaec 100644
@@ -157,7 +157,7 @@ struct tap_disk {
 	const char                  *disk_type;
 	td_flag_t                    flags;
 	int                          private_data_size;
-	int (*td_open)               (td_driver_t *, const char *, td_flag_t);
+	int (*td_open)               (td_driver_t *, const char *, td_flag_t, td_uuid_t);
 	int (*td_close)              (td_driver_t *);
 	int (*td_get_parent_id)      (td_driver_t *, td_disk_id_t *);
 	int (*td_validate_parent)    (td_driver_t *, td_driver_t *, td_flag_t);
Signature · 1 line
1.9.3
#28 lore raw

RFC PATCH v2 28/45 return the correct dev path

In reply to Wen Congyang

The user uses TAPDISK_MESSAGE_OPEN to pass the devpath to tapdisk2, and will use TAPDISK_MESSAGE_LIST to query and get the pid of the tapdisk2.

The devpath's format is: driver:params[|driver:params[...]]. The first vbd image only contains the first params, and we will return driver:params, not devpath. The devpath is stored in vbd->name, so return vbd->name instead of image->name.

---
 tools/blktap2/drivers/tapdisk-control.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
tools/blktap2/drivers/tapdisk-control.c +2 -7
index 0b5cf3c..3a4ec8e 100644
@@ -270,15 +270,10 @@ tapdisk_control_list(struct tapdisk_control_connection *connection,
 		response.u.list.state   = vbd->state;
 		response.u.list.path[0] = 0;
 
-		if (!list_empty(&vbd->images)) {
-			td_image_t *image = list_entry(vbd->images.next,
-						       td_image_t, next);
+		if (vbd->name)
 			snprintf(response.u.list.path,
 				 sizeof(response.u.list.path),
-				 "%s:%s",
-				 tapdisk_disk_types[image->type]->name,
-				 image->name);
-		}
+				 "%s", vbd->name);
 
 		tapdisk_control_write_message(connection->socket, &response, 2);
 	}
Signature · 1 line
1.9.3
#29 lore raw

RFC PATCH v2 29/45 blktap2: use correct way to get remus_image

In reply to Wen Congyang

We set remus_image in backup_read(). If we do flush before the first read operation, remus_image will be NULL. Pass image to remus via the callback td_open().

---
 tools/blktap2/drivers/block-aio.c         | 6 ++++--
 tools/blktap2/drivers/block-cache.c       | 5 +++--
 tools/blktap2/drivers/block-log.c         | 5 +++--
 tools/blktap2/drivers/block-qcow.c        | 6 ++++--
 tools/blktap2/drivers/block-ram.c         | 6 ++++--
 tools/blktap2/drivers/block-remus.c       | 8 ++++----
 tools/blktap2/drivers/block-vhd.c         | 6 ++++--
 tools/blktap2/drivers/tapdisk-interface.c | 3 +--
 tools/blktap2/drivers/tapdisk.h           | 2 +-
 9 files changed, 28 insertions(+), 19 deletions(-)
tools/blktap2/drivers/block-aio.c +4 -2
index 1b560e5..27ba07d 100644
@@ -40,6 +40,7 @@
 #include "tapdisk.h"
 #include "tapdisk-driver.h"
 #include "tapdisk-interface.h"
+#include "tapdisk-image.h"
 
 #define MAX_AIO_REQS         TAPDISK_DATA_REQUESTS
 
@@ -111,11 +112,12 @@ static int tdaio_get_image_info(int fd, td_disk_info_t *info)
 }
 
 /* Open the disk file and initialize aio state. */
-int tdaio_open(td_driver_t *driver, const char *name, td_flag_t flags,
-	       td_uuid_t uuid)
+int tdaio_open(td_driver_t *driver, td_image_t *image, td_uuid_t uuid)
 {
 	int i, fd, ret, o_flags;
 	struct tdaio_state *prv;
+	const char *name = image->name;
+	td_flag_t flags = image->flags;
 
 	ret = 0;
 	prv = (struct tdaio_state *)driver->data;
tools/blktap2/drivers/block-cache.c +3 -2
index cd6ea6a..ff2c773 100644
@@ -517,12 +517,13 @@ block_cache_put_request(block_cache_t *cache, block_cache_request_t *breq)
 }
 
 static int
-block_cache_open(td_driver_t *driver, const char *name, td_flag_t flags,
-		 td_uuid_t uuid)
+block_cache_open(td_driver_t *driver, td_image_t *image, td_uuid_t uuid)
 {
 	int i, err;
 	radix_tree_t *tree;
 	block_cache_t *cache;
+	const char *name = image->name;
+	td_flag_t flags = image->flags;
 
 	if (!td_flag_test(flags, TD_OPEN_RDONLY))
 		return -EINVAL;
tools/blktap2/drivers/block-log.c +3 -2
index 7b33b63..80351d3 100644
@@ -585,11 +585,12 @@ static void ctl_request(event_id_t id, char mode, void *private)
 
 static int tdlog_close(td_driver_t*);
 
-static int tdlog_open(td_driver_t* driver, const char* name, td_flag_t flags,
-		      td_uuid_t uuid)
+static int tdlog_open(td_driver_t* driver, td_image_t *image, td_uuid_t uuid)
 {
   struct tdlog_state* s = (struct tdlog_state*)driver->data;
   int rc;
+  const char *name = image->name;
+  td_flag_t flags = image->flags;
 
   memset(s, 0, sizeof(*s));
 
tools/blktap2/drivers/block-qcow.c +4 -2
index 64dfafc..c63bd9d 100644
@@ -45,6 +45,7 @@
 #include "qcow.h"
 #include "blk.h"
 #include "atomicio.h"
+#include "tapdisk-image.h"
 
 /* *BSD has no O_LARGEFILE */
 #ifndef O_LARGEFILE
@@ -865,14 +866,15 @@ out:
 }
 
 /* Open the disk file and initialize qcow state. */
-int tdqcow_open (td_driver_t *driver, const char *name, td_flag_t flags,
-		 td_uuid_t uuid)
+int tdqcow_open (td_driver_t *driver, td_image_t *image, td_uuid_t uuid)
 {
 	int fd, len, i, ret, size, o_flags;
 	td_disk_info_t *bs = &(driver->info);
 	struct tdqcow_state   *s  = (struct tdqcow_state *)driver->data;
 	QCowHeader header;
 	uint64_t final_cluster = 0;
+	const char *name = image->name;
+	td_flag_t flags = image->flags;
 
  	DPRINTF("QCOW: Opening %s\n", name);
 
tools/blktap2/drivers/block-ram.c +4 -2
index b64a194..3e148ab 100644
@@ -40,6 +40,7 @@
 #include "tapdisk.h"
 #include "tapdisk-driver.h"
 #include "tapdisk-interface.h"
+#include "tapdisk-image.h"
 
 char *img;
 long int   disksector_size;
@@ -108,13 +109,14 @@ static int get_image_info(int fd, td_disk_info_t *info)
 }
 
 /* Open the disk file and initialize ram state. */
-int tdram_open (td_driver_t *driver, const char *name, td_flag_t flags,
-		td_uuid_t uuid)
+int tdram_open (td_driver_t *driver, td_image_t *image, td_uuid_t uuid)
 {
 	char *p;
 	uint64_t size;
 	int i, fd, ret = 0, count = 0, o_flags;
 	struct tdram_state *prv = (struct tdram_state *)driver->data;
+	const char *name = image->name;
+	td_flag_t flags = image->flags;
 
 	connections++;
 
tools/blktap2/drivers/block-remus.c +4 -4
index 504f6b4..23a908a 100644
@@ -1152,8 +1152,6 @@ void backup_queue_read(td_driver_t *driver, td_request_t treq)
 {
 	struct tdremus_state *s = (struct tdremus_state *)driver->data;
 	int i;
-	if(!remus_image)
-		remus_image = treq.image;
 	
 	/* check if this read is queued in any currently ongoing flush */
 	if (ramdisk_read(&s->ramdisk, treq.sec, treq.secs, treq.buf)) {
@@ -1632,15 +1630,17 @@ static int ctl_register(struct tdremus_state *s)
 
 /* interface */
 
-static int tdremus_open(td_driver_t *driver, const char *name,
-			td_flag_t flags, td_uuid_t uuid)
+static int tdremus_open(td_driver_t *driver, td_image_t *image, td_uuid_t uuid)
 {
 	struct tdremus_state *s = (struct tdremus_state *)driver->data;
 	int rc;
+	const char *name = image->name;
+	td_flag_t flags = image->flags;
 
 	RPRINTF("opening %s\n", name);
 
 	device_vbd = tapdisk_server_get_vbd(uuid);
+	remus_image = image;
 
 	memset(s, 0, sizeof(*s));
 	s->server_fd.fd = -1;
tools/blktap2/drivers/block-vhd.c +4 -2
index 06e9c89..b20f724 100644
@@ -59,6 +59,7 @@
 #include "tapdisk-driver.h"
 #include "tapdisk-interface.h"
 #include "tapdisk-disktype.h"
+#include "tapdisk-image.h"
 
 unsigned int SPB;
 
@@ -675,10 +676,11 @@ __vhd_open(td_driver_t *driver, const char *name, vhd_flag_t flags)
 }
 
 static int
-_vhd_open(td_driver_t *driver, const char *name, td_flag_t flags,
-	  td_uuid_t uuid)
+_vhd_open(td_driver_t *driver, td_image_t *image, td_uuid_t uuid)
 {
 	vhd_flag_t vhd_flags = 0;
+	const char *name = image->name;
+	td_flag_t flags = image->flags;
 
 	if (flags & TD_OPEN_RDONLY)
 		vhd_flags |= VHD_FLAG_OPEN_RDONLY;
tools/blktap2/drivers/tapdisk-interface.c +1 -2
index 36b5393..a29de64 100644
@@ -79,8 +79,7 @@ __td_open(td_image_t *image, td_disk_info_t *info)
 	}
 
 	if (!td_flag_test(driver->state, TD_DRIVER_OPEN)) {
-		err = driver->ops->td_open(driver, image->name, image->flags,
-					   vbd->uuid);
+		err = driver->ops->td_open(driver, image, vbd->uuid);
 		if (err) {
 			if (!image->driver)
 				tapdisk_driver_free(driver);
tools/blktap2/drivers/tapdisk.h +1 -1
index 459eaec..3c3b51d 100644
@@ -157,7 +157,7 @@ struct tap_disk {
 	const char                  *disk_type;
 	td_flag_t                    flags;
 	int                          private_data_size;
-	int (*td_open)               (td_driver_t *, const char *, td_flag_t, td_uuid_t);
+	int (*td_open)               (td_driver_t *, td_image_t *, td_uuid_t);
 	int (*td_close)              (td_driver_t *);
 	int (*td_get_parent_id)      (td_driver_t *, td_disk_id_t *);
 	int (*td_validate_parent)    (td_driver_t *, td_driver_t *, td_flag_t);
Signature · 1 line
1.9.3
#30 lore raw

RFC PATCH v2 30/45 don't call client_flush() when switching to unprotected mode

In reply to Wen Congyang

If we switch the mode from primary to unprotected, the connection between primary and backup cannot be used.

---
 tools/blktap2/drivers/block-remus.c | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)
tools/blktap2/drivers/block-remus.c +18 -10
index 23a908a..b15c966 100644
@@ -881,6 +881,7 @@ static void primary_queue_write(td_driver_t *driver, td_request_t treq)
 }
 
 
+/* It is called when the user writes "flush" to control file */
 static int client_flush(td_driver_t *driver)
 {
 	struct tdremus_state *s = (struct tdremus_state *)driver->data;
@@ -891,7 +892,8 @@ static int client_flush(td_driver_t *driver)
 		/* connection not yet established, nothing to flush */
 		return 0;
 
-	if (mwrite(s->stream_fd.fd, TDREMUS_COMMIT, strlen(TDREMUS_COMMIT)) < 0) {
+	if (mwrite(s->stream_fd.fd, TDREMUS_COMMIT,
+	    strlen(TDREMUS_COMMIT)) < 0) {
 		RPRINTF("error flushing output");
 		close_stream_fd(s);
 		return -1;
@@ -920,7 +922,6 @@ static int primary_start(td_driver_t *driver)
 
 	tapdisk_remus.td_queue_read = primary_queue_read;
 	tapdisk_remus.td_queue_write = primary_queue_write;
-	s->queue_flush = client_flush;
 
 	s->stream_fd.fd = -1;
 	s->stream_fd.id = -1;
@@ -1504,16 +1505,23 @@ static void ctl_request(event_id_t id, char mode, void *private)
 		return;
 	}
 
-	/* TODO: need to get driver somehow */
 	msg[rc] = '\0';
-	if (!strncmp(msg, "flush", 5)) {
-		if (s->queue_flush)
-			if ((rc = s->queue_flush(driver))) {
-				RPRINTF("error passing flush request to backup");
-				ctl_respond(s, TDREMUS_FAIL);
-			}
-	} else {
+	if (strncmp(msg, "flush", 5)) {
 		RPRINTF("unknown command: %s\n", msg);
+		ctl_respond(s, TDREMUS_FAIL);
+		return;
+	}
+
+	if (s->mode != mode_primary) {
+		RPRINTF("We are not in primary mode\n");
+		ctl_respond(s, TDREMUS_FAIL);
+		return;
+	}
+
+	rc = client_flush(driver);
+	if (rc) {
+		RPRINTF("error passing flush request to backup");
+		ctl_respond(s, TDREMUS_FAIL);
 	}
 }
 
Signature · 1 line
1.9.3
#31 lore raw

RFC PATCH v2 31/45 remus: fix bug in tdremus_close()

In reply to Wen Congyang

We close ctl_fd.fd, but we don't unregister ctl_fd.id. It will cause select() return fails, and the user cannot talk with tapdisk2.

This patch also does some cleanup.

---
 tools/blktap2/drivers/block-remus.c | 90 ++++++++++++++++++++++---------------
 1 file changed, 53 insertions(+), 37 deletions(-)
tools/blktap2/drivers/block-remus.c +53 -37
index b15c966..d358b44 100644
@@ -151,9 +151,6 @@ typedef struct poll_fd {
 } poll_fd_t;
 
 struct tdremus_state {
-//  struct tap_disk* driver;
-	void* driver_data;
-
   /* XXX: this is needed so that the server can perform operations on
    * the driver from the stream_fd event handler. fix this. */
 	td_driver_t *tdremus_driver;
@@ -731,12 +728,26 @@ static int mwrite(int fd, void* buf, size_t len)
 
 static void inline close_stream_fd(struct tdremus_state *s)
 {
+	if (s->stream_fd.fd < 0)
+		return;
+
 	/* XXX: -2 is magic. replace with macro perhaps? */
 	tapdisk_server_unregister_event(s->stream_fd.id);
 	close(s->stream_fd.fd);
 	s->stream_fd.fd = -2;
 }
 
+static void close_server_fd(struct tdremus_state *s)
+{
+	if (s->server_fd.fd < 0)
+		return;
+
+	tapdisk_server_unregister_event(s->server_fd.id);
+	s->server_fd.id = -1;
+	close(s->stream_fd.fd);
+	s->stream_fd.fd = -1;
+}
+
 /* primary functions */
 static void remus_client_event(event_id_t, char mode, void *private);
 static void remus_connect_event(event_id_t id, char mode, void *private);
@@ -1348,12 +1359,7 @@ static int unprotected_start(td_driver_t *driver)
 	/* close the server socket */
 	close_stream_fd(s);
 
-	/* unregister the replication stream */
-	tapdisk_server_unregister_event(s->server_fd.id);
-
-	/* close the replication stream */
-	close(s->server_fd.fd);
-	s->server_fd.fd = -1;
+	close_server_fd(s);
 
 	/* install the unprotected read/write handlers */
 	tapdisk_remus.td_queue_read = unprotected_queue_read;
@@ -1561,27 +1567,27 @@ static int ctl_open(td_driver_t *driver, const char* name)
 			s->ctl_path[i] = '_';
 	}
 	if (asprintf(&s->msg_path, "%s.msg", s->ctl_path) < 0)
-		goto err_ctlfifo;
+		goto err_setmsgfifo;
 
 	if (mkfifo(s->ctl_path, S_IRWXU|S_IRWXG|S_IRWXO) && errno != EEXIST) {
 		RPRINTF("error creating control FIFO %s: %d\n", s->ctl_path, errno);
-		goto err_msgfifo;
+		goto err_mkctlfifo;
 	}
 
 	if (mkfifo(s->msg_path, S_IRWXU|S_IRWXG|S_IRWXO) && errno != EEXIST) {
 		RPRINTF("error creating message FIFO %s: %d\n", s->msg_path, errno);
-		goto err_msgfifo;
+		goto err_mkmsgfifo;
 	}
 
 	/* RDWR so that fd doesn't block select when no writer is present */
 	if ((s->ctl_fd.fd = open(s->ctl_path, O_RDWR)) < 0) {
 		RPRINTF("error opening control FIFO %s: %d\n", s->ctl_path, errno);
-		goto err_msgfifo;
+		goto err_openctlfifo;
 	}
 
 	if ((s->msg_fd.fd = open(s->msg_path, O_RDWR)) < 0) {
 		RPRINTF("error opening message FIFO %s: %d\n", s->msg_path, errno);
-		goto err_openctlfifo;
+		goto err_openmsgfifo;
 	}
 
 	RPRINTF("control FIFO %s\n", s->ctl_path);
@@ -1589,36 +1595,45 @@ static int ctl_open(td_driver_t *driver, const char* name)
 
 	return 0;
 
- err_openctlfifo:
+err_openmsgfifo:
 	close(s->ctl_fd.fd);
- err_msgfifo:
+	s->ctl_fd.fd = -1;
+err_openctlfifo:
+	unlink(s->ctl_path);
+err_mkmsgfifo:
+	unlink(s->msg_path);
+err_mkctlfifo:
 	free(s->msg_path);
 	s->msg_path = NULL;
- err_ctlfifo:
+err_setmsgfifo:
 	free(s->ctl_path);
 	s->ctl_path = NULL;
 	return -1;
 }
 
-static void ctl_close(td_driver_t *driver)
+static void ctl_close(struct tdremus_state *s)
 {
-	struct tdremus_state *s = (struct tdremus_state *)driver->data;
-
-	/* TODO: close *all* connections */
-
-	if(s->ctl_fd.fd)
+	if(s->ctl_fd.fd) {
 		close(s->ctl_fd.fd);
+		s->ctl_fd.fd = -1;
+	}
 
 	if (s->ctl_path) {
 		unlink(s->ctl_path);
 		free(s->ctl_path);
 		s->ctl_path = NULL;
 	}
+
 	if (s->msg_path) {
 		unlink(s->msg_path);
 		free(s->msg_path);
 		s->msg_path = NULL;
 	}
+
+	if (s->msg_fd.fd) {
+		close(s->msg_fd.fd);
+		s->msg_fd.fd = -1;
+	}
 }
 
 static int ctl_register(struct tdremus_state *s)
@@ -1636,6 +1651,16 @@ static int ctl_register(struct tdremus_state *s)
 	return 0;
 }
 
+static void ctl_unregister(struct tdremus_state *s)
+{
+	RPRINTF("unregistering ctl fifo\n");
+
+	if (s->ctl_fd.id >= 0) {
+		tapdisk_server_unregister_event(s->ctl_fd.id);
+		s->ctl_fd.id = -1;
+	}
+}
+
 /* interface */
 
 static int tdremus_open(td_driver_t *driver, td_image_t *image, td_uuid_t uuid)
@@ -1666,13 +1691,12 @@ static int tdremus_open(td_driver_t *driver, td_image_t *image, td_uuid_t uuid)
 
 	if ((rc = ctl_open(driver, name))) {
 		RPRINTF("error setting up control channel\n");
-		free(s->driver_data);
 		return rc;
 	}
 
 	if ((rc = ctl_register(s))) {
 		RPRINTF("error registering control channel\n");
-		free(s->driver_data);
+		ctl_close(s);
 		return rc;
 	}
 
@@ -1695,19 +1719,11 @@ static int tdremus_close(td_driver_t *driver)
 	RPRINTF("closing\n");
 	if (s->ramdisk.inprogress)
 		hashtable_destroy(s->ramdisk.inprogress, 0);
-	
-	if (s->driver_data) {
-		free(s->driver_data);
-		s->driver_data = NULL;
-	}
-	if (s->server_fd.fd >= 0) {
-		close(s->server_fd.fd);
-		s->server_fd.fd = -1;
-	}
-	if (s->stream_fd.fd >= 0)
-		close_stream_fd(s);
 
-	ctl_close(driver);
+	close_server_fd(s);
+	close_stream_fd(s);
+	ctl_unregister(s);
+	ctl_close(s);
 
 	return 0;
 }
Signature · 1 line
1.9.3
#32 lore raw

RFC PATCH v2 32/45 blktap2: use correct way to get free event id

In reply to Wen Congyang
---
 tools/blktap2/drivers/scheduler.c | 33 ++++++++++++++++++++++++++++++---
 1 file changed, 30 insertions(+), 3 deletions(-)
tools/blktap2/drivers/scheduler.c +30 -3
index 6b8d009..dd608dd 100644
@@ -160,6 +160,31 @@ scheduler_run_events(scheduler_t *s)
 	}
 }
 
+static int
+get_free_id(scheduler_t *s)
+{
+	event_t *event, *tmp;
+	int old_uuid = s->uuid;
+	int id = s->uuid++;
+
+	if (!s->uuid)
+		s->uuid++;
+
+retry:
+	scheduler_for_each_event(s, event, tmp)
+		if (event->id == id) {
+			id = s->uuid++;
+			if (!s->uuid)
+				s->uuid++;
+			if (id == old_uuid)
+				return 0;
+
+			goto retry;
+		}
+
+	return id;
+}
+
 int
 scheduler_register_event(scheduler_t *s, char mode, int fd,
 			 int timeout, event_cb_t cb, void *private)
@@ -187,10 +212,12 @@ scheduler_register_event(scheduler_t *s, char mode, int fd,
 	event->deadline = now.tv_sec + timeout;
 	event->cb       = cb;
 	event->private  = private;
-	event->id       = s->uuid++;
+	event->id       = get_free_id(s);
 
-	if (!s->uuid)
-		s->uuid++;
+	if (!event->id) {
+		free(event);
+		return -EBUSY;
+	}
 
 	list_add_tail(&event->next, &s->events);
 
Signature · 1 line
1.9.3
#33 lore raw

RFC PATCH v2 33/45 blktap2: don't return negative event id

In reply to Wen Congyang

If we find some error when registering a new event, we will return a negative value. So we should skip negative event id.

Also fix a wrong check of return value.

---
 tools/blktap2/drivers/scheduler.c       | 8 ++++----
 tools/blktap2/drivers/tapdisk-control.c | 2 +-
 2 files changed, 5 insertions(+), 5 deletions(-)
tools/blktap2/drivers/scheduler.c +4 -4
index dd608dd..e07528b 100644
@@ -167,15 +167,15 @@ get_free_id(scheduler_t *s)
 	int old_uuid = s->uuid;
 	int id = s->uuid++;
 
-	if (!s->uuid)
-		s->uuid++;
+	if (s->uuid < 0)
+		s->uuid = 1;
 
 retry:
 	scheduler_for_each_event(s, event, tmp)
 		if (event->id == id) {
 			id = s->uuid++;
-			if (!s->uuid)
-				s->uuid++;
+			if (s->uuid < 0)
+				s->uuid = 1;
 			if (id == old_uuid)
 				return 0;
 
tools/blktap2/drivers/tapdisk-control.c +1 -1
index 3a4ec8e..4e5f748 100644
@@ -700,7 +700,7 @@ tapdisk_control_accept(event_id_t id, char mode, void *private)
 					    connection->socket, 0,
 					    tapdisk_control_handle_request,
 					    connection);
-	if (err == -1) {
+	if (err < 0) {
 		close(fd);
 		free(connection);
 		EPRINTF("failed to register new control event: %d\n", err);
Signature · 1 line
1.9.3
#34 lore raw

RFC PATCH v2 34/45 blktap2: use correct way to define array.

In reply to Wen Congyang

Currently, we use the following way to define an array:

type array[] = {
    [index] = xxx,
    0,
};

So array[index+1] will be NULL. If index is not the last index, it will override another index.

tapdisk_vbd_index is not defined, but array[DISK_TYPE_VINDEX] is overridden, so we don't find this problem when building the source.

---
 tools/blktap2/drivers/tapdisk-disktype.c | 12 ++----------
 tools/blktap2/drivers/tapdisk-disktype.h |  2 +-
 2 files changed, 3 insertions(+), 11 deletions(-)
tools/blktap2/drivers/tapdisk-disktype.c +2 -10
index e9a6890..8d1383b 100644
@@ -82,12 +82,6 @@ static const disk_info_t block_cache_disk = {
        1,
 };
 
-static const disk_info_t vhd_index_disk = {
-       "vhdi",
-       "vhd index image (vhdi)",
-       1,
-};
-
 static const disk_info_t log_disk = {
 	"log",
 	"write logger (log)",
@@ -110,9 +104,8 @@ const disk_info_t *tapdisk_disk_types[] = {
 	[DISK_TYPE_QCOW]	= &qcow_disk,
 	[DISK_TYPE_BLOCK_CACHE] = &block_cache_disk,
 	[DISK_TYPE_LOG]	= &log_disk,
-	[DISK_TYPE_VINDEX]	= &vhd_index_disk,
 	[DISK_TYPE_REMUS]	= &remus_disk,
-	0,
+	[DISK_TYPE_MAX]		= NULL,
 };
 
 extern struct tap_disk tapdisk_aio;
@@ -137,10 +130,9 @@ const struct tap_disk *tapdisk_disk_drivers[] = {
 	[DISK_TYPE_RAM]         = &tapdisk_ram,
 	[DISK_TYPE_QCOW]        = &tapdisk_qcow,
 	[DISK_TYPE_BLOCK_CACHE] = &tapdisk_block_cache,
-	[DISK_TYPE_VINDEX]      = &tapdisk_vhd_index,
 	[DISK_TYPE_LOG]         = &tapdisk_log,
 	[DISK_TYPE_REMUS]       = &tapdisk_remus,
-	0,
+	[DISK_TYPE_MAX]         = NULL,
 };
 
 int
tools/blktap2/drivers/tapdisk-disktype.h +1 -1
index b697eea..c574990 100644
@@ -39,7 +39,7 @@
 #define DISK_TYPE_BLOCK_CACHE 7
 #define DISK_TYPE_LOG         8
 #define DISK_TYPE_REMUS       9
-#define DISK_TYPE_VINDEX      10
+#define DISK_TYPE_MAX         10
 
 #define DISK_TYPE_NAME_MAX    32
 
Signature · 1 line
1.9.3
#35 lore raw

RFC PATCH v2 35/45 blktap2: connect to backup asynchronously

In reply to Wen Congyang

tapdisk2 is a single thread process. If we use remus, we will block in primary_blocking_connect(). The user will not have any chance to talk with tapdisk2. So we should connect to backup asynchronously. Before the connection is established, we queue all I/O request, and handle it when the connection is established.

---
 tools/blktap2/drivers/block-remus.c | 760 +++++++++++++++++++++++-------------
 1 file changed, 479 insertions(+), 281 deletions(-)
tools/blktap2/drivers/block-remus.c +479 -281
index d358b44..c21f851 100644
@@ -63,10 +63,28 @@
 #define RAMDISK_HASHSIZE 128
 
 /* connect retry timeout (seconds) */
-#define REMUS_CONNRETRY_TIMEOUT 10
+#define REMUS_CONNRETRY_TIMEOUT 1
 
 #define RPRINTF(_f, _a...) syslog (LOG_DEBUG, "remus: " _f, ## _a)
 
+#define UNREGISTER_EVENT(id)					\
+	do {							\
+		if (id >= 0) {					\
+			tapdisk_server_unregister_event(id);	\
+			id = -1;				\
+		}						\
+	} while (0)
+
+#define CLOSE_FD(fd)			\
+	do {				\
+		if (fd >= 0) {		\
+			close(fd);	\
+			fd = -1;	\
+		}			\
+	} while (0)
+
+#define MAX_REMUS_REQUEST       TAPDISK_DATA_REQUESTS
+
 enum tdremus_mode {
 	mode_invalid = 0,
 	mode_unprotected,
@@ -74,17 +92,21 @@ enum tdremus_mode {
 	mode_backup
 };
 
+enum {
+	ERROR_INTERNAL = -1,
+	ERROR_IO = -2,
+	ERROR_CONNECTION = -3,
+};
+
 struct tdremus_req {
-	uint64_t sector;
-	int nb_sectors;
-	char buf[4096];
+	td_request_t treq;
 };
 
 struct req_ring {
 	/* waste one slot to distinguish between empty and full */
-	struct tdremus_req requests[MAX_REQUESTS * 2 + 1];
-	unsigned int head;
-	unsigned int tail;
+	struct tdremus_req pending_requests[MAX_REMUS_REQUEST + 1];
+	unsigned int prod;
+	unsigned int cons;
 };
 
 /* TODO: This isn't very pretty, but to properly generate our own treqs (needed
@@ -144,10 +166,21 @@ struct ramdisk_write_cbdata {
 
 typedef void (*queue_rw_t) (td_driver_t *driver, td_request_t treq);
 
-/* poll_fd type for blktap2 fd system. taken from block_log.c */
+/*
+ * If cid, rid and wid are -1, fd must be -1. It means that
+ * we are in unpritected mode or we don't start to connect
+ * to backup.
+ * If fd is an valid fd:
+ *  cid is valid, rid and wid must be invalid. It means that
+ *      the connection is in progress.
+ *  cid is invalid. rid or wid must be valid. It means that
+ *      the connection is established.
+ */
 typedef struct poll_fd {
 	int        fd;
-	event_id_t id;
+	event_id_t cid;
+	event_id_t rid;
+	event_id_t wid;
 } poll_fd_t;
 
 struct tdremus_state {
@@ -166,8 +199,11 @@ struct tdremus_state {
 	poll_fd_t server_fd;    /* server listen port */
 	poll_fd_t stream_fd;     /* replication channel */
 
-	/* queue write requests, batch-replicate at submit */
-	struct req_ring write_ring;
+	/*
+	 * queue I/O requests, batch-replicate when
+	 * the connection is established.
+	 */
+	struct req_ring queued_io;
 
 	/* ramdisk data*/
 	struct ramdisk ramdisk;
@@ -207,11 +243,13 @@ static int tdremus_close(td_driver_t *driver);
 
 static int switch_mode(td_driver_t *driver, enum tdremus_mode mode);
 static int ctl_respond(struct tdremus_state *s, const char *response);
+static int ctl_register(struct tdremus_state *s);
+static void ctl_unregister(struct tdremus_state *s);
 
 /* ring functions */
-static inline unsigned int ring_next(struct req_ring* ring, unsigned int pos)
+static inline unsigned int ring_next(unsigned int pos)
 {
-	if (++pos >= MAX_REQUESTS * 2 + 1)
+	if (++pos >= MAX_REMUS_REQUEST + 1)
 		return 0;
 
 	return pos;
@@ -219,13 +257,26 @@ static inline unsigned int ring_next(struct req_ring* ring, unsigned int pos)
 
 static inline int ring_isempty(struct req_ring* ring)
 {
-	return ring->head == ring->tail;
+	return ring->cons == ring->prod;
 }
 
 static inline int ring_isfull(struct req_ring* ring)
 {
-	return ring_next(ring, ring->tail) == ring->head;
+	return ring_next(ring->prod) == ring->cons;
+}
+
+static void ring_add_request(struct req_ring *ring, const td_request_t *treq)
+{
+	/* If ring is full, it means that tapdisk2 has some bug */
+	if (ring_isfull(ring)) {
+		RPRINTF("OOPS, ring is full\n");
+		exit(1);
+	}
+
+	ring->pending_requests[ring->prod].treq = *treq;
+	ring->prod = ring_next(ring->prod);
 }
+
 /* Prototype declarations */
 static int ramdisk_flush(td_driver_t *driver, struct tdremus_state* s);
 
@@ -728,30 +779,39 @@ static int mwrite(int fd, void* buf, size_t len)
 
 static void inline close_stream_fd(struct tdremus_state *s)
 {
-	if (s->stream_fd.fd < 0)
-		return;
 
-	/* XXX: -2 is magic. replace with macro perhaps? */
-	tapdisk_server_unregister_event(s->stream_fd.id);
-	close(s->stream_fd.fd);
-	s->stream_fd.fd = -2;
+	UNREGISTER_EVENT(s->stream_fd.cid);
+	UNREGISTER_EVENT(s->stream_fd.rid);
+	UNREGISTER_EVENT(s->stream_fd.wid);
+
+	/* close the connection */
+	CLOSE_FD(s->stream_fd.fd);
 }
 
 static void close_server_fd(struct tdremus_state *s)
 {
-	if (s->server_fd.fd < 0)
-		return;
-
-	tapdisk_server_unregister_event(s->server_fd.id);
-	s->server_fd.id = -1;
-	close(s->stream_fd.fd);
-	s->stream_fd.fd = -1;
+	UNREGISTER_EVENT(s->server_fd.cid);
+	CLOSE_FD(s->server_fd.fd);
 }
 
 /* primary functions */
 static void remus_client_event(event_id_t, char mode, void *private);
 static void remus_connect_event(event_id_t id, char mode, void *private);
 static void remus_retry_connect_event(event_id_t id, char mode, void *private);
+static int primary_forward_request(struct tdremus_state *s,
+				   const td_request_t *treq);
+
+/*
+ * It is called when we cannot connect to backup, or find I/O error when
+ * reading/writing.
+ */
+static void primary_failed(struct tdremus_state *s, int rc)
+{
+	close_stream_fd(s);
+	if (rc == ERROR_INTERNAL)
+		RPRINTF("switch to unprotected mode due to internal error");
+	switch_mode(s->tdremus_driver, mode_unprotected);
+}
 
 static int primary_do_connect(struct tdremus_state *state)
 {
@@ -760,281 +820,247 @@ static int primary_do_connect(struct tdremus_state *state)
 	int rc;
 	int flags;
 
-	RPRINTF("client connecting to %s:%d...\n", inet_ntoa(state->sa.sin_addr), ntohs(state->sa.sin_port));
+	RPRINTF("client connecting to %s:%d...\n",
+		inet_ntoa(state->sa.sin_addr), ntohs(state->sa.sin_port));
 
 	if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
 		RPRINTF("could not create client socket: %d\n", errno);
-		return -1;
+		return ERROR_INTERNAL;
 	}
+	state->stream_fd.fd = fd;
 
 	/* make socket nonblocking */
 	if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
 		flags = 0;
-	if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
-		return -1;
+	if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
+		RPRINTF("error setting fd %d to non block mode\n", fd);
+		return ERROR_INTERNAL;
+	}
 
-	/* once we have created the socket and populated the address, we can now start
-	 * our non-blocking connect. rather than duplicating code we trigger a timeout
-	 * on the socket fd, which calls out nonblocking connect code
+	/*
+	 * once we have created the socket and populated the address,
+	 * we can now start our non-blocking connect. rather than
+	 * duplicating code we trigger a timeout on the socket fd,
+	 * which calls out nonblocking connect code
 	 */
-	if((id = tapdisk_server_register_event(SCHEDULER_POLL_TIMEOUT, fd, 0, remus_retry_connect_event, state)) < 0) {
-		RPRINTF("error registering timeout client connection event handler: %s\n", strerror(id));
-		/* TODO: we leak a fd here */
-		return -1;
+	if((id = tapdisk_server_register_event(SCHEDULER_POLL_TIMEOUT, fd, 0,
+					       remus_retry_connect_event,
+					       state)) < 0) {
+		RPRINTF("error registering timeout client connection event handler: %s\n",
+			strerror(id));
+		return ERROR_INTERNAL;
 	}
-	state->stream_fd.fd = fd;
-	state->stream_fd.id = id;
+
+	state->stream_fd.cid = id;
 	return 0;
 }
 
-static int primary_blocking_connect(struct tdremus_state *state)
+static int remus_handle_queued_io(struct tdremus_state *s)
 {
-	int fd;
-	int id;
+	struct req_ring *queued_io = &s->queued_io;
+	unsigned int cons;
+	td_request_t *treq;
 	int rc;
-	int flags;
-
-	RPRINTF("client connecting to %s:%d...\n", inet_ntoa(state->sa.sin_addr), ntohs(state->sa.sin_port));
 
-	if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
-		RPRINTF("could not create client socket: %d\n", errno);
-		return -1;
-	}
+	while (!ring_isempty(queued_io)) {
+		cons = queued_io->cons;
+		treq = &queued_io->pending_requests[cons].treq;
 
-	do {
-		if ((rc = connect(fd, (struct sockaddr *)&state->sa,
-		    sizeof(state->sa))) < 0)
-		{
-			if (errno == ECONNREFUSED) {
-				RPRINTF("connection refused -- retrying in 1 second\n");
-				sleep(1);
-			} else {
-				RPRINTF("connection failed: %d\n", errno);
-				close(fd);
-				return -1;
-			}
+		if (treq->op == TD_OP_WRITE) {
+			rc = primary_forward_request(s, treq);
+			if (rc)
+				return rc;
 		}
-	} while (rc < 0);
-
-	RPRINTF("client connected\n");
-
-	/* make socket nonblocking */
-	if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
-		flags = 0;
-	if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
-	{
-		RPRINTF("error making socket nonblocking\n");
-		close(fd);
-		return -1;
-	}
 
-	if((id = tapdisk_server_register_event(SCHEDULER_POLL_READ_FD, fd, 0, remus_client_event, state)) < 0) {
-		RPRINTF("error registering client event handler: %s\n", strerror(id));
-		close(fd);
-		return -1;
+		td_forward_request(*treq);
+		queued_io->cons = ring_next(cons);
 	}
 
-	state->stream_fd.fd = fd;
-	state->stream_fd.id = id;
 	return 0;
 }
 
-/* on read, just pass request through */
-static void primary_queue_read(td_driver_t *driver, td_request_t treq)
-{
-	/* just pass read through */
-	td_forward_request(treq);
-}
-
-/* TODO:
- * The primary uses mwrite() to write the contents of a write request to the
- * backup. This effectively blocks until all data has been copied into a system
- * buffer or a timeout has occured. We may wish to instead use tapdisk's
- * nonblocking i/o interface, tapdisk_server_register_event(), to set timeouts
- * and write data in an asynchronous fashion.
- */
-static void primary_queue_write(td_driver_t *driver, td_request_t treq)
+static int remus_connection_done(struct tdremus_state *s)
 {
-	struct tdremus_state *s = (struct tdremus_state *)driver->data;
-
-	char header[sizeof(uint32_t) + sizeof(uint64_t)];
-	uint32_t *sectors = (uint32_t *)header;
-	uint64_t *sector = (uint64_t *)(header + sizeof(uint32_t));
+	event_id_t id;
 
-	// RPRINTF("write: stream_fd.fd: %d\n", s->stream_fd.fd);
+	/* the connect succeeded */
+	/* unregister this function and register a new event handler */
+	tapdisk_server_unregister_event(s->stream_fd.cid);
+	s->stream_fd.cid = -1;
 
-	/* -1 means we haven't connected yet, -2 means the connection was lost */
-	if(s->stream_fd.fd == -1) {
-		RPRINTF("connecting to backup...\n");
-		primary_blocking_connect(s);
+	id = tapdisk_server_register_event(SCHEDULER_POLL_READ_FD, s->stream_fd.fd,
+					   0, remus_client_event, s);
+	if(id < 0) {
+		RPRINTF("error registering client event handler: %s\n",
+			strerror(id));
+		return ERROR_INTERNAL;
 	}
+	s->stream_fd.rid = id;
 
-	*sectors = treq.secs;
-	*sector = treq.sec;
-
-	if (mwrite(s->stream_fd.fd, TDREMUS_WRITE, strlen(TDREMUS_WRITE)) < 0)
-		goto fail;
-	if (mwrite(s->stream_fd.fd, header, sizeof(header)) < 0)
-		goto fail;
+	/* handle the queued requests */
+	return remus_handle_queued_io(s);
+}
 
-	if (mwrite(s->stream_fd.fd, treq.buf, treq.secs * driver->info.sector_size) < 0)
-		goto fail;
+static int remus_retry_connect(struct tdremus_state *s)
+{
+	event_id_t id;
 
-	td_forward_request(treq);
+	tapdisk_server_unregister_event(s->stream_fd.cid);
+	s->stream_fd.cid = -1;
 
-	return;
+	RPRINTF("connect to backup 1 second later");
+	id = tapdisk_server_register_event(SCHEDULER_POLL_TIMEOUT,
+					   s->stream_fd.fd,
+					   REMUS_CONNRETRY_TIMEOUT,
+					   remus_retry_connect_event, s);
+	if (id < 0) {
+		RPRINTF("error registering timeout client connection event handler: %s\n",
+			strerror(id));
+		return ERROR_INTERNAL;
+	}
 
- fail:
-	/* switch to unprotected mode and tell tapdisk to retry */
-	RPRINTF("write request replication failed, switching to unprotected mode");
-	switch_mode(s->tdremus_driver, mode_unprotected);
-	td_complete_request(treq, -EBUSY);
+	s->stream_fd.cid = id;
+	return 0;
 }
 
-
-/* It is called when the user writes "flush" to control file */
-static int client_flush(td_driver_t *driver)
+static int remus_wait_connect_done(struct tdremus_state *s)
 {
-	struct tdremus_state *s = (struct tdremus_state *)driver->data;
-
-	// RPRINTF("committing output\n");
+	event_id_t id;
 
-	if (s->stream_fd.fd == -1)
-		/* connection not yet established, nothing to flush */
-		return 0;
+	tapdisk_server_unregister_event(s->stream_fd.cid);
+	s->stream_fd.cid = -1;
 
-	if (mwrite(s->stream_fd.fd, TDREMUS_COMMIT,
-	    strlen(TDREMUS_COMMIT)) < 0) {
-		RPRINTF("error flushing output");
-		close_stream_fd(s);
-		return -1;
+	id = tapdisk_server_register_event(SCHEDULER_POLL_WRITE_FD,
+					   s->stream_fd.fd, 0,
+					   remus_connect_event, s);
+	if (id < 0) {
+		RPRINTF("error registering client connection event handler: %s\n",
+			strerror(id));
+		return ERROR_INTERNAL;
 	}
+	s->stream_fd.cid = id;
 
 	return 0;
 }
 
-static int server_flush(td_driver_t *driver)
+/* return 1 if we need to reconnect to backup */
+static int check_connect_errno(int err)
 {
-	struct tdremus_state *s = (struct tdremus_state *)driver->data;
-	/* 
-	 * Nothing to flush in beginning.
+	/*
+	 * The fd is non-block, so we will not get ETIMEDOUT
+	 * after calling connect(). We only can get this errno
+	 * by getsockopt().
 	 */
-	if (!s->ramdisk.prev)
-		return 0;
-	/* Try to flush any remaining requests */
-	return ramdisk_flush(driver, s);	
-}
-
-static int primary_start(td_driver_t *driver)
-{
-	struct tdremus_state *s = (struct tdremus_state *)driver->data;
-
-	RPRINTF("activating client mode\n");
-
-	tapdisk_remus.td_queue_read = primary_queue_read;
-	tapdisk_remus.td_queue_write = primary_queue_write;
-
-	s->stream_fd.fd = -1;
-	s->stream_fd.id = -1;
+	if (err == ECONNREFUSED || err == ENETUNREACH ||
+	    err == EAGAIN || err == ECONNABORTED ||
+	    err == ETIMEDOUT)
+	    return 1;
 
 	return 0;
 }
 
-/* timeout callback */
 static void remus_retry_connect_event(event_id_t id, char mode, void *private)
 {
 	struct tdremus_state *s = (struct tdremus_state *)private;
+	int rc, ret;
 
 	/* do a non-blocking connect */
-	if (connect(s->stream_fd.fd, (struct sockaddr *)&s->sa, sizeof(s->sa))
-	    && errno != EINPROGRESS)
-	{
-		if(errno == ECONNREFUSED || errno == ENETUNREACH || errno == EAGAIN || errno == ECONNABORTED)
-		{
-			/* try again in a second */
-			tapdisk_server_unregister_event(s->stream_fd.id);
-			if((id = tapdisk_server_register_event(SCHEDULER_POLL_TIMEOUT, s->stream_fd.fd, REMUS_CONNRETRY_TIMEOUT, remus_retry_connect_event, s)) < 0) {
-				RPRINTF("error registering timeout client connection event handler: %s\n", strerror(id));
-				return;
-			}
-			s->stream_fd.id = id;
-		}
-		else
-		{
-			/* not recoverable */
-			RPRINTF("error connection to server %s\n", strerror(errno));
+	ret = connect(s->stream_fd.fd,
+		      (struct sockaddr *)&s->sa,
+		      sizeof(s->sa));
+	if (ret) {
+		if (errno == EINPROGRESS) {
+			/*
+			 * the connect returned EINPROGRESS (nonblocking
+			 * connect) we must wait for the fd to be writeable
+			 * to determine if the connect worked
+			 */
+			rc = remus_wait_connect_done(s);
+			if (rc)
+				goto fail;
 			return;
 		}
-	}
-	else
-	{
-		/* the connect returned EINPROGRESS (nonblocking connect) we must wait for the fd to be writeable to determine if the connect worked */
 
-		tapdisk_server_unregister_event(s->stream_fd.id);
-		if((id = tapdisk_server_register_event(SCHEDULER_POLL_WRITE_FD, s->stream_fd.fd, 0, remus_connect_event, s)) < 0) {
-			RPRINTF("error registering client connection event handler: %s\n", strerror(id));
+		if (check_connect_errno(errno)) {
+			rc = remus_retry_connect(s);
+			if (rc)
+				goto fail;
 			return;
 		}
-		s->stream_fd.id = id;
+
+		/* not recoverable */
+		RPRINTF("error connection to server %s\n", strerror(errno));
+		rc = ERROR_CONNECTION;
+		goto fail;
 	}
+
+	/* The connection is established unexpectedly */
+	rc = remus_connection_done(s);
+	if (rc)
+		goto fail;
+
+	return;
+
+fail:
+	primary_failed(s, rc);
+	return;
 }
 
 /* callback when nonblocking connect() is finished */
-/* called only by primary in unprotected state */
 static void remus_connect_event(event_id_t id, char mode, void *private)
 {
 	int socket_errno;
 	socklen_t socket_errno_size;
 	struct tdremus_state *s = (struct tdremus_state *)private;
+	int rc;
 
-	/* check to se if the connect succeeded */
+	/* check to see if the connect succeeded */
 	socket_errno_size = sizeof(socket_errno);
-	if (getsockopt(s->stream_fd.fd, SOL_SOCKET, SO_ERROR, &socket_errno, &socket_errno_size)) {
+	if (getsockopt(s->stream_fd.fd, SOL_SOCKET, SO_ERROR,
+		       &socket_errno, &socket_errno_size)) {
 		RPRINTF("error getting socket errno\n");
 		return;
 	}
 
 	RPRINTF("socket connect returned %d\n", socket_errno);
 
-	if(socket_errno)
-	{
+	if (socket_errno) {
 		/* the connect did not succeed */
+		if (check_connect_errno(socket_errno)) {
+			/*
+			 * we can probably assume that the backup is down.
+			 * just try again later
+			 */
+			rc = remus_retry_connect(s);
+			if (rc)
+				goto fail;
 
-		if(socket_errno == ECONNREFUSED || socket_errno == ENETUNREACH || socket_errno == ETIMEDOUT
-		   || socket_errno == ECONNABORTED || socket_errno == EAGAIN)
-		{
-			/* we can probably assume that the backup is down. just try again later */
-			tapdisk_server_unregister_event(s->stream_fd.id);
-			if((id = tapdisk_server_register_event(SCHEDULER_POLL_TIMEOUT, s->stream_fd.fd, REMUS_CONNRETRY_TIMEOUT, remus_retry_connect_event, s)) < 0) {
-				RPRINTF("error registering timeout client connection event handler: %s\n", strerror(id));
-				return;
-			}
-			s->stream_fd.id = id;
-		}
-		else
-		{
-			RPRINTF("socket connect returned %d, giving up\n", socket_errno);
-		}
-	}
-	else
-	{
-		/* the connect succeeded */
-
-		/* unregister this function and register a new event handler */
-		tapdisk_server_unregister_event(s->stream_fd.id);
-		if((id = tapdisk_server_register_event(SCHEDULER_POLL_READ_FD, s->stream_fd.fd, 0, remus_client_event, s)) < 0) {
-			RPRINTF("error registering client event handler: %s\n", strerror(id));
 			return;
+		} else {
+			RPRINTF("socket connect returned %d, giving up\n",
+				socket_errno);
+			rc = ERROR_CONNECTION;
+			goto fail;
 		}
-		s->stream_fd.id = id;
 
-		/* switch from unprotected to protected client */
-		switch_mode(s->tdremus_driver, mode_primary);
+		return;
 	}
+
+	rc = remus_connection_done(s);
+	if (rc)
+		goto fail;
+
+	return;
+
+fail:
+	primary_failed(s, rc);
 }
 
 
-/* we install this event handler on the primary once we have connected to the backup */
+/*
+ * we install this event handler on the primary once we have
+ * connected to the backup.
+ */
 /* wait for "done" message to commit checkpoint */
 static void remus_client_event(event_id_t id, char mode, void *private)
 {
@@ -1043,9 +1069,12 @@ static void remus_client_event(event_id_t id, char mode, void *private)
 	int rc;
 
 	if (mread(s->stream_fd.fd, req, sizeof(req) - 1) < 0) {
-		/* replication stream closed or otherwise broken (timeout, reset, &c) */
+		/*
+		 * replication stream closed or otherwise broken
+		 * (timeout, reset, &c)
+		 */
 		RPRINTF("error reading from backup\n");
-		close_stream_fd(s);
+		primary_failed(s, ERROR_IO);
 		return;
 	}
 
@@ -1056,22 +1085,169 @@ static void remus_client_event(event_id_t id, char mode, void *private)
 		ctl_respond(s, TDREMUS_DONE);
 	else {
 		RPRINTF("received unknown message: %s\n", req);
-		close_stream_fd(s);
+		primary_failed(s, ERROR_IO);
+	}
+
+	return;
+}
+
+static void primary_queue_read(td_driver_t *driver, td_request_t treq)
+{
+	struct tdremus_state *s = (struct tdremus_state *)driver->data;
+	struct req_ring *ring = &s->queued_io;
+
+	if (ring_isempty(ring)) {
+		/* just pass read through */
+		td_forward_request(treq);
+		return;
+	}
+
+	ring_add_request(ring, &treq);
+}
+
+static int primary_forward_request(struct tdremus_state *s,
+				   const td_request_t *treq)
+{
+	char header[sizeof(uint32_t) + sizeof(uint64_t)];
+	uint32_t *sectors = (uint32_t *)header;
+	uint64_t *sector = (uint64_t *)(header + sizeof(uint32_t));
+	td_driver_t *driver = s->tdremus_driver;
+
+	*sectors = treq->secs;
+	*sector = treq->sec;
+
+	if (mwrite(s->stream_fd.fd, TDREMUS_WRITE, strlen(TDREMUS_WRITE)) < 0)
+		return ERROR_IO;
+
+	if (mwrite(s->stream_fd.fd, header, sizeof(header)) < 0)
+		return ERROR_IO;
+
+	if (mwrite(s->stream_fd.fd, treq->buf,
+	    treq->secs * driver->info.sector_size) < 0)
+		return ERROR_IO;
+
+	return 0;
+}
+
+/* TODO:
+ * The primary uses mwrite() to write the contents of a write request to the
+ * backup. This effectively blocks until all data has been copied into a system
+ * buffer or a timeout has occured. We may wish to instead use tapdisk's
+ * nonblocking i/o interface, tapdisk_server_register_event(), to set timeouts
+ * and write data in an asynchronous fashion.
+ */
+static void primary_queue_write(td_driver_t *driver, td_request_t treq)
+{
+	struct tdremus_state *s = (struct tdremus_state *)driver->data;
+	int rc;
+
+	// RPRINTF("write: stream_fd.fd: %d\n", s->stream_fd.fd);
+
+	if(s->stream_fd.fd < 0) {
+		RPRINTF("connecting to backup...\n");
+		rc = primary_do_connect(s);
+		if (rc)
+			goto fail;
+	}
+
+	/* The connection is not established, just queue the request */
+	if (s->stream_fd.cid >= 0) {
+		ring_add_request(&s->queued_io, &treq);
+		return;
 	}
 
+	/* The connection is established */
+	rc = primary_forward_request(s, &treq);
+	if (rc)
+		goto fail;
+
+	td_forward_request(treq);
+
 	return;
+
+fail:
+	/* switch to unprotected mode and forward the request */
+	RPRINTF("write request replication failed, switching to unprotected mode");
+	primary_failed(s, rc);
+	td_forward_request(treq);
+}
+
+/* It is called when the user write "flush" to control file. */
+static int client_flush(td_driver_t *driver)
+{
+	struct tdremus_state *s = (struct tdremus_state *)driver->data;
+
+	// RPRINTF("committing output\n");
+
+	if (s->stream_fd.fd == -1)
+		/* connection not yet established, nothing to flush */
+		return 0;
+
+	if (mwrite(s->stream_fd.fd, TDREMUS_COMMIT,
+	    strlen(TDREMUS_COMMIT)) < 0) {
+		RPRINTF("error flushing output");
+		primary_failed(s, ERROR_IO);
+		return -1;
+	}
+
+	return 0;
+}
+
+/* It is called when switching the mode from primary to unprotected */
+static int primary_flush(td_driver_t *driver)
+{
+	struct tdremus_state *s = driver->data;
+	struct req_ring *ring = &s->queued_io;
+	unsigned int cons;
+
+	if (ring_isempty(ring))
+		return 0;
+
+	while (!ring_isempty(ring)) {
+		cons = ring->cons;
+		ring->cons = ring_next(cons);
+
+		td_forward_request(ring->pending_requests[cons].treq);
+	}
+
+	return 0;
+}
+
+static int primary_start(td_driver_t *driver)
+{
+	struct tdremus_state *s = (struct tdremus_state *)driver->data;
+
+	RPRINTF("activating client mode\n");
+
+	tapdisk_remus.td_queue_read = primary_queue_read;
+	tapdisk_remus.td_queue_write = primary_queue_write;
+	s->queue_flush = primary_flush;
+
+	s->stream_fd.fd = -1;
+	s->stream_fd.cid = -1;
+	s->stream_fd.rid = -1;
+	s->stream_fd.wid = -1;
+
+	return 0;
 }
 
 /* backup functions */
 static void remus_server_event(event_id_t id, char mode, void *private);
 
+/* It is called when we find some I/O error */
+static void backup_failed(struct tdremus_state *s, int rc)
+{
+	close_stream_fd(s);
+	close_server_fd(s);
+	/* We will switch to unprotected mode in backup_queue_write() */
+}
+
 /* returns the socket that receives write requests */
 static void remus_server_accept(event_id_t id, char mode, void* private)
 {
 	struct tdremus_state* s = (struct tdremus_state *) private;
 
 	int stream_fd;
-	event_id_t cid;
 
 	/* XXX: add address-based black/white list */
 	if ((stream_fd = accept(s->server_fd.fd, NULL, NULL)) < 0) {
@@ -1079,68 +1255,80 @@ static void remus_server_accept(event_id_t id, char mode, void* private)
 		return;
 	}
 
-	/* TODO: check to see if we are already replicating. if so just close the
-	 * connection (or do something smarter) */
+	/*
+	 * TODO: check to see if we are already replicating.
+	 * if so just close the connection (or do something
+	 * smarter)
+	 */
 	RPRINTF("server accepted connection\n");
 
 	/* add tapdisk event for replication stream */
-	cid = tapdisk_server_register_event(SCHEDULER_POLL_READ_FD, stream_fd, 0,
-					    remus_server_event, s);
+	id = tapdisk_server_register_event(SCHEDULER_POLL_READ_FD, stream_fd, 0,
+					   remus_server_event, s);
 
-	if(cid < 0) {
-		RPRINTF("error registering connection event handler: %s\n", strerror(errno));
+	if (id < 0) {
+		RPRINTF("error registering connection event handler: %s\n",
+			strerror(errno));
 		close(stream_fd);
 		return;
 	}
 
 	/* store replication file descriptor */
 	s->stream_fd.fd = stream_fd;
-	s->stream_fd.id = cid;
+	s->stream_fd.rid = id;
 }
 
 /* returns -2 if EADDRNOTAVAIL */
 static int remus_bind(struct tdremus_state* s)
 {
-//  struct sockaddr_in sa;
 	int opt;
 	int rc = -1;
+	event_id_t id;
 
 	if ((s->server_fd.fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
 		RPRINTF("could not create server socket: %d\n", errno);
 		return rc;
 	}
-	opt = 1;
-	if (setsockopt(s->server_fd.fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0)
-		RPRINTF("Error setting REUSEADDR on %d: %d\n", s->server_fd.fd, errno);
 
-	if (bind(s->server_fd.fd, (struct sockaddr *)&s->sa, sizeof(s->sa)) < 0) {
-		RPRINTF("could not bind server socket %d to %s:%d: %d %s\n", s->server_fd.fd,
-			inet_ntoa(s->sa.sin_addr), ntohs(s->sa.sin_port), errno, strerror(errno));
-		if (errno != EADDRINUSE)
+	opt = 1;
+	if (setsockopt(s->server_fd.fd, SOL_SOCKET,
+		       SO_REUSEADDR, &opt, sizeof(opt)) < 0)
+		RPRINTF("Error setting REUSEADDR on %d: %d\n",
+			s->server_fd.fd, errno);
+
+	if (bind(s->server_fd.fd, (struct sockaddr *)&s->sa,
+		 sizeof(s->sa)) < 0) {
+		RPRINTF("could not bind server socket %d to %s:%d: %d %s\n",
+			s->server_fd.fd, inet_ntoa(s->sa.sin_addr),
+			ntohs(s->sa.sin_port), errno, strerror(errno));
+		if (errno == EADDRNOTAVAIL)
 			rc = -2;
 		goto err_sfd;
 	}
+
 	if (listen(s->server_fd.fd, 10)) {
 		RPRINTF("could not listen on socket: %d\n", errno);
 		goto err_sfd;
 	}
 
-	/* The socket s now bound to the address and listening so we may now register
-   * the fd with tapdisk */
-
-	if((s->server_fd.id = tapdisk_server_register_event(SCHEDULER_POLL_READ_FD,
-							    s->server_fd.fd, 0,
-							    remus_server_accept, s)) < 0) {
+	/*
+	 * The socket s now bound to the address and listening so we
+	 * may now register the fd with tapdisk
+	 */
+	id =  tapdisk_server_register_event(SCHEDULER_POLL_READ_FD,
+					    s->server_fd.fd, 0,
+					    remus_server_accept, s);
+	if (id < 0) {
 		RPRINTF("error registering server connection event handler: %s",
-			strerror(s->server_fd.id));
+			strerror(id));
 		goto err_sfd;
 	}
+	s->server_fd.cid = id;
 
 	return 0;
 
- err_sfd:
-	close(s->server_fd.fd);
-	s->server_fd.fd = -1;
+err_sfd:
+	CLOSE_FD(s->server_fd.fd);
 
 	return rc;
 }
@@ -1190,10 +1378,21 @@ void backup_queue_write(td_driver_t *driver, td_request_t treq)
 	td_complete_request(treq, -EBUSY);
 }
 
+static int server_flush(td_driver_t *driver)
+{
+	struct tdremus_state *s = (struct tdremus_state *)driver->data;
+	/*
+	 * Nothing to flush in beginning.
+	 */
+	if (!s->ramdisk.prev)
+		return 0;
+	/* Try to flush any remaining requests */
+	return ramdisk_flush(driver, s);
+}
+
 static int backup_start(td_driver_t *driver)
 {
 	struct tdremus_state *s = (struct tdremus_state *)driver->data;
-	int fd;
 
 	if (ramdisk_start(driver) < 0)
 		return -1;
@@ -1201,16 +1400,15 @@ static int backup_start(td_driver_t *driver)
 	tapdisk_remus.td_queue_read = backup_queue_read;
 	tapdisk_remus.td_queue_write = backup_queue_write;
 	s->queue_flush = server_flush;
-	/* TODO set flush function */
 	return 0;
 }
 
-static int server_do_wreq(td_driver_t *driver)
+static void server_do_wreq(td_driver_t *driver)
 {
 	struct tdremus_state *s = (struct tdremus_state *)driver->data;
 	static tdremus_wire_t twreq;
 	char buf[4096];
-	int len, rc;
+	int len, rc = ERROR_IO;
 
 	char header[sizeof(uint32_t) + sizeof(uint64_t)];
 	uint32_t *sectors = (uint32_t *) header;
@@ -1227,39 +1425,40 @@ static int server_do_wreq(td_driver_t *driver)
 	// *sector);
 
 	if (len > sizeof(buf)) {
-		/* freak out! */
-		RPRINTF("write request too large: %d/%u\n", len, (unsigned)sizeof(buf));
-		return -1;
+		/* freak out! How to handle the remaining data from primary */
+		RPRINTF("write request too large: %d/%u\n",
+			len, (unsigned)sizeof(buf));
+		goto err;
 	}
 
 	if (mread(s->stream_fd.fd, buf, len) < 0)
 		goto err;
 
-	if (ramdisk_write(&s->ramdisk, *sector, *sectors, buf) < 0)
+	if (ramdisk_write(&s->ramdisk, *sector, *sectors, buf) < 0) {
+		rc = ERROR_INTERNAL;
 		goto err;
+	}
 
-	return 0;
+	return;
 
  err:
 	/* should start failover */
 	RPRINTF("backup write request error\n");
-	close_stream_fd(s);
-
-	return -1;
+	backup_failed(s, rc);
 }
 
-static int server_do_sreq(td_driver_t *driver)
+static void server_do_sreq(td_driver_t *driver)
 {
 	/*
 	  RPRINTF("submit request received\n");
   */
 
-	return 0;
+	return;
 }
 
 /* at this point, the server can start applying the most recent
  * ramdisk. */
-static int server_do_creq(td_driver_t *driver)
+static void server_do_creq(td_driver_t *driver)
 {
 	struct tdremus_state *s = (struct tdremus_state *)driver->data;
 
@@ -1269,9 +1468,7 @@ static int server_do_creq(td_driver_t *driver)
 
 	/* XXX this message should not be sent until flush completes! */
 	if (write(s->stream_fd.fd, TDREMUS_DONE, strlen(TDREMUS_DONE)) != 4)
-		return -1;
-
-	return 0;
+		backup_failed(s, ERROR_IO);
 }
 
 
@@ -1356,10 +1553,6 @@ static int unprotected_start(td_driver_t *driver)
 
 	RPRINTF("failure detected, activating passthrough\n");
 
-	/* close the server socket */
-	close_stream_fd(s);
-
-	close_server_fd(s);
 
 	/* install the unprotected read/write handlers */
 	tapdisk_remus.td_queue_read = unprotected_queue_read;
@@ -1486,6 +1679,19 @@ static int switch_mode(td_driver_t *driver, enum tdremus_mode mode)
 	return rc;
 }
 
+static void ctl_reopen(struct tdremus_state *s)
+{
+	ctl_unregister(s);
+	CLOSE_FD(s->ctl_fd.fd);
+	RPRINTF("FIFO closed\n");
+
+	if ((s->ctl_fd.fd = open(s->ctl_path, O_RDWR)) < 0) {
+		RPRINTF("error reopening FIFO: %d\n", errno);
+		return;
+	}
+	ctl_register(s);
+}
+
 static void ctl_request(event_id_t id, char mode, void *private)
 {
 	struct tdremus_state *s = (struct tdremus_state *)private;
@@ -1497,12 +1703,6 @@ static void ctl_request(event_id_t id, char mode, void *private)
 
 	if (!(rc = read(s->ctl_fd.fd, msg, sizeof(msg) - 1 /* append nul */))) {
 		RPRINTF("0-byte read received, reopening FIFO\n");
-		/*TODO: we may have to unregister/re-register with tapdisk_server */
-		close(s->ctl_fd.fd);
-		RPRINTF("FIFO closed\n");
-		if ((s->ctl_fd.fd = open(s->ctl_path, O_RDWR)) < 0) {
-			RPRINTF("error reopening FIFO: %d\n", errno);
-		}
 		return;
 	}
 
@@ -1641,10 +1841,11 @@ static int ctl_register(struct tdremus_state *s)
 	RPRINTF("registering ctl fifo\n");
 
 	/* register ctl fd */
-	s->ctl_fd.id = tapdisk_server_register_event(SCHEDULER_POLL_READ_FD, s->ctl_fd.fd, 0, ctl_request, s);
+	s->ctl_fd.cid = tapdisk_server_register_event(SCHEDULER_POLL_READ_FD, s->ctl_fd.fd, 0, ctl_request, s);
 
-	if (s->ctl_fd.id < 0) {
-		RPRINTF("error registering ctrl FIFO %s: %d\n", s->ctl_path, s->ctl_fd.id);
+	if (s->ctl_fd.cid < 0) {
+		RPRINTF("error registering ctrl FIFO %s: %d\n",
+			s->ctl_path, s->ctl_fd.cid);
 		return -1;
 	}
 
@@ -1655,10 +1856,7 @@ static void ctl_unregister(struct tdremus_state *s)
 {
 	RPRINTF("unregistering ctl fifo\n");
 
-	if (s->ctl_fd.id >= 0) {
-		tapdisk_server_unregister_event(s->ctl_fd.id);
-		s->ctl_fd.id = -1;
-	}
+	UNREGISTER_EVENT(s->ctl_fd.cid);
 }
 
 /* interface */
Signature · 1 line
1.9.3
#36 lore raw

RFC PATCH v2 36/45 switch to unprotected mode before closing

In reply to Wen Congyang

If the user wants to stop tapdisk2, he will do the following thing:
1. close the image
2. detach from blktap device

If there is some pending I/O request, close will fail. But the I/O request is pended in remus until the connection is established. Introduce a new callback td_pre_close() to flush these I/O requests.

---
 tools/blktap2/drivers/block-remus.c       | 15 +++++++++++++++
 tools/blktap2/drivers/tapdisk-control.c   |  6 ++++++
 tools/blktap2/drivers/tapdisk-interface.c | 18 ++++++++++++++++++
 tools/blktap2/drivers/tapdisk-interface.h |  1 +
 tools/blktap2/drivers/tapdisk-vbd.c       |  9 +++++++++
 tools/blktap2/drivers/tapdisk-vbd.h       |  1 +
 tools/blktap2/drivers/tapdisk.h           |  1 +
 7 files changed, 51 insertions(+)
tools/blktap2/drivers/block-remus.c +15 -0
index c21f851..5d27d41 100644
@@ -96,6 +96,7 @@ enum {
 	ERROR_INTERNAL = -1,
 	ERROR_IO = -2,
 	ERROR_CONNECTION = -3,
+	ERROR_CLOSE = -4,
 };
 
 struct tdremus_req {
@@ -810,6 +811,8 @@ static void primary_failed(struct tdremus_state *s, int rc)
 	close_stream_fd(s);
 	if (rc == ERROR_INTERNAL)
 		RPRINTF("switch to unprotected mode due to internal error");
+	if (rc == ERROR_CLOSE)
+		RPRINTF("switch to unprotected mode before closing");
 	switch_mode(s->tdremus_driver, mode_unprotected);
 }
 
@@ -1910,6 +1913,17 @@ static int tdremus_open(td_driver_t *driver, td_image_t *image, td_uuid_t uuid)
 	return -EIO;
 }
 
+static int tdremus_pre_close(td_driver_t *driver)
+{
+	struct tdremus_state *s = (struct tdremus_state *)driver->data;
+
+	if (s->mode != mode_primary)
+		return 0;
+
+	primary_failed(s, ERROR_CLOSE);
+	return 0;
+}
+
 static int tdremus_close(td_driver_t *driver)
 {
 	struct tdremus_state *s = (struct tdremus_state *)driver->data;
@@ -1944,6 +1958,7 @@ struct tap_disk tapdisk_remus = {
 	.td_open            = tdremus_open,
 	.td_queue_read      = unprotected_queue_read,
 	.td_queue_write     = unprotected_queue_write,
+	.td_pre_close       = tdremus_pre_close,
 	.td_close           = tdremus_close,
 	.td_get_parent_id   = tdremus_get_parent_id,
 	.td_validate_parent = tdremus_validate_parent,
tools/blktap2/drivers/tapdisk-control.c +6 -0
index 4e5f748..2fa4cbe 100644
@@ -508,6 +508,12 @@ tapdisk_control_close_image(struct tapdisk_control_connection *connection,
 		goto out;
 	}
 
+	/*
+	 * Some I/O requests are pended in the driver, and
+	 * flush these requests first.
+	 */
+	tapdisk_vbd_pre_close_vdi(vbd);
+
 	if (!list_empty(&vbd->pending_requests)) {
 		err = -EAGAIN;
 		goto out;
tools/blktap2/drivers/tapdisk-interface.c +18 -0
index a29de64..ed92e12 100644
@@ -105,6 +105,24 @@ td_open(td_image_t *image)
 }
 
 int
+td_pre_close(td_image_t *image)
+{
+	td_driver_t *driver;
+
+	driver = image->driver;
+	if (!driver)
+		return -ENODEV;
+
+	if (!driver->ops->td_pre_close)
+		return 0;
+
+	if (driver->refcnt && td_flag_test(driver->state, TD_DRIVER_OPEN))
+		driver->ops->td_pre_close(driver);
+
+	return 0;
+}
+
+int
 td_close(td_image_t *image)
 {
 	td_driver_t *driver;
tools/blktap2/drivers/tapdisk-interface.h +1 -0
index adc4376..ba9b3ea 100644
@@ -34,6 +34,7 @@
 int td_open(td_image_t *);
 int __td_open(td_image_t *, td_disk_info_t *);
 int td_load(td_image_t *);
+int td_pre_close(td_image_t *);
 int td_close(td_image_t *);
 int td_get_parent_id(td_image_t *, td_disk_id_t *);
 int td_validate_parent(td_image_t *, td_image_t *);
tools/blktap2/drivers/tapdisk-vbd.c +9 -0
index c665f27..aba545b 100644
@@ -180,6 +180,15 @@ tapdisk_vbd_validate_chain(td_vbd_t *vbd)
 }
 
 void
+tapdisk_vbd_pre_close_vdi(td_vbd_t *vbd)
+{
+	td_image_t *image, *tmp;
+
+	tapdisk_vbd_for_each_image(vbd, image, tmp)
+		td_pre_close(image);
+}
+
+void
 tapdisk_vbd_close_vdi(td_vbd_t *vbd)
 {
 	td_image_t *image, *tmp;
tools/blktap2/drivers/tapdisk-vbd.h +1 -0
index be084b2..040f2b8 100644
@@ -181,6 +181,7 @@ void tapdisk_vbd_free_stack(td_vbd_t *);
 int tapdisk_vbd_open_stack(td_vbd_t *, uint16_t, td_flag_t);
 int tapdisk_vbd_open_vdi(td_vbd_t *, const char *,
 			 uint16_t, uint16_t, td_flag_t);
+void tapdisk_vbd_pre_close_vdi(td_vbd_t *);
 void tapdisk_vbd_close_vdi(td_vbd_t *);
 
 int tapdisk_vbd_attach(td_vbd_t *, const char *, int);
tools/blktap2/drivers/tapdisk.h +1 -0
index 3c3b51d..16efd07 100644
@@ -158,6 +158,7 @@ struct tap_disk {
 	td_flag_t                    flags;
 	int                          private_data_size;
 	int (*td_open)               (td_driver_t *, td_image_t *, td_uuid_t);
+	int (*td_pre_close)          (td_driver_t *);
 	int (*td_close)              (td_driver_t *);
 	int (*td_get_parent_id)      (td_driver_t *, td_disk_id_t *);
 	int (*td_validate_parent)    (td_driver_t *, td_driver_t *, td_flag_t);
Signature · 1 line
1.9.3
#37 lore raw

RFC PATCH v2 37/45 blktap2: move async connect related codes to block-replication.c

In reply to Wen Congyang

COLO will reuse them.

---
 tools/blktap2/drivers/Makefile            |   2 +-
 tools/blktap2/drivers/block-remus.c       | 494 +++---------------------------
 tools/blktap2/drivers/block-replication.c | 468 ++++++++++++++++++++++++++++
 tools/blktap2/drivers/block-replication.h | 113 +++++++
 4 files changed, 630 insertions(+), 447 deletions(-)
 create mode 100644 tools/blktap2/drivers/block-replication.c
 create mode 100644 tools/blktap2/drivers/block-replication.h
tools/blktap2/drivers/Makefile +1 -1
index 1129ca1..c7a2ca4 100644
@@ -23,7 +23,7 @@ endif
 
 VHDLIBS    := -L$(LIBVHDDIR) -lvhd
 
-REMUS-OBJS  := block-remus.o
+REMUS-OBJS  := block-remus.o block-replication.o
 REMUS-OBJS  += hashtable.o
 REMUS-OBJS  += hashtable_itr.o
 REMUS-OBJS  += hashtable_utility.o
tools/blktap2/drivers/block-remus.c +48 -446

669 lines not on this page · raw message

tools/blktap2/drivers/block-replication.c new file +468 -0

468 lines not on this page · raw message

tools/blktap2/drivers/block-replication.h new file +113 -0

113 lines not on this page · raw message

Signature · 1 line
1.9.3
#38 lore raw

RFC PATCH v2 38/45 blktap2: move ramdisk related codes to block-replication.c

In reply to Wen Congyang

COLO will reuse them

---
 tools/blktap2/drivers/block-remus.c       | 485 ++----------------------------
 tools/blktap2/drivers/block-replication.c | 452 ++++++++++++++++++++++++++++
 tools/blktap2/drivers/block-replication.h |  48 +++
 3 files changed, 523 insertions(+), 462 deletions(-)
tools/blktap2/drivers/block-remus.c +23 -462
index 8b6f157..2713af1 100644
@@ -37,9 +37,6 @@
 #include "tapdisk-server.h"
 #include "tapdisk-driver.h"
 #include "tapdisk-interface.h"
-#include "hashtable.h"
-#include "hashtable_itr.h"
-#include "hashtable_utility.h"
 #include "block-replication.h"
 
 #include <errno.h>
@@ -58,7 +55,6 @@
 
 /* timeout for reads and writes in ms */
 #define HEARTBEAT_MS 1000
-#define RAMDISK_HASHSIZE 128
 
 /* connect retry timeout (seconds) */
 #define REMUS_CONNRETRY_TIMEOUT 1
@@ -97,51 +93,6 @@ td_vbd_t *device_vbd = NULL;
 td_image_t *remus_image = NULL;
 struct tap_disk tapdisk_remus;
 
-struct ramdisk {
-	size_t sector_size;
-	struct hashtable* h;
-	/* when a ramdisk is flushed, h is given a new empty hash for writes
-	 * while the old ramdisk (prev) is drained asynchronously.
-	 */
-	struct hashtable* prev;
-	/* count of outstanding requests to the base driver */
-	size_t inflight;
-	/* prev holds the requests to be flushed, while inprogress holds
-	 * requests being flushed. When requests complete, they are removed
-	 * from inprogress.
-	 * Whenever a new flush is merged with ongoing flush (i.e, prev),
-	 * we have to make sure that none of the new requests overlap with
-	 * ones in "inprogress". If it does, keep it back in prev and dont issue
-	 * IO until the current one finishes. If we allow this IO to proceed,
-	 * we might end up with two "overlapping" requests in the disk's queue and
-	 * the disk may not offer any guarantee on which one is written first.
-	 * IOW, make sure we dont create a write-after-write time ordering constraint.
-	 * 
-	 */
-	struct hashtable* inprogress;
-};
-
-/* the ramdisk intercepts the original callback for reads and writes.
- * This holds the original data. */
-/* Might be worth making this a static array in struct ramdisk to avoid
- * a malloc per request */
-
-struct tdremus_state;
-
-struct ramdisk_cbdata {
-	td_callback_t cb;
-	void* private;
-	char* buf;
-	struct tdremus_state* state;
-};
-
-struct ramdisk_write_cbdata {
-	struct tdremus_state* state;
-	char* buf;
-};
-
-typedef void (*queue_rw_t) (td_driver_t *driver, td_request_t treq);
-
 typedef struct poll_fd {
 	int        fd;
 	event_id_t id;
@@ -167,8 +118,14 @@ struct tdremus_state {
 	 */
 	struct req_ring queued_io;
 
-	/* ramdisk data*/
+	/* ramdisk data */
 	struct ramdisk ramdisk;
+	/*
+	 * The primary write request is queued in this
+	 * hashtable, and will be flushed to ramdisk when
+	 * the checkpoint finishes.
+	 */
+	struct hashtable *h;
 
 	/* mode methods */
 	enum tdremus_mode mode;
@@ -239,404 +196,20 @@ static void ring_add_request(struct req_ring *ring, const td_request_t *treq)
 	ring->prod = ring_next(ring->prod);
 }
 
-/* Prototype declarations */
-static int ramdisk_flush(td_driver_t *driver, struct tdremus_state* s);
-
-/* functions to create and sumbit treq's */
-
-static void
-replicated_write_callback(td_request_t treq, int err)
-{
-	struct tdremus_state *s = (struct tdremus_state *) treq.cb_data;
-	td_vbd_request_t *vreq;
-	int i;
-	uint64_t start;
-	vreq = (td_vbd_request_t *) treq.private;
-
-	/* the write failed for now, lets panic. this is very bad */
-	if (err) {
-		RPRINTF("ramdisk write failed, disk image is not consistent\n");
-		exit(-1);
-	}
-
-	/* The write succeeded. let's pull the vreq off whatever request list
-	 * it is on and free() it */
-	list_del(&vreq->next);
-	free(vreq);
-
-	s->ramdisk.inflight--;
-	start = treq.sec;
-	for (i = 0; i < treq.secs; i++) {
-		hashtable_remove(s->ramdisk.inprogress, &start);
-		start++;
-	}
-	free(treq.buf);
-
-	if (!s->ramdisk.inflight && !s->ramdisk.prev) {
-		/* TODO: the ramdisk has been flushed */
-	}
-}
-
-static inline int
-create_write_request(struct tdremus_state *state, td_sector_t sec, int secs, char *buf)
-{
-	td_request_t treq;
-	td_vbd_request_t *vreq;
-
-	treq.op      = TD_OP_WRITE;
-	treq.buf     = buf;
-	treq.sec     = sec;
-	treq.secs    = secs;
-	treq.image   = remus_image;
-	treq.cb      = replicated_write_callback;
-	treq.cb_data = state;
-	treq.id      = 0;
-	treq.sidx    = 0;
-
-	vreq         = calloc(1, sizeof(td_vbd_request_t));
-	treq.private = vreq;
-
-	if(!vreq)
-		return -1;
-
-	vreq->submitting = 1;
-	INIT_LIST_HEAD(&vreq->next);
-	tapdisk_vbd_move_request(treq.private, &device_vbd->pending_requests);
-
-	/* TODO:
-	 * we should probably leave it up to the caller to forward the request */
-	td_forward_request(treq);
-
-	vreq->submitting--;
-
-	return 0;
-}
-
-
-/* http://www.concentric.net/~Ttwang/tech/inthash.htm */
-static unsigned int uint64_hash(void* k)
-{
-	uint64_t key = *(uint64_t*)k;
-
-	key = (~key) + (key << 18);
-	key = key ^ (key >> 31);
-	key = key * 21;
-	key = key ^ (key >> 11);
-	key = key + (key << 6);
-	key = key ^ (key >> 22);
-
-	return (unsigned int)key;
-}
-
-static int rd_hash_equal(void* k1, void* k2)
-{
-	uint64_t key1, key2;
-
-	key1 = *(uint64_t*)k1;
-	key2 = *(uint64_t*)k2;
-
-	return key1 == key2;
-}
-
-static int ramdisk_read(struct ramdisk* ramdisk, uint64_t sector,
-			int nb_sectors, char* buf)
-{
-	int i;
-	char* v;
-	uint64_t key;
-
-	for (i = 0; i < nb_sectors; i++) {
-		key = sector + i;
-		/* check whether it is queued in a previous flush request */
-		if (!(ramdisk->prev && (v = hashtable_search(ramdisk->prev, &key)))) {
-			/* check whether it is an ongoing flush */
-			if (!(ramdisk->inprogress && (v = hashtable_search(ramdisk->inprogress, &key))))
-				return -1;
-		}
-		memcpy(buf + i * ramdisk->sector_size, v, ramdisk->sector_size);
-	}
-
-	return 0;
-}
-
-static int ramdisk_write_hash(struct hashtable* h, uint64_t sector, char* buf,
-			      size_t len)
-{
-	char* v;
-	uint64_t* key;
-
-	if ((v = hashtable_search(h, &sector))) {
-		memcpy(v, buf, len);
-		return 0;
-	}
-
-	if (!(v = malloc(len))) {
-		DPRINTF("ramdisk_write_hash: malloc failed\n");
-		return -1;
-	}
-	memcpy(v, buf, len);
-	if (!(key = malloc(sizeof(*key)))) {
-		DPRINTF("ramdisk_write_hash: error allocating key\n");
-		free(v);
-		return -1;
-	}
-	*key = sector;
-	if (!hashtable_insert(h, key, v)) {
-		DPRINTF("ramdisk_write_hash failed on sector %" PRIu64 "\n", sector);
-		free(key);
-		free(v);
-		return -1;
-	}
-
-	return 0;
-}
-
-static inline int ramdisk_write(struct ramdisk* ramdisk, uint64_t sector,
-				int nb_sectors, char* buf)
-{
-	int i, rc;
-
-	for (i = 0; i < nb_sectors; i++) {
-		rc = ramdisk_write_hash(ramdisk->h, sector + i,
-					buf + i * ramdisk->sector_size,
-					ramdisk->sector_size);
-		if (rc)
-			return rc;
-	}
-
-	return 0;
-}
-
-static int uint64_compare(const void* k1, const void* k2)
-{
-	uint64_t u1 = *(uint64_t*)k1;
-	uint64_t u2 = *(uint64_t*)k2;
-
-	/* u1 - u2 is unsigned */
-	return u1 < u2 ? -1 : u1 > u2 ? 1 : 0;
-}
-
-/* set psectors to an array of the sector numbers in the hash, returning
- * the number of entries (or -1 on error) */
-static int ramdisk_get_sectors(struct hashtable* h, uint64_t** psectors)
-{
-	struct hashtable_itr* itr;
-	uint64_t* sectors;
-	int count;
-
-	if (!(count = hashtable_count(h)))
-		return 0;
-
-	if (!(*psectors = malloc(count * sizeof(uint64_t)))) {
-		DPRINTF("ramdisk_get_sectors: error allocating sector map\n");
-		return -1;
-	}
-	sectors = *psectors;
-
-	itr = hashtable_iterator(h);
-	count = 0;
-	do {
-		sectors[count++] = *(uint64_t*)hashtable_iterator_key(itr);
-	} while (hashtable_iterator_advance(itr));
-	free(itr);
-
-	return count;
-}
-
-/*
-  return -1 for OOM
-  return -2 for merge lookup failure
-  return -3 for WAW race
-  return 0 on success.
-*/
-static int merge_requests(struct ramdisk* ramdisk, uint64_t start,
-			size_t count, char **mergedbuf)
-{
-	char* buf;
-	char* sector;
-	int i;
-	uint64_t *key;
-	int rc = 0;
-
-	if (!(buf = valloc(count * ramdisk->sector_size))) {
-		DPRINTF("merge_request: allocation failed\n");
-		return -1;
-	}
-
-	for (i = 0; i < count; i++) {
-		if (!(sector = hashtable_search(ramdisk->prev, &start))) {
-			DPRINTF("merge_request: lookup failed on %"PRIu64"\n", start);
-			free(buf);
-			rc = -2;
-			goto fail;
-		}
-
-		/* Check inprogress requests to avoid waw non-determinism */
-		if (hashtable_search(ramdisk->inprogress, &start)) {
-			DPRINTF("merge_request: WAR RACE on %"PRIu64"\n", start);
-			free(buf);
-			rc = -3;
-			goto fail;
-		}
-		/* Insert req into inprogress (brief period of duplication of hash entries until
-		 * they are removed from prev. Read tracking would not be reading wrong entries)
-		 */
-		if (!(key = malloc(sizeof(*key)))) {
-			DPRINTF("%s: error allocating key\n", __FUNCTION__);
-			free(buf);			
-			rc = -1;
-			goto fail;
-		}
-		*key = start;
-		if (!hashtable_insert(ramdisk->inprogress, key, NULL)) {
-			DPRINTF("%s failed to insert sector %" PRIu64 " into inprogress hash\n", 
-				__FUNCTION__, start);
-			free(key);
-			free(buf);
-			rc = -1;
-			goto fail;
-		}
-		memcpy(buf + i * ramdisk->sector_size, sector, ramdisk->sector_size);
-		start++;
-	}
-
-	*mergedbuf = buf;
-	return 0;
-fail:
-	for (start--; i >0; i--, start--)
-		hashtable_remove(ramdisk->inprogress, &start);
-	return rc;
-}
-
-/* The underlying driver may not handle having the whole ramdisk queued at
- * once. We queue what we can and let the callbacks attempt to queue more. */
-/* NOTE: may be called from callback, while dd->private still belongs to
- * the underlying driver */
-static int ramdisk_flush(td_driver_t *driver, struct tdremus_state* s)
-{
-	uint64_t* sectors;
-	char* buf = NULL;
-	uint64_t base, batchlen;
-	int i, j, count = 0;
-
-	// RPRINTF("ramdisk flush\n");
-
-	if ((count = ramdisk_get_sectors(s->ramdisk.prev, &sectors)) <= 0)
-		return count;
-
-	/* Create the inprogress table if empty */
-	if (!s->ramdisk.inprogress)
-		s->ramdisk.inprogress = create_hashtable(RAMDISK_HASHSIZE,
-							uint64_hash,
-							rd_hash_equal);
-	
-	/*
-	  RPRINTF("ramdisk: flushing %d sectors\n", count);
-	*/
-
-	/* sort and merge sectors to improve disk performance */
-	qsort(sectors, count, sizeof(*sectors), uint64_compare);
-
-	for (i = 0; i < count;) {
-		base = sectors[i++];
-		while (i < count && sectors[i] == sectors[i-1] + 1)
-			i++;
-		batchlen = sectors[i-1] - base + 1;
-
-		j = merge_requests(&s->ramdisk, base, batchlen, &buf);
-			
-		if (j) {
-			RPRINTF("ramdisk_flush: merge_requests failed:%s\n",
-				j == -1? "OOM": (j==-2? "missing sector" : "WAW race"));
-			if (j == -3) continue;
-			free(sectors);
-			return -1;
-		}
-
-		/* NOTE: create_write_request() creates a treq AND forwards it down
-		 * the driver chain */
-		// RPRINTF("forwarding write request at %" PRIu64 ", length: %" PRIu64 "\n", base, batchlen);
-		create_write_request(s, base, batchlen, buf);
-		//RPRINTF("write request at %" PRIu64 ", length: %" PRIu64 " forwarded\n", base, batchlen);
-
-		s->ramdisk.inflight++;
-
-		for (j = 0; j < batchlen; j++) {
-			buf = hashtable_search(s->ramdisk.prev, &base);
-			free(buf);
-			hashtable_remove(s->ramdisk.prev, &base);
-			base++;
-		}
-	}
-
-	if (!hashtable_count(s->ramdisk.prev)) {
-		/* everything is in flight */
-		hashtable_destroy(s->ramdisk.prev, 0);
-		s->ramdisk.prev = NULL;
-	}
-
-	free(sectors);
-
-	// RPRINTF("ramdisk flush done\n");
-	return 0;
-}
-
-/* flush ramdisk contents to disk */
-static int ramdisk_start_flush(td_driver_t *driver)
-{
-	struct tdremus_state *s = (struct tdremus_state *)driver->data;
-	uint64_t* key;
-	char* buf;
-	int rc = 0;
-	int i, j, count, batchlen;
-	uint64_t* sectors;
-
-	if (!hashtable_count(s->ramdisk.h)) {
-		/*
-		  RPRINTF("Nothing to flush\n");
-		*/
-		return 0;
-	}
-
-	if (s->ramdisk.prev) {
-		/* a flush request issued while a previous flush is still in progress
-		 * will merge with the previous request. If you want the previous
-		 * request to be consistent, wait for it to complete. */
-		if ((count = ramdisk_get_sectors(s->ramdisk.h, &sectors)) < 0)
-			return count;
-
-		for (i = 0; i < count; i++) {
-			buf = hashtable_search(s->ramdisk.h, sectors + i);
-			ramdisk_write_hash(s->ramdisk.prev, sectors[i], buf,
-					   s->ramdisk.sector_size);
-		}
-		free(sectors);
-
-		hashtable_destroy (s->ramdisk.h, 1);
-	} else
-		s->ramdisk.prev = s->ramdisk.h;
-
-	/* We create a new hashtable so that new writes can be performed before
-	 * the old hashtable is completely drained. */
-	s->ramdisk.h = create_hashtable(RAMDISK_HASHSIZE, uint64_hash,
-					rd_hash_equal);
-
-	return ramdisk_flush(driver, s);
-}
-
-
 static int ramdisk_start(td_driver_t *driver)
 {
 	struct tdremus_state *s = (struct tdremus_state *)driver->data;
 
-	if (s->ramdisk.h) {
+	if (s->h) {
 		RPRINTF("ramdisk already allocated\n");
 		return 0;
 	}
 
 	s->ramdisk.sector_size = driver->info.sector_size;
-	s->ramdisk.h = create_hashtable(RAMDISK_HASHSIZE, uint64_hash,
-					rd_hash_equal);
+	s->ramdisk.log_prefix = "remus";
+	s->ramdisk.image = remus_image;
+	ramdisk_init(&s->ramdisk);
+	s->h = ramdisk_new_hashtable();
 
 	DPRINTF("Ramdisk started, %zu bytes/sector\n", s->ramdisk.sector_size);
 
@@ -1024,10 +597,7 @@ static inline int server_writes_inflight(td_driver_t *driver)
 {
 	struct tdremus_state *s = (struct tdremus_state *)driver->data;
 
-	if (!s->ramdisk.inflight && !s->ramdisk.prev)
-		return 0;
-
-	return 1;
+	return ramdisk_writes_inflight(&s->ramdisk);
 }
 
 /* Due to block device prefetching this code may be called on the server side
@@ -1067,13 +637,9 @@ void backup_queue_write(td_driver_t *driver, td_request_t treq)
 static int server_flush(td_driver_t *driver)
 {
 	struct tdremus_state *s = (struct tdremus_state *)driver->data;
-	/*
-	 * Nothing to flush in beginning.
-	 */
-	if (!s->ramdisk.prev)
-		return 0;
+
 	/* Try to flush any remaining requests */
-	return ramdisk_flush(driver, s);
+	return ramdisk_flush(&s->ramdisk);
 }
 
 static int backup_start(td_driver_t *driver)
@@ -1120,7 +686,9 @@ static void server_do_wreq(td_driver_t *driver)
 	if (mread(s->stream_fd.fd, buf, len) < 0)
 		goto err;
 
-	if (ramdisk_write(&s->ramdisk, *sector, *sectors, buf) < 0) {
+	if (ramdisk_write_to_hashtable(s->h, *sector, *sectors,
+				       driver->info.sector_size, buf,
+				       "remus") < 0) {
 		rc = ERROR_INTERNAL;
 		goto err;
 	}
@@ -1150,7 +718,7 @@ static void server_do_creq(td_driver_t *driver)
 
 	// RPRINTF("committing buffer\n");
 
-	ramdisk_start_flush(driver);
+	ramdisk_start_flush(&s->ramdisk, &s->h);
 
 	/* XXX this message should not be sent until flush completes! */
 	if (write(s->stream_fd.fd, TDREMUS_DONE, strlen(TDREMUS_DONE)) != 4)
@@ -1199,12 +767,7 @@ void unprotected_queue_read(td_driver_t *driver, td_request_t treq)
 
 	/* wait for previous ramdisk to flush  before servicing reads */
 	if (server_writes_inflight(driver)) {
-		/* for now lets just return EBUSY.
-		 * if there are any left-over requests in prev,
-		 * kick em again.
-		 */
-		if(!s->ramdisk.inflight) /* nothing in inprogress */
-			ramdisk_flush(driver, s);
+		ramdisk_flush(&s->ramdisk);
 
 		td_complete_request(treq, -EBUSY);
 	}
@@ -1222,8 +785,7 @@ void unprotected_queue_write(td_driver_t *driver, td_request_t treq)
 	/* wait for previous ramdisk to flush */
 	if (server_writes_inflight(driver)) {
 		RPRINTF("queue_write: waiting for queue to drain");
-		if(!s->ramdisk.inflight) /* nothing in inprogress. Kick prev */
-			ramdisk_flush(driver, s);
+		ramdisk_flush(&s->ramdisk);
 		td_complete_request(treq, -EBUSY);
 	}
 	else {
@@ -1532,9 +1094,8 @@ static int tdremus_close(td_driver_t *driver)
 	struct tdremus_state *s = (struct tdremus_state *)driver->data;
 
 	RPRINTF("closing\n");
-	if (s->ramdisk.inprogress)
-		hashtable_destroy(s->ramdisk.inprogress, 0);
-
+	ramdisk_destroy(&s->ramdisk);
+	ramdisk_destroy_hashtable(s->h);
 	td_replication_connect_kill(&s->t);
 	ctl_unregister(s);
 	ctl_close(s);
tools/blktap2/drivers/block-replication.c +452 -0

467 lines not on this page · raw message

tools/blktap2/drivers/block-replication.h +48 -0

52 lines not on this page · raw message

Signature · 1 line
1.9.3
#39 lore raw

RFC PATCH v2 39/45 block-colo: implement colo disk replication

In reply to Wen Congyang

TODO:
update block-remus to use async io to instead
of mread/mwrite.

---
 tools/blktap2/drivers/Makefile            |    3 +
 tools/blktap2/drivers/block-colo.c        | 1151 +++++++++++++++++++++++++++++
 tools/blktap2/drivers/block-replication.c |  196 +++++
 tools/blktap2/drivers/block-replication.h |   56 ++
 tools/blktap2/drivers/tapdisk-disktype.c  |    9 +
 tools/blktap2/drivers/tapdisk-disktype.h  |    3 +-
 6 files changed, 1417 insertions(+), 1 deletion(-)
 create mode 100644 tools/blktap2/drivers/block-colo.c
tools/blktap2/drivers/Makefile +3 -0
index c7a2ca4..0f91ccf 100644
@@ -28,6 +28,8 @@ REMUS-OBJS  += hashtable.o
 REMUS-OBJS  += hashtable_itr.o
 REMUS-OBJS  += hashtable_utility.o
 
+COLO-OBJS += block-colo.o
+
 tapdisk2 tapdisk-stream tapdisk-diff $(QCOW_UTIL): AIOLIBS := -laio
 
 MEMSHRLIBS :=
@@ -74,6 +76,7 @@ BLK-OBJS-y  += aes.o
 BLK-OBJS-y  += md5.o
 BLK-OBJS-y  += $(PORTABLE-OBJS-y)
 BLK-OBJS-y  += $(REMUS-OBJS)
+BLK-OBJS-y  += $(COLO-OBJS)
 
 all: $(IBIN) lock-util qcow-util
 
tools/blktap2/drivers/block-colo.c new file +1151 -0

1,151 lines not on this page · raw message

tools/blktap2/drivers/block-replication.c +196 -0

205 lines not on this page · raw message

tools/blktap2/drivers/block-replication.h +56 -0

62 lines not on this page · raw message

tools/blktap2/drivers/tapdisk-disktype.c +9 -0
index 8d1383b..aa2afab 100644
@@ -94,6 +94,12 @@ static const disk_info_t remus_disk = {
        0,
 };
 
+static const disk_info_t colo_disk = {
+	"colo",
+	"colo disk replicator (COLO)",
+	0,
+};
+
 const disk_info_t *tapdisk_disk_types[] = {
 	[DISK_TYPE_AIO]	= &aio_disk,
 	[DISK_TYPE_SYNC]	= &sync_disk,
@@ -105,6 +111,7 @@ const disk_info_t *tapdisk_disk_types[] = {
 	[DISK_TYPE_BLOCK_CACHE] = &block_cache_disk,
 	[DISK_TYPE_LOG]	= &log_disk,
 	[DISK_TYPE_REMUS]	= &remus_disk,
+	[DISK_TYPE_COLO]	= &colo_disk,
 	[DISK_TYPE_MAX]		= NULL,
 };
 
@@ -119,6 +126,7 @@ extern struct tap_disk tapdisk_block_cache;
 extern struct tap_disk tapdisk_vhd_index;
 extern struct tap_disk tapdisk_log;
 extern struct tap_disk tapdisk_remus;
+extern struct tap_disk tapdisk_colo;
 
 const struct tap_disk *tapdisk_disk_drivers[] = {
 	[DISK_TYPE_AIO]         = &tapdisk_aio,
@@ -132,6 +140,7 @@ const struct tap_disk *tapdisk_disk_drivers[] = {
 	[DISK_TYPE_BLOCK_CACHE] = &tapdisk_block_cache,
 	[DISK_TYPE_LOG]         = &tapdisk_log,
 	[DISK_TYPE_REMUS]       = &tapdisk_remus,
+	[DISK_TYPE_COLO]        = &tapdisk_colo,
 	[DISK_TYPE_MAX]         = NULL,
 };
 
tools/blktap2/drivers/tapdisk-disktype.h +2 -1
index c574990..ee8cb02 100644
@@ -39,7 +39,8 @@
 #define DISK_TYPE_BLOCK_CACHE 7
 #define DISK_TYPE_LOG         8
 #define DISK_TYPE_REMUS       9
-#define DISK_TYPE_MAX         10
+#define DISK_TYPE_COLO        10
+#define DISK_TYPE_MAX         11
 
 #define DISK_TYPE_NAME_MAX    32
 
Signature · 1 line
1.9.3
#40 lore raw

RFC PATCH v2 40/45 pass correct file to qemu if we use blktap2

In reply to Wen Congyang

If we use blktap2, the correct file should be blktap device not the pdev_path.

---
 tools/libxl/libxl_dm.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
tools/libxl/libxl_dm.c +9 -2
index addacdb..d86ce15 100644
@@ -680,6 +680,7 @@ static char ** libxl__build_device_model_args_new(libxl__gc *gc,
                 libxl__device_disk_dev_number(disks[i].vdev, &disk, &part);
             const char *format = qemu_disk_format_string(disks[i].format);
             char *drive;
+            const char *pdev_path;
 
             if (dev_number == -1) {
                 LIBXL__LOG(ctx, LIBXL__LOG_WARNING, "unable to determine"
@@ -709,6 +710,12 @@ static char ** libxl__build_device_model_args_new(libxl__gc *gc,
                     continue;
                 }
 
+                if (disks[i].backend == LIBXL_DISK_BACKEND_TAP)
+                    pdev_path = libxl__blktap_devpath(gc, disks[i].pdev_path,
+                                                      disks[i].format);
+                else
+                    pdev_path = disks[i].pdev_path;
+
                 /*
                  * Explicit sd disks are passed through as is.
                  *
@@ -718,11 +725,11 @@ static char ** libxl__build_device_model_args_new(libxl__gc *gc,
                 if (strncmp(disks[i].vdev, "sd", 2) == 0)
                     drive = libxl__sprintf
                         (gc, "file=%s,if=scsi,bus=0,unit=%d,format=%s,cache=writeback",
-                         disks[i].pdev_path, disk, format);
+                         pdev_path, disk, format);
                 else if (disk < 4)
                     drive = libxl__sprintf
                         (gc, "file=%s,if=ide,index=%d,media=disk,format=%s,cache=writeback",
-                         disks[i].pdev_path, disk, format);
+                         pdev_path, disk, format);
                 else
                     continue; /* Do not emulate this disk */
             }
Signature · 1 line
1.9.3
#41 lore raw

RFC PATCH v2 41/45 support blktap remus in xl

In reply to Wen Congyang

With this patch, we can use blktap remus like this:
disk = [ 'format=remus,devtype=disk,access=w,vdev=hda,backendtype=tap,target=192.168.3.1:9000|aio:filename' ]

---
 tools/libxl/libxl_blktap2.c   | 33 +++++++++++++++++++++++++++++++++
 tools/libxl/libxl_device.c    |  4 +++-
 tools/libxl/libxl_dm.c        |  8 ++++++++
 tools/libxl/libxl_internal.h  |  4 ++++
 tools/libxl/libxl_noblktap2.c |  6 ++++++
 tools/libxl/libxl_types.idl   |  1 +
 tools/libxl/libxlu_disk_l.l   |  1 +
 7 files changed, 56 insertions(+), 1 deletion(-)
tools/libxl/libxl_blktap2.c +33 -0
index 2053403..7bbdfc8 100644
@@ -32,6 +32,10 @@ char *libxl__blktap_devpath(libxl__gc *gc,
     tap_list_t tap;
     int err;
 
+    if (format == LIBXL_DISK_FORMAT_REMUS)
+        if (libxl__blktap_get_real_format(gc, disk, format) < 0)
+            return NULL;
+
     type = libxl__device_disk_string_of_format(format);
     err = tap_ctl_find(type, disk, &tap);
     if (err == 0) {
@@ -84,6 +88,35 @@ int libxl__device_destroy_tapdisk(libxl__gc *gc, const char *params)
     return 0;
 }
 
+libxl_disk_format libxl__blktap_get_real_format(libxl__gc *gc,
+                                                const char *disk,
+                                                libxl_disk_format format)
+{
+    const char *type;
+
+    if (format != LIBXL_DISK_FORMAT_REMUS)
+        return format;
+
+    /* The format of disk: ip:port|xxx:file */
+    type = strchr(disk, '|');
+    if (!type) {
+        LOG(ERROR, "Unable to parse params %s", disk);
+        return ERROR_FAIL;
+    }
+
+    type++;
+
+    /* libxl only supports aio/vhd(see disk_try_backend()) */
+    if (!strncmp(type, "aio:", 4))
+        return LIBXL_DISK_FORMAT_RAW;
+    else if (!strncmp(type, "vhd:", 4))
+        return LIBXL_DISK_FORMAT_VHD;
+
+    LOG(ERROR, "Unsupported format: %s", type);
+
+    return ERROR_FAIL;
+}
+
 /*
  * Local variables:
  * mode: C
tools/libxl/libxl_device.c +3 -1
index 89dc824..a460d33 100644
@@ -211,7 +211,8 @@ static int disk_try_backend(disk_try_backend_args *a,
             return 0;
         }
         if (!(a->disk->format == LIBXL_DISK_FORMAT_RAW ||
-              a->disk->format == LIBXL_DISK_FORMAT_VHD)) {
+              a->disk->format == LIBXL_DISK_FORMAT_VHD ||
+              a->disk->format == LIBXL_DISK_FORMAT_REMUS)) {
             goto bad_format;
         }
         return backend;
@@ -295,6 +296,7 @@ char *libxl__device_disk_string_of_format(libxl_disk_format format)
         case LIBXL_DISK_FORMAT_VHD: return "vhd";
         case LIBXL_DISK_FORMAT_RAW:
         case LIBXL_DISK_FORMAT_EMPTY: return "aio";
+        case LIBXL_DISK_FORMAT_REMUS: return "remus";
         default: return NULL;
     }
 }
tools/libxl/libxl_dm.c +8 -0
index d86ce15..a7ce6d2 100644
@@ -681,6 +681,7 @@ static char ** libxl__build_device_model_args_new(libxl__gc *gc,
             const char *format = qemu_disk_format_string(disks[i].format);
             char *drive;
             const char *pdev_path;
+            libxl_disk_format real_format;
 
             if (dev_number == -1) {
                 LIBXL__LOG(ctx, LIBXL__LOG_WARNING, "unable to determine"
@@ -688,6 +689,13 @@ static char ** libxl__build_device_model_args_new(libxl__gc *gc,
                 continue;
             }
 
+            if (disks[i].format == LIBXL_DISK_FORMAT_REMUS) {
+                real_format = libxl__blktap_get_real_format(gc,
+                                                            disks[i].pdev_path,
+                                                            disks[i].format);
+                format = qemu_disk_format_string(real_format);
+            }
+
             if (disks[i].is_cdrom) {
                 if (disks[i].format == LIBXL_DISK_FORMAT_EMPTY)
                     drive = libxl__sprintf
tools/libxl/libxl_internal.h +4 -0
index 6fc26c9..4478c70 100644
@@ -1535,6 +1535,10 @@ _hidden char *libxl__blktap_devpath(libxl__gc *gc,
  */
 _hidden int libxl__device_destroy_tapdisk(libxl__gc *gc, const char *params);
 
+_hidden libxl_disk_format libxl__blktap_get_real_format(libxl__gc *gc,
+                                                        const char *disk,
+                                                        libxl_disk_format format);
+
 _hidden int libxl__device_from_disk(libxl__gc *gc, uint32_t domid,
                                    libxl_device_disk *disk,
                                    libxl__device *device);
tools/libxl/libxl_noblktap2.c +6 -0
index 5a86ed1..38696ec 100644
@@ -33,6 +33,12 @@ int libxl__device_destroy_tapdisk(libxl__gc *gc, const char *params)
     return 0;
 }
 
+libxl_disk_format libxl__blktap_get_real_format(const char *disk,
+                                                libxl_disk_format format)
+{
+    return format;
+}
+
 /*
  * Local variables:
  * mode: C
tools/libxl/libxl_types.idl +1 -0
index 599f137..6bcb8b6 100644
@@ -87,6 +87,7 @@ libxl_disk_format = Enumeration("disk_format", [
     (3, "VHD"),
     (4, "RAW"),
     (5, "EMPTY"),
+    (6, "REMUS"),
     ])
 
 libxl_disk_backend = Enumeration("disk_backend", [
tools/libxl/libxlu_disk_l.l +1 -0
index 1a5deb5..d9ff8a1 100644
@@ -102,6 +102,7 @@ static void setformat(DiskParseContext *dpc, const char *str) {
     else if (!strcmp(str,"qcow2"))  DSET(dpc,format,FORMAT,str,QCOW2);
     else if (!strcmp(str,"vhd"))    DSET(dpc,format,FORMAT,str,VHD);
     else if (!strcmp(str,"empty"))  DSET(dpc,format,FORMAT,str,EMPTY);
+    else if (!strcmp(str,"remus"))  DSET(dpc,format,FORMAT,str,REMUS);
     else xlu__disk_err(dpc,str,"unknown value for format");
 }
 
Signature · 1 line
1.9.3
#42 lore raw

RFC PATCH v2 42/45 support blktap colo in xl:

In reply to Wen Congyang

With this patch, we can use blktap remus like this:
disk = [ 'format=colo,devtype=disk,access=w,vdev=hda,backendtype=tap,target=192.168.3.1:9000|aio:filename' ]

---
 tools/libxl/libxl_blktap2.c | 6 ++++--
 tools/libxl/libxl_device.c  | 4 +++-
 tools/libxl/libxl_dm.c      | 3 ++-
 tools/libxl/libxl_types.idl | 1 +
 tools/libxl/libxlu_disk_l.l | 1 +
 5 files changed, 11 insertions(+), 4 deletions(-)
tools/libxl/libxl_blktap2.c +4 -2
index 7bbdfc8..cde0dee 100644
@@ -32,7 +32,8 @@ char *libxl__blktap_devpath(libxl__gc *gc,
     tap_list_t tap;
     int err;
 
-    if (format == LIBXL_DISK_FORMAT_REMUS)
+    if (format == LIBXL_DISK_FORMAT_REMUS ||
+        format == LIBXL_DISK_FORMAT_COLO)
         if (libxl__blktap_get_real_format(gc, disk, format) < 0)
             return NULL;
 
@@ -94,7 +95,8 @@ libxl_disk_format libxl__blktap_get_real_format(libxl__gc *gc,
 {
     const char *type;
 
-    if (format != LIBXL_DISK_FORMAT_REMUS)
+    if (format != LIBXL_DISK_FORMAT_REMUS &&
+        format != LIBXL_DISK_FORMAT_COLO)
         return format;
 
     /* The format of disk: ip:port|xxx:file */
tools/libxl/libxl_device.c +3 -1
index a460d33..6e23858 100644
@@ -212,7 +212,8 @@ static int disk_try_backend(disk_try_backend_args *a,
         }
         if (!(a->disk->format == LIBXL_DISK_FORMAT_RAW ||
               a->disk->format == LIBXL_DISK_FORMAT_VHD ||
-              a->disk->format == LIBXL_DISK_FORMAT_REMUS)) {
+              a->disk->format == LIBXL_DISK_FORMAT_REMUS ||
+              a->disk->format == LIBXL_DISK_FORMAT_COLO)) {
             goto bad_format;
         }
         return backend;
@@ -297,6 +298,7 @@ char *libxl__device_disk_string_of_format(libxl_disk_format format)
         case LIBXL_DISK_FORMAT_RAW:
         case LIBXL_DISK_FORMAT_EMPTY: return "aio";
         case LIBXL_DISK_FORMAT_REMUS: return "remus";
+        case LIBXL_DISK_FORMAT_COLO:  return "colo";
         default: return NULL;
     }
 }
tools/libxl/libxl_dm.c +2 -1
index a7ce6d2..a3cc768 100644
@@ -689,7 +689,8 @@ static char ** libxl__build_device_model_args_new(libxl__gc *gc,
                 continue;
             }
 
-            if (disks[i].format == LIBXL_DISK_FORMAT_REMUS) {
+            if (disks[i].format == LIBXL_DISK_FORMAT_REMUS||
+                disks[i].format == LIBXL_DISK_FORMAT_COLO) {
                 real_format = libxl__blktap_get_real_format(gc,
                                                             disks[i].pdev_path,
                                                             disks[i].format);
tools/libxl/libxl_types.idl +1 -0
index 6bcb8b6..3fe0812 100644
@@ -88,6 +88,7 @@ libxl_disk_format = Enumeration("disk_format", [
     (4, "RAW"),
     (5, "EMPTY"),
     (6, "REMUS"),
+    (7, "COLO"),
     ])
 
 libxl_disk_backend = Enumeration("disk_backend", [
tools/libxl/libxlu_disk_l.l +1 -0
index d9ff8a1..a6028b7 100644
@@ -103,6 +103,7 @@ static void setformat(DiskParseContext *dpc, const char *str) {
     else if (!strcmp(str,"vhd"))    DSET(dpc,format,FORMAT,str,VHD);
     else if (!strcmp(str,"empty"))  DSET(dpc,format,FORMAT,str,EMPTY);
     else if (!strcmp(str,"remus"))  DSET(dpc,format,FORMAT,str,REMUS);
+    else if (!strcmp(str,"colo"))   DSET(dpc,format,FORMAT,str,COLO);
     else xlu__disk_err(dpc,str,"unknown value for format");
 }
 
Signature · 1 line
1.9.3
#43 lore raw

RFC PATCH v2 43/45 update libxl__device_disk_from_xs_be() to support blktap device

In reply to Wen Congyang

If the disk backend is blktap device, we store "format:pdev_path" in tapdisk-params, and store "phy" in type. So use tapdisk-params to set libxl_device_disk instead of params and type.

---
 tools/libxl/libxl.c       | 44 ++++++++++++++++++++++++++++++++++++++++++--
 tools/libxl/libxl_utils.c | 23 +++++++++++++++++++++++
 tools/libxl/libxl_utils.h |  1 +
 3 files changed, 66 insertions(+), 2 deletions(-)
tools/libxl/libxl.c +42 -2
index e0817e8..dd69f02 100644
@@ -2373,6 +2373,47 @@ static int libxl__device_disk_from_xs_be(libxl__gc *gc,
         goto cleanup;
     }
 
+    disk->format = LIBXL_DISK_FORMAT_UNKNOWN;
+
+    /* "tapdisk-params" is only for tapdisk */
+    tmp = xs_read(ctx->xsh, XBT_NULL,
+                  libxl__sprintf(gc, "%s/tapdisk-params", be_path), &len);
+    if (tmp) {
+        char *pdev_path;
+        /* tmp is "format:pdev_path" */
+        pdev_path = strchr(tmp, ':');
+        if (!pdev_path) {
+            LOG(ERROR, "corrupted tapdisk-params: \"%s\"\n", tmp);
+            free(tmp);
+            goto cleanup;
+        }
+        disk->pdev_path = strdup(pdev_path + 1);
+        *pdev_path = '\0';
+        rc = libxl_string_to_format(ctx, tmp, &disk->format);
+        if (rc) {
+            LOG(ERROR, "unknown disk format: %s\n", tmp);
+            free(tmp);
+            goto cleanup;
+        }
+        if (disk->format != LIBXL_DISK_FORMAT_VHD &&
+            disk->format != LIBXL_DISK_FORMAT_RAW &&
+            disk->format != LIBXL_DISK_FORMAT_REMUS &&
+            disk->format != LIBXL_DISK_FORMAT_COLO) {
+            LOG(ERROR, "unsupported tapdisk format: %s\n", tmp);
+            free(tmp);
+            goto cleanup;
+        }
+        free(tmp);
+
+        /*
+         * The backend is tapdisk, so we store tapdev in params, and
+         * phy in type(see device_disk_add())
+         */
+        disk->backend = LIBXL_DISK_BACKEND_TAP;
+
+        goto skip_type;
+    }
+
     /* "params" may not be present; but everything else must be. */
     tmp = xs_read(ctx->xsh, XBT_NULL,
                   libxl__sprintf(gc, "%s/params", be_path), &len);
@@ -2392,6 +2433,7 @@ static int libxl__device_disk_from_xs_be(libxl__gc *gc,
     }
     libxl_string_to_backend(ctx, tmp, &(disk->backend));
 
+skip_type:
     disk->vdev = xs_read(ctx->xsh, XBT_NULL,
                          libxl__sprintf(gc, "%s/dev", be_path), &len);
     if (!disk->vdev) {
@@ -2425,8 +2467,6 @@ static int libxl__device_disk_from_xs_be(libxl__gc *gc,
     }
     disk->is_cdrom = !strcmp(tmp, "cdrom");
 
-    disk->format = LIBXL_DISK_FORMAT_UNKNOWN;
-
     return 0;
 cleanup:
     libxl_device_disk_dispose(disk);
tools/libxl/libxl_utils.c +23 -0
index 58df4f3..6c35ba8 100644
@@ -319,6 +319,29 @@ out:
     return rc;
 }
 
+int libxl_string_to_format(libxl_ctx *ctx, char *s, libxl_disk_format *format)
+{
+    int rc = 0;
+    if (!strcmp(s, "aio")) {
+        *format = LIBXL_DISK_FORMAT_RAW;
+    } else if (!strcmp(s, "qcow")) {
+        *format = LIBXL_DISK_FORMAT_QCOW;
+    } else if (!strcmp(s, "qcow2")) {
+        *format = LIBXL_DISK_FORMAT_QCOW2;
+    } else if (!strcmp(s, "vhd")) {
+        *format = LIBXL_DISK_FORMAT_VHD;
+    } else if (!strcmp(s, "remus")) {
+        *format = LIBXL_DISK_FORMAT_REMUS;
+    } else if (!strcmp(s, "colo")) {
+        *format = LIBXL_DISK_FORMAT_COLO;
+    } else {
+        *format = LIBXL_DISK_FORMAT_UNKNOWN;
+        rc = ERROR_FAIL;
+    }
+
+    return rc;
+}
+
 int libxl_read_file_contents(libxl_ctx *ctx, const char *filename,
                              void **data_r, int *datalen_r) {
     GC_INIT(ctx);
tools/libxl/libxl_utils.h +1 -0
index 117b229..9178836 100644
@@ -33,6 +33,7 @@ int libxl_get_stubdom_id(libxl_ctx *ctx, int guest_domid);
 int libxl_is_stubdom(libxl_ctx *ctx, uint32_t domid, uint32_t *target_domid);
 int libxl_create_logfile(libxl_ctx *ctx, const char *name, char **full_name);
 int libxl_string_to_backend(libxl_ctx *ctx, char *s, libxl_disk_backend *backend);
+int libxl_string_to_format(libxl_ctx *ctx, char *s, libxl_disk_format *format);
 
 int libxl_read_file_contents(libxl_ctx *ctx, const char *filename,
                              void **data_r, int *datalen_r);
Signature · 1 line
1.9.3
#44 lore raw

RFC PATCH v2 44/45 libxl/colo: setup and control disk replication for blktap2 backends

In reply to Wen Congyang

This patch adds the machinery required for protecting a guest's disk state, when the guest disk uses a blktap2 disk backend.
1. COLO blktap2 disk device: Implements the interfaces required by the checkpoint abstract device layer. A note about the implementation:
a) setup() is called for each disk attached to the guest.
During setup():
i) perform the sanity check: backend type should be LIBXL_DISK_BACKEND_TAP and format should be LIBXL_DISK_FORMAT_COLO.
ii) connect to the control socket: /var/run/tap/colo_xxx, xxx is
"host:port"(The character ':/' will be changed to '_').
b) The postsuspend callback() will write "flush" to this socket
c) The commit callback() will wait and read "done" from this socket

---
 tools/libxl/Makefile                       |   2 +-
 tools/libxl/libxl_colo_save.c              |   5 +-
 tools/libxl/libxl_colo_save_disk_blktap2.c | 216 +++++++++++++++++++++++++++++
 tools/libxl/libxl_create.c                 |   7 +
 tools/libxl/libxl_noblktap2.c              |  29 ++++
 5 files changed, 257 insertions(+), 2 deletions(-)
 create mode 100644 tools/libxl/libxl_colo_save_disk_blktap2.c
tools/libxl/Makefile +1 -1
index 1c32ae2..b4755c8 100644
@@ -45,7 +45,7 @@ LIBXLU_LIBS =
 
 LIBXL_OBJS-y = osdeps.o libxl_paths.o libxl_bootloader.o flexarray.o
 ifeq ($(LIBXL_BLKTAP),y)
-LIBXL_OBJS-y += libxl_blktap2.o
+LIBXL_OBJS-y += libxl_blktap2.o libxl_colo_save_disk_blktap2.o
 else
 LIBXL_OBJS-y += libxl_noblktap2.o
 endif
tools/libxl/libxl_colo_save.c +4 -1
index 75d83c8..b7070b2 100644
@@ -18,7 +18,10 @@
 #include "libxl_internal.h"
 #include "libxl_colo.h"
 
+extern const libxl__checkpoint_device_subkind_ops colo_save_device_blktap2_disk;
+
 static const libxl__checkpoint_device_subkind_ops *colo_ops[] = {
+    &colo_save_device_blktap2_disk,
     NULL,
 };
 
@@ -49,7 +52,7 @@ void libxl__colo_save_setup(libxl__egc *egc, libxl__colo_save_state *css)
     css->svm_running = false;
 
     /* TODO: disk/nic support */
-    cds->device_kind_flags = 0;
+    cds->device_kind_flags = LIBXL__CHECKPOINT_DEVICE_DISK;
     cds->ops = colo_ops;
     cds->callback = colo_save_setup_done;
     cds->ao = ao;
tools/libxl/libxl_colo_save_disk_blktap2.c new file +216 -0

216 lines not on this page · raw message

tools/libxl/libxl_create.c +7 -0
index 46bd02d..d1facef 100644
@@ -831,6 +831,13 @@ static void initiate_domain_create(libxl__egc *egc,
     for (i = 0; i < d_config->num_disks; i++) {
         ret = libxl__device_disk_setdefault(gc, &d_config->disks[i]);
         if (ret) goto error_out;
+
+        /* TODO: cleanup it when destroying the domain */
+        if (d_config->disks[i].backend == LIBXL_DISK_BACKEND_TAP &&
+            (d_config->disks[i].format == LIBXL_DISK_FORMAT_REMUS ||
+             d_config->disks[i].format == LIBXL_DISK_FORMAT_COLO))
+            libxl__blktap_devpath(gc, d_config->disks[i].pdev_path,
+                                  d_config->disks[i].format);
     }
 
     dcs->bl.ao = ao;
tools/libxl/libxl_noblktap2.c +29 -0
index 38696ec..46207b3 100644
@@ -39,6 +39,35 @@ libxl_disk_format libxl__blktap_get_real_format(const char *disk,
     return format;
 }
 
+static int blktap2_colo_init(libxl__checkpoint_device *cds)
+{
+    return 0;
+}
+
+static void blktap2_colo_cleanup(libxl__checkpoint_device *cds)
+{
+    return;
+}
+
+static void blktap2_colo_setup(libxl__checkpoint_device *cds)
+{
+    dev->aodev.rc = ERROR_FAIL;
+    dev->aodev.callback(dev->cds->egc, &dev->aodev);
+}
+
+static void blktap2_colo_teardown(libxl__checkpoint_device *cds)
+{
+    return;
+}
+
+const libxl__checkpoint_device_subkind_ops colo_save_device_blktap2_disk = {
+    .kind = LIBXL__CHECKPOINT_DEVICE_DISK,
+    .init = blktap2_colo_init,
+    .cleanup = blktap2_colo_cleanup,
+    .setup = blktap2_colo_setup,
+    .teardown = blktap2_colo_teardown,
+};
+
 /*
  * Local variables:
  * mode: C
Signature · 1 line
1.9.3
#45 lore raw

RFC PATCH v2 45/45 x86/hvm: Always set pending event injection when loading VMC[BS] state.

In reply to Wen Congyang

In colo mode, secondary vm is running, so VM_ENTRY_INTR_INFO may valid before restoring vmcs. If there is no pending event after restoring vm, we should clear it.

Also clear pending software exceptions.
Copy the fix to SVM as well.

---
 xen/arch/x86/hvm/svm/svm.c | 16 +++++++++-------
 xen/arch/x86/hvm/vmx/vmx.c | 25 ++++++++++++-------------
 2 files changed, 21 insertions(+), 20 deletions(-)
xen/arch/x86/hvm/svm/svm.c +9 -7
index 71b8a6a..f7a0cb8 100644
@@ -321,16 +321,18 @@ static int svm_vmcb_restore(struct vcpu *v, struct hvm_hw_cpu *c)
         vmcb_set_h_cr3(vmcb, pagetable_get_paddr(p2m_get_pagetable(p2m)));
     }
 
-    if ( c->pending_valid ) 
+    if ( c->pending_valid
+         && hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
     {
         gdprintk(XENLOG_INFO, "Re-injecting %#"PRIx32", %#"PRIx32"\n",
                  c->pending_event, c->error_code);
-
-        if ( hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
-        {
-            vmcb->eventinj.bytes = c->pending_event;
-            vmcb->eventinj.fields.errorcode = c->error_code;
-        }
+        vmcb->eventinj.bytes = c->pending_event;
+        vmcb->eventinj.fields.errorcode = c->error_code;
+    }
+    else
+    {
+        vmcb->eventinj.bytes = 0;
+        vmcb->eventinj.fields.errorcode = 0;
     }
 
     vmcb->cleanbits.bytes = 0;
xen/arch/x86/hvm/vmx/vmx.c +12 -13
index fb65c7d..5f143c0 100644
@@ -509,23 +509,22 @@ static int vmx_vmcs_restore(struct vcpu *v, struct hvm_hw_cpu *c)
 
     __vmwrite(GUEST_DR7, c->dr7);
 
-    vmx_vmcs_exit(v);
-
-    paging_update_paging_modes(v);
-
-    if ( c->pending_valid )
+    if ( c->pending_valid
+         && hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
     {
         gdprintk(XENLOG_INFO, "Re-injecting %#"PRIx32", %#"PRIx32"\n",
                  c->pending_event, c->error_code);
-
-        if ( hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
-        {
-            vmx_vmcs_enter(v);
-            __vmwrite(VM_ENTRY_INTR_INFO, c->pending_event);
-            __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE, c->error_code);
-            vmx_vmcs_exit(v);
-        }
+        __vmwrite(VM_ENTRY_INTR_INFO, c->pending_event);
+        __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE, c->error_code);
     }
+    else
+    {
+        __vmwrite(VM_ENTRY_INTR_INFO, 0);
+        __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE, 0);
+    }
+    vmx_vmcs_exit(v);
+
+    paging_update_paging_modes(v);
 
     return 0;
 }
Signature · 1 line
1.9.3
#46 lore raw

Re: [RFC Patch v2 45/45] x86/hvm: Always set pending event injection when loading VMC[BS] state.

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 08.08.14 at 09:01, <[email protected]> wrote:

In colo mode, secondary vm is running, so VM_ENTRY_INTR_INFO may valid before restoring vmcs. If there is no pending event after restoring vm, we should clear it.

Also clear pending software exceptions.
Copy the fix to SVM as well.

And with this you should also have Cc-ed the SVM maintainers, which I now did.

Quoted 11 lines
---
 xen/arch/x86/hvm/svm/svm.c | 16 +++++++++-------
 xen/arch/x86/hvm/vmx/vmx.c | 25 ++++++++++++-------------
 2 files changed, 21 insertions(+), 20 deletions(-)
xen/arch/x86/hvm/svm/svm.c +0 -0
index 71b8a6a..f7a0cb8 100644

xen/arch/x86/hvm/svm/svm.c static int svm_vmcb_restore(struct vcpu *v, struct · line 326

Quoted 7 lines
xen/arch/x86/hvm/svm/svm.c +2 -1
@@ -321,16 +321,18 @@ static int svm_vmcb_restore(struct vcpu *v, struct 
 hvm_hw_cpu *c)
         vmcb_set_h_cr3(vmcb, pagetable_get_paddr(p2m_get_pagetable(p2m)));
     }
 
-    if ( c->pending_valid ) 
+    if ( c->pending_valid
+         && hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )

Conventionally the operator goes at the end of the first line, not the beginning of the second.

Jan

Quoted 68 lines
xen/arch/x86/hvm/svm/svm.c +7 -6
     {
         gdprintk(XENLOG_INFO, "Re-injecting %#"PRIx32", %#"PRIx32"\n",
                  c->pending_event, c->error_code);
-
-        if ( hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
-        {
-            vmcb->eventinj.bytes = c->pending_event;
-            vmcb->eventinj.fields.errorcode = c->error_code;
-        }
+        vmcb->eventinj.bytes = c->pending_event;
+        vmcb->eventinj.fields.errorcode = c->error_code;
+    }
+    else
+    {
+        vmcb->eventinj.bytes = 0;
+        vmcb->eventinj.fields.errorcode = 0;
     }
 
     vmcb->cleanbits.bytes = 0;
xen/arch/x86/hvm/vmx/vmx.c +12 -13
index fb65c7d..5f143c0 100644
@@ -509,23 +509,22 @@ static int vmx_vmcs_restore(struct vcpu *v, struct 
 hvm_hw_cpu *c)
 
     __vmwrite(GUEST_DR7, c->dr7);
 
-    vmx_vmcs_exit(v);
-
-    paging_update_paging_modes(v);
-
-    if ( c->pending_valid )
+    if ( c->pending_valid
+         && hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
     {
         gdprintk(XENLOG_INFO, "Re-injecting %#"PRIx32", %#"PRIx32"\n",
                  c->pending_event, c->error_code);
-
-        if ( hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
-        {
-            vmx_vmcs_enter(v);
-            __vmwrite(VM_ENTRY_INTR_INFO, c->pending_event);
-            __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE, c->error_code);
-            vmx_vmcs_exit(v);
-        }
+        __vmwrite(VM_ENTRY_INTR_INFO, c->pending_event);
+        __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE, c->error_code);
     }
+    else
+    {
+        __vmwrite(VM_ENTRY_INTR_INFO, 0);
+        __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE, 0);
+    }
+    vmx_vmcs_exit(v);
+
+    paging_update_paging_modes(v);
 
     return 0;

}

Signature · 7 lines
1.9.3


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

Re: [RFC Patch v2 45/45] x86/hvm: Always set pending event injection when loading VMC[BS] state.

At 08/08/2014 03:24 PM, Jan Beulich Write:

Quoted 29 lines
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 08.08.14 at 09:01, <[email protected]> wrote:

In colo mode, secondary vm is running, so VM_ENTRY_INTR_INFO may valid before restoring vmcs. If there is no pending event after restoring vm, we should clear it.

Also clear pending software exceptions.
Copy the fix to SVM as well.

And with this you should also have Cc-ed the SVM maintainers, which I now did.

Quoted 15 lines
---
 xen/arch/x86/hvm/svm/svm.c | 16 +++++++++-------
 xen/arch/x86/hvm/vmx/vmx.c | 25 ++++++++++++-------------
 2 files changed, 21 insertions(+), 20 deletions(-)
xen/arch/x86/hvm/svm/svm.c +0 -0
index 71b8a6a..f7a0cb8 100644
@@ -321,16 +321,18 @@ static int svm_vmcb_restore(struct vcpu *v, struct 
 hvm_hw_cpu *c)
         vmcb_set_h_cr3(vmcb, pagetable_get_paddr(p2m_get_pagetable(p2m)));
     }
 
Quoted 6 lines

xen/arch/x86/hvm/svm/svm.c static int svm_vmcb_restore(struct vcpu *v, struct hvm_hw_cpu *c) · line 325

Quoted 3 lines
xen/arch/x86/hvm/svm/svm.c +2 -1
-    if ( c->pending_valid ) 
+    if ( c->pending_valid
+         && hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )

Conventionally the operator goes at the end of the first line, not the beginning of the second.

I will fixed.

Thanks
Wen Congyang

Quoted 76 lines

Jan

Quoted 59 lines
xen/arch/x86/hvm/svm/svm.c +7 -6
     {
         gdprintk(XENLOG_INFO, "Re-injecting %#"PRIx32", %#"PRIx32"\n",
                  c->pending_event, c->error_code);
-
-        if ( hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
-        {
-            vmcb->eventinj.bytes = c->pending_event;
-            vmcb->eventinj.fields.errorcode = c->error_code;
-        }
+        vmcb->eventinj.bytes = c->pending_event;
+        vmcb->eventinj.fields.errorcode = c->error_code;
+    }
+    else
+    {
+        vmcb->eventinj.bytes = 0;
+        vmcb->eventinj.fields.errorcode = 0;
     }
 
     vmcb->cleanbits.bytes = 0;
xen/arch/x86/hvm/vmx/vmx.c +12 -13
index fb65c7d..5f143c0 100644
@@ -509,23 +509,22 @@ static int vmx_vmcs_restore(struct vcpu *v, struct 
 hvm_hw_cpu *c)
 
     __vmwrite(GUEST_DR7, c->dr7);
 
-    vmx_vmcs_exit(v);
-
-    paging_update_paging_modes(v);
-
-    if ( c->pending_valid )
+    if ( c->pending_valid
+         && hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
     {
         gdprintk(XENLOG_INFO, "Re-injecting %#"PRIx32", %#"PRIx32"\n",
                  c->pending_event, c->error_code);
-
-        if ( hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
-        {
-            vmx_vmcs_enter(v);
-            __vmwrite(VM_ENTRY_INTR_INFO, c->pending_event);
-            __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE, c->error_code);
-            vmx_vmcs_exit(v);
-        }
+        __vmwrite(VM_ENTRY_INTR_INFO, c->pending_event);
+        __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE, c->error_code);
     }
+    else
+    {
+        __vmwrite(VM_ENTRY_INTR_INFO, 0);
+        __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE, 0);
+    }
+    vmx_vmcs_exit(v);
+
+    paging_update_paging_modes(v);
 
     return 0;
Quoted 9 lines

}

Signature · 7 lines
1.9.3


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

.

#48 lore raw

Re: [RFC Patch v2 45/45] x86/hvm: Always set pending event injection when loading VMC[BS] state.

In reply to Wen Congyang
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 08.08.14 at 09:01, <[email protected]> wrote:

In colo mode, secondary vm is running, so VM_ENTRY_INTR_INFO may valid before restoring vmcs. If there is no pending event after restoring vm, we should clear it.

Also clear pending software exceptions.
Copy the fix to SVM as well.

I only now realized that it's no surprise we're not getting acks from the VMX maintainers on this - the majority of them wasn't Cc-ed. Now done, but please take care to do so yourself in the future.

As to the SVM maintainers - Ping (I Cc-ed you on an earlier reply)?

Jan

Quoted 85 lines
---
 xen/arch/x86/hvm/svm/svm.c | 16 +++++++++-------
 xen/arch/x86/hvm/vmx/vmx.c | 25 ++++++++++++-------------
 2 files changed, 21 insertions(+), 20 deletions(-)
xen/arch/x86/hvm/svm/svm.c +9 -7
index 71b8a6a..f7a0cb8 100644
@@ -321,16 +321,18 @@ static int svm_vmcb_restore(struct vcpu *v, struct 
 hvm_hw_cpu *c)
         vmcb_set_h_cr3(vmcb, pagetable_get_paddr(p2m_get_pagetable(p2m)));
     }
 
-    if ( c->pending_valid ) 
+    if ( c->pending_valid
+         && hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
     {
         gdprintk(XENLOG_INFO, "Re-injecting %#"PRIx32", %#"PRIx32"\n",
                  c->pending_event, c->error_code);
-
-        if ( hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
-        {
-            vmcb->eventinj.bytes = c->pending_event;
-            vmcb->eventinj.fields.errorcode = c->error_code;
-        }
+        vmcb->eventinj.bytes = c->pending_event;
+        vmcb->eventinj.fields.errorcode = c->error_code;
+    }
+    else
+    {
+        vmcb->eventinj.bytes = 0;
+        vmcb->eventinj.fields.errorcode = 0;
     }
 
     vmcb->cleanbits.bytes = 0;
xen/arch/x86/hvm/vmx/vmx.c +12 -13
index fb65c7d..5f143c0 100644
@@ -509,23 +509,22 @@ static int vmx_vmcs_restore(struct vcpu *v, struct 
 hvm_hw_cpu *c)
 
     __vmwrite(GUEST_DR7, c->dr7);
 
-    vmx_vmcs_exit(v);
-
-    paging_update_paging_modes(v);
-
-    if ( c->pending_valid )
+    if ( c->pending_valid
+         && hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
     {
         gdprintk(XENLOG_INFO, "Re-injecting %#"PRIx32", %#"PRIx32"\n",
                  c->pending_event, c->error_code);
-
-        if ( hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
-        {
-            vmx_vmcs_enter(v);
-            __vmwrite(VM_ENTRY_INTR_INFO, c->pending_event);
-            __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE, c->error_code);
-            vmx_vmcs_exit(v);
-        }
+        __vmwrite(VM_ENTRY_INTR_INFO, c->pending_event);
+        __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE, c->error_code);
     }
+    else
+    {
+        __vmwrite(VM_ENTRY_INTR_INFO, 0);
+        __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE, 0);
+    }
+    vmx_vmcs_exit(v);
+
+    paging_update_paging_modes(v);
 
     return 0;

}

Signature · 7 lines
1.9.3


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

Re: [RFC Patch v2 45/45] x86/hvm: Always set pending event injection when loading VMC[BS] state.

At 08/27/2014 12:02 AM, Jan Beulich Write:

Quoted 10 lines
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 08.08.14 at 09:01, <[email protected]> wrote:

In colo mode, secondary vm is running, so VM_ENTRY_INTR_INFO may valid before restoring vmcs. If there is no pending event after restoring vm, we should clear it.

Also clear pending software exceptions.
Copy the fix to SVM as well.

Quoted 7 lines
Quoted 1 line

I only now realized that it's no surprise we're not getting acks from the VMX maintainers on this - the majority of them wasn't Cc-ed. Now done, but please take care to do so yourself in the future.

As to the SVM maintainers - Ping (I Cc-ed you on an earlier reply)?

Thanks for doing this.
I have repost it in the bugfix patchset, and cc vmx and svm maintainers

Wen Congyang

Quoted 93 lines

Jan

Quoted 76 lines
---
 xen/arch/x86/hvm/svm/svm.c | 16 +++++++++-------
 xen/arch/x86/hvm/vmx/vmx.c | 25 ++++++++++++-------------
 2 files changed, 21 insertions(+), 20 deletions(-)
xen/arch/x86/hvm/svm/svm.c +9 -7
index 71b8a6a..f7a0cb8 100644
@@ -321,16 +321,18 @@ static int svm_vmcb_restore(struct vcpu *v, struct 
 hvm_hw_cpu *c)
         vmcb_set_h_cr3(vmcb, pagetable_get_paddr(p2m_get_pagetable(p2m)));
     }
 
-    if ( c->pending_valid ) 
+    if ( c->pending_valid
+         && hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
     {
         gdprintk(XENLOG_INFO, "Re-injecting %#"PRIx32", %#"PRIx32"\n",
                  c->pending_event, c->error_code);
-
-        if ( hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
-        {
-            vmcb->eventinj.bytes = c->pending_event;
-            vmcb->eventinj.fields.errorcode = c->error_code;
-        }
+        vmcb->eventinj.bytes = c->pending_event;
+        vmcb->eventinj.fields.errorcode = c->error_code;
+    }
+    else
+    {
+        vmcb->eventinj.bytes = 0;
+        vmcb->eventinj.fields.errorcode = 0;
     }
 
     vmcb->cleanbits.bytes = 0;
xen/arch/x86/hvm/vmx/vmx.c +12 -13
index fb65c7d..5f143c0 100644
@@ -509,23 +509,22 @@ static int vmx_vmcs_restore(struct vcpu *v, struct 
 hvm_hw_cpu *c)
 
     __vmwrite(GUEST_DR7, c->dr7);
 
-    vmx_vmcs_exit(v);
-
-    paging_update_paging_modes(v);
-
-    if ( c->pending_valid )
+    if ( c->pending_valid
+         && hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
     {
         gdprintk(XENLOG_INFO, "Re-injecting %#"PRIx32", %#"PRIx32"\n",
                  c->pending_event, c->error_code);
-
-        if ( hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
-        {
-            vmx_vmcs_enter(v);
-            __vmwrite(VM_ENTRY_INTR_INFO, c->pending_event);
-            __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE, c->error_code);
-            vmx_vmcs_exit(v);
-        }
+        __vmwrite(VM_ENTRY_INTR_INFO, c->pending_event);
+        __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE, c->error_code);
     }
+    else
+    {
+        __vmwrite(VM_ENTRY_INTR_INFO, 0);
+        __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE, 0);
+    }
+    vmx_vmcs_exit(v);
+
+    paging_update_paging_modes(v);
 
     return 0;
Quoted 9 lines

}

Signature · 7 lines
1.9.3


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

.

#50 lore raw

Re: [RFC Patch v2 45/45] x86/hvm: Always set pending event injection when loading VMC[BS] state.

Quoted 11 lines · Wen Congyang

At 08/27/2014 12:02 AM, Jan Beulich Write:

Quoted 10 lines
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 08.08.14 at 09:01, <[email protected]> wrote:

In colo mode, secondary vm is running, so VM_ENTRY_INTR_INFO may valid before restoring vmcs. If there is no pending event after restoring vm, we should clear it.

Also clear pending software exceptions.
Copy the fix to SVM as well.

Quoted 9 lines
Quoted 6 lines
Quoted 1 line

I only now realized that it's no surprise we're not getting acks from the VMX maintainers on this - the majority of them wasn't Cc-ed. Now done, but please take care to do so yourself in the future.

As to the SVM maintainers - Ping (I Cc-ed you on an earlier reply)?

Thanks for doing this.
I have repost it in the bugfix patchset, and cc vmx and svm maintainers

Hi,
Apologies for the delay.

As for the svm changes, the patch seems fairly straightforward and harmless. However, I am not familiar with 'colo mode', so I'm not sure I understand the problem..

Is this a 'fix' we need so that we don't duplicate a pending software interrupt on the secondary VM? Is there a way to test this?

Thanks,
-Aravind.

Quoted 89 lines
Quoted 89 lines
Quoted 76 lines
---
  xen/arch/x86/hvm/svm/svm.c | 16 +++++++++-------
  xen/arch/x86/hvm/vmx/vmx.c | 25 ++++++++++++-------------
  2 files changed, 21 insertions(+), 20 deletions(-)
xen/arch/x86/hvm/svm/svm.c +9 -7
index 71b8a6a..f7a0cb8 100644
@@ -321,16 +321,18 @@ static int svm_vmcb_restore(struct vcpu *v, struct
 hvm_hw_cpu *c)
          vmcb_set_h_cr3(vmcb, pagetable_get_paddr(p2m_get_pagetable(p2m)));
      }
 
-    if ( c->pending_valid )
+    if ( c->pending_valid
+         && hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
      {
          gdprintk(XENLOG_INFO, "Re-injecting %#"PRIx32", %#"PRIx32"\n",
                   c->pending_event, c->error_code);
-
-        if ( hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
-        {
-            vmcb->eventinj.bytes = c->pending_event;
-            vmcb->eventinj.fields.errorcode = c->error_code;
-        }
+        vmcb->eventinj.bytes = c->pending_event;
+        vmcb->eventinj.fields.errorcode = c->error_code;
+    }
+    else
+    {
+        vmcb->eventinj.bytes = 0;
+        vmcb->eventinj.fields.errorcode = 0;
      }
 
      vmcb->cleanbits.bytes = 0;
xen/arch/x86/hvm/vmx/vmx.c +12 -13
index fb65c7d..5f143c0 100644
@@ -509,23 +509,22 @@ static int vmx_vmcs_restore(struct vcpu *v, struct
 hvm_hw_cpu *c)
 
      __vmwrite(GUEST_DR7, c->dr7);
 
-    vmx_vmcs_exit(v);
-
-    paging_update_paging_modes(v);
-
-    if ( c->pending_valid )
+    if ( c->pending_valid
+         && hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
      {
          gdprintk(XENLOG_INFO, "Re-injecting %#"PRIx32", %#"PRIx32"\n",
                   c->pending_event, c->error_code);
-
-        if ( hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
-        {
-            vmx_vmcs_enter(v);
-            __vmwrite(VM_ENTRY_INTR_INFO, c->pending_event);
-            __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE, c->error_code);
-            vmx_vmcs_exit(v);
-        }
+        __vmwrite(VM_ENTRY_INTR_INFO, c->pending_event);
+        __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE, c->error_code);
      }
+    else
+    {
+        __vmwrite(VM_ENTRY_INTR_INFO, 0);
+        __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE, 0);
+    }
+    vmx_vmcs_exit(v);
+
+    paging_update_paging_modes(v);
 
      return 0;
Quoted 9 lines

}

Signature · 7 lines
1.9.3


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

.

#51 lore raw

Re: [RFC Patch v2 45/45] x86/hvm: Always set pending event injection when loading VMC[BS] state.

At 08/27/2014 10:58 PM, Aravind Gopalakrishnan Write:

Quoted 22 lines · Wen Congyang
Quoted 11 lines

At 08/27/2014 12:02 AM, Jan Beulich Write:

Quoted 10 lines
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 08.08.14 at 09:01, <[email protected]> wrote:

In colo mode, secondary vm is running, so VM_ENTRY_INTR_INFO may valid before restoring vmcs. If there is no pending event after restoring vm, we should clear it.

Also clear pending software exceptions.
Copy the fix to SVM as well.

Quoted 9 lines
Quoted 6 lines
Quoted 1 line

I only now realized that it's no surprise we're not getting acks from the VMX maintainers on this - the majority of them wasn't Cc-ed. Now done, but please take care to do so yourself in the future.

As to the SVM maintainers - Ping (I Cc-ed you on an earlier reply)?

Thanks for doing this.
I have repost it in the bugfix patchset, and cc vmx and svm maintainers

Quoted 5 lines

Hi,
Apologies for the delay.

As for the svm changes, the patch seems fairly straightforward and harmless. However, I am not familiar with 'colo mode', so I'm not sure I understand the problem..

In colo mode, secondary vm runs like this:
1. suspend
2. update the vm's state(All state is transfered from primary)
3. resume

Before resuming secondary vm, it is the same with primary vm. Because the secondary vm is running before step1, so VM_ENTRY_INTR_INFO may be valid before resuming secondary vm(step3). If there is no pending event after resuming secondary vm, and we don't clear it, vmentry will fail(I only test vmx). I don't know the behavior in svm. This part of patch is wrote by Tim Deegan.

Quoted 3 lines

Is this a 'fix' we need so that we don't duplicate a pending software interrupt on the secondary VM? Is there a way to test this?

I don't know any other way except colo to test it.

Thanks
Wen Congyang

Quoted 91 lines

Thanks,
-Aravind.

Quoted 76 lines
Quoted 76 lines
Quoted 76 lines
---
  xen/arch/x86/hvm/svm/svm.c | 16 +++++++++-------
  xen/arch/x86/hvm/vmx/vmx.c | 25 ++++++++++++-------------
  2 files changed, 21 insertions(+), 20 deletions(-)
xen/arch/x86/hvm/svm/svm.c +9 -6
index 71b8a6a..f7a0cb8 100644
@@ -321,16 +321,18 @@ static int svm_vmcb_restore(struct vcpu *v, struct
 hvm_hw_cpu *c)
          vmcb_set_h_cr3(vmcb, pagetable_get_paddr(p2m_get_pagetable(p2m)));
      }
  -    if ( c->pending_valid )
+    if ( c->pending_valid
+         && hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
      {
          gdprintk(XENLOG_INFO, "Re-injecting %#"PRIx32", %#"PRIx32"\n",
                   c->pending_event, c->error_code);
-
-        if ( hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
-        {
-            vmcb->eventinj.bytes = c->pending_event;
-            vmcb->eventinj.fields.errorcode = c->error_code;
-        }
+        vmcb->eventinj.bytes = c->pending_event;
+        vmcb->eventinj.fields.errorcode = c->error_code;
+    }
+    else
+    {
+        vmcb->eventinj.bytes = 0;
+        vmcb->eventinj.fields.errorcode = 0;
      }
        vmcb->cleanbits.bytes = 0;
xen/arch/x86/hvm/vmx/vmx.c +12 -12
index fb65c7d..5f143c0 100644
@@ -509,23 +509,22 @@ static int vmx_vmcs_restore(struct vcpu *v, struct
 hvm_hw_cpu *c)
        __vmwrite(GUEST_DR7, c->dr7);
  -    vmx_vmcs_exit(v);
-
-    paging_update_paging_modes(v);
-
-    if ( c->pending_valid )
+    if ( c->pending_valid
+         && hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
      {
          gdprintk(XENLOG_INFO, "Re-injecting %#"PRIx32", %#"PRIx32"\n",
                   c->pending_event, c->error_code);
-
-        if ( hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
-        {
-            vmx_vmcs_enter(v);
-            __vmwrite(VM_ENTRY_INTR_INFO, c->pending_event);
-            __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE, c->error_code);
-            vmx_vmcs_exit(v);
-        }
+        __vmwrite(VM_ENTRY_INTR_INFO, c->pending_event);
+        __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE, c->error_code);
      }
+    else
+    {
+        __vmwrite(VM_ENTRY_INTR_INFO, 0);
+        __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE, 0);
+    }
+    vmx_vmcs_exit(v);
+
+    paging_update_paging_modes(v);
        return 0;
  }
Signature · 1 line
1.9.3
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

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

.

.

#52 lore raw

Re: [RFC Patch v2 45/45] x86/hvm: Always set pending event injection when loading VMC[BS] state.

Quoted 25 lines · Wen Congyang

At 08/27/2014 10:58 PM, Aravind Gopalakrishnan Write:

Quoted 24 lines · Wen Congyang
Quoted 11 lines

At 08/27/2014 12:02 AM, Jan Beulich Write:

Quoted 10 lines
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 08.08.14 at 09:01, <[email protected]> wrote:

In colo mode, secondary vm is running, so VM_ENTRY_INTR_INFO may valid before restoring vmcs. If there is no pending event after restoring vm, we should clear it.

Also clear pending software exceptions.
Copy the fix to SVM as well.

Quoted 9 lines
Quoted 6 lines
Quoted 1 line

I only now realized that it's no surprise we're not getting acks from the VMX maintainers on this - the majority of them wasn't Cc-ed. Now done, but please take care to do so yourself in the future.

As to the SVM maintainers - Ping (I Cc-ed you on an earlier reply)?

Thanks for doing this.
I have repost it in the bugfix patchset, and cc vmx and svm maintainers

Hi,
Apologies for the delay.

Quoted 6 lines
Quoted 2 lines

As for the svm changes, the patch seems fairly straightforward and harmless. However, I am not familiar with 'colo mode', so I'm not sure I understand the problem..

In colo mode, secondary vm runs like this:
1. suspend
2. update the vm's state(All state is transfered from primary)
3. resume

Is this accurate? From previous review, I seem to remember that you are pausing the vm, not suspending it, which is where all of these event issues derive from.

~Andrew

#53 lore raw

Re: [RFC Patch v2 45/45] x86/hvm: Always set pending event injection when loading VMC[BS] state.

At 08/28/2014 04:54 PM, Andrew Cooper Write:

Quoted 28 lines · Wen Congyang
Quoted 27 lines

At 08/27/2014 10:58 PM, Aravind Gopalakrishnan Write:

Quoted 26 lines · Wen Congyang
Quoted 11 lines

At 08/27/2014 12:02 AM, Jan Beulich Write:

Quoted 10 lines
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 08.08.14 at 09:01, <[email protected]> wrote:

In colo mode, secondary vm is running, so VM_ENTRY_INTR_INFO may valid before restoring vmcs. If there is no pending event after restoring vm, we should clear it.

Also clear pending software exceptions.
Copy the fix to SVM as well.

Quoted 9 lines
Quoted 6 lines
Quoted 1 line

I only now realized that it's no surprise we're not getting acks from the VMX maintainers on this - the majority of them wasn't Cc-ed. Now done, but please take care to do so yourself in the future.

As to the SVM maintainers - Ping (I Cc-ed you on an earlier reply)?

Thanks for doing this.
I have repost it in the bugfix patchset, and cc vmx and svm maintainers

Hi,
Apologies for the delay.

As for the svm changes, the patch seems fairly straightforward and harmless. However, I am not familiar with 'colo mode', so I'm not sure I understand the problem..

Quoted 8 lines
Quoted 4 lines

In colo mode, secondary vm runs like this:
1. suspend
2. update the vm's state(All state is transfered from primary)
3. resume

Is this accurate? From previous review, I seem to remember that you are pausing the vm, not suspending it, which is where all of these event issues derive from.

Not pause. We suspend the guest(not save the state). Pausing vm meant that the vm is not running, but the state cannot be updated. For example, if the vm uses pvdriver(not supported now), the backend and frontend share some information, and we only update frontend(backend state is not transfered from primary dom0)...

Thanks
Wen Congyang

Quoted 4 lines

~Andrew
.

#54 lore raw

Re: [RFC Patch v2 45/45] x86/hvm: Always set pending event injection when loading VMC[BS] state.

Quoted 52 lines

-----Original Message-----
From: [email protected] [mailto:xen-devel- [email protected]] On Behalf Of Wen Congyang
Sent: 28 August 2014 12:18
To: Andrew Cooper; Aravind Gopalakrishnan; Jan Beulich
Cc: Kevin Tian; Yang Hongyang; Ian Campbell; Eddie Dong; Ian Jackson; Tim (Xen.org); Jun Nakajima; Boris Ostrovsky; xen devel; [email protected]; Lai Jiangshan
Subject: Re: [Xen-devel] [RFC Patch v2 45/45] x86/hvm: Always set pending event injection when loading VMC[BS] state.

At 08/28/2014 04:54 PM, Andrew Cooper Write:

Quoted 6 lines · Wen Congyang
Quoted 5 lines

At 08/27/2014 10:58 PM, Aravind Gopalakrishnan Write:

Quoted 4 lines · Wen Congyang
Quoted 3 lines

At 08/27/2014 12:02 AM, Jan Beulich Write:

Quoted 2 lines
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 08.08.14 at 09:01, <[email protected]> wrote:

In colo mode, secondary vm is running, so VM_ENTRY_INTR_INFO

may

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 9 lines

valid before restoring vmcs. If there is no pending event after restoring vm, we should clear it.

Also clear pending software exceptions.
Copy the fix to SVM as well.

I only now realized that it's no surprise we're not getting acks from the VMX maintainers on this - the majority of them wasn't Cc-ed. Now done, but please take care to do so yourself in the future.

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

As to the SVM maintainers - Ping (I Cc-ed you on an earlier reply)?

Thanks for doing this.
I have repost it in the bugfix patchset, and cc vmx and svm maintainers

Hi,
Apologies for the delay.

As for the svm changes, the patch seems fairly straightforward and

harmless.

Quoted 1 line
Quoted 1 line
Quoted 1 line

However, I am not familiar with 'colo mode', so I'm not sure I understand

the problem..

Quoted 8 lines
Quoted 4 lines

In colo mode, secondary vm runs like this:
1. suspend
2. update the vm's state(All state is transfered from primary)
3. resume

Is this accurate? From previous review, I seem to remember that you are pausing the vm, not suspending it, which is where all of these event issues derive from.

Quoted 8 lines

Not pause. We suspend the guest(not save the state). Pausing vm meant that
the vm is not running, but the state cannot be updated. For example, if the vm uses pvdriver(not supported now), the backend and frontend share some
information, and we only update frontend(backend state is not transfered from primary dom0)...

If you're doing suspend/resume then PV drivers should re-attached to backends anyway so any state you did transfer would be somewhat pointless. Because of the re-attach though, resume is a pretty heavyweight operation. Is that really what you are doing?

Paul

Quoted 13 lines

Thanks
Wen Congyang

Quoted 4 lines

~Andrew
.

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

#55 lore raw

Re: [RFC Patch v2 45/45] x86/hvm: Always set pending event injection when loading VMC[BS] state.

At 08/28/2014 07:31 PM, Paul Durrant Write:

Quoted 61 lines
Quoted 52 lines

-----Original Message-----
From: [email protected] [mailto:xen-devel- [email protected]] On Behalf Of Wen Congyang
Sent: 28 August 2014 12:18
To: Andrew Cooper; Aravind Gopalakrishnan; Jan Beulich
Cc: Kevin Tian; Yang Hongyang; Ian Campbell; Eddie Dong; Ian Jackson; Tim (Xen.org); Jun Nakajima; Boris Ostrovsky; xen devel; [email protected]; Lai Jiangshan
Subject: Re: [Xen-devel] [RFC Patch v2 45/45] x86/hvm: Always set pending event injection when loading VMC[BS] state.

At 08/28/2014 04:54 PM, Andrew Cooper Write:

Quoted 6 lines · Wen Congyang
Quoted 5 lines

At 08/27/2014 10:58 PM, Aravind Gopalakrishnan Write:

Quoted 4 lines · Wen Congyang
Quoted 3 lines

At 08/27/2014 12:02 AM, Jan Beulich Write:

Quoted 2 lines
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 08.08.14 at 09:01, <[email protected]> wrote:

In colo mode, secondary vm is running, so VM_ENTRY_INTR_INFO

may

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 9 lines

valid before restoring vmcs. If there is no pending event after restoring vm, we should clear it.

Also clear pending software exceptions.
Copy the fix to SVM as well.

I only now realized that it's no surprise we're not getting acks from the VMX maintainers on this - the majority of them wasn't Cc-ed. Now done, but please take care to do so yourself in the future.

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

As to the SVM maintainers - Ping (I Cc-ed you on an earlier reply)?

Thanks for doing this.
I have repost it in the bugfix patchset, and cc vmx and svm maintainers

Hi,
Apologies for the delay.

As for the svm changes, the patch seems fairly straightforward and

harmless.

Quoted 1 line
Quoted 1 line
Quoted 1 line

However, I am not familiar with 'colo mode', so I'm not sure I understand

the problem..

Quoted 8 lines
Quoted 4 lines

In colo mode, secondary vm runs like this:
1. suspend
2. update the vm's state(All state is transfered from primary)
3. resume

Is this accurate? From previous review, I seem to remember that you are pausing the vm, not suspending it, which is where all of these event issues derive from.

Quoted 8 lines

Not pause. We suspend the guest(not save the state). Pausing vm meant that
the vm is not running, but the state cannot be updated. For example, if the vm uses pvdriver(not supported now), the backend and frontend share some
information, and we only update frontend(backend state is not transfered from primary dom0)...

Quoted 1 line

If you're doing suspend/resume then PV drivers should re-attached to backends anyway so any state you did transfer would be somewhat pointless. Because of the re-attach though, resume is a pretty heavyweight operation. Is that really what you are doing?

Yes, so I need to do more things to support pvm and pvhvm.

Thanks
Wen Congyang

Quoted 19 lines

Paul

Quoted 9 lines

Thanks
Wen Congyang

Quoted 4 lines

~Andrew
.

Quoted 4 lines

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

.

#56 lore raw

Re: [RFC Patch v2 45/45] x86/hvm: Always set pending event injection when loading VMC[BS] state.

In reply to Aravind Gopalakrishnan
Quoted 25 lines · Aravind Gopalakrishnan
Quoted 11 lines · Wen Congyang

At 08/27/2014 12:02 AM, Jan Beulich Write:

Quoted 10 lines
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 08.08.14 at 09:01, <[email protected]> wrote:

In colo mode, secondary vm is running, so VM_ENTRY_INTR_INFO may valid before restoring vmcs. If there is no pending event after restoring vm, we should clear it.

Also clear pending software exceptions.
Copy the fix to SVM as well.

Quoted 9 lines
Quoted 6 lines
Quoted 1 line

I only now realized that it's no surprise we're not getting acks from the VMX maintainers on this - the majority of them wasn't Cc-ed. Now done, but please take care to do so yourself in the future.

As to the SVM maintainers - Ping (I Cc-ed you on an earlier reply)?

Thanks for doing this.
I have repost it in the bugfix patchset, and cc vmx and svm maintainers

Hi,
Apologies for the delay.

Quoted 6 lines

As for the svm changes, the patch seems fairly straightforward and harmless. However, I am not familiar with 'colo mode', so I'm not sure I understand the problem..

Is this a 'fix' we need so that we don't duplicate a pending software interrupt on the secondary VM?

Yes. The fix is needed for any code that uses the HVM state restore to overwrite the state of a live vcpu. If the vcpu had a pending injection before the restore op it will still be pending afterwards, even though all the rest of the vcpu state has been reset.

We didn't see it before becasue we only ever load HVM state int a freshly creadted domain.

Quoted 1 line

Is there a way to test this?

No canned test. You could run a guest that takes a lot of faults and then restore another guest over it and look for spurious faults in the guest.

Tim.

Acked-by
#57 lore raw

Re: [RFC Patch v2 45/45] x86/hvm: Always set pending event injection when loading VMC[BS] state.

In reply to Jan Beulich
Quoted 10 lines

From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, August 26, 2014 9:02 AM

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 08.08.14 at 09:01, <[email protected]> wrote:

In colo mode, secondary vm is running, so VM_ENTRY_INTR_INFO may valid before restoring vmcs. If there is no pending event after restoring vm, we should clear it.

Quoted 8 lines
Quoted 4 lines

Also clear pending software exceptions.
Copy the fix to SVM as well.

I only now realized that it's no surprise we're not getting acks from the VMX maintainers on this - the majority of them wasn't Cc-ed. Now done, but please take care to do so yourself in the future.

thanks for forwarding. VMX part looks good to me.

Quoted 101 lines

As to the SVM maintainers - Ping (I Cc-ed you on an earlier reply)?

Jan

xen/arch/x86/hvm/svm/svm.c static int svm_vmcb_restore(struct vcpu *v, struct · line 322

Quoted 12 lines
---
 xen/arch/x86/hvm/svm/svm.c | 16 +++++++++-------
 xen/arch/x86/hvm/vmx/vmx.c | 25 ++++++++++++-------------
 2 files changed, 21 insertions(+), 20 deletions(-)
xen/arch/x86/hvm/svm/svm.c +0 -0
index 71b8a6a..f7a0cb8 100644
@@ -321,16 +321,18 @@ static int svm_vmcb_restore(struct vcpu *v, struct
 hvm_hw_cpu *c)
         vmcb_set_h_cr3(vmcb,

pagetable_get_paddr(p2m_get_pagetable(p2m)));

xen/arch/x86/hvm/svm/svm.c static int svm_vmcb_restore(struct vcpu *v, struct hvm_hw_cpu *c)

Quoted 5 lines
xen/arch/x86/hvm/svm/svm.c +2 -1
     }
 
-    if ( c->pending_valid )
+    if ( c->pending_valid
+         && hvm_event_needs_reinjection(c->pending_type,

c->pending_vector) )

xen/arch/x86/hvm/svm/svm.c static int svm_vmcb_restore(struct vcpu *v, struct · line 328

Quoted 2 lines
xen/arch/x86/hvm/svm/svm.c +0 -0
     {
         gdprintk(XENLOG_INFO,

"Re-injecting %#"PRIx32", %#"PRIx32"\n",

xen/arch/x86/hvm/svm/svm.c static int svm_vmcb_restore(struct vcpu *v, struct hvm_hw_cpu *c)

Quoted 3 lines
xen/arch/x86/hvm/svm/svm.c +0 -2
                  c->pending_event, c->error_code);
-
-        if ( hvm_event_needs_reinjection(c->pending_type,

c->pending_vector) )

Quoted 20 lines
xen/arch/x86/hvm/svm/svm.c +7 -4
-        {
-            vmcb->eventinj.bytes = c->pending_event;
-            vmcb->eventinj.fields.errorcode = c->error_code;
-        }
+        vmcb->eventinj.bytes = c->pending_event;
+        vmcb->eventinj.fields.errorcode = c->error_code;
+    }
+    else
+    {
+        vmcb->eventinj.bytes = 0;
+        vmcb->eventinj.fields.errorcode = 0;
     }
 
     vmcb->cleanbits.bytes = 0;
xen/arch/x86/hvm/vmx/vmx.c +0 -0
index fb65c7d..5f143c0 100644
@@ -509,23 +509,22 @@ static int vmx_vmcs_restore(struct vcpu *v, struct
 hvm_hw_cpu *c)

xen/arch/x86/hvm/vmx/vmx.c static int vmx_vmcs_restore(struct vcpu *v, struct · line 514

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +2 -5
 
     __vmwrite(GUEST_DR7, c->dr7);
 
-    vmx_vmcs_exit(v);
-
-    paging_update_paging_modes(v);
-
-    if ( c->pending_valid )
+    if ( c->pending_valid
+         && hvm_event_needs_reinjection(c->pending_type,

c->pending_vector) )

xen/arch/x86/hvm/vmx/vmx.c static int vmx_vmcs_restore(struct vcpu *v, struct · line 516

Quoted 2 lines
xen/arch/x86/hvm/vmx/vmx.c +0 -0
     {
         gdprintk(XENLOG_INFO,

"Re-injecting %#"PRIx32", %#"PRIx32"\n",

xen/arch/x86/hvm/vmx/vmx.c static int vmx_vmcs_restore(struct vcpu *v, struct · line 522

Quoted 3 lines
xen/arch/x86/hvm/vmx/vmx.c +0 -2
                  c->pending_event, c->error_code);
-
-        if ( hvm_event_needs_reinjection(c->pending_type,

c->pending_vector) )

xen/arch/x86/hvm/vmx/vmx.c static int vmx_vmcs_restore(struct vcpu *v, struct hvm_hw_cpu *c)

Quoted 4 lines
xen/arch/x86/hvm/vmx/vmx.c +0 -4
-        {
-            vmx_vmcs_enter(v);
-            __vmwrite(VM_ENTRY_INTR_INFO, c->pending_event);
-            __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE,

c->error_code);

xen/arch/x86/hvm/vmx/vmx.c static int vmx_vmcs_restore(struct vcpu *v, struct hvm_hw_cpu *c)

Quoted 4 lines
xen/arch/x86/hvm/vmx/vmx.c +2 -2
-            vmx_vmcs_exit(v);
-        }
+        __vmwrite(VM_ENTRY_INTR_INFO, c->pending_event);
+        __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE,

c->error_code);

Quoted 20 lines
xen/arch/x86/hvm/vmx/vmx.c +8 -0
     }
+    else
+    {
+        __vmwrite(VM_ENTRY_INTR_INFO, 0);
+        __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE, 0);
+    }
+    vmx_vmcs_exit(v);
+
+    paging_update_paging_modes(v);
 
     return 0;

}
--
1.9.3

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

#58 lore raw

Re: [RFC Patch v2 45/45] x86/hvm: Always set pending event injection when loading VMC[BS] state.

In reply to Wen Congyang
Quoted 33 lines · Wen Congyang

In colo mode, secondary vm is running, so VM_ENTRY_INTR_INFO may valid before restoring vmcs. If there is no pending event after restoring vm, we should clear it.

Also clear pending software exceptions.
Copy the fix to SVM as well.

---
 xen/arch/x86/hvm/svm/svm.c | 16 +++++++++-------
 xen/arch/x86/hvm/vmx/vmx.c | 25 ++++++++++++-------------
 2 files changed, 21 insertions(+), 20 deletions(-)
xen/arch/x86/hvm/svm/svm.c +2 -5
index 71b8a6a..f7a0cb8 100644
@@ -321,16 +321,18 @@ static int svm_vmcb_restore(struct vcpu *v, struct hvm_hw_cpu *c)
         vmcb_set_h_cr3(vmcb, pagetable_get_paddr(p2m_get_pagetable(p2m)));
     }
 
-    if ( c->pending_valid ) 
+    if ( c->pending_valid
+         && hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
     {
         gdprintk(XENLOG_INFO, "Re-injecting %#"PRIx32", %#"PRIx32"\n",
                  c->pending_event, c->error_code);
-
-        if ( hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
-        {
-            vmcb->eventinj.bytes = c->pending_event;

xen/arch/x86/hvm/svm/svm.c static int svm_vmcb_restore(struct vcpu *v, struct hvm_hw_cpu *c) · line 336

Quoted 10 lines
xen/arch/x86/hvm/svm/svm.c +7 -2
-            vmcb->eventinj.fields.errorcode = c->error_code;
-        }
+        vmcb->eventinj.bytes = c->pending_event;
+        vmcb->eventinj.fields.errorcode = c->error_code;
+    }
+    else
+    {
+        vmcb->eventinj.bytes = 0;
+        vmcb->eventinj.fields.errorcode = 0;
     }

vmcb->eventinj.bytes is part of a union which fully covers .fields. Explicitly setting errorcode=0 is redundant.

~Andrew

Quoted 42 lines
xen/arch/x86/hvm/svm/svm.c +0 -0
     vmcb->cleanbits.bytes = 0;
xen/arch/x86/hvm/vmx/vmx.c +12 -13
index fb65c7d..5f143c0 100644
@@ -509,23 +509,22 @@ static int vmx_vmcs_restore(struct vcpu *v, struct hvm_hw_cpu *c)
 
     __vmwrite(GUEST_DR7, c->dr7);
 
-    vmx_vmcs_exit(v);
-
-    paging_update_paging_modes(v);
-
-    if ( c->pending_valid )
+    if ( c->pending_valid
+         && hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
     {
         gdprintk(XENLOG_INFO, "Re-injecting %#"PRIx32", %#"PRIx32"\n",
                  c->pending_event, c->error_code);
-
-        if ( hvm_event_needs_reinjection(c->pending_type, c->pending_vector) )
-        {
-            vmx_vmcs_enter(v);
-            __vmwrite(VM_ENTRY_INTR_INFO, c->pending_event);
-            __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE, c->error_code);
-            vmx_vmcs_exit(v);
-        }
+        __vmwrite(VM_ENTRY_INTR_INFO, c->pending_event);
+        __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE, c->error_code);
     }
+    else
+    {
+        __vmwrite(VM_ENTRY_INTR_INFO, 0);
+        __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE, 0);
+    }
+    vmx_vmcs_exit(v);
+
+    paging_update_paging_modes(v);
 
     return 0;
 }
#59 lore raw

[RFC Patch v2 46/45] Introduce "xen-load-devices-state"

In reply to Wen Congyang

introduce a "xen-load-devices-state" QAPI command that can be used to load the state of all devices, but not the RAM or the block devices of the VM.

---
 qapi-schema.json |  18 ++++++++
 qmp-commands.hx  |  27 ++++++++++++
 savevm.c         | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 171 insertions(+)
qapi-schema.json +18 -0
index 391356f..c569856 100644
@@ -4689,3 +4689,21 @@
               'btn'     : 'InputBtnEvent',
               'rel'     : 'InputMoveEvent',
               'abs'     : 'InputMoveEvent' } }
+
+##
+# @xen-load-devices-state:
+#
+# Load the state of all devices from file. The RAM and the block devices
+# of the VM are not loaded by this command.
+#
+# @filename: the file to load the state of the devices from as binary
+# data. See xen-save-devices-state.txt for a description of the binary
+# format.
+#
+# Returns: Nothing on success
+#          If @filename cannot be opened, OpenFileFailed
+#          If an I/O error occurs while reading the file, IOError
+#
+# Since: 2.0
+##
+{ 'command': 'xen-load-devices-state', 'data': {'filename': 'str'} }
qmp-commands.hx +27 -0
index ed3ab92..b796be5 100644
@@ -586,6 +586,33 @@ Example:
 EQMP
 
     {
+        .name       = "xen-load-devices-state",
+        .args_type  = "filename:F",
+        .mhandler.cmd_new = qmp_marshal_input_xen_load_devices_state,
+    },
+
+SQMP
+xen-load-devices-state
+-------
+
+Load the state of all devices from file. The RAM and the block devices
+of the VM are not loaded by this command.
+
+Arguments:
+
+- "filename": the file to load the state of the devices from as binary
+data. See xen-save-devices-state.txt for a description of the binary
+format.
+
+Example:
+
+-> { "execute": "xen-load-devices-state",
+     "arguments": { "filename": "/tmp/resume" } }
+<- { "return": {} }
+
+EQMP
+
+    {
         .name       = "xen-set-global-dirty-log",
         .args_type  = "enable:b",
         .mhandler.cmd_new = qmp_marshal_input_xen_set_global_dirty_log,
savevm.c +126 -0

138 lines not on this page · raw message

Signature · 1 line
1.9.0
#60 lore raw
In reply to Wen Congyang
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 08.08.14 at 09:00, <[email protected]> wrote:

Patch 1-3 : bugfix

Such should imo be submitted as a separate prereq series.

Quoted 5 lines

Patch 4-11 : update some APIs which will be used by colo Patch 12-15: temporarily update remus to reuse remus device codes Patch 16-23: COLO framework related codes
Patch 24 : Hack patch, just for test
Patch 25-34: bugfix for blktap2

Quoted 8 lines

Patch 35-38: move some block-remus's codes to block-replication.c. These codes will
be reused by COLO.
Patch 39 : implement block-colo
Patch 40-43: update libxl to support blktap2
Patch 44 : implement disk replication
Patch 45 : hypervisor bugfix. We find this bug before rebasing colo to newest xen.
But we don't trigger this bug now.
Patch 46 : A patch for qemu-xen

And this probably goes for the whole series: Splitting this up by component would make it much clearer who of the maintainers needs to take a look at which pieces.

Jan

#61 lore raw

At 08/08/2014 03:19 PM, Jan Beulich Write:

Quoted 4 lines
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 08.08.14 at 09:00, <[email protected]> wrote:

Patch 1-3 : bugfix

Such should imo be submitted as a separate prereq series.

OK

Quoted 15 lines
Quoted 14 lines

Patch 4-11 : update some APIs which will be used by colo
Patch 12-15: temporarily update remus to reuse remus device codes Patch 16-23: COLO framework related codes
Patch 24 : Hack patch, just for test
Patch 25-34: bugfix for blktap2
Patch 35-38: move some block-remus's codes to block-replication.c. These codes will
be reused by COLO.
Patch 39 : implement block-colo
Patch 40-43: update libxl to support blktap2
Patch 44 : implement disk replication
Patch 45 : hypervisor bugfix. We find this bug before rebasing colo to newest xen.
But we don't trigger this bug now.
Patch 46 : A patch for qemu-xen

Quoted 3 lines

And this probably goes for the whole series: Splitting this up by component would make it much clearer who of the maintainers needs to take a look at which pieces.

OK.

Thanks
Wen Congyang

Quoted 5 lines

Jan

.

#62 lore raw
In reply to Jan Beulich

At 08/08/2014 03:19 PM, Jan Beulich Write:

Quoted 4 lines
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 08.08.14 at 09:00, <[email protected]> wrote:

Patch 1-3 : bugfix

Such should imo be submitted as a separate prereq series.

I have repost patch 1-3, 25-34, 40, 41, 43 in another series:
http://lists.xenproject.org/archives/html/xen-devel/2014-08/msg00971.html

Thanks
Wen Congyang

Quoted 23 lines
Quoted 14 lines

Patch 4-11 : update some APIs which will be used by colo
Patch 12-15: temporarily update remus to reuse remus device codes Patch 16-23: COLO framework related codes
Patch 24 : Hack patch, just for test
Patch 25-34: bugfix for blktap2
Patch 35-38: move some block-remus's codes to block-replication.c. These codes will
be reused by COLO.
Patch 39 : implement block-colo
Patch 40-43: update libxl to support blktap2
Patch 44 : implement disk replication
Patch 45 : hypervisor bugfix. We find this bug before rebasing colo to newest xen.
But we don't trigger this bug now.
Patch 46 : A patch for qemu-xen

And this probably goes for the whole series: Splitting this up by component would make it much clearer who of the maintainers needs to take a look at which pieces.

Jan

.

#63 lore raw
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 08.08.14 at 10:21, <[email protected]> wrote:

At 08/08/2014 03:19 PM, Jan Beulich Write:

Quoted 4 lines
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 08.08.14 at 09:00, <[email protected]> wrote:

Patch 1-3 : bugfix

Such should imo be submitted as a separate prereq series.

I have repost patch 1-3, 25-34, 40, 41, 43 in another series:
http://lists.xenproject.org/archives/html/xen-devel/2014-08/msg00971.html

Which then you said we should ignore.

Jan