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