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