]> git.proxmox.com Git - mirror_qemu.git/blame - migration/migration.c
Postcopy: Mark nohugepage before discard
[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;
1c12e1f5
PB
328 int ret;
329
bca7856a 330 migration_incoming_state_new(f);
093e3c42 331 postcopy_state_set(POSTCOPY_INCOMING_NONE);
7cf1fe6d 332 migrate_generate_event(MIGRATION_STATUS_ACTIVE);
1c12e1f5 333 ret = qemu_loadvm_state(f);
bca7856a 334
1c12e1f5 335 qemu_fclose(f);
905f26f2 336 free_xbzrle_decoded_buf();
bca7856a
DDAG
337 migration_incoming_state_destroy();
338
1c12e1f5 339 if (ret < 0) {
7cf1fe6d 340 migrate_generate_event(MIGRATION_STATUS_FAILED);
db80face 341 error_report("load of migration failed: %s", strerror(-ret));
3fcb38c2 342 migrate_decompress_threads_join();
4aead692 343 exit(EXIT_FAILURE);
511c0231 344 }
511c0231 345
0f15423c 346 /* Make sure all file formats flush their mutable metadata */
5a8a30db
KW
347 bdrv_invalidate_cache_all(&local_err);
348 if (local_err) {
ed1f3e00 349 migrate_generate_event(MIGRATION_STATUS_FAILED);
97baf9d9 350 error_report_err(local_err);
3fcb38c2 351 migrate_decompress_threads_join();
5a8a30db
KW
352 exit(EXIT_FAILURE);
353 }
0f15423c 354
92e37622
AS
355 /*
356 * This must happen after all error conditions are dealt with and
357 * we're sure the VM is going to be running on this host.
358 */
359 qemu_announce_self();
360
172c4356
JQ
361 /* If global state section was not received or we are in running
362 state, we need to obey autostart. Any other state is set with
363 runstate_set. */
df4b1024 364
172c4356
JQ
365 if (!global_state_received() ||
366 global_state_get_runstate() == RUN_STATE_RUNNING) {
df4b1024
JQ
367 if (autostart) {
368 vm_start();
369 } else {
370 runstate_set(RUN_STATE_PAUSED);
371 }
172c4356
JQ
372 } else {
373 runstate_set(global_state_get_runstate());
f5bbfba1 374 }
3fcb38c2 375 migrate_decompress_threads_join();
ed1f3e00
DDAG
376 /*
377 * This must happen after any state changes since as soon as an external
378 * observer sees this event they might start to prod at the VM assuming
379 * it's ready to use.
380 */
381 migrate_generate_event(MIGRATION_STATUS_COMPLETED);
511c0231
JQ
382}
383
82a4da79
PB
384void process_incoming_migration(QEMUFile *f)
385{
386 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
387 int fd = qemu_get_fd(f);
388
389 assert(fd != -1);
3fcb38c2 390 migrate_decompress_threads_create();
f9e8cacc 391 qemu_set_nonblock(fd);
82a4da79
PB
392 qemu_coroutine_enter(co, f);
393}
394
6decec93
DDAG
395/*
396 * Send a message on the return channel back to the source
397 * of the migration.
398 */
399void migrate_send_rp_message(MigrationIncomingState *mis,
400 enum mig_rp_message_type message_type,
401 uint16_t len, void *data)
402{
403 trace_migrate_send_rp_message((int)message_type, len);
404 qemu_mutex_lock(&mis->rp_mutex);
405 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
406 qemu_put_be16(mis->to_src_file, len);
407 qemu_put_buffer(mis->to_src_file, data, len);
408 qemu_fflush(mis->to_src_file);
409 qemu_mutex_unlock(&mis->rp_mutex);
410}
411
412/*
413 * Send a 'SHUT' message on the return channel with the given value
414 * to indicate that we've finished with the RP. Non-0 value indicates
415 * error.
416 */
417void migrate_send_rp_shut(MigrationIncomingState *mis,
418 uint32_t value)
419{
420 uint32_t buf;
421
422 buf = cpu_to_be32(value);
423 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
424}
425
426/*
427 * Send a 'PONG' message on the return channel with the given value
428 * (normally in response to a 'PING')
429 */
430void migrate_send_rp_pong(MigrationIncomingState *mis,
431 uint32_t value)
432{
433 uint32_t buf;
434
435 buf = cpu_to_be32(value);
436 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
437}
438
a0a3fd60
GC
439/* amount of nanoseconds we are willing to wait for migration to be down.
440 * the choice of nanoseconds is because it is the maximum resolution that
441 * get_clock() can achieve. It is an internal measure. All user-visible
442 * units must be in seconds */
f7cd55a0 443static uint64_t max_downtime = 300000000;
a0a3fd60
GC
444
445uint64_t migrate_max_downtime(void)
446{
447 return max_downtime;
448}
449
bbf6da32
OW
450MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
451{
452 MigrationCapabilityStatusList *head = NULL;
453 MigrationCapabilityStatusList *caps;
454 MigrationState *s = migrate_get_current();
455 int i;
456
387eedeb 457 caps = NULL; /* silence compiler warning */
bbf6da32
OW
458 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
459 if (head == NULL) {
460 head = g_malloc0(sizeof(*caps));
461 caps = head;
462 } else {
463 caps->next = g_malloc0(sizeof(*caps));
464 caps = caps->next;
465 }
466 caps->value =
467 g_malloc(sizeof(*caps->value));
468 caps->value->capability = i;
469 caps->value->state = s->enabled_capabilities[i];
470 }
471
472 return head;
473}
474
85de8323
LL
475MigrationParameters *qmp_query_migrate_parameters(Error **errp)
476{
477 MigrationParameters *params;
478 MigrationState *s = migrate_get_current();
479
480 params = g_malloc0(sizeof(*params));
481 params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
482 params->compress_threads =
483 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
484 params->decompress_threads =
485 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
1626fee3
JH
486 params->x_cpu_throttle_initial =
487 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL];
488 params->x_cpu_throttle_increment =
489 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT];
85de8323
LL
490
491 return params;
492}
493
f6844b99
DDAG
494/*
495 * Return true if we're already in the middle of a migration
496 * (i.e. any of the active or setup states)
497 */
498static bool migration_is_setup_or_active(int state)
499{
500 switch (state) {
501 case MIGRATION_STATUS_ACTIVE:
9ec055ae 502 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
f6844b99
DDAG
503 case MIGRATION_STATUS_SETUP:
504 return true;
505
506 default:
507 return false;
508
509 }
510}
511
f36d55af
OW
512static void get_xbzrle_cache_stats(MigrationInfo *info)
513{
514 if (migrate_use_xbzrle()) {
515 info->has_xbzrle_cache = true;
516 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
517 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
518 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
519 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
520 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
8bc39233 521 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
f36d55af
OW
522 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
523 }
524}
525
791e7c82 526MigrationInfo *qmp_query_migrate(Error **errp)
5bb7910a 527{
791e7c82 528 MigrationInfo *info = g_malloc0(sizeof(*info));
17549e84
JQ
529 MigrationState *s = migrate_get_current();
530
531 switch (s->state) {
31194731 532 case MIGRATION_STATUS_NONE:
17549e84
JQ
533 /* no migration has happened ever */
534 break;
31194731 535 case MIGRATION_STATUS_SETUP:
29ae8a41 536 info->has_status = true;
ed4fbd10 537 info->has_total_time = false;
29ae8a41 538 break;
31194731
HZ
539 case MIGRATION_STATUS_ACTIVE:
540 case MIGRATION_STATUS_CANCELLING:
791e7c82 541 info->has_status = true;
7aa939af 542 info->has_total_time = true;
bc72ad67 543 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
7aa939af 544 - s->total_time;
2c52ddf1
JQ
545 info->has_expected_downtime = true;
546 info->expected_downtime = s->expected_downtime;
ed4fbd10
MH
547 info->has_setup_time = true;
548 info->setup_time = s->setup_time;
17549e84 549
791e7c82
LC
550 info->has_ram = true;
551 info->ram = g_malloc0(sizeof(*info->ram));
552 info->ram->transferred = ram_bytes_transferred();
553 info->ram->remaining = ram_bytes_remaining();
554 info->ram->total = ram_bytes_total();
004d4c10 555 info->ram->duplicate = dup_mig_pages_transferred();
f1c72795 556 info->ram->skipped = skipped_mig_pages_transferred();
004d4c10
OW
557 info->ram->normal = norm_mig_pages_transferred();
558 info->ram->normal_bytes = norm_mig_bytes_transferred();
8d017193 559 info->ram->dirty_pages_rate = s->dirty_pages_rate;
7e114f8c 560 info->ram->mbps = s->mbps;
58570ed8 561 info->ram->dirty_sync_count = s->dirty_sync_count;
8d017193 562
17549e84 563 if (blk_mig_active()) {
791e7c82
LC
564 info->has_disk = true;
565 info->disk = g_malloc0(sizeof(*info->disk));
566 info->disk->transferred = blk_mig_bytes_transferred();
567 info->disk->remaining = blk_mig_bytes_remaining();
568 info->disk->total = blk_mig_bytes_total();
ff8d81d8 569 }
f36d55af 570
4782893e
JH
571 if (cpu_throttle_active()) {
572 info->has_x_cpu_throttle_percentage = true;
573 info->x_cpu_throttle_percentage = cpu_throttle_get_percentage();
574 }
575
9ec055ae
DDAG
576 get_xbzrle_cache_stats(info);
577 break;
578 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
579 /* Mostly the same as active; TODO add some postcopy stats */
580 info->has_status = true;
581 info->has_total_time = true;
582 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
583 - s->total_time;
584 info->has_expected_downtime = true;
585 info->expected_downtime = s->expected_downtime;
586 info->has_setup_time = true;
587 info->setup_time = s->setup_time;
588
589 info->has_ram = true;
590 info->ram = g_malloc0(sizeof(*info->ram));
591 info->ram->transferred = ram_bytes_transferred();
592 info->ram->remaining = ram_bytes_remaining();
593 info->ram->total = ram_bytes_total();
594 info->ram->duplicate = dup_mig_pages_transferred();
595 info->ram->skipped = skipped_mig_pages_transferred();
596 info->ram->normal = norm_mig_pages_transferred();
597 info->ram->normal_bytes = norm_mig_bytes_transferred();
598 info->ram->dirty_pages_rate = s->dirty_pages_rate;
599 info->ram->mbps = s->mbps;
600
601 if (blk_mig_active()) {
602 info->has_disk = true;
603 info->disk = g_malloc0(sizeof(*info->disk));
604 info->disk->transferred = blk_mig_bytes_transferred();
605 info->disk->remaining = blk_mig_bytes_remaining();
606 info->disk->total = blk_mig_bytes_total();
607 }
608
f36d55af 609 get_xbzrle_cache_stats(info);
17549e84 610 break;
31194731 611 case MIGRATION_STATUS_COMPLETED:
f36d55af
OW
612 get_xbzrle_cache_stats(info);
613
791e7c82 614 info->has_status = true;
00c14997 615 info->has_total_time = true;
7aa939af 616 info->total_time = s->total_time;
9c5a9fcf
JQ
617 info->has_downtime = true;
618 info->downtime = s->downtime;
ed4fbd10
MH
619 info->has_setup_time = true;
620 info->setup_time = s->setup_time;
d5f8a570
JQ
621
622 info->has_ram = true;
623 info->ram = g_malloc0(sizeof(*info->ram));
624 info->ram->transferred = ram_bytes_transferred();
625 info->ram->remaining = 0;
626 info->ram->total = ram_bytes_total();
004d4c10 627 info->ram->duplicate = dup_mig_pages_transferred();
f1c72795 628 info->ram->skipped = skipped_mig_pages_transferred();
004d4c10
OW
629 info->ram->normal = norm_mig_pages_transferred();
630 info->ram->normal_bytes = norm_mig_bytes_transferred();
7e114f8c 631 info->ram->mbps = s->mbps;
58570ed8 632 info->ram->dirty_sync_count = s->dirty_sync_count;
17549e84 633 break;
31194731 634 case MIGRATION_STATUS_FAILED:
791e7c82 635 info->has_status = true;
17549e84 636 break;
31194731 637 case MIGRATION_STATUS_CANCELLED:
791e7c82 638 info->has_status = true;
17549e84 639 break;
5bb7910a 640 }
cde63fbe 641 info->status = s->state;
791e7c82
LC
642
643 return info;
5bb7910a
AL
644}
645
00458433
OW
646void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
647 Error **errp)
648{
649 MigrationState *s = migrate_get_current();
650 MigrationCapabilityStatusList *cap;
651
f6844b99 652 if (migration_is_setup_or_active(s->state)) {
c6bd8c70 653 error_setg(errp, QERR_MIGRATION_ACTIVE);
00458433
OW
654 return;
655 }
656
657 for (cap = params; cap; cap = cap->next) {
658 s->enabled_capabilities[cap->value->capability] = cap->value->state;
659 }
53dd370c
DDAG
660
661 if (migrate_postcopy_ram()) {
662 if (migrate_use_compression()) {
663 /* The decompression threads asynchronously write into RAM
664 * rather than use the atomic copies needed to avoid
665 * userfaulting. It should be possible to fix the decompression
666 * threads for compatibility in future.
667 */
668 error_report("Postcopy is not currently compatible with "
669 "compression");
670 s->enabled_capabilities[MIGRATION_CAPABILITY_X_POSTCOPY_RAM] =
671 false;
672 }
673 }
00458433
OW
674}
675
85de8323
LL
676void qmp_migrate_set_parameters(bool has_compress_level,
677 int64_t compress_level,
678 bool has_compress_threads,
679 int64_t compress_threads,
680 bool has_decompress_threads,
1626fee3
JH
681 int64_t decompress_threads,
682 bool has_x_cpu_throttle_initial,
683 int64_t x_cpu_throttle_initial,
684 bool has_x_cpu_throttle_increment,
685 int64_t x_cpu_throttle_increment, Error **errp)
85de8323
LL
686{
687 MigrationState *s = migrate_get_current();
688
689 if (has_compress_level && (compress_level < 0 || compress_level > 9)) {
c6bd8c70
MA
690 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
691 "is invalid, it should be in the range of 0 to 9");
85de8323
LL
692 return;
693 }
694 if (has_compress_threads &&
695 (compress_threads < 1 || compress_threads > 255)) {
c6bd8c70
MA
696 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
697 "compress_threads",
698 "is invalid, it should be in the range of 1 to 255");
85de8323
LL
699 return;
700 }
701 if (has_decompress_threads &&
702 (decompress_threads < 1 || decompress_threads > 255)) {
c6bd8c70
MA
703 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
704 "decompress_threads",
705 "is invalid, it should be in the range of 1 to 255");
85de8323
LL
706 return;
707 }
1626fee3
JH
708 if (has_x_cpu_throttle_initial &&
709 (x_cpu_throttle_initial < 1 || x_cpu_throttle_initial > 99)) {
710 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
711 "x_cpu_throttle_initial",
712 "an integer in the range of 1 to 99");
713 }
714 if (has_x_cpu_throttle_increment &&
715 (x_cpu_throttle_increment < 1 || x_cpu_throttle_increment > 99)) {
716 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
717 "x_cpu_throttle_increment",
718 "an integer in the range of 1 to 99");
719 }
85de8323
LL
720
721 if (has_compress_level) {
722 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
723 }
724 if (has_compress_threads) {
725 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads;
726 }
727 if (has_decompress_threads) {
728 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
729 decompress_threads;
730 }
1626fee3
JH
731 if (has_x_cpu_throttle_initial) {
732 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
733 x_cpu_throttle_initial;
734 }
735
736 if (has_x_cpu_throttle_increment) {
737 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
738 x_cpu_throttle_increment;
739 }
85de8323
LL
740}
741
4886a1bc
DDAG
742void qmp_migrate_start_postcopy(Error **errp)
743{
744 MigrationState *s = migrate_get_current();
745
746 if (!migrate_postcopy_ram()) {
747 error_setg(errp, "Enable postcopy with migration_set_capability before"
748 " the start of migration");
749 return;
750 }
751
752 if (s->state == MIGRATION_STATUS_NONE) {
753 error_setg(errp, "Postcopy must be started after migration has been"
754 " started");
755 return;
756 }
757 /*
758 * we don't error if migration has finished since that would be racy
759 * with issuing this command.
760 */
761 atomic_set(&s->start_postcopy, true);
762}
763
065e2813
AL
764/* shared migration helpers */
765
51cf4c1a
Z
766static void migrate_set_state(MigrationState *s, int old_state, int new_state)
767{
a5c17b5f 768 if (atomic_cmpxchg(&s->state, old_state, new_state) == old_state) {
4ba4bc5e 769 trace_migrate_set_state(new_state);
b05dc723 770 migrate_generate_event(new_state);
51cf4c1a
Z
771 }
772}
773
bb1fadc4 774static void migrate_fd_cleanup(void *opaque)
065e2813 775{
bb1fadc4
PB
776 MigrationState *s = opaque;
777
778 qemu_bh_delete(s->cleanup_bh);
779 s->cleanup_bh = NULL;
780
6c595cde
DDAG
781 flush_page_queue(s);
782
065e2813 783 if (s->file) {
9013dca5 784 trace_migrate_fd_cleanup();
404a7c05 785 qemu_mutex_unlock_iothread();
1d34e4bf
DDAG
786 if (s->migration_thread_running) {
787 qemu_thread_join(&s->thread);
788 s->migration_thread_running = false;
789 }
404a7c05
PB
790 qemu_mutex_lock_iothread();
791
8706d2d5 792 migrate_compress_threads_join();
6f190a06
PB
793 qemu_fclose(s->file);
794 s->file = NULL;
065e2813
AL
795 }
796
9ec055ae
DDAG
797 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
798 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
7a2c1721 799
94f5a437
LL
800 if (s->state == MIGRATION_STATUS_CANCELLING) {
801 migrate_set_state(s, MIGRATION_STATUS_CANCELLING,
802 MIGRATION_STATUS_CANCELLED);
7a2c1721 803 }
a3fa1d78
PB
804
805 notifier_list_notify(&migration_state_notifiers, s);
065e2813
AL
806}
807
8b6b99b3 808void migrate_fd_error(MigrationState *s)
065e2813 809{
9013dca5 810 trace_migrate_fd_error();
bb1fadc4 811 assert(s->file == NULL);
7844337d 812 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
bb1fadc4 813 notifier_list_notify(&migration_state_notifiers, s);
458cf28e
JQ
814}
815
0edda1c4 816static void migrate_fd_cancel(MigrationState *s)
065e2813 817{
6f2b811a 818 int old_state ;
a26ba26e 819 QEMUFile *f = migrate_get_current()->file;
9013dca5 820 trace_migrate_fd_cancel();
065e2813 821
70b20477
DDAG
822 if (s->rp_state.from_dst_file) {
823 /* shutdown the rp socket, so causing the rp thread to shutdown */
824 qemu_file_shutdown(s->rp_state.from_dst_file);
825 }
826
6f2b811a
Z
827 do {
828 old_state = s->state;
f6844b99 829 if (!migration_is_setup_or_active(old_state)) {
6f2b811a
Z
830 break;
831 }
31194731
HZ
832 migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
833 } while (s->state != MIGRATION_STATUS_CANCELLING);
a26ba26e
DDAG
834
835 /*
836 * If we're unlucky the migration code might be stuck somewhere in a
837 * send/write while the network has failed and is waiting to timeout;
838 * if we've got shutdown(2) available then we can force it to quit.
839 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
840 * called in a bh, so there is no race against this cancel.
841 */
31194731 842 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
a26ba26e
DDAG
843 qemu_file_shutdown(f);
844 }
065e2813
AL
845}
846
99a0db9b
GH
847void add_migration_state_change_notifier(Notifier *notify)
848{
849 notifier_list_add(&migration_state_notifiers, notify);
850}
851
852void remove_migration_state_change_notifier(Notifier *notify)
853{
31552529 854 notifier_remove(notify);
99a0db9b
GH
855}
856
02edd2e7 857bool migration_in_setup(MigrationState *s)
afe2df69 858{
31194731 859 return s->state == MIGRATION_STATUS_SETUP;
afe2df69
GH
860}
861
7073693b 862bool migration_has_finished(MigrationState *s)
99a0db9b 863{
31194731 864 return s->state == MIGRATION_STATUS_COMPLETED;
99a0db9b 865}
0edda1c4 866
afe2df69
GH
867bool migration_has_failed(MigrationState *s)
868{
31194731
HZ
869 return (s->state == MIGRATION_STATUS_CANCELLED ||
870 s->state == MIGRATION_STATUS_FAILED);
afe2df69
GH
871}
872
9ec055ae
DDAG
873bool migration_in_postcopy(MigrationState *s)
874{
875 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
876}
877
aefeb18b 878MigrationState *migrate_init(const MigrationParams *params)
0edda1c4 879{
17549e84 880 MigrationState *s = migrate_get_current();
d0ae46c1 881 int64_t bandwidth_limit = s->bandwidth_limit;
bbf6da32 882 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
17ad9b35 883 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
43c60a81
LL
884 int compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
885 int compress_thread_count =
886 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
887 int decompress_thread_count =
888 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
1626fee3
JH
889 int x_cpu_throttle_initial =
890 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL];
891 int x_cpu_throttle_increment =
892 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT];
bbf6da32
OW
893
894 memcpy(enabled_capabilities, s->enabled_capabilities,
895 sizeof(enabled_capabilities));
0edda1c4 896
17549e84 897 memset(s, 0, sizeof(*s));
6607ae23 898 s->params = *params;
bbf6da32
OW
899 memcpy(s->enabled_capabilities, enabled_capabilities,
900 sizeof(enabled_capabilities));
17ad9b35 901 s->xbzrle_cache_size = xbzrle_cache_size;
1299c631 902
43c60a81
LL
903 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
904 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
905 compress_thread_count;
906 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
907 decompress_thread_count;
1626fee3
JH
908 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
909 x_cpu_throttle_initial;
910 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
911 x_cpu_throttle_increment;
0edda1c4 912 s->bandwidth_limit = bandwidth_limit;
7844337d 913 migrate_set_state(s, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
0edda1c4 914
6c595cde
DDAG
915 QSIMPLEQ_INIT(&s->src_page_requests);
916
bc72ad67 917 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
0edda1c4
JQ
918 return s;
919}
cab30143 920
fa2756b7
AL
921static GSList *migration_blockers;
922
923void migrate_add_blocker(Error *reason)
924{
925 migration_blockers = g_slist_prepend(migration_blockers, reason);
926}
927
928void migrate_del_blocker(Error *reason)
929{
930 migration_blockers = g_slist_remove(migration_blockers, reason);
931}
932
bf1ae1f4
DDAG
933void qmp_migrate_incoming(const char *uri, Error **errp)
934{
935 Error *local_err = NULL;
4debb5f5 936 static bool once = true;
bf1ae1f4
DDAG
937
938 if (!deferred_incoming) {
4debb5f5 939 error_setg(errp, "For use with '-incoming defer'");
bf1ae1f4
DDAG
940 return;
941 }
4debb5f5
DDAG
942 if (!once) {
943 error_setg(errp, "The incoming migration has already been started");
944 }
bf1ae1f4
DDAG
945
946 qemu_start_incoming_migration(uri, &local_err);
947
948 if (local_err) {
949 error_propagate(errp, local_err);
950 return;
951 }
952
4debb5f5 953 once = false;
bf1ae1f4
DDAG
954}
955
e1c37d0e
LC
956void qmp_migrate(const char *uri, bool has_blk, bool blk,
957 bool has_inc, bool inc, bool has_detach, bool detach,
958 Error **errp)
cab30143 959{
be7059cd 960 Error *local_err = NULL;
17549e84 961 MigrationState *s = migrate_get_current();
6607ae23 962 MigrationParams params;
cab30143 963 const char *p;
cab30143 964
8c0426ae
PP
965 params.blk = has_blk && blk;
966 params.shared = has_inc && inc;
6607ae23 967
f6844b99 968 if (migration_is_setup_or_active(s->state) ||
31194731 969 s->state == MIGRATION_STATUS_CANCELLING) {
c6bd8c70 970 error_setg(errp, QERR_MIGRATION_ACTIVE);
e1c37d0e 971 return;
cab30143 972 }
ca99993a
DDAG
973 if (runstate_check(RUN_STATE_INMIGRATE)) {
974 error_setg(errp, "Guest is waiting for an incoming migration");
975 return;
976 }
977
e1c37d0e
LC
978 if (qemu_savevm_state_blocked(errp)) {
979 return;
cab30143
JQ
980 }
981
fa2756b7 982 if (migration_blockers) {
e1c37d0e
LC
983 *errp = error_copy(migration_blockers->data);
984 return;
fa2756b7
AL
985 }
986
656a2334
JQ
987 /* We are starting a new migration, so we want to start in a clean
988 state. This change is only needed if previous migration
989 failed/was cancelled. We don't use migrate_set_state() because
990 we are setting the initial state, not changing it. */
991 s->state = MIGRATION_STATUS_NONE;
992
6607ae23 993 s = migrate_init(&params);
cab30143
JQ
994
995 if (strstart(uri, "tcp:", &p)) {
f37afb5a 996 tcp_start_outgoing_migration(s, p, &local_err);
2da776db 997#ifdef CONFIG_RDMA
41310c68 998 } else if (strstart(uri, "rdma:", &p)) {
2da776db
MH
999 rdma_start_outgoing_migration(s, p, &local_err);
1000#endif
cab30143
JQ
1001#if !defined(WIN32)
1002 } else if (strstart(uri, "exec:", &p)) {
f37afb5a 1003 exec_start_outgoing_migration(s, p, &local_err);
cab30143 1004 } else if (strstart(uri, "unix:", &p)) {
f37afb5a 1005 unix_start_outgoing_migration(s, p, &local_err);
cab30143 1006 } else if (strstart(uri, "fd:", &p)) {
f37afb5a 1007 fd_start_outgoing_migration(s, p, &local_err);
cab30143 1008#endif
99a0db9b 1009 } else {
c6bd8c70
MA
1010 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1011 "a valid migration protocol");
7844337d 1012 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
e1c37d0e 1013 return;
cab30143
JQ
1014 }
1015
f37afb5a 1016 if (local_err) {
342ab8d1 1017 migrate_fd_error(s);
f37afb5a 1018 error_propagate(errp, local_err);
e1c37d0e 1019 return;
1299c631 1020 }
cab30143
JQ
1021}
1022
6cdedb07 1023void qmp_migrate_cancel(Error **errp)
cab30143 1024{
17549e84 1025 migrate_fd_cancel(migrate_get_current());
cab30143
JQ
1026}
1027
9e1ba4cc
OW
1028void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1029{
1030 MigrationState *s = migrate_get_current();
c91e681a 1031 int64_t new_size;
9e1ba4cc
OW
1032
1033 /* Check for truncation */
1034 if (value != (size_t)value) {
c6bd8c70
MA
1035 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1036 "exceeding address space");
9e1ba4cc
OW
1037 return;
1038 }
1039
a5615b14
OW
1040 /* Cache should not be larger than guest ram size */
1041 if (value > ram_bytes_total()) {
c6bd8c70
MA
1042 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1043 "exceeds guest ram size ");
a5615b14
OW
1044 return;
1045 }
1046
c91e681a
OW
1047 new_size = xbzrle_cache_resize(value);
1048 if (new_size < 0) {
c6bd8c70
MA
1049 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1050 "is smaller than page size");
c91e681a
OW
1051 return;
1052 }
1053
1054 s->xbzrle_cache_size = new_size;
9e1ba4cc
OW
1055}
1056
1057int64_t qmp_query_migrate_cache_size(Error **errp)
1058{
1059 return migrate_xbzrle_cache_size();
1060}
1061
3dc85383 1062void qmp_migrate_set_speed(int64_t value, Error **errp)
cab30143 1063{
cab30143
JQ
1064 MigrationState *s;
1065
3dc85383
LC
1066 if (value < 0) {
1067 value = 0;
99a0db9b 1068 }
442773ce
PB
1069 if (value > SIZE_MAX) {
1070 value = SIZE_MAX;
1071 }
cab30143 1072
17549e84 1073 s = migrate_get_current();
3dc85383 1074 s->bandwidth_limit = value;
442773ce
PB
1075 if (s->file) {
1076 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
1077 }
cab30143
JQ
1078}
1079
4f0a993b 1080void qmp_migrate_set_downtime(double value, Error **errp)
cab30143 1081{
4f0a993b
LC
1082 value *= 1e9;
1083 value = MAX(0, MIN(UINT64_MAX, value));
1084 max_downtime = (uint64_t)value;
99a0db9b 1085}
17ad9b35 1086
53dd370c
DDAG
1087bool migrate_postcopy_ram(void)
1088{
1089 MigrationState *s;
1090
1091 s = migrate_get_current();
1092
1093 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_POSTCOPY_RAM];
1094}
1095
bde1e2ec
CV
1096bool migrate_auto_converge(void)
1097{
1098 MigrationState *s;
1099
1100 s = migrate_get_current();
1101
1102 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1103}
1104
323004a3
PL
1105bool migrate_zero_blocks(void)
1106{
1107 MigrationState *s;
1108
1109 s = migrate_get_current();
1110
1111 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1112}
1113
8706d2d5
LL
1114bool migrate_use_compression(void)
1115{
dde4e694
LL
1116 MigrationState *s;
1117
1118 s = migrate_get_current();
1119
1120 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
8706d2d5
LL
1121}
1122
1123int migrate_compress_level(void)
1124{
1125 MigrationState *s;
1126
1127 s = migrate_get_current();
1128
43c60a81 1129 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
8706d2d5
LL
1130}
1131
1132int migrate_compress_threads(void)
1133{
1134 MigrationState *s;
1135
1136 s = migrate_get_current();
1137
43c60a81 1138 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
8706d2d5
LL
1139}
1140
3fcb38c2
LL
1141int migrate_decompress_threads(void)
1142{
1143 MigrationState *s;
1144
1145 s = migrate_get_current();
1146
43c60a81 1147 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
3fcb38c2
LL
1148}
1149
b05dc723
JQ
1150bool migrate_use_events(void)
1151{
1152 MigrationState *s;
1153
1154 s = migrate_get_current();
1155
1156 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1157}
1158
17ad9b35
OW
1159int migrate_use_xbzrle(void)
1160{
1161 MigrationState *s;
1162
1163 s = migrate_get_current();
1164
1165 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1166}
1167
1168int64_t migrate_xbzrle_cache_size(void)
1169{
1170 MigrationState *s;
1171
1172 s = migrate_get_current();
1173
1174 return s->xbzrle_cache_size;
1175}
0d82d0e8 1176
70b20477
DDAG
1177/* migration thread support */
1178/*
1179 * Something bad happened to the RP stream, mark an error
1180 * The caller shall print or trace something to indicate why
1181 */
1182static void mark_source_rp_bad(MigrationState *s)
1183{
1184 s->rp_state.error = true;
1185}
1186
1187static struct rp_cmd_args {
1188 ssize_t len; /* -1 = variable */
1189 const char *name;
1190} rp_cmd_args[] = {
1191 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1192 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1193 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
1e2d90eb
DDAG
1194 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1195 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
70b20477
DDAG
1196 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1197};
1198
1e2d90eb
DDAG
1199/*
1200 * Process a request for pages received on the return path,
1201 * We're allowed to send more than requested (e.g. to round to our page size)
1202 * and we don't need to send pages that have already been sent.
1203 */
1204static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1205 ram_addr_t start, size_t len)
1206{
6c595cde
DDAG
1207 long our_host_ps = getpagesize();
1208
1e2d90eb 1209 trace_migrate_handle_rp_req_pages(rbname, start, len);
6c595cde
DDAG
1210
1211 /*
1212 * Since we currently insist on matching page sizes, just sanity check
1213 * we're being asked for whole host pages.
1214 */
1215 if (start & (our_host_ps-1) ||
1216 (len & (our_host_ps-1))) {
1217 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1218 " len: %zd", __func__, start, len);
1219 mark_source_rp_bad(ms);
1220 return;
1221 }
1222
1223 if (ram_save_queue_pages(ms, rbname, start, len)) {
1224 mark_source_rp_bad(ms);
1225 }
1e2d90eb
DDAG
1226}
1227
70b20477
DDAG
1228/*
1229 * Handles messages sent on the return path towards the source VM
1230 *
1231 */
1232static void *source_return_path_thread(void *opaque)
1233{
1234 MigrationState *ms = opaque;
1235 QEMUFile *rp = ms->rp_state.from_dst_file;
1236 uint16_t header_len, header_type;
1237 const int max_len = 512;
1238 uint8_t buf[max_len];
1239 uint32_t tmp32, sibling_error;
1e2d90eb
DDAG
1240 ram_addr_t start = 0; /* =0 to silence warning */
1241 size_t len = 0, expected_len;
70b20477
DDAG
1242 int res;
1243
1244 trace_source_return_path_thread_entry();
1245 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1246 migration_is_setup_or_active(ms->state)) {
1247 trace_source_return_path_thread_loop_top();
1248 header_type = qemu_get_be16(rp);
1249 header_len = qemu_get_be16(rp);
1250
1251 if (header_type >= MIG_RP_MSG_MAX ||
1252 header_type == MIG_RP_MSG_INVALID) {
1253 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1254 header_type, header_len);
1255 mark_source_rp_bad(ms);
1256 goto out;
1257 }
1258
1259 if ((rp_cmd_args[header_type].len != -1 &&
1260 header_len != rp_cmd_args[header_type].len) ||
1261 header_len > max_len) {
1262 error_report("RP: Received '%s' message (0x%04x) with"
1263 "incorrect length %d expecting %zu",
1264 rp_cmd_args[header_type].name, header_type, header_len,
1265 (size_t)rp_cmd_args[header_type].len);
1266 mark_source_rp_bad(ms);
1267 goto out;
1268 }
1269
1270 /* We know we've got a valid header by this point */
1271 res = qemu_get_buffer(rp, buf, header_len);
1272 if (res != header_len) {
1273 error_report("RP: Failed reading data for message 0x%04x"
1274 " read %d expected %d",
1275 header_type, res, header_len);
1276 mark_source_rp_bad(ms);
1277 goto out;
1278 }
1279
1280 /* OK, we have the message and the data */
1281 switch (header_type) {
1282 case MIG_RP_MSG_SHUT:
1283 sibling_error = be32_to_cpup((uint32_t *)buf);
1284 trace_source_return_path_thread_shut(sibling_error);
1285 if (sibling_error) {
1286 error_report("RP: Sibling indicated error %d", sibling_error);
1287 mark_source_rp_bad(ms);
1288 }
1289 /*
1290 * We'll let the main thread deal with closing the RP
1291 * we could do a shutdown(2) on it, but we're the only user
1292 * anyway, so there's nothing gained.
1293 */
1294 goto out;
1295
1296 case MIG_RP_MSG_PONG:
1297 tmp32 = be32_to_cpup((uint32_t *)buf);
1298 trace_source_return_path_thread_pong(tmp32);
1299 break;
1300
1e2d90eb
DDAG
1301 case MIG_RP_MSG_REQ_PAGES:
1302 start = be64_to_cpup((uint64_t *)buf);
1303 len = be32_to_cpup((uint32_t *)(buf + 8));
1304 migrate_handle_rp_req_pages(ms, NULL, start, len);
1305 break;
1306
1307 case MIG_RP_MSG_REQ_PAGES_ID:
1308 expected_len = 12 + 1; /* header + termination */
1309
1310 if (header_len >= expected_len) {
1311 start = be64_to_cpup((uint64_t *)buf);
1312 len = be32_to_cpup((uint32_t *)(buf + 8));
1313 /* Now we expect an idstr */
1314 tmp32 = buf[12]; /* Length of the following idstr */
1315 buf[13 + tmp32] = '\0';
1316 expected_len += tmp32;
1317 }
1318 if (header_len != expected_len) {
1319 error_report("RP: Req_Page_id with length %d expecting %zd",
1320 header_len, expected_len);
1321 mark_source_rp_bad(ms);
1322 goto out;
1323 }
1324 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
1325 break;
1326
70b20477
DDAG
1327 default:
1328 break;
1329 }
1330 }
1331 if (rp && qemu_file_get_error(rp)) {
1332 trace_source_return_path_thread_bad_end();
1333 mark_source_rp_bad(ms);
1334 }
1335
1336 trace_source_return_path_thread_end();
1337out:
1338 ms->rp_state.from_dst_file = NULL;
1339 qemu_fclose(rp);
1340 return NULL;
1341}
1342
70b20477
DDAG
1343static int open_return_path_on_source(MigrationState *ms)
1344{
1345
1346 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->file);
1347 if (!ms->rp_state.from_dst_file) {
1348 return -1;
1349 }
1350
1351 trace_open_return_path_on_source();
1352 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1353 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1354
1355 trace_open_return_path_on_source_continue();
1356
1357 return 0;
1358}
1359
70b20477
DDAG
1360/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1361static int await_return_path_close_on_source(MigrationState *ms)
1362{
1363 /*
1364 * If this is a normal exit then the destination will send a SHUT and the
1365 * rp_thread will exit, however if there's an error we need to cause
1366 * it to exit.
1367 */
1368 if (qemu_file_get_error(ms->file) && ms->rp_state.from_dst_file) {
1369 /*
1370 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1371 * waiting for the destination.
1372 */
1373 qemu_file_shutdown(ms->rp_state.from_dst_file);
1374 mark_source_rp_bad(ms);
1375 }
1376 trace_await_return_path_close_on_source_joining();
1377 qemu_thread_join(&ms->rp_state.rp_thread);
1378 trace_await_return_path_close_on_source_close();
1379 return ms->rp_state.error;
1380}
1381
1d34e4bf
DDAG
1382/*
1383 * Switch from normal iteration to postcopy
1384 * Returns non-0 on error
1385 */
1386static int postcopy_start(MigrationState *ms, bool *old_vm_running)
1387{
1388 int ret;
1389 const QEMUSizedBuffer *qsb;
1390 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1391 migrate_set_state(ms, MIGRATION_STATUS_ACTIVE,
1392 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1393
1394 trace_postcopy_start();
1395 qemu_mutex_lock_iothread();
1396 trace_postcopy_start_set_run();
1397
1398 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1399 *old_vm_running = runstate_is_running();
1400 global_state_store();
1401 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
1402
1403 if (ret < 0) {
1404 goto fail;
1405 }
1406
1407 /*
1408 * in Finish migrate and with the io-lock held everything should
1409 * be quiet, but we've potentially still got dirty pages and we
1410 * need to tell the destination to throw any pages it's already received
1411 * that are dirty
1412 */
1413 if (ram_postcopy_send_discard_bitmap(ms)) {
1414 error_report("postcopy send discard bitmap failed");
1415 goto fail;
1416 }
1417
1418 /*
1419 * send rest of state - note things that are doing postcopy
1420 * will notice we're in POSTCOPY_ACTIVE and not actually
1421 * wrap their state up here
1422 */
1423 qemu_file_set_rate_limit(ms->file, INT64_MAX);
1424 /* Ping just for debugging, helps line traces up */
1425 qemu_savevm_send_ping(ms->file, 2);
1426
1427 /*
1428 * While loading the device state we may trigger page transfer
1429 * requests and the fd must be free to process those, and thus
1430 * the destination must read the whole device state off the fd before
1431 * it starts processing it. Unfortunately the ad-hoc migration format
1432 * doesn't allow the destination to know the size to read without fully
1433 * parsing it through each devices load-state code (especially the open
1434 * coded devices that use get/put).
1435 * So we wrap the device state up in a package with a length at the start;
1436 * to do this we use a qemu_buf to hold the whole of the device state.
1437 */
1438 QEMUFile *fb = qemu_bufopen("w", NULL);
1439 if (!fb) {
1440 error_report("Failed to create buffered file");
1441 goto fail;
1442 }
1443
c76201ab
DDAG
1444 /*
1445 * Make sure the receiver can get incoming pages before we send the rest
1446 * of the state
1447 */
1448 qemu_savevm_send_postcopy_listen(fb);
1449
1d34e4bf
DDAG
1450 qemu_savevm_state_complete_precopy(fb);
1451 qemu_savevm_send_ping(fb, 3);
1452
1453 qemu_savevm_send_postcopy_run(fb);
1454
1455 /* <><> end of stuff going into the package */
1456 qsb = qemu_buf_get(fb);
1457
1458 /* Now send that blob */
1459 if (qemu_savevm_send_packaged(ms->file, qsb)) {
1460 goto fail_closefb;
1461 }
1462 qemu_fclose(fb);
1463 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
1464
1465 qemu_mutex_unlock_iothread();
1466
1467 /*
1468 * Although this ping is just for debug, it could potentially be
1469 * used for getting a better measurement of downtime at the source.
1470 */
1471 qemu_savevm_send_ping(ms->file, 4);
1472
1473 ret = qemu_file_get_error(ms->file);
1474 if (ret) {
1475 error_report("postcopy_start: Migration stream errored");
1476 migrate_set_state(ms, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1477 MIGRATION_STATUS_FAILED);
1478 }
1479
1480 return ret;
1481
1482fail_closefb:
1483 qemu_fclose(fb);
1484fail:
1485 migrate_set_state(ms, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1486 MIGRATION_STATUS_FAILED);
1487 qemu_mutex_unlock_iothread();
1488 return -1;
1489}
1490
09f6c85e
DDAG
1491/**
1492 * migration_completion: Used by migration_thread when there's not much left.
1493 * The caller 'breaks' the loop when this returns.
1494 *
1495 * @s: Current migration state
36f48567 1496 * @current_active_state: The migration state we expect to be in
09f6c85e
DDAG
1497 * @*old_vm_running: Pointer to old_vm_running flag
1498 * @*start_time: Pointer to time to update
1499 */
36f48567
DDAG
1500static void migration_completion(MigrationState *s, int current_active_state,
1501 bool *old_vm_running,
09f6c85e
DDAG
1502 int64_t *start_time)
1503{
1504 int ret;
1505
b10ac0c4
DDAG
1506 if (s->state == MIGRATION_STATUS_ACTIVE) {
1507 qemu_mutex_lock_iothread();
1508 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1509 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1510 *old_vm_running = runstate_is_running();
1511 ret = global_state_store();
1512
1513 if (!ret) {
1514 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
1515 if (ret >= 0) {
1516 qemu_file_set_rate_limit(s->file, INT64_MAX);
1517 qemu_savevm_state_complete_precopy(s->file);
1518 }
1519 }
1520 qemu_mutex_unlock_iothread();
09f6c85e 1521
b10ac0c4
DDAG
1522 if (ret < 0) {
1523 goto fail;
09f6c85e 1524 }
b10ac0c4
DDAG
1525 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1526 trace_migration_completion_postcopy_end();
1527
1528 qemu_savevm_state_complete_postcopy(s->file);
1529 trace_migration_completion_postcopy_end_after_complete();
09f6c85e 1530 }
09f6c85e 1531
b10ac0c4
DDAG
1532 /*
1533 * If rp was opened we must clean up the thread before
1534 * cleaning everything else up (since if there are no failures
1535 * it will wait for the destination to send it's status in
1536 * a SHUT command).
1537 * Postcopy opens rp if enabled (even if it's not avtivated)
1538 */
1539 if (migrate_postcopy_ram()) {
1540 int rp_error;
1541 trace_migration_completion_postcopy_end_before_rp();
1542 rp_error = await_return_path_close_on_source(s);
1543 trace_migration_completion_postcopy_end_after_rp(rp_error);
1544 if (rp_error) {
1545 goto fail;
1546 }
09f6c85e
DDAG
1547 }
1548
1549 if (qemu_file_get_error(s->file)) {
1550 trace_migration_completion_file_err();
1551 goto fail;
1552 }
1553
36f48567 1554 migrate_set_state(s, current_active_state, MIGRATION_STATUS_COMPLETED);
09f6c85e
DDAG
1555 return;
1556
1557fail:
36f48567 1558 migrate_set_state(s, current_active_state, MIGRATION_STATUS_FAILED);
09f6c85e
DDAG
1559}
1560
70b20477
DDAG
1561/*
1562 * Master migration thread on the source VM.
1563 * It drives the migration and pumps the data down the outgoing channel.
1564 */
5f496a1b 1565static void *migration_thread(void *opaque)
0d82d0e8 1566{
9848a404 1567 MigrationState *s = opaque;
1d34e4bf 1568 /* Used by the bandwidth calcs, updated later */
bc72ad67
AB
1569 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1570 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
be7172e2 1571 int64_t initial_bytes = 0;
0d82d0e8 1572 int64_t max_size = 0;
a3fa1d78 1573 int64_t start_time = initial_time;
94f5a437 1574 int64_t end_time;
a3fa1d78 1575 bool old_vm_running = false;
1d34e4bf
DDAG
1576 bool entered_postcopy = false;
1577 /* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */
1578 enum MigrationStatus current_active_state = MIGRATION_STATUS_ACTIVE;
76f5933a 1579
ab28bd23
PB
1580 rcu_register_thread();
1581
f796baa1 1582 qemu_savevm_state_header(s->file);
1d34e4bf
DDAG
1583
1584 if (migrate_postcopy_ram()) {
1585 /* Now tell the dest that it should open its end so it can reply */
1586 qemu_savevm_send_open_return_path(s->file);
1587
1588 /* And do a ping that will make stuff easier to debug */
1589 qemu_savevm_send_ping(s->file, 1);
1590
1591 /*
1592 * Tell the destination that we *might* want to do postcopy later;
1593 * if the other end can't do postcopy it should fail now, nice and
1594 * early.
1595 */
1596 qemu_savevm_send_postcopy_advise(s->file);
1597 }
1598
dba433c0 1599 qemu_savevm_state_begin(s->file, &s->params);
0d82d0e8 1600
bc72ad67 1601 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
1d34e4bf 1602 current_active_state = MIGRATION_STATUS_ACTIVE;
31194731 1603 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
29ae8a41 1604
9ec055ae
DDAG
1605 trace_migration_thread_setup_complete();
1606
1607 while (s->state == MIGRATION_STATUS_ACTIVE ||
1608 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
a3e879cd 1609 int64_t current_time;
c369f40d 1610 uint64_t pending_size;
0d82d0e8 1611
a0ff044b 1612 if (!qemu_file_rate_limit(s->file)) {
c31b098f
DDAG
1613 uint64_t pend_post, pend_nonpost;
1614
1615 qemu_savevm_state_pending(s->file, max_size, &pend_nonpost,
1616 &pend_post);
1617 pending_size = pend_nonpost + pend_post;
1618 trace_migrate_pending(pending_size, max_size,
1619 pend_post, pend_nonpost);
b22ff1fb 1620 if (pending_size && pending_size >= max_size) {
1d34e4bf
DDAG
1621 /* Still a significant amount to transfer */
1622
1623 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1624 if (migrate_postcopy_ram() &&
1625 s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE &&
1626 pend_nonpost <= max_size &&
1627 atomic_read(&s->start_postcopy)) {
1628
1629 if (!postcopy_start(s, &old_vm_running)) {
1630 current_active_state = MIGRATION_STATUS_POSTCOPY_ACTIVE;
1631 entered_postcopy = true;
1632 }
1633
1634 continue;
1635 }
1636 /* Just another iteration step */
35ecd943 1637 qemu_savevm_state_iterate(s->file, entered_postcopy);
c369f40d 1638 } else {
09f6c85e 1639 trace_migration_thread_low_pending(pending_size);
1d34e4bf 1640 migration_completion(s, current_active_state,
36f48567 1641 &old_vm_running, &start_time);
09f6c85e 1642 break;
c369f40d
JQ
1643 }
1644 }
f4410a5d 1645
fd45ee2c 1646 if (qemu_file_get_error(s->file)) {
1d34e4bf
DDAG
1647 migrate_set_state(s, current_active_state, MIGRATION_STATUS_FAILED);
1648 trace_migration_thread_file_err();
fd45ee2c
PB
1649 break;
1650 }
bc72ad67 1651 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
0d82d0e8 1652 if (current_time >= initial_time + BUFFER_DELAY) {
be7172e2 1653 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
77417f10 1654 uint64_t time_spent = current_time - initial_time;
0d82d0e8
JQ
1655 double bandwidth = transferred_bytes / time_spent;
1656 max_size = bandwidth * migrate_max_downtime() / 1000000;
1657
7e114f8c
MH
1658 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
1659 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
1660
9013dca5
AK
1661 trace_migrate_transferred(transferred_bytes, time_spent,
1662 bandwidth, max_size);
90f8ae72
JQ
1663 /* if we haven't sent anything, we don't want to recalculate
1664 10000 is a small enough number for our purposes */
1665 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
1666 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
1667 }
0d82d0e8 1668
1964a397 1669 qemu_file_reset_rate_limit(s->file);
0d82d0e8 1670 initial_time = current_time;
be7172e2 1671 initial_bytes = qemu_ftell(s->file);
0d82d0e8 1672 }
a0ff044b 1673 if (qemu_file_rate_limit(s->file)) {
0d82d0e8
JQ
1674 /* usleep expects microseconds */
1675 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
1676 }
a3fa1d78
PB
1677 }
1678
1d34e4bf 1679 trace_migration_thread_after_loop();
070afca2
JH
1680 /* If we enabled cpu throttling for auto-converge, turn it off. */
1681 cpu_throttle_stop();
94f5a437 1682 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
070afca2 1683
f4410a5d 1684 qemu_mutex_lock_iothread();
ea7415fa 1685 qemu_savevm_state_cleanup();
31194731 1686 if (s->state == MIGRATION_STATUS_COMPLETED) {
d6ed7312 1687 uint64_t transferred_bytes = qemu_ftell(s->file);
a3fa1d78 1688 s->total_time = end_time - s->total_time;
1d34e4bf
DDAG
1689 if (!entered_postcopy) {
1690 s->downtime = end_time - start_time;
1691 }
d6ed7312
PL
1692 if (s->total_time) {
1693 s->mbps = (((double) transferred_bytes * 8.0) /
1694 ((double) s->total_time)) / 1000;
1695 }
a3fa1d78
PB
1696 runstate_set(RUN_STATE_POSTMIGRATE);
1697 } else {
1d34e4bf 1698 if (old_vm_running && !entered_postcopy) {
a3fa1d78 1699 vm_start();
dba433c0 1700 }
0d82d0e8 1701 }
bb1fadc4 1702 qemu_bh_schedule(s->cleanup_bh);
dba433c0 1703 qemu_mutex_unlock_iothread();
f4410a5d 1704
ab28bd23 1705 rcu_unregister_thread();
0d82d0e8
JQ
1706 return NULL;
1707}
1708
9848a404 1709void migrate_fd_connect(MigrationState *s)
0d82d0e8 1710{
cc283e3b
JQ
1711 /* This is a best 1st approximation. ns to ms */
1712 s->expected_downtime = max_downtime/1000000;
bb1fadc4 1713 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
0d82d0e8 1714
442773ce
PB
1715 qemu_file_set_rate_limit(s->file,
1716 s->bandwidth_limit / XFER_LIMIT_RATIO);
1717
9287ac27
SH
1718 /* Notify before starting migration thread */
1719 notifier_list_notify(&migration_state_notifiers, s);
1720
1d34e4bf
DDAG
1721 /*
1722 * Open the return path; currently for postcopy but other things might
1723 * also want it.
1724 */
1725 if (migrate_postcopy_ram()) {
1726 if (open_return_path_on_source(s)) {
1727 error_report("Unable to open return-path for postcopy");
1728 migrate_set_state(s, MIGRATION_STATUS_SETUP,
1729 MIGRATION_STATUS_FAILED);
1730 migrate_fd_cleanup(s);
1731 return;
1732 }
1733 }
1734
8706d2d5 1735 migrate_compress_threads_create();
4900116e 1736 qemu_thread_create(&s->thread, "migration", migration_thread, s,
bb1fadc4 1737 QEMU_THREAD_JOINABLE);
1d34e4bf 1738 s->migration_thread_running = true;
0d82d0e8 1739}
093e3c42
DDAG
1740
1741PostcopyState postcopy_state_get(void)
1742{
1743 return atomic_mb_read(&incoming_postcopy_state);
1744}
1745
1746/* Set the state and return the old state */
1747PostcopyState postcopy_state_set(PostcopyState new_state)
1748{
1749 return atomic_xchg(&incoming_postcopy_state, new_state);
1750}
1751