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