xref: /qemu/migration/migration.c (revision 417296c8)
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 "socket.h"
24 #include "sysemu/runstate.h"
25 #include "sysemu/sysemu.h"
26 #include "sysemu/cpu-throttle.h"
27 #include "rdma.h"
28 #include "ram.h"
29 #include "migration/global_state.h"
30 #include "migration/misc.h"
31 #include "migration.h"
32 #include "savevm.h"
33 #include "qemu-file.h"
34 #include "channel.h"
35 #include "migration/vmstate.h"
36 #include "block/block.h"
37 #include "qapi/error.h"
38 #include "qapi/clone-visitor.h"
39 #include "qapi/qapi-visit-migration.h"
40 #include "qapi/qapi-visit-sockets.h"
41 #include "qapi/qapi-commands-migration.h"
42 #include "qapi/qapi-events-migration.h"
43 #include "qapi/qmp/qerror.h"
44 #include "qapi/qmp/qnull.h"
45 #include "qemu/rcu.h"
46 #include "block.h"
47 #include "postcopy-ram.h"
48 #include "qemu/thread.h"
49 #include "trace.h"
50 #include "exec/target_page.h"
51 #include "io/channel-buffer.h"
52 #include "io/channel-tls.h"
53 #include "migration/colo.h"
54 #include "hw/boards.h"
55 #include "hw/qdev-properties.h"
56 #include "hw/qdev-properties-system.h"
57 #include "monitor/monitor.h"
58 #include "net/announce.h"
59 #include "qemu/queue.h"
60 #include "multifd.h"
61 #include "threadinfo.h"
62 #include "qemu/yank.h"
63 #include "sysemu/cpus.h"
64 #include "yank_functions.h"
65 #include "sysemu/qtest.h"
66 #include "ui/qemu-spice.h"
67 
68 #define MAX_THROTTLE  (128 << 20)      /* Migration transfer speed throttling */
69 
70 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
71  * data. */
72 #define BUFFER_DELAY     100
73 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
74 
75 /* Time in milliseconds we are allowed to stop the source,
76  * for sending the last part */
77 #define DEFAULT_MIGRATE_SET_DOWNTIME 300
78 
79 /* Maximum migrate downtime set to 2000 seconds */
80 #define MAX_MIGRATE_DOWNTIME_SECONDS 2000
81 #define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
82 
83 /* Default compression thread count */
84 #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
85 /* Default decompression thread count, usually decompression is at
86  * least 4 times as fast as compression.*/
87 #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
88 /*0: means nocompress, 1: best speed, ... 9: best compress ratio */
89 #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
90 /* Define default autoconverge cpu throttle migration parameters */
91 #define DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD 50
92 #define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
93 #define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
94 #define DEFAULT_MIGRATE_MAX_CPU_THROTTLE 99
95 
96 /* Migration XBZRLE default cache size */
97 #define DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE (64 * 1024 * 1024)
98 
99 /* The delay time (in ms) between two COLO checkpoints */
100 #define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY (200 * 100)
101 #define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2
102 #define DEFAULT_MIGRATE_MULTIFD_COMPRESSION MULTIFD_COMPRESSION_NONE
103 /* 0: means nocompress, 1: best speed, ... 9: best compress ratio */
104 #define DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL 1
105 /* 0: means nocompress, 1: best speed, ... 20: best compress ratio */
106 #define DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL 1
107 
108 /* Background transfer rate for postcopy, 0 means unlimited, note
109  * that page requests can still exceed this limit.
110  */
111 #define DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH 0
112 
113 /*
114  * Parameters for self_announce_delay giving a stream of RARP/ARP
115  * packets after migration.
116  */
117 #define DEFAULT_MIGRATE_ANNOUNCE_INITIAL  50
118 #define DEFAULT_MIGRATE_ANNOUNCE_MAX     550
119 #define DEFAULT_MIGRATE_ANNOUNCE_ROUNDS    5
120 #define DEFAULT_MIGRATE_ANNOUNCE_STEP    100
121 
122 static NotifierList migration_state_notifiers =
123     NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
124 
125 /* Messages sent on the return path from destination to source */
126 enum mig_rp_message_type {
127     MIG_RP_MSG_INVALID = 0,  /* Must be 0 */
128     MIG_RP_MSG_SHUT,         /* sibling will not send any more RP messages */
129     MIG_RP_MSG_PONG,         /* Response to a PING; data (seq: be32 ) */
130 
131     MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */
132     MIG_RP_MSG_REQ_PAGES,    /* data (start: be64, len: be32) */
133     MIG_RP_MSG_RECV_BITMAP,  /* send recved_bitmap back to source */
134     MIG_RP_MSG_RESUME_ACK,   /* tell source that we are ready to resume */
135 
136     MIG_RP_MSG_MAX
137 };
138 
139 /* Migration capabilities set */
140 struct MigrateCapsSet {
141     int size;                       /* Capability set size */
142     MigrationCapability caps[];     /* Variadic array of capabilities */
143 };
144 typedef struct MigrateCapsSet MigrateCapsSet;
145 
146 /* Define and initialize MigrateCapsSet */
147 #define INITIALIZE_MIGRATE_CAPS_SET(_name, ...)   \
148     MigrateCapsSet _name = {    \
149         .size = sizeof((int []) { __VA_ARGS__ }) / sizeof(int), \
150         .caps = { __VA_ARGS__ } \
151     }
152 
153 /* Background-snapshot compatibility check list */
154 static const
155 INITIALIZE_MIGRATE_CAPS_SET(check_caps_background_snapshot,
156     MIGRATION_CAPABILITY_POSTCOPY_RAM,
157     MIGRATION_CAPABILITY_DIRTY_BITMAPS,
158     MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME,
159     MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE,
160     MIGRATION_CAPABILITY_RETURN_PATH,
161     MIGRATION_CAPABILITY_MULTIFD,
162     MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER,
163     MIGRATION_CAPABILITY_AUTO_CONVERGE,
164     MIGRATION_CAPABILITY_RELEASE_RAM,
165     MIGRATION_CAPABILITY_RDMA_PIN_ALL,
166     MIGRATION_CAPABILITY_COMPRESS,
167     MIGRATION_CAPABILITY_XBZRLE,
168     MIGRATION_CAPABILITY_X_COLO,
169     MIGRATION_CAPABILITY_VALIDATE_UUID,
170     MIGRATION_CAPABILITY_ZERO_COPY_SEND);
171 
172 /* When we add fault tolerance, we could have several
173    migrations at once.  For now we don't need to add
174    dynamic creation of migration */
175 
176 static MigrationState *current_migration;
177 static MigrationIncomingState *current_incoming;
178 
179 static GSList *migration_blockers;
180 
181 static bool migration_object_check(MigrationState *ms, Error **errp);
182 static int migration_maybe_pause(MigrationState *s,
183                                  int *current_active_state,
184                                  int new_state);
185 static void migrate_fd_cancel(MigrationState *s);
186 
187 static bool migrate_allow_multi_channels = true;
188 
189 void migrate_protocol_allow_multi_channels(bool allow)
190 {
191     migrate_allow_multi_channels = allow;
192 }
193 
194 bool migrate_multi_channels_is_allowed(void)
195 {
196     return migrate_allow_multi_channels;
197 }
198 
199 static gint page_request_addr_cmp(gconstpointer ap, gconstpointer bp)
200 {
201     uintptr_t a = (uintptr_t) ap, b = (uintptr_t) bp;
202 
203     return (a > b) - (a < b);
204 }
205 
206 void migration_object_init(void)
207 {
208     /* This can only be called once. */
209     assert(!current_migration);
210     current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION));
211 
212     /*
213      * Init the migrate incoming object as well no matter whether
214      * we'll use it or not.
215      */
216     assert(!current_incoming);
217     current_incoming = g_new0(MigrationIncomingState, 1);
218     current_incoming->state = MIGRATION_STATUS_NONE;
219     current_incoming->postcopy_remote_fds =
220         g_array_new(FALSE, TRUE, sizeof(struct PostCopyFD));
221     qemu_mutex_init(&current_incoming->rp_mutex);
222     qemu_mutex_init(&current_incoming->postcopy_prio_thread_mutex);
223     qemu_event_init(&current_incoming->main_thread_load_event, false);
224     qemu_sem_init(&current_incoming->postcopy_pause_sem_dst, 0);
225     qemu_sem_init(&current_incoming->postcopy_pause_sem_fault, 0);
226     qemu_sem_init(&current_incoming->postcopy_pause_sem_fast_load, 0);
227     qemu_mutex_init(&current_incoming->page_request_mutex);
228     current_incoming->page_requested = g_tree_new(page_request_addr_cmp);
229 
230     migration_object_check(current_migration, &error_fatal);
231 
232     blk_mig_init();
233     ram_mig_init();
234     dirty_bitmap_mig_init();
235 }
236 
237 void migration_cancel(const Error *error)
238 {
239     if (error) {
240         migrate_set_error(current_migration, error);
241     }
242     migrate_fd_cancel(current_migration);
243 }
244 
245 void migration_shutdown(void)
246 {
247     /*
248      * When the QEMU main thread exit, the COLO thread
249      * may wait a semaphore. So, we should wakeup the
250      * COLO thread before migration shutdown.
251      */
252     colo_shutdown();
253     /*
254      * Cancel the current migration - that will (eventually)
255      * stop the migration using this structure
256      */
257     migration_cancel(NULL);
258     object_unref(OBJECT(current_migration));
259 
260     /*
261      * Cancel outgoing migration of dirty bitmaps. It should
262      * at least unref used block nodes.
263      */
264     dirty_bitmap_mig_cancel_outgoing();
265 
266     /*
267      * Cancel incoming migration of dirty bitmaps. Dirty bitmaps
268      * are non-critical data, and their loss never considered as
269      * something serious.
270      */
271     dirty_bitmap_mig_cancel_incoming();
272 }
273 
274 /* For outgoing */
275 MigrationState *migrate_get_current(void)
276 {
277     /* This can only be called after the object created. */
278     assert(current_migration);
279     return current_migration;
280 }
281 
282 MigrationIncomingState *migration_incoming_get_current(void)
283 {
284     assert(current_incoming);
285     return current_incoming;
286 }
287 
288 void migration_incoming_transport_cleanup(MigrationIncomingState *mis)
289 {
290     if (mis->socket_address_list) {
291         qapi_free_SocketAddressList(mis->socket_address_list);
292         mis->socket_address_list = NULL;
293     }
294 
295     if (mis->transport_cleanup) {
296         mis->transport_cleanup(mis->transport_data);
297         mis->transport_data = mis->transport_cleanup = NULL;
298     }
299 }
300 
301 void migration_incoming_state_destroy(void)
302 {
303     struct MigrationIncomingState *mis = migration_incoming_get_current();
304 
305     if (mis->to_src_file) {
306         /* Tell source that we are done */
307         migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
308         qemu_fclose(mis->to_src_file);
309         mis->to_src_file = NULL;
310     }
311 
312     if (mis->from_src_file) {
313         migration_ioc_unregister_yank_from_file(mis->from_src_file);
314         qemu_fclose(mis->from_src_file);
315         mis->from_src_file = NULL;
316     }
317     if (mis->postcopy_remote_fds) {
318         g_array_free(mis->postcopy_remote_fds, TRUE);
319         mis->postcopy_remote_fds = NULL;
320     }
321 
322     migration_incoming_transport_cleanup(mis);
323     qemu_event_reset(&mis->main_thread_load_event);
324 
325     if (mis->page_requested) {
326         g_tree_destroy(mis->page_requested);
327         mis->page_requested = NULL;
328     }
329 
330     if (mis->postcopy_qemufile_dst) {
331         migration_ioc_unregister_yank_from_file(mis->postcopy_qemufile_dst);
332         qemu_fclose(mis->postcopy_qemufile_dst);
333         mis->postcopy_qemufile_dst = NULL;
334     }
335 
336     yank_unregister_instance(MIGRATION_YANK_INSTANCE);
337 }
338 
339 static void migrate_generate_event(int new_state)
340 {
341     if (migrate_use_events()) {
342         qapi_event_send_migration(new_state);
343     }
344 }
345 
346 static bool migrate_late_block_activate(void)
347 {
348     MigrationState *s;
349 
350     s = migrate_get_current();
351 
352     return s->enabled_capabilities[
353         MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE];
354 }
355 
356 /*
357  * Send a message on the return channel back to the source
358  * of the migration.
359  */
360 static int migrate_send_rp_message(MigrationIncomingState *mis,
361                                    enum mig_rp_message_type message_type,
362                                    uint16_t len, void *data)
363 {
364     int ret = 0;
365 
366     trace_migrate_send_rp_message((int)message_type, len);
367     QEMU_LOCK_GUARD(&mis->rp_mutex);
368 
369     /*
370      * It's possible that the file handle got lost due to network
371      * failures.
372      */
373     if (!mis->to_src_file) {
374         ret = -EIO;
375         return ret;
376     }
377 
378     qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
379     qemu_put_be16(mis->to_src_file, len);
380     qemu_put_buffer(mis->to_src_file, data, len);
381     qemu_fflush(mis->to_src_file);
382 
383     /* It's possible that qemu file got error during sending */
384     ret = qemu_file_get_error(mis->to_src_file);
385 
386     return ret;
387 }
388 
389 /* Request one page from the source VM at the given start address.
390  *   rb: the RAMBlock to request the page in
391  *   Start: Address offset within the RB
392  *   Len: Length in bytes required - must be a multiple of pagesize
393  */
394 int migrate_send_rp_message_req_pages(MigrationIncomingState *mis,
395                                       RAMBlock *rb, ram_addr_t start)
396 {
397     uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
398     size_t msglen = 12; /* start + len */
399     size_t len = qemu_ram_pagesize(rb);
400     enum mig_rp_message_type msg_type;
401     const char *rbname;
402     int rbname_len;
403 
404     *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
405     *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
406 
407     /*
408      * We maintain the last ramblock that we requested for page.  Note that we
409      * don't need locking because this function will only be called within the
410      * postcopy ram fault thread.
411      */
412     if (rb != mis->last_rb) {
413         mis->last_rb = rb;
414 
415         rbname = qemu_ram_get_idstr(rb);
416         rbname_len = strlen(rbname);
417 
418         assert(rbname_len < 256);
419 
420         bufc[msglen++] = rbname_len;
421         memcpy(bufc + msglen, rbname, rbname_len);
422         msglen += rbname_len;
423         msg_type = MIG_RP_MSG_REQ_PAGES_ID;
424     } else {
425         msg_type = MIG_RP_MSG_REQ_PAGES;
426     }
427 
428     return migrate_send_rp_message(mis, msg_type, msglen, bufc);
429 }
430 
431 int migrate_send_rp_req_pages(MigrationIncomingState *mis,
432                               RAMBlock *rb, ram_addr_t start, uint64_t haddr)
433 {
434     void *aligned = (void *)(uintptr_t)ROUND_DOWN(haddr, qemu_ram_pagesize(rb));
435     bool received = false;
436 
437     WITH_QEMU_LOCK_GUARD(&mis->page_request_mutex) {
438         received = ramblock_recv_bitmap_test_byte_offset(rb, start);
439         if (!received && !g_tree_lookup(mis->page_requested, aligned)) {
440             /*
441              * The page has not been received, and it's not yet in the page
442              * request list.  Queue it.  Set the value of element to 1, so that
443              * things like g_tree_lookup() will return TRUE (1) when found.
444              */
445             g_tree_insert(mis->page_requested, aligned, (gpointer)1);
446             mis->page_requested_count++;
447             trace_postcopy_page_req_add(aligned, mis->page_requested_count);
448         }
449     }
450 
451     /*
452      * If the page is there, skip sending the message.  We don't even need the
453      * lock because as long as the page arrived, it'll be there forever.
454      */
455     if (received) {
456         return 0;
457     }
458 
459     return migrate_send_rp_message_req_pages(mis, rb, start);
460 }
461 
462 static bool migration_colo_enabled;
463 bool migration_incoming_colo_enabled(void)
464 {
465     return migration_colo_enabled;
466 }
467 
468 void migration_incoming_disable_colo(void)
469 {
470     ram_block_discard_disable(false);
471     migration_colo_enabled = false;
472 }
473 
474 int migration_incoming_enable_colo(void)
475 {
476     if (ram_block_discard_disable(true)) {
477         error_report("COLO: cannot disable RAM discard");
478         return -EBUSY;
479     }
480     migration_colo_enabled = true;
481     return 0;
482 }
483 
484 void migrate_add_address(SocketAddress *address)
485 {
486     MigrationIncomingState *mis = migration_incoming_get_current();
487 
488     QAPI_LIST_PREPEND(mis->socket_address_list,
489                       QAPI_CLONE(SocketAddress, address));
490 }
491 
492 static void qemu_start_incoming_migration(const char *uri, Error **errp)
493 {
494     const char *p = NULL;
495 
496     migrate_protocol_allow_multi_channels(false); /* reset it anyway */
497     qapi_event_send_migration(MIGRATION_STATUS_SETUP);
498     if (strstart(uri, "tcp:", &p) ||
499         strstart(uri, "unix:", NULL) ||
500         strstart(uri, "vsock:", NULL)) {
501         migrate_protocol_allow_multi_channels(true);
502         socket_start_incoming_migration(p ? p : uri, errp);
503 #ifdef CONFIG_RDMA
504     } else if (strstart(uri, "rdma:", &p)) {
505         rdma_start_incoming_migration(p, errp);
506 #endif
507     } else if (strstart(uri, "exec:", &p)) {
508         exec_start_incoming_migration(p, errp);
509     } else if (strstart(uri, "fd:", &p)) {
510         fd_start_incoming_migration(p, errp);
511     } else {
512         error_setg(errp, "unknown migration protocol: %s", uri);
513     }
514 }
515 
516 static void process_incoming_migration_bh(void *opaque)
517 {
518     Error *local_err = NULL;
519     MigrationIncomingState *mis = opaque;
520 
521     /* If capability late_block_activate is set:
522      * Only fire up the block code now if we're going to restart the
523      * VM, else 'cont' will do it.
524      * This causes file locking to happen; so we don't want it to happen
525      * unless we really are starting the VM.
526      */
527     if (!migrate_late_block_activate() ||
528          (autostart && (!global_state_received() ||
529             global_state_get_runstate() == RUN_STATE_RUNNING))) {
530         /* Make sure all file formats throw away their mutable metadata.
531          * If we get an error here, just don't restart the VM yet. */
532         bdrv_activate_all(&local_err);
533         if (local_err) {
534             error_report_err(local_err);
535             local_err = NULL;
536             autostart = false;
537         }
538     }
539 
540     /*
541      * This must happen after all error conditions are dealt with and
542      * we're sure the VM is going to be running on this host.
543      */
544     qemu_announce_self(&mis->announce_timer, migrate_announce_params());
545 
546     if (multifd_load_cleanup(&local_err) != 0) {
547         error_report_err(local_err);
548         autostart = false;
549     }
550     /* If global state section was not received or we are in running
551        state, we need to obey autostart. Any other state is set with
552        runstate_set. */
553 
554     dirty_bitmap_mig_before_vm_start();
555 
556     if (!global_state_received() ||
557         global_state_get_runstate() == RUN_STATE_RUNNING) {
558         if (autostart) {
559             vm_start();
560         } else {
561             runstate_set(RUN_STATE_PAUSED);
562         }
563     } else if (migration_incoming_colo_enabled()) {
564         migration_incoming_disable_colo();
565         vm_start();
566     } else {
567         runstate_set(global_state_get_runstate());
568     }
569     /*
570      * This must happen after any state changes since as soon as an external
571      * observer sees this event they might start to prod at the VM assuming
572      * it's ready to use.
573      */
574     migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
575                       MIGRATION_STATUS_COMPLETED);
576     qemu_bh_delete(mis->bh);
577     migration_incoming_state_destroy();
578 }
579 
580 static void coroutine_fn
581 process_incoming_migration_co(void *opaque)
582 {
583     MigrationIncomingState *mis = migration_incoming_get_current();
584     PostcopyState ps;
585     int ret;
586     Error *local_err = NULL;
587 
588     assert(mis->from_src_file);
589     mis->migration_incoming_co = qemu_coroutine_self();
590     mis->largest_page_size = qemu_ram_pagesize_largest();
591     postcopy_state_set(POSTCOPY_INCOMING_NONE);
592     migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
593                       MIGRATION_STATUS_ACTIVE);
594     ret = qemu_loadvm_state(mis->from_src_file);
595 
596     ps = postcopy_state_get();
597     trace_process_incoming_migration_co_end(ret, ps);
598     if (ps != POSTCOPY_INCOMING_NONE) {
599         if (ps == POSTCOPY_INCOMING_ADVISE) {
600             /*
601              * Where a migration had postcopy enabled (and thus went to advise)
602              * but managed to complete within the precopy period, we can use
603              * the normal exit.
604              */
605             postcopy_ram_incoming_cleanup(mis);
606         } else if (ret >= 0) {
607             /*
608              * Postcopy was started, cleanup should happen at the end of the
609              * postcopy thread.
610              */
611             trace_process_incoming_migration_co_postcopy_end_main();
612             return;
613         }
614         /* Else if something went wrong then just fall out of the normal exit */
615     }
616 
617     /* we get COLO info, and know if we are in COLO mode */
618     if (!ret && migration_incoming_colo_enabled()) {
619         /* Make sure all file formats throw away their mutable metadata */
620         bdrv_activate_all(&local_err);
621         if (local_err) {
622             error_report_err(local_err);
623             goto fail;
624         }
625 
626         qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
627              colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
628         mis->have_colo_incoming_thread = true;
629         qemu_coroutine_yield();
630 
631         qemu_mutex_unlock_iothread();
632         /* Wait checkpoint incoming thread exit before free resource */
633         qemu_thread_join(&mis->colo_incoming_thread);
634         qemu_mutex_lock_iothread();
635         /* We hold the global iothread lock, so it is safe here */
636         colo_release_ram_cache();
637     }
638 
639     if (ret < 0) {
640         error_report("load of migration failed: %s", strerror(-ret));
641         goto fail;
642     }
643     mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
644     qemu_bh_schedule(mis->bh);
645     mis->migration_incoming_co = NULL;
646     return;
647 fail:
648     local_err = NULL;
649     migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
650                       MIGRATION_STATUS_FAILED);
651     qemu_fclose(mis->from_src_file);
652     if (multifd_load_cleanup(&local_err) != 0) {
653         error_report_err(local_err);
654     }
655     exit(EXIT_FAILURE);
656 }
657 
658 /**
659  * migration_incoming_setup: Setup incoming migration
660  * @f: file for main migration channel
661  * @errp: where to put errors
662  *
663  * Returns: %true on success, %false on error.
664  */
665 static bool migration_incoming_setup(QEMUFile *f, Error **errp)
666 {
667     MigrationIncomingState *mis = migration_incoming_get_current();
668 
669     if (!mis->from_src_file) {
670         mis->from_src_file = f;
671     }
672     qemu_file_set_blocking(f, false);
673     return true;
674 }
675 
676 void migration_incoming_process(void)
677 {
678     Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL);
679     qemu_coroutine_enter(co);
680 }
681 
682 /* Returns true if recovered from a paused migration, otherwise false */
683 static bool postcopy_try_recover(void)
684 {
685     MigrationIncomingState *mis = migration_incoming_get_current();
686 
687     if (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
688         /* Resumed from a paused postcopy migration */
689 
690         /* This should be set already in migration_incoming_setup() */
691         assert(mis->from_src_file);
692         /* Postcopy has standalone thread to do vm load */
693         qemu_file_set_blocking(mis->from_src_file, true);
694 
695         /* Re-configure the return path */
696         mis->to_src_file = qemu_file_get_return_path(mis->from_src_file);
697 
698         migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
699                           MIGRATION_STATUS_POSTCOPY_RECOVER);
700 
701         /*
702          * Here, we only wake up the main loading thread (while the
703          * rest threads will still be waiting), so that we can receive
704          * commands from source now, and answer it if needed. The
705          * rest threads will be woken up afterwards until we are sure
706          * that source is ready to reply to page requests.
707          */
708         qemu_sem_post(&mis->postcopy_pause_sem_dst);
709         return true;
710     }
711 
712     return false;
713 }
714 
715 void migration_fd_process_incoming(QEMUFile *f, Error **errp)
716 {
717     if (!migration_incoming_setup(f, errp)) {
718         return;
719     }
720     if (postcopy_try_recover()) {
721         return;
722     }
723     migration_incoming_process();
724 }
725 
726 static bool migration_needs_multiple_sockets(void)
727 {
728     return migrate_use_multifd() || migrate_postcopy_preempt();
729 }
730 
731 void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp)
732 {
733     MigrationIncomingState *mis = migration_incoming_get_current();
734     Error *local_err = NULL;
735     QEMUFile *f;
736     bool default_channel = true;
737     uint32_t channel_magic = 0;
738     int ret = 0;
739 
740     if (migrate_use_multifd() && !migrate_postcopy_ram() &&
741         qio_channel_has_feature(ioc, QIO_CHANNEL_FEATURE_READ_MSG_PEEK)) {
742         /*
743          * With multiple channels, it is possible that we receive channels
744          * out of order on destination side, causing incorrect mapping of
745          * source channels on destination side. Check channel MAGIC to
746          * decide type of channel. Please note this is best effort, postcopy
747          * preempt channel does not send any magic number so avoid it for
748          * postcopy live migration. Also tls live migration already does
749          * tls handshake while initializing main channel so with tls this
750          * issue is not possible.
751          */
752         ret = migration_channel_read_peek(ioc, (void *)&channel_magic,
753                                           sizeof(channel_magic), &local_err);
754 
755         if (ret != 0) {
756             error_propagate(errp, local_err);
757             return;
758         }
759 
760         default_channel = (channel_magic == cpu_to_be32(QEMU_VM_FILE_MAGIC));
761     } else {
762         default_channel = !mis->from_src_file;
763     }
764 
765     if (multifd_load_setup(errp) != 0) {
766         error_setg(errp, "Failed to setup multifd channels");
767         return;
768     }
769 
770     if (default_channel) {
771         f = qemu_file_new_input(ioc);
772 
773         if (!migration_incoming_setup(f, errp)) {
774             return;
775         }
776     } else {
777         /* Multiple connections */
778         assert(migration_needs_multiple_sockets());
779         if (migrate_use_multifd()) {
780             multifd_recv_new_channel(ioc, &local_err);
781         } else {
782             assert(migrate_postcopy_preempt());
783             f = qemu_file_new_input(ioc);
784             postcopy_preempt_new_channel(mis, f);
785         }
786         if (local_err) {
787             error_propagate(errp, local_err);
788             return;
789         }
790     }
791 
792     if (migration_has_all_channels()) {
793         /* If it's a recovery, we're done */
794         if (postcopy_try_recover()) {
795             return;
796         }
797         migration_incoming_process();
798     }
799 }
800 
801 /**
802  * @migration_has_all_channels: We have received all channels that we need
803  *
804  * Returns true when we have got connections to all the channels that
805  * we need for migration.
806  */
807 bool migration_has_all_channels(void)
808 {
809     MigrationIncomingState *mis = migration_incoming_get_current();
810 
811     if (!mis->from_src_file) {
812         return false;
813     }
814 
815     if (migrate_use_multifd()) {
816         return multifd_recv_all_channels_created();
817     }
818 
819     if (migrate_postcopy_preempt()) {
820         return mis->postcopy_qemufile_dst != NULL;
821     }
822 
823     return true;
824 }
825 
826 /*
827  * Send a 'SHUT' message on the return channel with the given value
828  * to indicate that we've finished with the RP.  Non-0 value indicates
829  * error.
830  */
831 void migrate_send_rp_shut(MigrationIncomingState *mis,
832                           uint32_t value)
833 {
834     uint32_t buf;
835 
836     buf = cpu_to_be32(value);
837     migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
838 }
839 
840 /*
841  * Send a 'PONG' message on the return channel with the given value
842  * (normally in response to a 'PING')
843  */
844 void migrate_send_rp_pong(MigrationIncomingState *mis,
845                           uint32_t value)
846 {
847     uint32_t buf;
848 
849     buf = cpu_to_be32(value);
850     migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
851 }
852 
853 void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
854                                  char *block_name)
855 {
856     char buf[512];
857     int len;
858     int64_t res;
859 
860     /*
861      * First, we send the header part. It contains only the len of
862      * idstr, and the idstr itself.
863      */
864     len = strlen(block_name);
865     buf[0] = len;
866     memcpy(buf + 1, block_name, len);
867 
868     if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
869         error_report("%s: MSG_RP_RECV_BITMAP only used for recovery",
870                      __func__);
871         return;
872     }
873 
874     migrate_send_rp_message(mis, MIG_RP_MSG_RECV_BITMAP, len + 1, buf);
875 
876     /*
877      * Next, we dump the received bitmap to the stream.
878      *
879      * TODO: currently we are safe since we are the only one that is
880      * using the to_src_file handle (fault thread is still paused),
881      * and it's ok even not taking the mutex. However the best way is
882      * to take the lock before sending the message header, and release
883      * the lock after sending the bitmap.
884      */
885     qemu_mutex_lock(&mis->rp_mutex);
886     res = ramblock_recv_bitmap_send(mis->to_src_file, block_name);
887     qemu_mutex_unlock(&mis->rp_mutex);
888 
889     trace_migrate_send_rp_recv_bitmap(block_name, res);
890 }
891 
892 void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value)
893 {
894     uint32_t buf;
895 
896     buf = cpu_to_be32(value);
897     migrate_send_rp_message(mis, MIG_RP_MSG_RESUME_ACK, sizeof(buf), &buf);
898 }
899 
900 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
901 {
902     MigrationCapabilityStatusList *head = NULL, **tail = &head;
903     MigrationCapabilityStatus *caps;
904     MigrationState *s = migrate_get_current();
905     int i;
906 
907     for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
908 #ifndef CONFIG_LIVE_BLOCK_MIGRATION
909         if (i == MIGRATION_CAPABILITY_BLOCK) {
910             continue;
911         }
912 #endif
913         caps = g_malloc0(sizeof(*caps));
914         caps->capability = i;
915         caps->state = s->enabled_capabilities[i];
916         QAPI_LIST_APPEND(tail, caps);
917     }
918 
919     return head;
920 }
921 
922 MigrationParameters *qmp_query_migrate_parameters(Error **errp)
923 {
924     MigrationParameters *params;
925     MigrationState *s = migrate_get_current();
926 
927     /* TODO use QAPI_CLONE() instead of duplicating it inline */
928     params = g_malloc0(sizeof(*params));
929     params->has_compress_level = true;
930     params->compress_level = s->parameters.compress_level;
931     params->has_compress_threads = true;
932     params->compress_threads = s->parameters.compress_threads;
933     params->has_compress_wait_thread = true;
934     params->compress_wait_thread = s->parameters.compress_wait_thread;
935     params->has_decompress_threads = true;
936     params->decompress_threads = s->parameters.decompress_threads;
937     params->has_throttle_trigger_threshold = true;
938     params->throttle_trigger_threshold = s->parameters.throttle_trigger_threshold;
939     params->has_cpu_throttle_initial = true;
940     params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
941     params->has_cpu_throttle_increment = true;
942     params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
943     params->has_cpu_throttle_tailslow = true;
944     params->cpu_throttle_tailslow = s->parameters.cpu_throttle_tailslow;
945     params->tls_creds = g_strdup(s->parameters.tls_creds);
946     params->tls_hostname = g_strdup(s->parameters.tls_hostname);
947     params->tls_authz = g_strdup(s->parameters.tls_authz ?
948                                  s->parameters.tls_authz : "");
949     params->has_max_bandwidth = true;
950     params->max_bandwidth = s->parameters.max_bandwidth;
951     params->has_downtime_limit = true;
952     params->downtime_limit = s->parameters.downtime_limit;
953     params->has_x_checkpoint_delay = true;
954     params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
955     params->has_block_incremental = true;
956     params->block_incremental = s->parameters.block_incremental;
957     params->has_multifd_channels = true;
958     params->multifd_channels = s->parameters.multifd_channels;
959     params->has_multifd_compression = true;
960     params->multifd_compression = s->parameters.multifd_compression;
961     params->has_multifd_zlib_level = true;
962     params->multifd_zlib_level = s->parameters.multifd_zlib_level;
963     params->has_multifd_zstd_level = true;
964     params->multifd_zstd_level = s->parameters.multifd_zstd_level;
965     params->has_xbzrle_cache_size = true;
966     params->xbzrle_cache_size = s->parameters.xbzrle_cache_size;
967     params->has_max_postcopy_bandwidth = true;
968     params->max_postcopy_bandwidth = s->parameters.max_postcopy_bandwidth;
969     params->has_max_cpu_throttle = true;
970     params->max_cpu_throttle = s->parameters.max_cpu_throttle;
971     params->has_announce_initial = true;
972     params->announce_initial = s->parameters.announce_initial;
973     params->has_announce_max = true;
974     params->announce_max = s->parameters.announce_max;
975     params->has_announce_rounds = true;
976     params->announce_rounds = s->parameters.announce_rounds;
977     params->has_announce_step = true;
978     params->announce_step = s->parameters.announce_step;
979 
980     if (s->parameters.has_block_bitmap_mapping) {
981         params->has_block_bitmap_mapping = true;
982         params->block_bitmap_mapping =
983             QAPI_CLONE(BitmapMigrationNodeAliasList,
984                        s->parameters.block_bitmap_mapping);
985     }
986 
987     return params;
988 }
989 
990 void qmp_client_migrate_info(const char *protocol, const char *hostname,
991                              bool has_port, int64_t port,
992                              bool has_tls_port, int64_t tls_port,
993                              const char *cert_subject,
994                              Error **errp)
995 {
996     if (strcmp(protocol, "spice") == 0) {
997         if (!qemu_using_spice(errp)) {
998             return;
999         }
1000 
1001         if (!has_port && !has_tls_port) {
1002             error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
1003             return;
1004         }
1005 
1006         if (qemu_spice.migrate_info(hostname,
1007                                     has_port ? port : -1,
1008                                     has_tls_port ? tls_port : -1,
1009                                     cert_subject)) {
1010             error_setg(errp, "Could not set up display for migration");
1011             return;
1012         }
1013         return;
1014     }
1015 
1016     error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "'spice'");
1017 }
1018 
1019 AnnounceParameters *migrate_announce_params(void)
1020 {
1021     static AnnounceParameters ap;
1022 
1023     MigrationState *s = migrate_get_current();
1024 
1025     ap.initial = s->parameters.announce_initial;
1026     ap.max = s->parameters.announce_max;
1027     ap.rounds = s->parameters.announce_rounds;
1028     ap.step = s->parameters.announce_step;
1029 
1030     return &ap;
1031 }
1032 
1033 /*
1034  * Return true if we're already in the middle of a migration
1035  * (i.e. any of the active or setup states)
1036  */
1037 bool migration_is_setup_or_active(int state)
1038 {
1039     switch (state) {
1040     case MIGRATION_STATUS_ACTIVE:
1041     case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1042     case MIGRATION_STATUS_POSTCOPY_PAUSED:
1043     case MIGRATION_STATUS_POSTCOPY_RECOVER:
1044     case MIGRATION_STATUS_SETUP:
1045     case MIGRATION_STATUS_PRE_SWITCHOVER:
1046     case MIGRATION_STATUS_DEVICE:
1047     case MIGRATION_STATUS_WAIT_UNPLUG:
1048     case MIGRATION_STATUS_COLO:
1049         return true;
1050 
1051     default:
1052         return false;
1053 
1054     }
1055 }
1056 
1057 bool migration_is_running(int state)
1058 {
1059     switch (state) {
1060     case MIGRATION_STATUS_ACTIVE:
1061     case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1062     case MIGRATION_STATUS_POSTCOPY_PAUSED:
1063     case MIGRATION_STATUS_POSTCOPY_RECOVER:
1064     case MIGRATION_STATUS_SETUP:
1065     case MIGRATION_STATUS_PRE_SWITCHOVER:
1066     case MIGRATION_STATUS_DEVICE:
1067     case MIGRATION_STATUS_WAIT_UNPLUG:
1068     case MIGRATION_STATUS_CANCELLING:
1069         return true;
1070 
1071     default:
1072         return false;
1073 
1074     }
1075 }
1076 
1077 static bool migrate_show_downtime(MigrationState *s)
1078 {
1079     return (s->state == MIGRATION_STATUS_COMPLETED) || migration_in_postcopy();
1080 }
1081 
1082 static void populate_time_info(MigrationInfo *info, MigrationState *s)
1083 {
1084     info->has_status = true;
1085     info->has_setup_time = true;
1086     info->setup_time = s->setup_time;
1087 
1088     if (s->state == MIGRATION_STATUS_COMPLETED) {
1089         info->has_total_time = true;
1090         info->total_time = s->total_time;
1091     } else {
1092         info->has_total_time = true;
1093         info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
1094                            s->start_time;
1095     }
1096 
1097     if (migrate_show_downtime(s)) {
1098         info->has_downtime = true;
1099         info->downtime = s->downtime;
1100     } else {
1101         info->has_expected_downtime = true;
1102         info->expected_downtime = s->expected_downtime;
1103     }
1104 }
1105 
1106 static void populate_ram_info(MigrationInfo *info, MigrationState *s)
1107 {
1108     size_t page_size = qemu_target_page_size();
1109 
1110     info->ram = g_malloc0(sizeof(*info->ram));
1111     info->ram->transferred = stat64_get(&ram_atomic_counters.transferred);
1112     info->ram->total = ram_bytes_total();
1113     info->ram->duplicate = stat64_get(&ram_atomic_counters.duplicate);
1114     /* legacy value.  It is not used anymore */
1115     info->ram->skipped = 0;
1116     info->ram->normal = stat64_get(&ram_atomic_counters.normal);
1117     info->ram->normal_bytes = info->ram->normal * page_size;
1118     info->ram->mbps = s->mbps;
1119     info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
1120     info->ram->dirty_sync_missed_zero_copy =
1121             ram_counters.dirty_sync_missed_zero_copy;
1122     info->ram->postcopy_requests = ram_counters.postcopy_requests;
1123     info->ram->page_size = page_size;
1124     info->ram->multifd_bytes = ram_counters.multifd_bytes;
1125     info->ram->pages_per_second = s->pages_per_second;
1126     info->ram->precopy_bytes = ram_counters.precopy_bytes;
1127     info->ram->downtime_bytes = ram_counters.downtime_bytes;
1128     info->ram->postcopy_bytes = stat64_get(&ram_atomic_counters.postcopy_bytes);
1129 
1130     if (migrate_use_xbzrle()) {
1131         info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
1132         info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
1133         info->xbzrle_cache->bytes = xbzrle_counters.bytes;
1134         info->xbzrle_cache->pages = xbzrle_counters.pages;
1135         info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
1136         info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
1137         info->xbzrle_cache->encoding_rate = xbzrle_counters.encoding_rate;
1138         info->xbzrle_cache->overflow = xbzrle_counters.overflow;
1139     }
1140 
1141     if (migrate_use_compression()) {
1142         info->compression = g_malloc0(sizeof(*info->compression));
1143         info->compression->pages = compression_counters.pages;
1144         info->compression->busy = compression_counters.busy;
1145         info->compression->busy_rate = compression_counters.busy_rate;
1146         info->compression->compressed_size =
1147                                     compression_counters.compressed_size;
1148         info->compression->compression_rate =
1149                                     compression_counters.compression_rate;
1150     }
1151 
1152     if (cpu_throttle_active()) {
1153         info->has_cpu_throttle_percentage = true;
1154         info->cpu_throttle_percentage = cpu_throttle_get_percentage();
1155     }
1156 
1157     if (s->state != MIGRATION_STATUS_COMPLETED) {
1158         info->ram->remaining = ram_bytes_remaining();
1159         info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
1160     }
1161 }
1162 
1163 static void populate_disk_info(MigrationInfo *info)
1164 {
1165     if (blk_mig_active()) {
1166         info->disk = g_malloc0(sizeof(*info->disk));
1167         info->disk->transferred = blk_mig_bytes_transferred();
1168         info->disk->remaining = blk_mig_bytes_remaining();
1169         info->disk->total = blk_mig_bytes_total();
1170     }
1171 }
1172 
1173 static void fill_source_migration_info(MigrationInfo *info)
1174 {
1175     MigrationState *s = migrate_get_current();
1176     int state = qatomic_read(&s->state);
1177     GSList *cur_blocker = migration_blockers;
1178 
1179     info->blocked_reasons = NULL;
1180 
1181     /*
1182      * There are two types of reasons a migration might be blocked;
1183      * a) devices marked in VMState as non-migratable, and
1184      * b) Explicit migration blockers
1185      * We need to add both of them here.
1186      */
1187     qemu_savevm_non_migratable_list(&info->blocked_reasons);
1188 
1189     while (cur_blocker) {
1190         QAPI_LIST_PREPEND(info->blocked_reasons,
1191                           g_strdup(error_get_pretty(cur_blocker->data)));
1192         cur_blocker = g_slist_next(cur_blocker);
1193     }
1194     info->has_blocked_reasons = info->blocked_reasons != NULL;
1195 
1196     switch (state) {
1197     case MIGRATION_STATUS_NONE:
1198         /* no migration has happened ever */
1199         /* do not overwrite destination migration status */
1200         return;
1201     case MIGRATION_STATUS_SETUP:
1202         info->has_status = true;
1203         info->has_total_time = false;
1204         break;
1205     case MIGRATION_STATUS_ACTIVE:
1206     case MIGRATION_STATUS_CANCELLING:
1207     case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1208     case MIGRATION_STATUS_PRE_SWITCHOVER:
1209     case MIGRATION_STATUS_DEVICE:
1210     case MIGRATION_STATUS_POSTCOPY_PAUSED:
1211     case MIGRATION_STATUS_POSTCOPY_RECOVER:
1212         /* TODO add some postcopy stats */
1213         populate_time_info(info, s);
1214         populate_ram_info(info, s);
1215         populate_disk_info(info);
1216         populate_vfio_info(info);
1217         break;
1218     case MIGRATION_STATUS_COLO:
1219         info->has_status = true;
1220         /* TODO: display COLO specific information (checkpoint info etc.) */
1221         break;
1222     case MIGRATION_STATUS_COMPLETED:
1223         populate_time_info(info, s);
1224         populate_ram_info(info, s);
1225         populate_vfio_info(info);
1226         break;
1227     case MIGRATION_STATUS_FAILED:
1228         info->has_status = true;
1229         if (s->error) {
1230             info->error_desc = g_strdup(error_get_pretty(s->error));
1231         }
1232         break;
1233     case MIGRATION_STATUS_CANCELLED:
1234         info->has_status = true;
1235         break;
1236     case MIGRATION_STATUS_WAIT_UNPLUG:
1237         info->has_status = true;
1238         break;
1239     }
1240     info->status = state;
1241 }
1242 
1243 typedef enum WriteTrackingSupport {
1244     WT_SUPPORT_UNKNOWN = 0,
1245     WT_SUPPORT_ABSENT,
1246     WT_SUPPORT_AVAILABLE,
1247     WT_SUPPORT_COMPATIBLE
1248 } WriteTrackingSupport;
1249 
1250 static
1251 WriteTrackingSupport migrate_query_write_tracking(void)
1252 {
1253     /* Check if kernel supports required UFFD features */
1254     if (!ram_write_tracking_available()) {
1255         return WT_SUPPORT_ABSENT;
1256     }
1257     /*
1258      * Check if current memory configuration is
1259      * compatible with required UFFD features.
1260      */
1261     if (!ram_write_tracking_compatible()) {
1262         return WT_SUPPORT_AVAILABLE;
1263     }
1264 
1265     return WT_SUPPORT_COMPATIBLE;
1266 }
1267 
1268 /**
1269  * @migration_caps_check - check capability validity
1270  *
1271  * @cap_list: old capability list, array of bool
1272  * @params: new capabilities to be applied soon
1273  * @errp: set *errp if the check failed, with reason
1274  *
1275  * Returns true if check passed, otherwise false.
1276  */
1277 static bool migrate_caps_check(bool *cap_list,
1278                                MigrationCapabilityStatusList *params,
1279                                Error **errp)
1280 {
1281     MigrationCapabilityStatusList *cap;
1282     bool old_postcopy_cap;
1283     MigrationIncomingState *mis = migration_incoming_get_current();
1284 
1285     old_postcopy_cap = cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM];
1286 
1287     for (cap = params; cap; cap = cap->next) {
1288         cap_list[cap->value->capability] = cap->value->state;
1289     }
1290 
1291 #ifndef CONFIG_LIVE_BLOCK_MIGRATION
1292     if (cap_list[MIGRATION_CAPABILITY_BLOCK]) {
1293         error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
1294                    "block migration");
1295         error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
1296         return false;
1297     }
1298 #endif
1299 
1300 #ifndef CONFIG_REPLICATION
1301     if (cap_list[MIGRATION_CAPABILITY_X_COLO]) {
1302         error_setg(errp, "QEMU compiled without replication module"
1303                    " can't enable COLO");
1304         error_append_hint(errp, "Please enable replication before COLO.\n");
1305         return false;
1306     }
1307 #endif
1308 
1309     if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
1310         /* This check is reasonably expensive, so only when it's being
1311          * set the first time, also it's only the destination that needs
1312          * special support.
1313          */
1314         if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
1315             !postcopy_ram_supported_by_host(mis)) {
1316             /* postcopy_ram_supported_by_host will have emitted a more
1317              * detailed message
1318              */
1319             error_setg(errp, "Postcopy is not supported");
1320             return false;
1321         }
1322 
1323         if (cap_list[MIGRATION_CAPABILITY_X_IGNORE_SHARED]) {
1324             error_setg(errp, "Postcopy is not compatible with ignore-shared");
1325             return false;
1326         }
1327     }
1328 
1329     if (cap_list[MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT]) {
1330         WriteTrackingSupport wt_support;
1331         int idx;
1332         /*
1333          * Check if 'background-snapshot' capability is supported by
1334          * host kernel and compatible with guest memory configuration.
1335          */
1336         wt_support = migrate_query_write_tracking();
1337         if (wt_support < WT_SUPPORT_AVAILABLE) {
1338             error_setg(errp, "Background-snapshot is not supported by host kernel");
1339             return false;
1340         }
1341         if (wt_support < WT_SUPPORT_COMPATIBLE) {
1342             error_setg(errp, "Background-snapshot is not compatible "
1343                     "with guest memory configuration");
1344             return false;
1345         }
1346 
1347         /*
1348          * Check if there are any migration capabilities
1349          * incompatible with 'background-snapshot'.
1350          */
1351         for (idx = 0; idx < check_caps_background_snapshot.size; idx++) {
1352             int incomp_cap = check_caps_background_snapshot.caps[idx];
1353             if (cap_list[incomp_cap]) {
1354                 error_setg(errp,
1355                         "Background-snapshot is not compatible with %s",
1356                         MigrationCapability_str(incomp_cap));
1357                 return false;
1358             }
1359         }
1360     }
1361 
1362 #ifdef CONFIG_LINUX
1363     if (cap_list[MIGRATION_CAPABILITY_ZERO_COPY_SEND] &&
1364         (!cap_list[MIGRATION_CAPABILITY_MULTIFD] ||
1365          cap_list[MIGRATION_CAPABILITY_COMPRESS] ||
1366          cap_list[MIGRATION_CAPABILITY_XBZRLE] ||
1367          migrate_multifd_compression() ||
1368          migrate_use_tls())) {
1369         error_setg(errp,
1370                    "Zero copy only available for non-compressed non-TLS multifd migration");
1371         return false;
1372     }
1373 #else
1374     if (cap_list[MIGRATION_CAPABILITY_ZERO_COPY_SEND]) {
1375         error_setg(errp,
1376                    "Zero copy currently only available on Linux");
1377         return false;
1378     }
1379 #endif
1380 
1381 
1382     /* incoming side only */
1383     if (runstate_check(RUN_STATE_INMIGRATE) &&
1384         !migrate_multi_channels_is_allowed() &&
1385         cap_list[MIGRATION_CAPABILITY_MULTIFD]) {
1386         error_setg(errp, "multifd is not supported by current protocol");
1387         return false;
1388     }
1389 
1390     if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_PREEMPT]) {
1391         if (!cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
1392             error_setg(errp, "Postcopy preempt requires postcopy-ram");
1393             return false;
1394         }
1395 
1396         /*
1397          * Preempt mode requires urgent pages to be sent in separate
1398          * channel, OTOH compression logic will disorder all pages into
1399          * different compression channels, which is not compatible with the
1400          * preempt assumptions on channel assignments.
1401          */
1402         if (cap_list[MIGRATION_CAPABILITY_COMPRESS]) {
1403             error_setg(errp, "Postcopy preempt not compatible with compress");
1404             return false;
1405         }
1406     }
1407 
1408     if (cap_list[MIGRATION_CAPABILITY_MULTIFD]) {
1409         if (cap_list[MIGRATION_CAPABILITY_COMPRESS]) {
1410             error_setg(errp, "Multifd is not compatible with compress");
1411             return false;
1412         }
1413     }
1414 
1415     return true;
1416 }
1417 
1418 static void fill_destination_migration_info(MigrationInfo *info)
1419 {
1420     MigrationIncomingState *mis = migration_incoming_get_current();
1421 
1422     if (mis->socket_address_list) {
1423         info->has_socket_address = true;
1424         info->socket_address =
1425             QAPI_CLONE(SocketAddressList, mis->socket_address_list);
1426     }
1427 
1428     switch (mis->state) {
1429     case MIGRATION_STATUS_NONE:
1430         return;
1431     case MIGRATION_STATUS_SETUP:
1432     case MIGRATION_STATUS_CANCELLING:
1433     case MIGRATION_STATUS_CANCELLED:
1434     case MIGRATION_STATUS_ACTIVE:
1435     case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1436     case MIGRATION_STATUS_POSTCOPY_PAUSED:
1437     case MIGRATION_STATUS_POSTCOPY_RECOVER:
1438     case MIGRATION_STATUS_FAILED:
1439     case MIGRATION_STATUS_COLO:
1440         info->has_status = true;
1441         break;
1442     case MIGRATION_STATUS_COMPLETED:
1443         info->has_status = true;
1444         fill_destination_postcopy_migration_info(info);
1445         break;
1446     }
1447     info->status = mis->state;
1448 }
1449 
1450 MigrationInfo *qmp_query_migrate(Error **errp)
1451 {
1452     MigrationInfo *info = g_malloc0(sizeof(*info));
1453 
1454     fill_destination_migration_info(info);
1455     fill_source_migration_info(info);
1456 
1457     return info;
1458 }
1459 
1460 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
1461                                   Error **errp)
1462 {
1463     MigrationState *s = migrate_get_current();
1464     MigrationCapabilityStatusList *cap;
1465     bool cap_list[MIGRATION_CAPABILITY__MAX];
1466 
1467     if (migration_is_running(s->state)) {
1468         error_setg(errp, QERR_MIGRATION_ACTIVE);
1469         return;
1470     }
1471 
1472     memcpy(cap_list, s->enabled_capabilities, sizeof(cap_list));
1473     if (!migrate_caps_check(cap_list, params, errp)) {
1474         return;
1475     }
1476 
1477     for (cap = params; cap; cap = cap->next) {
1478         s->enabled_capabilities[cap->value->capability] = cap->value->state;
1479     }
1480 }
1481 
1482 /*
1483  * Check whether the parameters are valid. Error will be put into errp
1484  * (if provided). Return true if valid, otherwise false.
1485  */
1486 static bool migrate_params_check(MigrationParameters *params, Error **errp)
1487 {
1488     if (params->has_compress_level &&
1489         (params->compress_level > 9)) {
1490         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
1491                    "a value between 0 and 9");
1492         return false;
1493     }
1494 
1495     if (params->has_compress_threads && (params->compress_threads < 1)) {
1496         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1497                    "compress_threads",
1498                    "a value between 1 and 255");
1499         return false;
1500     }
1501 
1502     if (params->has_decompress_threads && (params->decompress_threads < 1)) {
1503         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1504                    "decompress_threads",
1505                    "a value between 1 and 255");
1506         return false;
1507     }
1508 
1509     if (params->has_throttle_trigger_threshold &&
1510         (params->throttle_trigger_threshold < 1 ||
1511          params->throttle_trigger_threshold > 100)) {
1512         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1513                    "throttle_trigger_threshold",
1514                    "an integer in the range of 1 to 100");
1515         return false;
1516     }
1517 
1518     if (params->has_cpu_throttle_initial &&
1519         (params->cpu_throttle_initial < 1 ||
1520          params->cpu_throttle_initial > 99)) {
1521         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1522                    "cpu_throttle_initial",
1523                    "an integer in the range of 1 to 99");
1524         return false;
1525     }
1526 
1527     if (params->has_cpu_throttle_increment &&
1528         (params->cpu_throttle_increment < 1 ||
1529          params->cpu_throttle_increment > 99)) {
1530         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1531                    "cpu_throttle_increment",
1532                    "an integer in the range of 1 to 99");
1533         return false;
1534     }
1535 
1536     if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) {
1537         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1538                    "max_bandwidth",
1539                    "an integer in the range of 0 to "stringify(SIZE_MAX)
1540                    " bytes/second");
1541         return false;
1542     }
1543 
1544     if (params->has_downtime_limit &&
1545         (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
1546         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1547                    "downtime_limit",
1548                    "an integer in the range of 0 to "
1549                     stringify(MAX_MIGRATE_DOWNTIME)" ms");
1550         return false;
1551     }
1552 
1553     /* x_checkpoint_delay is now always positive */
1554 
1555     if (params->has_multifd_channels && (params->multifd_channels < 1)) {
1556         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1557                    "multifd_channels",
1558                    "a value between 1 and 255");
1559         return false;
1560     }
1561 
1562     if (params->has_multifd_zlib_level &&
1563         (params->multifd_zlib_level > 9)) {
1564         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zlib_level",
1565                    "a value between 0 and 9");
1566         return false;
1567     }
1568 
1569     if (params->has_multifd_zstd_level &&
1570         (params->multifd_zstd_level > 20)) {
1571         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zstd_level",
1572                    "a value between 0 and 20");
1573         return false;
1574     }
1575 
1576     if (params->has_xbzrle_cache_size &&
1577         (params->xbzrle_cache_size < qemu_target_page_size() ||
1578          !is_power_of_2(params->xbzrle_cache_size))) {
1579         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1580                    "xbzrle_cache_size",
1581                    "a power of two no less than the target page size");
1582         return false;
1583     }
1584 
1585     if (params->has_max_cpu_throttle &&
1586         (params->max_cpu_throttle < params->cpu_throttle_initial ||
1587          params->max_cpu_throttle > 99)) {
1588         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1589                    "max_cpu_throttle",
1590                    "an integer in the range of cpu_throttle_initial to 99");
1591         return false;
1592     }
1593 
1594     if (params->has_announce_initial &&
1595         params->announce_initial > 100000) {
1596         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1597                    "announce_initial",
1598                    "a value between 0 and 100000");
1599         return false;
1600     }
1601     if (params->has_announce_max &&
1602         params->announce_max > 100000) {
1603         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1604                    "announce_max",
1605                    "a value between 0 and 100000");
1606        return false;
1607     }
1608     if (params->has_announce_rounds &&
1609         params->announce_rounds > 1000) {
1610         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1611                    "announce_rounds",
1612                    "a value between 0 and 1000");
1613        return false;
1614     }
1615     if (params->has_announce_step &&
1616         (params->announce_step < 1 ||
1617         params->announce_step > 10000)) {
1618         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1619                    "announce_step",
1620                    "a value between 0 and 10000");
1621        return false;
1622     }
1623 
1624     if (params->has_block_bitmap_mapping &&
1625         !check_dirty_bitmap_mig_alias_map(params->block_bitmap_mapping, errp)) {
1626         error_prepend(errp, "Invalid mapping given for block-bitmap-mapping: ");
1627         return false;
1628     }
1629 
1630 #ifdef CONFIG_LINUX
1631     if (migrate_use_zero_copy_send() &&
1632         ((params->has_multifd_compression && params->multifd_compression) ||
1633          (params->tls_creds && *params->tls_creds))) {
1634         error_setg(errp,
1635                    "Zero copy only available for non-compressed non-TLS multifd migration");
1636         return false;
1637     }
1638 #endif
1639 
1640     return true;
1641 }
1642 
1643 static void migrate_params_test_apply(MigrateSetParameters *params,
1644                                       MigrationParameters *dest)
1645 {
1646     *dest = migrate_get_current()->parameters;
1647 
1648     /* TODO use QAPI_CLONE() instead of duplicating it inline */
1649 
1650     if (params->has_compress_level) {
1651         dest->compress_level = params->compress_level;
1652     }
1653 
1654     if (params->has_compress_threads) {
1655         dest->compress_threads = params->compress_threads;
1656     }
1657 
1658     if (params->has_compress_wait_thread) {
1659         dest->compress_wait_thread = params->compress_wait_thread;
1660     }
1661 
1662     if (params->has_decompress_threads) {
1663         dest->decompress_threads = params->decompress_threads;
1664     }
1665 
1666     if (params->has_throttle_trigger_threshold) {
1667         dest->throttle_trigger_threshold = params->throttle_trigger_threshold;
1668     }
1669 
1670     if (params->has_cpu_throttle_initial) {
1671         dest->cpu_throttle_initial = params->cpu_throttle_initial;
1672     }
1673 
1674     if (params->has_cpu_throttle_increment) {
1675         dest->cpu_throttle_increment = params->cpu_throttle_increment;
1676     }
1677 
1678     if (params->has_cpu_throttle_tailslow) {
1679         dest->cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1680     }
1681 
1682     if (params->tls_creds) {
1683         assert(params->tls_creds->type == QTYPE_QSTRING);
1684         dest->tls_creds = params->tls_creds->u.s;
1685     }
1686 
1687     if (params->tls_hostname) {
1688         assert(params->tls_hostname->type == QTYPE_QSTRING);
1689         dest->tls_hostname = params->tls_hostname->u.s;
1690     }
1691 
1692     if (params->has_max_bandwidth) {
1693         dest->max_bandwidth = params->max_bandwidth;
1694     }
1695 
1696     if (params->has_downtime_limit) {
1697         dest->downtime_limit = params->downtime_limit;
1698     }
1699 
1700     if (params->has_x_checkpoint_delay) {
1701         dest->x_checkpoint_delay = params->x_checkpoint_delay;
1702     }
1703 
1704     if (params->has_block_incremental) {
1705         dest->block_incremental = params->block_incremental;
1706     }
1707     if (params->has_multifd_channels) {
1708         dest->multifd_channels = params->multifd_channels;
1709     }
1710     if (params->has_multifd_compression) {
1711         dest->multifd_compression = params->multifd_compression;
1712     }
1713     if (params->has_xbzrle_cache_size) {
1714         dest->xbzrle_cache_size = params->xbzrle_cache_size;
1715     }
1716     if (params->has_max_postcopy_bandwidth) {
1717         dest->max_postcopy_bandwidth = params->max_postcopy_bandwidth;
1718     }
1719     if (params->has_max_cpu_throttle) {
1720         dest->max_cpu_throttle = params->max_cpu_throttle;
1721     }
1722     if (params->has_announce_initial) {
1723         dest->announce_initial = params->announce_initial;
1724     }
1725     if (params->has_announce_max) {
1726         dest->announce_max = params->announce_max;
1727     }
1728     if (params->has_announce_rounds) {
1729         dest->announce_rounds = params->announce_rounds;
1730     }
1731     if (params->has_announce_step) {
1732         dest->announce_step = params->announce_step;
1733     }
1734 
1735     if (params->has_block_bitmap_mapping) {
1736         dest->has_block_bitmap_mapping = true;
1737         dest->block_bitmap_mapping = params->block_bitmap_mapping;
1738     }
1739 }
1740 
1741 static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
1742 {
1743     MigrationState *s = migrate_get_current();
1744 
1745     /* TODO use QAPI_CLONE() instead of duplicating it inline */
1746 
1747     if (params->has_compress_level) {
1748         s->parameters.compress_level = params->compress_level;
1749     }
1750 
1751     if (params->has_compress_threads) {
1752         s->parameters.compress_threads = params->compress_threads;
1753     }
1754 
1755     if (params->has_compress_wait_thread) {
1756         s->parameters.compress_wait_thread = params->compress_wait_thread;
1757     }
1758 
1759     if (params->has_decompress_threads) {
1760         s->parameters.decompress_threads = params->decompress_threads;
1761     }
1762 
1763     if (params->has_throttle_trigger_threshold) {
1764         s->parameters.throttle_trigger_threshold = params->throttle_trigger_threshold;
1765     }
1766 
1767     if (params->has_cpu_throttle_initial) {
1768         s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
1769     }
1770 
1771     if (params->has_cpu_throttle_increment) {
1772         s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
1773     }
1774 
1775     if (params->has_cpu_throttle_tailslow) {
1776         s->parameters.cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1777     }
1778 
1779     if (params->tls_creds) {
1780         g_free(s->parameters.tls_creds);
1781         assert(params->tls_creds->type == QTYPE_QSTRING);
1782         s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
1783     }
1784 
1785     if (params->tls_hostname) {
1786         g_free(s->parameters.tls_hostname);
1787         assert(params->tls_hostname->type == QTYPE_QSTRING);
1788         s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
1789     }
1790 
1791     if (params->tls_authz) {
1792         g_free(s->parameters.tls_authz);
1793         assert(params->tls_authz->type == QTYPE_QSTRING);
1794         s->parameters.tls_authz = g_strdup(params->tls_authz->u.s);
1795     }
1796 
1797     if (params->has_max_bandwidth) {
1798         s->parameters.max_bandwidth = params->max_bandwidth;
1799         if (s->to_dst_file && !migration_in_postcopy()) {
1800             qemu_file_set_rate_limit(s->to_dst_file,
1801                                 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
1802         }
1803     }
1804 
1805     if (params->has_downtime_limit) {
1806         s->parameters.downtime_limit = params->downtime_limit;
1807     }
1808 
1809     if (params->has_x_checkpoint_delay) {
1810         s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
1811         if (migration_in_colo_state()) {
1812             colo_checkpoint_notify(s);
1813         }
1814     }
1815 
1816     if (params->has_block_incremental) {
1817         s->parameters.block_incremental = params->block_incremental;
1818     }
1819     if (params->has_multifd_channels) {
1820         s->parameters.multifd_channels = params->multifd_channels;
1821     }
1822     if (params->has_multifd_compression) {
1823         s->parameters.multifd_compression = params->multifd_compression;
1824     }
1825     if (params->has_xbzrle_cache_size) {
1826         s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
1827         xbzrle_cache_resize(params->xbzrle_cache_size, errp);
1828     }
1829     if (params->has_max_postcopy_bandwidth) {
1830         s->parameters.max_postcopy_bandwidth = params->max_postcopy_bandwidth;
1831         if (s->to_dst_file && migration_in_postcopy()) {
1832             qemu_file_set_rate_limit(s->to_dst_file,
1833                     s->parameters.max_postcopy_bandwidth / XFER_LIMIT_RATIO);
1834         }
1835     }
1836     if (params->has_max_cpu_throttle) {
1837         s->parameters.max_cpu_throttle = params->max_cpu_throttle;
1838     }
1839     if (params->has_announce_initial) {
1840         s->parameters.announce_initial = params->announce_initial;
1841     }
1842     if (params->has_announce_max) {
1843         s->parameters.announce_max = params->announce_max;
1844     }
1845     if (params->has_announce_rounds) {
1846         s->parameters.announce_rounds = params->announce_rounds;
1847     }
1848     if (params->has_announce_step) {
1849         s->parameters.announce_step = params->announce_step;
1850     }
1851 
1852     if (params->has_block_bitmap_mapping) {
1853         qapi_free_BitmapMigrationNodeAliasList(
1854             s->parameters.block_bitmap_mapping);
1855 
1856         s->parameters.has_block_bitmap_mapping = true;
1857         s->parameters.block_bitmap_mapping =
1858             QAPI_CLONE(BitmapMigrationNodeAliasList,
1859                        params->block_bitmap_mapping);
1860     }
1861 }
1862 
1863 void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
1864 {
1865     MigrationParameters tmp;
1866 
1867     /* TODO Rewrite "" to null instead */
1868     if (params->tls_creds
1869         && params->tls_creds->type == QTYPE_QNULL) {
1870         qobject_unref(params->tls_creds->u.n);
1871         params->tls_creds->type = QTYPE_QSTRING;
1872         params->tls_creds->u.s = strdup("");
1873     }
1874     /* TODO Rewrite "" to null instead */
1875     if (params->tls_hostname
1876         && params->tls_hostname->type == QTYPE_QNULL) {
1877         qobject_unref(params->tls_hostname->u.n);
1878         params->tls_hostname->type = QTYPE_QSTRING;
1879         params->tls_hostname->u.s = strdup("");
1880     }
1881 
1882     migrate_params_test_apply(params, &tmp);
1883 
1884     if (!migrate_params_check(&tmp, errp)) {
1885         /* Invalid parameter */
1886         return;
1887     }
1888 
1889     migrate_params_apply(params, errp);
1890 }
1891 
1892 
1893 void qmp_migrate_start_postcopy(Error **errp)
1894 {
1895     MigrationState *s = migrate_get_current();
1896 
1897     if (!migrate_postcopy()) {
1898         error_setg(errp, "Enable postcopy with migrate_set_capability before"
1899                          " the start of migration");
1900         return;
1901     }
1902 
1903     if (s->state == MIGRATION_STATUS_NONE) {
1904         error_setg(errp, "Postcopy must be started after migration has been"
1905                          " started");
1906         return;
1907     }
1908     /*
1909      * we don't error if migration has finished since that would be racy
1910      * with issuing this command.
1911      */
1912     qatomic_set(&s->start_postcopy, true);
1913 }
1914 
1915 /* shared migration helpers */
1916 
1917 void migrate_set_state(int *state, int old_state, int new_state)
1918 {
1919     assert(new_state < MIGRATION_STATUS__MAX);
1920     if (qatomic_cmpxchg(state, old_state, new_state) == old_state) {
1921         trace_migrate_set_state(MigrationStatus_str(new_state));
1922         migrate_generate_event(new_state);
1923     }
1924 }
1925 
1926 static MigrationCapabilityStatus *migrate_cap_add(MigrationCapability index,
1927                                                   bool state)
1928 {
1929     MigrationCapabilityStatus *cap;
1930 
1931     cap = g_new0(MigrationCapabilityStatus, 1);
1932     cap->capability = index;
1933     cap->state = state;
1934 
1935     return cap;
1936 }
1937 
1938 void migrate_set_block_enabled(bool value, Error **errp)
1939 {
1940     MigrationCapabilityStatusList *cap = NULL;
1941 
1942     QAPI_LIST_PREPEND(cap, migrate_cap_add(MIGRATION_CAPABILITY_BLOCK, value));
1943     qmp_migrate_set_capabilities(cap, errp);
1944     qapi_free_MigrationCapabilityStatusList(cap);
1945 }
1946 
1947 static void migrate_set_block_incremental(MigrationState *s, bool value)
1948 {
1949     s->parameters.block_incremental = value;
1950 }
1951 
1952 static void block_cleanup_parameters(MigrationState *s)
1953 {
1954     if (s->must_remove_block_options) {
1955         /* setting to false can never fail */
1956         migrate_set_block_enabled(false, &error_abort);
1957         migrate_set_block_incremental(s, false);
1958         s->must_remove_block_options = false;
1959     }
1960 }
1961 
1962 static void migrate_fd_cleanup(MigrationState *s)
1963 {
1964     qemu_bh_delete(s->cleanup_bh);
1965     s->cleanup_bh = NULL;
1966 
1967     g_free(s->hostname);
1968     s->hostname = NULL;
1969     json_writer_free(s->vmdesc);
1970     s->vmdesc = NULL;
1971 
1972     qemu_savevm_state_cleanup();
1973 
1974     if (s->to_dst_file) {
1975         QEMUFile *tmp;
1976 
1977         trace_migrate_fd_cleanup();
1978         qemu_mutex_unlock_iothread();
1979         if (s->migration_thread_running) {
1980             qemu_thread_join(&s->thread);
1981             s->migration_thread_running = false;
1982         }
1983         qemu_mutex_lock_iothread();
1984 
1985         multifd_save_cleanup();
1986         qemu_mutex_lock(&s->qemu_file_lock);
1987         tmp = s->to_dst_file;
1988         s->to_dst_file = NULL;
1989         qemu_mutex_unlock(&s->qemu_file_lock);
1990         /*
1991          * Close the file handle without the lock to make sure the
1992          * critical section won't block for long.
1993          */
1994         migration_ioc_unregister_yank_from_file(tmp);
1995         qemu_fclose(tmp);
1996     }
1997 
1998     if (s->postcopy_qemufile_src) {
1999         migration_ioc_unregister_yank_from_file(s->postcopy_qemufile_src);
2000         qemu_fclose(s->postcopy_qemufile_src);
2001         s->postcopy_qemufile_src = NULL;
2002     }
2003 
2004     assert(!migration_is_active(s));
2005 
2006     if (s->state == MIGRATION_STATUS_CANCELLING) {
2007         migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
2008                           MIGRATION_STATUS_CANCELLED);
2009     }
2010 
2011     if (s->error) {
2012         /* It is used on info migrate.  We can't free it */
2013         error_report_err(error_copy(s->error));
2014     }
2015     notifier_list_notify(&migration_state_notifiers, s);
2016     block_cleanup_parameters(s);
2017     yank_unregister_instance(MIGRATION_YANK_INSTANCE);
2018 }
2019 
2020 static void migrate_fd_cleanup_schedule(MigrationState *s)
2021 {
2022     /*
2023      * Ref the state for bh, because it may be called when
2024      * there're already no other refs
2025      */
2026     object_ref(OBJECT(s));
2027     qemu_bh_schedule(s->cleanup_bh);
2028 }
2029 
2030 static void migrate_fd_cleanup_bh(void *opaque)
2031 {
2032     MigrationState *s = opaque;
2033     migrate_fd_cleanup(s);
2034     object_unref(OBJECT(s));
2035 }
2036 
2037 void migrate_set_error(MigrationState *s, const Error *error)
2038 {
2039     QEMU_LOCK_GUARD(&s->error_mutex);
2040     if (!s->error) {
2041         s->error = error_copy(error);
2042     }
2043 }
2044 
2045 static void migrate_error_free(MigrationState *s)
2046 {
2047     QEMU_LOCK_GUARD(&s->error_mutex);
2048     if (s->error) {
2049         error_free(s->error);
2050         s->error = NULL;
2051     }
2052 }
2053 
2054 void migrate_fd_error(MigrationState *s, const Error *error)
2055 {
2056     trace_migrate_fd_error(error_get_pretty(error));
2057     assert(s->to_dst_file == NULL);
2058     migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2059                       MIGRATION_STATUS_FAILED);
2060     migrate_set_error(s, error);
2061 }
2062 
2063 static void migrate_fd_cancel(MigrationState *s)
2064 {
2065     int old_state ;
2066     QEMUFile *f = migrate_get_current()->to_dst_file;
2067     trace_migrate_fd_cancel();
2068 
2069     WITH_QEMU_LOCK_GUARD(&s->qemu_file_lock) {
2070         if (s->rp_state.from_dst_file) {
2071             /* shutdown the rp socket, so causing the rp thread to shutdown */
2072             qemu_file_shutdown(s->rp_state.from_dst_file);
2073         }
2074     }
2075 
2076     do {
2077         old_state = s->state;
2078         if (!migration_is_running(old_state)) {
2079             break;
2080         }
2081         /* If the migration is paused, kick it out of the pause */
2082         if (old_state == MIGRATION_STATUS_PRE_SWITCHOVER) {
2083             qemu_sem_post(&s->pause_sem);
2084         }
2085         migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
2086     } while (s->state != MIGRATION_STATUS_CANCELLING);
2087 
2088     /*
2089      * If we're unlucky the migration code might be stuck somewhere in a
2090      * send/write while the network has failed and is waiting to timeout;
2091      * if we've got shutdown(2) available then we can force it to quit.
2092      * The outgoing qemu file gets closed in migrate_fd_cleanup that is
2093      * called in a bh, so there is no race against this cancel.
2094      */
2095     if (s->state == MIGRATION_STATUS_CANCELLING && f) {
2096         qemu_file_shutdown(f);
2097     }
2098     if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
2099         Error *local_err = NULL;
2100 
2101         bdrv_activate_all(&local_err);
2102         if (local_err) {
2103             error_report_err(local_err);
2104         } else {
2105             s->block_inactive = false;
2106         }
2107     }
2108 }
2109 
2110 void add_migration_state_change_notifier(Notifier *notify)
2111 {
2112     notifier_list_add(&migration_state_notifiers, notify);
2113 }
2114 
2115 void remove_migration_state_change_notifier(Notifier *notify)
2116 {
2117     notifier_remove(notify);
2118 }
2119 
2120 bool migration_in_setup(MigrationState *s)
2121 {
2122     return s->state == MIGRATION_STATUS_SETUP;
2123 }
2124 
2125 bool migration_has_finished(MigrationState *s)
2126 {
2127     return s->state == MIGRATION_STATUS_COMPLETED;
2128 }
2129 
2130 bool migration_has_failed(MigrationState *s)
2131 {
2132     return (s->state == MIGRATION_STATUS_CANCELLED ||
2133             s->state == MIGRATION_STATUS_FAILED);
2134 }
2135 
2136 bool migration_in_postcopy(void)
2137 {
2138     MigrationState *s = migrate_get_current();
2139 
2140     switch (s->state) {
2141     case MIGRATION_STATUS_POSTCOPY_ACTIVE:
2142     case MIGRATION_STATUS_POSTCOPY_PAUSED:
2143     case MIGRATION_STATUS_POSTCOPY_RECOVER:
2144         return true;
2145     default:
2146         return false;
2147     }
2148 }
2149 
2150 bool migration_in_postcopy_after_devices(MigrationState *s)
2151 {
2152     return migration_in_postcopy() && s->postcopy_after_devices;
2153 }
2154 
2155 bool migration_in_incoming_postcopy(void)
2156 {
2157     PostcopyState ps = postcopy_state_get();
2158 
2159     return ps >= POSTCOPY_INCOMING_DISCARD && ps < POSTCOPY_INCOMING_END;
2160 }
2161 
2162 bool migration_incoming_postcopy_advised(void)
2163 {
2164     PostcopyState ps = postcopy_state_get();
2165 
2166     return ps >= POSTCOPY_INCOMING_ADVISE && ps < POSTCOPY_INCOMING_END;
2167 }
2168 
2169 bool migration_in_bg_snapshot(void)
2170 {
2171     MigrationState *s = migrate_get_current();
2172 
2173     return migrate_background_snapshot() &&
2174             migration_is_setup_or_active(s->state);
2175 }
2176 
2177 bool migration_is_idle(void)
2178 {
2179     MigrationState *s = current_migration;
2180 
2181     if (!s) {
2182         return true;
2183     }
2184 
2185     switch (s->state) {
2186     case MIGRATION_STATUS_NONE:
2187     case MIGRATION_STATUS_CANCELLED:
2188     case MIGRATION_STATUS_COMPLETED:
2189     case MIGRATION_STATUS_FAILED:
2190         return true;
2191     case MIGRATION_STATUS_SETUP:
2192     case MIGRATION_STATUS_CANCELLING:
2193     case MIGRATION_STATUS_ACTIVE:
2194     case MIGRATION_STATUS_POSTCOPY_ACTIVE:
2195     case MIGRATION_STATUS_COLO:
2196     case MIGRATION_STATUS_PRE_SWITCHOVER:
2197     case MIGRATION_STATUS_DEVICE:
2198     case MIGRATION_STATUS_WAIT_UNPLUG:
2199         return false;
2200     case MIGRATION_STATUS__MAX:
2201         g_assert_not_reached();
2202     }
2203 
2204     return false;
2205 }
2206 
2207 bool migration_is_active(MigrationState *s)
2208 {
2209     return (s->state == MIGRATION_STATUS_ACTIVE ||
2210             s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
2211 }
2212 
2213 void migrate_init(MigrationState *s)
2214 {
2215     /*
2216      * Reinitialise all migration state, except
2217      * parameters/capabilities that the user set, and
2218      * locks.
2219      */
2220     s->cleanup_bh = 0;
2221     s->vm_start_bh = 0;
2222     s->to_dst_file = NULL;
2223     s->state = MIGRATION_STATUS_NONE;
2224     s->rp_state.from_dst_file = NULL;
2225     s->rp_state.error = false;
2226     s->mbps = 0.0;
2227     s->pages_per_second = 0.0;
2228     s->downtime = 0;
2229     s->expected_downtime = 0;
2230     s->setup_time = 0;
2231     s->start_postcopy = false;
2232     s->postcopy_after_devices = false;
2233     s->migration_thread_running = false;
2234     error_free(s->error);
2235     s->error = NULL;
2236     s->hostname = NULL;
2237 
2238     migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
2239 
2240     s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2241     s->total_time = 0;
2242     s->vm_was_running = false;
2243     s->iteration_initial_bytes = 0;
2244     s->threshold_size = 0;
2245 }
2246 
2247 int migrate_add_blocker_internal(Error *reason, Error **errp)
2248 {
2249     /* Snapshots are similar to migrations, so check RUN_STATE_SAVE_VM too. */
2250     if (runstate_check(RUN_STATE_SAVE_VM) || !migration_is_idle()) {
2251         error_propagate_prepend(errp, error_copy(reason),
2252                                 "disallowing migration blocker "
2253                                 "(migration/snapshot in progress) for: ");
2254         return -EBUSY;
2255     }
2256 
2257     migration_blockers = g_slist_prepend(migration_blockers, reason);
2258     return 0;
2259 }
2260 
2261 int migrate_add_blocker(Error *reason, Error **errp)
2262 {
2263     if (only_migratable) {
2264         error_propagate_prepend(errp, error_copy(reason),
2265                                 "disallowing migration blocker "
2266                                 "(--only-migratable) for: ");
2267         return -EACCES;
2268     }
2269 
2270     return migrate_add_blocker_internal(reason, errp);
2271 }
2272 
2273 void migrate_del_blocker(Error *reason)
2274 {
2275     migration_blockers = g_slist_remove(migration_blockers, reason);
2276 }
2277 
2278 void qmp_migrate_incoming(const char *uri, Error **errp)
2279 {
2280     Error *local_err = NULL;
2281     static bool once = true;
2282 
2283     if (!once) {
2284         error_setg(errp, "The incoming migration has already been started");
2285         return;
2286     }
2287     if (!runstate_check(RUN_STATE_INMIGRATE)) {
2288         error_setg(errp, "'-incoming' was not specified on the command line");
2289         return;
2290     }
2291 
2292     if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) {
2293         return;
2294     }
2295 
2296     qemu_start_incoming_migration(uri, &local_err);
2297 
2298     if (local_err) {
2299         yank_unregister_instance(MIGRATION_YANK_INSTANCE);
2300         error_propagate(errp, local_err);
2301         return;
2302     }
2303 
2304     once = false;
2305 }
2306 
2307 void qmp_migrate_recover(const char *uri, Error **errp)
2308 {
2309     MigrationIncomingState *mis = migration_incoming_get_current();
2310 
2311     /*
2312      * Don't even bother to use ERRP_GUARD() as it _must_ always be set by
2313      * callers (no one should ignore a recover failure); if there is, it's a
2314      * programming error.
2315      */
2316     assert(errp);
2317 
2318     if (mis->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
2319         error_setg(errp, "Migrate recover can only be run "
2320                    "when postcopy is paused.");
2321         return;
2322     }
2323 
2324     /* If there's an existing transport, release it */
2325     migration_incoming_transport_cleanup(mis);
2326 
2327     /*
2328      * Note that this call will never start a real migration; it will
2329      * only re-setup the migration stream and poke existing migration
2330      * to continue using that newly established channel.
2331      */
2332     qemu_start_incoming_migration(uri, errp);
2333 }
2334 
2335 void qmp_migrate_pause(Error **errp)
2336 {
2337     MigrationState *ms = migrate_get_current();
2338     MigrationIncomingState *mis = migration_incoming_get_current();
2339     int ret;
2340 
2341     if (ms->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2342         /* Source side, during postcopy */
2343         qemu_mutex_lock(&ms->qemu_file_lock);
2344         ret = qemu_file_shutdown(ms->to_dst_file);
2345         qemu_mutex_unlock(&ms->qemu_file_lock);
2346         if (ret) {
2347             error_setg(errp, "Failed to pause source migration");
2348         }
2349         return;
2350     }
2351 
2352     if (mis->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2353         ret = qemu_file_shutdown(mis->from_src_file);
2354         if (ret) {
2355             error_setg(errp, "Failed to pause destination migration");
2356         }
2357         return;
2358     }
2359 
2360     error_setg(errp, "migrate-pause is currently only supported "
2361                "during postcopy-active state");
2362 }
2363 
2364 bool migration_is_blocked(Error **errp)
2365 {
2366     if (qemu_savevm_state_blocked(errp)) {
2367         return true;
2368     }
2369 
2370     if (migration_blockers) {
2371         error_propagate(errp, error_copy(migration_blockers->data));
2372         return true;
2373     }
2374 
2375     return false;
2376 }
2377 
2378 /* Returns true if continue to migrate, or false if error detected */
2379 static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc,
2380                             bool resume, Error **errp)
2381 {
2382     Error *local_err = NULL;
2383 
2384     if (resume) {
2385         if (s->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
2386             error_setg(errp, "Cannot resume if there is no "
2387                        "paused migration");
2388             return false;
2389         }
2390 
2391         /*
2392          * Postcopy recovery won't work well with release-ram
2393          * capability since release-ram will drop the page buffer as
2394          * long as the page is put into the send buffer.  So if there
2395          * is a network failure happened, any page buffers that have
2396          * not yet reached the destination VM but have already been
2397          * sent from the source VM will be lost forever.  Let's refuse
2398          * the client from resuming such a postcopy migration.
2399          * Luckily release-ram was designed to only be used when src
2400          * and destination VMs are on the same host, so it should be
2401          * fine.
2402          */
2403         if (migrate_release_ram()) {
2404             error_setg(errp, "Postcopy recovery cannot work "
2405                        "when release-ram capability is set");
2406             return false;
2407         }
2408 
2409         /* This is a resume, skip init status */
2410         return true;
2411     }
2412 
2413     if (migration_is_running(s->state)) {
2414         error_setg(errp, QERR_MIGRATION_ACTIVE);
2415         return false;
2416     }
2417 
2418     if (runstate_check(RUN_STATE_INMIGRATE)) {
2419         error_setg(errp, "Guest is waiting for an incoming migration");
2420         return false;
2421     }
2422 
2423     if (runstate_check(RUN_STATE_POSTMIGRATE)) {
2424         error_setg(errp, "Can't migrate the vm that was paused due to "
2425                    "previous migration");
2426         return false;
2427     }
2428 
2429     if (migration_is_blocked(errp)) {
2430         return false;
2431     }
2432 
2433     if (blk || blk_inc) {
2434         if (migrate_colo_enabled()) {
2435             error_setg(errp, "No disk migration is required in COLO mode");
2436             return false;
2437         }
2438         if (migrate_use_block() || migrate_use_block_incremental()) {
2439             error_setg(errp, "Command options are incompatible with "
2440                        "current migration capabilities");
2441             return false;
2442         }
2443         migrate_set_block_enabled(true, &local_err);
2444         if (local_err) {
2445             error_propagate(errp, local_err);
2446             return false;
2447         }
2448         s->must_remove_block_options = true;
2449     }
2450 
2451     if (blk_inc) {
2452         migrate_set_block_incremental(s, true);
2453     }
2454 
2455     migrate_init(s);
2456     /*
2457      * set ram_counters compression_counters memory to zero for a
2458      * new migration
2459      */
2460     memset(&ram_counters, 0, sizeof(ram_counters));
2461     memset(&compression_counters, 0, sizeof(compression_counters));
2462 
2463     return true;
2464 }
2465 
2466 void qmp_migrate(const char *uri, bool has_blk, bool blk,
2467                  bool has_inc, bool inc, bool has_detach, bool detach,
2468                  bool has_resume, bool resume, Error **errp)
2469 {
2470     Error *local_err = NULL;
2471     MigrationState *s = migrate_get_current();
2472     const char *p = NULL;
2473 
2474     if (!migrate_prepare(s, has_blk && blk, has_inc && inc,
2475                          has_resume && resume, errp)) {
2476         /* Error detected, put into errp */
2477         return;
2478     }
2479 
2480     if (!(has_resume && resume)) {
2481         if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) {
2482             return;
2483         }
2484     }
2485 
2486     migrate_protocol_allow_multi_channels(false);
2487     if (strstart(uri, "tcp:", &p) ||
2488         strstart(uri, "unix:", NULL) ||
2489         strstart(uri, "vsock:", NULL)) {
2490         migrate_protocol_allow_multi_channels(true);
2491         socket_start_outgoing_migration(s, p ? p : uri, &local_err);
2492 #ifdef CONFIG_RDMA
2493     } else if (strstart(uri, "rdma:", &p)) {
2494         rdma_start_outgoing_migration(s, p, &local_err);
2495 #endif
2496     } else if (strstart(uri, "exec:", &p)) {
2497         exec_start_outgoing_migration(s, p, &local_err);
2498     } else if (strstart(uri, "fd:", &p)) {
2499         fd_start_outgoing_migration(s, p, &local_err);
2500     } else {
2501         if (!(has_resume && resume)) {
2502             yank_unregister_instance(MIGRATION_YANK_INSTANCE);
2503         }
2504         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
2505                    "a valid migration protocol");
2506         migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2507                           MIGRATION_STATUS_FAILED);
2508         block_cleanup_parameters(s);
2509         return;
2510     }
2511 
2512     if (local_err) {
2513         if (!(has_resume && resume)) {
2514             yank_unregister_instance(MIGRATION_YANK_INSTANCE);
2515         }
2516         migrate_fd_error(s, local_err);
2517         error_propagate(errp, local_err);
2518         return;
2519     }
2520 }
2521 
2522 void qmp_migrate_cancel(Error **errp)
2523 {
2524     migration_cancel(NULL);
2525 }
2526 
2527 void qmp_migrate_continue(MigrationStatus state, Error **errp)
2528 {
2529     MigrationState *s = migrate_get_current();
2530     if (s->state != state) {
2531         error_setg(errp,  "Migration not in expected state: %s",
2532                    MigrationStatus_str(s->state));
2533         return;
2534     }
2535     qemu_sem_post(&s->pause_sem);
2536 }
2537 
2538 bool migrate_release_ram(void)
2539 {
2540     MigrationState *s;
2541 
2542     s = migrate_get_current();
2543 
2544     return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
2545 }
2546 
2547 bool migrate_postcopy_ram(void)
2548 {
2549     MigrationState *s;
2550 
2551     s = migrate_get_current();
2552 
2553     return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
2554 }
2555 
2556 bool migrate_postcopy(void)
2557 {
2558     return migrate_postcopy_ram() || migrate_dirty_bitmaps();
2559 }
2560 
2561 bool migrate_auto_converge(void)
2562 {
2563     MigrationState *s;
2564 
2565     s = migrate_get_current();
2566 
2567     return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
2568 }
2569 
2570 bool migrate_zero_blocks(void)
2571 {
2572     MigrationState *s;
2573 
2574     s = migrate_get_current();
2575 
2576     return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
2577 }
2578 
2579 bool migrate_postcopy_blocktime(void)
2580 {
2581     MigrationState *s;
2582 
2583     s = migrate_get_current();
2584 
2585     return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME];
2586 }
2587 
2588 bool migrate_use_compression(void)
2589 {
2590     MigrationState *s;
2591 
2592     s = migrate_get_current();
2593 
2594     return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
2595 }
2596 
2597 int migrate_compress_level(void)
2598 {
2599     MigrationState *s;
2600 
2601     s = migrate_get_current();
2602 
2603     return s->parameters.compress_level;
2604 }
2605 
2606 int migrate_compress_threads(void)
2607 {
2608     MigrationState *s;
2609 
2610     s = migrate_get_current();
2611 
2612     return s->parameters.compress_threads;
2613 }
2614 
2615 int migrate_compress_wait_thread(void)
2616 {
2617     MigrationState *s;
2618 
2619     s = migrate_get_current();
2620 
2621     return s->parameters.compress_wait_thread;
2622 }
2623 
2624 int migrate_decompress_threads(void)
2625 {
2626     MigrationState *s;
2627 
2628     s = migrate_get_current();
2629 
2630     return s->parameters.decompress_threads;
2631 }
2632 
2633 bool migrate_dirty_bitmaps(void)
2634 {
2635     MigrationState *s;
2636 
2637     s = migrate_get_current();
2638 
2639     return s->enabled_capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS];
2640 }
2641 
2642 bool migrate_ignore_shared(void)
2643 {
2644     MigrationState *s;
2645 
2646     s = migrate_get_current();
2647 
2648     return s->enabled_capabilities[MIGRATION_CAPABILITY_X_IGNORE_SHARED];
2649 }
2650 
2651 bool migrate_validate_uuid(void)
2652 {
2653     MigrationState *s;
2654 
2655     s = migrate_get_current();
2656 
2657     return s->enabled_capabilities[MIGRATION_CAPABILITY_VALIDATE_UUID];
2658 }
2659 
2660 bool migrate_use_events(void)
2661 {
2662     MigrationState *s;
2663 
2664     s = migrate_get_current();
2665 
2666     return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
2667 }
2668 
2669 bool migrate_use_multifd(void)
2670 {
2671     MigrationState *s;
2672 
2673     s = migrate_get_current();
2674 
2675     return s->enabled_capabilities[MIGRATION_CAPABILITY_MULTIFD];
2676 }
2677 
2678 bool migrate_pause_before_switchover(void)
2679 {
2680     MigrationState *s;
2681 
2682     s = migrate_get_current();
2683 
2684     return s->enabled_capabilities[
2685         MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER];
2686 }
2687 
2688 int migrate_multifd_channels(void)
2689 {
2690     MigrationState *s;
2691 
2692     s = migrate_get_current();
2693 
2694     return s->parameters.multifd_channels;
2695 }
2696 
2697 MultiFDCompression migrate_multifd_compression(void)
2698 {
2699     MigrationState *s;
2700 
2701     s = migrate_get_current();
2702 
2703     assert(s->parameters.multifd_compression < MULTIFD_COMPRESSION__MAX);
2704     return s->parameters.multifd_compression;
2705 }
2706 
2707 int migrate_multifd_zlib_level(void)
2708 {
2709     MigrationState *s;
2710 
2711     s = migrate_get_current();
2712 
2713     return s->parameters.multifd_zlib_level;
2714 }
2715 
2716 int migrate_multifd_zstd_level(void)
2717 {
2718     MigrationState *s;
2719 
2720     s = migrate_get_current();
2721 
2722     return s->parameters.multifd_zstd_level;
2723 }
2724 
2725 #ifdef CONFIG_LINUX
2726 bool migrate_use_zero_copy_send(void)
2727 {
2728     MigrationState *s;
2729 
2730     s = migrate_get_current();
2731 
2732     return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_COPY_SEND];
2733 }
2734 #endif
2735 
2736 int migrate_use_tls(void)
2737 {
2738     MigrationState *s;
2739 
2740     s = migrate_get_current();
2741 
2742     return s->parameters.tls_creds && *s->parameters.tls_creds;
2743 }
2744 
2745 int migrate_use_xbzrle(void)
2746 {
2747     MigrationState *s;
2748 
2749     s = migrate_get_current();
2750 
2751     return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
2752 }
2753 
2754 uint64_t migrate_xbzrle_cache_size(void)
2755 {
2756     MigrationState *s;
2757 
2758     s = migrate_get_current();
2759 
2760     return s->parameters.xbzrle_cache_size;
2761 }
2762 
2763 static int64_t migrate_max_postcopy_bandwidth(void)
2764 {
2765     MigrationState *s;
2766 
2767     s = migrate_get_current();
2768 
2769     return s->parameters.max_postcopy_bandwidth;
2770 }
2771 
2772 bool migrate_use_block(void)
2773 {
2774     MigrationState *s;
2775 
2776     s = migrate_get_current();
2777 
2778     return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
2779 }
2780 
2781 bool migrate_use_return_path(void)
2782 {
2783     MigrationState *s;
2784 
2785     s = migrate_get_current();
2786 
2787     return s->enabled_capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
2788 }
2789 
2790 bool migrate_use_block_incremental(void)
2791 {
2792     MigrationState *s;
2793 
2794     s = migrate_get_current();
2795 
2796     return s->parameters.block_incremental;
2797 }
2798 
2799 bool migrate_background_snapshot(void)
2800 {
2801     MigrationState *s;
2802 
2803     s = migrate_get_current();
2804 
2805     return s->enabled_capabilities[MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT];
2806 }
2807 
2808 bool migrate_postcopy_preempt(void)
2809 {
2810     MigrationState *s;
2811 
2812     s = migrate_get_current();
2813 
2814     return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_PREEMPT];
2815 }
2816 
2817 /* migration thread support */
2818 /*
2819  * Something bad happened to the RP stream, mark an error
2820  * The caller shall print or trace something to indicate why
2821  */
2822 static void mark_source_rp_bad(MigrationState *s)
2823 {
2824     s->rp_state.error = true;
2825 }
2826 
2827 static struct rp_cmd_args {
2828     ssize_t     len; /* -1 = variable */
2829     const char *name;
2830 } rp_cmd_args[] = {
2831     [MIG_RP_MSG_INVALID]        = { .len = -1, .name = "INVALID" },
2832     [MIG_RP_MSG_SHUT]           = { .len =  4, .name = "SHUT" },
2833     [MIG_RP_MSG_PONG]           = { .len =  4, .name = "PONG" },
2834     [MIG_RP_MSG_REQ_PAGES]      = { .len = 12, .name = "REQ_PAGES" },
2835     [MIG_RP_MSG_REQ_PAGES_ID]   = { .len = -1, .name = "REQ_PAGES_ID" },
2836     [MIG_RP_MSG_RECV_BITMAP]    = { .len = -1, .name = "RECV_BITMAP" },
2837     [MIG_RP_MSG_RESUME_ACK]     = { .len =  4, .name = "RESUME_ACK" },
2838     [MIG_RP_MSG_MAX]            = { .len = -1, .name = "MAX" },
2839 };
2840 
2841 /*
2842  * Process a request for pages received on the return path,
2843  * We're allowed to send more than requested (e.g. to round to our page size)
2844  * and we don't need to send pages that have already been sent.
2845  */
2846 static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
2847                                        ram_addr_t start, size_t len)
2848 {
2849     long our_host_ps = qemu_real_host_page_size();
2850 
2851     trace_migrate_handle_rp_req_pages(rbname, start, len);
2852 
2853     /*
2854      * Since we currently insist on matching page sizes, just sanity check
2855      * we're being asked for whole host pages.
2856      */
2857     if (!QEMU_IS_ALIGNED(start, our_host_ps) ||
2858         !QEMU_IS_ALIGNED(len, our_host_ps)) {
2859         error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
2860                      " len: %zd", __func__, start, len);
2861         mark_source_rp_bad(ms);
2862         return;
2863     }
2864 
2865     if (ram_save_queue_pages(rbname, start, len)) {
2866         mark_source_rp_bad(ms);
2867     }
2868 }
2869 
2870 /* Return true to retry, false to quit */
2871 static bool postcopy_pause_return_path_thread(MigrationState *s)
2872 {
2873     trace_postcopy_pause_return_path();
2874 
2875     qemu_sem_wait(&s->postcopy_pause_rp_sem);
2876 
2877     trace_postcopy_pause_return_path_continued();
2878 
2879     return true;
2880 }
2881 
2882 static int migrate_handle_rp_recv_bitmap(MigrationState *s, char *block_name)
2883 {
2884     RAMBlock *block = qemu_ram_block_by_name(block_name);
2885 
2886     if (!block) {
2887         error_report("%s: invalid block name '%s'", __func__, block_name);
2888         return -EINVAL;
2889     }
2890 
2891     /* Fetch the received bitmap and refresh the dirty bitmap */
2892     return ram_dirty_bitmap_reload(s, block);
2893 }
2894 
2895 static int migrate_handle_rp_resume_ack(MigrationState *s, uint32_t value)
2896 {
2897     trace_source_return_path_thread_resume_ack(value);
2898 
2899     if (value != MIGRATION_RESUME_ACK_VALUE) {
2900         error_report("%s: illegal resume_ack value %"PRIu32,
2901                      __func__, value);
2902         return -1;
2903     }
2904 
2905     /* Now both sides are active. */
2906     migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
2907                       MIGRATION_STATUS_POSTCOPY_ACTIVE);
2908 
2909     /* Notify send thread that time to continue send pages */
2910     qemu_sem_post(&s->rp_state.rp_sem);
2911 
2912     return 0;
2913 }
2914 
2915 /*
2916  * Release ms->rp_state.from_dst_file (and postcopy_qemufile_src if
2917  * existed) in a safe way.
2918  */
2919 static void migration_release_dst_files(MigrationState *ms)
2920 {
2921     QEMUFile *file;
2922 
2923     WITH_QEMU_LOCK_GUARD(&ms->qemu_file_lock) {
2924         /*
2925          * Reset the from_dst_file pointer first before releasing it, as we
2926          * can't block within lock section
2927          */
2928         file = ms->rp_state.from_dst_file;
2929         ms->rp_state.from_dst_file = NULL;
2930     }
2931 
2932     /*
2933      * Do the same to postcopy fast path socket too if there is.  No
2934      * locking needed because this qemufile should only be managed by
2935      * return path thread.
2936      */
2937     if (ms->postcopy_qemufile_src) {
2938         migration_ioc_unregister_yank_from_file(ms->postcopy_qemufile_src);
2939         qemu_file_shutdown(ms->postcopy_qemufile_src);
2940         qemu_fclose(ms->postcopy_qemufile_src);
2941         ms->postcopy_qemufile_src = NULL;
2942     }
2943 
2944     qemu_fclose(file);
2945 }
2946 
2947 /*
2948  * Handles messages sent on the return path towards the source VM
2949  *
2950  */
2951 static void *source_return_path_thread(void *opaque)
2952 {
2953     MigrationState *ms = opaque;
2954     QEMUFile *rp = ms->rp_state.from_dst_file;
2955     uint16_t header_len, header_type;
2956     uint8_t buf[512];
2957     uint32_t tmp32, sibling_error;
2958     ram_addr_t start = 0; /* =0 to silence warning */
2959     size_t  len = 0, expected_len;
2960     int res;
2961 
2962     trace_source_return_path_thread_entry();
2963     rcu_register_thread();
2964 
2965 retry:
2966     while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
2967            migration_is_setup_or_active(ms->state)) {
2968         trace_source_return_path_thread_loop_top();
2969         header_type = qemu_get_be16(rp);
2970         header_len = qemu_get_be16(rp);
2971 
2972         if (qemu_file_get_error(rp)) {
2973             mark_source_rp_bad(ms);
2974             goto out;
2975         }
2976 
2977         if (header_type >= MIG_RP_MSG_MAX ||
2978             header_type == MIG_RP_MSG_INVALID) {
2979             error_report("RP: Received invalid message 0x%04x length 0x%04x",
2980                          header_type, header_len);
2981             mark_source_rp_bad(ms);
2982             goto out;
2983         }
2984 
2985         if ((rp_cmd_args[header_type].len != -1 &&
2986             header_len != rp_cmd_args[header_type].len) ||
2987             header_len > sizeof(buf)) {
2988             error_report("RP: Received '%s' message (0x%04x) with"
2989                          "incorrect length %d expecting %zu",
2990                          rp_cmd_args[header_type].name, header_type, header_len,
2991                          (size_t)rp_cmd_args[header_type].len);
2992             mark_source_rp_bad(ms);
2993             goto out;
2994         }
2995 
2996         /* We know we've got a valid header by this point */
2997         res = qemu_get_buffer(rp, buf, header_len);
2998         if (res != header_len) {
2999             error_report("RP: Failed reading data for message 0x%04x"
3000                          " read %d expected %d",
3001                          header_type, res, header_len);
3002             mark_source_rp_bad(ms);
3003             goto out;
3004         }
3005 
3006         /* OK, we have the message and the data */
3007         switch (header_type) {
3008         case MIG_RP_MSG_SHUT:
3009             sibling_error = ldl_be_p(buf);
3010             trace_source_return_path_thread_shut(sibling_error);
3011             if (sibling_error) {
3012                 error_report("RP: Sibling indicated error %d", sibling_error);
3013                 mark_source_rp_bad(ms);
3014             }
3015             /*
3016              * We'll let the main thread deal with closing the RP
3017              * we could do a shutdown(2) on it, but we're the only user
3018              * anyway, so there's nothing gained.
3019              */
3020             goto out;
3021 
3022         case MIG_RP_MSG_PONG:
3023             tmp32 = ldl_be_p(buf);
3024             trace_source_return_path_thread_pong(tmp32);
3025             break;
3026 
3027         case MIG_RP_MSG_REQ_PAGES:
3028             start = ldq_be_p(buf);
3029             len = ldl_be_p(buf + 8);
3030             migrate_handle_rp_req_pages(ms, NULL, start, len);
3031             break;
3032 
3033         case MIG_RP_MSG_REQ_PAGES_ID:
3034             expected_len = 12 + 1; /* header + termination */
3035 
3036             if (header_len >= expected_len) {
3037                 start = ldq_be_p(buf);
3038                 len = ldl_be_p(buf + 8);
3039                 /* Now we expect an idstr */
3040                 tmp32 = buf[12]; /* Length of the following idstr */
3041                 buf[13 + tmp32] = '\0';
3042                 expected_len += tmp32;
3043             }
3044             if (header_len != expected_len) {
3045                 error_report("RP: Req_Page_id with length %d expecting %zd",
3046                              header_len, expected_len);
3047                 mark_source_rp_bad(ms);
3048                 goto out;
3049             }
3050             migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
3051             break;
3052 
3053         case MIG_RP_MSG_RECV_BITMAP:
3054             if (header_len < 1) {
3055                 error_report("%s: missing block name", __func__);
3056                 mark_source_rp_bad(ms);
3057                 goto out;
3058             }
3059             /* Format: len (1B) + idstr (<255B). This ends the idstr. */
3060             buf[buf[0] + 1] = '\0';
3061             if (migrate_handle_rp_recv_bitmap(ms, (char *)(buf + 1))) {
3062                 mark_source_rp_bad(ms);
3063                 goto out;
3064             }
3065             break;
3066 
3067         case MIG_RP_MSG_RESUME_ACK:
3068             tmp32 = ldl_be_p(buf);
3069             if (migrate_handle_rp_resume_ack(ms, tmp32)) {
3070                 mark_source_rp_bad(ms);
3071                 goto out;
3072             }
3073             break;
3074 
3075         default:
3076             break;
3077         }
3078     }
3079 
3080 out:
3081     res = qemu_file_get_error(rp);
3082     if (res) {
3083         if (res && migration_in_postcopy()) {
3084             /*
3085              * Maybe there is something we can do: it looks like a
3086              * network down issue, and we pause for a recovery.
3087              */
3088             migration_release_dst_files(ms);
3089             rp = NULL;
3090             if (postcopy_pause_return_path_thread(ms)) {
3091                 /*
3092                  * Reload rp, reset the rest.  Referencing it is safe since
3093                  * it's reset only by us above, or when migration completes
3094                  */
3095                 rp = ms->rp_state.from_dst_file;
3096                 ms->rp_state.error = false;
3097                 goto retry;
3098             }
3099         }
3100 
3101         trace_source_return_path_thread_bad_end();
3102         mark_source_rp_bad(ms);
3103     }
3104 
3105     trace_source_return_path_thread_end();
3106     migration_release_dst_files(ms);
3107     rcu_unregister_thread();
3108     return NULL;
3109 }
3110 
3111 static int open_return_path_on_source(MigrationState *ms,
3112                                       bool create_thread)
3113 {
3114     ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
3115     if (!ms->rp_state.from_dst_file) {
3116         return -1;
3117     }
3118 
3119     trace_open_return_path_on_source();
3120 
3121     if (!create_thread) {
3122         /* We're done */
3123         return 0;
3124     }
3125 
3126     qemu_thread_create(&ms->rp_state.rp_thread, "return path",
3127                        source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
3128     ms->rp_state.rp_thread_created = true;
3129 
3130     trace_open_return_path_on_source_continue();
3131 
3132     return 0;
3133 }
3134 
3135 /* Returns 0 if the RP was ok, otherwise there was an error on the RP */
3136 static int await_return_path_close_on_source(MigrationState *ms)
3137 {
3138     /*
3139      * If this is a normal exit then the destination will send a SHUT and the
3140      * rp_thread will exit, however if there's an error we need to cause
3141      * it to exit.
3142      */
3143     if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
3144         /*
3145          * shutdown(2), if we have it, will cause it to unblock if it's stuck
3146          * waiting for the destination.
3147          */
3148         qemu_file_shutdown(ms->rp_state.from_dst_file);
3149         mark_source_rp_bad(ms);
3150     }
3151     trace_await_return_path_close_on_source_joining();
3152     qemu_thread_join(&ms->rp_state.rp_thread);
3153     ms->rp_state.rp_thread_created = false;
3154     trace_await_return_path_close_on_source_close();
3155     return ms->rp_state.error;
3156 }
3157 
3158 /*
3159  * Switch from normal iteration to postcopy
3160  * Returns non-0 on error
3161  */
3162 static int postcopy_start(MigrationState *ms)
3163 {
3164     int ret;
3165     QIOChannelBuffer *bioc;
3166     QEMUFile *fb;
3167     int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3168     int64_t bandwidth = migrate_max_postcopy_bandwidth();
3169     bool restart_block = false;
3170     int cur_state = MIGRATION_STATUS_ACTIVE;
3171 
3172     if (postcopy_preempt_wait_channel(ms)) {
3173         migrate_set_state(&ms->state, ms->state, MIGRATION_STATUS_FAILED);
3174         return -1;
3175     }
3176 
3177     if (!migrate_pause_before_switchover()) {
3178         migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
3179                           MIGRATION_STATUS_POSTCOPY_ACTIVE);
3180     }
3181 
3182     trace_postcopy_start();
3183     qemu_mutex_lock_iothread();
3184     trace_postcopy_start_set_run();
3185 
3186     qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
3187     global_state_store();
3188     ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
3189     if (ret < 0) {
3190         goto fail;
3191     }
3192 
3193     ret = migration_maybe_pause(ms, &cur_state,
3194                                 MIGRATION_STATUS_POSTCOPY_ACTIVE);
3195     if (ret < 0) {
3196         goto fail;
3197     }
3198 
3199     ret = bdrv_inactivate_all();
3200     if (ret < 0) {
3201         goto fail;
3202     }
3203     restart_block = true;
3204 
3205     /*
3206      * Cause any non-postcopiable, but iterative devices to
3207      * send out their final data.
3208      */
3209     qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
3210 
3211     /*
3212      * in Finish migrate and with the io-lock held everything should
3213      * be quiet, but we've potentially still got dirty pages and we
3214      * need to tell the destination to throw any pages it's already received
3215      * that are dirty
3216      */
3217     if (migrate_postcopy_ram()) {
3218         ram_postcopy_send_discard_bitmap(ms);
3219     }
3220 
3221     /*
3222      * send rest of state - note things that are doing postcopy
3223      * will notice we're in POSTCOPY_ACTIVE and not actually
3224      * wrap their state up here
3225      */
3226     /* 0 max-postcopy-bandwidth means unlimited */
3227     if (!bandwidth) {
3228         qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
3229     } else {
3230         qemu_file_set_rate_limit(ms->to_dst_file, bandwidth / XFER_LIMIT_RATIO);
3231     }
3232     if (migrate_postcopy_ram()) {
3233         /* Ping just for debugging, helps line traces up */
3234         qemu_savevm_send_ping(ms->to_dst_file, 2);
3235     }
3236 
3237     /*
3238      * While loading the device state we may trigger page transfer
3239      * requests and the fd must be free to process those, and thus
3240      * the destination must read the whole device state off the fd before
3241      * it starts processing it.  Unfortunately the ad-hoc migration format
3242      * doesn't allow the destination to know the size to read without fully
3243      * parsing it through each devices load-state code (especially the open
3244      * coded devices that use get/put).
3245      * So we wrap the device state up in a package with a length at the start;
3246      * to do this we use a qemu_buf to hold the whole of the device state.
3247      */
3248     bioc = qio_channel_buffer_new(4096);
3249     qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
3250     fb = qemu_file_new_output(QIO_CHANNEL(bioc));
3251     object_unref(OBJECT(bioc));
3252 
3253     /*
3254      * Make sure the receiver can get incoming pages before we send the rest
3255      * of the state
3256      */
3257     qemu_savevm_send_postcopy_listen(fb);
3258 
3259     qemu_savevm_state_complete_precopy(fb, false, false);
3260     if (migrate_postcopy_ram()) {
3261         qemu_savevm_send_ping(fb, 3);
3262     }
3263 
3264     qemu_savevm_send_postcopy_run(fb);
3265 
3266     /* <><> end of stuff going into the package */
3267 
3268     /* Last point of recovery; as soon as we send the package the destination
3269      * can open devices and potentially start running.
3270      * Lets just check again we've not got any errors.
3271      */
3272     ret = qemu_file_get_error(ms->to_dst_file);
3273     if (ret) {
3274         error_report("postcopy_start: Migration stream errored (pre package)");
3275         goto fail_closefb;
3276     }
3277 
3278     restart_block = false;
3279 
3280     /* Now send that blob */
3281     if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
3282         goto fail_closefb;
3283     }
3284     qemu_fclose(fb);
3285 
3286     /* Send a notify to give a chance for anything that needs to happen
3287      * at the transition to postcopy and after the device state; in particular
3288      * spice needs to trigger a transition now
3289      */
3290     ms->postcopy_after_devices = true;
3291     notifier_list_notify(&migration_state_notifiers, ms);
3292 
3293     ms->downtime =  qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
3294 
3295     qemu_mutex_unlock_iothread();
3296 
3297     if (migrate_postcopy_ram()) {
3298         /*
3299          * Although this ping is just for debug, it could potentially be
3300          * used for getting a better measurement of downtime at the source.
3301          */
3302         qemu_savevm_send_ping(ms->to_dst_file, 4);
3303     }
3304 
3305     if (migrate_release_ram()) {
3306         ram_postcopy_migrated_memory_release(ms);
3307     }
3308 
3309     ret = qemu_file_get_error(ms->to_dst_file);
3310     if (ret) {
3311         error_report("postcopy_start: Migration stream errored");
3312         migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
3313                               MIGRATION_STATUS_FAILED);
3314     }
3315 
3316     trace_postcopy_preempt_enabled(migrate_postcopy_preempt());
3317 
3318     return ret;
3319 
3320 fail_closefb:
3321     qemu_fclose(fb);
3322 fail:
3323     migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
3324                           MIGRATION_STATUS_FAILED);
3325     if (restart_block) {
3326         /* A failure happened early enough that we know the destination hasn't
3327          * accessed block devices, so we're safe to recover.
3328          */
3329         Error *local_err = NULL;
3330 
3331         bdrv_activate_all(&local_err);
3332         if (local_err) {
3333             error_report_err(local_err);
3334         }
3335     }
3336     qemu_mutex_unlock_iothread();
3337     return -1;
3338 }
3339 
3340 /**
3341  * migration_maybe_pause: Pause if required to by
3342  * migrate_pause_before_switchover called with the iothread locked
3343  * Returns: 0 on success
3344  */
3345 static int migration_maybe_pause(MigrationState *s,
3346                                  int *current_active_state,
3347                                  int new_state)
3348 {
3349     if (!migrate_pause_before_switchover()) {
3350         return 0;
3351     }
3352 
3353     /* Since leaving this state is not atomic with posting the semaphore
3354      * it's possible that someone could have issued multiple migrate_continue
3355      * and the semaphore is incorrectly positive at this point;
3356      * the docs say it's undefined to reinit a semaphore that's already
3357      * init'd, so use timedwait to eat up any existing posts.
3358      */
3359     while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) {
3360         /* This block intentionally left blank */
3361     }
3362 
3363     /*
3364      * If the migration is cancelled when it is in the completion phase,
3365      * the migration state is set to MIGRATION_STATUS_CANCELLING.
3366      * So we don't need to wait a semaphore, otherwise we would always
3367      * wait for the 'pause_sem' semaphore.
3368      */
3369     if (s->state != MIGRATION_STATUS_CANCELLING) {
3370         qemu_mutex_unlock_iothread();
3371         migrate_set_state(&s->state, *current_active_state,
3372                           MIGRATION_STATUS_PRE_SWITCHOVER);
3373         qemu_sem_wait(&s->pause_sem);
3374         migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER,
3375                           new_state);
3376         *current_active_state = new_state;
3377         qemu_mutex_lock_iothread();
3378     }
3379 
3380     return s->state == new_state ? 0 : -EINVAL;
3381 }
3382 
3383 /**
3384  * migration_completion: Used by migration_thread when there's not much left.
3385  *   The caller 'breaks' the loop when this returns.
3386  *
3387  * @s: Current migration state
3388  */
3389 static void migration_completion(MigrationState *s)
3390 {
3391     int ret;
3392     int current_active_state = s->state;
3393 
3394     if (s->state == MIGRATION_STATUS_ACTIVE) {
3395         qemu_mutex_lock_iothread();
3396         s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3397         qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
3398         s->vm_was_running = runstate_is_running();
3399         ret = global_state_store();
3400 
3401         if (!ret) {
3402             bool inactivate = !migrate_colo_enabled();
3403             ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
3404             trace_migration_completion_vm_stop(ret);
3405             if (ret >= 0) {
3406                 ret = migration_maybe_pause(s, &current_active_state,
3407                                             MIGRATION_STATUS_DEVICE);
3408             }
3409             if (ret >= 0) {
3410                 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
3411                 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
3412                                                          inactivate);
3413             }
3414             if (inactivate && ret >= 0) {
3415                 s->block_inactive = true;
3416             }
3417         }
3418         qemu_mutex_unlock_iothread();
3419 
3420         if (ret < 0) {
3421             goto fail;
3422         }
3423     } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
3424         trace_migration_completion_postcopy_end();
3425 
3426         qemu_mutex_lock_iothread();
3427         qemu_savevm_state_complete_postcopy(s->to_dst_file);
3428         qemu_mutex_unlock_iothread();
3429 
3430         /* Shutdown the postcopy fast path thread */
3431         if (migrate_postcopy_preempt()) {
3432             postcopy_preempt_shutdown_file(s);
3433         }
3434 
3435         trace_migration_completion_postcopy_end_after_complete();
3436     } else {
3437         goto fail;
3438     }
3439 
3440     /*
3441      * If rp was opened we must clean up the thread before
3442      * cleaning everything else up (since if there are no failures
3443      * it will wait for the destination to send it's status in
3444      * a SHUT command).
3445      */
3446     if (s->rp_state.rp_thread_created) {
3447         int rp_error;
3448         trace_migration_return_path_end_before();
3449         rp_error = await_return_path_close_on_source(s);
3450         trace_migration_return_path_end_after(rp_error);
3451         if (rp_error) {
3452             goto fail_invalidate;
3453         }
3454     }
3455 
3456     if (qemu_file_get_error(s->to_dst_file)) {
3457         trace_migration_completion_file_err();
3458         goto fail_invalidate;
3459     }
3460 
3461     if (migrate_colo_enabled() && s->state == MIGRATION_STATUS_ACTIVE) {
3462         /* COLO does not support postcopy */
3463         migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE,
3464                           MIGRATION_STATUS_COLO);
3465     } else {
3466         migrate_set_state(&s->state, current_active_state,
3467                           MIGRATION_STATUS_COMPLETED);
3468     }
3469 
3470     return;
3471 
3472 fail_invalidate:
3473     /* If not doing postcopy, vm_start() will be called: let's regain
3474      * control on images.
3475      */
3476     if (s->state == MIGRATION_STATUS_ACTIVE ||
3477         s->state == MIGRATION_STATUS_DEVICE) {
3478         Error *local_err = NULL;
3479 
3480         qemu_mutex_lock_iothread();
3481         bdrv_activate_all(&local_err);
3482         if (local_err) {
3483             error_report_err(local_err);
3484         } else {
3485             s->block_inactive = false;
3486         }
3487         qemu_mutex_unlock_iothread();
3488     }
3489 
3490 fail:
3491     migrate_set_state(&s->state, current_active_state,
3492                       MIGRATION_STATUS_FAILED);
3493 }
3494 
3495 /**
3496  * bg_migration_completion: Used by bg_migration_thread when after all the
3497  *   RAM has been saved. The caller 'breaks' the loop when this returns.
3498  *
3499  * @s: Current migration state
3500  */
3501 static void bg_migration_completion(MigrationState *s)
3502 {
3503     int current_active_state = s->state;
3504 
3505     /*
3506      * Stop tracking RAM writes - un-protect memory, un-register UFFD
3507      * memory ranges, flush kernel wait queues and wake up threads
3508      * waiting for write fault to be resolved.
3509      */
3510     ram_write_tracking_stop();
3511 
3512     if (s->state == MIGRATION_STATUS_ACTIVE) {
3513         /*
3514          * By this moment we have RAM content saved into the migration stream.
3515          * The next step is to flush the non-RAM content (device state)
3516          * right after the ram content. The device state has been stored into
3517          * the temporary buffer before RAM saving started.
3518          */
3519         qemu_put_buffer(s->to_dst_file, s->bioc->data, s->bioc->usage);
3520         qemu_fflush(s->to_dst_file);
3521     } else if (s->state == MIGRATION_STATUS_CANCELLING) {
3522         goto fail;
3523     }
3524 
3525     if (qemu_file_get_error(s->to_dst_file)) {
3526         trace_migration_completion_file_err();
3527         goto fail;
3528     }
3529 
3530     migrate_set_state(&s->state, current_active_state,
3531                       MIGRATION_STATUS_COMPLETED);
3532     return;
3533 
3534 fail:
3535     migrate_set_state(&s->state, current_active_state,
3536                       MIGRATION_STATUS_FAILED);
3537 }
3538 
3539 bool migrate_colo_enabled(void)
3540 {
3541     MigrationState *s = migrate_get_current();
3542     return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
3543 }
3544 
3545 typedef enum MigThrError {
3546     /* No error detected */
3547     MIG_THR_ERR_NONE = 0,
3548     /* Detected error, but resumed successfully */
3549     MIG_THR_ERR_RECOVERED = 1,
3550     /* Detected fatal error, need to exit */
3551     MIG_THR_ERR_FATAL = 2,
3552 } MigThrError;
3553 
3554 static int postcopy_resume_handshake(MigrationState *s)
3555 {
3556     qemu_savevm_send_postcopy_resume(s->to_dst_file);
3557 
3558     while (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
3559         qemu_sem_wait(&s->rp_state.rp_sem);
3560     }
3561 
3562     if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
3563         return 0;
3564     }
3565 
3566     return -1;
3567 }
3568 
3569 /* Return zero if success, or <0 for error */
3570 static int postcopy_do_resume(MigrationState *s)
3571 {
3572     int ret;
3573 
3574     /*
3575      * Call all the resume_prepare() hooks, so that modules can be
3576      * ready for the migration resume.
3577      */
3578     ret = qemu_savevm_state_resume_prepare(s);
3579     if (ret) {
3580         error_report("%s: resume_prepare() failure detected: %d",
3581                      __func__, ret);
3582         return ret;
3583     }
3584 
3585     /*
3586      * Last handshake with destination on the resume (destination will
3587      * switch to postcopy-active afterwards)
3588      */
3589     ret = postcopy_resume_handshake(s);
3590     if (ret) {
3591         error_report("%s: handshake failed: %d", __func__, ret);
3592         return ret;
3593     }
3594 
3595     return 0;
3596 }
3597 
3598 /*
3599  * We don't return until we are in a safe state to continue current
3600  * postcopy migration.  Returns MIG_THR_ERR_RECOVERED if recovered, or
3601  * MIG_THR_ERR_FATAL if unrecovery failure happened.
3602  */
3603 static MigThrError postcopy_pause(MigrationState *s)
3604 {
3605     assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
3606 
3607     while (true) {
3608         QEMUFile *file;
3609 
3610         /*
3611          * Current channel is possibly broken. Release it.  Note that this is
3612          * guaranteed even without lock because to_dst_file should only be
3613          * modified by the migration thread.  That also guarantees that the
3614          * unregister of yank is safe too without the lock.  It should be safe
3615          * even to be within the qemu_file_lock, but we didn't do that to avoid
3616          * taking more mutex (yank_lock) within qemu_file_lock.  TL;DR: we make
3617          * the qemu_file_lock critical section as small as possible.
3618          */
3619         assert(s->to_dst_file);
3620         migration_ioc_unregister_yank_from_file(s->to_dst_file);
3621         qemu_mutex_lock(&s->qemu_file_lock);
3622         file = s->to_dst_file;
3623         s->to_dst_file = NULL;
3624         qemu_mutex_unlock(&s->qemu_file_lock);
3625 
3626         qemu_file_shutdown(file);
3627         qemu_fclose(file);
3628 
3629         migrate_set_state(&s->state, s->state,
3630                           MIGRATION_STATUS_POSTCOPY_PAUSED);
3631 
3632         error_report("Detected IO failure for postcopy. "
3633                      "Migration paused.");
3634 
3635         /*
3636          * We wait until things fixed up. Then someone will setup the
3637          * status back for us.
3638          */
3639         while (s->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
3640             qemu_sem_wait(&s->postcopy_pause_sem);
3641         }
3642 
3643         if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
3644             /* Woken up by a recover procedure. Give it a shot */
3645 
3646             if (postcopy_preempt_wait_channel(s)) {
3647                 /*
3648                  * Preempt enabled, and new channel create failed; loop
3649                  * back to wait for another recovery.
3650                  */
3651                 continue;
3652             }
3653 
3654             /*
3655              * Firstly, let's wake up the return path now, with a new
3656              * return path channel.
3657              */
3658             qemu_sem_post(&s->postcopy_pause_rp_sem);
3659 
3660             /* Do the resume logic */
3661             if (postcopy_do_resume(s) == 0) {
3662                 /* Let's continue! */
3663                 trace_postcopy_pause_continued();
3664                 return MIG_THR_ERR_RECOVERED;
3665             } else {
3666                 /*
3667                  * Something wrong happened during the recovery, let's
3668                  * pause again. Pause is always better than throwing
3669                  * data away.
3670                  */
3671                 continue;
3672             }
3673         } else {
3674             /* This is not right... Time to quit. */
3675             return MIG_THR_ERR_FATAL;
3676         }
3677     }
3678 }
3679 
3680 static MigThrError migration_detect_error(MigrationState *s)
3681 {
3682     int ret;
3683     int state = s->state;
3684     Error *local_error = NULL;
3685 
3686     if (state == MIGRATION_STATUS_CANCELLING ||
3687         state == MIGRATION_STATUS_CANCELLED) {
3688         /* End the migration, but don't set the state to failed */
3689         return MIG_THR_ERR_FATAL;
3690     }
3691 
3692     /*
3693      * Try to detect any file errors.  Note that postcopy_qemufile_src will
3694      * be NULL when postcopy preempt is not enabled.
3695      */
3696     ret = qemu_file_get_error_obj_any(s->to_dst_file,
3697                                       s->postcopy_qemufile_src,
3698                                       &local_error);
3699     if (!ret) {
3700         /* Everything is fine */
3701         assert(!local_error);
3702         return MIG_THR_ERR_NONE;
3703     }
3704 
3705     if (local_error) {
3706         migrate_set_error(s, local_error);
3707         error_free(local_error);
3708     }
3709 
3710     if (state == MIGRATION_STATUS_POSTCOPY_ACTIVE && ret) {
3711         /*
3712          * For postcopy, we allow the network to be down for a
3713          * while. After that, it can be continued by a
3714          * recovery phase.
3715          */
3716         return postcopy_pause(s);
3717     } else {
3718         /*
3719          * For precopy (or postcopy with error outside IO), we fail
3720          * with no time.
3721          */
3722         migrate_set_state(&s->state, state, MIGRATION_STATUS_FAILED);
3723         trace_migration_thread_file_err();
3724 
3725         /* Time to stop the migration, now. */
3726         return MIG_THR_ERR_FATAL;
3727     }
3728 }
3729 
3730 /* How many bytes have we transferred since the beginning of the migration */
3731 static uint64_t migration_total_bytes(MigrationState *s)
3732 {
3733     return qemu_file_total_transferred(s->to_dst_file) +
3734         ram_counters.multifd_bytes;
3735 }
3736 
3737 static void migration_calculate_complete(MigrationState *s)
3738 {
3739     uint64_t bytes = migration_total_bytes(s);
3740     int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3741     int64_t transfer_time;
3742 
3743     s->total_time = end_time - s->start_time;
3744     if (!s->downtime) {
3745         /*
3746          * It's still not set, so we are precopy migration.  For
3747          * postcopy, downtime is calculated during postcopy_start().
3748          */
3749         s->downtime = end_time - s->downtime_start;
3750     }
3751 
3752     transfer_time = s->total_time - s->setup_time;
3753     if (transfer_time) {
3754         s->mbps = ((double) bytes * 8.0) / transfer_time / 1000;
3755     }
3756 }
3757 
3758 static void update_iteration_initial_status(MigrationState *s)
3759 {
3760     /*
3761      * Update these three fields at the same time to avoid mismatch info lead
3762      * wrong speed calculation.
3763      */
3764     s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3765     s->iteration_initial_bytes = migration_total_bytes(s);
3766     s->iteration_initial_pages = ram_get_total_transferred_pages();
3767 }
3768 
3769 static void migration_update_counters(MigrationState *s,
3770                                       int64_t current_time)
3771 {
3772     uint64_t transferred, transferred_pages, time_spent;
3773     uint64_t current_bytes; /* bytes transferred since the beginning */
3774     double bandwidth;
3775 
3776     if (current_time < s->iteration_start_time + BUFFER_DELAY) {
3777         return;
3778     }
3779 
3780     current_bytes = migration_total_bytes(s);
3781     transferred = current_bytes - s->iteration_initial_bytes;
3782     time_spent = current_time - s->iteration_start_time;
3783     bandwidth = (double)transferred / time_spent;
3784     s->threshold_size = bandwidth * s->parameters.downtime_limit;
3785 
3786     s->mbps = (((double) transferred * 8.0) /
3787                ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
3788 
3789     transferred_pages = ram_get_total_transferred_pages() -
3790                             s->iteration_initial_pages;
3791     s->pages_per_second = (double) transferred_pages /
3792                              (((double) time_spent / 1000.0));
3793 
3794     /*
3795      * if we haven't sent anything, we don't want to
3796      * recalculate. 10000 is a small enough number for our purposes
3797      */
3798     if (ram_counters.dirty_pages_rate && transferred > 10000) {
3799         s->expected_downtime = ram_counters.remaining / bandwidth;
3800     }
3801 
3802     qemu_file_reset_rate_limit(s->to_dst_file);
3803 
3804     update_iteration_initial_status(s);
3805 
3806     trace_migrate_transferred(transferred, time_spent,
3807                               bandwidth, s->threshold_size);
3808 }
3809 
3810 /* Migration thread iteration status */
3811 typedef enum {
3812     MIG_ITERATE_RESUME,         /* Resume current iteration */
3813     MIG_ITERATE_SKIP,           /* Skip current iteration */
3814     MIG_ITERATE_BREAK,          /* Break the loop */
3815 } MigIterateState;
3816 
3817 /*
3818  * Return true if continue to the next iteration directly, false
3819  * otherwise.
3820  */
3821 static MigIterateState migration_iteration_run(MigrationState *s)
3822 {
3823     uint64_t pend_pre, pend_compat, pend_post;
3824     bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE;
3825 
3826     qemu_savevm_state_pending_estimate(&pend_pre, &pend_compat, &pend_post);
3827     uint64_t pending_size = pend_pre + pend_compat + pend_post;
3828 
3829     trace_migrate_pending_estimate(pending_size,
3830                                    pend_pre, pend_compat, pend_post);
3831 
3832     if (pend_pre + pend_compat <= s->threshold_size) {
3833         qemu_savevm_state_pending_exact(&pend_pre, &pend_compat, &pend_post);
3834         pending_size = pend_pre + pend_compat + pend_post;
3835         trace_migrate_pending_exact(pending_size,
3836                                     pend_pre, pend_compat, pend_post);
3837     }
3838 
3839     if (!pending_size || pending_size < s->threshold_size) {
3840         trace_migration_thread_low_pending(pending_size);
3841         migration_completion(s);
3842         return MIG_ITERATE_BREAK;
3843     }
3844 
3845     /* Still a significant amount to transfer */
3846     if (!in_postcopy && pend_pre <= s->threshold_size &&
3847         qatomic_read(&s->start_postcopy)) {
3848         if (postcopy_start(s)) {
3849             error_report("%s: postcopy failed to start", __func__);
3850         }
3851         return MIG_ITERATE_SKIP;
3852     }
3853 
3854     /* Just another iteration step */
3855     qemu_savevm_state_iterate(s->to_dst_file, in_postcopy);
3856     return MIG_ITERATE_RESUME;
3857 }
3858 
3859 static void migration_iteration_finish(MigrationState *s)
3860 {
3861     /* If we enabled cpu throttling for auto-converge, turn it off. */
3862     cpu_throttle_stop();
3863 
3864     qemu_mutex_lock_iothread();
3865     switch (s->state) {
3866     case MIGRATION_STATUS_COMPLETED:
3867         migration_calculate_complete(s);
3868         runstate_set(RUN_STATE_POSTMIGRATE);
3869         break;
3870     case MIGRATION_STATUS_COLO:
3871         if (!migrate_colo_enabled()) {
3872             error_report("%s: critical error: calling COLO code without "
3873                          "COLO enabled", __func__);
3874         }
3875         migrate_start_colo_process(s);
3876         s->vm_was_running = true;
3877         /* Fallthrough */
3878     case MIGRATION_STATUS_FAILED:
3879     case MIGRATION_STATUS_CANCELLED:
3880     case MIGRATION_STATUS_CANCELLING:
3881         if (s->vm_was_running) {
3882             if (!runstate_check(RUN_STATE_SHUTDOWN)) {
3883                 vm_start();
3884             }
3885         } else {
3886             if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
3887                 runstate_set(RUN_STATE_POSTMIGRATE);
3888             }
3889         }
3890         break;
3891 
3892     default:
3893         /* Should not reach here, but if so, forgive the VM. */
3894         error_report("%s: Unknown ending state %d", __func__, s->state);
3895         break;
3896     }
3897     migrate_fd_cleanup_schedule(s);
3898     qemu_mutex_unlock_iothread();
3899 }
3900 
3901 static void bg_migration_iteration_finish(MigrationState *s)
3902 {
3903     qemu_mutex_lock_iothread();
3904     switch (s->state) {
3905     case MIGRATION_STATUS_COMPLETED:
3906         migration_calculate_complete(s);
3907         break;
3908 
3909     case MIGRATION_STATUS_ACTIVE:
3910     case MIGRATION_STATUS_FAILED:
3911     case MIGRATION_STATUS_CANCELLED:
3912     case MIGRATION_STATUS_CANCELLING:
3913         break;
3914 
3915     default:
3916         /* Should not reach here, but if so, forgive the VM. */
3917         error_report("%s: Unknown ending state %d", __func__, s->state);
3918         break;
3919     }
3920 
3921     migrate_fd_cleanup_schedule(s);
3922     qemu_mutex_unlock_iothread();
3923 }
3924 
3925 /*
3926  * Return true if continue to the next iteration directly, false
3927  * otherwise.
3928  */
3929 static MigIterateState bg_migration_iteration_run(MigrationState *s)
3930 {
3931     int res;
3932 
3933     res = qemu_savevm_state_iterate(s->to_dst_file, false);
3934     if (res > 0) {
3935         bg_migration_completion(s);
3936         return MIG_ITERATE_BREAK;
3937     }
3938 
3939     return MIG_ITERATE_RESUME;
3940 }
3941 
3942 void migration_make_urgent_request(void)
3943 {
3944     qemu_sem_post(&migrate_get_current()->rate_limit_sem);
3945 }
3946 
3947 void migration_consume_urgent_request(void)
3948 {
3949     qemu_sem_wait(&migrate_get_current()->rate_limit_sem);
3950 }
3951 
3952 /* Returns true if the rate limiting was broken by an urgent request */
3953 bool migration_rate_limit(void)
3954 {
3955     int64_t now = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3956     MigrationState *s = migrate_get_current();
3957 
3958     bool urgent = false;
3959     migration_update_counters(s, now);
3960     if (qemu_file_rate_limit(s->to_dst_file)) {
3961 
3962         if (qemu_file_get_error(s->to_dst_file)) {
3963             return false;
3964         }
3965         /*
3966          * Wait for a delay to do rate limiting OR
3967          * something urgent to post the semaphore.
3968          */
3969         int ms = s->iteration_start_time + BUFFER_DELAY - now;
3970         trace_migration_rate_limit_pre(ms);
3971         if (qemu_sem_timedwait(&s->rate_limit_sem, ms) == 0) {
3972             /*
3973              * We were woken by one or more urgent things but
3974              * the timedwait will have consumed one of them.
3975              * The service routine for the urgent wake will dec
3976              * the semaphore itself for each item it consumes,
3977              * so add this one we just eat back.
3978              */
3979             qemu_sem_post(&s->rate_limit_sem);
3980             urgent = true;
3981         }
3982         trace_migration_rate_limit_post(urgent);
3983     }
3984     return urgent;
3985 }
3986 
3987 /*
3988  * if failover devices are present, wait they are completely
3989  * unplugged
3990  */
3991 
3992 static void qemu_savevm_wait_unplug(MigrationState *s, int old_state,
3993                                     int new_state)
3994 {
3995     if (qemu_savevm_state_guest_unplug_pending()) {
3996         migrate_set_state(&s->state, old_state, MIGRATION_STATUS_WAIT_UNPLUG);
3997 
3998         while (s->state == MIGRATION_STATUS_WAIT_UNPLUG &&
3999                qemu_savevm_state_guest_unplug_pending()) {
4000             qemu_sem_timedwait(&s->wait_unplug_sem, 250);
4001         }
4002         if (s->state != MIGRATION_STATUS_WAIT_UNPLUG) {
4003             int timeout = 120; /* 30 seconds */
4004             /*
4005              * migration has been canceled
4006              * but as we have started an unplug we must wait the end
4007              * to be able to plug back the card
4008              */
4009             while (timeout-- && qemu_savevm_state_guest_unplug_pending()) {
4010                 qemu_sem_timedwait(&s->wait_unplug_sem, 250);
4011             }
4012             if (qemu_savevm_state_guest_unplug_pending() &&
4013                 !qtest_enabled()) {
4014                 warn_report("migration: partially unplugged device on "
4015                             "failure");
4016             }
4017         }
4018 
4019         migrate_set_state(&s->state, MIGRATION_STATUS_WAIT_UNPLUG, new_state);
4020     } else {
4021         migrate_set_state(&s->state, old_state, new_state);
4022     }
4023 }
4024 
4025 /*
4026  * Master migration thread on the source VM.
4027  * It drives the migration and pumps the data down the outgoing channel.
4028  */
4029 static void *migration_thread(void *opaque)
4030 {
4031     MigrationState *s = opaque;
4032     MigrationThread *thread = NULL;
4033     int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
4034     MigThrError thr_error;
4035     bool urgent = false;
4036 
4037     thread = MigrationThreadAdd("live_migration", qemu_get_thread_id());
4038 
4039     rcu_register_thread();
4040 
4041     object_ref(OBJECT(s));
4042     update_iteration_initial_status(s);
4043 
4044     qemu_savevm_state_header(s->to_dst_file);
4045 
4046     /*
4047      * If we opened the return path, we need to make sure dst has it
4048      * opened as well.
4049      */
4050     if (s->rp_state.rp_thread_created) {
4051         /* Now tell the dest that it should open its end so it can reply */
4052         qemu_savevm_send_open_return_path(s->to_dst_file);
4053 
4054         /* And do a ping that will make stuff easier to debug */
4055         qemu_savevm_send_ping(s->to_dst_file, 1);
4056     }
4057 
4058     if (migrate_postcopy()) {
4059         /*
4060          * Tell the destination that we *might* want to do postcopy later;
4061          * if the other end can't do postcopy it should fail now, nice and
4062          * early.
4063          */
4064         qemu_savevm_send_postcopy_advise(s->to_dst_file);
4065     }
4066 
4067     if (migrate_colo_enabled()) {
4068         /* Notify migration destination that we enable COLO */
4069         qemu_savevm_send_colo_enable(s->to_dst_file);
4070     }
4071 
4072     qemu_savevm_state_setup(s->to_dst_file);
4073 
4074     qemu_savevm_wait_unplug(s, MIGRATION_STATUS_SETUP,
4075                                MIGRATION_STATUS_ACTIVE);
4076 
4077     s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
4078 
4079     trace_migration_thread_setup_complete();
4080 
4081     while (migration_is_active(s)) {
4082         if (urgent || !qemu_file_rate_limit(s->to_dst_file)) {
4083             MigIterateState iter_state = migration_iteration_run(s);
4084             if (iter_state == MIG_ITERATE_SKIP) {
4085                 continue;
4086             } else if (iter_state == MIG_ITERATE_BREAK) {
4087                 break;
4088             }
4089         }
4090 
4091         /*
4092          * Try to detect any kind of failures, and see whether we
4093          * should stop the migration now.
4094          */
4095         thr_error = migration_detect_error(s);
4096         if (thr_error == MIG_THR_ERR_FATAL) {
4097             /* Stop migration */
4098             break;
4099         } else if (thr_error == MIG_THR_ERR_RECOVERED) {
4100             /*
4101              * Just recovered from a e.g. network failure, reset all
4102              * the local variables. This is important to avoid
4103              * breaking transferred_bytes and bandwidth calculation
4104              */
4105             update_iteration_initial_status(s);
4106         }
4107 
4108         urgent = migration_rate_limit();
4109     }
4110 
4111     trace_migration_thread_after_loop();
4112     migration_iteration_finish(s);
4113     object_unref(OBJECT(s));
4114     rcu_unregister_thread();
4115     MigrationThreadDel(thread);
4116     return NULL;
4117 }
4118 
4119 static void bg_migration_vm_start_bh(void *opaque)
4120 {
4121     MigrationState *s = opaque;
4122 
4123     qemu_bh_delete(s->vm_start_bh);
4124     s->vm_start_bh = NULL;
4125 
4126     vm_start();
4127     s->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - s->downtime_start;
4128 }
4129 
4130 /**
4131  * Background snapshot thread, based on live migration code.
4132  * This is an alternative implementation of live migration mechanism
4133  * introduced specifically to support background snapshots.
4134  *
4135  * It takes advantage of userfault_fd write protection mechanism introduced
4136  * in v5.7 kernel. Compared to existing dirty page logging migration much
4137  * lesser stream traffic is produced resulting in smaller snapshot images,
4138  * simply cause of no page duplicates can get into the stream.
4139  *
4140  * Another key point is that generated vmstate stream reflects machine state
4141  * 'frozen' at the beginning of snapshot creation compared to dirty page logging
4142  * mechanism, which effectively results in that saved snapshot is the state of VM
4143  * at the end of the process.
4144  */
4145 static void *bg_migration_thread(void *opaque)
4146 {
4147     MigrationState *s = opaque;
4148     int64_t setup_start;
4149     MigThrError thr_error;
4150     QEMUFile *fb;
4151     bool early_fail = true;
4152 
4153     rcu_register_thread();
4154     object_ref(OBJECT(s));
4155 
4156     qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
4157 
4158     setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
4159     /*
4160      * We want to save vmstate for the moment when migration has been
4161      * initiated but also we want to save RAM content while VM is running.
4162      * The RAM content should appear first in the vmstate. So, we first
4163      * stash the non-RAM part of the vmstate to the temporary buffer,
4164      * then write RAM part of the vmstate to the migration stream
4165      * with vCPUs running and, finally, write stashed non-RAM part of
4166      * the vmstate from the buffer to the migration stream.
4167      */
4168     s->bioc = qio_channel_buffer_new(512 * 1024);
4169     qio_channel_set_name(QIO_CHANNEL(s->bioc), "vmstate-buffer");
4170     fb = qemu_file_new_output(QIO_CHANNEL(s->bioc));
4171     object_unref(OBJECT(s->bioc));
4172 
4173     update_iteration_initial_status(s);
4174 
4175     /*
4176      * Prepare for tracking memory writes with UFFD-WP - populate
4177      * RAM pages before protecting.
4178      */
4179 #ifdef __linux__
4180     ram_write_tracking_prepare();
4181 #endif
4182 
4183     qemu_savevm_state_header(s->to_dst_file);
4184     qemu_savevm_state_setup(s->to_dst_file);
4185 
4186     qemu_savevm_wait_unplug(s, MIGRATION_STATUS_SETUP,
4187                                MIGRATION_STATUS_ACTIVE);
4188 
4189     s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
4190 
4191     trace_migration_thread_setup_complete();
4192     s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
4193 
4194     qemu_mutex_lock_iothread();
4195 
4196     /*
4197      * If VM is currently in suspended state, then, to make a valid runstate
4198      * transition in vm_stop_force_state() we need to wakeup it up.
4199      */
4200     qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
4201     s->vm_was_running = runstate_is_running();
4202 
4203     if (global_state_store()) {
4204         goto fail;
4205     }
4206     /* Forcibly stop VM before saving state of vCPUs and devices */
4207     if (vm_stop_force_state(RUN_STATE_PAUSED)) {
4208         goto fail;
4209     }
4210     /*
4211      * Put vCPUs in sync with shadow context structures, then
4212      * save their state to channel-buffer along with devices.
4213      */
4214     cpu_synchronize_all_states();
4215     if (qemu_savevm_state_complete_precopy_non_iterable(fb, false, false)) {
4216         goto fail;
4217     }
4218     /*
4219      * Since we are going to get non-iterable state data directly
4220      * from s->bioc->data, explicit flush is needed here.
4221      */
4222     qemu_fflush(fb);
4223 
4224     /* Now initialize UFFD context and start tracking RAM writes */
4225     if (ram_write_tracking_start()) {
4226         goto fail;
4227     }
4228     early_fail = false;
4229 
4230     /*
4231      * Start VM from BH handler to avoid write-fault lock here.
4232      * UFFD-WP protection for the whole RAM is already enabled so
4233      * calling VM state change notifiers from vm_start() would initiate
4234      * writes to virtio VQs memory which is in write-protected region.
4235      */
4236     s->vm_start_bh = qemu_bh_new(bg_migration_vm_start_bh, s);
4237     qemu_bh_schedule(s->vm_start_bh);
4238 
4239     qemu_mutex_unlock_iothread();
4240 
4241     while (migration_is_active(s)) {
4242         MigIterateState iter_state = bg_migration_iteration_run(s);
4243         if (iter_state == MIG_ITERATE_SKIP) {
4244             continue;
4245         } else if (iter_state == MIG_ITERATE_BREAK) {
4246             break;
4247         }
4248 
4249         /*
4250          * Try to detect any kind of failures, and see whether we
4251          * should stop the migration now.
4252          */
4253         thr_error = migration_detect_error(s);
4254         if (thr_error == MIG_THR_ERR_FATAL) {
4255             /* Stop migration */
4256             break;
4257         }
4258 
4259         migration_update_counters(s, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
4260     }
4261 
4262     trace_migration_thread_after_loop();
4263 
4264 fail:
4265     if (early_fail) {
4266         migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE,
4267                 MIGRATION_STATUS_FAILED);
4268         qemu_mutex_unlock_iothread();
4269     }
4270 
4271     bg_migration_iteration_finish(s);
4272 
4273     qemu_fclose(fb);
4274     object_unref(OBJECT(s));
4275     rcu_unregister_thread();
4276 
4277     return NULL;
4278 }
4279 
4280 void migrate_fd_connect(MigrationState *s, Error *error_in)
4281 {
4282     Error *local_err = NULL;
4283     int64_t rate_limit;
4284     bool resume = s->state == MIGRATION_STATUS_POSTCOPY_PAUSED;
4285 
4286     /*
4287      * If there's a previous error, free it and prepare for another one.
4288      * Meanwhile if migration completes successfully, there won't have an error
4289      * dumped when calling migrate_fd_cleanup().
4290      */
4291     migrate_error_free(s);
4292 
4293     s->expected_downtime = s->parameters.downtime_limit;
4294     if (resume) {
4295         assert(s->cleanup_bh);
4296     } else {
4297         assert(!s->cleanup_bh);
4298         s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup_bh, s);
4299     }
4300     if (error_in) {
4301         migrate_fd_error(s, error_in);
4302         if (resume) {
4303             /*
4304              * Don't do cleanup for resume if channel is invalid, but only dump
4305              * the error.  We wait for another channel connect from the user.
4306              * The error_report still gives HMP user a hint on what failed.
4307              * It's normally done in migrate_fd_cleanup(), but call it here
4308              * explicitly.
4309              */
4310             error_report_err(error_copy(s->error));
4311         } else {
4312             migrate_fd_cleanup(s);
4313         }
4314         return;
4315     }
4316 
4317     if (resume) {
4318         /* This is a resumed migration */
4319         rate_limit = s->parameters.max_postcopy_bandwidth /
4320             XFER_LIMIT_RATIO;
4321     } else {
4322         /* This is a fresh new migration */
4323         rate_limit = s->parameters.max_bandwidth / XFER_LIMIT_RATIO;
4324 
4325         /* Notify before starting migration thread */
4326         notifier_list_notify(&migration_state_notifiers, s);
4327     }
4328 
4329     qemu_file_set_rate_limit(s->to_dst_file, rate_limit);
4330     qemu_file_set_blocking(s->to_dst_file, true);
4331 
4332     /*
4333      * Open the return path. For postcopy, it is used exclusively. For
4334      * precopy, only if user specified "return-path" capability would
4335      * QEMU uses the return path.
4336      */
4337     if (migrate_postcopy_ram() || migrate_use_return_path()) {
4338         if (open_return_path_on_source(s, !resume)) {
4339             error_report("Unable to open return-path for postcopy");
4340             migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
4341             migrate_fd_cleanup(s);
4342             return;
4343         }
4344     }
4345 
4346     /* This needs to be done before resuming a postcopy */
4347     if (postcopy_preempt_setup(s, &local_err)) {
4348         error_report_err(local_err);
4349         migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
4350                           MIGRATION_STATUS_FAILED);
4351         migrate_fd_cleanup(s);
4352         return;
4353     }
4354 
4355     if (resume) {
4356         /* Wakeup the main migration thread to do the recovery */
4357         migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
4358                           MIGRATION_STATUS_POSTCOPY_RECOVER);
4359         qemu_sem_post(&s->postcopy_pause_sem);
4360         return;
4361     }
4362 
4363     if (multifd_save_setup(&local_err) != 0) {
4364         error_report_err(local_err);
4365         migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
4366                           MIGRATION_STATUS_FAILED);
4367         migrate_fd_cleanup(s);
4368         return;
4369     }
4370 
4371     if (migrate_background_snapshot()) {
4372         qemu_thread_create(&s->thread, "bg_snapshot",
4373                 bg_migration_thread, s, QEMU_THREAD_JOINABLE);
4374     } else {
4375         qemu_thread_create(&s->thread, "live_migration",
4376                 migration_thread, s, QEMU_THREAD_JOINABLE);
4377     }
4378     s->migration_thread_running = true;
4379 }
4380 
4381 void migration_global_dump(Monitor *mon)
4382 {
4383     MigrationState *ms = migrate_get_current();
4384 
4385     monitor_printf(mon, "globals:\n");
4386     monitor_printf(mon, "store-global-state: %s\n",
4387                    ms->store_global_state ? "on" : "off");
4388     monitor_printf(mon, "only-migratable: %s\n",
4389                    only_migratable ? "on" : "off");
4390     monitor_printf(mon, "send-configuration: %s\n",
4391                    ms->send_configuration ? "on" : "off");
4392     monitor_printf(mon, "send-section-footer: %s\n",
4393                    ms->send_section_footer ? "on" : "off");
4394     monitor_printf(mon, "decompress-error-check: %s\n",
4395                    ms->decompress_error_check ? "on" : "off");
4396     monitor_printf(mon, "clear-bitmap-shift: %u\n",
4397                    ms->clear_bitmap_shift);
4398 }
4399 
4400 #define DEFINE_PROP_MIG_CAP(name, x)             \
4401     DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
4402 
4403 static Property migration_properties[] = {
4404     DEFINE_PROP_BOOL("store-global-state", MigrationState,
4405                      store_global_state, true),
4406     DEFINE_PROP_BOOL("send-configuration", MigrationState,
4407                      send_configuration, true),
4408     DEFINE_PROP_BOOL("send-section-footer", MigrationState,
4409                      send_section_footer, true),
4410     DEFINE_PROP_BOOL("decompress-error-check", MigrationState,
4411                       decompress_error_check, true),
4412     DEFINE_PROP_UINT8("x-clear-bitmap-shift", MigrationState,
4413                       clear_bitmap_shift, CLEAR_BITMAP_SHIFT_DEFAULT),
4414 
4415     /* Migration parameters */
4416     DEFINE_PROP_UINT8("x-compress-level", MigrationState,
4417                       parameters.compress_level,
4418                       DEFAULT_MIGRATE_COMPRESS_LEVEL),
4419     DEFINE_PROP_UINT8("x-compress-threads", MigrationState,
4420                       parameters.compress_threads,
4421                       DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
4422     DEFINE_PROP_BOOL("x-compress-wait-thread", MigrationState,
4423                       parameters.compress_wait_thread, true),
4424     DEFINE_PROP_UINT8("x-decompress-threads", MigrationState,
4425                       parameters.decompress_threads,
4426                       DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
4427     DEFINE_PROP_UINT8("x-throttle-trigger-threshold", MigrationState,
4428                       parameters.throttle_trigger_threshold,
4429                       DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD),
4430     DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
4431                       parameters.cpu_throttle_initial,
4432                       DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
4433     DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
4434                       parameters.cpu_throttle_increment,
4435                       DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
4436     DEFINE_PROP_BOOL("x-cpu-throttle-tailslow", MigrationState,
4437                       parameters.cpu_throttle_tailslow, false),
4438     DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
4439                       parameters.max_bandwidth, MAX_THROTTLE),
4440     DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
4441                       parameters.downtime_limit,
4442                       DEFAULT_MIGRATE_SET_DOWNTIME),
4443     DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
4444                       parameters.x_checkpoint_delay,
4445                       DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
4446     DEFINE_PROP_UINT8("multifd-channels", MigrationState,
4447                       parameters.multifd_channels,
4448                       DEFAULT_MIGRATE_MULTIFD_CHANNELS),
4449     DEFINE_PROP_MULTIFD_COMPRESSION("multifd-compression", MigrationState,
4450                       parameters.multifd_compression,
4451                       DEFAULT_MIGRATE_MULTIFD_COMPRESSION),
4452     DEFINE_PROP_UINT8("multifd-zlib-level", MigrationState,
4453                       parameters.multifd_zlib_level,
4454                       DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL),
4455     DEFINE_PROP_UINT8("multifd-zstd-level", MigrationState,
4456                       parameters.multifd_zstd_level,
4457                       DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL),
4458     DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
4459                       parameters.xbzrle_cache_size,
4460                       DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
4461     DEFINE_PROP_SIZE("max-postcopy-bandwidth", MigrationState,
4462                       parameters.max_postcopy_bandwidth,
4463                       DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH),
4464     DEFINE_PROP_UINT8("max-cpu-throttle", MigrationState,
4465                       parameters.max_cpu_throttle,
4466                       DEFAULT_MIGRATE_MAX_CPU_THROTTLE),
4467     DEFINE_PROP_SIZE("announce-initial", MigrationState,
4468                       parameters.announce_initial,
4469                       DEFAULT_MIGRATE_ANNOUNCE_INITIAL),
4470     DEFINE_PROP_SIZE("announce-max", MigrationState,
4471                       parameters.announce_max,
4472                       DEFAULT_MIGRATE_ANNOUNCE_MAX),
4473     DEFINE_PROP_SIZE("announce-rounds", MigrationState,
4474                       parameters.announce_rounds,
4475                       DEFAULT_MIGRATE_ANNOUNCE_ROUNDS),
4476     DEFINE_PROP_SIZE("announce-step", MigrationState,
4477                       parameters.announce_step,
4478                       DEFAULT_MIGRATE_ANNOUNCE_STEP),
4479     DEFINE_PROP_STRING("tls-creds", MigrationState, parameters.tls_creds),
4480     DEFINE_PROP_STRING("tls-hostname", MigrationState, parameters.tls_hostname),
4481     DEFINE_PROP_STRING("tls-authz", MigrationState, parameters.tls_authz),
4482 
4483     /* Migration capabilities */
4484     DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
4485     DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
4486     DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
4487     DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
4488     DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
4489     DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
4490     DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
4491     DEFINE_PROP_MIG_CAP("x-postcopy-preempt",
4492                         MIGRATION_CAPABILITY_POSTCOPY_PREEMPT),
4493     DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
4494     DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
4495     DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
4496     DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
4497     DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_MULTIFD),
4498     DEFINE_PROP_MIG_CAP("x-background-snapshot",
4499             MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT),
4500 #ifdef CONFIG_LINUX
4501     DEFINE_PROP_MIG_CAP("x-zero-copy-send",
4502             MIGRATION_CAPABILITY_ZERO_COPY_SEND),
4503 #endif
4504 
4505     DEFINE_PROP_END_OF_LIST(),
4506 };
4507 
4508 static void migration_class_init(ObjectClass *klass, void *data)
4509 {
4510     DeviceClass *dc = DEVICE_CLASS(klass);
4511 
4512     dc->user_creatable = false;
4513     device_class_set_props(dc, migration_properties);
4514 }
4515 
4516 static void migration_instance_finalize(Object *obj)
4517 {
4518     MigrationState *ms = MIGRATION_OBJ(obj);
4519 
4520     qemu_mutex_destroy(&ms->error_mutex);
4521     qemu_mutex_destroy(&ms->qemu_file_lock);
4522     qemu_sem_destroy(&ms->wait_unplug_sem);
4523     qemu_sem_destroy(&ms->rate_limit_sem);
4524     qemu_sem_destroy(&ms->pause_sem);
4525     qemu_sem_destroy(&ms->postcopy_pause_sem);
4526     qemu_sem_destroy(&ms->postcopy_pause_rp_sem);
4527     qemu_sem_destroy(&ms->rp_state.rp_sem);
4528     qemu_sem_destroy(&ms->postcopy_qemufile_src_sem);
4529     error_free(ms->error);
4530 }
4531 
4532 static void migration_instance_init(Object *obj)
4533 {
4534     MigrationState *ms = MIGRATION_OBJ(obj);
4535     MigrationParameters *params = &ms->parameters;
4536 
4537     ms->state = MIGRATION_STATUS_NONE;
4538     ms->mbps = -1;
4539     ms->pages_per_second = -1;
4540     qemu_sem_init(&ms->pause_sem, 0);
4541     qemu_mutex_init(&ms->error_mutex);
4542 
4543     params->tls_hostname = g_strdup("");
4544     params->tls_creds = g_strdup("");
4545 
4546     /* Set has_* up only for parameter checks */
4547     params->has_compress_level = true;
4548     params->has_compress_threads = true;
4549     params->has_compress_wait_thread = true;
4550     params->has_decompress_threads = true;
4551     params->has_throttle_trigger_threshold = true;
4552     params->has_cpu_throttle_initial = true;
4553     params->has_cpu_throttle_increment = true;
4554     params->has_cpu_throttle_tailslow = true;
4555     params->has_max_bandwidth = true;
4556     params->has_downtime_limit = true;
4557     params->has_x_checkpoint_delay = true;
4558     params->has_block_incremental = true;
4559     params->has_multifd_channels = true;
4560     params->has_multifd_compression = true;
4561     params->has_multifd_zlib_level = true;
4562     params->has_multifd_zstd_level = true;
4563     params->has_xbzrle_cache_size = true;
4564     params->has_max_postcopy_bandwidth = true;
4565     params->has_max_cpu_throttle = true;
4566     params->has_announce_initial = true;
4567     params->has_announce_max = true;
4568     params->has_announce_rounds = true;
4569     params->has_announce_step = true;
4570 
4571     qemu_sem_init(&ms->postcopy_pause_sem, 0);
4572     qemu_sem_init(&ms->postcopy_pause_rp_sem, 0);
4573     qemu_sem_init(&ms->rp_state.rp_sem, 0);
4574     qemu_sem_init(&ms->rate_limit_sem, 0);
4575     qemu_sem_init(&ms->wait_unplug_sem, 0);
4576     qemu_sem_init(&ms->postcopy_qemufile_src_sem, 0);
4577     qemu_mutex_init(&ms->qemu_file_lock);
4578 }
4579 
4580 /*
4581  * Return true if check pass, false otherwise. Error will be put
4582  * inside errp if provided.
4583  */
4584 static bool migration_object_check(MigrationState *ms, Error **errp)
4585 {
4586     MigrationCapabilityStatusList *head = NULL;
4587     /* Assuming all off */
4588     bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret;
4589     int i;
4590 
4591     if (!migrate_params_check(&ms->parameters, errp)) {
4592         return false;
4593     }
4594 
4595     for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
4596         if (ms->enabled_capabilities[i]) {
4597             QAPI_LIST_PREPEND(head, migrate_cap_add(i, true));
4598         }
4599     }
4600 
4601     ret = migrate_caps_check(cap_list, head, errp);
4602 
4603     /* It works with head == NULL */
4604     qapi_free_MigrationCapabilityStatusList(head);
4605 
4606     return ret;
4607 }
4608 
4609 static const TypeInfo migration_type = {
4610     .name = TYPE_MIGRATION,
4611     /*
4612      * NOTE: TYPE_MIGRATION is not really a device, as the object is
4613      * not created using qdev_new(), it is not attached to the qdev
4614      * device tree, and it is never realized.
4615      *
4616      * TODO: Make this TYPE_OBJECT once QOM provides something like
4617      * TYPE_DEVICE's "-global" properties.
4618      */
4619     .parent = TYPE_DEVICE,
4620     .class_init = migration_class_init,
4621     .class_size = sizeof(MigrationClass),
4622     .instance_size = sizeof(MigrationState),
4623     .instance_init = migration_instance_init,
4624     .instance_finalize = migration_instance_finalize,
4625 };
4626 
4627 static void register_migration_types(void)
4628 {
4629     type_register_static(&migration_type);
4630 }
4631 
4632 type_init(register_migration_types);
4633