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