]> git.proxmox.com Git - mirror_qemu.git/blame - migration/migration.c
migration: new state "postcopy-recover"
[mirror_qemu.git] / migration / migration.c
CommitLineData
5bb7910a
AL
1/*
2 * QEMU live migration
3 *
4 * Copyright IBM, Corp. 2008
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
6b620ca3
PB
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
5bb7910a
AL
14 */
15
1393a485 16#include "qemu/osdep.h"
f348b6d1 17#include "qemu/cutils.h"
d49b6836 18#include "qemu/error-report.h"
795c40b8 19#include "migration/blocker.h"
f4dbe1bf 20#include "exec.h"
7fcac4a2 21#include "fd.h"
61e8b148 22#include "socket.h"
e1a3ecee 23#include "rdma.h"
7b1e1a22 24#include "ram.h"
84a899de 25#include "migration/global_state.h"
c4b63b7c 26#include "migration/misc.h"
6666c96a 27#include "migration.h"
20a519a0 28#include "savevm.h"
40014d81 29#include "qemu-file-channel.h"
08a0aee1 30#include "qemu-file.h"
987772d9 31#include "migration/vmstate.h"
737e150e 32#include "block/block.h"
e688df6b 33#include "qapi/error.h"
9af23989
MA
34#include "qapi/qapi-commands-migration.h"
35#include "qapi/qapi-events-migration.h"
cc7a8ea7 36#include "qapi/qmp/qerror.h"
15280c36 37#include "qapi/qmp/qnull.h"
ab28bd23 38#include "qemu/rcu.h"
2c9e6fec 39#include "block.h"
be07b0ac 40#include "postcopy-ram.h"
766bd176 41#include "qemu/thread.h"
c09e5bb1 42#include "trace.h"
51180423 43#include "exec/target_page.h"
61b67d47 44#include "io/channel-buffer.h"
35a6ed4f 45#include "migration/colo.h"
4ffdb337 46#include "hw/boards.h"
9d18af93 47#include "monitor/monitor.h"
065e2813 48
dc325627 49#define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
5bb7910a 50
5b4e1eb7
JQ
51/* Amount of time to allocate to each "chunk" of bandwidth-throttled
52 * data. */
53#define BUFFER_DELAY 100
54#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
55
2ff30257
AA
56/* Time in milliseconds we are allowed to stop the source,
57 * for sending the last part */
58#define DEFAULT_MIGRATE_SET_DOWNTIME 300
59
87c9cc1c
DHB
60/* Maximum migrate downtime set to 2000 seconds */
61#define MAX_MIGRATE_DOWNTIME_SECONDS 2000
62#define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
63
8706d2d5
LL
64/* Default compression thread count */
65#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
3fcb38c2
LL
66/* Default decompression thread count, usually decompression is at
67 * least 4 times as fast as compression.*/
68#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
8706d2d5
LL
69/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
70#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
1626fee3 71/* Define default autoconverge cpu throttle migration parameters */
d85a31d1
JH
72#define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
73#define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
8706d2d5 74
17ad9b35 75/* Migration XBZRLE default cache size */
73af8dd8 76#define DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE (64 * 1024 * 1024)
17ad9b35 77
68b53591
HZ
78/* The delay time (in ms) between two COLO checkpoints
79 * Note: Please change this default value to 10000 when we support hybrid mode.
80 */
81#define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY 200
4075fb1c 82#define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2
0fb86605 83#define DEFAULT_MIGRATE_MULTIFD_PAGE_COUNT 16
68b53591 84
99a0db9b
GH
85static NotifierList migration_state_notifiers =
86 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
87
adde220a
DDAG
88static bool deferred_incoming;
89
da6f1790
JQ
90/* Messages sent on the return path from destination to source */
91enum mig_rp_message_type {
92 MIG_RP_MSG_INVALID = 0, /* Must be 0 */
93 MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */
94 MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */
95
96 MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */
97 MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */
98
99 MIG_RP_MSG_MAX
100};
101
17549e84
JQ
102/* When we add fault tolerance, we could have several
103 migrations at once. For now we don't need to add
104 dynamic creation of migration */
105
e5cb7e76
PX
106static MigrationState *current_migration;
107
8b0b29dc 108static bool migration_object_check(MigrationState *ms, Error **errp);
0331c8ca
DDAG
109static int migration_maybe_pause(MigrationState *s,
110 int *current_active_state,
111 int new_state);
8b0b29dc 112
e5cb7e76
PX
113void migration_object_init(void)
114{
4ffdb337 115 MachineState *ms = MACHINE(qdev_get_machine());
8b0b29dc 116 Error *err = NULL;
4ffdb337 117
e5cb7e76
PX
118 /* This can only be called once. */
119 assert(!current_migration);
120 current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION));
4ffdb337 121
8b0b29dc
PX
122 if (!migration_object_check(current_migration, &err)) {
123 error_report_err(err);
124 exit(1);
125 }
126
4ffdb337
PX
127 /*
128 * We cannot really do this in migration_instance_init() since at
129 * that time global properties are not yet applied, then this
130 * value will be definitely replaced by something else.
131 */
132 if (ms->enforce_config_section) {
133 current_migration->send_configuration = true;
134 }
e5cb7e76
PX
135}
136
1f895604
VSO
137void migration_object_finalize(void)
138{
139 object_unref(OBJECT(current_migration));
140}
141
bca7856a 142/* For outgoing */
859bc756 143MigrationState *migrate_get_current(void)
17549e84 144{
e5cb7e76
PX
145 /* This can only be called after the object created. */
146 assert(current_migration);
147 return current_migration;
17549e84
JQ
148}
149
bca7856a
DDAG
150MigrationIncomingState *migration_incoming_get_current(void)
151{
b4b076da
JQ
152 static bool once;
153 static MigrationIncomingState mis_current;
bca7856a 154
b4b076da
JQ
155 if (!once) {
156 mis_current.state = MIGRATION_STATUS_NONE;
157 memset(&mis_current, 0, sizeof(MigrationIncomingState));
00fa4fc8
DDAG
158 mis_current.postcopy_remote_fds = g_array_new(FALSE, TRUE,
159 sizeof(struct PostCopyFD));
b4b076da
JQ
160 qemu_mutex_init(&mis_current.rp_mutex);
161 qemu_event_init(&mis_current.main_thread_load_event, false);
b411b844 162 qemu_sem_init(&mis_current.postcopy_pause_sem_dst, 0);
3a7804c3 163 qemu_sem_init(&mis_current.postcopy_pause_sem_fault, 0);
b35ebdf0
VSO
164
165 init_dirty_bitmap_incoming_migration();
166
b4b076da
JQ
167 once = true;
168 }
169 return &mis_current;
bca7856a
DDAG
170}
171
172void migration_incoming_state_destroy(void)
173{
b4b076da
JQ
174 struct MigrationIncomingState *mis = migration_incoming_get_current();
175
3482655b 176 if (mis->to_src_file) {
660819b1
PX
177 /* Tell source that we are done */
178 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
3482655b
PX
179 qemu_fclose(mis->to_src_file);
180 mis->to_src_file = NULL;
181 }
182
660819b1
PX
183 if (mis->from_src_file) {
184 qemu_fclose(mis->from_src_file);
185 mis->from_src_file = NULL;
186 }
00fa4fc8
DDAG
187 if (mis->postcopy_remote_fds) {
188 g_array_free(mis->postcopy_remote_fds, TRUE);
189 mis->postcopy_remote_fds = NULL;
190 }
660819b1 191
5089e186 192 qemu_event_reset(&mis->main_thread_load_event);
bca7856a
DDAG
193}
194
b05dc723
JQ
195static void migrate_generate_event(int new_state)
196{
197 if (migrate_use_events()) {
198 qapi_event_send_migration(new_state, &error_abort);
b05dc723
JQ
199 }
200}
201
adde220a
DDAG
202/*
203 * Called on -incoming with a defer: uri.
204 * The migration can be started later after any parameters have been
205 * changed.
206 */
207static void deferred_incoming_migration(Error **errp)
208{
209 if (deferred_incoming) {
210 error_setg(errp, "Incoming migration already deferred");
211 }
212 deferred_incoming = true;
213}
214
da6f1790
JQ
215/*
216 * Send a message on the return channel back to the source
217 * of the migration.
218 */
d6208e35
PX
219static int migrate_send_rp_message(MigrationIncomingState *mis,
220 enum mig_rp_message_type message_type,
221 uint16_t len, void *data)
da6f1790 222{
d6208e35
PX
223 int ret = 0;
224
da6f1790
JQ
225 trace_migrate_send_rp_message((int)message_type, len);
226 qemu_mutex_lock(&mis->rp_mutex);
d6208e35
PX
227
228 /*
229 * It's possible that the file handle got lost due to network
230 * failures.
231 */
232 if (!mis->to_src_file) {
233 ret = -EIO;
234 goto error;
235 }
236
da6f1790
JQ
237 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
238 qemu_put_be16(mis->to_src_file, len);
239 qemu_put_buffer(mis->to_src_file, data, len);
240 qemu_fflush(mis->to_src_file);
d6208e35
PX
241
242 /* It's possible that qemu file got error during sending */
243 ret = qemu_file_get_error(mis->to_src_file);
244
245error:
da6f1790 246 qemu_mutex_unlock(&mis->rp_mutex);
d6208e35 247 return ret;
da6f1790
JQ
248}
249
1e2d90eb
DDAG
250/* Request a range of pages from the source VM at the given
251 * start address.
252 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
253 * as the last request (a name must have been given previously)
254 * Start: Address offset within the RB
255 * Len: Length in bytes required - must be a multiple of pagesize
256 */
d6208e35
PX
257int migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
258 ram_addr_t start, size_t len)
1e2d90eb 259{
cb8d4c8f 260 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
1e2d90eb 261 size_t msglen = 12; /* start + len */
d6208e35 262 enum mig_rp_message_type msg_type;
1e2d90eb
DDAG
263
264 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
265 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
266
267 if (rbname) {
268 int rbname_len = strlen(rbname);
269 assert(rbname_len < 256);
270
271 bufc[msglen++] = rbname_len;
272 memcpy(bufc + msglen, rbname, rbname_len);
273 msglen += rbname_len;
d6208e35 274 msg_type = MIG_RP_MSG_REQ_PAGES_ID;
1e2d90eb 275 } else {
d6208e35 276 msg_type = MIG_RP_MSG_REQ_PAGES;
1e2d90eb 277 }
d6208e35
PX
278
279 return migrate_send_rp_message(mis, msg_type, msglen, bufc);
1e2d90eb
DDAG
280}
281
43eaae28 282void qemu_start_incoming_migration(const char *uri, Error **errp)
5bb7910a 283{
34c9dd8e
AL
284 const char *p;
285
7cf1fe6d 286 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
adde220a
DDAG
287 if (!strcmp(uri, "defer")) {
288 deferred_incoming_migration(errp);
289 } else if (strstart(uri, "tcp:", &p)) {
43eaae28 290 tcp_start_incoming_migration(p, errp);
2da776db 291#ifdef CONFIG_RDMA
adde220a 292 } else if (strstart(uri, "rdma:", &p)) {
2da776db
MH
293 rdma_start_incoming_migration(p, errp);
294#endif
adde220a 295 } else if (strstart(uri, "exec:", &p)) {
43eaae28 296 exec_start_incoming_migration(p, errp);
adde220a 297 } else if (strstart(uri, "unix:", &p)) {
43eaae28 298 unix_start_incoming_migration(p, errp);
adde220a 299 } else if (strstart(uri, "fd:", &p)) {
43eaae28 300 fd_start_incoming_migration(p, errp);
adde220a 301 } else {
312fd5f2 302 error_setg(errp, "unknown migration protocol: %s", uri);
8ca5e801 303 }
5bb7910a
AL
304}
305
0aa6aefc
DL
306static void process_incoming_migration_bh(void *opaque)
307{
308 Error *local_err = NULL;
309 MigrationIncomingState *mis = opaque;
310
a18a73d7
DDAG
311 /* Make sure all file formats flush their mutable metadata.
312 * If we get an error here, just don't restart the VM yet. */
313 bdrv_invalidate_cache_all(&local_err);
314 if (local_err) {
315 error_report_err(local_err);
316 local_err = NULL;
317 autostart = false;
d35ff5e6
KW
318 }
319
0aa6aefc
DL
320 /*
321 * This must happen after all error conditions are dealt with and
322 * we're sure the VM is going to be running on this host.
323 */
324 qemu_announce_self();
325
f986c3d2
JQ
326 if (multifd_load_cleanup(&local_err) != 0) {
327 error_report_err(local_err);
328 autostart = false;
329 }
0aa6aefc
DL
330 /* If global state section was not received or we are in running
331 state, we need to obey autostart. Any other state is set with
332 runstate_set. */
333
b35ebdf0
VSO
334 dirty_bitmap_mig_before_vm_start();
335
0aa6aefc
DL
336 if (!global_state_received() ||
337 global_state_get_runstate() == RUN_STATE_RUNNING) {
338 if (autostart) {
339 vm_start();
340 } else {
341 runstate_set(RUN_STATE_PAUSED);
342 }
343 } else {
344 runstate_set(global_state_get_runstate());
345 }
0aa6aefc
DL
346 /*
347 * This must happen after any state changes since as soon as an external
348 * observer sees this event they might start to prod at the VM assuming
349 * it's ready to use.
350 */
351 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
352 MIGRATION_STATUS_COMPLETED);
353 qemu_bh_delete(mis->bh);
354 migration_incoming_state_destroy();
355}
356
82a4da79 357static void process_incoming_migration_co(void *opaque)
511c0231 358{
b4b076da 359 MigrationIncomingState *mis = migration_incoming_get_current();
e9bef235 360 PostcopyState ps;
1c12e1f5
PB
361 int ret;
362
4f0fae7f 363 assert(mis->from_src_file);
67f11b5c 364 mis->largest_page_size = qemu_ram_pagesize_largest();
093e3c42 365 postcopy_state_set(POSTCOPY_INCOMING_NONE);
93d7af6f
HZ
366 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
367 MIGRATION_STATUS_ACTIVE);
4f0fae7f 368 ret = qemu_loadvm_state(mis->from_src_file);
bca7856a 369
e9bef235
DDAG
370 ps = postcopy_state_get();
371 trace_process_incoming_migration_co_end(ret, ps);
372 if (ps != POSTCOPY_INCOMING_NONE) {
373 if (ps == POSTCOPY_INCOMING_ADVISE) {
374 /*
375 * Where a migration had postcopy enabled (and thus went to advise)
376 * but managed to complete within the precopy period, we can use
377 * the normal exit.
378 */
379 postcopy_ram_incoming_cleanup(mis);
380 } else if (ret >= 0) {
381 /*
382 * Postcopy was started, cleanup should happen at the end of the
383 * postcopy thread.
384 */
385 trace_process_incoming_migration_co_postcopy_end_main();
386 return;
387 }
388 /* Else if something went wrong then just fall out of the normal exit */
389 }
390
25d0c16f
HZ
391 /* we get COLO info, and know if we are in COLO mode */
392 if (!ret && migration_incoming_enable_colo()) {
393 mis->migration_incoming_co = qemu_coroutine_self();
394 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
395 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
396 mis->have_colo_incoming_thread = true;
397 qemu_coroutine_yield();
398
399 /* Wait checkpoint incoming thread exit before free resource */
400 qemu_thread_join(&mis->colo_incoming_thread);
401 }
402
1c12e1f5 403 if (ret < 0) {
f986c3d2
JQ
404 Error *local_err = NULL;
405
93d7af6f
HZ
406 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
407 MIGRATION_STATUS_FAILED);
db80face 408 error_report("load of migration failed: %s", strerror(-ret));
3a0f2cea 409 qemu_fclose(mis->from_src_file);
f986c3d2
JQ
410 if (multifd_load_cleanup(&local_err) != 0) {
411 error_report_err(local_err);
412 }
4aead692 413 exit(EXIT_FAILURE);
511c0231 414 }
0aa6aefc
DL
415 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
416 qemu_bh_schedule(mis->bh);
511c0231
JQ
417}
418
e595a01a 419static void migration_incoming_setup(QEMUFile *f)
82a4da79 420{
4f0fae7f 421 MigrationIncomingState *mis = migration_incoming_get_current();
82a4da79 422
f986c3d2
JQ
423 if (multifd_load_setup() != 0) {
424 /* We haven't been able to create multifd threads
425 nothing better to do */
426 exit(EXIT_FAILURE);
427 }
428
4f0fae7f
JQ
429 if (!mis->from_src_file) {
430 mis->from_src_file = f;
431 }
06ad5135 432 qemu_file_set_blocking(f, false);
e595a01a
JQ
433}
434
36c2f8be 435void migration_incoming_process(void)
e595a01a
JQ
436{
437 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL);
0b8b8753 438 qemu_coroutine_enter(co);
82a4da79
PB
439}
440
e595a01a
JQ
441void migration_fd_process_incoming(QEMUFile *f)
442{
443 migration_incoming_setup(f);
444 migration_incoming_process();
445}
446
4f0fae7f
JQ
447void migration_ioc_process_incoming(QIOChannel *ioc)
448{
449 MigrationIncomingState *mis = migration_incoming_get_current();
450
451 if (!mis->from_src_file) {
452 QEMUFile *f = qemu_fopen_channel_input(ioc);
36c2f8be 453 migration_incoming_setup(f);
71bb07db 454 return;
4f0fae7f 455 }
71bb07db 456 multifd_recv_new_channel(ioc);
4f0fae7f
JQ
457}
458
428d8908
JQ
459/**
460 * @migration_has_all_channels: We have received all channels that we need
461 *
462 * Returns true when we have got connections to all the channels that
463 * we need for migration.
464 */
465bool migration_has_all_channels(void)
466{
62c1e0ca
JQ
467 bool all_channels;
468
469 all_channels = multifd_recv_all_channels_created();
470
471 return all_channels;
428d8908
JQ
472}
473
6decec93
DDAG
474/*
475 * Send a 'SHUT' message on the return channel with the given value
476 * to indicate that we've finished with the RP. Non-0 value indicates
477 * error.
478 */
479void migrate_send_rp_shut(MigrationIncomingState *mis,
480 uint32_t value)
481{
482 uint32_t buf;
483
484 buf = cpu_to_be32(value);
485 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
486}
487
488/*
489 * Send a 'PONG' message on the return channel with the given value
490 * (normally in response to a 'PING')
491 */
492void migrate_send_rp_pong(MigrationIncomingState *mis,
493 uint32_t value)
494{
495 uint32_t buf;
496
497 buf = cpu_to_be32(value);
498 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
499}
500
bbf6da32
OW
501MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
502{
503 MigrationCapabilityStatusList *head = NULL;
504 MigrationCapabilityStatusList *caps;
505 MigrationState *s = migrate_get_current();
506 int i;
507
387eedeb 508 caps = NULL; /* silence compiler warning */
7fb1cf16 509 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
ed1701c6
DDAG
510#ifndef CONFIG_LIVE_BLOCK_MIGRATION
511 if (i == MIGRATION_CAPABILITY_BLOCK) {
512 continue;
513 }
514#endif
bbf6da32
OW
515 if (head == NULL) {
516 head = g_malloc0(sizeof(*caps));
517 caps = head;
518 } else {
519 caps->next = g_malloc0(sizeof(*caps));
520 caps = caps->next;
521 }
522 caps->value =
523 g_malloc(sizeof(*caps->value));
524 caps->value->capability = i;
525 caps->value->state = s->enabled_capabilities[i];
526 }
527
528 return head;
529}
530
85de8323
LL
531MigrationParameters *qmp_query_migrate_parameters(Error **errp)
532{
533 MigrationParameters *params;
534 MigrationState *s = migrate_get_current();
535
e87fae4c 536 /* TODO use QAPI_CLONE() instead of duplicating it inline */
85de8323 537 params = g_malloc0(sizeof(*params));
de63ab61 538 params->has_compress_level = true;
2594f56d 539 params->compress_level = s->parameters.compress_level;
de63ab61 540 params->has_compress_threads = true;
2594f56d 541 params->compress_threads = s->parameters.compress_threads;
de63ab61 542 params->has_decompress_threads = true;
2594f56d 543 params->decompress_threads = s->parameters.decompress_threads;
de63ab61 544 params->has_cpu_throttle_initial = true;
2594f56d 545 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
de63ab61 546 params->has_cpu_throttle_increment = true;
2594f56d 547 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
8cc99dcd 548 params->has_tls_creds = true;
69ef1f36 549 params->tls_creds = g_strdup(s->parameters.tls_creds);
8cc99dcd 550 params->has_tls_hostname = true;
69ef1f36 551 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
2ff30257
AA
552 params->has_max_bandwidth = true;
553 params->max_bandwidth = s->parameters.max_bandwidth;
554 params->has_downtime_limit = true;
555 params->downtime_limit = s->parameters.downtime_limit;
fe39a4d4 556 params->has_x_checkpoint_delay = true;
68b53591 557 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
2833c59b
JQ
558 params->has_block_incremental = true;
559 params->block_incremental = s->parameters.block_incremental;
4075fb1c
JQ
560 params->has_x_multifd_channels = true;
561 params->x_multifd_channels = s->parameters.x_multifd_channels;
0fb86605
JQ
562 params->has_x_multifd_page_count = true;
563 params->x_multifd_page_count = s->parameters.x_multifd_page_count;
73af8dd8
JQ
564 params->has_xbzrle_cache_size = true;
565 params->xbzrle_cache_size = s->parameters.xbzrle_cache_size;
85de8323
LL
566
567 return params;
568}
569
f6844b99
DDAG
570/*
571 * Return true if we're already in the middle of a migration
572 * (i.e. any of the active or setup states)
573 */
574static bool migration_is_setup_or_active(int state)
575{
576 switch (state) {
577 case MIGRATION_STATUS_ACTIVE:
9ec055ae 578 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
a688d2c1 579 case MIGRATION_STATUS_POSTCOPY_PAUSED:
135b87b4 580 case MIGRATION_STATUS_POSTCOPY_RECOVER:
f6844b99 581 case MIGRATION_STATUS_SETUP:
31e06077
DDAG
582 case MIGRATION_STATUS_PRE_SWITCHOVER:
583 case MIGRATION_STATUS_DEVICE:
f6844b99
DDAG
584 return true;
585
586 default:
587 return false;
588
589 }
590}
591
a22463a5
DDAG
592static void populate_ram_info(MigrationInfo *info, MigrationState *s)
593{
594 info->has_ram = true;
595 info->ram = g_malloc0(sizeof(*info->ram));
9360447d 596 info->ram->transferred = ram_counters.transferred;
a22463a5 597 info->ram->total = ram_bytes_total();
9360447d 598 info->ram->duplicate = ram_counters.duplicate;
bedf53c1
JQ
599 /* legacy value. It is not used anymore */
600 info->ram->skipped = 0;
9360447d
JQ
601 info->ram->normal = ram_counters.normal;
602 info->ram->normal_bytes = ram_counters.normal *
20afaed9 603 qemu_target_page_size();
a22463a5 604 info->ram->mbps = s->mbps;
9360447d
JQ
605 info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
606 info->ram->postcopy_requests = ram_counters.postcopy_requests;
030ce1f8 607 info->ram->page_size = qemu_target_page_size();
a22463a5 608
114f5aee
JQ
609 if (migrate_use_xbzrle()) {
610 info->has_xbzrle_cache = true;
611 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
612 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
9360447d
JQ
613 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
614 info->xbzrle_cache->pages = xbzrle_counters.pages;
615 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
616 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
617 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
114f5aee
JQ
618 }
619
338182c8
JQ
620 if (cpu_throttle_active()) {
621 info->has_cpu_throttle_percentage = true;
622 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
623 }
624
a22463a5
DDAG
625 if (s->state != MIGRATION_STATUS_COMPLETED) {
626 info->ram->remaining = ram_bytes_remaining();
9360447d 627 info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
a22463a5
DDAG
628 }
629}
630
930ac04c
JQ
631static void populate_disk_info(MigrationInfo *info)
632{
633 if (blk_mig_active()) {
634 info->has_disk = true;
635 info->disk = g_malloc0(sizeof(*info->disk));
636 info->disk->transferred = blk_mig_bytes_transferred();
637 info->disk->remaining = blk_mig_bytes_remaining();
638 info->disk->total = blk_mig_bytes_total();
639 }
640}
641
65ace060 642static void fill_source_migration_info(MigrationInfo *info)
5bb7910a 643{
17549e84
JQ
644 MigrationState *s = migrate_get_current();
645
646 switch (s->state) {
31194731 647 case MIGRATION_STATUS_NONE:
17549e84 648 /* no migration has happened ever */
65ace060
AP
649 /* do not overwrite destination migration status */
650 return;
17549e84 651 break;
31194731 652 case MIGRATION_STATUS_SETUP:
29ae8a41 653 info->has_status = true;
ed4fbd10 654 info->has_total_time = false;
29ae8a41 655 break;
31194731
HZ
656 case MIGRATION_STATUS_ACTIVE:
657 case MIGRATION_STATUS_CANCELLING:
9ec055ae 658 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
31e06077
DDAG
659 case MIGRATION_STATUS_PRE_SWITCHOVER:
660 case MIGRATION_STATUS_DEVICE:
a688d2c1 661 case MIGRATION_STATUS_POSTCOPY_PAUSED:
135b87b4 662 case MIGRATION_STATUS_POSTCOPY_RECOVER:
c8f9f4f4 663 /* TODO add some postcopy stats */
9ec055ae
DDAG
664 info->has_status = true;
665 info->has_total_time = true;
666 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
4af246a3 667 - s->start_time;
9ec055ae
DDAG
668 info->has_expected_downtime = true;
669 info->expected_downtime = s->expected_downtime;
670 info->has_setup_time = true;
671 info->setup_time = s->setup_time;
672
a22463a5 673 populate_ram_info(info, s);
930ac04c 674 populate_disk_info(info);
17549e84 675 break;
0b827d5e
HZ
676 case MIGRATION_STATUS_COLO:
677 info->has_status = true;
678 /* TODO: display COLO specific information (checkpoint info etc.) */
679 break;
31194731 680 case MIGRATION_STATUS_COMPLETED:
791e7c82 681 info->has_status = true;
00c14997 682 info->has_total_time = true;
7aa939af 683 info->total_time = s->total_time;
9c5a9fcf
JQ
684 info->has_downtime = true;
685 info->downtime = s->downtime;
ed4fbd10
MH
686 info->has_setup_time = true;
687 info->setup_time = s->setup_time;
d5f8a570 688
a22463a5 689 populate_ram_info(info, s);
17549e84 690 break;
31194731 691 case MIGRATION_STATUS_FAILED:
791e7c82 692 info->has_status = true;
d59ce6f3
DB
693 if (s->error) {
694 info->has_error_desc = true;
695 info->error_desc = g_strdup(error_get_pretty(s->error));
696 }
17549e84 697 break;
31194731 698 case MIGRATION_STATUS_CANCELLED:
791e7c82 699 info->has_status = true;
17549e84 700 break;
5bb7910a 701 }
cde63fbe 702 info->status = s->state;
5bb7910a
AL
703}
704
4a84214e
PX
705/**
706 * @migration_caps_check - check capability validity
707 *
708 * @cap_list: old capability list, array of bool
709 * @params: new capabilities to be applied soon
710 * @errp: set *errp if the check failed, with reason
711 *
712 * Returns true if check passed, otherwise false.
713 */
714static bool migrate_caps_check(bool *cap_list,
715 MigrationCapabilityStatusList *params,
716 Error **errp)
00458433 717{
00458433 718 MigrationCapabilityStatusList *cap;
4a84214e 719 bool old_postcopy_cap;
d7651f15 720 MigrationIncomingState *mis = migration_incoming_get_current();
00458433 721
4a84214e 722 old_postcopy_cap = cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM];
00458433
OW
723
724 for (cap = params; cap; cap = cap->next) {
4a84214e
PX
725 cap_list[cap->value->capability] = cap->value->state;
726 }
727
ed1701c6 728#ifndef CONFIG_LIVE_BLOCK_MIGRATION
4a84214e
PX
729 if (cap_list[MIGRATION_CAPABILITY_BLOCK]) {
730 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
731 "block migration");
732 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
733 return false;
00458433 734 }
4a84214e 735#endif
53dd370c 736
4a84214e
PX
737 if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
738 if (cap_list[MIGRATION_CAPABILITY_COMPRESS]) {
53dd370c
DDAG
739 /* The decompression threads asynchronously write into RAM
740 * rather than use the atomic copies needed to avoid
741 * userfaulting. It should be possible to fix the decompression
742 * threads for compatibility in future.
743 */
4a84214e
PX
744 error_setg(errp, "Postcopy is not currently compatible "
745 "with compression");
746 return false;
53dd370c 747 }
4a84214e 748
096631bd
DDAG
749 /* This check is reasonably expensive, so only when it's being
750 * set the first time, also it's only the destination that needs
751 * special support.
752 */
753 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
d7651f15 754 !postcopy_ram_supported_by_host(mis)) {
096631bd
DDAG
755 /* postcopy_ram_supported_by_host will have emitted a more
756 * detailed message
757 */
4a84214e
PX
758 error_setg(errp, "Postcopy is not supported");
759 return false;
096631bd 760 }
53dd370c 761 }
4a84214e
PX
762
763 return true;
764}
765
65ace060
AP
766static void fill_destination_migration_info(MigrationInfo *info)
767{
768 MigrationIncomingState *mis = migration_incoming_get_current();
769
770 switch (mis->state) {
771 case MIGRATION_STATUS_NONE:
772 return;
773 break;
774 case MIGRATION_STATUS_SETUP:
775 case MIGRATION_STATUS_CANCELLING:
776 case MIGRATION_STATUS_CANCELLED:
777 case MIGRATION_STATUS_ACTIVE:
778 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
779 case MIGRATION_STATUS_FAILED:
780 case MIGRATION_STATUS_COLO:
781 info->has_status = true;
782 break;
783 case MIGRATION_STATUS_COMPLETED:
784 info->has_status = true;
785 fill_destination_postcopy_migration_info(info);
786 break;
787 }
788 info->status = mis->state;
789}
790
791MigrationInfo *qmp_query_migrate(Error **errp)
792{
793 MigrationInfo *info = g_malloc0(sizeof(*info));
794
795 fill_destination_migration_info(info);
796 fill_source_migration_info(info);
797
798 return info;
799}
800
4a84214e
PX
801void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
802 Error **errp)
803{
804 MigrationState *s = migrate_get_current();
805 MigrationCapabilityStatusList *cap;
dd0ee30c 806 bool cap_list[MIGRATION_CAPABILITY__MAX];
4a84214e
PX
807
808 if (migration_is_setup_or_active(s->state)) {
809 error_setg(errp, QERR_MIGRATION_ACTIVE);
810 return;
811 }
812
dd0ee30c
PX
813 memcpy(cap_list, s->enabled_capabilities, sizeof(cap_list));
814 if (!migrate_caps_check(cap_list, params, errp)) {
4a84214e
PX
815 return;
816 }
817
818 for (cap = params; cap; cap = cap->next) {
819 s->enabled_capabilities[cap->value->capability] = cap->value->state;
820 }
00458433
OW
821}
822
16d063bc
PX
823/*
824 * Check whether the parameters are valid. Error will be put into errp
825 * (if provided). Return true if valid, otherwise false.
826 */
827static bool migrate_params_check(MigrationParameters *params, Error **errp)
85de8323 828{
7f375e04 829 if (params->has_compress_level &&
741d4086 830 (params->compress_level > 9)) {
c6bd8c70
MA
831 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
832 "is invalid, it should be in the range of 0 to 9");
16d063bc 833 return false;
85de8323 834 }
16d063bc 835
741d4086 836 if (params->has_compress_threads && (params->compress_threads < 1)) {
c6bd8c70
MA
837 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
838 "compress_threads",
839 "is invalid, it should be in the range of 1 to 255");
16d063bc 840 return false;
85de8323 841 }
16d063bc 842
741d4086 843 if (params->has_decompress_threads && (params->decompress_threads < 1)) {
c6bd8c70
MA
844 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
845 "decompress_threads",
846 "is invalid, it should be in the range of 1 to 255");
16d063bc 847 return false;
85de8323 848 }
16d063bc 849
7f375e04
EB
850 if (params->has_cpu_throttle_initial &&
851 (params->cpu_throttle_initial < 1 ||
852 params->cpu_throttle_initial > 99)) {
1626fee3 853 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
d85a31d1 854 "cpu_throttle_initial",
1626fee3 855 "an integer in the range of 1 to 99");
16d063bc 856 return false;
1626fee3 857 }
16d063bc 858
7f375e04
EB
859 if (params->has_cpu_throttle_increment &&
860 (params->cpu_throttle_increment < 1 ||
861 params->cpu_throttle_increment > 99)) {
1626fee3 862 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
d85a31d1 863 "cpu_throttle_increment",
1626fee3 864 "an integer in the range of 1 to 99");
16d063bc 865 return false;
1626fee3 866 }
16d063bc 867
741d4086 868 if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) {
2ff30257
AA
869 error_setg(errp, "Parameter 'max_bandwidth' expects an integer in the"
870 " range of 0 to %zu bytes/second", SIZE_MAX);
16d063bc 871 return false;
2ff30257 872 }
16d063bc 873
2ff30257 874 if (params->has_downtime_limit &&
741d4086 875 (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
87c9cc1c
DHB
876 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
877 "the range of 0 to %d milliseconds",
878 MAX_MIGRATE_DOWNTIME);
16d063bc 879 return false;
2ff30257 880 }
16d063bc 881
741d4086
JQ
882 /* x_checkpoint_delay is now always positive */
883
884 if (params->has_x_multifd_channels && (params->x_multifd_channels < 1)) {
4075fb1c
JQ
885 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
886 "multifd_channels",
887 "is invalid, it should be in the range of 1 to 255");
888 return false;
889 }
0fb86605 890 if (params->has_x_multifd_page_count &&
741d4086
JQ
891 (params->x_multifd_page_count < 1 ||
892 params->x_multifd_page_count > 10000)) {
0fb86605
JQ
893 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
894 "multifd_page_count",
895 "is invalid, it should be in the range of 1 to 10000");
896 return false;
897 }
16d063bc 898
73af8dd8
JQ
899 if (params->has_xbzrle_cache_size &&
900 (params->xbzrle_cache_size < qemu_target_page_size() ||
901 !is_power_of_2(params->xbzrle_cache_size))) {
902 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
903 "xbzrle_cache_size",
904 "is invalid, it should be bigger than target page size"
905 " and a power of two");
906 return false;
907 }
908
16d063bc
PX
909 return true;
910}
911
1bda8b3c
MA
912static void migrate_params_test_apply(MigrateSetParameters *params,
913 MigrationParameters *dest)
914{
915 *dest = migrate_get_current()->parameters;
916
917 /* TODO use QAPI_CLONE() instead of duplicating it inline */
918
919 if (params->has_compress_level) {
920 dest->compress_level = params->compress_level;
921 }
922
923 if (params->has_compress_threads) {
924 dest->compress_threads = params->compress_threads;
925 }
926
927 if (params->has_decompress_threads) {
928 dest->decompress_threads = params->decompress_threads;
929 }
930
931 if (params->has_cpu_throttle_initial) {
932 dest->cpu_throttle_initial = params->cpu_throttle_initial;
933 }
934
935 if (params->has_cpu_throttle_increment) {
936 dest->cpu_throttle_increment = params->cpu_throttle_increment;
937 }
938
939 if (params->has_tls_creds) {
01fa5598
MA
940 assert(params->tls_creds->type == QTYPE_QSTRING);
941 dest->tls_creds = g_strdup(params->tls_creds->u.s);
1bda8b3c
MA
942 }
943
944 if (params->has_tls_hostname) {
01fa5598
MA
945 assert(params->tls_hostname->type == QTYPE_QSTRING);
946 dest->tls_hostname = g_strdup(params->tls_hostname->u.s);
1bda8b3c
MA
947 }
948
949 if (params->has_max_bandwidth) {
950 dest->max_bandwidth = params->max_bandwidth;
951 }
952
953 if (params->has_downtime_limit) {
954 dest->downtime_limit = params->downtime_limit;
955 }
956
957 if (params->has_x_checkpoint_delay) {
958 dest->x_checkpoint_delay = params->x_checkpoint_delay;
959 }
960
961 if (params->has_block_incremental) {
962 dest->block_incremental = params->block_incremental;
963 }
5e7577a1
JQ
964 if (params->has_x_multifd_channels) {
965 dest->x_multifd_channels = params->x_multifd_channels;
966 }
967 if (params->has_x_multifd_page_count) {
968 dest->x_multifd_page_count = params->x_multifd_page_count;
969 }
73af8dd8
JQ
970 if (params->has_xbzrle_cache_size) {
971 dest->xbzrle_cache_size = params->xbzrle_cache_size;
972 }
1bda8b3c
MA
973}
974
73af8dd8 975static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
16d063bc
PX
976{
977 MigrationState *s = migrate_get_current();
978
e87fae4c
MA
979 /* TODO use QAPI_CLONE() instead of duplicating it inline */
980
7f375e04
EB
981 if (params->has_compress_level) {
982 s->parameters.compress_level = params->compress_level;
85de8323 983 }
476c72aa 984
7f375e04
EB
985 if (params->has_compress_threads) {
986 s->parameters.compress_threads = params->compress_threads;
85de8323 987 }
476c72aa 988
7f375e04
EB
989 if (params->has_decompress_threads) {
990 s->parameters.decompress_threads = params->decompress_threads;
85de8323 991 }
476c72aa 992
7f375e04
EB
993 if (params->has_cpu_throttle_initial) {
994 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
1626fee3 995 }
476c72aa 996
7f375e04
EB
997 if (params->has_cpu_throttle_increment) {
998 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
1626fee3 999 }
476c72aa 1000
7f375e04 1001 if (params->has_tls_creds) {
69ef1f36 1002 g_free(s->parameters.tls_creds);
01fa5598
MA
1003 assert(params->tls_creds->type == QTYPE_QSTRING);
1004 s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
69ef1f36 1005 }
476c72aa 1006
7f375e04 1007 if (params->has_tls_hostname) {
69ef1f36 1008 g_free(s->parameters.tls_hostname);
01fa5598
MA
1009 assert(params->tls_hostname->type == QTYPE_QSTRING);
1010 s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
69ef1f36 1011 }
476c72aa 1012
2ff30257
AA
1013 if (params->has_max_bandwidth) {
1014 s->parameters.max_bandwidth = params->max_bandwidth;
1015 if (s->to_dst_file) {
1016 qemu_file_set_rate_limit(s->to_dst_file,
1017 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
1018 }
1019 }
476c72aa 1020
2ff30257
AA
1021 if (params->has_downtime_limit) {
1022 s->parameters.downtime_limit = params->downtime_limit;
1023 }
68b53591
HZ
1024
1025 if (params->has_x_checkpoint_delay) {
1026 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
479125d5
HZ
1027 if (migration_in_colo_state()) {
1028 colo_checkpoint_notify(s);
1029 }
68b53591 1030 }
476c72aa 1031
2833c59b
JQ
1032 if (params->has_block_incremental) {
1033 s->parameters.block_incremental = params->block_incremental;
1034 }
4075fb1c
JQ
1035 if (params->has_x_multifd_channels) {
1036 s->parameters.x_multifd_channels = params->x_multifd_channels;
1037 }
0fb86605
JQ
1038 if (params->has_x_multifd_page_count) {
1039 s->parameters.x_multifd_page_count = params->x_multifd_page_count;
1040 }
73af8dd8
JQ
1041 if (params->has_xbzrle_cache_size) {
1042 s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
1043 xbzrle_cache_resize(params->xbzrle_cache_size, errp);
1044 }
85de8323
LL
1045}
1046
1bda8b3c 1047void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
476c72aa 1048{
1bda8b3c
MA
1049 MigrationParameters tmp;
1050
01fa5598
MA
1051 /* TODO Rewrite "" to null instead */
1052 if (params->has_tls_creds
1053 && params->tls_creds->type == QTYPE_QNULL) {
cb3e7f08 1054 qobject_unref(params->tls_creds->u.n);
01fa5598
MA
1055 params->tls_creds->type = QTYPE_QSTRING;
1056 params->tls_creds->u.s = strdup("");
1057 }
1058 /* TODO Rewrite "" to null instead */
1059 if (params->has_tls_hostname
1060 && params->tls_hostname->type == QTYPE_QNULL) {
cb3e7f08 1061 qobject_unref(params->tls_hostname->u.n);
01fa5598
MA
1062 params->tls_hostname->type = QTYPE_QSTRING;
1063 params->tls_hostname->u.s = strdup("");
1064 }
1065
1bda8b3c
MA
1066 migrate_params_test_apply(params, &tmp);
1067
1068 if (!migrate_params_check(&tmp, errp)) {
476c72aa
PX
1069 /* Invalid parameter */
1070 return;
1071 }
1072
73af8dd8 1073 migrate_params_apply(params, errp);
476c72aa
PX
1074}
1075
2594f56d 1076
4886a1bc
DDAG
1077void qmp_migrate_start_postcopy(Error **errp)
1078{
1079 MigrationState *s = migrate_get_current();
1080
16b0fd32 1081 if (!migrate_postcopy()) {
a54d340b 1082 error_setg(errp, "Enable postcopy with migrate_set_capability before"
4886a1bc
DDAG
1083 " the start of migration");
1084 return;
1085 }
1086
1087 if (s->state == MIGRATION_STATUS_NONE) {
1088 error_setg(errp, "Postcopy must be started after migration has been"
1089 " started");
1090 return;
1091 }
1092 /*
1093 * we don't error if migration has finished since that would be racy
1094 * with issuing this command.
1095 */
1096 atomic_set(&s->start_postcopy, true);
1097}
1098
065e2813
AL
1099/* shared migration helpers */
1100
48781e5b 1101void migrate_set_state(int *state, int old_state, int new_state)
51cf4c1a 1102{
a31fedee 1103 assert(new_state < MIGRATION_STATUS__MAX);
48781e5b 1104 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
a31fedee 1105 trace_migrate_set_state(MigrationStatus_str(new_state));
b05dc723 1106 migrate_generate_event(new_state);
51cf4c1a
Z
1107 }
1108}
1109
4e4a3d3a
PX
1110static MigrationCapabilityStatusList *migrate_cap_add(
1111 MigrationCapabilityStatusList *list,
1112 MigrationCapability index,
1113 bool state)
2833c59b
JQ
1114{
1115 MigrationCapabilityStatusList *cap;
1116
1117 cap = g_new0(MigrationCapabilityStatusList, 1);
1118 cap->value = g_new0(MigrationCapabilityStatus, 1);
4e4a3d3a
PX
1119 cap->value->capability = index;
1120 cap->value->state = state;
1121 cap->next = list;
1122
1123 return cap;
1124}
1125
1126void migrate_set_block_enabled(bool value, Error **errp)
1127{
1128 MigrationCapabilityStatusList *cap;
1129
1130 cap = migrate_cap_add(NULL, MIGRATION_CAPABILITY_BLOCK, value);
2833c59b
JQ
1131 qmp_migrate_set_capabilities(cap, errp);
1132 qapi_free_MigrationCapabilityStatusList(cap);
1133}
1134
1135static void migrate_set_block_incremental(MigrationState *s, bool value)
1136{
1137 s->parameters.block_incremental = value;
1138}
1139
1140static void block_cleanup_parameters(MigrationState *s)
1141{
1142 if (s->must_remove_block_options) {
1143 /* setting to false can never fail */
1144 migrate_set_block_enabled(false, &error_abort);
1145 migrate_set_block_incremental(s, false);
1146 s->must_remove_block_options = false;
1147 }
1148}
1149
bb1fadc4 1150static void migrate_fd_cleanup(void *opaque)
065e2813 1151{
bb1fadc4
PB
1152 MigrationState *s = opaque;
1153
1154 qemu_bh_delete(s->cleanup_bh);
1155 s->cleanup_bh = NULL;
1156
0ceccd85
PX
1157 qemu_savevm_state_cleanup();
1158
89a02a9f 1159 if (s->to_dst_file) {
f986c3d2
JQ
1160 Error *local_err = NULL;
1161
9013dca5 1162 trace_migrate_fd_cleanup();
404a7c05 1163 qemu_mutex_unlock_iothread();
1d34e4bf
DDAG
1164 if (s->migration_thread_running) {
1165 qemu_thread_join(&s->thread);
1166 s->migration_thread_running = false;
1167 }
404a7c05
PB
1168 qemu_mutex_lock_iothread();
1169
f986c3d2
JQ
1170 if (multifd_save_cleanup(&local_err) != 0) {
1171 error_report_err(local_err);
1172 }
89a02a9f
HZ
1173 qemu_fclose(s->to_dst_file);
1174 s->to_dst_file = NULL;
065e2813
AL
1175 }
1176
9ec055ae
DDAG
1177 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
1178 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
7a2c1721 1179
94f5a437 1180 if (s->state == MIGRATION_STATUS_CANCELLING) {
48781e5b 1181 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
94f5a437 1182 MIGRATION_STATUS_CANCELLED);
7a2c1721 1183 }
a3fa1d78 1184
87db1a7d
JQ
1185 if (s->error) {
1186 /* It is used on info migrate. We can't free it */
1187 error_report_err(error_copy(s->error));
1188 }
a3fa1d78 1189 notifier_list_notify(&migration_state_notifiers, s);
2833c59b 1190 block_cleanup_parameters(s);
065e2813
AL
1191}
1192
87db1a7d
JQ
1193void migrate_set_error(MigrationState *s, const Error *error)
1194{
1195 qemu_mutex_lock(&s->error_mutex);
1196 if (!s->error) {
1197 s->error = error_copy(error);
1198 }
1199 qemu_mutex_unlock(&s->error_mutex);
1200}
1201
d59ce6f3 1202void migrate_fd_error(MigrationState *s, const Error *error)
065e2813 1203{
25174055 1204 trace_migrate_fd_error(error_get_pretty(error));
89a02a9f 1205 assert(s->to_dst_file == NULL);
48781e5b
HZ
1206 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1207 MIGRATION_STATUS_FAILED);
87db1a7d 1208 migrate_set_error(s, error);
458cf28e
JQ
1209}
1210
0edda1c4 1211static void migrate_fd_cancel(MigrationState *s)
065e2813 1212{
6f2b811a 1213 int old_state ;
89a02a9f 1214 QEMUFile *f = migrate_get_current()->to_dst_file;
9013dca5 1215 trace_migrate_fd_cancel();
065e2813 1216
70b20477
DDAG
1217 if (s->rp_state.from_dst_file) {
1218 /* shutdown the rp socket, so causing the rp thread to shutdown */
1219 qemu_file_shutdown(s->rp_state.from_dst_file);
1220 }
1221
6f2b811a
Z
1222 do {
1223 old_state = s->state;
f6844b99 1224 if (!migration_is_setup_or_active(old_state)) {
6f2b811a
Z
1225 break;
1226 }
a7b36b48
DDAG
1227 /* If the migration is paused, kick it out of the pause */
1228 if (old_state == MIGRATION_STATUS_PRE_SWITCHOVER) {
1229 qemu_sem_post(&s->pause_sem);
1230 }
48781e5b 1231 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
31194731 1232 } while (s->state != MIGRATION_STATUS_CANCELLING);
a26ba26e
DDAG
1233
1234 /*
1235 * If we're unlucky the migration code might be stuck somewhere in a
1236 * send/write while the network has failed and is waiting to timeout;
1237 * if we've got shutdown(2) available then we can force it to quit.
1238 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1239 * called in a bh, so there is no race against this cancel.
1240 */
31194731 1241 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
a26ba26e
DDAG
1242 qemu_file_shutdown(f);
1243 }
1d2acc31
HZ
1244 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
1245 Error *local_err = NULL;
1246
1247 bdrv_invalidate_cache_all(&local_err);
1248 if (local_err) {
1249 error_report_err(local_err);
1250 } else {
1251 s->block_inactive = false;
1252 }
1253 }
065e2813
AL
1254}
1255
99a0db9b
GH
1256void add_migration_state_change_notifier(Notifier *notify)
1257{
1258 notifier_list_add(&migration_state_notifiers, notify);
1259}
1260
1261void remove_migration_state_change_notifier(Notifier *notify)
1262{
31552529 1263 notifier_remove(notify);
99a0db9b
GH
1264}
1265
02edd2e7 1266bool migration_in_setup(MigrationState *s)
afe2df69 1267{
31194731 1268 return s->state == MIGRATION_STATUS_SETUP;
afe2df69
GH
1269}
1270
7073693b 1271bool migration_has_finished(MigrationState *s)
99a0db9b 1272{
31194731 1273 return s->state == MIGRATION_STATUS_COMPLETED;
99a0db9b 1274}
0edda1c4 1275
afe2df69
GH
1276bool migration_has_failed(MigrationState *s)
1277{
31194731
HZ
1278 return (s->state == MIGRATION_STATUS_CANCELLED ||
1279 s->state == MIGRATION_STATUS_FAILED);
afe2df69
GH
1280}
1281
5727309d 1282bool migration_in_postcopy(void)
9ec055ae 1283{
5727309d
JQ
1284 MigrationState *s = migrate_get_current();
1285
9ec055ae
DDAG
1286 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1287}
1288
b82fc321
DDAG
1289bool migration_in_postcopy_after_devices(MigrationState *s)
1290{
5727309d 1291 return migration_in_postcopy() && s->postcopy_after_devices;
b82fc321
DDAG
1292}
1293
fab35005 1294bool migration_is_idle(void)
fe44dc91 1295{
fab35005 1296 MigrationState *s = migrate_get_current();
fe44dc91
AA
1297
1298 switch (s->state) {
1299 case MIGRATION_STATUS_NONE:
1300 case MIGRATION_STATUS_CANCELLED:
1301 case MIGRATION_STATUS_COMPLETED:
1302 case MIGRATION_STATUS_FAILED:
1303 return true;
1304 case MIGRATION_STATUS_SETUP:
1305 case MIGRATION_STATUS_CANCELLING:
1306 case MIGRATION_STATUS_ACTIVE:
1307 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1308 case MIGRATION_STATUS_COLO:
31e06077
DDAG
1309 case MIGRATION_STATUS_PRE_SWITCHOVER:
1310 case MIGRATION_STATUS_DEVICE:
fe44dc91
AA
1311 return false;
1312 case MIGRATION_STATUS__MAX:
1313 g_assert_not_reached();
1314 }
1315
1316 return false;
1317}
1318
3e0c8050 1319void migrate_init(MigrationState *s)
0edda1c4 1320{
389775d1
DDAG
1321 /*
1322 * Reinitialise all migration state, except
1323 * parameters/capabilities that the user set, and
1324 * locks.
1325 */
1326 s->bytes_xfer = 0;
1327 s->xfer_limit = 0;
1328 s->cleanup_bh = 0;
89a02a9f 1329 s->to_dst_file = NULL;
389775d1 1330 s->state = MIGRATION_STATUS_NONE;
389775d1
DDAG
1331 s->rp_state.from_dst_file = NULL;
1332 s->rp_state.error = false;
1333 s->mbps = 0.0;
1334 s->downtime = 0;
1335 s->expected_downtime = 0;
389775d1 1336 s->setup_time = 0;
389775d1 1337 s->start_postcopy = false;
b82fc321 1338 s->postcopy_after_devices = false;
389775d1 1339 s->migration_thread_running = false;
d59ce6f3
DB
1340 error_free(s->error);
1341 s->error = NULL;
389775d1 1342
48781e5b 1343 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
0edda1c4 1344
4af246a3
PX
1345 s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1346 s->total_time = 0;
7287cbd4 1347 s->vm_was_running = false;
b15df1ae
PX
1348 s->iteration_initial_bytes = 0;
1349 s->threshold_size = 0;
0edda1c4 1350}
cab30143 1351
fa2756b7
AL
1352static GSList *migration_blockers;
1353
fe44dc91 1354int migrate_add_blocker(Error *reason, Error **errp)
fa2756b7 1355{
3df663e5 1356 if (migrate_get_current()->only_migratable) {
b67b8c3a
AA
1357 error_propagate(errp, error_copy(reason));
1358 error_prepend(errp, "disallowing migration blocker "
1359 "(--only_migratable) for: ");
1360 return -EACCES;
1361 }
1362
fab35005 1363 if (migration_is_idle()) {
fe44dc91
AA
1364 migration_blockers = g_slist_prepend(migration_blockers, reason);
1365 return 0;
1366 }
1367
1368 error_propagate(errp, error_copy(reason));
1369 error_prepend(errp, "disallowing migration blocker (migration in "
1370 "progress) for: ");
1371 return -EBUSY;
fa2756b7
AL
1372}
1373
1374void migrate_del_blocker(Error *reason)
1375{
1376 migration_blockers = g_slist_remove(migration_blockers, reason);
1377}
1378
bf1ae1f4
DDAG
1379void qmp_migrate_incoming(const char *uri, Error **errp)
1380{
1381 Error *local_err = NULL;
4debb5f5 1382 static bool once = true;
bf1ae1f4
DDAG
1383
1384 if (!deferred_incoming) {
4debb5f5 1385 error_setg(errp, "For use with '-incoming defer'");
bf1ae1f4
DDAG
1386 return;
1387 }
4debb5f5
DDAG
1388 if (!once) {
1389 error_setg(errp, "The incoming migration has already been started");
1390 }
bf1ae1f4
DDAG
1391
1392 qemu_start_incoming_migration(uri, &local_err);
1393
1394 if (local_err) {
1395 error_propagate(errp, local_err);
1396 return;
1397 }
1398
4debb5f5 1399 once = false;
bf1ae1f4
DDAG
1400}
1401
24f3902b
GK
1402bool migration_is_blocked(Error **errp)
1403{
1404 if (qemu_savevm_state_blocked(errp)) {
1405 return true;
1406 }
1407
1408 if (migration_blockers) {
250561e1 1409 error_propagate(errp, error_copy(migration_blockers->data));
24f3902b
GK
1410 return true;
1411 }
1412
1413 return false;
1414}
1415
d3e35b8f
PX
1416/* Returns true if continue to migrate, or false if error detected */
1417static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc,
1418 bool resume, Error **errp)
cab30143 1419{
be7059cd 1420 Error *local_err = NULL;
d3e35b8f
PX
1421
1422 if (resume) {
1423 if (s->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
1424 error_setg(errp, "Cannot resume if there is no "
1425 "paused migration");
1426 return false;
1427 }
1428 /* This is a resume, skip init status */
1429 return true;
1430 }
cab30143 1431
f6844b99 1432 if (migration_is_setup_or_active(s->state) ||
0b827d5e
HZ
1433 s->state == MIGRATION_STATUS_CANCELLING ||
1434 s->state == MIGRATION_STATUS_COLO) {
c6bd8c70 1435 error_setg(errp, QERR_MIGRATION_ACTIVE);
d3e35b8f 1436 return false;
cab30143 1437 }
d3e35b8f 1438
ca99993a
DDAG
1439 if (runstate_check(RUN_STATE_INMIGRATE)) {
1440 error_setg(errp, "Guest is waiting for an incoming migration");
d3e35b8f 1441 return false;
ca99993a
DDAG
1442 }
1443
24f3902b 1444 if (migration_is_blocked(errp)) {
d3e35b8f 1445 return false;
fa2756b7
AL
1446 }
1447
d3e35b8f 1448 if (blk || blk_inc) {
2833c59b
JQ
1449 if (migrate_use_block() || migrate_use_block_incremental()) {
1450 error_setg(errp, "Command options are incompatible with "
1451 "current migration capabilities");
d3e35b8f 1452 return false;
2833c59b
JQ
1453 }
1454 migrate_set_block_enabled(true, &local_err);
1455 if (local_err) {
1456 error_propagate(errp, local_err);
d3e35b8f 1457 return false;
2833c59b
JQ
1458 }
1459 s->must_remove_block_options = true;
1460 }
1461
d3e35b8f 1462 if (blk_inc) {
2833c59b
JQ
1463 migrate_set_block_incremental(s, true);
1464 }
1465
3e0c8050 1466 migrate_init(s);
cab30143 1467
d3e35b8f
PX
1468 return true;
1469}
1470
1471void qmp_migrate(const char *uri, bool has_blk, bool blk,
1472 bool has_inc, bool inc, bool has_detach, bool detach,
1473 bool has_resume, bool resume, Error **errp)
1474{
1475 Error *local_err = NULL;
1476 MigrationState *s = migrate_get_current();
1477 const char *p;
1478
1479 if (!migrate_prepare(s, has_blk && blk, has_inc && inc,
1480 has_resume && resume, errp)) {
1481 /* Error detected, put into errp */
1482 return;
1483 }
1484
cab30143 1485 if (strstart(uri, "tcp:", &p)) {
f37afb5a 1486 tcp_start_outgoing_migration(s, p, &local_err);
2da776db 1487#ifdef CONFIG_RDMA
41310c68 1488 } else if (strstart(uri, "rdma:", &p)) {
2da776db
MH
1489 rdma_start_outgoing_migration(s, p, &local_err);
1490#endif
cab30143 1491 } else if (strstart(uri, "exec:", &p)) {
f37afb5a 1492 exec_start_outgoing_migration(s, p, &local_err);
cab30143 1493 } else if (strstart(uri, "unix:", &p)) {
f37afb5a 1494 unix_start_outgoing_migration(s, p, &local_err);
cab30143 1495 } else if (strstart(uri, "fd:", &p)) {
f37afb5a 1496 fd_start_outgoing_migration(s, p, &local_err);
99a0db9b 1497 } else {
c6bd8c70
MA
1498 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1499 "a valid migration protocol");
48781e5b
HZ
1500 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1501 MIGRATION_STATUS_FAILED);
09576e74 1502 block_cleanup_parameters(s);
e1c37d0e 1503 return;
cab30143
JQ
1504 }
1505
f37afb5a 1506 if (local_err) {
d59ce6f3 1507 migrate_fd_error(s, local_err);
f37afb5a 1508 error_propagate(errp, local_err);
e1c37d0e 1509 return;
1299c631 1510 }
cab30143
JQ
1511}
1512
6cdedb07 1513void qmp_migrate_cancel(Error **errp)
cab30143 1514{
17549e84 1515 migrate_fd_cancel(migrate_get_current());
cab30143
JQ
1516}
1517
89cfc02c
DDAG
1518void qmp_migrate_continue(MigrationStatus state, Error **errp)
1519{
1520 MigrationState *s = migrate_get_current();
1521 if (s->state != state) {
1522 error_setg(errp, "Migration not in expected state: %s",
1523 MigrationStatus_str(s->state));
1524 return;
1525 }
1526 qemu_sem_post(&s->pause_sem);
1527}
1528
9e1ba4cc
OW
1529void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1530{
73af8dd8
JQ
1531 MigrateSetParameters p = {
1532 .has_xbzrle_cache_size = true,
1533 .xbzrle_cache_size = value,
1534 };
c91e681a 1535
73af8dd8 1536 qmp_migrate_set_parameters(&p, errp);
9e1ba4cc
OW
1537}
1538
1539int64_t qmp_query_migrate_cache_size(Error **errp)
1540{
1541 return migrate_xbzrle_cache_size();
1542}
1543
3dc85383 1544void qmp_migrate_set_speed(int64_t value, Error **errp)
cab30143 1545{
1bda8b3c 1546 MigrateSetParameters p = {
2ff30257
AA
1547 .has_max_bandwidth = true,
1548 .max_bandwidth = value,
1549 };
cab30143 1550
2ff30257 1551 qmp_migrate_set_parameters(&p, errp);
cab30143
JQ
1552}
1553
4f0a993b 1554void qmp_migrate_set_downtime(double value, Error **errp)
cab30143 1555{
87c9cc1c
DHB
1556 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
1557 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
1558 "the range of 0 to %d seconds",
1559 MAX_MIGRATE_DOWNTIME_SECONDS);
1560 return;
1561 }
1562
2ff30257
AA
1563 value *= 1000; /* Convert to milliseconds */
1564 value = MAX(0, MIN(INT64_MAX, value));
1565
1bda8b3c 1566 MigrateSetParameters p = {
2ff30257
AA
1567 .has_downtime_limit = true,
1568 .downtime_limit = value,
1569 };
1570
1571 qmp_migrate_set_parameters(&p, errp);
99a0db9b 1572}
17ad9b35 1573
53f09a10
PB
1574bool migrate_release_ram(void)
1575{
1576 MigrationState *s;
1577
1578 s = migrate_get_current();
1579
1580 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
1581}
1582
53dd370c
DDAG
1583bool migrate_postcopy_ram(void)
1584{
1585 MigrationState *s;
1586
1587 s = migrate_get_current();
1588
32c3db5b 1589 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
53dd370c
DDAG
1590}
1591
58110f0a
VSO
1592bool migrate_postcopy(void)
1593{
dd6bb914 1594 return migrate_postcopy_ram() || migrate_dirty_bitmaps();
58110f0a
VSO
1595}
1596
bde1e2ec
CV
1597bool migrate_auto_converge(void)
1598{
1599 MigrationState *s;
1600
1601 s = migrate_get_current();
1602
1603 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1604}
1605
323004a3
PL
1606bool migrate_zero_blocks(void)
1607{
1608 MigrationState *s;
1609
1610 s = migrate_get_current();
1611
1612 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1613}
1614
f22f928e
AP
1615bool migrate_postcopy_blocktime(void)
1616{
1617 MigrationState *s;
1618
1619 s = migrate_get_current();
1620
1621 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME];
1622}
1623
8706d2d5
LL
1624bool migrate_use_compression(void)
1625{
dde4e694
LL
1626 MigrationState *s;
1627
1628 s = migrate_get_current();
1629
1630 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
8706d2d5
LL
1631}
1632
1633int migrate_compress_level(void)
1634{
1635 MigrationState *s;
1636
1637 s = migrate_get_current();
1638
2594f56d 1639 return s->parameters.compress_level;
8706d2d5
LL
1640}
1641
1642int migrate_compress_threads(void)
1643{
1644 MigrationState *s;
1645
1646 s = migrate_get_current();
1647
2594f56d 1648 return s->parameters.compress_threads;
8706d2d5
LL
1649}
1650
3fcb38c2
LL
1651int migrate_decompress_threads(void)
1652{
1653 MigrationState *s;
1654
1655 s = migrate_get_current();
1656
2594f56d 1657 return s->parameters.decompress_threads;
3fcb38c2
LL
1658}
1659
55efc8c2
VSO
1660bool migrate_dirty_bitmaps(void)
1661{
1662 MigrationState *s;
1663
1664 s = migrate_get_current();
1665
1666 return s->enabled_capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS];
1667}
1668
b05dc723
JQ
1669bool migrate_use_events(void)
1670{
1671 MigrationState *s;
1672
1673 s = migrate_get_current();
1674
1675 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1676}
1677
30126bbf
JQ
1678bool migrate_use_multifd(void)
1679{
1680 MigrationState *s;
1681
1682 s = migrate_get_current();
1683
1684 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_MULTIFD];
1685}
1686
93fbd031
DDAG
1687bool migrate_pause_before_switchover(void)
1688{
1689 MigrationState *s;
1690
1691 s = migrate_get_current();
1692
1693 return s->enabled_capabilities[
1694 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER];
1695}
1696
4075fb1c
JQ
1697int migrate_multifd_channels(void)
1698{
1699 MigrationState *s;
1700
1701 s = migrate_get_current();
1702
1703 return s->parameters.x_multifd_channels;
1704}
1705
0fb86605
JQ
1706int migrate_multifd_page_count(void)
1707{
1708 MigrationState *s;
1709
1710 s = migrate_get_current();
1711
1712 return s->parameters.x_multifd_page_count;
1713}
1714
17ad9b35
OW
1715int migrate_use_xbzrle(void)
1716{
1717 MigrationState *s;
1718
1719 s = migrate_get_current();
1720
1721 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1722}
1723
1724int64_t migrate_xbzrle_cache_size(void)
1725{
1726 MigrationState *s;
1727
1728 s = migrate_get_current();
1729
73af8dd8 1730 return s->parameters.xbzrle_cache_size;
17ad9b35 1731}
0d82d0e8 1732
2833c59b
JQ
1733bool migrate_use_block(void)
1734{
1735 MigrationState *s;
1736
1737 s = migrate_get_current();
1738
1739 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
1740}
1741
c788ada8
PX
1742bool migrate_use_return_path(void)
1743{
1744 MigrationState *s;
1745
1746 s = migrate_get_current();
1747
1748 return s->enabled_capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
1749}
1750
2833c59b
JQ
1751bool migrate_use_block_incremental(void)
1752{
1753 MigrationState *s;
1754
1755 s = migrate_get_current();
1756
1757 return s->parameters.block_incremental;
1758}
1759
70b20477
DDAG
1760/* migration thread support */
1761/*
1762 * Something bad happened to the RP stream, mark an error
1763 * The caller shall print or trace something to indicate why
1764 */
1765static void mark_source_rp_bad(MigrationState *s)
1766{
1767 s->rp_state.error = true;
1768}
1769
1770static struct rp_cmd_args {
1771 ssize_t len; /* -1 = variable */
1772 const char *name;
1773} rp_cmd_args[] = {
1774 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1775 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1776 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
1e2d90eb
DDAG
1777 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1778 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
70b20477
DDAG
1779 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1780};
1781
1e2d90eb
DDAG
1782/*
1783 * Process a request for pages received on the return path,
1784 * We're allowed to send more than requested (e.g. to round to our page size)
1785 * and we don't need to send pages that have already been sent.
1786 */
1787static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1788 ram_addr_t start, size_t len)
1789{
6c595cde
DDAG
1790 long our_host_ps = getpagesize();
1791
1e2d90eb 1792 trace_migrate_handle_rp_req_pages(rbname, start, len);
6c595cde
DDAG
1793
1794 /*
1795 * Since we currently insist on matching page sizes, just sanity check
1796 * we're being asked for whole host pages.
1797 */
1798 if (start & (our_host_ps-1) ||
1799 (len & (our_host_ps-1))) {
1800 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1801 " len: %zd", __func__, start, len);
1802 mark_source_rp_bad(ms);
1803 return;
1804 }
1805
96506894 1806 if (ram_save_queue_pages(rbname, start, len)) {
6c595cde
DDAG
1807 mark_source_rp_bad(ms);
1808 }
1e2d90eb
DDAG
1809}
1810
14b1742e
PX
1811/* Return true to retry, false to quit */
1812static bool postcopy_pause_return_path_thread(MigrationState *s)
1813{
1814 trace_postcopy_pause_return_path();
1815
1816 qemu_sem_wait(&s->postcopy_pause_rp_sem);
1817
1818 trace_postcopy_pause_return_path_continued();
1819
1820 return true;
1821}
1822
70b20477
DDAG
1823/*
1824 * Handles messages sent on the return path towards the source VM
1825 *
1826 */
1827static void *source_return_path_thread(void *opaque)
1828{
1829 MigrationState *ms = opaque;
1830 QEMUFile *rp = ms->rp_state.from_dst_file;
1831 uint16_t header_len, header_type;
568b01ca 1832 uint8_t buf[512];
70b20477 1833 uint32_t tmp32, sibling_error;
1e2d90eb
DDAG
1834 ram_addr_t start = 0; /* =0 to silence warning */
1835 size_t len = 0, expected_len;
70b20477
DDAG
1836 int res;
1837
1838 trace_source_return_path_thread_entry();
14b1742e
PX
1839
1840retry:
70b20477
DDAG
1841 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1842 migration_is_setup_or_active(ms->state)) {
1843 trace_source_return_path_thread_loop_top();
1844 header_type = qemu_get_be16(rp);
1845 header_len = qemu_get_be16(rp);
1846
7a9ddfbf
PX
1847 if (qemu_file_get_error(rp)) {
1848 mark_source_rp_bad(ms);
1849 goto out;
1850 }
1851
70b20477
DDAG
1852 if (header_type >= MIG_RP_MSG_MAX ||
1853 header_type == MIG_RP_MSG_INVALID) {
1854 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1855 header_type, header_len);
1856 mark_source_rp_bad(ms);
1857 goto out;
1858 }
1859
1860 if ((rp_cmd_args[header_type].len != -1 &&
1861 header_len != rp_cmd_args[header_type].len) ||
568b01ca 1862 header_len > sizeof(buf)) {
70b20477
DDAG
1863 error_report("RP: Received '%s' message (0x%04x) with"
1864 "incorrect length %d expecting %zu",
1865 rp_cmd_args[header_type].name, header_type, header_len,
1866 (size_t)rp_cmd_args[header_type].len);
1867 mark_source_rp_bad(ms);
1868 goto out;
1869 }
1870
1871 /* We know we've got a valid header by this point */
1872 res = qemu_get_buffer(rp, buf, header_len);
1873 if (res != header_len) {
1874 error_report("RP: Failed reading data for message 0x%04x"
1875 " read %d expected %d",
1876 header_type, res, header_len);
1877 mark_source_rp_bad(ms);
1878 goto out;
1879 }
1880
1881 /* OK, we have the message and the data */
1882 switch (header_type) {
1883 case MIG_RP_MSG_SHUT:
4d885131 1884 sibling_error = ldl_be_p(buf);
70b20477
DDAG
1885 trace_source_return_path_thread_shut(sibling_error);
1886 if (sibling_error) {
1887 error_report("RP: Sibling indicated error %d", sibling_error);
1888 mark_source_rp_bad(ms);
1889 }
1890 /*
1891 * We'll let the main thread deal with closing the RP
1892 * we could do a shutdown(2) on it, but we're the only user
1893 * anyway, so there's nothing gained.
1894 */
1895 goto out;
1896
1897 case MIG_RP_MSG_PONG:
4d885131 1898 tmp32 = ldl_be_p(buf);
70b20477
DDAG
1899 trace_source_return_path_thread_pong(tmp32);
1900 break;
1901
1e2d90eb 1902 case MIG_RP_MSG_REQ_PAGES:
4d885131
PM
1903 start = ldq_be_p(buf);
1904 len = ldl_be_p(buf + 8);
1e2d90eb
DDAG
1905 migrate_handle_rp_req_pages(ms, NULL, start, len);
1906 break;
1907
1908 case MIG_RP_MSG_REQ_PAGES_ID:
1909 expected_len = 12 + 1; /* header + termination */
1910
1911 if (header_len >= expected_len) {
4d885131
PM
1912 start = ldq_be_p(buf);
1913 len = ldl_be_p(buf + 8);
1e2d90eb
DDAG
1914 /* Now we expect an idstr */
1915 tmp32 = buf[12]; /* Length of the following idstr */
1916 buf[13 + tmp32] = '\0';
1917 expected_len += tmp32;
1918 }
1919 if (header_len != expected_len) {
1920 error_report("RP: Req_Page_id with length %d expecting %zd",
1921 header_len, expected_len);
1922 mark_source_rp_bad(ms);
1923 goto out;
1924 }
1925 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
1926 break;
1927
70b20477
DDAG
1928 default:
1929 break;
1930 }
1931 }
14b1742e
PX
1932
1933out:
1934 res = qemu_file_get_error(rp);
1935 if (res) {
1936 if (res == -EIO) {
1937 /*
1938 * Maybe there is something we can do: it looks like a
1939 * network down issue, and we pause for a recovery.
1940 */
1941 if (postcopy_pause_return_path_thread(ms)) {
1942 /* Reload rp, reset the rest */
1943 rp = ms->rp_state.from_dst_file;
1944 ms->rp_state.error = false;
1945 goto retry;
1946 }
1947 }
1948
70b20477
DDAG
1949 trace_source_return_path_thread_bad_end();
1950 mark_source_rp_bad(ms);
1951 }
1952
1953 trace_source_return_path_thread_end();
70b20477
DDAG
1954 ms->rp_state.from_dst_file = NULL;
1955 qemu_fclose(rp);
1956 return NULL;
1957}
1958
d3e35b8f
PX
1959static int open_return_path_on_source(MigrationState *ms,
1960 bool create_thread)
70b20477
DDAG
1961{
1962
89a02a9f 1963 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
70b20477
DDAG
1964 if (!ms->rp_state.from_dst_file) {
1965 return -1;
1966 }
1967
1968 trace_open_return_path_on_source();
d3e35b8f
PX
1969
1970 if (!create_thread) {
1971 /* We're done */
1972 return 0;
1973 }
1974
70b20477
DDAG
1975 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1976 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1977
1978 trace_open_return_path_on_source_continue();
1979
1980 return 0;
1981}
1982
70b20477
DDAG
1983/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1984static int await_return_path_close_on_source(MigrationState *ms)
1985{
1986 /*
1987 * If this is a normal exit then the destination will send a SHUT and the
1988 * rp_thread will exit, however if there's an error we need to cause
1989 * it to exit.
1990 */
89a02a9f 1991 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
70b20477
DDAG
1992 /*
1993 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1994 * waiting for the destination.
1995 */
1996 qemu_file_shutdown(ms->rp_state.from_dst_file);
1997 mark_source_rp_bad(ms);
1998 }
1999 trace_await_return_path_close_on_source_joining();
2000 qemu_thread_join(&ms->rp_state.rp_thread);
2001 trace_await_return_path_close_on_source_close();
2002 return ms->rp_state.error;
2003}
2004
1d34e4bf
DDAG
2005/*
2006 * Switch from normal iteration to postcopy
2007 * Returns non-0 on error
2008 */
7287cbd4 2009static int postcopy_start(MigrationState *ms)
1d34e4bf
DDAG
2010{
2011 int ret;
61b67d47
DB
2012 QIOChannelBuffer *bioc;
2013 QEMUFile *fb;
1d34e4bf 2014 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
ef8d6488 2015 bool restart_block = false;
0331c8ca
DDAG
2016 int cur_state = MIGRATION_STATUS_ACTIVE;
2017 if (!migrate_pause_before_switchover()) {
2018 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
2019 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2020 }
1d34e4bf
DDAG
2021
2022 trace_postcopy_start();
2023 qemu_mutex_lock_iothread();
2024 trace_postcopy_start_set_run();
2025
2026 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1d34e4bf
DDAG
2027 global_state_store();
2028 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
76b1c7fe
KW
2029 if (ret < 0) {
2030 goto fail;
2031 }
1d34e4bf 2032
0331c8ca
DDAG
2033 ret = migration_maybe_pause(ms, &cur_state,
2034 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2035 if (ret < 0) {
2036 goto fail;
2037 }
2038
76b1c7fe 2039 ret = bdrv_inactivate_all();
1d34e4bf
DDAG
2040 if (ret < 0) {
2041 goto fail;
2042 }
ef8d6488 2043 restart_block = true;
1d34e4bf 2044
1c0d249d
DDAG
2045 /*
2046 * Cause any non-postcopiable, but iterative devices to
2047 * send out their final data.
2048 */
a1fbe750 2049 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
1c0d249d 2050
1d34e4bf
DDAG
2051 /*
2052 * in Finish migrate and with the io-lock held everything should
2053 * be quiet, but we've potentially still got dirty pages and we
2054 * need to tell the destination to throw any pages it's already received
2055 * that are dirty
2056 */
58110f0a
VSO
2057 if (migrate_postcopy_ram()) {
2058 if (ram_postcopy_send_discard_bitmap(ms)) {
2059 error_report("postcopy send discard bitmap failed");
2060 goto fail;
2061 }
1d34e4bf
DDAG
2062 }
2063
2064 /*
2065 * send rest of state - note things that are doing postcopy
2066 * will notice we're in POSTCOPY_ACTIVE and not actually
2067 * wrap their state up here
2068 */
89a02a9f 2069 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
58110f0a
VSO
2070 if (migrate_postcopy_ram()) {
2071 /* Ping just for debugging, helps line traces up */
2072 qemu_savevm_send_ping(ms->to_dst_file, 2);
2073 }
1d34e4bf
DDAG
2074
2075 /*
2076 * While loading the device state we may trigger page transfer
2077 * requests and the fd must be free to process those, and thus
2078 * the destination must read the whole device state off the fd before
2079 * it starts processing it. Unfortunately the ad-hoc migration format
2080 * doesn't allow the destination to know the size to read without fully
2081 * parsing it through each devices load-state code (especially the open
2082 * coded devices that use get/put).
2083 * So we wrap the device state up in a package with a length at the start;
2084 * to do this we use a qemu_buf to hold the whole of the device state.
2085 */
61b67d47 2086 bioc = qio_channel_buffer_new(4096);
6f01f136 2087 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
61b67d47
DB
2088 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
2089 object_unref(OBJECT(bioc));
1d34e4bf 2090
c76201ab
DDAG
2091 /*
2092 * Make sure the receiver can get incoming pages before we send the rest
2093 * of the state
2094 */
2095 qemu_savevm_send_postcopy_listen(fb);
2096
a1fbe750 2097 qemu_savevm_state_complete_precopy(fb, false, false);
58110f0a
VSO
2098 if (migrate_postcopy_ram()) {
2099 qemu_savevm_send_ping(fb, 3);
2100 }
1d34e4bf
DDAG
2101
2102 qemu_savevm_send_postcopy_run(fb);
2103
2104 /* <><> end of stuff going into the package */
1d34e4bf 2105
ef8d6488
DDAG
2106 /* Last point of recovery; as soon as we send the package the destination
2107 * can open devices and potentially start running.
2108 * Lets just check again we've not got any errors.
2109 */
2110 ret = qemu_file_get_error(ms->to_dst_file);
2111 if (ret) {
2112 error_report("postcopy_start: Migration stream errored (pre package)");
2113 goto fail_closefb;
2114 }
2115
2116 restart_block = false;
2117
1d34e4bf 2118 /* Now send that blob */
61b67d47 2119 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
1d34e4bf
DDAG
2120 goto fail_closefb;
2121 }
2122 qemu_fclose(fb);
b82fc321
DDAG
2123
2124 /* Send a notify to give a chance for anything that needs to happen
2125 * at the transition to postcopy and after the device state; in particular
2126 * spice needs to trigger a transition now
2127 */
2128 ms->postcopy_after_devices = true;
2129 notifier_list_notify(&migration_state_notifiers, ms);
2130
1d34e4bf
DDAG
2131 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
2132
2133 qemu_mutex_unlock_iothread();
2134
58110f0a
VSO
2135 if (migrate_postcopy_ram()) {
2136 /*
2137 * Although this ping is just for debug, it could potentially be
2138 * used for getting a better measurement of downtime at the source.
2139 */
2140 qemu_savevm_send_ping(ms->to_dst_file, 4);
2141 }
1d34e4bf 2142
ced1c616
PB
2143 if (migrate_release_ram()) {
2144 ram_postcopy_migrated_memory_release(ms);
2145 }
2146
89a02a9f 2147 ret = qemu_file_get_error(ms->to_dst_file);
1d34e4bf
DDAG
2148 if (ret) {
2149 error_report("postcopy_start: Migration stream errored");
48781e5b 2150 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1d34e4bf
DDAG
2151 MIGRATION_STATUS_FAILED);
2152 }
2153
2154 return ret;
2155
2156fail_closefb:
2157 qemu_fclose(fb);
2158fail:
48781e5b 2159 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1d34e4bf 2160 MIGRATION_STATUS_FAILED);
ef8d6488
DDAG
2161 if (restart_block) {
2162 /* A failure happened early enough that we know the destination hasn't
2163 * accessed block devices, so we're safe to recover.
2164 */
2165 Error *local_err = NULL;
2166
2167 bdrv_invalidate_cache_all(&local_err);
2168 if (local_err) {
2169 error_report_err(local_err);
2170 }
2171 }
1d34e4bf
DDAG
2172 qemu_mutex_unlock_iothread();
2173 return -1;
2174}
2175
e91d8951
DDAG
2176/**
2177 * migration_maybe_pause: Pause if required to by
2178 * migrate_pause_before_switchover called with the iothread locked
2179 * Returns: 0 on success
2180 */
0331c8ca
DDAG
2181static int migration_maybe_pause(MigrationState *s,
2182 int *current_active_state,
2183 int new_state)
e91d8951
DDAG
2184{
2185 if (!migrate_pause_before_switchover()) {
2186 return 0;
2187 }
2188
2189 /* Since leaving this state is not atomic with posting the semaphore
2190 * it's possible that someone could have issued multiple migrate_continue
2191 * and the semaphore is incorrectly positive at this point;
2192 * the docs say it's undefined to reinit a semaphore that's already
2193 * init'd, so use timedwait to eat up any existing posts.
2194 */
2195 while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) {
2196 /* This block intentionally left blank */
2197 }
2198
2199 qemu_mutex_unlock_iothread();
2200 migrate_set_state(&s->state, *current_active_state,
2201 MIGRATION_STATUS_PRE_SWITCHOVER);
2202 qemu_sem_wait(&s->pause_sem);
2203 migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER,
0331c8ca
DDAG
2204 new_state);
2205 *current_active_state = new_state;
e91d8951
DDAG
2206 qemu_mutex_lock_iothread();
2207
0331c8ca 2208 return s->state == new_state ? 0 : -EINVAL;
e91d8951
DDAG
2209}
2210
09f6c85e
DDAG
2211/**
2212 * migration_completion: Used by migration_thread when there's not much left.
2213 * The caller 'breaks' the loop when this returns.
2214 *
2215 * @s: Current migration state
09f6c85e 2216 */
2ad87305 2217static void migration_completion(MigrationState *s)
09f6c85e
DDAG
2218{
2219 int ret;
2ad87305 2220 int current_active_state = s->state;
09f6c85e 2221
b10ac0c4
DDAG
2222 if (s->state == MIGRATION_STATUS_ACTIVE) {
2223 qemu_mutex_lock_iothread();
64909f97 2224 s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
b10ac0c4 2225 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
7287cbd4 2226 s->vm_was_running = runstate_is_running();
b10ac0c4
DDAG
2227 ret = global_state_store();
2228
2229 if (!ret) {
a1fbe750 2230 bool inactivate = !migrate_colo_enabled();
b10ac0c4 2231 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
e91d8951 2232 if (ret >= 0) {
0331c8ca
DDAG
2233 ret = migration_maybe_pause(s, &current_active_state,
2234 MIGRATION_STATUS_DEVICE);
e91d8951 2235 }
f07fa4cb
KW
2236 if (ret >= 0) {
2237 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
a1fbe750
FZ
2238 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
2239 inactivate);
f07fa4cb 2240 }
a1fbe750
FZ
2241 if (inactivate && ret >= 0) {
2242 s->block_inactive = true;
b10ac0c4
DDAG
2243 }
2244 }
2245 qemu_mutex_unlock_iothread();
09f6c85e 2246
b10ac0c4
DDAG
2247 if (ret < 0) {
2248 goto fail;
09f6c85e 2249 }
b10ac0c4
DDAG
2250 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2251 trace_migration_completion_postcopy_end();
2252
89a02a9f 2253 qemu_savevm_state_complete_postcopy(s->to_dst_file);
b10ac0c4 2254 trace_migration_completion_postcopy_end_after_complete();
09f6c85e 2255 }
09f6c85e 2256
b10ac0c4
DDAG
2257 /*
2258 * If rp was opened we must clean up the thread before
2259 * cleaning everything else up (since if there are no failures
2260 * it will wait for the destination to send it's status in
2261 * a SHUT command).
b10ac0c4 2262 */
0425dc97 2263 if (s->rp_state.from_dst_file) {
b10ac0c4 2264 int rp_error;
0425dc97 2265 trace_migration_return_path_end_before();
b10ac0c4 2266 rp_error = await_return_path_close_on_source(s);
0425dc97 2267 trace_migration_return_path_end_after(rp_error);
b10ac0c4 2268 if (rp_error) {
fe904ea8 2269 goto fail_invalidate;
b10ac0c4 2270 }
09f6c85e
DDAG
2271 }
2272
89a02a9f 2273 if (qemu_file_get_error(s->to_dst_file)) {
09f6c85e 2274 trace_migration_completion_file_err();
fe904ea8 2275 goto fail_invalidate;
09f6c85e
DDAG
2276 }
2277
0b827d5e
HZ
2278 if (!migrate_colo_enabled()) {
2279 migrate_set_state(&s->state, current_active_state,
2280 MIGRATION_STATUS_COMPLETED);
2281 }
2282
09f6c85e
DDAG
2283 return;
2284
fe904ea8
GK
2285fail_invalidate:
2286 /* If not doing postcopy, vm_start() will be called: let's regain
2287 * control on images.
2288 */
6039dd5b
DDAG
2289 if (s->state == MIGRATION_STATUS_ACTIVE ||
2290 s->state == MIGRATION_STATUS_DEVICE) {
fe904ea8
GK
2291 Error *local_err = NULL;
2292
1d2acc31 2293 qemu_mutex_lock_iothread();
fe904ea8
GK
2294 bdrv_invalidate_cache_all(&local_err);
2295 if (local_err) {
2296 error_report_err(local_err);
1d2acc31
HZ
2297 } else {
2298 s->block_inactive = false;
fe904ea8 2299 }
1d2acc31 2300 qemu_mutex_unlock_iothread();
fe904ea8
GK
2301 }
2302
09f6c85e 2303fail:
48781e5b
HZ
2304 migrate_set_state(&s->state, current_active_state,
2305 MIGRATION_STATUS_FAILED);
09f6c85e
DDAG
2306}
2307
35a6ed4f
HZ
2308bool migrate_colo_enabled(void)
2309{
2310 MigrationState *s = migrate_get_current();
2311 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
2312}
2313
b23c2ade
PX
2314typedef enum MigThrError {
2315 /* No error detected */
2316 MIG_THR_ERR_NONE = 0,
2317 /* Detected error, but resumed successfully */
2318 MIG_THR_ERR_RECOVERED = 1,
2319 /* Detected fatal error, need to exit */
2320 MIG_THR_ERR_FATAL = 2,
2321} MigThrError;
2322
135b87b4
PX
2323/* Return zero if success, or <0 for error */
2324static int postcopy_do_resume(MigrationState *s)
2325{
2326 /* TODO: do the resume logic */
2327 return 0;
2328}
2329
b23c2ade
PX
2330/*
2331 * We don't return until we are in a safe state to continue current
2332 * postcopy migration. Returns MIG_THR_ERR_RECOVERED if recovered, or
2333 * MIG_THR_ERR_FATAL if unrecovery failure happened.
2334 */
2335static MigThrError postcopy_pause(MigrationState *s)
2336{
2337 assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
b23c2ade 2338
135b87b4
PX
2339 while (true) {
2340 migrate_set_state(&s->state, s->state,
2341 MIGRATION_STATUS_POSTCOPY_PAUSED);
b23c2ade 2342
135b87b4
PX
2343 /* Current channel is possibly broken. Release it. */
2344 assert(s->to_dst_file);
2345 qemu_file_shutdown(s->to_dst_file);
2346 qemu_fclose(s->to_dst_file);
2347 s->to_dst_file = NULL;
b23c2ade 2348
135b87b4
PX
2349 error_report("Detected IO failure for postcopy. "
2350 "Migration paused.");
b23c2ade 2351
135b87b4
PX
2352 /*
2353 * We wait until things fixed up. Then someone will setup the
2354 * status back for us.
2355 */
2356 while (s->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
2357 qemu_sem_wait(&s->postcopy_pause_sem);
2358 }
2359
2360 if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
2361 /* Woken up by a recover procedure. Give it a shot */
b23c2ade 2362
135b87b4
PX
2363 /*
2364 * Firstly, let's wake up the return path now, with a new
2365 * return path channel.
2366 */
2367 qemu_sem_post(&s->postcopy_pause_rp_sem);
2368
2369 /* Do the resume logic */
2370 if (postcopy_do_resume(s) == 0) {
2371 /* Let's continue! */
2372 trace_postcopy_pause_continued();
2373 return MIG_THR_ERR_RECOVERED;
2374 } else {
2375 /*
2376 * Something wrong happened during the recovery, let's
2377 * pause again. Pause is always better than throwing
2378 * data away.
2379 */
2380 continue;
2381 }
2382 } else {
2383 /* This is not right... Time to quit. */
2384 return MIG_THR_ERR_FATAL;
2385 }
2386 }
b23c2ade
PX
2387}
2388
2389static MigThrError migration_detect_error(MigrationState *s)
2390{
2391 int ret;
2392
2393 /* Try to detect any file errors */
2394 ret = qemu_file_get_error(s->to_dst_file);
2395
2396 if (!ret) {
2397 /* Everything is fine */
2398 return MIG_THR_ERR_NONE;
2399 }
2400
2401 if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE && ret == -EIO) {
2402 /*
2403 * For postcopy, we allow the network to be down for a
2404 * while. After that, it can be continued by a
2405 * recovery phase.
2406 */
2407 return postcopy_pause(s);
2408 } else {
2409 /*
2410 * For precopy (or postcopy with error outside IO), we fail
2411 * with no time.
2412 */
2413 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
2414 trace_migration_thread_file_err();
2415
2416 /* Time to stop the migration, now. */
2417 return MIG_THR_ERR_FATAL;
2418 }
2419}
2420
cf011f08
PX
2421static void migration_calculate_complete(MigrationState *s)
2422{
2423 uint64_t bytes = qemu_ftell(s->to_dst_file);
2424 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2425
2426 s->total_time = end_time - s->start_time;
2427 if (!s->downtime) {
2428 /*
2429 * It's still not set, so we are precopy migration. For
2430 * postcopy, downtime is calculated during postcopy_start().
2431 */
2432 s->downtime = end_time - s->downtime_start;
2433 }
2434
2435 if (s->total_time) {
2436 s->mbps = ((double) bytes * 8.0) / s->total_time / 1000;
2437 }
2438}
2439
b15df1ae
PX
2440static void migration_update_counters(MigrationState *s,
2441 int64_t current_time)
2442{
2443 uint64_t transferred, time_spent;
b15df1ae
PX
2444 double bandwidth;
2445
2446 if (current_time < s->iteration_start_time + BUFFER_DELAY) {
2447 return;
2448 }
2449
2450 transferred = qemu_ftell(s->to_dst_file) - s->iteration_initial_bytes;
2451 time_spent = current_time - s->iteration_start_time;
2452 bandwidth = (double)transferred / time_spent;
0781c1ed 2453 s->threshold_size = bandwidth * s->parameters.downtime_limit;
b15df1ae
PX
2454
2455 s->mbps = (((double) transferred * 8.0) /
2456 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
2457
2458 /*
2459 * if we haven't sent anything, we don't want to
2460 * recalculate. 10000 is a small enough number for our purposes
2461 */
2462 if (ram_counters.dirty_pages_rate && transferred > 10000) {
2463 s->expected_downtime = ram_counters.dirty_pages_rate *
2464 qemu_target_page_size() / bandwidth;
2465 }
2466
2467 qemu_file_reset_rate_limit(s->to_dst_file);
2468
2469 s->iteration_start_time = current_time;
2470 s->iteration_initial_bytes = qemu_ftell(s->to_dst_file);
2471
2472 trace_migrate_transferred(transferred, time_spent,
0781c1ed 2473 bandwidth, s->threshold_size);
b15df1ae
PX
2474}
2475
2ad87305
PX
2476/* Migration thread iteration status */
2477typedef enum {
2478 MIG_ITERATE_RESUME, /* Resume current iteration */
2479 MIG_ITERATE_SKIP, /* Skip current iteration */
2480 MIG_ITERATE_BREAK, /* Break the loop */
2481} MigIterateState;
2482
2483/*
2484 * Return true if continue to the next iteration directly, false
2485 * otherwise.
2486 */
2487static MigIterateState migration_iteration_run(MigrationState *s)
2488{
47995026 2489 uint64_t pending_size, pend_pre, pend_compat, pend_post;
2ad87305
PX
2490 bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE;
2491
47995026
VSO
2492 qemu_savevm_state_pending(s->to_dst_file, s->threshold_size, &pend_pre,
2493 &pend_compat, &pend_post);
2494 pending_size = pend_pre + pend_compat + pend_post;
2ad87305
PX
2495
2496 trace_migrate_pending(pending_size, s->threshold_size,
47995026 2497 pend_pre, pend_compat, pend_post);
2ad87305
PX
2498
2499 if (pending_size && pending_size >= s->threshold_size) {
2500 /* Still a significant amount to transfer */
2501 if (migrate_postcopy() && !in_postcopy &&
47995026 2502 pend_pre <= s->threshold_size &&
2ad87305
PX
2503 atomic_read(&s->start_postcopy)) {
2504 if (postcopy_start(s)) {
2505 error_report("%s: postcopy failed to start", __func__);
2506 }
2507 return MIG_ITERATE_SKIP;
2508 }
2509 /* Just another iteration step */
2510 qemu_savevm_state_iterate(s->to_dst_file,
2511 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
2512 } else {
2513 trace_migration_thread_low_pending(pending_size);
2514 migration_completion(s);
2515 return MIG_ITERATE_BREAK;
2516 }
2517
2518 return MIG_ITERATE_RESUME;
2519}
2520
199aa6d4
PX
2521static void migration_iteration_finish(MigrationState *s)
2522{
2523 /* If we enabled cpu throttling for auto-converge, turn it off. */
2524 cpu_throttle_stop();
2525
2526 qemu_mutex_lock_iothread();
2527 switch (s->state) {
2528 case MIGRATION_STATUS_COMPLETED:
2529 migration_calculate_complete(s);
2530 runstate_set(RUN_STATE_POSTMIGRATE);
2531 break;
2532
2533 case MIGRATION_STATUS_ACTIVE:
2534 /*
2535 * We should really assert here, but since it's during
2536 * migration, let's try to reduce the usage of assertions.
2537 */
2538 if (!migrate_colo_enabled()) {
2539 error_report("%s: critical error: calling COLO code without "
2540 "COLO enabled", __func__);
2541 }
2542 migrate_start_colo_process(s);
2543 /*
2544 * Fixme: we will run VM in COLO no matter its old running state.
2545 * After exited COLO, we will keep running.
2546 */
2547 s->vm_was_running = true;
2548 /* Fallthrough */
2549 case MIGRATION_STATUS_FAILED:
2550 case MIGRATION_STATUS_CANCELLED:
2551 if (s->vm_was_running) {
2552 vm_start();
2553 } else {
2554 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
2555 runstate_set(RUN_STATE_POSTMIGRATE);
2556 }
2557 }
2558 break;
2559
2560 default:
2561 /* Should not reach here, but if so, forgive the VM. */
2562 error_report("%s: Unknown ending state %d", __func__, s->state);
2563 break;
2564 }
2565 qemu_bh_schedule(s->cleanup_bh);
2566 qemu_mutex_unlock_iothread();
2567}
2568
70b20477
DDAG
2569/*
2570 * Master migration thread on the source VM.
2571 * It drives the migration and pumps the data down the outgoing channel.
2572 */
5f496a1b 2573static void *migration_thread(void *opaque)
0d82d0e8 2574{
9848a404 2575 MigrationState *s = opaque;
bc72ad67 2576 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
b23c2ade 2577 MigThrError thr_error;
76f5933a 2578
ab28bd23
PB
2579 rcu_register_thread();
2580
b15df1ae
PX
2581 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2582
89a02a9f 2583 qemu_savevm_state_header(s->to_dst_file);
1d34e4bf 2584
62a02658
PX
2585 /*
2586 * If we opened the return path, we need to make sure dst has it
2587 * opened as well.
2588 */
2589 if (s->rp_state.from_dst_file) {
1d34e4bf 2590 /* Now tell the dest that it should open its end so it can reply */
89a02a9f 2591 qemu_savevm_send_open_return_path(s->to_dst_file);
1d34e4bf
DDAG
2592
2593 /* And do a ping that will make stuff easier to debug */
89a02a9f 2594 qemu_savevm_send_ping(s->to_dst_file, 1);
0425dc97 2595 }
1d34e4bf 2596
58110f0a 2597 if (migrate_postcopy()) {
1d34e4bf
DDAG
2598 /*
2599 * Tell the destination that we *might* want to do postcopy later;
2600 * if the other end can't do postcopy it should fail now, nice and
2601 * early.
2602 */
89a02a9f 2603 qemu_savevm_send_postcopy_advise(s->to_dst_file);
1d34e4bf
DDAG
2604 }
2605
9907e842 2606 qemu_savevm_state_setup(s->to_dst_file);
0d82d0e8 2607
bc72ad67 2608 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
48781e5b
HZ
2609 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2610 MIGRATION_STATUS_ACTIVE);
29ae8a41 2611
9ec055ae
DDAG
2612 trace_migration_thread_setup_complete();
2613
2614 while (s->state == MIGRATION_STATUS_ACTIVE ||
2615 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
a3e879cd 2616 int64_t current_time;
0d82d0e8 2617
89a02a9f 2618 if (!qemu_file_rate_limit(s->to_dst_file)) {
2ad87305
PX
2619 MigIterateState iter_state = migration_iteration_run(s);
2620 if (iter_state == MIG_ITERATE_SKIP) {
2621 continue;
2622 } else if (iter_state == MIG_ITERATE_BREAK) {
09f6c85e 2623 break;
c369f40d
JQ
2624 }
2625 }
f4410a5d 2626
b23c2ade
PX
2627 /*
2628 * Try to detect any kind of failures, and see whether we
2629 * should stop the migration now.
2630 */
2631 thr_error = migration_detect_error(s);
2632 if (thr_error == MIG_THR_ERR_FATAL) {
2633 /* Stop migration */
fd45ee2c 2634 break;
b23c2ade
PX
2635 } else if (thr_error == MIG_THR_ERR_RECOVERED) {
2636 /*
2637 * Just recovered from a e.g. network failure, reset all
2638 * the local variables. This is important to avoid
2639 * breaking transferred_bytes and bandwidth calculation
2640 */
2641 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2642 s->iteration_initial_bytes = 0;
fd45ee2c 2643 }
b15df1ae 2644
bc72ad67 2645 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
0d82d0e8 2646
b15df1ae
PX
2647 migration_update_counters(s, current_time);
2648
89a02a9f 2649 if (qemu_file_rate_limit(s->to_dst_file)) {
0d82d0e8 2650 /* usleep expects microseconds */
b15df1ae
PX
2651 g_usleep((s->iteration_start_time + BUFFER_DELAY -
2652 current_time) * 1000);
0d82d0e8 2653 }
a3fa1d78
PB
2654 }
2655
1d34e4bf 2656 trace_migration_thread_after_loop();
199aa6d4 2657 migration_iteration_finish(s);
ab28bd23 2658 rcu_unregister_thread();
0d82d0e8
JQ
2659 return NULL;
2660}
2661
cce8040b 2662void migrate_fd_connect(MigrationState *s, Error *error_in)
0d82d0e8 2663{
d3e35b8f
PX
2664 int64_t rate_limit;
2665 bool resume = s->state == MIGRATION_STATUS_POSTCOPY_PAUSED;
2666
2ff30257 2667 s->expected_downtime = s->parameters.downtime_limit;
bb1fadc4 2668 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
cce8040b
DDAG
2669 if (error_in) {
2670 migrate_fd_error(s, error_in);
2671 migrate_fd_cleanup(s);
2672 return;
2673 }
0d82d0e8 2674
d3e35b8f
PX
2675 if (resume) {
2676 /* This is a resumed migration */
2677 rate_limit = INT64_MAX;
2678 } else {
2679 /* This is a fresh new migration */
2680 rate_limit = s->parameters.max_bandwidth / XFER_LIMIT_RATIO;
2681 s->expected_downtime = s->parameters.downtime_limit;
2682 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
442773ce 2683
d3e35b8f
PX
2684 /* Notify before starting migration thread */
2685 notifier_list_notify(&migration_state_notifiers, s);
2686 }
2687
2688 qemu_file_set_rate_limit(s->to_dst_file, rate_limit);
2689 qemu_file_set_blocking(s->to_dst_file, true);
9287ac27 2690
1d34e4bf 2691 /*
c788ada8
PX
2692 * Open the return path. For postcopy, it is used exclusively. For
2693 * precopy, only if user specified "return-path" capability would
2694 * QEMU uses the return path.
1d34e4bf 2695 */
c788ada8 2696 if (migrate_postcopy_ram() || migrate_use_return_path()) {
d3e35b8f 2697 if (open_return_path_on_source(s, !resume)) {
1d34e4bf 2698 error_report("Unable to open return-path for postcopy");
d3e35b8f 2699 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
1d34e4bf
DDAG
2700 migrate_fd_cleanup(s);
2701 return;
2702 }
2703 }
2704
d3e35b8f 2705 if (resume) {
135b87b4
PX
2706 /* Wakeup the main migration thread to do the recovery */
2707 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
2708 MIGRATION_STATUS_POSTCOPY_RECOVER);
2709 qemu_sem_post(&s->postcopy_pause_sem);
d3e35b8f
PX
2710 return;
2711 }
2712
f986c3d2
JQ
2713 if (multifd_save_setup() != 0) {
2714 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2715 MIGRATION_STATUS_FAILED);
2716 migrate_fd_cleanup(s);
2717 return;
2718 }
009fad7f 2719 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
bb1fadc4 2720 QEMU_THREAD_JOINABLE);
1d34e4bf 2721 s->migration_thread_running = true;
0d82d0e8 2722}
093e3c42 2723
9d18af93
PX
2724void migration_global_dump(Monitor *mon)
2725{
2726 MigrationState *ms = migrate_get_current();
2727
6f0f6428
JQ
2728 monitor_printf(mon, "globals:\n");
2729 monitor_printf(mon, "store-global-state: %s\n",
2730 ms->store_global_state ? "on" : "off");
2731 monitor_printf(mon, "only-migratable: %s\n",
2732 ms->only_migratable ? "on" : "off");
2733 monitor_printf(mon, "send-configuration: %s\n",
2734 ms->send_configuration ? "on" : "off");
2735 monitor_printf(mon, "send-section-footer: %s\n",
2736 ms->send_section_footer ? "on" : "off");
9d18af93
PX
2737}
2738
20814758
PX
2739#define DEFINE_PROP_MIG_CAP(name, x) \
2740 DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
2741
5272298c
PX
2742static Property migration_properties[] = {
2743 DEFINE_PROP_BOOL("store-global-state", MigrationState,
2744 store_global_state, true),
3df663e5 2745 DEFINE_PROP_BOOL("only-migratable", MigrationState, only_migratable, false),
71dd4c1a
PX
2746 DEFINE_PROP_BOOL("send-configuration", MigrationState,
2747 send_configuration, true),
15c38503
PX
2748 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
2749 send_section_footer, true),
89632faf
PX
2750
2751 /* Migration parameters */
741d4086 2752 DEFINE_PROP_UINT8("x-compress-level", MigrationState,
89632faf
PX
2753 parameters.compress_level,
2754 DEFAULT_MIGRATE_COMPRESS_LEVEL),
741d4086 2755 DEFINE_PROP_UINT8("x-compress-threads", MigrationState,
89632faf
PX
2756 parameters.compress_threads,
2757 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
741d4086 2758 DEFINE_PROP_UINT8("x-decompress-threads", MigrationState,
89632faf
PX
2759 parameters.decompress_threads,
2760 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
741d4086 2761 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
89632faf
PX
2762 parameters.cpu_throttle_initial,
2763 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
741d4086 2764 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
89632faf
PX
2765 parameters.cpu_throttle_increment,
2766 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
741d4086 2767 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
89632faf 2768 parameters.max_bandwidth, MAX_THROTTLE),
741d4086 2769 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
89632faf
PX
2770 parameters.downtime_limit,
2771 DEFAULT_MIGRATE_SET_DOWNTIME),
741d4086 2772 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
89632faf
PX
2773 parameters.x_checkpoint_delay,
2774 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
741d4086 2775 DEFINE_PROP_UINT8("x-multifd-channels", MigrationState,
4075fb1c
JQ
2776 parameters.x_multifd_channels,
2777 DEFAULT_MIGRATE_MULTIFD_CHANNELS),
741d4086 2778 DEFINE_PROP_UINT32("x-multifd-page-count", MigrationState,
0fb86605
JQ
2779 parameters.x_multifd_page_count,
2780 DEFAULT_MIGRATE_MULTIFD_PAGE_COUNT),
73af8dd8
JQ
2781 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
2782 parameters.xbzrle_cache_size,
2783 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
20814758
PX
2784
2785 /* Migration capabilities */
2786 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
2787 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
2788 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
2789 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
2790 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
2791 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
2792 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
2793 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
2794 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
2795 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
2796 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
30126bbf 2797 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_X_MULTIFD),
20814758 2798
5272298c
PX
2799 DEFINE_PROP_END_OF_LIST(),
2800};
2801
e5cb7e76
PX
2802static void migration_class_init(ObjectClass *klass, void *data)
2803{
2804 DeviceClass *dc = DEVICE_CLASS(klass);
2805
2806 dc->user_creatable = false;
5272298c 2807 dc->props = migration_properties;
e5cb7e76
PX
2808}
2809
b91bf5e4
MAL
2810static void migration_instance_finalize(Object *obj)
2811{
2812 MigrationState *ms = MIGRATION_OBJ(obj);
2813 MigrationParameters *params = &ms->parameters;
2814
87db1a7d 2815 qemu_mutex_destroy(&ms->error_mutex);
b91bf5e4
MAL
2816 g_free(params->tls_hostname);
2817 g_free(params->tls_creds);
e91d8951 2818 qemu_sem_destroy(&ms->pause_sem);
b23c2ade 2819 qemu_sem_destroy(&ms->postcopy_pause_sem);
14b1742e 2820 qemu_sem_destroy(&ms->postcopy_pause_rp_sem);
ab105cc1 2821 error_free(ms->error);
b91bf5e4
MAL
2822}
2823
e5cb7e76
PX
2824static void migration_instance_init(Object *obj)
2825{
2826 MigrationState *ms = MIGRATION_OBJ(obj);
8b0b29dc 2827 MigrationParameters *params = &ms->parameters;
e5cb7e76
PX
2828
2829 ms->state = MIGRATION_STATUS_NONE;
e5cb7e76 2830 ms->mbps = -1;
e91d8951 2831 qemu_sem_init(&ms->pause_sem, 0);
87db1a7d 2832 qemu_mutex_init(&ms->error_mutex);
8b0b29dc
PX
2833
2834 params->tls_hostname = g_strdup("");
2835 params->tls_creds = g_strdup("");
2836
2837 /* Set has_* up only for parameter checks */
2838 params->has_compress_level = true;
2839 params->has_compress_threads = true;
2840 params->has_decompress_threads = true;
2841 params->has_cpu_throttle_initial = true;
2842 params->has_cpu_throttle_increment = true;
2843 params->has_max_bandwidth = true;
2844 params->has_downtime_limit = true;
2845 params->has_x_checkpoint_delay = true;
2846 params->has_block_incremental = true;
4075fb1c 2847 params->has_x_multifd_channels = true;
0fb86605 2848 params->has_x_multifd_page_count = true;
73af8dd8 2849 params->has_xbzrle_cache_size = true;
b23c2ade
PX
2850
2851 qemu_sem_init(&ms->postcopy_pause_sem, 0);
14b1742e 2852 qemu_sem_init(&ms->postcopy_pause_rp_sem, 0);
8b0b29dc
PX
2853}
2854
2855/*
2856 * Return true if check pass, false otherwise. Error will be put
2857 * inside errp if provided.
2858 */
2859static bool migration_object_check(MigrationState *ms, Error **errp)
2860{
6b19a7d9
PX
2861 MigrationCapabilityStatusList *head = NULL;
2862 /* Assuming all off */
2863 bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret;
2864 int i;
2865
8b0b29dc
PX
2866 if (!migrate_params_check(&ms->parameters, errp)) {
2867 return false;
2868 }
2869
6b19a7d9
PX
2870 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
2871 if (ms->enabled_capabilities[i]) {
2872 head = migrate_cap_add(head, i, true);
2873 }
2874 }
2875
2876 ret = migrate_caps_check(cap_list, head, errp);
2877
2878 /* It works with head == NULL */
2879 qapi_free_MigrationCapabilityStatusList(head);
2880
2881 return ret;
e5cb7e76
PX
2882}
2883
2884static const TypeInfo migration_type = {
2885 .name = TYPE_MIGRATION,
01f6e14c 2886 /*
c8d3ff38
PX
2887 * NOTE: TYPE_MIGRATION is not really a device, as the object is
2888 * not created using qdev_create(), it is not attached to the qdev
2889 * device tree, and it is never realized.
2890 *
2891 * TODO: Make this TYPE_OBJECT once QOM provides something like
2892 * TYPE_DEVICE's "-global" properties.
01f6e14c 2893 */
e5cb7e76
PX
2894 .parent = TYPE_DEVICE,
2895 .class_init = migration_class_init,
2896 .class_size = sizeof(MigrationClass),
2897 .instance_size = sizeof(MigrationState),
2898 .instance_init = migration_instance_init,
b91bf5e4 2899 .instance_finalize = migration_instance_finalize,
e5cb7e76
PX
2900};
2901
2902static void register_migration_types(void)
2903{
2904 type_register_static(&migration_type);
2905}
2906
2907type_init(register_migration_types);