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