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