Skip to contentSkip to conversation

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

RFC PATCH Superseded Wen Congyang 3 participants 37 messages Sep 5, 2014 – Sep 26, 2014

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 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)
3. nic replication(use colo-agent)

This patchset is based on remus-v19, bugfix, and colo-prepare patchset, and use migration v1. Only supports hvm guest now. The codes are also hosted on github:
https://github.com/wencongyang/xen/tree/colo-v3

TODO list:
1. Use migration v2 to implement COLO
2. support pvm

Known bugs:
1. qemu may segment fault
2. secondary vm is running, but don't response.

Usage:
1. update the vm's configfile:
disk:

        disk = [ 'format=colo,devtype=disk,access=w,vdev=hda,backendtype=tap,target=192.168.3.1:9000|aio:/root/images/hvm/hvm_nopv/hvm.img' ]
   nic:
        vif = [ 'mac=00:16:4f:00:00:11, bridge=br0, model=e1000, forwarddev=eth0' ]

Note: the forwarddev of primary and secondary host should be connected directly, and no other app uses it. If you don't have such nic, you can use vlan to make it.
2. build colo-agent:
You can get colo-agent from github:
https://github.com/wencongyang/colo-agent
3. run:
xl remus -c <domname> <secondary host IP>

Patch 1-4 : update remus to reuse remus device codes
Patch 5-12 : COLO framework related codes
Patch 13-16: move some block-remus's codes to block-replication.c. These codes will
be reused by COLO.
Patch 17 : implement block-colo
Patch 18 : update libxl to support blktap2
Patch 19 : implement disk replication
Patch 20-22: implement nic replication
Patch 23 : A patch for qemu-xen

Changlog from v2 to v3:
1. rebase to newest remus
2. add nic replication support

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

Wen Congyang (22):
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
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
support blktap COLO in xl:
libxl/colo: setup and control disk replication for blktap2 backends setup and control colo-agent for primary vm
setup and control colo-agent for secondary vm
colo: cmdline switches and config vars to control colo-agent

 docs/man/xl.conf.pod.5                             |    6 +
 docs/man/xl.pod.1                                  |   11 +-
 tools/blktap2/drivers/Makefile                     |    5 +-
 tools/blktap2/drivers/block-colo.c                 | 1151 +++++++++++++++++++
 tools/blktap2/drivers/block-remus.c                | 1174 +++++---------------
 tools/blktap2/drivers/block-replication.c          | 1116 +++++++++++++++++++
 tools/blktap2/drivers/block-replication.h          |  217 ++++
 tools/blktap2/drivers/tapdisk-control.c            |    6 +
 tools/blktap2/drivers/tapdisk-disktype.c           |    9 +
 tools/blktap2/drivers/tapdisk-disktype.h           |    3 +-
 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 +
 tools/hotplug/Linux/Makefile                       |    2 +
 tools/hotplug/Linux/colo-agent-setup               |  198 ++++
 tools/hotplug/Linux/remus-netbuf-setup             |   45 +-
 tools/hotplug/Linux/xen-network-ft.sh              |  102 ++
 tools/libxc/xc_domain_restore.c                    |   44 +-
 tools/libxc/xc_domain_save.c                       |   66 +-
 tools/libxc/xenguest.h                             |   40 +
 tools/libxl/Makefile                               |   11 +-
 tools/libxl/colo-tc.c                              |  589 ++++++++++
 tools/libxl/libxl.c                                |   78 +-
 tools/libxl/libxl.h                                |    3 +-
 tools/libxl/libxl_blktap2.c                        |    6 +-
 ...xl_remus_device.c => libxl_checkpoint_device.c} |  229 ++--
 tools/libxl/libxl_colo.h                           |   48 +
 tools/libxl/libxl_colo_nic.c                       |  309 ++++++
 tools/libxl/libxl_colo_restore.c                   |  990 +++++++++++++++++
 tools/libxl/libxl_colo_save.c                      |  809 ++++++++++++++
 tools/libxl/libxl_colo_save_disk_blktap2.c         |  214 ++++
 tools/libxl/libxl_create.c                         |  151 ++-
 tools/libxl/libxl_device.c                         |    4 +-
 tools/libxl/libxl_dm.c                             |    3 +-
 tools/libxl/libxl_dom.c                            |  234 +---
 tools/libxl/libxl_internal.h                       |  218 ++--
 tools/libxl/libxl_netbuffer.c                      |  127 ++-
 tools/libxl/libxl_noblktap2.c                      |   29 +
 tools/libxl/libxl_nonetbuffer.c                    |   14 +-
 tools/libxl/libxl_remus.c                          |  377 +++++++
 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                        |   15 +-
 tools/libxl/libxl_types_internal.idl               |    4 +-
 tools/libxl/libxl_utils.c                          |    2 +
 tools/libxl/libxlu_disk_l.l                        |    1 +
 tools/libxl/xl.c                                   |    3 +
 tools/libxl/xl.h                                   |    1 +
 tools/libxl/xl_cmdimpl.c                           |   95 +-
 tools/libxl/xl_cmdtable.c                          |    3 +-
 55 files changed, 7433 insertions(+), 1581 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
 create mode 100755 tools/hotplug/Linux/colo-agent-setup
 create mode 100644 tools/hotplug/Linux/xen-network-ft.sh
 create mode 100644 tools/libxl/colo-tc.c
 rename tools/libxl/{libxl_remus_device.c => libxl_checkpoint_device.c} (40%)
 create mode 100644 tools/libxl/libxl_colo.h
 create mode 100644 tools/libxl/libxl_colo_nic.c
 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 v3 1/22 move remus related codes to libxl_remus.c

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

Cc Shriram Rajagopalan <[email protected]>
---
 tools/libxl/Makefile      |   2 +-
 tools/libxl/libxl.c       |  57 +-------
 tools/libxl/libxl_dom.c   | 220 +------------------------------
 tools/libxl/libxl_remus.c | 322 ++++++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_remus.h |  28 ++++
 5 files changed, 356 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 +322 -0

322 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
#2 lore raw

RFC PATCH v3 2/22 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_instance_ops\b/libxl__checkpoint_device_instance_ops/g' tools/libxl/*.[ch]
6. perl -pi -e 's/\blibxl__remus_callback\b/libxl__checkpoint_callback/g' tools/libxl/*.[ch]
7. perl -pi -e 's/\bremus_device_init\b/checkpoint_device_init/g' tools/libxl/*.[ch]
8. perl -pi -e 's/\bremus_devices_setup\b/checkpoint_devices_setup/g' tools/libxl/*.[ch]
9. perl -pi -e 's/\bdefine_remus_checkpoint_api\b/define_checkpoint_api/g' tools/libxl/*.[ch]
10. perl -pi -e 's/\brds\b/cds/g' tools/libxl/*.[ch]
11. perl -pi -e 's/REMUS_DEVICE/CHECKPOINT_DEVICE/g' tools/libxl/*.[ch] tools/libxl/*.idl
12. perl -pi -e 's/REMUS_DEVOPS/CHECKPOINT_DEVOPS/g' tools/libxl/*.[ch] tools/libxl/*.idl
13. perl -pi -e 's/REMUS_NIC/CHECKPOINT_NIC/g' tools/libxl/*.[ch] tools/libxl/*.idl
14. perl -pi -e 's/REMUS_DISK/CHECKPOINT_DISK/g' tools/libxl/*.[ch] tools/libxl/*.idl
15. perl -pi -e 's/\bremus\b/checkpoint/g' tools/libxl/libxl_checkpoint_device.[ch]
16. perl -pi -e 's/\bremus device/checkpoint device/g' tools/libxl/libxl_internal.h
17. perl -pi -e 's/\bRemus device/checkpoint device/g' tools/libxl/libxl_internal.h
18. perl -pi -e 's/\bremus abstract/checkpoint abstract/g' tools/libxl/libxl_internal.h
19. perl -pi -e 's/\bremus invocation/checkpoint invocation/g' tools/libxl/libxl_internal.h
20. perl -pi -e 's/\blibxl__remus_device_\(/libxl__checkpoint_device_(/g' tools/libxl/libxl_internal.h

Cc Shriram Rajagopalan <[email protected]>
---
 tools/libxl/Makefile                               |   2 +-
 ...xl_remus_device.c => libxl_checkpoint_device.c} | 200 ++++++++++-----------
 tools/libxl/libxl_internal.h                       | 112 ++++++------
 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 +-
 tools/libxl/libxl_types_internal.idl               |   4 +-
 9 files changed, 298 insertions(+), 298 deletions(-)
 rename tools/libxl/{libxl_remus_device.c => libxl_checkpoint_device.c} (46%)
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 +100 -100

354 lines not on this page · raw message

tools/libxl/libxl_internal.h +56 -56

215 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 5ac32a4..48b846c 100644
@@ -22,26 +22,26 @@ int libxl__netbuffer_enabled(libxl__gc *gc)
     return 0;
 }
 
-int init_subkind_nic(libxl__remus_devices_state *rds)
+int init_subkind_nic(libxl__checkpoint_devices_state *cds)
 {
     return 0;
 }
 
-void cleanup_subkind_nic(libxl__remus_devices_state *rds)
+void cleanup_subkind_nic(libxl__checkpoint_devices_state *cds)
 {
     return;
 }
 
-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);
 }
 
-const libxl__remus_device_instance_ops remus_device_nic = {
-    .kind = LIBXL__DEVICE_KIND_REMUS_NIC,
+const libxl__checkpoint_device_instance_ops remus_device_nic = {
+    .kind = LIBXL__DEVICE_KIND_CHECKPOINT_NIC,
     .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

172 lines not on this page · raw message

tools/libxl/libxl_types.idl +2 -2
index a6291e7..8d0fa85 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", [
tools/libxl/libxl_types_internal.idl +2 -2
index 720232e..757b060 100644
@@ -22,8 +22,8 @@ libxl__device_kind = Enumeration("device_kind", [
     (6, "VKBD"),
     (7, "CONSOLE"),
     (8, "VTPM"),
-    (9, "REMUS_NIC"),
-    (10, "REMUS_DISK"),
+    (9, "CHECKPOINT_NIC"),
+    (10, "CHECKPOINT_DISK"),
     ])
 
 libxl__console_backend = Enumeration("console_backend", [
Signature · 1 line
1.9.3
#3 lore raw

RFC PATCH v3 3/22 adjust the indentation

In reply to Wen Congyang
---
 tools/libxl/libxl_checkpoint_device.c | 23 ++++++++++++-----------
 tools/libxl/libxl_internal.h          | 21 ++++++++++++---------
 tools/libxl/libxl_remus.c             | 12 ++++++++----
 3 files changed, 32 insertions(+), 24 deletions(-)
tools/libxl/libxl_checkpoint_device.c +12 -11
index 729131a..41b9529 100644
@@ -71,9 +71,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__device_kind kind,
-                                              void *libxl_dev)
+                libxl__checkpoint_devices_state *cds,
+                libxl__device_kind kind,
+                void *libxl_dev)
 {
     libxl__checkpoint_device *dev = NULL;
 
@@ -88,9 +88,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;
 
@@ -136,7 +137,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;
@@ -192,7 +193,7 @@ static void devices_setup_cb(libxl__egc *egc,
 }
 
 void libxl__checkpoint_devices_teardown(libxl__egc *egc,
-                                   libxl__checkpoint_devices_state *cds)
+                                        libxl__checkpoint_devices_state *cds)
 {
     int i;
     libxl__checkpoint_device *dev;
@@ -254,12 +255,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 +12 -9
index a928595..628914f 100644
@@ -2546,7 +2546,8 @@ typedef struct libxl__save_helper_state {
  * Each device type needs to implement the interfaces specified in
  * the libxl__checkpoint_device_instance_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
@@ -2607,7 +2608,8 @@ int init_subkind_drbd_disk(libxl__checkpoint_devices_state *cds);
 void cleanup_subkind_drbd_disk(libxl__checkpoint_devices_state *cds);
 
 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
@@ -2615,7 +2617,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;
@@ -2629,7 +2631,8 @@ struct libxl__checkpoint_devices_state {
     /*
      * this array is allocated before setup the checkpoint devices by the
      * checkpoint abstract layer.
-     * devs may be NULL, means there's no checkpoint devices that has been set up.
+     * devs may be NULL, means there's no checkpoint devices that has been
+     * set up.
      * the size of this array is 'num_devices', which is the total number
      * of libxl nic devices and disk devices(num_nics + num_disks).
      */
@@ -2689,15 +2692,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 c6551a8..2ffab2a 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);
@@ -226,7 +229,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
#4 lore raw

RFC PATCH v3 4/22 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 are
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.
3. init/cleanup device subkind: we call (init|cleanup)_subkind_nic and (init|cleanup)_subkind_drbd_disk directly in checkpoint device. Call them before calling libxl__checkpoint_devices_setup() or after calling libxl__checkpoint_devices_teardown().

Cc Shriram Rajagopalan <[email protected]>
---
 tools/libxl/libxl.c                   |  2 +-
 tools/libxl/libxl_checkpoint_device.c | 52 ++------------------
 tools/libxl/libxl_dom.c               |  3 +-
 tools/libxl/libxl_internal.h          | 37 ++++++++++-----
 tools/libxl/libxl_netbuffer.c         | 51 +++++++++++---------
 tools/libxl/libxl_remus.c             | 89 +++++++++++++++++++++++++++--------
 tools/libxl/libxl_remus.h             |  5 +-
 tools/libxl/libxl_remus_disk_drbd.c   |  9 ++--
 8 files changed, 136 insertions(+), 112 deletions(-)
tools/libxl/libxl.c +1 -1
index 24e209b..f0cc6ca 100644
@@ -831,7 +831,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 -49

85 lines not on this page · raw message

tools/libxl/libxl_dom.c +1 -2
index 11a8a66..6488ffd 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 (libxl_defbool_val(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 628914f..d575a4a 100644
@@ -2624,6 +2624,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_instance_ops **ops;
 
     /*----- private for abstract layer only -----*/
 
@@ -2644,16 +2646,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;
 };
 
 /*
@@ -2701,6 +2693,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 -----*/
@@ -2764,9 +2777,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 +70 -19

207 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 3f1e16a..586e1b6 100644
@@ -28,10 +28,12 @@ typedef struct libxl__remus_drbd_disk {
 
 int init_subkind_drbd_disk(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;
 }
@@ -95,6 +97,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 */
@@ -106,7 +109,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
#5 lore raw

RFC PATCH v3 5/22 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 d575a4a..756d54a 100644
@@ -3073,6 +3073,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
#6 lore raw

RFC PATCH v3 6/22 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 f0cc6ca..8e7d87c 100644
@@ -822,7 +822,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 2e076c1..de17151 100644
@@ -853,7 +853,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 ee328e9..545100b 100644
@@ -1432,8 +1432,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)
 {
@@ -1444,6 +1444,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);
@@ -1472,7 +1473,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);
 }
 
@@ -1482,7 +1483,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 756d54a..190c0d4 100644
@@ -2769,6 +2769,7 @@ struct libxl__domain_suspend_state {
 
     uint32_t domid;
     int fd;
+    int recv_fd;
     libxl_domain_type type;
     int live;
     int debug;
@@ -3033,6 +3034,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 8d0fa85..29b024a 100644
@@ -178,6 +178,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
 #
@@ -308,6 +314,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 e2a896a..8702e08 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 */
 };
 
@@ -2070,6 +2071,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;
 
@@ -2086,6 +2088,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);
@@ -2247,6 +2250,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,
@@ -3784,6 +3788,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;
 
@@ -3954,6 +3959,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;
@@ -4393,6 +4399,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
#7 lore raw

RFC PATCH v3 7/22 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 6488ffd..e49f40f 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 190c0d4..d5b3197 100644
@@ -2738,6 +2738,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
@@ -3029,6 +3030,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;
@@ -3041,6 +3062,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
#8 lore raw

