colo: make colo_checkpoint_notify static and provide simpler API
[qemu/armbru.git] / migration / colo.c
blobc9e0b909b9f0d691327800fa06b14996943d4f11
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 "sysemu/sysemu.h"
15 #include "qapi/error.h"
16 #include "qapi/qapi-commands-migration.h"
17 #include "migration.h"
18 #include "qemu-file.h"
19 #include "savevm.h"
20 #include "migration/colo.h"
21 #include "block.h"
22 #include "io/channel-buffer.h"
23 #include "trace.h"
24 #include "qemu/error-report.h"
25 #include "qemu/main-loop.h"
26 #include "qemu/rcu.h"
27 #include "migration/failover.h"
28 #include "migration/ram.h"
29 #ifdef CONFIG_REPLICATION
30 #include "block/replication.h"
31 #endif
32 #include "net/colo-compare.h"
33 #include "net/colo.h"
34 #include "block/block.h"
35 #include "qapi/qapi-events-migration.h"
36 #include "sysemu/cpus.h"
37 #include "sysemu/runstate.h"
38 #include "net/filter.h"
39 #include "options.h"
41 static bool vmstate_loading;
42 static Notifier packets_compare_notifier;
44 /* User need to know colo mode after COLO failover */
45 static COLOMode last_colo_mode;
47 #define COLO_BUFFER_BASE_SIZE (4 * 1024 * 1024)
49 bool migration_in_colo_state(void)
51 MigrationState *s = migrate_get_current();
53 return (s->state == MIGRATION_STATUS_COLO);
56 bool migration_incoming_in_colo_state(void)
58 MigrationIncomingState *mis = migration_incoming_get_current();
60 return mis && (mis->state == MIGRATION_STATUS_COLO);
63 static bool colo_runstate_is_stopped(void)
65 return runstate_check(RUN_STATE_COLO) || !runstate_is_running();
68 static void colo_checkpoint_notify(void *opaque)
70 MigrationState *s = opaque;
71 int64_t next_notify_time;
73 qemu_event_set(&s->colo_checkpoint_event);
74 s->colo_checkpoint_time = qemu_clock_get_ms(QEMU_CLOCK_HOST);
75 next_notify_time = s->colo_checkpoint_time + migrate_checkpoint_delay();
76 timer_mod(s->colo_delay_timer, next_notify_time);
79 void colo_checkpoint_delay_set(void)
81 if (migration_in_colo_state()) {
82 colo_checkpoint_notify(migrate_get_current());
86 static void secondary_vm_do_failover(void)
88 /* COLO needs enable block-replication */
89 #ifdef CONFIG_REPLICATION
90 int old_state;
91 MigrationIncomingState *mis = migration_incoming_get_current();
92 Error *local_err = NULL;
94 /* Can not do failover during the process of VM's loading VMstate, Or
95 * it will break the secondary VM.
97 if (vmstate_loading) {
98 old_state = failover_set_state(FAILOVER_STATUS_ACTIVE,
99 FAILOVER_STATUS_RELAUNCH);
100 if (old_state != FAILOVER_STATUS_ACTIVE) {
101 error_report("Unknown error while do failover for secondary VM,"
102 "old_state: %s", FailoverStatus_str(old_state));
104 return;
107 migrate_set_state(&mis->state, MIGRATION_STATUS_COLO,
108 MIGRATION_STATUS_COMPLETED);
110 replication_stop_all(true, &local_err);
111 if (local_err) {
112 error_report_err(local_err);
113 local_err = NULL;
116 /* Notify all filters of all NIC to do checkpoint */
117 colo_notify_filters_event(COLO_EVENT_FAILOVER, &local_err);
118 if (local_err) {
119 error_report_err(local_err);
122 if (!autostart) {
123 error_report("\"-S\" qemu option will be ignored in secondary side");
124 /* recover runstate to normal migration finish state */
125 autostart = true;
128 * Make sure COLO incoming thread not block in recv or send,
129 * If mis->from_src_file and mis->to_src_file use the same fd,
130 * The second shutdown() will return -1, we ignore this value,
131 * It is harmless.
133 if (mis->from_src_file) {
134 qemu_file_shutdown(mis->from_src_file);
136 if (mis->to_src_file) {
137 qemu_file_shutdown(mis->to_src_file);
140 old_state = failover_set_state(FAILOVER_STATUS_ACTIVE,
141 FAILOVER_STATUS_COMPLETED);
142 if (old_state != FAILOVER_STATUS_ACTIVE) {
143 error_report("Incorrect state (%s) while doing failover for "
144 "secondary VM", FailoverStatus_str(old_state));
145 return;
147 /* Notify COLO incoming thread that failover work is finished */
148 qemu_sem_post(&mis->colo_incoming_sem);
150 /* For Secondary VM, jump to incoming co */
151 if (mis->migration_incoming_co) {
152 qemu_coroutine_enter(mis->migration_incoming_co);
154 #else
155 abort();
156 #endif
159 static void primary_vm_do_failover(void)
161 #ifdef CONFIG_REPLICATION
162 MigrationState *s = migrate_get_current();
163 int old_state;
164 Error *local_err = NULL;
166 migrate_set_state(&s->state, MIGRATION_STATUS_COLO,
167 MIGRATION_STATUS_COMPLETED);
169 * kick COLO thread which might wait at
170 * qemu_sem_wait(&s->colo_checkpoint_sem).
172 colo_checkpoint_notify(s);
175 * Wake up COLO thread which may blocked in recv() or send(),
176 * The s->rp_state.from_dst_file and s->to_dst_file may use the
177 * same fd, but we still shutdown the fd for twice, it is harmless.
179 if (s->to_dst_file) {
180 qemu_file_shutdown(s->to_dst_file);
182 if (s->rp_state.from_dst_file) {
183 qemu_file_shutdown(s->rp_state.from_dst_file);
186 old_state = failover_set_state(FAILOVER_STATUS_ACTIVE,
187 FAILOVER_STATUS_COMPLETED);
188 if (old_state != FAILOVER_STATUS_ACTIVE) {
189 error_report("Incorrect state (%s) while doing failover for Primary VM",
190 FailoverStatus_str(old_state));
191 return;
194 replication_stop_all(true, &local_err);
195 if (local_err) {
196 error_report_err(local_err);
197 local_err = NULL;
200 /* Notify COLO thread that failover work is finished */
201 qemu_sem_post(&s->colo_exit_sem);
202 #else
203 abort();
204 #endif
207 COLOMode get_colo_mode(void)
209 if (migration_in_colo_state()) {
210 return COLO_MODE_PRIMARY;
211 } else if (migration_incoming_in_colo_state()) {
212 return COLO_MODE_SECONDARY;
213 } else {
214 return COLO_MODE_NONE;
218 void colo_do_failover(void)
220 /* Make sure VM stopped while failover happened. */
221 if (!colo_runstate_is_stopped()) {
222 vm_stop_force_state(RUN_STATE_COLO);
225 switch (last_colo_mode = get_colo_mode()) {
226 case COLO_MODE_PRIMARY:
227 primary_vm_do_failover();
228 break;
229 case COLO_MODE_SECONDARY:
230 secondary_vm_do_failover();
231 break;
232 default:
233 error_report("colo_do_failover failed because the colo mode"
234 " could not be obtained");
238 #ifdef CONFIG_REPLICATION
239 void qmp_xen_set_replication(bool enable, bool primary,
240 bool has_failover, bool failover,
241 Error **errp)
243 ReplicationMode mode = primary ?
244 REPLICATION_MODE_PRIMARY :
245 REPLICATION_MODE_SECONDARY;
247 if (has_failover && enable) {
248 error_setg(errp, "Parameter 'failover' is only for"
249 " stopping replication");
250 return;
253 if (enable) {
254 replication_start_all(mode, errp);
255 } else {
256 if (!has_failover) {
257 failover = NULL;
259 replication_stop_all(failover, failover ? NULL : errp);
263 ReplicationStatus *qmp_query_xen_replication_status(Error **errp)
265 Error *err = NULL;
266 ReplicationStatus *s = g_new0(ReplicationStatus, 1);
268 replication_get_error_all(&err);
269 if (err) {
270 s->error = true;
271 s->desc = g_strdup(error_get_pretty(err));
272 } else {
273 s->error = false;
276 error_free(err);
277 return s;
280 void qmp_xen_colo_do_checkpoint(Error **errp)
282 Error *err = NULL;
284 replication_do_checkpoint_all(&err);
285 if (err) {
286 error_propagate(errp, err);
287 return;
289 /* Notify all filters of all NIC to do checkpoint */
290 colo_notify_filters_event(COLO_EVENT_CHECKPOINT, errp);
292 #endif
294 COLOStatus *qmp_query_colo_status(Error **errp)
296 COLOStatus *s = g_new0(COLOStatus, 1);
298 s->mode = get_colo_mode();
299 s->last_mode = last_colo_mode;
301 switch (failover_get_state()) {
302 case FAILOVER_STATUS_NONE:
303 s->reason = COLO_EXIT_REASON_NONE;
304 break;
305 case FAILOVER_STATUS_COMPLETED:
306 s->reason = COLO_EXIT_REASON_REQUEST;
307 break;
308 default:
309 if (migration_in_colo_state()) {
310 s->reason = COLO_EXIT_REASON_PROCESSING;
311 } else {
312 s->reason = COLO_EXIT_REASON_ERROR;
316 return s;
319 static void colo_send_message(QEMUFile *f, COLOMessage msg,
320 Error **errp)
322 int ret;
324 if (msg >= COLO_MESSAGE__MAX) {
325 error_setg(errp, "%s: Invalid message", __func__);
326 return;
328 qemu_put_be32(f, msg);
329 qemu_fflush(f);
331 ret = qemu_file_get_error(f);
332 if (ret < 0) {
333 error_setg_errno(errp, -ret, "Can't send COLO message");
335 trace_colo_send_message(COLOMessage_str(msg));
338 static void colo_send_message_value(QEMUFile *f, COLOMessage msg,
339 uint64_t value, Error **errp)
341 Error *local_err = NULL;
342 int ret;
344 colo_send_message(f, msg, &local_err);
345 if (local_err) {
346 error_propagate(errp, local_err);
347 return;
349 qemu_put_be64(f, value);
350 qemu_fflush(f);
352 ret = qemu_file_get_error(f);
353 if (ret < 0) {
354 error_setg_errno(errp, -ret, "Failed to send value for message:%s",
355 COLOMessage_str(msg));
359 static COLOMessage colo_receive_message(QEMUFile *f, Error **errp)
361 COLOMessage msg;
362 int ret;
364 msg = qemu_get_be32(f);
365 ret = qemu_file_get_error(f);
366 if (ret < 0) {
367 error_setg_errno(errp, -ret, "Can't receive COLO message");
368 return msg;
370 if (msg >= COLO_MESSAGE__MAX) {
371 error_setg(errp, "%s: Invalid message", __func__);
372 return msg;
374 trace_colo_receive_message(COLOMessage_str(msg));
375 return msg;
378 static void colo_receive_check_message(QEMUFile *f, COLOMessage expect_msg,
379 Error **errp)
381 COLOMessage msg;
382 Error *local_err = NULL;
384 msg = colo_receive_message(f, &local_err);
385 if (local_err) {
386 error_propagate(errp, local_err);
387 return;
389 if (msg != expect_msg) {
390 error_setg(errp, "Unexpected COLO message %d, expected %d",
391 msg, expect_msg);
395 static uint64_t colo_receive_message_value(QEMUFile *f, uint32_t expect_msg,
396 Error **errp)
398 Error *local_err = NULL;
399 uint64_t value;
400 int ret;
402 colo_receive_check_message(f, expect_msg, &local_err);
403 if (local_err) {
404 error_propagate(errp, local_err);
405 return 0;
408 value = qemu_get_be64(f);
409 ret = qemu_file_get_error(f);
410 if (ret < 0) {
411 error_setg_errno(errp, -ret, "Failed to get value for COLO message: %s",
412 COLOMessage_str(expect_msg));
414 return value;
417 static int colo_do_checkpoint_transaction(MigrationState *s,
418 QIOChannelBuffer *bioc,
419 QEMUFile *fb)
421 Error *local_err = NULL;
422 int ret = -1;
424 colo_send_message(s->to_dst_file, COLO_MESSAGE_CHECKPOINT_REQUEST,
425 &local_err);
426 if (local_err) {
427 goto out;
430 colo_receive_check_message(s->rp_state.from_dst_file,
431 COLO_MESSAGE_CHECKPOINT_REPLY, &local_err);
432 if (local_err) {
433 goto out;
435 /* Reset channel-buffer directly */
436 qio_channel_io_seek(QIO_CHANNEL(bioc), 0, 0, NULL);
437 bioc->usage = 0;
439 qemu_mutex_lock_iothread();
440 if (failover_get_state() != FAILOVER_STATUS_NONE) {
441 qemu_mutex_unlock_iothread();
442 goto out;
444 vm_stop_force_state(RUN_STATE_COLO);
445 qemu_mutex_unlock_iothread();
446 trace_colo_vm_state_change("run", "stop");
448 * Failover request bh could be called after vm_stop_force_state(),
449 * So we need check failover_request_is_active() again.
451 if (failover_get_state() != FAILOVER_STATUS_NONE) {
452 goto out;
454 qemu_mutex_lock_iothread();
456 #ifdef CONFIG_REPLICATION
457 replication_do_checkpoint_all(&local_err);
458 if (local_err) {
459 qemu_mutex_unlock_iothread();
460 goto out;
462 #else
463 abort();
464 #endif
466 colo_send_message(s->to_dst_file, COLO_MESSAGE_VMSTATE_SEND, &local_err);
467 if (local_err) {
468 qemu_mutex_unlock_iothread();
469 goto out;
471 /* Note: device state is saved into buffer */
472 ret = qemu_save_device_state(fb);
474 qemu_mutex_unlock_iothread();
475 if (ret < 0) {
476 goto out;
479 if (migrate_auto_converge()) {
480 mig_throttle_counter_reset();
483 * Only save VM's live state, which not including device state.
484 * TODO: We may need a timeout mechanism to prevent COLO process
485 * to be blocked here.
487 qemu_savevm_live_state(s->to_dst_file);
489 qemu_fflush(fb);
492 * We need the size of the VMstate data in Secondary side,
493 * With which we can decide how much data should be read.
495 colo_send_message_value(s->to_dst_file, COLO_MESSAGE_VMSTATE_SIZE,
496 bioc->usage, &local_err);
497 if (local_err) {
498 goto out;
501 qemu_put_buffer(s->to_dst_file, bioc->data, bioc->usage);
502 qemu_fflush(s->to_dst_file);
503 ret = qemu_file_get_error(s->to_dst_file);
504 if (ret < 0) {
505 goto out;
508 colo_receive_check_message(s->rp_state.from_dst_file,
509 COLO_MESSAGE_VMSTATE_RECEIVED, &local_err);
510 if (local_err) {
511 goto out;
514 qemu_event_reset(&s->colo_checkpoint_event);
515 colo_notify_compares_event(NULL, COLO_EVENT_CHECKPOINT, &local_err);
516 if (local_err) {
517 goto out;
520 colo_receive_check_message(s->rp_state.from_dst_file,
521 COLO_MESSAGE_VMSTATE_LOADED, &local_err);
522 if (local_err) {
523 goto out;
526 ret = 0;
528 qemu_mutex_lock_iothread();
529 vm_start();
530 qemu_mutex_unlock_iothread();
531 trace_colo_vm_state_change("stop", "run");
533 out:
534 if (local_err) {
535 error_report_err(local_err);
537 return ret;
540 static void colo_compare_notify_checkpoint(Notifier *notifier, void *data)
542 colo_checkpoint_notify(data);
545 static void colo_process_checkpoint(MigrationState *s)
547 QIOChannelBuffer *bioc;
548 QEMUFile *fb = NULL;
549 Error *local_err = NULL;
550 int ret;
552 if (get_colo_mode() != COLO_MODE_PRIMARY) {
553 error_report("COLO mode must be COLO_MODE_PRIMARY");
554 return;
557 failover_init_state();
559 s->rp_state.from_dst_file = qemu_file_get_return_path(s->to_dst_file);
560 if (!s->rp_state.from_dst_file) {
561 error_report("Open QEMUFile from_dst_file failed");
562 goto out;
565 packets_compare_notifier.notify = colo_compare_notify_checkpoint;
566 colo_compare_register_notifier(&packets_compare_notifier);
569 * Wait for Secondary finish loading VM states and enter COLO
570 * restore.
572 colo_receive_check_message(s->rp_state.from_dst_file,
573 COLO_MESSAGE_CHECKPOINT_READY, &local_err);
574 if (local_err) {
575 goto out;
577 bioc = qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE);
578 fb = qemu_file_new_output(QIO_CHANNEL(bioc));
579 object_unref(OBJECT(bioc));
581 qemu_mutex_lock_iothread();
582 #ifdef CONFIG_REPLICATION
583 replication_start_all(REPLICATION_MODE_PRIMARY, &local_err);
584 if (local_err) {
585 qemu_mutex_unlock_iothread();
586 goto out;
588 #else
589 abort();
590 #endif
592 vm_start();
593 qemu_mutex_unlock_iothread();
594 trace_colo_vm_state_change("stop", "run");
596 timer_mod(s->colo_delay_timer, qemu_clock_get_ms(QEMU_CLOCK_HOST) +
597 migrate_checkpoint_delay());
599 while (s->state == MIGRATION_STATUS_COLO) {
600 if (failover_get_state() != FAILOVER_STATUS_NONE) {
601 error_report("failover request");
602 goto out;
605 qemu_event_wait(&s->colo_checkpoint_event);
607 if (s->state != MIGRATION_STATUS_COLO) {
608 goto out;
610 ret = colo_do_checkpoint_transaction(s, bioc, fb);
611 if (ret < 0) {
612 goto out;
616 out:
617 /* Throw the unreported error message after exited from loop */
618 if (local_err) {
619 error_report_err(local_err);
622 if (fb) {
623 qemu_fclose(fb);
627 * There are only two reasons we can get here, some error happened
628 * or the user triggered failover.
630 switch (failover_get_state()) {
631 case FAILOVER_STATUS_COMPLETED:
632 qapi_event_send_colo_exit(COLO_MODE_PRIMARY,
633 COLO_EXIT_REASON_REQUEST);
634 break;
635 default:
636 qapi_event_send_colo_exit(COLO_MODE_PRIMARY,
637 COLO_EXIT_REASON_ERROR);
640 /* Hope this not to be too long to wait here */
641 qemu_sem_wait(&s->colo_exit_sem);
642 qemu_sem_destroy(&s->colo_exit_sem);
645 * It is safe to unregister notifier after failover finished.
646 * Besides, colo_delay_timer and colo_checkpoint_sem can't be
647 * released before unregister notifier, or there will be use-after-free
648 * error.
650 colo_compare_unregister_notifier(&packets_compare_notifier);
651 timer_free(s->colo_delay_timer);
652 qemu_event_destroy(&s->colo_checkpoint_event);
655 * Must be called after failover BH is completed,
656 * Or the failover BH may shutdown the wrong fd that
657 * re-used by other threads after we release here.
659 if (s->rp_state.from_dst_file) {
660 qemu_fclose(s->rp_state.from_dst_file);
661 s->rp_state.from_dst_file = NULL;
665 void migrate_start_colo_process(MigrationState *s)
667 qemu_mutex_unlock_iothread();
668 qemu_event_init(&s->colo_checkpoint_event, false);
669 s->colo_delay_timer = timer_new_ms(QEMU_CLOCK_HOST,
670 colo_checkpoint_notify, s);
672 qemu_sem_init(&s->colo_exit_sem, 0);
673 colo_process_checkpoint(s);
674 qemu_mutex_lock_iothread();
677 static void colo_incoming_process_checkpoint(MigrationIncomingState *mis,
678 QEMUFile *fb, QIOChannelBuffer *bioc, Error **errp)
680 uint64_t total_size;
681 uint64_t value;
682 Error *local_err = NULL;
683 int ret;
685 qemu_mutex_lock_iothread();
686 vm_stop_force_state(RUN_STATE_COLO);
687 qemu_mutex_unlock_iothread();
688 trace_colo_vm_state_change("run", "stop");
690 /* FIXME: This is unnecessary for periodic checkpoint mode */
691 colo_send_message(mis->to_src_file, COLO_MESSAGE_CHECKPOINT_REPLY,
692 &local_err);
693 if (local_err) {
694 error_propagate(errp, local_err);
695 return;
698 colo_receive_check_message(mis->from_src_file,
699 COLO_MESSAGE_VMSTATE_SEND, &local_err);
700 if (local_err) {
701 error_propagate(errp, local_err);
702 return;
705 qemu_mutex_lock_iothread();
706 cpu_synchronize_all_states();
707 ret = qemu_loadvm_state_main(mis->from_src_file, mis);
708 qemu_mutex_unlock_iothread();
710 if (ret < 0) {
711 error_setg(errp, "Load VM's live state (ram) error");
712 return;
715 value = colo_receive_message_value(mis->from_src_file,
716 COLO_MESSAGE_VMSTATE_SIZE, &local_err);
717 if (local_err) {
718 error_propagate(errp, local_err);
719 return;
723 * Read VM device state data into channel buffer,
724 * It's better to re-use the memory allocated.
725 * Here we need to handle the channel buffer directly.
727 if (value > bioc->capacity) {
728 bioc->capacity = value;
729 bioc->data = g_realloc(bioc->data, bioc->capacity);
731 total_size = qemu_get_buffer(mis->from_src_file, bioc->data, value);
732 if (total_size != value) {
733 error_setg(errp, "Got %" PRIu64 " VMState data, less than expected"
734 " %" PRIu64, total_size, value);
735 return;
737 bioc->usage = total_size;
738 qio_channel_io_seek(QIO_CHANNEL(bioc), 0, 0, NULL);
740 colo_send_message(mis->to_src_file, COLO_MESSAGE_VMSTATE_RECEIVED,
741 &local_err);
742 if (local_err) {
743 error_propagate(errp, local_err);
744 return;
747 qemu_mutex_lock_iothread();
748 vmstate_loading = true;
749 colo_flush_ram_cache();
750 ret = qemu_load_device_state(fb);
751 if (ret < 0) {
752 error_setg(errp, "COLO: load device state failed");
753 vmstate_loading = false;
754 qemu_mutex_unlock_iothread();
755 return;
758 #ifdef CONFIG_REPLICATION
759 replication_get_error_all(&local_err);
760 if (local_err) {
761 error_propagate(errp, local_err);
762 vmstate_loading = false;
763 qemu_mutex_unlock_iothread();
764 return;
767 /* discard colo disk buffer */
768 replication_do_checkpoint_all(&local_err);
769 if (local_err) {
770 error_propagate(errp, local_err);
771 vmstate_loading = false;
772 qemu_mutex_unlock_iothread();
773 return;
775 #else
776 abort();
777 #endif
778 /* Notify all filters of all NIC to do checkpoint */
779 colo_notify_filters_event(COLO_EVENT_CHECKPOINT, &local_err);
781 if (local_err) {
782 error_propagate(errp, local_err);
783 vmstate_loading = false;
784 qemu_mutex_unlock_iothread();
785 return;
788 vmstate_loading = false;
789 vm_start();
790 qemu_mutex_unlock_iothread();
791 trace_colo_vm_state_change("stop", "run");
793 if (failover_get_state() == FAILOVER_STATUS_RELAUNCH) {
794 return;
797 colo_send_message(mis->to_src_file, COLO_MESSAGE_VMSTATE_LOADED,
798 &local_err);
799 error_propagate(errp, local_err);
802 static void colo_wait_handle_message(MigrationIncomingState *mis,
803 QEMUFile *fb, QIOChannelBuffer *bioc, Error **errp)
805 COLOMessage msg;
806 Error *local_err = NULL;
808 msg = colo_receive_message(mis->from_src_file, &local_err);
809 if (local_err) {
810 error_propagate(errp, local_err);
811 return;
814 switch (msg) {
815 case COLO_MESSAGE_CHECKPOINT_REQUEST:
816 colo_incoming_process_checkpoint(mis, fb, bioc, errp);
817 break;
818 default:
819 error_setg(errp, "Got unknown COLO message: %d", msg);
820 break;
824 void colo_shutdown(void)
826 MigrationIncomingState *mis = NULL;
827 MigrationState *s = NULL;
829 switch (get_colo_mode()) {
830 case COLO_MODE_PRIMARY:
831 s = migrate_get_current();
832 qemu_event_set(&s->colo_checkpoint_event);
833 qemu_sem_post(&s->colo_exit_sem);
834 break;
835 case COLO_MODE_SECONDARY:
836 mis = migration_incoming_get_current();
837 qemu_sem_post(&mis->colo_incoming_sem);
838 break;
839 default:
840 break;
844 void *colo_process_incoming_thread(void *opaque)
846 MigrationIncomingState *mis = opaque;
847 QEMUFile *fb = NULL;
848 QIOChannelBuffer *bioc = NULL; /* Cache incoming device state */
849 Error *local_err = NULL;
851 rcu_register_thread();
852 qemu_sem_init(&mis->colo_incoming_sem, 0);
854 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
855 MIGRATION_STATUS_COLO);
857 if (get_colo_mode() != COLO_MODE_SECONDARY) {
858 error_report("COLO mode must be COLO_MODE_SECONDARY");
859 return NULL;
862 failover_init_state();
864 mis->to_src_file = qemu_file_get_return_path(mis->from_src_file);
865 if (!mis->to_src_file) {
866 error_report("COLO incoming thread: Open QEMUFile to_src_file failed");
867 goto out;
870 * Note: the communication between Primary side and Secondary side
871 * should be sequential, we set the fd to unblocked in migration incoming
872 * coroutine, and here we are in the COLO incoming thread, so it is ok to
873 * set the fd back to blocked.
875 qemu_file_set_blocking(mis->from_src_file, true);
877 colo_incoming_start_dirty_log();
879 bioc = qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE);
880 fb = qemu_file_new_input(QIO_CHANNEL(bioc));
881 object_unref(OBJECT(bioc));
883 qemu_mutex_lock_iothread();
884 #ifdef CONFIG_REPLICATION
885 replication_start_all(REPLICATION_MODE_SECONDARY, &local_err);
886 if (local_err) {
887 qemu_mutex_unlock_iothread();
888 goto out;
890 #else
891 abort();
892 #endif
893 vm_start();
894 qemu_mutex_unlock_iothread();
895 trace_colo_vm_state_change("stop", "run");
897 colo_send_message(mis->to_src_file, COLO_MESSAGE_CHECKPOINT_READY,
898 &local_err);
899 if (local_err) {
900 goto out;
903 while (mis->state == MIGRATION_STATUS_COLO) {
904 colo_wait_handle_message(mis, fb, bioc, &local_err);
905 if (local_err) {
906 error_report_err(local_err);
907 break;
910 if (failover_get_state() == FAILOVER_STATUS_RELAUNCH) {
911 failover_set_state(FAILOVER_STATUS_RELAUNCH,
912 FAILOVER_STATUS_NONE);
913 failover_request_active(NULL);
914 break;
917 if (failover_get_state() != FAILOVER_STATUS_NONE) {
918 error_report("failover request");
919 break;
923 out:
925 * There are only two reasons we can get here, some error happened
926 * or the user triggered failover.
928 switch (failover_get_state()) {
929 case FAILOVER_STATUS_COMPLETED:
930 qapi_event_send_colo_exit(COLO_MODE_SECONDARY,
931 COLO_EXIT_REASON_REQUEST);
932 break;
933 default:
934 qapi_event_send_colo_exit(COLO_MODE_SECONDARY,
935 COLO_EXIT_REASON_ERROR);
938 if (fb) {
939 qemu_fclose(fb);
942 /* Hope this not to be too long to loop here */
943 qemu_sem_wait(&mis->colo_incoming_sem);
944 qemu_sem_destroy(&mis->colo_incoming_sem);
946 rcu_unregister_thread();
947 return NULL;