COLO: Add 'x-colo-lost-heartbeat' command to trigger failover
[qemu/ar7.git] / migration / colo.c
blob12fa0b411157911d4e59498602cb40a569933642
1 /*
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"
18 #include "trace.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)
27 return false;
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,
45 Error **errp)
47 int ret;
49 if (msg >= COLO_MESSAGE__MAX) {
50 error_setg(errp, "%s: Invalid message", __func__);
51 return;
53 qemu_put_be32(f, msg);
54 qemu_fflush(f);
56 ret = qemu_file_get_error(f);
57 if (ret < 0) {
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;
67 int ret;
69 colo_send_message(f, msg, &local_err);
70 if (local_err) {
71 error_propagate(errp, local_err);
72 return;
74 qemu_put_be64(f, value);
75 qemu_fflush(f);
77 ret = qemu_file_get_error(f);
78 if (ret < 0) {
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)
86 COLOMessage msg;
87 int ret;
89 msg = qemu_get_be32(f);
90 ret = qemu_file_get_error(f);
91 if (ret < 0) {
92 error_setg_errno(errp, -ret, "Can't receive COLO message");
93 return msg;
95 if (msg >= COLO_MESSAGE__MAX) {
96 error_setg(errp, "%s: Invalid message", __func__);
97 return msg;
99 trace_colo_receive_message(COLOMessage_lookup[msg]);
100 return msg;
103 static void colo_receive_check_message(QEMUFile *f, COLOMessage expect_msg,
104 Error **errp)
106 COLOMessage msg;
107 Error *local_err = NULL;
109 msg = colo_receive_message(f, &local_err);
110 if (local_err) {
111 error_propagate(errp, local_err);
112 return;
114 if (msg != expect_msg) {
115 error_setg(errp, "Unexpected COLO message %d, expected %d",
116 msg, expect_msg);
120 static uint64_t colo_receive_message_value(QEMUFile *f, uint32_t expect_msg,
121 Error **errp)
123 Error *local_err = NULL;
124 uint64_t value;
125 int ret;
127 colo_receive_check_message(f, expect_msg, &local_err);
128 if (local_err) {
129 error_propagate(errp, local_err);
130 return 0;
133 value = qemu_get_be64(f);
134 ret = qemu_file_get_error(f);
135 if (ret < 0) {
136 error_setg_errno(errp, -ret, "Failed to get value for COLO message: %s",
137 COLOMessage_lookup[expect_msg]);
139 return value;
142 static int colo_do_checkpoint_transaction(MigrationState *s,
143 QIOChannelBuffer *bioc,
144 QEMUFile *fb)
146 Error *local_err = NULL;
147 int ret = -1;
149 colo_send_message(s->to_dst_file, COLO_MESSAGE_CHECKPOINT_REQUEST,
150 &local_err);
151 if (local_err) {
152 goto out;
155 colo_receive_check_message(s->rp_state.from_dst_file,
156 COLO_MESSAGE_CHECKPOINT_REPLY, &local_err);
157 if (local_err) {
158 goto out;
160 /* Reset channel-buffer directly */
161 qio_channel_io_seek(QIO_CHANNEL(bioc), 0, 0, NULL);
162 bioc->usage = 0;
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 */
170 s->params.blk = 0;
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();
178 qemu_fflush(fb);
180 colo_send_message(s->to_dst_file, COLO_MESSAGE_VMSTATE_SEND, &local_err);
181 if (local_err) {
182 goto out;
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);
190 if (local_err) {
191 goto out;
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);
197 if (ret < 0) {
198 goto out;
201 colo_receive_check_message(s->rp_state.from_dst_file,
202 COLO_MESSAGE_VMSTATE_RECEIVED, &local_err);
203 if (local_err) {
204 goto out;
207 colo_receive_check_message(s->rp_state.from_dst_file,
208 COLO_MESSAGE_VMSTATE_LOADED, &local_err);
209 if (local_err) {
210 goto out;
213 ret = 0;
215 qemu_mutex_lock_iothread();
216 vm_start();
217 qemu_mutex_unlock_iothread();
218 trace_colo_vm_state_change("stop", "run");
220 out:
221 if (local_err) {
222 error_report_err(local_err);
224 return ret;
227 static void colo_process_checkpoint(MigrationState *s)
229 QIOChannelBuffer *bioc;
230 QEMUFile *fb = NULL;
231 int64_t current_time, checkpoint_time = qemu_clock_get_ms(QEMU_CLOCK_HOST);
232 Error *local_err = NULL;
233 int ret;
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");
238 goto out;
242 * Wait for Secondary finish loading VM states and enter COLO
243 * restore.
245 colo_receive_check_message(s->rp_state.from_dst_file,
246 COLO_MESSAGE_CHECKPOINT_READY, &local_err);
247 if (local_err) {
248 goto out;
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();
255 vm_start();
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) {
263 int64_t delay_ms;
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);
270 if (ret < 0) {
271 goto out;
273 checkpoint_time = qemu_clock_get_ms(QEMU_CLOCK_HOST);
276 out:
277 /* Throw the unreported error message after exited from loop */
278 if (local_err) {
279 error_report_err(local_err);
282 if (fb) {
283 qemu_fclose(fb);
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,
301 Error **errp)
303 COLOMessage msg;
304 Error *local_err = NULL;
306 msg = colo_receive_message(f, &local_err);
307 if (local_err) {
308 error_propagate(errp, local_err);
309 return;
312 switch (msg) {
313 case COLO_MESSAGE_CHECKPOINT_REQUEST:
314 *checkpoint_request = 1;
315 break;
316 default:
317 *checkpoint_request = 0;
318 error_setg(errp, "Got unknown COLO message: %d", msg);
319 break;
323 void *colo_process_incoming_thread(void *opaque)
325 MigrationIncomingState *mis = opaque;
326 QEMUFile *fb = NULL;
327 QIOChannelBuffer *bioc = NULL; /* Cache incoming device state */
328 uint64_t total_size;
329 uint64_t value;
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");
338 goto out;
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,
353 &local_err);
354 if (local_err) {
355 goto out;
358 while (mis->state == MIGRATION_STATUS_COLO) {
359 int request;
361 colo_wait_handle_message(mis->from_src_file, &request, &local_err);
362 if (local_err) {
363 goto out;
365 assert(request);
366 /* FIXME: This is unnecessary for periodic checkpoint mode */
367 colo_send_message(mis->to_src_file, COLO_MESSAGE_CHECKPOINT_REPLY,
368 &local_err);
369 if (local_err) {
370 goto out;
373 colo_receive_check_message(mis->from_src_file,
374 COLO_MESSAGE_VMSTATE_SEND, &local_err);
375 if (local_err) {
376 goto out;
379 value = colo_receive_message_value(mis->from_src_file,
380 COLO_MESSAGE_VMSTATE_SIZE, &local_err);
381 if (local_err) {
382 goto out;
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);
398 goto out;
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,
404 &local_err);
405 if (local_err) {
406 goto out;
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();
414 goto out;
416 qemu_mutex_unlock_iothread();
418 colo_send_message(mis->to_src_file, COLO_MESSAGE_VMSTATE_LOADED,
419 &local_err);
420 if (local_err) {
421 goto out;
425 out:
426 /* Throw the unreported error message after exited from loop */
427 if (local_err) {
428 error_report_err(local_err);
431 if (fb) {
432 qemu_fclose(fb);
435 if (mis->to_src_file) {
436 qemu_fclose(mis->to_src_file);
438 migration_incoming_exit_colo();
440 return NULL;