xref: /openbmc/qemu/migration/colo.c (revision 37b0b24e)
1 /*
2  * COarse-grain LOck-stepping Virtual Machines for Non-stop Service (COLO)
3  * (a.k.a. Fault Tolerance or Continuous Replication)
4  *
5  * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
6  * Copyright (c) 2016 FUJITSU LIMITED
7  * Copyright (c) 2016 Intel Corporation
8  *
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.
11  */
12 
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 "qapi/qmp/qerror.h"
37 #include "sysemu/cpus.h"
38 #include "sysemu/runstate.h"
39 #include "net/filter.h"
40 
41 static bool vmstate_loading;
42 static Notifier packets_compare_notifier;
43 
44 /* User need to know colo mode after COLO failover */
45 static COLOMode last_colo_mode;
46 
47 #define COLO_BUFFER_BASE_SIZE (4 * 1024 * 1024)
48 
49 bool migration_in_colo_state(void)
50 {
51     MigrationState *s = migrate_get_current();
52 
53     return (s->state == MIGRATION_STATUS_COLO);
54 }
55 
56 bool migration_incoming_in_colo_state(void)
57 {
58     MigrationIncomingState *mis = migration_incoming_get_current();
59 
60     return mis && (mis->state == MIGRATION_STATUS_COLO);
61 }
62 
63 static bool colo_runstate_is_stopped(void)
64 {
65     return runstate_check(RUN_STATE_COLO) || !runstate_is_running();
66 }
67 
68 static void secondary_vm_do_failover(void)
69 {
70 /* COLO needs enable block-replication */
71 #ifdef CONFIG_REPLICATION
72     int old_state;
73     MigrationIncomingState *mis = migration_incoming_get_current();
74     Error *local_err = NULL;
75 
76     /* Can not do failover during the process of VM's loading VMstate, Or
77      * it will break the secondary VM.
78      */
79     if (vmstate_loading) {
80         old_state = failover_set_state(FAILOVER_STATUS_ACTIVE,
81                         FAILOVER_STATUS_RELAUNCH);
82         if (old_state != FAILOVER_STATUS_ACTIVE) {
83             error_report("Unknown error while do failover for secondary VM,"
84                          "old_state: %s", FailoverStatus_str(old_state));
85         }
86         return;
87     }
88 
89     migrate_set_state(&mis->state, MIGRATION_STATUS_COLO,
90                       MIGRATION_STATUS_COMPLETED);
91 
92     replication_stop_all(true, &local_err);
93     if (local_err) {
94         error_report_err(local_err);
95         local_err = NULL;
96     }
97 
98     /* Notify all filters of all NIC to do checkpoint */
99     colo_notify_filters_event(COLO_EVENT_FAILOVER, &local_err);
100     if (local_err) {
101         error_report_err(local_err);
102     }
103 
104     if (!autostart) {
105         error_report("\"-S\" qemu option will be ignored in secondary side");
106         /* recover runstate to normal migration finish state */
107         autostart = true;
108     }
109     /*
110      * Make sure COLO incoming thread not block in recv or send,
111      * If mis->from_src_file and mis->to_src_file use the same fd,
112      * The second shutdown() will return -1, we ignore this value,
113      * It is harmless.
114      */
115     if (mis->from_src_file) {
116         qemu_file_shutdown(mis->from_src_file);
117     }
118     if (mis->to_src_file) {
119         qemu_file_shutdown(mis->to_src_file);
120     }
121 
122     old_state = failover_set_state(FAILOVER_STATUS_ACTIVE,
123                                    FAILOVER_STATUS_COMPLETED);
124     if (old_state != FAILOVER_STATUS_ACTIVE) {
125         error_report("Incorrect state (%s) while doing failover for "
126                      "secondary VM", FailoverStatus_str(old_state));
127         return;
128     }
129     /* Notify COLO incoming thread that failover work is finished */
130     qemu_sem_post(&mis->colo_incoming_sem);
131 
132     /* For Secondary VM, jump to incoming co */
133     if (mis->migration_incoming_co) {
134         qemu_coroutine_enter(mis->migration_incoming_co);
135     }
136 #else
137     abort();
138 #endif
139 }
140 
141 static void primary_vm_do_failover(void)
142 {
143 #ifdef CONFIG_REPLICATION
144     MigrationState *s = migrate_get_current();
145     int old_state;
146     Error *local_err = NULL;
147 
148     migrate_set_state(&s->state, MIGRATION_STATUS_COLO,
149                       MIGRATION_STATUS_COMPLETED);
150     /*
151      * kick COLO thread which might wait at
152      * qemu_sem_wait(&s->colo_checkpoint_sem).
153      */
154     colo_checkpoint_notify(s);
155 
156     /*
157      * Wake up COLO thread which may blocked in recv() or send(),
158      * The s->rp_state.from_dst_file and s->to_dst_file may use the
159      * same fd, but we still shutdown the fd for twice, it is harmless.
160      */
161     if (s->to_dst_file) {
162         qemu_file_shutdown(s->to_dst_file);
163     }
164     if (s->rp_state.from_dst_file) {
165         qemu_file_shutdown(s->rp_state.from_dst_file);
166     }
167 
168     old_state = failover_set_state(FAILOVER_STATUS_ACTIVE,
169                                    FAILOVER_STATUS_COMPLETED);
170     if (old_state != FAILOVER_STATUS_ACTIVE) {
171         error_report("Incorrect state (%s) while doing failover for Primary VM",
172                      FailoverStatus_str(old_state));
173         return;
174     }
175 
176     replication_stop_all(true, &local_err);
177     if (local_err) {
178         error_report_err(local_err);
179         local_err = NULL;
180     }
181 
182     /* Notify COLO thread that failover work is finished */
183     qemu_sem_post(&s->colo_exit_sem);
184 #else
185     abort();
186 #endif
187 }
188 
189 COLOMode get_colo_mode(void)
190 {
191     if (migration_in_colo_state()) {
192         return COLO_MODE_PRIMARY;
193     } else if (migration_incoming_in_colo_state()) {
194         return COLO_MODE_SECONDARY;
195     } else {
196         return COLO_MODE_NONE;
197     }
198 }
199 
200 void colo_do_failover(void)
201 {
202     /* Make sure VM stopped while failover happened. */
203     if (!colo_runstate_is_stopped()) {
204         vm_stop_force_state(RUN_STATE_COLO);
205     }
206 
207     switch (last_colo_mode = get_colo_mode()) {
208     case COLO_MODE_PRIMARY:
209         primary_vm_do_failover();
210         break;
211     case COLO_MODE_SECONDARY:
212         secondary_vm_do_failover();
213         break;
214     default:
215         error_report("colo_do_failover failed because the colo mode"
216                      " could not be obtained");
217     }
218 }
219 
220 #ifdef CONFIG_REPLICATION
221 void qmp_xen_set_replication(bool enable, bool primary,
222                              bool has_failover, bool failover,
223                              Error **errp)
224 {
225     ReplicationMode mode = primary ?
226                            REPLICATION_MODE_PRIMARY :
227                            REPLICATION_MODE_SECONDARY;
228 
229     if (has_failover && enable) {
230         error_setg(errp, "Parameter 'failover' is only for"
231                    " stopping replication");
232         return;
233     }
234 
235     if (enable) {
236         replication_start_all(mode, errp);
237     } else {
238         if (!has_failover) {
239             failover = NULL;
240         }
241         replication_stop_all(failover, failover ? NULL : errp);
242     }
243 }
244 
245 ReplicationStatus *qmp_query_xen_replication_status(Error **errp)
246 {
247     Error *err = NULL;
248     ReplicationStatus *s = g_new0(ReplicationStatus, 1);
249 
250     replication_get_error_all(&err);
251     if (err) {
252         s->error = true;
253         s->desc = g_strdup(error_get_pretty(err));
254     } else {
255         s->error = false;
256     }
257 
258     error_free(err);
259     return s;
260 }
261 
262 void qmp_xen_colo_do_checkpoint(Error **errp)
263 {
264     Error *err = NULL;
265 
266     replication_do_checkpoint_all(&err);
267     if (err) {
268         error_propagate(errp, err);
269         return;
270     }
271     /* Notify all filters of all NIC to do checkpoint */
272     colo_notify_filters_event(COLO_EVENT_CHECKPOINT, errp);
273 }
274 #endif
275 
276 COLOStatus *qmp_query_colo_status(Error **errp)
277 {
278     COLOStatus *s = g_new0(COLOStatus, 1);
279 
280     s->mode = get_colo_mode();
281     s->last_mode = last_colo_mode;
282 
283     switch (failover_get_state()) {
284     case FAILOVER_STATUS_NONE:
285         s->reason = COLO_EXIT_REASON_NONE;
286         break;
287     case FAILOVER_STATUS_COMPLETED:
288         s->reason = COLO_EXIT_REASON_REQUEST;
289         break;
290     default:
291         if (migration_in_colo_state()) {
292             s->reason = COLO_EXIT_REASON_PROCESSING;
293         } else {
294             s->reason = COLO_EXIT_REASON_ERROR;
295         }
296     }
297 
298     return s;
299 }
300 
301 static void colo_send_message(QEMUFile *f, COLOMessage msg,
302                               Error **errp)
303 {
304     int ret;
305 
306     if (msg >= COLO_MESSAGE__MAX) {
307         error_setg(errp, "%s: Invalid message", __func__);
308         return;
309     }
310     qemu_put_be32(f, msg);
311     qemu_fflush(f);
312 
313     ret = qemu_file_get_error(f);
314     if (ret < 0) {
315         error_setg_errno(errp, -ret, "Can't send COLO message");
316     }
317     trace_colo_send_message(COLOMessage_str(msg));
318 }
319 
320 static void colo_send_message_value(QEMUFile *f, COLOMessage msg,
321                                     uint64_t value, Error **errp)
322 {
323     Error *local_err = NULL;
324     int ret;
325 
326     colo_send_message(f, msg, &local_err);
327     if (local_err) {
328         error_propagate(errp, local_err);
329         return;
330     }
331     qemu_put_be64(f, value);
332     qemu_fflush(f);
333 
334     ret = qemu_file_get_error(f);
335     if (ret < 0) {
336         error_setg_errno(errp, -ret, "Failed to send value for message:%s",
337                          COLOMessage_str(msg));
338     }
339 }
340 
341 static COLOMessage colo_receive_message(QEMUFile *f, Error **errp)
342 {
343     COLOMessage msg;
344     int ret;
345 
346     msg = qemu_get_be32(f);
347     ret = qemu_file_get_error(f);
348     if (ret < 0) {
349         error_setg_errno(errp, -ret, "Can't receive COLO message");
350         return msg;
351     }
352     if (msg >= COLO_MESSAGE__MAX) {
353         error_setg(errp, "%s: Invalid message", __func__);
354         return msg;
355     }
356     trace_colo_receive_message(COLOMessage_str(msg));
357     return msg;
358 }
359 
360 static void colo_receive_check_message(QEMUFile *f, COLOMessage expect_msg,
361                                        Error **errp)
362 {
363     COLOMessage msg;
364     Error *local_err = NULL;
365 
366     msg = colo_receive_message(f, &local_err);
367     if (local_err) {
368         error_propagate(errp, local_err);
369         return;
370     }
371     if (msg != expect_msg) {
372         error_setg(errp, "Unexpected COLO message %d, expected %d",
373                           msg, expect_msg);
374     }
375 }
376 
377 static uint64_t colo_receive_message_value(QEMUFile *f, uint32_t expect_msg,
378                                            Error **errp)
379 {
380     Error *local_err = NULL;
381     uint64_t value;
382     int ret;
383 
384     colo_receive_check_message(f, expect_msg, &local_err);
385     if (local_err) {
386         error_propagate(errp, local_err);
387         return 0;
388     }
389 
390     value = qemu_get_be64(f);
391     ret = qemu_file_get_error(f);
392     if (ret < 0) {
393         error_setg_errno(errp, -ret, "Failed to get value for COLO message: %s",
394                          COLOMessage_str(expect_msg));
395     }
396     return value;
397 }
398 
399 static int colo_do_checkpoint_transaction(MigrationState *s,
400                                           QIOChannelBuffer *bioc,
401                                           QEMUFile *fb)
402 {
403     Error *local_err = NULL;
404     int ret = -1;
405 
406     colo_send_message(s->to_dst_file, COLO_MESSAGE_CHECKPOINT_REQUEST,
407                       &local_err);
408     if (local_err) {
409         goto out;
410     }
411 
412     colo_receive_check_message(s->rp_state.from_dst_file,
413                     COLO_MESSAGE_CHECKPOINT_REPLY, &local_err);
414     if (local_err) {
415         goto out;
416     }
417     /* Reset channel-buffer directly */
418     qio_channel_io_seek(QIO_CHANNEL(bioc), 0, 0, NULL);
419     bioc->usage = 0;
420 
421     qemu_mutex_lock_iothread();
422     if (failover_get_state() != FAILOVER_STATUS_NONE) {
423         qemu_mutex_unlock_iothread();
424         goto out;
425     }
426     vm_stop_force_state(RUN_STATE_COLO);
427     qemu_mutex_unlock_iothread();
428     trace_colo_vm_state_change("run", "stop");
429     /*
430      * Failover request bh could be called after vm_stop_force_state(),
431      * So we need check failover_request_is_active() again.
432      */
433     if (failover_get_state() != FAILOVER_STATUS_NONE) {
434         goto out;
435     }
436     qemu_mutex_lock_iothread();
437 
438 #ifdef CONFIG_REPLICATION
439     replication_do_checkpoint_all(&local_err);
440     if (local_err) {
441         qemu_mutex_unlock_iothread();
442         goto out;
443     }
444 #else
445         abort();
446 #endif
447 
448     colo_send_message(s->to_dst_file, COLO_MESSAGE_VMSTATE_SEND, &local_err);
449     if (local_err) {
450         qemu_mutex_unlock_iothread();
451         goto out;
452     }
453     /* Note: device state is saved into buffer */
454     ret = qemu_save_device_state(fb);
455 
456     qemu_mutex_unlock_iothread();
457     if (ret < 0) {
458         goto out;
459     }
460 
461     if (migrate_auto_converge()) {
462         mig_throttle_counter_reset();
463     }
464     /*
465      * Only save VM's live state, which not including device state.
466      * TODO: We may need a timeout mechanism to prevent COLO process
467      * to be blocked here.
468      */
469     qemu_savevm_live_state(s->to_dst_file);
470 
471     qemu_fflush(fb);
472 
473     /*
474      * We need the size of the VMstate data in Secondary side,
475      * With which we can decide how much data should be read.
476      */
477     colo_send_message_value(s->to_dst_file, COLO_MESSAGE_VMSTATE_SIZE,
478                             bioc->usage, &local_err);
479     if (local_err) {
480         goto out;
481     }
482 
483     qemu_put_buffer(s->to_dst_file, bioc->data, bioc->usage);
484     qemu_fflush(s->to_dst_file);
485     ret = qemu_file_get_error(s->to_dst_file);
486     if (ret < 0) {
487         goto out;
488     }
489 
490     colo_receive_check_message(s->rp_state.from_dst_file,
491                        COLO_MESSAGE_VMSTATE_RECEIVED, &local_err);
492     if (local_err) {
493         goto out;
494     }
495 
496     qemu_event_reset(&s->colo_checkpoint_event);
497     colo_notify_compares_event(NULL, COLO_EVENT_CHECKPOINT, &local_err);
498     if (local_err) {
499         goto out;
500     }
501 
502     colo_receive_check_message(s->rp_state.from_dst_file,
503                        COLO_MESSAGE_VMSTATE_LOADED, &local_err);
504     if (local_err) {
505         goto out;
506     }
507 
508     ret = 0;
509 
510     qemu_mutex_lock_iothread();
511     vm_start();
512     qemu_mutex_unlock_iothread();
513     trace_colo_vm_state_change("stop", "run");
514 
515 out:
516     if (local_err) {
517         error_report_err(local_err);
518     }
519     return ret;
520 }
521 
522 static void colo_compare_notify_checkpoint(Notifier *notifier, void *data)
523 {
524     colo_checkpoint_notify(data);
525 }
526 
527 static void colo_process_checkpoint(MigrationState *s)
528 {
529     QIOChannelBuffer *bioc;
530     QEMUFile *fb = NULL;
531     Error *local_err = NULL;
532     int ret;
533 
534     if (get_colo_mode() != COLO_MODE_PRIMARY) {
535         error_report("COLO mode must be COLO_MODE_PRIMARY");
536         return;
537     }
538 
539     failover_init_state();
540 
541     s->rp_state.from_dst_file = qemu_file_get_return_path(s->to_dst_file);
542     if (!s->rp_state.from_dst_file) {
543         error_report("Open QEMUFile from_dst_file failed");
544         goto out;
545     }
546 
547     packets_compare_notifier.notify = colo_compare_notify_checkpoint;
548     colo_compare_register_notifier(&packets_compare_notifier);
549 
550     /*
551      * Wait for Secondary finish loading VM states and enter COLO
552      * restore.
553      */
554     colo_receive_check_message(s->rp_state.from_dst_file,
555                        COLO_MESSAGE_CHECKPOINT_READY, &local_err);
556     if (local_err) {
557         goto out;
558     }
559     bioc = qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE);
560     fb = qemu_file_new_output(QIO_CHANNEL(bioc));
561     object_unref(OBJECT(bioc));
562 
563     qemu_mutex_lock_iothread();
564 #ifdef CONFIG_REPLICATION
565     replication_start_all(REPLICATION_MODE_PRIMARY, &local_err);
566     if (local_err) {
567         qemu_mutex_unlock_iothread();
568         goto out;
569     }
570 #else
571         abort();
572 #endif
573 
574     vm_start();
575     qemu_mutex_unlock_iothread();
576     trace_colo_vm_state_change("stop", "run");
577 
578     timer_mod(s->colo_delay_timer, qemu_clock_get_ms(QEMU_CLOCK_HOST) +
579               s->parameters.x_checkpoint_delay);
580 
581     while (s->state == MIGRATION_STATUS_COLO) {
582         if (failover_get_state() != FAILOVER_STATUS_NONE) {
583             error_report("failover request");
584             goto out;
585         }
586 
587         qemu_event_wait(&s->colo_checkpoint_event);
588 
589         if (s->state != MIGRATION_STATUS_COLO) {
590             goto out;
591         }
592         ret = colo_do_checkpoint_transaction(s, bioc, fb);
593         if (ret < 0) {
594             goto out;
595         }
596     }
597 
598 out:
599     /* Throw the unreported error message after exited from loop */
600     if (local_err) {
601         error_report_err(local_err);
602     }
603 
604     if (fb) {
605         qemu_fclose(fb);
606     }
607 
608     /*
609      * There are only two reasons we can get here, some error happened
610      * or the user triggered failover.
611      */
612     switch (failover_get_state()) {
613     case FAILOVER_STATUS_COMPLETED:
614         qapi_event_send_colo_exit(COLO_MODE_PRIMARY,
615                                   COLO_EXIT_REASON_REQUEST);
616         break;
617     default:
618         qapi_event_send_colo_exit(COLO_MODE_PRIMARY,
619                                   COLO_EXIT_REASON_ERROR);
620     }
621 
622     /* Hope this not to be too long to wait here */
623     qemu_sem_wait(&s->colo_exit_sem);
624     qemu_sem_destroy(&s->colo_exit_sem);
625 
626     /*
627      * It is safe to unregister notifier after failover finished.
628      * Besides, colo_delay_timer and colo_checkpoint_sem can't be
629      * released before unregister notifier, or there will be use-after-free
630      * error.
631      */
632     colo_compare_unregister_notifier(&packets_compare_notifier);
633     timer_free(s->colo_delay_timer);
634     qemu_event_destroy(&s->colo_checkpoint_event);
635 
636     /*
637      * Must be called after failover BH is completed,
638      * Or the failover BH may shutdown the wrong fd that
639      * re-used by other threads after we release here.
640      */
641     if (s->rp_state.from_dst_file) {
642         qemu_fclose(s->rp_state.from_dst_file);
643         s->rp_state.from_dst_file = NULL;
644     }
645 }
646 
647 void colo_checkpoint_notify(void *opaque)
648 {
649     MigrationState *s = opaque;
650     int64_t next_notify_time;
651 
652     qemu_event_set(&s->colo_checkpoint_event);
653     s->colo_checkpoint_time = qemu_clock_get_ms(QEMU_CLOCK_HOST);
654     next_notify_time = s->colo_checkpoint_time +
655                     s->parameters.x_checkpoint_delay;
656     timer_mod(s->colo_delay_timer, next_notify_time);
657 }
658 
659 void migrate_start_colo_process(MigrationState *s)
660 {
661     qemu_mutex_unlock_iothread();
662     qemu_event_init(&s->colo_checkpoint_event, false);
663     s->colo_delay_timer =  timer_new_ms(QEMU_CLOCK_HOST,
664                                 colo_checkpoint_notify, s);
665 
666     qemu_sem_init(&s->colo_exit_sem, 0);
667     colo_process_checkpoint(s);
668     qemu_mutex_lock_iothread();
669 }
670 
671 static void colo_incoming_process_checkpoint(MigrationIncomingState *mis,
672                       QEMUFile *fb, QIOChannelBuffer *bioc, Error **errp)
673 {
674     uint64_t total_size;
675     uint64_t value;
676     Error *local_err = NULL;
677     int ret;
678 
679     qemu_mutex_lock_iothread();
680     vm_stop_force_state(RUN_STATE_COLO);
681     qemu_mutex_unlock_iothread();
682     trace_colo_vm_state_change("run", "stop");
683 
684     /* FIXME: This is unnecessary for periodic checkpoint mode */
685     colo_send_message(mis->to_src_file, COLO_MESSAGE_CHECKPOINT_REPLY,
686                  &local_err);
687     if (local_err) {
688         error_propagate(errp, local_err);
689         return;
690     }
691 
692     colo_receive_check_message(mis->from_src_file,
693                        COLO_MESSAGE_VMSTATE_SEND, &local_err);
694     if (local_err) {
695         error_propagate(errp, local_err);
696         return;
697     }
698 
699     qemu_mutex_lock_iothread();
700     cpu_synchronize_all_states();
701     ret = qemu_loadvm_state_main(mis->from_src_file, mis);
702     qemu_mutex_unlock_iothread();
703 
704     if (ret < 0) {
705         error_setg(errp, "Load VM's live state (ram) error");
706         return;
707     }
708 
709     value = colo_receive_message_value(mis->from_src_file,
710                              COLO_MESSAGE_VMSTATE_SIZE, &local_err);
711     if (local_err) {
712         error_propagate(errp, local_err);
713         return;
714     }
715 
716     /*
717      * Read VM device state data into channel buffer,
718      * It's better to re-use the memory allocated.
719      * Here we need to handle the channel buffer directly.
720      */
721     if (value > bioc->capacity) {
722         bioc->capacity = value;
723         bioc->data = g_realloc(bioc->data, bioc->capacity);
724     }
725     total_size = qemu_get_buffer(mis->from_src_file, bioc->data, value);
726     if (total_size != value) {
727         error_setg(errp, "Got %" PRIu64 " VMState data, less than expected"
728                     " %" PRIu64, total_size, value);
729         return;
730     }
731     bioc->usage = total_size;
732     qio_channel_io_seek(QIO_CHANNEL(bioc), 0, 0, NULL);
733 
734     colo_send_message(mis->to_src_file, COLO_MESSAGE_VMSTATE_RECEIVED,
735                  &local_err);
736     if (local_err) {
737         error_propagate(errp, local_err);
738         return;
739     }
740 
741     qemu_mutex_lock_iothread();
742     vmstate_loading = true;
743     colo_flush_ram_cache();
744     ret = qemu_load_device_state(fb);
745     if (ret < 0) {
746         error_setg(errp, "COLO: load device state failed");
747         vmstate_loading = false;
748         qemu_mutex_unlock_iothread();
749         return;
750     }
751 
752 #ifdef CONFIG_REPLICATION
753     replication_get_error_all(&local_err);
754     if (local_err) {
755         error_propagate(errp, local_err);
756         vmstate_loading = false;
757         qemu_mutex_unlock_iothread();
758         return;
759     }
760 
761     /* discard colo disk buffer */
762     replication_do_checkpoint_all(&local_err);
763     if (local_err) {
764         error_propagate(errp, local_err);
765         vmstate_loading = false;
766         qemu_mutex_unlock_iothread();
767         return;
768     }
769 #else
770     abort();
771 #endif
772     /* Notify all filters of all NIC to do checkpoint */
773     colo_notify_filters_event(COLO_EVENT_CHECKPOINT, &local_err);
774 
775     if (local_err) {
776         error_propagate(errp, local_err);
777         vmstate_loading = false;
778         qemu_mutex_unlock_iothread();
779         return;
780     }
781 
782     vmstate_loading = false;
783     vm_start();
784     qemu_mutex_unlock_iothread();
785     trace_colo_vm_state_change("stop", "run");
786 
787     if (failover_get_state() == FAILOVER_STATUS_RELAUNCH) {
788         return;
789     }
790 
791     colo_send_message(mis->to_src_file, COLO_MESSAGE_VMSTATE_LOADED,
792                  &local_err);
793     error_propagate(errp, local_err);
794 }
795 
796 static void colo_wait_handle_message(MigrationIncomingState *mis,
797                 QEMUFile *fb, QIOChannelBuffer *bioc, Error **errp)
798 {
799     COLOMessage msg;
800     Error *local_err = NULL;
801 
802     msg = colo_receive_message(mis->from_src_file, &local_err);
803     if (local_err) {
804         error_propagate(errp, local_err);
805         return;
806     }
807 
808     switch (msg) {
809     case COLO_MESSAGE_CHECKPOINT_REQUEST:
810         colo_incoming_process_checkpoint(mis, fb, bioc, errp);
811         break;
812     default:
813         error_setg(errp, "Got unknown COLO message: %d", msg);
814         break;
815     }
816 }
817 
818 void colo_shutdown(void)
819 {
820     MigrationIncomingState *mis = NULL;
821     MigrationState *s = NULL;
822 
823     switch (get_colo_mode()) {
824     case COLO_MODE_PRIMARY:
825         s = migrate_get_current();
826         qemu_event_set(&s->colo_checkpoint_event);
827         qemu_sem_post(&s->colo_exit_sem);
828         break;
829     case COLO_MODE_SECONDARY:
830         mis = migration_incoming_get_current();
831         qemu_sem_post(&mis->colo_incoming_sem);
832         break;
833     default:
834         break;
835     }
836 }
837 
838 void *colo_process_incoming_thread(void *opaque)
839 {
840     MigrationIncomingState *mis = opaque;
841     QEMUFile *fb = NULL;
842     QIOChannelBuffer *bioc = NULL; /* Cache incoming device state */
843     Error *local_err = NULL;
844 
845     rcu_register_thread();
846     qemu_sem_init(&mis->colo_incoming_sem, 0);
847 
848     migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
849                       MIGRATION_STATUS_COLO);
850 
851     if (get_colo_mode() != COLO_MODE_SECONDARY) {
852         error_report("COLO mode must be COLO_MODE_SECONDARY");
853         return NULL;
854     }
855 
856     failover_init_state();
857 
858     mis->to_src_file = qemu_file_get_return_path(mis->from_src_file);
859     if (!mis->to_src_file) {
860         error_report("COLO incoming thread: Open QEMUFile to_src_file failed");
861         goto out;
862     }
863     /*
864      * Note: the communication between Primary side and Secondary side
865      * should be sequential, we set the fd to unblocked in migration incoming
866      * coroutine, and here we are in the COLO incoming thread, so it is ok to
867      * set the fd back to blocked.
868      */
869     qemu_file_set_blocking(mis->from_src_file, true);
870 
871     colo_incoming_start_dirty_log();
872 
873     bioc = qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE);
874     fb = qemu_file_new_input(QIO_CHANNEL(bioc));
875     object_unref(OBJECT(bioc));
876 
877     qemu_mutex_lock_iothread();
878 #ifdef CONFIG_REPLICATION
879     replication_start_all(REPLICATION_MODE_SECONDARY, &local_err);
880     if (local_err) {
881         qemu_mutex_unlock_iothread();
882         goto out;
883     }
884 #else
885         abort();
886 #endif
887     vm_start();
888     qemu_mutex_unlock_iothread();
889     trace_colo_vm_state_change("stop", "run");
890 
891     colo_send_message(mis->to_src_file, COLO_MESSAGE_CHECKPOINT_READY,
892                       &local_err);
893     if (local_err) {
894         goto out;
895     }
896 
897     while (mis->state == MIGRATION_STATUS_COLO) {
898         colo_wait_handle_message(mis, fb, bioc, &local_err);
899         if (local_err) {
900             error_report_err(local_err);
901             break;
902         }
903 
904         if (failover_get_state() == FAILOVER_STATUS_RELAUNCH) {
905             failover_set_state(FAILOVER_STATUS_RELAUNCH,
906                             FAILOVER_STATUS_NONE);
907             failover_request_active(NULL);
908             break;
909         }
910 
911         if (failover_get_state() != FAILOVER_STATUS_NONE) {
912             error_report("failover request");
913             break;
914         }
915     }
916 
917 out:
918     /*
919      * There are only two reasons we can get here, some error happened
920      * or the user triggered failover.
921      */
922     switch (failover_get_state()) {
923     case FAILOVER_STATUS_COMPLETED:
924         qapi_event_send_colo_exit(COLO_MODE_SECONDARY,
925                                   COLO_EXIT_REASON_REQUEST);
926         break;
927     default:
928         qapi_event_send_colo_exit(COLO_MODE_SECONDARY,
929                                   COLO_EXIT_REASON_ERROR);
930     }
931 
932     if (fb) {
933         qemu_fclose(fb);
934     }
935 
936     /* Hope this not to be too long to loop here */
937     qemu_sem_wait(&mis->colo_incoming_sem);
938     qemu_sem_destroy(&mis->colo_incoming_sem);
939 
940     rcu_unregister_thread();
941     return NULL;
942 }
943