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 "qapi/error.h"
16 #include "qapi/qapi-commands-migration.h"
17 #include "qemu-file-channel.h"
18 #include "migration.h"
19 #include "qemu-file.h"
21 #include "migration/colo.h"
23 #include "io/channel-buffer.h"
25 #include "qemu/error-report.h"
26 #include "migration/failover.h"
27 #ifdef CONFIG_REPLICATION
28 #include "replication.h"
30 #include "net/colo-compare.h"
32 #include "block/block.h"
33 #include "qapi/qapi-events-migration.h"
34 #include "qapi/qmp/qerror.h"
35 #include "sysemu/cpus.h"
36 #include "net/filter.h"
38 static bool vmstate_loading;
39 static Notifier packets_compare_notifier;
41 #define COLO_BUFFER_BASE_SIZE (4 * 1024 * 1024)
43 bool migration_in_colo_state(void)
45 MigrationState *s = migrate_get_current();
47 return (s->state == MIGRATION_STATUS_COLO);
50 bool migration_incoming_in_colo_state(void)
52 MigrationIncomingState *mis = migration_incoming_get_current();
54 return mis && (mis->state == MIGRATION_STATUS_COLO);
57 static bool colo_runstate_is_stopped(void)
59 return runstate_check(RUN_STATE_COLO) || !runstate_is_running();
62 static void secondary_vm_do_failover(void)
64 /* COLO needs enable block-replication */
65 #ifdef CONFIG_REPLICATION
67 MigrationIncomingState *mis = migration_incoming_get_current();
68 Error *local_err = NULL;
70 /* Can not do failover during the process of VM's loading VMstate, Or
71 * it will break the secondary VM.
73 if (vmstate_loading) {
74 old_state = failover_set_state(FAILOVER_STATUS_ACTIVE,
75 FAILOVER_STATUS_RELAUNCH);
76 if (old_state != FAILOVER_STATUS_ACTIVE) {
77 error_report("Unknown error while do failover for secondary VM,"
78 "old_state: %s", FailoverStatus_str(old_state));
83 migrate_set_state(&mis->state, MIGRATION_STATUS_COLO,
84 MIGRATION_STATUS_COMPLETED);
86 replication_stop_all(true, &local_err);
88 error_report_err(local_err);
91 /* Notify all filters of all NIC to do checkpoint */
92 colo_notify_filters_event(COLO_EVENT_FAILOVER, &local_err);
94 error_report_err(local_err);
98 error_report("\"-S\" qemu option will be ignored in secondary side");
99 /* recover runstate to normal migration finish state */
103 * Make sure COLO incoming thread not block in recv or send,
104 * If mis->from_src_file and mis->to_src_file use the same fd,
105 * The second shutdown() will return -1, we ignore this value,
108 if (mis->from_src_file) {
109 qemu_file_shutdown(mis->from_src_file);
111 if (mis->to_src_file) {
112 qemu_file_shutdown(mis->to_src_file);
115 old_state = failover_set_state(FAILOVER_STATUS_ACTIVE,
116 FAILOVER_STATUS_COMPLETED);
117 if (old_state != FAILOVER_STATUS_ACTIVE) {
118 error_report("Incorrect state (%s) while doing failover for "
119 "secondary VM", FailoverStatus_str(old_state));
122 /* Notify COLO incoming thread that failover work is finished */
123 qemu_sem_post(&mis->colo_incoming_sem);
124 /* For Secondary VM, jump to incoming co */
125 if (mis->migration_incoming_co) {
126 qemu_coroutine_enter(mis->migration_incoming_co);
133 static void primary_vm_do_failover(void)
135 #ifdef CONFIG_REPLICATION
136 MigrationState *s = migrate_get_current();
138 Error *local_err = NULL;
140 migrate_set_state(&s->state, MIGRATION_STATUS_COLO,
141 MIGRATION_STATUS_COMPLETED);
143 * kick COLO thread which might wait at
144 * qemu_sem_wait(&s->colo_checkpoint_sem).
146 colo_checkpoint_notify(migrate_get_current());
149 * Wake up COLO thread which may blocked in recv() or send(),
150 * The s->rp_state.from_dst_file and s->to_dst_file may use the
151 * same fd, but we still shutdown the fd for twice, it is harmless.
153 if (s->to_dst_file) {
154 qemu_file_shutdown(s->to_dst_file);
156 if (s->rp_state.from_dst_file) {
157 qemu_file_shutdown(s->rp_state.from_dst_file);
160 old_state = failover_set_state(FAILOVER_STATUS_ACTIVE,
161 FAILOVER_STATUS_COMPLETED);
162 if (old_state != FAILOVER_STATUS_ACTIVE) {
163 error_report("Incorrect state (%s) while doing failover for Primary VM",
164 FailoverStatus_str(old_state));
168 replication_stop_all(true, &local_err);
170 error_report_err(local_err);
174 /* Notify COLO thread that failover work is finished */
175 qemu_sem_post(&s->colo_exit_sem);
181 COLOMode get_colo_mode(void)
183 if (migration_in_colo_state()) {
184 return COLO_MODE_PRIMARY;
185 } else if (migration_incoming_in_colo_state()) {
186 return COLO_MODE_SECONDARY;
188 return COLO_MODE_NONE;
192 void colo_do_failover(MigrationState *s)
194 /* Make sure VM stopped while failover happened. */
195 if (!colo_runstate_is_stopped()) {
196 vm_stop_force_state(RUN_STATE_COLO);
199 if (get_colo_mode() == COLO_MODE_PRIMARY) {
200 primary_vm_do_failover();
202 secondary_vm_do_failover();
206 #ifdef CONFIG_REPLICATION
207 void qmp_xen_set_replication(bool enable, bool primary,
208 bool has_failover, bool failover,
211 ReplicationMode mode = primary ?
212 REPLICATION_MODE_PRIMARY :
213 REPLICATION_MODE_SECONDARY;
215 if (has_failover && enable) {
216 error_setg(errp, "Parameter 'failover' is only for"
217 " stopping replication");
222 replication_start_all(mode, errp);
227 replication_stop_all(failover, failover ? NULL : errp);
231 ReplicationStatus *qmp_query_xen_replication_status(Error **errp)
234 ReplicationStatus *s = g_new0(ReplicationStatus, 1);
236 replication_get_error_all(&err);
240 s->desc = g_strdup(error_get_pretty(err));
249 void qmp_xen_colo_do_checkpoint(Error **errp)
251 replication_do_checkpoint_all(errp);
255 COLOStatus *qmp_query_colo_status(Error **errp)
257 COLOStatus *s = g_new0(COLOStatus, 1);
259 s->mode = get_colo_mode();
261 switch (failover_get_state()) {
262 case FAILOVER_STATUS_NONE:
263 s->reason = COLO_EXIT_REASON_NONE;
265 case FAILOVER_STATUS_REQUIRE:
266 s->reason = COLO_EXIT_REASON_REQUEST;
269 s->reason = COLO_EXIT_REASON_ERROR;
275 static void colo_send_message(QEMUFile *f, COLOMessage msg,
280 if (msg >= COLO_MESSAGE__MAX) {
281 error_setg(errp, "%s: Invalid message", __func__);
284 qemu_put_be32(f, msg);
287 ret = qemu_file_get_error(f);
289 error_setg_errno(errp, -ret, "Can't send COLO message");
291 trace_colo_send_message(COLOMessage_str(msg));
294 static void colo_send_message_value(QEMUFile *f, COLOMessage msg,
295 uint64_t value, Error **errp)
297 Error *local_err = NULL;
300 colo_send_message(f, msg, &local_err);
302 error_propagate(errp, local_err);
305 qemu_put_be64(f, value);
308 ret = qemu_file_get_error(f);
310 error_setg_errno(errp, -ret, "Failed to send value for message:%s",
311 COLOMessage_str(msg));
315 static COLOMessage colo_receive_message(QEMUFile *f, Error **errp)
320 msg = qemu_get_be32(f);
321 ret = qemu_file_get_error(f);
323 error_setg_errno(errp, -ret, "Can't receive COLO message");
326 if (msg >= COLO_MESSAGE__MAX) {
327 error_setg(errp, "%s: Invalid message", __func__);
330 trace_colo_receive_message(COLOMessage_str(msg));
334 static void colo_receive_check_message(QEMUFile *f, COLOMessage expect_msg,
338 Error *local_err = NULL;
340 msg = colo_receive_message(f, &local_err);
342 error_propagate(errp, local_err);
345 if (msg != expect_msg) {
346 error_setg(errp, "Unexpected COLO message %d, expected %d",
351 static uint64_t colo_receive_message_value(QEMUFile *f, uint32_t expect_msg,
354 Error *local_err = NULL;
358 colo_receive_check_message(f, expect_msg, &local_err);
360 error_propagate(errp, local_err);
364 value = qemu_get_be64(f);
365 ret = qemu_file_get_error(f);
367 error_setg_errno(errp, -ret, "Failed to get value for COLO message: %s",
368 COLOMessage_str(expect_msg));
373 static int colo_do_checkpoint_transaction(MigrationState *s,
374 QIOChannelBuffer *bioc,
377 Error *local_err = NULL;
380 colo_send_message(s->to_dst_file, COLO_MESSAGE_CHECKPOINT_REQUEST,
386 colo_receive_check_message(s->rp_state.from_dst_file,
387 COLO_MESSAGE_CHECKPOINT_REPLY, &local_err);
391 /* Reset channel-buffer directly */
392 qio_channel_io_seek(QIO_CHANNEL(bioc), 0, 0, NULL);
395 qemu_mutex_lock_iothread();
396 if (failover_get_state() != FAILOVER_STATUS_NONE) {
397 qemu_mutex_unlock_iothread();
400 vm_stop_force_state(RUN_STATE_COLO);
401 qemu_mutex_unlock_iothread();
402 trace_colo_vm_state_change("run", "stop");
404 * Failover request bh could be called after vm_stop_force_state(),
405 * So we need check failover_request_is_active() again.
407 if (failover_get_state() != FAILOVER_STATUS_NONE) {
411 colo_notify_compares_event(NULL, COLO_EVENT_CHECKPOINT, &local_err);
416 /* Disable block migration */
417 migrate_set_block_enabled(false, &local_err);
418 qemu_mutex_lock_iothread();
420 #ifdef CONFIG_REPLICATION
421 replication_do_checkpoint_all(&local_err);
423 qemu_mutex_unlock_iothread();
430 colo_send_message(s->to_dst_file, COLO_MESSAGE_VMSTATE_SEND, &local_err);
432 qemu_mutex_unlock_iothread();
435 /* Note: device state is saved into buffer */
436 ret = qemu_save_device_state(fb);
438 qemu_mutex_unlock_iothread();
443 * Only save VM's live state, which not including device state.
444 * TODO: We may need a timeout mechanism to prevent COLO process
445 * to be blocked here.
447 qemu_savevm_live_state(s->to_dst_file);
452 * We need the size of the VMstate data in Secondary side,
453 * With which we can decide how much data should be read.
455 colo_send_message_value(s->to_dst_file, COLO_MESSAGE_VMSTATE_SIZE,
456 bioc->usage, &local_err);
461 qemu_put_buffer(s->to_dst_file, bioc->data, bioc->usage);
462 qemu_fflush(s->to_dst_file);
463 ret = qemu_file_get_error(s->to_dst_file);
468 colo_receive_check_message(s->rp_state.from_dst_file,
469 COLO_MESSAGE_VMSTATE_RECEIVED, &local_err);
474 colo_receive_check_message(s->rp_state.from_dst_file,
475 COLO_MESSAGE_VMSTATE_LOADED, &local_err);
482 qemu_mutex_lock_iothread();
484 qemu_mutex_unlock_iothread();
485 trace_colo_vm_state_change("stop", "run");
489 error_report_err(local_err);
494 static void colo_compare_notify_checkpoint(Notifier *notifier, void *data)
496 colo_checkpoint_notify(data);
499 static void colo_process_checkpoint(MigrationState *s)
501 QIOChannelBuffer *bioc;
503 int64_t current_time = qemu_clock_get_ms(QEMU_CLOCK_HOST);
504 Error *local_err = NULL;
507 failover_init_state();
509 s->rp_state.from_dst_file = qemu_file_get_return_path(s->to_dst_file);
510 if (!s->rp_state.from_dst_file) {
511 error_report("Open QEMUFile from_dst_file failed");
515 packets_compare_notifier.notify = colo_compare_notify_checkpoint;
516 colo_compare_register_notifier(&packets_compare_notifier);
519 * Wait for Secondary finish loading VM states and enter COLO
522 colo_receive_check_message(s->rp_state.from_dst_file,
523 COLO_MESSAGE_CHECKPOINT_READY, &local_err);
527 bioc = qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE);
528 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
529 object_unref(OBJECT(bioc));
531 qemu_mutex_lock_iothread();
532 #ifdef CONFIG_REPLICATION
533 replication_start_all(REPLICATION_MODE_PRIMARY, &local_err);
535 qemu_mutex_unlock_iothread();
543 qemu_mutex_unlock_iothread();
544 trace_colo_vm_state_change("stop", "run");
546 timer_mod(s->colo_delay_timer,
547 current_time + s->parameters.x_checkpoint_delay);
549 while (s->state == MIGRATION_STATUS_COLO) {
550 if (failover_get_state() != FAILOVER_STATUS_NONE) {
551 error_report("failover request");
555 qemu_sem_wait(&s->colo_checkpoint_sem);
557 if (s->state != MIGRATION_STATUS_COLO) {
560 ret = colo_do_checkpoint_transaction(s, bioc, fb);
567 /* Throw the unreported error message after exited from loop */
569 error_report_err(local_err);
577 * There are only two reasons we can get here, some error happened
578 * or the user triggered failover.
580 switch (failover_get_state()) {
581 case FAILOVER_STATUS_NONE:
582 qapi_event_send_colo_exit(COLO_MODE_PRIMARY,
583 COLO_EXIT_REASON_ERROR);
585 case FAILOVER_STATUS_REQUIRE:
586 qapi_event_send_colo_exit(COLO_MODE_PRIMARY,
587 COLO_EXIT_REASON_REQUEST);
593 /* Hope this not to be too long to wait here */
594 qemu_sem_wait(&s->colo_exit_sem);
595 qemu_sem_destroy(&s->colo_exit_sem);
598 * It is safe to unregister notifier after failover finished.
599 * Besides, colo_delay_timer and colo_checkpoint_sem can't be
600 * released befor unregister notifier, or there will be use-after-free
603 colo_compare_unregister_notifier(&packets_compare_notifier);
604 timer_del(s->colo_delay_timer);
605 timer_free(s->colo_delay_timer);
606 qemu_sem_destroy(&s->colo_checkpoint_sem);
609 * Must be called after failover BH is completed,
610 * Or the failover BH may shutdown the wrong fd that
611 * re-used by other threads after we release here.
613 if (s->rp_state.from_dst_file) {
614 qemu_fclose(s->rp_state.from_dst_file);
618 void colo_checkpoint_notify(void *opaque)
620 MigrationState *s = opaque;
621 int64_t next_notify_time;
623 qemu_sem_post(&s->colo_checkpoint_sem);
624 s->colo_checkpoint_time = qemu_clock_get_ms(QEMU_CLOCK_HOST);
625 next_notify_time = s->colo_checkpoint_time +
626 s->parameters.x_checkpoint_delay;
627 timer_mod(s->colo_delay_timer, next_notify_time);
630 void migrate_start_colo_process(MigrationState *s)
632 qemu_mutex_unlock_iothread();
633 qemu_sem_init(&s->colo_checkpoint_sem, 0);
634 s->colo_delay_timer = timer_new_ms(QEMU_CLOCK_HOST,
635 colo_checkpoint_notify, s);
637 qemu_sem_init(&s->colo_exit_sem, 0);
638 migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE,
639 MIGRATION_STATUS_COLO);
640 colo_process_checkpoint(s);
641 qemu_mutex_lock_iothread();
644 static void colo_wait_handle_message(QEMUFile *f, int *checkpoint_request,
648 Error *local_err = NULL;
650 msg = colo_receive_message(f, &local_err);
652 error_propagate(errp, local_err);
657 case COLO_MESSAGE_CHECKPOINT_REQUEST:
658 *checkpoint_request = 1;
661 *checkpoint_request = 0;
662 error_setg(errp, "Got unknown COLO message: %d", msg);
667 void *colo_process_incoming_thread(void *opaque)
669 MigrationIncomingState *mis = opaque;
671 QIOChannelBuffer *bioc = NULL; /* Cache incoming device state */
674 Error *local_err = NULL;
677 rcu_register_thread();
678 qemu_sem_init(&mis->colo_incoming_sem, 0);
680 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
681 MIGRATION_STATUS_COLO);
683 failover_init_state();
685 mis->to_src_file = qemu_file_get_return_path(mis->from_src_file);
686 if (!mis->to_src_file) {
687 error_report("COLO incoming thread: Open QEMUFile to_src_file failed");
691 * Note: the communication between Primary side and Secondary side
692 * should be sequential, we set the fd to unblocked in migration incoming
693 * coroutine, and here we are in the COLO incoming thread, so it is ok to
694 * set the fd back to blocked.
696 qemu_file_set_blocking(mis->from_src_file, true);
698 bioc = qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE);
699 fb = qemu_fopen_channel_input(QIO_CHANNEL(bioc));
700 object_unref(OBJECT(bioc));
702 qemu_mutex_lock_iothread();
703 #ifdef CONFIG_REPLICATION
704 replication_start_all(REPLICATION_MODE_SECONDARY, &local_err);
706 qemu_mutex_unlock_iothread();
713 trace_colo_vm_state_change("stop", "run");
714 qemu_mutex_unlock_iothread();
716 colo_send_message(mis->to_src_file, COLO_MESSAGE_CHECKPOINT_READY,
722 while (mis->state == MIGRATION_STATUS_COLO) {
725 colo_wait_handle_message(mis->from_src_file, &request, &local_err);
730 if (failover_get_state() != FAILOVER_STATUS_NONE) {
731 error_report("failover request");
735 qemu_mutex_lock_iothread();
736 vm_stop_force_state(RUN_STATE_COLO);
737 trace_colo_vm_state_change("run", "stop");
738 qemu_mutex_unlock_iothread();
740 /* FIXME: This is unnecessary for periodic checkpoint mode */
741 colo_send_message(mis->to_src_file, COLO_MESSAGE_CHECKPOINT_REPLY,
747 colo_receive_check_message(mis->from_src_file,
748 COLO_MESSAGE_VMSTATE_SEND, &local_err);
753 qemu_mutex_lock_iothread();
754 cpu_synchronize_all_pre_loadvm();
755 ret = qemu_loadvm_state_main(mis->from_src_file, mis);
756 qemu_mutex_unlock_iothread();
759 error_report("Load VM's live state (ram) error");
763 value = colo_receive_message_value(mis->from_src_file,
764 COLO_MESSAGE_VMSTATE_SIZE, &local_err);
770 * Read VM device state data into channel buffer,
771 * It's better to re-use the memory allocated.
772 * Here we need to handle the channel buffer directly.
774 if (value > bioc->capacity) {
775 bioc->capacity = value;
776 bioc->data = g_realloc(bioc->data, bioc->capacity);
778 total_size = qemu_get_buffer(mis->from_src_file, bioc->data, value);
779 if (total_size != value) {
780 error_report("Got %" PRIu64 " VMState data, less than expected"
781 " %" PRIu64, total_size, value);
784 bioc->usage = total_size;
785 qio_channel_io_seek(QIO_CHANNEL(bioc), 0, 0, NULL);
787 colo_send_message(mis->to_src_file, COLO_MESSAGE_VMSTATE_RECEIVED,
793 qemu_mutex_lock_iothread();
794 vmstate_loading = true;
795 ret = qemu_load_device_state(fb);
797 error_report("COLO: load device state failed");
798 qemu_mutex_unlock_iothread();
802 #ifdef CONFIG_REPLICATION
803 replication_get_error_all(&local_err);
805 qemu_mutex_unlock_iothread();
809 /* discard colo disk buffer */
810 replication_do_checkpoint_all(&local_err);
812 qemu_mutex_unlock_iothread();
818 /* Notify all filters of all NIC to do checkpoint */
819 colo_notify_filters_event(COLO_EVENT_CHECKPOINT, &local_err);
822 qemu_mutex_unlock_iothread();
826 vmstate_loading = false;
828 trace_colo_vm_state_change("stop", "run");
829 qemu_mutex_unlock_iothread();
831 if (failover_get_state() == FAILOVER_STATUS_RELAUNCH) {
832 failover_set_state(FAILOVER_STATUS_RELAUNCH,
833 FAILOVER_STATUS_NONE);
834 failover_request_active(NULL);
838 colo_send_message(mis->to_src_file, COLO_MESSAGE_VMSTATE_LOADED,
846 vmstate_loading = false;
847 /* Throw the unreported error message after exited from loop */
849 error_report_err(local_err);
852 switch (failover_get_state()) {
853 case FAILOVER_STATUS_NONE:
854 qapi_event_send_colo_exit(COLO_MODE_SECONDARY,
855 COLO_EXIT_REASON_ERROR);
857 case FAILOVER_STATUS_REQUIRE:
858 qapi_event_send_colo_exit(COLO_MODE_SECONDARY,
859 COLO_EXIT_REASON_REQUEST);
869 /* Hope this not to be too long to loop here */
870 qemu_sem_wait(&mis->colo_incoming_sem);
871 qemu_sem_destroy(&mis->colo_incoming_sem);
872 /* Must be called after failover BH is completed */
873 if (mis->to_src_file) {
874 qemu_fclose(mis->to_src_file);
875 mis->to_src_file = NULL;
878 rcu_unregister_thread();