xref: /openbmc/qemu/migration/migration.c (revision 0b2ff2ce)
1 /*
2  * QEMU live migration
3  *
4  * Copyright IBM, Corp. 2008
5  *
6  * Authors:
7  *  Anthony Liguori   <aliguori@us.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  *
12  * Contributions after 2012-01-13 are licensed under the terms of the
13  * GNU GPL, version 2 or (at your option) any later version.
14  */
15 
16 #include "qemu-common.h"
17 #include "qemu/main-loop.h"
18 #include "migration/migration.h"
19 #include "monitor/monitor.h"
20 #include "migration/qemu-file.h"
21 #include "sysemu/sysemu.h"
22 #include "block/block.h"
23 #include "qemu/sockets.h"
24 #include "migration/block.h"
25 #include "qemu/thread.h"
26 #include "qmp-commands.h"
27 #include "trace.h"
28 
29 #define MAX_THROTTLE  (32 << 20)      /* Migration speed throttling */
30 
31 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
32  * data. */
33 #define BUFFER_DELAY     100
34 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
35 
36 /* Default compression thread count */
37 #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
38 /* Default decompression thread count, usually decompression is at
39  * least 4 times as fast as compression.*/
40 #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
41 /*0: means nocompress, 1: best speed, ... 9: best compress ratio */
42 #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
43 
44 /* Migration XBZRLE default cache size */
45 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
46 
47 static NotifierList migration_state_notifiers =
48     NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
49 
50 static bool deferred_incoming;
51 
52 /* When we add fault tolerance, we could have several
53    migrations at once.  For now we don't need to add
54    dynamic creation of migration */
55 
56 MigrationState *migrate_get_current(void)
57 {
58     static MigrationState current_migration = {
59         .state = MIGRATION_STATUS_NONE,
60         .bandwidth_limit = MAX_THROTTLE,
61         .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
62         .mbps = -1,
63         .parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] =
64                 DEFAULT_MIGRATE_COMPRESS_LEVEL,
65         .parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
66                 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
67         .parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
68                 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
69     };
70 
71     return &current_migration;
72 }
73 
74 /*
75  * Called on -incoming with a defer: uri.
76  * The migration can be started later after any parameters have been
77  * changed.
78  */
79 static void deferred_incoming_migration(Error **errp)
80 {
81     if (deferred_incoming) {
82         error_setg(errp, "Incoming migration already deferred");
83     }
84     deferred_incoming = true;
85 }
86 
87 void qemu_start_incoming_migration(const char *uri, Error **errp)
88 {
89     const char *p;
90 
91     if (!strcmp(uri, "defer")) {
92         deferred_incoming_migration(errp);
93     } else if (strstart(uri, "tcp:", &p)) {
94         tcp_start_incoming_migration(p, errp);
95 #ifdef CONFIG_RDMA
96     } else if (strstart(uri, "rdma:", &p)) {
97         rdma_start_incoming_migration(p, errp);
98 #endif
99 #if !defined(WIN32)
100     } else if (strstart(uri, "exec:", &p)) {
101         exec_start_incoming_migration(p, errp);
102     } else if (strstart(uri, "unix:", &p)) {
103         unix_start_incoming_migration(p, errp);
104     } else if (strstart(uri, "fd:", &p)) {
105         fd_start_incoming_migration(p, errp);
106 #endif
107     } else {
108         error_setg(errp, "unknown migration protocol: %s", uri);
109     }
110 }
111 
112 static void process_incoming_migration_co(void *opaque)
113 {
114     QEMUFile *f = opaque;
115     Error *local_err = NULL;
116     int ret;
117 
118     ret = qemu_loadvm_state(f);
119     qemu_fclose(f);
120     free_xbzrle_decoded_buf();
121     if (ret < 0) {
122         error_report("load of migration failed: %s", strerror(-ret));
123         migrate_decompress_threads_join();
124         exit(EXIT_FAILURE);
125     }
126     qemu_announce_self();
127 
128     /* Make sure all file formats flush their mutable metadata */
129     bdrv_invalidate_cache_all(&local_err);
130     if (local_err) {
131         error_report_err(local_err);
132         migrate_decompress_threads_join();
133         exit(EXIT_FAILURE);
134     }
135 
136     if (autostart) {
137         vm_start();
138     } else {
139         runstate_set(RUN_STATE_PAUSED);
140     }
141     migrate_decompress_threads_join();
142 }
143 
144 void process_incoming_migration(QEMUFile *f)
145 {
146     Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
147     int fd = qemu_get_fd(f);
148 
149     assert(fd != -1);
150     migrate_decompress_threads_create();
151     qemu_set_nonblock(fd);
152     qemu_coroutine_enter(co, f);
153 }
154 
155 /* amount of nanoseconds we are willing to wait for migration to be down.
156  * the choice of nanoseconds is because it is the maximum resolution that
157  * get_clock() can achieve. It is an internal measure. All user-visible
158  * units must be in seconds */
159 static uint64_t max_downtime = 300000000;
160 
161 uint64_t migrate_max_downtime(void)
162 {
163     return max_downtime;
164 }
165 
166 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
167 {
168     MigrationCapabilityStatusList *head = NULL;
169     MigrationCapabilityStatusList *caps;
170     MigrationState *s = migrate_get_current();
171     int i;
172 
173     caps = NULL; /* silence compiler warning */
174     for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
175         if (head == NULL) {
176             head = g_malloc0(sizeof(*caps));
177             caps = head;
178         } else {
179             caps->next = g_malloc0(sizeof(*caps));
180             caps = caps->next;
181         }
182         caps->value =
183             g_malloc(sizeof(*caps->value));
184         caps->value->capability = i;
185         caps->value->state = s->enabled_capabilities[i];
186     }
187 
188     return head;
189 }
190 
191 MigrationParameters *qmp_query_migrate_parameters(Error **errp)
192 {
193     MigrationParameters *params;
194     MigrationState *s = migrate_get_current();
195 
196     params = g_malloc0(sizeof(*params));
197     params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
198     params->compress_threads =
199             s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
200     params->decompress_threads =
201             s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
202 
203     return params;
204 }
205 
206 static void get_xbzrle_cache_stats(MigrationInfo *info)
207 {
208     if (migrate_use_xbzrle()) {
209         info->has_xbzrle_cache = true;
210         info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
211         info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
212         info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
213         info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
214         info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
215         info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
216         info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
217     }
218 }
219 
220 MigrationInfo *qmp_query_migrate(Error **errp)
221 {
222     MigrationInfo *info = g_malloc0(sizeof(*info));
223     MigrationState *s = migrate_get_current();
224 
225     switch (s->state) {
226     case MIGRATION_STATUS_NONE:
227         /* no migration has happened ever */
228         break;
229     case MIGRATION_STATUS_SETUP:
230         info->has_status = true;
231         info->has_total_time = false;
232         break;
233     case MIGRATION_STATUS_ACTIVE:
234     case MIGRATION_STATUS_CANCELLING:
235         info->has_status = true;
236         info->has_total_time = true;
237         info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
238             - s->total_time;
239         info->has_expected_downtime = true;
240         info->expected_downtime = s->expected_downtime;
241         info->has_setup_time = true;
242         info->setup_time = s->setup_time;
243 
244         info->has_ram = true;
245         info->ram = g_malloc0(sizeof(*info->ram));
246         info->ram->transferred = ram_bytes_transferred();
247         info->ram->remaining = ram_bytes_remaining();
248         info->ram->total = ram_bytes_total();
249         info->ram->duplicate = dup_mig_pages_transferred();
250         info->ram->skipped = skipped_mig_pages_transferred();
251         info->ram->normal = norm_mig_pages_transferred();
252         info->ram->normal_bytes = norm_mig_bytes_transferred();
253         info->ram->dirty_pages_rate = s->dirty_pages_rate;
254         info->ram->mbps = s->mbps;
255         info->ram->dirty_sync_count = s->dirty_sync_count;
256 
257         if (blk_mig_active()) {
258             info->has_disk = true;
259             info->disk = g_malloc0(sizeof(*info->disk));
260             info->disk->transferred = blk_mig_bytes_transferred();
261             info->disk->remaining = blk_mig_bytes_remaining();
262             info->disk->total = blk_mig_bytes_total();
263         }
264 
265         get_xbzrle_cache_stats(info);
266         break;
267     case MIGRATION_STATUS_COMPLETED:
268         get_xbzrle_cache_stats(info);
269 
270         info->has_status = true;
271         info->has_total_time = true;
272         info->total_time = s->total_time;
273         info->has_downtime = true;
274         info->downtime = s->downtime;
275         info->has_setup_time = true;
276         info->setup_time = s->setup_time;
277 
278         info->has_ram = true;
279         info->ram = g_malloc0(sizeof(*info->ram));
280         info->ram->transferred = ram_bytes_transferred();
281         info->ram->remaining = 0;
282         info->ram->total = ram_bytes_total();
283         info->ram->duplicate = dup_mig_pages_transferred();
284         info->ram->skipped = skipped_mig_pages_transferred();
285         info->ram->normal = norm_mig_pages_transferred();
286         info->ram->normal_bytes = norm_mig_bytes_transferred();
287         info->ram->mbps = s->mbps;
288         info->ram->dirty_sync_count = s->dirty_sync_count;
289         break;
290     case MIGRATION_STATUS_FAILED:
291         info->has_status = true;
292         break;
293     case MIGRATION_STATUS_CANCELLED:
294         info->has_status = true;
295         break;
296     }
297     info->status = s->state;
298 
299     return info;
300 }
301 
302 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
303                                   Error **errp)
304 {
305     MigrationState *s = migrate_get_current();
306     MigrationCapabilityStatusList *cap;
307 
308     if (s->state == MIGRATION_STATUS_ACTIVE ||
309         s->state == MIGRATION_STATUS_SETUP) {
310         error_set(errp, QERR_MIGRATION_ACTIVE);
311         return;
312     }
313 
314     for (cap = params; cap; cap = cap->next) {
315         s->enabled_capabilities[cap->value->capability] = cap->value->state;
316     }
317 }
318 
319 void qmp_migrate_set_parameters(bool has_compress_level,
320                                 int64_t compress_level,
321                                 bool has_compress_threads,
322                                 int64_t compress_threads,
323                                 bool has_decompress_threads,
324                                 int64_t decompress_threads, Error **errp)
325 {
326     MigrationState *s = migrate_get_current();
327 
328     if (has_compress_level && (compress_level < 0 || compress_level > 9)) {
329         error_set(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
330                   "is invalid, it should be in the range of 0 to 9");
331         return;
332     }
333     if (has_compress_threads &&
334             (compress_threads < 1 || compress_threads > 255)) {
335         error_set(errp, QERR_INVALID_PARAMETER_VALUE,
336                   "compress_threads",
337                   "is invalid, it should be in the range of 1 to 255");
338         return;
339     }
340     if (has_decompress_threads &&
341             (decompress_threads < 1 || decompress_threads > 255)) {
342         error_set(errp, QERR_INVALID_PARAMETER_VALUE,
343                   "decompress_threads",
344                   "is invalid, it should be in the range of 1 to 255");
345         return;
346     }
347 
348     if (has_compress_level) {
349         s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
350     }
351     if (has_compress_threads) {
352         s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads;
353     }
354     if (has_decompress_threads) {
355         s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
356                                                     decompress_threads;
357     }
358 }
359 
360 /* shared migration helpers */
361 
362 static void migrate_set_state(MigrationState *s, int old_state, int new_state)
363 {
364     if (atomic_cmpxchg(&s->state, old_state, new_state) == new_state) {
365         trace_migrate_set_state(new_state);
366     }
367 }
368 
369 static void migrate_fd_cleanup(void *opaque)
370 {
371     MigrationState *s = opaque;
372 
373     qemu_bh_delete(s->cleanup_bh);
374     s->cleanup_bh = NULL;
375 
376     if (s->file) {
377         trace_migrate_fd_cleanup();
378         qemu_mutex_unlock_iothread();
379         qemu_thread_join(&s->thread);
380         qemu_mutex_lock_iothread();
381 
382         migrate_compress_threads_join();
383         qemu_fclose(s->file);
384         s->file = NULL;
385     }
386 
387     assert(s->state != MIGRATION_STATUS_ACTIVE);
388 
389     if (s->state != MIGRATION_STATUS_COMPLETED) {
390         qemu_savevm_state_cancel();
391         if (s->state == MIGRATION_STATUS_CANCELLING) {
392             migrate_set_state(s, MIGRATION_STATUS_CANCELLING,
393                               MIGRATION_STATUS_CANCELLED);
394         }
395     }
396 
397     notifier_list_notify(&migration_state_notifiers, s);
398 }
399 
400 void migrate_fd_error(MigrationState *s)
401 {
402     trace_migrate_fd_error();
403     assert(s->file == NULL);
404     s->state = MIGRATION_STATUS_FAILED;
405     trace_migrate_set_state(MIGRATION_STATUS_FAILED);
406     notifier_list_notify(&migration_state_notifiers, s);
407 }
408 
409 static void migrate_fd_cancel(MigrationState *s)
410 {
411     int old_state ;
412     QEMUFile *f = migrate_get_current()->file;
413     trace_migrate_fd_cancel();
414 
415     do {
416         old_state = s->state;
417         if (old_state != MIGRATION_STATUS_SETUP &&
418             old_state != MIGRATION_STATUS_ACTIVE) {
419             break;
420         }
421         migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
422     } while (s->state != MIGRATION_STATUS_CANCELLING);
423 
424     /*
425      * If we're unlucky the migration code might be stuck somewhere in a
426      * send/write while the network has failed and is waiting to timeout;
427      * if we've got shutdown(2) available then we can force it to quit.
428      * The outgoing qemu file gets closed in migrate_fd_cleanup that is
429      * called in a bh, so there is no race against this cancel.
430      */
431     if (s->state == MIGRATION_STATUS_CANCELLING && f) {
432         qemu_file_shutdown(f);
433     }
434 }
435 
436 void add_migration_state_change_notifier(Notifier *notify)
437 {
438     notifier_list_add(&migration_state_notifiers, notify);
439 }
440 
441 void remove_migration_state_change_notifier(Notifier *notify)
442 {
443     notifier_remove(notify);
444 }
445 
446 bool migration_in_setup(MigrationState *s)
447 {
448     return s->state == MIGRATION_STATUS_SETUP;
449 }
450 
451 bool migration_has_finished(MigrationState *s)
452 {
453     return s->state == MIGRATION_STATUS_COMPLETED;
454 }
455 
456 bool migration_has_failed(MigrationState *s)
457 {
458     return (s->state == MIGRATION_STATUS_CANCELLED ||
459             s->state == MIGRATION_STATUS_FAILED);
460 }
461 
462 static MigrationState *migrate_init(const MigrationParams *params)
463 {
464     MigrationState *s = migrate_get_current();
465     int64_t bandwidth_limit = s->bandwidth_limit;
466     bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
467     int64_t xbzrle_cache_size = s->xbzrle_cache_size;
468     int compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
469     int compress_thread_count =
470             s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
471     int decompress_thread_count =
472             s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
473 
474     memcpy(enabled_capabilities, s->enabled_capabilities,
475            sizeof(enabled_capabilities));
476 
477     memset(s, 0, sizeof(*s));
478     s->params = *params;
479     memcpy(s->enabled_capabilities, enabled_capabilities,
480            sizeof(enabled_capabilities));
481     s->xbzrle_cache_size = xbzrle_cache_size;
482 
483     s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
484     s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
485                compress_thread_count;
486     s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
487                decompress_thread_count;
488     s->bandwidth_limit = bandwidth_limit;
489     s->state = MIGRATION_STATUS_SETUP;
490     trace_migrate_set_state(MIGRATION_STATUS_SETUP);
491 
492     s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
493     return s;
494 }
495 
496 static GSList *migration_blockers;
497 
498 void migrate_add_blocker(Error *reason)
499 {
500     migration_blockers = g_slist_prepend(migration_blockers, reason);
501 }
502 
503 void migrate_del_blocker(Error *reason)
504 {
505     migration_blockers = g_slist_remove(migration_blockers, reason);
506 }
507 
508 void qmp_migrate_incoming(const char *uri, Error **errp)
509 {
510     Error *local_err = NULL;
511     static bool once = true;
512 
513     if (!deferred_incoming) {
514         error_setg(errp, "For use with '-incoming defer'");
515         return;
516     }
517     if (!once) {
518         error_setg(errp, "The incoming migration has already been started");
519     }
520 
521     qemu_start_incoming_migration(uri, &local_err);
522 
523     if (local_err) {
524         error_propagate(errp, local_err);
525         return;
526     }
527 
528     once = false;
529 }
530 
531 void qmp_migrate(const char *uri, bool has_blk, bool blk,
532                  bool has_inc, bool inc, bool has_detach, bool detach,
533                  Error **errp)
534 {
535     Error *local_err = NULL;
536     MigrationState *s = migrate_get_current();
537     MigrationParams params;
538     const char *p;
539 
540     params.blk = has_blk && blk;
541     params.shared = has_inc && inc;
542 
543     if (s->state == MIGRATION_STATUS_ACTIVE ||
544         s->state == MIGRATION_STATUS_SETUP ||
545         s->state == MIGRATION_STATUS_CANCELLING) {
546         error_set(errp, QERR_MIGRATION_ACTIVE);
547         return;
548     }
549 
550     if (runstate_check(RUN_STATE_INMIGRATE)) {
551         error_setg(errp, "Guest is waiting for an incoming migration");
552         return;
553     }
554 
555     if (qemu_savevm_state_blocked(errp)) {
556         return;
557     }
558 
559     if (migration_blockers) {
560         *errp = error_copy(migration_blockers->data);
561         return;
562     }
563 
564     s = migrate_init(&params);
565 
566     if (strstart(uri, "tcp:", &p)) {
567         tcp_start_outgoing_migration(s, p, &local_err);
568 #ifdef CONFIG_RDMA
569     } else if (strstart(uri, "rdma:", &p)) {
570         rdma_start_outgoing_migration(s, p, &local_err);
571 #endif
572 #if !defined(WIN32)
573     } else if (strstart(uri, "exec:", &p)) {
574         exec_start_outgoing_migration(s, p, &local_err);
575     } else if (strstart(uri, "unix:", &p)) {
576         unix_start_outgoing_migration(s, p, &local_err);
577     } else if (strstart(uri, "fd:", &p)) {
578         fd_start_outgoing_migration(s, p, &local_err);
579 #endif
580     } else {
581         error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
582         s->state = MIGRATION_STATUS_FAILED;
583         return;
584     }
585 
586     if (local_err) {
587         migrate_fd_error(s);
588         error_propagate(errp, local_err);
589         return;
590     }
591 }
592 
593 void qmp_migrate_cancel(Error **errp)
594 {
595     migrate_fd_cancel(migrate_get_current());
596 }
597 
598 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
599 {
600     MigrationState *s = migrate_get_current();
601     int64_t new_size;
602 
603     /* Check for truncation */
604     if (value != (size_t)value) {
605         error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
606                   "exceeding address space");
607         return;
608     }
609 
610     /* Cache should not be larger than guest ram size */
611     if (value > ram_bytes_total()) {
612         error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
613                   "exceeds guest ram size ");
614         return;
615     }
616 
617     new_size = xbzrle_cache_resize(value);
618     if (new_size < 0) {
619         error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
620                   "is smaller than page size");
621         return;
622     }
623 
624     s->xbzrle_cache_size = new_size;
625 }
626 
627 int64_t qmp_query_migrate_cache_size(Error **errp)
628 {
629     return migrate_xbzrle_cache_size();
630 }
631 
632 void qmp_migrate_set_speed(int64_t value, Error **errp)
633 {
634     MigrationState *s;
635 
636     if (value < 0) {
637         value = 0;
638     }
639     if (value > SIZE_MAX) {
640         value = SIZE_MAX;
641     }
642 
643     s = migrate_get_current();
644     s->bandwidth_limit = value;
645     if (s->file) {
646         qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
647     }
648 }
649 
650 void qmp_migrate_set_downtime(double value, Error **errp)
651 {
652     value *= 1e9;
653     value = MAX(0, MIN(UINT64_MAX, value));
654     max_downtime = (uint64_t)value;
655 }
656 
657 bool migrate_auto_converge(void)
658 {
659     MigrationState *s;
660 
661     s = migrate_get_current();
662 
663     return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
664 }
665 
666 bool migrate_zero_blocks(void)
667 {
668     MigrationState *s;
669 
670     s = migrate_get_current();
671 
672     return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
673 }
674 
675 bool migrate_use_compression(void)
676 {
677     MigrationState *s;
678 
679     s = migrate_get_current();
680 
681     return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
682 }
683 
684 int migrate_compress_level(void)
685 {
686     MigrationState *s;
687 
688     s = migrate_get_current();
689 
690     return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
691 }
692 
693 int migrate_compress_threads(void)
694 {
695     MigrationState *s;
696 
697     s = migrate_get_current();
698 
699     return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
700 }
701 
702 int migrate_decompress_threads(void)
703 {
704     MigrationState *s;
705 
706     s = migrate_get_current();
707 
708     return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
709 }
710 
711 int migrate_use_xbzrle(void)
712 {
713     MigrationState *s;
714 
715     s = migrate_get_current();
716 
717     return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
718 }
719 
720 int64_t migrate_xbzrle_cache_size(void)
721 {
722     MigrationState *s;
723 
724     s = migrate_get_current();
725 
726     return s->xbzrle_cache_size;
727 }
728 
729 /* migration thread support */
730 
731 static void *migration_thread(void *opaque)
732 {
733     MigrationState *s = opaque;
734     int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
735     int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
736     int64_t initial_bytes = 0;
737     int64_t max_size = 0;
738     int64_t start_time = initial_time;
739     bool old_vm_running = false;
740 
741     qemu_savevm_state_begin(s->file, &s->params);
742 
743     s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
744     migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
745 
746     while (s->state == MIGRATION_STATUS_ACTIVE) {
747         int64_t current_time;
748         uint64_t pending_size;
749 
750         if (!qemu_file_rate_limit(s->file)) {
751             pending_size = qemu_savevm_state_pending(s->file, max_size);
752             trace_migrate_pending(pending_size, max_size);
753             if (pending_size && pending_size >= max_size) {
754                 qemu_savevm_state_iterate(s->file);
755             } else {
756                 int ret;
757 
758                 qemu_mutex_lock_iothread();
759                 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
760                 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
761                 old_vm_running = runstate_is_running();
762 
763                 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
764                 if (ret >= 0) {
765                     qemu_file_set_rate_limit(s->file, INT64_MAX);
766                     qemu_savevm_state_complete(s->file);
767                 }
768                 qemu_mutex_unlock_iothread();
769 
770                 if (ret < 0) {
771                     migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
772                                       MIGRATION_STATUS_FAILED);
773                     break;
774                 }
775 
776                 if (!qemu_file_get_error(s->file)) {
777                     migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
778                                       MIGRATION_STATUS_COMPLETED);
779                     break;
780                 }
781             }
782         }
783 
784         if (qemu_file_get_error(s->file)) {
785             migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
786                               MIGRATION_STATUS_FAILED);
787             break;
788         }
789         current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
790         if (current_time >= initial_time + BUFFER_DELAY) {
791             uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
792             uint64_t time_spent = current_time - initial_time;
793             double bandwidth = transferred_bytes / time_spent;
794             max_size = bandwidth * migrate_max_downtime() / 1000000;
795 
796             s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
797                     ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
798 
799             trace_migrate_transferred(transferred_bytes, time_spent,
800                                       bandwidth, max_size);
801             /* if we haven't sent anything, we don't want to recalculate
802                10000 is a small enough number for our purposes */
803             if (s->dirty_bytes_rate && transferred_bytes > 10000) {
804                 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
805             }
806 
807             qemu_file_reset_rate_limit(s->file);
808             initial_time = current_time;
809             initial_bytes = qemu_ftell(s->file);
810         }
811         if (qemu_file_rate_limit(s->file)) {
812             /* usleep expects microseconds */
813             g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
814         }
815     }
816 
817     qemu_mutex_lock_iothread();
818     if (s->state == MIGRATION_STATUS_COMPLETED) {
819         int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
820         uint64_t transferred_bytes = qemu_ftell(s->file);
821         s->total_time = end_time - s->total_time;
822         s->downtime = end_time - start_time;
823         if (s->total_time) {
824             s->mbps = (((double) transferred_bytes * 8.0) /
825                        ((double) s->total_time)) / 1000;
826         }
827         runstate_set(RUN_STATE_POSTMIGRATE);
828     } else {
829         if (old_vm_running) {
830             vm_start();
831         }
832     }
833     qemu_bh_schedule(s->cleanup_bh);
834     qemu_mutex_unlock_iothread();
835 
836     return NULL;
837 }
838 
839 void migrate_fd_connect(MigrationState *s)
840 {
841     s->state = MIGRATION_STATUS_SETUP;
842     trace_migrate_set_state(MIGRATION_STATUS_SETUP);
843 
844     /* This is a best 1st approximation. ns to ms */
845     s->expected_downtime = max_downtime/1000000;
846     s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
847 
848     qemu_file_set_rate_limit(s->file,
849                              s->bandwidth_limit / XFER_LIMIT_RATIO);
850 
851     /* Notify before starting migration thread */
852     notifier_list_notify(&migration_state_notifiers, s);
853 
854     migrate_compress_threads_create();
855     qemu_thread_create(&s->thread, "migration", migration_thread, s,
856                        QEMU_THREAD_JOINABLE);
857 }
858