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 "qemu/timer.h"
15 #include "sysemu/sysemu.h"
16 #include "qemu-file-channel.h"
17 #include "migration/migration.h"
18 #include "migration/qemu-file.h"
19 #include "migration/colo.h"
20 #include "migration/block.h"
21 #include "io/channel-buffer.h"
23 #include "qemu/error-report.h"
24 #include "qapi/error.h"
25 #include "migration/failover.h"
26 #include "replication.h"
27 #include "qmp-commands.h"
29 static bool vmstate_loading
;
31 #define COLO_BUFFER_BASE_SIZE (4 * 1024 * 1024)
33 bool colo_supported(void)
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();
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_lookup
[old_state
]);
75 migrate_set_state(&mis
->state
, MIGRATION_STATUS_COLO
,
76 MIGRATION_STATUS_COMPLETED
);
79 error_report("\"-S\" qemu option will be ignored in secondary side");
80 /* recover runstate to normal migration finish state */
84 * Make sure COLO incoming thread not block in recv or send,
85 * If mis->from_src_file and mis->to_src_file use the same fd,
86 * The second shutdown() will return -1, we ignore this value,
89 if (mis
->from_src_file
) {
90 qemu_file_shutdown(mis
->from_src_file
);
92 if (mis
->to_src_file
) {
93 qemu_file_shutdown(mis
->to_src_file
);
96 old_state
= failover_set_state(FAILOVER_STATUS_ACTIVE
,
97 FAILOVER_STATUS_COMPLETED
);
98 if (old_state
!= FAILOVER_STATUS_ACTIVE
) {
99 error_report("Incorrect state (%s) while doing failover for "
100 "secondary VM", FailoverStatus_lookup
[old_state
]);
103 /* Notify COLO incoming thread that failover work is finished */
104 qemu_sem_post(&mis
->colo_incoming_sem
);
105 /* For Secondary VM, jump to incoming co */
106 if (mis
->migration_incoming_co
) {
107 qemu_coroutine_enter(mis
->migration_incoming_co
);
111 static void primary_vm_do_failover(void)
113 MigrationState
*s
= migrate_get_current();
116 migrate_set_state(&s
->state
, MIGRATION_STATUS_COLO
,
117 MIGRATION_STATUS_COMPLETED
);
120 * Wake up COLO thread which may blocked in recv() or send(),
121 * The s->rp_state.from_dst_file and s->to_dst_file may use the
122 * same fd, but we still shutdown the fd for twice, it is harmless.
124 if (s
->to_dst_file
) {
125 qemu_file_shutdown(s
->to_dst_file
);
127 if (s
->rp_state
.from_dst_file
) {
128 qemu_file_shutdown(s
->rp_state
.from_dst_file
);
131 old_state
= failover_set_state(FAILOVER_STATUS_ACTIVE
,
132 FAILOVER_STATUS_COMPLETED
);
133 if (old_state
!= FAILOVER_STATUS_ACTIVE
) {
134 error_report("Incorrect state (%s) while doing failover for Primary VM",
135 FailoverStatus_lookup
[old_state
]);
138 /* Notify COLO thread that failover work is finished */
139 qemu_sem_post(&s
->colo_exit_sem
);
142 void colo_do_failover(MigrationState
*s
)
144 /* Make sure VM stopped while failover happened. */
145 if (!colo_runstate_is_stopped()) {
146 vm_stop_force_state(RUN_STATE_COLO
);
149 if (get_colo_mode() == COLO_MODE_PRIMARY
) {
150 primary_vm_do_failover();
152 secondary_vm_do_failover();
156 void qmp_xen_set_replication(bool enable
, bool primary
,
157 bool has_failover
, bool failover
,
160 #ifdef CONFIG_REPLICATION
161 ReplicationMode mode
= primary
?
162 REPLICATION_MODE_PRIMARY
:
163 REPLICATION_MODE_SECONDARY
;
165 if (has_failover
&& enable
) {
166 error_setg(errp
, "Parameter 'failover' is only for"
167 " stopping replication");
172 replication_start_all(mode
, errp
);
177 replication_stop_all(failover
, failover
? NULL
: errp
);
184 ReplicationStatus
*qmp_query_xen_replication_status(Error
**errp
)
186 #ifdef CONFIG_REPLICATION
188 ReplicationStatus
*s
= g_new0(ReplicationStatus
, 1);
190 replication_get_error_all(&err
);
194 s
->desc
= g_strdup(error_get_pretty(err
));
206 void qmp_xen_colo_do_checkpoint(Error
**errp
)
208 #ifdef CONFIG_REPLICATION
209 replication_do_checkpoint_all(errp
);
215 static void colo_send_message(QEMUFile
*f
, COLOMessage msg
,
220 if (msg
>= COLO_MESSAGE__MAX
) {
221 error_setg(errp
, "%s: Invalid message", __func__
);
224 qemu_put_be32(f
, msg
);
227 ret
= qemu_file_get_error(f
);
229 error_setg_errno(errp
, -ret
, "Can't send COLO message");
231 trace_colo_send_message(COLOMessage_lookup
[msg
]);
234 static void colo_send_message_value(QEMUFile
*f
, COLOMessage msg
,
235 uint64_t value
, Error
**errp
)
237 Error
*local_err
= NULL
;
240 colo_send_message(f
, msg
, &local_err
);
242 error_propagate(errp
, local_err
);
245 qemu_put_be64(f
, value
);
248 ret
= qemu_file_get_error(f
);
250 error_setg_errno(errp
, -ret
, "Failed to send value for message:%s",
251 COLOMessage_lookup
[msg
]);
255 static COLOMessage
colo_receive_message(QEMUFile
*f
, Error
**errp
)
260 msg
= qemu_get_be32(f
);
261 ret
= qemu_file_get_error(f
);
263 error_setg_errno(errp
, -ret
, "Can't receive COLO message");
266 if (msg
>= COLO_MESSAGE__MAX
) {
267 error_setg(errp
, "%s: Invalid message", __func__
);
270 trace_colo_receive_message(COLOMessage_lookup
[msg
]);
274 static void colo_receive_check_message(QEMUFile
*f
, COLOMessage expect_msg
,
278 Error
*local_err
= NULL
;
280 msg
= colo_receive_message(f
, &local_err
);
282 error_propagate(errp
, local_err
);
285 if (msg
!= expect_msg
) {
286 error_setg(errp
, "Unexpected COLO message %d, expected %d",
291 static uint64_t colo_receive_message_value(QEMUFile
*f
, uint32_t expect_msg
,
294 Error
*local_err
= NULL
;
298 colo_receive_check_message(f
, expect_msg
, &local_err
);
300 error_propagate(errp
, local_err
);
304 value
= qemu_get_be64(f
);
305 ret
= qemu_file_get_error(f
);
307 error_setg_errno(errp
, -ret
, "Failed to get value for COLO message: %s",
308 COLOMessage_lookup
[expect_msg
]);
313 static int colo_do_checkpoint_transaction(MigrationState
*s
,
314 QIOChannelBuffer
*bioc
,
317 Error
*local_err
= NULL
;
320 colo_send_message(s
->to_dst_file
, COLO_MESSAGE_CHECKPOINT_REQUEST
,
326 colo_receive_check_message(s
->rp_state
.from_dst_file
,
327 COLO_MESSAGE_CHECKPOINT_REPLY
, &local_err
);
331 /* Reset channel-buffer directly */
332 qio_channel_io_seek(QIO_CHANNEL(bioc
), 0, 0, NULL
);
335 qemu_mutex_lock_iothread();
336 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
337 qemu_mutex_unlock_iothread();
340 vm_stop_force_state(RUN_STATE_COLO
);
341 qemu_mutex_unlock_iothread();
342 trace_colo_vm_state_change("run", "stop");
344 * Failover request bh could be called after vm_stop_force_state(),
345 * So we need check failover_request_is_active() again.
347 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
351 /* Disable block migration */
352 migrate_set_block_enabled(false, &local_err
);
353 qemu_savevm_state_header(fb
);
354 qemu_savevm_state_begin(fb
);
355 qemu_mutex_lock_iothread();
356 qemu_savevm_state_complete_precopy(fb
, false);
357 qemu_mutex_unlock_iothread();
361 colo_send_message(s
->to_dst_file
, COLO_MESSAGE_VMSTATE_SEND
, &local_err
);
366 * We need the size of the VMstate data in Secondary side,
367 * With which we can decide how much data should be read.
369 colo_send_message_value(s
->to_dst_file
, COLO_MESSAGE_VMSTATE_SIZE
,
370 bioc
->usage
, &local_err
);
375 qemu_put_buffer(s
->to_dst_file
, bioc
->data
, bioc
->usage
);
376 qemu_fflush(s
->to_dst_file
);
377 ret
= qemu_file_get_error(s
->to_dst_file
);
382 colo_receive_check_message(s
->rp_state
.from_dst_file
,
383 COLO_MESSAGE_VMSTATE_RECEIVED
, &local_err
);
388 colo_receive_check_message(s
->rp_state
.from_dst_file
,
389 COLO_MESSAGE_VMSTATE_LOADED
, &local_err
);
396 qemu_mutex_lock_iothread();
398 qemu_mutex_unlock_iothread();
399 trace_colo_vm_state_change("stop", "run");
403 error_report_err(local_err
);
408 static void colo_process_checkpoint(MigrationState
*s
)
410 QIOChannelBuffer
*bioc
;
412 int64_t current_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
413 Error
*local_err
= NULL
;
416 failover_init_state();
418 s
->rp_state
.from_dst_file
= qemu_file_get_return_path(s
->to_dst_file
);
419 if (!s
->rp_state
.from_dst_file
) {
420 error_report("Open QEMUFile from_dst_file failed");
425 * Wait for Secondary finish loading VM states and enter COLO
428 colo_receive_check_message(s
->rp_state
.from_dst_file
,
429 COLO_MESSAGE_CHECKPOINT_READY
, &local_err
);
433 bioc
= qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE
);
434 fb
= qemu_fopen_channel_output(QIO_CHANNEL(bioc
));
435 object_unref(OBJECT(bioc
));
437 qemu_mutex_lock_iothread();
439 qemu_mutex_unlock_iothread();
440 trace_colo_vm_state_change("stop", "run");
442 timer_mod(s
->colo_delay_timer
,
443 current_time
+ s
->parameters
.x_checkpoint_delay
);
445 while (s
->state
== MIGRATION_STATUS_COLO
) {
446 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
447 error_report("failover request");
451 qemu_sem_wait(&s
->colo_checkpoint_sem
);
453 ret
= colo_do_checkpoint_transaction(s
, bioc
, fb
);
460 /* Throw the unreported error message after exited from loop */
462 error_report_err(local_err
);
469 timer_del(s
->colo_delay_timer
);
471 /* Hope this not to be too long to wait here */
472 qemu_sem_wait(&s
->colo_exit_sem
);
473 qemu_sem_destroy(&s
->colo_exit_sem
);
475 * Must be called after failover BH is completed,
476 * Or the failover BH may shutdown the wrong fd that
477 * re-used by other threads after we release here.
479 if (s
->rp_state
.from_dst_file
) {
480 qemu_fclose(s
->rp_state
.from_dst_file
);
484 void colo_checkpoint_notify(void *opaque
)
486 MigrationState
*s
= opaque
;
487 int64_t next_notify_time
;
489 qemu_sem_post(&s
->colo_checkpoint_sem
);
490 s
->colo_checkpoint_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
491 next_notify_time
= s
->colo_checkpoint_time
+
492 s
->parameters
.x_checkpoint_delay
;
493 timer_mod(s
->colo_delay_timer
, next_notify_time
);
496 void migrate_start_colo_process(MigrationState
*s
)
498 qemu_mutex_unlock_iothread();
499 qemu_sem_init(&s
->colo_checkpoint_sem
, 0);
500 s
->colo_delay_timer
= timer_new_ms(QEMU_CLOCK_HOST
,
501 colo_checkpoint_notify
, s
);
503 qemu_sem_init(&s
->colo_exit_sem
, 0);
504 migrate_set_state(&s
->state
, MIGRATION_STATUS_ACTIVE
,
505 MIGRATION_STATUS_COLO
);
506 colo_process_checkpoint(s
);
507 qemu_mutex_lock_iothread();
510 static void colo_wait_handle_message(QEMUFile
*f
, int *checkpoint_request
,
514 Error
*local_err
= NULL
;
516 msg
= colo_receive_message(f
, &local_err
);
518 error_propagate(errp
, local_err
);
523 case COLO_MESSAGE_CHECKPOINT_REQUEST
:
524 *checkpoint_request
= 1;
527 *checkpoint_request
= 0;
528 error_setg(errp
, "Got unknown COLO message: %d", msg
);
533 void *colo_process_incoming_thread(void *opaque
)
535 MigrationIncomingState
*mis
= opaque
;
537 QIOChannelBuffer
*bioc
= NULL
; /* Cache incoming device state */
540 Error
*local_err
= NULL
;
542 qemu_sem_init(&mis
->colo_incoming_sem
, 0);
544 migrate_set_state(&mis
->state
, MIGRATION_STATUS_ACTIVE
,
545 MIGRATION_STATUS_COLO
);
547 failover_init_state();
549 mis
->to_src_file
= qemu_file_get_return_path(mis
->from_src_file
);
550 if (!mis
->to_src_file
) {
551 error_report("COLO incoming thread: Open QEMUFile to_src_file failed");
555 * Note: the communication between Primary side and Secondary side
556 * should be sequential, we set the fd to unblocked in migration incoming
557 * coroutine, and here we are in the COLO incoming thread, so it is ok to
558 * set the fd back to blocked.
560 qemu_file_set_blocking(mis
->from_src_file
, true);
562 bioc
= qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE
);
563 fb
= qemu_fopen_channel_input(QIO_CHANNEL(bioc
));
564 object_unref(OBJECT(bioc
));
566 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_CHECKPOINT_READY
,
572 while (mis
->state
== MIGRATION_STATUS_COLO
) {
575 colo_wait_handle_message(mis
->from_src_file
, &request
, &local_err
);
580 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
581 error_report("failover request");
585 /* FIXME: This is unnecessary for periodic checkpoint mode */
586 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_CHECKPOINT_REPLY
,
592 colo_receive_check_message(mis
->from_src_file
,
593 COLO_MESSAGE_VMSTATE_SEND
, &local_err
);
598 value
= colo_receive_message_value(mis
->from_src_file
,
599 COLO_MESSAGE_VMSTATE_SIZE
, &local_err
);
605 * Read VM device state data into channel buffer,
606 * It's better to re-use the memory allocated.
607 * Here we need to handle the channel buffer directly.
609 if (value
> bioc
->capacity
) {
610 bioc
->capacity
= value
;
611 bioc
->data
= g_realloc(bioc
->data
, bioc
->capacity
);
613 total_size
= qemu_get_buffer(mis
->from_src_file
, bioc
->data
, value
);
614 if (total_size
!= value
) {
615 error_report("Got %" PRIu64
" VMState data, less than expected"
616 " %" PRIu64
, total_size
, value
);
619 bioc
->usage
= total_size
;
620 qio_channel_io_seek(QIO_CHANNEL(bioc
), 0, 0, NULL
);
622 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_VMSTATE_RECEIVED
,
628 qemu_mutex_lock_iothread();
629 qemu_system_reset(VMRESET_SILENT
);
630 vmstate_loading
= true;
631 if (qemu_loadvm_state(fb
) < 0) {
632 error_report("COLO: loadvm failed");
633 qemu_mutex_unlock_iothread();
637 vmstate_loading
= false;
638 qemu_mutex_unlock_iothread();
640 if (failover_get_state() == FAILOVER_STATUS_RELAUNCH
) {
641 failover_set_state(FAILOVER_STATUS_RELAUNCH
,
642 FAILOVER_STATUS_NONE
);
643 failover_request_active(NULL
);
647 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_VMSTATE_LOADED
,
655 vmstate_loading
= false;
656 /* Throw the unreported error message after exited from loop */
658 error_report_err(local_err
);
665 /* Hope this not to be too long to loop here */
666 qemu_sem_wait(&mis
->colo_incoming_sem
);
667 qemu_sem_destroy(&mis
->colo_incoming_sem
);
668 /* Must be called after failover BH is completed */
669 if (mis
->to_src_file
) {
670 qemu_fclose(mis
->to_src_file
);
672 migration_incoming_exit_colo();