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