]> Git Repo - qemu.git/blob - migration/migration.c
Merge remote-tracking branch 'ehabkost/tags/numa-pull-request' into staging
[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 "qemu/main-loop.h"
20 #include "migration/blocker.h"
21 #include "migration/migration.h"
22 #include "qemu-file-channel.h"
23 #include "migration/qemu-file.h"
24 #include "migration/vmstate.h"
25 #include "sysemu/sysemu.h"
26 #include "block/block.h"
27 #include "qapi/qmp/qerror.h"
28 #include "qapi/util.h"
29 #include "qemu/sockets.h"
30 #include "qemu/rcu.h"
31 #include "migration/block.h"
32 #include "postcopy-ram.h"
33 #include "qemu/thread.h"
34 #include "qmp-commands.h"
35 #include "trace.h"
36 #include "qapi-event.h"
37 #include "qom/cpu.h"
38 #include "exec/memory.h"
39 #include "exec/address-spaces.h"
40 #include "exec/target_page.h"
41 #include "io/channel-buffer.h"
42 #include "io/channel-tls.h"
43 #include "migration/colo.h"
44
45 #define MAX_THROTTLE  (32 << 20)      /* Migration transfer speed throttling */
46
47 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
48  * data. */
49 #define BUFFER_DELAY     100
50 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
51
52 /* Time in milliseconds we are allowed to stop the source,
53  * for sending the last part */
54 #define DEFAULT_MIGRATE_SET_DOWNTIME 300
55
56 /* Maximum migrate downtime set to 2000 seconds */
57 #define MAX_MIGRATE_DOWNTIME_SECONDS 2000
58 #define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
59
60 /* Default compression thread count */
61 #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
62 /* Default decompression thread count, usually decompression is at
63  * least 4 times as fast as compression.*/
64 #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
65 /*0: means nocompress, 1: best speed, ... 9: best compress ratio */
66 #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
67 /* Define default autoconverge cpu throttle migration parameters */
68 #define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
69 #define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
70
71 /* Migration XBZRLE default cache size */
72 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
73
74 /* The delay time (in ms) between two COLO checkpoints
75  * Note: Please change this default value to 10000 when we support hybrid mode.
76  */
77 #define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY 200
78
79 static NotifierList migration_state_notifiers =
80     NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
81
82 static bool deferred_incoming;
83
84 /* When we add fault tolerance, we could have several
85    migrations at once.  For now we don't need to add
86    dynamic creation of migration */
87
88 /* For outgoing */
89 MigrationState *migrate_get_current(void)
90 {
91     static bool once;
92     static MigrationState current_migration = {
93         .state = MIGRATION_STATUS_NONE,
94         .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
95         .mbps = -1,
96         .parameters = {
97             .compress_level = DEFAULT_MIGRATE_COMPRESS_LEVEL,
98             .compress_threads = DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
99             .decompress_threads = DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
100             .cpu_throttle_initial = DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL,
101             .cpu_throttle_increment = DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT,
102             .max_bandwidth = MAX_THROTTLE,
103             .downtime_limit = DEFAULT_MIGRATE_SET_DOWNTIME,
104             .x_checkpoint_delay = DEFAULT_MIGRATE_X_CHECKPOINT_DELAY,
105         },
106     };
107
108     if (!once) {
109         current_migration.parameters.tls_creds = g_strdup("");
110         current_migration.parameters.tls_hostname = g_strdup("");
111         once = true;
112     }
113     return &current_migration;
114 }
115
116 MigrationIncomingState *migration_incoming_get_current(void)
117 {
118     static bool once;
119     static MigrationIncomingState mis_current;
120
121     if (!once) {
122         mis_current.state = MIGRATION_STATUS_NONE;
123         memset(&mis_current, 0, sizeof(MigrationIncomingState));
124         QLIST_INIT(&mis_current.loadvm_handlers);
125         qemu_mutex_init(&mis_current.rp_mutex);
126         qemu_event_init(&mis_current.main_thread_load_event, false);
127         once = true;
128     }
129     return &mis_current;
130 }
131
132 void migration_incoming_state_destroy(void)
133 {
134     struct MigrationIncomingState *mis = migration_incoming_get_current();
135
136     qemu_event_destroy(&mis->main_thread_load_event);
137     loadvm_free_handlers(mis);
138 }
139
140
141 typedef struct {
142     bool optional;
143     uint32_t size;
144     uint8_t runstate[100];
145     RunState state;
146     bool received;
147 } GlobalState;
148
149 static GlobalState global_state;
150
151 int global_state_store(void)
152 {
153     if (!runstate_store((char *)global_state.runstate,
154                         sizeof(global_state.runstate))) {
155         error_report("runstate name too big: %s", global_state.runstate);
156         trace_migrate_state_too_big();
157         return -EINVAL;
158     }
159     return 0;
160 }
161
162 void global_state_store_running(void)
163 {
164     const char *state = RunState_lookup[RUN_STATE_RUNNING];
165     strncpy((char *)global_state.runstate,
166            state, sizeof(global_state.runstate));
167 }
168
169 static bool global_state_received(void)
170 {
171     return global_state.received;
172 }
173
174 static RunState global_state_get_runstate(void)
175 {
176     return global_state.state;
177 }
178
179 void global_state_set_optional(void)
180 {
181     global_state.optional = true;
182 }
183
184 static bool global_state_needed(void *opaque)
185 {
186     GlobalState *s = opaque;
187     char *runstate = (char *)s->runstate;
188
189     /* If it is not optional, it is mandatory */
190
191     if (s->optional == false) {
192         return true;
193     }
194
195     /* If state is running or paused, it is not needed */
196
197     if (strcmp(runstate, "running") == 0 ||
198         strcmp(runstate, "paused") == 0) {
199         return false;
200     }
201
202     /* for any other state it is needed */
203     return true;
204 }
205
206 static int global_state_post_load(void *opaque, int version_id)
207 {
208     GlobalState *s = opaque;
209     Error *local_err = NULL;
210     int r;
211     char *runstate = (char *)s->runstate;
212
213     s->received = true;
214     trace_migrate_global_state_post_load(runstate);
215
216     r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE__MAX,
217                                 -1, &local_err);
218
219     if (r == -1) {
220         if (local_err) {
221             error_report_err(local_err);
222         }
223         return -EINVAL;
224     }
225     s->state = r;
226
227     return 0;
228 }
229
230 static void global_state_pre_save(void *opaque)
231 {
232     GlobalState *s = opaque;
233
234     trace_migrate_global_state_pre_save((char *)s->runstate);
235     s->size = strlen((char *)s->runstate) + 1;
236 }
237
238 static const VMStateDescription vmstate_globalstate = {
239     .name = "globalstate",
240     .version_id = 1,
241     .minimum_version_id = 1,
242     .post_load = global_state_post_load,
243     .pre_save = global_state_pre_save,
244     .needed = global_state_needed,
245     .fields = (VMStateField[]) {
246         VMSTATE_UINT32(size, GlobalState),
247         VMSTATE_BUFFER(runstate, GlobalState),
248         VMSTATE_END_OF_LIST()
249     },
250 };
251
252 void register_global_state(void)
253 {
254     /* We would use it independently that we receive it */
255     strcpy((char *)&global_state.runstate, "");
256     global_state.received = false;
257     vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
258 }
259
260 static void migrate_generate_event(int new_state)
261 {
262     if (migrate_use_events()) {
263         qapi_event_send_migration(new_state, &error_abort);
264     }
265 }
266
267 /*
268  * Called on -incoming with a defer: uri.
269  * The migration can be started later after any parameters have been
270  * changed.
271  */
272 static void deferred_incoming_migration(Error **errp)
273 {
274     if (deferred_incoming) {
275         error_setg(errp, "Incoming migration already deferred");
276     }
277     deferred_incoming = true;
278 }
279
280 /* Request a range of pages from the source VM at the given
281  * start address.
282  *   rbname: Name of the RAMBlock to request the page in, if NULL it's the same
283  *           as the last request (a name must have been given previously)
284  *   Start: Address offset within the RB
285  *   Len: Length in bytes required - must be a multiple of pagesize
286  */
287 void migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
288                                ram_addr_t start, size_t len)
289 {
290     uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
291     size_t msglen = 12; /* start + len */
292
293     *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
294     *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
295
296     if (rbname) {
297         int rbname_len = strlen(rbname);
298         assert(rbname_len < 256);
299
300         bufc[msglen++] = rbname_len;
301         memcpy(bufc + msglen, rbname, rbname_len);
302         msglen += rbname_len;
303         migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES_ID, msglen, bufc);
304     } else {
305         migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES, msglen, bufc);
306     }
307 }
308
309 void qemu_start_incoming_migration(const char *uri, Error **errp)
310 {
311     const char *p;
312
313     qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
314     if (!strcmp(uri, "defer")) {
315         deferred_incoming_migration(errp);
316     } else if (strstart(uri, "tcp:", &p)) {
317         tcp_start_incoming_migration(p, errp);
318 #ifdef CONFIG_RDMA
319     } else if (strstart(uri, "rdma:", &p)) {
320         rdma_start_incoming_migration(p, errp);
321 #endif
322     } else if (strstart(uri, "exec:", &p)) {
323         exec_start_incoming_migration(p, errp);
324     } else if (strstart(uri, "unix:", &p)) {
325         unix_start_incoming_migration(p, errp);
326     } else if (strstart(uri, "fd:", &p)) {
327         fd_start_incoming_migration(p, errp);
328     } else {
329         error_setg(errp, "unknown migration protocol: %s", uri);
330     }
331 }
332
333 static void process_incoming_migration_bh(void *opaque)
334 {
335     Error *local_err = NULL;
336     MigrationIncomingState *mis = opaque;
337
338     /* Make sure all file formats flush their mutable metadata.
339      * If we get an error here, just don't restart the VM yet. */
340     bdrv_invalidate_cache_all(&local_err);
341     if (local_err) {
342         error_report_err(local_err);
343         local_err = NULL;
344         autostart = false;
345     }
346
347     /*
348      * This must happen after all error conditions are dealt with and
349      * we're sure the VM is going to be running on this host.
350      */
351     qemu_announce_self();
352
353     /* If global state section was not received or we are in running
354        state, we need to obey autostart. Any other state is set with
355        runstate_set. */
356
357     if (!global_state_received() ||
358         global_state_get_runstate() == RUN_STATE_RUNNING) {
359         if (autostart) {
360             vm_start();
361         } else {
362             runstate_set(RUN_STATE_PAUSED);
363         }
364     } else {
365         runstate_set(global_state_get_runstate());
366     }
367     migrate_decompress_threads_join();
368     /*
369      * This must happen after any state changes since as soon as an external
370      * observer sees this event they might start to prod at the VM assuming
371      * it's ready to use.
372      */
373     migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
374                       MIGRATION_STATUS_COMPLETED);
375     qemu_bh_delete(mis->bh);
376     migration_incoming_state_destroy();
377 }
378
379 static void process_incoming_migration_co(void *opaque)
380 {
381     QEMUFile *f = opaque;
382     MigrationIncomingState *mis = migration_incoming_get_current();
383     PostcopyState ps;
384     int ret;
385
386     mis->from_src_file = f;
387     mis->largest_page_size = qemu_ram_pagesize_largest();
388     postcopy_state_set(POSTCOPY_INCOMING_NONE);
389     migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
390                       MIGRATION_STATUS_ACTIVE);
391     ret = qemu_loadvm_state(f);
392
393     ps = postcopy_state_get();
394     trace_process_incoming_migration_co_end(ret, ps);
395     if (ps != POSTCOPY_INCOMING_NONE) {
396         if (ps == POSTCOPY_INCOMING_ADVISE) {
397             /*
398              * Where a migration had postcopy enabled (and thus went to advise)
399              * but managed to complete within the precopy period, we can use
400              * the normal exit.
401              */
402             postcopy_ram_incoming_cleanup(mis);
403         } else if (ret >= 0) {
404             /*
405              * Postcopy was started, cleanup should happen at the end of the
406              * postcopy thread.
407              */
408             trace_process_incoming_migration_co_postcopy_end_main();
409             return;
410         }
411         /* Else if something went wrong then just fall out of the normal exit */
412     }
413
414     /* we get COLO info, and know if we are in COLO mode */
415     if (!ret && migration_incoming_enable_colo()) {
416         mis->migration_incoming_co = qemu_coroutine_self();
417         qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
418              colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
419         mis->have_colo_incoming_thread = true;
420         qemu_coroutine_yield();
421
422         /* Wait checkpoint incoming thread exit before free resource */
423         qemu_thread_join(&mis->colo_incoming_thread);
424     }
425
426     if (ret < 0) {
427         migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
428                           MIGRATION_STATUS_FAILED);
429         error_report("load of migration failed: %s", strerror(-ret));
430         migrate_decompress_threads_join();
431         exit(EXIT_FAILURE);
432     }
433
434     qemu_fclose(f);
435     free_xbzrle_decoded_buf();
436
437     mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
438     qemu_bh_schedule(mis->bh);
439 }
440
441 void migration_fd_process_incoming(QEMUFile *f)
442 {
443     Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, f);
444
445     migrate_decompress_threads_create();
446     qemu_file_set_blocking(f, false);
447     qemu_coroutine_enter(co);
448 }
449
450 /*
451  * Send a message on the return channel back to the source
452  * of the migration.
453  */
454 void migrate_send_rp_message(MigrationIncomingState *mis,
455                              enum mig_rp_message_type message_type,
456                              uint16_t len, void *data)
457 {
458     trace_migrate_send_rp_message((int)message_type, len);
459     qemu_mutex_lock(&mis->rp_mutex);
460     qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
461     qemu_put_be16(mis->to_src_file, len);
462     qemu_put_buffer(mis->to_src_file, data, len);
463     qemu_fflush(mis->to_src_file);
464     qemu_mutex_unlock(&mis->rp_mutex);
465 }
466
467 /*
468  * Send a 'SHUT' message on the return channel with the given value
469  * to indicate that we've finished with the RP.  Non-0 value indicates
470  * error.
471  */
472 void migrate_send_rp_shut(MigrationIncomingState *mis,
473                           uint32_t value)
474 {
475     uint32_t buf;
476
477     buf = cpu_to_be32(value);
478     migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
479 }
480
481 /*
482  * Send a 'PONG' message on the return channel with the given value
483  * (normally in response to a 'PING')
484  */
485 void migrate_send_rp_pong(MigrationIncomingState *mis,
486                           uint32_t value)
487 {
488     uint32_t buf;
489
490     buf = cpu_to_be32(value);
491     migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
492 }
493
494 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
495 {
496     MigrationCapabilityStatusList *head = NULL;
497     MigrationCapabilityStatusList *caps;
498     MigrationState *s = migrate_get_current();
499     int i;
500
501     caps = NULL; /* silence compiler warning */
502     for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
503 #ifndef CONFIG_LIVE_BLOCK_MIGRATION
504         if (i == MIGRATION_CAPABILITY_BLOCK) {
505             continue;
506         }
507 #endif
508         if (i == MIGRATION_CAPABILITY_X_COLO && !colo_supported()) {
509             continue;
510         }
511         if (head == NULL) {
512             head = g_malloc0(sizeof(*caps));
513             caps = head;
514         } else {
515             caps->next = g_malloc0(sizeof(*caps));
516             caps = caps->next;
517         }
518         caps->value =
519             g_malloc(sizeof(*caps->value));
520         caps->value->capability = i;
521         caps->value->state = s->enabled_capabilities[i];
522     }
523
524     return head;
525 }
526
527 MigrationParameters *qmp_query_migrate_parameters(Error **errp)
528 {
529     MigrationParameters *params;
530     MigrationState *s = migrate_get_current();
531
532     params = g_malloc0(sizeof(*params));
533     params->has_compress_level = true;
534     params->compress_level = s->parameters.compress_level;
535     params->has_compress_threads = true;
536     params->compress_threads = s->parameters.compress_threads;
537     params->has_decompress_threads = true;
538     params->decompress_threads = s->parameters.decompress_threads;
539     params->has_cpu_throttle_initial = true;
540     params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
541     params->has_cpu_throttle_increment = true;
542     params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
543     params->has_tls_creds = !!s->parameters.tls_creds;
544     params->tls_creds = g_strdup(s->parameters.tls_creds);
545     params->has_tls_hostname = !!s->parameters.tls_hostname;
546     params->tls_hostname = g_strdup(s->parameters.tls_hostname);
547     params->has_max_bandwidth = true;
548     params->max_bandwidth = s->parameters.max_bandwidth;
549     params->has_downtime_limit = true;
550     params->downtime_limit = s->parameters.downtime_limit;
551     params->has_x_checkpoint_delay = true;
552     params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
553     params->has_block_incremental = true;
554     params->block_incremental = s->parameters.block_incremental;
555
556     return params;
557 }
558
559 /*
560  * Return true if we're already in the middle of a migration
561  * (i.e. any of the active or setup states)
562  */
563 static bool migration_is_setup_or_active(int state)
564 {
565     switch (state) {
566     case MIGRATION_STATUS_ACTIVE:
567     case MIGRATION_STATUS_POSTCOPY_ACTIVE:
568     case MIGRATION_STATUS_SETUP:
569         return true;
570
571     default:
572         return false;
573
574     }
575 }
576
577 static void get_xbzrle_cache_stats(MigrationInfo *info)
578 {
579     if (migrate_use_xbzrle()) {
580         info->has_xbzrle_cache = true;
581         info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
582         info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
583         info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
584         info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
585         info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
586         info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
587         info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
588     }
589 }
590
591 static void populate_ram_info(MigrationInfo *info, MigrationState *s)
592 {
593     info->has_ram = true;
594     info->ram = g_malloc0(sizeof(*info->ram));
595     info->ram->transferred = ram_bytes_transferred();
596     info->ram->total = ram_bytes_total();
597     info->ram->duplicate = dup_mig_pages_transferred();
598     /* legacy value.  It is not used anymore */
599     info->ram->skipped = 0;
600     info->ram->normal = norm_mig_pages_transferred();
601     info->ram->normal_bytes = norm_mig_pages_transferred() *
602         qemu_target_page_size();
603     info->ram->mbps = s->mbps;
604     info->ram->dirty_sync_count = ram_dirty_sync_count();
605     info->ram->postcopy_requests = ram_postcopy_requests();
606     info->ram->page_size = qemu_target_page_size();
607
608     if (s->state != MIGRATION_STATUS_COMPLETED) {
609         info->ram->remaining = ram_bytes_remaining();
610         info->ram->dirty_pages_rate = ram_dirty_pages_rate();
611     }
612 }
613
614 MigrationInfo *qmp_query_migrate(Error **errp)
615 {
616     MigrationInfo *info = g_malloc0(sizeof(*info));
617     MigrationState *s = migrate_get_current();
618
619     switch (s->state) {
620     case MIGRATION_STATUS_NONE:
621         /* no migration has happened ever */
622         break;
623     case MIGRATION_STATUS_SETUP:
624         info->has_status = true;
625         info->has_total_time = false;
626         break;
627     case MIGRATION_STATUS_ACTIVE:
628     case MIGRATION_STATUS_CANCELLING:
629         info->has_status = true;
630         info->has_total_time = true;
631         info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
632             - s->total_time;
633         info->has_expected_downtime = true;
634         info->expected_downtime = s->expected_downtime;
635         info->has_setup_time = true;
636         info->setup_time = s->setup_time;
637
638         populate_ram_info(info, s);
639
640         if (blk_mig_active()) {
641             info->has_disk = true;
642             info->disk = g_malloc0(sizeof(*info->disk));
643             info->disk->transferred = blk_mig_bytes_transferred();
644             info->disk->remaining = blk_mig_bytes_remaining();
645             info->disk->total = blk_mig_bytes_total();
646         }
647
648         if (cpu_throttle_active()) {
649             info->has_cpu_throttle_percentage = true;
650             info->cpu_throttle_percentage = cpu_throttle_get_percentage();
651         }
652
653         get_xbzrle_cache_stats(info);
654         break;
655     case MIGRATION_STATUS_POSTCOPY_ACTIVE:
656         /* Mostly the same as active; TODO add some postcopy stats */
657         info->has_status = true;
658         info->has_total_time = true;
659         info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
660             - s->total_time;
661         info->has_expected_downtime = true;
662         info->expected_downtime = s->expected_downtime;
663         info->has_setup_time = true;
664         info->setup_time = s->setup_time;
665
666         populate_ram_info(info, s);
667
668         if (blk_mig_active()) {
669             info->has_disk = true;
670             info->disk = g_malloc0(sizeof(*info->disk));
671             info->disk->transferred = blk_mig_bytes_transferred();
672             info->disk->remaining = blk_mig_bytes_remaining();
673             info->disk->total = blk_mig_bytes_total();
674         }
675
676         get_xbzrle_cache_stats(info);
677         break;
678     case MIGRATION_STATUS_COLO:
679         info->has_status = true;
680         /* TODO: display COLO specific information (checkpoint info etc.) */
681         break;
682     case MIGRATION_STATUS_COMPLETED:
683         get_xbzrle_cache_stats(info);
684
685         info->has_status = true;
686         info->has_total_time = true;
687         info->total_time = s->total_time;
688         info->has_downtime = true;
689         info->downtime = s->downtime;
690         info->has_setup_time = true;
691         info->setup_time = s->setup_time;
692
693         populate_ram_info(info, s);
694         break;
695     case MIGRATION_STATUS_FAILED:
696         info->has_status = true;
697         if (s->error) {
698             info->has_error_desc = true;
699             info->error_desc = g_strdup(error_get_pretty(s->error));
700         }
701         break;
702     case MIGRATION_STATUS_CANCELLED:
703         info->has_status = true;
704         break;
705     }
706     info->status = s->state;
707
708     return info;
709 }
710
711 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
712                                   Error **errp)
713 {
714     MigrationState *s = migrate_get_current();
715     MigrationCapabilityStatusList *cap;
716     bool old_postcopy_cap = migrate_postcopy_ram();
717
718     if (migration_is_setup_or_active(s->state)) {
719         error_setg(errp, QERR_MIGRATION_ACTIVE);
720         return;
721     }
722
723     for (cap = params; cap; cap = cap->next) {
724 #ifndef CONFIG_LIVE_BLOCK_MIGRATION
725         if (cap->value->capability == MIGRATION_CAPABILITY_BLOCK
726             && cap->value->state) {
727             error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
728                        "block migration");
729             error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
730             continue;
731         }
732 #endif
733         if (cap->value->capability == MIGRATION_CAPABILITY_X_COLO) {
734             if (!colo_supported()) {
735                 error_setg(errp, "COLO is not currently supported, please"
736                              " configure with --enable-colo option in order to"
737                              " support COLO feature");
738                 continue;
739             }
740         }
741         s->enabled_capabilities[cap->value->capability] = cap->value->state;
742     }
743
744     if (migrate_postcopy_ram()) {
745         if (migrate_use_compression()) {
746             /* The decompression threads asynchronously write into RAM
747              * rather than use the atomic copies needed to avoid
748              * userfaulting.  It should be possible to fix the decompression
749              * threads for compatibility in future.
750              */
751             error_report("Postcopy is not currently compatible with "
752                          "compression");
753             s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM] =
754                 false;
755         }
756         /* This check is reasonably expensive, so only when it's being
757          * set the first time, also it's only the destination that needs
758          * special support.
759          */
760         if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
761             !postcopy_ram_supported_by_host()) {
762             /* postcopy_ram_supported_by_host will have emitted a more
763              * detailed message
764              */
765             error_report("Postcopy is not supported");
766             s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM] =
767                 false;
768         }
769     }
770 }
771
772 void qmp_migrate_set_parameters(MigrationParameters *params, Error **errp)
773 {
774     MigrationState *s = migrate_get_current();
775
776     if (params->has_compress_level &&
777         (params->compress_level < 0 || params->compress_level > 9)) {
778         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
779                    "is invalid, it should be in the range of 0 to 9");
780         return;
781     }
782     if (params->has_compress_threads &&
783         (params->compress_threads < 1 || params->compress_threads > 255)) {
784         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
785                    "compress_threads",
786                    "is invalid, it should be in the range of 1 to 255");
787         return;
788     }
789     if (params->has_decompress_threads &&
790         (params->decompress_threads < 1 || params->decompress_threads > 255)) {
791         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
792                    "decompress_threads",
793                    "is invalid, it should be in the range of 1 to 255");
794         return;
795     }
796     if (params->has_cpu_throttle_initial &&
797         (params->cpu_throttle_initial < 1 ||
798          params->cpu_throttle_initial > 99)) {
799         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
800                    "cpu_throttle_initial",
801                    "an integer in the range of 1 to 99");
802         return;
803     }
804     if (params->has_cpu_throttle_increment &&
805         (params->cpu_throttle_increment < 1 ||
806          params->cpu_throttle_increment > 99)) {
807         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
808                    "cpu_throttle_increment",
809                    "an integer in the range of 1 to 99");
810         return;
811     }
812     if (params->has_max_bandwidth &&
813         (params->max_bandwidth < 0 || params->max_bandwidth > SIZE_MAX)) {
814         error_setg(errp, "Parameter 'max_bandwidth' expects an integer in the"
815                          " range of 0 to %zu bytes/second", SIZE_MAX);
816         return;
817     }
818     if (params->has_downtime_limit &&
819         (params->downtime_limit < 0 ||
820          params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
821         error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
822                          "the range of 0 to %d milliseconds",
823                          MAX_MIGRATE_DOWNTIME);
824         return;
825     }
826     if (params->has_x_checkpoint_delay && (params->x_checkpoint_delay < 0)) {
827         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
828                     "x_checkpoint_delay",
829                     "is invalid, it should be positive");
830     }
831
832     if (params->has_compress_level) {
833         s->parameters.compress_level = params->compress_level;
834     }
835     if (params->has_compress_threads) {
836         s->parameters.compress_threads = params->compress_threads;
837     }
838     if (params->has_decompress_threads) {
839         s->parameters.decompress_threads = params->decompress_threads;
840     }
841     if (params->has_cpu_throttle_initial) {
842         s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
843     }
844     if (params->has_cpu_throttle_increment) {
845         s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
846     }
847     if (params->has_tls_creds) {
848         g_free(s->parameters.tls_creds);
849         s->parameters.tls_creds = g_strdup(params->tls_creds);
850     }
851     if (params->has_tls_hostname) {
852         g_free(s->parameters.tls_hostname);
853         s->parameters.tls_hostname = g_strdup(params->tls_hostname);
854     }
855     if (params->has_max_bandwidth) {
856         s->parameters.max_bandwidth = params->max_bandwidth;
857         if (s->to_dst_file) {
858             qemu_file_set_rate_limit(s->to_dst_file,
859                                 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
860         }
861     }
862     if (params->has_downtime_limit) {
863         s->parameters.downtime_limit = params->downtime_limit;
864     }
865
866     if (params->has_x_checkpoint_delay) {
867         s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
868         if (migration_in_colo_state()) {
869             colo_checkpoint_notify(s);
870         }
871     }
872     if (params->has_block_incremental) {
873         s->parameters.block_incremental = params->block_incremental;
874     }
875 }
876
877
878 void qmp_migrate_start_postcopy(Error **errp)
879 {
880     MigrationState *s = migrate_get_current();
881
882     if (!migrate_postcopy_ram()) {
883         error_setg(errp, "Enable postcopy with migrate_set_capability before"
884                          " the start of migration");
885         return;
886     }
887
888     if (s->state == MIGRATION_STATUS_NONE) {
889         error_setg(errp, "Postcopy must be started after migration has been"
890                          " started");
891         return;
892     }
893     /*
894      * we don't error if migration has finished since that would be racy
895      * with issuing this command.
896      */
897     atomic_set(&s->start_postcopy, true);
898 }
899
900 /* shared migration helpers */
901
902 void migrate_set_state(int *state, int old_state, int new_state)
903 {
904     if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
905         trace_migrate_set_state(new_state);
906         migrate_generate_event(new_state);
907     }
908 }
909
910 void migrate_set_block_enabled(bool value, Error **errp)
911 {
912     MigrationCapabilityStatusList *cap;
913
914     cap = g_new0(MigrationCapabilityStatusList, 1);
915     cap->value = g_new0(MigrationCapabilityStatus, 1);
916     cap->value->capability = MIGRATION_CAPABILITY_BLOCK;
917     cap->value->state = value;
918     qmp_migrate_set_capabilities(cap, errp);
919     qapi_free_MigrationCapabilityStatusList(cap);
920 }
921
922 static void migrate_set_block_incremental(MigrationState *s, bool value)
923 {
924     s->parameters.block_incremental = value;
925 }
926
927 static void block_cleanup_parameters(MigrationState *s)
928 {
929     if (s->must_remove_block_options) {
930         /* setting to false can never fail */
931         migrate_set_block_enabled(false, &error_abort);
932         migrate_set_block_incremental(s, false);
933         s->must_remove_block_options = false;
934     }
935 }
936
937 static void migrate_fd_cleanup(void *opaque)
938 {
939     MigrationState *s = opaque;
940
941     qemu_bh_delete(s->cleanup_bh);
942     s->cleanup_bh = NULL;
943
944     migration_page_queue_free();
945
946     if (s->to_dst_file) {
947         trace_migrate_fd_cleanup();
948         qemu_mutex_unlock_iothread();
949         if (s->migration_thread_running) {
950             qemu_thread_join(&s->thread);
951             s->migration_thread_running = false;
952         }
953         qemu_mutex_lock_iothread();
954
955         migrate_compress_threads_join();
956         qemu_fclose(s->to_dst_file);
957         s->to_dst_file = NULL;
958     }
959
960     assert((s->state != MIGRATION_STATUS_ACTIVE) &&
961            (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
962
963     if (s->state == MIGRATION_STATUS_CANCELLING) {
964         migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
965                           MIGRATION_STATUS_CANCELLED);
966     }
967
968     notifier_list_notify(&migration_state_notifiers, s);
969     block_cleanup_parameters(s);
970 }
971
972 void migrate_fd_error(MigrationState *s, const Error *error)
973 {
974     trace_migrate_fd_error(error_get_pretty(error));
975     assert(s->to_dst_file == NULL);
976     migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
977                       MIGRATION_STATUS_FAILED);
978     if (!s->error) {
979         s->error = error_copy(error);
980     }
981     notifier_list_notify(&migration_state_notifiers, s);
982     block_cleanup_parameters(s);
983 }
984
985 static void migrate_fd_cancel(MigrationState *s)
986 {
987     int old_state ;
988     QEMUFile *f = migrate_get_current()->to_dst_file;
989     trace_migrate_fd_cancel();
990
991     if (s->rp_state.from_dst_file) {
992         /* shutdown the rp socket, so causing the rp thread to shutdown */
993         qemu_file_shutdown(s->rp_state.from_dst_file);
994     }
995
996     do {
997         old_state = s->state;
998         if (!migration_is_setup_or_active(old_state)) {
999             break;
1000         }
1001         migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
1002     } while (s->state != MIGRATION_STATUS_CANCELLING);
1003
1004     /*
1005      * If we're unlucky the migration code might be stuck somewhere in a
1006      * send/write while the network has failed and is waiting to timeout;
1007      * if we've got shutdown(2) available then we can force it to quit.
1008      * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1009      * called in a bh, so there is no race against this cancel.
1010      */
1011     if (s->state == MIGRATION_STATUS_CANCELLING && f) {
1012         qemu_file_shutdown(f);
1013     }
1014     if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
1015         Error *local_err = NULL;
1016
1017         bdrv_invalidate_cache_all(&local_err);
1018         if (local_err) {
1019             error_report_err(local_err);
1020         } else {
1021             s->block_inactive = false;
1022         }
1023     }
1024     block_cleanup_parameters(s);
1025 }
1026
1027 void add_migration_state_change_notifier(Notifier *notify)
1028 {
1029     notifier_list_add(&migration_state_notifiers, notify);
1030 }
1031
1032 void remove_migration_state_change_notifier(Notifier *notify)
1033 {
1034     notifier_remove(notify);
1035 }
1036
1037 bool migration_in_setup(MigrationState *s)
1038 {
1039     return s->state == MIGRATION_STATUS_SETUP;
1040 }
1041
1042 bool migration_has_finished(MigrationState *s)
1043 {
1044     return s->state == MIGRATION_STATUS_COMPLETED;
1045 }
1046
1047 bool migration_has_failed(MigrationState *s)
1048 {
1049     return (s->state == MIGRATION_STATUS_CANCELLED ||
1050             s->state == MIGRATION_STATUS_FAILED);
1051 }
1052
1053 bool migration_in_postcopy(void)
1054 {
1055     MigrationState *s = migrate_get_current();
1056
1057     return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1058 }
1059
1060 bool migration_in_postcopy_after_devices(MigrationState *s)
1061 {
1062     return migration_in_postcopy() && s->postcopy_after_devices;
1063 }
1064
1065 bool migration_is_idle(void)
1066 {
1067     MigrationState *s = migrate_get_current();
1068
1069     switch (s->state) {
1070     case MIGRATION_STATUS_NONE:
1071     case MIGRATION_STATUS_CANCELLED:
1072     case MIGRATION_STATUS_COMPLETED:
1073     case MIGRATION_STATUS_FAILED:
1074         return true;
1075     case MIGRATION_STATUS_SETUP:
1076     case MIGRATION_STATUS_CANCELLING:
1077     case MIGRATION_STATUS_ACTIVE:
1078     case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1079     case MIGRATION_STATUS_COLO:
1080         return false;
1081     case MIGRATION_STATUS__MAX:
1082         g_assert_not_reached();
1083     }
1084
1085     return false;
1086 }
1087
1088 MigrationState *migrate_init(void)
1089 {
1090     MigrationState *s = migrate_get_current();
1091
1092     /*
1093      * Reinitialise all migration state, except
1094      * parameters/capabilities that the user set, and
1095      * locks.
1096      */
1097     s->bytes_xfer = 0;
1098     s->xfer_limit = 0;
1099     s->cleanup_bh = 0;
1100     s->to_dst_file = NULL;
1101     s->state = MIGRATION_STATUS_NONE;
1102     s->rp_state.from_dst_file = NULL;
1103     s->rp_state.error = false;
1104     s->mbps = 0.0;
1105     s->downtime = 0;
1106     s->expected_downtime = 0;
1107     s->setup_time = 0;
1108     s->start_postcopy = false;
1109     s->postcopy_after_devices = false;
1110     s->migration_thread_running = false;
1111     error_free(s->error);
1112     s->error = NULL;
1113
1114     migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
1115
1116     s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1117     return s;
1118 }
1119
1120 static GSList *migration_blockers;
1121
1122 int migrate_add_blocker(Error *reason, Error **errp)
1123 {
1124     if (only_migratable) {
1125         error_propagate(errp, error_copy(reason));
1126         error_prepend(errp, "disallowing migration blocker "
1127                           "(--only_migratable) for: ");
1128         return -EACCES;
1129     }
1130
1131     if (migration_is_idle()) {
1132         migration_blockers = g_slist_prepend(migration_blockers, reason);
1133         return 0;
1134     }
1135
1136     error_propagate(errp, error_copy(reason));
1137     error_prepend(errp, "disallowing migration blocker (migration in "
1138                       "progress) for: ");
1139     return -EBUSY;
1140 }
1141
1142 void migrate_del_blocker(Error *reason)
1143 {
1144     migration_blockers = g_slist_remove(migration_blockers, reason);
1145 }
1146
1147 void qmp_migrate_incoming(const char *uri, Error **errp)
1148 {
1149     Error *local_err = NULL;
1150     static bool once = true;
1151
1152     if (!deferred_incoming) {
1153         error_setg(errp, "For use with '-incoming defer'");
1154         return;
1155     }
1156     if (!once) {
1157         error_setg(errp, "The incoming migration has already been started");
1158     }
1159
1160     qemu_start_incoming_migration(uri, &local_err);
1161
1162     if (local_err) {
1163         error_propagate(errp, local_err);
1164         return;
1165     }
1166
1167     once = false;
1168 }
1169
1170 bool migration_is_blocked(Error **errp)
1171 {
1172     if (qemu_savevm_state_blocked(errp)) {
1173         return true;
1174     }
1175
1176     if (migration_blockers) {
1177         *errp = error_copy(migration_blockers->data);
1178         return true;
1179     }
1180
1181     return false;
1182 }
1183
1184 void qmp_migrate(const char *uri, bool has_blk, bool blk,
1185                  bool has_inc, bool inc, bool has_detach, bool detach,
1186                  Error **errp)
1187 {
1188     Error *local_err = NULL;
1189     MigrationState *s = migrate_get_current();
1190     const char *p;
1191
1192     if (migration_is_setup_or_active(s->state) ||
1193         s->state == MIGRATION_STATUS_CANCELLING ||
1194         s->state == MIGRATION_STATUS_COLO) {
1195         error_setg(errp, QERR_MIGRATION_ACTIVE);
1196         return;
1197     }
1198     if (runstate_check(RUN_STATE_INMIGRATE)) {
1199         error_setg(errp, "Guest is waiting for an incoming migration");
1200         return;
1201     }
1202
1203     if (migration_is_blocked(errp)) {
1204         return;
1205     }
1206
1207     if ((has_blk && blk) || (has_inc && inc)) {
1208         if (migrate_use_block() || migrate_use_block_incremental()) {
1209             error_setg(errp, "Command options are incompatible with "
1210                        "current migration capabilities");
1211             return;
1212         }
1213         migrate_set_block_enabled(true, &local_err);
1214         if (local_err) {
1215             error_propagate(errp, local_err);
1216             return;
1217         }
1218         s->must_remove_block_options = true;
1219     }
1220
1221     if (has_inc && inc) {
1222         migrate_set_block_incremental(s, true);
1223     }
1224
1225     s = migrate_init();
1226
1227     if (strstart(uri, "tcp:", &p)) {
1228         tcp_start_outgoing_migration(s, p, &local_err);
1229 #ifdef CONFIG_RDMA
1230     } else if (strstart(uri, "rdma:", &p)) {
1231         rdma_start_outgoing_migration(s, p, &local_err);
1232 #endif
1233     } else if (strstart(uri, "exec:", &p)) {
1234         exec_start_outgoing_migration(s, p, &local_err);
1235     } else if (strstart(uri, "unix:", &p)) {
1236         unix_start_outgoing_migration(s, p, &local_err);
1237     } else if (strstart(uri, "fd:", &p)) {
1238         fd_start_outgoing_migration(s, p, &local_err);
1239     } else {
1240         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1241                    "a valid migration protocol");
1242         migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1243                           MIGRATION_STATUS_FAILED);
1244         return;
1245     }
1246
1247     if (local_err) {
1248         migrate_fd_error(s, local_err);
1249         error_propagate(errp, local_err);
1250         return;
1251     }
1252 }
1253
1254 void qmp_migrate_cancel(Error **errp)
1255 {
1256     migrate_fd_cancel(migrate_get_current());
1257 }
1258
1259 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1260 {
1261     MigrationState *s = migrate_get_current();
1262     int64_t new_size;
1263
1264     /* Check for truncation */
1265     if (value != (size_t)value) {
1266         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1267                    "exceeding address space");
1268         return;
1269     }
1270
1271     /* Cache should not be larger than guest ram size */
1272     if (value > ram_bytes_total()) {
1273         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1274                    "exceeds guest ram size ");
1275         return;
1276     }
1277
1278     new_size = xbzrle_cache_resize(value);
1279     if (new_size < 0) {
1280         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1281                    "is smaller than page size");
1282         return;
1283     }
1284
1285     s->xbzrle_cache_size = new_size;
1286 }
1287
1288 int64_t qmp_query_migrate_cache_size(Error **errp)
1289 {
1290     return migrate_xbzrle_cache_size();
1291 }
1292
1293 void qmp_migrate_set_speed(int64_t value, Error **errp)
1294 {
1295     MigrationParameters p = {
1296         .has_max_bandwidth = true,
1297         .max_bandwidth = value,
1298     };
1299
1300     qmp_migrate_set_parameters(&p, errp);
1301 }
1302
1303 void qmp_migrate_set_downtime(double value, Error **errp)
1304 {
1305     if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
1306         error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
1307                          "the range of 0 to %d seconds",
1308                          MAX_MIGRATE_DOWNTIME_SECONDS);
1309         return;
1310     }
1311
1312     value *= 1000; /* Convert to milliseconds */
1313     value = MAX(0, MIN(INT64_MAX, value));
1314
1315     MigrationParameters p = {
1316         .has_downtime_limit = true,
1317         .downtime_limit = value,
1318     };
1319
1320     qmp_migrate_set_parameters(&p, errp);
1321 }
1322
1323 bool migrate_release_ram(void)
1324 {
1325     MigrationState *s;
1326
1327     s = migrate_get_current();
1328
1329     return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
1330 }
1331
1332 bool migrate_postcopy_ram(void)
1333 {
1334     MigrationState *s;
1335
1336     s = migrate_get_current();
1337
1338     return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
1339 }
1340
1341 bool migrate_auto_converge(void)
1342 {
1343     MigrationState *s;
1344
1345     s = migrate_get_current();
1346
1347     return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1348 }
1349
1350 bool migrate_zero_blocks(void)
1351 {
1352     MigrationState *s;
1353
1354     s = migrate_get_current();
1355
1356     return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1357 }
1358
1359 bool migrate_use_compression(void)
1360 {
1361     MigrationState *s;
1362
1363     s = migrate_get_current();
1364
1365     return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
1366 }
1367
1368 int migrate_compress_level(void)
1369 {
1370     MigrationState *s;
1371
1372     s = migrate_get_current();
1373
1374     return s->parameters.compress_level;
1375 }
1376
1377 int migrate_compress_threads(void)
1378 {
1379     MigrationState *s;
1380
1381     s = migrate_get_current();
1382
1383     return s->parameters.compress_threads;
1384 }
1385
1386 int migrate_decompress_threads(void)
1387 {
1388     MigrationState *s;
1389
1390     s = migrate_get_current();
1391
1392     return s->parameters.decompress_threads;
1393 }
1394
1395 bool migrate_use_events(void)
1396 {
1397     MigrationState *s;
1398
1399     s = migrate_get_current();
1400
1401     return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1402 }
1403
1404 int migrate_use_xbzrle(void)
1405 {
1406     MigrationState *s;
1407
1408     s = migrate_get_current();
1409
1410     return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1411 }
1412
1413 int64_t migrate_xbzrle_cache_size(void)
1414 {
1415     MigrationState *s;
1416
1417     s = migrate_get_current();
1418
1419     return s->xbzrle_cache_size;
1420 }
1421
1422 bool migrate_use_block(void)
1423 {
1424     MigrationState *s;
1425
1426     s = migrate_get_current();
1427
1428     return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
1429 }
1430
1431 bool migrate_use_block_incremental(void)
1432 {
1433     MigrationState *s;
1434
1435     s = migrate_get_current();
1436
1437     return s->parameters.block_incremental;
1438 }
1439
1440 /* migration thread support */
1441 /*
1442  * Something bad happened to the RP stream, mark an error
1443  * The caller shall print or trace something to indicate why
1444  */
1445 static void mark_source_rp_bad(MigrationState *s)
1446 {
1447     s->rp_state.error = true;
1448 }
1449
1450 static struct rp_cmd_args {
1451     ssize_t     len; /* -1 = variable */
1452     const char *name;
1453 } rp_cmd_args[] = {
1454     [MIG_RP_MSG_INVALID]        = { .len = -1, .name = "INVALID" },
1455     [MIG_RP_MSG_SHUT]           = { .len =  4, .name = "SHUT" },
1456     [MIG_RP_MSG_PONG]           = { .len =  4, .name = "PONG" },
1457     [MIG_RP_MSG_REQ_PAGES]      = { .len = 12, .name = "REQ_PAGES" },
1458     [MIG_RP_MSG_REQ_PAGES_ID]   = { .len = -1, .name = "REQ_PAGES_ID" },
1459     [MIG_RP_MSG_MAX]            = { .len = -1, .name = "MAX" },
1460 };
1461
1462 /*
1463  * Process a request for pages received on the return path,
1464  * We're allowed to send more than requested (e.g. to round to our page size)
1465  * and we don't need to send pages that have already been sent.
1466  */
1467 static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1468                                        ram_addr_t start, size_t len)
1469 {
1470     long our_host_ps = getpagesize();
1471
1472     trace_migrate_handle_rp_req_pages(rbname, start, len);
1473
1474     /*
1475      * Since we currently insist on matching page sizes, just sanity check
1476      * we're being asked for whole host pages.
1477      */
1478     if (start & (our_host_ps-1) ||
1479        (len & (our_host_ps-1))) {
1480         error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1481                      " len: %zd", __func__, start, len);
1482         mark_source_rp_bad(ms);
1483         return;
1484     }
1485
1486     if (ram_save_queue_pages(rbname, start, len)) {
1487         mark_source_rp_bad(ms);
1488     }
1489 }
1490
1491 /*
1492  * Handles messages sent on the return path towards the source VM
1493  *
1494  */
1495 static void *source_return_path_thread(void *opaque)
1496 {
1497     MigrationState *ms = opaque;
1498     QEMUFile *rp = ms->rp_state.from_dst_file;
1499     uint16_t header_len, header_type;
1500     uint8_t buf[512];
1501     uint32_t tmp32, sibling_error;
1502     ram_addr_t start = 0; /* =0 to silence warning */
1503     size_t  len = 0, expected_len;
1504     int res;
1505
1506     trace_source_return_path_thread_entry();
1507     while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1508            migration_is_setup_or_active(ms->state)) {
1509         trace_source_return_path_thread_loop_top();
1510         header_type = qemu_get_be16(rp);
1511         header_len = qemu_get_be16(rp);
1512
1513         if (header_type >= MIG_RP_MSG_MAX ||
1514             header_type == MIG_RP_MSG_INVALID) {
1515             error_report("RP: Received invalid message 0x%04x length 0x%04x",
1516                     header_type, header_len);
1517             mark_source_rp_bad(ms);
1518             goto out;
1519         }
1520
1521         if ((rp_cmd_args[header_type].len != -1 &&
1522             header_len != rp_cmd_args[header_type].len) ||
1523             header_len > sizeof(buf)) {
1524             error_report("RP: Received '%s' message (0x%04x) with"
1525                     "incorrect length %d expecting %zu",
1526                     rp_cmd_args[header_type].name, header_type, header_len,
1527                     (size_t)rp_cmd_args[header_type].len);
1528             mark_source_rp_bad(ms);
1529             goto out;
1530         }
1531
1532         /* We know we've got a valid header by this point */
1533         res = qemu_get_buffer(rp, buf, header_len);
1534         if (res != header_len) {
1535             error_report("RP: Failed reading data for message 0x%04x"
1536                          " read %d expected %d",
1537                          header_type, res, header_len);
1538             mark_source_rp_bad(ms);
1539             goto out;
1540         }
1541
1542         /* OK, we have the message and the data */
1543         switch (header_type) {
1544         case MIG_RP_MSG_SHUT:
1545             sibling_error = ldl_be_p(buf);
1546             trace_source_return_path_thread_shut(sibling_error);
1547             if (sibling_error) {
1548                 error_report("RP: Sibling indicated error %d", sibling_error);
1549                 mark_source_rp_bad(ms);
1550             }
1551             /*
1552              * We'll let the main thread deal with closing the RP
1553              * we could do a shutdown(2) on it, but we're the only user
1554              * anyway, so there's nothing gained.
1555              */
1556             goto out;
1557
1558         case MIG_RP_MSG_PONG:
1559             tmp32 = ldl_be_p(buf);
1560             trace_source_return_path_thread_pong(tmp32);
1561             break;
1562
1563         case MIG_RP_MSG_REQ_PAGES:
1564             start = ldq_be_p(buf);
1565             len = ldl_be_p(buf + 8);
1566             migrate_handle_rp_req_pages(ms, NULL, start, len);
1567             break;
1568
1569         case MIG_RP_MSG_REQ_PAGES_ID:
1570             expected_len = 12 + 1; /* header + termination */
1571
1572             if (header_len >= expected_len) {
1573                 start = ldq_be_p(buf);
1574                 len = ldl_be_p(buf + 8);
1575                 /* Now we expect an idstr */
1576                 tmp32 = buf[12]; /* Length of the following idstr */
1577                 buf[13 + tmp32] = '\0';
1578                 expected_len += tmp32;
1579             }
1580             if (header_len != expected_len) {
1581                 error_report("RP: Req_Page_id with length %d expecting %zd",
1582                         header_len, expected_len);
1583                 mark_source_rp_bad(ms);
1584                 goto out;
1585             }
1586             migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
1587             break;
1588
1589         default:
1590             break;
1591         }
1592     }
1593     if (qemu_file_get_error(rp)) {
1594         trace_source_return_path_thread_bad_end();
1595         mark_source_rp_bad(ms);
1596     }
1597
1598     trace_source_return_path_thread_end();
1599 out:
1600     ms->rp_state.from_dst_file = NULL;
1601     qemu_fclose(rp);
1602     return NULL;
1603 }
1604
1605 static int open_return_path_on_source(MigrationState *ms)
1606 {
1607
1608     ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
1609     if (!ms->rp_state.from_dst_file) {
1610         return -1;
1611     }
1612
1613     trace_open_return_path_on_source();
1614     qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1615                        source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1616
1617     trace_open_return_path_on_source_continue();
1618
1619     return 0;
1620 }
1621
1622 /* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1623 static int await_return_path_close_on_source(MigrationState *ms)
1624 {
1625     /*
1626      * If this is a normal exit then the destination will send a SHUT and the
1627      * rp_thread will exit, however if there's an error we need to cause
1628      * it to exit.
1629      */
1630     if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
1631         /*
1632          * shutdown(2), if we have it, will cause it to unblock if it's stuck
1633          * waiting for the destination.
1634          */
1635         qemu_file_shutdown(ms->rp_state.from_dst_file);
1636         mark_source_rp_bad(ms);
1637     }
1638     trace_await_return_path_close_on_source_joining();
1639     qemu_thread_join(&ms->rp_state.rp_thread);
1640     trace_await_return_path_close_on_source_close();
1641     return ms->rp_state.error;
1642 }
1643
1644 /*
1645  * Switch from normal iteration to postcopy
1646  * Returns non-0 on error
1647  */
1648 static int postcopy_start(MigrationState *ms, bool *old_vm_running)
1649 {
1650     int ret;
1651     QIOChannelBuffer *bioc;
1652     QEMUFile *fb;
1653     int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1654     bool restart_block = false;
1655     migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
1656                       MIGRATION_STATUS_POSTCOPY_ACTIVE);
1657
1658     trace_postcopy_start();
1659     qemu_mutex_lock_iothread();
1660     trace_postcopy_start_set_run();
1661
1662     qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1663     *old_vm_running = runstate_is_running();
1664     global_state_store();
1665     ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
1666     if (ret < 0) {
1667         goto fail;
1668     }
1669
1670     ret = bdrv_inactivate_all();
1671     if (ret < 0) {
1672         goto fail;
1673     }
1674     restart_block = true;
1675
1676     /*
1677      * Cause any non-postcopiable, but iterative devices to
1678      * send out their final data.
1679      */
1680     qemu_savevm_state_complete_precopy(ms->to_dst_file, true);
1681
1682     /*
1683      * in Finish migrate and with the io-lock held everything should
1684      * be quiet, but we've potentially still got dirty pages and we
1685      * need to tell the destination to throw any pages it's already received
1686      * that are dirty
1687      */
1688     if (ram_postcopy_send_discard_bitmap(ms)) {
1689         error_report("postcopy send discard bitmap failed");
1690         goto fail;
1691     }
1692
1693     /*
1694      * send rest of state - note things that are doing postcopy
1695      * will notice we're in POSTCOPY_ACTIVE and not actually
1696      * wrap their state up here
1697      */
1698     qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
1699     /* Ping just for debugging, helps line traces up */
1700     qemu_savevm_send_ping(ms->to_dst_file, 2);
1701
1702     /*
1703      * While loading the device state we may trigger page transfer
1704      * requests and the fd must be free to process those, and thus
1705      * the destination must read the whole device state off the fd before
1706      * it starts processing it.  Unfortunately the ad-hoc migration format
1707      * doesn't allow the destination to know the size to read without fully
1708      * parsing it through each devices load-state code (especially the open
1709      * coded devices that use get/put).
1710      * So we wrap the device state up in a package with a length at the start;
1711      * to do this we use a qemu_buf to hold the whole of the device state.
1712      */
1713     bioc = qio_channel_buffer_new(4096);
1714     qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
1715     fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
1716     object_unref(OBJECT(bioc));
1717
1718     /*
1719      * Make sure the receiver can get incoming pages before we send the rest
1720      * of the state
1721      */
1722     qemu_savevm_send_postcopy_listen(fb);
1723
1724     qemu_savevm_state_complete_precopy(fb, false);
1725     qemu_savevm_send_ping(fb, 3);
1726
1727     qemu_savevm_send_postcopy_run(fb);
1728
1729     /* <><> end of stuff going into the package */
1730
1731     /* Last point of recovery; as soon as we send the package the destination
1732      * can open devices and potentially start running.
1733      * Lets just check again we've not got any errors.
1734      */
1735     ret = qemu_file_get_error(ms->to_dst_file);
1736     if (ret) {
1737         error_report("postcopy_start: Migration stream errored (pre package)");
1738         goto fail_closefb;
1739     }
1740
1741     restart_block = false;
1742
1743     /* Now send that blob */
1744     if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
1745         goto fail_closefb;
1746     }
1747     qemu_fclose(fb);
1748
1749     /* Send a notify to give a chance for anything that needs to happen
1750      * at the transition to postcopy and after the device state; in particular
1751      * spice needs to trigger a transition now
1752      */
1753     ms->postcopy_after_devices = true;
1754     notifier_list_notify(&migration_state_notifiers, ms);
1755
1756     ms->downtime =  qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
1757
1758     qemu_mutex_unlock_iothread();
1759
1760     /*
1761      * Although this ping is just for debug, it could potentially be
1762      * used for getting a better measurement of downtime at the source.
1763      */
1764     qemu_savevm_send_ping(ms->to_dst_file, 4);
1765
1766     if (migrate_release_ram()) {
1767         ram_postcopy_migrated_memory_release(ms);
1768     }
1769
1770     ret = qemu_file_get_error(ms->to_dst_file);
1771     if (ret) {
1772         error_report("postcopy_start: Migration stream errored");
1773         migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1774                               MIGRATION_STATUS_FAILED);
1775     }
1776
1777     return ret;
1778
1779 fail_closefb:
1780     qemu_fclose(fb);
1781 fail:
1782     migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1783                           MIGRATION_STATUS_FAILED);
1784     if (restart_block) {
1785         /* A failure happened early enough that we know the destination hasn't
1786          * accessed block devices, so we're safe to recover.
1787          */
1788         Error *local_err = NULL;
1789
1790         bdrv_invalidate_cache_all(&local_err);
1791         if (local_err) {
1792             error_report_err(local_err);
1793         }
1794     }
1795     qemu_mutex_unlock_iothread();
1796     return -1;
1797 }
1798
1799 /**
1800  * migration_completion: Used by migration_thread when there's not much left.
1801  *   The caller 'breaks' the loop when this returns.
1802  *
1803  * @s: Current migration state
1804  * @current_active_state: The migration state we expect to be in
1805  * @*old_vm_running: Pointer to old_vm_running flag
1806  * @*start_time: Pointer to time to update
1807  */
1808 static void migration_completion(MigrationState *s, int current_active_state,
1809                                  bool *old_vm_running,
1810                                  int64_t *start_time)
1811 {
1812     int ret;
1813
1814     if (s->state == MIGRATION_STATUS_ACTIVE) {
1815         qemu_mutex_lock_iothread();
1816         *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1817         qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1818         *old_vm_running = runstate_is_running();
1819         ret = global_state_store();
1820
1821         if (!ret) {
1822             ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
1823             /*
1824              * Don't mark the image with BDRV_O_INACTIVE flag if
1825              * we will go into COLO stage later.
1826              */
1827             if (ret >= 0 && !migrate_colo_enabled()) {
1828                 ret = bdrv_inactivate_all();
1829             }
1830             if (ret >= 0) {
1831                 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
1832                 qemu_savevm_state_complete_precopy(s->to_dst_file, false);
1833                 s->block_inactive = true;
1834             }
1835         }
1836         qemu_mutex_unlock_iothread();
1837
1838         if (ret < 0) {
1839             goto fail;
1840         }
1841     } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1842         trace_migration_completion_postcopy_end();
1843
1844         qemu_savevm_state_complete_postcopy(s->to_dst_file);
1845         trace_migration_completion_postcopy_end_after_complete();
1846     }
1847
1848     /*
1849      * If rp was opened we must clean up the thread before
1850      * cleaning everything else up (since if there are no failures
1851      * it will wait for the destination to send it's status in
1852      * a SHUT command).
1853      * Postcopy opens rp if enabled (even if it's not avtivated)
1854      */
1855     if (migrate_postcopy_ram()) {
1856         int rp_error;
1857         trace_migration_completion_postcopy_end_before_rp();
1858         rp_error = await_return_path_close_on_source(s);
1859         trace_migration_completion_postcopy_end_after_rp(rp_error);
1860         if (rp_error) {
1861             goto fail_invalidate;
1862         }
1863     }
1864
1865     if (qemu_file_get_error(s->to_dst_file)) {
1866         trace_migration_completion_file_err();
1867         goto fail_invalidate;
1868     }
1869
1870     if (!migrate_colo_enabled()) {
1871         migrate_set_state(&s->state, current_active_state,
1872                           MIGRATION_STATUS_COMPLETED);
1873     }
1874
1875     return;
1876
1877 fail_invalidate:
1878     /* If not doing postcopy, vm_start() will be called: let's regain
1879      * control on images.
1880      */
1881     if (s->state == MIGRATION_STATUS_ACTIVE) {
1882         Error *local_err = NULL;
1883
1884         qemu_mutex_lock_iothread();
1885         bdrv_invalidate_cache_all(&local_err);
1886         if (local_err) {
1887             error_report_err(local_err);
1888         } else {
1889             s->block_inactive = false;
1890         }
1891         qemu_mutex_unlock_iothread();
1892     }
1893
1894 fail:
1895     migrate_set_state(&s->state, current_active_state,
1896                       MIGRATION_STATUS_FAILED);
1897 }
1898
1899 bool migrate_colo_enabled(void)
1900 {
1901     MigrationState *s = migrate_get_current();
1902     return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
1903 }
1904
1905 /*
1906  * Master migration thread on the source VM.
1907  * It drives the migration and pumps the data down the outgoing channel.
1908  */
1909 static void *migration_thread(void *opaque)
1910 {
1911     MigrationState *s = opaque;
1912     /* Used by the bandwidth calcs, updated later */
1913     int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1914     int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
1915     int64_t initial_bytes = 0;
1916     /*
1917      * The final stage happens when the remaining data is smaller than
1918      * this threshold; it's calculated from the requested downtime and
1919      * measured bandwidth
1920      */
1921     int64_t threshold_size = 0;
1922     int64_t start_time = initial_time;
1923     int64_t end_time;
1924     bool old_vm_running = false;
1925     bool entered_postcopy = false;
1926     /* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */
1927     enum MigrationStatus current_active_state = MIGRATION_STATUS_ACTIVE;
1928     bool enable_colo = migrate_colo_enabled();
1929
1930     rcu_register_thread();
1931
1932     qemu_savevm_state_header(s->to_dst_file);
1933
1934     if (migrate_postcopy_ram()) {
1935         /* Now tell the dest that it should open its end so it can reply */
1936         qemu_savevm_send_open_return_path(s->to_dst_file);
1937
1938         /* And do a ping that will make stuff easier to debug */
1939         qemu_savevm_send_ping(s->to_dst_file, 1);
1940
1941         /*
1942          * Tell the destination that we *might* want to do postcopy later;
1943          * if the other end can't do postcopy it should fail now, nice and
1944          * early.
1945          */
1946         qemu_savevm_send_postcopy_advise(s->to_dst_file);
1947     }
1948
1949     qemu_savevm_state_begin(s->to_dst_file);
1950
1951     s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
1952     migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1953                       MIGRATION_STATUS_ACTIVE);
1954
1955     trace_migration_thread_setup_complete();
1956
1957     while (s->state == MIGRATION_STATUS_ACTIVE ||
1958            s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1959         int64_t current_time;
1960         uint64_t pending_size;
1961
1962         if (!qemu_file_rate_limit(s->to_dst_file)) {
1963             uint64_t pend_post, pend_nonpost;
1964
1965             qemu_savevm_state_pending(s->to_dst_file, threshold_size,
1966                                       &pend_nonpost, &pend_post);
1967             pending_size = pend_nonpost + pend_post;
1968             trace_migrate_pending(pending_size, threshold_size,
1969                                   pend_post, pend_nonpost);
1970             if (pending_size && pending_size >= threshold_size) {
1971                 /* Still a significant amount to transfer */
1972
1973                 if (migrate_postcopy_ram() &&
1974                     s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE &&
1975                     pend_nonpost <= threshold_size &&
1976                     atomic_read(&s->start_postcopy)) {
1977
1978                     if (!postcopy_start(s, &old_vm_running)) {
1979                         current_active_state = MIGRATION_STATUS_POSTCOPY_ACTIVE;
1980                         entered_postcopy = true;
1981                     }
1982
1983                     continue;
1984                 }
1985                 /* Just another iteration step */
1986                 qemu_savevm_state_iterate(s->to_dst_file, entered_postcopy);
1987             } else {
1988                 trace_migration_thread_low_pending(pending_size);
1989                 migration_completion(s, current_active_state,
1990                                      &old_vm_running, &start_time);
1991                 break;
1992             }
1993         }
1994
1995         if (qemu_file_get_error(s->to_dst_file)) {
1996             migrate_set_state(&s->state, current_active_state,
1997                               MIGRATION_STATUS_FAILED);
1998             trace_migration_thread_file_err();
1999             break;
2000         }
2001         current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2002         if (current_time >= initial_time + BUFFER_DELAY) {
2003             uint64_t transferred_bytes = qemu_ftell(s->to_dst_file) -
2004                                          initial_bytes;
2005             uint64_t time_spent = current_time - initial_time;
2006             double bandwidth = (double)transferred_bytes / time_spent;
2007             threshold_size = bandwidth * s->parameters.downtime_limit;
2008
2009             s->mbps = (((double) transferred_bytes * 8.0) /
2010                     ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
2011
2012             trace_migrate_transferred(transferred_bytes, time_spent,
2013                                       bandwidth, threshold_size);
2014             /* if we haven't sent anything, we don't want to recalculate
2015                10000 is a small enough number for our purposes */
2016             if (ram_dirty_pages_rate() && transferred_bytes > 10000) {
2017                 s->expected_downtime = ram_dirty_pages_rate() *
2018                     qemu_target_page_size() / bandwidth;
2019             }
2020
2021             qemu_file_reset_rate_limit(s->to_dst_file);
2022             initial_time = current_time;
2023             initial_bytes = qemu_ftell(s->to_dst_file);
2024         }
2025         if (qemu_file_rate_limit(s->to_dst_file)) {
2026             /* usleep expects microseconds */
2027             g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
2028         }
2029     }
2030
2031     trace_migration_thread_after_loop();
2032     /* If we enabled cpu throttling for auto-converge, turn it off. */
2033     cpu_throttle_stop();
2034     end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2035
2036     qemu_mutex_lock_iothread();
2037     /*
2038      * The resource has been allocated by migration will be reused in COLO
2039      * process, so don't release them.
2040      */
2041     if (!enable_colo) {
2042         qemu_savevm_state_cleanup();
2043     }
2044     if (s->state == MIGRATION_STATUS_COMPLETED) {
2045         uint64_t transferred_bytes = qemu_ftell(s->to_dst_file);
2046         s->total_time = end_time - s->total_time;
2047         if (!entered_postcopy) {
2048             s->downtime = end_time - start_time;
2049         }
2050         if (s->total_time) {
2051             s->mbps = (((double) transferred_bytes * 8.0) /
2052                        ((double) s->total_time)) / 1000;
2053         }
2054         runstate_set(RUN_STATE_POSTMIGRATE);
2055     } else {
2056         if (s->state == MIGRATION_STATUS_ACTIVE && enable_colo) {
2057             migrate_start_colo_process(s);
2058             qemu_savevm_state_cleanup();
2059             /*
2060             * Fixme: we will run VM in COLO no matter its old running state.
2061             * After exited COLO, we will keep running.
2062             */
2063             old_vm_running = true;
2064         }
2065         if (old_vm_running && !entered_postcopy) {
2066             vm_start();
2067         } else {
2068             if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
2069                 runstate_set(RUN_STATE_POSTMIGRATE);
2070             }
2071         }
2072     }
2073     qemu_bh_schedule(s->cleanup_bh);
2074     qemu_mutex_unlock_iothread();
2075
2076     rcu_unregister_thread();
2077     return NULL;
2078 }
2079
2080 void migrate_fd_connect(MigrationState *s)
2081 {
2082     s->expected_downtime = s->parameters.downtime_limit;
2083     s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
2084
2085     qemu_file_set_blocking(s->to_dst_file, true);
2086     qemu_file_set_rate_limit(s->to_dst_file,
2087                              s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
2088
2089     /* Notify before starting migration thread */
2090     notifier_list_notify(&migration_state_notifiers, s);
2091
2092     /*
2093      * Open the return path; currently for postcopy but other things might
2094      * also want it.
2095      */
2096     if (migrate_postcopy_ram()) {
2097         if (open_return_path_on_source(s)) {
2098             error_report("Unable to open return-path for postcopy");
2099             migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2100                               MIGRATION_STATUS_FAILED);
2101             migrate_fd_cleanup(s);
2102             return;
2103         }
2104     }
2105
2106     migrate_compress_threads_create();
2107     qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
2108                        QEMU_THREAD_JOINABLE);
2109     s->migration_thread_running = true;
2110 }
2111
This page took 0.140532 seconds and 4 git commands to generate.