linux-user: allocate heap memory for execve arguments
[qemu/ar7.git] / migration / colo.c
blob929b31c50c23a7d103935b9e02a514374aaac2fe
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 "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"
22 #include "trace.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)
35 return true;
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)
59 int old_state;
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]);
72 return;
75 migrate_set_state(&mis->state, MIGRATION_STATUS_COLO,
76 MIGRATION_STATUS_COMPLETED);
78 if (!autostart) {
79 error_report("\"-S\" qemu option will be ignored in secondary side");
80 /* recover runstate to normal migration finish state */
81 autostart = true;
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,
87 * It is harmless.
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]);
101 return;
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();
114 int old_state;
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]);
136 return;
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();
151 } else {
152 secondary_vm_do_failover();
156 void qmp_xen_set_replication(bool enable, bool primary,
157 bool has_failover, bool failover,
158 Error **errp)
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");
168 return;
171 if (enable) {
172 replication_start_all(mode, errp);
173 } else {
174 if (!has_failover) {
175 failover = NULL;
177 replication_stop_all(failover, failover ? NULL : errp);
179 #else
180 abort();
181 #endif
184 ReplicationStatus *qmp_query_xen_replication_status(Error **errp)
186 #ifdef CONFIG_REPLICATION
187 Error *err = NULL;
188 ReplicationStatus *s = g_new0(ReplicationStatus, 1);
190 replication_get_error_all(&err);
191 if (err) {
192 s->error = true;
193 s->has_desc = true;
194 s->desc = g_strdup(error_get_pretty(err));
195 } else {
196 s->error = false;
199 error_free(err);
200 return s;
201 #else
202 abort();
203 #endif
206 void qmp_xen_colo_do_checkpoint(Error **errp)
208 #ifdef CONFIG_REPLICATION
209 replication_do_checkpoint_all(errp);
210 #else
211 abort();
212 #endif
215 static void colo_send_message(QEMUFile *f, COLOMessage msg,
216 Error **errp)
218 int ret;
220 if (msg >= COLO_MESSAGE__MAX) {
221 error_setg(errp, "%s: Invalid message", __func__);
222 return;
224 qemu_put_be32(f, msg);
225 qemu_fflush(f);
227 ret = qemu_file_get_error(f);
228 if (ret < 0) {
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;
238 int ret;
240 colo_send_message(f, msg, &local_err);
241 if (local_err) {
242 error_propagate(errp, local_err);
243 return;
245 qemu_put_be64(f, value);
246 qemu_fflush(f);
248 ret = qemu_file_get_error(f);
249 if (ret < 0) {
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)
257 COLOMessage msg;
258 int ret;
260 msg = qemu_get_be32(f);
261 ret = qemu_file_get_error(f);
262 if (ret < 0) {
263 error_setg_errno(errp, -ret, "Can't receive COLO message");
264 return msg;
266 if (msg >= COLO_MESSAGE__MAX) {
267 error_setg(errp, "%s: Invalid message", __func__);
268 return msg;
270 trace_colo_receive_message(COLOMessage_lookup[msg]);
271 return msg;
274 static void colo_receive_check_message(QEMUFile *f, COLOMessage expect_msg,
275 Error **errp)
277 COLOMessage msg;
278 Error *local_err = NULL;
280 msg = colo_receive_message(f, &local_err);
281 if (local_err) {
282 error_propagate(errp, local_err);
283 return;
285 if (msg != expect_msg) {
286 error_setg(errp, "Unexpected COLO message %d, expected %d",
287 msg, expect_msg);
291 static uint64_t colo_receive_message_value(QEMUFile *f, uint32_t expect_msg,
292 Error **errp)
294 Error *local_err = NULL;
295 uint64_t value;
296 int ret;
298 colo_receive_check_message(f, expect_msg, &local_err);
299 if (local_err) {
300 error_propagate(errp, local_err);
301 return 0;
304 value = qemu_get_be64(f);
305 ret = qemu_file_get_error(f);
306 if (ret < 0) {
307 error_setg_errno(errp, -ret, "Failed to get value for COLO message: %s",
308 COLOMessage_lookup[expect_msg]);
310 return value;
313 static int colo_do_checkpoint_transaction(MigrationState *s,
314 QIOChannelBuffer *bioc,
315 QEMUFile *fb)
317 Error *local_err = NULL;
318 int ret = -1;
320 colo_send_message(s->to_dst_file, COLO_MESSAGE_CHECKPOINT_REQUEST,
321 &local_err);
322 if (local_err) {
323 goto out;
326 colo_receive_check_message(s->rp_state.from_dst_file,
327 COLO_MESSAGE_CHECKPOINT_REPLY, &local_err);
328 if (local_err) {
329 goto out;
331 /* Reset channel-buffer directly */
332 qio_channel_io_seek(QIO_CHANNEL(bioc), 0, 0, NULL);
333 bioc->usage = 0;
335 qemu_mutex_lock_iothread();
336 if (failover_get_state() != FAILOVER_STATUS_NONE) {
337 qemu_mutex_unlock_iothread();
338 goto out;
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) {
348 goto out;
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();
359 qemu_fflush(fb);
361 colo_send_message(s->to_dst_file, COLO_MESSAGE_VMSTATE_SEND, &local_err);
362 if (local_err) {
363 goto out;
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);
371 if (local_err) {
372 goto out;
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);
378 if (ret < 0) {
379 goto out;
382 colo_receive_check_message(s->rp_state.from_dst_file,
383 COLO_MESSAGE_VMSTATE_RECEIVED, &local_err);
384 if (local_err) {
385 goto out;
388 colo_receive_check_message(s->rp_state.from_dst_file,
389 COLO_MESSAGE_VMSTATE_LOADED, &local_err);
390 if (local_err) {
391 goto out;
394 ret = 0;
396 qemu_mutex_lock_iothread();
397 vm_start();
398 qemu_mutex_unlock_iothread();
399 trace_colo_vm_state_change("stop", "run");
401 out:
402 if (local_err) {
403 error_report_err(local_err);
405 return ret;
408 static void colo_process_checkpoint(MigrationState *s)
410 QIOChannelBuffer *bioc;
411 QEMUFile *fb = NULL;
412 int64_t current_time = qemu_clock_get_ms(QEMU_CLOCK_HOST);
413 Error *local_err = NULL;
414 int ret;
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");
421 goto out;
425 * Wait for Secondary finish loading VM states and enter COLO
426 * restore.
428 colo_receive_check_message(s->rp_state.from_dst_file,
429 COLO_MESSAGE_CHECKPOINT_READY, &local_err);
430 if (local_err) {
431 goto out;
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();
438 vm_start();
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");
448 goto out;
451 qemu_sem_wait(&s->colo_checkpoint_sem);
453 ret = colo_do_checkpoint_transaction(s, bioc, fb);
454 if (ret < 0) {
455 goto out;
459 out:
460 /* Throw the unreported error message after exited from loop */
461 if (local_err) {
462 error_report_err(local_err);
465 if (fb) {
466 qemu_fclose(fb);
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,
511 Error **errp)
513 COLOMessage msg;
514 Error *local_err = NULL;
516 msg = colo_receive_message(f, &local_err);
517 if (local_err) {
518 error_propagate(errp, local_err);
519 return;
522 switch (msg) {
523 case COLO_MESSAGE_CHECKPOINT_REQUEST:
524 *checkpoint_request = 1;
525 break;
526 default:
527 *checkpoint_request = 0;
528 error_setg(errp, "Got unknown COLO message: %d", msg);
529 break;
533 void *colo_process_incoming_thread(void *opaque)
535 MigrationIncomingState *mis = opaque;
536 QEMUFile *fb = NULL;
537 QIOChannelBuffer *bioc = NULL; /* Cache incoming device state */
538 uint64_t total_size;
539 uint64_t value;
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");
552 goto out;
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,
567 &local_err);
568 if (local_err) {
569 goto out;
572 while (mis->state == MIGRATION_STATUS_COLO) {
573 int request = 0;
575 colo_wait_handle_message(mis->from_src_file, &request, &local_err);
576 if (local_err) {
577 goto out;
579 assert(request);
580 if (failover_get_state() != FAILOVER_STATUS_NONE) {
581 error_report("failover request");
582 goto out;
585 /* FIXME: This is unnecessary for periodic checkpoint mode */
586 colo_send_message(mis->to_src_file, COLO_MESSAGE_CHECKPOINT_REPLY,
587 &local_err);
588 if (local_err) {
589 goto out;
592 colo_receive_check_message(mis->from_src_file,
593 COLO_MESSAGE_VMSTATE_SEND, &local_err);
594 if (local_err) {
595 goto out;
598 value = colo_receive_message_value(mis->from_src_file,
599 COLO_MESSAGE_VMSTATE_SIZE, &local_err);
600 if (local_err) {
601 goto out;
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);
617 goto out;
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,
623 &local_err);
624 if (local_err) {
625 goto out;
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();
634 goto out;
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);
644 goto out;
647 colo_send_message(mis->to_src_file, COLO_MESSAGE_VMSTATE_LOADED,
648 &local_err);
649 if (local_err) {
650 goto out;
654 out:
655 vmstate_loading = false;
656 /* Throw the unreported error message after exited from loop */
657 if (local_err) {
658 error_report_err(local_err);
661 if (fb) {
662 qemu_fclose(fb);
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();
674 return NULL;