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