2 * COarse-grain LOck-stepping Virtual Machines for Non-stop Service (COLO)
3 * (a.k.a. Fault Tolerance or Continuous Replication)
5 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
6 * Copyright (c) 2016 FUJITSU LIMITED
7 * Copyright (c) 2016 Intel Corporation
9 * This work is licensed under the terms of the GNU GPL, version 2 or
10 * later. See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "sysemu/sysemu.h"
15 #include "qemu-file-channel.h"
16 #include "migration.h"
17 #include "qemu-file.h"
19 #include "migration/colo.h"
21 #include "io/channel-buffer.h"
23 #include "qemu/error-report.h"
24 #include "migration/failover.h"
25 #include "replication.h"
26 #include "qmp-commands.h"
28 static bool vmstate_loading;
30 #define COLO_BUFFER_BASE_SIZE (4 * 1024 * 1024)
32 bool colo_supported(void)
37 bool migration_in_colo_state(void)
39 MigrationState *s = migrate_get_current();
41 return (s->state == MIGRATION_STATUS_COLO);
44 bool migration_incoming_in_colo_state(void)
46 MigrationIncomingState *mis = migration_incoming_get_current();
48 return mis && (mis->state == MIGRATION_STATUS_COLO);
51 static bool colo_runstate_is_stopped(void)
53 return runstate_check(RUN_STATE_COLO) || !runstate_is_running();
56 static void secondary_vm_do_failover(void)
59 MigrationIncomingState *mis = migration_incoming_get_current();
61 /* Can not do failover during the process of VM's loading VMstate, Or
62 * it will break the secondary VM.
64 if (vmstate_loading) {
65 old_state = failover_set_state(FAILOVER_STATUS_ACTIVE,
66 FAILOVER_STATUS_RELAUNCH);
67 if (old_state != FAILOVER_STATUS_ACTIVE) {
68 error_report("Unknown error while do failover for secondary VM,"
69 "old_state: %s", FailoverStatus_lookup[old_state]);
74 migrate_set_state(&mis->state, MIGRATION_STATUS_COLO,
75 MIGRATION_STATUS_COMPLETED);
78 error_report("\"-S\" qemu option will be ignored in secondary side");
79 /* recover runstate to normal migration finish state */
83 * Make sure COLO incoming thread not block in recv or send,
84 * If mis->from_src_file and mis->to_src_file use the same fd,
85 * The second shutdown() will return -1, we ignore this value,
88 if (mis->from_src_file) {
89 qemu_file_shutdown(mis->from_src_file);
91 if (mis->to_src_file) {
92 qemu_file_shutdown(mis->to_src_file);
95 old_state = failover_set_state(FAILOVER_STATUS_ACTIVE,
96 FAILOVER_STATUS_COMPLETED);
97 if (old_state != FAILOVER_STATUS_ACTIVE) {
98 error_report("Incorrect state (%s) while doing failover for "
99 "secondary VM", FailoverStatus_lookup[old_state]);
102 /* Notify COLO incoming thread that failover work is finished */
103 qemu_sem_post(&mis->colo_incoming_sem);
104 /* For Secondary VM, jump to incoming co */
105 if (mis->migration_incoming_co) {
106 qemu_coroutine_enter(mis->migration_incoming_co);
110 static void primary_vm_do_failover(void)
112 MigrationState *s = migrate_get_current();
115 migrate_set_state(&s->state, MIGRATION_STATUS_COLO,
116 MIGRATION_STATUS_COMPLETED);
119 * Wake up COLO thread which may blocked in recv() or send(),
120 * The s->rp_state.from_dst_file and s->to_dst_file may use the
121 * same fd, but we still shutdown the fd for twice, it is harmless.
123 if (s->to_dst_file) {
124 qemu_file_shutdown(s->to_dst_file);
126 if (s->rp_state.from_dst_file) {
127 qemu_file_shutdown(s->rp_state.from_dst_file);
130 old_state = failover_set_state(FAILOVER_STATUS_ACTIVE,
131 FAILOVER_STATUS_COMPLETED);
132 if (old_state != FAILOVER_STATUS_ACTIVE) {
133 error_report("Incorrect state (%s) while doing failover for Primary VM",
134 FailoverStatus_lookup[old_state]);
137 /* Notify COLO thread that failover work is finished */
138 qemu_sem_post(&s->colo_exit_sem);
141 void colo_do_failover(MigrationState *s)
143 /* Make sure VM stopped while failover happened. */
144 if (!colo_runstate_is_stopped()) {
145 vm_stop_force_state(RUN_STATE_COLO);
148 if (get_colo_mode() == COLO_MODE_PRIMARY) {
149 primary_vm_do_failover();
151 secondary_vm_do_failover();
155 void qmp_xen_set_replication(bool enable, bool primary,
156 bool has_failover, bool failover,
159 #ifdef CONFIG_REPLICATION
160 ReplicationMode mode = primary ?
161 REPLICATION_MODE_PRIMARY :
162 REPLICATION_MODE_SECONDARY;
164 if (has_failover && enable) {
165 error_setg(errp, "Parameter 'failover' is only for"
166 " stopping replication");
171 replication_start_all(mode, errp);
176 replication_stop_all(failover, failover ? NULL : errp);
183 ReplicationStatus *qmp_query_xen_replication_status(Error **errp)
185 #ifdef CONFIG_REPLICATION
187 ReplicationStatus *s = g_new0(ReplicationStatus, 1);
189 replication_get_error_all(&err);
193 s->desc = g_strdup(error_get_pretty(err));
205 void qmp_xen_colo_do_checkpoint(Error **errp)
207 #ifdef CONFIG_REPLICATION
208 replication_do_checkpoint_all(errp);
214 static void colo_send_message(QEMUFile *f, COLOMessage msg,
219 if (msg >= COLO_MESSAGE__MAX) {
220 error_setg(errp, "%s: Invalid message", __func__);
223 qemu_put_be32(f, msg);
226 ret = qemu_file_get_error(f);
228 error_setg_errno(errp, -ret, "Can't send COLO message");
230 trace_colo_send_message(COLOMessage_lookup[msg]);
233 static void colo_send_message_value(QEMUFile *f, COLOMessage msg,
234 uint64_t value, Error **errp)
236 Error *local_err = NULL;
239 colo_send_message(f, msg, &local_err);
241 error_propagate(errp, local_err);
244 qemu_put_be64(f, value);
247 ret = qemu_file_get_error(f);
249 error_setg_errno(errp, -ret, "Failed to send value for message:%s",
250 COLOMessage_lookup[msg]);
254 static COLOMessage colo_receive_message(QEMUFile *f, Error **errp)
259 msg = qemu_get_be32(f);
260 ret = qemu_file_get_error(f);
262 error_setg_errno(errp, -ret, "Can't receive COLO message");
265 if (msg >= COLO_MESSAGE__MAX) {
266 error_setg(errp, "%s: Invalid message", __func__);
269 trace_colo_receive_message(COLOMessage_lookup[msg]);
273 static void colo_receive_check_message(QEMUFile *f, COLOMessage expect_msg,
277 Error *local_err = NULL;
279 msg = colo_receive_message(f, &local_err);
281 error_propagate(errp, local_err);
284 if (msg != expect_msg) {
285 error_setg(errp, "Unexpected COLO message %d, expected %d",
290 static uint64_t colo_receive_message_value(QEMUFile *f, uint32_t expect_msg,
293 Error *local_err = NULL;
297 colo_receive_check_message(f, expect_msg, &local_err);
299 error_propagate(errp, local_err);
303 value = qemu_get_be64(f);
304 ret = qemu_file_get_error(f);
306 error_setg_errno(errp, -ret, "Failed to get value for COLO message: %s",
307 COLOMessage_lookup[expect_msg]);
312 static int colo_do_checkpoint_transaction(MigrationState *s,
313 QIOChannelBuffer *bioc,
316 Error *local_err = NULL;
319 colo_send_message(s->to_dst_file, COLO_MESSAGE_CHECKPOINT_REQUEST,
325 colo_receive_check_message(s->rp_state.from_dst_file,
326 COLO_MESSAGE_CHECKPOINT_REPLY, &local_err);
330 /* Reset channel-buffer directly */
331 qio_channel_io_seek(QIO_CHANNEL(bioc), 0, 0, NULL);
334 qemu_mutex_lock_iothread();
335 if (failover_get_state() != FAILOVER_STATUS_NONE) {
336 qemu_mutex_unlock_iothread();
339 vm_stop_force_state(RUN_STATE_COLO);
340 qemu_mutex_unlock_iothread();
341 trace_colo_vm_state_change("run", "stop");
343 * Failover request bh could be called after vm_stop_force_state(),
344 * So we need check failover_request_is_active() again.
346 if (failover_get_state() != FAILOVER_STATUS_NONE) {
350 /* Disable block migration */
351 migrate_set_block_enabled(false, &local_err);
352 qemu_savevm_state_header(fb);
353 qemu_savevm_state_begin(fb);
354 qemu_mutex_lock_iothread();
355 qemu_savevm_state_complete_precopy(fb, false, false);
356 qemu_mutex_unlock_iothread();
360 colo_send_message(s->to_dst_file, COLO_MESSAGE_VMSTATE_SEND, &local_err);
365 * We need the size of the VMstate data in Secondary side,
366 * With which we can decide how much data should be read.
368 colo_send_message_value(s->to_dst_file, COLO_MESSAGE_VMSTATE_SIZE,
369 bioc->usage, &local_err);
374 qemu_put_buffer(s->to_dst_file, bioc->data, bioc->usage);
375 qemu_fflush(s->to_dst_file);
376 ret = qemu_file_get_error(s->to_dst_file);
381 colo_receive_check_message(s->rp_state.from_dst_file,
382 COLO_MESSAGE_VMSTATE_RECEIVED, &local_err);
387 colo_receive_check_message(s->rp_state.from_dst_file,
388 COLO_MESSAGE_VMSTATE_LOADED, &local_err);
395 qemu_mutex_lock_iothread();
397 qemu_mutex_unlock_iothread();
398 trace_colo_vm_state_change("stop", "run");
402 error_report_err(local_err);
407 static void colo_process_checkpoint(MigrationState *s)
409 QIOChannelBuffer *bioc;
411 int64_t current_time = qemu_clock_get_ms(QEMU_CLOCK_HOST);
412 Error *local_err = NULL;
415 failover_init_state();
417 s->rp_state.from_dst_file = qemu_file_get_return_path(s->to_dst_file);
418 if (!s->rp_state.from_dst_file) {
419 error_report("Open QEMUFile from_dst_file failed");
424 * Wait for Secondary finish loading VM states and enter COLO
427 colo_receive_check_message(s->rp_state.from_dst_file,
428 COLO_MESSAGE_CHECKPOINT_READY, &local_err);
432 bioc = qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE);
433 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
434 object_unref(OBJECT(bioc));
436 qemu_mutex_lock_iothread();
438 qemu_mutex_unlock_iothread();
439 trace_colo_vm_state_change("stop", "run");
441 timer_mod(s->colo_delay_timer,
442 current_time + s->parameters.x_checkpoint_delay);
444 while (s->state == MIGRATION_STATUS_COLO) {
445 if (failover_get_state() != FAILOVER_STATUS_NONE) {
446 error_report("failover request");
450 qemu_sem_wait(&s->colo_checkpoint_sem);
452 ret = colo_do_checkpoint_transaction(s, bioc, fb);
459 /* Throw the unreported error message after exited from loop */
461 error_report_err(local_err);
468 timer_del(s->colo_delay_timer);
470 /* Hope this not to be too long to wait here */
471 qemu_sem_wait(&s->colo_exit_sem);
472 qemu_sem_destroy(&s->colo_exit_sem);
474 * Must be called after failover BH is completed,
475 * Or the failover BH may shutdown the wrong fd that
476 * re-used by other threads after we release here.
478 if (s->rp_state.from_dst_file) {
479 qemu_fclose(s->rp_state.from_dst_file);
483 void colo_checkpoint_notify(void *opaque)
485 MigrationState *s = opaque;
486 int64_t next_notify_time;
488 qemu_sem_post(&s->colo_checkpoint_sem);
489 s->colo_checkpoint_time = qemu_clock_get_ms(QEMU_CLOCK_HOST);
490 next_notify_time = s->colo_checkpoint_time +
491 s->parameters.x_checkpoint_delay;
492 timer_mod(s->colo_delay_timer, next_notify_time);
495 void migrate_start_colo_process(MigrationState *s)
497 qemu_mutex_unlock_iothread();
498 qemu_sem_init(&s->colo_checkpoint_sem, 0);
499 s->colo_delay_timer = timer_new_ms(QEMU_CLOCK_HOST,
500 colo_checkpoint_notify, s);
502 qemu_sem_init(&s->colo_exit_sem, 0);
503 migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE,
504 MIGRATION_STATUS_COLO);
505 colo_process_checkpoint(s);
506 qemu_mutex_lock_iothread();
509 static void colo_wait_handle_message(QEMUFile *f, int *checkpoint_request,
513 Error *local_err = NULL;
515 msg = colo_receive_message(f, &local_err);
517 error_propagate(errp, local_err);
522 case COLO_MESSAGE_CHECKPOINT_REQUEST:
523 *checkpoint_request = 1;
526 *checkpoint_request = 0;
527 error_setg(errp, "Got unknown COLO message: %d", msg);
532 void *colo_process_incoming_thread(void *opaque)
534 MigrationIncomingState *mis = opaque;
536 QIOChannelBuffer *bioc = NULL; /* Cache incoming device state */
539 Error *local_err = NULL;
541 qemu_sem_init(&mis->colo_incoming_sem, 0);
543 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
544 MIGRATION_STATUS_COLO);
546 failover_init_state();
548 mis->to_src_file = qemu_file_get_return_path(mis->from_src_file);
549 if (!mis->to_src_file) {
550 error_report("COLO incoming thread: Open QEMUFile to_src_file failed");
554 * Note: the communication between Primary side and Secondary side
555 * should be sequential, we set the fd to unblocked in migration incoming
556 * coroutine, and here we are in the COLO incoming thread, so it is ok to
557 * set the fd back to blocked.
559 qemu_file_set_blocking(mis->from_src_file, true);
561 bioc = qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE);
562 fb = qemu_fopen_channel_input(QIO_CHANNEL(bioc));
563 object_unref(OBJECT(bioc));
565 colo_send_message(mis->to_src_file, COLO_MESSAGE_CHECKPOINT_READY,
571 while (mis->state == MIGRATION_STATUS_COLO) {
574 colo_wait_handle_message(mis->from_src_file, &request, &local_err);
579 if (failover_get_state() != FAILOVER_STATUS_NONE) {
580 error_report("failover request");
584 /* FIXME: This is unnecessary for periodic checkpoint mode */
585 colo_send_message(mis->to_src_file, COLO_MESSAGE_CHECKPOINT_REPLY,
591 colo_receive_check_message(mis->from_src_file,
592 COLO_MESSAGE_VMSTATE_SEND, &local_err);
597 value = colo_receive_message_value(mis->from_src_file,
598 COLO_MESSAGE_VMSTATE_SIZE, &local_err);
604 * Read VM device state data into channel buffer,
605 * It's better to re-use the memory allocated.
606 * Here we need to handle the channel buffer directly.
608 if (value > bioc->capacity) {
609 bioc->capacity = value;
610 bioc->data = g_realloc(bioc->data, bioc->capacity);
612 total_size = qemu_get_buffer(mis->from_src_file, bioc->data, value);
613 if (total_size != value) {
614 error_report("Got %" PRIu64 " VMState data, less than expected"
615 " %" PRIu64, total_size, value);
618 bioc->usage = total_size;
619 qio_channel_io_seek(QIO_CHANNEL(bioc), 0, 0, NULL);
621 colo_send_message(mis->to_src_file, COLO_MESSAGE_VMSTATE_RECEIVED,
627 qemu_mutex_lock_iothread();
628 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
629 vmstate_loading = true;
630 if (qemu_loadvm_state(fb) < 0) {
631 error_report("COLO: loadvm failed");
632 qemu_mutex_unlock_iothread();
636 vmstate_loading = false;
637 qemu_mutex_unlock_iothread();
639 if (failover_get_state() == FAILOVER_STATUS_RELAUNCH) {
640 failover_set_state(FAILOVER_STATUS_RELAUNCH,
641 FAILOVER_STATUS_NONE);
642 failover_request_active(NULL);
646 colo_send_message(mis->to_src_file, COLO_MESSAGE_VMSTATE_LOADED,
654 vmstate_loading = false;
655 /* Throw the unreported error message after exited from loop */
657 error_report_err(local_err);
664 /* Hope this not to be too long to loop here */
665 qemu_sem_wait(&mis->colo_incoming_sem);
666 qemu_sem_destroy(&mis->colo_incoming_sem);
667 /* Must be called after failover BH is completed */
668 if (mis->to_src_file) {
669 qemu_fclose(mis->to_src_file);
671 migration_incoming_exit_colo();