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