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"
31 static bool vmstate_loading
;
32 static Notifier packets_compare_notifier
;
34 #define COLO_BUFFER_BASE_SIZE (4 * 1024 * 1024)
36 bool migration_in_colo_state(void)
38 MigrationState
*s
= migrate_get_current();
40 return (s
->state
== MIGRATION_STATUS_COLO
);
43 bool migration_incoming_in_colo_state(void)
45 MigrationIncomingState
*mis
= migration_incoming_get_current();
47 return mis
&& (mis
->state
== MIGRATION_STATUS_COLO
);
50 static bool colo_runstate_is_stopped(void)
52 return runstate_check(RUN_STATE_COLO
) || !runstate_is_running();
55 static void secondary_vm_do_failover(void)
58 MigrationIncomingState
*mis
= migration_incoming_get_current();
60 /* Can not do failover during the process of VM's loading VMstate, Or
61 * it will break the secondary VM.
63 if (vmstate_loading
) {
64 old_state
= failover_set_state(FAILOVER_STATUS_ACTIVE
,
65 FAILOVER_STATUS_RELAUNCH
);
66 if (old_state
!= FAILOVER_STATUS_ACTIVE
) {
67 error_report("Unknown error while do failover for secondary VM,"
68 "old_state: %s", FailoverStatus_str(old_state
));
73 migrate_set_state(&mis
->state
, MIGRATION_STATUS_COLO
,
74 MIGRATION_STATUS_COMPLETED
);
77 error_report("\"-S\" qemu option will be ignored in secondary side");
78 /* recover runstate to normal migration finish state */
82 * Make sure COLO incoming thread not block in recv or send,
83 * If mis->from_src_file and mis->to_src_file use the same fd,
84 * The second shutdown() will return -1, we ignore this value,
87 if (mis
->from_src_file
) {
88 qemu_file_shutdown(mis
->from_src_file
);
90 if (mis
->to_src_file
) {
91 qemu_file_shutdown(mis
->to_src_file
);
94 old_state
= failover_set_state(FAILOVER_STATUS_ACTIVE
,
95 FAILOVER_STATUS_COMPLETED
);
96 if (old_state
!= FAILOVER_STATUS_ACTIVE
) {
97 error_report("Incorrect state (%s) while doing failover for "
98 "secondary VM", FailoverStatus_str(old_state
));
101 /* Notify COLO incoming thread that failover work is finished */
102 qemu_sem_post(&mis
->colo_incoming_sem
);
103 /* For Secondary VM, jump to incoming co */
104 if (mis
->migration_incoming_co
) {
105 qemu_coroutine_enter(mis
->migration_incoming_co
);
109 static void primary_vm_do_failover(void)
111 MigrationState
*s
= migrate_get_current();
114 migrate_set_state(&s
->state
, MIGRATION_STATUS_COLO
,
115 MIGRATION_STATUS_COMPLETED
);
118 * Wake up COLO thread which may blocked in recv() or send(),
119 * The s->rp_state.from_dst_file and s->to_dst_file may use the
120 * same fd, but we still shutdown the fd for twice, it is harmless.
122 if (s
->to_dst_file
) {
123 qemu_file_shutdown(s
->to_dst_file
);
125 if (s
->rp_state
.from_dst_file
) {
126 qemu_file_shutdown(s
->rp_state
.from_dst_file
);
129 old_state
= failover_set_state(FAILOVER_STATUS_ACTIVE
,
130 FAILOVER_STATUS_COMPLETED
);
131 if (old_state
!= FAILOVER_STATUS_ACTIVE
) {
132 error_report("Incorrect state (%s) while doing failover for Primary VM",
133 FailoverStatus_str(old_state
));
136 /* Notify COLO thread that failover work is finished */
137 qemu_sem_post(&s
->colo_exit_sem
);
140 void colo_do_failover(MigrationState
*s
)
142 /* Make sure VM stopped while failover happened. */
143 if (!colo_runstate_is_stopped()) {
144 vm_stop_force_state(RUN_STATE_COLO
);
147 if (get_colo_mode() == COLO_MODE_PRIMARY
) {
148 primary_vm_do_failover();
150 secondary_vm_do_failover();
154 void qmp_xen_set_replication(bool enable
, bool primary
,
155 bool has_failover
, bool failover
,
158 #ifdef CONFIG_REPLICATION
159 ReplicationMode mode
= primary
?
160 REPLICATION_MODE_PRIMARY
:
161 REPLICATION_MODE_SECONDARY
;
163 if (has_failover
&& enable
) {
164 error_setg(errp
, "Parameter 'failover' is only for"
165 " stopping replication");
170 replication_start_all(mode
, errp
);
175 replication_stop_all(failover
, failover
? NULL
: errp
);
182 ReplicationStatus
*qmp_query_xen_replication_status(Error
**errp
)
184 #ifdef CONFIG_REPLICATION
186 ReplicationStatus
*s
= g_new0(ReplicationStatus
, 1);
188 replication_get_error_all(&err
);
192 s
->desc
= g_strdup(error_get_pretty(err
));
204 void qmp_xen_colo_do_checkpoint(Error
**errp
)
206 #ifdef CONFIG_REPLICATION
207 replication_do_checkpoint_all(errp
);
213 static void colo_send_message(QEMUFile
*f
, COLOMessage msg
,
218 if (msg
>= COLO_MESSAGE__MAX
) {
219 error_setg(errp
, "%s: Invalid message", __func__
);
222 qemu_put_be32(f
, msg
);
225 ret
= qemu_file_get_error(f
);
227 error_setg_errno(errp
, -ret
, "Can't send COLO message");
229 trace_colo_send_message(COLOMessage_str(msg
));
232 static void colo_send_message_value(QEMUFile
*f
, COLOMessage msg
,
233 uint64_t value
, Error
**errp
)
235 Error
*local_err
= NULL
;
238 colo_send_message(f
, msg
, &local_err
);
240 error_propagate(errp
, local_err
);
243 qemu_put_be64(f
, value
);
246 ret
= qemu_file_get_error(f
);
248 error_setg_errno(errp
, -ret
, "Failed to send value for message:%s",
249 COLOMessage_str(msg
));
253 static COLOMessage
colo_receive_message(QEMUFile
*f
, Error
**errp
)
258 msg
= qemu_get_be32(f
);
259 ret
= qemu_file_get_error(f
);
261 error_setg_errno(errp
, -ret
, "Can't receive COLO message");
264 if (msg
>= COLO_MESSAGE__MAX
) {
265 error_setg(errp
, "%s: Invalid message", __func__
);
268 trace_colo_receive_message(COLOMessage_str(msg
));
272 static void colo_receive_check_message(QEMUFile
*f
, COLOMessage expect_msg
,
276 Error
*local_err
= NULL
;
278 msg
= colo_receive_message(f
, &local_err
);
280 error_propagate(errp
, local_err
);
283 if (msg
!= expect_msg
) {
284 error_setg(errp
, "Unexpected COLO message %d, expected %d",
289 static uint64_t colo_receive_message_value(QEMUFile
*f
, uint32_t expect_msg
,
292 Error
*local_err
= NULL
;
296 colo_receive_check_message(f
, expect_msg
, &local_err
);
298 error_propagate(errp
, local_err
);
302 value
= qemu_get_be64(f
);
303 ret
= qemu_file_get_error(f
);
305 error_setg_errno(errp
, -ret
, "Failed to get value for COLO message: %s",
306 COLOMessage_str(expect_msg
));
311 static int colo_do_checkpoint_transaction(MigrationState
*s
,
312 QIOChannelBuffer
*bioc
,
315 Error
*local_err
= NULL
;
318 colo_send_message(s
->to_dst_file
, COLO_MESSAGE_CHECKPOINT_REQUEST
,
324 colo_receive_check_message(s
->rp_state
.from_dst_file
,
325 COLO_MESSAGE_CHECKPOINT_REPLY
, &local_err
);
329 /* Reset channel-buffer directly */
330 qio_channel_io_seek(QIO_CHANNEL(bioc
), 0, 0, NULL
);
333 qemu_mutex_lock_iothread();
334 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
335 qemu_mutex_unlock_iothread();
338 vm_stop_force_state(RUN_STATE_COLO
);
339 qemu_mutex_unlock_iothread();
340 trace_colo_vm_state_change("run", "stop");
342 * Failover request bh could be called after vm_stop_force_state(),
343 * So we need check failover_request_is_active() again.
345 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
349 colo_notify_compares_event(NULL
, COLO_EVENT_CHECKPOINT
, &local_err
);
354 /* Disable block migration */
355 migrate_set_block_enabled(false, &local_err
);
356 qemu_savevm_state_header(fb
);
357 qemu_savevm_state_setup(fb
);
358 qemu_mutex_lock_iothread();
359 qemu_savevm_state_complete_precopy(fb
, false, false);
360 qemu_mutex_unlock_iothread();
364 colo_send_message(s
->to_dst_file
, COLO_MESSAGE_VMSTATE_SEND
, &local_err
);
369 * We need the size of the VMstate data in Secondary side,
370 * With which we can decide how much data should be read.
372 colo_send_message_value(s
->to_dst_file
, COLO_MESSAGE_VMSTATE_SIZE
,
373 bioc
->usage
, &local_err
);
378 qemu_put_buffer(s
->to_dst_file
, bioc
->data
, bioc
->usage
);
379 qemu_fflush(s
->to_dst_file
);
380 ret
= qemu_file_get_error(s
->to_dst_file
);
385 colo_receive_check_message(s
->rp_state
.from_dst_file
,
386 COLO_MESSAGE_VMSTATE_RECEIVED
, &local_err
);
391 colo_receive_check_message(s
->rp_state
.from_dst_file
,
392 COLO_MESSAGE_VMSTATE_LOADED
, &local_err
);
399 qemu_mutex_lock_iothread();
401 qemu_mutex_unlock_iothread();
402 trace_colo_vm_state_change("stop", "run");
406 error_report_err(local_err
);
411 static void colo_compare_notify_checkpoint(Notifier
*notifier
, void *data
)
413 colo_checkpoint_notify(data
);
416 static void colo_process_checkpoint(MigrationState
*s
)
418 QIOChannelBuffer
*bioc
;
420 int64_t current_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
421 Error
*local_err
= NULL
;
424 failover_init_state();
426 s
->rp_state
.from_dst_file
= qemu_file_get_return_path(s
->to_dst_file
);
427 if (!s
->rp_state
.from_dst_file
) {
428 error_report("Open QEMUFile from_dst_file failed");
432 packets_compare_notifier
.notify
= colo_compare_notify_checkpoint
;
433 colo_compare_register_notifier(&packets_compare_notifier
);
436 * Wait for Secondary finish loading VM states and enter COLO
439 colo_receive_check_message(s
->rp_state
.from_dst_file
,
440 COLO_MESSAGE_CHECKPOINT_READY
, &local_err
);
444 bioc
= qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE
);
445 fb
= qemu_fopen_channel_output(QIO_CHANNEL(bioc
));
446 object_unref(OBJECT(bioc
));
448 qemu_mutex_lock_iothread();
450 qemu_mutex_unlock_iothread();
451 trace_colo_vm_state_change("stop", "run");
453 timer_mod(s
->colo_delay_timer
,
454 current_time
+ s
->parameters
.x_checkpoint_delay
);
456 while (s
->state
== MIGRATION_STATUS_COLO
) {
457 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
458 error_report("failover request");
462 qemu_sem_wait(&s
->colo_checkpoint_sem
);
464 ret
= colo_do_checkpoint_transaction(s
, bioc
, fb
);
471 /* Throw the unreported error message after exited from loop */
473 error_report_err(local_err
);
480 /* Hope this not to be too long to wait here */
481 qemu_sem_wait(&s
->colo_exit_sem
);
482 qemu_sem_destroy(&s
->colo_exit_sem
);
485 * It is safe to unregister notifier after failover finished.
486 * Besides, colo_delay_timer and colo_checkpoint_sem can't be
487 * released befor unregister notifier, or there will be use-after-free
490 colo_compare_unregister_notifier(&packets_compare_notifier
);
491 timer_del(s
->colo_delay_timer
);
492 timer_free(s
->colo_delay_timer
);
493 qemu_sem_destroy(&s
->colo_checkpoint_sem
);
496 * Must be called after failover BH is completed,
497 * Or the failover BH may shutdown the wrong fd that
498 * re-used by other threads after we release here.
500 if (s
->rp_state
.from_dst_file
) {
501 qemu_fclose(s
->rp_state
.from_dst_file
);
505 void colo_checkpoint_notify(void *opaque
)
507 MigrationState
*s
= opaque
;
508 int64_t next_notify_time
;
510 qemu_sem_post(&s
->colo_checkpoint_sem
);
511 s
->colo_checkpoint_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
512 next_notify_time
= s
->colo_checkpoint_time
+
513 s
->parameters
.x_checkpoint_delay
;
514 timer_mod(s
->colo_delay_timer
, next_notify_time
);
517 void migrate_start_colo_process(MigrationState
*s
)
519 qemu_mutex_unlock_iothread();
520 qemu_sem_init(&s
->colo_checkpoint_sem
, 0);
521 s
->colo_delay_timer
= timer_new_ms(QEMU_CLOCK_HOST
,
522 colo_checkpoint_notify
, s
);
524 qemu_sem_init(&s
->colo_exit_sem
, 0);
525 migrate_set_state(&s
->state
, MIGRATION_STATUS_ACTIVE
,
526 MIGRATION_STATUS_COLO
);
527 colo_process_checkpoint(s
);
528 qemu_mutex_lock_iothread();
531 static void colo_wait_handle_message(QEMUFile
*f
, int *checkpoint_request
,
535 Error
*local_err
= NULL
;
537 msg
= colo_receive_message(f
, &local_err
);
539 error_propagate(errp
, local_err
);
544 case COLO_MESSAGE_CHECKPOINT_REQUEST
:
545 *checkpoint_request
= 1;
548 *checkpoint_request
= 0;
549 error_setg(errp
, "Got unknown COLO message: %d", msg
);
554 void *colo_process_incoming_thread(void *opaque
)
556 MigrationIncomingState
*mis
= opaque
;
558 QIOChannelBuffer
*bioc
= NULL
; /* Cache incoming device state */
561 Error
*local_err
= NULL
;
563 rcu_register_thread();
564 qemu_sem_init(&mis
->colo_incoming_sem
, 0);
566 migrate_set_state(&mis
->state
, MIGRATION_STATUS_ACTIVE
,
567 MIGRATION_STATUS_COLO
);
569 failover_init_state();
571 mis
->to_src_file
= qemu_file_get_return_path(mis
->from_src_file
);
572 if (!mis
->to_src_file
) {
573 error_report("COLO incoming thread: Open QEMUFile to_src_file failed");
577 * Note: the communication between Primary side and Secondary side
578 * should be sequential, we set the fd to unblocked in migration incoming
579 * coroutine, and here we are in the COLO incoming thread, so it is ok to
580 * set the fd back to blocked.
582 qemu_file_set_blocking(mis
->from_src_file
, true);
584 bioc
= qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE
);
585 fb
= qemu_fopen_channel_input(QIO_CHANNEL(bioc
));
586 object_unref(OBJECT(bioc
));
588 qemu_mutex_lock_iothread();
590 trace_colo_vm_state_change("stop", "run");
591 qemu_mutex_unlock_iothread();
593 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_CHECKPOINT_READY
,
599 while (mis
->state
== MIGRATION_STATUS_COLO
) {
602 colo_wait_handle_message(mis
->from_src_file
, &request
, &local_err
);
607 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
608 error_report("failover request");
612 qemu_mutex_lock_iothread();
613 vm_stop_force_state(RUN_STATE_COLO
);
614 trace_colo_vm_state_change("run", "stop");
615 qemu_mutex_unlock_iothread();
617 /* FIXME: This is unnecessary for periodic checkpoint mode */
618 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_CHECKPOINT_REPLY
,
624 colo_receive_check_message(mis
->from_src_file
,
625 COLO_MESSAGE_VMSTATE_SEND
, &local_err
);
630 value
= colo_receive_message_value(mis
->from_src_file
,
631 COLO_MESSAGE_VMSTATE_SIZE
, &local_err
);
637 * Read VM device state data into channel buffer,
638 * It's better to re-use the memory allocated.
639 * Here we need to handle the channel buffer directly.
641 if (value
> bioc
->capacity
) {
642 bioc
->capacity
= value
;
643 bioc
->data
= g_realloc(bioc
->data
, bioc
->capacity
);
645 total_size
= qemu_get_buffer(mis
->from_src_file
, bioc
->data
, value
);
646 if (total_size
!= value
) {
647 error_report("Got %" PRIu64
" VMState data, less than expected"
648 " %" PRIu64
, total_size
, value
);
651 bioc
->usage
= total_size
;
652 qio_channel_io_seek(QIO_CHANNEL(bioc
), 0, 0, NULL
);
654 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_VMSTATE_RECEIVED
,
660 qemu_mutex_lock_iothread();
661 qemu_system_reset(SHUTDOWN_CAUSE_NONE
);
662 vmstate_loading
= true;
663 if (qemu_loadvm_state(fb
) < 0) {
664 error_report("COLO: loadvm failed");
665 qemu_mutex_unlock_iothread();
669 vmstate_loading
= false;
671 trace_colo_vm_state_change("stop", "run");
672 qemu_mutex_unlock_iothread();
674 if (failover_get_state() == FAILOVER_STATUS_RELAUNCH
) {
675 failover_set_state(FAILOVER_STATUS_RELAUNCH
,
676 FAILOVER_STATUS_NONE
);
677 failover_request_active(NULL
);
681 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_VMSTATE_LOADED
,
689 vmstate_loading
= false;
690 /* Throw the unreported error message after exited from loop */
692 error_report_err(local_err
);
699 /* Hope this not to be too long to loop here */
700 qemu_sem_wait(&mis
->colo_incoming_sem
);
701 qemu_sem_destroy(&mis
->colo_incoming_sem
);
702 /* Must be called after failover BH is completed */
703 if (mis
->to_src_file
) {
704 qemu_fclose(mis
->to_src_file
);
706 migration_incoming_exit_colo();
708 rcu_unregister_thread();