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