RFC PATCH v3 8/22 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 8e7d87c..c86b988 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
@@ -831,7 +832,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 (libxl_defbool_val(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 e49f40f..ea08e41 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 && libxl_defbool_val(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 && libxl_defbool_val(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 d5b3197..1cc90fb 100644
@@ -2526,7 +2526,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:
@@ -2544,7 +2544,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_instance_ops if it wishes to support Remus.
+ * the libxl__checkpoint_device_instance_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_instance_ops libxl__checkpoint_device_in
 
 /*
  * Interfaces to be implemented by every device subkind 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
@@ -2716,6 +2716,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;
@@ -2779,7 +2798,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 29b024a..a8be7ba 100644
@@ -611,6 +611,7 @@ libxl_domain_remus_info = Struct("domain_remus_info",[
     ("netbuf",       libxl_defbool),
     ("netbufscript", string),
     ("diskbuf",      libxl_defbool),
+    ("colo",         libxl_defbool)
     ])
 
 libxl_event_type = Enumeration("event_type", [
Signature · 1 line
1.9.3
#9 lore raw

RFC PATCH v3 9/22 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
#10 lore raw

RFC PATCH v3 10/22 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 21a6177..f7f683a 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) ) {
@@ -2301,6 +2303,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 */
@@ -2367,6 +2370,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
#11 lore raw

RFC PATCH v3 11/22 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 f7f683a..267e1e1 100644
@@ -2389,7 +2389,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 bb5ef9f..3fd587b 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 6e7d2c9..ec5946d 100644
@@ -1086,6 +1086,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
#12 lore raw

RFC PATCH v3 12/22 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/libxl.c       |  6 ++++++
 tools/libxl/xl_cmdimpl.c  | 48 ++++++++++++++++++++++++++++++++++++++---------
 tools/libxl/xl_cmdtable.c |  3 ++-
 4 files changed, 56 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/libxl.c +6 -0
index c86b988..39a1879 100644
@@ -802,6 +802,12 @@ int libxl_domain_remus_start(libxl_ctx *ctx, libxl_domain_remus_info *info,
         goto out;
     }
 
+    /* The caller must set this defbool */
+    if (libxl_defbool_is_default(info->colo)) {
+        LOG(ERROR, "colo mode must be enabled/disabled");
+        goto out;
+    }
+
     libxl_defbool_setdefault(&info->unsafe, false);
     libxl_defbool_setdefault(&info->blackhole, false);
     libxl_defbool_setdefault(&info->compression, true);
tools/libxl/xl_cmdimpl.c +39 -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
#13 lore raw

RFC PATCH v3 13/22 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.

Cc Shriram Rajagopalan <[email protected]>
---
 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
#14 lore raw

Re: [RFC Patch v3 13/22] blktap2: connect to backup asynchronously

Quoted 9 lines · 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.

Hi
This is useful functionality but also very dangerous. I am afraid your
comments are quite sparse in addressing how the I/O requests are handled at the primary until the connection is established.
Before this patch, the guest would block halfway through boot, until the backup connection is established. What happens after this patch? Will the guest's write requests go through? If so, are you merging writes to same block in your internal ring? What about reads? Do you query your in memory queue to serve these reads?
You are basically introducing a copy on write semantics at the primary
until a backup connection is established, with the faulting writes and subsequent reads being served out of memory. What happens to the network io during this wait time? Are all outputs blocked?
The DRBD version allows the primary to function just like a normal VM until Remus is started. When Remus is started, it ensures that both disks are in sync before the initial migration starts. Are you doing something similar here?

The patch is unfortunately too mangled to follow. You may have to break it up, as the diff lines seem to straddle across function boundaries in confusing ways, making it hard to understand.

Quoted 4 lines
Cc Shriram Rajagopalan <[email protected]>
---
 tools/blktap2/drivers/block-remus.c | 760

+++++++++++++++++++++++-------------

Review

Quoted 3 lines
 1 file changed, 479 insertions(+), 281 deletions(-)
(quoted diff) +0 -0

b/tools/blktap2/drivers/block-remus.c

Quoted 51 lines

index d358b44..c21f851 100644

tools/blktap2/drivers/block-remus.c +26 -4
@@ -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 {

tools/blktap2/drivers/block-remus.c enum tdremus_mode { · line 112

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +3 -3
        /* 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

Quoted 39 lines
tools/blktap2/drivers/block-remus.c +18 -4
@@ -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;

tools/blktap2/drivers/block-remus.c static int tdremus_close(td_driver_t *driver); · line 212

Quoted 8 lines
tools/blktap2/drivers/block-remus.c +2 -1
@@ -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)

tools/blktap2/drivers/block-remus.c static inline unsigned int ring_next(struct

Quoted 8 lines
tools/blktap2/drivers/block-remus.c +2 -1
+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)

Quoted 4 lines
tools/blktap2/drivers/block-remus.c +0 -1
 static inline int ring_isempty(struct req_ring* ring)
 {
-       return ring->head == ring->tail;

tools/blktap2/drivers/block-remus.c static inline unsigned int ring_next(struct req_ring* ring, unsigned int pos)

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +5 -1
+       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)

Quoted 40 lines
tools/blktap2/drivers/block-remus.c +16 -11
+{
+       /* 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;

tools/blktap2/drivers/block-remus.c static int mwrite(int fd, void* buf, size_t len) · line 800

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +2 -2
-       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);

tools/blktap2/drivers/block-remus.c static int mwrite(int fd, void* buf, size_t len)

Quoted 12 lines
tools/blktap2/drivers/block-remus.c +12 -0
+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");

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct

Quoted 6 lines
tools/blktap2/drivers/block-remus.c +2 -0
+       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)

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 4 lines
tools/blktap2/drivers/block-remus.c +0 -1
        int rc;
        int flags;
 
-       RPRINTF("client connecting to %s:%d...\n",

inet_ntoa(state->sa.sin_addr), ntohs(state->sa.sin_port));

Quoted 11 lines
tools/blktap2/drivers/block-remus.c +4 -1
+       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 */

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +4 -3
        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

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct · line 777

Quoted 1 line
tools/blktap2/drivers/block-remus.c +0 -1
-        * our non-blocking connect. rather than duplicating code we

trigger a timeout

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 8 lines
tools/blktap2/drivers/block-remus.c +5 -2
-        * 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) {

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct · line 781

Quoted 1 line
tools/blktap2/drivers/block-remus.c +0 -1
-               RPRINTF("error registering timeout client connection

event handler: %s\n", strerror(id));

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 3 lines
tools/blktap2/drivers/block-remus.c +1 -2
-               /* TODO: we leak a fd here */
-               return -1;
+       if((id = tapdisk_server_register_event(SCHEDULER_POLL_TIMEOUT,

fd, 0,

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 3 lines
tools/blktap2/drivers/block-remus.c +3 -0
+                                              remus_retry_connect_event,
+                                              state)) < 0) {
+               RPRINTF("error registering timeout client connection

event handler: %s\n",

Quoted 12 lines
tools/blktap2/drivers/block-remus.c +5 -3
+                       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)

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +3 -5
 {
-       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));

Quoted 5 lines
tools/blktap2/drivers/block-remus.c +0 -4
-       if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
-               RPRINTF("could not create client socket: %d\n", errno);
-               return -1;
-       }

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +3 -6
+       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");

Quoted 16 lines
tools/blktap2/drivers/block-remus.c +4 -11
-                               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 */

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +0 -9
-       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) {

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct · line 831

Quoted 1 line
tools/blktap2/drivers/block-remus.c +0 -1
-               RPRINTF("error registering client event handler: %s\n",

strerror(id));

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +2 -4
-               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;
 }

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +0 -9
 
-/* 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

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct · line 850

Quoted 1 line
tools/blktap2/drivers/block-remus.c +0 -1
- * backup. This effectively blocks until all data has been copied into a

system

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct · line 852

Quoted 2 lines
tools/blktap2/drivers/block-remus.c +0 -2
- * 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

Quoted 9 lines
tools/blktap2/drivers/block-remus.c +1 -7
- * 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;

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +5 -3
-       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 */

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 4 lines
tools/blktap2/drivers/block-remus.c +1 -3
-       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,

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 12 lines
tools/blktap2/drivers/block-remus.c +6 -4
+                                          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)

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 8 lines
tools/blktap2/drivers/block-remus.c +3 -4
-               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)

Quoted 7 lines
tools/blktap2/drivers/block-remus.c +4 -2
-               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);

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +8 -1
+       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",

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 7 lines
tools/blktap2/drivers/block-remus.c +3 -3
+                       strerror(id));
+               return ERROR_INTERNAL;
+       }
 
- fail:
-       /* switch to unprotected mode and tell tapdisk to retry */
-       RPRINTF("write request replication failed, switching to

unprotected mode");

Quoted 22 lines
tools/blktap2/drivers/block-remus.c +6 -11
-       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;
 

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +5 -5
-       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",

Quoted 37 lines
tools/blktap2/drivers/block-remus.c +9 -21
+                       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;

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +4 -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)

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 6 lines
tools/blktap2/drivers/block-remus.c +1 -1
 {
        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))

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 3 lines
tools/blktap2/drivers/block-remus.c +0 -3
-           && errno != EINPROGRESS)
-       {
-               if(errno == ECONNREFUSED || errno == ENETUNREACH || errno

== EAGAIN || errno == ECONNABORTED)

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 4 lines
tools/blktap2/drivers/block-remus.c +0 -4
-               {
-                       /* 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) {

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct · line 956

Quoted 1 line
tools/blktap2/drivers/block-remus.c +0 -1
-                               RPRINTF("error registering timeout client

connection event handler: %s\n", strerror(id));

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 8 lines
tools/blktap2/drivers/block-remus.c +0 -8
-                               return;
-                       }
-                       s->stream_fd.id = id;
-               }
-               else
-               {
-                       /* not recoverable */
-                       RPRINTF("error connection to server %s\n",

strerror(errno));

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 8 lines
tools/blktap2/drivers/block-remus.c +8 -0
+       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

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 11 lines
tools/blktap2/drivers/block-remus.c +5 -4
+                        * 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 */

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct · line 972

Quoted 3 lines
tools/blktap2/drivers/block-remus.c +0 -2
-               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) {

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct · line 973

Quoted 1 line
tools/blktap2/drivers/block-remus.c +0 -1
-                       RPRINTF("error registering client connection

event handler: %s\n", strerror(id));

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +7 -1
+               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));

Quoted 19 lines
tools/blktap2/drivers/block-remus.c +13 -1
+               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)

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +2 -2
 {
        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)) {

Quoted 12 lines
tools/blktap2/drivers/block-remus.c +3 -2
+       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 */

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +8 -1
+               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

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct · line 1002

Quoted 1 line
tools/blktap2/drivers/block-remus.c +0 -1
-                  || socket_errno == ECONNABORTED || socket_errno ==

EAGAIN)

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct · line 1004

Quoted 2 lines
tools/blktap2/drivers/block-remus.c +0 -2
-               {
-                       /* we can probably assume that the backup is

down. just try again later */

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct · line 1006

Quoted 2 lines
tools/blktap2/drivers/block-remus.c +0 -2
-                       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) {

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct · line 1007

Quoted 1 line
tools/blktap2/drivers/block-remus.c +0 -1
-                               RPRINTF("error registering timeout client

connection event handler: %s\n", strerror(id));

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 7 lines
tools/blktap2/drivers/block-remus.c +0 -7
-                               return;
-                       }
-                       s->stream_fd.id = id;
-               }
-               else
-               {
-                       RPRINTF("socket connect returned %d, giving

up\n", socket_errno);

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 7 lines
tools/blktap2/drivers/block-remus.c +0 -7
-               }
-       }
-       else
-       {
-               /* the connect succeeded */
-
-               /* unregister this function and register a new event

handler */

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct · line 1023

Quoted 2 lines
tools/blktap2/drivers/block-remus.c +0 -2
-               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) {

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct · line 1024

Quoted 1 line
tools/blktap2/drivers/block-remus.c +0 -1
-                       RPRINTF("error registering client event handler:

%s\n", strerror(id));

Quoted 16 lines
tools/blktap2/drivers/block-remus.c +9 -3
                        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)

tools/blktap2/drivers/block-remus.c static int primary_do_connect(struct tdremus_state *state)

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +6 -1
+               goto fail;
+
+       return;
+
+fail:
+       primary_failed(s, rc);
 }
 
 
-/* we install this event handler on the primary once we have connected

to the backup */

