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 "qemu-file-channel.h"
16 #include "migration.h"
17 #include "qemu-file.h"
19 #include "migration/colo.h"
21 #include "io/channel-buffer.h"
23 #include "qemu/error-report.h"
24 #include "migration/failover.h"
25 #include "replication.h"
26 #include "qmp-commands.h"
28 static bool vmstate_loading;
30 #define COLO_BUFFER_BASE_SIZE (4 * 1024 * 1024)
32 bool migration_in_colo_state(void)
34 MigrationState *s = migrate_get_current();
36 return (s->state == MIGRATION_STATUS_COLO);
39 bool migration_incoming_in_colo_state(void)
41 MigrationIncomingState *mis = migration_incoming_get_current();
43 return mis && (mis->state == MIGRATION_STATUS_COLO);
46 static bool colo_runstate_is_stopped(void)
48 return runstate_check(RUN_STATE_COLO) || !runstate_is_running();
51 static void secondary_vm_do_failover(void)
54 MigrationIncomingState *mis = migration_incoming_get_current();
56 /* Can not do failover during the process of VM's loading VMstate, Or
57 * it will break the secondary VM.
59 if (vmstate_loading) {
60 old_state = failover_set_state(FAILOVER_STATUS_ACTIVE,
61 FAILOVER_STATUS_RELAUNCH);
62 if (old_state != FAILOVER_STATUS_ACTIVE) {
63 error_report("Unknown error while do failover for secondary VM,"
64 "old_state: %s", FailoverStatus_str(old_state));
69 migrate_set_state(&mis->state, MIGRATION_STATUS_COLO,
70 MIGRATION_STATUS_COMPLETED);
73 error_report("\"-S\" qemu option will be ignored in secondary side");
74 /* recover runstate to normal migration finish state */
78 * Make sure COLO incoming thread not block in recv or send,
79 * If mis->from_src_file and mis->to_src_file use the same fd,
80 * The second shutdown() will return -1, we ignore this value,
83 if (mis->from_src_file) {
84 qemu_file_shutdown(mis->from_src_file);
86 if (mis->to_src_file) {
87 qemu_file_shutdown(mis->to_src_file);
90 old_state = failover_set_state(FAILOVER_STATUS_ACTIVE,
91 FAILOVER_STATUS_COMPLETED);
92 if (old_state != FAILOVER_STATUS_ACTIVE) {
93 error_report("Incorrect state (%s) while doing failover for "
94 "secondary VM", FailoverStatus_str(old_state));
97 /* Notify COLO incoming thread that failover work is finished */
98 qemu_sem_post(&mis->colo_incoming_sem);
99 /* For Secondary VM, jump to incoming co */
100 if (mis->migration_incoming_co) {
101 qemu_coroutine_enter(mis->migration_incoming_co);
105 static void primary_vm_do_failover(void)
107 MigrationState *s = migrate_get_current();
110 migrate_set_state(&s->state, MIGRATION_STATUS_COLO,
111 MIGRATION_STATUS_COMPLETED);
114 * Wake up COLO thread which may blocked in recv() or send(),
115 * The s->rp_state.from_dst_file and s->to_dst_file may use the
116 * same fd, but we still shutdown the fd for twice, it is harmless.
118 if (s->to_dst_file) {
119 qemu_file_shutdown(s->to_dst_file);
121 if (s->rp_state.from_dst_file) {
122 qemu_file_shutdown(s->rp_state.from_dst_file);
125 old_state = failover_set_state(FAILOVER_STATUS_ACTIVE,
126 FAILOVER_STATUS_COMPLETED);
127 if (old_state != FAILOVER_STATUS_ACTIVE) {
128 error_report("Incorrect state (%s) while doing failover for Primary VM",
129 FailoverStatus_str(old_state));
132 /* Notify COLO thread that failover work is finished */
133 qemu_sem_post(&s->colo_exit_sem);
136 void colo_do_failover(MigrationState *s)
138 /* Make sure VM stopped while failover happened. */
139 if (!colo_runstate_is_stopped()) {
140 vm_stop_force_state(RUN_STATE_COLO);
143 if (get_colo_mode() == COLO_MODE_PRIMARY) {
144 primary_vm_do_failover();
146 secondary_vm_do_failover();
150 void qmp_xen_set_replication(bool enable, bool primary,
151 bool has_failover, bool failover,
154 #ifdef CONFIG_REPLICATION
155 ReplicationMode mode = primary ?
156 REPLICATION_MODE_PRIMARY :
157 REPLICATION_MODE_SECONDARY;
159 if (has_failover && enable) {
160 error_setg(errp, "Parameter 'failover' is only for"
161 " stopping replication");
166 replication_start_all(mode, errp);
171 replication_stop_all(failover, failover ? NULL : errp);
178 ReplicationStatus *qmp_query_xen_replication_status(Error **errp)
180 #ifdef CONFIG_REPLICATION
182 ReplicationStatus *s = g_new0(ReplicationStatus, 1);
184 replication_get_error_all(&err);
188 s->desc = g_strdup(error_get_pretty(err));
200 void qmp_xen_colo_do_checkpoint(Error **errp)
202 #ifdef CONFIG_REPLICATION
203 replication_do_checkpoint_all(errp);
209 static void colo_send_message(QEMUFile *f, COLOMessage msg,
214 if (msg >= COLO_MESSAGE__MAX) {
215 error_setg(errp, "%s: Invalid message", __func__);
218 qemu_put_be32(f, msg);
221 ret = qemu_file_get_error(f);
223 error_setg_errno(errp, -ret, "Can't send COLO message");
225 trace_colo_send_message(COLOMessage_str(msg));
228 static void colo_send_message_value(QEMUFile *f, COLOMessage msg,
229 uint64_t value, Error **errp)
231 Error *local_err = NULL;
234 colo_send_message(f, msg, &local_err);
236 error_propagate(errp, local_err);
239 qemu_put_be64(f, value);
242 ret = qemu_file_get_error(f);
244 error_setg_errno(errp, -ret, "Failed to send value for message:%s",
245 COLOMessage_str(msg));
249 static COLOMessage colo_receive_message(QEMUFile *f, Error **errp)
254 msg = qemu_get_be32(f);
255 ret = qemu_file_get_error(f);
257 error_setg_errno(errp, -ret, "Can't receive COLO message");
260 if (msg >= COLO_MESSAGE__MAX) {
261 error_setg(errp, "%s: Invalid message", __func__);
264 trace_colo_receive_message(COLOMessage_str(msg));
268 static void colo_receive_check_message(QEMUFile *f, COLOMessage expect_msg,
272 Error *local_err = NULL;
274 msg = colo_receive_message(f, &local_err);
276 error_propagate(errp, local_err);
279 if (msg != expect_msg) {
280 error_setg(errp, "Unexpected COLO message %d, expected %d",
285 static uint64_t colo_receive_message_value(QEMUFile *f, uint32_t expect_msg,
288 Error *local_err = NULL;
292 colo_receive_check_message(f, expect_msg, &local_err);
294 error_propagate(errp, local_err);
298 value = qemu_get_be64(f);
299 ret = qemu_file_get_error(f);
301 error_setg_errno(errp, -ret, "Failed to get value for COLO message: %s",
302 COLOMessage_str(expect_msg));
307 static int colo_do_checkpoint_transaction(MigrationState *s,
308 QIOChannelBuffer *bioc,
311 Error *local_err = NULL;
314 colo_send_message(s->to_dst_file, COLO_MESSAGE_CHECKPOINT_REQUEST,
320 colo_receive_check_message(s->rp_state.from_dst_file,
321 COLO_MESSAGE_CHECKPOINT_REPLY, &local_err);
325 /* Reset channel-buffer directly */
326 qio_channel_io_seek(QIO_CHANNEL(bioc), 0, 0, NULL);
329 qemu_mutex_lock_iothread();
330 if (failover_get_state() != FAILOVER_STATUS_NONE) {
331 qemu_mutex_unlock_iothread();
334 vm_stop_force_state(RUN_STATE_COLO);
335 qemu_mutex_unlock_iothread();
336 trace_colo_vm_state_change("run", "stop");
338 * Failover request bh could be called after vm_stop_force_state(),
339 * So we need check failover_request_is_active() again.
341 if (failover_get_state() != FAILOVER_STATUS_NONE) {
345 /* Disable block migration */
346 migrate_set_block_enabled(false, &local_err);
347 qemu_savevm_state_header(fb);
348 qemu_savevm_state_setup(fb);
349 qemu_mutex_lock_iothread();
350 qemu_savevm_state_complete_precopy(fb, false, false);
351 qemu_mutex_unlock_iothread();
355 colo_send_message(s->to_dst_file, COLO_MESSAGE_VMSTATE_SEND, &local_err);
360 * We need the size of the VMstate data in Secondary side,
361 * With which we can decide how much data should be read.
363 colo_send_message_value(s->to_dst_file, COLO_MESSAGE_VMSTATE_SIZE,
364 bioc->usage, &local_err);
369 qemu_put_buffer(s->to_dst_file, bioc->data, bioc->usage);
370 qemu_fflush(s->to_dst_file);
371 ret = qemu_file_get_error(s->to_dst_file);
376 colo_receive_check_message(s->rp_state.from_dst_file,
377 COLO_MESSAGE_VMSTATE_RECEIVED, &local_err);
382 colo_receive_check_message(s->rp_state.from_dst_file,
383 COLO_MESSAGE_VMSTATE_LOADED, &local_err);
390 qemu_mutex_lock_iothread();
392 qemu_mutex_unlock_iothread();
393 trace_colo_vm_state_change("stop", "run");
397 error_report_err(local_err);
402 static void colo_process_checkpoint(MigrationState *s)
404 QIOChannelBuffer *bioc;
406 int64_t current_time = qemu_clock_get_ms(QEMU_CLOCK_HOST);
407 Error *local_err = NULL;
410 failover_init_state();
412 s->rp_state.from_dst_file = qemu_file_get_return_path(s->to_dst_file);
413 if (!s->rp_state.from_dst_file) {
414 error_report("Open QEMUFile from_dst_file failed");
419 * Wait for Secondary finish loading VM states and enter COLO
422 colo_receive_check_message(s->rp_state.from_dst_file,
423 COLO_MESSAGE_CHECKPOINT_READY, &local_err);
427 bioc = qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE);
428 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
429 object_unref(OBJECT(bioc));
431 qemu_mutex_lock_iothread();
433 qemu_mutex_unlock_iothread();
434 trace_colo_vm_state_change("stop", "run");
436 timer_mod(s->colo_delay_timer,
437 current_time + s->parameters.x_checkpoint_delay);
439 while (s->state == MIGRATION_STATUS_COLO) {
440 if (failover_get_state() != FAILOVER_STATUS_NONE) {
441 error_report("failover request");
445 qemu_sem_wait(&s->colo_checkpoint_sem);
447 ret = colo_do_checkpoint_transaction(s, bioc, fb);
454 /* Throw the unreported error message after exited from loop */
456 error_report_err(local_err);
463 timer_del(s->colo_delay_timer);
465 /* Hope this not to be too long to wait here */
466 qemu_sem_wait(&s->colo_exit_sem);
467 qemu_sem_destroy(&s->colo_exit_sem);
469 * Must be called after failover BH is completed,
470 * Or the failover BH may shutdown the wrong fd that
471 * re-used by other threads after we release here.
473 if (s->rp_state.from_dst_file) {
474 qemu_fclose(s->rp_state.from_dst_file);
478 void colo_checkpoint_notify(void *opaque)
480 MigrationState *s = opaque;
481 int64_t next_notify_time;
483 qemu_sem_post(&s->colo_checkpoint_sem);
484 s->colo_checkpoint_time = qemu_clock_get_ms(QEMU_CLOCK_HOST);
485 next_notify_time = s->colo_checkpoint_time +
486 s->parameters.x_checkpoint_delay;
487 timer_mod(s->colo_delay_timer, next_notify_time);
490 void migrate_start_colo_process(MigrationState *s)
492 qemu_mutex_unlock_iothread();
493 qemu_sem_init(&s->colo_checkpoint_sem, 0);
494 s->colo_delay_timer = timer_new_ms(QEMU_CLOCK_HOST,
495 colo_checkpoint_notify, s);
497 qemu_sem_init(&s->colo_exit_sem, 0);
498 migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE,
499 MIGRATION_STATUS_COLO);
500 colo_process_checkpoint(s);
501 qemu_mutex_lock_iothread();
504 static void colo_wait_handle_message(QEMUFile *f, int *checkpoint_request,
508 Error *local_err = NULL;
510 msg = colo_receive_message(f, &local_err);
512 error_propagate(errp, local_err);
517 case COLO_MESSAGE_CHECKPOINT_REQUEST:
518 *checkpoint_request = 1;
521 *checkpoint_request = 0;
522 error_setg(errp, "Got unknown COLO message: %d", msg);
527 void *colo_process_incoming_thread(void *opaque)
529 MigrationIncomingState *mis = opaque;
531 QIOChannelBuffer *bioc = NULL; /* Cache incoming device state */
534 Error *local_err = NULL;
536 qemu_sem_init(&mis->colo_incoming_sem, 0);
538 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
539 MIGRATION_STATUS_COLO);
541 failover_init_state();
543 mis->to_src_file = qemu_file_get_return_path(mis->from_src_file);
544 if (!mis->to_src_file) {
545 error_report("COLO incoming thread: Open QEMUFile to_src_file failed");
549 * Note: the communication between Primary side and Secondary side
550 * should be sequential, we set the fd to unblocked in migration incoming
551 * coroutine, and here we are in the COLO incoming thread, so it is ok to
552 * set the fd back to blocked.
554 qemu_file_set_blocking(mis->from_src_file, true);
556 bioc = qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE);
557 fb = qemu_fopen_channel_input(QIO_CHANNEL(bioc));
558 object_unref(OBJECT(bioc));
560 colo_send_message(mis->to_src_file, COLO_MESSAGE_CHECKPOINT_READY,
566 while (mis->state == MIGRATION_STATUS_COLO) {
569 colo_wait_handle_message(mis->from_src_file, &request, &local_err);
574 if (failover_get_state() != FAILOVER_STATUS_NONE) {
575 error_report("failover request");
579 /* FIXME: This is unnecessary for periodic checkpoint mode */
580 colo_send_message(mis->to_src_file, COLO_MESSAGE_CHECKPOINT_REPLY,
586 colo_receive_check_message(mis->from_src_file,
587 COLO_MESSAGE_VMSTATE_SEND, &local_err);
592 value = colo_receive_message_value(mis->from_src_file,
593 COLO_MESSAGE_VMSTATE_SIZE, &local_err);
599 * Read VM device state data into channel buffer,
600 * It's better to re-use the memory allocated.
601 * Here we need to handle the channel buffer directly.
603 if (value > bioc->capacity) {
604 bioc->capacity = value;
605 bioc->data = g_realloc(bioc->data, bioc->capacity);
607 total_size = qemu_get_buffer(mis->from_src_file, bioc->data, value);
608 if (total_size != value) {
609 error_report("Got %" PRIu64 " VMState data, less than expected"
610 " %" PRIu64, total_size, value);
613 bioc->usage = total_size;
614 qio_channel_io_seek(QIO_CHANNEL(bioc), 0, 0, NULL);
616 colo_send_message(mis->to_src_file, COLO_MESSAGE_VMSTATE_RECEIVED,
622 qemu_mutex_lock_iothread();
623 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
624 vmstate_loading = true;
625 if (qemu_loadvm_state(fb) < 0) {
626 error_report("COLO: loadvm failed");
627 qemu_mutex_unlock_iothread();
631 vmstate_loading = false;
632 qemu_mutex_unlock_iothread();
634 if (failover_get_state() == FAILOVER_STATUS_RELAUNCH) {
635 failover_set_state(FAILOVER_STATUS_RELAUNCH,
636 FAILOVER_STATUS_NONE);
637 failover_request_active(NULL);
641 colo_send_message(mis->to_src_file, COLO_MESSAGE_VMSTATE_LOADED,
649 vmstate_loading = false;
650 /* Throw the unreported error message after exited from loop */
652 error_report_err(local_err);
659 /* Hope this not to be too long to loop here */
660 qemu_sem_wait(&mis->colo_incoming_sem);
661 qemu_sem_destroy(&mis->colo_incoming_sem);
662 /* Must be called after failover BH is completed */
663 if (mis->to_src_file) {
664 qemu_fclose(mis->to_src_file);
666 migration_incoming_exit_colo();