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 #define COLO_BUFFER_BASE_SIZE (4 * 1024 * 1024)
25 bool colo_supported(void)
30 bool migration_in_colo_state(void)
32 MigrationState
*s
= migrate_get_current();
34 return (s
->state
== MIGRATION_STATUS_COLO
);
37 bool migration_incoming_in_colo_state(void)
39 MigrationIncomingState
*mis
= migration_incoming_get_current();
41 return mis
&& (mis
->state
== MIGRATION_STATUS_COLO
);
44 static void colo_send_message(QEMUFile
*f
, COLOMessage msg
,
49 if (msg
>= COLO_MESSAGE__MAX
) {
50 error_setg(errp
, "%s: Invalid message", __func__
);
53 qemu_put_be32(f
, msg
);
56 ret
= qemu_file_get_error(f
);
58 error_setg_errno(errp
, -ret
, "Can't send COLO message");
60 trace_colo_send_message(COLOMessage_lookup
[msg
]);
63 static void colo_send_message_value(QEMUFile
*f
, COLOMessage msg
,
64 uint64_t value
, Error
**errp
)
66 Error
*local_err
= NULL
;
69 colo_send_message(f
, msg
, &local_err
);
71 error_propagate(errp
, local_err
);
74 qemu_put_be64(f
, value
);
77 ret
= qemu_file_get_error(f
);
79 error_setg_errno(errp
, -ret
, "Failed to send value for message:%s",
80 COLOMessage_lookup
[msg
]);
84 static COLOMessage
colo_receive_message(QEMUFile
*f
, Error
**errp
)
89 msg
= qemu_get_be32(f
);
90 ret
= qemu_file_get_error(f
);
92 error_setg_errno(errp
, -ret
, "Can't receive COLO message");
95 if (msg
>= COLO_MESSAGE__MAX
) {
96 error_setg(errp
, "%s: Invalid message", __func__
);
99 trace_colo_receive_message(COLOMessage_lookup
[msg
]);
103 static void colo_receive_check_message(QEMUFile
*f
, COLOMessage expect_msg
,
107 Error
*local_err
= NULL
;
109 msg
= colo_receive_message(f
, &local_err
);
111 error_propagate(errp
, local_err
);
114 if (msg
!= expect_msg
) {
115 error_setg(errp
, "Unexpected COLO message %d, expected %d",
120 static uint64_t colo_receive_message_value(QEMUFile
*f
, uint32_t expect_msg
,
123 Error
*local_err
= NULL
;
127 colo_receive_check_message(f
, expect_msg
, &local_err
);
129 error_propagate(errp
, local_err
);
133 value
= qemu_get_be64(f
);
134 ret
= qemu_file_get_error(f
);
136 error_setg_errno(errp
, -ret
, "Failed to get value for COLO message: %s",
137 COLOMessage_lookup
[expect_msg
]);
142 static int colo_do_checkpoint_transaction(MigrationState
*s
,
143 QIOChannelBuffer
*bioc
,
146 Error
*local_err
= NULL
;
149 colo_send_message(s
->to_dst_file
, COLO_MESSAGE_CHECKPOINT_REQUEST
,
155 colo_receive_check_message(s
->rp_state
.from_dst_file
,
156 COLO_MESSAGE_CHECKPOINT_REPLY
, &local_err
);
160 /* Reset channel-buffer directly */
161 qio_channel_io_seek(QIO_CHANNEL(bioc
), 0, 0, NULL
);
164 qemu_mutex_lock_iothread();
165 vm_stop_force_state(RUN_STATE_COLO
);
166 qemu_mutex_unlock_iothread();
167 trace_colo_vm_state_change("run", "stop");
169 /* Disable block migration */
171 s
->params
.shared
= 0;
172 qemu_savevm_state_header(fb
);
173 qemu_savevm_state_begin(fb
, &s
->params
);
174 qemu_mutex_lock_iothread();
175 qemu_savevm_state_complete_precopy(fb
, false);
176 qemu_mutex_unlock_iothread();
180 colo_send_message(s
->to_dst_file
, COLO_MESSAGE_VMSTATE_SEND
, &local_err
);
185 * We need the size of the VMstate data in Secondary side,
186 * With which we can decide how much data should be read.
188 colo_send_message_value(s
->to_dst_file
, COLO_MESSAGE_VMSTATE_SIZE
,
189 bioc
->usage
, &local_err
);
194 qemu_put_buffer(s
->to_dst_file
, bioc
->data
, bioc
->usage
);
195 qemu_fflush(s
->to_dst_file
);
196 ret
= qemu_file_get_error(s
->to_dst_file
);
201 colo_receive_check_message(s
->rp_state
.from_dst_file
,
202 COLO_MESSAGE_VMSTATE_RECEIVED
, &local_err
);
207 colo_receive_check_message(s
->rp_state
.from_dst_file
,
208 COLO_MESSAGE_VMSTATE_LOADED
, &local_err
);
215 qemu_mutex_lock_iothread();
217 qemu_mutex_unlock_iothread();
218 trace_colo_vm_state_change("stop", "run");
222 error_report_err(local_err
);
227 static void colo_process_checkpoint(MigrationState
*s
)
229 QIOChannelBuffer
*bioc
;
231 int64_t current_time
, checkpoint_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
232 Error
*local_err
= NULL
;
235 s
->rp_state
.from_dst_file
= qemu_file_get_return_path(s
->to_dst_file
);
236 if (!s
->rp_state
.from_dst_file
) {
237 error_report("Open QEMUFile from_dst_file failed");
242 * Wait for Secondary finish loading VM states and enter COLO
245 colo_receive_check_message(s
->rp_state
.from_dst_file
,
246 COLO_MESSAGE_CHECKPOINT_READY
, &local_err
);
250 bioc
= qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE
);
251 fb
= qemu_fopen_channel_output(QIO_CHANNEL(bioc
));
252 object_unref(OBJECT(bioc
));
254 qemu_mutex_lock_iothread();
256 qemu_mutex_unlock_iothread();
257 trace_colo_vm_state_change("stop", "run");
259 while (s
->state
== MIGRATION_STATUS_COLO
) {
260 current_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
261 if (current_time
- checkpoint_time
<
262 s
->parameters
.x_checkpoint_delay
) {
265 delay_ms
= s
->parameters
.x_checkpoint_delay
-
266 (current_time
- checkpoint_time
);
267 g_usleep(delay_ms
* 1000);
269 ret
= colo_do_checkpoint_transaction(s
, bioc
, fb
);
273 checkpoint_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
277 /* Throw the unreported error message after exited from loop */
279 error_report_err(local_err
);
286 if (s
->rp_state
.from_dst_file
) {
287 qemu_fclose(s
->rp_state
.from_dst_file
);
291 void migrate_start_colo_process(MigrationState
*s
)
293 qemu_mutex_unlock_iothread();
294 migrate_set_state(&s
->state
, MIGRATION_STATUS_ACTIVE
,
295 MIGRATION_STATUS_COLO
);
296 colo_process_checkpoint(s
);
297 qemu_mutex_lock_iothread();
300 static void colo_wait_handle_message(QEMUFile
*f
, int *checkpoint_request
,
304 Error
*local_err
= NULL
;
306 msg
= colo_receive_message(f
, &local_err
);
308 error_propagate(errp
, local_err
);
313 case COLO_MESSAGE_CHECKPOINT_REQUEST
:
314 *checkpoint_request
= 1;
317 *checkpoint_request
= 0;
318 error_setg(errp
, "Got unknown COLO message: %d", msg
);
323 void *colo_process_incoming_thread(void *opaque
)
325 MigrationIncomingState
*mis
= opaque
;
327 QIOChannelBuffer
*bioc
= NULL
; /* Cache incoming device state */
330 Error
*local_err
= NULL
;
332 migrate_set_state(&mis
->state
, MIGRATION_STATUS_ACTIVE
,
333 MIGRATION_STATUS_COLO
);
335 mis
->to_src_file
= qemu_file_get_return_path(mis
->from_src_file
);
336 if (!mis
->to_src_file
) {
337 error_report("COLO incoming thread: Open QEMUFile to_src_file failed");
341 * Note: the communication between Primary side and Secondary side
342 * should be sequential, we set the fd to unblocked in migration incoming
343 * coroutine, and here we are in the COLO incoming thread, so it is ok to
344 * set the fd back to blocked.
346 qemu_file_set_blocking(mis
->from_src_file
, true);
348 bioc
= qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE
);
349 fb
= qemu_fopen_channel_input(QIO_CHANNEL(bioc
));
350 object_unref(OBJECT(bioc
));
352 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_CHECKPOINT_READY
,
358 while (mis
->state
== MIGRATION_STATUS_COLO
) {
361 colo_wait_handle_message(mis
->from_src_file
, &request
, &local_err
);
366 /* FIXME: This is unnecessary for periodic checkpoint mode */
367 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_CHECKPOINT_REPLY
,
373 colo_receive_check_message(mis
->from_src_file
,
374 COLO_MESSAGE_VMSTATE_SEND
, &local_err
);
379 value
= colo_receive_message_value(mis
->from_src_file
,
380 COLO_MESSAGE_VMSTATE_SIZE
, &local_err
);
386 * Read VM device state data into channel buffer,
387 * It's better to re-use the memory allocated.
388 * Here we need to handle the channel buffer directly.
390 if (value
> bioc
->capacity
) {
391 bioc
->capacity
= value
;
392 bioc
->data
= g_realloc(bioc
->data
, bioc
->capacity
);
394 total_size
= qemu_get_buffer(mis
->from_src_file
, bioc
->data
, value
);
395 if (total_size
!= value
) {
396 error_report("Got %" PRIu64
" VMState data, less than expected"
397 " %" PRIu64
, total_size
, value
);
400 bioc
->usage
= total_size
;
401 qio_channel_io_seek(QIO_CHANNEL(bioc
), 0, 0, NULL
);
403 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_VMSTATE_RECEIVED
,
409 qemu_mutex_lock_iothread();
410 qemu_system_reset(VMRESET_SILENT
);
411 if (qemu_loadvm_state(fb
) < 0) {
412 error_report("COLO: loadvm failed");
413 qemu_mutex_unlock_iothread();
416 qemu_mutex_unlock_iothread();
418 colo_send_message(mis
->to_src_file
, COLO_MESSAGE_VMSTATE_LOADED
,
426 /* Throw the unreported error message after exited from loop */
428 error_report_err(local_err
);
435 if (mis
->to_src_file
) {
436 qemu_fclose(mis
->to_src_file
);
438 migration_incoming_exit_colo();