tools/blktap2/drivers/block-remus.c static void remus_client_event(event_id_t id, char

Quoted 8 lines
tools/blktap2/drivers/block-remus.c +4 -0
+/*
+ * 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)

tools/blktap2/drivers/block-remus.c static void remus_client_event(event_id_t id, char mode, void *private)

Quoted 4 lines
tools/blktap2/drivers/block-remus.c +0 -1
        int rc;
 
        if (mread(s->stream_fd.fd, req, sizeof(req) - 1) < 0) {
-               /* replication stream closed or otherwise broken

(timeout, reset, &c) */

tools/blktap2/drivers/block-remus.c static void remus_client_event(event_id_t id,

Quoted 11 lines
tools/blktap2/drivers/block-remus.c +5 -1
+               /*
+                * 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)

Quoted 26 lines
tools/blktap2/drivers/block-remus.c +22 -1
                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)

tools/blktap2/drivers/block-remus.c static void remus_client_event(event_id_t id, char mode, void *private)

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +10 -0
+{
+       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)

Quoted 4 lines
tools/blktap2/drivers/block-remus.c +4 -0
+               return ERROR_IO;
+
+       if (mwrite(s->stream_fd.fd, header, sizeof(header)) < 0)
+               return ERROR_IO;

tools/blktap2/drivers/block-remus.c static void remus_client_event(event_id_t id, char mode, void *private)

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +10 -0
+
+       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

tools/blktap2/drivers/block-remus.c static void remus_client_event(event_id_t id, · line 1134

Quoted 1 line
tools/blktap2/drivers/block-remus.c +1 -0
+ * backup. This effectively blocks until all data has been copied into a

system

tools/blktap2/drivers/block-remus.c static void remus_client_event(event_id_t id, · line 1136

Quoted 2 lines
tools/blktap2/drivers/block-remus.c +2 -0
+ * 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

Quoted 24 lines
tools/blktap2/drivers/block-remus.c +22 -0
+ * 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);

tools/blktap2/drivers/block-remus.c static void remus_client_event(event_id_t id, char mode, void *private)

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +9 -0
+       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");

tools/blktap2/drivers/block-remus.c static void remus_server_accept(event_id_t id,

Quoted 85 lines
tools/blktap2/drivers/block-remus.c +69 -1
+       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)

tools/blktap2/drivers/block-remus.c static void remus_server_accept(event_id_t id, · line 1082

Quoted 4 lines
tools/blktap2/drivers/block-remus.c +0 -1
                return;
        }
 
-       /* TODO: check to see if we are already replicating. if so just

close the

tools/blktap2/drivers/block-remus.c static void remus_server_accept(event_id_t id, char mode, void* private)

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +5 -2
-        * 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,

tools/blktap2/drivers/block-remus.c static void remus_server_accept(event_id_t id, · line 1266

Quoted 2 lines
tools/blktap2/drivers/block-remus.c +1 -1
-                                           remus_server_event, s);
+       id = tapdisk_server_register_event(SCHEDULER_POLL_READ_FD,

stream_fd, 0,

tools/blktap2/drivers/block-remus.c static void remus_server_accept(event_id_t id, char mode, void* private)

Quoted 4 lines
tools/blktap2/drivers/block-remus.c +1 -2
+                                          remus_server_event, s);
 
-       if(cid < 0) {
-               RPRINTF("error registering connection event handler:

%s\n", strerror(errno));

tools/blktap2/drivers/block-remus.c static void remus_server_accept(event_id_t id, · line 1270

Quoted 2 lines
tools/blktap2/drivers/block-remus.c +2 -0
+       if (id < 0) {
+               RPRINTF("error registering connection event handler:

%s\n",

Quoted 15 lines
tools/blktap2/drivers/block-remus.c +2 -2
+                       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;

tools/blktap2/drivers/block-remus.c static void remus_server_accept(event_id_t id, char mode, void* private)

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +1 -2
        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)

tools/blktap2/drivers/block-remus.c static void remus_server_accept(event_id_t id, · line 1114

Quoted 1 line
tools/blktap2/drivers/block-remus.c +0 -1
-               RPRINTF("Error setting REUSEADDR on %d: %d\n",

s->server_fd.fd, errno);

tools/blktap2/drivers/block-remus.c static void remus_server_accept(event_id_t id, · line 1115

Quoted 2 lines
tools/blktap2/drivers/block-remus.c +0 -1
-       if (bind(s->server_fd.fd, (struct sockaddr *)&s->sa,

sizeof(s->sa)) < 0) {

tools/blktap2/drivers/block-remus.c static void remus_server_accept(event_id_t id, · line 1116

Quoted 1 line
tools/blktap2/drivers/block-remus.c +0 -1
-               RPRINTF("could not bind server socket %d to %s:%d: %d

%s\n", s->server_fd.fd,

tools/blktap2/drivers/block-remus.c static void remus_server_accept(event_id_t id, · line 1117

Quoted 1 line
tools/blktap2/drivers/block-remus.c +0 -1
-                       inet_ntoa(s->sa.sin_addr), ntohs(s->sa.sin_port),

errno, strerror(errno));

tools/blktap2/drivers/block-remus.c static void remus_server_accept(event_id_t id, char mode, void* private)

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +9 -1
-               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",

tools/blktap2/drivers/block-remus.c static void remus_server_accept(event_id_t id, char mode, void* private)

Quoted 13 lines
tools/blktap2/drivers/block-remus.c +4 -1
+                       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

tools/blktap2/drivers/block-remus.c static void remus_server_accept(event_id_t id, char mode, void* private)

Quoted 3 lines
tools/blktap2/drivers/block-remus.c +0 -3
-   * the fd with tapdisk */
-
-       if((s->server_fd.id =

tapdisk_server_register_event(SCHEDULER_POLL_READ_FD,

tools/blktap2/drivers/block-remus.c static void remus_server_accept(event_id_t id, · line 1131

Quoted 1 line
tools/blktap2/drivers/block-remus.c +0 -1
-

s->server_fd.fd, 0,

tools/blktap2/drivers/block-remus.c static void remus_server_accept(event_id_t id, · line 1132

Quoted 1 line
tools/blktap2/drivers/block-remus.c +0 -1
-

remus_server_accept, s)) < 0) {

tools/blktap2/drivers/block-remus.c static void remus_server_accept(event_id_t id, char mode, void* private)

Quoted 9 lines
tools/blktap2/drivers/block-remus.c +8 -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",

tools/blktap2/drivers/block-remus.c void backup_queue_write(td_driver_t *driver,

Quoted 17 lines
tools/blktap2/drivers/block-remus.c +4 -4
-                       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)

Quoted 42 lines
tools/blktap2/drivers/block-remus.c +14 -4
        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;

tools/blktap2/drivers/block-remus.c static int server_do_wreq(td_driver_t *driver) · line 1231

Quoted 5 lines
tools/blktap2/drivers/block-remus.c +0 -2
@@ -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));

tools/blktap2/drivers/block-remus.c static int server_do_wreq(td_driver_t *driver) · line 1428

Quoted 2 lines
tools/blktap2/drivers/block-remus.c +1 -1
-               return -1;
+               /* freak out! How to handle the remaining data from

primary */

Quoted 45 lines
tools/blktap2/drivers/block-remus.c +11 -8
+               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;
 

tools/blktap2/drivers/block-remus.c static int server_do_creq(td_driver_t *driver) · line 1470

Quoted 3 lines
tools/blktap2/drivers/block-remus.c +0 -0
@@ -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)

tools/blktap2/drivers/block-remus.c static int switch_mode(td_driver_t *driver, enum

Quoted 19 lines
tools/blktap2/drivers/block-remus.c +1 -7
-               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)

tools/blktap2/drivers/block-remus.c static void ctl_request(event_id_t id, char mode,

Quoted 20 lines
tools/blktap2/drivers/block-remus.c +13 -0
        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)

tools/blktap2/drivers/block-remus.c static void ctl_request(event_id_t id, char mode, · line 1703

Quoted 2 lines
tools/blktap2/drivers/block-remus.c +0 -0
        if (!(rc = read(s->ctl_fd.fd, msg, sizeof(msg) - 1 /* append nul

*/))) {

tools/blktap2/drivers/block-remus.c static void ctl_request(event_id_t id, char mode, · line 1499

Quoted 2 lines
tools/blktap2/drivers/block-remus.c +0 -1
                RPRINTF("0-byte read received, reopening FIFO\n");
-               /*TODO: we may have to unregister/re-register with

tapdisk_server */

tools/blktap2/drivers/block-remus.c static int ctl_register(struct tdremus_state *s) · line 1644

Quoted 13 lines
tools/blktap2/drivers/block-remus.c +0 -6
-               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);

tools/blktap2/drivers/block-remus.c static int ctl_register(struct tdremus_state *s) · line 1844

Quoted 1 line
tools/blktap2/drivers/block-remus.c +1 -0
+       s->ctl_fd.cid =

tapdisk_server_register_event(SCHEDULER_POLL_READ_FD, s->ctl_fd.fd, 0, ctl_request, s);

tools/blktap2/drivers/block-remus.c static int ctl_register(struct tdremus_state *s) · line 1646

Quoted 3 lines
tools/blktap2/drivers/block-remus.c +0 -2
-       if (s->ctl_fd.id < 0) {
-               RPRINTF("error registering ctrl FIFO %s: %d\n",

s->ctl_path, s->ctl_fd.id);

Quoted 19 lines
tools/blktap2/drivers/block-remus.c +4 -5
+       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 */
--
Quoted 2 lines

1.9.3

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

#15 lore raw

Re: [RFC Patch v3 13/22] blktap2: connect to backup asynchronously

Quoted 11 lines · Shriram Rajagopalan
Quoted 9 lines · 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.

Quoted 6 lines

Hi
This is useful functionality but also very dangerous. I am afraid your
comments are quite sparse in addressing how the I/O requests are handled at the primary until the connection is established.
Before this patch, the guest would block halfway through boot, until the backup connection is established. What happens after this patch?

This patch doesn't change the behavior. ALl I/O requests are queued in a list. When the connection is established, we will handle the pended request.

Quoted 3 lines

Will the guest's write requests go through? If so, are you merging writes to same block in your internal ring? What about reads? Do you query your in memory queue to serve these reads?

Before the connections is established, if there are no pended I/O requests, the read requests can just go through. If there are some pended I/O requests, read requests are also pended. All write requests are pended before the connection is established. After the coonection is established, read reqeusts can just go through, and write requests are forwarded and go through.

Quoted 8 lines

You are basically introducing a copy on write semantics at the primary
until a backup connection is established, with the faulting writes and subsequent reads being served out of memory. What happens to the network io during this wait time? Are all outputs blocked?
The DRBD version allows the primary to function just like a normal VM until Remus is started. When Remus is started, it ensures that both disks are in sync before the initial migration starts. Are you doing something similar here?

No, block-remus doesn't support it now, and it is hard to implement, because we don't have meta data, and we don't care the file format.

Quoted 5 lines

The patch is unfortunately too mangled to follow. You may have to break it up, as the diff lines seem to straddle across function boundaries in confusing ways, making it hard to understand.

OK, I will try it. And I think I need to update the commit log too.

Thanks
Wen Congyang

#16 lore raw

RFC PATCH v3 14/22 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.

Cc Shriram Rajagopalan <[email protected]>
---
 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
#17 lore raw

RFC PATCH v3 15/22 blktap2: move async connect related codes to block-replication.c

In reply to Wen Congyang

COLO will reuse them.

Cc Shriram Rajagopalan <[email protected]>
---
 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 37c3485..3d8ed8a 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
Acked-by on v3 15/22
#18 lore raw

Re: [RFC Patch v3 15/22] blktap2: move async connect related codes to block-replication.c

Quoted 8 lines · Wen Congyang

COLO will reuse them.

Cc Shriram Rajagopalan <[email protected]>
---
 tools/blktap2/drivers/Makefile            |   2 +-
 tools/blktap2/drivers/block-remus.c       | 494

+++---------------------------

Quoted 1 line
 tools/blktap2/drivers/block-replication.c | 468

++++++++++++++++++++++++++++

Review

Quoted 6 lines
 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
(quoted diff) +0 -0

b/tools/blktap2/drivers/Makefile

Review

Quoted 13 lines

index 37c3485..3d8ed8a 100644

tools/blktap2/drivers/Makefile +1 -1
@@ -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
(quoted diff) +0 -0

b/tools/blktap2/drivers/block-remus.c

Quoted 109 lines

index 5d27d41..8b6f157 100644

tools/blktap2/drivers/block-remus.c +3 -54
@@ -40,6 +40,7 @@
 #include "hashtable.h"
 #include "hashtable_itr.h"
 #include "hashtable_utility.h"
+#include "block-replication.h"
 
 #include <errno.h>
 #include <inttypes.h>
@@ -49,10 +50,7 @@
 #include <string.h>
 #include <sys/time.h>
 #include <sys/types.h>
-#include <sys/socket.h>
-#include <netdb.h>
 #include <netinet/in.h>
-#include <arpa/inet.h>
 #include <sys/param.h>
 #include <sys/sysctl.h>
 #include <unistd.h>
@@ -67,22 +65,6 @@
 
 #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 {
@@ -92,13 +74,6 @@ enum tdremus_mode {
        mode_backup
 };
 
-enum {
-       ERROR_INTERNAL = -1,
-       ERROR_IO = -2,
-       ERROR_CONNECTION = -3,
-       ERROR_CLOSE = -4,
-};
-
 struct tdremus_req {
        td_request_t treq;
 };
@@ -167,21 +142,9 @@ struct ramdisk_write_cbdata {
 
 typedef void (*queue_rw_t) (td_driver_t *driver, td_request_t treq);
 
-/*
- * 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 cid;
-       event_id_t rid;
-       event_id_t wid;
+       event_id_t id;
 } poll_fd_t;
 
 struct tdremus_state {
@@ -195,9 +158,7 @@ struct tdremus_state {
        char*     msg_path; /* output completion message here */
        poll_fd_t msg_fd;
 
-  /* replication host */
-       struct sockaddr_in sa;
-       poll_fd_t server_fd;    /* server listen port */
+       td_replication_connect_t t;
        poll_fd_t stream_fd;     /* replication channel */
 
        /*
@@ -777,28 +738,8 @@ static int mwrite(int fd, void* buf, size_t len)
        select(fd + 1, NULL, &wfds, NULL, &tv);
 }
 
-
-static void inline close_stream_fd(struct tdremus_state *s)
-{
-
-       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);
-}
-

tools/blktap2/drivers/block-remus.c static int mwrite(int fd, void* buf, size_t len) · line 801

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +0 -9
-static void close_server_fd(struct tdremus_state *s)
-{
-       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);

tools/blktap2/drivers/block-remus.c static int primary_forward_request(struct

Quoted 5 lines
tools/blktap2/drivers/block-remus.c +1 -0
+static void remus_client_event(event_id_t id, char mode, void *private);
 static int primary_forward_request(struct tdremus_state *s,
                                   const td_request_t *treq);
 
@@ -808,56 +749,15 @@ static int primary_forward_request(struct

tdremus_state *s,

tools/blktap2/drivers/block-remus.c static int primary_forward_request(struct tdremus_state *s,

Quoted 7 lines
tools/blktap2/drivers/block-remus.c +1 -1
  */
 static void primary_failed(struct tdremus_state *s, int rc)
 {
-       close_stream_fd(s);
+       td_replication_connect_kill(&s->t);
        if (rc == ERROR_INTERNAL)
                RPRINTF("switch to unprotected mode due to internal

error");

Quoted 27 lines
tools/blktap2/drivers/block-remus.c +1 -21
        if (rc == ERROR_CLOSE)
                RPRINTF("switch to unprotected mode before closing");
+       UNREGISTER_EVENT(s->stream_fd.id);
        switch_mode(s->tdremus_driver, mode_unprotected);
 }
 
-static int primary_do_connect(struct tdremus_state *state)
-{
-       event_id_t id;
-       int fd;
-       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 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) {
-               RPRINTF("error setting fd %d to non block mode\n", fd);

tools/blktap2/drivers/block-remus.c static int primary_forward_request(struct tdremus_state *s,

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +0 -10
-               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
-        */
-       if((id = tapdisk_server_register_event(SCHEDULER_POLL_TIMEOUT,

fd, 0,

tools/blktap2/drivers/block-remus.c static int primary_forward_request(struct tdremus_state *s,

Quoted 3 lines
tools/blktap2/drivers/block-remus.c +0 -3
-                                              remus_retry_connect_event,
-                                              state)) < 0) {
-               RPRINTF("error registering timeout client connection

event handler: %s\n",

tools/blktap2/drivers/block-remus.c static int remus_handle_queued_io(struct

Quoted 12 lines
tools/blktap2/drivers/block-remus.c +0 -8
-                       strerror(id));
-               return ERROR_INTERNAL;
-       }
-
-       state->stream_fd.cid = id;
-       return 0;
-}
-
 static int remus_handle_queued_io(struct tdremus_state *s)
 {
        struct req_ring *queued_io = &s->queued_io;
@@ -882,184 +782,35 @@ static int remus_handle_queued_io(struct

tdremus_state *s)

Quoted 9 lines
tools/blktap2/drivers/block-remus.c +2 -1
        return 0;
 }
 
-static int remus_connection_done(struct tdremus_state *s)
+static void remus_client_established(td_replication_connect_t *t, int rc)
 {
+       struct tdremus_state *s = CONTAINER_OF(t, *s, t);
        event_id_t id;
 

tools/blktap2/drivers/block-remus.c static int remus_handle_queued_io(struct tdremus_state *s)

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +4 -5
-       /* 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;
+       if (rc) {
+               primary_failed(s, rc);
+               return;
+       }
 
-       id = tapdisk_server_register_event(SCHEDULER_POLL_READ_FD,

s->stream_fd.fd,

Quoted 18 lines
tools/blktap2/drivers/block-remus.c +2 -12
+       /* the connect succeeded */
+       id = tapdisk_server_register_event(SCHEDULER_POLL_READ_FD, t->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;
-
-       /* handle the queued requests */
-       return remus_handle_queued_io(s);
-}
-
-static int remus_retry_connect(struct tdremus_state *s)
-{
-       event_id_t id;
-

tools/blktap2/drivers/block-remus.c static int remus_handle_queued_io(struct tdremus_state *s)

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +0 -10
-       tapdisk_server_unregister_event(s->stream_fd.cid);
-       s->stream_fd.cid = -1;
-
-       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",

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +0 -10
-                       strerror(id));
-               return ERROR_INTERNAL;
-       }
-
-       s->stream_fd.cid = id;
-       return 0;
-}
-
-static int remus_wait_connect_done(struct tdremus_state *s)
-{

tools/blktap2/drivers/block-remus.c static int remus_handle_queued_io(struct tdremus_state *s)

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +0 -10
-       event_id_t id;
-
-       tapdisk_server_unregister_event(s->stream_fd.cid);
-       s->stream_fd.cid = -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",

Quoted 15 lines
tools/blktap2/drivers/block-remus.c +0 -15
-                       strerror(id));
-               return ERROR_INTERNAL;
-       }
-       s->stream_fd.cid = id;
-
-       return 0;
-}
-
-/* return 1 if we need to reconnect to backup */
-static int check_connect_errno(int err)
-{
-       /*
-        * The fd is non-block, so we will not get ETIMEDOUT
-        * after calling connect(). We only can get this errno
-        * by getsockopt().

tools/blktap2/drivers/block-remus.c static int remus_handle_queued_io(struct tdremus_state *s)

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +0 -10
-        */
-       if (err == ECONNREFUSED || err == ENETUNREACH ||
-           err == EAGAIN || err == ECONNABORTED ||
-           err == ETIMEDOUT)
-           return 1;
-
-       return 0;
-}
-
-static void remus_retry_connect_event(event_id_t id, char mode, void

*private)

tools/blktap2/drivers/block-remus.c static int remus_handle_queued_io(struct tdremus_state *s)

Quoted 13 lines
tools/blktap2/drivers/block-remus.c +0 -13
-{
-       struct tdremus_state *s = (struct tdremus_state *)private;
-       int rc, ret;
-
-       /* do a non-blocking connect */
-       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

Quoted 7 lines
tools/blktap2/drivers/block-remus.c +0 -7
-                        * to determine if the connect worked
-                        */
-                       rc = remus_wait_connect_done(s);
-                       if (rc)
-                               goto fail;
-                       return;
-               }

tools/blktap2/drivers/block-remus.c static int remus_handle_queued_io(struct tdremus_state *s)

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +0 -10
-
-               if (check_connect_errno(errno)) {
-                       rc = remus_retry_connect(s);
-                       if (rc)
-                               goto fail;
-                       return;
-               }
-
-               /* not recoverable */
-               RPRINTF("error connection to server %s\n",

strerror(errno));

tools/blktap2/drivers/block-remus.c static int primary_forward_request(struct

Quoted 77 lines
tools/blktap2/drivers/block-remus.c +6 -60
-               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 */
-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 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)) {
-               RPRINTF("error getting socket errno\n");
+               primary_failed(s, ERROR_INTERNAL);
                return;
        }
 
-       RPRINTF("socket connect returned %d\n", socket_errno);
+       s->stream_fd.fd = t->fd;
+       s->stream_fd.id = id;
 
-       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;
-
-                       return;
-               } else {
-                       RPRINTF("socket connect returned %d, giving up\n",
-                               socket_errno);
-                       rc = ERROR_CONNECTION;
-                       goto fail;
-               }
-
-               return;
-       }
-
-       rc = remus_connection_done(s);
+       /* handle the queued requests */
+       rc = remus_handle_queued_io(s);
        if (rc)
-               goto fail;
-
-       return;
-
-fail:
-       primary_failed(s, rc);
+               primary_failed(s, rc);
 }
 
-
 /*
  * we install this event handler on the primary once we have
  * connected to the backup.
@@ -1142,19 +893,21 @@ static int primary_forward_request(struct

tdremus_state *s,

tools/blktap2/drivers/block-remus.c static void remus_server_event(event_id_t id,

Quoted 37 lines
tools/blktap2/drivers/block-remus.c +7 -7
 static void primary_queue_write(td_driver_t *driver, td_request_t treq)
 {
        struct tdremus_state *s = (struct tdremus_state *)driver->data;
-       int rc;
+       int rc, ret;
 
        // RPRINTF("write: stream_fd.fd: %d\n", s->stream_fd.fd);
 
-       if(s->stream_fd.fd < 0) {
+       ret = td_replication_connect_status(&s->t);
+       if(ret == -1) {
                RPRINTF("connecting to backup...\n");
-               rc = primary_do_connect(s);
+               s->t.callback = remus_client_established;
+               rc = td_replication_client_start(&s->t);
                if (rc)
                        goto fail;
        }
 
        /* The connection is not established, just queue the request */
-       if (s->stream_fd.cid >= 0) {
+       if (ret != 1) {
                ring_add_request(&s->queued_io, &treq);
                return;
        }
@@ -1227,9 +980,7 @@ static int primary_start(td_driver_t *driver)
        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;
+       s->stream_fd.id = -1;
 
        return 0;
 }
@@ -1240,100 +991,32 @@ static void remus_server_event(event_id_t id,

char mode, void *private);

Quoted 25 lines
tools/blktap2/drivers/block-remus.c +4 -12
 /* 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);
+       td_replication_connect_kill(&s->t);
        /* 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)
+static void remus_server_established(td_replication_connect_t *t, int rc)
 {
-       struct tdremus_state* s = (struct tdremus_state *) private;
-
-       int stream_fd;
-
-       /* XXX: add address-based black/white list */
-       if ((stream_fd = accept(s->server_fd.fd, NULL, NULL)) < 0) {
-               RPRINTF("error accepting connection: %d\n", errno);
-               return;
-       }
+       struct tdremus_state *s = CONTAINER_OF(t, *s, t);
+       event_id_t id;
 

tools/blktap2/drivers/block-remus.c static void remus_server_event(event_id_t id, char mode, void *private); · line 1321

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +1 -7
-       /*
-        * TODO: check to see if we are already replicating.
-        * if so just close the connection (or do something
-        * smarter)
-        */
-       RPRINTF("server accepted connection\n");
+       /* rc is always 0 */
 
        /* add tapdisk event for replication stream */
-       id = tapdisk_server_register_event(SCHEDULER_POLL_READ_FD,

stream_fd, 0,

tools/blktap2/drivers/block-remus.c static int remus_handle_queued_io(struct tdremus_state *s) · line 796

Quoted 1 line
tools/blktap2/drivers/block-remus.c +1 -0
+       id = tapdisk_server_register_event(SCHEDULER_POLL_READ_FD, t->fd,

0,

tools/blktap2/drivers/block-remus.c static void remus_server_event(event_id_t id, char mode, void *private);

Quoted 4 lines
tools/blktap2/drivers/block-remus.c +0 -0
                                           remus_server_event, s);
 
        if (id < 0) {
                RPRINTF("error registering connection event handler:

%s\n",

Quoted 22 lines
tools/blktap2/drivers/block-remus.c +1 -16
                        strerror(errno));
-               close(stream_fd);
+               td_replication_server_restart(t);
                return;
        }
 
        /* store replication file descriptor */
-       s->stream_fd.fd = stream_fd;
-       s->stream_fd.rid = id;
-}
-
-/* returns -2 if EADDRNOTAVAIL */
-static int remus_bind(struct tdremus_state* s)
-{
-       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;
-       }

tools/blktap2/drivers/block-remus.c static void remus_server_event(event_id_t id, char mode, void *private);

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +0 -10
-
-       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",

Quoted 11 lines
tools/blktap2/drivers/block-remus.c +0 -11
-                       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;
-       }

tools/blktap2/drivers/block-remus.c static void remus_server_event(event_id_t id, char mode, void *private);

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +0 -10
-
-       /*
-        * 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",

Quoted 36 lines
tools/blktap2/drivers/block-remus.c +2 -27
-                       strerror(id));
-               goto err_sfd;
-       }
-       s->server_fd.cid = id;
-
-       return 0;
-
-err_sfd:
-       CLOSE_FD(s->server_fd.fd);
-
-       return rc;
+       s->stream_fd.fd = t->fd;
+       s->stream_fd.id = id;
 }
 
 /* wait for latest checkpoint to be applied */
@@ -1566,90 +1249,6 @@ static int unprotected_start(td_driver_t *driver)
 
 
 /* control */
-
-static inline int resolve_address(const char* addr, struct in_addr* ia)
-{
-       struct hostent* he;
-       uint32_t ip;
-
-       if (!(he = gethostbyname(addr))) {
-               RPRINTF("error resolving %s: %d\n", addr, h_errno);
-               return -1;
-       }
-
-       if (!he->h_addr_list[0]) {
-               RPRINTF("no address found for %s\n", addr);
-               return -1;
-       }
-

tools/blktap2/drivers/block-remus.c static int unprotected_start(td_driver_t *driver) · line 1594

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +0 -10
-       /* network byte order */
-       ip = *((uint32_t**)he->h_addr_list)[0];
-       ia->s_addr = ip;
-
-       return 0;
-}
-
-static int get_args(td_driver_t *driver, const char* name)
-{
-       struct tdremus_state *state = (struct tdremus_state

*)driver->data;

Quoted 21 lines
tools/blktap2/drivers/block-remus.c +0 -21
-       char* host;
-       char* port;
-//  char* driver_str;
-//  char* parent;
-//  int type;
-//  char* path;
-//  unsigned long ulport;
-//  int i;
-//  struct sockaddr_in server_addr_in;
-
-       int gai_status;
-       int valid_addr;
-       struct addrinfo gai_hints;
-       struct addrinfo *servinfo, *servinfo_itr;
-
-       memset(&gai_hints, 0, sizeof gai_hints);
-       gai_hints.ai_family = AF_UNSPEC;
-       gai_hints.ai_socktype = SOCK_STREAM;
-
-       port = strchr(name, ':');
-       if (!port) {

tools/blktap2/drivers/block-remus.c static int unprotected_start(td_driver_t *driver)

Quoted 10 lines
tools/blktap2/drivers/block-remus.c +0 -10
-               RPRINTF("missing host in %s\n", name);
-               return -ENOENT;
-       }
-       if (!(host = strndup(name, port - name))) {
-               RPRINTF("unable to allocate host\n");
-               return -ENOMEM;
-       }
-       port++;
-
-       if ((gai_status = getaddrinfo(host, port, &gai_hints, &servinfo))

!= 0) {

tools/blktap2/drivers/block-remus.c static int unprotected_start(td_driver_t *driver) · line 1626

Quoted 1 line
tools/blktap2/drivers/block-remus.c +0 -1
-               RPRINTF("getaddrinfo error: %s\n",

gai_strerror(gai_status));

tools/blktap2/drivers/block-remus.c static int unprotected_start(td_driver_t *driver)

Quoted 6 lines
tools/blktap2/drivers/block-remus.c +0 -6
-               return -ENOENT;
-       }
-
-       /* TODO: do something smarter here */
-       valid_addr = 0;
-       for(servinfo_itr = servinfo; servinfo_itr != NULL; servinfo_itr =

servinfo_itr->ai_next) {

tools/blktap2/drivers/block-remus.c static int unprotected_start(td_driver_t *driver)

Quoted 7 lines
tools/blktap2/drivers/block-remus.c +0 -7
-               void *addr;
-               char *ipver;
-
-               if (servinfo_itr->ai_family == AF_INET) {
-                       valid_addr = 1;
-                       memset(&state->sa, 0, sizeof(state->sa));
-                       state->sa = *(struct sockaddr_in

*)servinfo_itr->ai_addr;

tools/blktap2/drivers/block-remus.c static int unprotected_start(td_driver_t *driver)

Quoted 9 lines
tools/blktap2/drivers/block-remus.c +0 -9
-                       break;
-               }
-       }
-       freeaddrinfo(servinfo);
-
-       if (!valid_addr)
-               return -ENOENT;
-
-       RPRINTF("host: %s, port: %d\n", inet_ntoa(state->sa.sin_addr),

ntohs(state->sa.sin_port));

tools/blktap2/drivers/block-remus.c static int ctl_register(struct tdremus_state *s) · line 1847

Quoted 12 lines
tools/blktap2/drivers/block-remus.c +0 -5
-
-       return 0;
-}
-
 static int switch_mode(td_driver_t *driver, enum tdremus_mode mode)
 {
        struct tdremus_state *s = (struct tdremus_state *)driver->data;
@@ -1844,11 +1443,11 @@ static int ctl_register(struct tdremus_state *s)
        RPRINTF("registering ctl fifo\n");
 
        /* register ctl fd */
-       s->ctl_fd.cid =

tapdisk_server_register_event(SCHEDULER_POLL_READ_FD, s->ctl_fd.fd, 0, ctl_request, s);

tools/blktap2/drivers/block-remus.c static int ctl_register(struct tdremus_state *s) · line 1446

Quoted 1 line
tools/blktap2/drivers/block-remus.c +1 -0
+       s->ctl_fd.id =

tapdisk_server_register_event(SCHEDULER_POLL_READ_FD, s->ctl_fd.fd, 0, ctl_request, s);

Quoted 19 lines
tools/blktap2/drivers/block-remus.c +3 -3
-       if (s->ctl_fd.cid < 0) {
+       if (s->ctl_fd.id < 0) {
                RPRINTF("error registering ctrl FIFO %s: %d\n",
-                       s->ctl_path, s->ctl_fd.cid);
+                       s->ctl_path, s->ctl_fd.id);
                return -1;
        }
 
@@ -1859,7 +1458,7 @@ static void ctl_unregister(struct tdremus_state *s)
 {
        RPRINTF("unregistering ctl fifo\n");
 
-       UNREGISTER_EVENT(s->ctl_fd.cid);
+       UNREGISTER_EVENT(s->ctl_fd.id);
 }
 
 /* interface */

tools/blktap2/drivers/block-remus.c static void ctl_unregister(struct tdremus_state *s) · line 1466

Quoted 1 line
tools/blktap2/drivers/block-remus.c +0 -0
@@ -1867,6 +1466,7 @@ static void ctl_unregister(struct tdremus_state *s)
 static int tdremus_open(td_driver_t *driver, td_image_t *image,

td_uuid_t uuid)

tools/blktap2/drivers/block-remus.c static int tdremus_open(td_driver_t *driver,

Quoted 7 lines
tools/blktap2/drivers/block-remus.c +1 -0
 {
        struct tdremus_state *s = (struct tdremus_state *)driver->data;
+       td_replication_connect_t *t = &s->t;
        int rc;
        const char *name = image->name;
        td_flag_t flags = image->flags;
@@ -1877,7 +1477,6 @@ static int tdremus_open(td_driver_t *driver,

td_image_t *image, td_uuid_t uuid)

tools/blktap2/drivers/block-remus.c static int tdremus_open(td_driver_t *driver,

Quoted 8 lines
tools/blktap2/drivers/block-remus.c +0 -1
        remus_image = image;
 
        memset(s, 0, sizeof(*s));
-       s->server_fd.fd = -1;
        s->stream_fd.fd = -1;
        s->ctl_fd.fd = -1;
        s->msg_fd.fd = -1;
@@ -1886,8 +1485,12 @@ static int tdremus_open(td_driver_t *driver,

td_image_t *image, td_uuid_t uuid)

tools/blktap2/drivers/block-remus.c static int tdremus_open(td_driver_t *driver,

Quoted 14 lines
tools/blktap2/drivers/block-remus.c +5 -1
         * the driver stack from the stream_fd event handler */
        s->tdremus_driver = driver;
 
+       t->log_prefix = "remus";
+       t->retry_timeout_s = REMUS_CONNRETRY_TIMEOUT;
+       t->max_connections = 10;
+       t->callback = remus_server_established;
        /* parse name to get info etc */
-       if ((rc = get_args(driver, name)))
+       if ((rc = td_replication_connect_init(t, name)))
                return rc;
 
        if ((rc = ctl_open(driver, name))) {
@@ -1901,7 +1504,7 @@ static int tdremus_open(td_driver_t *driver,

td_image_t *image, td_uuid_t uuid)

Review

Quoted 19 lines
tools/blktap2/drivers/block-remus.c +2 -3
                return rc;
        }
 
-       if (!(rc = remus_bind(s)))
+       if (!(rc = td_replication_server_start(t)))
                rc = switch_mode(driver, mode_backup);
        else if (rc == -2)
                rc = switch_mode(driver, mode_primary);
@@ -1932,8 +1535,7 @@ static int tdremus_close(td_driver_t *driver)
        if (s->ramdisk.inprogress)
                hashtable_destroy(s->ramdisk.inprogress, 0);
 
-       close_server_fd(s);
-       close_stream_fd(s);
+       td_replication_connect_kill(&s->t);
        ctl_unregister(s);
        ctl_close(s);
 
(quoted diff) +0 -0

b/tools/blktap2/drivers/block-replication.c

tools/blktap2/drivers/block-replication.c line 6

Quoted 11 lines

new file mode 100644
index 0000000..e4b2679

tools/blktap2/drivers/block-replication.c new file +6 -0
@@ -0,0 +1,468 @@
+/*
+ * 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

Quoted 14 lines
tools/blktap2/drivers/block-replication.c new file +14 -0
+ * 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.
+ */
+
+#include "tapdisk-server.h"
+#include "block-replication.h"
+
+#include <string.h>
+#include <errno.h>

tools/blktap2/drivers/block-replication.c

Quoted 10 lines
tools/blktap2/drivers/block-replication.c new file +10 -0
+#include <sys/types.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <syslog.h>
+#include <stdlib.h>
+#include <arpa/inet.h>
+
+#undef DPRINTF
+#undef EPRINTF
+#define DPRINTF(_f, _a...) syslog (LOG_DEBUG, "%s: " _f, log_prefix, ##

_a)

Quoted 29 lines
tools/blktap2/drivers/block-replication.c new file +29 -0
+#define EPRINTF(_f, _a...) syslog (LOG_ERR, "%s: " _f, log_prefix, ## _a)
+
+/* connection status */
+enum {
+       connection_none,
+       connection_in_progress,
+       connection_established,
+       connection_closed,
+};
+
+/* common functions */
+/* args should be host:port */
+static int get_args(td_replication_connect_t *t, const char* name)
+{
+       char* host;
+       const char* port;
+       int gai_status;
+       int valid_addr;
+       struct addrinfo gai_hints;
+       struct addrinfo *servinfo, *servinfo_itr;
+       const char *log_prefix = t->log_prefix;
+
+       memset(&gai_hints, 0, sizeof gai_hints);
+       gai_hints.ai_family = AF_UNSPEC;
+       gai_hints.ai_socktype = SOCK_STREAM;
+
+       port = strchr(name, ':');
+       if (!port) {
+               EPRINTF("missing host in %s\n", name);

tools/blktap2/drivers/block-replication.c

Quoted 10 lines
tools/blktap2/drivers/block-replication.c new file +10 -0
+               return -ENOENT;
+       }
+       if (!(host = strndup(name, port - name))) {
+               EPRINTF("unable to allocate host\n");
+               return -ENOMEM;
+       }
+       port++;
+       if ((gai_status = getaddrinfo(host, port,
+                                     &gai_hints, &servinfo)) != 0) {
+               EPRINTF("getaddrinfo error: %s\n",

gai_strerror(gai_status));

tools/blktap2/drivers/block-replication.c

Quoted 13 lines
tools/blktap2/drivers/block-replication.c new file +13 -0
+               free(host);
+               return -ENOENT;
+       }
+       free(host);
+
+       /* TODO: do something smarter here */
+       valid_addr = 0;
+       for (servinfo_itr = servinfo; servinfo_itr != NULL;
+            servinfo_itr = servinfo_itr->ai_next) {
+               if (servinfo_itr->ai_family == AF_INET) {
+                       valid_addr = 1;
+                       memset(&t->sa, 0, sizeof(t->sa));
+                       t->sa = *(struct sockaddr_in

*)servinfo_itr->ai_addr;

Quoted 5 lines
tools/blktap2/drivers/block-replication.c new file +5 -0
+                       break;
+               }
+       }
+       freeaddrinfo(servinfo);
+

tools/blktap2/drivers/block-replication.c

Quoted 10 lines
tools/blktap2/drivers/block-replication.c new file +10 -0
+       if (!valid_addr)
+               return -ENOENT;
+
+       DPRINTF("host: %s, port: %d\n", inet_ntoa(t->sa.sin_addr),
+               ntohs(t->sa.sin_port));
+
+       return 0;
+}
+
+int td_replication_connect_init(td_replication_connect_t *t, const char

*name)

Quoted 62 lines
tools/blktap2/drivers/block-replication.c new file +62 -0
+{
+       int rc;
+
+       rc = get_args(t, name);
+       if (rc)
+               return rc;
+
+       t->listen_fd = -1;
+       t->id = -1;
+       t->status = connection_none;
+       return 0;
+}
+
+int td_replication_connect_status(td_replication_connect_t *t)
+{
+       const char *log_prefix = t->log_prefix;
+
+       switch (t->status) {
+       case connection_none:
+       case connection_closed:
+               return -1;
+       case connection_in_progress:
+               return 0;
+       case connection_established:
+               return 1;
+       default:
+               EPRINTF("td_replication_connect is corruptted\n");
+               return -2;
+       }
+}
+
+void td_replication_connect_kill(td_replication_connect_t *t)
+{
+       if (t->status != connection_in_progress &&
+           t->status != connection_established)
+               return;
+
+       UNREGISTER_EVENT(t->id);
+       CLOSE_FD(t->fd);
+       CLOSE_FD(t->listen_fd);
+       t->status = connection_closed;
+}
+
+/* server */
+static void td_replication_server_accept(event_id_t id, char mode,
+                                        void *private);
+
+int td_replication_server_start(td_replication_connect_t *t)
+{
+       int opt;
+       int rc = -1;
+       event_id_t id;
+       int fd;
+       const char *log_prefix = t->log_prefix;
+
+       if (t->status == connection_in_progress ||
+           t->status == connection_established)
+               return rc;
+
+       fd = socket(AF_INET, SOCK_STREAM, 0);
+       if (fd < 0) {
+               EPRINTF("could not create server socket: %d\n", errno);

tools/blktap2/drivers/block-replication.c

Quoted 10 lines
tools/blktap2/drivers/block-replication.c new file +10 -0
+               return rc;
+       }
+
+       opt = 1;
+       if (setsockopt(fd, SOL_SOCKET,
+                      SO_REUSEADDR, &opt, sizeof(opt)) < 0)
+               DPRINTF("Error setting REUSEADDR on %d: %d\n", fd, errno);
+
+       if (bind(fd, (struct sockaddr *)&t->sa, sizeof(t->sa)) < 0) {
+               DPRINTF("could not bind server socket %d to %s:%d: %d

%s\n",

Quoted 9 lines
tools/blktap2/drivers/block-replication.c new file +9 -0
+                       fd, inet_ntoa(t->sa.sin_addr),
+                       ntohs(t->sa.sin_port), errno, strerror(errno));
+               if (errno == EADDRNOTAVAIL)
+                       rc = -2;
+               goto err;
+       }
+
+       if (listen(fd, t->max_connections)) {
+               EPRINTF("could not listen on socket: %d\n", errno);

tools/blktap2/drivers/block-replication.c

Quoted 10 lines
tools/blktap2/drivers/block-replication.c new file +10 -0
+               goto err;
+       }
+
+       /*
+        * The socket is 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,
+                                           fd, 0,
+                                           td_replication_server_accept,

t);

tools/blktap2/drivers/block-replication.c line 190

Quoted 2 lines
tools/blktap2/drivers/block-replication.c new file +2 -0
+       if (id < 0) {
+               EPRINTF("error registering server connection event

handler: %s",

Quoted 89 lines
tools/blktap2/drivers/block-replication.c new file +89 -0
+                       strerror(id));
+               goto err;
+       }
+       t->listen_fd = fd;
+       t->id = id;
+       t->status = connection_in_progress;
+
+       return 0;
+
+err:
+       close(fd);
+       return rc;
+}
+
+static void td_replication_server_accept(event_id_t id, char mode,
+                                        void *private)
+{
+       td_replication_connect_t *t = private;
+       int fd;
+       const char *log_prefix = t->log_prefix;
+
+       /* XXX: add address-based black/white list */
+       fd = accept(t->listen_fd, NULL, NULL);
+       if (fd < 0) {
+               EPRINTF("error accepting connection: %d\n", errno);
+               return;
+       }
+
+       if (t->status == connection_established) {
+               EPRINTF("connection is already established\n");
+               close(fd);
+               return;
+       }
+
+       DPRINTF("server accepted connection\n");
+       t->fd = fd;
+       t->status = connection_established;
+       t->callback(t, 0);
+}
+
+int td_replication_server_restart(td_replication_connect_t *t)
+{
+       switch (t->status) {
+       case connection_in_progress:
+               return 0;
+       case connection_established:
+               CLOSE_FD(t->fd);
+               t->status = connection_in_progress;
+               return 0;
+       case connection_none:
+       case connection_closed:
+               return td_replication_server_start(t);
+       default:
+               /* not reached */
+               return -1;
+       }
+}
+
+/* client */
+static void td_replication_retry_connect_event(event_id_t id, char mode,
+                                              void *private);
+static void td_replication_connect_event(event_id_t id, char mode,
+                                        void *private);
+int td_replication_client_start(td_replication_connect_t *t)
+{
+       event_id_t id;
+       int fd;
+       int rc;
+       int flags;
+       const char *log_prefix = t->log_prefix;
+
+       if (t->status == connection_in_progress ||
+           t->status == connection_established)
+               return ERROR_INTERNAL;
+
+       DPRINTF("client connecting to %s:%d...\n",
+               inet_ntoa(t->sa.sin_addr), ntohs(t->sa.sin_port));
+
+       if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
+               EPRINTF("could not create client socket: %d\n", errno);
+               return ERROR_INTERNAL;
+       }
+
+       /* make socket nonblocking */
+       if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
+               flags = 0;
+       if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
+               EPRINTF("error setting fd %d to non block mode\n", fd);
+               goto err;

tools/blktap2/drivers/block-replication.c line 296

Quoted 10 lines
tools/blktap2/drivers/block-replication.c new file +10 -0
+       }
+
+       /*
+        * 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
+        */
+       id = tapdisk_server_register_event(SCHEDULER_POLL_TIMEOUT, fd, 0,
+

td_replication_retry_connect_event,

tools/blktap2/drivers/block-replication.c

Quoted 3 lines
tools/blktap2/drivers/block-replication.c new file +3 -0
+                                          t);
+       if(id < 0) {
+               EPRINTF("error registering timeout client connection

event handler: %s\n",

Quoted 5 lines
tools/blktap2/drivers/block-replication.c new file +5 -0
+                       strerror(id));
+               goto err;
+       }
+
+       t->fd = fd;

tools/blktap2/drivers/block-replication.c

Quoted 10 lines
tools/blktap2/drivers/block-replication.c new file +10 -0
+       t->id = id;
+       t->status = connection_in_progress;
+       return 0;
+
+err:
+       close(fd);
+       return ERROR_INTERNAL;
+}
+
+static void td_replication_client_failed(td_replication_connect_t *t,

int rc)

Quoted 13 lines
tools/blktap2/drivers/block-replication.c new file +13 -0
+{
+       td_replication_connect_kill(t);
+       t->callback(t, rc);
+}
+
+static void td_replication_client_done(td_replication_connect_t *t)
+{
+       UNREGISTER_EVENT(t->id);
+       t->status = connection_established;
+       t->callback(t, 0);
+}
+
+static int td_replication_retry_connect(td_replication_connect_t *t)

tools/blktap2/drivers/block-replication.c line 337

Quoted 10 lines
tools/blktap2/drivers/block-replication.c new file +10 -0
+{
+       event_id_t id;
+       const char *log_prefix = t->log_prefix;
+
+       UNREGISTER_EVENT(t->id);
+
+       DPRINTF("connect to server 1 second later");
+       id = tapdisk_server_register_event(SCHEDULER_POLL_TIMEOUT,
+                                          t->fd, t->retry_timeout_s,
+

td_replication_retry_connect_event,

tools/blktap2/drivers/block-replication.c

Quoted 3 lines
tools/blktap2/drivers/block-replication.c new file +3 -0
+                                          t);
+       if (id < 0) {
+               EPRINTF("error registering timeout client connection

event handler: %s\n",

Quoted 8 lines
tools/blktap2/drivers/block-replication.c new file +8 -0
+                       strerror(id));
+               return ERROR_INTERNAL;
+       }
+
+       t->id = id;
+       return 0;
+}
+

tools/blktap2/drivers/block-replication.c

Quoted 10 lines
tools/blktap2/drivers/block-replication.c new file +10 -0
+static int td_replication_wait_connect_done(td_replication_connect_t *t)
+{
+       event_id_t id;
+       const char *log_prefix = t->log_prefix;
+
+       UNREGISTER_EVENT(t->id);
+
+       id = tapdisk_server_register_event(SCHEDULER_POLL_WRITE_FD,
+                                          t->fd, 0,
+                                          td_replication_connect_event,

t);

tools/blktap2/drivers/block-replication.c line 353

Quoted 2 lines
tools/blktap2/drivers/block-replication.c new file +2 -0
+       if (id < 0) {
+               EPRINTF("error registering client connection event

handler: %s\n",

Quoted 28 lines
tools/blktap2/drivers/block-replication.c new file +28 -0
+                       strerror(id));
+               return ERROR_INTERNAL;
+       }
+       t->id = id;
+
+       return 0;
+}
+
+/* return 1 if we need to reconnect to backup server */
+static int check_connect_errno(int err)
+{
+       /*
+        * The fd is non-block, so we will not get ETIMEDOUT
+        * after calling connect(). We only can get this errno
+        * by getsockopt().
+        */
+       if (err == ECONNREFUSED || err == ENETUNREACH ||
+           err == EAGAIN || err == ECONNABORTED ||
+           err == ETIMEDOUT)
+           return 1;
+
+       return 0;
+}
+
+static void td_replication_retry_connect_event(event_id_t id, char mode,
+                                              void *private)
+{
+       td_replication_connect_t *t = private;

tools/blktap2/drivers/block-replication.c

Quoted 10 lines
tools/blktap2/drivers/block-replication.c new file +10 -0
+       int rc, ret;
+       const char *log_prefix = t->log_prefix;
+
+       /* do a non-blocking connect */
+       ret = connect(t->fd, (struct sockaddr *)&t->sa, sizeof(t->sa));
+       if (ret) {
+               if (errno == EINPROGRESS) {
+                       /*
+                        * the connect returned EINPROGRESS (nonblocking
+                        * connect) we must wait for the fd to be

writeable

Quoted 7 lines
tools/blktap2/drivers/block-replication.c new file +7 -0
+                        * to determine if the connect worked
+                        */
+                       rc = td_replication_wait_connect_done(t);
+                       if (rc)
+                               goto fail;
+                       return;
+               }

tools/blktap2/drivers/block-replication.c

Quoted 10 lines
tools/blktap2/drivers/block-replication.c new file +10 -0
+
+               if (check_connect_errno(errno)) {
+                       rc = td_replication_retry_connect(t);
+                       if (rc)
+                               goto fail;
+                       return;
+               }
+
+               /* not recoverable */
+               EPRINTF("error connection to server %s\n",

strerror(errno));

Review

Quoted 61 lines
tools/blktap2/drivers/block-replication.c new file +60 -0
+               rc = ERROR_CONNECTION;
+               goto fail;
+       }
+
+       /* The connection is established unexpectedly */
+       td_replication_client_done(t);
+
+       return;
+
+fail:
+       td_replication_client_failed(t, rc);
+}
+
+/* callback when nonblocking connect() is finished */
+static void td_replication_connect_event(event_id_t id, char mode,
+                                        void *private)
+{
+       int socket_errno;
+       socklen_t socket_errno_size;
+       td_replication_connect_t *t = private;
+       int rc;
+       const char *log_prefix = t->log_prefix;
+
+       /* check to see if the connect succeeded */
+       socket_errno_size = sizeof(socket_errno);
+       if (getsockopt(t->fd, SOL_SOCKET, SO_ERROR,
+                      &socket_errno, &socket_errno_size)) {
+               EPRINTF("error getting socket errno\n");
+               return;
+       }
+
+       DPRINTF("socket connect returned %d\n", 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 = td_replication_retry_connect(t);
+                       if (rc)
+                               goto fail;
+
+                       return;
+               } else {
+                       EPRINTF("socket connect returned %d, giving up\n",
+                               socket_errno);
+                       rc = ERROR_CONNECTION;
+                       goto fail;
+               }
+       }
+
+       td_replication_client_done(t);
+
+       return;
+
+fail:
+       td_replication_client_failed(t, rc);
+}
(quoted diff) +0 -0

b/tools/blktap2/drivers/block-replication.h

tools/blktap2/drivers/block-replication.h line 6

Quoted 11 lines

new file mode 100644
index 0000000..0bd6e71

tools/blktap2/drivers/block-replication.h new file +6 -0
@@ -0,0 +1,113 @@
+/*
+ * 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

Quoted 40 lines
tools/blktap2/drivers/block-replication.h new file +40 -0
+ * 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 BLOCK_REPLICATION_H
+#define BLOCK_REPLICATION_H
+
+#include "scheduler.h"
+#include <sys/socket.h>
+#include <netdb.h>
+
+#define CONTAINER_OF(inner_ptr, outer, member_name)                    \
+       ({                                                              \
+               typeof(outer) *container_of_;                           \
+               container_of_ = (void*)((char*)(inner_ptr) -            \
+                               offsetof(typeof(outer), member_name));  \
+               (void)(&container_of_->member_name ==                   \
+                      (typeof(inner_ptr))0) /* type check */;          \
+               container_of_;                                          \
+       })
+
+#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)

tools/blktap2/drivers/block-replication.h

Quoted 10 lines
tools/blktap2/drivers/block-replication.h new file +10 -0
+
+enum {
+       ERROR_INTERNAL = -1,
+       ERROR_IO = -2,
+       ERROR_CONNECTION = -3,
+       ERROR_CLOSE = -4,
+};
+
+typedef struct td_replication_connect td_replication_connect_t;
+typedef void td_replication_callback(td_replication_connect_t *r, int

rc);

Quoted 16 lines
tools/blktap2/drivers/block-replication.h new file +16 -0
+
+struct td_replication_connect {
+       /*
+        * caller must fill these in before calling
+        * td_replication_connect_init()
+        */
+       const char *log_prefix;
+       td_replication_callback *callback;
+       int retry_timeout_s;
+       int max_connections;
+       /*
+        * The caller uses this fd to read/write after
+        * the connection is established
+        */
+       int fd;
+

tools/blktap2/drivers/block-replication.h

Quoted 10 lines
tools/blktap2/drivers/block-replication.h new file +10 -0
+       /* private */
+       struct sockaddr_in sa;
+       int listen_fd;
+       event_id_t id;
+
+       int status;
+};
+
+/* return -errno if failure happened, otherwise return 0 */
+int td_replication_connect_init(td_replication_connect_t *t, const char

*name);

Quoted 32 lines
tools/blktap2/drivers/block-replication.h new file +31 -1
+/*
+ * Return value:
+ *   -1: connection is closed or not connected
+ *    0: connection is in progress
+ *    1: connection is established
+ */
+int td_replication_connect_status(td_replication_connect_t *t);
+void td_replication_connect_kill(td_replication_connect_t *t);
+
+/*
+ * Return value:
+ *   -2: this caller should be client
+ *   -1: error
+ *    0: connection is in progress
+ */
+int td_replication_server_start(td_replication_connect_t *t);
+/*
+ * Return value:
+ *   -2: this caller should be client
+ *   -1: error
+ *    0: connection is in progress
+ */
+int td_replication_server_restart(td_replication_connect_t *t);
+/*
+ * Return value:
+ *   -1: error
+ *    0: connection is in progress
+ */
+int td_replication_client_start(td_replication_connect_t *t);
+
+#endif
--
Quoted 2 lines

1.9.3

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

#19 lore raw

RFC PATCH v3 16/22 blktap2: move ramdisk related codes to block-replication.c

In reply to Wen Congyang

COLO will reuse them

Cc Shriram Rajagopalan <[email protected]>
---
 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
Acked-by on v3 16/22
#20 lore raw

Re: [RFC Patch v3 16/22] blktap2: move ramdisk related codes to block-replication.c

Quoted 7 lines · Wen Congyang

COLO will reuse them

Cc Shriram Rajagopalan <[email protected]>
---
 tools/blktap2/drivers/block-remus.c       | 485

++----------------------------

Quoted 1 line
 tools/blktap2/drivers/block-replication.c | 452

++++++++++++++++++++++++++++

Review

Quoted 4 lines
 tools/blktap2/drivers/block-replication.h |  48 +++
 3 files changed, 523 insertions(+), 462 deletions(-)
(quoted diff) +0 -0

b/tools/blktap2/drivers/block-remus.c

Quoted 3 lines

index 8b6f157..2713af1 100644

tools/blktap2/drivers/block-remus.c +0 -0

...snip..

tools/blktap2/drivers/block-remus.c static void server_do_wreq(td_driver_t *driver) · line 689

Quoted 8 lines

static int backup_start(td_driver_t *driver)

tools/blktap2/drivers/block-remus.c +1 -1
@@ -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,

Please rename to something else.
Eg, ramdisk flush to disk?

I am fine with the rest of the patch. Have you tested this setup with Remus (not Colo) and ensured that you haven't reintroduced the write after write issue, that was fixed a couple of years ago? Although I don't know how you would test with 4.5 code base.

(subject to addressing the renaming comment)

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

#21 lore raw

Re: [RFC Patch v3 16/22] blktap2: move ramdisk related codes to block-replication.c

Quoted 26 lines · Shriram Rajagopalan
Quoted 7 lines · Wen Congyang

COLO will reuse them

Cc Shriram Rajagopalan <[email protected]>
---
 tools/blktap2/drivers/block-remus.c       | 485

++----------------------------

Quoted 1 line
 tools/blktap2/drivers/block-replication.c | 452

++++++++++++++++++++++++++++

Review

Quoted 4 lines
 tools/blktap2/drivers/block-replication.h |  48 +++
 3 files changed, 523 insertions(+), 462 deletions(-)
(quoted diff) +0 -0

b/tools/blktap2/drivers/block-remus.c

Quoted 3 lines

index 8b6f157..2713af1 100644

tools/blktap2/drivers/block-remus.c +0 -0

...snip..

Quoted 6 lines

static int backup_start(td_driver_t *driver)

tools/blktap2/drivers/block-remus.c +0 -0
@@ -1120,7 +686,9 @@ static void server_do_wreq(td_driver_t *driver)
        if (mread(s->stream_fd.fd, buf, len) < 0)
                goto err;
 
Quoted 5 lines

tools/blktap2/drivers/block-remus.c static void server_do_wreq(td_driver_t *driver) · line 689

Quoted 2 lines
tools/blktap2/drivers/block-remus.c +1 -1
-       if (ramdisk_write(&s->ramdisk, *sector, *sectors, buf) < 0) {
+       if (ramdisk_write_to_hashtable(s->h, *sector, *sectors,

Please rename to something else.
Eg, ramdisk flush to disk?

No, we just write to cache, not flush to disk. We use hashtable to organize the cache. What about ramdisk_write_to_cache()?

Thanks
Wen Congyang

Quoted 16 lines

I am fine with the rest of the patch. Have you tested this setup with Remus (not Colo) and ensured that you haven't reintroduced the write after write issue, that was fixed a couple of years ago? Although I don't know how you would test with 4.5 code base.

(subject to addressing the renaming comment)

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

#22 lore raw

RFC PATCH v3 17/22 block-colo: implement colo disk replication

In reply to Wen Congyang

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

Cc Shriram Rajagopalan <[email protected]>
---
 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 3d8ed8a..21d21b9 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
#23 lore raw

RFC PATCH v3 18/22 support blktap COLO in xl:

In reply to Wen Congyang

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

Cc Shriram Rajagopalan <[email protected]>
---
 tools/libxl/libxl.c         | 3 ++-
 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/libxl_utils.c   | 2 ++
 tools/libxl/libxlu_disk_l.l | 1 +
 7 files changed, 15 insertions(+), 5 deletions(-)
tools/libxl/libxl.c +2 -1
index 39a1879..717df75 100644
@@ -2414,7 +2414,8 @@ static int libxl__device_disk_from_xs_be(libxl__gc *gc,
         }
         if (disk->format != LIBXL_DISK_FORMAT_VHD &&
             disk->format != LIBXL_DISK_FORMAT_RAW &&
-            disk->format != LIBXL_DISK_FORMAT_REMUS) {
+            disk->format != LIBXL_DISK_FORMAT_REMUS &&
+            disk->format != LIBXL_DISK_FORMAT_COLO) {
             LOG(ERROR, "unsupported tapdisk format: %s\n", tmp);
             free(tmp);
             goto cleanup;
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 bf11973..c9ccb02 100644
@@ -727,7 +727,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 a8be7ba..325d41b 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/libxl_utils.c +2 -0
index ad7cf92..6c35ba8 100644
@@ -332,6 +332,8 @@ int libxl_string_to_format(libxl_ctx *ctx, char *s, libxl_disk_format *format)
         *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;
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
#24 lore raw

RFC PATCH v3 19/22 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

---
 docs/man/xl.pod.1                          |   3 +-
 tools/libxl/Makefile                       |   2 +-
 tools/libxl/libxl_colo_save.c              |  36 ++++-
 tools/libxl/libxl_colo_save_disk_blktap2.c | 214 +++++++++++++++++++++++++++++
 tools/libxl/libxl_create.c                 |   7 +
 tools/libxl/libxl_internal.h               |   2 +
 tools/libxl/libxl_noblktap2.c              |  29 ++++
 7 files changed, 289 insertions(+), 4 deletions(-)
 create mode 100644 tools/libxl/libxl_colo_save_disk_blktap2.c
docs/man/xl.pod.1 +2 -1
index 297cd04..d528e7a 100644
@@ -434,7 +434,8 @@ 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.
+     There is no support for network at the moment.
+     Disk replication support is limited to blktap2 disks.
 
 B<OPTIONS>
 
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 +34 -2
index 7b76d3f..7d6f269 100644
@@ -18,10 +18,36 @@
 #include "libxl_internal.h"
 #include "libxl_colo.h"
 
+extern const libxl__checkpoint_device_instance_ops colo_save_device_blktap2_disk;
+
 static const libxl__checkpoint_device_instance_ops *colo_ops[] = {
+    &colo_save_device_blktap2_disk,
     NULL,
 };
 
+/* ================= helper functions ================= */
+static int init_device_subkind(libxl__checkpoint_devices_state *cds)
+{
+    /* init device subkind-specific state in the libxl ctx */
+    int rc;
+    STATE_AO_GC(cds->ao);
+
+    rc = init_subkind_drbd_disk(cds);
+    if (rc) goto out;
+
+    rc = 0;
+out:
+    return rc;
+}
+
+static void cleanup_device_subkind(libxl__checkpoint_devices_state *cds)
+{
+    /* cleanup device subkind-specific state in the libxl ctx */
+    STATE_AO_GC(cds->ao);
+
+    cleanup_subkind_blktap2_disk(cds);
+}
+
 /* ================= colo: setup save environment ================= */
 static void colo_save_setup_done(libxl__egc *egc,
                                  libxl__checkpoint_devices_state *cds,
@@ -48,13 +74,16 @@ void libxl__colo_save_setup(libxl__egc *egc, libxl__colo_save_state *css)
     css->recv_fd = dss->recv_fd;
     css->svm_running = false;
 
-    /* TODO: disk/nic support */
-    cds->device_kind_flags = 0;
+    /* TODO: nic support */
+    cds->device_kind_flags = (1 << LIBXL__DEVICE_KIND_CHECKPOINT_DISK);
     cds->ops = colo_ops;
     cds->callback = colo_save_setup_done;
     cds->ao = ao;
     cds->domid = dss->domid;
 
+    if (init_device_subkind(cds))
+        goto out;
+
     libxl__checkpoint_devices_setup(egc, &css->cds);
 
     return;
@@ -92,6 +121,7 @@ static void colo_save_setup_failed(libxl__egc *egc,
         LOG(ERROR, "COLO: failed to teardown device after setup failed"
             " for guest with domid %u, rc %d", cds->domid, rc);
 
+    cleanup_device_subkind(cds);
     libxl__ao_complete(egc, ao, rc);
 }
 
@@ -122,6 +152,8 @@ static void colo_teardown_done(libxl__egc *egc,
 {
     libxl__colo_save_state *css = CONTAINER_OF(cds, *css, cds);
     libxl__domain_suspend_state *dss = CONTAINER_OF(css, *dss, css);
+
+    cleanup_device_subkind(cds);
     dss->callback(egc, dss, rc);
 }
 
tools/libxl/libxl_colo_save_disk_blktap2.c new file +214 -0

214 lines not on this page · raw message

tools/libxl/libxl_create.c +7 -0
index ec5946d..b3a2f33 100644
@@ -854,6 +854,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_internal.h +2 -0
index 1cc90fb..120b389 100644
@@ -2606,6 +2606,8 @@ int init_subkind_nic(libxl__checkpoint_devices_state *cds);
 void cleanup_subkind_nic(libxl__checkpoint_devices_state *cds);
 int init_subkind_drbd_disk(libxl__checkpoint_devices_state *cds);
 void cleanup_subkind_drbd_disk(libxl__checkpoint_devices_state *cds);
+int init_subkind_blktap2_disk(libxl__checkpoint_devices_state *cds);
+void cleanup_subkind_blktap2_disk(libxl__checkpoint_devices_state *cds);
 
 typedef void libxl__checkpoint_callback(libxl__egc *,
                                         libxl__checkpoint_devices_state *,
tools/libxl/libxl_noblktap2.c +29 -0
index 38696ec..b66ab95 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_instance_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
#25 lore raw

RFC PATCH v3 20/22 setup and control colo-agent for primary vm

In reply to Wen Congyang

This patch adds the machinery required for protecting a primary vm's network device state. This patch comprises of three parts:

1. Hotplug scripts: The colo-agent-setup script is responsible for setting up and tearing down the necessary infrastructure required for COLO agent. This script should be invoked by libxl for each of the guest's network interfaces, when starting or stopping COLO.

COLO agent is a agent that will compare the packets from primary vm and secondary vm, and trigger a new checkpoint if they are different.

Apart from returning success/failure indication via the usual hotplug entries in xenstore, this script also writes to xenstore, the name of the ifb device to be used to control COLO agent.

The script relies on libnl3 command line utilities to perform various setup/teardown functions. The script is confined to Linux platforms only since NetBSD does not seem to have libnl3.

2. Checkpoint network device: Implements the interfaces required by the checkpoint abstract device layer. A note about the implementation:
a) setup() and teardown() are called for each vif attached to the primary vm.
During setup(), the hotplug script is called to setup COLO agent for given vif. The script does the follow things:
i) choose two available IFB devices from system(called IFB_PRIMARY,
and IFB_SECONDARY), and set up the colo qdisc on these two ifb devices.
ii) copy and forward the egress traffic to the FORWARD device iii) redirect vif egress traffic to the IFB_PRIMARY device iv) redirect FORWARD device egress traffic to the IFB_SECONDARY device

During teardown(), the hotplug scripts are called again for each vif. The scripts does the follow things:
i) remove the vif->IFB_PRIMARY traffic redirection
ii) remove the FORWARD->IFB_SECONDARY traffic redirection iii) remove the vif->FORWARD traffic forwarding.
iv) release the two ifb devices and the colo qdisc associated with
them

