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 /* User need to know colo mode after COLO failover */
42 static COLOMode last_colo_mode
;
44 #define COLO_BUFFER_BASE_SIZE (4 * 1024 * 1024)
46 bool migration_in_colo_state(void)
48 MigrationState
*s
= migrate_get_current();
50 return (s
->state
== MIGRATION_STATUS_COLO
);
53 bool migration_incoming_in_colo_state(void)
55 MigrationIncomingState
*mis
= migration_incoming_get_current();
57 return mis
&& (mis
->state
== MIGRATION_STATUS_COLO
);
60 static bool colo_runstate_is_stopped(void)
62 return runstate_check(RUN_STATE_COLO
) || !runstate_is_running();
65 static void secondary_vm_do_failover(void)
67 /* COLO needs enable block-replication */
68 #ifdef CONFIG_REPLICATION
70 MigrationIncomingState
*mis
= migration_incoming_get_current();
71 Error
*local_err
= NULL
;
73 /* Can not do failover during the process of VM's loading VMstate, Or
74 * it will break the secondary VM.
76 if (vmstate_loading
) {
77 old_state
= failover_set_state(FAILOVER_STATUS_ACTIVE
,
78 FAILOVER_STATUS_RELAUNCH
);
79 if (old_state
!= FAILOVER_STATUS_ACTIVE
) {
80 error_report("Unknown error while do failover for secondary VM,"
81 "old_state: %s", FailoverStatus_str(old_state
));
86 migrate_set_state(&mis
->state
, MIGRATION_STATUS_COLO
,
87 MIGRATION_STATUS_COMPLETED
);
89 replication_stop_all(true, &local_err
);
91 error_report_err(local_err
);
94 /* Notify all filters of all NIC to do checkpoint */
95 colo_notify_filters_event(COLO_EVENT_FAILOVER
, &local_err
);
97 error_report_err(local_err
);
101 error_report("\"-S\" qemu option will be ignored in secondary side");
102 /* recover runstate to normal migration finish state */
106 * Make sure COLO incoming thread not block in recv or send,
107 * If mis->from_src_file and mis->to_src_file use the same fd,
108 * The second shutdown() will return -1, we ignore this value,
111 if (mis
->from_src_file
) {
112 qemu_file_shutdown(mis
->from_src_file
);
114 if (mis
->to_src_file
) {
115 qemu_file_shutdown(mis
->to_src_file
);
118 old_state
= failover_set_state(FAILOVER_STATUS_ACTIVE
,
119 FAILOVER_STATUS_COMPLETED
);
120 if (old_state
!= FAILOVER_STATUS_ACTIVE
) {
121 error_report("Incorrect state (%s) while doing failover for "
122 "secondary VM", FailoverStatus_str(old_state
));
125 /* Notify COLO incoming thread that failover work is finished */
126 qemu_sem_post(&mis
->colo_incoming_sem
);
128 /* For Secondary VM, jump to incoming co */
129 if (mis
->migration_incoming_co
) {
130 qemu_coroutine_enter(mis
->migration_incoming_co
);
137 static void primary_vm_do_failover(void)
139 #ifdef CONFIG_REPLICATION
140 MigrationState
*s
= migrate_get_current();
142 Error
*local_err
= NULL
;
144 migrate_set_state(&s
->state
, MIGRATION_STATUS_COLO
,
145 MIGRATION_STATUS_COMPLETED
);
147 * kick COLO thread which might wait at
148 * qemu_sem_wait(&s->colo_checkpoint_sem).
150 colo_checkpoint_notify(migrate_get_current());
153 * Wake up COLO thread which may blocked in recv() or send(),
154 * The s->rp_state.from_dst_file and s->to_dst_file may use the
155 * same fd, but we still shutdown the fd for twice, it is harmless.
157 if (s
->to_dst_file
) {
158 qemu_file_shutdown(s
->to_dst_file
);
160 if (s
->rp_state
.from_dst_file
) {
161 qemu_file_shutdown(s
->rp_state
.from_dst_file
);
164 old_state
= failover_set_state(FAILOVER_STATUS_ACTIVE
,
165 FAILOVER_STATUS_COMPLETED
);
166 if (old_state
!= FAILOVER_STATUS_ACTIVE
) {
167 error_report("Incorrect state (%s) while doing failover for Primary VM",
168 FailoverStatus_str(old_state
));
172 replication_stop_all(true, &local_err
);
174 error_report_err(local_err
);
178 /* Notify COLO thread that failover work is finished */
179 qemu_sem_post(&s
->colo_exit_sem
);
185 COLOMode
get_colo_mode(void)
187 if (migration_in_colo_state()) {
188 return COLO_MODE_PRIMARY
;
189 } else if (migration_incoming_in_colo_state()) {
190 return COLO_MODE_SECONDARY
;
192 return COLO_MODE_NONE
;
196 void colo_do_failover(void)
198 /* Make sure VM stopped while failover happened. */
199 if (!colo_runstate_is_stopped()) {
200 vm_stop_force_state(RUN_STATE_COLO
);
203 switch (get_colo_mode()) {
204 case COLO_MODE_PRIMARY
:
205 primary_vm_do_failover();
207 case COLO_MODE_SECONDARY
:
208 secondary_vm_do_failover();
211 error_report("colo_do_failover failed because the colo mode"
212 " could not be obtained");
216 #ifdef CONFIG_REPLICATION
217 void qmp_xen_set_replication(bool enable
, bool primary
,
218 bool has_failover
, bool failover
,
221 ReplicationMode mode
= primary
?
222 REPLICATION_MODE_PRIMARY
:
223 REPLICATION_MODE_SECONDARY
;
225 if (has_failover
&& enable
) {
226 error_setg(errp
, "Parameter 'failover' is only for"
227 " stopping replication");
232 replication_start_all(mode
, errp
);
237 replication_stop_all(failover
, failover
? NULL
: errp
);
241 ReplicationStatus
*qmp_query_xen_replication_status(Error
**errp
)
244 ReplicationStatus
*s
= g_new0(ReplicationStatus
, 1);
246 replication_get_error_all(&err
);
250 s
->desc
= g_strdup(error_get_pretty(err
));
259 void qmp_xen_colo_do_checkpoint(Error
**errp
)
261 replication_do_checkpoint_all(errp
);
265 COLOStatus
*qmp_query_colo_status(Error
**errp
)
267 COLOStatus
*s
= g_new0(COLOStatus
, 1);
269 s
->mode
= get_colo_mode();
270 s
->last_mode
= last_colo_mode
;
272 switch (failover_get_state()) {
273 case FAILOVER_STATUS_NONE
:
274 s
->reason
= COLO_EXIT_REASON_NONE
;
276 case FAILOVER_STATUS_COMPLETED
:
277 s
->reason
= COLO_EXIT_REASON_REQUEST
;
280 if (migration_in_colo_state()) {
281 s
->reason
= COLO_EXIT_REASON_PROCESSING
;
283 s
->reason
= COLO_EXIT_REASON_ERROR
;
290 static void colo_send_message(QEMUFile
*f
, COLOMessage msg
,
295 if (msg
>= COLO_MESSAGE__MAX
) {
296 error_setg(errp
, "%s: Invalid message", __func__
);
299 qemu_put_be32(f
, msg
);
302 ret
= qemu_file_get_error(f
);
304 error_setg_errno(errp
, -ret
, "Can't send COLO message");
306 trace_colo_send_message(COLOMessage_str(msg
));
309 static void colo_send_message_value(QEMUFile
*f
, COLOMessage msg
,
310 uint64_t value
, Error
**errp
)
312 Error
*local_err
= NULL
;
315 colo_send_message(f
, msg
, &local_err
);
317 error_propagate(errp
, local_err
);
320 qemu_put_be64(f
, value
);
323 ret
= qemu_file_get_error(f
);
325 error_setg_errno(errp
, -ret
, "Failed to send value for message:%s",
326 COLOMessage_str(msg
));
330 static COLOMessage
colo_receive_message(QEMUFile
*f
, Error
**errp
)
335 msg
= qemu_get_be32(f
);
336 ret
= qemu_file_get_error(f
);
338 error_setg_errno(errp
, -ret
, "Can't receive COLO message");
341 if (msg
>= COLO_MESSAGE__MAX
) {
342 error_setg(errp
, "%s: Invalid message", __func__
);
345 trace_colo_receive_message(COLOMessage_str(msg
));
349 static void colo_receive_check_message(QEMUFile
*f
, COLOMessage expect_msg
,
353 Error
*local_err
= NULL
;
355 msg
= colo_receive_message(f
, &local_err
);
357 error_propagate(errp
, local_err
);
360 if (msg
!= expect_msg
) {
361 error_setg(errp
, "Unexpected COLO message %d, expected %d",
366 static uint64_t colo_receive_message_value(QEMUFile
*f
, uint32_t expect_msg
,
369 Error
*local_err
= NULL
;
373 colo_receive_check_message(f
, expect_msg
, &local_err
);
375 error_propagate(errp
, local_err
);
379 value
= qemu_get_be64(f
);
380 ret
= qemu_file_get_error(f
);
382 error_setg_errno(errp
, -ret
, "Failed to get value for COLO message: %s",
383 COLOMessage_str(expect_msg
));
388 static int colo_do_checkpoint_transaction(MigrationState
*s
,
389 QIOChannelBuffer
*bioc
,
392 Error
*local_err
= NULL
;
395 colo_send_message(s
->to_dst_file
, COLO_MESSAGE_CHECKPOINT_REQUEST
,
401 colo_receive_check_message(s
->rp_state
.from_dst_file
,
402 COLO_MESSAGE_CHECKPOINT_REPLY
, &local_err
);
406 /* Reset channel-buffer directly */
407 qio_channel_io_seek(QIO_CHANNEL(bioc
), 0, 0, NULL
);
410 qemu_mutex_lock_iothread();
411 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
412 qemu_mutex_unlock_iothread();
415 vm_stop_force_state(RUN_STATE_COLO
);
416 qemu_mutex_unlock_iothread();
417 trace_colo_vm_state_change("run", "stop");
419 * Failover request bh could be called after vm_stop_force_state(),
420 * So we need check failover_request_is_active() again.
422 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
426 colo_notify_compares_event(NULL
, COLO_EVENT_CHECKPOINT
, &local_err
);
431 /* Disable block migration */
432 migrate_set_block_enabled(false, &local_err
);
433 qemu_mutex_lock_iothread();
435 #ifdef CONFIG_REPLICATION
436 replication_do_checkpoint_all(&local_err
);
438 qemu_mutex_unlock_iothread();
445 colo_send_message(s
->to_dst_file
, COLO_MESSAGE_VMSTATE_SEND
, &local_err
);
447 qemu_mutex_unlock_iothread();
450 /* Note: device state is saved into buffer */
451 ret
= qemu_save_device_state(fb
);
453 qemu_mutex_unlock_iothread();
458 * Only save VM's live state, which not including device state.
459 * TODO: We may need a timeout mechanism to prevent COLO process
460 * to be blocked here.
462 qemu_savevm_live_state(s
->to_dst_file
);
467 * We need the size of the VMstate data in Secondary side,
468 * With which we can decide how much data should be read.
470 colo_send_message_value(s
->to_dst_file
, COLO_MESSAGE_VMSTATE_SIZE
,
471 bioc
->usage
, &local_err
);
476 qemu_put_buffer(s
->to_dst_file
, bioc
->data
, bioc
->usage
);
477 qemu_fflush(s
->to_dst_file
);
478 ret
= qemu_file_get_error(s
->to_dst_file
);
483 colo_receive_check_message(s
->rp_state
.from_dst_file
,
484 COLO_MESSAGE_VMSTATE_RECEIVED
, &local_err
);
489 colo_receive_check_message(s
->rp_state
.from_dst_file
,
490 COLO_MESSAGE_VMSTATE_LOADED
, &local_err
);
497 qemu_mutex_lock_iothread();
499 qemu_mutex_unlock_iothread();
500 trace_colo_vm_state_change("stop", "run");
504 error_report_err(local_err
);
509 static void colo_compare_notify_checkpoint(Notifier
*notifier
, void *data
)
511 colo_checkpoint_notify(data
);
514 static void colo_process_checkpoint(MigrationState
*s
)
516 QIOChannelBuffer
*bioc
;
518 int64_t current_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
519 Error
*local_err
= NULL
;
522 last_colo_mode
= get_colo_mode();
523 if (last_colo_mode
!= COLO_MODE_PRIMARY
) {
524 error_report("COLO mode must be COLO_MODE_PRIMARY");
528 failover_init_state();
530 s
->rp_state
.from_dst_file
= qemu_file_get_return_path(s
->to_dst_file
);
531 if (!s
->rp_state
.from_dst_file
) {
532 error_report("Open QEMUFile from_dst_file failed");
536 packets_compare_notifier
.notify
= colo_compare_notify_checkpoint
;
537 colo_compare_register_notifier(&packets_compare_notifier
);
540 * Wait for Secondary finish loading VM states and enter COLO
543 colo_receive_check_message(s
->rp_state
.from_dst_file
,
544 COLO_MESSAGE_CHECKPOINT_READY
, &local_err
);
548 bioc
= qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE
);
549 fb
= qemu_fopen_channel_output(QIO_CHANNEL(bioc
));
550 object_unref(OBJECT(bioc
));
552 qemu_mutex_lock_iothread();
553 #ifdef CONFIG_REPLICATION
554 replication_start_all(REPLICATION_MODE_PRIMARY
, &local_err
);
556 qemu_mutex_unlock_iothread();
564 qemu_mutex_unlock_iothread();
565 trace_colo_vm_state_change("stop", "run");
567 timer_mod(s
->colo_delay_timer
,
568 current_time
+ s
->parameters
.x_checkpoint_delay
);
570 while (s
->state
== MIGRATION_STATUS_COLO
) {
571 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
572 error_report("failover request");
576 qemu_sem_wait(&s
->colo_checkpoint_sem
);
578 if (s
->state
!= MIGRATION_STATUS_COLO
) {
581 ret
= colo_do_checkpoint_transaction(s
, bioc
, fb
);
588 /* Throw the unreported error message after exited from loop */
590 error_report_err(local_err
);
598 * There are only two reasons we can get here, some error happened
599 * or the user triggered failover.
601 switch (failover_get_state()) {
602 case FAILOVER_STATUS_COMPLETED
:
603 qapi_event_send_colo_exit(COLO_MODE_PRIMARY
,
604 COLO_EXIT_REASON_REQUEST
);
607 qapi_event_send_colo_exit(COLO_MODE_PRIMARY
,
608 COLO_EXIT_REASON_ERROR
);
611 /* Hope this not to be too long to wait here */
612 qemu_sem_wait(&s
->colo_exit_sem
);
613 qemu_sem_destroy(&s
->colo_exit_sem
);
616 * It is safe to unregister notifier after failover finished.
617 * Besides, colo_delay_timer and colo_checkpoint_sem can't be
618 * released befor unregister notifier, or there will be use-after-free
621 colo_compare_unregister_notifier(&packets_compare_notifier
);
622 timer_del(s
->colo_delay_timer
);
623 timer_free(s
->colo_delay_timer
);
624 qemu_sem_destroy(&s
->colo_checkpoint_sem
);
627 * Must be called after failover BH is completed,
628 * Or the failover BH may shutdown the wrong fd that
629 * re-used by other threads after we release here.
631 if (s
->rp_state
.from_dst_file
) {
632 qemu_fclose(s
->rp_state
.from_dst_file
);
636 void colo_checkpoint_notify(void *opaque
)
638 MigrationState
*s
= opaque
;
639 int64_t next_notify_time
;
641 qemu_sem_post(&s
->colo_checkpoint_sem
);
642 s
->colo_checkpoint_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
643 next_notify_time
= s
->colo_checkpoint_time
+
644 s
->parameters
.x_checkpoint_delay
;
645 timer_mod(s
->colo_delay_timer
, next_notify_time
);
648 void migrate_start_colo_process(MigrationState
*s
)
650 qemu_mutex_unlock_iothread();
651 qemu_sem_init(&s
->colo_checkpoint_sem
, 0);
652 s
->colo_delay_timer
= timer_new_ms(QEMU_CLOCK_HOST
,
653 colo_checkpoint_notify
, s
);
655 qemu_sem_init(&s
->colo_exit_sem
, 0);
656 migrate_set_state(&s
->state
, MIGRATION_STATUS_ACTIVE
,
657 MIGRATION_STATUS_COLO
);
658 colo_process_checkpoint(s
);
659 qemu_mutex_lock_iothread();
662 static void colo_wait_handle_message(QEMUFile
*f
, int *checkpoint_request
,
666 Error
*local_err
= NULL
;
668 msg
= colo_receive_message(f
, &local_err
);
670 error_propagate(errp
, local_err
);
675 case COLO_MESSAGE_CHECKPOINT_REQUEST
:
676 *checkpoint_request
= 1;
679 *checkpoint_request
= 0;
680 error_setg(errp
, "Got unknown COLO message: %d", msg
);
685 void *colo_process_incoming_thread(void *opaque
)
687 MigrationIncomingState
*mis
= opaque
;
689 QIOChannelBuffer
*bioc
= NULL
; /* Cache incoming device state */
692 Error
*local_err
= NULL
;
695 rcu_register_thread();
696 qemu_sem_init(&mis
->colo_incoming_sem
, 0);
698 migrate_set_state(&mis
->state
, MIGRATION_STATUS_ACTIVE
,
699 MIGRATION_STATUS_COLO
);
701 last_colo_mode
= get_colo_mode();
702 if (last_colo_mode
!= COLO_MODE_SECONDARY
) {
703 error_report("COLO mode must be COLO_MODE_SECONDARY");
707 failover_init_state();
709 mis
->to_src_file
= qemu_file_get_return_path(mis
->from_src_file
);
710 if (!mis
->to_src_file
) {
711 error_report("COLO incoming thread: Open QEMUFile to_src_file failed");
715 * Note: the communication between Primary side and Secondary side
716 * should be sequential, we set the fd to unblocked in migration incoming
717 * coroutine, and here we are in the COLO incoming thread, so it is ok to
718 * set the fd back to blocked.
720 qemu_file_set_blocking(mis
->from_src_file
, true);
722 bioc
= qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE
);
723 fb
= qemu_fopen_channel_input(QIO_CHANNEL(bioc
));
724 object_unref(OBJECT(bioc
));
726 qemu_mutex_lock_iothread();
727 #ifdef CONFIG_REPLICATION
728 replication_start_all(REPLICATION_MODE_SECONDARY
, &local_err
);
730 qemu_mutex_unlock_iothread();
737 trace_colo_vm_state_change("stop", "run");
738 qemu_mutex_unlock_iothread();
740 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_CHECKPOINT_READY
,
746 while (mis
->state
== MIGRATION_STATUS_COLO
) {
749 colo_wait_handle_message(mis
->from_src_file
, &request
, &local_err
);
754 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
755 error_report("failover request");
759 qemu_mutex_lock_iothread();
760 vm_stop_force_state(RUN_STATE_COLO
);
761 trace_colo_vm_state_change("run", "stop");
762 qemu_mutex_unlock_iothread();
764 /* FIXME: This is unnecessary for periodic checkpoint mode */
765 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_CHECKPOINT_REPLY
,
771 colo_receive_check_message(mis
->from_src_file
,
772 COLO_MESSAGE_VMSTATE_SEND
, &local_err
);
777 qemu_mutex_lock_iothread();
778 cpu_synchronize_all_pre_loadvm();
779 ret
= qemu_loadvm_state_main(mis
->from_src_file
, mis
);
780 qemu_mutex_unlock_iothread();
783 error_report("Load VM's live state (ram) error");
787 value
= colo_receive_message_value(mis
->from_src_file
,
788 COLO_MESSAGE_VMSTATE_SIZE
, &local_err
);
794 * Read VM device state data into channel buffer,
795 * It's better to re-use the memory allocated.
796 * Here we need to handle the channel buffer directly.
798 if (value
> bioc
->capacity
) {
799 bioc
->capacity
= value
;
800 bioc
->data
= g_realloc(bioc
->data
, bioc
->capacity
);
802 total_size
= qemu_get_buffer(mis
->from_src_file
, bioc
->data
, value
);
803 if (total_size
!= value
) {
804 error_report("Got %" PRIu64
" VMState data, less than expected"
805 " %" PRIu64
, total_size
, value
);
808 bioc
->usage
= total_size
;
809 qio_channel_io_seek(QIO_CHANNEL(bioc
), 0, 0, NULL
);
811 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_VMSTATE_RECEIVED
,
817 qemu_mutex_lock_iothread();
818 vmstate_loading
= true;
819 ret
= qemu_load_device_state(fb
);
821 error_report("COLO: load device state failed");
822 qemu_mutex_unlock_iothread();
826 #ifdef CONFIG_REPLICATION
827 replication_get_error_all(&local_err
);
829 qemu_mutex_unlock_iothread();
833 /* discard colo disk buffer */
834 replication_do_checkpoint_all(&local_err
);
836 qemu_mutex_unlock_iothread();
842 /* Notify all filters of all NIC to do checkpoint */
843 colo_notify_filters_event(COLO_EVENT_CHECKPOINT
, &local_err
);
846 qemu_mutex_unlock_iothread();
850 vmstate_loading
= false;
852 trace_colo_vm_state_change("stop", "run");
853 qemu_mutex_unlock_iothread();
855 if (failover_get_state() == FAILOVER_STATUS_RELAUNCH
) {
856 failover_set_state(FAILOVER_STATUS_RELAUNCH
,
857 FAILOVER_STATUS_NONE
);
858 failover_request_active(NULL
);
862 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_VMSTATE_LOADED
,
870 vmstate_loading
= false;
871 /* Throw the unreported error message after exited from loop */
873 error_report_err(local_err
);
877 * There are only two reasons we can get here, some error happened
878 * or the user triggered failover.
880 switch (failover_get_state()) {
881 case FAILOVER_STATUS_COMPLETED
:
882 qapi_event_send_colo_exit(COLO_MODE_SECONDARY
,
883 COLO_EXIT_REASON_REQUEST
);
886 qapi_event_send_colo_exit(COLO_MODE_SECONDARY
,
887 COLO_EXIT_REASON_ERROR
);
894 /* Hope this not to be too long to loop here */
895 qemu_sem_wait(&mis
->colo_incoming_sem
);
896 qemu_sem_destroy(&mis
->colo_incoming_sem
);
897 /* Must be called after failover BH is completed */
898 if (mis
->to_src_file
) {
899 qemu_fclose(mis
->to_src_file
);
900 mis
->to_src_file
= NULL
;
903 rcu_unregister_thread();