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"
33 static bool vmstate_loading
;
34 static Notifier packets_compare_notifier
;
36 #define COLO_BUFFER_BASE_SIZE (4 * 1024 * 1024)
38 bool migration_in_colo_state(void)
40 MigrationState
*s
= migrate_get_current();
42 return (s
->state
== MIGRATION_STATUS_COLO
);
45 bool migration_incoming_in_colo_state(void)
47 MigrationIncomingState
*mis
= migration_incoming_get_current();
49 return mis
&& (mis
->state
== MIGRATION_STATUS_COLO
);
52 static bool colo_runstate_is_stopped(void)
54 return runstate_check(RUN_STATE_COLO
) || !runstate_is_running();
57 static void secondary_vm_do_failover(void)
60 MigrationIncomingState
*mis
= migration_incoming_get_current();
61 Error
*local_err
= NULL
;
63 /* Can not do failover during the process of VM's loading VMstate, Or
64 * it will break the secondary VM.
66 if (vmstate_loading
) {
67 old_state
= failover_set_state(FAILOVER_STATUS_ACTIVE
,
68 FAILOVER_STATUS_RELAUNCH
);
69 if (old_state
!= FAILOVER_STATUS_ACTIVE
) {
70 error_report("Unknown error while do failover for secondary VM,"
71 "old_state: %s", FailoverStatus_str(old_state
));
76 migrate_set_state(&mis
->state
, MIGRATION_STATUS_COLO
,
77 MIGRATION_STATUS_COMPLETED
);
79 replication_stop_all(true, &local_err
);
81 error_report_err(local_err
);
85 error_report("\"-S\" qemu option will be ignored in secondary side");
86 /* recover runstate to normal migration finish state */
90 * Make sure COLO incoming thread not block in recv or send,
91 * If mis->from_src_file and mis->to_src_file use the same fd,
92 * The second shutdown() will return -1, we ignore this value,
95 if (mis
->from_src_file
) {
96 qemu_file_shutdown(mis
->from_src_file
);
98 if (mis
->to_src_file
) {
99 qemu_file_shutdown(mis
->to_src_file
);
102 old_state
= failover_set_state(FAILOVER_STATUS_ACTIVE
,
103 FAILOVER_STATUS_COMPLETED
);
104 if (old_state
!= FAILOVER_STATUS_ACTIVE
) {
105 error_report("Incorrect state (%s) while doing failover for "
106 "secondary VM", FailoverStatus_str(old_state
));
109 /* Notify COLO incoming thread that failover work is finished */
110 qemu_sem_post(&mis
->colo_incoming_sem
);
111 /* For Secondary VM, jump to incoming co */
112 if (mis
->migration_incoming_co
) {
113 qemu_coroutine_enter(mis
->migration_incoming_co
);
117 static void primary_vm_do_failover(void)
119 MigrationState
*s
= migrate_get_current();
121 Error
*local_err
= NULL
;
123 migrate_set_state(&s
->state
, MIGRATION_STATUS_COLO
,
124 MIGRATION_STATUS_COMPLETED
);
127 * Wake up COLO thread which may blocked in recv() or send(),
128 * The s->rp_state.from_dst_file and s->to_dst_file may use the
129 * same fd, but we still shutdown the fd for twice, it is harmless.
131 if (s
->to_dst_file
) {
132 qemu_file_shutdown(s
->to_dst_file
);
134 if (s
->rp_state
.from_dst_file
) {
135 qemu_file_shutdown(s
->rp_state
.from_dst_file
);
138 old_state
= failover_set_state(FAILOVER_STATUS_ACTIVE
,
139 FAILOVER_STATUS_COMPLETED
);
140 if (old_state
!= FAILOVER_STATUS_ACTIVE
) {
141 error_report("Incorrect state (%s) while doing failover for Primary VM",
142 FailoverStatus_str(old_state
));
146 replication_stop_all(true, &local_err
);
148 error_report_err(local_err
);
152 /* Notify COLO thread that failover work is finished */
153 qemu_sem_post(&s
->colo_exit_sem
);
156 COLOMode
get_colo_mode(void)
158 if (migration_in_colo_state()) {
159 return COLO_MODE_PRIMARY
;
160 } else if (migration_incoming_in_colo_state()) {
161 return COLO_MODE_SECONDARY
;
163 return COLO_MODE_NONE
;
167 void colo_do_failover(MigrationState
*s
)
169 /* Make sure VM stopped while failover happened. */
170 if (!colo_runstate_is_stopped()) {
171 vm_stop_force_state(RUN_STATE_COLO
);
174 if (get_colo_mode() == COLO_MODE_PRIMARY
) {
175 primary_vm_do_failover();
177 secondary_vm_do_failover();
181 void qmp_xen_set_replication(bool enable
, bool primary
,
182 bool has_failover
, bool failover
,
185 #ifdef CONFIG_REPLICATION
186 ReplicationMode mode
= primary
?
187 REPLICATION_MODE_PRIMARY
:
188 REPLICATION_MODE_SECONDARY
;
190 if (has_failover
&& enable
) {
191 error_setg(errp
, "Parameter 'failover' is only for"
192 " stopping replication");
197 replication_start_all(mode
, errp
);
202 replication_stop_all(failover
, failover
? NULL
: errp
);
209 ReplicationStatus
*qmp_query_xen_replication_status(Error
**errp
)
211 #ifdef CONFIG_REPLICATION
213 ReplicationStatus
*s
= g_new0(ReplicationStatus
, 1);
215 replication_get_error_all(&err
);
219 s
->desc
= g_strdup(error_get_pretty(err
));
231 void qmp_xen_colo_do_checkpoint(Error
**errp
)
233 #ifdef CONFIG_REPLICATION
234 replication_do_checkpoint_all(errp
);
240 static void colo_send_message(QEMUFile
*f
, COLOMessage msg
,
245 if (msg
>= COLO_MESSAGE__MAX
) {
246 error_setg(errp
, "%s: Invalid message", __func__
);
249 qemu_put_be32(f
, msg
);
252 ret
= qemu_file_get_error(f
);
254 error_setg_errno(errp
, -ret
, "Can't send COLO message");
256 trace_colo_send_message(COLOMessage_str(msg
));
259 static void colo_send_message_value(QEMUFile
*f
, COLOMessage msg
,
260 uint64_t value
, Error
**errp
)
262 Error
*local_err
= NULL
;
265 colo_send_message(f
, msg
, &local_err
);
267 error_propagate(errp
, local_err
);
270 qemu_put_be64(f
, value
);
273 ret
= qemu_file_get_error(f
);
275 error_setg_errno(errp
, -ret
, "Failed to send value for message:%s",
276 COLOMessage_str(msg
));
280 static COLOMessage
colo_receive_message(QEMUFile
*f
, Error
**errp
)
285 msg
= qemu_get_be32(f
);
286 ret
= qemu_file_get_error(f
);
288 error_setg_errno(errp
, -ret
, "Can't receive COLO message");
291 if (msg
>= COLO_MESSAGE__MAX
) {
292 error_setg(errp
, "%s: Invalid message", __func__
);
295 trace_colo_receive_message(COLOMessage_str(msg
));
299 static void colo_receive_check_message(QEMUFile
*f
, COLOMessage expect_msg
,
303 Error
*local_err
= NULL
;
305 msg
= colo_receive_message(f
, &local_err
);
307 error_propagate(errp
, local_err
);
310 if (msg
!= expect_msg
) {
311 error_setg(errp
, "Unexpected COLO message %d, expected %d",
316 static uint64_t colo_receive_message_value(QEMUFile
*f
, uint32_t expect_msg
,
319 Error
*local_err
= NULL
;
323 colo_receive_check_message(f
, expect_msg
, &local_err
);
325 error_propagate(errp
, local_err
);
329 value
= qemu_get_be64(f
);
330 ret
= qemu_file_get_error(f
);
332 error_setg_errno(errp
, -ret
, "Failed to get value for COLO message: %s",
333 COLOMessage_str(expect_msg
));
338 static int colo_do_checkpoint_transaction(MigrationState
*s
,
339 QIOChannelBuffer
*bioc
,
342 Error
*local_err
= NULL
;
345 colo_send_message(s
->to_dst_file
, COLO_MESSAGE_CHECKPOINT_REQUEST
,
351 colo_receive_check_message(s
->rp_state
.from_dst_file
,
352 COLO_MESSAGE_CHECKPOINT_REPLY
, &local_err
);
356 /* Reset channel-buffer directly */
357 qio_channel_io_seek(QIO_CHANNEL(bioc
), 0, 0, NULL
);
360 qemu_mutex_lock_iothread();
361 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
362 qemu_mutex_unlock_iothread();
365 vm_stop_force_state(RUN_STATE_COLO
);
366 qemu_mutex_unlock_iothread();
367 trace_colo_vm_state_change("run", "stop");
369 * Failover request bh could be called after vm_stop_force_state(),
370 * So we need check failover_request_is_active() again.
372 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
376 colo_notify_compares_event(NULL
, COLO_EVENT_CHECKPOINT
, &local_err
);
381 /* Disable block migration */
382 migrate_set_block_enabled(false, &local_err
);
383 qemu_savevm_state_header(fb
);
384 qemu_savevm_state_setup(fb
);
385 qemu_mutex_lock_iothread();
386 replication_do_checkpoint_all(&local_err
);
388 qemu_mutex_unlock_iothread();
391 qemu_savevm_state_complete_precopy(fb
, false, false);
392 qemu_mutex_unlock_iothread();
396 colo_send_message(s
->to_dst_file
, COLO_MESSAGE_VMSTATE_SEND
, &local_err
);
401 * We need the size of the VMstate data in Secondary side,
402 * With which we can decide how much data should be read.
404 colo_send_message_value(s
->to_dst_file
, COLO_MESSAGE_VMSTATE_SIZE
,
405 bioc
->usage
, &local_err
);
410 qemu_put_buffer(s
->to_dst_file
, bioc
->data
, bioc
->usage
);
411 qemu_fflush(s
->to_dst_file
);
412 ret
= qemu_file_get_error(s
->to_dst_file
);
417 colo_receive_check_message(s
->rp_state
.from_dst_file
,
418 COLO_MESSAGE_VMSTATE_RECEIVED
, &local_err
);
423 colo_receive_check_message(s
->rp_state
.from_dst_file
,
424 COLO_MESSAGE_VMSTATE_LOADED
, &local_err
);
431 qemu_mutex_lock_iothread();
433 qemu_mutex_unlock_iothread();
434 trace_colo_vm_state_change("stop", "run");
438 error_report_err(local_err
);
443 static void colo_compare_notify_checkpoint(Notifier
*notifier
, void *data
)
445 colo_checkpoint_notify(data
);
448 static void colo_process_checkpoint(MigrationState
*s
)
450 QIOChannelBuffer
*bioc
;
452 int64_t current_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
453 Error
*local_err
= NULL
;
456 failover_init_state();
458 s
->rp_state
.from_dst_file
= qemu_file_get_return_path(s
->to_dst_file
);
459 if (!s
->rp_state
.from_dst_file
) {
460 error_report("Open QEMUFile from_dst_file failed");
464 packets_compare_notifier
.notify
= colo_compare_notify_checkpoint
;
465 colo_compare_register_notifier(&packets_compare_notifier
);
468 * Wait for Secondary finish loading VM states and enter COLO
471 colo_receive_check_message(s
->rp_state
.from_dst_file
,
472 COLO_MESSAGE_CHECKPOINT_READY
, &local_err
);
476 bioc
= qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE
);
477 fb
= qemu_fopen_channel_output(QIO_CHANNEL(bioc
));
478 object_unref(OBJECT(bioc
));
480 qemu_mutex_lock_iothread();
481 replication_start_all(REPLICATION_MODE_PRIMARY
, &local_err
);
483 qemu_mutex_unlock_iothread();
488 qemu_mutex_unlock_iothread();
489 trace_colo_vm_state_change("stop", "run");
491 timer_mod(s
->colo_delay_timer
,
492 current_time
+ s
->parameters
.x_checkpoint_delay
);
494 while (s
->state
== MIGRATION_STATUS_COLO
) {
495 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
496 error_report("failover request");
500 qemu_sem_wait(&s
->colo_checkpoint_sem
);
502 ret
= colo_do_checkpoint_transaction(s
, bioc
, fb
);
509 /* Throw the unreported error message after exited from loop */
511 error_report_err(local_err
);
519 * There are only two reasons we can get here, some error happened
520 * or the user triggered failover.
522 switch (failover_get_state()) {
523 case FAILOVER_STATUS_NONE
:
524 qapi_event_send_colo_exit(COLO_MODE_PRIMARY
,
525 COLO_EXIT_REASON_ERROR
);
527 case FAILOVER_STATUS_REQUIRE
:
528 qapi_event_send_colo_exit(COLO_MODE_PRIMARY
,
529 COLO_EXIT_REASON_REQUEST
);
535 /* Hope this not to be too long to wait here */
536 qemu_sem_wait(&s
->colo_exit_sem
);
537 qemu_sem_destroy(&s
->colo_exit_sem
);
540 * It is safe to unregister notifier after failover finished.
541 * Besides, colo_delay_timer and colo_checkpoint_sem can't be
542 * released befor unregister notifier, or there will be use-after-free
545 colo_compare_unregister_notifier(&packets_compare_notifier
);
546 timer_del(s
->colo_delay_timer
);
547 timer_free(s
->colo_delay_timer
);
548 qemu_sem_destroy(&s
->colo_checkpoint_sem
);
551 * Must be called after failover BH is completed,
552 * Or the failover BH may shutdown the wrong fd that
553 * re-used by other threads after we release here.
555 if (s
->rp_state
.from_dst_file
) {
556 qemu_fclose(s
->rp_state
.from_dst_file
);
560 void colo_checkpoint_notify(void *opaque
)
562 MigrationState
*s
= opaque
;
563 int64_t next_notify_time
;
565 qemu_sem_post(&s
->colo_checkpoint_sem
);
566 s
->colo_checkpoint_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
567 next_notify_time
= s
->colo_checkpoint_time
+
568 s
->parameters
.x_checkpoint_delay
;
569 timer_mod(s
->colo_delay_timer
, next_notify_time
);
572 void migrate_start_colo_process(MigrationState
*s
)
574 qemu_mutex_unlock_iothread();
575 qemu_sem_init(&s
->colo_checkpoint_sem
, 0);
576 s
->colo_delay_timer
= timer_new_ms(QEMU_CLOCK_HOST
,
577 colo_checkpoint_notify
, s
);
579 qemu_sem_init(&s
->colo_exit_sem
, 0);
580 migrate_set_state(&s
->state
, MIGRATION_STATUS_ACTIVE
,
581 MIGRATION_STATUS_COLO
);
582 colo_process_checkpoint(s
);
583 qemu_mutex_lock_iothread();
586 static void colo_wait_handle_message(QEMUFile
*f
, int *checkpoint_request
,
590 Error
*local_err
= NULL
;
592 msg
= colo_receive_message(f
, &local_err
);
594 error_propagate(errp
, local_err
);
599 case COLO_MESSAGE_CHECKPOINT_REQUEST
:
600 *checkpoint_request
= 1;
603 *checkpoint_request
= 0;
604 error_setg(errp
, "Got unknown COLO message: %d", msg
);
609 void *colo_process_incoming_thread(void *opaque
)
611 MigrationIncomingState
*mis
= opaque
;
613 QIOChannelBuffer
*bioc
= NULL
; /* Cache incoming device state */
616 Error
*local_err
= NULL
;
618 rcu_register_thread();
619 qemu_sem_init(&mis
->colo_incoming_sem
, 0);
621 migrate_set_state(&mis
->state
, MIGRATION_STATUS_ACTIVE
,
622 MIGRATION_STATUS_COLO
);
624 failover_init_state();
626 mis
->to_src_file
= qemu_file_get_return_path(mis
->from_src_file
);
627 if (!mis
->to_src_file
) {
628 error_report("COLO incoming thread: Open QEMUFile to_src_file failed");
632 * Note: the communication between Primary side and Secondary side
633 * should be sequential, we set the fd to unblocked in migration incoming
634 * coroutine, and here we are in the COLO incoming thread, so it is ok to
635 * set the fd back to blocked.
637 qemu_file_set_blocking(mis
->from_src_file
, true);
639 bioc
= qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE
);
640 fb
= qemu_fopen_channel_input(QIO_CHANNEL(bioc
));
641 object_unref(OBJECT(bioc
));
643 qemu_mutex_lock_iothread();
644 replication_start_all(REPLICATION_MODE_SECONDARY
, &local_err
);
646 qemu_mutex_unlock_iothread();
650 trace_colo_vm_state_change("stop", "run");
651 qemu_mutex_unlock_iothread();
653 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_CHECKPOINT_READY
,
659 while (mis
->state
== MIGRATION_STATUS_COLO
) {
662 colo_wait_handle_message(mis
->from_src_file
, &request
, &local_err
);
667 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
668 error_report("failover request");
672 qemu_mutex_lock_iothread();
673 vm_stop_force_state(RUN_STATE_COLO
);
674 trace_colo_vm_state_change("run", "stop");
675 qemu_mutex_unlock_iothread();
677 /* FIXME: This is unnecessary for periodic checkpoint mode */
678 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_CHECKPOINT_REPLY
,
684 colo_receive_check_message(mis
->from_src_file
,
685 COLO_MESSAGE_VMSTATE_SEND
, &local_err
);
690 value
= colo_receive_message_value(mis
->from_src_file
,
691 COLO_MESSAGE_VMSTATE_SIZE
, &local_err
);
697 * Read VM device state data into channel buffer,
698 * It's better to re-use the memory allocated.
699 * Here we need to handle the channel buffer directly.
701 if (value
> bioc
->capacity
) {
702 bioc
->capacity
= value
;
703 bioc
->data
= g_realloc(bioc
->data
, bioc
->capacity
);
705 total_size
= qemu_get_buffer(mis
->from_src_file
, bioc
->data
, value
);
706 if (total_size
!= value
) {
707 error_report("Got %" PRIu64
" VMState data, less than expected"
708 " %" PRIu64
, total_size
, value
);
711 bioc
->usage
= total_size
;
712 qio_channel_io_seek(QIO_CHANNEL(bioc
), 0, 0, NULL
);
714 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_VMSTATE_RECEIVED
,
720 qemu_mutex_lock_iothread();
721 qemu_system_reset(SHUTDOWN_CAUSE_NONE
);
722 vmstate_loading
= true;
723 if (qemu_loadvm_state(fb
) < 0) {
724 error_report("COLO: loadvm failed");
725 qemu_mutex_unlock_iothread();
729 replication_get_error_all(&local_err
);
731 qemu_mutex_unlock_iothread();
734 /* discard colo disk buffer */
735 replication_do_checkpoint_all(&local_err
);
737 qemu_mutex_unlock_iothread();
741 vmstate_loading
= false;
743 trace_colo_vm_state_change("stop", "run");
744 qemu_mutex_unlock_iothread();
746 if (failover_get_state() == FAILOVER_STATUS_RELAUNCH
) {
747 failover_set_state(FAILOVER_STATUS_RELAUNCH
,
748 FAILOVER_STATUS_NONE
);
749 failover_request_active(NULL
);
753 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_VMSTATE_LOADED
,
761 vmstate_loading
= false;
762 /* Throw the unreported error message after exited from loop */
764 error_report_err(local_err
);
767 switch (failover_get_state()) {
768 case FAILOVER_STATUS_NONE
:
769 qapi_event_send_colo_exit(COLO_MODE_SECONDARY
,
770 COLO_EXIT_REASON_ERROR
);
772 case FAILOVER_STATUS_REQUIRE
:
773 qapi_event_send_colo_exit(COLO_MODE_SECONDARY
,
774 COLO_EXIT_REASON_REQUEST
);
784 /* Hope this not to be too long to loop here */
785 qemu_sem_wait(&mis
->colo_incoming_sem
);
786 qemu_sem_destroy(&mis
->colo_incoming_sem
);
787 /* Must be called after failover BH is completed */
788 if (mis
->to_src_file
) {
789 qemu_fclose(mis
->to_src_file
);
791 migration_incoming_disable_colo();
793 rcu_unregister_thread();