b) The checkpoint callbacks are not for each vif. So we implement it in libxl_colo_save.c

3. colo-tc: a simple command like tc, and just for the qdisc colo.

---
 tools/hotplug/Linux/Makefile           |   2 +
 tools/hotplug/Linux/colo-agent-setup   | 198 +++++++++++
 tools/hotplug/Linux/remus-netbuf-setup |  45 +--
 tools/hotplug/Linux/xen-network-ft.sh  | 102 ++++++
 tools/libxl/Makefile                   |   8 +-
 tools/libxl/colo-tc.c                  | 589 +++++++++++++++++++++++++++++++++
 tools/libxl/libxl.c                    |   6 +
 tools/libxl/libxl_colo_nic.c           | 288 ++++++++++++++++
 tools/libxl/libxl_colo_save.c          | 177 +++++++++-
 tools/libxl/libxl_internal.h           |   8 +
 tools/libxl/libxl_types.idl            |   1 +
 tools/libxl/xl_cmdimpl.c               |   3 +
 12 files changed, 1376 insertions(+), 51 deletions(-)
 create mode 100755 tools/hotplug/Linux/colo-agent-setup
 create mode 100644 tools/hotplug/Linux/xen-network-ft.sh
 create mode 100644 tools/libxl/colo-tc.c
 create mode 100644 tools/libxl/libxl_colo_nic.c
