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 "migration/colo.h"
17 #include "io/channel-buffer.h"
19 #include "qemu/error-report.h"
20 #include "qapi/error.h"
21 #include "migration/failover.h"
23 static bool vmstate_loading
;
25 #define COLO_BUFFER_BASE_SIZE (4 * 1024 * 1024)
27 bool colo_supported(void)
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_lookup
[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_lookup
[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_lookup
[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 static void colo_send_message(QEMUFile
*f
, COLOMessage msg
,
155 if (msg
>= COLO_MESSAGE__MAX
) {
156 error_setg(errp
, "%s: Invalid message", __func__
);
159 qemu_put_be32(f
, msg
);
162 ret
= qemu_file_get_error(f
);
164 error_setg_errno(errp
, -ret
, "Can't send COLO message");
166 trace_colo_send_message(COLOMessage_lookup
[msg
]);
169 static void colo_send_message_value(QEMUFile
*f
, COLOMessage msg
,
170 uint64_t value
, Error
**errp
)
172 Error
*local_err
= NULL
;
175 colo_send_message(f
, msg
, &local_err
);
177 error_propagate(errp
, local_err
);
180 qemu_put_be64(f
, value
);
183 ret
= qemu_file_get_error(f
);
185 error_setg_errno(errp
, -ret
, "Failed to send value for message:%s",
186 COLOMessage_lookup
[msg
]);
190 static COLOMessage
colo_receive_message(QEMUFile
*f
, Error
**errp
)
195 msg
= qemu_get_be32(f
);
196 ret
= qemu_file_get_error(f
);
198 error_setg_errno(errp
, -ret
, "Can't receive COLO message");
201 if (msg
>= COLO_MESSAGE__MAX
) {
202 error_setg(errp
, "%s: Invalid message", __func__
);
205 trace_colo_receive_message(COLOMessage_lookup
[msg
]);
209 static void colo_receive_check_message(QEMUFile
*f
, COLOMessage expect_msg
,
213 Error
*local_err
= NULL
;
215 msg
= colo_receive_message(f
, &local_err
);
217 error_propagate(errp
, local_err
);
220 if (msg
!= expect_msg
) {
221 error_setg(errp
, "Unexpected COLO message %d, expected %d",
226 static uint64_t colo_receive_message_value(QEMUFile
*f
, uint32_t expect_msg
,
229 Error
*local_err
= NULL
;
233 colo_receive_check_message(f
, expect_msg
, &local_err
);
235 error_propagate(errp
, local_err
);
239 value
= qemu_get_be64(f
);
240 ret
= qemu_file_get_error(f
);
242 error_setg_errno(errp
, -ret
, "Failed to get value for COLO message: %s",
243 COLOMessage_lookup
[expect_msg
]);
248 static int colo_do_checkpoint_transaction(MigrationState
*s
,
249 QIOChannelBuffer
*bioc
,
252 Error
*local_err
= NULL
;
255 colo_send_message(s
->to_dst_file
, COLO_MESSAGE_CHECKPOINT_REQUEST
,
261 colo_receive_check_message(s
->rp_state
.from_dst_file
,
262 COLO_MESSAGE_CHECKPOINT_REPLY
, &local_err
);
266 /* Reset channel-buffer directly */
267 qio_channel_io_seek(QIO_CHANNEL(bioc
), 0, 0, NULL
);
270 qemu_mutex_lock_iothread();
271 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
272 qemu_mutex_unlock_iothread();
275 vm_stop_force_state(RUN_STATE_COLO
);
276 qemu_mutex_unlock_iothread();
277 trace_colo_vm_state_change("run", "stop");
279 * Failover request bh could be called after vm_stop_force_state(),
280 * So we need check failover_request_is_active() again.
282 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
286 /* Disable block migration */
288 s
->params
.shared
= 0;
289 qemu_savevm_state_header(fb
);
290 qemu_savevm_state_begin(fb
, &s
->params
);
291 qemu_mutex_lock_iothread();
292 qemu_savevm_state_complete_precopy(fb
, false);
293 qemu_mutex_unlock_iothread();
297 colo_send_message(s
->to_dst_file
, COLO_MESSAGE_VMSTATE_SEND
, &local_err
);
302 * We need the size of the VMstate data in Secondary side,
303 * With which we can decide how much data should be read.
305 colo_send_message_value(s
->to_dst_file
, COLO_MESSAGE_VMSTATE_SIZE
,
306 bioc
->usage
, &local_err
);
311 qemu_put_buffer(s
->to_dst_file
, bioc
->data
, bioc
->usage
);
312 qemu_fflush(s
->to_dst_file
);
313 ret
= qemu_file_get_error(s
->to_dst_file
);
318 colo_receive_check_message(s
->rp_state
.from_dst_file
,
319 COLO_MESSAGE_VMSTATE_RECEIVED
, &local_err
);
324 colo_receive_check_message(s
->rp_state
.from_dst_file
,
325 COLO_MESSAGE_VMSTATE_LOADED
, &local_err
);
332 qemu_mutex_lock_iothread();
334 qemu_mutex_unlock_iothread();
335 trace_colo_vm_state_change("stop", "run");
339 error_report_err(local_err
);
344 static void colo_process_checkpoint(MigrationState
*s
)
346 QIOChannelBuffer
*bioc
;
348 int64_t current_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
349 Error
*local_err
= NULL
;
352 failover_init_state();
354 s
->rp_state
.from_dst_file
= qemu_file_get_return_path(s
->to_dst_file
);
355 if (!s
->rp_state
.from_dst_file
) {
356 error_report("Open QEMUFile from_dst_file failed");
361 * Wait for Secondary finish loading VM states and enter COLO
364 colo_receive_check_message(s
->rp_state
.from_dst_file
,
365 COLO_MESSAGE_CHECKPOINT_READY
, &local_err
);
369 bioc
= qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE
);
370 fb
= qemu_fopen_channel_output(QIO_CHANNEL(bioc
));
371 object_unref(OBJECT(bioc
));
373 qemu_mutex_lock_iothread();
375 qemu_mutex_unlock_iothread();
376 trace_colo_vm_state_change("stop", "run");
378 timer_mod(s
->colo_delay_timer
,
379 current_time
+ s
->parameters
.x_checkpoint_delay
);
381 while (s
->state
== MIGRATION_STATUS_COLO
) {
382 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
383 error_report("failover request");
387 qemu_sem_wait(&s
->colo_checkpoint_sem
);
389 ret
= colo_do_checkpoint_transaction(s
, bioc
, fb
);
396 /* Throw the unreported error message after exited from loop */
398 error_report_err(local_err
);
405 timer_del(s
->colo_delay_timer
);
407 /* Hope this not to be too long to wait here */
408 qemu_sem_wait(&s
->colo_exit_sem
);
409 qemu_sem_destroy(&s
->colo_exit_sem
);
411 * Must be called after failover BH is completed,
412 * Or the failover BH may shutdown the wrong fd that
413 * re-used by other threads after we release here.
415 if (s
->rp_state
.from_dst_file
) {
416 qemu_fclose(s
->rp_state
.from_dst_file
);
420 void colo_checkpoint_notify(void *opaque
)
422 MigrationState
*s
= opaque
;
423 int64_t next_notify_time
;
425 qemu_sem_post(&s
->colo_checkpoint_sem
);
426 s
->colo_checkpoint_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
427 next_notify_time
= s
->colo_checkpoint_time
+
428 s
->parameters
.x_checkpoint_delay
;
429 timer_mod(s
->colo_delay_timer
, next_notify_time
);
432 void migrate_start_colo_process(MigrationState
*s
)
434 qemu_mutex_unlock_iothread();
435 qemu_sem_init(&s
->colo_checkpoint_sem
, 0);
436 s
->colo_delay_timer
= timer_new_ms(QEMU_CLOCK_HOST
,
437 colo_checkpoint_notify
, s
);
439 qemu_sem_init(&s
->colo_exit_sem
, 0);
440 migrate_set_state(&s
->state
, MIGRATION_STATUS_ACTIVE
,
441 MIGRATION_STATUS_COLO
);
442 colo_process_checkpoint(s
);
443 qemu_mutex_lock_iothread();
446 static void colo_wait_handle_message(QEMUFile
*f
, int *checkpoint_request
,
450 Error
*local_err
= NULL
;
452 msg
= colo_receive_message(f
, &local_err
);
454 error_propagate(errp
, local_err
);
459 case COLO_MESSAGE_CHECKPOINT_REQUEST
:
460 *checkpoint_request
= 1;
463 *checkpoint_request
= 0;
464 error_setg(errp
, "Got unknown COLO message: %d", msg
);
469 void *colo_process_incoming_thread(void *opaque
)
471 MigrationIncomingState
*mis
= opaque
;
473 QIOChannelBuffer
*bioc
= NULL
; /* Cache incoming device state */
476 Error
*local_err
= NULL
;
478 qemu_sem_init(&mis
->colo_incoming_sem
, 0);
480 migrate_set_state(&mis
->state
, MIGRATION_STATUS_ACTIVE
,
481 MIGRATION_STATUS_COLO
);
483 failover_init_state();
485 mis
->to_src_file
= qemu_file_get_return_path(mis
->from_src_file
);
486 if (!mis
->to_src_file
) {
487 error_report("COLO incoming thread: Open QEMUFile to_src_file failed");
491 * Note: the communication between Primary side and Secondary side
492 * should be sequential, we set the fd to unblocked in migration incoming
493 * coroutine, and here we are in the COLO incoming thread, so it is ok to
494 * set the fd back to blocked.
496 qemu_file_set_blocking(mis
->from_src_file
, true);
498 bioc
= qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE
);
499 fb
= qemu_fopen_channel_input(QIO_CHANNEL(bioc
));
500 object_unref(OBJECT(bioc
));
502 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_CHECKPOINT_READY
,
508 while (mis
->state
== MIGRATION_STATUS_COLO
) {
511 colo_wait_handle_message(mis
->from_src_file
, &request
, &local_err
);
516 if (failover_get_state() != FAILOVER_STATUS_NONE
) {
517 error_report("failover request");
521 /* FIXME: This is unnecessary for periodic checkpoint mode */
522 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_CHECKPOINT_REPLY
,
528 colo_receive_check_message(mis
->from_src_file
,
529 COLO_MESSAGE_VMSTATE_SEND
, &local_err
);
534 value
= colo_receive_message_value(mis
->from_src_file
,
535 COLO_MESSAGE_VMSTATE_SIZE
, &local_err
);
541 * Read VM device state data into channel buffer,
542 * It's better to re-use the memory allocated.
543 * Here we need to handle the channel buffer directly.
545 if (value
> bioc
->capacity
) {
546 bioc
->capacity
= value
;
547 bioc
->data
= g_realloc(bioc
->data
, bioc
->capacity
);
549 total_size
= qemu_get_buffer(mis
->from_src_file
, bioc
->data
, value
);
550 if (total_size
!= value
) {
551 error_report("Got %" PRIu64
" VMState data, less than expected"
552 " %" PRIu64
, total_size
, value
);
555 bioc
->usage
= total_size
;
556 qio_channel_io_seek(QIO_CHANNEL(bioc
), 0, 0, NULL
);
558 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_VMSTATE_RECEIVED
,
564 qemu_mutex_lock_iothread();
565 qemu_system_reset(VMRESET_SILENT
);
566 vmstate_loading
= true;
567 if (qemu_loadvm_state(fb
) < 0) {
568 error_report("COLO: loadvm failed");
569 qemu_mutex_unlock_iothread();
573 vmstate_loading
= false;
574 qemu_mutex_unlock_iothread();
576 if (failover_get_state() == FAILOVER_STATUS_RELAUNCH
) {
577 failover_set_state(FAILOVER_STATUS_RELAUNCH
,
578 FAILOVER_STATUS_NONE
);
579 failover_request_active(NULL
);
583 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_VMSTATE_LOADED
,
591 vmstate_loading
= false;
592 /* Throw the unreported error message after exited from loop */
594 error_report_err(local_err
);
601 /* Hope this not to be too long to loop here */
602 qemu_sem_wait(&mis
->colo_incoming_sem
);
603 qemu_sem_destroy(&mis
->colo_incoming_sem
);
604 /* Must be called after failover BH is completed */
605 if (mis
->to_src_file
) {
606 qemu_fclose(mis
->to_src_file
);
608 migration_incoming_exit_colo();