@@ -10,6 +10,7 @@
* later. See the COPYING file in the top-level directory.
*/
+#include <unistd.h>
#include "sysemu/sysemu.h"
#include "migration/colo.h"
#include "trace.h"
@@ -34,22 +35,147 @@ bool migration_incoming_in_colo_state(void)
return mis && (mis->state == MIGRATION_STATUS_COLO);
}
+static void colo_put_cmd(QEMUFile *f, COLOMessage cmd,
+ Error **errp)
+{
+ int ret;
+
+ if (cmd >= COLO_MESSAGE__MAX) {
+ error_setg(errp, "%s: Invalid cmd", __func__);
+ return;
+ }
+ qemu_put_be32(f, cmd);
+ qemu_fflush(f);
+
+ ret = qemu_file_get_error(f);
+ if (ret < 0) {
+ error_setg_errno(errp, -ret, "Can't put COLO command");
+ }
+ trace_colo_put_cmd(COLOMessage_lookup[cmd]);
+}
+
+static COLOMessage colo_get_cmd(QEMUFile *f, Error **errp)
+{
+ COLOMessage cmd;
+ int ret;
+
+ cmd = qemu_get_be32(f);
+ ret = qemu_file_get_error(f);
+ if (ret < 0) {
+ error_setg_errno(errp, -ret, "Can't get COLO command");
+ return cmd;
+ }
+ if (cmd >= COLO_MESSAGE__MAX) {
+ error_setg(errp, "%s: Invalid cmd", __func__);
+ return cmd;
+ }
+ trace_colo_get_cmd(COLOMessage_lookup[cmd]);
+ return cmd;
+}
+
+static void colo_get_check_cmd(QEMUFile *f, COLOMessage expect_cmd,
+ Error **errp)
+{
+ COLOMessage cmd;
+ Error *local_err = NULL;
+
+ cmd = colo_get_cmd(f, &local_err);
+ if (local_err) {
+ error_propagate(errp, local_err);
+ return;
+ }
+ if (cmd != expect_cmd) {
+ error_setg(errp, "Unexpected COLO command %d, expected %d",
+ expect_cmd, cmd);
+ }
+}
+
+static int colo_do_checkpoint_transaction(MigrationState *s)
+{
+ Error *local_err = NULL;
+
+ colo_put_cmd(s->to_dst_file, COLO_MESSAGE_CHECKPOINT_REQUEST,
+ &local_err);
+ if (local_err) {
+ goto out;
+ }
+
+ colo_get_check_cmd(s->rp_state.from_dst_file,
+ COLO_MESSAGE_CHECKPOINT_REPLY, &local_err);
+ if (local_err) {
+ goto out;
+ }
+
+ /* TODO: suspend and save vm state to colo buffer */
+
+ colo_put_cmd(s->to_dst_file, COLO_MESSAGE_VMSTATE_SEND, &local_err);
+ if (local_err) {
+ goto out;
+ }
+
+ /* TODO: send vmstate to Secondary */
+
+ colo_get_check_cmd(s->rp_state.from_dst_file,
+ COLO_MESSAGE_VMSTATE_RECEIVED, &local_err);
+ if (local_err) {
+ goto out;
+ }
+
+ colo_get_check_cmd(s->rp_state.from_dst_file,
+ COLO_MESSAGE_VMSTATE_LOADED, &local_err);
+ if (local_err) {
+ goto out;
+ }
+
+ /* TODO: resume Primary */
+
+ return 0;
+out:
+ if (local_err) {
+ error_report_err(local_err);
+ }
+ return -EINVAL;
+}
+
static void colo_process_checkpoint(MigrationState *s)
{
+ Error *local_err = NULL;
+ int ret;
+
s->rp_state.from_dst_file = qemu_file_get_return_path(s->to_dst_file);
if (!s->rp_state.from_dst_file) {
error_report("Open QEMUFile from_dst_file failed");
goto out;
}
+ /*
+ * Wait for Secondary finish loading vm states and enter COLO
+ * restore.
+ */
+ colo_get_check_cmd(s->rp_state.from_dst_file,
+ COLO_MESSAGE_CHECKPOINT_READY, &local_err);
+ if (local_err) {
+ goto out;
+ }
+
qemu_mutex_lock_iothread();
vm_start();
qemu_mutex_unlock_iothread();
trace_colo_vm_state_change("stop", "run");
- /*TODO: COLO checkpoint savevm loop*/
+ while (s->state == MIGRATION_STATUS_COLO) {
+ /* start a colo checkpoint */
+ ret = colo_do_checkpoint_transaction(s);
+ if (ret < 0) {
+ goto out;
+ }
+ }
out:
+ /* Throw the unreported error message after exited from loop */
+ if (local_err) {
+ error_report_err(local_err);
+ }
migrate_set_state(&s->state, MIGRATION_STATUS_COLO,
MIGRATION_STATUS_COMPLETED);
@@ -67,9 +193,33 @@ void migrate_start_colo_process(MigrationState *s)
qemu_mutex_lock_iothread();
}
+static void colo_wait_handle_cmd(QEMUFile *f, int *checkpoint_request,
+ Error **errp)
+{
+ COLOMessage cmd;
+ Error *local_err = NULL;
+
+ cmd = colo_get_cmd(f, &local_err);
+ if (local_err) {
+ error_propagate(errp, local_err);
+ return;
+ }
+
+ switch (cmd) {
+ case COLO_MESSAGE_CHECKPOINT_REQUEST:
+ *checkpoint_request = 1;
+ break;
+ default:
+ *checkpoint_request = 0;
+ error_setg(errp, "Got unknown COLO command: %d", cmd);
+ break;
+ }
+}
+
void *colo_process_incoming_thread(void *opaque)
{
MigrationIncomingState *mis = opaque;
+ Error *local_err = NULL;
migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
MIGRATION_STATUS_COLO);
@@ -85,9 +235,56 @@ void *colo_process_incoming_thread(void *opaque)
*/
qemu_file_set_blocking(mis->from_src_file, true);
- /* TODO: COLO checkpoint restore loop */
+ colo_put_cmd(mis->to_src_file, COLO_MESSAGE_CHECKPOINT_READY,
+ &local_err);
+ if (local_err) {
+ goto out;
+ }
+
+ while (mis->state == MIGRATION_STATUS_COLO) {
+ int request;
+
+ colo_wait_handle_cmd(mis->from_src_file, &request, &local_err);
+ if (local_err) {
+ goto out;
+ }
+ assert(request);
+ /* FIXME: This is unnecessary for periodic checkpoint mode */
+ colo_put_cmd(mis->to_src_file, COLO_MESSAGE_CHECKPOINT_REPLY,
+ &local_err);
+ if (local_err) {
+ goto out;
+ }
+
+ colo_get_check_cmd(mis->from_src_file,
+ COLO_MESSAGE_VMSTATE_SEND, &local_err);
+ if (local_err) {
+ goto out;
+ }
+
+ /* TODO: read migration data into colo buffer */
+
+ colo_put_cmd(mis->to_src_file, COLO_MESSAGE_VMSTATE_RECEIVED,
+ &local_err);
+ if (local_err) {
+ goto out;
+ }
+
+ /* TODO: load vm state */
+
+ colo_put_cmd(mis->to_src_file, COLO_MESSAGE_VMSTATE_LOADED,
+ &local_err);
+ if (local_err) {
+ goto out;
+ }
+ }
out:
+ /* Throw the unreported error message after exited from loop */
+ if (local_err) {
+ error_report_err(local_err);
+ }
+
if (mis->to_src_file) {
qemu_fclose(mis->to_src_file);
}
@@ -718,6 +718,31 @@
{ 'command': 'migrate-start-postcopy' }
##
+# @COLOMessage
+#
+# The message transmission between PVM and SVM
+#
+# @checkpoint-ready: SVM is ready for checkpointing
+#
+# @checkpoint-request: PVM tells SVM to prepare for new checkpointing
+#
+# @checkpoint-reply: SVM gets PVM's checkpoint request
+#
+# @vmstate-send: VM's state will be sent by PVM.
+#
+# @vmstate-size: The total size of VMstate.
+#
+# @vmstate-received: VM's state has been received by SVM.
+#
+# @vmstate-loaded: VM's state has been loaded by SVM.
+#
+# Since: 2.6
+##
+{ 'enum': 'COLOMessage',
+ 'data': [ 'checkpoint-ready', 'checkpoint-request', 'checkpoint-reply',
+ 'vmstate-send', 'vmstate-size', 'vmstate-received',
+ 'vmstate-loaded' ] }
+
# @MouseInfo:
#
# Information about a mouse device.
@@ -1583,6 +1583,8 @@ postcopy_ram_incoming_cleanup_join(void) ""
# migration/colo.c
colo_vm_state_change(const char *old, const char *new) "Change '%s' => '%s'"
+colo_put_cmd(const char *msg) "Send '%s' cmd"
+colo_get_cmd(const char *msg) "Receive '%s' cmd"
# kvm-all.c
kvm_ioctl(int type, void *arg) "type 0x%x, arg %p"