tools/hotplug/Linux/Makefile +2 -0
index 5317fef..be6e95b 100644
@@ -26,12 +26,14 @@ XEN_SCRIPTS += vscsi
 XEN_SCRIPTS += block-iscsi
 XEN_SCRIPTS += block-drbd-probe
 XEN_SCRIPTS += $(XEN_SCRIPTS-y)
+XEN_SCRIPTS += colo-agent-setup
 
 SUBDIRS-$(CONFIG_SYSTEMD) += systemd
 
 XEN_SCRIPT_DATA = xen-script-common.sh locking.sh logging.sh
 XEN_SCRIPT_DATA += xen-hotplug-common.sh xen-network-common.sh vif-common.sh
 XEN_SCRIPT_DATA += block-common.sh
+XEN_SCRIPT_DATA += xen-network-ft.sh
 
 UDEV_RULES_DIR = $(CONFIG_DIR)/udev
 UDEV_RULES = xen-backend.rules $(UDEV_RULES-y)
tools/hotplug/Linux/colo-agent-setup new file +198 -0

198 lines not on this page · raw message

tools/hotplug/Linux/remus-netbuf-setup +3 -42

63 lines not on this page · raw message

tools/hotplug/Linux/xen-network-ft.sh new file +102 -0

102 lines not on this page · raw message

tools/libxl/Makefile +6 -2
index b4755c8..3ea902d 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_colo_save.o
+LIBXL_OBJS-y += libxl_colo_restore.o libxl_colo_save.o libxl_colo_nic.o
 
 LIBXL_OBJS-$(CONFIG_X86) += libxl_cpuid.o libxl_x86.o
 LIBXL_OBJS-$(CONFIG_ARM) += libxl_nocpuid.o libxl_arm.o
@@ -126,7 +126,7 @@ LIBXLU_OBJS = libxlu_cfg_y.o libxlu_cfg_l.o libxlu_cfg.o \
 	libxlu_disk_l.o libxlu_disk.o libxlu_vif.o libxlu_pci.o
 $(LIBXLU_OBJS): CFLAGS += $(CFLAGS_libxenctrl) # For xentoollog.h
 
-CLIENTS = xl testidl libxl-save-helper
+CLIENTS = xl testidl libxl-save-helper colo-tc
 
 CFLAGS_XL += $(CFLAGS_libxenlight)
 CFLAGS_XL += -Wshadow
@@ -247,6 +247,9 @@ libxl-save-helper: $(SAVE_HELPER_OBJS) libxenlight.so
 testidl: testidl.o libxlutil.so libxenlight.so
 	$(CC) $(LDFLAGS) -o $@ testidl.o libxlutil.so $(LDLIBS_libxenlight) $(LDLIBS_libxenctrl) $(APPEND_LDFLAGS)
 
+colo-tc: colo-tc.o
+	$(CC) $(LDFLAGS) -o $@ colo-tc.o
+
 .PHONY: install
 install: all
 	$(INSTALL_DIR) $(DESTDIR)$(SBINDIR)
@@ -256,6 +259,7 @@ install: all
 	$(INSTALL_DIR) $(DESTDIR)$(PRIVATE_BINDIR)
 	$(INSTALL_PROG) xl $(DESTDIR)$(SBINDIR)
 	$(INSTALL_PROG) libxl-save-helper $(DESTDIR)$(PRIVATE_BINDIR)
+	$(INSTALL_PROG) colo-tc $(DESTDIR)$(PRIVATE_BINDIR)
 	$(INSTALL_SHLIB) libxenlight.so.$(MAJOR).$(MINOR) $(DESTDIR)$(LIBDIR)
 	$(SYMLINK_SHLIB) libxenlight.so.$(MAJOR).$(MINOR) $(DESTDIR)$(LIBDIR)/libxenlight.so.$(MAJOR)
 	$(SYMLINK_SHLIB) libxenlight.so.$(MAJOR) $(DESTDIR)$(LIBDIR)/libxenlight.so
