xref: /openbmc/qemu/migration/migration.c (revision eea1e5c9)
1 /*
2  * QEMU live migration
3  *
4  * Copyright IBM, Corp. 2008
5  *
6  * Authors:
7  *  Anthony Liguori   <aliguori@us.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  *
12  * Contributions after 2012-01-13 are licensed under the terms of the
13  * GNU GPL, version 2 or (at your option) any later version.
14  */
15 
16 #include "qemu/osdep.h"
17 #include "qemu/cutils.h"
18 #include "qemu/error-report.h"
19 #include "qemu/main-loop.h"
20 #include "migration/blocker.h"
21 #include "exec.h"
22 #include "fd.h"
23 #include "file.h"
24 #include "socket.h"
25 #include "sysemu/runstate.h"
26 #include "sysemu/sysemu.h"
27 #include "sysemu/cpu-throttle.h"
28 #include "rdma.h"
29 #include "ram.h"
30 #include "ram-compress.h"
31 #include "migration/global_state.h"
32 #include "migration/misc.h"
33 #include "migration.h"
34 #include "migration-stats.h"
35 #include "savevm.h"
36 #include "qemu-file.h"
37 #include "channel.h"
38 #include "migration/vmstate.h"
39 #include "block/block.h"
40 #include "qapi/error.h"
41 #include "qapi/clone-visitor.h"
42 #include "qapi/qapi-visit-migration.h"
43 #include "qapi/qapi-visit-sockets.h"
44 #include "qapi/qapi-commands-migration.h"
45 #include "qapi/qapi-events-migration.h"
46 #include "qapi/qmp/qerror.h"
47 #include "qapi/qmp/qnull.h"
48 #include "qemu/rcu.h"
49 #include "block.h"
50 #include "postcopy-ram.h"
51 #include "qemu/thread.h"
52 #include "trace.h"
53 #include "exec/target_page.h"
54 #include "io/channel-buffer.h"
55 #include "io/channel-tls.h"
56 #include "migration/colo.h"
57 #include "hw/boards.h"
58 #include "monitor/monitor.h"
59 #include "net/announce.h"
60 #include "qemu/queue.h"
61 #include "multifd.h"
62 #include "threadinfo.h"
63 #include "qemu/yank.h"
64 #include "sysemu/cpus.h"
65 #include "yank_functions.h"
66 #include "sysemu/qtest.h"
67 #include "options.h"
68 #include "sysemu/dirtylimit.h"
69 
70 static NotifierList migration_state_notifiers =
71     NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
72 
73 /* Messages sent on the return path from destination to source */
74 enum mig_rp_message_type {
75     MIG_RP_MSG_INVALID = 0,  /* Must be 0 */
76     MIG_RP_MSG_SHUT,         /* sibling will not send any more RP messages */
77     MIG_RP_MSG_PONG,         /* Response to a PING; data (seq: be32 ) */
78 
79     MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */
80     MIG_RP_MSG_REQ_PAGES,    /* data (start: be64, len: be32) */
81     MIG_RP_MSG_RECV_BITMAP,  /* send recved_bitmap back to source */
82     MIG_RP_MSG_RESUME_ACK,   /* tell source that we are ready to resume */
83     MIG_RP_MSG_SWITCHOVER_ACK, /* Tell source it's OK to do switchover */
84 
85     MIG_RP_MSG_MAX
86 };
87 
88 /* When we add fault tolerance, we could have several
89    migrations at once.  For now we don't need to add
90    dynamic creation of migration */
91 
92 static MigrationState *current_migration;
93 static MigrationIncomingState *current_incoming;
94 
95 static GSList *migration_blockers;
96 
97 static bool migration_object_check(MigrationState *ms, Error **errp);
98 static int migration_maybe_pause(MigrationState *s,
99                                  int *current_active_state,
100                                  int new_state);
101 static void migrate_fd_cancel(MigrationState *s);
102 static int close_return_path_on_source(MigrationState *s);
103 
104 static void migration_downtime_start(MigrationState *s)
105 {
106     trace_vmstate_downtime_checkpoint("src-downtime-start");
107     s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
108 }
109 
110 static void migration_downtime_end(MigrationState *s)
111 {
112     int64_t now = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
113 
114     /*
115      * If downtime already set, should mean that postcopy already set it,
116      * then that should be the real downtime already.
117      */
118     if (!s->downtime) {
119         s->downtime = now - s->downtime_start;
120     }
121 
122     trace_vmstate_downtime_checkpoint("src-downtime-end");
123 }
124 
125 static bool migration_needs_multiple_sockets(void)
126 {
127     return migrate_multifd() || migrate_postcopy_preempt();
128 }
129 
130 static bool uri_supports_multi_channels(const char *uri)
131 {
132     return strstart(uri, "tcp:", NULL) || strstart(uri, "unix:", NULL) ||
133            strstart(uri, "vsock:", NULL);
134 }
135 
136 static bool
137 migration_channels_and_uri_compatible(const char *uri, Error **errp)
138 {
139     if (migration_needs_multiple_sockets() &&
140         !uri_supports_multi_channels(uri)) {
141         error_setg(errp, "Migration requires multi-channel URIs (e.g. tcp)");
142         return false;
143     }
144 
145     return true;
146 }
147 
148 static gint page_request_addr_cmp(gconstpointer ap, gconstpointer bp)
149 {
150     uintptr_t a = (uintptr_t) ap, b = (uintptr_t) bp;
151 
152     return (a > b) - (a < b);
153 }
154 
155 int migration_stop_vm(RunState state)
156 {
157     int ret = vm_stop_force_state(state);
158 
159     trace_vmstate_downtime_checkpoint("src-vm-stopped");
160 
161     return ret;
162 }
163 
164 void migration_object_init(void)
165 {
166     /* This can only be called once. */
167     assert(!current_migration);
168     current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION));
169 
170     /*
171      * Init the migrate incoming object as well no matter whether
172      * we'll use it or not.
173      */
174     assert(!current_incoming);
175     current_incoming = g_new0(MigrationIncomingState, 1);
176     current_incoming->state = MIGRATION_STATUS_NONE;
177     current_incoming->postcopy_remote_fds =
178         g_array_new(FALSE, TRUE, sizeof(struct PostCopyFD));
179     qemu_mutex_init(&current_incoming->rp_mutex);
180     qemu_mutex_init(&current_incoming->postcopy_prio_thread_mutex);
181     qemu_event_init(&current_incoming->main_thread_load_event, false);
182     qemu_sem_init(&current_incoming->postcopy_pause_sem_dst, 0);
183     qemu_sem_init(&current_incoming->postcopy_pause_sem_fault, 0);
184     qemu_sem_init(&current_incoming->postcopy_pause_sem_fast_load, 0);
185     qemu_sem_init(&current_incoming->postcopy_qemufile_dst_done, 0);
186 
187     qemu_mutex_init(&current_incoming->page_request_mutex);
188     qemu_cond_init(&current_incoming->page_request_cond);
189     current_incoming->page_requested = g_tree_new(page_request_addr_cmp);
190 
191     migration_object_check(current_migration, &error_fatal);
192 
193     blk_mig_init();
194     ram_mig_init();
195     dirty_bitmap_mig_init();
196 }
197 
198 void migration_cancel(const Error *error)
199 {
200     if (error) {
201         migrate_set_error(current_migration, error);
202     }
203     if (migrate_dirty_limit()) {
204         qmp_cancel_vcpu_dirty_limit(false, -1, NULL);
205     }
206     migrate_fd_cancel(current_migration);
207 }
208 
209 void migration_shutdown(void)
210 {
211     /*
212      * When the QEMU main thread exit, the COLO thread
213      * may wait a semaphore. So, we should wakeup the
214      * COLO thread before migration shutdown.
215      */
216     colo_shutdown();
217     /*
218      * Cancel the current migration - that will (eventually)
219      * stop the migration using this structure
220      */
221     migration_cancel(NULL);
222     object_unref(OBJECT(current_migration));
223 
224     /*
225      * Cancel outgoing migration of dirty bitmaps. It should
226      * at least unref used block nodes.
227      */
228     dirty_bitmap_mig_cancel_outgoing();
229 
230     /*
231      * Cancel incoming migration of dirty bitmaps. Dirty bitmaps
232      * are non-critical data, and their loss never considered as
233      * something serious.
234      */
235     dirty_bitmap_mig_cancel_incoming();
236 }
237 
238 /* For outgoing */
239 MigrationState *migrate_get_current(void)
240 {
241     /* This can only be called after the object created. */
242     assert(current_migration);
243     return current_migration;
244 }
245 
246 MigrationIncomingState *migration_incoming_get_current(void)
247 {
248     assert(current_incoming);
249     return current_incoming;
250 }
251 
252 void migration_incoming_transport_cleanup(MigrationIncomingState *mis)
253 {
254     if (mis->socket_address_list) {
255         qapi_free_SocketAddressList(mis->socket_address_list);
256         mis->socket_address_list = NULL;
257     }
258 
259     if (mis->transport_cleanup) {
260         mis->transport_cleanup(mis->transport_data);
261         mis->transport_data = mis->transport_cleanup = NULL;
262     }
263 }
264 
265 void migration_incoming_state_destroy(void)
266 {
267     struct MigrationIncomingState *mis = migration_incoming_get_current();
268 
269     multifd_load_cleanup();
270     compress_threads_load_cleanup();
271 
272     if (mis->to_src_file) {
273         /* Tell source that we are done */
274         migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
275         qemu_fclose(mis->to_src_file);
276         mis->to_src_file = NULL;
277     }
278 
279     if (mis->from_src_file) {
280         migration_ioc_unregister_yank_from_file(mis->from_src_file);
281         qemu_fclose(mis->from_src_file);
282         mis->from_src_file = NULL;
283     }
284     if (mis->postcopy_remote_fds) {
285         g_array_free(mis->postcopy_remote_fds, TRUE);
286         mis->postcopy_remote_fds = NULL;
287     }
288 
289     migration_incoming_transport_cleanup(mis);
290     qemu_event_reset(&mis->main_thread_load_event);
291 
292     if (mis->page_requested) {
293         g_tree_destroy(mis->page_requested);
294         mis->page_requested = NULL;
295     }
296 
297     if (mis->postcopy_qemufile_dst) {
298         migration_ioc_unregister_yank_from_file(mis->postcopy_qemufile_dst);
299         qemu_fclose(mis->postcopy_qemufile_dst);
300         mis->postcopy_qemufile_dst = NULL;
301     }
302 
303     yank_unregister_instance(MIGRATION_YANK_INSTANCE);
304 }
305 
306 static void migrate_generate_event(int new_state)
307 {
308     if (migrate_events()) {
309         qapi_event_send_migration(new_state);
310     }
311 }
312 
313 /*
314  * Send a message on the return channel back to the source
315  * of the migration.
316  */
317 static int migrate_send_rp_message(MigrationIncomingState *mis,
318                                    enum mig_rp_message_type message_type,
319                                    uint16_t len, void *data)
320 {
321     int ret = 0;
322 
323     trace_migrate_send_rp_message((int)message_type, len);
324     QEMU_LOCK_GUARD(&mis->rp_mutex);
325 
326     /*
327      * It's possible that the file handle got lost due to network
328      * failures.
329      */
330     if (!mis->to_src_file) {
331         ret = -EIO;
332         return ret;
333     }
334 
335     qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
336     qemu_put_be16(mis->to_src_file, len);
337     qemu_put_buffer(mis->to_src_file, data, len);
338     return qemu_fflush(mis->to_src_file);
339 }
340 
341 /* Request one page from the source VM at the given start address.
342  *   rb: the RAMBlock to request the page in
343  *   Start: Address offset within the RB
344  *   Len: Length in bytes required - must be a multiple of pagesize
345  */
346 int migrate_send_rp_message_req_pages(MigrationIncomingState *mis,
347                                       RAMBlock *rb, ram_addr_t start)
348 {
349     uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
350     size_t msglen = 12; /* start + len */
351     size_t len = qemu_ram_pagesize(rb);
352     enum mig_rp_message_type msg_type;
353     const char *rbname;
354     int rbname_len;
355 
356     *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
357     *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
358 
359     /*
360      * We maintain the last ramblock that we requested for page.  Note that we
361      * don't need locking because this function will only be called within the
362      * postcopy ram fault thread.
363      */
364     if (rb != mis->last_rb) {
365         mis->last_rb = rb;
366 
367         rbname = qemu_ram_get_idstr(rb);
368         rbname_len = strlen(rbname);
369 
370         assert(rbname_len < 256);
371 
372         bufc[msglen++] = rbname_len;
373         memcpy(bufc + msglen, rbname, rbname_len);
374         msglen += rbname_len;
375         msg_type = MIG_RP_MSG_REQ_PAGES_ID;
376     } else {
377         msg_type = MIG_RP_MSG_REQ_PAGES;
378     }
379 
380     return migrate_send_rp_message(mis, msg_type, msglen, bufc);
381 }
382 
383 int migrate_send_rp_req_pages(MigrationIncomingState *mis,
384                               RAMBlock *rb, ram_addr_t start, uint64_t haddr)
385 {
386     void *aligned = (void *)(uintptr_t)ROUND_DOWN(haddr, qemu_ram_pagesize(rb));
387     bool received = false;
388 
389     WITH_QEMU_LOCK_GUARD(&mis->page_request_mutex) {
390         received = ramblock_recv_bitmap_test_byte_offset(rb, start);
391         if (!received && !g_tree_lookup(mis->page_requested, aligned)) {
392             /*
393              * The page has not been received, and it's not yet in the page
394              * request list.  Queue it.  Set the value of element to 1, so that
395              * things like g_tree_lookup() will return TRUE (1) when found.
396              */
397             g_tree_insert(mis->page_requested, aligned, (gpointer)1);
398             qatomic_inc(&mis->page_requested_count);
399             trace_postcopy_page_req_add(aligned, mis->page_requested_count);
400         }
401     }
402 
403     /*
404      * If the page is there, skip sending the message.  We don't even need the
405      * lock because as long as the page arrived, it'll be there forever.
406      */
407     if (received) {
408         return 0;
409     }
410 
411     return migrate_send_rp_message_req_pages(mis, rb, start);
412 }
413 
414 static bool migration_colo_enabled;
415 bool migration_incoming_colo_enabled(void)
416 {
417     return migration_colo_enabled;
418 }
419 
420 void migration_incoming_disable_colo(void)
421 {
422     ram_block_discard_disable(false);
423     migration_colo_enabled = false;
424 }
425 
426 int migration_incoming_enable_colo(void)
427 {
428 #ifndef CONFIG_REPLICATION
429     error_report("ENABLE_COLO command come in migration stream, but COLO "
430                  "module is not built in");
431     return -ENOTSUP;
432 #endif
433 
434     if (!migrate_colo()) {
435         error_report("ENABLE_COLO command come in migration stream, but c-colo "
436                      "capability is not set");
437         return -EINVAL;
438     }
439 
440     if (ram_block_discard_disable(true)) {
441         error_report("COLO: cannot disable RAM discard");
442         return -EBUSY;
443     }
444     migration_colo_enabled = true;
445     return 0;
446 }
447 
448 void migrate_add_address(SocketAddress *address)
449 {
450     MigrationIncomingState *mis = migration_incoming_get_current();
451 
452     QAPI_LIST_PREPEND(mis->socket_address_list,
453                       QAPI_CLONE(SocketAddress, address));
454 }
455 
456 static void qemu_start_incoming_migration(const char *uri, Error **errp)
457 {
458     const char *p = NULL;
459     MigrationIncomingState *mis = migration_incoming_get_current();
460 
461     /* URI is not suitable for migration? */
462     if (!migration_channels_and_uri_compatible(uri, errp)) {
463         return;
464     }
465 
466     migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
467                       MIGRATION_STATUS_SETUP);
468 
469     if (strstart(uri, "tcp:", &p) ||
470         strstart(uri, "unix:", NULL) ||
471         strstart(uri, "vsock:", NULL)) {
472         socket_start_incoming_migration(p ? p : uri, errp);
473 #ifdef CONFIG_RDMA
474     } else if (strstart(uri, "rdma:", &p)) {
475         if (migrate_compress()) {
476             error_setg(errp, "RDMA and compression can't be used together");
477             return;
478         }
479         if (migrate_xbzrle()) {
480             error_setg(errp, "RDMA and XBZRLE can't be used together");
481             return;
482         }
483         if (migrate_multifd()) {
484             error_setg(errp, "RDMA and multifd can't be used together");
485             return;
486         }
487         rdma_start_incoming_migration(p, errp);
488 #endif
489     } else if (strstart(uri, "exec:", &p)) {
490         exec_start_incoming_migration(p, errp);
491     } else if (strstart(uri, "fd:", &p)) {
492         fd_start_incoming_migration(p, errp);
493     } else if (strstart(uri, "file:", &p)) {
494         file_start_incoming_migration(p, errp);
495     } else {
496         error_setg(errp, "unknown migration protocol: %s", uri);
497     }
498 }
499 
500 static void process_incoming_migration_bh(void *opaque)
501 {
502     Error *local_err = NULL;
503     MigrationIncomingState *mis = opaque;
504 
505     trace_vmstate_downtime_checkpoint("dst-precopy-bh-enter");
506 
507     /* If capability late_block_activate is set:
508      * Only fire up the block code now if we're going to restart the
509      * VM, else 'cont' will do it.
510      * This causes file locking to happen; so we don't want it to happen
511      * unless we really are starting the VM.
512      */
513     if (!migrate_late_block_activate() ||
514          (autostart && (!global_state_received() ||
515             global_state_get_runstate() == RUN_STATE_RUNNING))) {
516         /* Make sure all file formats throw away their mutable metadata.
517          * If we get an error here, just don't restart the VM yet. */
518         bdrv_activate_all(&local_err);
519         if (local_err) {
520             error_report_err(local_err);
521             local_err = NULL;
522             autostart = false;
523         }
524     }
525 
526     /*
527      * This must happen after all error conditions are dealt with and
528      * we're sure the VM is going to be running on this host.
529      */
530     qemu_announce_self(&mis->announce_timer, migrate_announce_params());
531 
532     trace_vmstate_downtime_checkpoint("dst-precopy-bh-announced");
533 
534     multifd_load_shutdown();
535 
536     dirty_bitmap_mig_before_vm_start();
537 
538     if (!global_state_received() ||
539         global_state_get_runstate() == RUN_STATE_RUNNING) {
540         if (autostart) {
541             vm_start();
542         } else {
543             runstate_set(RUN_STATE_PAUSED);
544         }
545     } else if (migration_incoming_colo_enabled()) {
546         migration_incoming_disable_colo();
547         vm_start();
548     } else {
549         runstate_set(global_state_get_runstate());
550     }
551     trace_vmstate_downtime_checkpoint("dst-precopy-bh-vm-started");
552     /*
553      * This must happen after any state changes since as soon as an external
554      * observer sees this event they might start to prod at the VM assuming
555      * it's ready to use.
556      */
557     migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
558                       MIGRATION_STATUS_COMPLETED);
559     qemu_bh_delete(mis->bh);
560     migration_incoming_state_destroy();
561 }
562 
563 static void coroutine_fn
564 process_incoming_migration_co(void *opaque)
565 {
566     MigrationIncomingState *mis = migration_incoming_get_current();
567     PostcopyState ps;
568     int ret;
569 
570     assert(mis->from_src_file);
571 
572     if (compress_threads_load_setup(mis->from_src_file)) {
573         error_report("Failed to setup decompress threads");
574         goto fail;
575     }
576 
577     mis->largest_page_size = qemu_ram_pagesize_largest();
578     postcopy_state_set(POSTCOPY_INCOMING_NONE);
579     migrate_set_state(&mis->state, MIGRATION_STATUS_SETUP,
580                       MIGRATION_STATUS_ACTIVE);
581 
582     mis->loadvm_co = qemu_coroutine_self();
583     ret = qemu_loadvm_state(mis->from_src_file);
584     mis->loadvm_co = NULL;
585 
586     trace_vmstate_downtime_checkpoint("dst-precopy-loadvm-completed");
587 
588     ps = postcopy_state_get();
589     trace_process_incoming_migration_co_end(ret, ps);
590     if (ps != POSTCOPY_INCOMING_NONE) {
591         if (ps == POSTCOPY_INCOMING_ADVISE) {
592             /*
593              * Where a migration had postcopy enabled (and thus went to advise)
594              * but managed to complete within the precopy period, we can use
595              * the normal exit.
596              */
597             postcopy_ram_incoming_cleanup(mis);
598         } else if (ret >= 0) {
599             /*
600              * Postcopy was started, cleanup should happen at the end of the
601              * postcopy thread.
602              */
603             trace_process_incoming_migration_co_postcopy_end_main();
604             return;
605         }
606         /* Else if something went wrong then just fall out of the normal exit */
607     }
608 
609     if (ret < 0) {
610         error_report("load of migration failed: %s", strerror(-ret));
611         goto fail;
612     }
613 
614     if (colo_incoming_co() < 0) {
615         goto fail;
616     }
617 
618     mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
619     qemu_bh_schedule(mis->bh);
620     return;
621 fail:
622     migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
623                       MIGRATION_STATUS_FAILED);
624     qemu_fclose(mis->from_src_file);
625 
626     multifd_load_cleanup();
627     compress_threads_load_cleanup();
628 
629     exit(EXIT_FAILURE);
630 }
631 
632 /**
633  * migration_incoming_setup: Setup incoming migration
634  * @f: file for main migration channel
635  * @errp: where to put errors
636  *
637  * Returns: %true on success, %false on error.
638  */
639 static bool migration_incoming_setup(QEMUFile *f, Error **errp)
640 {
641     MigrationIncomingState *mis = migration_incoming_get_current();
642 
643     if (!mis->from_src_file) {
644         mis->from_src_file = f;
645     }
646     qemu_file_set_blocking(f, false);
647     return true;
648 }
649 
650 void migration_incoming_process(void)
651 {
652     Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL);
653     qemu_coroutine_enter(co);
654 }
655 
656 /* Returns true if recovered from a paused migration, otherwise false */
657 static bool postcopy_try_recover(void)
658 {
659     MigrationIncomingState *mis = migration_incoming_get_current();
660 
661     if (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
662         /* Resumed from a paused postcopy migration */
663 
664         /* This should be set already in migration_incoming_setup() */
665         assert(mis->from_src_file);
666         /* Postcopy has standalone thread to do vm load */
667         qemu_file_set_blocking(mis->from_src_file, true);
668 
669         /* Re-configure the return path */
670         mis->to_src_file = qemu_file_get_return_path(mis->from_src_file);
671 
672         migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
673                           MIGRATION_STATUS_POSTCOPY_RECOVER);
674 
675         /*
676          * Here, we only wake up the main loading thread (while the
677          * rest threads will still be waiting), so that we can receive
678          * commands from source now, and answer it if needed. The
679          * rest threads will be woken up afterwards until we are sure
680          * that source is ready to reply to page requests.
681          */
682         qemu_sem_post(&mis->postcopy_pause_sem_dst);
683         return true;
684     }
685 
686     return false;
687 }
688 
689 void migration_fd_process_incoming(QEMUFile *f, Error **errp)
690 {
691     if (!migration_incoming_setup(f, errp)) {
692         return;
693     }
694     if (postcopy_try_recover()) {
695         return;
696     }
697     migration_incoming_process();
698 }
699 
700 /*
701  * Returns true when we want to start a new incoming migration process,
702  * false otherwise.
703  */
704 static bool migration_should_start_incoming(bool main_channel)
705 {
706     /* Multifd doesn't start unless all channels are established */
707     if (migrate_multifd()) {
708         return migration_has_all_channels();
709     }
710 
711     /* Preempt channel only starts when the main channel is created */
712     if (migrate_postcopy_preempt()) {
713         return main_channel;
714     }
715 
716     /*
717      * For all the rest types of migration, we should only reach here when
718      * it's the main channel that's being created, and we should always
719      * proceed with this channel.
720      */
721     assert(main_channel);
722     return true;
723 }
724 
725 void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp)
726 {
727     MigrationIncomingState *mis = migration_incoming_get_current();
728     Error *local_err = NULL;
729     QEMUFile *f;
730     bool default_channel = true;
731     uint32_t channel_magic = 0;
732     int ret = 0;
733 
734     if (migrate_multifd() && !migrate_postcopy_ram() &&
735         qio_channel_has_feature(ioc, QIO_CHANNEL_FEATURE_READ_MSG_PEEK)) {
736         /*
737          * With multiple channels, it is possible that we receive channels
738          * out of order on destination side, causing incorrect mapping of
739          * source channels on destination side. Check channel MAGIC to
740          * decide type of channel. Please note this is best effort, postcopy
741          * preempt channel does not send any magic number so avoid it for
742          * postcopy live migration. Also tls live migration already does
743          * tls handshake while initializing main channel so with tls this
744          * issue is not possible.
745          */
746         ret = migration_channel_read_peek(ioc, (void *)&channel_magic,
747                                           sizeof(channel_magic), &local_err);
748 
749         if (ret != 0) {
750             error_propagate(errp, local_err);
751             return;
752         }
753 
754         default_channel = (channel_magic == cpu_to_be32(QEMU_VM_FILE_MAGIC));
755     } else {
756         default_channel = !mis->from_src_file;
757     }
758 
759     if (multifd_load_setup(errp) != 0) {
760         error_setg(errp, "Failed to setup multifd channels");
761         return;
762     }
763 
764     if (default_channel) {
765         f = qemu_file_new_input(ioc);
766 
767         if (!migration_incoming_setup(f, errp)) {
768             return;
769         }
770     } else {
771         /* Multiple connections */
772         assert(migration_needs_multiple_sockets());
773         if (migrate_multifd()) {
774             multifd_recv_new_channel(ioc, &local_err);
775         } else {
776             assert(migrate_postcopy_preempt());
777             f = qemu_file_new_input(ioc);
778             postcopy_preempt_new_channel(mis, f);
779         }
780         if (local_err) {
781             error_propagate(errp, local_err);
782             return;
783         }
784     }
785 
786     if (migration_should_start_incoming(default_channel)) {
787         /* If it's a recovery, we're done */
788         if (postcopy_try_recover()) {
789             return;
790         }
791         migration_incoming_process();
792     }
793 }
794 
795 /**
796  * @migration_has_all_channels: We have received all channels that we need
797  *
798  * Returns true when we have got connections to all the channels that
799  * we need for migration.
800  */
801 bool migration_has_all_channels(void)
802 {
803     MigrationIncomingState *mis = migration_incoming_get_current();
804 
805     if (!mis->from_src_file) {
806         return false;
807     }
808 
809     if (migrate_multifd()) {
810         return multifd_recv_all_channels_created();
811     }
812 
813     if (migrate_postcopy_preempt()) {
814         return mis->postcopy_qemufile_dst != NULL;
815     }
816 
817     return true;
818 }
819 
820 int migrate_send_rp_switchover_ack(MigrationIncomingState *mis)
821 {
822     return migrate_send_rp_message(mis, MIG_RP_MSG_SWITCHOVER_ACK, 0, NULL);
823 }
824 
825 /*
826  * Send a 'SHUT' message on the return channel with the given value
827  * to indicate that we've finished with the RP.  Non-0 value indicates
828  * error.
829  */
830 void migrate_send_rp_shut(MigrationIncomingState *mis,
831                           uint32_t value)
832 {
833     uint32_t buf;
834 
835     buf = cpu_to_be32(value);
836     migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
837 }
838 
839 /*
840  * Send a 'PONG' message on the return channel with the given value
841  * (normally in response to a 'PING')
842  */
843 void migrate_send_rp_pong(MigrationIncomingState *mis,
844                           uint32_t value)
845 {
846     uint32_t buf;
847 
848     buf = cpu_to_be32(value);
849     migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
850 }
851 
852 void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
853                                  char *block_name)
854 {
855     char buf[512];
856     int len;
857     int64_t res;
858 
859     /*
860      * First, we send the header part. It contains only the len of
861      * idstr, and the idstr itself.
862      */
863     len = strlen(block_name);
864     buf[0] = len;
865     memcpy(buf + 1, block_name, len);
866 
867     if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
868         error_report("%s: MSG_RP_RECV_BITMAP only used for recovery",
869                      __func__);
870         return;
871     }
872 
873     migrate_send_rp_message(mis, MIG_RP_MSG_RECV_BITMAP, len + 1, buf);
874 
875     /*
876      * Next, we dump the received bitmap to the stream.
877      *
878      * TODO: currently we are safe since we are the only one that is
879      * using the to_src_file handle (fault thread is still paused),
880      * and it's ok even not taking the mutex. However the best way is
881      * to take the lock before sending the message header, and release
882      * the lock after sending the bitmap.
883      */
884     qemu_mutex_lock(&mis->rp_mutex);
885     res = ramblock_recv_bitmap_send(mis->to_src_file, block_name);
886     qemu_mutex_unlock(&mis->rp_mutex);
887 
888     trace_migrate_send_rp_recv_bitmap(block_name, res);
889 }
890 
891 void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value)
892 {
893     uint32_t buf;
894 
895     buf = cpu_to_be32(value);
896     migrate_send_rp_message(mis, MIG_RP_MSG_RESUME_ACK, sizeof(buf), &buf);
897 }
898 
899 /*
900  * Return true if we're already in the middle of a migration
901  * (i.e. any of the active or setup states)
902  */
903 bool migration_is_setup_or_active(int state)
904 {
905     switch (state) {
906     case MIGRATION_STATUS_ACTIVE:
907     case MIGRATION_STATUS_POSTCOPY_ACTIVE:
908     case MIGRATION_STATUS_POSTCOPY_PAUSED:
909     case MIGRATION_STATUS_POSTCOPY_RECOVER:
910     case MIGRATION_STATUS_SETUP:
911     case MIGRATION_STATUS_PRE_SWITCHOVER:
912     case MIGRATION_STATUS_DEVICE:
913     case MIGRATION_STATUS_WAIT_UNPLUG:
914     case MIGRATION_STATUS_COLO:
915         return true;
916 
917     default:
918         return false;
919 
920     }
921 }
922 
923 bool migration_is_running(int state)
924 {
925     switch (state) {
926     case MIGRATION_STATUS_ACTIVE:
927     case MIGRATION_STATUS_POSTCOPY_ACTIVE:
928     case MIGRATION_STATUS_POSTCOPY_PAUSED:
929     case MIGRATION_STATUS_POSTCOPY_RECOVER:
930     case MIGRATION_STATUS_SETUP:
931     case MIGRATION_STATUS_PRE_SWITCHOVER:
932     case MIGRATION_STATUS_DEVICE:
933     case MIGRATION_STATUS_WAIT_UNPLUG:
934     case MIGRATION_STATUS_CANCELLING:
935         return true;
936 
937     default:
938         return false;
939 
940     }
941 }
942 
943 static bool migrate_show_downtime(MigrationState *s)
944 {
945     return (s->state == MIGRATION_STATUS_COMPLETED) || migration_in_postcopy();
946 }
947 
948 static void populate_time_info(MigrationInfo *info, MigrationState *s)
949 {
950     info->has_status = true;
951     info->has_setup_time = true;
952     info->setup_time = s->setup_time;
953 
954     if (s->state == MIGRATION_STATUS_COMPLETED) {
955         info->has_total_time = true;
956         info->total_time = s->total_time;
957     } else {
958         info->has_total_time = true;
959         info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
960                            s->start_time;
961     }
962 
963     if (migrate_show_downtime(s)) {
964         info->has_downtime = true;
965         info->downtime = s->downtime;
966     } else {
967         info->has_expected_downtime = true;
968         info->expected_downtime = s->expected_downtime;
969     }
970 }
971 
972 static void populate_ram_info(MigrationInfo *info, MigrationState *s)
973 {
974     size_t page_size = qemu_target_page_size();
975 
976     info->ram = g_malloc0(sizeof(*info->ram));
977     info->ram->transferred = migration_transferred_bytes();
978     info->ram->total = ram_bytes_total();
979     info->ram->duplicate = stat64_get(&mig_stats.zero_pages);
980     /* legacy value.  It is not used anymore */
981     info->ram->skipped = 0;
982     info->ram->normal = stat64_get(&mig_stats.normal_pages);
983     info->ram->normal_bytes = info->ram->normal * page_size;
984     info->ram->mbps = s->mbps;
985     info->ram->dirty_sync_count =
986         stat64_get(&mig_stats.dirty_sync_count);
987     info->ram->dirty_sync_missed_zero_copy =
988         stat64_get(&mig_stats.dirty_sync_missed_zero_copy);
989     info->ram->postcopy_requests =
990         stat64_get(&mig_stats.postcopy_requests);
991     info->ram->page_size = page_size;
992     info->ram->multifd_bytes = stat64_get(&mig_stats.multifd_bytes);
993     info->ram->pages_per_second = s->pages_per_second;
994     info->ram->precopy_bytes = stat64_get(&mig_stats.precopy_bytes);
995     info->ram->downtime_bytes = stat64_get(&mig_stats.downtime_bytes);
996     info->ram->postcopy_bytes = stat64_get(&mig_stats.postcopy_bytes);
997 
998     if (migrate_xbzrle()) {
999         info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
1000         info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
1001         info->xbzrle_cache->bytes = xbzrle_counters.bytes;
1002         info->xbzrle_cache->pages = xbzrle_counters.pages;
1003         info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
1004         info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
1005         info->xbzrle_cache->encoding_rate = xbzrle_counters.encoding_rate;
1006         info->xbzrle_cache->overflow = xbzrle_counters.overflow;
1007     }
1008 
1009     populate_compress(info);
1010 
1011     if (cpu_throttle_active()) {
1012         info->has_cpu_throttle_percentage = true;
1013         info->cpu_throttle_percentage = cpu_throttle_get_percentage();
1014     }
1015 
1016     if (s->state != MIGRATION_STATUS_COMPLETED) {
1017         info->ram->remaining = ram_bytes_remaining();
1018         info->ram->dirty_pages_rate =
1019            stat64_get(&mig_stats.dirty_pages_rate);
1020     }
1021 
1022     if (migrate_dirty_limit() && dirtylimit_in_service()) {
1023         info->has_dirty_limit_throttle_time_per_round = true;
1024         info->dirty_limit_throttle_time_per_round =
1025                             dirtylimit_throttle_time_per_round();
1026 
1027         info->has_dirty_limit_ring_full_time = true;
1028         info->dirty_limit_ring_full_time = dirtylimit_ring_full_time();
1029     }
1030 }
1031 
1032 static void populate_disk_info(MigrationInfo *info)
1033 {
1034     if (blk_mig_active()) {
1035         info->disk = g_malloc0(sizeof(*info->disk));
1036         info->disk->transferred = blk_mig_bytes_transferred();
1037         info->disk->remaining = blk_mig_bytes_remaining();
1038         info->disk->total = blk_mig_bytes_total();
1039     }
1040 }
1041 
1042 static void fill_source_migration_info(MigrationInfo *info)
1043 {
1044     MigrationState *s = migrate_get_current();
1045     int state = qatomic_read(&s->state);
1046     GSList *cur_blocker = migration_blockers;
1047 
1048     info->blocked_reasons = NULL;
1049 
1050     /*
1051      * There are two types of reasons a migration might be blocked;
1052      * a) devices marked in VMState as non-migratable, and
1053      * b) Explicit migration blockers
1054      * We need to add both of them here.
1055      */
1056     qemu_savevm_non_migratable_list(&info->blocked_reasons);
1057 
1058     while (cur_blocker) {
1059         QAPI_LIST_PREPEND(info->blocked_reasons,
1060                           g_strdup(error_get_pretty(cur_blocker->data)));
1061         cur_blocker = g_slist_next(cur_blocker);
1062     }
1063     info->has_blocked_reasons = info->blocked_reasons != NULL;
1064 
1065     switch (state) {
1066     case MIGRATION_STATUS_NONE:
1067         /* no migration has happened ever */
1068         /* do not overwrite destination migration status */
1069         return;
1070     case MIGRATION_STATUS_SETUP:
1071         info->has_status = true;
1072         info->has_total_time = false;
1073         break;
1074     case MIGRATION_STATUS_ACTIVE:
1075     case MIGRATION_STATUS_CANCELLING:
1076     case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1077     case MIGRATION_STATUS_PRE_SWITCHOVER:
1078     case MIGRATION_STATUS_DEVICE:
1079     case MIGRATION_STATUS_POSTCOPY_PAUSED:
1080     case MIGRATION_STATUS_POSTCOPY_RECOVER:
1081         /* TODO add some postcopy stats */
1082         populate_time_info(info, s);
1083         populate_ram_info(info, s);
1084         populate_disk_info(info);
1085         migration_populate_vfio_info(info);
1086         break;
1087     case MIGRATION_STATUS_COLO:
1088         info->has_status = true;
1089         /* TODO: display COLO specific information (checkpoint info etc.) */
1090         break;
1091     case MIGRATION_STATUS_COMPLETED:
1092         populate_time_info(info, s);
1093         populate_ram_info(info, s);
1094         migration_populate_vfio_info(info);
1095         break;
1096     case MIGRATION_STATUS_FAILED:
1097         info->has_status = true;
1098         break;
1099     case MIGRATION_STATUS_CANCELLED:
1100         info->has_status = true;
1101         break;
1102     case MIGRATION_STATUS_WAIT_UNPLUG:
1103         info->has_status = true;
1104         break;
1105     }
1106     info->status = state;
1107 
1108     QEMU_LOCK_GUARD(&s->error_mutex);
1109     if (s->error) {
1110         info->error_desc = g_strdup(error_get_pretty(s->error));
1111     }
1112 }
1113 
1114 static void fill_destination_migration_info(MigrationInfo *info)
1115 {
1116     MigrationIncomingState *mis = migration_incoming_get_current();
1117 
1118     if (mis->socket_address_list) {
1119         info->has_socket_address = true;
1120         info->socket_address =
1121             QAPI_CLONE(SocketAddressList, mis->socket_address_list);
1122     }
1123 
1124     switch (mis->state) {
1125     case MIGRATION_STATUS_NONE:
1126         return;
1127     case MIGRATION_STATUS_SETUP:
1128     case MIGRATION_STATUS_CANCELLING:
1129     case MIGRATION_STATUS_CANCELLED:
1130     case MIGRATION_STATUS_ACTIVE:
1131     case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1132     case MIGRATION_STATUS_POSTCOPY_PAUSED:
1133     case MIGRATION_STATUS_POSTCOPY_RECOVER:
1134     case MIGRATION_STATUS_FAILED:
1135     case MIGRATION_STATUS_COLO:
1136         info->has_status = true;
1137         break;
1138     case MIGRATION_STATUS_COMPLETED:
1139         info->has_status = true;
1140         fill_destination_postcopy_migration_info(info);
1141         break;
1142     }
1143     info->status = mis->state;
1144 }
1145 
1146 MigrationInfo *qmp_query_migrate(Error **errp)
1147 {
1148     MigrationInfo *info = g_malloc0(sizeof(*info));
1149 
1150     fill_destination_migration_info(info);
1151     fill_source_migration_info(info);
1152 
1153     return info;
1154 }
1155 
1156 void qmp_migrate_start_postcopy(Error **errp)
1157 {
1158     MigrationState *s = migrate_get_current();
1159 
1160     if (!migrate_postcopy()) {
1161         error_setg(errp, "Enable postcopy with migrate_set_capability before"
1162                          " the start of migration");
1163         return;
1164     }
1165 
1166     if (s->state == MIGRATION_STATUS_NONE) {
1167         error_setg(errp, "Postcopy must be started after migration has been"
1168                          " started");
1169         return;
1170     }
1171     /*
1172      * we don't error if migration has finished since that would be racy
1173      * with issuing this command.
1174      */
1175     qatomic_set(&s->start_postcopy, true);
1176 }
1177 
1178 /* shared migration helpers */
1179 
1180 void migrate_set_state(int *state, int old_state, int new_state)
1181 {
1182     assert(new_state < MIGRATION_STATUS__MAX);
1183     if (qatomic_cmpxchg(state, old_state, new_state) == old_state) {
1184         trace_migrate_set_state(MigrationStatus_str(new_state));
1185         migrate_generate_event(new_state);
1186     }
1187 }
1188 
1189 static void migrate_fd_cleanup(MigrationState *s)
1190 {
1191     qemu_bh_delete(s->cleanup_bh);
1192     s->cleanup_bh = NULL;
1193 
1194     g_free(s->hostname);
1195     s->hostname = NULL;
1196     json_writer_free(s->vmdesc);
1197     s->vmdesc = NULL;
1198 
1199     qemu_savevm_state_cleanup();
1200 
1201     if (s->to_dst_file) {
1202         QEMUFile *tmp;
1203 
1204         trace_migrate_fd_cleanup();
1205         qemu_mutex_unlock_iothread();
1206         if (s->migration_thread_running) {
1207             qemu_thread_join(&s->thread);
1208             s->migration_thread_running = false;
1209         }
1210         qemu_mutex_lock_iothread();
1211 
1212         multifd_save_cleanup();
1213         qemu_mutex_lock(&s->qemu_file_lock);
1214         tmp = s->to_dst_file;
1215         s->to_dst_file = NULL;
1216         qemu_mutex_unlock(&s->qemu_file_lock);
1217         /*
1218          * Close the file handle without the lock to make sure the
1219          * critical section won't block for long.
1220          */
1221         migration_ioc_unregister_yank_from_file(tmp);
1222         qemu_fclose(tmp);
1223     }
1224 
1225     /*
1226      * We already cleaned up to_dst_file, so errors from the return
1227      * path might be due to that, ignore them.
1228      */
1229     close_return_path_on_source(s);
1230 
1231     assert(!migration_is_active(s));
1232 
1233     if (s->state == MIGRATION_STATUS_CANCELLING) {
1234         migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
1235                           MIGRATION_STATUS_CANCELLED);
1236     }
1237 
1238     if (s->error) {
1239         /* It is used on info migrate.  We can't free it */
1240         error_report_err(error_copy(s->error));
1241     }
1242     migration_call_notifiers(s);
1243     block_cleanup_parameters();
1244     yank_unregister_instance(MIGRATION_YANK_INSTANCE);
1245 }
1246 
1247 static void migrate_fd_cleanup_schedule(MigrationState *s)
1248 {
1249     /*
1250      * Ref the state for bh, because it may be called when
1251      * there're already no other refs
1252      */
1253     object_ref(OBJECT(s));
1254     qemu_bh_schedule(s->cleanup_bh);
1255 }
1256 
1257 static void migrate_fd_cleanup_bh(void *opaque)
1258 {
1259     MigrationState *s = opaque;
1260     migrate_fd_cleanup(s);
1261     object_unref(OBJECT(s));
1262 }
1263 
1264 void migrate_set_error(MigrationState *s, const Error *error)
1265 {
1266     QEMU_LOCK_GUARD(&s->error_mutex);
1267     if (!s->error) {
1268         s->error = error_copy(error);
1269     }
1270 }
1271 
1272 bool migrate_has_error(MigrationState *s)
1273 {
1274     /* The lock is not helpful here, but still follow the rule */
1275     QEMU_LOCK_GUARD(&s->error_mutex);
1276     return qatomic_read(&s->error);
1277 }
1278 
1279 static void migrate_error_free(MigrationState *s)
1280 {
1281     QEMU_LOCK_GUARD(&s->error_mutex);
1282     if (s->error) {
1283         error_free(s->error);
1284         s->error = NULL;
1285     }
1286 }
1287 
1288 static void migrate_fd_error(MigrationState *s, const Error *error)
1289 {
1290     trace_migrate_fd_error(error_get_pretty(error));
1291     assert(s->to_dst_file == NULL);
1292     migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1293                       MIGRATION_STATUS_FAILED);
1294     migrate_set_error(s, error);
1295 }
1296 
1297 static void migrate_fd_cancel(MigrationState *s)
1298 {
1299     int old_state ;
1300 
1301     trace_migrate_fd_cancel();
1302 
1303     WITH_QEMU_LOCK_GUARD(&s->qemu_file_lock) {
1304         if (s->rp_state.from_dst_file) {
1305             /* shutdown the rp socket, so causing the rp thread to shutdown */
1306             qemu_file_shutdown(s->rp_state.from_dst_file);
1307         }
1308     }
1309 
1310     do {
1311         old_state = s->state;
1312         if (!migration_is_running(old_state)) {
1313             break;
1314         }
1315         /* If the migration is paused, kick it out of the pause */
1316         if (old_state == MIGRATION_STATUS_PRE_SWITCHOVER) {
1317             qemu_sem_post(&s->pause_sem);
1318         }
1319         migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
1320     } while (s->state != MIGRATION_STATUS_CANCELLING);
1321 
1322     /*
1323      * If we're unlucky the migration code might be stuck somewhere in a
1324      * send/write while the network has failed and is waiting to timeout;
1325      * if we've got shutdown(2) available then we can force it to quit.
1326      */
1327     if (s->state == MIGRATION_STATUS_CANCELLING) {
1328         WITH_QEMU_LOCK_GUARD(&s->qemu_file_lock) {
1329             if (s->to_dst_file) {
1330                 qemu_file_shutdown(s->to_dst_file);
1331             }
1332         }
1333     }
1334     if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
1335         Error *local_err = NULL;
1336 
1337         bdrv_activate_all(&local_err);
1338         if (local_err) {
1339             error_report_err(local_err);
1340         } else {
1341             s->block_inactive = false;
1342         }
1343     }
1344 }
1345 
1346 void migration_add_notifier(Notifier *notify,
1347                             void (*func)(Notifier *notifier, void *data))
1348 {
1349     notify->notify = func;
1350     notifier_list_add(&migration_state_notifiers, notify);
1351 }
1352 
1353 void migration_remove_notifier(Notifier *notify)
1354 {
1355     if (notify->notify) {
1356         notifier_remove(notify);
1357         notify->notify = NULL;
1358     }
1359 }
1360 
1361 void migration_call_notifiers(MigrationState *s)
1362 {
1363     notifier_list_notify(&migration_state_notifiers, s);
1364 }
1365 
1366 bool migration_in_setup(MigrationState *s)
1367 {
1368     return s->state == MIGRATION_STATUS_SETUP;
1369 }
1370 
1371 bool migration_has_finished(MigrationState *s)
1372 {
1373     return s->state == MIGRATION_STATUS_COMPLETED;
1374 }
1375 
1376 bool migration_has_failed(MigrationState *s)
1377 {
1378     return (s->state == MIGRATION_STATUS_CANCELLED ||
1379             s->state == MIGRATION_STATUS_FAILED);
1380 }
1381 
1382 bool migration_in_postcopy(void)
1383 {
1384     MigrationState *s = migrate_get_current();
1385 
1386     switch (s->state) {
1387     case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1388     case MIGRATION_STATUS_POSTCOPY_PAUSED:
1389     case MIGRATION_STATUS_POSTCOPY_RECOVER:
1390         return true;
1391     default:
1392         return false;
1393     }
1394 }
1395 
1396 bool migration_in_postcopy_after_devices(MigrationState *s)
1397 {
1398     return migration_in_postcopy() && s->postcopy_after_devices;
1399 }
1400 
1401 bool migration_in_incoming_postcopy(void)
1402 {
1403     PostcopyState ps = postcopy_state_get();
1404 
1405     return ps >= POSTCOPY_INCOMING_DISCARD && ps < POSTCOPY_INCOMING_END;
1406 }
1407 
1408 bool migration_incoming_postcopy_advised(void)
1409 {
1410     PostcopyState ps = postcopy_state_get();
1411 
1412     return ps >= POSTCOPY_INCOMING_ADVISE && ps < POSTCOPY_INCOMING_END;
1413 }
1414 
1415 bool migration_in_bg_snapshot(void)
1416 {
1417     MigrationState *s = migrate_get_current();
1418 
1419     return migrate_background_snapshot() &&
1420             migration_is_setup_or_active(s->state);
1421 }
1422 
1423 bool migration_is_idle(void)
1424 {
1425     MigrationState *s = current_migration;
1426 
1427     if (!s) {
1428         return true;
1429     }
1430 
1431     switch (s->state) {
1432     case MIGRATION_STATUS_NONE:
1433     case MIGRATION_STATUS_CANCELLED:
1434     case MIGRATION_STATUS_COMPLETED:
1435     case MIGRATION_STATUS_FAILED:
1436         return true;
1437     case MIGRATION_STATUS_SETUP:
1438     case MIGRATION_STATUS_CANCELLING:
1439     case MIGRATION_STATUS_ACTIVE:
1440     case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1441     case MIGRATION_STATUS_COLO:
1442     case MIGRATION_STATUS_PRE_SWITCHOVER:
1443     case MIGRATION_STATUS_DEVICE:
1444     case MIGRATION_STATUS_WAIT_UNPLUG:
1445         return false;
1446     case MIGRATION_STATUS__MAX:
1447         g_assert_not_reached();
1448     }
1449 
1450     return false;
1451 }
1452 
1453 bool migration_is_active(MigrationState *s)
1454 {
1455     return (s->state == MIGRATION_STATUS_ACTIVE ||
1456             s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1457 }
1458 
1459 int migrate_init(MigrationState *s, Error **errp)
1460 {
1461     int ret;
1462 
1463     ret = qemu_savevm_state_prepare(errp);
1464     if (ret) {
1465         return ret;
1466     }
1467 
1468     /*
1469      * Reinitialise all migration state, except
1470      * parameters/capabilities that the user set, and
1471      * locks.
1472      */
1473     s->cleanup_bh = 0;
1474     s->vm_start_bh = 0;
1475     s->to_dst_file = NULL;
1476     s->state = MIGRATION_STATUS_NONE;
1477     s->rp_state.from_dst_file = NULL;
1478     s->rp_state.error = false;
1479     s->mbps = 0.0;
1480     s->pages_per_second = 0.0;
1481     s->downtime = 0;
1482     s->expected_downtime = 0;
1483     s->setup_time = 0;
1484     s->start_postcopy = false;
1485     s->postcopy_after_devices = false;
1486     s->migration_thread_running = false;
1487     error_free(s->error);
1488     s->error = NULL;
1489     s->hostname = NULL;
1490     s->vmdesc = NULL;
1491 
1492     migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
1493 
1494     s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1495     s->total_time = 0;
1496     s->vm_old_state = -1;
1497     s->iteration_initial_bytes = 0;
1498     s->threshold_size = 0;
1499     s->switchover_acked = false;
1500     s->rdma_migration = false;
1501     /*
1502      * set mig_stats memory to zero for a new migration
1503      */
1504     memset(&mig_stats, 0, sizeof(mig_stats));
1505     migration_reset_vfio_bytes_transferred();
1506 
1507     return 0;
1508 }
1509 
1510 int migrate_add_blocker_internal(Error **reasonp, Error **errp)
1511 {
1512     /* Snapshots are similar to migrations, so check RUN_STATE_SAVE_VM too. */
1513     if (runstate_check(RUN_STATE_SAVE_VM) || !migration_is_idle()) {
1514         error_propagate_prepend(errp, *reasonp,
1515                                 "disallowing migration blocker "
1516                                 "(migration/snapshot in progress) for: ");
1517         *reasonp = NULL;
1518         return -EBUSY;
1519     }
1520 
1521     migration_blockers = g_slist_prepend(migration_blockers, *reasonp);
1522     return 0;
1523 }
1524 
1525 int migrate_add_blocker(Error **reasonp, Error **errp)
1526 {
1527     if (only_migratable) {
1528         error_propagate_prepend(errp, *reasonp,
1529                                 "disallowing migration blocker "
1530                                 "(--only-migratable) for: ");
1531         *reasonp = NULL;
1532         return -EACCES;
1533     }
1534 
1535     return migrate_add_blocker_internal(reasonp, errp);
1536 }
1537 
1538 void migrate_del_blocker(Error **reasonp)
1539 {
1540     if (*reasonp) {
1541         migration_blockers = g_slist_remove(migration_blockers, *reasonp);
1542         error_free(*reasonp);
1543         *reasonp = NULL;
1544     }
1545 }
1546 
1547 void qmp_migrate_incoming(const char *uri, Error **errp)
1548 {
1549     Error *local_err = NULL;
1550     static bool once = true;
1551 
1552     if (!once) {
1553         error_setg(errp, "The incoming migration has already been started");
1554         return;
1555     }
1556     if (!runstate_check(RUN_STATE_INMIGRATE)) {
1557         error_setg(errp, "'-incoming' was not specified on the command line");
1558         return;
1559     }
1560 
1561     if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) {
1562         return;
1563     }
1564 
1565     qemu_start_incoming_migration(uri, &local_err);
1566 
1567     if (local_err) {
1568         yank_unregister_instance(MIGRATION_YANK_INSTANCE);
1569         error_propagate(errp, local_err);
1570         return;
1571     }
1572 
1573     once = false;
1574 }
1575 
1576 void qmp_migrate_recover(const char *uri, Error **errp)
1577 {
1578     MigrationIncomingState *mis = migration_incoming_get_current();
1579 
1580     /*
1581      * Don't even bother to use ERRP_GUARD() as it _must_ always be set by
1582      * callers (no one should ignore a recover failure); if there is, it's a
1583      * programming error.
1584      */
1585     assert(errp);
1586 
1587     if (mis->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
1588         error_setg(errp, "Migrate recover can only be run "
1589                    "when postcopy is paused.");
1590         return;
1591     }
1592 
1593     /* If there's an existing transport, release it */
1594     migration_incoming_transport_cleanup(mis);
1595 
1596     /*
1597      * Note that this call will never start a real migration; it will
1598      * only re-setup the migration stream and poke existing migration
1599      * to continue using that newly established channel.
1600      */
1601     qemu_start_incoming_migration(uri, errp);
1602 }
1603 
1604 void qmp_migrate_pause(Error **errp)
1605 {
1606     MigrationState *ms = migrate_get_current();
1607     MigrationIncomingState *mis = migration_incoming_get_current();
1608     int ret = 0;
1609 
1610     if (ms->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1611         /* Source side, during postcopy */
1612         qemu_mutex_lock(&ms->qemu_file_lock);
1613         if (ms->to_dst_file) {
1614             ret = qemu_file_shutdown(ms->to_dst_file);
1615         }
1616         qemu_mutex_unlock(&ms->qemu_file_lock);
1617         if (ret) {
1618             error_setg(errp, "Failed to pause source migration");
1619         }
1620         return;
1621     }
1622 
1623     if (mis->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1624         ret = qemu_file_shutdown(mis->from_src_file);
1625         if (ret) {
1626             error_setg(errp, "Failed to pause destination migration");
1627         }
1628         return;
1629     }
1630 
1631     error_setg(errp, "migrate-pause is currently only supported "
1632                "during postcopy-active state");
1633 }
1634 
1635 bool migration_is_blocked(Error **errp)
1636 {
1637     if (qemu_savevm_state_blocked(errp)) {
1638         return true;
1639     }
1640 
1641     if (migration_blockers) {
1642         error_propagate(errp, error_copy(migration_blockers->data));
1643         return true;
1644     }
1645 
1646     return false;
1647 }
1648 
1649 /* Returns true if continue to migrate, or false if error detected */
1650 static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc,
1651                             bool resume, Error **errp)
1652 {
1653     Error *local_err = NULL;
1654 
1655     if (blk_inc) {
1656         warn_report("parameter 'inc' is deprecated;"
1657                     " use blockdev-mirror with NBD instead");
1658     }
1659 
1660     if (blk) {
1661         warn_report("parameter 'blk' is deprecated;"
1662                     " use blockdev-mirror with NBD instead");
1663     }
1664 
1665     if (resume) {
1666         if (s->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
1667             error_setg(errp, "Cannot resume if there is no "
1668                        "paused migration");
1669             return false;
1670         }
1671 
1672         /*
1673          * Postcopy recovery won't work well with release-ram
1674          * capability since release-ram will drop the page buffer as
1675          * long as the page is put into the send buffer.  So if there
1676          * is a network failure happened, any page buffers that have
1677          * not yet reached the destination VM but have already been
1678          * sent from the source VM will be lost forever.  Let's refuse
1679          * the client from resuming such a postcopy migration.
1680          * Luckily release-ram was designed to only be used when src
1681          * and destination VMs are on the same host, so it should be
1682          * fine.
1683          */
1684         if (migrate_release_ram()) {
1685             error_setg(errp, "Postcopy recovery cannot work "
1686                        "when release-ram capability is set");
1687             return false;
1688         }
1689 
1690         /* This is a resume, skip init status */
1691         return true;
1692     }
1693 
1694     if (migration_is_running(s->state)) {
1695         error_setg(errp, QERR_MIGRATION_ACTIVE);
1696         return false;
1697     }
1698 
1699     if (runstate_check(RUN_STATE_INMIGRATE)) {
1700         error_setg(errp, "Guest is waiting for an incoming migration");
1701         return false;
1702     }
1703 
1704     if (runstate_check(RUN_STATE_POSTMIGRATE)) {
1705         error_setg(errp, "Can't migrate the vm that was paused due to "
1706                    "previous migration");
1707         return false;
1708     }
1709 
1710     if (migration_is_blocked(errp)) {
1711         return false;
1712     }
1713 
1714     if (blk || blk_inc) {
1715         if (migrate_colo()) {
1716             error_setg(errp, "No disk migration is required in COLO mode");
1717             return false;
1718         }
1719         if (migrate_block() || migrate_block_incremental()) {
1720             error_setg(errp, "Command options are incompatible with "
1721                        "current migration capabilities");
1722             return false;
1723         }
1724         if (!migrate_cap_set(MIGRATION_CAPABILITY_BLOCK, true, &local_err)) {
1725             error_propagate(errp, local_err);
1726             return false;
1727         }
1728         s->must_remove_block_options = true;
1729     }
1730 
1731     if (blk_inc) {
1732         migrate_set_block_incremental(true);
1733     }
1734 
1735     if (migrate_init(s, errp)) {
1736         return false;
1737     }
1738 
1739     return true;
1740 }
1741 
1742 void qmp_migrate(const char *uri, bool has_blk, bool blk,
1743                  bool has_inc, bool inc, bool has_detach, bool detach,
1744                  bool has_resume, bool resume, Error **errp)
1745 {
1746     bool resume_requested;
1747     Error *local_err = NULL;
1748     MigrationState *s = migrate_get_current();
1749     const char *p = NULL;
1750 
1751     /* URI is not suitable for migration? */
1752     if (!migration_channels_and_uri_compatible(uri, errp)) {
1753         return;
1754     }
1755 
1756     resume_requested = has_resume && resume;
1757     if (!migrate_prepare(s, has_blk && blk, has_inc && inc,
1758                          resume_requested, errp)) {
1759         /* Error detected, put into errp */
1760         return;
1761     }
1762 
1763     if (!resume_requested) {
1764         if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) {
1765             return;
1766         }
1767     }
1768 
1769     if (strstart(uri, "tcp:", &p) ||
1770         strstart(uri, "unix:", NULL) ||
1771         strstart(uri, "vsock:", NULL)) {
1772         socket_start_outgoing_migration(s, p ? p : uri, &local_err);
1773 #ifdef CONFIG_RDMA
1774     } else if (strstart(uri, "rdma:", &p)) {
1775         rdma_start_outgoing_migration(s, p, &local_err);
1776 #endif
1777     } else if (strstart(uri, "exec:", &p)) {
1778         exec_start_outgoing_migration(s, p, &local_err);
1779     } else if (strstart(uri, "fd:", &p)) {
1780         fd_start_outgoing_migration(s, p, &local_err);
1781     } else if (strstart(uri, "file:", &p)) {
1782         file_start_outgoing_migration(s, p, &local_err);
1783     } else {
1784         error_setg(&local_err, QERR_INVALID_PARAMETER_VALUE, "uri",
1785                    "a valid migration protocol");
1786         migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1787                           MIGRATION_STATUS_FAILED);
1788         block_cleanup_parameters();
1789     }
1790 
1791     if (local_err) {
1792         if (!resume_requested) {
1793             yank_unregister_instance(MIGRATION_YANK_INSTANCE);
1794         }
1795         migrate_fd_error(s, local_err);
1796         error_propagate(errp, local_err);
1797         return;
1798     }
1799 }
1800 
1801 void qmp_migrate_cancel(Error **errp)
1802 {
1803     migration_cancel(NULL);
1804 }
1805 
1806 void qmp_migrate_continue(MigrationStatus state, Error **errp)
1807 {
1808     MigrationState *s = migrate_get_current();
1809     if (s->state != state) {
1810         error_setg(errp,  "Migration not in expected state: %s",
1811                    MigrationStatus_str(s->state));
1812         return;
1813     }
1814     qemu_sem_post(&s->pause_sem);
1815 }
1816 
1817 /* migration thread support */
1818 /*
1819  * Something bad happened to the RP stream, mark an error
1820  * The caller shall print or trace something to indicate why
1821  */
1822 static void mark_source_rp_bad(MigrationState *s)
1823 {
1824     s->rp_state.error = true;
1825 }
1826 
1827 void migration_rp_wait(MigrationState *s)
1828 {
1829     qemu_sem_wait(&s->rp_state.rp_sem);
1830 }
1831 
1832 void migration_rp_kick(MigrationState *s)
1833 {
1834     qemu_sem_post(&s->rp_state.rp_sem);
1835 }
1836 
1837 static struct rp_cmd_args {
1838     ssize_t     len; /* -1 = variable */
1839     const char *name;
1840 } rp_cmd_args[] = {
1841     [MIG_RP_MSG_INVALID]        = { .len = -1, .name = "INVALID" },
1842     [MIG_RP_MSG_SHUT]           = { .len =  4, .name = "SHUT" },
1843     [MIG_RP_MSG_PONG]           = { .len =  4, .name = "PONG" },
1844     [MIG_RP_MSG_REQ_PAGES]      = { .len = 12, .name = "REQ_PAGES" },
1845     [MIG_RP_MSG_REQ_PAGES_ID]   = { .len = -1, .name = "REQ_PAGES_ID" },
1846     [MIG_RP_MSG_RECV_BITMAP]    = { .len = -1, .name = "RECV_BITMAP" },
1847     [MIG_RP_MSG_RESUME_ACK]     = { .len =  4, .name = "RESUME_ACK" },
1848     [MIG_RP_MSG_SWITCHOVER_ACK] = { .len =  0, .name = "SWITCHOVER_ACK" },
1849     [MIG_RP_MSG_MAX]            = { .len = -1, .name = "MAX" },
1850 };
1851 
1852 /*
1853  * Process a request for pages received on the return path,
1854  * We're allowed to send more than requested (e.g. to round to our page size)
1855  * and we don't need to send pages that have already been sent.
1856  */
1857 static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1858                                        ram_addr_t start, size_t len)
1859 {
1860     long our_host_ps = qemu_real_host_page_size();
1861 
1862     trace_migrate_handle_rp_req_pages(rbname, start, len);
1863 
1864     /*
1865      * Since we currently insist on matching page sizes, just sanity check
1866      * we're being asked for whole host pages.
1867      */
1868     if (!QEMU_IS_ALIGNED(start, our_host_ps) ||
1869         !QEMU_IS_ALIGNED(len, our_host_ps)) {
1870         error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1871                      " len: %zd", __func__, start, len);
1872         mark_source_rp_bad(ms);
1873         return;
1874     }
1875 
1876     if (ram_save_queue_pages(rbname, start, len)) {
1877         mark_source_rp_bad(ms);
1878     }
1879 }
1880 
1881 static int migrate_handle_rp_recv_bitmap(MigrationState *s, char *block_name)
1882 {
1883     RAMBlock *block = qemu_ram_block_by_name(block_name);
1884 
1885     if (!block) {
1886         error_report("%s: invalid block name '%s'", __func__, block_name);
1887         return -EINVAL;
1888     }
1889 
1890     /* Fetch the received bitmap and refresh the dirty bitmap */
1891     return ram_dirty_bitmap_reload(s, block);
1892 }
1893 
1894 static int migrate_handle_rp_resume_ack(MigrationState *s, uint32_t value)
1895 {
1896     trace_source_return_path_thread_resume_ack(value);
1897 
1898     if (value != MIGRATION_RESUME_ACK_VALUE) {
1899         error_report("%s: illegal resume_ack value %"PRIu32,
1900                      __func__, value);
1901         return -1;
1902     }
1903 
1904     /* Now both sides are active. */
1905     migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
1906                       MIGRATION_STATUS_POSTCOPY_ACTIVE);
1907 
1908     /* Notify send thread that time to continue send pages */
1909     migration_rp_kick(s);
1910 
1911     return 0;
1912 }
1913 
1914 /*
1915  * Release ms->rp_state.from_dst_file (and postcopy_qemufile_src if
1916  * existed) in a safe way.
1917  */
1918 static void migration_release_dst_files(MigrationState *ms)
1919 {
1920     QEMUFile *file;
1921 
1922     WITH_QEMU_LOCK_GUARD(&ms->qemu_file_lock) {
1923         /*
1924          * Reset the from_dst_file pointer first before releasing it, as we
1925          * can't block within lock section
1926          */
1927         file = ms->rp_state.from_dst_file;
1928         ms->rp_state.from_dst_file = NULL;
1929     }
1930 
1931     /*
1932      * Do the same to postcopy fast path socket too if there is.  No
1933      * locking needed because this qemufile should only be managed by
1934      * return path thread.
1935      */
1936     if (ms->postcopy_qemufile_src) {
1937         migration_ioc_unregister_yank_from_file(ms->postcopy_qemufile_src);
1938         qemu_file_shutdown(ms->postcopy_qemufile_src);
1939         qemu_fclose(ms->postcopy_qemufile_src);
1940         ms->postcopy_qemufile_src = NULL;
1941     }
1942 
1943     qemu_fclose(file);
1944 }
1945 
1946 /*
1947  * Handles messages sent on the return path towards the source VM
1948  *
1949  */
1950 static void *source_return_path_thread(void *opaque)
1951 {
1952     MigrationState *ms = opaque;
1953     QEMUFile *rp = ms->rp_state.from_dst_file;
1954     uint16_t header_len, header_type;
1955     uint8_t buf[512];
1956     uint32_t tmp32, sibling_error;
1957     ram_addr_t start = 0; /* =0 to silence warning */
1958     size_t  len = 0, expected_len;
1959     int res;
1960 
1961     trace_source_return_path_thread_entry();
1962     rcu_register_thread();
1963 
1964     while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1965            migration_is_setup_or_active(ms->state)) {
1966         trace_source_return_path_thread_loop_top();
1967         header_type = qemu_get_be16(rp);
1968         header_len = qemu_get_be16(rp);
1969 
1970         if (qemu_file_get_error(rp)) {
1971             mark_source_rp_bad(ms);
1972             goto out;
1973         }
1974 
1975         if (header_type >= MIG_RP_MSG_MAX ||
1976             header_type == MIG_RP_MSG_INVALID) {
1977             error_report("RP: Received invalid message 0x%04x length 0x%04x",
1978                          header_type, header_len);
1979             mark_source_rp_bad(ms);
1980             goto out;
1981         }
1982 
1983         if ((rp_cmd_args[header_type].len != -1 &&
1984             header_len != rp_cmd_args[header_type].len) ||
1985             header_len > sizeof(buf)) {
1986             error_report("RP: Received '%s' message (0x%04x) with"
1987                          "incorrect length %d expecting %zu",
1988                          rp_cmd_args[header_type].name, header_type, header_len,
1989                          (size_t)rp_cmd_args[header_type].len);
1990             mark_source_rp_bad(ms);
1991             goto out;
1992         }
1993 
1994         /* We know we've got a valid header by this point */
1995         res = qemu_get_buffer(rp, buf, header_len);
1996         if (res != header_len) {
1997             error_report("RP: Failed reading data for message 0x%04x"
1998                          " read %d expected %d",
1999                          header_type, res, header_len);
2000             mark_source_rp_bad(ms);
2001             goto out;
2002         }
2003 
2004         /* OK, we have the message and the data */
2005         switch (header_type) {
2006         case MIG_RP_MSG_SHUT:
2007             sibling_error = ldl_be_p(buf);
2008             trace_source_return_path_thread_shut(sibling_error);
2009             if (sibling_error) {
2010                 error_report("RP: Sibling indicated error %d", sibling_error);
2011                 mark_source_rp_bad(ms);
2012             }
2013             /*
2014              * We'll let the main thread deal with closing the RP
2015              * we could do a shutdown(2) on it, but we're the only user
2016              * anyway, so there's nothing gained.
2017              */
2018             goto out;
2019 
2020         case MIG_RP_MSG_PONG:
2021             tmp32 = ldl_be_p(buf);
2022             trace_source_return_path_thread_pong(tmp32);
2023             qemu_sem_post(&ms->rp_state.rp_pong_acks);
2024             break;
2025 
2026         case MIG_RP_MSG_REQ_PAGES:
2027             start = ldq_be_p(buf);
2028             len = ldl_be_p(buf + 8);
2029             migrate_handle_rp_req_pages(ms, NULL, start, len);
2030             break;
2031 
2032         case MIG_RP_MSG_REQ_PAGES_ID:
2033             expected_len = 12 + 1; /* header + termination */
2034 
2035             if (header_len >= expected_len) {
2036                 start = ldq_be_p(buf);
2037                 len = ldl_be_p(buf + 8);
2038                 /* Now we expect an idstr */
2039                 tmp32 = buf[12]; /* Length of the following idstr */
2040                 buf[13 + tmp32] = '\0';
2041                 expected_len += tmp32;
2042             }
2043             if (header_len != expected_len) {
2044                 error_report("RP: Req_Page_id with length %d expecting %zd",
2045                              header_len, expected_len);
2046                 mark_source_rp_bad(ms);
2047                 goto out;
2048             }
2049             migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
2050             break;
2051 
2052         case MIG_RP_MSG_RECV_BITMAP:
2053             if (header_len < 1) {
2054                 error_report("%s: missing block name", __func__);
2055                 mark_source_rp_bad(ms);
2056                 goto out;
2057             }
2058             /* Format: len (1B) + idstr (<255B). This ends the idstr. */
2059             buf[buf[0] + 1] = '\0';
2060             if (migrate_handle_rp_recv_bitmap(ms, (char *)(buf + 1))) {
2061                 mark_source_rp_bad(ms);
2062                 goto out;
2063             }
2064             break;
2065 
2066         case MIG_RP_MSG_RESUME_ACK:
2067             tmp32 = ldl_be_p(buf);
2068             if (migrate_handle_rp_resume_ack(ms, tmp32)) {
2069                 mark_source_rp_bad(ms);
2070                 goto out;
2071             }
2072             break;
2073 
2074         case MIG_RP_MSG_SWITCHOVER_ACK:
2075             ms->switchover_acked = true;
2076             trace_source_return_path_thread_switchover_acked();
2077             break;
2078 
2079         default:
2080             break;
2081         }
2082     }
2083 
2084 out:
2085     if (qemu_file_get_error(rp)) {
2086         trace_source_return_path_thread_bad_end();
2087         mark_source_rp_bad(ms);
2088     }
2089 
2090     trace_source_return_path_thread_end();
2091     rcu_unregister_thread();
2092     return NULL;
2093 }
2094 
2095 static int open_return_path_on_source(MigrationState *ms)
2096 {
2097     ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
2098     if (!ms->rp_state.from_dst_file) {
2099         return -1;
2100     }
2101 
2102     trace_open_return_path_on_source();
2103 
2104     qemu_thread_create(&ms->rp_state.rp_thread, "return path",
2105                        source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
2106     ms->rp_state.rp_thread_created = true;
2107 
2108     trace_open_return_path_on_source_continue();
2109 
2110     return 0;
2111 }
2112 
2113 static int close_return_path_on_source(MigrationState *ms)
2114 {
2115     int ret;
2116 
2117     if (!ms->rp_state.rp_thread_created) {
2118         return 0;
2119     }
2120 
2121     trace_migration_return_path_end_before();
2122 
2123     /*
2124      * If this is a normal exit then the destination will send a SHUT
2125      * and the rp_thread will exit, however if there's an error we
2126      * need to cause it to exit. shutdown(2), if we have it, will
2127      * cause it to unblock if it's stuck waiting for the destination.
2128      */
2129     WITH_QEMU_LOCK_GUARD(&ms->qemu_file_lock) {
2130         if (ms->to_dst_file && ms->rp_state.from_dst_file &&
2131             qemu_file_get_error(ms->to_dst_file)) {
2132             qemu_file_shutdown(ms->rp_state.from_dst_file);
2133         }
2134     }
2135 
2136     trace_await_return_path_close_on_source_joining();
2137     qemu_thread_join(&ms->rp_state.rp_thread);
2138     ms->rp_state.rp_thread_created = false;
2139     trace_await_return_path_close_on_source_close();
2140 
2141     ret = ms->rp_state.error;
2142     ms->rp_state.error = false;
2143 
2144     migration_release_dst_files(ms);
2145 
2146     trace_migration_return_path_end_after(ret);
2147     return ret;
2148 }
2149 
2150 static inline void
2151 migration_wait_main_channel(MigrationState *ms)
2152 {
2153     /* Wait until one PONG message received */
2154     qemu_sem_wait(&ms->rp_state.rp_pong_acks);
2155 }
2156 
2157 /*
2158  * Switch from normal iteration to postcopy
2159  * Returns non-0 on error
2160  */
2161 static int postcopy_start(MigrationState *ms, Error **errp)
2162 {
2163     int ret;
2164     QIOChannelBuffer *bioc;
2165     QEMUFile *fb;
2166     uint64_t bandwidth = migrate_max_postcopy_bandwidth();
2167     bool restart_block = false;
2168     int cur_state = MIGRATION_STATUS_ACTIVE;
2169 
2170     if (migrate_postcopy_preempt()) {
2171         migration_wait_main_channel(ms);
2172         if (postcopy_preempt_establish_channel(ms)) {
2173             migrate_set_state(&ms->state, ms->state, MIGRATION_STATUS_FAILED);
2174             return -1;
2175         }
2176     }
2177 
2178     if (!migrate_pause_before_switchover()) {
2179         migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
2180                           MIGRATION_STATUS_POSTCOPY_ACTIVE);
2181     }
2182 
2183     trace_postcopy_start();
2184     qemu_mutex_lock_iothread();
2185     trace_postcopy_start_set_run();
2186 
2187     migration_downtime_start(ms);
2188 
2189     qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
2190     global_state_store();
2191     ret = migration_stop_vm(RUN_STATE_FINISH_MIGRATE);
2192     if (ret < 0) {
2193         goto fail;
2194     }
2195 
2196     ret = migration_maybe_pause(ms, &cur_state,
2197                                 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2198     if (ret < 0) {
2199         goto fail;
2200     }
2201 
2202     ret = bdrv_inactivate_all();
2203     if (ret < 0) {
2204         goto fail;
2205     }
2206     restart_block = true;
2207 
2208     /*
2209      * Cause any non-postcopiable, but iterative devices to
2210      * send out their final data.
2211      */
2212     qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
2213 
2214     /*
2215      * in Finish migrate and with the io-lock held everything should
2216      * be quiet, but we've potentially still got dirty pages and we
2217      * need to tell the destination to throw any pages it's already received
2218      * that are dirty
2219      */
2220     if (migrate_postcopy_ram()) {
2221         ram_postcopy_send_discard_bitmap(ms);
2222     }
2223 
2224     /*
2225      * send rest of state - note things that are doing postcopy
2226      * will notice we're in POSTCOPY_ACTIVE and not actually
2227      * wrap their state up here
2228      */
2229     migration_rate_set(bandwidth);
2230     if (migrate_postcopy_ram()) {
2231         /* Ping just for debugging, helps line traces up */
2232         qemu_savevm_send_ping(ms->to_dst_file, 2);
2233     }
2234 
2235     /*
2236      * While loading the device state we may trigger page transfer
2237      * requests and the fd must be free to process those, and thus
2238      * the destination must read the whole device state off the fd before
2239      * it starts processing it.  Unfortunately the ad-hoc migration format
2240      * doesn't allow the destination to know the size to read without fully
2241      * parsing it through each devices load-state code (especially the open
2242      * coded devices that use get/put).
2243      * So we wrap the device state up in a package with a length at the start;
2244      * to do this we use a qemu_buf to hold the whole of the device state.
2245      */
2246     bioc = qio_channel_buffer_new(4096);
2247     qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
2248     fb = qemu_file_new_output(QIO_CHANNEL(bioc));
2249     object_unref(OBJECT(bioc));
2250 
2251     /*
2252      * Make sure the receiver can get incoming pages before we send the rest
2253      * of the state
2254      */
2255     qemu_savevm_send_postcopy_listen(fb);
2256 
2257     qemu_savevm_state_complete_precopy(fb, false, false);
2258     if (migrate_postcopy_ram()) {
2259         qemu_savevm_send_ping(fb, 3);
2260     }
2261 
2262     qemu_savevm_send_postcopy_run(fb);
2263 
2264     /* <><> end of stuff going into the package */
2265 
2266     /* Last point of recovery; as soon as we send the package the destination
2267      * can open devices and potentially start running.
2268      * Lets just check again we've not got any errors.
2269      */
2270     ret = qemu_file_get_error(ms->to_dst_file);
2271     if (ret) {
2272         error_setg(errp, "postcopy_start: Migration stream errored (pre package)");
2273         goto fail_closefb;
2274     }
2275 
2276     restart_block = false;
2277 
2278     /* Now send that blob */
2279     if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
2280         goto fail_closefb;
2281     }
2282     qemu_fclose(fb);
2283 
2284     /* Send a notify to give a chance for anything that needs to happen
2285      * at the transition to postcopy and after the device state; in particular
2286      * spice needs to trigger a transition now
2287      */
2288     ms->postcopy_after_devices = true;
2289     migration_call_notifiers(ms);
2290 
2291     migration_downtime_end(ms);
2292 
2293     qemu_mutex_unlock_iothread();
2294 
2295     if (migrate_postcopy_ram()) {
2296         /*
2297          * Although this ping is just for debug, it could potentially be
2298          * used for getting a better measurement of downtime at the source.
2299          */
2300         qemu_savevm_send_ping(ms->to_dst_file, 4);
2301     }
2302 
2303     if (migrate_release_ram()) {
2304         ram_postcopy_migrated_memory_release(ms);
2305     }
2306 
2307     ret = qemu_file_get_error(ms->to_dst_file);
2308     if (ret) {
2309         error_setg(errp, "postcopy_start: Migration stream errored");
2310         migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
2311                               MIGRATION_STATUS_FAILED);
2312     }
2313 
2314     trace_postcopy_preempt_enabled(migrate_postcopy_preempt());
2315 
2316     return ret;
2317 
2318 fail_closefb:
2319     qemu_fclose(fb);
2320 fail:
2321     migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
2322                           MIGRATION_STATUS_FAILED);
2323     if (restart_block) {
2324         /* A failure happened early enough that we know the destination hasn't
2325          * accessed block devices, so we're safe to recover.
2326          */
2327         Error *local_err = NULL;
2328 
2329         bdrv_activate_all(&local_err);
2330         if (local_err) {
2331             error_report_err(local_err);
2332         }
2333     }
2334     qemu_mutex_unlock_iothread();
2335     return -1;
2336 }
2337 
2338 /**
2339  * migration_maybe_pause: Pause if required to by
2340  * migrate_pause_before_switchover called with the iothread locked
2341  * Returns: 0 on success
2342  */
2343 static int migration_maybe_pause(MigrationState *s,
2344                                  int *current_active_state,
2345                                  int new_state)
2346 {
2347     if (!migrate_pause_before_switchover()) {
2348         return 0;
2349     }
2350 
2351     /* Since leaving this state is not atomic with posting the semaphore
2352      * it's possible that someone could have issued multiple migrate_continue
2353      * and the semaphore is incorrectly positive at this point;
2354      * the docs say it's undefined to reinit a semaphore that's already
2355      * init'd, so use timedwait to eat up any existing posts.
2356      */
2357     while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) {
2358         /* This block intentionally left blank */
2359     }
2360 
2361     /*
2362      * If the migration is cancelled when it is in the completion phase,
2363      * the migration state is set to MIGRATION_STATUS_CANCELLING.
2364      * So we don't need to wait a semaphore, otherwise we would always
2365      * wait for the 'pause_sem' semaphore.
2366      */
2367     if (s->state != MIGRATION_STATUS_CANCELLING) {
2368         qemu_mutex_unlock_iothread();
2369         migrate_set_state(&s->state, *current_active_state,
2370                           MIGRATION_STATUS_PRE_SWITCHOVER);
2371         qemu_sem_wait(&s->pause_sem);
2372         migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER,
2373                           new_state);
2374         *current_active_state = new_state;
2375         qemu_mutex_lock_iothread();
2376     }
2377 
2378     return s->state == new_state ? 0 : -EINVAL;
2379 }
2380 
2381 static int migration_completion_precopy(MigrationState *s,
2382                                         int *current_active_state)
2383 {
2384     int ret;
2385 
2386     qemu_mutex_lock_iothread();
2387     migration_downtime_start(s);
2388     qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
2389 
2390     s->vm_old_state = runstate_get();
2391     global_state_store();
2392 
2393     ret = migration_stop_vm(RUN_STATE_FINISH_MIGRATE);
2394     trace_migration_completion_vm_stop(ret);
2395     if (ret < 0) {
2396         goto out_unlock;
2397     }
2398 
2399     ret = migration_maybe_pause(s, current_active_state,
2400                                 MIGRATION_STATUS_DEVICE);
2401     if (ret < 0) {
2402         goto out_unlock;
2403     }
2404 
2405     /*
2406      * Inactivate disks except in COLO, and track that we have done so in order
2407      * to remember to reactivate them if migration fails or is cancelled.
2408      */
2409     s->block_inactive = !migrate_colo();
2410     migration_rate_set(RATE_LIMIT_DISABLED);
2411     ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
2412                                              s->block_inactive);
2413 out_unlock:
2414     qemu_mutex_unlock_iothread();
2415     return ret;
2416 }
2417 
2418 static void migration_completion_postcopy(MigrationState *s)
2419 {
2420     trace_migration_completion_postcopy_end();
2421 
2422     qemu_mutex_lock_iothread();
2423     qemu_savevm_state_complete_postcopy(s->to_dst_file);
2424     qemu_mutex_unlock_iothread();
2425 
2426     /*
2427      * Shutdown the postcopy fast path thread.  This is only needed when dest
2428      * QEMU binary is old (7.1/7.2).  QEMU 8.0+ doesn't need this.
2429      */
2430     if (migrate_postcopy_preempt() && s->preempt_pre_7_2) {
2431         postcopy_preempt_shutdown_file(s);
2432     }
2433 
2434     trace_migration_completion_postcopy_end_after_complete();
2435 }
2436 
2437 static void migration_completion_failed(MigrationState *s,
2438                                         int current_active_state)
2439 {
2440     if (s->block_inactive && (s->state == MIGRATION_STATUS_ACTIVE ||
2441                               s->state == MIGRATION_STATUS_DEVICE)) {
2442         /*
2443          * If not doing postcopy, vm_start() will be called: let's
2444          * regain control on images.
2445          */
2446         Error *local_err = NULL;
2447 
2448         qemu_mutex_lock_iothread();
2449         bdrv_activate_all(&local_err);
2450         if (local_err) {
2451             error_report_err(local_err);
2452         } else {
2453             s->block_inactive = false;
2454         }
2455         qemu_mutex_unlock_iothread();
2456     }
2457 
2458     migrate_set_state(&s->state, current_active_state,
2459                       MIGRATION_STATUS_FAILED);
2460 }
2461 
2462 /**
2463  * migration_completion: Used by migration_thread when there's not much left.
2464  *   The caller 'breaks' the loop when this returns.
2465  *
2466  * @s: Current migration state
2467  */
2468 static void migration_completion(MigrationState *s)
2469 {
2470     int ret = 0;
2471     int current_active_state = s->state;
2472 
2473     if (s->state == MIGRATION_STATUS_ACTIVE) {
2474         ret = migration_completion_precopy(s, &current_active_state);
2475     } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2476         migration_completion_postcopy(s);
2477     } else {
2478         ret = -1;
2479     }
2480 
2481     if (ret < 0) {
2482         goto fail;
2483     }
2484 
2485     if (close_return_path_on_source(s)) {
2486         goto fail;
2487     }
2488 
2489     if (qemu_file_get_error(s->to_dst_file)) {
2490         trace_migration_completion_file_err();
2491         goto fail;
2492     }
2493 
2494     if (migrate_colo() && s->state == MIGRATION_STATUS_ACTIVE) {
2495         /* COLO does not support postcopy */
2496         migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE,
2497                           MIGRATION_STATUS_COLO);
2498     } else {
2499         migrate_set_state(&s->state, current_active_state,
2500                           MIGRATION_STATUS_COMPLETED);
2501     }
2502 
2503     return;
2504 
2505 fail:
2506     migration_completion_failed(s, current_active_state);
2507 }
2508 
2509 /**
2510  * bg_migration_completion: Used by bg_migration_thread when after all the
2511  *   RAM has been saved. The caller 'breaks' the loop when this returns.
2512  *
2513  * @s: Current migration state
2514  */
2515 static void bg_migration_completion(MigrationState *s)
2516 {
2517     int current_active_state = s->state;
2518 
2519     if (s->state == MIGRATION_STATUS_ACTIVE) {
2520         /*
2521          * By this moment we have RAM content saved into the migration stream.
2522          * The next step is to flush the non-RAM content (device state)
2523          * right after the ram content. The device state has been stored into
2524          * the temporary buffer before RAM saving started.
2525          */
2526         qemu_put_buffer(s->to_dst_file, s->bioc->data, s->bioc->usage);
2527         qemu_fflush(s->to_dst_file);
2528     } else if (s->state == MIGRATION_STATUS_CANCELLING) {
2529         goto fail;
2530     }
2531 
2532     if (qemu_file_get_error(s->to_dst_file)) {
2533         trace_migration_completion_file_err();
2534         goto fail;
2535     }
2536 
2537     migrate_set_state(&s->state, current_active_state,
2538                       MIGRATION_STATUS_COMPLETED);
2539     return;
2540 
2541 fail:
2542     migrate_set_state(&s->state, current_active_state,
2543                       MIGRATION_STATUS_FAILED);
2544 }
2545 
2546 typedef enum MigThrError {
2547     /* No error detected */
2548     MIG_THR_ERR_NONE = 0,
2549     /* Detected error, but resumed successfully */
2550     MIG_THR_ERR_RECOVERED = 1,
2551     /* Detected fatal error, need to exit */
2552     MIG_THR_ERR_FATAL = 2,
2553 } MigThrError;
2554 
2555 static int postcopy_resume_handshake(MigrationState *s)
2556 {
2557     qemu_savevm_send_postcopy_resume(s->to_dst_file);
2558 
2559     while (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
2560         migration_rp_wait(s);
2561     }
2562 
2563     if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2564         return 0;
2565     }
2566 
2567     return -1;
2568 }
2569 
2570 /* Return zero if success, or <0 for error */
2571 static int postcopy_do_resume(MigrationState *s)
2572 {
2573     int ret;
2574 
2575     /*
2576      * Call all the resume_prepare() hooks, so that modules can be
2577      * ready for the migration resume.
2578      */
2579     ret = qemu_savevm_state_resume_prepare(s);
2580     if (ret) {
2581         error_report("%s: resume_prepare() failure detected: %d",
2582                      __func__, ret);
2583         return ret;
2584     }
2585 
2586     /*
2587      * If preempt is enabled, re-establish the preempt channel.  Note that
2588      * we do it after resume prepare to make sure the main channel will be
2589      * created before the preempt channel.  E.g. with weak network, the
2590      * dest QEMU may get messed up with the preempt and main channels on
2591      * the order of connection setup.  This guarantees the correct order.
2592      */
2593     ret = postcopy_preempt_establish_channel(s);
2594     if (ret) {
2595         error_report("%s: postcopy_preempt_establish_channel(): %d",
2596                      __func__, ret);
2597         return ret;
2598     }
2599 
2600     /*
2601      * Last handshake with destination on the resume (destination will
2602      * switch to postcopy-active afterwards)
2603      */
2604     ret = postcopy_resume_handshake(s);
2605     if (ret) {
2606         error_report("%s: handshake failed: %d", __func__, ret);
2607         return ret;
2608     }
2609 
2610     return 0;
2611 }
2612 
2613 /*
2614  * We don't return until we are in a safe state to continue current
2615  * postcopy migration.  Returns MIG_THR_ERR_RECOVERED if recovered, or
2616  * MIG_THR_ERR_FATAL if unrecovery failure happened.
2617  */
2618 static MigThrError postcopy_pause(MigrationState *s)
2619 {
2620     assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
2621 
2622     while (true) {
2623         QEMUFile *file;
2624 
2625         /*
2626          * Current channel is possibly broken. Release it.  Note that this is
2627          * guaranteed even without lock because to_dst_file should only be
2628          * modified by the migration thread.  That also guarantees that the
2629          * unregister of yank is safe too without the lock.  It should be safe
2630          * even to be within the qemu_file_lock, but we didn't do that to avoid
2631          * taking more mutex (yank_lock) within qemu_file_lock.  TL;DR: we make
2632          * the qemu_file_lock critical section as small as possible.
2633          */
2634         assert(s->to_dst_file);
2635         migration_ioc_unregister_yank_from_file(s->to_dst_file);
2636         qemu_mutex_lock(&s->qemu_file_lock);
2637         file = s->to_dst_file;
2638         s->to_dst_file = NULL;
2639         qemu_mutex_unlock(&s->qemu_file_lock);
2640 
2641         qemu_file_shutdown(file);
2642         qemu_fclose(file);
2643 
2644         /*
2645          * We're already pausing, so ignore any errors on the return
2646          * path and just wait for the thread to finish. It will be
2647          * re-created when we resume.
2648          */
2649         close_return_path_on_source(s);
2650 
2651         migrate_set_state(&s->state, s->state,
2652                           MIGRATION_STATUS_POSTCOPY_PAUSED);
2653 
2654         error_report("Detected IO failure for postcopy. "
2655                      "Migration paused.");
2656 
2657         /*
2658          * We wait until things fixed up. Then someone will setup the
2659          * status back for us.
2660          */
2661         while (s->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
2662             qemu_sem_wait(&s->postcopy_pause_sem);
2663         }
2664 
2665         if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
2666             /* Woken up by a recover procedure. Give it a shot */
2667 
2668             /* Do the resume logic */
2669             if (postcopy_do_resume(s) == 0) {
2670                 /* Let's continue! */
2671                 trace_postcopy_pause_continued();
2672                 return MIG_THR_ERR_RECOVERED;
2673             } else {
2674                 /*
2675                  * Something wrong happened during the recovery, let's
2676                  * pause again. Pause is always better than throwing
2677                  * data away.
2678                  */
2679                 continue;
2680             }
2681         } else {
2682             /* This is not right... Time to quit. */
2683             return MIG_THR_ERR_FATAL;
2684         }
2685     }
2686 }
2687 
2688 static MigThrError migration_detect_error(MigrationState *s)
2689 {
2690     int ret;
2691     int state = s->state;
2692     Error *local_error = NULL;
2693 
2694     if (state == MIGRATION_STATUS_CANCELLING ||
2695         state == MIGRATION_STATUS_CANCELLED) {
2696         /* End the migration, but don't set the state to failed */
2697         return MIG_THR_ERR_FATAL;
2698     }
2699 
2700     /*
2701      * Try to detect any file errors.  Note that postcopy_qemufile_src will
2702      * be NULL when postcopy preempt is not enabled.
2703      */
2704     ret = qemu_file_get_error_obj_any(s->to_dst_file,
2705                                       s->postcopy_qemufile_src,
2706                                       &local_error);
2707     if (!ret) {
2708         /* Everything is fine */
2709         assert(!local_error);
2710         return MIG_THR_ERR_NONE;
2711     }
2712 
2713     if (local_error) {
2714         migrate_set_error(s, local_error);
2715         error_free(local_error);
2716     }
2717 
2718     if (state == MIGRATION_STATUS_POSTCOPY_ACTIVE && ret) {
2719         /*
2720          * For postcopy, we allow the network to be down for a
2721          * while. After that, it can be continued by a
2722          * recovery phase.
2723          */
2724         return postcopy_pause(s);
2725     } else {
2726         /*
2727          * For precopy (or postcopy with error outside IO), we fail
2728          * with no time.
2729          */
2730         migrate_set_state(&s->state, state, MIGRATION_STATUS_FAILED);
2731         trace_migration_thread_file_err();
2732 
2733         /* Time to stop the migration, now. */
2734         return MIG_THR_ERR_FATAL;
2735     }
2736 }
2737 
2738 static void migration_calculate_complete(MigrationState *s)
2739 {
2740     uint64_t bytes = migration_transferred_bytes();
2741     int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2742     int64_t transfer_time;
2743 
2744     migration_downtime_end(s);
2745     s->total_time = end_time - s->start_time;
2746     transfer_time = s->total_time - s->setup_time;
2747     if (transfer_time) {
2748         s->mbps = ((double) bytes * 8.0) / transfer_time / 1000;
2749     }
2750 }
2751 
2752 static void update_iteration_initial_status(MigrationState *s)
2753 {
2754     /*
2755      * Update these three fields at the same time to avoid mismatch info lead
2756      * wrong speed calculation.
2757      */
2758     s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2759     s->iteration_initial_bytes = migration_transferred_bytes();
2760     s->iteration_initial_pages = ram_get_total_transferred_pages();
2761 }
2762 
2763 static void migration_update_counters(MigrationState *s,
2764                                       int64_t current_time)
2765 {
2766     uint64_t transferred, transferred_pages, time_spent;
2767     uint64_t current_bytes; /* bytes transferred since the beginning */
2768     uint64_t switchover_bw;
2769     /* Expected bandwidth when switching over to destination QEMU */
2770     double expected_bw_per_ms;
2771     double bandwidth;
2772 
2773     if (current_time < s->iteration_start_time + BUFFER_DELAY) {
2774         return;
2775     }
2776 
2777     switchover_bw = migrate_avail_switchover_bandwidth();
2778     current_bytes = migration_transferred_bytes();
2779     transferred = current_bytes - s->iteration_initial_bytes;
2780     time_spent = current_time - s->iteration_start_time;
2781     bandwidth = (double)transferred / time_spent;
2782 
2783     if (switchover_bw) {
2784         /*
2785          * If the user specified a switchover bandwidth, let's trust the
2786          * user so that can be more accurate than what we estimated.
2787          */
2788         expected_bw_per_ms = switchover_bw / 1000;
2789     } else {
2790         /* If the user doesn't specify bandwidth, we use the estimated */
2791         expected_bw_per_ms = bandwidth;
2792     }
2793 
2794     s->threshold_size = expected_bw_per_ms * migrate_downtime_limit();
2795 
2796     s->mbps = (((double) transferred * 8.0) /
2797                ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
2798 
2799     transferred_pages = ram_get_total_transferred_pages() -
2800                             s->iteration_initial_pages;
2801     s->pages_per_second = (double) transferred_pages /
2802                              (((double) time_spent / 1000.0));
2803 
2804     /*
2805      * if we haven't sent anything, we don't want to
2806      * recalculate. 10000 is a small enough number for our purposes
2807      */
2808     if (stat64_get(&mig_stats.dirty_pages_rate) &&
2809         transferred > 10000) {
2810         s->expected_downtime =
2811             stat64_get(&mig_stats.dirty_bytes_last_sync) / expected_bw_per_ms;
2812     }
2813 
2814     migration_rate_reset();
2815 
2816     update_iteration_initial_status(s);
2817 
2818     trace_migrate_transferred(transferred, time_spent,
2819                               /* Both in unit bytes/ms */
2820                               bandwidth, switchover_bw / 1000,
2821                               s->threshold_size);
2822 }
2823 
2824 static bool migration_can_switchover(MigrationState *s)
2825 {
2826     if (!migrate_switchover_ack()) {
2827         return true;
2828     }
2829 
2830     /* No reason to wait for switchover ACK if VM is stopped */
2831     if (!runstate_is_running()) {
2832         return true;
2833     }
2834 
2835     return s->switchover_acked;
2836 }
2837 
2838 /* Migration thread iteration status */
2839 typedef enum {
2840     MIG_ITERATE_RESUME,         /* Resume current iteration */
2841     MIG_ITERATE_SKIP,           /* Skip current iteration */
2842     MIG_ITERATE_BREAK,          /* Break the loop */
2843 } MigIterateState;
2844 
2845 /*
2846  * Return true if continue to the next iteration directly, false
2847  * otherwise.
2848  */
2849 static MigIterateState migration_iteration_run(MigrationState *s)
2850 {
2851     uint64_t must_precopy, can_postcopy;
2852     Error *local_err = NULL;
2853     bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE;
2854     bool can_switchover = migration_can_switchover(s);
2855 
2856     qemu_savevm_state_pending_estimate(&must_precopy, &can_postcopy);
2857     uint64_t pending_size = must_precopy + can_postcopy;
2858 
2859     trace_migrate_pending_estimate(pending_size, must_precopy, can_postcopy);
2860 
2861     if (must_precopy <= s->threshold_size) {
2862         qemu_savevm_state_pending_exact(&must_precopy, &can_postcopy);
2863         pending_size = must_precopy + can_postcopy;
2864         trace_migrate_pending_exact(pending_size, must_precopy, can_postcopy);
2865     }
2866 
2867     if ((!pending_size || pending_size < s->threshold_size) && can_switchover) {
2868         trace_migration_thread_low_pending(pending_size);
2869         migration_completion(s);
2870         return MIG_ITERATE_BREAK;
2871     }
2872 
2873     /* Still a significant amount to transfer */
2874     if (!in_postcopy && must_precopy <= s->threshold_size && can_switchover &&
2875         qatomic_read(&s->start_postcopy)) {
2876         if (postcopy_start(s, &local_err)) {
2877             migrate_set_error(s, local_err);
2878             error_report_err(local_err);
2879         }
2880         return MIG_ITERATE_SKIP;
2881     }
2882 
2883     /* Just another iteration step */
2884     qemu_savevm_state_iterate(s->to_dst_file, in_postcopy);
2885     return MIG_ITERATE_RESUME;
2886 }
2887 
2888 static void migration_iteration_finish(MigrationState *s)
2889 {
2890     /* If we enabled cpu throttling for auto-converge, turn it off. */
2891     cpu_throttle_stop();
2892 
2893     qemu_mutex_lock_iothread();
2894     switch (s->state) {
2895     case MIGRATION_STATUS_COMPLETED:
2896         migration_calculate_complete(s);
2897         runstate_set(RUN_STATE_POSTMIGRATE);
2898         break;
2899     case MIGRATION_STATUS_COLO:
2900         assert(migrate_colo());
2901         migrate_start_colo_process(s);
2902         s->vm_old_state = RUN_STATE_RUNNING;
2903         /* Fallthrough */
2904     case MIGRATION_STATUS_FAILED:
2905     case MIGRATION_STATUS_CANCELLED:
2906     case MIGRATION_STATUS_CANCELLING:
2907         if (s->vm_old_state == RUN_STATE_RUNNING) {
2908             if (!runstate_check(RUN_STATE_SHUTDOWN)) {
2909                 vm_start();
2910             }
2911         } else {
2912             if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
2913                 runstate_set(s->vm_old_state);
2914             }
2915         }
2916         break;
2917 
2918     default:
2919         /* Should not reach here, but if so, forgive the VM. */
2920         error_report("%s: Unknown ending state %d", __func__, s->state);
2921         break;
2922     }
2923     migrate_fd_cleanup_schedule(s);
2924     qemu_mutex_unlock_iothread();
2925 }
2926 
2927 static void bg_migration_iteration_finish(MigrationState *s)
2928 {
2929     /*
2930      * Stop tracking RAM writes - un-protect memory, un-register UFFD
2931      * memory ranges, flush kernel wait queues and wake up threads
2932      * waiting for write fault to be resolved.
2933      */
2934     ram_write_tracking_stop();
2935 
2936     qemu_mutex_lock_iothread();
2937     switch (s->state) {
2938     case MIGRATION_STATUS_COMPLETED:
2939         migration_calculate_complete(s);
2940         break;
2941 
2942     case MIGRATION_STATUS_ACTIVE:
2943     case MIGRATION_STATUS_FAILED:
2944     case MIGRATION_STATUS_CANCELLED:
2945     case MIGRATION_STATUS_CANCELLING:
2946         break;
2947 
2948     default:
2949         /* Should not reach here, but if so, forgive the VM. */
2950         error_report("%s: Unknown ending state %d", __func__, s->state);
2951         break;
2952     }
2953 
2954     migrate_fd_cleanup_schedule(s);
2955     qemu_mutex_unlock_iothread();
2956 }
2957 
2958 /*
2959  * Return true if continue to the next iteration directly, false
2960  * otherwise.
2961  */
2962 static MigIterateState bg_migration_iteration_run(MigrationState *s)
2963 {
2964     int res;
2965 
2966     res = qemu_savevm_state_iterate(s->to_dst_file, false);
2967     if (res > 0) {
2968         bg_migration_completion(s);
2969         return MIG_ITERATE_BREAK;
2970     }
2971 
2972     return MIG_ITERATE_RESUME;
2973 }
2974 
2975 void migration_make_urgent_request(void)
2976 {
2977     qemu_sem_post(&migrate_get_current()->rate_limit_sem);
2978 }
2979 
2980 void migration_consume_urgent_request(void)
2981 {
2982     qemu_sem_wait(&migrate_get_current()->rate_limit_sem);
2983 }
2984 
2985 /* Returns true if the rate limiting was broken by an urgent request */
2986 bool migration_rate_limit(void)
2987 {
2988     int64_t now = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2989     MigrationState *s = migrate_get_current();
2990 
2991     bool urgent = false;
2992     migration_update_counters(s, now);
2993     if (migration_rate_exceeded(s->to_dst_file)) {
2994 
2995         if (qemu_file_get_error(s->to_dst_file)) {
2996             return false;
2997         }
2998         /*
2999          * Wait for a delay to do rate limiting OR
3000          * something urgent to post the semaphore.
3001          */
3002         int ms = s->iteration_start_time + BUFFER_DELAY - now;
3003         trace_migration_rate_limit_pre(ms);
3004         if (qemu_sem_timedwait(&s->rate_limit_sem, ms) == 0) {
3005             /*
3006              * We were woken by one or more urgent things but
3007              * the timedwait will have consumed one of them.
3008              * The service routine for the urgent wake will dec
3009              * the semaphore itself for each item it consumes,
3010              * so add this one we just eat back.
3011              */
3012             qemu_sem_post(&s->rate_limit_sem);
3013             urgent = true;
3014         }
3015         trace_migration_rate_limit_post(urgent);
3016     }
3017     return urgent;
3018 }
3019 
3020 /*
3021  * if failover devices are present, wait they are completely
3022  * unplugged
3023  */
3024 
3025 static void qemu_savevm_wait_unplug(MigrationState *s, int old_state,
3026                                     int new_state)
3027 {
3028     if (qemu_savevm_state_guest_unplug_pending()) {
3029         migrate_set_state(&s->state, old_state, MIGRATION_STATUS_WAIT_UNPLUG);
3030 
3031         while (s->state == MIGRATION_STATUS_WAIT_UNPLUG &&
3032                qemu_savevm_state_guest_unplug_pending()) {
3033             qemu_sem_timedwait(&s->wait_unplug_sem, 250);
3034         }
3035         if (s->state != MIGRATION_STATUS_WAIT_UNPLUG) {
3036             int timeout = 120; /* 30 seconds */
3037             /*
3038              * migration has been canceled
3039              * but as we have started an unplug we must wait the end
3040              * to be able to plug back the card
3041              */
3042             while (timeout-- && qemu_savevm_state_guest_unplug_pending()) {
3043                 qemu_sem_timedwait(&s->wait_unplug_sem, 250);
3044             }
3045             if (qemu_savevm_state_guest_unplug_pending() &&
3046                 !qtest_enabled()) {
3047                 warn_report("migration: partially unplugged device on "
3048                             "failure");
3049             }
3050         }
3051 
3052         migrate_set_state(&s->state, MIGRATION_STATUS_WAIT_UNPLUG, new_state);
3053     } else {
3054         migrate_set_state(&s->state, old_state, new_state);
3055     }
3056 }
3057 
3058 /*
3059  * Master migration thread on the source VM.
3060  * It drives the migration and pumps the data down the outgoing channel.
3061  */
3062 static void *migration_thread(void *opaque)
3063 {
3064     MigrationState *s = opaque;
3065     MigrationThread *thread = NULL;
3066     int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
3067     MigThrError thr_error;
3068     bool urgent = false;
3069 
3070     thread = migration_threads_add("live_migration", qemu_get_thread_id());
3071 
3072     rcu_register_thread();
3073 
3074     object_ref(OBJECT(s));
3075     update_iteration_initial_status(s);
3076 
3077     qemu_mutex_lock_iothread();
3078     qemu_savevm_state_header(s->to_dst_file);
3079     qemu_mutex_unlock_iothread();
3080 
3081     /*
3082      * If we opened the return path, we need to make sure dst has it
3083      * opened as well.
3084      */
3085     if (s->rp_state.rp_thread_created) {
3086         /* Now tell the dest that it should open its end so it can reply */
3087         qemu_savevm_send_open_return_path(s->to_dst_file);
3088 
3089         /* And do a ping that will make stuff easier to debug */
3090         qemu_savevm_send_ping(s->to_dst_file, 1);
3091     }
3092 
3093     if (migrate_postcopy()) {
3094         /*
3095          * Tell the destination that we *might* want to do postcopy later;
3096          * if the other end can't do postcopy it should fail now, nice and
3097          * early.
3098          */
3099         qemu_savevm_send_postcopy_advise(s->to_dst_file);
3100     }
3101 
3102     if (migrate_colo()) {
3103         /* Notify migration destination that we enable COLO */
3104         qemu_savevm_send_colo_enable(s->to_dst_file);
3105     }
3106 
3107     qemu_mutex_lock_iothread();
3108     qemu_savevm_state_setup(s->to_dst_file);
3109     qemu_mutex_unlock_iothread();
3110 
3111     qemu_savevm_wait_unplug(s, MIGRATION_STATUS_SETUP,
3112                                MIGRATION_STATUS_ACTIVE);
3113 
3114     s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
3115 
3116     trace_migration_thread_setup_complete();
3117 
3118     while (migration_is_active(s)) {
3119         if (urgent || !migration_rate_exceeded(s->to_dst_file)) {
3120             MigIterateState iter_state = migration_iteration_run(s);
3121             if (iter_state == MIG_ITERATE_SKIP) {
3122                 continue;
3123             } else if (iter_state == MIG_ITERATE_BREAK) {
3124                 break;
3125             }
3126         }
3127 
3128         /*
3129          * Try to detect any kind of failures, and see whether we
3130          * should stop the migration now.
3131          */
3132         thr_error = migration_detect_error(s);
3133         if (thr_error == MIG_THR_ERR_FATAL) {
3134             /* Stop migration */
3135             break;
3136         } else if (thr_error == MIG_THR_ERR_RECOVERED) {
3137             /*
3138              * Just recovered from a e.g. network failure, reset all
3139              * the local variables. This is important to avoid
3140              * breaking transferred_bytes and bandwidth calculation
3141              */
3142             update_iteration_initial_status(s);
3143         }
3144 
3145         urgent = migration_rate_limit();
3146     }
3147 
3148     trace_migration_thread_after_loop();
3149     migration_iteration_finish(s);
3150     object_unref(OBJECT(s));
3151     rcu_unregister_thread();
3152     migration_threads_remove(thread);
3153     return NULL;
3154 }
3155 
3156 static void bg_migration_vm_start_bh(void *opaque)
3157 {
3158     MigrationState *s = opaque;
3159 
3160     qemu_bh_delete(s->vm_start_bh);
3161     s->vm_start_bh = NULL;
3162 
3163     vm_start();
3164     migration_downtime_end(s);
3165 }
3166 
3167 /**
3168  * Background snapshot thread, based on live migration code.
3169  * This is an alternative implementation of live migration mechanism
3170  * introduced specifically to support background snapshots.
3171  *
3172  * It takes advantage of userfault_fd write protection mechanism introduced
3173  * in v5.7 kernel. Compared to existing dirty page logging migration much
3174  * lesser stream traffic is produced resulting in smaller snapshot images,
3175  * simply cause of no page duplicates can get into the stream.
3176  *
3177  * Another key point is that generated vmstate stream reflects machine state
3178  * 'frozen' at the beginning of snapshot creation compared to dirty page logging
3179  * mechanism, which effectively results in that saved snapshot is the state of VM
3180  * at the end of the process.
3181  */
3182 static void *bg_migration_thread(void *opaque)
3183 {
3184     MigrationState *s = opaque;
3185     int64_t setup_start;
3186     MigThrError thr_error;
3187     QEMUFile *fb;
3188     bool early_fail = true;
3189 
3190     rcu_register_thread();
3191     object_ref(OBJECT(s));
3192 
3193     migration_rate_set(RATE_LIMIT_DISABLED);
3194 
3195     setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
3196     /*
3197      * We want to save vmstate for the moment when migration has been
3198      * initiated but also we want to save RAM content while VM is running.
3199      * The RAM content should appear first in the vmstate. So, we first
3200      * stash the non-RAM part of the vmstate to the temporary buffer,
3201      * then write RAM part of the vmstate to the migration stream
3202      * with vCPUs running and, finally, write stashed non-RAM part of
3203      * the vmstate from the buffer to the migration stream.
3204      */
3205     s->bioc = qio_channel_buffer_new(512 * 1024);
3206     qio_channel_set_name(QIO_CHANNEL(s->bioc), "vmstate-buffer");
3207     fb = qemu_file_new_output(QIO_CHANNEL(s->bioc));
3208     object_unref(OBJECT(s->bioc));
3209 
3210     update_iteration_initial_status(s);
3211 
3212     /*
3213      * Prepare for tracking memory writes with UFFD-WP - populate
3214      * RAM pages before protecting.
3215      */
3216 #ifdef __linux__
3217     ram_write_tracking_prepare();
3218 #endif
3219 
3220     qemu_mutex_lock_iothread();
3221     qemu_savevm_state_header(s->to_dst_file);
3222     qemu_savevm_state_setup(s->to_dst_file);
3223     qemu_mutex_unlock_iothread();
3224 
3225     qemu_savevm_wait_unplug(s, MIGRATION_STATUS_SETUP,
3226                                MIGRATION_STATUS_ACTIVE);
3227 
3228     s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
3229 
3230     trace_migration_thread_setup_complete();
3231     migration_downtime_start(s);
3232 
3233     qemu_mutex_lock_iothread();
3234 
3235     /*
3236      * If VM is currently in suspended state, then, to make a valid runstate
3237      * transition in vm_stop_force_state() we need to wakeup it up.
3238      */
3239     qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
3240     s->vm_old_state = runstate_get();
3241 
3242     global_state_store();
3243     /* Forcibly stop VM before saving state of vCPUs and devices */
3244     if (migration_stop_vm(RUN_STATE_PAUSED)) {
3245         goto fail;
3246     }
3247     /*
3248      * Put vCPUs in sync with shadow context structures, then
3249      * save their state to channel-buffer along with devices.
3250      */
3251     cpu_synchronize_all_states();
3252     if (qemu_savevm_state_complete_precopy_non_iterable(fb, false, false)) {
3253         goto fail;
3254     }
3255     /*
3256      * Since we are going to get non-iterable state data directly
3257      * from s->bioc->data, explicit flush is needed here.
3258      */
3259     qemu_fflush(fb);
3260 
3261     /* Now initialize UFFD context and start tracking RAM writes */
3262     if (ram_write_tracking_start()) {
3263         goto fail;
3264     }
3265     early_fail = false;
3266 
3267     /*
3268      * Start VM from BH handler to avoid write-fault lock here.
3269      * UFFD-WP protection for the whole RAM is already enabled so
3270      * calling VM state change notifiers from vm_start() would initiate
3271      * writes to virtio VQs memory which is in write-protected region.
3272      */
3273     s->vm_start_bh = qemu_bh_new(bg_migration_vm_start_bh, s);
3274     qemu_bh_schedule(s->vm_start_bh);
3275 
3276     qemu_mutex_unlock_iothread();
3277 
3278     while (migration_is_active(s)) {
3279         MigIterateState iter_state = bg_migration_iteration_run(s);
3280         if (iter_state == MIG_ITERATE_SKIP) {
3281             continue;
3282         } else if (iter_state == MIG_ITERATE_BREAK) {
3283             break;
3284         }
3285 
3286         /*
3287          * Try to detect any kind of failures, and see whether we
3288          * should stop the migration now.
3289          */
3290         thr_error = migration_detect_error(s);
3291         if (thr_error == MIG_THR_ERR_FATAL) {
3292             /* Stop migration */
3293             break;
3294         }
3295 
3296         migration_update_counters(s, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
3297     }
3298 
3299     trace_migration_thread_after_loop();
3300 
3301 fail:
3302     if (early_fail) {
3303         migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE,
3304                 MIGRATION_STATUS_FAILED);
3305         qemu_mutex_unlock_iothread();
3306     }
3307 
3308     bg_migration_iteration_finish(s);
3309 
3310     qemu_fclose(fb);
3311     object_unref(OBJECT(s));
3312     rcu_unregister_thread();
3313 
3314     return NULL;
3315 }
3316 
3317 void migrate_fd_connect(MigrationState *s, Error *error_in)
3318 {
3319     Error *local_err = NULL;
3320     uint64_t rate_limit;
3321     bool resume = s->state == MIGRATION_STATUS_POSTCOPY_PAUSED;
3322 
3323     /*
3324      * If there's a previous error, free it and prepare for another one.
3325      * Meanwhile if migration completes successfully, there won't have an error
3326      * dumped when calling migrate_fd_cleanup().
3327      */
3328     migrate_error_free(s);
3329 
3330     s->expected_downtime = migrate_downtime_limit();
3331     if (resume) {
3332         assert(s->cleanup_bh);
3333     } else {
3334         assert(!s->cleanup_bh);
3335         s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup_bh, s);
3336     }
3337     if (error_in) {
3338         migrate_fd_error(s, error_in);
3339         if (resume) {
3340             /*
3341              * Don't do cleanup for resume if channel is invalid, but only dump
3342              * the error.  We wait for another channel connect from the user.
3343              * The error_report still gives HMP user a hint on what failed.
3344              * It's normally done in migrate_fd_cleanup(), but call it here
3345              * explicitly.
3346              */
3347             error_report_err(error_copy(s->error));
3348         } else {
3349             migrate_fd_cleanup(s);
3350         }
3351         return;
3352     }
3353 
3354     if (resume) {
3355         /* This is a resumed migration */
3356         rate_limit = migrate_max_postcopy_bandwidth();
3357     } else {
3358         /* This is a fresh new migration */
3359         rate_limit = migrate_max_bandwidth();
3360 
3361         /* Notify before starting migration thread */
3362         migration_call_notifiers(s);
3363     }
3364 
3365     migration_rate_set(rate_limit);
3366     qemu_file_set_blocking(s->to_dst_file, true);
3367 
3368     /*
3369      * Open the return path. For postcopy, it is used exclusively. For
3370      * precopy, only if user specified "return-path" capability would
3371      * QEMU uses the return path.
3372      */
3373     if (migrate_postcopy_ram() || migrate_return_path()) {
3374         if (open_return_path_on_source(s)) {
3375             error_setg(&local_err, "Unable to open return-path for postcopy");
3376             migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
3377             migrate_set_error(s, local_err);
3378             error_report_err(local_err);
3379             migrate_fd_cleanup(s);
3380             return;
3381         }
3382     }
3383 
3384     /*
3385      * This needs to be done before resuming a postcopy.  Note: for newer
3386      * QEMUs we will delay the channel creation until postcopy_start(), to
3387      * avoid disorder of channel creations.
3388      */
3389     if (migrate_postcopy_preempt() && s->preempt_pre_7_2) {
3390         postcopy_preempt_setup(s);
3391     }
3392 
3393     if (resume) {
3394         /* Wakeup the main migration thread to do the recovery */
3395         migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
3396                           MIGRATION_STATUS_POSTCOPY_RECOVER);
3397         qemu_sem_post(&s->postcopy_pause_sem);
3398         return;
3399     }
3400 
3401     if (multifd_save_setup(&local_err) != 0) {
3402         migrate_set_error(s, local_err);
3403         error_report_err(local_err);
3404         migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3405                           MIGRATION_STATUS_FAILED);
3406         migrate_fd_cleanup(s);
3407         return;
3408     }
3409 
3410     if (migrate_background_snapshot()) {
3411         qemu_thread_create(&s->thread, "bg_snapshot",
3412                 bg_migration_thread, s, QEMU_THREAD_JOINABLE);
3413     } else {
3414         qemu_thread_create(&s->thread, "live_migration",
3415                 migration_thread, s, QEMU_THREAD_JOINABLE);
3416     }
3417     s->migration_thread_running = true;
3418 }
3419 
3420 static void migration_class_init(ObjectClass *klass, void *data)
3421 {
3422     DeviceClass *dc = DEVICE_CLASS(klass);
3423 
3424     dc->user_creatable = false;
3425     device_class_set_props(dc, migration_properties);
3426 }
3427 
3428 static void migration_instance_finalize(Object *obj)
3429 {
3430     MigrationState *ms = MIGRATION_OBJ(obj);
3431 
3432     qemu_mutex_destroy(&ms->error_mutex);
3433     qemu_mutex_destroy(&ms->qemu_file_lock);
3434     qemu_sem_destroy(&ms->wait_unplug_sem);
3435     qemu_sem_destroy(&ms->rate_limit_sem);
3436     qemu_sem_destroy(&ms->pause_sem);
3437     qemu_sem_destroy(&ms->postcopy_pause_sem);
3438     qemu_sem_destroy(&ms->rp_state.rp_sem);
3439     qemu_sem_destroy(&ms->rp_state.rp_pong_acks);
3440     qemu_sem_destroy(&ms->postcopy_qemufile_src_sem);
3441     error_free(ms->error);
3442 }
3443 
3444 static void migration_instance_init(Object *obj)
3445 {
3446     MigrationState *ms = MIGRATION_OBJ(obj);
3447 
3448     ms->state = MIGRATION_STATUS_NONE;
3449     ms->mbps = -1;
3450     ms->pages_per_second = -1;
3451     qemu_sem_init(&ms->pause_sem, 0);
3452     qemu_mutex_init(&ms->error_mutex);
3453 
3454     migrate_params_init(&ms->parameters);
3455 
3456     qemu_sem_init(&ms->postcopy_pause_sem, 0);
3457     qemu_sem_init(&ms->rp_state.rp_sem, 0);
3458     qemu_sem_init(&ms->rp_state.rp_pong_acks, 0);
3459     qemu_sem_init(&ms->rate_limit_sem, 0);
3460     qemu_sem_init(&ms->wait_unplug_sem, 0);
3461     qemu_sem_init(&ms->postcopy_qemufile_src_sem, 0);
3462     qemu_mutex_init(&ms->qemu_file_lock);
3463 }
3464 
3465 /*
3466  * Return true if check pass, false otherwise. Error will be put
3467  * inside errp if provided.
3468  */
3469 static bool migration_object_check(MigrationState *ms, Error **errp)
3470 {
3471     /* Assuming all off */
3472     bool old_caps[MIGRATION_CAPABILITY__MAX] = { 0 };
3473 
3474     if (!migrate_params_check(&ms->parameters, errp)) {
3475         return false;
3476     }
3477 
3478     return migrate_caps_check(old_caps, ms->capabilities, errp);
3479 }
3480 
3481 static const TypeInfo migration_type = {
3482     .name = TYPE_MIGRATION,
3483     /*
3484      * NOTE: TYPE_MIGRATION is not really a device, as the object is
3485      * not created using qdev_new(), it is not attached to the qdev
3486      * device tree, and it is never realized.
3487      *
3488      * TODO: Make this TYPE_OBJECT once QOM provides something like
3489      * TYPE_DEVICE's "-global" properties.
3490      */
3491     .parent = TYPE_DEVICE,
3492     .class_init = migration_class_init,
3493     .class_size = sizeof(MigrationClass),
3494     .instance_size = sizeof(MigrationState),
3495     .instance_init = migration_instance_init,
3496     .instance_finalize = migration_instance_finalize,
3497 };
3498 
3499 static void register_migration_types(void)
3500 {
3501     type_register_static(&migration_type);
3502 }
3503 
3504 type_init(register_migration_types);
3505