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