tools/libxl/colo-tc.c new file +589 -0

589 lines not on this page · raw message

tools/libxl/libxl.c +6 -0
index 717df75..feca31f 100644
@@ -3091,6 +3091,11 @@ void libxl__device_nic_add(libxl__egc *egc, uint32_t domid,
         flexarray_append(back, nic->ifname);
     }
 
+    if (nic->forwarddev) {
+        flexarray_append(back, "forwarddev");
+        flexarray_append(back, nic->forwarddev);
+    }
+
     flexarray_append(back, "mac");
     flexarray_append(back,libxl__sprintf(gc,
                                     LIBXL_MAC_FMT, LIBXL_MAC_BYTES(nic->mac)));
@@ -3179,6 +3184,7 @@ static int libxl__device_nic_from_xs_be(libxl__gc *gc,
     nic->ip = READ_BACKEND(NOGC, "ip");
     nic->bridge = READ_BACKEND(NOGC, "bridge");
     nic->script = READ_BACKEND(NOGC, "script");
+    nic->forwarddev = READ_BACKEND(NOGC, "forwarddev");
 
     /* vif_ioemu nics use the same xenstore entries as vif interfaces */
     tmp = READ_BACKEND(gc, "type");
tools/libxl/libxl_colo_nic.c new file +288 -0

288 lines not on this page · raw message

tools/libxl/libxl_colo_save.c +170 -7

250 lines not on this page · raw message

tools/libxl/libxl_internal.h +8 -0
index 120b389..b1a7208 100644
@@ -2608,6 +2608,8 @@ int init_subkind_drbd_disk(libxl__checkpoint_devices_state *cds);
 void cleanup_subkind_drbd_disk(libxl__checkpoint_devices_state *cds);
 int init_subkind_blktap2_disk(libxl__checkpoint_devices_state *cds);
 void cleanup_subkind_blktap2_disk(libxl__checkpoint_devices_state *cds);
+int init_subkind_colo_nic(libxl__checkpoint_devices_state *cds);
+void cleanup_subkind_colo_nic(libxl__checkpoint_devices_state *cds);
 
 typedef void libxl__checkpoint_callback(libxl__egc *,
                                         libxl__checkpoint_devices_state *,
@@ -2724,6 +2726,7 @@ struct libxl__colo_save_state {
     libxl__checkpoint_devices_state cds;
     int send_fd;
     int recv_fd;
+    char *colo_agent_script;
 
     /* private */
     libxl__datacopier_state dc;
@@ -2735,6 +2738,11 @@ struct libxl__colo_save_state {
     uint8_t temp_buff[9];
     void (*callback)(libxl__egc *, libxl__colo_save_state *);
     bool svm_running;
+
+    /* private, used by colo-agent */
+    int fd;
+    int vm_fd;
+    libxl__ev_child child;
 };
 
 /*----- Domain suspend (save) state structure -----*/
tools/libxl/libxl_types.idl +1 -0
index 325d41b..dbefe09 100644
@@ -477,6 +477,7 @@ libxl_device_nic = Struct("device_nic", [
     ("rate_bytes_per_interval", uint64),
     ("rate_interval_usecs", uint32),
     ("gatewaydev", string),
+    ("forwarddev", string)
     ])
 
 libxl_device_pci = Struct("device_pci", [
tools/libxl/xl_cmdimpl.c +3 -0
index 3709dd2..945f604 100644
@@ -1388,6 +1388,9 @@ static void parse_config_data(const char *config_source,
                 } else if (!strcmp(p, "gatewaydev")) {
                     free(nic->gatewaydev);
                     nic->gatewaydev = strdup(p2 + 1);
+                } else if (!strcmp(p, "forwarddev")) {
+                    free(nic->forwarddev);
+                    nic->forwarddev = strdup(p2 + 1);
                 }
             } while ((p = strtok(NULL, ",")) != NULL);
 skip_nic:
Signature · 1 line
1.9.3
#26 lore raw

RFC PATCH v3 21/22 setup and control colo-agent for secondary vm

In reply to Wen Congyang

This patch adds the machinery required for protecting a secondary vm's network device state. This patch implements the interfaces required by the checkpoint abstract device layer. A note about the implementation:
a) setup() and teardown() are called for each vif attached to the secondary vm.
During setup(), the hotplug script is called to setup COLO agent for given vif. The script does the follow things:
i) redirect vif egress traffic to the FORWARD device ii) redirect FORWARD device egress traffic to vif

During teardown(), the hotplug scripts are called again for each vif. The scripts does the follow things:
i) remove the vif->FORWARD traffic redirection
ii) remove the FORWARD->vif traffic redirection

b) Nothing should be done for secondary vm's network device.

---
 tools/libxl/libxl_colo_nic.c     |  21 ++++++
 tools/libxl/libxl_colo_restore.c | 152 +++++++++++++++++++++++++++++++++------
 tools/libxl/libxl_internal.h     |   2 +
 3 files changed, 155 insertions(+), 20 deletions(-)
tools/libxl/libxl_colo_nic.c +21 -0
index 56bccad..9955ef1 100644
@@ -286,3 +286,24 @@ const libxl__checkpoint_device_instance_ops colo_save_device_nic = {
     .setup = colo_nic_save_setup,
     .teardown = colo_nic_save_teardown,
 };
+
+/* ======== secondary ======== */
+static void colo_nic_restore_setup(libxl__checkpoint_device *dev)
+{
+    libxl__colo_restore_state *crs = CONTAINER_OF(dev->cds, *crs, cds);
+
+    colo_nic_setup(dev, secondary, crs->colo_agent_script);
+}
+
+static void colo_nic_restore_teardown(libxl__checkpoint_device *dev)
+{
+    libxl__colo_restore_state *crs = CONTAINER_OF(dev->cds, *crs, cds);
+
+    colo_nic_teardown(dev, secondary, crs->colo_agent_script);
+}
+
+const libxl__checkpoint_device_instance_ops colo_restore_device_nic = {
+    .kind = LIBXL__DEVICE_KIND_CHECKPOINT_NIC,
+    .setup = colo_nic_restore_setup,
+    .teardown = colo_nic_restore_teardown,
+};
tools/libxl/libxl_colo_restore.c +132 -20

224 lines not on this page · raw message

tools/libxl/libxl_internal.h +2 -0
index b1a7208..00dfa1e 100644
@@ -3078,10 +3078,12 @@ struct libxl__colo_restore_state {
     int pae;
     int superpages;
     libxl__colo_callback *callback;
+    char *colo_agent_script;
 
     /* private, colo restore checkpoint state */
     libxl__domain_create_cb *saved_cb;
     void *crcs;
+    libxl__checkpoint_devices_state cds;
 };
 
 struct libxl__domain_create_state {
Signature · 1 line
1.9.3
#27 lore raw

RFC PATCH v3 22/22 colo: cmdline switches and config vars to control colo-agent

In reply to Wen Congyang

Add cmdline switches to 'xl migrate-receive' command to specify a domain-specific hotplug script to setup COLO agent.

Add a new config var 'colo.default.agentscript' to xl.conf, that allows the user to override the default global script used to setup COLO agent.

---
 docs/man/xl.conf.pod.5        |  6 ++++++
 docs/man/xl.pod.1             |  1 -
 tools/libxl/libxl_colo_save.c |  6 ++++++
 tools/libxl/libxl_create.c    | 17 +++++++++++++---
 tools/libxl/libxl_internal.h  |  1 +
 tools/libxl/libxl_types.idl   |  1 +
 tools/libxl/xl.c              |  3 +++
 tools/libxl/xl.h              |  1 +
 tools/libxl/xl_cmdimpl.c      | 47 ++++++++++++++++++++++++++++++++-----------
 9 files changed, 67 insertions(+), 16 deletions(-)
docs/man/xl.conf.pod.5 +6 -0
index 8ae19bb..c65213d 100644
@@ -111,6 +111,12 @@ Configures the default script used by Remus to setup network buffering.
 
 Default: C</etc/xen/scripts/remus-netbuf-setup>
 
+=item B<colo.default.agentscript="PATH">
+
+Configures the default script used by COLO to setup colo-agent.
+
+Default: C</etc/xen/scripts/colo-agent-setup>
+
 =item B<output_format="json|sxp">
 
 Configures the default output format used by xl when printing "machine
docs/man/xl.pod.1 +0 -1
index d528e7a..b22bc23 100644
@@ -434,7 +434,6 @@ 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 at the moment.
      Disk replication support is limited to blktap2 disks.
 
 B<OPTIONS>
tools/libxl/libxl_colo_save.c +6 -0
index d9f151e..98e4f25 100644
@@ -231,6 +231,12 @@ void libxl__colo_save_setup(libxl__egc *egc, libxl__colo_save_state *css)
     css->vm_fd = -1;
     libxl__ev_child_init(&css->child);
 
+    if (dss->remus->netbufscript)
+        css->colo_agent_script = libxl__strdup(gc, dss->remus->netbufscript);
+    else
+        css->colo_agent_script = GCSPRINTF("%s/colo-agent-setup",
+                                           libxl__xen_script_dir_path());
+
     cds->device_kind_flags = (1 << LIBXL__DEVICE_KIND_CHECKPOINT_DISK) |
                              (1 << LIBXL__DEVICE_KIND_CHECKPOINT_NIC);
     cds->ops = colo_ops;
tools/libxl/libxl_create.c +14 -3
index b3a2f33..e14046f 100644
@@ -1104,6 +1104,11 @@ static void domcreate_bootloader_done(libxl__egc *egc,
         crs->superpages = superpages;
         crs->pae = pae;
         crs->callback = libxl__colo_restore_setup_done;
+        if (dcs->colo_agent_script)
+            crs->colo_agent_script = libxl__strdup(gc, dcs->colo_agent_script);
+        else
+            crs->colo_agent_script = GCSPRINTF("%s/colo-agent-setup",
+                                               libxl__xen_script_dir_path());
         libxl__colo_restore_setup(egc, crs);
     } else
         libxl__xc_domain_restore(egc, dcs,
@@ -1554,6 +1559,7 @@ static void domain_create_cb(libxl__egc *egc,
 static int do_domain_create(libxl_ctx *ctx, libxl_domain_config *d_config,
                             uint32_t *domid, int restore_fd,
                             int send_fd, int checkpointed_stream,
+                            const char *colo_agent_script,
                             const libxl_asyncop_how *ao_how,
                             const libxl_asyncprogress_how *aop_console_how)
 {
@@ -1567,6 +1573,7 @@ static int do_domain_create(libxl_ctx *ctx, libxl_domain_config *d_config,
     cdcs->dcs.send_fd = send_fd;
     cdcs->dcs.callback = domain_create_cb;
     cdcs->dcs.checkpointed_stream = checkpointed_stream;
+    cdcs->dcs.colo_agent_script = colo_agent_script;
     libxl__ao_progress_gethow(&cdcs->dcs.aop_console_how, aop_console_how);
     cdcs->domid_out = domid;
 
@@ -1593,7 +1600,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, -1, 0,
+    return do_domain_create(ctx, d_config, domid, -1, -1, 0, NULL,
                             ao_how, aop_console_how);
 }
 
@@ -1604,12 +1611,16 @@ int libxl_domain_create_restore(libxl_ctx *ctx, libxl_domain_config *d_config,
                                 const libxl_asyncprogress_how *aop_console_how)
 {
     int send_fd = -1;
+    char *colo_agent_script = NULL;
 
-    if (params->checkpointed_stream == LIBXL_CHECKPOINTED_STREAM_COLO)
+    if (params->checkpointed_stream == LIBXL_CHECKPOINTED_STREAM_COLO) {
         send_fd = params->send_fd;
+        colo_agent_script = params->colo_agent_script;
+    }
 
     return do_domain_create(ctx, d_config, domid, restore_fd, send_fd,
-                            params->checkpointed_stream, ao_how, aop_console_how);
+                            params->checkpointed_stream, colo_agent_script,
+                            ao_how, aop_console_how);
 }
 
 /*
tools/libxl/libxl_internal.h +1 -0
index 00dfa1e..3851b07 100644
@@ -3097,6 +3097,7 @@ struct libxl__domain_create_state {
     /* private to domain_create */
     int guest_domid;
     int checkpointed_stream;
+    const char *colo_agent_script;
     libxl__domain_build_state build_state;
     libxl__colo_restore_state crs;
     libxl__bootloader_state bl;
tools/libxl/libxl_types.idl +1 -0
index dbefe09..346c471 100644
@@ -316,6 +316,7 @@ libxl_domain_create_info = Struct("domain_create_info",[
 libxl_domain_restore_params = Struct("domain_restore_params", [
     ("checkpointed_stream", integer),
     ("send_fd", integer),
+    ("colo_agent_script", string),
     ])
 
 libxl_domain_sched_params = Struct("domain_sched_params",[
tools/libxl/xl.c +3 -0
index f014306..3d746f5 100644
@@ -45,6 +45,7 @@ char *default_bridge = NULL;
 char *default_gatewaydev = NULL;
 char *default_vifbackend = NULL;
 char *default_remus_netbufscript = NULL;
+char *default_colo_agent_script = NULL;
 enum output_format default_output_format = OUTPUT_FORMAT_JSON;
 int claim_mode = 1;
 bool progress_use_cr = 0;
@@ -179,6 +180,8 @@ static void parse_global_config(const char *configfile,
 
     xlu_cfg_replace_string (config, "remus.default.netbufscript",
         &default_remus_netbufscript, 0);
+    xlu_cfg_replace_string (config, "colo.default.agentscript",
+        &default_colo_agent_script, 0);
 
     xlu_cfg_destroy(config);
 }
tools/libxl/xl.h +1 -0
index 087eb8c..e273640 100644
@@ -171,6 +171,7 @@ extern char *default_bridge;
 extern char *default_gatewaydev;
 extern char *default_vifbackend;
 extern char *default_remus_netbufscript;
+extern char *default_colo_agent_script;
 extern char *blkdev_start;
 
 enum output_format {
tools/libxl/xl_cmdimpl.c +35 -12

106 lines not on this page · raw message

Signature · 1 line
1.9.3
#28 lore raw

[RFC Patch v3 23/22] 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.

CC Stefano Stabellini <[email protected]>
---
 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
#29 lore raw

Re: [RFC Patch v3 23/22] Introduce "xen-load-devices-state"

Quoted 3 lines · 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.

Hello Wen,
please CC qemu-devel too for QEMU patches.

Could you please explain why do you need this command?

Is the main issue that there are no QMP commands today to load the state of a VM? It looks like that for vm restore we are using the -incoming command line option, but there is no alternative over QMP.

[...]

Quoted 96 lines
savevm.c +88 -0
index 22123be..c6aa502 100644
@@ -863,6 +863,105 @@ out:
     return ret;
 }
 
+static int qemu_load_devices_state(QEMUFile *f)
+{
+    uint8_t section_type;
+    unsigned int v;
+    int ret;
+
+    if (qemu_savevm_state_blocked(NULL)) {
+        return -EINVAL;
+    }
+
+    v = qemu_get_be32(f);
+    if (v != QEMU_VM_FILE_MAGIC) {
+        return -EINVAL;
+    }
+
+    v = qemu_get_be32(f);
+    if (v == QEMU_VM_FILE_VERSION_COMPAT) {
+        fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
+        return -ENOTSUP;
+    }
+    if (v != QEMU_VM_FILE_VERSION) {
+        return -ENOTSUP;
+    }
+
+    while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
+        uint32_t instance_id, version_id, section_id;
+        SaveStateEntry *se;
+        char idstr[257];
+        int len;
+
+        switch (section_type) {
+        case QEMU_VM_SECTION_FULL:
+            /* Read section start */
+            section_id = qemu_get_be32(f);
+            len = qemu_get_byte(f);
+            qemu_get_buffer(f, (uint8_t *)idstr, len);
+            idstr[len] = 0;
+            instance_id = qemu_get_be32(f);
+            version_id = qemu_get_be32(f);
+
+            /* Find savevm section */
+            se = find_se(idstr, instance_id);
+            if (se == NULL) {
+                fprintf(stderr, "Unknown savevm section or instance '%s' %d\n",
+                        idstr, instance_id);
+                ret = -EINVAL;
+                goto out;
+            }
+
+            /* Validate version */
+            if (version_id > se->version_id) {
+                fprintf(stderr, "loadvm: unsupported version %d for '%s' v%d\n",
+                        version_id, idstr, se->version_id);
+                ret = -EINVAL;
+                goto out;
+            }
+
+            /* Validate if it is a device's state */
+            if (se->is_ram) {
+                fprintf(stderr, "loadvm: %s is not devices state\n", idstr);
+                ret = -EINVAL;
+                goto out;
+            }
+
+            ret = vmstate_load(f, se, version_id);
+            if (ret < 0) {
+                fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
+                        instance_id, idstr);
+                goto out;
+            }
+            break;
+        case QEMU_VM_SECTION_START:
+        case QEMU_VM_SECTION_PART:
+        case QEMU_VM_SECTION_END:
+            /*
+             * The file is saved by the command xen-save-devices-state,
+             * So it should not contain section start/part/end.
+             */
+        default:
+            fprintf(stderr, "Unknown savevm section type %d\n", section_type);
+            ret = -EINVAL;
+            goto out;
+        }
+    }
+
+    cpu_synchronize_all_post_init();
+
+    ret = 0;

savevm.c out: · line 963

Quoted 10 lines
savevm.c +10 -0
+
+out:
+    if (ret == 0) {
+        if (qemu_file_get_error(f)) {
+            ret = -EIO;
+        }
+    }
+
+    return ret;
+}

Assuming that the state file only contains device states, why don't you just call qemu_loadvm_state to implement the command?

Quoted 46 lines
savevm.c +27 -0
 static BlockDriverState *find_vmstate_bs(void)
 {
     BlockDriverState *bs = NULL;
@@ -1027,6 +1126,33 @@ void qmp_xen_save_devices_state(const char *filename, Error **errp)
     }
 }
 
+void qmp_xen_load_devices_state(const char *filename, Error **errp)
+{
+    QEMUFile *f;
+    int saved_vm_running;
+    int ret;
+
+    saved_vm_running = runstate_is_running();
+    vm_stop(RUN_STATE_RESTORE_VM);
+
+    f = qemu_fopen(filename, "rb");
+    if (!f) {
+        error_setg_file_open(errp, errno, filename);
+        goto out;
+    }
+
+    ret = qemu_load_devices_state(f);
+    qemu_fclose(f);
+    if (ret < 0) {
+        error_set(errp, QERR_IO_ERROR);
+    }
+
+out:
+    if (saved_vm_running) {
+        vm_start();
+    }
+}
+
 int load_vmstate(const char *name)
 {
     BlockDriverState *bs, *bs_vm_state;
Signature · 7 lines
1.9.0


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

Re: [Qemu-devel] [Xen-devel] [RFC Patch v3 23/22] Introduce "xen-load-devices-state"

In reply to Wen Congyang
Quoted 3 lines · 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.

Hello Wen,
please CC qemu-devel too for QEMU patches.

Could you please explain why do you need this command?

Is the main issue that there are no QMP commands today to load the state of a VM? It looks like that for vm restore we are using the -incoming command line option, but there is no alternative over QMP.

[...]

Quoted 96 lines
savevm.c +88 -0
index 22123be..c6aa502 100644
@@ -863,6 +863,105 @@ out:
     return ret;
 }
 
+static int qemu_load_devices_state(QEMUFile *f)
+{
+    uint8_t section_type;
+    unsigned int v;
+    int ret;
+
+    if (qemu_savevm_state_blocked(NULL)) {
+        return -EINVAL;
+    }
+
+    v = qemu_get_be32(f);
+    if (v != QEMU_VM_FILE_MAGIC) {
+        return -EINVAL;
+    }
+
+    v = qemu_get_be32(f);
+    if (v == QEMU_VM_FILE_VERSION_COMPAT) {
+        fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
+        return -ENOTSUP;
+    }
+    if (v != QEMU_VM_FILE_VERSION) {
+        return -ENOTSUP;
+    }
+
+    while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
+        uint32_t instance_id, version_id, section_id;
+        SaveStateEntry *se;
+        char idstr[257];
+        int len;
+
+        switch (section_type) {
+        case QEMU_VM_SECTION_FULL:
+            /* Read section start */
+            section_id = qemu_get_be32(f);
+            len = qemu_get_byte(f);
+            qemu_get_buffer(f, (uint8_t *)idstr, len);
+            idstr[len] = 0;
+            instance_id = qemu_get_be32(f);
+            version_id = qemu_get_be32(f);
+
+            /* Find savevm section */
+            se = find_se(idstr, instance_id);
+            if (se == NULL) {
+                fprintf(stderr, "Unknown savevm section or instance '%s' %d\n",
+                        idstr, instance_id);
+                ret = -EINVAL;
+                goto out;
+            }
+
+            /* Validate version */
+            if (version_id > se->version_id) {
+                fprintf(stderr, "loadvm: unsupported version %d for '%s' v%d\n",
+                        version_id, idstr, se->version_id);
+                ret = -EINVAL;
+                goto out;
+            }
+
+            /* Validate if it is a device's state */
+            if (se->is_ram) {
+                fprintf(stderr, "loadvm: %s is not devices state\n", idstr);
+                ret = -EINVAL;
+                goto out;
+            }
+
+            ret = vmstate_load(f, se, version_id);
+            if (ret < 0) {
+                fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
+                        instance_id, idstr);
+                goto out;
+            }
+            break;
+        case QEMU_VM_SECTION_START:
+        case QEMU_VM_SECTION_PART:
+        case QEMU_VM_SECTION_END:
+            /*
+             * The file is saved by the command xen-save-devices-state,
+             * So it should not contain section start/part/end.
+             */
+        default:
+            fprintf(stderr, "Unknown savevm section type %d\n", section_type);
+            ret = -EINVAL;
+            goto out;
+        }
+    }
+
+    cpu_synchronize_all_post_init();
+
+    ret = 0;

savevm.c out: · line 963

Quoted 10 lines
savevm.c +10 -0
+
+out:
+    if (ret == 0) {
+        if (qemu_file_get_error(f)) {
+            ret = -EIO;
+        }
+    }
+
+    return ret;
+}

Assuming that the state file only contains device states, why don't you just call qemu_loadvm_state to implement the command?

Quoted 46 lines
savevm.c +27 -0
 static BlockDriverState *find_vmstate_bs(void)
 {
     BlockDriverState *bs = NULL;
@@ -1027,6 +1126,33 @@ void qmp_xen_save_devices_state(const char *filename, Error **errp)
     }
 }
 
+void qmp_xen_load_devices_state(const char *filename, Error **errp)
+{
+    QEMUFile *f;
+    int saved_vm_running;
+    int ret;
+
+    saved_vm_running = runstate_is_running();
+    vm_stop(RUN_STATE_RESTORE_VM);
+
+    f = qemu_fopen(filename, "rb");
+    if (!f) {
+        error_setg_file_open(errp, errno, filename);
+        goto out;
+    }
+
+    ret = qemu_load_devices_state(f);
+    qemu_fclose(f);
+    if (ret < 0) {
+        error_set(errp, QERR_IO_ERROR);
+    }
+
+out:
+    if (saved_vm_running) {
+        vm_start();
+    }
+}
+
 int load_vmstate(const char *name)
 {
     BlockDriverState *bs, *bs_vm_state;
Signature · 7 lines
1.9.0


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

Re: [RFC Patch v3 23/22] Introduce "xen-load-devices-state"

At 09/06/2014 05:57 AM, Stefano Stabellini Write:

Quoted 5 lines · Wen Congyang
Quoted 3 lines

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.

Quoted 8 lines

Hello Wen,
please CC qemu-devel too for QEMU patches.

Could you please explain why do you need this command?

Is the main issue that there are no QMP commands today to load the state of a VM? It looks like that for vm restore we are using the -incoming command line option, but there is no alternative over QMP.

We only have hmp commands savevm/loadvm, and qmp commands xen-save-devices-state.

We use this new command for COLO:
1. suspend both primay vm and secondary vm
2. sync the state
3. resume both primary vm and secondary vm

In such case, we need to update all devices's state in any time.

Quoted 105 lines

[...]

Quoted 101 lines
savevm.c +93 -0
index 22123be..c6aa502 100644
@@ -863,6 +863,105 @@ out:
     return ret;
 }
 
+static int qemu_load_devices_state(QEMUFile *f)
+{
+    uint8_t section_type;
+    unsigned int v;
+    int ret;
+
+    if (qemu_savevm_state_blocked(NULL)) {
+        return -EINVAL;
+    }
+
+    v = qemu_get_be32(f);
+    if (v != QEMU_VM_FILE_MAGIC) {
+        return -EINVAL;
+    }
+
+    v = qemu_get_be32(f);
+    if (v == QEMU_VM_FILE_VERSION_COMPAT) {
+        fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
+        return -ENOTSUP;
+    }
+    if (v != QEMU_VM_FILE_VERSION) {
+        return -ENOTSUP;
+    }
+
+    while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
+        uint32_t instance_id, version_id, section_id;
+        SaveStateEntry *se;
+        char idstr[257];
+        int len;
+
+        switch (section_type) {
+        case QEMU_VM_SECTION_FULL:
+            /* Read section start */
+            section_id = qemu_get_be32(f);
+            len = qemu_get_byte(f);
+            qemu_get_buffer(f, (uint8_t *)idstr, len);
+            idstr[len] = 0;
+            instance_id = qemu_get_be32(f);
+            version_id = qemu_get_be32(f);
+
+            /* Find savevm section */
+            se = find_se(idstr, instance_id);
+            if (se == NULL) {
+                fprintf(stderr, "Unknown savevm section or instance '%s' %d\n",
+                        idstr, instance_id);
+                ret = -EINVAL;
+                goto out;
+            }
+
+            /* Validate version */
+            if (version_id > se->version_id) {
+                fprintf(stderr, "loadvm: unsupported version %d for '%s' v%d\n",
+                        version_id, idstr, se->version_id);
+                ret = -EINVAL;
+                goto out;
+            }
+
+            /* Validate if it is a device's state */
+            if (se->is_ram) {
+                fprintf(stderr, "loadvm: %s is not devices state\n", idstr);
+                ret = -EINVAL;
+                goto out;
+            }
+
+            ret = vmstate_load(f, se, version_id);
+            if (ret < 0) {
+                fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
+                        instance_id, idstr);
+                goto out;
+            }
+            break;
+        case QEMU_VM_SECTION_START:
+        case QEMU_VM_SECTION_PART:
+        case QEMU_VM_SECTION_END:
+            /*
+             * The file is saved by the command xen-save-devices-state,
+             * So it should not contain section start/part/end.
+             */
+        default:
+            fprintf(stderr, "Unknown savevm section type %d\n", section_type);
+            ret = -EINVAL;
+            goto out;
+        }
+    }
+
+    cpu_synchronize_all_post_init();
+
+    ret = 0;
+
+out:
+    if (ret == 0) {
+        if (qemu_file_get_error(f)) {
+            ret = -EIO;
Quoted 8 lines

savevm.c out: · line 963

Quoted 5 lines
savevm.c +5 -0
+        }
+    }
+
+    return ret;
+}

