]> Git Repo - qemu.git/blame - migration/migration.c
qapi: Use QAPI_LIST_APPEND in trivial cases
[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 <[email protected]>
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
1393a485 16#include "qemu/osdep.h"
f348b6d1 17#include "qemu/cutils.h"
d49b6836 18#include "qemu/error-report.h"
db725815 19#include "qemu/main-loop.h"
795c40b8 20#include "migration/blocker.h"
f4dbe1bf 21#include "exec.h"
7fcac4a2 22#include "fd.h"
61e8b148 23#include "socket.h"
54d31236 24#include "sysemu/runstate.h"
46517dd4 25#include "sysemu/sysemu.h"
b0c3cf94 26#include "sysemu/cpu-throttle.h"
e1a3ecee 27#include "rdma.h"
7b1e1a22 28#include "ram.h"
84a899de 29#include "migration/global_state.h"
c4b63b7c 30#include "migration/misc.h"
6666c96a 31#include "migration.h"
20a519a0 32#include "savevm.h"
40014d81 33#include "qemu-file-channel.h"
08a0aee1 34#include "qemu-file.h"
987772d9 35#include "migration/vmstate.h"
737e150e 36#include "block/block.h"
e688df6b 37#include "qapi/error.h"
9aca82ba 38#include "qapi/clone-visitor.h"
31e4c354 39#include "qapi/qapi-visit-migration.h"
9aca82ba 40#include "qapi/qapi-visit-sockets.h"
9af23989
MA
41#include "qapi/qapi-commands-migration.h"
42#include "qapi/qapi-events-migration.h"
cc7a8ea7 43#include "qapi/qmp/qerror.h"
15280c36 44#include "qapi/qmp/qnull.h"
ab28bd23 45#include "qemu/rcu.h"
2c9e6fec 46#include "block.h"
be07b0ac 47#include "postcopy-ram.h"
766bd176 48#include "qemu/thread.h"
c09e5bb1 49#include "trace.h"
51180423 50#include "exec/target_page.h"
61b67d47 51#include "io/channel-buffer.h"
35a6ed4f 52#include "migration/colo.h"
4ffdb337 53#include "hw/boards.h"
a27bd6c7 54#include "hw/qdev-properties.h"
ce35e229 55#include "hw/qdev-properties-system.h"
9d18af93 56#include "monitor/monitor.h"
50510ea2 57#include "net/announce.h"
c7e0acd5 58#include "qemu/queue.h"
d32ca5ad 59#include "multifd.h"
b5eea99e 60#include "qemu/yank.h"
065e2813 61
3710586c
KW
62#ifdef CONFIG_VFIO
63#include "hw/vfio/vfio-common.h"
64#endif
65
7590a2ae 66#define MAX_THROTTLE (128 << 20) /* Migration transfer speed throttling */
5bb7910a 67
5b4e1eb7
JQ
68/* Amount of time to allocate to each "chunk" of bandwidth-throttled
69 * data. */
70#define BUFFER_DELAY 100
71#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
72
2ff30257
AA
73/* Time in milliseconds we are allowed to stop the source,
74 * for sending the last part */
75#define DEFAULT_MIGRATE_SET_DOWNTIME 300
76
87c9cc1c
DHB
77/* Maximum migrate downtime set to 2000 seconds */
78#define MAX_MIGRATE_DOWNTIME_SECONDS 2000
79#define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
80
8706d2d5
LL
81/* Default compression thread count */
82#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
3fcb38c2
LL
83/* Default decompression thread count, usually decompression is at
84 * least 4 times as fast as compression.*/
85#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
8706d2d5
LL
86/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
87#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
1626fee3 88/* Define default autoconverge cpu throttle migration parameters */
dc14a470 89#define DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD 50
d85a31d1
JH
90#define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
91#define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
4cbc9c7f 92#define DEFAULT_MIGRATE_MAX_CPU_THROTTLE 99
8706d2d5 93
17ad9b35 94/* Migration XBZRLE default cache size */
73af8dd8 95#define DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE (64 * 1024 * 1024)
17ad9b35 96
131b2153
ZC
97/* The delay time (in ms) between two COLO checkpoints */
98#define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY (200 * 100)
4075fb1c 99#define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2
96eef042 100#define DEFAULT_MIGRATE_MULTIFD_COMPRESSION MULTIFD_COMPRESSION_NONE
9004db48
JQ
101/* 0: means nocompress, 1: best speed, ... 9: best compress ratio */
102#define DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL 1
6a9ad154
JQ
103/* 0: means nocompress, 1: best speed, ... 20: best compress ratio */
104#define DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL 1
68b53591 105
7e555c6c
DDAG
106/* Background transfer rate for postcopy, 0 means unlimited, note
107 * that page requests can still exceed this limit.
108 */
109#define DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH 0
110
ee3d96ba
DDAG
111/*
112 * Parameters for self_announce_delay giving a stream of RARP/ARP
113 * packets after migration.
114 */
115#define DEFAULT_MIGRATE_ANNOUNCE_INITIAL 50
116#define DEFAULT_MIGRATE_ANNOUNCE_MAX 550
117#define DEFAULT_MIGRATE_ANNOUNCE_ROUNDS 5
118#define DEFAULT_MIGRATE_ANNOUNCE_STEP 100
119
99a0db9b
GH
120static NotifierList migration_state_notifiers =
121 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
122
da6f1790
JQ
123/* Messages sent on the return path from destination to source */
124enum mig_rp_message_type {
125 MIG_RP_MSG_INVALID = 0, /* Must be 0 */
126 MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */
127 MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */
128
129 MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */
130 MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */
a335debb 131 MIG_RP_MSG_RECV_BITMAP, /* send recved_bitmap back to source */
13955b89 132 MIG_RP_MSG_RESUME_ACK, /* tell source that we are ready to resume */
da6f1790
JQ
133
134 MIG_RP_MSG_MAX
135};
136
17549e84
JQ
137/* When we add fault tolerance, we could have several
138 migrations at once. For now we don't need to add
139 dynamic creation of migration */
140
e5cb7e76 141static MigrationState *current_migration;
e1b1b1bc 142static MigrationIncomingState *current_incoming;
e5cb7e76 143
8b0b29dc 144static bool migration_object_check(MigrationState *ms, Error **errp);
0331c8ca
DDAG
145static int migration_maybe_pause(MigrationState *s,
146 int *current_active_state,
147 int new_state);
892ae715 148static void migrate_fd_cancel(MigrationState *s);
8b0b29dc 149
8f8bfffc
PX
150static gint page_request_addr_cmp(gconstpointer ap, gconstpointer bp)
151{
152 uintptr_t a = (uintptr_t) ap, b = (uintptr_t) bp;
153
154 return (a > b) - (a < b);
155}
156
e5cb7e76
PX
157void migration_object_init(void)
158{
8b0b29dc 159 Error *err = NULL;
4ffdb337 160
e5cb7e76
PX
161 /* This can only be called once. */
162 assert(!current_migration);
163 current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION));
4ffdb337 164
e1b1b1bc
PX
165 /*
166 * Init the migrate incoming object as well no matter whether
167 * we'll use it or not.
168 */
169 assert(!current_incoming);
170 current_incoming = g_new0(MigrationIncomingState, 1);
171 current_incoming->state = MIGRATION_STATUS_NONE;
172 current_incoming->postcopy_remote_fds =
173 g_array_new(FALSE, TRUE, sizeof(struct PostCopyFD));
174 qemu_mutex_init(&current_incoming->rp_mutex);
175 qemu_event_init(&current_incoming->main_thread_load_event, false);
176 qemu_sem_init(&current_incoming->postcopy_pause_sem_dst, 0);
177 qemu_sem_init(&current_incoming->postcopy_pause_sem_fault, 0);
8f8bfffc
PX
178 qemu_mutex_init(&current_incoming->page_request_mutex);
179 current_incoming->page_requested = g_tree_new(page_request_addr_cmp);
e1b1b1bc 180
8b0b29dc
PX
181 if (!migration_object_check(current_migration, &err)) {
182 error_report_err(err);
183 exit(1);
184 }
e0d17dfd
PB
185
186 blk_mig_init();
187 ram_mig_init();
188 dirty_bitmap_mig_init();
e5cb7e76
PX
189}
190
892ae715 191void migration_shutdown(void)
1f895604 192{
892ae715
DDAG
193 /*
194 * Cancel the current migration - that will (eventually)
195 * stop the migration using this structure
196 */
197 migrate_fd_cancel(current_migration);
1f895604 198 object_unref(OBJECT(current_migration));
1499ab09
VSO
199
200 /*
201 * Cancel outgoing migration of dirty bitmaps. It should
202 * at least unref used block nodes.
203 */
204 dirty_bitmap_mig_cancel_outgoing();
205
206 /*
207 * Cancel incoming migration of dirty bitmaps. Dirty bitmaps
208 * are non-critical data, and their loss never considered as
209 * something serious.
210 */
211 dirty_bitmap_mig_cancel_incoming();
1f895604
VSO
212}
213
bca7856a 214/* For outgoing */
859bc756 215MigrationState *migrate_get_current(void)
17549e84 216{
e5cb7e76
PX
217 /* This can only be called after the object created. */
218 assert(current_migration);
219 return current_migration;
17549e84
JQ
220}
221
bca7856a
DDAG
222MigrationIncomingState *migration_incoming_get_current(void)
223{
e1b1b1bc
PX
224 assert(current_incoming);
225 return current_incoming;
bca7856a
DDAG
226}
227
228void migration_incoming_state_destroy(void)
229{
b4b076da
JQ
230 struct MigrationIncomingState *mis = migration_incoming_get_current();
231
3482655b 232 if (mis->to_src_file) {
660819b1
PX
233 /* Tell source that we are done */
234 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
3482655b
PX
235 qemu_fclose(mis->to_src_file);
236 mis->to_src_file = NULL;
237 }
238
660819b1
PX
239 if (mis->from_src_file) {
240 qemu_fclose(mis->from_src_file);
241 mis->from_src_file = NULL;
242 }
00fa4fc8
DDAG
243 if (mis->postcopy_remote_fds) {
244 g_array_free(mis->postcopy_remote_fds, TRUE);
245 mis->postcopy_remote_fds = NULL;
246 }
660819b1 247
1783c00f
DDAG
248 qemu_event_reset(&mis->main_thread_load_event);
249
8f8bfffc
PX
250 if (mis->page_requested) {
251 g_tree_destroy(mis->page_requested);
252 mis->page_requested = NULL;
253 }
254
9aca82ba
JQ
255 if (mis->socket_address_list) {
256 qapi_free_SocketAddressList(mis->socket_address_list);
257 mis->socket_address_list = NULL;
258 }
b5eea99e
LS
259
260 yank_unregister_instance(MIGRATION_YANK_INSTANCE);
bca7856a
DDAG
261}
262
b05dc723
JQ
263static void migrate_generate_event(int new_state)
264{
265 if (migrate_use_events()) {
3ab72385 266 qapi_event_send_migration(new_state);
b05dc723
JQ
267 }
268}
269
0f073f44
DDAG
270static bool migrate_late_block_activate(void)
271{
272 MigrationState *s;
273
274 s = migrate_get_current();
275
276 return s->enabled_capabilities[
277 MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE];
278}
279
da6f1790
JQ
280/*
281 * Send a message on the return channel back to the source
282 * of the migration.
283 */
d6208e35
PX
284static int migrate_send_rp_message(MigrationIncomingState *mis,
285 enum mig_rp_message_type message_type,
286 uint16_t len, void *data)
da6f1790 287{
d6208e35
PX
288 int ret = 0;
289
da6f1790
JQ
290 trace_migrate_send_rp_message((int)message_type, len);
291 qemu_mutex_lock(&mis->rp_mutex);
d6208e35
PX
292
293 /*
294 * It's possible that the file handle got lost due to network
295 * failures.
296 */
297 if (!mis->to_src_file) {
298 ret = -EIO;
299 goto error;
300 }
301
da6f1790
JQ
302 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
303 qemu_put_be16(mis->to_src_file, len);
304 qemu_put_buffer(mis->to_src_file, data, len);
305 qemu_fflush(mis->to_src_file);
d6208e35
PX
306
307 /* It's possible that qemu file got error during sending */
308 ret = qemu_file_get_error(mis->to_src_file);
309
310error:
da6f1790 311 qemu_mutex_unlock(&mis->rp_mutex);
d6208e35 312 return ret;
da6f1790
JQ
313}
314
2e2bce16
PX
315/* Request one page from the source VM at the given start address.
316 * rb: the RAMBlock to request the page in
1e2d90eb
DDAG
317 * Start: Address offset within the RB
318 * Len: Length in bytes required - must be a multiple of pagesize
319 */
7a267fc4
PX
320int migrate_send_rp_message_req_pages(MigrationIncomingState *mis,
321 RAMBlock *rb, ram_addr_t start)
1e2d90eb 322{
cb8d4c8f 323 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
1e2d90eb 324 size_t msglen = 12; /* start + len */
2e2bce16 325 size_t len = qemu_ram_pagesize(rb);
d6208e35 326 enum mig_rp_message_type msg_type;
2e2bce16
PX
327 const char *rbname;
328 int rbname_len;
1e2d90eb
DDAG
329
330 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
331 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
332
2e2bce16
PX
333 /*
334 * We maintain the last ramblock that we requested for page. Note that we
335 * don't need locking because this function will only be called within the
336 * postcopy ram fault thread.
337 */
338 if (rb != mis->last_rb) {
339 mis->last_rb = rb;
340
341 rbname = qemu_ram_get_idstr(rb);
342 rbname_len = strlen(rbname);
343
1e2d90eb
DDAG
344 assert(rbname_len < 256);
345
346 bufc[msglen++] = rbname_len;
347 memcpy(bufc + msglen, rbname, rbname_len);
348 msglen += rbname_len;
d6208e35 349 msg_type = MIG_RP_MSG_REQ_PAGES_ID;
1e2d90eb 350 } else {
d6208e35 351 msg_type = MIG_RP_MSG_REQ_PAGES;
1e2d90eb 352 }
d6208e35
PX
353
354 return migrate_send_rp_message(mis, msg_type, msglen, bufc);
1e2d90eb
DDAG
355}
356
7a267fc4 357int migrate_send_rp_req_pages(MigrationIncomingState *mis,
8f8bfffc 358 RAMBlock *rb, ram_addr_t start, uint64_t haddr)
7a267fc4 359{
8f8bfffc 360 void *aligned = (void *)(uintptr_t)(haddr & (-qemu_ram_pagesize(rb)));
a2429283 361 bool received = false;
8f8bfffc
PX
362
363 WITH_QEMU_LOCK_GUARD(&mis->page_request_mutex) {
364 received = ramblock_recv_bitmap_test_byte_offset(rb, start);
365 if (!received && !g_tree_lookup(mis->page_requested, aligned)) {
366 /*
367 * The page has not been received, and it's not yet in the page
368 * request list. Queue it. Set the value of element to 1, so that
369 * things like g_tree_lookup() will return TRUE (1) when found.
370 */
371 g_tree_insert(mis->page_requested, aligned, (gpointer)1);
372 mis->page_requested_count++;
373 trace_postcopy_page_req_add(aligned, mis->page_requested_count);
374 }
375 }
376
377 /*
378 * If the page is there, skip sending the message. We don't even need the
379 * lock because as long as the page arrived, it'll be there forever.
380 */
381 if (received) {
382 return 0;
383 }
384
7a267fc4
PX
385 return migrate_send_rp_message_req_pages(mis, rb, start);
386}
387
aad555c2
ZC
388static bool migration_colo_enabled;
389bool migration_incoming_colo_enabled(void)
390{
391 return migration_colo_enabled;
392}
393
394void migration_incoming_disable_colo(void)
395{
18b1d3c9 396 ram_block_discard_disable(false);
aad555c2
ZC
397 migration_colo_enabled = false;
398}
399
18b1d3c9 400int migration_incoming_enable_colo(void)
aad555c2 401{
18b1d3c9
DH
402 if (ram_block_discard_disable(true)) {
403 error_report("COLO: cannot disable RAM discard");
404 return -EBUSY;
405 }
aad555c2 406 migration_colo_enabled = true;
18b1d3c9 407 return 0;
aad555c2
ZC
408}
409
9aca82ba
JQ
410void migrate_add_address(SocketAddress *address)
411{
412 MigrationIncomingState *mis = migration_incoming_get_current();
9aca82ba 413
54aa3de7
EB
414 QAPI_LIST_PREPEND(mis->socket_address_list,
415 QAPI_CLONE(SocketAddress, address));
9aca82ba
JQ
416}
417
e69d50d6 418static void qemu_start_incoming_migration(const char *uri, Error **errp)
5bb7910a 419{
d658f65c 420 const char *p = NULL;
34c9dd8e 421
b5eea99e
LS
422 if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) {
423 return;
424 }
425
3ab72385 426 qapi_event_send_migration(MIGRATION_STATUS_SETUP);
e69d50d6
PB
427 if (strstart(uri, "tcp:", &p) ||
428 strstart(uri, "unix:", NULL) ||
429 strstart(uri, "vsock:", NULL)) {
d658f65c 430 socket_start_incoming_migration(p ? p : uri, errp);
2da776db 431#ifdef CONFIG_RDMA
adde220a 432 } else if (strstart(uri, "rdma:", &p)) {
2da776db
MH
433 rdma_start_incoming_migration(p, errp);
434#endif
adde220a 435 } else if (strstart(uri, "exec:", &p)) {
43eaae28 436 exec_start_incoming_migration(p, errp);
adde220a 437 } else if (strstart(uri, "fd:", &p)) {
43eaae28 438 fd_start_incoming_migration(p, errp);
adde220a 439 } else {
b5eea99e 440 yank_unregister_instance(MIGRATION_YANK_INSTANCE);
312fd5f2 441 error_setg(errp, "unknown migration protocol: %s", uri);
8ca5e801 442 }
5bb7910a
AL
443}
444
0aa6aefc
DL
445static void process_incoming_migration_bh(void *opaque)
446{
447 Error *local_err = NULL;
448 MigrationIncomingState *mis = opaque;
449
0f073f44
DDAG
450 /* If capability late_block_activate is set:
451 * Only fire up the block code now if we're going to restart the
452 * VM, else 'cont' will do it.
453 * This causes file locking to happen; so we don't want it to happen
454 * unless we really are starting the VM.
455 */
456 if (!migrate_late_block_activate() ||
457 (autostart && (!global_state_received() ||
458 global_state_get_runstate() == RUN_STATE_RUNNING))) {
459 /* Make sure all file formats flush their mutable metadata.
460 * If we get an error here, just don't restart the VM yet. */
461 bdrv_invalidate_cache_all(&local_err);
462 if (local_err) {
463 error_report_err(local_err);
464 local_err = NULL;
465 autostart = false;
466 }
d35ff5e6
KW
467 }
468
0aa6aefc
DL
469 /*
470 * This must happen after all error conditions are dealt with and
471 * we're sure the VM is going to be running on this host.
472 */
7659505c 473 qemu_announce_self(&mis->announce_timer, migrate_announce_params());
0aa6aefc 474
f986c3d2
JQ
475 if (multifd_load_cleanup(&local_err) != 0) {
476 error_report_err(local_err);
477 autostart = false;
478 }
0aa6aefc
DL
479 /* If global state section was not received or we are in running
480 state, we need to obey autostart. Any other state is set with
481 runstate_set. */
482
b35ebdf0
VSO
483 dirty_bitmap_mig_before_vm_start();
484
0aa6aefc
DL
485 if (!global_state_received() ||
486 global_state_get_runstate() == RUN_STATE_RUNNING) {
487 if (autostart) {
488 vm_start();
489 } else {
490 runstate_set(RUN_STATE_PAUSED);
491 }
db009729
ZC
492 } else if (migration_incoming_colo_enabled()) {
493 migration_incoming_disable_colo();
494 vm_start();
0aa6aefc
DL
495 } else {
496 runstate_set(global_state_get_runstate());
497 }
0aa6aefc
DL
498 /*
499 * This must happen after any state changes since as soon as an external
500 * observer sees this event they might start to prod at the VM assuming
501 * it's ready to use.
502 */
503 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
504 MIGRATION_STATUS_COMPLETED);
505 qemu_bh_delete(mis->bh);
506 migration_incoming_state_destroy();
507}
508
82a4da79 509static void process_incoming_migration_co(void *opaque)
511c0231 510{
b4b076da 511 MigrationIncomingState *mis = migration_incoming_get_current();
e9bef235 512 PostcopyState ps;
1c12e1f5 513 int ret;
8e48ac95 514 Error *local_err = NULL;
1c12e1f5 515
4f0fae7f 516 assert(mis->from_src_file);
92370989 517 mis->migration_incoming_co = qemu_coroutine_self();
67f11b5c 518 mis->largest_page_size = qemu_ram_pagesize_largest();
093e3c42 519 postcopy_state_set(POSTCOPY_INCOMING_NONE);
93d7af6f
HZ
520 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
521 MIGRATION_STATUS_ACTIVE);
4f0fae7f 522 ret = qemu_loadvm_state(mis->from_src_file);
bca7856a 523
e9bef235
DDAG
524 ps = postcopy_state_get();
525 trace_process_incoming_migration_co_end(ret, ps);
526 if (ps != POSTCOPY_INCOMING_NONE) {
527 if (ps == POSTCOPY_INCOMING_ADVISE) {
528 /*
529 * Where a migration had postcopy enabled (and thus went to advise)
530 * but managed to complete within the precopy period, we can use
531 * the normal exit.
532 */
533 postcopy_ram_incoming_cleanup(mis);
534 } else if (ret >= 0) {
535 /*
536 * Postcopy was started, cleanup should happen at the end of the
537 * postcopy thread.
538 */
539 trace_process_incoming_migration_co_postcopy_end_main();
540 return;
541 }
542 /* Else if something went wrong then just fall out of the normal exit */
543 }
544
25d0c16f 545 /* we get COLO info, and know if we are in COLO mode */
aad555c2 546 if (!ret && migration_incoming_colo_enabled()) {
8e48ac95
ZC
547 /* Make sure all file formats flush their mutable metadata */
548 bdrv_invalidate_cache_all(&local_err);
549 if (local_err) {
8e48ac95 550 error_report_err(local_err);
6d99c2d4 551 goto fail;
8e48ac95
ZC
552 }
553
25d0c16f
HZ
554 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
555 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
556 mis->have_colo_incoming_thread = true;
557 qemu_coroutine_yield();
558
559 /* Wait checkpoint incoming thread exit before free resource */
560 qemu_thread_join(&mis->colo_incoming_thread);
13af18f2
ZC
561 /* We hold the global iothread lock, so it is safe here */
562 colo_release_ram_cache();
25d0c16f
HZ
563 }
564
1c12e1f5 565 if (ret < 0) {
db80face 566 error_report("load of migration failed: %s", strerror(-ret));
6d99c2d4 567 goto fail;
511c0231 568 }
0aa6aefc
DL
569 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
570 qemu_bh_schedule(mis->bh);
92370989 571 mis->migration_incoming_co = NULL;
6d99c2d4
FL
572 return;
573fail:
574 local_err = NULL;
575 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
576 MIGRATION_STATUS_FAILED);
577 qemu_fclose(mis->from_src_file);
578 if (multifd_load_cleanup(&local_err) != 0) {
579 error_report_err(local_err);
580 }
581 exit(EXIT_FAILURE);
511c0231
JQ
582}
583
b673eab4
JQ
584/**
585 * @migration_incoming_setup: Setup incoming migration
586 *
587 * Returns 0 for no error or 1 for error
588 *
589 * @f: file for main migration channel
590 * @errp: where to put errors
591 */
592static int migration_incoming_setup(QEMUFile *f, Error **errp)
82a4da79 593{
4f0fae7f 594 MigrationIncomingState *mis = migration_incoming_get_current();
b673eab4 595 Error *local_err = NULL;
82a4da79 596
b673eab4 597 if (multifd_load_setup(&local_err) != 0) {
f986c3d2
JQ
598 /* We haven't been able to create multifd threads
599 nothing better to do */
b673eab4 600 error_report_err(local_err);
f986c3d2
JQ
601 exit(EXIT_FAILURE);
602 }
603
4f0fae7f
JQ
604 if (!mis->from_src_file) {
605 mis->from_src_file = f;
606 }
06ad5135 607 qemu_file_set_blocking(f, false);
b673eab4 608 return 0;
e595a01a
JQ
609}
610
36c2f8be 611void migration_incoming_process(void)
e595a01a
JQ
612{
613 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL);
0b8b8753 614 qemu_coroutine_enter(co);
82a4da79
PB
615}
616
884835fa
PX
617/* Returns true if recovered from a paused migration, otherwise false */
618static bool postcopy_try_recover(QEMUFile *f)
e595a01a 619{
d96c9e8d
PX
620 MigrationIncomingState *mis = migration_incoming_get_current();
621
622 if (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
623 /* Resumed from a paused postcopy migration */
624
625 mis->from_src_file = f;
626 /* Postcopy has standalone thread to do vm load */
627 qemu_file_set_blocking(f, true);
628
629 /* Re-configure the return path */
630 mis->to_src_file = qemu_file_get_return_path(f);
631
632 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
633 MIGRATION_STATUS_POSTCOPY_RECOVER);
634
635 /*
636 * Here, we only wake up the main loading thread (while the
637 * fault thread will still be waiting), so that we can receive
638 * commands from source now, and answer it if needed. The
639 * fault thread will be woken up afterwards until we are sure
640 * that source is ready to reply to page requests.
641 */
642 qemu_sem_post(&mis->postcopy_pause_sem_dst);
884835fa
PX
643 return true;
644 }
645
646 return false;
647}
648
b673eab4 649void migration_fd_process_incoming(QEMUFile *f, Error **errp)
884835fa 650{
b673eab4
JQ
651 Error *local_err = NULL;
652
884835fa
PX
653 if (postcopy_try_recover(f)) {
654 return;
d96c9e8d 655 }
884835fa 656
b673eab4 657 if (migration_incoming_setup(f, &local_err)) {
2155ceaf 658 error_propagate(errp, local_err);
b673eab4
JQ
659 return;
660 }
884835fa 661 migration_incoming_process();
e595a01a
JQ
662}
663
49ed0d24 664void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp)
4f0fae7f
JQ
665{
666 MigrationIncomingState *mis = migration_incoming_get_current();
b673eab4 667 Error *local_err = NULL;
a429e7f4 668 bool start_migration;
4f0fae7f
JQ
669
670 if (!mis->from_src_file) {
a429e7f4 671 /* The first connection (multifd may have multiple) */
4f0fae7f 672 QEMUFile *f = qemu_fopen_channel_input(ioc);
a429e7f4
PX
673
674 /* If it's a recovery, we're done */
884835fa
PX
675 if (postcopy_try_recover(f)) {
676 return;
677 }
a429e7f4 678
b673eab4 679 if (migration_incoming_setup(f, &local_err)) {
2155ceaf 680 error_propagate(errp, local_err);
b673eab4
JQ
681 return;
682 }
a429e7f4
PX
683
684 /*
685 * Common migration only needs one channel, so we can start
686 * right now. Multifd needs more than one channel, we wait.
687 */
688 start_migration = !migrate_use_multifd();
689 } else {
690 /* Multiple connections */
691 assert(migrate_use_multifd());
49ed0d24
FL
692 start_migration = multifd_recv_new_channel(ioc, &local_err);
693 if (local_err) {
694 error_propagate(errp, local_err);
695 return;
696 }
4f0fae7f 697 }
81e62053 698
a429e7f4 699 if (start_migration) {
81e62053
PX
700 migration_incoming_process();
701 }
4f0fae7f
JQ
702}
703
428d8908
JQ
704/**
705 * @migration_has_all_channels: We have received all channels that we need
706 *
707 * Returns true when we have got connections to all the channels that
708 * we need for migration.
709 */
710bool migration_has_all_channels(void)
711{
ca273df3 712 MigrationIncomingState *mis = migration_incoming_get_current();
62c1e0ca
JQ
713 bool all_channels;
714
715 all_channels = multifd_recv_all_channels_created();
716
ca273df3 717 return all_channels && mis->from_src_file != NULL;
428d8908
JQ
718}
719
6decec93
DDAG
720/*
721 * Send a 'SHUT' message on the return channel with the given value
722 * to indicate that we've finished with the RP. Non-0 value indicates
723 * error.
724 */
725void migrate_send_rp_shut(MigrationIncomingState *mis,
726 uint32_t value)
727{
728 uint32_t buf;
729
730 buf = cpu_to_be32(value);
731 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
732}
733
734/*
735 * Send a 'PONG' message on the return channel with the given value
736 * (normally in response to a 'PING')
737 */
738void migrate_send_rp_pong(MigrationIncomingState *mis,
739 uint32_t value)
740{
741 uint32_t buf;
742
743 buf = cpu_to_be32(value);
744 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
745}
746
a335debb
PX
747void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
748 char *block_name)
749{
750 char buf[512];
751 int len;
752 int64_t res;
753
754 /*
755 * First, we send the header part. It contains only the len of
756 * idstr, and the idstr itself.
757 */
758 len = strlen(block_name);
759 buf[0] = len;
760 memcpy(buf + 1, block_name, len);
761
762 if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
763 error_report("%s: MSG_RP_RECV_BITMAP only used for recovery",
764 __func__);
765 return;
766 }
767
768 migrate_send_rp_message(mis, MIG_RP_MSG_RECV_BITMAP, len + 1, buf);
769
770 /*
771 * Next, we dump the received bitmap to the stream.
772 *
773 * TODO: currently we are safe since we are the only one that is
774 * using the to_src_file handle (fault thread is still paused),
775 * and it's ok even not taking the mutex. However the best way is
776 * to take the lock before sending the message header, and release
777 * the lock after sending the bitmap.
778 */
779 qemu_mutex_lock(&mis->rp_mutex);
780 res = ramblock_recv_bitmap_send(mis->to_src_file, block_name);
781 qemu_mutex_unlock(&mis->rp_mutex);
782
783 trace_migrate_send_rp_recv_bitmap(block_name, res);
784}
785
13955b89
PX
786void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value)
787{
788 uint32_t buf;
789
790 buf = cpu_to_be32(value);
791 migrate_send_rp_message(mis, MIG_RP_MSG_RESUME_ACK, sizeof(buf), &buf);
792}
793
bbf6da32
OW
794MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
795{
796 MigrationCapabilityStatusList *head = NULL;
797 MigrationCapabilityStatusList *caps;
798 MigrationState *s = migrate_get_current();
799 int i;
800
387eedeb 801 caps = NULL; /* silence compiler warning */
7fb1cf16 802 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
ed1701c6
DDAG
803#ifndef CONFIG_LIVE_BLOCK_MIGRATION
804 if (i == MIGRATION_CAPABILITY_BLOCK) {
805 continue;
806 }
807#endif
bbf6da32
OW
808 if (head == NULL) {
809 head = g_malloc0(sizeof(*caps));
810 caps = head;
811 } else {
812 caps->next = g_malloc0(sizeof(*caps));
813 caps = caps->next;
814 }
815 caps->value =
816 g_malloc(sizeof(*caps->value));
817 caps->value->capability = i;
818 caps->value->state = s->enabled_capabilities[i];
819 }
820
821 return head;
822}
823
85de8323
LL
824MigrationParameters *qmp_query_migrate_parameters(Error **errp)
825{
826 MigrationParameters *params;
827 MigrationState *s = migrate_get_current();
828
e87fae4c 829 /* TODO use QAPI_CLONE() instead of duplicating it inline */
85de8323 830 params = g_malloc0(sizeof(*params));
de63ab61 831 params->has_compress_level = true;
2594f56d 832 params->compress_level = s->parameters.compress_level;
de63ab61 833 params->has_compress_threads = true;
2594f56d 834 params->compress_threads = s->parameters.compress_threads;
1d58872a
XG
835 params->has_compress_wait_thread = true;
836 params->compress_wait_thread = s->parameters.compress_wait_thread;
de63ab61 837 params->has_decompress_threads = true;
2594f56d 838 params->decompress_threads = s->parameters.decompress_threads;
dc14a470
KZ
839 params->has_throttle_trigger_threshold = true;
840 params->throttle_trigger_threshold = s->parameters.throttle_trigger_threshold;
de63ab61 841 params->has_cpu_throttle_initial = true;
2594f56d 842 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
de63ab61 843 params->has_cpu_throttle_increment = true;
2594f56d 844 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
cbbf8182
KZ
845 params->has_cpu_throttle_tailslow = true;
846 params->cpu_throttle_tailslow = s->parameters.cpu_throttle_tailslow;
8cc99dcd 847 params->has_tls_creds = true;
69ef1f36 848 params->tls_creds = g_strdup(s->parameters.tls_creds);
8cc99dcd 849 params->has_tls_hostname = true;
69ef1f36 850 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
d2f1d29b 851 params->has_tls_authz = true;
7cd75cbd
MZ
852 params->tls_authz = g_strdup(s->parameters.tls_authz ?
853 s->parameters.tls_authz : "");
2ff30257
AA
854 params->has_max_bandwidth = true;
855 params->max_bandwidth = s->parameters.max_bandwidth;
856 params->has_downtime_limit = true;
857 params->downtime_limit = s->parameters.downtime_limit;
fe39a4d4 858 params->has_x_checkpoint_delay = true;
68b53591 859 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
2833c59b
JQ
860 params->has_block_incremental = true;
861 params->block_incremental = s->parameters.block_incremental;
cbfd6c95
JQ
862 params->has_multifd_channels = true;
863 params->multifd_channels = s->parameters.multifd_channels;
96eef042
JQ
864 params->has_multifd_compression = true;
865 params->multifd_compression = s->parameters.multifd_compression;
9004db48
JQ
866 params->has_multifd_zlib_level = true;
867 params->multifd_zlib_level = s->parameters.multifd_zlib_level;
6a9ad154
JQ
868 params->has_multifd_zstd_level = true;
869 params->multifd_zstd_level = s->parameters.multifd_zstd_level;
73af8dd8
JQ
870 params->has_xbzrle_cache_size = true;
871 params->xbzrle_cache_size = s->parameters.xbzrle_cache_size;
7e555c6c
DDAG
872 params->has_max_postcopy_bandwidth = true;
873 params->max_postcopy_bandwidth = s->parameters.max_postcopy_bandwidth;
4cbc9c7f
LQ
874 params->has_max_cpu_throttle = true;
875 params->max_cpu_throttle = s->parameters.max_cpu_throttle;
ee3d96ba
DDAG
876 params->has_announce_initial = true;
877 params->announce_initial = s->parameters.announce_initial;
878 params->has_announce_max = true;
879 params->announce_max = s->parameters.announce_max;
880 params->has_announce_rounds = true;
881 params->announce_rounds = s->parameters.announce_rounds;
882 params->has_announce_step = true;
883 params->announce_step = s->parameters.announce_step;
85de8323 884
31e4c354
HR
885 if (s->parameters.has_block_bitmap_mapping) {
886 params->has_block_bitmap_mapping = true;
887 params->block_bitmap_mapping =
888 QAPI_CLONE(BitmapMigrationNodeAliasList,
889 s->parameters.block_bitmap_mapping);
890 }
891
85de8323
LL
892 return params;
893}
894
ee3d96ba
DDAG
895AnnounceParameters *migrate_announce_params(void)
896{
897 static AnnounceParameters ap;
898
899 MigrationState *s = migrate_get_current();
900
901 ap.initial = s->parameters.announce_initial;
902 ap.max = s->parameters.announce_max;
903 ap.rounds = s->parameters.announce_rounds;
904 ap.step = s->parameters.announce_step;
905
906 return &ap;
907}
908
f6844b99
DDAG
909/*
910 * Return true if we're already in the middle of a migration
911 * (i.e. any of the active or setup states)
912 */
3d63da16 913bool migration_is_setup_or_active(int state)
f6844b99
DDAG
914{
915 switch (state) {
916 case MIGRATION_STATUS_ACTIVE:
9ec055ae 917 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
a688d2c1 918 case MIGRATION_STATUS_POSTCOPY_PAUSED:
135b87b4 919 case MIGRATION_STATUS_POSTCOPY_RECOVER:
f6844b99 920 case MIGRATION_STATUS_SETUP:
31e06077
DDAG
921 case MIGRATION_STATUS_PRE_SWITCHOVER:
922 case MIGRATION_STATUS_DEVICE:
c7e0acd5 923 case MIGRATION_STATUS_WAIT_UNPLUG:
19dd408a 924 case MIGRATION_STATUS_COLO:
f6844b99
DDAG
925 return true;
926
927 default:
928 return false;
929
930 }
931}
932
392d87e2
JQ
933bool migration_is_running(int state)
934{
935 switch (state) {
936 case MIGRATION_STATUS_ACTIVE:
937 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
938 case MIGRATION_STATUS_POSTCOPY_PAUSED:
939 case MIGRATION_STATUS_POSTCOPY_RECOVER:
940 case MIGRATION_STATUS_SETUP:
941 case MIGRATION_STATUS_PRE_SWITCHOVER:
942 case MIGRATION_STATUS_DEVICE:
943 case MIGRATION_STATUS_WAIT_UNPLUG:
944 case MIGRATION_STATUS_CANCELLING:
392d87e2
JQ
945 return true;
946
947 default:
948 return false;
949
950 }
951}
952
640dfb14
WY
953static void populate_time_info(MigrationInfo *info, MigrationState *s)
954{
955 info->has_status = true;
956 info->has_setup_time = true;
957 info->setup_time = s->setup_time;
958 if (s->state == MIGRATION_STATUS_COMPLETED) {
959 info->has_total_time = true;
960 info->total_time = s->total_time;
961 info->has_downtime = true;
962 info->downtime = s->downtime;
963 } else {
964 info->has_total_time = true;
965 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
966 s->start_time;
967 info->has_expected_downtime = true;
968 info->expected_downtime = s->expected_downtime;
969 }
970}
971
a22463a5
DDAG
972static void populate_ram_info(MigrationInfo *info, MigrationState *s)
973{
974 info->has_ram = true;
975 info->ram = g_malloc0(sizeof(*info->ram));
9360447d 976 info->ram->transferred = ram_counters.transferred;
a22463a5 977 info->ram->total = ram_bytes_total();
9360447d 978 info->ram->duplicate = ram_counters.duplicate;
bedf53c1
JQ
979 /* legacy value. It is not used anymore */
980 info->ram->skipped = 0;
9360447d
JQ
981 info->ram->normal = ram_counters.normal;
982 info->ram->normal_bytes = ram_counters.normal *
20afaed9 983 qemu_target_page_size();
a22463a5 984 info->ram->mbps = s->mbps;
9360447d
JQ
985 info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
986 info->ram->postcopy_requests = ram_counters.postcopy_requests;
030ce1f8 987 info->ram->page_size = qemu_target_page_size();
a61c45bd 988 info->ram->multifd_bytes = ram_counters.multifd_bytes;
aecbfe9c 989 info->ram->pages_per_second = s->pages_per_second;
a22463a5 990
114f5aee
JQ
991 if (migrate_use_xbzrle()) {
992 info->has_xbzrle_cache = true;
993 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
994 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
9360447d
JQ
995 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
996 info->xbzrle_cache->pages = xbzrle_counters.pages;
997 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
998 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
e460a4b1 999 info->xbzrle_cache->encoding_rate = xbzrle_counters.encoding_rate;
9360447d 1000 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
114f5aee
JQ
1001 }
1002
76e03000
XG
1003 if (migrate_use_compression()) {
1004 info->has_compression = true;
1005 info->compression = g_malloc0(sizeof(*info->compression));
1006 info->compression->pages = compression_counters.pages;
1007 info->compression->busy = compression_counters.busy;
1008 info->compression->busy_rate = compression_counters.busy_rate;
1009 info->compression->compressed_size =
1010 compression_counters.compressed_size;
1011 info->compression->compression_rate =
1012 compression_counters.compression_rate;
1013 }
1014
338182c8
JQ
1015 if (cpu_throttle_active()) {
1016 info->has_cpu_throttle_percentage = true;
1017 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
1018 }
1019
a22463a5
DDAG
1020 if (s->state != MIGRATION_STATUS_COMPLETED) {
1021 info->ram->remaining = ram_bytes_remaining();
9360447d 1022 info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
a22463a5
DDAG
1023 }
1024}
1025
930ac04c
JQ
1026static void populate_disk_info(MigrationInfo *info)
1027{
1028 if (blk_mig_active()) {
1029 info->has_disk = true;
1030 info->disk = g_malloc0(sizeof(*info->disk));
1031 info->disk->transferred = blk_mig_bytes_transferred();
1032 info->disk->remaining = blk_mig_bytes_remaining();
1033 info->disk->total = blk_mig_bytes_total();
1034 }
1035}
1036
3710586c
KW
1037static void populate_vfio_info(MigrationInfo *info)
1038{
1039#ifdef CONFIG_VFIO
1040 if (vfio_mig_active()) {
1041 info->has_vfio = true;
1042 info->vfio = g_malloc0(sizeof(*info->vfio));
1043 info->vfio->transferred = vfio_mig_bytes_transferred();
1044 }
1045#endif
1046}
1047
65ace060 1048static void fill_source_migration_info(MigrationInfo *info)
5bb7910a 1049{
17549e84
JQ
1050 MigrationState *s = migrate_get_current();
1051
1052 switch (s->state) {
31194731 1053 case MIGRATION_STATUS_NONE:
17549e84 1054 /* no migration has happened ever */
65ace060
AP
1055 /* do not overwrite destination migration status */
1056 return;
31194731 1057 case MIGRATION_STATUS_SETUP:
29ae8a41 1058 info->has_status = true;
ed4fbd10 1059 info->has_total_time = false;
29ae8a41 1060 break;
31194731
HZ
1061 case MIGRATION_STATUS_ACTIVE:
1062 case MIGRATION_STATUS_CANCELLING:
9ec055ae 1063 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
31e06077
DDAG
1064 case MIGRATION_STATUS_PRE_SWITCHOVER:
1065 case MIGRATION_STATUS_DEVICE:
a688d2c1 1066 case MIGRATION_STATUS_POSTCOPY_PAUSED:
135b87b4 1067 case MIGRATION_STATUS_POSTCOPY_RECOVER:
640dfb14
WY
1068 /* TODO add some postcopy stats */
1069 populate_time_info(info, s);
a22463a5 1070 populate_ram_info(info, s);
930ac04c 1071 populate_disk_info(info);
3710586c 1072 populate_vfio_info(info);
17549e84 1073 break;
0b827d5e
HZ
1074 case MIGRATION_STATUS_COLO:
1075 info->has_status = true;
1076 /* TODO: display COLO specific information (checkpoint info etc.) */
1077 break;
31194731 1078 case MIGRATION_STATUS_COMPLETED:
640dfb14 1079 populate_time_info(info, s);
a22463a5 1080 populate_ram_info(info, s);
3710586c 1081 populate_vfio_info(info);
17549e84 1082 break;
31194731 1083 case MIGRATION_STATUS_FAILED:
791e7c82 1084 info->has_status = true;
d59ce6f3
DB
1085 if (s->error) {
1086 info->has_error_desc = true;
1087 info->error_desc = g_strdup(error_get_pretty(s->error));
1088 }
17549e84 1089 break;
31194731 1090 case MIGRATION_STATUS_CANCELLED:
791e7c82 1091 info->has_status = true;
17549e84 1092 break;
c7e0acd5
JF
1093 case MIGRATION_STATUS_WAIT_UNPLUG:
1094 info->has_status = true;
1095 break;
5bb7910a 1096 }
cde63fbe 1097 info->status = s->state;
5bb7910a
AL
1098}
1099
4a84214e
PX
1100/**
1101 * @migration_caps_check - check capability validity
1102 *
1103 * @cap_list: old capability list, array of bool
1104 * @params: new capabilities to be applied soon
1105 * @errp: set *errp if the check failed, with reason
1106 *
1107 * Returns true if check passed, otherwise false.
1108 */
1109static bool migrate_caps_check(bool *cap_list,
1110 MigrationCapabilityStatusList *params,
1111 Error **errp)
00458433 1112{
00458433 1113 MigrationCapabilityStatusList *cap;
4a84214e 1114 bool old_postcopy_cap;
d7651f15 1115 MigrationIncomingState *mis = migration_incoming_get_current();
00458433 1116
4a84214e 1117 old_postcopy_cap = cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM];
00458433
OW
1118
1119 for (cap = params; cap; cap = cap->next) {
4a84214e
PX
1120 cap_list[cap->value->capability] = cap->value->state;
1121 }
1122
ed1701c6 1123#ifndef CONFIG_LIVE_BLOCK_MIGRATION
4a84214e
PX
1124 if (cap_list[MIGRATION_CAPABILITY_BLOCK]) {
1125 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
1126 "block migration");
1127 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
1128 return false;
00458433 1129 }
4a84214e 1130#endif
53dd370c 1131
7e934f5b
ZC
1132#ifndef CONFIG_REPLICATION
1133 if (cap_list[MIGRATION_CAPABILITY_X_COLO]) {
1134 error_setg(errp, "QEMU compiled without replication module"
1135 " can't enable COLO");
1136 error_append_hint(errp, "Please enable replication before COLO.\n");
1137 return false;
1138 }
1139#endif
1140
4a84214e 1141 if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
096631bd
DDAG
1142 /* This check is reasonably expensive, so only when it's being
1143 * set the first time, also it's only the destination that needs
1144 * special support.
1145 */
1146 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
d7651f15 1147 !postcopy_ram_supported_by_host(mis)) {
096631bd
DDAG
1148 /* postcopy_ram_supported_by_host will have emitted a more
1149 * detailed message
1150 */
4a84214e
PX
1151 error_setg(errp, "Postcopy is not supported");
1152 return false;
096631bd 1153 }
18269069
YK
1154
1155 if (cap_list[MIGRATION_CAPABILITY_X_IGNORE_SHARED]) {
1156 error_setg(errp, "Postcopy is not compatible with ignore-shared");
1157 return false;
1158 }
53dd370c 1159 }
4a84214e
PX
1160
1161 return true;
1162}
1163
65ace060
AP
1164static void fill_destination_migration_info(MigrationInfo *info)
1165{
1166 MigrationIncomingState *mis = migration_incoming_get_current();
1167
9aca82ba
JQ
1168 if (mis->socket_address_list) {
1169 info->has_socket_address = true;
1170 info->socket_address =
1171 QAPI_CLONE(SocketAddressList, mis->socket_address_list);
1172 }
1173
65ace060
AP
1174 switch (mis->state) {
1175 case MIGRATION_STATUS_NONE:
1176 return;
65ace060
AP
1177 case MIGRATION_STATUS_SETUP:
1178 case MIGRATION_STATUS_CANCELLING:
1179 case MIGRATION_STATUS_CANCELLED:
1180 case MIGRATION_STATUS_ACTIVE:
1181 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
3c9928d9
PX
1182 case MIGRATION_STATUS_POSTCOPY_PAUSED:
1183 case MIGRATION_STATUS_POSTCOPY_RECOVER:
65ace060
AP
1184 case MIGRATION_STATUS_FAILED:
1185 case MIGRATION_STATUS_COLO:
1186 info->has_status = true;
1187 break;
1188 case MIGRATION_STATUS_COMPLETED:
1189 info->has_status = true;
1190 fill_destination_postcopy_migration_info(info);
1191 break;
1192 }
1193 info->status = mis->state;
1194}
1195
1196MigrationInfo *qmp_query_migrate(Error **errp)
1197{
1198 MigrationInfo *info = g_malloc0(sizeof(*info));
1199
1200 fill_destination_migration_info(info);
1201 fill_source_migration_info(info);
1202
1203 return info;
1204}
1205
4a84214e
PX
1206void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
1207 Error **errp)
1208{
1209 MigrationState *s = migrate_get_current();
1210 MigrationCapabilityStatusList *cap;
dd0ee30c 1211 bool cap_list[MIGRATION_CAPABILITY__MAX];
4a84214e 1212
392d87e2 1213 if (migration_is_running(s->state)) {
4a84214e
PX
1214 error_setg(errp, QERR_MIGRATION_ACTIVE);
1215 return;
1216 }
1217
dd0ee30c
PX
1218 memcpy(cap_list, s->enabled_capabilities, sizeof(cap_list));
1219 if (!migrate_caps_check(cap_list, params, errp)) {
4a84214e
PX
1220 return;
1221 }
1222
1223 for (cap = params; cap; cap = cap->next) {
1224 s->enabled_capabilities[cap->value->capability] = cap->value->state;
1225 }
00458433
OW
1226}
1227
16d063bc
PX
1228/*
1229 * Check whether the parameters are valid. Error will be put into errp
1230 * (if provided). Return true if valid, otherwise false.
1231 */
1232static bool migrate_params_check(MigrationParameters *params, Error **errp)
85de8323 1233{
7f375e04 1234 if (params->has_compress_level &&
741d4086 1235 (params->compress_level > 9)) {
c6bd8c70
MA
1236 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
1237 "is invalid, it should be in the range of 0 to 9");
16d063bc 1238 return false;
85de8323 1239 }
16d063bc 1240
741d4086 1241 if (params->has_compress_threads && (params->compress_threads < 1)) {
c6bd8c70
MA
1242 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1243 "compress_threads",
1244 "is invalid, it should be in the range of 1 to 255");
16d063bc 1245 return false;
85de8323 1246 }
16d063bc 1247
741d4086 1248 if (params->has_decompress_threads && (params->decompress_threads < 1)) {
c6bd8c70
MA
1249 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1250 "decompress_threads",
1251 "is invalid, it should be in the range of 1 to 255");
16d063bc 1252 return false;
85de8323 1253 }
16d063bc 1254
dc14a470
KZ
1255 if (params->has_throttle_trigger_threshold &&
1256 (params->throttle_trigger_threshold < 1 ||
1257 params->throttle_trigger_threshold > 100)) {
1258 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1259 "throttle_trigger_threshold",
1260 "an integer in the range of 1 to 100");
1261 return false;
1262 }
1263
7f375e04
EB
1264 if (params->has_cpu_throttle_initial &&
1265 (params->cpu_throttle_initial < 1 ||
1266 params->cpu_throttle_initial > 99)) {
1626fee3 1267 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
d85a31d1 1268 "cpu_throttle_initial",
1626fee3 1269 "an integer in the range of 1 to 99");
16d063bc 1270 return false;
1626fee3 1271 }
16d063bc 1272
7f375e04
EB
1273 if (params->has_cpu_throttle_increment &&
1274 (params->cpu_throttle_increment < 1 ||
1275 params->cpu_throttle_increment > 99)) {
1626fee3 1276 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
d85a31d1 1277 "cpu_throttle_increment",
1626fee3 1278 "an integer in the range of 1 to 99");
16d063bc 1279 return false;
1626fee3 1280 }
16d063bc 1281
741d4086 1282 if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) {
2ee30cf0
MZ
1283 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1284 "max_bandwidth",
1285 "an integer in the range of 0 to "stringify(SIZE_MAX)
1286 " bytes/second");
16d063bc 1287 return false;
2ff30257 1288 }
16d063bc 1289
2ff30257 1290 if (params->has_downtime_limit &&
741d4086 1291 (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
2ee30cf0
MZ
1292 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1293 "downtime_limit",
1294 "an integer in the range of 0 to "
7ac5529a 1295 stringify(MAX_MIGRATE_DOWNTIME)" ms");
16d063bc 1296 return false;
2ff30257 1297 }
16d063bc 1298
741d4086
JQ
1299 /* x_checkpoint_delay is now always positive */
1300
cbfd6c95 1301 if (params->has_multifd_channels && (params->multifd_channels < 1)) {
4075fb1c
JQ
1302 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1303 "multifd_channels",
1304 "is invalid, it should be in the range of 1 to 255");
1305 return false;
1306 }
16d063bc 1307
9004db48
JQ
1308 if (params->has_multifd_zlib_level &&
1309 (params->multifd_zlib_level > 9)) {
1310 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zlib_level",
1311 "is invalid, it should be in the range of 0 to 9");
1312 return false;
1313 }
1314
6a9ad154
JQ
1315 if (params->has_multifd_zstd_level &&
1316 (params->multifd_zstd_level > 20)) {
1317 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zstd_level",
1318 "is invalid, it should be in the range of 0 to 20");
1319 return false;
1320 }
1321
73af8dd8
JQ
1322 if (params->has_xbzrle_cache_size &&
1323 (params->xbzrle_cache_size < qemu_target_page_size() ||
1324 !is_power_of_2(params->xbzrle_cache_size))) {
1325 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1326 "xbzrle_cache_size",
1327 "is invalid, it should be bigger than target page size"
06b1c6f8 1328 " and a power of 2");
73af8dd8
JQ
1329 return false;
1330 }
1331
4cbc9c7f
LQ
1332 if (params->has_max_cpu_throttle &&
1333 (params->max_cpu_throttle < params->cpu_throttle_initial ||
1334 params->max_cpu_throttle > 99)) {
1335 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1336 "max_cpu_throttle",
1337 "an integer in the range of cpu_throttle_initial to 99");
1338 return false;
1339 }
1340
ee3d96ba
DDAG
1341 if (params->has_announce_initial &&
1342 params->announce_initial > 100000) {
1343 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1344 "announce_initial",
1345 "is invalid, it must be less than 100000 ms");
1346 return false;
1347 }
1348 if (params->has_announce_max &&
1349 params->announce_max > 100000) {
1350 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1351 "announce_max",
1352 "is invalid, it must be less than 100000 ms");
1353 return false;
1354 }
1355 if (params->has_announce_rounds &&
1356 params->announce_rounds > 1000) {
1357 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1358 "announce_rounds",
1359 "is invalid, it must be in the range of 0 to 1000");
1360 return false;
1361 }
1362 if (params->has_announce_step &&
1363 (params->announce_step < 1 ||
1364 params->announce_step > 10000)) {
1365 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1366 "announce_step",
1367 "is invalid, it must be in the range of 1 to 10000 ms");
1368 return false;
1369 }
31e4c354
HR
1370
1371 if (params->has_block_bitmap_mapping &&
1372 !check_dirty_bitmap_mig_alias_map(params->block_bitmap_mapping, errp)) {
1373 error_prepend(errp, "Invalid mapping given for block-bitmap-mapping: ");
1374 return false;
1375 }
1376
16d063bc
PX
1377 return true;
1378}
1379
1bda8b3c
MA
1380static void migrate_params_test_apply(MigrateSetParameters *params,
1381 MigrationParameters *dest)
1382{
1383 *dest = migrate_get_current()->parameters;
1384
1385 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1386
1387 if (params->has_compress_level) {
1388 dest->compress_level = params->compress_level;
1389 }
1390
1391 if (params->has_compress_threads) {
1392 dest->compress_threads = params->compress_threads;
1393 }
1394
1d58872a
XG
1395 if (params->has_compress_wait_thread) {
1396 dest->compress_wait_thread = params->compress_wait_thread;
1397 }
1398
1bda8b3c
MA
1399 if (params->has_decompress_threads) {
1400 dest->decompress_threads = params->decompress_threads;
1401 }
1402
dc14a470
KZ
1403 if (params->has_throttle_trigger_threshold) {
1404 dest->throttle_trigger_threshold = params->throttle_trigger_threshold;
1405 }
1406
1bda8b3c
MA
1407 if (params->has_cpu_throttle_initial) {
1408 dest->cpu_throttle_initial = params->cpu_throttle_initial;
1409 }
1410
1411 if (params->has_cpu_throttle_increment) {
1412 dest->cpu_throttle_increment = params->cpu_throttle_increment;
1413 }
1414
cbbf8182
KZ
1415 if (params->has_cpu_throttle_tailslow) {
1416 dest->cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1417 }
1418
1bda8b3c 1419 if (params->has_tls_creds) {
01fa5598 1420 assert(params->tls_creds->type == QTYPE_QSTRING);
9728ebfb 1421 dest->tls_creds = params->tls_creds->u.s;
1bda8b3c
MA
1422 }
1423
1424 if (params->has_tls_hostname) {
01fa5598 1425 assert(params->tls_hostname->type == QTYPE_QSTRING);
9728ebfb 1426 dest->tls_hostname = params->tls_hostname->u.s;
1bda8b3c
MA
1427 }
1428
1429 if (params->has_max_bandwidth) {
1430 dest->max_bandwidth = params->max_bandwidth;
1431 }
1432
1433 if (params->has_downtime_limit) {
1434 dest->downtime_limit = params->downtime_limit;
1435 }
1436
1437 if (params->has_x_checkpoint_delay) {
1438 dest->x_checkpoint_delay = params->x_checkpoint_delay;
1439 }
1440
1441 if (params->has_block_incremental) {
1442 dest->block_incremental = params->block_incremental;
1443 }
cbfd6c95
JQ
1444 if (params->has_multifd_channels) {
1445 dest->multifd_channels = params->multifd_channels;
5e7577a1 1446 }
96eef042
JQ
1447 if (params->has_multifd_compression) {
1448 dest->multifd_compression = params->multifd_compression;
1449 }
73af8dd8
JQ
1450 if (params->has_xbzrle_cache_size) {
1451 dest->xbzrle_cache_size = params->xbzrle_cache_size;
1452 }
7e555c6c
DDAG
1453 if (params->has_max_postcopy_bandwidth) {
1454 dest->max_postcopy_bandwidth = params->max_postcopy_bandwidth;
1455 }
4cbc9c7f
LQ
1456 if (params->has_max_cpu_throttle) {
1457 dest->max_cpu_throttle = params->max_cpu_throttle;
1458 }
ee3d96ba
DDAG
1459 if (params->has_announce_initial) {
1460 dest->announce_initial = params->announce_initial;
1461 }
1462 if (params->has_announce_max) {
1463 dest->announce_max = params->announce_max;
1464 }
1465 if (params->has_announce_rounds) {
1466 dest->announce_rounds = params->announce_rounds;
1467 }
1468 if (params->has_announce_step) {
1469 dest->announce_step = params->announce_step;
1470 }
31e4c354
HR
1471
1472 if (params->has_block_bitmap_mapping) {
1473 dest->has_block_bitmap_mapping = true;
1474 dest->block_bitmap_mapping = params->block_bitmap_mapping;
1475 }
1bda8b3c
MA
1476}
1477
73af8dd8 1478static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
16d063bc
PX
1479{
1480 MigrationState *s = migrate_get_current();
1481
e87fae4c
MA
1482 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1483
7f375e04
EB
1484 if (params->has_compress_level) {
1485 s->parameters.compress_level = params->compress_level;
85de8323 1486 }
476c72aa 1487
7f375e04
EB
1488 if (params->has_compress_threads) {
1489 s->parameters.compress_threads = params->compress_threads;
85de8323 1490 }
476c72aa 1491
1d58872a
XG
1492 if (params->has_compress_wait_thread) {
1493 s->parameters.compress_wait_thread = params->compress_wait_thread;
1494 }
1495
7f375e04
EB
1496 if (params->has_decompress_threads) {
1497 s->parameters.decompress_threads = params->decompress_threads;
85de8323 1498 }
476c72aa 1499
dc14a470
KZ
1500 if (params->has_throttle_trigger_threshold) {
1501 s->parameters.throttle_trigger_threshold = params->throttle_trigger_threshold;
1502 }
1503
7f375e04
EB
1504 if (params->has_cpu_throttle_initial) {
1505 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
1626fee3 1506 }
476c72aa 1507
7f375e04
EB
1508 if (params->has_cpu_throttle_increment) {
1509 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
1626fee3 1510 }
476c72aa 1511
cbbf8182
KZ
1512 if (params->has_cpu_throttle_tailslow) {
1513 s->parameters.cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1514 }
1515
7f375e04 1516 if (params->has_tls_creds) {
69ef1f36 1517 g_free(s->parameters.tls_creds);
01fa5598
MA
1518 assert(params->tls_creds->type == QTYPE_QSTRING);
1519 s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
69ef1f36 1520 }
476c72aa 1521
7f375e04 1522 if (params->has_tls_hostname) {
69ef1f36 1523 g_free(s->parameters.tls_hostname);
01fa5598
MA
1524 assert(params->tls_hostname->type == QTYPE_QSTRING);
1525 s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
69ef1f36 1526 }
476c72aa 1527
d2f1d29b
DB
1528 if (params->has_tls_authz) {
1529 g_free(s->parameters.tls_authz);
1530 assert(params->tls_authz->type == QTYPE_QSTRING);
1531 s->parameters.tls_authz = g_strdup(params->tls_authz->u.s);
1532 }
1533
2ff30257
AA
1534 if (params->has_max_bandwidth) {
1535 s->parameters.max_bandwidth = params->max_bandwidth;
c38c1c14 1536 if (s->to_dst_file && !migration_in_postcopy()) {
2ff30257
AA
1537 qemu_file_set_rate_limit(s->to_dst_file,
1538 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
1539 }
1540 }
476c72aa 1541
2ff30257
AA
1542 if (params->has_downtime_limit) {
1543 s->parameters.downtime_limit = params->downtime_limit;
1544 }
68b53591
HZ
1545
1546 if (params->has_x_checkpoint_delay) {
1547 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
479125d5
HZ
1548 if (migration_in_colo_state()) {
1549 colo_checkpoint_notify(s);
1550 }
68b53591 1551 }
476c72aa 1552
2833c59b
JQ
1553 if (params->has_block_incremental) {
1554 s->parameters.block_incremental = params->block_incremental;
1555 }
cbfd6c95
JQ
1556 if (params->has_multifd_channels) {
1557 s->parameters.multifd_channels = params->multifd_channels;
4075fb1c 1558 }
96eef042
JQ
1559 if (params->has_multifd_compression) {
1560 s->parameters.multifd_compression = params->multifd_compression;
1561 }
73af8dd8
JQ
1562 if (params->has_xbzrle_cache_size) {
1563 s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
1564 xbzrle_cache_resize(params->xbzrle_cache_size, errp);
1565 }
7e555c6c
DDAG
1566 if (params->has_max_postcopy_bandwidth) {
1567 s->parameters.max_postcopy_bandwidth = params->max_postcopy_bandwidth;
c38c1c14
DDAG
1568 if (s->to_dst_file && migration_in_postcopy()) {
1569 qemu_file_set_rate_limit(s->to_dst_file,
1570 s->parameters.max_postcopy_bandwidth / XFER_LIMIT_RATIO);
1571 }
7e555c6c 1572 }
4cbc9c7f
LQ
1573 if (params->has_max_cpu_throttle) {
1574 s->parameters.max_cpu_throttle = params->max_cpu_throttle;
1575 }
ee3d96ba
DDAG
1576 if (params->has_announce_initial) {
1577 s->parameters.announce_initial = params->announce_initial;
1578 }
1579 if (params->has_announce_max) {
1580 s->parameters.announce_max = params->announce_max;
1581 }
1582 if (params->has_announce_rounds) {
1583 s->parameters.announce_rounds = params->announce_rounds;
1584 }
1585 if (params->has_announce_step) {
1586 s->parameters.announce_step = params->announce_step;
1587 }
31e4c354
HR
1588
1589 if (params->has_block_bitmap_mapping) {
1590 qapi_free_BitmapMigrationNodeAliasList(
1591 s->parameters.block_bitmap_mapping);
1592
1593 s->parameters.has_block_bitmap_mapping = true;
1594 s->parameters.block_bitmap_mapping =
1595 QAPI_CLONE(BitmapMigrationNodeAliasList,
1596 params->block_bitmap_mapping);
1597 }
85de8323
LL
1598}
1599
1bda8b3c 1600void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
476c72aa 1601{
1bda8b3c
MA
1602 MigrationParameters tmp;
1603
01fa5598
MA
1604 /* TODO Rewrite "" to null instead */
1605 if (params->has_tls_creds
1606 && params->tls_creds->type == QTYPE_QNULL) {
cb3e7f08 1607 qobject_unref(params->tls_creds->u.n);
01fa5598
MA
1608 params->tls_creds->type = QTYPE_QSTRING;
1609 params->tls_creds->u.s = strdup("");
1610 }
1611 /* TODO Rewrite "" to null instead */
1612 if (params->has_tls_hostname
1613 && params->tls_hostname->type == QTYPE_QNULL) {
cb3e7f08 1614 qobject_unref(params->tls_hostname->u.n);
01fa5598
MA
1615 params->tls_hostname->type = QTYPE_QSTRING;
1616 params->tls_hostname->u.s = strdup("");
1617 }
1618
1bda8b3c
MA
1619 migrate_params_test_apply(params, &tmp);
1620
1621 if (!migrate_params_check(&tmp, errp)) {
476c72aa
PX
1622 /* Invalid parameter */
1623 return;
1624 }
1625
73af8dd8 1626 migrate_params_apply(params, errp);
476c72aa
PX
1627}
1628
2594f56d 1629
4886a1bc
DDAG
1630void qmp_migrate_start_postcopy(Error **errp)
1631{
1632 MigrationState *s = migrate_get_current();
1633
16b0fd32 1634 if (!migrate_postcopy()) {
a54d340b 1635 error_setg(errp, "Enable postcopy with migrate_set_capability before"
4886a1bc
DDAG
1636 " the start of migration");
1637 return;
1638 }
1639
1640 if (s->state == MIGRATION_STATUS_NONE) {
1641 error_setg(errp, "Postcopy must be started after migration has been"
1642 " started");
1643 return;
1644 }
1645 /*
1646 * we don't error if migration has finished since that would be racy
1647 * with issuing this command.
1648 */
d73415a3 1649 qatomic_set(&s->start_postcopy, true);
4886a1bc
DDAG
1650}
1651
065e2813
AL
1652/* shared migration helpers */
1653
48781e5b 1654void migrate_set_state(int *state, int old_state, int new_state)
51cf4c1a 1655{
a31fedee 1656 assert(new_state < MIGRATION_STATUS__MAX);
d73415a3 1657 if (qatomic_cmpxchg(state, old_state, new_state) == old_state) {
a31fedee 1658 trace_migrate_set_state(MigrationStatus_str(new_state));
b05dc723 1659 migrate_generate_event(new_state);
51cf4c1a
Z
1660 }
1661}
1662
eaedde52
EB
1663static MigrationCapabilityStatus *migrate_cap_add(MigrationCapability index,
1664 bool state)
2833c59b 1665{
eaedde52 1666 MigrationCapabilityStatus *cap;
2833c59b 1667
eaedde52
EB
1668 cap = g_new0(MigrationCapabilityStatus, 1);
1669 cap->capability = index;
1670 cap->state = state;
4e4a3d3a
PX
1671
1672 return cap;
1673}
1674
1675void migrate_set_block_enabled(bool value, Error **errp)
1676{
eaedde52 1677 MigrationCapabilityStatusList *cap = NULL;
4e4a3d3a 1678
eaedde52 1679 QAPI_LIST_PREPEND(cap, migrate_cap_add(MIGRATION_CAPABILITY_BLOCK, value));
2833c59b
JQ
1680 qmp_migrate_set_capabilities(cap, errp);
1681 qapi_free_MigrationCapabilityStatusList(cap);
1682}
1683
1684static void migrate_set_block_incremental(MigrationState *s, bool value)
1685{
1686 s->parameters.block_incremental = value;
1687}
1688
1689static void block_cleanup_parameters(MigrationState *s)
1690{
1691 if (s->must_remove_block_options) {
1692 /* setting to false can never fail */
1693 migrate_set_block_enabled(false, &error_abort);
1694 migrate_set_block_incremental(s, false);
1695 s->must_remove_block_options = false;
1696 }
1697}
1698
fd392cfa 1699static void migrate_fd_cleanup(MigrationState *s)
065e2813 1700{
bb1fadc4
PB
1701 qemu_bh_delete(s->cleanup_bh);
1702 s->cleanup_bh = NULL;
1703
0ceccd85
PX
1704 qemu_savevm_state_cleanup();
1705
89a02a9f 1706 if (s->to_dst_file) {
62df066f 1707 QEMUFile *tmp;
f986c3d2 1708
9013dca5 1709 trace_migrate_fd_cleanup();
404a7c05 1710 qemu_mutex_unlock_iothread();
1d34e4bf
DDAG
1711 if (s->migration_thread_running) {
1712 qemu_thread_join(&s->thread);
1713 s->migration_thread_running = false;
1714 }
404a7c05
PB
1715 qemu_mutex_lock_iothread();
1716
1398b2e3 1717 multifd_save_cleanup();
62df066f
PX
1718 qemu_mutex_lock(&s->qemu_file_lock);
1719 tmp = s->to_dst_file;
89a02a9f 1720 s->to_dst_file = NULL;
62df066f
PX
1721 qemu_mutex_unlock(&s->qemu_file_lock);
1722 /*
1723 * Close the file handle without the lock to make sure the
1724 * critical section won't block for long.
1725 */
1726 qemu_fclose(tmp);
065e2813
AL
1727 }
1728
8f8d528e 1729 assert(!migration_is_active(s));
7a2c1721 1730
94f5a437 1731 if (s->state == MIGRATION_STATUS_CANCELLING) {
48781e5b 1732 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
94f5a437 1733 MIGRATION_STATUS_CANCELLED);
7a2c1721 1734 }
a3fa1d78 1735
87db1a7d
JQ
1736 if (s->error) {
1737 /* It is used on info migrate. We can't free it */
1738 error_report_err(error_copy(s->error));
1739 }
a3fa1d78 1740 notifier_list_notify(&migration_state_notifiers, s);
2833c59b 1741 block_cleanup_parameters(s);
b5eea99e 1742 yank_unregister_instance(MIGRATION_YANK_INSTANCE);
065e2813
AL
1743}
1744
fd392cfa
YK
1745static void migrate_fd_cleanup_schedule(MigrationState *s)
1746{
1747 /*
1748 * Ref the state for bh, because it may be called when
1749 * there're already no other refs
1750 */
1751 object_ref(OBJECT(s));
1752 qemu_bh_schedule(s->cleanup_bh);
1753}
1754
1755static void migrate_fd_cleanup_bh(void *opaque)
1756{
1757 MigrationState *s = opaque;
1758 migrate_fd_cleanup(s);
1759 object_unref(OBJECT(s));
1760}
1761
87db1a7d
JQ
1762void migrate_set_error(MigrationState *s, const Error *error)
1763{
6e8a355d 1764 QEMU_LOCK_GUARD(&s->error_mutex);
87db1a7d
JQ
1765 if (!s->error) {
1766 s->error = error_copy(error);
1767 }
87db1a7d
JQ
1768}
1769
d59ce6f3 1770void migrate_fd_error(MigrationState *s, const Error *error)
065e2813 1771{
25174055 1772 trace_migrate_fd_error(error_get_pretty(error));
89a02a9f 1773 assert(s->to_dst_file == NULL);
48781e5b
HZ
1774 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1775 MIGRATION_STATUS_FAILED);
87db1a7d 1776 migrate_set_error(s, error);
458cf28e
JQ
1777}
1778
0edda1c4 1779static void migrate_fd_cancel(MigrationState *s)
065e2813 1780{
6f2b811a 1781 int old_state ;
89a02a9f 1782 QEMUFile *f = migrate_get_current()->to_dst_file;
9013dca5 1783 trace_migrate_fd_cancel();
065e2813 1784
70b20477
DDAG
1785 if (s->rp_state.from_dst_file) {
1786 /* shutdown the rp socket, so causing the rp thread to shutdown */
1787 qemu_file_shutdown(s->rp_state.from_dst_file);
1788 }
1789
6f2b811a
Z
1790 do {
1791 old_state = s->state;
392d87e2 1792 if (!migration_is_running(old_state)) {
6f2b811a
Z
1793 break;
1794 }
a7b36b48
DDAG
1795 /* If the migration is paused, kick it out of the pause */
1796 if (old_state == MIGRATION_STATUS_PRE_SWITCHOVER) {
1797 qemu_sem_post(&s->pause_sem);
1798 }
48781e5b 1799 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
31194731 1800 } while (s->state != MIGRATION_STATUS_CANCELLING);
a26ba26e
DDAG
1801
1802 /*
1803 * If we're unlucky the migration code might be stuck somewhere in a
1804 * send/write while the network has failed and is waiting to timeout;
1805 * if we've got shutdown(2) available then we can force it to quit.
1806 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1807 * called in a bh, so there is no race against this cancel.
1808 */
31194731 1809 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
a26ba26e
DDAG
1810 qemu_file_shutdown(f);
1811 }
1d2acc31
HZ
1812 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
1813 Error *local_err = NULL;
1814
1815 bdrv_invalidate_cache_all(&local_err);
1816 if (local_err) {
1817 error_report_err(local_err);
1818 } else {
1819 s->block_inactive = false;
1820 }
1821 }
065e2813
AL
1822}
1823
99a0db9b
GH
1824void add_migration_state_change_notifier(Notifier *notify)
1825{
1826 notifier_list_add(&migration_state_notifiers, notify);
1827}
1828
1829void remove_migration_state_change_notifier(Notifier *notify)
1830{
31552529 1831 notifier_remove(notify);
99a0db9b
GH
1832}
1833
02edd2e7 1834bool migration_in_setup(MigrationState *s)
afe2df69 1835{
31194731 1836 return s->state == MIGRATION_STATUS_SETUP;
afe2df69
GH
1837}
1838
7073693b 1839bool migration_has_finished(MigrationState *s)
99a0db9b 1840{
31194731 1841 return s->state == MIGRATION_STATUS_COMPLETED;
99a0db9b 1842}
0edda1c4 1843
afe2df69
GH
1844bool migration_has_failed(MigrationState *s)
1845{
31194731
HZ
1846 return (s->state == MIGRATION_STATUS_CANCELLED ||
1847 s->state == MIGRATION_STATUS_FAILED);
afe2df69
GH
1848}
1849
5727309d 1850bool migration_in_postcopy(void)
9ec055ae 1851{
5727309d
JQ
1852 MigrationState *s = migrate_get_current();
1853
3748fef9
DDAG
1854 switch (s->state) {
1855 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1856 case MIGRATION_STATUS_POSTCOPY_PAUSED:
1857 case MIGRATION_STATUS_POSTCOPY_RECOVER:
1858 return true;
1859 default:
1860 return false;
1861 }
9ec055ae
DDAG
1862}
1863
b82fc321
DDAG
1864bool migration_in_postcopy_after_devices(MigrationState *s)
1865{
5727309d 1866 return migration_in_postcopy() && s->postcopy_after_devices;
b82fc321
DDAG
1867}
1868
06df2e69
DH
1869bool migration_in_incoming_postcopy(void)
1870{
1871 PostcopyState ps = postcopy_state_get();
1872
1873 return ps >= POSTCOPY_INCOMING_DISCARD && ps < POSTCOPY_INCOMING_END;
1874}
1875
fab35005 1876bool migration_is_idle(void)
fe44dc91 1877{
daff7f0b
MA
1878 MigrationState *s = current_migration;
1879
1880 if (!s) {
1881 return true;
1882 }
fe44dc91
AA
1883
1884 switch (s->state) {
1885 case MIGRATION_STATUS_NONE:
1886 case MIGRATION_STATUS_CANCELLED:
1887 case MIGRATION_STATUS_COMPLETED:
1888 case MIGRATION_STATUS_FAILED:
1889 return true;
1890 case MIGRATION_STATUS_SETUP:
1891 case MIGRATION_STATUS_CANCELLING:
1892 case MIGRATION_STATUS_ACTIVE:
1893 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1894 case MIGRATION_STATUS_COLO:
31e06077
DDAG
1895 case MIGRATION_STATUS_PRE_SWITCHOVER:
1896 case MIGRATION_STATUS_DEVICE:
c7e0acd5 1897 case MIGRATION_STATUS_WAIT_UNPLUG:
fe44dc91
AA
1898 return false;
1899 case MIGRATION_STATUS__MAX:
1900 g_assert_not_reached();
1901 }
1902
1903 return false;
1904}
1905
8f8d528e
WY
1906bool migration_is_active(MigrationState *s)
1907{
1908 return (s->state == MIGRATION_STATUS_ACTIVE ||
1909 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1910}
1911
3e0c8050 1912void migrate_init(MigrationState *s)
0edda1c4 1913{
389775d1
DDAG
1914 /*
1915 * Reinitialise all migration state, except
1916 * parameters/capabilities that the user set, and
1917 * locks.
1918 */
389775d1 1919 s->cleanup_bh = 0;
89a02a9f 1920 s->to_dst_file = NULL;
389775d1 1921 s->state = MIGRATION_STATUS_NONE;
389775d1
DDAG
1922 s->rp_state.from_dst_file = NULL;
1923 s->rp_state.error = false;
1924 s->mbps = 0.0;
aecbfe9c 1925 s->pages_per_second = 0.0;
389775d1
DDAG
1926 s->downtime = 0;
1927 s->expected_downtime = 0;
389775d1 1928 s->setup_time = 0;
389775d1 1929 s->start_postcopy = false;
b82fc321 1930 s->postcopy_after_devices = false;
389775d1 1931 s->migration_thread_running = false;
d59ce6f3
DB
1932 error_free(s->error);
1933 s->error = NULL;
d8053e73 1934 s->hostname = NULL;
389775d1 1935
48781e5b 1936 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
0edda1c4 1937
4af246a3
PX
1938 s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1939 s->total_time = 0;
7287cbd4 1940 s->vm_was_running = false;
b15df1ae
PX
1941 s->iteration_initial_bytes = 0;
1942 s->threshold_size = 0;
0edda1c4 1943}
cab30143 1944
fa2756b7
AL
1945static GSList *migration_blockers;
1946
fe44dc91 1947int migrate_add_blocker(Error *reason, Error **errp)
fa2756b7 1948{
811f8652 1949 if (only_migratable) {
4b576648
MA
1950 error_propagate_prepend(errp, error_copy(reason),
1951 "disallowing migration blocker "
b6eca81e 1952 "(--only-migratable) for: ");
b67b8c3a
AA
1953 return -EACCES;
1954 }
1955
fab35005 1956 if (migration_is_idle()) {
fe44dc91
AA
1957 migration_blockers = g_slist_prepend(migration_blockers, reason);
1958 return 0;
1959 }
1960
4b576648
MA
1961 error_propagate_prepend(errp, error_copy(reason),
1962 "disallowing migration blocker "
1963 "(migration in progress) for: ");
fe44dc91 1964 return -EBUSY;
fa2756b7
AL
1965}
1966
1967void migrate_del_blocker(Error *reason)
1968{
1969 migration_blockers = g_slist_remove(migration_blockers, reason);
1970}
1971
bf1ae1f4
DDAG
1972void qmp_migrate_incoming(const char *uri, Error **errp)
1973{
1974 Error *local_err = NULL;
4debb5f5 1975 static bool once = true;
bf1ae1f4 1976
4debb5f5
DDAG
1977 if (!once) {
1978 error_setg(errp, "The incoming migration has already been started");
603d5a42 1979 return;
4debb5f5 1980 }
e69d50d6
PB
1981 if (!runstate_check(RUN_STATE_INMIGRATE)) {
1982 error_setg(errp, "'-incoming' was not specified on the command line");
1983 return;
1984 }
bf1ae1f4
DDAG
1985
1986 qemu_start_incoming_migration(uri, &local_err);
1987
1988 if (local_err) {
1989 error_propagate(errp, local_err);
1990 return;
1991 }
1992
4debb5f5 1993 once = false;
bf1ae1f4
DDAG
1994}
1995
02affd41
PX
1996void qmp_migrate_recover(const char *uri, Error **errp)
1997{
1998 MigrationIncomingState *mis = migration_incoming_get_current();
1999
2000 if (mis->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
2001 error_setg(errp, "Migrate recover can only be run "
2002 "when postcopy is paused.");
2003 return;
2004 }
2005
d73415a3 2006 if (qatomic_cmpxchg(&mis->postcopy_recover_triggered,
02affd41
PX
2007 false, true) == true) {
2008 error_setg(errp, "Migrate recovery is triggered already");
2009 return;
2010 }
2011
2012 /*
2013 * Note that this call will never start a real migration; it will
2014 * only re-setup the migration stream and poke existing migration
2015 * to continue using that newly established channel.
2016 */
b5eea99e 2017 yank_unregister_instance(MIGRATION_YANK_INSTANCE);
02affd41
PX
2018 qemu_start_incoming_migration(uri, errp);
2019}
2020
bfbf89c2
PX
2021void qmp_migrate_pause(Error **errp)
2022{
2023 MigrationState *ms = migrate_get_current();
2024 MigrationIncomingState *mis = migration_incoming_get_current();
2025 int ret;
2026
2027 if (ms->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2028 /* Source side, during postcopy */
2029 qemu_mutex_lock(&ms->qemu_file_lock);
2030 ret = qemu_file_shutdown(ms->to_dst_file);
2031 qemu_mutex_unlock(&ms->qemu_file_lock);
2032 if (ret) {
2033 error_setg(errp, "Failed to pause source migration");
2034 }
2035 return;
2036 }
2037
2038 if (mis->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2039 ret = qemu_file_shutdown(mis->from_src_file);
2040 if (ret) {
2041 error_setg(errp, "Failed to pause destination migration");
2042 }
2043 return;
2044 }
2045
2046 error_setg(errp, "migrate-pause is currently only supported "
2047 "during postcopy-active state");
2048}
2049
24f3902b
GK
2050bool migration_is_blocked(Error **errp)
2051{
2052 if (qemu_savevm_state_blocked(errp)) {
2053 return true;
2054 }
2055
2056 if (migration_blockers) {
250561e1 2057 error_propagate(errp, error_copy(migration_blockers->data));
24f3902b
GK
2058 return true;
2059 }
2060
2061 return false;
2062}
2063
d3e35b8f
PX
2064/* Returns true if continue to migrate, or false if error detected */
2065static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc,
2066 bool resume, Error **errp)
cab30143 2067{
be7059cd 2068 Error *local_err = NULL;
d3e35b8f
PX
2069
2070 if (resume) {
2071 if (s->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
2072 error_setg(errp, "Cannot resume if there is no "
2073 "paused migration");
2074 return false;
2075 }
97ca211c
PX
2076
2077 /*
2078 * Postcopy recovery won't work well with release-ram
2079 * capability since release-ram will drop the page buffer as
2080 * long as the page is put into the send buffer. So if there
2081 * is a network failure happened, any page buffers that have
2082 * not yet reached the destination VM but have already been
2083 * sent from the source VM will be lost forever. Let's refuse
2084 * the client from resuming such a postcopy migration.
2085 * Luckily release-ram was designed to only be used when src
2086 * and destination VMs are on the same host, so it should be
2087 * fine.
2088 */
2089 if (migrate_release_ram()) {
2090 error_setg(errp, "Postcopy recovery cannot work "
2091 "when release-ram capability is set");
2092 return false;
2093 }
2094
d3e35b8f
PX
2095 /* This is a resume, skip init status */
2096 return true;
2097 }
cab30143 2098
392d87e2 2099 if (migration_is_running(s->state)) {
c6bd8c70 2100 error_setg(errp, QERR_MIGRATION_ACTIVE);
d3e35b8f 2101 return false;
cab30143 2102 }
d3e35b8f 2103
ca99993a
DDAG
2104 if (runstate_check(RUN_STATE_INMIGRATE)) {
2105 error_setg(errp, "Guest is waiting for an incoming migration");
d3e35b8f 2106 return false;
ca99993a
DDAG
2107 }
2108
36d0fe65
T
2109 if (runstate_check(RUN_STATE_POSTMIGRATE)) {
2110 error_setg(errp, "Can't migrate the vm that was paused due to "
2111 "previous migration");
2112 return false;
2113 }
2114
24f3902b 2115 if (migration_is_blocked(errp)) {
d3e35b8f 2116 return false;
fa2756b7
AL
2117 }
2118
d3e35b8f 2119 if (blk || blk_inc) {
2833c59b
JQ
2120 if (migrate_use_block() || migrate_use_block_incremental()) {
2121 error_setg(errp, "Command options are incompatible with "
2122 "current migration capabilities");
d3e35b8f 2123 return false;
2833c59b
JQ
2124 }
2125 migrate_set_block_enabled(true, &local_err);
2126 if (local_err) {
2127 error_propagate(errp, local_err);
d3e35b8f 2128 return false;
2833c59b
JQ
2129 }
2130 s->must_remove_block_options = true;
2131 }
2132
d3e35b8f 2133 if (blk_inc) {
2833c59b
JQ
2134 migrate_set_block_incremental(s, true);
2135 }
2136
3e0c8050 2137 migrate_init(s);
87f3bd87
IR
2138 /*
2139 * set ram_counters memory to zero for a
2140 * new migration
2141 */
2142 memset(&ram_counters, 0, sizeof(ram_counters));
cab30143 2143
d3e35b8f
PX
2144 return true;
2145}
2146
2147void qmp_migrate(const char *uri, bool has_blk, bool blk,
2148 bool has_inc, bool inc, bool has_detach, bool detach,
2149 bool has_resume, bool resume, Error **errp)
2150{
2151 Error *local_err = NULL;
2152 MigrationState *s = migrate_get_current();
d658f65c 2153 const char *p = NULL;
d3e35b8f
PX
2154
2155 if (!migrate_prepare(s, has_blk && blk, has_inc && inc,
2156 has_resume && resume, errp)) {
2157 /* Error detected, put into errp */
2158 return;
2159 }
2160
b5eea99e
LS
2161 if (!(has_resume && resume)) {
2162 if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) {
2163 return;
2164 }
2165 }
2166
d658f65c 2167 if (strstart(uri, "tcp:", &p) ||
9ba3b2ba
LM
2168 strstart(uri, "unix:", NULL) ||
2169 strstart(uri, "vsock:", NULL)) {
d658f65c 2170 socket_start_outgoing_migration(s, p ? p : uri, &local_err);
2da776db 2171#ifdef CONFIG_RDMA
41310c68 2172 } else if (strstart(uri, "rdma:", &p)) {
2da776db
MH
2173 rdma_start_outgoing_migration(s, p, &local_err);
2174#endif
cab30143 2175 } else if (strstart(uri, "exec:", &p)) {
f37afb5a 2176 exec_start_outgoing_migration(s, p, &local_err);
cab30143 2177 } else if (strstart(uri, "fd:", &p)) {
f37afb5a 2178 fd_start_outgoing_migration(s, p, &local_err);
99a0db9b 2179 } else {
b5eea99e
LS
2180 if (!(has_resume && resume)) {
2181 yank_unregister_instance(MIGRATION_YANK_INSTANCE);
2182 }
c6bd8c70
MA
2183 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
2184 "a valid migration protocol");
48781e5b
HZ
2185 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2186 MIGRATION_STATUS_FAILED);
09576e74 2187 block_cleanup_parameters(s);
e1c37d0e 2188 return;
cab30143
JQ
2189 }
2190
f37afb5a 2191 if (local_err) {
b5eea99e
LS
2192 if (!(has_resume && resume)) {
2193 yank_unregister_instance(MIGRATION_YANK_INSTANCE);
2194 }
d59ce6f3 2195 migrate_fd_error(s, local_err);
f37afb5a 2196 error_propagate(errp, local_err);
e1c37d0e 2197 return;
1299c631 2198 }
cab30143
JQ
2199}
2200
6cdedb07 2201void qmp_migrate_cancel(Error **errp)
cab30143 2202{
17549e84 2203 migrate_fd_cancel(migrate_get_current());
cab30143
JQ
2204}
2205
89cfc02c
DDAG
2206void qmp_migrate_continue(MigrationStatus state, Error **errp)
2207{
2208 MigrationState *s = migrate_get_current();
2209 if (s->state != state) {
2210 error_setg(errp, "Migration not in expected state: %s",
2211 MigrationStatus_str(s->state));
2212 return;
2213 }
2214 qemu_sem_post(&s->pause_sem);
2215}
2216
9e1ba4cc
OW
2217void qmp_migrate_set_cache_size(int64_t value, Error **errp)
2218{
73af8dd8
JQ
2219 MigrateSetParameters p = {
2220 .has_xbzrle_cache_size = true,
2221 .xbzrle_cache_size = value,
2222 };
c91e681a 2223
73af8dd8 2224 qmp_migrate_set_parameters(&p, errp);
9e1ba4cc
OW
2225}
2226
2227int64_t qmp_query_migrate_cache_size(Error **errp)
2228{
2229 return migrate_xbzrle_cache_size();
2230}
2231
3dc85383 2232void qmp_migrate_set_speed(int64_t value, Error **errp)
cab30143 2233{
1bda8b3c 2234 MigrateSetParameters p = {
2ff30257
AA
2235 .has_max_bandwidth = true,
2236 .max_bandwidth = value,
2237 };
cab30143 2238
2ff30257 2239 qmp_migrate_set_parameters(&p, errp);
cab30143
JQ
2240}
2241
4f0a993b 2242void qmp_migrate_set_downtime(double value, Error **errp)
cab30143 2243{
87c9cc1c 2244 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
2ee30cf0
MZ
2245 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
2246 "downtime_limit",
2247 "an integer in the range of 0 to "
2248 stringify(MAX_MIGRATE_DOWNTIME_SECONDS)" seconds");
87c9cc1c
DHB
2249 return;
2250 }
2251
2ff30257 2252 value *= 1000; /* Convert to milliseconds */
2ff30257 2253
1bda8b3c 2254 MigrateSetParameters p = {
2ff30257 2255 .has_downtime_limit = true,
2667c987 2256 .downtime_limit = (int64_t)value,
2ff30257
AA
2257 };
2258
2259 qmp_migrate_set_parameters(&p, errp);
99a0db9b 2260}
17ad9b35 2261
53f09a10
PB
2262bool migrate_release_ram(void)
2263{
2264 MigrationState *s;
2265
2266 s = migrate_get_current();
2267
2268 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
2269}
2270
53dd370c
DDAG
2271bool migrate_postcopy_ram(void)
2272{
2273 MigrationState *s;
2274
2275 s = migrate_get_current();
2276
32c3db5b 2277 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
53dd370c
DDAG
2278}
2279
58110f0a
VSO
2280bool migrate_postcopy(void)
2281{
dd6bb914 2282 return migrate_postcopy_ram() || migrate_dirty_bitmaps();
58110f0a
VSO
2283}
2284
bde1e2ec
CV
2285bool migrate_auto_converge(void)
2286{
2287 MigrationState *s;
2288
2289 s = migrate_get_current();
2290
2291 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
2292}
2293
323004a3
PL
2294bool migrate_zero_blocks(void)
2295{
2296 MigrationState *s;
2297
2298 s = migrate_get_current();
2299
2300 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
2301}
2302
f22f928e
AP
2303bool migrate_postcopy_blocktime(void)
2304{
2305 MigrationState *s;
2306
2307 s = migrate_get_current();
2308
2309 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME];
2310}
2311
8706d2d5
LL
2312bool migrate_use_compression(void)
2313{
dde4e694
LL
2314 MigrationState *s;
2315
2316 s = migrate_get_current();
2317
2318 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
8706d2d5
LL
2319}
2320
2321int migrate_compress_level(void)
2322{
2323 MigrationState *s;
2324
2325 s = migrate_get_current();
2326
2594f56d 2327 return s->parameters.compress_level;
8706d2d5
LL
2328}
2329
2330int migrate_compress_threads(void)
2331{
2332 MigrationState *s;
2333
2334 s = migrate_get_current();
2335
2594f56d 2336 return s->parameters.compress_threads;
8706d2d5
LL
2337}
2338
1d58872a
XG
2339int migrate_compress_wait_thread(void)
2340{
2341 MigrationState *s;
2342
2343 s = migrate_get_current();
2344
2345 return s->parameters.compress_wait_thread;
2346}
2347
3fcb38c2
LL
2348int migrate_decompress_threads(void)
2349{
2350 MigrationState *s;
2351
2352 s = migrate_get_current();
2353
2594f56d 2354 return s->parameters.decompress_threads;
3fcb38c2
LL
2355}
2356
55efc8c2
VSO
2357bool migrate_dirty_bitmaps(void)
2358{
2359 MigrationState *s;
2360
2361 s = migrate_get_current();
2362
2363 return s->enabled_capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS];
2364}
2365
18269069
YK
2366bool migrate_ignore_shared(void)
2367{
2368 MigrationState *s;
2369
2370 s = migrate_get_current();
2371
2372 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_IGNORE_SHARED];
2373}
2374
b9d68df6
YK
2375bool migrate_validate_uuid(void)
2376{
2377 MigrationState *s;
2378
2379 s = migrate_get_current();
2380
2381 return s->enabled_capabilities[MIGRATION_CAPABILITY_VALIDATE_UUID];
2382}
2383
b05dc723
JQ
2384bool migrate_use_events(void)
2385{
2386 MigrationState *s;
2387
2388 s = migrate_get_current();
2389
2390 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
2391}
2392
30126bbf
JQ
2393bool migrate_use_multifd(void)
2394{
2395 MigrationState *s;
2396
2397 s = migrate_get_current();
2398
cbfd6c95 2399 return s->enabled_capabilities[MIGRATION_CAPABILITY_MULTIFD];
30126bbf
JQ
2400}
2401
93fbd031
DDAG
2402bool migrate_pause_before_switchover(void)
2403{
2404 MigrationState *s;
2405
2406 s = migrate_get_current();
2407
2408 return s->enabled_capabilities[
2409 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER];
2410}
2411
4075fb1c
JQ
2412int migrate_multifd_channels(void)
2413{
2414 MigrationState *s;
2415
2416 s = migrate_get_current();
2417
cbfd6c95 2418 return s->parameters.multifd_channels;
4075fb1c
JQ
2419}
2420
ab7cbb0b
JQ
2421MultiFDCompression migrate_multifd_compression(void)
2422{
2423 MigrationState *s;
2424
2425 s = migrate_get_current();
2426
2427 return s->parameters.multifd_compression;
2428}
2429
9004db48
JQ
2430int migrate_multifd_zlib_level(void)
2431{
2432 MigrationState *s;
2433
2434 s = migrate_get_current();
2435
2436 return s->parameters.multifd_zlib_level;
2437}
2438
6a9ad154
JQ
2439int migrate_multifd_zstd_level(void)
2440{
2441 MigrationState *s;
2442
2443 s = migrate_get_current();
2444
2445 return s->parameters.multifd_zstd_level;
2446}
2447
17ad9b35
OW
2448int migrate_use_xbzrle(void)
2449{
2450 MigrationState *s;
2451
2452 s = migrate_get_current();
2453
2454 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
2455}
2456
2457int64_t migrate_xbzrle_cache_size(void)
2458{
2459 MigrationState *s;
2460
2461 s = migrate_get_current();
2462
73af8dd8 2463 return s->parameters.xbzrle_cache_size;
17ad9b35 2464}
0d82d0e8 2465
7e555c6c
DDAG
2466static int64_t migrate_max_postcopy_bandwidth(void)
2467{
2468 MigrationState *s;
2469
2470 s = migrate_get_current();
2471
2472 return s->parameters.max_postcopy_bandwidth;
2473}
2474
2833c59b
JQ
2475bool migrate_use_block(void)
2476{
2477 MigrationState *s;
2478
2479 s = migrate_get_current();
2480
2481 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
2482}
2483
c788ada8
PX
2484bool migrate_use_return_path(void)
2485{
2486 MigrationState *s;
2487
2488 s = migrate_get_current();
2489
2490 return s->enabled_capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
2491}
2492
2833c59b
JQ
2493bool migrate_use_block_incremental(void)
2494{
2495 MigrationState *s;
2496
2497 s = migrate_get_current();
2498
2499 return s->parameters.block_incremental;
2500}
2501
70b20477
DDAG
2502/* migration thread support */
2503/*
2504 * Something bad happened to the RP stream, mark an error
2505 * The caller shall print or trace something to indicate why
2506 */
2507static void mark_source_rp_bad(MigrationState *s)
2508{
2509 s->rp_state.error = true;
2510}
2511
2512static struct rp_cmd_args {
2513 ssize_t len; /* -1 = variable */
2514 const char *name;
2515} rp_cmd_args[] = {
2516 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
2517 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
2518 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
1e2d90eb
DDAG
2519 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
2520 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
a335debb 2521 [MIG_RP_MSG_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" },
13955b89 2522 [MIG_RP_MSG_RESUME_ACK] = { .len = 4, .name = "RESUME_ACK" },
70b20477
DDAG
2523 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
2524};
2525
1e2d90eb
DDAG
2526/*
2527 * Process a request for pages received on the return path,
2528 * We're allowed to send more than requested (e.g. to round to our page size)
2529 * and we don't need to send pages that have already been sent.
2530 */
2531static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
2532 ram_addr_t start, size_t len)
2533{
038adc2f 2534 long our_host_ps = qemu_real_host_page_size;
6c595cde 2535
1e2d90eb 2536 trace_migrate_handle_rp_req_pages(rbname, start, len);
6c595cde
DDAG
2537
2538 /*
2539 * Since we currently insist on matching page sizes, just sanity check
2540 * we're being asked for whole host pages.
2541 */
395cb450
BY
2542 if (start & (our_host_ps - 1) ||
2543 (len & (our_host_ps - 1))) {
6c595cde
DDAG
2544 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
2545 " len: %zd", __func__, start, len);
2546 mark_source_rp_bad(ms);
2547 return;
2548 }
2549
96506894 2550 if (ram_save_queue_pages(rbname, start, len)) {
6c595cde
DDAG
2551 mark_source_rp_bad(ms);
2552 }
1e2d90eb
DDAG
2553}
2554
14b1742e
PX
2555/* Return true to retry, false to quit */
2556static bool postcopy_pause_return_path_thread(MigrationState *s)
2557{
2558 trace_postcopy_pause_return_path();
2559
2560 qemu_sem_wait(&s->postcopy_pause_rp_sem);
2561
2562 trace_postcopy_pause_return_path_continued();
2563
2564 return true;
2565}
2566
a335debb
PX
2567static int migrate_handle_rp_recv_bitmap(MigrationState *s, char *block_name)
2568{
2569 RAMBlock *block = qemu_ram_block_by_name(block_name);
2570
2571 if (!block) {
2572 error_report("%s: invalid block name '%s'", __func__, block_name);
2573 return -EINVAL;
2574 }
2575
2576 /* Fetch the received bitmap and refresh the dirty bitmap */
2577 return ram_dirty_bitmap_reload(s, block);
2578}
2579
13955b89
PX
2580static int migrate_handle_rp_resume_ack(MigrationState *s, uint32_t value)
2581{
2582 trace_source_return_path_thread_resume_ack(value);
2583
2584 if (value != MIGRATION_RESUME_ACK_VALUE) {
2585 error_report("%s: illegal resume_ack value %"PRIu32,
2586 __func__, value);
2587 return -1;
2588 }
2589
2590 /* Now both sides are active. */
2591 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
2592 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2593
94190696
PX
2594 /* Notify send thread that time to continue send pages */
2595 qemu_sem_post(&s->rp_state.rp_sem);
13955b89
PX
2596
2597 return 0;
2598}
2599
70b20477
DDAG
2600/*
2601 * Handles messages sent on the return path towards the source VM
2602 *
2603 */
2604static void *source_return_path_thread(void *opaque)
2605{
2606 MigrationState *ms = opaque;
2607 QEMUFile *rp = ms->rp_state.from_dst_file;
2608 uint16_t header_len, header_type;
568b01ca 2609 uint8_t buf[512];
70b20477 2610 uint32_t tmp32, sibling_error;
1e2d90eb
DDAG
2611 ram_addr_t start = 0; /* =0 to silence warning */
2612 size_t len = 0, expected_len;
70b20477
DDAG
2613 int res;
2614
2615 trace_source_return_path_thread_entry();
74637e6f 2616 rcu_register_thread();
14b1742e
PX
2617
2618retry:
70b20477
DDAG
2619 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
2620 migration_is_setup_or_active(ms->state)) {
2621 trace_source_return_path_thread_loop_top();
2622 header_type = qemu_get_be16(rp);
2623 header_len = qemu_get_be16(rp);
2624
7a9ddfbf
PX
2625 if (qemu_file_get_error(rp)) {
2626 mark_source_rp_bad(ms);
2627 goto out;
2628 }
2629
70b20477
DDAG
2630 if (header_type >= MIG_RP_MSG_MAX ||
2631 header_type == MIG_RP_MSG_INVALID) {
2632 error_report("RP: Received invalid message 0x%04x length 0x%04x",
ed8b2828 2633 header_type, header_len);
70b20477
DDAG
2634 mark_source_rp_bad(ms);
2635 goto out;
2636 }
2637
2638 if ((rp_cmd_args[header_type].len != -1 &&
2639 header_len != rp_cmd_args[header_type].len) ||
568b01ca 2640 header_len > sizeof(buf)) {
70b20477 2641 error_report("RP: Received '%s' message (0x%04x) with"
ed8b2828
MZ
2642 "incorrect length %d expecting %zu",
2643 rp_cmd_args[header_type].name, header_type, header_len,
2644 (size_t)rp_cmd_args[header_type].len);
70b20477
DDAG
2645 mark_source_rp_bad(ms);
2646 goto out;
2647 }
2648
2649 /* We know we've got a valid header by this point */
2650 res = qemu_get_buffer(rp, buf, header_len);
2651 if (res != header_len) {
2652 error_report("RP: Failed reading data for message 0x%04x"
2653 " read %d expected %d",
2654 header_type, res, header_len);
2655 mark_source_rp_bad(ms);
2656 goto out;
2657 }
2658
2659 /* OK, we have the message and the data */
2660 switch (header_type) {
2661 case MIG_RP_MSG_SHUT:
4d885131 2662 sibling_error = ldl_be_p(buf);
70b20477
DDAG
2663 trace_source_return_path_thread_shut(sibling_error);
2664 if (sibling_error) {
2665 error_report("RP: Sibling indicated error %d", sibling_error);
2666 mark_source_rp_bad(ms);
2667 }
2668 /*
2669 * We'll let the main thread deal with closing the RP
2670 * we could do a shutdown(2) on it, but we're the only user
2671 * anyway, so there's nothing gained.
2672 */
2673 goto out;
2674
2675 case MIG_RP_MSG_PONG:
4d885131 2676 tmp32 = ldl_be_p(buf);
70b20477
DDAG
2677 trace_source_return_path_thread_pong(tmp32);
2678 break;
2679
1e2d90eb 2680 case MIG_RP_MSG_REQ_PAGES:
4d885131
PM
2681 start = ldq_be_p(buf);
2682 len = ldl_be_p(buf + 8);
1e2d90eb
DDAG
2683 migrate_handle_rp_req_pages(ms, NULL, start, len);
2684 break;
2685
2686 case MIG_RP_MSG_REQ_PAGES_ID:
2687 expected_len = 12 + 1; /* header + termination */
2688
2689 if (header_len >= expected_len) {
4d885131
PM
2690 start = ldq_be_p(buf);
2691 len = ldl_be_p(buf + 8);
1e2d90eb
DDAG
2692 /* Now we expect an idstr */
2693 tmp32 = buf[12]; /* Length of the following idstr */
2694 buf[13 + tmp32] = '\0';
2695 expected_len += tmp32;
2696 }
2697 if (header_len != expected_len) {
2698 error_report("RP: Req_Page_id with length %d expecting %zd",
ed8b2828 2699 header_len, expected_len);
1e2d90eb
DDAG
2700 mark_source_rp_bad(ms);
2701 goto out;
2702 }
2703 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
2704 break;
2705
a335debb
PX
2706 case MIG_RP_MSG_RECV_BITMAP:
2707 if (header_len < 1) {
2708 error_report("%s: missing block name", __func__);
2709 mark_source_rp_bad(ms);
2710 goto out;
2711 }
2712 /* Format: len (1B) + idstr (<255B). This ends the idstr. */
2713 buf[buf[0] + 1] = '\0';
2714 if (migrate_handle_rp_recv_bitmap(ms, (char *)(buf + 1))) {
2715 mark_source_rp_bad(ms);
2716 goto out;
2717 }
2718 break;
2719
13955b89
PX
2720 case MIG_RP_MSG_RESUME_ACK:
2721 tmp32 = ldl_be_p(buf);
2722 if (migrate_handle_rp_resume_ack(ms, tmp32)) {
2723 mark_source_rp_bad(ms);
2724 goto out;
2725 }
2726 break;
2727
70b20477
DDAG
2728 default:
2729 break;
2730 }
2731 }
14b1742e
PX
2732
2733out:
2734 res = qemu_file_get_error(rp);
2735 if (res) {
fb14a42a 2736 if (res == -EIO && migration_in_postcopy()) {
14b1742e
PX
2737 /*
2738 * Maybe there is something we can do: it looks like a
2739 * network down issue, and we pause for a recovery.
2740 */
2741 if (postcopy_pause_return_path_thread(ms)) {
2742 /* Reload rp, reset the rest */
0284a2a8
MAL
2743 if (rp != ms->rp_state.from_dst_file) {
2744 qemu_fclose(rp);
2745 rp = ms->rp_state.from_dst_file;
2746 }
14b1742e
PX
2747 ms->rp_state.error = false;
2748 goto retry;
2749 }
2750 }
2751
70b20477
DDAG
2752 trace_source_return_path_thread_bad_end();
2753 mark_source_rp_bad(ms);
2754 }
2755
2756 trace_source_return_path_thread_end();
70b20477
DDAG
2757 ms->rp_state.from_dst_file = NULL;
2758 qemu_fclose(rp);
74637e6f 2759 rcu_unregister_thread();
70b20477
DDAG
2760 return NULL;
2761}
2762
d3e35b8f
PX
2763static int open_return_path_on_source(MigrationState *ms,
2764 bool create_thread)
70b20477
DDAG
2765{
2766
89a02a9f 2767 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
70b20477
DDAG
2768 if (!ms->rp_state.from_dst_file) {
2769 return -1;
2770 }
2771
2772 trace_open_return_path_on_source();
d3e35b8f
PX
2773
2774 if (!create_thread) {
2775 /* We're done */
2776 return 0;
2777 }
2778
70b20477
DDAG
2779 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
2780 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
2781
2782 trace_open_return_path_on_source_continue();
2783
2784 return 0;
2785}
2786
70b20477
DDAG
2787/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
2788static int await_return_path_close_on_source(MigrationState *ms)
2789{
2790 /*
2791 * If this is a normal exit then the destination will send a SHUT and the
2792 * rp_thread will exit, however if there's an error we need to cause
2793 * it to exit.
2794 */
89a02a9f 2795 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
70b20477
DDAG
2796 /*
2797 * shutdown(2), if we have it, will cause it to unblock if it's stuck
2798 * waiting for the destination.
2799 */
2800 qemu_file_shutdown(ms->rp_state.from_dst_file);
2801 mark_source_rp_bad(ms);
2802 }
2803 trace_await_return_path_close_on_source_joining();
2804 qemu_thread_join(&ms->rp_state.rp_thread);
2805 trace_await_return_path_close_on_source_close();
2806 return ms->rp_state.error;
2807}
2808
1d34e4bf
DDAG
2809/*
2810 * Switch from normal iteration to postcopy
2811 * Returns non-0 on error
2812 */
7287cbd4 2813static int postcopy_start(MigrationState *ms)
1d34e4bf
DDAG
2814{
2815 int ret;
61b67d47
DB
2816 QIOChannelBuffer *bioc;
2817 QEMUFile *fb;
1d34e4bf 2818 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
7e555c6c 2819 int64_t bandwidth = migrate_max_postcopy_bandwidth();
ef8d6488 2820 bool restart_block = false;
0331c8ca
DDAG
2821 int cur_state = MIGRATION_STATUS_ACTIVE;
2822 if (!migrate_pause_before_switchover()) {
2823 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
2824 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2825 }
1d34e4bf
DDAG
2826
2827 trace_postcopy_start();
2828 qemu_mutex_lock_iothread();
2829 trace_postcopy_start_set_run();
2830
fb064112 2831 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
1d34e4bf
DDAG
2832 global_state_store();
2833 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
76b1c7fe
KW
2834 if (ret < 0) {
2835 goto fail;
2836 }
1d34e4bf 2837
0331c8ca
DDAG
2838 ret = migration_maybe_pause(ms, &cur_state,
2839 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2840 if (ret < 0) {
2841 goto fail;
2842 }
2843
76b1c7fe 2844 ret = bdrv_inactivate_all();
1d34e4bf
DDAG
2845 if (ret < 0) {
2846 goto fail;
2847 }
ef8d6488 2848 restart_block = true;
1d34e4bf 2849
1c0d249d
DDAG
2850 /*
2851 * Cause any non-postcopiable, but iterative devices to
2852 * send out their final data.
2853 */
a1fbe750 2854 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
1c0d249d 2855
1d34e4bf
DDAG
2856 /*
2857 * in Finish migrate and with the io-lock held everything should
2858 * be quiet, but we've potentially still got dirty pages and we
2859 * need to tell the destination to throw any pages it's already received
2860 * that are dirty
2861 */
58110f0a
VSO
2862 if (migrate_postcopy_ram()) {
2863 if (ram_postcopy_send_discard_bitmap(ms)) {
2864 error_report("postcopy send discard bitmap failed");
2865 goto fail;
2866 }
1d34e4bf
DDAG
2867 }
2868
2869 /*
2870 * send rest of state - note things that are doing postcopy
2871 * will notice we're in POSTCOPY_ACTIVE and not actually
2872 * wrap their state up here
2873 */
7e555c6c
DDAG
2874 /* 0 max-postcopy-bandwidth means unlimited */
2875 if (!bandwidth) {
2876 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
2877 } else {
2878 qemu_file_set_rate_limit(ms->to_dst_file, bandwidth / XFER_LIMIT_RATIO);
2879 }
58110f0a
VSO
2880 if (migrate_postcopy_ram()) {
2881 /* Ping just for debugging, helps line traces up */
2882 qemu_savevm_send_ping(ms->to_dst_file, 2);
2883 }
1d34e4bf
DDAG
2884
2885 /*
2886 * While loading the device state we may trigger page transfer
2887 * requests and the fd must be free to process those, and thus
2888 * the destination must read the whole device state off the fd before
2889 * it starts processing it. Unfortunately the ad-hoc migration format
2890 * doesn't allow the destination to know the size to read without fully
2891 * parsing it through each devices load-state code (especially the open
2892 * coded devices that use get/put).
2893 * So we wrap the device state up in a package with a length at the start;
2894 * to do this we use a qemu_buf to hold the whole of the device state.
2895 */
61b67d47 2896 bioc = qio_channel_buffer_new(4096);
6f01f136 2897 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
61b67d47
DB
2898 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
2899 object_unref(OBJECT(bioc));
1d34e4bf 2900
c76201ab
DDAG
2901 /*
2902 * Make sure the receiver can get incoming pages before we send the rest
2903 * of the state
2904 */
2905 qemu_savevm_send_postcopy_listen(fb);
2906
a1fbe750 2907 qemu_savevm_state_complete_precopy(fb, false, false);
58110f0a
VSO
2908 if (migrate_postcopy_ram()) {
2909 qemu_savevm_send_ping(fb, 3);
2910 }
1d34e4bf
DDAG
2911
2912 qemu_savevm_send_postcopy_run(fb);
2913
2914 /* <><> end of stuff going into the package */
1d34e4bf 2915
ef8d6488
DDAG
2916 /* Last point of recovery; as soon as we send the package the destination
2917 * can open devices and potentially start running.
2918 * Lets just check again we've not got any errors.
2919 */
2920 ret = qemu_file_get_error(ms->to_dst_file);
2921 if (ret) {
2922 error_report("postcopy_start: Migration stream errored (pre package)");
2923 goto fail_closefb;
2924 }
2925
2926 restart_block = false;
2927
1d34e4bf 2928 /* Now send that blob */
61b67d47 2929 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
1d34e4bf
DDAG
2930 goto fail_closefb;
2931 }
2932 qemu_fclose(fb);
b82fc321
DDAG
2933
2934 /* Send a notify to give a chance for anything that needs to happen
2935 * at the transition to postcopy and after the device state; in particular
2936 * spice needs to trigger a transition now
2937 */
2938 ms->postcopy_after_devices = true;
2939 notifier_list_notify(&migration_state_notifiers, ms);
2940
1d34e4bf
DDAG
2941 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
2942
2943 qemu_mutex_unlock_iothread();
2944
58110f0a
VSO
2945 if (migrate_postcopy_ram()) {
2946 /*
2947 * Although this ping is just for debug, it could potentially be
2948 * used for getting a better measurement of downtime at the source.
2949 */
2950 qemu_savevm_send_ping(ms->to_dst_file, 4);
2951 }
1d34e4bf 2952
ced1c616
PB
2953 if (migrate_release_ram()) {
2954 ram_postcopy_migrated_memory_release(ms);
2955 }
2956
89a02a9f 2957 ret = qemu_file_get_error(ms->to_dst_file);
1d34e4bf
DDAG
2958 if (ret) {
2959 error_report("postcopy_start: Migration stream errored");
48781e5b 2960 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1d34e4bf
DDAG
2961 MIGRATION_STATUS_FAILED);
2962 }
2963
2964 return ret;
2965
2966fail_closefb:
2967 qemu_fclose(fb);
2968fail:
48781e5b 2969 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1d34e4bf 2970 MIGRATION_STATUS_FAILED);
ef8d6488
DDAG
2971 if (restart_block) {
2972 /* A failure happened early enough that we know the destination hasn't
2973 * accessed block devices, so we're safe to recover.
2974 */
2975 Error *local_err = NULL;
2976
2977 bdrv_invalidate_cache_all(&local_err);
2978 if (local_err) {
2979 error_report_err(local_err);
2980 }
2981 }
1d34e4bf
DDAG
2982 qemu_mutex_unlock_iothread();
2983 return -1;
2984}
2985
e91d8951
DDAG
2986/**
2987 * migration_maybe_pause: Pause if required to by
2988 * migrate_pause_before_switchover called with the iothread locked
2989 * Returns: 0 on success
2990 */
0331c8ca
DDAG
2991static int migration_maybe_pause(MigrationState *s,
2992 int *current_active_state,
2993 int new_state)
e91d8951
DDAG
2994{
2995 if (!migrate_pause_before_switchover()) {
2996 return 0;
2997 }
2998
2999 /* Since leaving this state is not atomic with posting the semaphore
3000 * it's possible that someone could have issued multiple migrate_continue
3001 * and the semaphore is incorrectly positive at this point;
3002 * the docs say it's undefined to reinit a semaphore that's already
3003 * init'd, so use timedwait to eat up any existing posts.
3004 */
3005 while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) {
3006 /* This block intentionally left blank */
3007 }
3008
8958338b
ZF
3009 /*
3010 * If the migration is cancelled when it is in the completion phase,
3011 * the migration state is set to MIGRATION_STATUS_CANCELLING.
3012 * So we don't need to wait a semaphore, otherwise we would always
3013 * wait for the 'pause_sem' semaphore.
3014 */
3015 if (s->state != MIGRATION_STATUS_CANCELLING) {
3016 qemu_mutex_unlock_iothread();
3017 migrate_set_state(&s->state, *current_active_state,
3018 MIGRATION_STATUS_PRE_SWITCHOVER);
3019 qemu_sem_wait(&s->pause_sem);
3020 migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER,
3021 new_state);
3022 *current_active_state = new_state;
3023 qemu_mutex_lock_iothread();
3024 }
e91d8951 3025
0331c8ca 3026 return s->state == new_state ? 0 : -EINVAL;
e91d8951
DDAG
3027}
3028
09f6c85e
DDAG
3029/**
3030 * migration_completion: Used by migration_thread when there's not much left.
3031 * The caller 'breaks' the loop when this returns.
3032 *
3033 * @s: Current migration state
09f6c85e 3034 */
2ad87305 3035static void migration_completion(MigrationState *s)
09f6c85e
DDAG
3036{
3037 int ret;
2ad87305 3038 int current_active_state = s->state;
09f6c85e 3039
b10ac0c4
DDAG
3040 if (s->state == MIGRATION_STATUS_ACTIVE) {
3041 qemu_mutex_lock_iothread();
64909f97 3042 s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
fb064112 3043 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
7287cbd4 3044 s->vm_was_running = runstate_is_running();
b10ac0c4
DDAG
3045 ret = global_state_store();
3046
3047 if (!ret) {
a1fbe750 3048 bool inactivate = !migrate_colo_enabled();
b10ac0c4 3049 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
e91d8951 3050 if (ret >= 0) {
0331c8ca
DDAG
3051 ret = migration_maybe_pause(s, &current_active_state,
3052 MIGRATION_STATUS_DEVICE);
e91d8951 3053 }
f07fa4cb
KW
3054 if (ret >= 0) {
3055 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
a1fbe750
FZ
3056 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
3057 inactivate);
f07fa4cb 3058 }
a1fbe750
FZ
3059 if (inactivate && ret >= 0) {
3060 s->block_inactive = true;
b10ac0c4
DDAG
3061 }
3062 }
3063 qemu_mutex_unlock_iothread();
09f6c85e 3064
b10ac0c4
DDAG
3065 if (ret < 0) {
3066 goto fail;
09f6c85e 3067 }
b10ac0c4
DDAG
3068 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
3069 trace_migration_completion_postcopy_end();
3070
89a02a9f 3071 qemu_savevm_state_complete_postcopy(s->to_dst_file);
b10ac0c4 3072 trace_migration_completion_postcopy_end_after_complete();
6ba11211
LM
3073 } else if (s->state == MIGRATION_STATUS_CANCELLING) {
3074 goto fail;
09f6c85e 3075 }
09f6c85e 3076
b10ac0c4
DDAG
3077 /*
3078 * If rp was opened we must clean up the thread before
3079 * cleaning everything else up (since if there are no failures
3080 * it will wait for the destination to send it's status in
3081 * a SHUT command).
b10ac0c4 3082 */
0425dc97 3083 if (s->rp_state.from_dst_file) {
b10ac0c4 3084 int rp_error;
0425dc97 3085 trace_migration_return_path_end_before();
b10ac0c4 3086 rp_error = await_return_path_close_on_source(s);
0425dc97 3087 trace_migration_return_path_end_after(rp_error);
b10ac0c4 3088 if (rp_error) {
fe904ea8 3089 goto fail_invalidate;
b10ac0c4 3090 }
09f6c85e
DDAG
3091 }
3092
89a02a9f 3093 if (qemu_file_get_error(s->to_dst_file)) {
09f6c85e 3094 trace_migration_completion_file_err();
fe904ea8 3095 goto fail_invalidate;
09f6c85e
DDAG
3096 }
3097
0b827d5e
HZ
3098 if (!migrate_colo_enabled()) {
3099 migrate_set_state(&s->state, current_active_state,
3100 MIGRATION_STATUS_COMPLETED);
3101 }
3102
09f6c85e
DDAG
3103 return;
3104
fe904ea8
GK
3105fail_invalidate:
3106 /* If not doing postcopy, vm_start() will be called: let's regain
3107 * control on images.
3108 */
6039dd5b
DDAG
3109 if (s->state == MIGRATION_STATUS_ACTIVE ||
3110 s->state == MIGRATION_STATUS_DEVICE) {
fe904ea8
GK
3111 Error *local_err = NULL;
3112
1d2acc31 3113 qemu_mutex_lock_iothread();
fe904ea8
GK
3114 bdrv_invalidate_cache_all(&local_err);
3115 if (local_err) {
3116 error_report_err(local_err);
1d2acc31
HZ
3117 } else {
3118 s->block_inactive = false;
fe904ea8 3119 }
1d2acc31 3120 qemu_mutex_unlock_iothread();
fe904ea8
GK
3121 }
3122
09f6c85e 3123fail:
48781e5b
HZ
3124 migrate_set_state(&s->state, current_active_state,
3125 MIGRATION_STATUS_FAILED);
09f6c85e
DDAG
3126}
3127
35a6ed4f
HZ
3128bool migrate_colo_enabled(void)
3129{
3130 MigrationState *s = migrate_get_current();
3131 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
3132}
3133
b23c2ade
PX
3134typedef enum MigThrError {
3135 /* No error detected */
3136 MIG_THR_ERR_NONE = 0,
3137 /* Detected error, but resumed successfully */
3138 MIG_THR_ERR_RECOVERED = 1,
3139 /* Detected fatal error, need to exit */
3140 MIG_THR_ERR_FATAL = 2,
3141} MigThrError;
3142
94190696
PX
3143static int postcopy_resume_handshake(MigrationState *s)
3144{
3145 qemu_savevm_send_postcopy_resume(s->to_dst_file);
3146
3147 while (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
3148 qemu_sem_wait(&s->rp_state.rp_sem);
3149 }
3150
3151 if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
3152 return 0;
3153 }
3154
3155 return -1;
3156}
3157
135b87b4
PX
3158/* Return zero if success, or <0 for error */
3159static int postcopy_do_resume(MigrationState *s)
3160{
d1b8eadb
PX
3161 int ret;
3162
3163 /*
3164 * Call all the resume_prepare() hooks, so that modules can be
3165 * ready for the migration resume.
3166 */
3167 ret = qemu_savevm_state_resume_prepare(s);
3168 if (ret) {
3169 error_report("%s: resume_prepare() failure detected: %d",
3170 __func__, ret);
3171 return ret;
3172 }
3173
3174 /*
94190696
PX
3175 * Last handshake with destination on the resume (destination will
3176 * switch to postcopy-active afterwards)
d1b8eadb 3177 */
94190696
PX
3178 ret = postcopy_resume_handshake(s);
3179 if (ret) {
3180 error_report("%s: handshake failed: %d", __func__, ret);
3181 return ret;
3182 }
d1b8eadb 3183
135b87b4
PX
3184 return 0;
3185}
3186
b23c2ade
PX
3187/*
3188 * We don't return until we are in a safe state to continue current
3189 * postcopy migration. Returns MIG_THR_ERR_RECOVERED if recovered, or
3190 * MIG_THR_ERR_FATAL if unrecovery failure happened.
3191 */
3192static MigThrError postcopy_pause(MigrationState *s)
3193{
3194 assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
b23c2ade 3195
135b87b4 3196 while (true) {
62df066f
PX
3197 QEMUFile *file;
3198
135b87b4
PX
3199 /* Current channel is possibly broken. Release it. */
3200 assert(s->to_dst_file);
62df066f
PX
3201 qemu_mutex_lock(&s->qemu_file_lock);
3202 file = s->to_dst_file;
135b87b4 3203 s->to_dst_file = NULL;
62df066f
PX
3204 qemu_mutex_unlock(&s->qemu_file_lock);
3205
3206 qemu_file_shutdown(file);
3207 qemu_fclose(file);
b23c2ade 3208
d246ea50
PX
3209 migrate_set_state(&s->state, s->state,
3210 MIGRATION_STATUS_POSTCOPY_PAUSED);
3211
135b87b4
PX
3212 error_report("Detected IO failure for postcopy. "
3213 "Migration paused.");
b23c2ade 3214
135b87b4
PX
3215 /*
3216 * We wait until things fixed up. Then someone will setup the
3217 * status back for us.
3218 */
3219 while (s->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
3220 qemu_sem_wait(&s->postcopy_pause_sem);
3221 }
3222
3223 if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
3224 /* Woken up by a recover procedure. Give it a shot */
b23c2ade 3225
135b87b4
PX
3226 /*
3227 * Firstly, let's wake up the return path now, with a new
3228 * return path channel.
3229 */
3230 qemu_sem_post(&s->postcopy_pause_rp_sem);
3231
3232 /* Do the resume logic */
3233 if (postcopy_do_resume(s) == 0) {
3234 /* Let's continue! */
3235 trace_postcopy_pause_continued();
3236 return MIG_THR_ERR_RECOVERED;
3237 } else {
3238 /*
3239 * Something wrong happened during the recovery, let's
3240 * pause again. Pause is always better than throwing
3241 * data away.
3242 */
3243 continue;
3244 }
3245 } else {
3246 /* This is not right... Time to quit. */
3247 return MIG_THR_ERR_FATAL;
3248 }
3249 }
b23c2ade
PX
3250}
3251
3252static MigThrError migration_detect_error(MigrationState *s)
3253{
3254 int ret;
c3c5eae6 3255 int state = s->state;
3d661c8a 3256 Error *local_error = NULL;
c3c5eae6
DDAG
3257
3258 if (state == MIGRATION_STATUS_CANCELLING ||
3259 state == MIGRATION_STATUS_CANCELLED) {
3260 /* End the migration, but don't set the state to failed */
3261 return MIG_THR_ERR_FATAL;
3262 }
b23c2ade
PX
3263
3264 /* Try to detect any file errors */
3d661c8a 3265 ret = qemu_file_get_error_obj(s->to_dst_file, &local_error);
b23c2ade
PX
3266 if (!ret) {
3267 /* Everything is fine */
3d661c8a 3268 assert(!local_error);
b23c2ade
PX
3269 return MIG_THR_ERR_NONE;
3270 }
3271
3d661c8a
YK
3272 if (local_error) {
3273 migrate_set_error(s, local_error);
3274 error_free(local_error);
3275 }
3276
c3c5eae6 3277 if (state == MIGRATION_STATUS_POSTCOPY_ACTIVE && ret == -EIO) {
b23c2ade
PX
3278 /*
3279 * For postcopy, we allow the network to be down for a
3280 * while. After that, it can be continued by a
3281 * recovery phase.
3282 */
3283 return postcopy_pause(s);
3284 } else {
3285 /*
3286 * For precopy (or postcopy with error outside IO), we fail
3287 * with no time.
3288 */
c3c5eae6 3289 migrate_set_state(&s->state, state, MIGRATION_STATUS_FAILED);
b23c2ade
PX
3290 trace_migration_thread_file_err();
3291
3292 /* Time to stop the migration, now. */
3293 return MIG_THR_ERR_FATAL;
3294 }
3295}
3296
268dcd46 3297/* How many bytes have we transferred since the beginning of the migration */
0c8f0efd
JQ
3298static uint64_t migration_total_bytes(MigrationState *s)
3299{
b9ee2f7d 3300 return qemu_ftell(s->to_dst_file) + ram_counters.multifd_bytes;
0c8f0efd
JQ
3301}
3302
cf011f08
PX
3303static void migration_calculate_complete(MigrationState *s)
3304{
0c8f0efd 3305 uint64_t bytes = migration_total_bytes(s);
cf011f08 3306 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
6cde6fbe 3307 int64_t transfer_time;
cf011f08
PX
3308
3309 s->total_time = end_time - s->start_time;
3310 if (!s->downtime) {
3311 /*
3312 * It's still not set, so we are precopy migration. For
3313 * postcopy, downtime is calculated during postcopy_start().
3314 */
3315 s->downtime = end_time - s->downtime_start;
3316 }
3317
6cde6fbe
JQ
3318 transfer_time = s->total_time - s->setup_time;
3319 if (transfer_time) {
3320 s->mbps = ((double) bytes * 8.0) / transfer_time / 1000;
cf011f08
PX
3321 }
3322}
3323
87f3bd87
IR
3324static void update_iteration_initial_status(MigrationState *s)
3325{
3326 /*
3327 * Update these three fields at the same time to avoid mismatch info lead
3328 * wrong speed calculation.
3329 */
3330 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3331 s->iteration_initial_bytes = migration_total_bytes(s);
3332 s->iteration_initial_pages = ram_get_total_transferred_pages();
3333}
3334
b15df1ae
PX
3335static void migration_update_counters(MigrationState *s,
3336 int64_t current_time)
3337{
aecbfe9c 3338 uint64_t transferred, transferred_pages, time_spent;
0c8f0efd 3339 uint64_t current_bytes; /* bytes transferred since the beginning */
b15df1ae
PX
3340 double bandwidth;
3341
3342 if (current_time < s->iteration_start_time + BUFFER_DELAY) {
3343 return;
3344 }
3345
0c8f0efd
JQ
3346 current_bytes = migration_total_bytes(s);
3347 transferred = current_bytes - s->iteration_initial_bytes;
b15df1ae
PX
3348 time_spent = current_time - s->iteration_start_time;
3349 bandwidth = (double)transferred / time_spent;
0781c1ed 3350 s->threshold_size = bandwidth * s->parameters.downtime_limit;
b15df1ae
PX
3351
3352 s->mbps = (((double) transferred * 8.0) /
3353 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
3354
aecbfe9c
XG
3355 transferred_pages = ram_get_total_transferred_pages() -
3356 s->iteration_initial_pages;
3357 s->pages_per_second = (double) transferred_pages /
3358 (((double) time_spent / 1000.0));
3359
b15df1ae
PX
3360 /*
3361 * if we haven't sent anything, we don't want to
3362 * recalculate. 10000 is a small enough number for our purposes
3363 */
3364 if (ram_counters.dirty_pages_rate && transferred > 10000) {
650af890 3365 s->expected_downtime = ram_counters.remaining / bandwidth;
b15df1ae
PX
3366 }
3367
3368 qemu_file_reset_rate_limit(s->to_dst_file);
3369
87f3bd87 3370 update_iteration_initial_status(s);
b15df1ae
PX
3371
3372 trace_migrate_transferred(transferred, time_spent,
0781c1ed 3373 bandwidth, s->threshold_size);
b15df1ae
PX
3374}
3375
2ad87305
PX
3376/* Migration thread iteration status */
3377typedef enum {
3378 MIG_ITERATE_RESUME, /* Resume current iteration */
3379 MIG_ITERATE_SKIP, /* Skip current iteration */
3380 MIG_ITERATE_BREAK, /* Break the loop */
3381} MigIterateState;
3382
3383/*
3384 * Return true if continue to the next iteration directly, false
3385 * otherwise.
3386 */
3387static MigIterateState migration_iteration_run(MigrationState *s)
3388{
47995026 3389 uint64_t pending_size, pend_pre, pend_compat, pend_post;
2ad87305
PX
3390 bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE;
3391
47995026
VSO
3392 qemu_savevm_state_pending(s->to_dst_file, s->threshold_size, &pend_pre,
3393 &pend_compat, &pend_post);
3394 pending_size = pend_pre + pend_compat + pend_post;
2ad87305
PX
3395
3396 trace_migrate_pending(pending_size, s->threshold_size,
47995026 3397 pend_pre, pend_compat, pend_post);
2ad87305
PX
3398
3399 if (pending_size && pending_size >= s->threshold_size) {
3400 /* Still a significant amount to transfer */
52aec709 3401 if (!in_postcopy && pend_pre <= s->threshold_size &&
d73415a3 3402 qatomic_read(&s->start_postcopy)) {
2ad87305
PX
3403 if (postcopy_start(s)) {
3404 error_report("%s: postcopy failed to start", __func__);
3405 }
3406 return MIG_ITERATE_SKIP;
3407 }
3408 /* Just another iteration step */
17d9351b 3409 qemu_savevm_state_iterate(s->to_dst_file, in_postcopy);
2ad87305
PX
3410 } else {
3411 trace_migration_thread_low_pending(pending_size);
3412 migration_completion(s);
3413 return MIG_ITERATE_BREAK;
3414 }
3415
3416 return MIG_ITERATE_RESUME;
3417}
3418
199aa6d4
PX
3419static void migration_iteration_finish(MigrationState *s)
3420{
3421 /* If we enabled cpu throttling for auto-converge, turn it off. */
3422 cpu_throttle_stop();
3423
3424 qemu_mutex_lock_iothread();
3425 switch (s->state) {
3426 case MIGRATION_STATUS_COMPLETED:
3427 migration_calculate_complete(s);
3428 runstate_set(RUN_STATE_POSTMIGRATE);
3429 break;
3430
3431 case MIGRATION_STATUS_ACTIVE:
3432 /*
3433 * We should really assert here, but since it's during
3434 * migration, let's try to reduce the usage of assertions.
3435 */
3436 if (!migrate_colo_enabled()) {
3437 error_report("%s: critical error: calling COLO code without "
3438 "COLO enabled", __func__);
3439 }
3440 migrate_start_colo_process(s);
3441 /*
3442 * Fixme: we will run VM in COLO no matter its old running state.
3443 * After exited COLO, we will keep running.
3444 */
3445 s->vm_was_running = true;
3446 /* Fallthrough */
3447 case MIGRATION_STATUS_FAILED:
3448 case MIGRATION_STATUS_CANCELLED:
57225e5f 3449 case MIGRATION_STATUS_CANCELLING:
199aa6d4
PX
3450 if (s->vm_was_running) {
3451 vm_start();
3452 } else {
3453 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
3454 runstate_set(RUN_STATE_POSTMIGRATE);
3455 }
3456 }
3457 break;
3458
3459 default:
3460 /* Should not reach here, but if so, forgive the VM. */
3461 error_report("%s: Unknown ending state %d", __func__, s->state);
3462 break;
3463 }
fd392cfa 3464 migrate_fd_cleanup_schedule(s);
199aa6d4
PX
3465 qemu_mutex_unlock_iothread();
3466}
3467
ad767bed
DDAG
3468void migration_make_urgent_request(void)
3469{
3470 qemu_sem_post(&migrate_get_current()->rate_limit_sem);
3471}
3472
3473void migration_consume_urgent_request(void)
3474{
3475 qemu_sem_wait(&migrate_get_current()->rate_limit_sem);
3476}
3477
97e1e067
DDAG
3478/* Returns true if the rate limiting was broken by an urgent request */
3479bool migration_rate_limit(void)
3480{
3481 int64_t now = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3482 MigrationState *s = migrate_get_current();
3483
3484 bool urgent = false;
3485 migration_update_counters(s, now);
3486 if (qemu_file_rate_limit(s->to_dst_file)) {
77386127
LS
3487
3488 if (qemu_file_get_error(s->to_dst_file)) {
3489 return false;
3490 }
97e1e067
DDAG
3491 /*
3492 * Wait for a delay to do rate limiting OR
3493 * something urgent to post the semaphore.
3494 */
3495 int ms = s->iteration_start_time + BUFFER_DELAY - now;
3496 trace_migration_rate_limit_pre(ms);
3497 if (qemu_sem_timedwait(&s->rate_limit_sem, ms) == 0) {
3498 /*
3499 * We were woken by one or more urgent things but
3500 * the timedwait will have consumed one of them.
3501 * The service routine for the urgent wake will dec
3502 * the semaphore itself for each item it consumes,
3503 * so add this one we just eat back.
3504 */
3505 qemu_sem_post(&s->rate_limit_sem);
3506 urgent = true;
3507 }
3508 trace_migration_rate_limit_post(urgent);
3509 }
3510 return urgent;
3511}
3512
70b20477
DDAG
3513/*
3514 * Master migration thread on the source VM.
3515 * It drives the migration and pumps the data down the outgoing channel.
3516 */
5f496a1b 3517static void *migration_thread(void *opaque)
0d82d0e8 3518{
9848a404 3519 MigrationState *s = opaque;
bc72ad67 3520 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
b23c2ade 3521 MigThrError thr_error;
ad767bed 3522 bool urgent = false;
76f5933a 3523
ab28bd23
PB
3524 rcu_register_thread();
3525
892ae715 3526 object_ref(OBJECT(s));
87f3bd87 3527 update_iteration_initial_status(s);
b15df1ae 3528
89a02a9f 3529 qemu_savevm_state_header(s->to_dst_file);
1d34e4bf 3530
62a02658
PX
3531 /*
3532 * If we opened the return path, we need to make sure dst has it
3533 * opened as well.
3534 */
3535 if (s->rp_state.from_dst_file) {
1d34e4bf 3536 /* Now tell the dest that it should open its end so it can reply */
89a02a9f 3537 qemu_savevm_send_open_return_path(s->to_dst_file);
1d34e4bf
DDAG
3538
3539 /* And do a ping that will make stuff easier to debug */
89a02a9f 3540 qemu_savevm_send_ping(s->to_dst_file, 1);
0425dc97 3541 }
1d34e4bf 3542
58110f0a 3543 if (migrate_postcopy()) {
1d34e4bf
DDAG
3544 /*
3545 * Tell the destination that we *might* want to do postcopy later;
3546 * if the other end can't do postcopy it should fail now, nice and
3547 * early.
3548 */
89a02a9f 3549 qemu_savevm_send_postcopy_advise(s->to_dst_file);
1d34e4bf
DDAG
3550 }
3551
aad555c2
ZC
3552 if (migrate_colo_enabled()) {
3553 /* Notify migration destination that we enable COLO */
3554 qemu_savevm_send_colo_enable(s->to_dst_file);
3555 }
3556
9907e842 3557 qemu_savevm_state_setup(s->to_dst_file);
0d82d0e8 3558
d05de9e3 3559 if (qemu_savevm_state_guest_unplug_pending()) {
c7e0acd5
JF
3560 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3561 MIGRATION_STATUS_WAIT_UNPLUG);
3562
3563 while (s->state == MIGRATION_STATUS_WAIT_UNPLUG &&
3564 qemu_savevm_state_guest_unplug_pending()) {
3565 qemu_sem_timedwait(&s->wait_unplug_sem, 250);
3566 }
3567
3568 migrate_set_state(&s->state, MIGRATION_STATUS_WAIT_UNPLUG,
3569 MIGRATION_STATUS_ACTIVE);
3570 }
3571
bc72ad67 3572 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
48781e5b
HZ
3573 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3574 MIGRATION_STATUS_ACTIVE);
29ae8a41 3575
9ec055ae
DDAG
3576 trace_migration_thread_setup_complete();
3577
8f8d528e 3578 while (migration_is_active(s)) {
ad767bed 3579 if (urgent || !qemu_file_rate_limit(s->to_dst_file)) {
2ad87305
PX
3580 MigIterateState iter_state = migration_iteration_run(s);
3581 if (iter_state == MIG_ITERATE_SKIP) {
3582 continue;
3583 } else if (iter_state == MIG_ITERATE_BREAK) {
09f6c85e 3584 break;
c369f40d
JQ
3585 }
3586 }
f4410a5d 3587
b23c2ade
PX
3588 /*
3589 * Try to detect any kind of failures, and see whether we
3590 * should stop the migration now.
3591 */
3592 thr_error = migration_detect_error(s);
3593 if (thr_error == MIG_THR_ERR_FATAL) {
3594 /* Stop migration */
fd45ee2c 3595 break;
b23c2ade
PX
3596 } else if (thr_error == MIG_THR_ERR_RECOVERED) {
3597 /*
3598 * Just recovered from a e.g. network failure, reset all
3599 * the local variables. This is important to avoid
3600 * breaking transferred_bytes and bandwidth calculation
3601 */
87f3bd87 3602 update_iteration_initial_status(s);
fd45ee2c 3603 }
b15df1ae 3604
97e1e067 3605 urgent = migration_rate_limit();
a3fa1d78
PB
3606 }
3607
1d34e4bf 3608 trace_migration_thread_after_loop();
199aa6d4 3609 migration_iteration_finish(s);
892ae715 3610 object_unref(OBJECT(s));
ab28bd23 3611 rcu_unregister_thread();
0d82d0e8
JQ
3612 return NULL;
3613}
3614
cce8040b 3615void migrate_fd_connect(MigrationState *s, Error *error_in)
0d82d0e8 3616{
00f4b572 3617 Error *local_err = NULL;
d3e35b8f
PX
3618 int64_t rate_limit;
3619 bool resume = s->state == MIGRATION_STATUS_POSTCOPY_PAUSED;
3620
2ff30257 3621 s->expected_downtime = s->parameters.downtime_limit;
9cbc3649
MAL
3622 if (resume) {
3623 assert(s->cleanup_bh);
3624 } else {
3625 assert(!s->cleanup_bh);
3626 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup_bh, s);
3627 }
cce8040b
DDAG
3628 if (error_in) {
3629 migrate_fd_error(s, error_in);
3630 migrate_fd_cleanup(s);
3631 return;
3632 }
0d82d0e8 3633
d3e35b8f
PX
3634 if (resume) {
3635 /* This is a resumed migration */
8504ddec
PX
3636 rate_limit = s->parameters.max_postcopy_bandwidth /
3637 XFER_LIMIT_RATIO;
d3e35b8f
PX
3638 } else {
3639 /* This is a fresh new migration */
3640 rate_limit = s->parameters.max_bandwidth / XFER_LIMIT_RATIO;
442773ce 3641
d3e35b8f
PX
3642 /* Notify before starting migration thread */
3643 notifier_list_notify(&migration_state_notifiers, s);
3644 }
3645
3646 qemu_file_set_rate_limit(s->to_dst_file, rate_limit);
3647 qemu_file_set_blocking(s->to_dst_file, true);
9287ac27 3648
1d34e4bf 3649 /*
c788ada8
PX
3650 * Open the return path. For postcopy, it is used exclusively. For
3651 * precopy, only if user specified "return-path" capability would
3652 * QEMU uses the return path.
1d34e4bf 3653 */
c788ada8 3654 if (migrate_postcopy_ram() || migrate_use_return_path()) {
d3e35b8f 3655 if (open_return_path_on_source(s, !resume)) {
1d34e4bf 3656 error_report("Unable to open return-path for postcopy");
d3e35b8f 3657 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
1d34e4bf
DDAG
3658 migrate_fd_cleanup(s);
3659 return;
3660 }
3661 }
3662
d3e35b8f 3663 if (resume) {
135b87b4
PX
3664 /* Wakeup the main migration thread to do the recovery */
3665 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
3666 MIGRATION_STATUS_POSTCOPY_RECOVER);
3667 qemu_sem_post(&s->postcopy_pause_sem);
d3e35b8f
PX
3668 return;
3669 }
3670
00f4b572
JQ
3671 if (multifd_save_setup(&local_err) != 0) {
3672 error_report_err(local_err);
f986c3d2
JQ
3673 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3674 MIGRATION_STATUS_FAILED);
3675 migrate_fd_cleanup(s);
3676 return;
3677 }
009fad7f 3678 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
bb1fadc4 3679 QEMU_THREAD_JOINABLE);
1d34e4bf 3680 s->migration_thread_running = true;
0d82d0e8 3681}
093e3c42 3682
9d18af93
PX
3683void migration_global_dump(Monitor *mon)
3684{
3685 MigrationState *ms = migrate_get_current();
3686
6f0f6428
JQ
3687 monitor_printf(mon, "globals:\n");
3688 monitor_printf(mon, "store-global-state: %s\n",
3689 ms->store_global_state ? "on" : "off");
3690 monitor_printf(mon, "only-migratable: %s\n",
811f8652 3691 only_migratable ? "on" : "off");
6f0f6428
JQ
3692 monitor_printf(mon, "send-configuration: %s\n",
3693 ms->send_configuration ? "on" : "off");
3694 monitor_printf(mon, "send-section-footer: %s\n",
3695 ms->send_section_footer ? "on" : "off");
f548222c
XG
3696 monitor_printf(mon, "decompress-error-check: %s\n",
3697 ms->decompress_error_check ? "on" : "off");
002cad6b
PX
3698 monitor_printf(mon, "clear-bitmap-shift: %u\n",
3699 ms->clear_bitmap_shift);
9d18af93
PX
3700}
3701
20814758
PX
3702#define DEFINE_PROP_MIG_CAP(name, x) \
3703 DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
3704
5272298c
PX
3705static Property migration_properties[] = {
3706 DEFINE_PROP_BOOL("store-global-state", MigrationState,
3707 store_global_state, true),
71dd4c1a
PX
3708 DEFINE_PROP_BOOL("send-configuration", MigrationState,
3709 send_configuration, true),
15c38503
PX
3710 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
3711 send_section_footer, true),
f548222c
XG
3712 DEFINE_PROP_BOOL("decompress-error-check", MigrationState,
3713 decompress_error_check, true),
002cad6b
PX
3714 DEFINE_PROP_UINT8("x-clear-bitmap-shift", MigrationState,
3715 clear_bitmap_shift, CLEAR_BITMAP_SHIFT_DEFAULT),
89632faf
PX
3716
3717 /* Migration parameters */
741d4086 3718 DEFINE_PROP_UINT8("x-compress-level", MigrationState,
89632faf
PX
3719 parameters.compress_level,
3720 DEFAULT_MIGRATE_COMPRESS_LEVEL),
741d4086 3721 DEFINE_PROP_UINT8("x-compress-threads", MigrationState,
89632faf
PX
3722 parameters.compress_threads,
3723 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
1d58872a
XG
3724 DEFINE_PROP_BOOL("x-compress-wait-thread", MigrationState,
3725 parameters.compress_wait_thread, true),
741d4086 3726 DEFINE_PROP_UINT8("x-decompress-threads", MigrationState,
89632faf
PX
3727 parameters.decompress_threads,
3728 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
dc14a470
KZ
3729 DEFINE_PROP_UINT8("x-throttle-trigger-threshold", MigrationState,
3730 parameters.throttle_trigger_threshold,
3731 DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD),
741d4086 3732 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
89632faf
PX
3733 parameters.cpu_throttle_initial,
3734 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
741d4086 3735 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
89632faf
PX
3736 parameters.cpu_throttle_increment,
3737 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
cbbf8182
KZ
3738 DEFINE_PROP_BOOL("x-cpu-throttle-tailslow", MigrationState,
3739 parameters.cpu_throttle_tailslow, false),
741d4086 3740 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
89632faf 3741 parameters.max_bandwidth, MAX_THROTTLE),
741d4086 3742 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
89632faf
PX
3743 parameters.downtime_limit,
3744 DEFAULT_MIGRATE_SET_DOWNTIME),
741d4086 3745 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
89632faf
PX
3746 parameters.x_checkpoint_delay,
3747 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
cbfd6c95
JQ
3748 DEFINE_PROP_UINT8("multifd-channels", MigrationState,
3749 parameters.multifd_channels,
4075fb1c 3750 DEFAULT_MIGRATE_MULTIFD_CHANNELS),
96eef042
JQ
3751 DEFINE_PROP_MULTIFD_COMPRESSION("multifd-compression", MigrationState,
3752 parameters.multifd_compression,
3753 DEFAULT_MIGRATE_MULTIFD_COMPRESSION),
9004db48
JQ
3754 DEFINE_PROP_UINT8("multifd-zlib-level", MigrationState,
3755 parameters.multifd_zlib_level,
3756 DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL),
6a9ad154
JQ
3757 DEFINE_PROP_UINT8("multifd-zstd-level", MigrationState,
3758 parameters.multifd_zstd_level,
3759 DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL),
73af8dd8
JQ
3760 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
3761 parameters.xbzrle_cache_size,
3762 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
7e555c6c
DDAG
3763 DEFINE_PROP_SIZE("max-postcopy-bandwidth", MigrationState,
3764 parameters.max_postcopy_bandwidth,
3765 DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH),
4cbc9c7f
LQ
3766 DEFINE_PROP_UINT8("max-cpu-throttle", MigrationState,
3767 parameters.max_cpu_throttle,
3768 DEFAULT_MIGRATE_MAX_CPU_THROTTLE),
ee3d96ba
DDAG
3769 DEFINE_PROP_SIZE("announce-initial", MigrationState,
3770 parameters.announce_initial,
3771 DEFAULT_MIGRATE_ANNOUNCE_INITIAL),
3772 DEFINE_PROP_SIZE("announce-max", MigrationState,
3773 parameters.announce_max,
3774 DEFAULT_MIGRATE_ANNOUNCE_MAX),
3775 DEFINE_PROP_SIZE("announce-rounds", MigrationState,
3776 parameters.announce_rounds,
3777 DEFAULT_MIGRATE_ANNOUNCE_ROUNDS),
3778 DEFINE_PROP_SIZE("announce-step", MigrationState,
3779 parameters.announce_step,
3780 DEFAULT_MIGRATE_ANNOUNCE_STEP),
20814758
PX
3781
3782 /* Migration capabilities */
3783 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
3784 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
3785 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
3786 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
3787 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
3788 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
3789 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
3790 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
3791 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
3792 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
3793 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
cbfd6c95 3794 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_MULTIFD),
20814758 3795
5272298c
PX
3796 DEFINE_PROP_END_OF_LIST(),
3797};
3798
e5cb7e76
PX
3799static void migration_class_init(ObjectClass *klass, void *data)
3800{
3801 DeviceClass *dc = DEVICE_CLASS(klass);
3802
3803 dc->user_creatable = false;
4f67d30b 3804 device_class_set_props(dc, migration_properties);
e5cb7e76
PX
3805}
3806
b91bf5e4
MAL
3807static void migration_instance_finalize(Object *obj)
3808{
3809 MigrationState *ms = MIGRATION_OBJ(obj);
3810 MigrationParameters *params = &ms->parameters;
3811
87db1a7d 3812 qemu_mutex_destroy(&ms->error_mutex);
62df066f 3813 qemu_mutex_destroy(&ms->qemu_file_lock);
b91bf5e4
MAL
3814 g_free(params->tls_hostname);
3815 g_free(params->tls_creds);
c7e0acd5 3816 qemu_sem_destroy(&ms->wait_unplug_sem);
ad767bed 3817 qemu_sem_destroy(&ms->rate_limit_sem);
e91d8951 3818 qemu_sem_destroy(&ms->pause_sem);
b23c2ade 3819 qemu_sem_destroy(&ms->postcopy_pause_sem);
14b1742e 3820 qemu_sem_destroy(&ms->postcopy_pause_rp_sem);
edd090c7 3821 qemu_sem_destroy(&ms->rp_state.rp_sem);
ab105cc1 3822 error_free(ms->error);
b91bf5e4
MAL
3823}
3824
e5cb7e76
PX
3825static void migration_instance_init(Object *obj)
3826{
3827 MigrationState *ms = MIGRATION_OBJ(obj);
8b0b29dc 3828 MigrationParameters *params = &ms->parameters;
e5cb7e76
PX
3829
3830 ms->state = MIGRATION_STATUS_NONE;
e5cb7e76 3831 ms->mbps = -1;
aecbfe9c 3832 ms->pages_per_second = -1;
e91d8951 3833 qemu_sem_init(&ms->pause_sem, 0);
87db1a7d 3834 qemu_mutex_init(&ms->error_mutex);
8b0b29dc
PX
3835
3836 params->tls_hostname = g_strdup("");
3837 params->tls_creds = g_strdup("");
3838
3839 /* Set has_* up only for parameter checks */
3840 params->has_compress_level = true;
3841 params->has_compress_threads = true;
3842 params->has_decompress_threads = true;
dc14a470 3843 params->has_throttle_trigger_threshold = true;
8b0b29dc
PX
3844 params->has_cpu_throttle_initial = true;
3845 params->has_cpu_throttle_increment = true;
cbbf8182 3846 params->has_cpu_throttle_tailslow = true;
8b0b29dc
PX
3847 params->has_max_bandwidth = true;
3848 params->has_downtime_limit = true;
3849 params->has_x_checkpoint_delay = true;
3850 params->has_block_incremental = true;
cbfd6c95 3851 params->has_multifd_channels = true;
96eef042 3852 params->has_multifd_compression = true;
9004db48 3853 params->has_multifd_zlib_level = true;
6a9ad154 3854 params->has_multifd_zstd_level = true;
73af8dd8 3855 params->has_xbzrle_cache_size = true;
7e555c6c 3856 params->has_max_postcopy_bandwidth = true;
4cbc9c7f 3857 params->has_max_cpu_throttle = true;
ee3d96ba
DDAG
3858 params->has_announce_initial = true;
3859 params->has_announce_max = true;
3860 params->has_announce_rounds = true;
3861 params->has_announce_step = true;
b23c2ade
PX
3862
3863 qemu_sem_init(&ms->postcopy_pause_sem, 0);
14b1742e 3864 qemu_sem_init(&ms->postcopy_pause_rp_sem, 0);
edd090c7 3865 qemu_sem_init(&ms->rp_state.rp_sem, 0);
ad767bed 3866 qemu_sem_init(&ms->rate_limit_sem, 0);
c7e0acd5 3867 qemu_sem_init(&ms->wait_unplug_sem, 0);
62df066f 3868 qemu_mutex_init(&ms->qemu_file_lock);
8b0b29dc
PX
3869}
3870
3871/*
3872 * Return true if check pass, false otherwise. Error will be put
3873 * inside errp if provided.
3874 */
3875static bool migration_object_check(MigrationState *ms, Error **errp)
3876{
6b19a7d9
PX
3877 MigrationCapabilityStatusList *head = NULL;
3878 /* Assuming all off */
3879 bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret;
3880 int i;
3881
8b0b29dc
PX
3882 if (!migrate_params_check(&ms->parameters, errp)) {
3883 return false;
3884 }
3885
6b19a7d9
PX
3886 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
3887 if (ms->enabled_capabilities[i]) {
eaedde52 3888 QAPI_LIST_PREPEND(head, migrate_cap_add(i, true));
6b19a7d9
PX
3889 }
3890 }
3891
3892 ret = migrate_caps_check(cap_list, head, errp);
3893
3894 /* It works with head == NULL */
3895 qapi_free_MigrationCapabilityStatusList(head);
3896
3897 return ret;
e5cb7e76
PX
3898}
3899
3900static const TypeInfo migration_type = {
3901 .name = TYPE_MIGRATION,
01f6e14c 3902 /*
c8d3ff38 3903 * NOTE: TYPE_MIGRATION is not really a device, as the object is
2194abd6 3904 * not created using qdev_new(), it is not attached to the qdev
c8d3ff38
PX
3905 * device tree, and it is never realized.
3906 *
3907 * TODO: Make this TYPE_OBJECT once QOM provides something like
3908 * TYPE_DEVICE's "-global" properties.
01f6e14c 3909 */
e5cb7e76
PX
3910 .parent = TYPE_DEVICE,
3911 .class_init = migration_class_init,
3912 .class_size = sizeof(MigrationClass),
3913 .instance_size = sizeof(MigrationState),
3914 .instance_init = migration_instance_init,
b91bf5e4 3915 .instance_finalize = migration_instance_finalize,
e5cb7e76
PX
3916};
3917
3918static void register_migration_types(void)
3919{
3920 type_register_static(&migration_type);
3921}
3922
3923type_init(register_migration_types);
This page took 1.471436 seconds and 4 git commands to generate.