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