Assuming that the state file only contains device states, why don't you just call qemu_loadvm_state to implement the command?

Do you mean there is no need to check the file? If the file contains some memory, it will cause unexpected problem.

Thanks
Wen Congyang

Quoted 51 lines
Quoted 49 lines
savevm.c +27 -0
 static BlockDriverState *find_vmstate_bs(void)
 {
     BlockDriverState *bs = NULL;
@@ -1027,6 +1126,33 @@ void qmp_xen_save_devices_state(const char *filename, Error **errp)
     }
 }
 
+void qmp_xen_load_devices_state(const char *filename, Error **errp)
+{
+    QEMUFile *f;
+    int saved_vm_running;
+    int ret;
+
+    saved_vm_running = runstate_is_running();
+    vm_stop(RUN_STATE_RESTORE_VM);
+
+    f = qemu_fopen(filename, "rb");
+    if (!f) {
+        error_setg_file_open(errp, errno, filename);
+        goto out;
+    }
+
+    ret = qemu_load_devices_state(f);
+    qemu_fclose(f);
+    if (ret < 0) {
+        error_set(errp, QERR_IO_ERROR);
+    }
+
+out:
+    if (saved_vm_running) {
+        vm_start();
+    }
+}
+
 int load_vmstate(const char *name)
 {
     BlockDriverState *bs, *bs_vm_state;
Signature · 7 lines
1.9.0


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

.

#32 lore raw

Re: [Qemu-devel] [Xen-devel] [RFC Patch v3 23/22] Introduce "xen-load-devices-state"

In reply to Stefano Stabellini

At 09/06/2014 05:57 AM, Stefano Stabellini Write:

Quoted 5 lines · Wen Congyang
Quoted 3 lines

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.

Quoted 8 lines

Hello Wen,
please CC qemu-devel too for QEMU patches.

Could you please explain why do you need this command?

Is the main issue that there are no QMP commands today to load the state of a VM? It looks like that for vm restore we are using the -incoming command line option, but there is no alternative over QMP.

We only have hmp commands savevm/loadvm, and qmp commands xen-save-devices-state.

We use this new command for COLO:
1. suspend both primay vm and secondary vm
2. sync the state
3. resume both primary vm and secondary vm

In such case, we need to update all devices's state in any time.

Quoted 105 lines

[...]

Quoted 101 lines
savevm.c +93 -0
index 22123be..c6aa502 100644
@@ -863,6 +863,105 @@ out:
     return ret;
 }
 
+static int qemu_load_devices_state(QEMUFile *f)
+{
+    uint8_t section_type;
+    unsigned int v;
+    int ret;
+
+    if (qemu_savevm_state_blocked(NULL)) {
+        return -EINVAL;
+    }
+
+    v = qemu_get_be32(f);
+    if (v != QEMU_VM_FILE_MAGIC) {
+        return -EINVAL;
+    }
+
+    v = qemu_get_be32(f);
+    if (v == QEMU_VM_FILE_VERSION_COMPAT) {
+        fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
+        return -ENOTSUP;
+    }
+    if (v != QEMU_VM_FILE_VERSION) {
+        return -ENOTSUP;
+    }
+
+    while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
+        uint32_t instance_id, version_id, section_id;
+        SaveStateEntry *se;
+        char idstr[257];
+        int len;
+
+        switch (section_type) {
+        case QEMU_VM_SECTION_FULL:
+            /* Read section start */
+            section_id = qemu_get_be32(f);
+            len = qemu_get_byte(f);
+            qemu_get_buffer(f, (uint8_t *)idstr, len);
+            idstr[len] = 0;
+            instance_id = qemu_get_be32(f);
+            version_id = qemu_get_be32(f);
+
+            /* Find savevm section */
+            se = find_se(idstr, instance_id);
+            if (se == NULL) {
+                fprintf(stderr, "Unknown savevm section or instance '%s' %d\n",
+                        idstr, instance_id);
+                ret = -EINVAL;
+                goto out;
+            }
+
+            /* Validate version */
+            if (version_id > se->version_id) {
+                fprintf(stderr, "loadvm: unsupported version %d for '%s' v%d\n",
+                        version_id, idstr, se->version_id);
+                ret = -EINVAL;
+                goto out;
+            }
+
+            /* Validate if it is a device's state */
+            if (se->is_ram) {
+                fprintf(stderr, "loadvm: %s is not devices state\n", idstr);
+                ret = -EINVAL;
+                goto out;
+            }
+
+            ret = vmstate_load(f, se, version_id);
+            if (ret < 0) {
+                fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
+                        instance_id, idstr);
+                goto out;
+            }
+            break;
+        case QEMU_VM_SECTION_START:
+        case QEMU_VM_SECTION_PART:
+        case QEMU_VM_SECTION_END:
+            /*
+             * The file is saved by the command xen-save-devices-state,
+             * So it should not contain section start/part/end.
+             */
+        default:
+            fprintf(stderr, "Unknown savevm section type %d\n", section_type);
+            ret = -EINVAL;
+            goto out;
+        }
+    }
+
+    cpu_synchronize_all_post_init();
+
+    ret = 0;
+
+out:
+    if (ret == 0) {
+        if (qemu_file_get_error(f)) {
+            ret = -EIO;
Quoted 8 lines

savevm.c out: · line 963

Quoted 5 lines
savevm.c +5 -0
+        }
+    }
+
+    return ret;
+}

Assuming that the state file only contains device states, why don't you just call qemu_loadvm_state to implement the command?

Do you mean there is no need to check the file? If the file contains some memory, it will cause unexpected problem.

Thanks
Wen Congyang

Quoted 51 lines
Quoted 49 lines
savevm.c +27 -0
 static BlockDriverState *find_vmstate_bs(void)
 {
     BlockDriverState *bs = NULL;
@@ -1027,6 +1126,33 @@ void qmp_xen_save_devices_state(const char *filename, Error **errp)
     }
 }
 
+void qmp_xen_load_devices_state(const char *filename, Error **errp)
+{
+    QEMUFile *f;
+    int saved_vm_running;
+    int ret;
+
+    saved_vm_running = runstate_is_running();
+    vm_stop(RUN_STATE_RESTORE_VM);
+
+    f = qemu_fopen(filename, "rb");
+    if (!f) {
+        error_setg_file_open(errp, errno, filename);
+        goto out;
+    }
+
+    ret = qemu_load_devices_state(f);
+    qemu_fclose(f);
+    if (ret < 0) {
+        error_set(errp, QERR_IO_ERROR);
+    }
+
+out:
+    if (saved_vm_running) {
+        vm_start();
+    }
+}
+
 int load_vmstate(const char *name)
 {
     BlockDriverState *bs, *bs_vm_state;
Signature · 7 lines
1.9.0


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

.

#33 lore raw

Re: [RFC Patch v3 23/22] Introduce "xen-load-devices-state"

Quoted 15 lines · Wen Congyang

At 09/06/2014 05:57 AM, Stefano Stabellini Write:

Quoted 5 lines · Wen Congyang
Quoted 3 lines

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.

Quoted 8 lines

Hello Wen,
please CC qemu-devel too for QEMU patches.

Could you please explain why do you need this command?

Is the main issue that there are no QMP commands today to load the state of a VM? It looks like that for vm restore we are using the -incoming command line option, but there is no alternative over QMP.

Quoted 8 lines

We only have hmp commands savevm/loadvm, and qmp commands xen-save-devices-state.

We use this new command for COLO:
1. suspend both primay vm and secondary vm
2. sync the state
3. resume both primary vm and secondary vm

In such case, we need to update all devices's state in any time.

OK. Please state this in the commit message.

Quoted 113 lines
Quoted 104 lines

[...]

Quoted 101 lines
savevm.c +93 -0
index 22123be..c6aa502 100644
@@ -863,6 +863,105 @@ out:
     return ret;
 }
 
+static int qemu_load_devices_state(QEMUFile *f)
+{
+    uint8_t section_type;
+    unsigned int v;
+    int ret;
+
+    if (qemu_savevm_state_blocked(NULL)) {
+        return -EINVAL;
+    }
+
+    v = qemu_get_be32(f);
+    if (v != QEMU_VM_FILE_MAGIC) {
+        return -EINVAL;
+    }
+
+    v = qemu_get_be32(f);
+    if (v == QEMU_VM_FILE_VERSION_COMPAT) {
+        fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
+        return -ENOTSUP;
+    }
+    if (v != QEMU_VM_FILE_VERSION) {
+        return -ENOTSUP;
+    }
+
+    while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
+        uint32_t instance_id, version_id, section_id;
+        SaveStateEntry *se;
+        char idstr[257];
+        int len;
+
+        switch (section_type) {
+        case QEMU_VM_SECTION_FULL:
+            /* Read section start */
+            section_id = qemu_get_be32(f);
+            len = qemu_get_byte(f);
+            qemu_get_buffer(f, (uint8_t *)idstr, len);
+            idstr[len] = 0;
+            instance_id = qemu_get_be32(f);
+            version_id = qemu_get_be32(f);
+
+            /* Find savevm section */
+            se = find_se(idstr, instance_id);
+            if (se == NULL) {
+                fprintf(stderr, "Unknown savevm section or instance '%s' %d\n",
+                        idstr, instance_id);
+                ret = -EINVAL;
+                goto out;
+            }
+
+            /* Validate version */
+            if (version_id > se->version_id) {
+                fprintf(stderr, "loadvm: unsupported version %d for '%s' v%d\n",
+                        version_id, idstr, se->version_id);
+                ret = -EINVAL;
+                goto out;
+            }
+
+            /* Validate if it is a device's state */
+            if (se->is_ram) {
+                fprintf(stderr, "loadvm: %s is not devices state\n", idstr);
+                ret = -EINVAL;
+                goto out;
+            }
+
+            ret = vmstate_load(f, se, version_id);
+            if (ret < 0) {
+                fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
+                        instance_id, idstr);
+                goto out;
+            }
+            break;
+        case QEMU_VM_SECTION_START:
+        case QEMU_VM_SECTION_PART:
+        case QEMU_VM_SECTION_END:
+            /*
+             * The file is saved by the command xen-save-devices-state,
+             * So it should not contain section start/part/end.
+             */
+        default:
+            fprintf(stderr, "Unknown savevm section type %d\n", section_type);
+            ret = -EINVAL;
+            goto out;
+        }
+    }
+
+    cpu_synchronize_all_post_init();
+
+    ret = 0;
+
+out:
+    if (ret == 0) {
+        if (qemu_file_get_error(f)) {
+            ret = -EIO;
Quoted 8 lines

savevm.c out: · line 963

Quoted 5 lines
savevm.c +5 -0
+        }
+    }
+
+    return ret;
+}

Assuming that the state file only contains device states, why don't you just call qemu_loadvm_state to implement the command?

Quoted 2 lines

Do you mean there is no need to check the file? If the file contains some memory, it will cause unexpected problem.

I would prefer to avoid duplicating qemu_loadvm_state just to add an if (se->is_ram) check.
I would rather introduce a generic loadvm QMP command and rely on the fact that we are saving only device states via xen-save-devices-state.

Given that loading memory in QEMU on Xen always leads to errors, maybe we could still add a check in qemu_loadvm_state anyway. Something like:

savevm.c +7 -0
index e19ae0a..759eefa 100644
@@ -938,6 +938,13 @@ int qemu_loadvm_state(QEMUFile *f)
                 goto out;
             }
 
+            /* Validate if it is a device's state */
+            if (xen_enabled() && se->is_ram) {
+                fprintf(stderr, "loadvm: %s RAM loading not allowed on Xen\n", idstr);
+                ret = -EINVAL;
+                goto out;
+            }
+
             /* Add entry */
             le = g_malloc0(sizeof(*le));
 
Quoted 55 lines

Thanks
Wen Congyang

Quoted 51 lines
Quoted 49 lines
savevm.c +27 -0
 static BlockDriverState *find_vmstate_bs(void)
 {
     BlockDriverState *bs = NULL;
@@ -1027,6 +1126,33 @@ void qmp_xen_save_devices_state(const char *filename, Error **errp)
     }
 }
 
