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 #include "replication.h"
28 #include "net/colo-compare.h"
30 #include "block/block.h"
31 #include "qapi/qapi-events-migration.h"
32 #include "qapi/qmp/qerror.h"
33 #include "sysemu/cpus.h"
34 #include "net/filter.h"
36 static bool vmstate_loading
;
37 static Notifier packets_compare_notifier
;
39 #define COLO_BUFFER_BASE_SIZE (4 * 1024 * 1024)
41 bool migration_in_colo_state(void)
43 MigrationState
*s
= migrate_get_current();
45 return (s
->state
== MIGRATION_STATUS_COLO
);
48 bool migration_incoming_in_colo_state(void)
50 MigrationIncomingState
*mis
= migration_incoming_get_current();
52 return mis
&& (mis
->state
== MIGRATION_STATUS_COLO
);
55 static bool colo_runstate_is_stopped(void)
57 return runstate_check(RUN_STATE_COLO
) || !runstate_is_running();
60 static void secondary_vm_do_failover(void)
62 /* COLO needs enable block-replication */
63 #ifdef CONFIG_REPLICATION
65 MigrationIncomingState
*mis
= migration_incoming_get_current();
66 Error
*local_err
= NULL
;
68 /* Can not do failover during the process of VM's loading VMstate, Or
69 * it will break the secondary VM.
71 if (vmstate_loading
) {
72 old_state
= failover_set_state(FAILOVER_STATUS_ACTIVE
,
73 FAILOVER_STATUS_RELAUNCH
);
74 if (old_state
!= FAILOVER_STATUS_ACTIVE
) {
75 error_report("Unknown error while do failover for secondary VM,"
76 "old_state: %s", FailoverStatus_str(old_state
));
81 migrate_set_state(&mis
->state
, MIGRATION_STATUS_COLO
,
82 MIGRATION_STATUS_COMPLETED
);
84 replication_stop_all(true, &local_err
);
86 error_report_err(local_err
);
89 /* Notify all filters of all NIC to do checkpoint */
90 colo_notify_filters_event(COLO_EVENT_FAILOVER
, &local_err
);
92 error_report_err(local_err
);
96 error_report("\"-S\" qemu option will be ignored in secondary side");
97 /* recover runstate to normal migration finish state */
101 * Make sure COLO incoming thread not block in recv or send,
102 * If mis->from_src_file and mis->to_src_file use the same fd,
103 * The second shutdown() will return -1, we ignore this value,
106 if (mis
->from_src_file
) {
107 qemu_file_shutdown(mis
->from_src_file
);
109 if (mis
->to_src_file
) {
110 qemu_file_shutdown(mis
->to_src_file
);
113 old_state
= failover_set_state(FAILOVER_STATUS_ACTIVE
,
114 FAILOVER_STATUS_COMPLETED
);
115 if (old_state
!= FAILOVER_STATUS_ACTIVE
) {
116 error_report("Incorrect state (%s) while doing failover for "
117 "secondary VM", FailoverStatus_str(old_state
));
120 /* Notify COLO incoming thread that failover work is finished */
121 qemu_sem_post(&mis
->colo_incoming_sem
);
122 /* For Secondary VM, jump to incoming co */
123 if (mis
->migration_incoming_co
) {
124 qemu_coroutine_enter(mis
->migration_incoming_co
);
131 static void primary_vm_do_failover(void)
133 #ifdef CONFIG_REPLICATION
134 MigrationState
*s
= migrate_get_current();
136 Error
*local_err
= NULL
;
138 migrate_set_state(&s
->state
, MIGRATION_STATUS_COLO
,
139 MIGRATION_STATUS_COMPLETED
);
141 * kick COLO thread which might wait at
142 * qemu_sem_wait(&s->colo_checkpoint_sem).
144 colo_checkpoint_notify(migrate_get_current());
147 * Wake up COLO thread which may blocked in recv() or send(),
148 * The s->rp_state.from_dst_file and s->to_dst_file may use the
149 * same fd, but we still shutdown the fd for twice, it is harmless.
151 if (s
->to_dst_file
) {
152 qemu_file_shutdown(s
->to_dst_file
);
154 if (s
->rp_state
.from_dst_file
) {
155 qemu_file_shutdown(s
->rp_state
.from_dst_file
);
158 old_state
= failover_set_state(FAILOVER_STATUS_ACTIVE
,
159 FAILOVER_STATUS_COMPLETED
);
160 if (old_state
!= FAILOVER_STATUS_ACTIVE
) {
161 error_report("Incorrect state (%s) while doing failover for Primary VM",
162 FailoverStatus_str(old_state
));
166 replication_stop_all(true, &local_err
);
168 error_report_err(local_err
);
172 /* Notify COLO thread that failover work is finished */
173 qemu_sem_post(&s
->colo_exit_sem
);
179 COLOMode
get_colo_mode(void)
181 if (migration_in_colo_state()) {
182 return COLO_MODE_PRIMARY
;
183 } else if (migration_incoming_in_colo_state()) {
184 return COLO_MODE_SECONDARY
;
186 return COLO_MODE_NONE
;
190 void colo_do_failover(MigrationState
*s
)
192 /* Make sure VM stopped while failover happened. */
193 if (!colo_runstate_is_stopped()) {
194 vm_stop_force_state(RUN_STATE_COLO
);
197 if (get_colo_mode() == COLO_MODE_PRIMARY
) {
198 primary_vm_do_failover();
200 secondary_vm_do_failover();
204 void qmp_xen_set_replication(bool enable
, bool primary
,
205 bool has_failover
, bool failover
,
208 #ifdef CONFIG_REPLICATION
209 ReplicationMode mode
= primary
?
210 REPLICATION_MODE_PRIMARY
:
211 REPLICATION_MODE_SECONDARY
;
213 if (has_failover
&& enable
) {
214 error_setg(errp
, "Parameter 'failover' is only for"
215 " stopping replication");
220 replication_start_all(mode
, errp
);
225 replication_stop_all(failover
, failover
? NULL
: errp
);
232 ReplicationStatus
*qmp_query_xen_replication_status(Error
**errp
)
234 #ifdef CONFIG_REPLICATION
236 ReplicationStatus
*s
= g_new0(ReplicationStatus
, 1);
238 replication_get_error_all(&err
);
242 s
->desc
= g_strdup(error_get_pretty(err
));
254 void qmp_xen_colo_do_checkpoint(Error
**errp
)
256 #ifdef CONFIG_REPLICATION
257 replication_do_checkpoint_all(errp
);
263 COLOStatus
*qmp_query_colo_status(Error
**errp
)
265 COLOStatus
*s
= g_new0(COLOStatus
, 1);
267 s
->mode
= get_colo_mode();
269 switch (failover_get_state()) {
270 case FAILOVER_STATUS_NONE
:
271 s
->reason
= COLO_EXIT_REASON_NONE
;
273 case FAILOVER_STATUS_REQUIRE
:
274 s
->reason
= COLO_EXIT_REASON_REQUEST
;
277 s
->reason
= COLO_EXIT_REASON_ERROR
;
283 static void colo_send_message(QEMUFile
*f
, COLOMessage msg
,
288 if (msg
>= COLO_MESSAGE__MAX
) {
289 error_setg(errp
, "%s: Invalid message", __func__
);
292 qemu_put_be32(f
, msg
);
295 ret
= qemu_file_get_error(f
);
297 error_setg_errno(errp
, -ret
, "Can't send COLO message");
299 trace_colo_send_message(COLOMessage_str(msg
));
302 static void colo_send_message_value(QEMUFile
*f
, COLOMessage msg
,
303 uint64_t value
, Error
**errp
)
305 Error
*local_err
= NULL
;
308 colo_send_message(f
, msg
, &local_err
);
310 error_propagate(errp
, local_err
);
313 qemu_put_be64(f
, value
);
316 ret
= qemu_file_get_error(f
);
318 error_setg_errno(errp
, -ret
, "Failed to send value for message:%s",
319 COLOMessage_str(msg
));
323 static COLOMessage
colo_receive_message(QEMUFile
*f
, Error
**errp
)
328 msg
= qemu_get_be32(f
);
329 ret
= qemu_file_get_error(f
);
331 error_setg_errno(errp
, -ret
, "Can't receive COLO message");
334 if (msg
>= COLO_MESSAGE__MAX
) {
335 error_setg(errp
, "%s: Invalid message", __func__
);
338 trace_colo_receive_message(COLOMessage_str(msg
));
342 static void colo_receive_check_message(QEMUFile
*f
, COLOMessage expect_msg
,
346 Error
*local_err
= NULL
;
348 msg
= colo_receive_message(f
, &local_err
);
350 error_propagate(errp
, local_err
);
353 if (msg
!= expect_msg
) {
354 error_setg(errp
, "Unexpected COLO message %d, expected %d",
359 static uint64_t colo_receive_message_value(QEMUFile
*f
, uint32_t expect_msg
,
362 Error
*local_err
= NULL
;
366 colo_receive_check_message(f
, expect_msg
, &local_err
);
368 error_propagate(errp
, local_err
);
372 value
= qemu_get_be64(f
);
373 ret
= qemu_file_get_error(f
);
375 error_setg_errno(errp
, -ret
, "Failed to get value for COLO message: %s",
376 COLOMessage_str(expect_msg
));
381 static int colo_do_checkpoint_transaction(MigrationState
*s
,
382 QIOChannelBuffer
*bioc
,
385 Error
*local_err
= NULL
;
388 colo_send_message(s
->to_dst_file
, COLO_MESSAGE_CHECKPOINT_REQUEST
,
394 colo_receive_check_message(s
->rp_state
.from_dst_file
,
395 COLO_MESSAGE_CHECKPOINT_REPLY
, &local_err
);
399 /* Reset channel-buffer directly */
400 qio_channel_io_seek(QIO_CHANNEL(bioc
), 0, 0, NULL
);
403 qemu_mutex_lock_iothread();
404 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
405 qemu_mutex_unlock_iothread();
408 vm_stop_force_state(RUN_STATE_COLO
);
409 qemu_mutex_unlock_iothread();
410 trace_colo_vm_state_change("run", "stop");
412 * Failover request bh could be called after vm_stop_force_state(),
413 * So we need check failover_request_is_active() again.
415 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
419 colo_notify_compares_event(NULL
, COLO_EVENT_CHECKPOINT
, &local_err
);
424 /* Disable block migration */
425 migrate_set_block_enabled(false, &local_err
);
426 qemu_mutex_lock_iothread();
428 #ifdef CONFIG_REPLICATION
429 replication_do_checkpoint_all(&local_err
);
431 qemu_mutex_unlock_iothread();
438 colo_send_message(s
->to_dst_file
, COLO_MESSAGE_VMSTATE_SEND
, &local_err
);
440 qemu_mutex_unlock_iothread();
443 /* Note: device state is saved into buffer */
444 ret
= qemu_save_device_state(fb
);
446 qemu_mutex_unlock_iothread();
451 * Only save VM's live state, which not including device state.
452 * TODO: We may need a timeout mechanism to prevent COLO process
453 * to be blocked here.
455 qemu_savevm_live_state(s
->to_dst_file
);
460 * We need the size of the VMstate data in Secondary side,
461 * With which we can decide how much data should be read.
463 colo_send_message_value(s
->to_dst_file
, COLO_MESSAGE_VMSTATE_SIZE
,
464 bioc
->usage
, &local_err
);
469 qemu_put_buffer(s
->to_dst_file
, bioc
->data
, bioc
->usage
);
470 qemu_fflush(s
->to_dst_file
);
471 ret
= qemu_file_get_error(s
->to_dst_file
);
476 colo_receive_check_message(s
->rp_state
.from_dst_file
,
477 COLO_MESSAGE_VMSTATE_RECEIVED
, &local_err
);
482 colo_receive_check_message(s
->rp_state
.from_dst_file
,
483 COLO_MESSAGE_VMSTATE_LOADED
, &local_err
);
490 qemu_mutex_lock_iothread();
492 qemu_mutex_unlock_iothread();
493 trace_colo_vm_state_change("stop", "run");
497 error_report_err(local_err
);
502 static void colo_compare_notify_checkpoint(Notifier
*notifier
, void *data
)
504 colo_checkpoint_notify(data
);
507 static void colo_process_checkpoint(MigrationState
*s
)
509 QIOChannelBuffer
*bioc
;
511 int64_t current_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
512 Error
*local_err
= NULL
;
515 failover_init_state();
517 s
->rp_state
.from_dst_file
= qemu_file_get_return_path(s
->to_dst_file
);
518 if (!s
->rp_state
.from_dst_file
) {
519 error_report("Open QEMUFile from_dst_file failed");
523 packets_compare_notifier
.notify
= colo_compare_notify_checkpoint
;
524 colo_compare_register_notifier(&packets_compare_notifier
);
527 * Wait for Secondary finish loading VM states and enter COLO
530 colo_receive_check_message(s
->rp_state
.from_dst_file
,
531 COLO_MESSAGE_CHECKPOINT_READY
, &local_err
);
535 bioc
= qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE
);
536 fb
= qemu_fopen_channel_output(QIO_CHANNEL(bioc
));
537 object_unref(OBJECT(bioc
));
539 qemu_mutex_lock_iothread();
540 #ifdef CONFIG_REPLICATION
541 replication_start_all(REPLICATION_MODE_PRIMARY
, &local_err
);
543 qemu_mutex_unlock_iothread();
551 qemu_mutex_unlock_iothread();
552 trace_colo_vm_state_change("stop", "run");
554 timer_mod(s
->colo_delay_timer
,
555 current_time
+ s
->parameters
.x_checkpoint_delay
);
557 while (s
->state
== MIGRATION_STATUS_COLO
) {
558 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
559 error_report("failover request");
563 qemu_sem_wait(&s
->colo_checkpoint_sem
);
565 if (s
->state
!= MIGRATION_STATUS_COLO
) {
568 ret
= colo_do_checkpoint_transaction(s
, bioc
, fb
);
575 /* Throw the unreported error message after exited from loop */
577 error_report_err(local_err
);
585 * There are only two reasons we can get here, some error happened
586 * or the user triggered failover.
588 switch (failover_get_state()) {
589 case FAILOVER_STATUS_NONE
:
590 qapi_event_send_colo_exit(COLO_MODE_PRIMARY
,
591 COLO_EXIT_REASON_ERROR
);
593 case FAILOVER_STATUS_REQUIRE
:
594 qapi_event_send_colo_exit(COLO_MODE_PRIMARY
,
595 COLO_EXIT_REASON_REQUEST
);
601 /* Hope this not to be too long to wait here */
602 qemu_sem_wait(&s
->colo_exit_sem
);
603 qemu_sem_destroy(&s
->colo_exit_sem
);
606 * It is safe to unregister notifier after failover finished.
607 * Besides, colo_delay_timer and colo_checkpoint_sem can't be
608 * released befor unregister notifier, or there will be use-after-free
611 colo_compare_unregister_notifier(&packets_compare_notifier
);
612 timer_del(s
->colo_delay_timer
);
613 timer_free(s
->colo_delay_timer
);
614 qemu_sem_destroy(&s
->colo_checkpoint_sem
);
617 * Must be called after failover BH is completed,
618 * Or the failover BH may shutdown the wrong fd that
619 * re-used by other threads after we release here.
621 if (s
->rp_state
.from_dst_file
) {
622 qemu_fclose(s
->rp_state
.from_dst_file
);
626 void colo_checkpoint_notify(void *opaque
)
628 MigrationState
*s
= opaque
;
629 int64_t next_notify_time
;
631 qemu_sem_post(&s
->colo_checkpoint_sem
);
632 s
->colo_checkpoint_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
633 next_notify_time
= s
->colo_checkpoint_time
+
634 s
->parameters
.x_checkpoint_delay
;
635 timer_mod(s
->colo_delay_timer
, next_notify_time
);
638 void migrate_start_colo_process(MigrationState
*s
)
640 qemu_mutex_unlock_iothread();
641 qemu_sem_init(&s
->colo_checkpoint_sem
, 0);
642 s
->colo_delay_timer
= timer_new_ms(QEMU_CLOCK_HOST
,
643 colo_checkpoint_notify
, s
);
645 qemu_sem_init(&s
->colo_exit_sem
, 0);
646 migrate_set_state(&s
->state
, MIGRATION_STATUS_ACTIVE
,
647 MIGRATION_STATUS_COLO
);
648 colo_process_checkpoint(s
);
649 qemu_mutex_lock_iothread();
652 static void colo_wait_handle_message(QEMUFile
*f
, int *checkpoint_request
,
656 Error
*local_err
= NULL
;
658 msg
= colo_receive_message(f
, &local_err
);
660 error_propagate(errp
, local_err
);
665 case COLO_MESSAGE_CHECKPOINT_REQUEST
:
666 *checkpoint_request
= 1;
669 *checkpoint_request
= 0;
670 error_setg(errp
, "Got unknown COLO message: %d", msg
);
675 void *colo_process_incoming_thread(void *opaque
)
677 MigrationIncomingState
*mis
= opaque
;
679 QIOChannelBuffer
*bioc
= NULL
; /* Cache incoming device state */
682 Error
*local_err
= NULL
;
685 rcu_register_thread();
686 qemu_sem_init(&mis
->colo_incoming_sem
, 0);
688 migrate_set_state(&mis
->state
, MIGRATION_STATUS_ACTIVE
,
689 MIGRATION_STATUS_COLO
);
691 failover_init_state();
693 mis
->to_src_file
= qemu_file_get_return_path(mis
->from_src_file
);
694 if (!mis
->to_src_file
) {
695 error_report("COLO incoming thread: Open QEMUFile to_src_file failed");
699 * Note: the communication between Primary side and Secondary side
700 * should be sequential, we set the fd to unblocked in migration incoming
701 * coroutine, and here we are in the COLO incoming thread, so it is ok to
702 * set the fd back to blocked.
704 qemu_file_set_blocking(mis
->from_src_file
, true);
706 bioc
= qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE
);
707 fb
= qemu_fopen_channel_input(QIO_CHANNEL(bioc
));
708 object_unref(OBJECT(bioc
));
710 qemu_mutex_lock_iothread();
711 #ifdef CONFIG_REPLICATION
712 replication_start_all(REPLICATION_MODE_SECONDARY
, &local_err
);
714 qemu_mutex_unlock_iothread();
721 trace_colo_vm_state_change("stop", "run");
722 qemu_mutex_unlock_iothread();
724 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_CHECKPOINT_READY
,
730 while (mis
->state
== MIGRATION_STATUS_COLO
) {
733 colo_wait_handle_message(mis
->from_src_file
, &request
, &local_err
);
738 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
739 error_report("failover request");
743 qemu_mutex_lock_iothread();
744 vm_stop_force_state(RUN_STATE_COLO
);
745 trace_colo_vm_state_change("run", "stop");
746 qemu_mutex_unlock_iothread();
748 /* FIXME: This is unnecessary for periodic checkpoint mode */
749 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_CHECKPOINT_REPLY
,
755 colo_receive_check_message(mis
->from_src_file
,
756 COLO_MESSAGE_VMSTATE_SEND
, &local_err
);
761 qemu_mutex_lock_iothread();
762 cpu_synchronize_all_pre_loadvm();
763 ret
= qemu_loadvm_state_main(mis
->from_src_file
, mis
);
764 qemu_mutex_unlock_iothread();
767 error_report("Load VM's live state (ram) error");
771 value
= colo_receive_message_value(mis
->from_src_file
,
772 COLO_MESSAGE_VMSTATE_SIZE
, &local_err
);
778 * Read VM device state data into channel buffer,
779 * It's better to re-use the memory allocated.
780 * Here we need to handle the channel buffer directly.
782 if (value
> bioc
->capacity
) {
783 bioc
->capacity
= value
;
784 bioc
->data
= g_realloc(bioc
->data
, bioc
->capacity
);
786 total_size
= qemu_get_buffer(mis
->from_src_file
, bioc
->data
, value
);
787 if (total_size
!= value
) {
788 error_report("Got %" PRIu64
" VMState data, less than expected"
789 " %" PRIu64
, total_size
, value
);
792 bioc
->usage
= total_size
;
793 qio_channel_io_seek(QIO_CHANNEL(bioc
), 0, 0, NULL
);
795 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_VMSTATE_RECEIVED
,
801 qemu_mutex_lock_iothread();
802 vmstate_loading
= true;
803 ret
= qemu_load_device_state(fb
);
805 error_report("COLO: load device state failed");
806 qemu_mutex_unlock_iothread();
810 #ifdef CONFIG_REPLICATION
811 replication_get_error_all(&local_err
);
813 qemu_mutex_unlock_iothread();
817 /* discard colo disk buffer */
818 replication_do_checkpoint_all(&local_err
);
820 qemu_mutex_unlock_iothread();
826 /* Notify all filters of all NIC to do checkpoint */
827 colo_notify_filters_event(COLO_EVENT_CHECKPOINT
, &local_err
);
830 qemu_mutex_unlock_iothread();
834 vmstate_loading
= false;
836 trace_colo_vm_state_change("stop", "run");
837 qemu_mutex_unlock_iothread();
839 if (failover_get_state() == FAILOVER_STATUS_RELAUNCH
) {
840 failover_set_state(FAILOVER_STATUS_RELAUNCH
,
841 FAILOVER_STATUS_NONE
);
842 failover_request_active(NULL
);
846 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_VMSTATE_LOADED
,
854 vmstate_loading
= false;
855 /* Throw the unreported error message after exited from loop */
857 error_report_err(local_err
);
860 switch (failover_get_state()) {
861 case FAILOVER_STATUS_NONE
:
862 qapi_event_send_colo_exit(COLO_MODE_SECONDARY
,
863 COLO_EXIT_REASON_ERROR
);
865 case FAILOVER_STATUS_REQUIRE
:
866 qapi_event_send_colo_exit(COLO_MODE_SECONDARY
,
867 COLO_EXIT_REASON_REQUEST
);
877 /* Hope this not to be too long to loop here */
878 qemu_sem_wait(&mis
->colo_incoming_sem
);
879 qemu_sem_destroy(&mis
->colo_incoming_sem
);
880 /* Must be called after failover BH is completed */
881 if (mis
->to_src_file
) {
882 qemu_fclose(mis
->to_src_file
);
884 migration_incoming_disable_colo();
886 rcu_unregister_thread();