bb4c92ef9397e9d398c04fe6343212227a78c529
[sdk/emulator/qemu.git] / migration / migration.c
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/error-report.h"
18 #include "qemu/main-loop.h"
19 #include "migration/migration.h"
20 #include "migration/qemu-file.h"
21 #include "sysemu/sysemu.h"
22 #include "block/block.h"
23 #include "qapi/qmp/qerror.h"
24 #include "qemu/sockets.h"
25 #include "qemu/rcu.h"
26 #include "migration/block.h"
27 #include "qemu/thread.h"
28 #include "qmp-commands.h"
29 #include "trace.h"
30 #include "qapi/util.h"
31 #include "qapi-event.h"
32 #include "qom/cpu.h"
33
34 #define MAX_THROTTLE  (32 << 20)      /* Migration transfer speed throttling */
35
36 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
37  * data. */
38 #define BUFFER_DELAY     100
39 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
40
41 /* Default compression thread count */
42 #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
43 /* Default decompression thread count, usually decompression is at
44  * least 4 times as fast as compression.*/
45 #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
46 /*0: means nocompress, 1: best speed, ... 9: best compress ratio */
47 #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
48 /* Define default autoconverge cpu throttle migration parameters */
49 #define DEFAULT_MIGRATE_X_CPU_THROTTLE_INITIAL 20
50 #define DEFAULT_MIGRATE_X_CPU_THROTTLE_INCREMENT 10
51
52 /* Migration XBZRLE default cache size */
53 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
54
55 static NotifierList migration_state_notifiers =
56     NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
57
58 static bool deferred_incoming;
59
60 /*
61  * Current state of incoming postcopy; note this is not part of
62  * MigrationIncomingState since it's state is used during cleanup
63  * at the end as MIS is being freed.
64  */
65 static PostcopyState incoming_postcopy_state;
66
67 /* When we add fault tolerance, we could have several
68    migrations at once.  For now we don't need to add
69    dynamic creation of migration */
70
71 /* For outgoing */
72 MigrationState *migrate_get_current(void)
73 {
74     static MigrationState current_migration = {
75         .state = MIGRATION_STATUS_NONE,
76         .bandwidth_limit = MAX_THROTTLE,
77         .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
78         .mbps = -1,
79         .parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] =
80                 DEFAULT_MIGRATE_COMPRESS_LEVEL,
81         .parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
82                 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
83         .parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
84                 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
85         .parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
86                 DEFAULT_MIGRATE_X_CPU_THROTTLE_INITIAL,
87         .parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
88                 DEFAULT_MIGRATE_X_CPU_THROTTLE_INCREMENT,
89     };
90
91     return &current_migration;
92 }
93
94 /* For incoming */
95 static MigrationIncomingState *mis_current;
96
97 MigrationIncomingState *migration_incoming_get_current(void)
98 {
99     return mis_current;
100 }
101
102 MigrationIncomingState *migration_incoming_state_new(QEMUFile* f)
103 {
104     mis_current = g_new0(MigrationIncomingState, 1);
105     mis_current->from_src_file = f;
106     QLIST_INIT(&mis_current->loadvm_handlers);
107     qemu_mutex_init(&mis_current->rp_mutex);
108     qemu_event_init(&mis_current->main_thread_load_event, false);
109
110     return mis_current;
111 }
112
113 void migration_incoming_state_destroy(void)
114 {
115     qemu_event_destroy(&mis_current->main_thread_load_event);
116     loadvm_free_handlers(mis_current);
117     g_free(mis_current);
118     mis_current = NULL;
119 }
120
121
122 typedef struct {
123     bool optional;
124     uint32_t size;
125     uint8_t runstate[100];
126     RunState state;
127     bool received;
128 } GlobalState;
129
130 static GlobalState global_state;
131
132 int global_state_store(void)
133 {
134     if (!runstate_store((char *)global_state.runstate,
135                         sizeof(global_state.runstate))) {
136         error_report("runstate name too big: %s", global_state.runstate);
137         trace_migrate_state_too_big();
138         return -EINVAL;
139     }
140     return 0;
141 }
142
143 void global_state_store_running(void)
144 {
145     const char *state = RunState_lookup[RUN_STATE_RUNNING];
146     strncpy((char *)global_state.runstate,
147            state, sizeof(global_state.runstate));
148 }
149
150 static bool global_state_received(void)
151 {
152     return global_state.received;
153 }
154
155 static RunState global_state_get_runstate(void)
156 {
157     return global_state.state;
158 }
159
160 void global_state_set_optional(void)
161 {
162     global_state.optional = true;
163 }
164
165 static bool global_state_needed(void *opaque)
166 {
167     GlobalState *s = opaque;
168     char *runstate = (char *)s->runstate;
169
170     /* If it is not optional, it is mandatory */
171
172     if (s->optional == false) {
173         return true;
174     }
175
176     /* If state is running or paused, it is not needed */
177
178     if (strcmp(runstate, "running") == 0 ||
179         strcmp(runstate, "paused") == 0) {
180         return false;
181     }
182
183     /* for any other state it is needed */
184     return true;
185 }
186
187 static int global_state_post_load(void *opaque, int version_id)
188 {
189     GlobalState *s = opaque;
190     Error *local_err = NULL;
191     int r;
192     char *runstate = (char *)s->runstate;
193
194     s->received = true;
195     trace_migrate_global_state_post_load(runstate);
196
197     r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE_MAX,
198                                 -1, &local_err);
199
200     if (r == -1) {
201         if (local_err) {
202             error_report_err(local_err);
203         }
204         return -EINVAL;
205     }
206     s->state = r;
207
208     return 0;
209 }
210
211 static void global_state_pre_save(void *opaque)
212 {
213     GlobalState *s = opaque;
214
215     trace_migrate_global_state_pre_save((char *)s->runstate);
216     s->size = strlen((char *)s->runstate) + 1;
217 }
218
219 static const VMStateDescription vmstate_globalstate = {
220     .name = "globalstate",
221     .version_id = 1,
222     .minimum_version_id = 1,
223     .post_load = global_state_post_load,
224     .pre_save = global_state_pre_save,
225     .needed = global_state_needed,
226     .fields = (VMStateField[]) {
227         VMSTATE_UINT32(size, GlobalState),
228         VMSTATE_BUFFER(runstate, GlobalState),
229         VMSTATE_END_OF_LIST()
230     },
231 };
232
233 void register_global_state(void)
234 {
235     /* We would use it independently that we receive it */
236     strcpy((char *)&global_state.runstate, "");
237     global_state.received = false;
238     vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
239 }
240
241 static void migrate_generate_event(int new_state)
242 {
243     if (migrate_use_events()) {
244         qapi_event_send_migration(new_state, &error_abort);
245     }
246 }
247
248 /*
249  * Called on -incoming with a defer: uri.
250  * The migration can be started later after any parameters have been
251  * changed.
252  */
253 static void deferred_incoming_migration(Error **errp)
254 {
255     if (deferred_incoming) {
256         error_setg(errp, "Incoming migration already deferred");
257     }
258     deferred_incoming = true;
259 }
260
261 void qemu_start_incoming_migration(const char *uri, Error **errp)
262 {
263     const char *p;
264
265     qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
266     if (!strcmp(uri, "defer")) {
267         deferred_incoming_migration(errp);
268     } else if (strstart(uri, "tcp:", &p)) {
269         tcp_start_incoming_migration(p, errp);
270 #ifdef CONFIG_RDMA
271     } else if (strstart(uri, "rdma:", &p)) {
272         rdma_start_incoming_migration(p, errp);
273 #endif
274 #if !defined(WIN32)
275     } else if (strstart(uri, "exec:", &p)) {
276         exec_start_incoming_migration(p, errp);
277     } else if (strstart(uri, "unix:", &p)) {
278         unix_start_incoming_migration(p, errp);
279     } else if (strstart(uri, "fd:", &p)) {
280         fd_start_incoming_migration(p, errp);
281 #endif
282     } else {
283         error_setg(errp, "unknown migration protocol: %s", uri);
284     }
285 }
286
287 static void process_incoming_migration_co(void *opaque)
288 {
289     QEMUFile *f = opaque;
290     Error *local_err = NULL;
291     int ret;
292
293     migration_incoming_state_new(f);
294     postcopy_state_set(POSTCOPY_INCOMING_NONE);
295     migrate_generate_event(MIGRATION_STATUS_ACTIVE);
296     ret = qemu_loadvm_state(f);
297
298     qemu_fclose(f);
299     free_xbzrle_decoded_buf();
300     migration_incoming_state_destroy();
301
302     if (ret < 0) {
303         migrate_generate_event(MIGRATION_STATUS_FAILED);
304         error_report("load of migration failed: %s", strerror(-ret));
305         migrate_decompress_threads_join();
306         exit(EXIT_FAILURE);
307     }
308
309     /* Make sure all file formats flush their mutable metadata */
310     bdrv_invalidate_cache_all(&local_err);
311     if (local_err) {
312         migrate_generate_event(MIGRATION_STATUS_FAILED);
313         error_report_err(local_err);
314         migrate_decompress_threads_join();
315         exit(EXIT_FAILURE);
316     }
317
318     /*
319      * This must happen after all error conditions are dealt with and
320      * we're sure the VM is going to be running on this host.
321      */
322     qemu_announce_self();
323
324     /* If global state section was not received or we are in running
325        state, we need to obey autostart. Any other state is set with
326        runstate_set. */
327
328     if (!global_state_received() ||
329         global_state_get_runstate() == RUN_STATE_RUNNING) {
330         if (autostart) {
331             vm_start();
332         } else {
333             runstate_set(RUN_STATE_PAUSED);
334         }
335     } else {
336         runstate_set(global_state_get_runstate());
337     }
338     migrate_decompress_threads_join();
339     /*
340      * This must happen after any state changes since as soon as an external
341      * observer sees this event they might start to prod at the VM assuming
342      * it's ready to use.
343      */
344     migrate_generate_event(MIGRATION_STATUS_COMPLETED);
345 }
346
347 void process_incoming_migration(QEMUFile *f)
348 {
349     Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
350     int fd = qemu_get_fd(f);
351
352     assert(fd != -1);
353     migrate_decompress_threads_create();
354     qemu_set_nonblock(fd);
355     qemu_coroutine_enter(co, f);
356 }
357
358 /*
359  * Send a message on the return channel back to the source
360  * of the migration.
361  */
362 void migrate_send_rp_message(MigrationIncomingState *mis,
363                              enum mig_rp_message_type message_type,
364                              uint16_t len, void *data)
365 {
366     trace_migrate_send_rp_message((int)message_type, len);
367     qemu_mutex_lock(&mis->rp_mutex);
368     qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
369     qemu_put_be16(mis->to_src_file, len);
370     qemu_put_buffer(mis->to_src_file, data, len);
371     qemu_fflush(mis->to_src_file);
372     qemu_mutex_unlock(&mis->rp_mutex);
373 }
374
375 /*
376  * Send a 'SHUT' message on the return channel with the given value
377  * to indicate that we've finished with the RP.  Non-0 value indicates
378  * error.
379  */
380 void migrate_send_rp_shut(MigrationIncomingState *mis,
381                           uint32_t value)
382 {
383     uint32_t buf;
384
385     buf = cpu_to_be32(value);
386     migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
387 }
388
389 /*
390  * Send a 'PONG' message on the return channel with the given value
391  * (normally in response to a 'PING')
392  */
393 void migrate_send_rp_pong(MigrationIncomingState *mis,
394                           uint32_t value)
395 {
396     uint32_t buf;
397
398     buf = cpu_to_be32(value);
399     migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
400 }
401
402 /* amount of nanoseconds we are willing to wait for migration to be down.
403  * the choice of nanoseconds is because it is the maximum resolution that
404  * get_clock() can achieve. It is an internal measure. All user-visible
405  * units must be in seconds */
406 static uint64_t max_downtime = 300000000;
407
408 uint64_t migrate_max_downtime(void)
409 {
410     return max_downtime;
411 }
412
413 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
414 {
415     MigrationCapabilityStatusList *head = NULL;
416     MigrationCapabilityStatusList *caps;
417     MigrationState *s = migrate_get_current();
418     int i;
419
420     caps = NULL; /* silence compiler warning */
421     for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
422         if (head == NULL) {
423             head = g_malloc0(sizeof(*caps));
424             caps = head;
425         } else {
426             caps->next = g_malloc0(sizeof(*caps));
427             caps = caps->next;
428         }
429         caps->value =
430             g_malloc(sizeof(*caps->value));
431         caps->value->capability = i;
432         caps->value->state = s->enabled_capabilities[i];
433     }
434
435     return head;
436 }
437
438 MigrationParameters *qmp_query_migrate_parameters(Error **errp)
439 {
440     MigrationParameters *params;
441     MigrationState *s = migrate_get_current();
442
443     params = g_malloc0(sizeof(*params));
444     params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
445     params->compress_threads =
446             s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
447     params->decompress_threads =
448             s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
449     params->x_cpu_throttle_initial =
450             s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL];
451     params->x_cpu_throttle_increment =
452             s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT];
453
454     return params;
455 }
456
457 /*
458  * Return true if we're already in the middle of a migration
459  * (i.e. any of the active or setup states)
460  */
461 static bool migration_is_setup_or_active(int state)
462 {
463     switch (state) {
464     case MIGRATION_STATUS_ACTIVE:
465     case MIGRATION_STATUS_SETUP:
466         return true;
467
468     default:
469         return false;
470
471     }
472 }
473
474 static void get_xbzrle_cache_stats(MigrationInfo *info)
475 {
476     if (migrate_use_xbzrle()) {
477         info->has_xbzrle_cache = true;
478         info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
479         info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
480         info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
481         info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
482         info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
483         info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
484         info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
485     }
486 }
487
488 MigrationInfo *qmp_query_migrate(Error **errp)
489 {
490     MigrationInfo *info = g_malloc0(sizeof(*info));
491     MigrationState *s = migrate_get_current();
492
493     switch (s->state) {
494     case MIGRATION_STATUS_NONE:
495         /* no migration has happened ever */
496         break;
497     case MIGRATION_STATUS_SETUP:
498         info->has_status = true;
499         info->has_total_time = false;
500         break;
501     case MIGRATION_STATUS_ACTIVE:
502     case MIGRATION_STATUS_CANCELLING:
503         info->has_status = true;
504         info->has_total_time = true;
505         info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
506             - s->total_time;
507         info->has_expected_downtime = true;
508         info->expected_downtime = s->expected_downtime;
509         info->has_setup_time = true;
510         info->setup_time = s->setup_time;
511
512         info->has_ram = true;
513         info->ram = g_malloc0(sizeof(*info->ram));
514         info->ram->transferred = ram_bytes_transferred();
515         info->ram->remaining = ram_bytes_remaining();
516         info->ram->total = ram_bytes_total();
517         info->ram->duplicate = dup_mig_pages_transferred();
518         info->ram->skipped = skipped_mig_pages_transferred();
519         info->ram->normal = norm_mig_pages_transferred();
520         info->ram->normal_bytes = norm_mig_bytes_transferred();
521         info->ram->dirty_pages_rate = s->dirty_pages_rate;
522         info->ram->mbps = s->mbps;
523         info->ram->dirty_sync_count = s->dirty_sync_count;
524
525         if (blk_mig_active()) {
526             info->has_disk = true;
527             info->disk = g_malloc0(sizeof(*info->disk));
528             info->disk->transferred = blk_mig_bytes_transferred();
529             info->disk->remaining = blk_mig_bytes_remaining();
530             info->disk->total = blk_mig_bytes_total();
531         }
532
533         if (cpu_throttle_active()) {
534             info->has_x_cpu_throttle_percentage = true;
535             info->x_cpu_throttle_percentage = cpu_throttle_get_percentage();
536         }
537
538         get_xbzrle_cache_stats(info);
539         break;
540     case MIGRATION_STATUS_COMPLETED:
541         get_xbzrle_cache_stats(info);
542
543         info->has_status = true;
544         info->has_total_time = true;
545         info->total_time = s->total_time;
546         info->has_downtime = true;
547         info->downtime = s->downtime;
548         info->has_setup_time = true;
549         info->setup_time = s->setup_time;
550
551         info->has_ram = true;
552         info->ram = g_malloc0(sizeof(*info->ram));
553         info->ram->transferred = ram_bytes_transferred();
554         info->ram->remaining = 0;
555         info->ram->total = ram_bytes_total();
556         info->ram->duplicate = dup_mig_pages_transferred();
557         info->ram->skipped = skipped_mig_pages_transferred();
558         info->ram->normal = norm_mig_pages_transferred();
559         info->ram->normal_bytes = norm_mig_bytes_transferred();
560         info->ram->mbps = s->mbps;
561         info->ram->dirty_sync_count = s->dirty_sync_count;
562         break;
563     case MIGRATION_STATUS_FAILED:
564         info->has_status = true;
565         break;
566     case MIGRATION_STATUS_CANCELLED:
567         info->has_status = true;
568         break;
569     }
570     info->status = s->state;
571
572     return info;
573 }
574
575 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
576                                   Error **errp)
577 {
578     MigrationState *s = migrate_get_current();
579     MigrationCapabilityStatusList *cap;
580
581     if (migration_is_setup_or_active(s->state)) {
582         error_setg(errp, QERR_MIGRATION_ACTIVE);
583         return;
584     }
585
586     for (cap = params; cap; cap = cap->next) {
587         s->enabled_capabilities[cap->value->capability] = cap->value->state;
588     }
589
590     if (migrate_postcopy_ram()) {
591         if (migrate_use_compression()) {
592             /* The decompression threads asynchronously write into RAM
593              * rather than use the atomic copies needed to avoid
594              * userfaulting.  It should be possible to fix the decompression
595              * threads for compatibility in future.
596              */
597             error_report("Postcopy is not currently compatible with "
598                          "compression");
599             s->enabled_capabilities[MIGRATION_CAPABILITY_X_POSTCOPY_RAM] =
600                 false;
601         }
602     }
603 }
604
605 void qmp_migrate_set_parameters(bool has_compress_level,
606                                 int64_t compress_level,
607                                 bool has_compress_threads,
608                                 int64_t compress_threads,
609                                 bool has_decompress_threads,
610                                 int64_t decompress_threads,
611                                 bool has_x_cpu_throttle_initial,
612                                 int64_t x_cpu_throttle_initial,
613                                 bool has_x_cpu_throttle_increment,
614                                 int64_t x_cpu_throttle_increment, Error **errp)
615 {
616     MigrationState *s = migrate_get_current();
617
618     if (has_compress_level && (compress_level < 0 || compress_level > 9)) {
619         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
620                    "is invalid, it should be in the range of 0 to 9");
621         return;
622     }
623     if (has_compress_threads &&
624             (compress_threads < 1 || compress_threads > 255)) {
625         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
626                    "compress_threads",
627                    "is invalid, it should be in the range of 1 to 255");
628         return;
629     }
630     if (has_decompress_threads &&
631             (decompress_threads < 1 || decompress_threads > 255)) {
632         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
633                    "decompress_threads",
634                    "is invalid, it should be in the range of 1 to 255");
635         return;
636     }
637     if (has_x_cpu_throttle_initial &&
638             (x_cpu_throttle_initial < 1 || x_cpu_throttle_initial > 99)) {
639         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
640                    "x_cpu_throttle_initial",
641                    "an integer in the range of 1 to 99");
642     }
643     if (has_x_cpu_throttle_increment &&
644             (x_cpu_throttle_increment < 1 || x_cpu_throttle_increment > 99)) {
645         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
646                    "x_cpu_throttle_increment",
647                    "an integer in the range of 1 to 99");
648     }
649
650     if (has_compress_level) {
651         s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
652     }
653     if (has_compress_threads) {
654         s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads;
655     }
656     if (has_decompress_threads) {
657         s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
658                                                     decompress_threads;
659     }
660     if (has_x_cpu_throttle_initial) {
661         s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
662                                                     x_cpu_throttle_initial;
663     }
664
665     if (has_x_cpu_throttle_increment) {
666         s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
667                                                     x_cpu_throttle_increment;
668     }
669 }
670
671 /* shared migration helpers */
672
673 static void migrate_set_state(MigrationState *s, int old_state, int new_state)
674 {
675     if (atomic_cmpxchg(&s->state, old_state, new_state) == old_state) {
676         trace_migrate_set_state(new_state);
677         migrate_generate_event(new_state);
678     }
679 }
680
681 static void migrate_fd_cleanup(void *opaque)
682 {
683     MigrationState *s = opaque;
684
685     qemu_bh_delete(s->cleanup_bh);
686     s->cleanup_bh = NULL;
687
688     if (s->file) {
689         trace_migrate_fd_cleanup();
690         qemu_mutex_unlock_iothread();
691         qemu_thread_join(&s->thread);
692         qemu_mutex_lock_iothread();
693
694         migrate_compress_threads_join();
695         qemu_fclose(s->file);
696         s->file = NULL;
697     }
698
699     assert(s->state != MIGRATION_STATUS_ACTIVE);
700
701     if (s->state == MIGRATION_STATUS_CANCELLING) {
702         migrate_set_state(s, MIGRATION_STATUS_CANCELLING,
703                           MIGRATION_STATUS_CANCELLED);
704     }
705
706     notifier_list_notify(&migration_state_notifiers, s);
707 }
708
709 void migrate_fd_error(MigrationState *s)
710 {
711     trace_migrate_fd_error();
712     assert(s->file == NULL);
713     migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
714     notifier_list_notify(&migration_state_notifiers, s);
715 }
716
717 static void migrate_fd_cancel(MigrationState *s)
718 {
719     int old_state ;
720     QEMUFile *f = migrate_get_current()->file;
721     trace_migrate_fd_cancel();
722
723     if (s->rp_state.from_dst_file) {
724         /* shutdown the rp socket, so causing the rp thread to shutdown */
725         qemu_file_shutdown(s->rp_state.from_dst_file);
726     }
727
728     do {
729         old_state = s->state;
730         if (!migration_is_setup_or_active(old_state)) {
731             break;
732         }
733         migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
734     } while (s->state != MIGRATION_STATUS_CANCELLING);
735
736     /*
737      * If we're unlucky the migration code might be stuck somewhere in a
738      * send/write while the network has failed and is waiting to timeout;
739      * if we've got shutdown(2) available then we can force it to quit.
740      * The outgoing qemu file gets closed in migrate_fd_cleanup that is
741      * called in a bh, so there is no race against this cancel.
742      */
743     if (s->state == MIGRATION_STATUS_CANCELLING && f) {
744         qemu_file_shutdown(f);
745     }
746 }
747
748 void add_migration_state_change_notifier(Notifier *notify)
749 {
750     notifier_list_add(&migration_state_notifiers, notify);
751 }
752
753 void remove_migration_state_change_notifier(Notifier *notify)
754 {
755     notifier_remove(notify);
756 }
757
758 bool migration_in_setup(MigrationState *s)
759 {
760     return s->state == MIGRATION_STATUS_SETUP;
761 }
762
763 bool migration_has_finished(MigrationState *s)
764 {
765     return s->state == MIGRATION_STATUS_COMPLETED;
766 }
767
768 bool migration_has_failed(MigrationState *s)
769 {
770     return (s->state == MIGRATION_STATUS_CANCELLED ||
771             s->state == MIGRATION_STATUS_FAILED);
772 }
773
774 MigrationState *migrate_init(const MigrationParams *params)
775 {
776     MigrationState *s = migrate_get_current();
777     int64_t bandwidth_limit = s->bandwidth_limit;
778     bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
779     int64_t xbzrle_cache_size = s->xbzrle_cache_size;
780     int compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
781     int compress_thread_count =
782             s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
783     int decompress_thread_count =
784             s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
785     int x_cpu_throttle_initial =
786             s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL];
787     int x_cpu_throttle_increment =
788             s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT];
789
790     memcpy(enabled_capabilities, s->enabled_capabilities,
791            sizeof(enabled_capabilities));
792
793     memset(s, 0, sizeof(*s));
794     s->params = *params;
795     memcpy(s->enabled_capabilities, enabled_capabilities,
796            sizeof(enabled_capabilities));
797     s->xbzrle_cache_size = xbzrle_cache_size;
798
799     s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
800     s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
801                compress_thread_count;
802     s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
803                decompress_thread_count;
804     s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
805                 x_cpu_throttle_initial;
806     s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
807                 x_cpu_throttle_increment;
808     s->bandwidth_limit = bandwidth_limit;
809     migrate_set_state(s, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
810
811     s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
812     return s;
813 }
814
815 static GSList *migration_blockers;
816
817 void migrate_add_blocker(Error *reason)
818 {
819     migration_blockers = g_slist_prepend(migration_blockers, reason);
820 }
821
822 void migrate_del_blocker(Error *reason)
823 {
824     migration_blockers = g_slist_remove(migration_blockers, reason);
825 }
826
827 void qmp_migrate_incoming(const char *uri, Error **errp)
828 {
829     Error *local_err = NULL;
830     static bool once = true;
831
832     if (!deferred_incoming) {
833         error_setg(errp, "For use with '-incoming defer'");
834         return;
835     }
836     if (!once) {
837         error_setg(errp, "The incoming migration has already been started");
838     }
839
840     qemu_start_incoming_migration(uri, &local_err);
841
842     if (local_err) {
843         error_propagate(errp, local_err);
844         return;
845     }
846
847     once = false;
848 }
849
850 void qmp_migrate(const char *uri, bool has_blk, bool blk,
851                  bool has_inc, bool inc, bool has_detach, bool detach,
852                  Error **errp)
853 {
854     Error *local_err = NULL;
855     MigrationState *s = migrate_get_current();
856     MigrationParams params;
857     const char *p;
858
859     params.blk = has_blk && blk;
860     params.shared = has_inc && inc;
861
862     if (migration_is_setup_or_active(s->state) ||
863         s->state == MIGRATION_STATUS_CANCELLING) {
864         error_setg(errp, QERR_MIGRATION_ACTIVE);
865         return;
866     }
867     if (runstate_check(RUN_STATE_INMIGRATE)) {
868         error_setg(errp, "Guest is waiting for an incoming migration");
869         return;
870     }
871
872     if (qemu_savevm_state_blocked(errp)) {
873         return;
874     }
875
876     if (migration_blockers) {
877         *errp = error_copy(migration_blockers->data);
878         return;
879     }
880
881     /* We are starting a new migration, so we want to start in a clean
882        state.  This change is only needed if previous migration
883        failed/was cancelled.  We don't use migrate_set_state() because
884        we are setting the initial state, not changing it. */
885     s->state = MIGRATION_STATUS_NONE;
886
887     s = migrate_init(&params);
888
889     if (strstart(uri, "tcp:", &p)) {
890         tcp_start_outgoing_migration(s, p, &local_err);
891 #ifdef CONFIG_RDMA
892     } else if (strstart(uri, "rdma:", &p)) {
893         rdma_start_outgoing_migration(s, p, &local_err);
894 #endif
895 #if !defined(WIN32)
896     } else if (strstart(uri, "exec:", &p)) {
897         exec_start_outgoing_migration(s, p, &local_err);
898     } else if (strstart(uri, "unix:", &p)) {
899         unix_start_outgoing_migration(s, p, &local_err);
900     } else if (strstart(uri, "fd:", &p)) {
901         fd_start_outgoing_migration(s, p, &local_err);
902 #endif
903     } else {
904         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
905                    "a valid migration protocol");
906         migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
907         return;
908     }
909
910     if (local_err) {
911         migrate_fd_error(s);
912         error_propagate(errp, local_err);
913         return;
914     }
915 }
916
917 void qmp_migrate_cancel(Error **errp)
918 {
919     migrate_fd_cancel(migrate_get_current());
920 }
921
922 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
923 {
924     MigrationState *s = migrate_get_current();
925     int64_t new_size;
926
927     /* Check for truncation */
928     if (value != (size_t)value) {
929         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
930                    "exceeding address space");
931         return;
932     }
933
934     /* Cache should not be larger than guest ram size */
935     if (value > ram_bytes_total()) {
936         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
937                    "exceeds guest ram size ");
938         return;
939     }
940
941     new_size = xbzrle_cache_resize(value);
942     if (new_size < 0) {
943         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
944                    "is smaller than page size");
945         return;
946     }
947
948     s->xbzrle_cache_size = new_size;
949 }
950
951 int64_t qmp_query_migrate_cache_size(Error **errp)
952 {
953     return migrate_xbzrle_cache_size();
954 }
955
956 void qmp_migrate_set_speed(int64_t value, Error **errp)
957 {
958     MigrationState *s;
959
960     if (value < 0) {
961         value = 0;
962     }
963     if (value > SIZE_MAX) {
964         value = SIZE_MAX;
965     }
966
967     s = migrate_get_current();
968     s->bandwidth_limit = value;
969     if (s->file) {
970         qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
971     }
972 }
973
974 void qmp_migrate_set_downtime(double value, Error **errp)
975 {
976     value *= 1e9;
977     value = MAX(0, MIN(UINT64_MAX, value));
978     max_downtime = (uint64_t)value;
979 }
980
981 bool migrate_postcopy_ram(void)
982 {
983     MigrationState *s;
984
985     s = migrate_get_current();
986
987     return s->enabled_capabilities[MIGRATION_CAPABILITY_X_POSTCOPY_RAM];
988 }
989
990 bool migrate_auto_converge(void)
991 {
992     MigrationState *s;
993
994     s = migrate_get_current();
995
996     return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
997 }
998
999 bool migrate_zero_blocks(void)
1000 {
1001     MigrationState *s;
1002
1003     s = migrate_get_current();
1004
1005     return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1006 }
1007
1008 bool migrate_use_compression(void)
1009 {
1010     MigrationState *s;
1011
1012     s = migrate_get_current();
1013
1014     return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
1015 }
1016
1017 int migrate_compress_level(void)
1018 {
1019     MigrationState *s;
1020
1021     s = migrate_get_current();
1022
1023     return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
1024 }
1025
1026 int migrate_compress_threads(void)
1027 {
1028     MigrationState *s;
1029
1030     s = migrate_get_current();
1031
1032     return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
1033 }
1034
1035 int migrate_decompress_threads(void)
1036 {
1037     MigrationState *s;
1038
1039     s = migrate_get_current();
1040
1041     return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
1042 }
1043
1044 bool migrate_use_events(void)
1045 {
1046     MigrationState *s;
1047
1048     s = migrate_get_current();
1049
1050     return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1051 }
1052
1053 int migrate_use_xbzrle(void)
1054 {
1055     MigrationState *s;
1056
1057     s = migrate_get_current();
1058
1059     return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1060 }
1061
1062 int64_t migrate_xbzrle_cache_size(void)
1063 {
1064     MigrationState *s;
1065
1066     s = migrate_get_current();
1067
1068     return s->xbzrle_cache_size;
1069 }
1070
1071 /* migration thread support */
1072 /*
1073  * Something bad happened to the RP stream, mark an error
1074  * The caller shall print or trace something to indicate why
1075  */
1076 static void mark_source_rp_bad(MigrationState *s)
1077 {
1078     s->rp_state.error = true;
1079 }
1080
1081 static struct rp_cmd_args {
1082     ssize_t     len; /* -1 = variable */
1083     const char *name;
1084 } rp_cmd_args[] = {
1085     [MIG_RP_MSG_INVALID]        = { .len = -1, .name = "INVALID" },
1086     [MIG_RP_MSG_SHUT]           = { .len =  4, .name = "SHUT" },
1087     [MIG_RP_MSG_PONG]           = { .len =  4, .name = "PONG" },
1088     [MIG_RP_MSG_MAX]            = { .len = -1, .name = "MAX" },
1089 };
1090
1091 /*
1092  * Handles messages sent on the return path towards the source VM
1093  *
1094  */
1095 static void *source_return_path_thread(void *opaque)
1096 {
1097     MigrationState *ms = opaque;
1098     QEMUFile *rp = ms->rp_state.from_dst_file;
1099     uint16_t header_len, header_type;
1100     const int max_len = 512;
1101     uint8_t buf[max_len];
1102     uint32_t tmp32, sibling_error;
1103     int res;
1104
1105     trace_source_return_path_thread_entry();
1106     while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1107            migration_is_setup_or_active(ms->state)) {
1108         trace_source_return_path_thread_loop_top();
1109         header_type = qemu_get_be16(rp);
1110         header_len = qemu_get_be16(rp);
1111
1112         if (header_type >= MIG_RP_MSG_MAX ||
1113             header_type == MIG_RP_MSG_INVALID) {
1114             error_report("RP: Received invalid message 0x%04x length 0x%04x",
1115                     header_type, header_len);
1116             mark_source_rp_bad(ms);
1117             goto out;
1118         }
1119
1120         if ((rp_cmd_args[header_type].len != -1 &&
1121             header_len != rp_cmd_args[header_type].len) ||
1122             header_len > max_len) {
1123             error_report("RP: Received '%s' message (0x%04x) with"
1124                     "incorrect length %d expecting %zu",
1125                     rp_cmd_args[header_type].name, header_type, header_len,
1126                     (size_t)rp_cmd_args[header_type].len);
1127             mark_source_rp_bad(ms);
1128             goto out;
1129         }
1130
1131         /* We know we've got a valid header by this point */
1132         res = qemu_get_buffer(rp, buf, header_len);
1133         if (res != header_len) {
1134             error_report("RP: Failed reading data for message 0x%04x"
1135                          " read %d expected %d",
1136                          header_type, res, header_len);
1137             mark_source_rp_bad(ms);
1138             goto out;
1139         }
1140
1141         /* OK, we have the message and the data */
1142         switch (header_type) {
1143         case MIG_RP_MSG_SHUT:
1144             sibling_error = be32_to_cpup((uint32_t *)buf);
1145             trace_source_return_path_thread_shut(sibling_error);
1146             if (sibling_error) {
1147                 error_report("RP: Sibling indicated error %d", sibling_error);
1148                 mark_source_rp_bad(ms);
1149             }
1150             /*
1151              * We'll let the main thread deal with closing the RP
1152              * we could do a shutdown(2) on it, but we're the only user
1153              * anyway, so there's nothing gained.
1154              */
1155             goto out;
1156
1157         case MIG_RP_MSG_PONG:
1158             tmp32 = be32_to_cpup((uint32_t *)buf);
1159             trace_source_return_path_thread_pong(tmp32);
1160             break;
1161
1162         default:
1163             break;
1164         }
1165     }
1166     if (rp && qemu_file_get_error(rp)) {
1167         trace_source_return_path_thread_bad_end();
1168         mark_source_rp_bad(ms);
1169     }
1170
1171     trace_source_return_path_thread_end();
1172 out:
1173     ms->rp_state.from_dst_file = NULL;
1174     qemu_fclose(rp);
1175     return NULL;
1176 }
1177
1178 __attribute__ (( unused )) /* Until later in patch series */
1179 static int open_return_path_on_source(MigrationState *ms)
1180 {
1181
1182     ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->file);
1183     if (!ms->rp_state.from_dst_file) {
1184         return -1;
1185     }
1186
1187     trace_open_return_path_on_source();
1188     qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1189                        source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1190
1191     trace_open_return_path_on_source_continue();
1192
1193     return 0;
1194 }
1195
1196 __attribute__ (( unused )) /* Until later in patch series */
1197 /* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1198 static int await_return_path_close_on_source(MigrationState *ms)
1199 {
1200     /*
1201      * If this is a normal exit then the destination will send a SHUT and the
1202      * rp_thread will exit, however if there's an error we need to cause
1203      * it to exit.
1204      */
1205     if (qemu_file_get_error(ms->file) && ms->rp_state.from_dst_file) {
1206         /*
1207          * shutdown(2), if we have it, will cause it to unblock if it's stuck
1208          * waiting for the destination.
1209          */
1210         qemu_file_shutdown(ms->rp_state.from_dst_file);
1211         mark_source_rp_bad(ms);
1212     }
1213     trace_await_return_path_close_on_source_joining();
1214     qemu_thread_join(&ms->rp_state.rp_thread);
1215     trace_await_return_path_close_on_source_close();
1216     return ms->rp_state.error;
1217 }
1218
1219 /**
1220  * migration_completion: Used by migration_thread when there's not much left.
1221  *   The caller 'breaks' the loop when this returns.
1222  *
1223  * @s: Current migration state
1224  * @*old_vm_running: Pointer to old_vm_running flag
1225  * @*start_time: Pointer to time to update
1226  */
1227 static void migration_completion(MigrationState *s, bool *old_vm_running,
1228                                  int64_t *start_time)
1229 {
1230     int ret;
1231
1232     qemu_mutex_lock_iothread();
1233     *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1234     qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1235     *old_vm_running = runstate_is_running();
1236
1237     ret = global_state_store();
1238     if (!ret) {
1239         ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
1240         if (ret >= 0) {
1241             qemu_file_set_rate_limit(s->file, INT64_MAX);
1242             qemu_savevm_state_complete_precopy(s->file);
1243         }
1244     }
1245     qemu_mutex_unlock_iothread();
1246
1247     if (ret < 0) {
1248         goto fail;
1249     }
1250
1251     if (qemu_file_get_error(s->file)) {
1252         trace_migration_completion_file_err();
1253         goto fail;
1254     }
1255
1256     migrate_set_state(s, MIGRATION_STATUS_ACTIVE, MIGRATION_STATUS_COMPLETED);
1257     return;
1258
1259 fail:
1260     migrate_set_state(s, MIGRATION_STATUS_ACTIVE, MIGRATION_STATUS_FAILED);
1261 }
1262
1263 /*
1264  * Master migration thread on the source VM.
1265  * It drives the migration and pumps the data down the outgoing channel.
1266  */
1267 static void *migration_thread(void *opaque)
1268 {
1269     MigrationState *s = opaque;
1270     int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1271     int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
1272     int64_t initial_bytes = 0;
1273     int64_t max_size = 0;
1274     int64_t start_time = initial_time;
1275     int64_t end_time;
1276     bool old_vm_running = false;
1277
1278     rcu_register_thread();
1279
1280     qemu_savevm_state_header(s->file);
1281     qemu_savevm_state_begin(s->file, &s->params);
1282
1283     s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
1284     migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
1285
1286     while (s->state == MIGRATION_STATUS_ACTIVE) {
1287         int64_t current_time;
1288         uint64_t pending_size;
1289
1290         if (!qemu_file_rate_limit(s->file)) {
1291             uint64_t pend_post, pend_nonpost;
1292
1293             qemu_savevm_state_pending(s->file, max_size, &pend_nonpost,
1294                                       &pend_post);
1295             pending_size = pend_nonpost + pend_post;
1296             trace_migrate_pending(pending_size, max_size,
1297                                   pend_post, pend_nonpost);
1298             if (pending_size && pending_size >= max_size) {
1299                 qemu_savevm_state_iterate(s->file);
1300             } else {
1301                 trace_migration_thread_low_pending(pending_size);
1302                 migration_completion(s, &old_vm_running, &start_time);
1303                 break;
1304             }
1305         }
1306
1307         if (qemu_file_get_error(s->file)) {
1308             migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
1309                               MIGRATION_STATUS_FAILED);
1310             break;
1311         }
1312         current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1313         if (current_time >= initial_time + BUFFER_DELAY) {
1314             uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
1315             uint64_t time_spent = current_time - initial_time;
1316             double bandwidth = transferred_bytes / time_spent;
1317             max_size = bandwidth * migrate_max_downtime() / 1000000;
1318
1319             s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
1320                     ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
1321
1322             trace_migrate_transferred(transferred_bytes, time_spent,
1323                                       bandwidth, max_size);
1324             /* if we haven't sent anything, we don't want to recalculate
1325                10000 is a small enough number for our purposes */
1326             if (s->dirty_bytes_rate && transferred_bytes > 10000) {
1327                 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
1328             }
1329
1330             qemu_file_reset_rate_limit(s->file);
1331             initial_time = current_time;
1332             initial_bytes = qemu_ftell(s->file);
1333         }
1334         if (qemu_file_rate_limit(s->file)) {
1335             /* usleep expects microseconds */
1336             g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
1337         }
1338     }
1339
1340     /* If we enabled cpu throttling for auto-converge, turn it off. */
1341     cpu_throttle_stop();
1342     end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1343
1344     qemu_mutex_lock_iothread();
1345     qemu_savevm_state_cleanup();
1346     if (s->state == MIGRATION_STATUS_COMPLETED) {
1347         uint64_t transferred_bytes = qemu_ftell(s->file);
1348         s->total_time = end_time - s->total_time;
1349         s->downtime = end_time - start_time;
1350         if (s->total_time) {
1351             s->mbps = (((double) transferred_bytes * 8.0) /
1352                        ((double) s->total_time)) / 1000;
1353         }
1354         runstate_set(RUN_STATE_POSTMIGRATE);
1355     } else {
1356         if (old_vm_running) {
1357             vm_start();
1358         }
1359     }
1360     qemu_bh_schedule(s->cleanup_bh);
1361     qemu_mutex_unlock_iothread();
1362
1363     rcu_unregister_thread();
1364     return NULL;
1365 }
1366
1367 void migrate_fd_connect(MigrationState *s)
1368 {
1369     /* This is a best 1st approximation. ns to ms */
1370     s->expected_downtime = max_downtime/1000000;
1371     s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
1372
1373     qemu_file_set_rate_limit(s->file,
1374                              s->bandwidth_limit / XFER_LIMIT_RATIO);
1375
1376     /* Notify before starting migration thread */
1377     notifier_list_notify(&migration_state_notifiers, s);
1378
1379     migrate_compress_threads_create();
1380     qemu_thread_create(&s->thread, "migration", migration_thread, s,
1381                        QEMU_THREAD_JOINABLE);
1382 }
1383
1384 PostcopyState  postcopy_state_get(void)
1385 {
1386     return atomic_mb_read(&incoming_postcopy_state);
1387 }
1388
1389 /* Set the state and return the old state */
1390 PostcopyState postcopy_state_set(PostcopyState new_state)
1391 {
1392     return atomic_xchg(&incoming_postcopy_state, new_state);
1393 }
1394