+void qmp_xen_load_devices_state(const char *filename, Error **errp)
+{
+    QEMUFile *f;
+    int saved_vm_running;
+    int ret;
+
+    saved_vm_running = runstate_is_running();
+    vm_stop(RUN_STATE_RESTORE_VM);
+
+    f = qemu_fopen(filename, "rb");
+    if (!f) {
+        error_setg_file_open(errp, errno, filename);
+        goto out;
+    }
+
+    ret = qemu_load_devices_state(f);
+    qemu_fclose(f);
+    if (ret < 0) {
+        error_set(errp, QERR_IO_ERROR);
+    }
+
+out:
+    if (saved_vm_running) {
+        vm_start();
+    }
+}
+
 int load_vmstate(const char *name)
 {
     BlockDriverState *bs, *bs_vm_state;
Signature · 7 lines
1.9.0


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

.

#34 lore raw

Re: [Qemu-devel] [Xen-devel] [RFC Patch v3 23/22] Introduce "xen-load-devices-state"

In reply to Wen Congyang
Quoted 15 lines · Wen Congyang

At 09/06/2014 05:57 AM, Stefano Stabellini Write:

Quoted 5 lines · Wen Congyang
Quoted 3 lines

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.

Quoted 8 lines

Hello Wen,
please CC qemu-devel too for QEMU patches.

Could you please explain why do you need this command?

Is the main issue that there are no QMP commands today to load the state of a VM? It looks like that for vm restore we are using the -incoming command line option, but there is no alternative over QMP.

Quoted 8 lines

We only have hmp commands savevm/loadvm, and qmp commands xen-save-devices-state.

We use this new command for COLO:
1. suspend both primay vm and secondary vm
2. sync the state
3. resume both primary vm and secondary vm

In such case, we need to update all devices's state in any time.

OK. Please state this in the commit message.

Quoted 113 lines
Quoted 104 lines

[...]

Quoted 101 lines
savevm.c +93 -0
index 22123be..c6aa502 100644
@@ -863,6 +863,105 @@ out:
     return ret;
 }
 
+static int qemu_load_devices_state(QEMUFile *f)
+{
+    uint8_t section_type;
+    unsigned int v;
+    int ret;
+
+    if (qemu_savevm_state_blocked(NULL)) {
+        return -EINVAL;
+    }
+
+    v = qemu_get_be32(f);
+    if (v != QEMU_VM_FILE_MAGIC) {
+        return -EINVAL;
+    }
+
+    v = qemu_get_be32(f);
+    if (v == QEMU_VM_FILE_VERSION_COMPAT) {
+        fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
+        return -ENOTSUP;
+    }
+    if (v != QEMU_VM_FILE_VERSION) {
+        return -ENOTSUP;
+    }
+
+    while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
+        uint32_t instance_id, version_id, section_id;
+        SaveStateEntry *se;
+        char idstr[257];
+        int len;
+
+        switch (section_type) {
+        case QEMU_VM_SECTION_FULL:
+            /* Read section start */
+            section_id = qemu_get_be32(f);
+            len = qemu_get_byte(f);
+            qemu_get_buffer(f, (uint8_t *)idstr, len);
+            idstr[len] = 0;
+            instance_id = qemu_get_be32(f);
+            version_id = qemu_get_be32(f);
+
+            /* Find savevm section */
+            se = find_se(idstr, instance_id);
+            if (se == NULL) {
+                fprintf(stderr, "Unknown savevm section or instance '%s' %d\n",
+                        idstr, instance_id);
+                ret = -EINVAL;
+                goto out;
+            }
+
+            /* Validate version */
+            if (version_id > se->version_id) {
+                fprintf(stderr, "loadvm: unsupported version %d for '%s' v%d\n",
+                        version_id, idstr, se->version_id);
+                ret = -EINVAL;
+                goto out;
+            }
+
+            /* Validate if it is a device's state */
+            if (se->is_ram) {
+                fprintf(stderr, "loadvm: %s is not devices state\n", idstr);
+                ret = -EINVAL;
+                goto out;
+            }
+
+            ret = vmstate_load(f, se, version_id);
+            if (ret < 0) {
+                fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
+                        instance_id, idstr);
+                goto out;
+            }
+            break;
+        case QEMU_VM_SECTION_START:
+        case QEMU_VM_SECTION_PART:
+        case QEMU_VM_SECTION_END:
+            /*
+             * The file is saved by the command xen-save-devices-state,
+             * So it should not contain section start/part/end.
+             */
+        default:
+            fprintf(stderr, "Unknown savevm section type %d\n", section_type);
+            ret = -EINVAL;
+            goto out;
+        }
+    }
+
+    cpu_synchronize_all_post_init();
+
+    ret = 0;
+
+out:
+    if (ret == 0) {
+        if (qemu_file_get_error(f)) {
+            ret = -EIO;
Quoted 8 lines

savevm.c out: · line 963

Quoted 5 lines
savevm.c +5 -0
+        }
+    }
+
+    return ret;
+}

Assuming that the state file only contains device states, why don't you just call qemu_loadvm_state to implement the command?

Quoted 2 lines

Do you mean there is no need to check the file? If the file contains some memory, it will cause unexpected problem.

I would prefer to avoid duplicating qemu_loadvm_state just to add an if (se->is_ram) check.
I would rather introduce a generic loadvm QMP command and rely on the fact that we are saving only device states via xen-save-devices-state.

Given that loading memory in QEMU on Xen always leads to errors, maybe we could still add a check in qemu_loadvm_state anyway. Something like:

savevm.c +7 -0
index e19ae0a..759eefa 100644
@@ -938,6 +938,13 @@ int qemu_loadvm_state(QEMUFile *f)
                 goto out;
             }
 
+            /* Validate if it is a device's state */
+            if (xen_enabled() && se->is_ram) {
+                fprintf(stderr, "loadvm: %s RAM loading not allowed on Xen\n", idstr);
+                ret = -EINVAL;
+                goto out;
+            }
+
             /* Add entry */
             le = g_malloc0(sizeof(*le));
 
Quoted 55 lines

Thanks
Wen Congyang

Quoted 51 lines
Quoted 49 lines
savevm.c +27 -0
 static BlockDriverState *find_vmstate_bs(void)
 {
     BlockDriverState *bs = NULL;
@@ -1027,6 +1126,33 @@ void qmp_xen_save_devices_state(const char *filename, Error **errp)
     }
 }
 
+void qmp_xen_load_devices_state(const char *filename, Error **errp)
+{
+    QEMUFile *f;
+    int saved_vm_running;
+    int ret;
+
+    saved_vm_running = runstate_is_running();
+    vm_stop(RUN_STATE_RESTORE_VM);
+
+    f = qemu_fopen(filename, "rb");
+    if (!f) {
+        error_setg_file_open(errp, errno, filename);
+        goto out;
+    }
+
+    ret = qemu_load_devices_state(f);
+    qemu_fclose(f);
+    if (ret < 0) {
+        error_set(errp, QERR_IO_ERROR);
+    }
+
+out:
+    if (saved_vm_running) {
+        vm_start();
+    }
+}
+
 int load_vmstate(const char *name)
 {
     BlockDriverState *bs, *bs_vm_state;
Signature · 7 lines
1.9.0


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

.

#35 lore raw

Re: [Qemu-devel] [Xen-devel] [RFC Patch v3 23/22] Introduce "xen-load-devices-state"

Quoted 159 lines · Stefano Stabellini
Quoted 15 lines · Wen Congyang

At 09/06/2014 05:57 AM, Stefano Stabellini Write:

Quoted 5 lines · Wen Congyang
Quoted 3 lines

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.

Quoted 8 lines

Hello Wen,
please CC qemu-devel too for QEMU patches.

Could you please explain why do you need this command?

Is the main issue that there are no QMP commands today to load the state of a VM? It looks like that for vm restore we are using the -incoming command line option, but there is no alternative over QMP.

Quoted 8 lines

We only have hmp commands savevm/loadvm, and qmp commands xen-save-devices-state.

We use this new command for COLO:
1. suspend both primay vm and secondary vm
2. sync the state
3. resume both primary vm and secondary vm

In such case, we need to update all devices's state in any time.

OK. Please state this in the commit message.

Quoted 113 lines
Quoted 104 lines

[...]

Quoted 101 lines
savevm.c +93 -0
index 22123be..c6aa502 100644
@@ -863,6 +863,105 @@ out:
     return ret;
 }
 
+static int qemu_load_devices_state(QEMUFile *f)
+{
+    uint8_t section_type;
+    unsigned int v;
+    int ret;
+
+    if (qemu_savevm_state_blocked(NULL)) {
+        return -EINVAL;
+    }
+
+    v = qemu_get_be32(f);
+    if (v != QEMU_VM_FILE_MAGIC) {
+        return -EINVAL;
+    }
+
+    v = qemu_get_be32(f);
+    if (v == QEMU_VM_FILE_VERSION_COMPAT) {
+        fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
+        return -ENOTSUP;
+    }
+    if (v != QEMU_VM_FILE_VERSION) {
+        return -ENOTSUP;
+    }
+
+    while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
+        uint32_t instance_id, version_id, section_id;
+        SaveStateEntry *se;
+        char idstr[257];
+        int len;
+
+        switch (section_type) {
+        case QEMU_VM_SECTION_FULL:
+            /* Read section start */
+            section_id = qemu_get_be32(f);
+            len = qemu_get_byte(f);
+            qemu_get_buffer(f, (uint8_t *)idstr, len);
+            idstr[len] = 0;
+            instance_id = qemu_get_be32(f);
+            version_id = qemu_get_be32(f);
+
+            /* Find savevm section */
+            se = find_se(idstr, instance_id);
+            if (se == NULL) {
+                fprintf(stderr, "Unknown savevm section or instance '%s' %d\n",
+                        idstr, instance_id);
+                ret = -EINVAL;
+                goto out;
+            }
+
+            /* Validate version */
+            if (version_id > se->version_id) {
+                fprintf(stderr, "loadvm: unsupported version %d for '%s' v%d\n",
+                        version_id, idstr, se->version_id);
+                ret = -EINVAL;
+                goto out;
+            }
+
+            /* Validate if it is a device's state */
+            if (se->is_ram) {
+                fprintf(stderr, "loadvm: %s is not devices state\n", idstr);
+                ret = -EINVAL;
+                goto out;
+            }
+
+            ret = vmstate_load(f, se, version_id);
+            if (ret < 0) {
+                fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
+                        instance_id, idstr);
+                goto out;
+            }
+            break;
+        case QEMU_VM_SECTION_START:
+        case QEMU_VM_SECTION_PART:
+        case QEMU_VM_SECTION_END:
+            /*
+             * The file is saved by the command xen-save-devices-state,
+             * So it should not contain section start/part/end.
+             */
+        default:
+            fprintf(stderr, "Unknown savevm section type %d\n", section_type);
+            ret = -EINVAL;
+            goto out;
+        }
+    }
+
+    cpu_synchronize_all_post_init();
+
+    ret = 0;
+
+out:
+    if (ret == 0) {
+        if (qemu_file_get_error(f)) {
+            ret = -EIO;
Quoted 8 lines

savevm.c out: · line 963

Quoted 5 lines
savevm.c +5 -0
+        }
+    }
+
+    return ret;
+}

Assuming that the state file only contains device states, why don't you just call qemu_loadvm_state to implement the command?

Quoted 2 lines

Do you mean there is no need to check the file? If the file contains some memory, it will cause unexpected problem.

I would prefer to avoid duplicating qemu_loadvm_state just to add an if (se->is_ram) check.
I would rather introduce a generic loadvm QMP command and rely on the fact that we are saving only device states via xen-save-devices-state.

Given that loading memory in QEMU on Xen always leads to errors, maybe we could still add a check in qemu_loadvm_state anyway. Something like:

savevm.c +0 -0
index e19ae0a..759eefa 100644
@@ -938,6 +938,13 @@ int qemu_loadvm_state(QEMUFile *f)
                 goto out;
             }

savevm.c int qemu_loadvm_state(QEMUFile *f) · line 949

Quoted 10 lines
savevm.c +7 -0
 
+            /* Validate if it is a device's state */
+            if (xen_enabled() && se->is_ram) {
+                fprintf(stderr, "loadvm: %s RAM loading not allowed on Xen\n", idstr);
+                ret = -EINVAL;
+                goto out;
+            }
+
             /* Add entry */
             le = g_malloc0(sizeof(*le));

Thanks, I think it works for me.

Wen Congyang

Quoted 61 lines
Quoted 4 lines
Quoted 55 lines

Thanks
Wen Congyang

Quoted 51 lines
Quoted 49 lines
savevm.c +27 -0
 static BlockDriverState *find_vmstate_bs(void)
 {
     BlockDriverState *bs = NULL;
@@ -1027,6 +1126,33 @@ void qmp_xen_save_devices_state(const char *filename, Error **errp)
     }
 }
 
+void qmp_xen_load_devices_state(const char *filename, Error **errp)
+{
+    QEMUFile *f;
+    int saved_vm_running;
+    int ret;
+
+    saved_vm_running = runstate_is_running();
+    vm_stop(RUN_STATE_RESTORE_VM);
+
+    f = qemu_fopen(filename, "rb");
+    if (!f) {
+        error_setg_file_open(errp, errno, filename);
+        goto out;
+    }
+
+    ret = qemu_load_devices_state(f);
+    qemu_fclose(f);
+    if (ret < 0) {
+        error_set(errp, QERR_IO_ERROR);
+    }
+
+out:
+    if (saved_vm_running) {
+        vm_start();
+    }
+}
+
 int load_vmstate(const char *name)
 {
     BlockDriverState *bs, *bs_vm_state;
Signature · 7 lines
1.9.0


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

.

.

#36 lore raw

Re: [RFC Patch v3 23/22] Introduce "xen-load-devices-state"

In reply to Stefano Stabellini
Quoted 159 lines · Stefano Stabellini
Quoted 15 lines · Wen Congyang

At 09/06/2014 05:57 AM, Stefano Stabellini Write:

Quoted 5 lines · Wen Congyang
Quoted 3 lines

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.

Quoted 8 lines

Hello Wen,
please CC qemu-devel too for QEMU patches.

Could you please explain why do you need this command?

Is the main issue that there are no QMP commands today to load the state of a VM? It looks like that for vm restore we are using the -incoming command line option, but there is no alternative over QMP.

Quoted 8 lines

We only have hmp commands savevm/loadvm, and qmp commands xen-save-devices-state.

We use this new command for COLO:
1. suspend both primay vm and secondary vm
2. sync the state
3. resume both primary vm and secondary vm

In such case, we need to update all devices's state in any time.

OK. Please state this in the commit message.

Quoted 113 lines
Quoted 104 lines

[...]

Quoted 101 lines
savevm.c +93 -0
index 22123be..c6aa502 100644
@@ -863,6 +863,105 @@ out:
     return ret;
 }
 
+static int qemu_load_devices_state(QEMUFile *f)
+{
+    uint8_t section_type;
+    unsigned int v;
+    int ret;
+
+    if (qemu_savevm_state_blocked(NULL)) {
+        return -EINVAL;
+    }
+
+    v = qemu_get_be32(f);
+    if (v != QEMU_VM_FILE_MAGIC) {
+        return -EINVAL;
+    }
+
+    v = qemu_get_be32(f);
+    if (v == QEMU_VM_FILE_VERSION_COMPAT) {
+        fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
+        return -ENOTSUP;
+    }
+    if (v != QEMU_VM_FILE_VERSION) {
+        return -ENOTSUP;
+    }
+
+    while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
+        uint32_t instance_id, version_id, section_id;
+        SaveStateEntry *se;
+        char idstr[257];
+        int len;
+
+        switch (section_type) {
+        case QEMU_VM_SECTION_FULL:
+            /* Read section start */
+            section_id = qemu_get_be32(f);
+            len = qemu_get_byte(f);
+            qemu_get_buffer(f, (uint8_t *)idstr, len);
+            idstr[len] = 0;
+            instance_id = qemu_get_be32(f);
+            version_id = qemu_get_be32(f);
+
+            /* Find savevm section */
+            se = find_se(idstr, instance_id);
+            if (se == NULL) {
+                fprintf(stderr, "Unknown savevm section or instance '%s' %d\n",
+                        idstr, instance_id);
+                ret = -EINVAL;
+                goto out;
+            }
+
+            /* Validate version */
+            if (version_id > se->version_id) {
+                fprintf(stderr, "loadvm: unsupported version %d for '%s' v%d\n",
+                        version_id, idstr, se->version_id);
+                ret = -EINVAL;
+                goto out;
+            }
+
+            /* Validate if it is a device's state */
+            if (se->is_ram) {
+                fprintf(stderr, "loadvm: %s is not devices state\n", idstr);
+                ret = -EINVAL;
+                goto out;
+            }
+
+            ret = vmstate_load(f, se, version_id);
+            if (ret < 0) {
+                fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
+                        instance_id, idstr);
+                goto out;
+            }
+            break;
+        case QEMU_VM_SECTION_START:
+        case QEMU_VM_SECTION_PART:
+        case QEMU_VM_SECTION_END:
+            /*
+             * The file is saved by the command xen-save-devices-state,
+             * So it should not contain section start/part/end.
+             */
+        default:
+            fprintf(stderr, "Unknown savevm section type %d\n", section_type);
+            ret = -EINVAL;
+            goto out;
+        }
+    }
+
+    cpu_synchronize_all_post_init();
+
+    ret = 0;
+
+out:
+    if (ret == 0) {
+        if (qemu_file_get_error(f)) {
+            ret = -EIO;
Quoted 8 lines

savevm.c out: · line 963

Quoted 5 lines
savevm.c +5 -0
+        }
+    }
+
+    return ret;
+}

Assuming that the state file only contains device states, why don't you just call qemu_loadvm_state to implement the command?

Quoted 2 lines

Do you mean there is no need to check the file? If the file contains some memory, it will cause unexpected problem.

I would prefer to avoid duplicating qemu_loadvm_state just to add an if (se->is_ram) check.
I would rather introduce a generic loadvm QMP command and rely on the fact that we are saving only device states via xen-save-devices-state.

Given that loading memory in QEMU on Xen always leads to errors, maybe we could still add a check in qemu_loadvm_state anyway. Something like:

savevm.c +0 -0
index e19ae0a..759eefa 100644
@@ -938,6 +938,13 @@ int qemu_loadvm_state(QEMUFile *f)
                 goto out;
             }

savevm.c int qemu_loadvm_state(QEMUFile *f) · line 949

Quoted 10 lines
savevm.c +7 -0
 
+            /* Validate if it is a device's state */
+            if (xen_enabled() && se->is_ram) {
+                fprintf(stderr, "loadvm: %s RAM loading not allowed on Xen\n", idstr);
+                ret = -EINVAL;
+                goto out;
+            }
+
             /* Add entry */
             le = g_malloc0(sizeof(*le));

Thanks, I think it works for me.

Wen Congyang

Quoted 61 lines
Quoted 4 lines
Quoted 55 lines

Thanks
Wen Congyang

Quoted 51 lines
Quoted 49 lines
savevm.c +27 -0
 static BlockDriverState *find_vmstate_bs(void)
 {
     BlockDriverState *bs = NULL;
@@ -1027,6 +1126,33 @@ void qmp_xen_save_devices_state(const char *filename, Error **errp)
     }
 }
 
+void qmp_xen_load_devices_state(const char *filename, Error **errp)
+{
+    QEMUFile *f;
+    int saved_vm_running;
+    int ret;
+
+    saved_vm_running = runstate_is_running();
+    vm_stop(RUN_STATE_RESTORE_VM);
+
+    f = qemu_fopen(filename, "rb");
+    if (!f) {
+        error_setg_file_open(errp, errno, filename);
+        goto out;
+    }
+
+    ret = qemu_load_devices_state(f);
+    qemu_fclose(f);
+    if (ret < 0) {
+        error_set(errp, QERR_IO_ERROR);
+    }
+
+out:
+    if (saved_vm_running) {
+        vm_start();
+    }
+}
+
 int load_vmstate(const char *name)
 {
     BlockDriverState *bs, *bs_vm_state;
Signature · 7 lines
1.9.0


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

.

.