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"
32 static bool vmstate_loading
;
33 static Notifier packets_compare_notifier
;
35 #define COLO_BUFFER_BASE_SIZE (4 * 1024 * 1024)
37 bool migration_in_colo_state(void)
39 MigrationState
*s
= migrate_get_current();
41 return (s
->state
== MIGRATION_STATUS_COLO
);
44 bool migration_incoming_in_colo_state(void)
46 MigrationIncomingState
*mis
= migration_incoming_get_current();
48 return mis
&& (mis
->state
== MIGRATION_STATUS_COLO
);
51 static bool colo_runstate_is_stopped(void)
53 return runstate_check(RUN_STATE_COLO
) || !runstate_is_running();
56 static void secondary_vm_do_failover(void)
59 MigrationIncomingState
*mis
= migration_incoming_get_current();
60 Error
*local_err
= NULL
;
62 /* Can not do failover during the process of VM's loading VMstate, Or
63 * it will break the secondary VM.
65 if (vmstate_loading
) {
66 old_state
= failover_set_state(FAILOVER_STATUS_ACTIVE
,
67 FAILOVER_STATUS_RELAUNCH
);
68 if (old_state
!= FAILOVER_STATUS_ACTIVE
) {
69 error_report("Unknown error while do failover for secondary VM,"
70 "old_state: %s", FailoverStatus_str(old_state
));
75 migrate_set_state(&mis
->state
, MIGRATION_STATUS_COLO
,
76 MIGRATION_STATUS_COMPLETED
);
78 replication_stop_all(true, &local_err
);
80 error_report_err(local_err
);
84 error_report("\"-S\" qemu option will be ignored in secondary side");
85 /* recover runstate to normal migration finish state */
89 * Make sure COLO incoming thread not block in recv or send,
90 * If mis->from_src_file and mis->to_src_file use the same fd,
91 * The second shutdown() will return -1, we ignore this value,
94 if (mis
->from_src_file
) {
95 qemu_file_shutdown(mis
->from_src_file
);
97 if (mis
->to_src_file
) {
98 qemu_file_shutdown(mis
->to_src_file
);
101 old_state
= failover_set_state(FAILOVER_STATUS_ACTIVE
,
102 FAILOVER_STATUS_COMPLETED
);
103 if (old_state
!= FAILOVER_STATUS_ACTIVE
) {
104 error_report("Incorrect state (%s) while doing failover for "
105 "secondary VM", FailoverStatus_str(old_state
));
108 /* Notify COLO incoming thread that failover work is finished */
109 qemu_sem_post(&mis
->colo_incoming_sem
);
110 /* For Secondary VM, jump to incoming co */
111 if (mis
->migration_incoming_co
) {
112 qemu_coroutine_enter(mis
->migration_incoming_co
);
116 static void primary_vm_do_failover(void)
118 MigrationState
*s
= migrate_get_current();
120 Error
*local_err
= NULL
;
122 migrate_set_state(&s
->state
, MIGRATION_STATUS_COLO
,
123 MIGRATION_STATUS_COMPLETED
);
126 * Wake up COLO thread which may blocked in recv() or send(),
127 * The s->rp_state.from_dst_file and s->to_dst_file may use the
128 * same fd, but we still shutdown the fd for twice, it is harmless.
130 if (s
->to_dst_file
) {
131 qemu_file_shutdown(s
->to_dst_file
);
133 if (s
->rp_state
.from_dst_file
) {
134 qemu_file_shutdown(s
->rp_state
.from_dst_file
);
137 old_state
= failover_set_state(FAILOVER_STATUS_ACTIVE
,
138 FAILOVER_STATUS_COMPLETED
);
139 if (old_state
!= FAILOVER_STATUS_ACTIVE
) {
140 error_report("Incorrect state (%s) while doing failover for Primary VM",
141 FailoverStatus_str(old_state
));
145 replication_stop_all(true, &local_err
);
147 error_report_err(local_err
);
151 /* Notify COLO thread that failover work is finished */
152 qemu_sem_post(&s
->colo_exit_sem
);
155 void colo_do_failover(MigrationState
*s
)
157 /* Make sure VM stopped while failover happened. */
158 if (!colo_runstate_is_stopped()) {
159 vm_stop_force_state(RUN_STATE_COLO
);
162 if (get_colo_mode() == COLO_MODE_PRIMARY
) {
163 primary_vm_do_failover();
165 secondary_vm_do_failover();
169 void qmp_xen_set_replication(bool enable
, bool primary
,
170 bool has_failover
, bool failover
,
173 #ifdef CONFIG_REPLICATION
174 ReplicationMode mode
= primary
?
175 REPLICATION_MODE_PRIMARY
:
176 REPLICATION_MODE_SECONDARY
;
178 if (has_failover
&& enable
) {
179 error_setg(errp
, "Parameter 'failover' is only for"
180 " stopping replication");
185 replication_start_all(mode
, errp
);
190 replication_stop_all(failover
, failover
? NULL
: errp
);
197 ReplicationStatus
*qmp_query_xen_replication_status(Error
**errp
)
199 #ifdef CONFIG_REPLICATION
201 ReplicationStatus
*s
= g_new0(ReplicationStatus
, 1);
203 replication_get_error_all(&err
);
207 s
->desc
= g_strdup(error_get_pretty(err
));
219 void qmp_xen_colo_do_checkpoint(Error
**errp
)
221 #ifdef CONFIG_REPLICATION
222 replication_do_checkpoint_all(errp
);
228 static void colo_send_message(QEMUFile
*f
, COLOMessage msg
,
233 if (msg
>= COLO_MESSAGE__MAX
) {
234 error_setg(errp
, "%s: Invalid message", __func__
);
237 qemu_put_be32(f
, msg
);
240 ret
= qemu_file_get_error(f
);
242 error_setg_errno(errp
, -ret
, "Can't send COLO message");
244 trace_colo_send_message(COLOMessage_str(msg
));
247 static void colo_send_message_value(QEMUFile
*f
, COLOMessage msg
,
248 uint64_t value
, Error
**errp
)
250 Error
*local_err
= NULL
;
253 colo_send_message(f
, msg
, &local_err
);
255 error_propagate(errp
, local_err
);
258 qemu_put_be64(f
, value
);
261 ret
= qemu_file_get_error(f
);
263 error_setg_errno(errp
, -ret
, "Failed to send value for message:%s",
264 COLOMessage_str(msg
));
268 static COLOMessage
colo_receive_message(QEMUFile
*f
, Error
**errp
)
273 msg
= qemu_get_be32(f
);
274 ret
= qemu_file_get_error(f
);
276 error_setg_errno(errp
, -ret
, "Can't receive COLO message");
279 if (msg
>= COLO_MESSAGE__MAX
) {
280 error_setg(errp
, "%s: Invalid message", __func__
);
283 trace_colo_receive_message(COLOMessage_str(msg
));
287 static void colo_receive_check_message(QEMUFile
*f
, COLOMessage expect_msg
,
291 Error
*local_err
= NULL
;
293 msg
= colo_receive_message(f
, &local_err
);
295 error_propagate(errp
, local_err
);
298 if (msg
!= expect_msg
) {
299 error_setg(errp
, "Unexpected COLO message %d, expected %d",
304 static uint64_t colo_receive_message_value(QEMUFile
*f
, uint32_t expect_msg
,
307 Error
*local_err
= NULL
;
311 colo_receive_check_message(f
, expect_msg
, &local_err
);
313 error_propagate(errp
, local_err
);
317 value
= qemu_get_be64(f
);
318 ret
= qemu_file_get_error(f
);
320 error_setg_errno(errp
, -ret
, "Failed to get value for COLO message: %s",
321 COLOMessage_str(expect_msg
));
326 static int colo_do_checkpoint_transaction(MigrationState
*s
,
327 QIOChannelBuffer
*bioc
,
330 Error
*local_err
= NULL
;
333 colo_send_message(s
->to_dst_file
, COLO_MESSAGE_CHECKPOINT_REQUEST
,
339 colo_receive_check_message(s
->rp_state
.from_dst_file
,
340 COLO_MESSAGE_CHECKPOINT_REPLY
, &local_err
);
344 /* Reset channel-buffer directly */
345 qio_channel_io_seek(QIO_CHANNEL(bioc
), 0, 0, NULL
);
348 qemu_mutex_lock_iothread();
349 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
350 qemu_mutex_unlock_iothread();
353 vm_stop_force_state(RUN_STATE_COLO
);
354 qemu_mutex_unlock_iothread();
355 trace_colo_vm_state_change("run", "stop");
357 * Failover request bh could be called after vm_stop_force_state(),
358 * So we need check failover_request_is_active() again.
360 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
364 colo_notify_compares_event(NULL
, COLO_EVENT_CHECKPOINT
, &local_err
);
369 /* Disable block migration */
370 migrate_set_block_enabled(false, &local_err
);
371 qemu_savevm_state_header(fb
);
372 qemu_savevm_state_setup(fb
);
373 qemu_mutex_lock_iothread();
374 replication_do_checkpoint_all(&local_err
);
376 qemu_mutex_unlock_iothread();
379 qemu_savevm_state_complete_precopy(fb
, false, false);
380 qemu_mutex_unlock_iothread();
384 colo_send_message(s
->to_dst_file
, COLO_MESSAGE_VMSTATE_SEND
, &local_err
);
389 * We need the size of the VMstate data in Secondary side,
390 * With which we can decide how much data should be read.
392 colo_send_message_value(s
->to_dst_file
, COLO_MESSAGE_VMSTATE_SIZE
,
393 bioc
->usage
, &local_err
);
398 qemu_put_buffer(s
->to_dst_file
, bioc
->data
, bioc
->usage
);
399 qemu_fflush(s
->to_dst_file
);
400 ret
= qemu_file_get_error(s
->to_dst_file
);
405 colo_receive_check_message(s
->rp_state
.from_dst_file
,
406 COLO_MESSAGE_VMSTATE_RECEIVED
, &local_err
);
411 colo_receive_check_message(s
->rp_state
.from_dst_file
,
412 COLO_MESSAGE_VMSTATE_LOADED
, &local_err
);
419 qemu_mutex_lock_iothread();
421 qemu_mutex_unlock_iothread();
422 trace_colo_vm_state_change("stop", "run");
426 error_report_err(local_err
);
431 static void colo_compare_notify_checkpoint(Notifier
*notifier
, void *data
)
433 colo_checkpoint_notify(data
);
436 static void colo_process_checkpoint(MigrationState
*s
)
438 QIOChannelBuffer
*bioc
;
440 int64_t current_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
441 Error
*local_err
= NULL
;
444 failover_init_state();
446 s
->rp_state
.from_dst_file
= qemu_file_get_return_path(s
->to_dst_file
);
447 if (!s
->rp_state
.from_dst_file
) {
448 error_report("Open QEMUFile from_dst_file failed");
452 packets_compare_notifier
.notify
= colo_compare_notify_checkpoint
;
453 colo_compare_register_notifier(&packets_compare_notifier
);
456 * Wait for Secondary finish loading VM states and enter COLO
459 colo_receive_check_message(s
->rp_state
.from_dst_file
,
460 COLO_MESSAGE_CHECKPOINT_READY
, &local_err
);
464 bioc
= qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE
);
465 fb
= qemu_fopen_channel_output(QIO_CHANNEL(bioc
));
466 object_unref(OBJECT(bioc
));
468 qemu_mutex_lock_iothread();
469 replication_start_all(REPLICATION_MODE_PRIMARY
, &local_err
);
471 qemu_mutex_unlock_iothread();
476 qemu_mutex_unlock_iothread();
477 trace_colo_vm_state_change("stop", "run");
479 timer_mod(s
->colo_delay_timer
,
480 current_time
+ s
->parameters
.x_checkpoint_delay
);
482 while (s
->state
== MIGRATION_STATUS_COLO
) {
483 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
484 error_report("failover request");
488 qemu_sem_wait(&s
->colo_checkpoint_sem
);
490 ret
= colo_do_checkpoint_transaction(s
, bioc
, fb
);
497 /* Throw the unreported error message after exited from loop */
499 error_report_err(local_err
);
506 /* Hope this not to be too long to wait here */
507 qemu_sem_wait(&s
->colo_exit_sem
);
508 qemu_sem_destroy(&s
->colo_exit_sem
);
511 * It is safe to unregister notifier after failover finished.
512 * Besides, colo_delay_timer and colo_checkpoint_sem can't be
513 * released befor unregister notifier, or there will be use-after-free
516 colo_compare_unregister_notifier(&packets_compare_notifier
);
517 timer_del(s
->colo_delay_timer
);
518 timer_free(s
->colo_delay_timer
);
519 qemu_sem_destroy(&s
->colo_checkpoint_sem
);
522 * Must be called after failover BH is completed,
523 * Or the failover BH may shutdown the wrong fd that
524 * re-used by other threads after we release here.
526 if (s
->rp_state
.from_dst_file
) {
527 qemu_fclose(s
->rp_state
.from_dst_file
);
531 void colo_checkpoint_notify(void *opaque
)
533 MigrationState
*s
= opaque
;
534 int64_t next_notify_time
;
536 qemu_sem_post(&s
->colo_checkpoint_sem
);
537 s
->colo_checkpoint_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
538 next_notify_time
= s
->colo_checkpoint_time
+
539 s
->parameters
.x_checkpoint_delay
;
540 timer_mod(s
->colo_delay_timer
, next_notify_time
);
543 void migrate_start_colo_process(MigrationState
*s
)
545 qemu_mutex_unlock_iothread();
546 qemu_sem_init(&s
->colo_checkpoint_sem
, 0);
547 s
->colo_delay_timer
= timer_new_ms(QEMU_CLOCK_HOST
,
548 colo_checkpoint_notify
, s
);
550 qemu_sem_init(&s
->colo_exit_sem
, 0);
551 migrate_set_state(&s
->state
, MIGRATION_STATUS_ACTIVE
,
552 MIGRATION_STATUS_COLO
);
553 colo_process_checkpoint(s
);
554 qemu_mutex_lock_iothread();
557 static void colo_wait_handle_message(QEMUFile
*f
, int *checkpoint_request
,
561 Error
*local_err
= NULL
;
563 msg
= colo_receive_message(f
, &local_err
);
565 error_propagate(errp
, local_err
);
570 case COLO_MESSAGE_CHECKPOINT_REQUEST
:
571 *checkpoint_request
= 1;
574 *checkpoint_request
= 0;
575 error_setg(errp
, "Got unknown COLO message: %d", msg
);
580 void *colo_process_incoming_thread(void *opaque
)
582 MigrationIncomingState
*mis
= opaque
;
584 QIOChannelBuffer
*bioc
= NULL
; /* Cache incoming device state */
587 Error
*local_err
= NULL
;
589 rcu_register_thread();
590 qemu_sem_init(&mis
->colo_incoming_sem
, 0);
592 migrate_set_state(&mis
->state
, MIGRATION_STATUS_ACTIVE
,
593 MIGRATION_STATUS_COLO
);
595 failover_init_state();
597 mis
->to_src_file
= qemu_file_get_return_path(mis
->from_src_file
);
598 if (!mis
->to_src_file
) {
599 error_report("COLO incoming thread: Open QEMUFile to_src_file failed");
603 * Note: the communication between Primary side and Secondary side
604 * should be sequential, we set the fd to unblocked in migration incoming
605 * coroutine, and here we are in the COLO incoming thread, so it is ok to
606 * set the fd back to blocked.
608 qemu_file_set_blocking(mis
->from_src_file
, true);
610 bioc
= qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE
);
611 fb
= qemu_fopen_channel_input(QIO_CHANNEL(bioc
));
612 object_unref(OBJECT(bioc
));
614 qemu_mutex_lock_iothread();
615 replication_start_all(REPLICATION_MODE_SECONDARY
, &local_err
);
617 qemu_mutex_unlock_iothread();
621 trace_colo_vm_state_change("stop", "run");
622 qemu_mutex_unlock_iothread();
624 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_CHECKPOINT_READY
,
630 while (mis
->state
== MIGRATION_STATUS_COLO
) {
633 colo_wait_handle_message(mis
->from_src_file
, &request
, &local_err
);
638 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
639 error_report("failover request");
643 qemu_mutex_lock_iothread();
644 vm_stop_force_state(RUN_STATE_COLO
);
645 trace_colo_vm_state_change("run", "stop");
646 qemu_mutex_unlock_iothread();
648 /* FIXME: This is unnecessary for periodic checkpoint mode */
649 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_CHECKPOINT_REPLY
,
655 colo_receive_check_message(mis
->from_src_file
,
656 COLO_MESSAGE_VMSTATE_SEND
, &local_err
);
661 value
= colo_receive_message_value(mis
->from_src_file
,
662 COLO_MESSAGE_VMSTATE_SIZE
, &local_err
);
668 * Read VM device state data into channel buffer,
669 * It's better to re-use the memory allocated.
670 * Here we need to handle the channel buffer directly.
672 if (value
> bioc
->capacity
) {
673 bioc
->capacity
= value
;
674 bioc
->data
= g_realloc(bioc
->data
, bioc
->capacity
);
676 total_size
= qemu_get_buffer(mis
->from_src_file
, bioc
->data
, value
);
677 if (total_size
!= value
) {
678 error_report("Got %" PRIu64
" VMState data, less than expected"
679 " %" PRIu64
, total_size
, value
);
682 bioc
->usage
= total_size
;
683 qio_channel_io_seek(QIO_CHANNEL(bioc
), 0, 0, NULL
);
685 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_VMSTATE_RECEIVED
,
691 qemu_mutex_lock_iothread();
692 qemu_system_reset(SHUTDOWN_CAUSE_NONE
);
693 vmstate_loading
= true;
694 if (qemu_loadvm_state(fb
) < 0) {
695 error_report("COLO: loadvm failed");
696 qemu_mutex_unlock_iothread();
700 replication_get_error_all(&local_err
);
702 qemu_mutex_unlock_iothread();
705 /* discard colo disk buffer */
706 replication_do_checkpoint_all(&local_err
);
708 qemu_mutex_unlock_iothread();
712 vmstate_loading
= false;
714 trace_colo_vm_state_change("stop", "run");
715 qemu_mutex_unlock_iothread();
717 if (failover_get_state() == FAILOVER_STATUS_RELAUNCH
) {
718 failover_set_state(FAILOVER_STATUS_RELAUNCH
,
719 FAILOVER_STATUS_NONE
);
720 failover_request_active(NULL
);
724 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_VMSTATE_LOADED
,
732 vmstate_loading
= false;
733 /* Throw the unreported error message after exited from loop */
735 error_report_err(local_err
);
742 /* Hope this not to be too long to loop here */
743 qemu_sem_wait(&mis
->colo_incoming_sem
);
744 qemu_sem_destroy(&mis
->colo_incoming_sem
);
745 /* Must be called after failover BH is completed */
746 if (mis
->to_src_file
) {
747 qemu_fclose(mis
->to_src_file
);
749 migration_incoming_exit_colo();
751 rcu_unregister_thread();