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 "qemu/main-loop.h"
28 #include "migration/failover.h"
29 #ifdef CONFIG_REPLICATION
30 #include "replication.h"
32 #include "net/colo-compare.h"
34 #include "block/block.h"
35 #include "qapi/qapi-events-migration.h"
36 #include "qapi/qmp/qerror.h"
37 #include "sysemu/cpus.h"
38 #include "net/filter.h"
40 static bool vmstate_loading
;
41 static Notifier packets_compare_notifier
;
43 /* User need to know colo mode after COLO failover */
44 static COLOMode last_colo_mode
;
46 #define COLO_BUFFER_BASE_SIZE (4 * 1024 * 1024)
48 bool migration_in_colo_state(void)
50 MigrationState
*s
= migrate_get_current();
52 return (s
->state
== MIGRATION_STATUS_COLO
);
55 bool migration_incoming_in_colo_state(void)
57 MigrationIncomingState
*mis
= migration_incoming_get_current();
59 return mis
&& (mis
->state
== MIGRATION_STATUS_COLO
);
62 static bool colo_runstate_is_stopped(void)
64 return runstate_check(RUN_STATE_COLO
) || !runstate_is_running();
67 static void secondary_vm_do_failover(void)
69 /* COLO needs enable block-replication */
70 #ifdef CONFIG_REPLICATION
72 MigrationIncomingState
*mis
= migration_incoming_get_current();
73 Error
*local_err
= NULL
;
75 /* Can not do failover during the process of VM's loading VMstate, Or
76 * it will break the secondary VM.
78 if (vmstate_loading
) {
79 old_state
= failover_set_state(FAILOVER_STATUS_ACTIVE
,
80 FAILOVER_STATUS_RELAUNCH
);
81 if (old_state
!= FAILOVER_STATUS_ACTIVE
) {
82 error_report("Unknown error while do failover for secondary VM,"
83 "old_state: %s", FailoverStatus_str(old_state
));
88 migrate_set_state(&mis
->state
, MIGRATION_STATUS_COLO
,
89 MIGRATION_STATUS_COMPLETED
);
91 replication_stop_all(true, &local_err
);
93 error_report_err(local_err
);
96 /* Notify all filters of all NIC to do checkpoint */
97 colo_notify_filters_event(COLO_EVENT_FAILOVER
, &local_err
);
99 error_report_err(local_err
);
103 error_report("\"-S\" qemu option will be ignored in secondary side");
104 /* recover runstate to normal migration finish state */
108 * Make sure COLO incoming thread not block in recv or send,
109 * If mis->from_src_file and mis->to_src_file use the same fd,
110 * The second shutdown() will return -1, we ignore this value,
113 if (mis
->from_src_file
) {
114 qemu_file_shutdown(mis
->from_src_file
);
116 if (mis
->to_src_file
) {
117 qemu_file_shutdown(mis
->to_src_file
);
120 old_state
= failover_set_state(FAILOVER_STATUS_ACTIVE
,
121 FAILOVER_STATUS_COMPLETED
);
122 if (old_state
!= FAILOVER_STATUS_ACTIVE
) {
123 error_report("Incorrect state (%s) while doing failover for "
124 "secondary VM", FailoverStatus_str(old_state
));
127 /* Notify COLO incoming thread that failover work is finished */
128 qemu_sem_post(&mis
->colo_incoming_sem
);
130 /* For Secondary VM, jump to incoming co */
131 if (mis
->migration_incoming_co
) {
132 qemu_coroutine_enter(mis
->migration_incoming_co
);
139 static void primary_vm_do_failover(void)
141 #ifdef CONFIG_REPLICATION
142 MigrationState
*s
= migrate_get_current();
144 Error
*local_err
= NULL
;
146 migrate_set_state(&s
->state
, MIGRATION_STATUS_COLO
,
147 MIGRATION_STATUS_COMPLETED
);
149 * kick COLO thread which might wait at
150 * qemu_sem_wait(&s->colo_checkpoint_sem).
152 colo_checkpoint_notify(migrate_get_current());
155 * Wake up COLO thread which may blocked in recv() or send(),
156 * The s->rp_state.from_dst_file and s->to_dst_file may use the
157 * same fd, but we still shutdown the fd for twice, it is harmless.
159 if (s
->to_dst_file
) {
160 qemu_file_shutdown(s
->to_dst_file
);
162 if (s
->rp_state
.from_dst_file
) {
163 qemu_file_shutdown(s
->rp_state
.from_dst_file
);
166 old_state
= failover_set_state(FAILOVER_STATUS_ACTIVE
,
167 FAILOVER_STATUS_COMPLETED
);
168 if (old_state
!= FAILOVER_STATUS_ACTIVE
) {
169 error_report("Incorrect state (%s) while doing failover for Primary VM",
170 FailoverStatus_str(old_state
));
174 replication_stop_all(true, &local_err
);
176 error_report_err(local_err
);
180 /* Notify COLO thread that failover work is finished */
181 qemu_sem_post(&s
->colo_exit_sem
);
187 COLOMode
get_colo_mode(void)
189 if (migration_in_colo_state()) {
190 return COLO_MODE_PRIMARY
;
191 } else if (migration_incoming_in_colo_state()) {
192 return COLO_MODE_SECONDARY
;
194 return COLO_MODE_NONE
;
198 void colo_do_failover(void)
200 /* Make sure VM stopped while failover happened. */
201 if (!colo_runstate_is_stopped()) {
202 vm_stop_force_state(RUN_STATE_COLO
);
205 switch (get_colo_mode()) {
206 case COLO_MODE_PRIMARY
:
207 primary_vm_do_failover();
209 case COLO_MODE_SECONDARY
:
210 secondary_vm_do_failover();
213 error_report("colo_do_failover failed because the colo mode"
214 " could not be obtained");
218 #ifdef CONFIG_REPLICATION
219 void qmp_xen_set_replication(bool enable
, bool primary
,
220 bool has_failover
, bool failover
,
223 ReplicationMode mode
= primary
?
224 REPLICATION_MODE_PRIMARY
:
225 REPLICATION_MODE_SECONDARY
;
227 if (has_failover
&& enable
) {
228 error_setg(errp
, "Parameter 'failover' is only for"
229 " stopping replication");
234 replication_start_all(mode
, errp
);
239 replication_stop_all(failover
, failover
? NULL
: errp
);
243 ReplicationStatus
*qmp_query_xen_replication_status(Error
**errp
)
246 ReplicationStatus
*s
= g_new0(ReplicationStatus
, 1);
248 replication_get_error_all(&err
);
252 s
->desc
= g_strdup(error_get_pretty(err
));
261 void qmp_xen_colo_do_checkpoint(Error
**errp
)
263 replication_do_checkpoint_all(errp
);
264 /* Notify all filters of all NIC to do checkpoint */
265 colo_notify_filters_event(COLO_EVENT_CHECKPOINT
, errp
);
269 COLOStatus
*qmp_query_colo_status(Error
**errp
)
271 COLOStatus
*s
= g_new0(COLOStatus
, 1);
273 s
->mode
= get_colo_mode();
274 s
->last_mode
= last_colo_mode
;
276 switch (failover_get_state()) {
277 case FAILOVER_STATUS_NONE
:
278 s
->reason
= COLO_EXIT_REASON_NONE
;
280 case FAILOVER_STATUS_COMPLETED
:
281 s
->reason
= COLO_EXIT_REASON_REQUEST
;
284 if (migration_in_colo_state()) {
285 s
->reason
= COLO_EXIT_REASON_PROCESSING
;
287 s
->reason
= COLO_EXIT_REASON_ERROR
;
294 static void colo_send_message(QEMUFile
*f
, COLOMessage msg
,
299 if (msg
>= COLO_MESSAGE__MAX
) {
300 error_setg(errp
, "%s: Invalid message", __func__
);
303 qemu_put_be32(f
, msg
);
306 ret
= qemu_file_get_error(f
);
308 error_setg_errno(errp
, -ret
, "Can't send COLO message");
310 trace_colo_send_message(COLOMessage_str(msg
));
313 static void colo_send_message_value(QEMUFile
*f
, COLOMessage msg
,
314 uint64_t value
, Error
**errp
)
316 Error
*local_err
= NULL
;
319 colo_send_message(f
, msg
, &local_err
);
321 error_propagate(errp
, local_err
);
324 qemu_put_be64(f
, value
);
327 ret
= qemu_file_get_error(f
);
329 error_setg_errno(errp
, -ret
, "Failed to send value for message:%s",
330 COLOMessage_str(msg
));
334 static COLOMessage
colo_receive_message(QEMUFile
*f
, Error
**errp
)
339 msg
= qemu_get_be32(f
);
340 ret
= qemu_file_get_error(f
);
342 error_setg_errno(errp
, -ret
, "Can't receive COLO message");
345 if (msg
>= COLO_MESSAGE__MAX
) {
346 error_setg(errp
, "%s: Invalid message", __func__
);
349 trace_colo_receive_message(COLOMessage_str(msg
));
353 static void colo_receive_check_message(QEMUFile
*f
, COLOMessage expect_msg
,
357 Error
*local_err
= NULL
;
359 msg
= colo_receive_message(f
, &local_err
);
361 error_propagate(errp
, local_err
);
364 if (msg
!= expect_msg
) {
365 error_setg(errp
, "Unexpected COLO message %d, expected %d",
370 static uint64_t colo_receive_message_value(QEMUFile
*f
, uint32_t expect_msg
,
373 Error
*local_err
= NULL
;
377 colo_receive_check_message(f
, expect_msg
, &local_err
);
379 error_propagate(errp
, local_err
);
383 value
= qemu_get_be64(f
);
384 ret
= qemu_file_get_error(f
);
386 error_setg_errno(errp
, -ret
, "Failed to get value for COLO message: %s",
387 COLOMessage_str(expect_msg
));
392 static int colo_do_checkpoint_transaction(MigrationState
*s
,
393 QIOChannelBuffer
*bioc
,
396 Error
*local_err
= NULL
;
399 colo_send_message(s
->to_dst_file
, COLO_MESSAGE_CHECKPOINT_REQUEST
,
405 colo_receive_check_message(s
->rp_state
.from_dst_file
,
406 COLO_MESSAGE_CHECKPOINT_REPLY
, &local_err
);
410 /* Reset channel-buffer directly */
411 qio_channel_io_seek(QIO_CHANNEL(bioc
), 0, 0, NULL
);
414 qemu_mutex_lock_iothread();
415 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
416 qemu_mutex_unlock_iothread();
419 vm_stop_force_state(RUN_STATE_COLO
);
420 qemu_mutex_unlock_iothread();
421 trace_colo_vm_state_change("run", "stop");
423 * Failover request bh could be called after vm_stop_force_state(),
424 * So we need check failover_request_is_active() again.
426 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
430 colo_notify_compares_event(NULL
, COLO_EVENT_CHECKPOINT
, &local_err
);
435 /* Disable block migration */
436 migrate_set_block_enabled(false, &local_err
);
437 qemu_mutex_lock_iothread();
439 #ifdef CONFIG_REPLICATION
440 replication_do_checkpoint_all(&local_err
);
442 qemu_mutex_unlock_iothread();
449 colo_send_message(s
->to_dst_file
, COLO_MESSAGE_VMSTATE_SEND
, &local_err
);
451 qemu_mutex_unlock_iothread();
454 /* Note: device state is saved into buffer */
455 ret
= qemu_save_device_state(fb
);
457 qemu_mutex_unlock_iothread();
462 * Only save VM's live state, which not including device state.
463 * TODO: We may need a timeout mechanism to prevent COLO process
464 * to be blocked here.
466 qemu_savevm_live_state(s
->to_dst_file
);
471 * We need the size of the VMstate data in Secondary side,
472 * With which we can decide how much data should be read.
474 colo_send_message_value(s
->to_dst_file
, COLO_MESSAGE_VMSTATE_SIZE
,
475 bioc
->usage
, &local_err
);
480 qemu_put_buffer(s
->to_dst_file
, bioc
->data
, bioc
->usage
);
481 qemu_fflush(s
->to_dst_file
);
482 ret
= qemu_file_get_error(s
->to_dst_file
);
487 colo_receive_check_message(s
->rp_state
.from_dst_file
,
488 COLO_MESSAGE_VMSTATE_RECEIVED
, &local_err
);
493 colo_receive_check_message(s
->rp_state
.from_dst_file
,
494 COLO_MESSAGE_VMSTATE_LOADED
, &local_err
);
501 qemu_mutex_lock_iothread();
503 qemu_mutex_unlock_iothread();
504 trace_colo_vm_state_change("stop", "run");
508 error_report_err(local_err
);
513 static void colo_compare_notify_checkpoint(Notifier
*notifier
, void *data
)
515 colo_checkpoint_notify(data
);
518 static void colo_process_checkpoint(MigrationState
*s
)
520 QIOChannelBuffer
*bioc
;
522 int64_t current_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
523 Error
*local_err
= NULL
;
526 last_colo_mode
= get_colo_mode();
527 if (last_colo_mode
!= COLO_MODE_PRIMARY
) {
528 error_report("COLO mode must be COLO_MODE_PRIMARY");
532 failover_init_state();
534 s
->rp_state
.from_dst_file
= qemu_file_get_return_path(s
->to_dst_file
);
535 if (!s
->rp_state
.from_dst_file
) {
536 error_report("Open QEMUFile from_dst_file failed");
540 packets_compare_notifier
.notify
= colo_compare_notify_checkpoint
;
541 colo_compare_register_notifier(&packets_compare_notifier
);
544 * Wait for Secondary finish loading VM states and enter COLO
547 colo_receive_check_message(s
->rp_state
.from_dst_file
,
548 COLO_MESSAGE_CHECKPOINT_READY
, &local_err
);
552 bioc
= qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE
);
553 fb
= qemu_fopen_channel_output(QIO_CHANNEL(bioc
));
554 object_unref(OBJECT(bioc
));
556 qemu_mutex_lock_iothread();
557 #ifdef CONFIG_REPLICATION
558 replication_start_all(REPLICATION_MODE_PRIMARY
, &local_err
);
560 qemu_mutex_unlock_iothread();
568 qemu_mutex_unlock_iothread();
569 trace_colo_vm_state_change("stop", "run");
571 timer_mod(s
->colo_delay_timer
,
572 current_time
+ s
->parameters
.x_checkpoint_delay
);
574 while (s
->state
== MIGRATION_STATUS_COLO
) {
575 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
576 error_report("failover request");
580 qemu_sem_wait(&s
->colo_checkpoint_sem
);
582 if (s
->state
!= MIGRATION_STATUS_COLO
) {
585 ret
= colo_do_checkpoint_transaction(s
, bioc
, fb
);
592 /* Throw the unreported error message after exited from loop */
594 error_report_err(local_err
);
602 * There are only two reasons we can get here, some error happened
603 * or the user triggered failover.
605 switch (failover_get_state()) {
606 case FAILOVER_STATUS_COMPLETED
:
607 qapi_event_send_colo_exit(COLO_MODE_PRIMARY
,
608 COLO_EXIT_REASON_REQUEST
);
611 qapi_event_send_colo_exit(COLO_MODE_PRIMARY
,
612 COLO_EXIT_REASON_ERROR
);
615 /* Hope this not to be too long to wait here */
616 qemu_sem_wait(&s
->colo_exit_sem
);
617 qemu_sem_destroy(&s
->colo_exit_sem
);
620 * It is safe to unregister notifier after failover finished.
621 * Besides, colo_delay_timer and colo_checkpoint_sem can't be
622 * released befor unregister notifier, or there will be use-after-free
625 colo_compare_unregister_notifier(&packets_compare_notifier
);
626 timer_del(s
->colo_delay_timer
);
627 timer_free(s
->colo_delay_timer
);
628 qemu_sem_destroy(&s
->colo_checkpoint_sem
);
631 * Must be called after failover BH is completed,
632 * Or the failover BH may shutdown the wrong fd that
633 * re-used by other threads after we release here.
635 if (s
->rp_state
.from_dst_file
) {
636 qemu_fclose(s
->rp_state
.from_dst_file
);
640 void colo_checkpoint_notify(void *opaque
)
642 MigrationState
*s
= opaque
;
643 int64_t next_notify_time
;
645 qemu_sem_post(&s
->colo_checkpoint_sem
);
646 s
->colo_checkpoint_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
647 next_notify_time
= s
->colo_checkpoint_time
+
648 s
->parameters
.x_checkpoint_delay
;
649 timer_mod(s
->colo_delay_timer
, next_notify_time
);
652 void migrate_start_colo_process(MigrationState
*s
)
654 qemu_mutex_unlock_iothread();
655 qemu_sem_init(&s
->colo_checkpoint_sem
, 0);
656 s
->colo_delay_timer
= timer_new_ms(QEMU_CLOCK_HOST
,
657 colo_checkpoint_notify
, s
);
659 qemu_sem_init(&s
->colo_exit_sem
, 0);
660 migrate_set_state(&s
->state
, MIGRATION_STATUS_ACTIVE
,
661 MIGRATION_STATUS_COLO
);
662 colo_process_checkpoint(s
);
663 qemu_mutex_lock_iothread();
666 static void colo_wait_handle_message(QEMUFile
*f
, int *checkpoint_request
,
670 Error
*local_err
= NULL
;
672 msg
= colo_receive_message(f
, &local_err
);
674 error_propagate(errp
, local_err
);
679 case COLO_MESSAGE_CHECKPOINT_REQUEST
:
680 *checkpoint_request
= 1;
683 *checkpoint_request
= 0;
684 error_setg(errp
, "Got unknown COLO message: %d", msg
);
689 void *colo_process_incoming_thread(void *opaque
)
691 MigrationIncomingState
*mis
= opaque
;
693 QIOChannelBuffer
*bioc
= NULL
; /* Cache incoming device state */
696 Error
*local_err
= NULL
;
699 rcu_register_thread();
700 qemu_sem_init(&mis
->colo_incoming_sem
, 0);
702 migrate_set_state(&mis
->state
, MIGRATION_STATUS_ACTIVE
,
703 MIGRATION_STATUS_COLO
);
705 last_colo_mode
= get_colo_mode();
706 if (last_colo_mode
!= COLO_MODE_SECONDARY
) {
707 error_report("COLO mode must be COLO_MODE_SECONDARY");
711 failover_init_state();
713 mis
->to_src_file
= qemu_file_get_return_path(mis
->from_src_file
);
714 if (!mis
->to_src_file
) {
715 error_report("COLO incoming thread: Open QEMUFile to_src_file failed");
719 * Note: the communication between Primary side and Secondary side
720 * should be sequential, we set the fd to unblocked in migration incoming
721 * coroutine, and here we are in the COLO incoming thread, so it is ok to
722 * set the fd back to blocked.
724 qemu_file_set_blocking(mis
->from_src_file
, true);
726 bioc
= qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE
);
727 fb
= qemu_fopen_channel_input(QIO_CHANNEL(bioc
));
728 object_unref(OBJECT(bioc
));
730 qemu_mutex_lock_iothread();
731 #ifdef CONFIG_REPLICATION
732 replication_start_all(REPLICATION_MODE_SECONDARY
, &local_err
);
734 qemu_mutex_unlock_iothread();
741 trace_colo_vm_state_change("stop", "run");
742 qemu_mutex_unlock_iothread();
744 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_CHECKPOINT_READY
,
750 while (mis
->state
== MIGRATION_STATUS_COLO
) {
753 colo_wait_handle_message(mis
->from_src_file
, &request
, &local_err
);
758 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
759 error_report("failover request");
763 qemu_mutex_lock_iothread();
764 vm_stop_force_state(RUN_STATE_COLO
);
765 trace_colo_vm_state_change("run", "stop");
766 qemu_mutex_unlock_iothread();
768 /* FIXME: This is unnecessary for periodic checkpoint mode */
769 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_CHECKPOINT_REPLY
,
775 colo_receive_check_message(mis
->from_src_file
,
776 COLO_MESSAGE_VMSTATE_SEND
, &local_err
);
781 qemu_mutex_lock_iothread();
782 cpu_synchronize_all_pre_loadvm();
783 ret
= qemu_loadvm_state_main(mis
->from_src_file
, mis
);
784 qemu_mutex_unlock_iothread();
787 error_report("Load VM's live state (ram) error");
791 value
= colo_receive_message_value(mis
->from_src_file
,
792 COLO_MESSAGE_VMSTATE_SIZE
, &local_err
);
798 * Read VM device state data into channel buffer,
799 * It's better to re-use the memory allocated.
800 * Here we need to handle the channel buffer directly.
802 if (value
> bioc
->capacity
) {
803 bioc
->capacity
= value
;
804 bioc
->data
= g_realloc(bioc
->data
, bioc
->capacity
);
806 total_size
= qemu_get_buffer(mis
->from_src_file
, bioc
->data
, value
);
807 if (total_size
!= value
) {
808 error_report("Got %" PRIu64
" VMState data, less than expected"
809 " %" PRIu64
, total_size
, value
);
812 bioc
->usage
= total_size
;
813 qio_channel_io_seek(QIO_CHANNEL(bioc
), 0, 0, NULL
);
815 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_VMSTATE_RECEIVED
,
821 qemu_mutex_lock_iothread();
822 vmstate_loading
= true;
823 ret
= qemu_load_device_state(fb
);
825 error_report("COLO: load device state failed");
826 qemu_mutex_unlock_iothread();
830 #ifdef CONFIG_REPLICATION
831 replication_get_error_all(&local_err
);
833 qemu_mutex_unlock_iothread();
837 /* discard colo disk buffer */
838 replication_do_checkpoint_all(&local_err
);
840 qemu_mutex_unlock_iothread();
846 /* Notify all filters of all NIC to do checkpoint */
847 colo_notify_filters_event(COLO_EVENT_CHECKPOINT
, &local_err
);
850 qemu_mutex_unlock_iothread();
854 vmstate_loading
= false;
856 trace_colo_vm_state_change("stop", "run");
857 qemu_mutex_unlock_iothread();
859 if (failover_get_state() == FAILOVER_STATUS_RELAUNCH
) {
860 failover_set_state(FAILOVER_STATUS_RELAUNCH
,
861 FAILOVER_STATUS_NONE
);
862 failover_request_active(NULL
);
866 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_VMSTATE_LOADED
,
874 vmstate_loading
= false;
875 /* Throw the unreported error message after exited from loop */
877 error_report_err(local_err
);
881 * There are only two reasons we can get here, some error happened
882 * or the user triggered failover.
884 switch (failover_get_state()) {
885 case FAILOVER_STATUS_COMPLETED
:
886 qapi_event_send_colo_exit(COLO_MODE_SECONDARY
,
887 COLO_EXIT_REASON_REQUEST
);
890 qapi_event_send_colo_exit(COLO_MODE_SECONDARY
,
891 COLO_EXIT_REASON_ERROR
);
898 /* Hope this not to be too long to loop here */
899 qemu_sem_wait(&mis
->colo_incoming_sem
);
900 qemu_sem_destroy(&mis
->colo_incoming_sem
);
901 /* Must be called after failover BH is completed */
902 if (mis
->to_src_file
) {
903 qemu_fclose(mis
->to_src_file
);
904 mis
->to_src_file
= NULL
;
907 rcu_unregister_thread();