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