]> git.proxmox.com Git - mirror_qemu.git/blame - migration/migration.c
migration: create new section to store global state
[mirror_qemu.git] / migration / migration.c
CommitLineData
5bb7910a
AL
1/*
2 * QEMU live migration
3 *
4 * Copyright IBM, Corp. 2008
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
6b620ca3
PB
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
5bb7910a
AL
14 */
15
16#include "qemu-common.h"
d49b6836 17#include "qemu/error-report.h"
6a1751b7 18#include "qemu/main-loop.h"
caf71f86 19#include "migration/migration.h"
0d82d0e8 20#include "migration/qemu-file.h"
9c17d615 21#include "sysemu/sysemu.h"
737e150e 22#include "block/block.h"
cc7a8ea7 23#include "qapi/qmp/qerror.h"
1de7afc9 24#include "qemu/sockets.h"
caf71f86 25#include "migration/block.h"
766bd176 26#include "qemu/thread.h"
791e7c82 27#include "qmp-commands.h"
c09e5bb1 28#include "trace.h"
df4b1024 29#include "qapi/util.h"
065e2813 30
d0ae46c1 31#define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
5bb7910a 32
5b4e1eb7
JQ
33/* Amount of time to allocate to each "chunk" of bandwidth-throttled
34 * data. */
35#define BUFFER_DELAY 100
36#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
37
8706d2d5
LL
38/* Default compression thread count */
39#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
3fcb38c2
LL
40/* Default decompression thread count, usually decompression is at
41 * least 4 times as fast as compression.*/
42#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
8706d2d5
LL
43/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
44#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
45
17ad9b35
OW
46/* Migration XBZRLE default cache size */
47#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
48
99a0db9b
GH
49static NotifierList migration_state_notifiers =
50 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
51
adde220a
DDAG
52static bool deferred_incoming;
53
17549e84
JQ
54/* When we add fault tolerance, we could have several
55 migrations at once. For now we don't need to add
56 dynamic creation of migration */
57
bca7856a 58/* For outgoing */
859bc756 59MigrationState *migrate_get_current(void)
17549e84
JQ
60{
61 static MigrationState current_migration = {
31194731 62 .state = MIGRATION_STATUS_NONE,
d0ae46c1 63 .bandwidth_limit = MAX_THROTTLE,
17ad9b35 64 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
7e114f8c 65 .mbps = -1,
43c60a81
LL
66 .parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] =
67 DEFAULT_MIGRATE_COMPRESS_LEVEL,
68 .parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
69 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
70 .parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
71 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
17549e84
JQ
72 };
73
74 return &current_migration;
75}
76
bca7856a
DDAG
77/* For incoming */
78static MigrationIncomingState *mis_current;
79
80MigrationIncomingState *migration_incoming_get_current(void)
81{
82 return mis_current;
83}
84
85MigrationIncomingState *migration_incoming_state_new(QEMUFile* f)
86{
87 mis_current = g_malloc0(sizeof(MigrationIncomingState));
88 mis_current->file = f;
1a8f46f8 89 QLIST_INIT(&mis_current->loadvm_handlers);
bca7856a
DDAG
90
91 return mis_current;
92}
93
94void migration_incoming_state_destroy(void)
95{
1a8f46f8 96 loadvm_free_handlers(mis_current);
bca7856a
DDAG
97 g_free(mis_current);
98 mis_current = NULL;
99}
100
df4b1024
JQ
101
102typedef struct {
103 uint32_t size;
104 uint8_t runstate[100];
105} GlobalState;
106
107static GlobalState global_state;
108
109static int global_state_store(void)
110{
111 if (!runstate_store((char *)global_state.runstate,
112 sizeof(global_state.runstate))) {
113 error_report("runstate name too big: %s", global_state.runstate);
114 trace_migrate_state_too_big();
115 return -EINVAL;
116 }
117 return 0;
118}
119
120static char *global_state_get_runstate(void)
121{
122 return (char *)global_state.runstate;
123}
124
125static int global_state_post_load(void *opaque, int version_id)
126{
127 GlobalState *s = opaque;
128 int ret = 0;
129 char *runstate = (char *)s->runstate;
130
131 trace_migrate_global_state_post_load(runstate);
132
133 if (strcmp(runstate, "running") != 0) {
134 Error *local_err = NULL;
135 int r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE_MAX,
136 -1, &local_err);
137
138 if (r == -1) {
139 if (local_err) {
140 error_report_err(local_err);
141 }
142 return -EINVAL;
143 }
144 ret = vm_stop_force_state(r);
145 }
146
147 return ret;
148}
149
150static void global_state_pre_save(void *opaque)
151{
152 GlobalState *s = opaque;
153
154 trace_migrate_global_state_pre_save((char *)s->runstate);
155 s->size = strlen((char *)s->runstate) + 1;
156}
157
158static const VMStateDescription vmstate_globalstate = {
159 .name = "globalstate",
160 .version_id = 1,
161 .minimum_version_id = 1,
162 .post_load = global_state_post_load,
163 .pre_save = global_state_pre_save,
164 .fields = (VMStateField[]) {
165 VMSTATE_UINT32(size, GlobalState),
166 VMSTATE_BUFFER(runstate, GlobalState),
167 VMSTATE_END_OF_LIST()
168 },
169};
170
171void register_global_state(void)
172{
173 /* We would use it independently that we receive it */
174 strcpy((char *)&global_state.runstate, "");
175 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
176}
177
adde220a
DDAG
178/*
179 * Called on -incoming with a defer: uri.
180 * The migration can be started later after any parameters have been
181 * changed.
182 */
183static void deferred_incoming_migration(Error **errp)
184{
185 if (deferred_incoming) {
186 error_setg(errp, "Incoming migration already deferred");
187 }
188 deferred_incoming = true;
189}
190
43eaae28 191void qemu_start_incoming_migration(const char *uri, Error **errp)
5bb7910a 192{
34c9dd8e
AL
193 const char *p;
194
adde220a
DDAG
195 if (!strcmp(uri, "defer")) {
196 deferred_incoming_migration(errp);
197 } else if (strstart(uri, "tcp:", &p)) {
43eaae28 198 tcp_start_incoming_migration(p, errp);
2da776db 199#ifdef CONFIG_RDMA
adde220a 200 } else if (strstart(uri, "rdma:", &p)) {
2da776db
MH
201 rdma_start_incoming_migration(p, errp);
202#endif
065e2813 203#if !defined(WIN32)
adde220a 204 } else if (strstart(uri, "exec:", &p)) {
43eaae28 205 exec_start_incoming_migration(p, errp);
adde220a 206 } else if (strstart(uri, "unix:", &p)) {
43eaae28 207 unix_start_incoming_migration(p, errp);
adde220a 208 } else if (strstart(uri, "fd:", &p)) {
43eaae28 209 fd_start_incoming_migration(p, errp);
065e2813 210#endif
adde220a 211 } else {
312fd5f2 212 error_setg(errp, "unknown migration protocol: %s", uri);
8ca5e801 213 }
5bb7910a
AL
214}
215
82a4da79 216static void process_incoming_migration_co(void *opaque)
511c0231 217{
82a4da79 218 QEMUFile *f = opaque;
5a8a30db 219 Error *local_err = NULL;
1c12e1f5
PB
220 int ret;
221
bca7856a
DDAG
222 migration_incoming_state_new(f);
223
1c12e1f5 224 ret = qemu_loadvm_state(f);
bca7856a 225
1c12e1f5 226 qemu_fclose(f);
905f26f2 227 free_xbzrle_decoded_buf();
bca7856a
DDAG
228 migration_incoming_state_destroy();
229
1c12e1f5 230 if (ret < 0) {
db80face 231 error_report("load of migration failed: %s", strerror(-ret));
3fcb38c2 232 migrate_decompress_threads_join();
4aead692 233 exit(EXIT_FAILURE);
511c0231
JQ
234 }
235 qemu_announce_self();
511c0231 236
0f15423c 237 /* Make sure all file formats flush their mutable metadata */
5a8a30db
KW
238 bdrv_invalidate_cache_all(&local_err);
239 if (local_err) {
97baf9d9 240 error_report_err(local_err);
3fcb38c2 241 migrate_decompress_threads_join();
5a8a30db
KW
242 exit(EXIT_FAILURE);
243 }
0f15423c 244
df4b1024
JQ
245 /* runstate == "" means that we haven't received it through the
246 * wire, so we obey autostart. runstate == runing means that we
247 * need to run it, we need to make sure that we do it after
248 * everything else has finished. Every other state change is done
249 * at the post_load function */
250
251 if (strcmp(global_state_get_runstate(), "running") == 0) {
511c0231 252 vm_start();
df4b1024
JQ
253 } else if (strcmp(global_state_get_runstate(), "") == 0) {
254 if (autostart) {
255 vm_start();
256 } else {
257 runstate_set(RUN_STATE_PAUSED);
258 }
f5bbfba1 259 }
3fcb38c2 260 migrate_decompress_threads_join();
511c0231
JQ
261}
262
82a4da79
PB
263void process_incoming_migration(QEMUFile *f)
264{
265 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
266 int fd = qemu_get_fd(f);
267
268 assert(fd != -1);
3fcb38c2 269 migrate_decompress_threads_create();
f9e8cacc 270 qemu_set_nonblock(fd);
82a4da79
PB
271 qemu_coroutine_enter(co, f);
272}
273
a0a3fd60
GC
274/* amount of nanoseconds we are willing to wait for migration to be down.
275 * the choice of nanoseconds is because it is the maximum resolution that
276 * get_clock() can achieve. It is an internal measure. All user-visible
277 * units must be in seconds */
f7cd55a0 278static uint64_t max_downtime = 300000000;
a0a3fd60
GC
279
280uint64_t migrate_max_downtime(void)
281{
282 return max_downtime;
283}
284
bbf6da32
OW
285MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
286{
287 MigrationCapabilityStatusList *head = NULL;
288 MigrationCapabilityStatusList *caps;
289 MigrationState *s = migrate_get_current();
290 int i;
291
387eedeb 292 caps = NULL; /* silence compiler warning */
bbf6da32
OW
293 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
294 if (head == NULL) {
295 head = g_malloc0(sizeof(*caps));
296 caps = head;
297 } else {
298 caps->next = g_malloc0(sizeof(*caps));
299 caps = caps->next;
300 }
301 caps->value =
302 g_malloc(sizeof(*caps->value));
303 caps->value->capability = i;
304 caps->value->state = s->enabled_capabilities[i];
305 }
306
307 return head;
308}
309
85de8323
LL
310MigrationParameters *qmp_query_migrate_parameters(Error **errp)
311{
312 MigrationParameters *params;
313 MigrationState *s = migrate_get_current();
314
315 params = g_malloc0(sizeof(*params));
316 params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
317 params->compress_threads =
318 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
319 params->decompress_threads =
320 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
321
322 return params;
323}
324
f36d55af
OW
325static void get_xbzrle_cache_stats(MigrationInfo *info)
326{
327 if (migrate_use_xbzrle()) {
328 info->has_xbzrle_cache = true;
329 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
330 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
331 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
332 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
333 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
8bc39233 334 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
f36d55af
OW
335 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
336 }
337}
338
791e7c82 339MigrationInfo *qmp_query_migrate(Error **errp)
5bb7910a 340{
791e7c82 341 MigrationInfo *info = g_malloc0(sizeof(*info));
17549e84
JQ
342 MigrationState *s = migrate_get_current();
343
344 switch (s->state) {
31194731 345 case MIGRATION_STATUS_NONE:
17549e84
JQ
346 /* no migration has happened ever */
347 break;
31194731 348 case MIGRATION_STATUS_SETUP:
29ae8a41 349 info->has_status = true;
ed4fbd10 350 info->has_total_time = false;
29ae8a41 351 break;
31194731
HZ
352 case MIGRATION_STATUS_ACTIVE:
353 case MIGRATION_STATUS_CANCELLING:
791e7c82 354 info->has_status = true;
7aa939af 355 info->has_total_time = true;
bc72ad67 356 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
7aa939af 357 - s->total_time;
2c52ddf1
JQ
358 info->has_expected_downtime = true;
359 info->expected_downtime = s->expected_downtime;
ed4fbd10
MH
360 info->has_setup_time = true;
361 info->setup_time = s->setup_time;
17549e84 362
791e7c82
LC
363 info->has_ram = true;
364 info->ram = g_malloc0(sizeof(*info->ram));
365 info->ram->transferred = ram_bytes_transferred();
366 info->ram->remaining = ram_bytes_remaining();
367 info->ram->total = ram_bytes_total();
004d4c10 368 info->ram->duplicate = dup_mig_pages_transferred();
f1c72795 369 info->ram->skipped = skipped_mig_pages_transferred();
004d4c10
OW
370 info->ram->normal = norm_mig_pages_transferred();
371 info->ram->normal_bytes = norm_mig_bytes_transferred();
8d017193 372 info->ram->dirty_pages_rate = s->dirty_pages_rate;
7e114f8c 373 info->ram->mbps = s->mbps;
58570ed8 374 info->ram->dirty_sync_count = s->dirty_sync_count;
8d017193 375
17549e84 376 if (blk_mig_active()) {
791e7c82
LC
377 info->has_disk = true;
378 info->disk = g_malloc0(sizeof(*info->disk));
379 info->disk->transferred = blk_mig_bytes_transferred();
380 info->disk->remaining = blk_mig_bytes_remaining();
381 info->disk->total = blk_mig_bytes_total();
ff8d81d8 382 }
f36d55af
OW
383
384 get_xbzrle_cache_stats(info);
17549e84 385 break;
31194731 386 case MIGRATION_STATUS_COMPLETED:
f36d55af
OW
387 get_xbzrle_cache_stats(info);
388
791e7c82 389 info->has_status = true;
00c14997 390 info->has_total_time = true;
7aa939af 391 info->total_time = s->total_time;
9c5a9fcf
JQ
392 info->has_downtime = true;
393 info->downtime = s->downtime;
ed4fbd10
MH
394 info->has_setup_time = true;
395 info->setup_time = s->setup_time;
d5f8a570
JQ
396
397 info->has_ram = true;
398 info->ram = g_malloc0(sizeof(*info->ram));
399 info->ram->transferred = ram_bytes_transferred();
400 info->ram->remaining = 0;
401 info->ram->total = ram_bytes_total();
004d4c10 402 info->ram->duplicate = dup_mig_pages_transferred();
f1c72795 403 info->ram->skipped = skipped_mig_pages_transferred();
004d4c10
OW
404 info->ram->normal = norm_mig_pages_transferred();
405 info->ram->normal_bytes = norm_mig_bytes_transferred();
7e114f8c 406 info->ram->mbps = s->mbps;
58570ed8 407 info->ram->dirty_sync_count = s->dirty_sync_count;
17549e84 408 break;
31194731 409 case MIGRATION_STATUS_FAILED:
791e7c82 410 info->has_status = true;
17549e84 411 break;
31194731 412 case MIGRATION_STATUS_CANCELLED:
791e7c82 413 info->has_status = true;
17549e84 414 break;
5bb7910a 415 }
cde63fbe 416 info->status = s->state;
791e7c82
LC
417
418 return info;
5bb7910a
AL
419}
420
00458433
OW
421void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
422 Error **errp)
423{
424 MigrationState *s = migrate_get_current();
425 MigrationCapabilityStatusList *cap;
426
31194731
HZ
427 if (s->state == MIGRATION_STATUS_ACTIVE ||
428 s->state == MIGRATION_STATUS_SETUP) {
c6bd8c70 429 error_setg(errp, QERR_MIGRATION_ACTIVE);
00458433
OW
430 return;
431 }
432
433 for (cap = params; cap; cap = cap->next) {
434 s->enabled_capabilities[cap->value->capability] = cap->value->state;
435 }
436}
437
85de8323
LL
438void qmp_migrate_set_parameters(bool has_compress_level,
439 int64_t compress_level,
440 bool has_compress_threads,
441 int64_t compress_threads,
442 bool has_decompress_threads,
443 int64_t decompress_threads, Error **errp)
444{
445 MigrationState *s = migrate_get_current();
446
447 if (has_compress_level && (compress_level < 0 || compress_level > 9)) {
c6bd8c70
MA
448 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
449 "is invalid, it should be in the range of 0 to 9");
85de8323
LL
450 return;
451 }
452 if (has_compress_threads &&
453 (compress_threads < 1 || compress_threads > 255)) {
c6bd8c70
MA
454 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
455 "compress_threads",
456 "is invalid, it should be in the range of 1 to 255");
85de8323
LL
457 return;
458 }
459 if (has_decompress_threads &&
460 (decompress_threads < 1 || decompress_threads > 255)) {
c6bd8c70
MA
461 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
462 "decompress_threads",
463 "is invalid, it should be in the range of 1 to 255");
85de8323
LL
464 return;
465 }
466
467 if (has_compress_level) {
468 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
469 }
470 if (has_compress_threads) {
471 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads;
472 }
473 if (has_decompress_threads) {
474 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
475 decompress_threads;
476 }
477}
478
065e2813
AL
479/* shared migration helpers */
480
51cf4c1a
Z
481static void migrate_set_state(MigrationState *s, int old_state, int new_state)
482{
483 if (atomic_cmpxchg(&s->state, old_state, new_state) == new_state) {
484 trace_migrate_set_state(new_state);
485 }
486}
487
bb1fadc4 488static void migrate_fd_cleanup(void *opaque)
065e2813 489{
bb1fadc4
PB
490 MigrationState *s = opaque;
491
492 qemu_bh_delete(s->cleanup_bh);
493 s->cleanup_bh = NULL;
494
065e2813 495 if (s->file) {
9013dca5 496 trace_migrate_fd_cleanup();
404a7c05
PB
497 qemu_mutex_unlock_iothread();
498 qemu_thread_join(&s->thread);
499 qemu_mutex_lock_iothread();
500
8706d2d5 501 migrate_compress_threads_join();
6f190a06
PB
502 qemu_fclose(s->file);
503 s->file = NULL;
065e2813
AL
504 }
505
31194731 506 assert(s->state != MIGRATION_STATUS_ACTIVE);
7a2c1721 507
31194731 508 if (s->state != MIGRATION_STATUS_COMPLETED) {
7a2c1721 509 qemu_savevm_state_cancel();
31194731
HZ
510 if (s->state == MIGRATION_STATUS_CANCELLING) {
511 migrate_set_state(s, MIGRATION_STATUS_CANCELLING,
512 MIGRATION_STATUS_CANCELLED);
51cf4c1a 513 }
7a2c1721 514 }
a3fa1d78
PB
515
516 notifier_list_notify(&migration_state_notifiers, s);
065e2813
AL
517}
518
8b6b99b3 519void migrate_fd_error(MigrationState *s)
065e2813 520{
9013dca5 521 trace_migrate_fd_error();
bb1fadc4 522 assert(s->file == NULL);
31194731
HZ
523 s->state = MIGRATION_STATUS_FAILED;
524 trace_migrate_set_state(MIGRATION_STATUS_FAILED);
bb1fadc4 525 notifier_list_notify(&migration_state_notifiers, s);
458cf28e
JQ
526}
527
0edda1c4 528static void migrate_fd_cancel(MigrationState *s)
065e2813 529{
6f2b811a 530 int old_state ;
a26ba26e 531 QEMUFile *f = migrate_get_current()->file;
9013dca5 532 trace_migrate_fd_cancel();
065e2813 533
6f2b811a
Z
534 do {
535 old_state = s->state;
31194731
HZ
536 if (old_state != MIGRATION_STATUS_SETUP &&
537 old_state != MIGRATION_STATUS_ACTIVE) {
6f2b811a
Z
538 break;
539 }
31194731
HZ
540 migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
541 } while (s->state != MIGRATION_STATUS_CANCELLING);
a26ba26e
DDAG
542
543 /*
544 * If we're unlucky the migration code might be stuck somewhere in a
545 * send/write while the network has failed and is waiting to timeout;
546 * if we've got shutdown(2) available then we can force it to quit.
547 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
548 * called in a bh, so there is no race against this cancel.
549 */
31194731 550 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
a26ba26e
DDAG
551 qemu_file_shutdown(f);
552 }
065e2813
AL
553}
554
99a0db9b
GH
555void add_migration_state_change_notifier(Notifier *notify)
556{
557 notifier_list_add(&migration_state_notifiers, notify);
558}
559
560void remove_migration_state_change_notifier(Notifier *notify)
561{
31552529 562 notifier_remove(notify);
99a0db9b
GH
563}
564
02edd2e7 565bool migration_in_setup(MigrationState *s)
afe2df69 566{
31194731 567 return s->state == MIGRATION_STATUS_SETUP;
afe2df69
GH
568}
569
7073693b 570bool migration_has_finished(MigrationState *s)
99a0db9b 571{
31194731 572 return s->state == MIGRATION_STATUS_COMPLETED;
99a0db9b 573}
0edda1c4 574
afe2df69
GH
575bool migration_has_failed(MigrationState *s)
576{
31194731
HZ
577 return (s->state == MIGRATION_STATUS_CANCELLED ||
578 s->state == MIGRATION_STATUS_FAILED);
afe2df69
GH
579}
580
6607ae23 581static MigrationState *migrate_init(const MigrationParams *params)
0edda1c4 582{
17549e84 583 MigrationState *s = migrate_get_current();
d0ae46c1 584 int64_t bandwidth_limit = s->bandwidth_limit;
bbf6da32 585 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
17ad9b35 586 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
43c60a81
LL
587 int compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
588 int compress_thread_count =
589 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
590 int decompress_thread_count =
591 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
bbf6da32
OW
592
593 memcpy(enabled_capabilities, s->enabled_capabilities,
594 sizeof(enabled_capabilities));
0edda1c4 595
17549e84 596 memset(s, 0, sizeof(*s));
6607ae23 597 s->params = *params;
bbf6da32
OW
598 memcpy(s->enabled_capabilities, enabled_capabilities,
599 sizeof(enabled_capabilities));
17ad9b35 600 s->xbzrle_cache_size = xbzrle_cache_size;
1299c631 601
43c60a81
LL
602 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
603 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
604 compress_thread_count;
605 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
606 decompress_thread_count;
0edda1c4 607 s->bandwidth_limit = bandwidth_limit;
31194731
HZ
608 s->state = MIGRATION_STATUS_SETUP;
609 trace_migrate_set_state(MIGRATION_STATUS_SETUP);
0edda1c4 610
bc72ad67 611 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
0edda1c4
JQ
612 return s;
613}
cab30143 614
fa2756b7
AL
615static GSList *migration_blockers;
616
617void migrate_add_blocker(Error *reason)
618{
619 migration_blockers = g_slist_prepend(migration_blockers, reason);
620}
621
622void migrate_del_blocker(Error *reason)
623{
624 migration_blockers = g_slist_remove(migration_blockers, reason);
625}
626
bf1ae1f4
DDAG
627void qmp_migrate_incoming(const char *uri, Error **errp)
628{
629 Error *local_err = NULL;
4debb5f5 630 static bool once = true;
bf1ae1f4
DDAG
631
632 if (!deferred_incoming) {
4debb5f5 633 error_setg(errp, "For use with '-incoming defer'");
bf1ae1f4
DDAG
634 return;
635 }
4debb5f5
DDAG
636 if (!once) {
637 error_setg(errp, "The incoming migration has already been started");
638 }
bf1ae1f4
DDAG
639
640 qemu_start_incoming_migration(uri, &local_err);
641
642 if (local_err) {
643 error_propagate(errp, local_err);
644 return;
645 }
646
4debb5f5 647 once = false;
bf1ae1f4
DDAG
648}
649
e1c37d0e
LC
650void qmp_migrate(const char *uri, bool has_blk, bool blk,
651 bool has_inc, bool inc, bool has_detach, bool detach,
652 Error **errp)
cab30143 653{
be7059cd 654 Error *local_err = NULL;
17549e84 655 MigrationState *s = migrate_get_current();
6607ae23 656 MigrationParams params;
cab30143 657 const char *p;
cab30143 658
8c0426ae
PP
659 params.blk = has_blk && blk;
660 params.shared = has_inc && inc;
6607ae23 661
31194731
HZ
662 if (s->state == MIGRATION_STATUS_ACTIVE ||
663 s->state == MIGRATION_STATUS_SETUP ||
664 s->state == MIGRATION_STATUS_CANCELLING) {
c6bd8c70 665 error_setg(errp, QERR_MIGRATION_ACTIVE);
e1c37d0e 666 return;
cab30143
JQ
667 }
668
ca99993a
DDAG
669 if (runstate_check(RUN_STATE_INMIGRATE)) {
670 error_setg(errp, "Guest is waiting for an incoming migration");
671 return;
672 }
673
e1c37d0e
LC
674 if (qemu_savevm_state_blocked(errp)) {
675 return;
cab30143
JQ
676 }
677
fa2756b7 678 if (migration_blockers) {
e1c37d0e
LC
679 *errp = error_copy(migration_blockers->data);
680 return;
fa2756b7
AL
681 }
682
6607ae23 683 s = migrate_init(&params);
cab30143
JQ
684
685 if (strstart(uri, "tcp:", &p)) {
f37afb5a 686 tcp_start_outgoing_migration(s, p, &local_err);
2da776db 687#ifdef CONFIG_RDMA
41310c68 688 } else if (strstart(uri, "rdma:", &p)) {
2da776db
MH
689 rdma_start_outgoing_migration(s, p, &local_err);
690#endif
cab30143
JQ
691#if !defined(WIN32)
692 } else if (strstart(uri, "exec:", &p)) {
f37afb5a 693 exec_start_outgoing_migration(s, p, &local_err);
cab30143 694 } else if (strstart(uri, "unix:", &p)) {
f37afb5a 695 unix_start_outgoing_migration(s, p, &local_err);
cab30143 696 } else if (strstart(uri, "fd:", &p)) {
f37afb5a 697 fd_start_outgoing_migration(s, p, &local_err);
cab30143 698#endif
99a0db9b 699 } else {
c6bd8c70
MA
700 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
701 "a valid migration protocol");
31194731 702 s->state = MIGRATION_STATUS_FAILED;
e1c37d0e 703 return;
cab30143
JQ
704 }
705
f37afb5a 706 if (local_err) {
342ab8d1 707 migrate_fd_error(s);
f37afb5a 708 error_propagate(errp, local_err);
e1c37d0e 709 return;
1299c631 710 }
cab30143
JQ
711}
712
6cdedb07 713void qmp_migrate_cancel(Error **errp)
cab30143 714{
17549e84 715 migrate_fd_cancel(migrate_get_current());
cab30143
JQ
716}
717
9e1ba4cc
OW
718void qmp_migrate_set_cache_size(int64_t value, Error **errp)
719{
720 MigrationState *s = migrate_get_current();
c91e681a 721 int64_t new_size;
9e1ba4cc
OW
722
723 /* Check for truncation */
724 if (value != (size_t)value) {
c6bd8c70
MA
725 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
726 "exceeding address space");
9e1ba4cc
OW
727 return;
728 }
729
a5615b14
OW
730 /* Cache should not be larger than guest ram size */
731 if (value > ram_bytes_total()) {
c6bd8c70
MA
732 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
733 "exceeds guest ram size ");
a5615b14
OW
734 return;
735 }
736
c91e681a
OW
737 new_size = xbzrle_cache_resize(value);
738 if (new_size < 0) {
c6bd8c70
MA
739 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
740 "is smaller than page size");
c91e681a
OW
741 return;
742 }
743
744 s->xbzrle_cache_size = new_size;
9e1ba4cc
OW
745}
746
747int64_t qmp_query_migrate_cache_size(Error **errp)
748{
749 return migrate_xbzrle_cache_size();
750}
751
3dc85383 752void qmp_migrate_set_speed(int64_t value, Error **errp)
cab30143 753{
cab30143
JQ
754 MigrationState *s;
755
3dc85383
LC
756 if (value < 0) {
757 value = 0;
99a0db9b 758 }
442773ce
PB
759 if (value > SIZE_MAX) {
760 value = SIZE_MAX;
761 }
cab30143 762
17549e84 763 s = migrate_get_current();
3dc85383 764 s->bandwidth_limit = value;
442773ce
PB
765 if (s->file) {
766 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
767 }
cab30143
JQ
768}
769
4f0a993b 770void qmp_migrate_set_downtime(double value, Error **errp)
cab30143 771{
4f0a993b
LC
772 value *= 1e9;
773 value = MAX(0, MIN(UINT64_MAX, value));
774 max_downtime = (uint64_t)value;
99a0db9b 775}
17ad9b35 776
bde1e2ec
CV
777bool migrate_auto_converge(void)
778{
779 MigrationState *s;
780
781 s = migrate_get_current();
782
783 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
784}
785
323004a3
PL
786bool migrate_zero_blocks(void)
787{
788 MigrationState *s;
789
790 s = migrate_get_current();
791
792 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
793}
794
8706d2d5
LL
795bool migrate_use_compression(void)
796{
dde4e694
LL
797 MigrationState *s;
798
799 s = migrate_get_current();
800
801 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
8706d2d5
LL
802}
803
804int migrate_compress_level(void)
805{
806 MigrationState *s;
807
808 s = migrate_get_current();
809
43c60a81 810 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
8706d2d5
LL
811}
812
813int migrate_compress_threads(void)
814{
815 MigrationState *s;
816
817 s = migrate_get_current();
818
43c60a81 819 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
8706d2d5
LL
820}
821
3fcb38c2
LL
822int migrate_decompress_threads(void)
823{
824 MigrationState *s;
825
826 s = migrate_get_current();
827
43c60a81 828 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
3fcb38c2
LL
829}
830
17ad9b35
OW
831int migrate_use_xbzrle(void)
832{
833 MigrationState *s;
834
835 s = migrate_get_current();
836
837 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
838}
839
840int64_t migrate_xbzrle_cache_size(void)
841{
842 MigrationState *s;
843
844 s = migrate_get_current();
845
846 return s->xbzrle_cache_size;
847}
0d82d0e8
JQ
848
849/* migration thread support */
850
5f496a1b 851static void *migration_thread(void *opaque)
0d82d0e8 852{
9848a404 853 MigrationState *s = opaque;
bc72ad67
AB
854 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
855 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
be7172e2 856 int64_t initial_bytes = 0;
0d82d0e8 857 int64_t max_size = 0;
a3fa1d78
PB
858 int64_t start_time = initial_time;
859 bool old_vm_running = false;
76f5933a 860
f796baa1 861 qemu_savevm_state_header(s->file);
dba433c0 862 qemu_savevm_state_begin(s->file, &s->params);
0d82d0e8 863
bc72ad67 864 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
31194731 865 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
29ae8a41 866
31194731 867 while (s->state == MIGRATION_STATUS_ACTIVE) {
a3e879cd 868 int64_t current_time;
c369f40d 869 uint64_t pending_size;
0d82d0e8 870
a0ff044b 871 if (!qemu_file_rate_limit(s->file)) {
c369f40d 872 pending_size = qemu_savevm_state_pending(s->file, max_size);
9013dca5 873 trace_migrate_pending(pending_size, max_size);
b22ff1fb 874 if (pending_size && pending_size >= max_size) {
dba433c0 875 qemu_savevm_state_iterate(s->file);
c369f40d 876 } else {
0e1146a7
KW
877 int ret;
878
32c835ba 879 qemu_mutex_lock_iothread();
bc72ad67 880 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
c369f40d 881 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
a3fa1d78 882 old_vm_running = runstate_is_running();
0e1146a7 883
df4b1024
JQ
884 ret = global_state_store();
885 if (!ret) {
886 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
887 if (ret >= 0) {
888 qemu_file_set_rate_limit(s->file, INT64_MAX);
889 qemu_savevm_state_complete(s->file);
890 }
0e1146a7 891 }
32c835ba 892 qemu_mutex_unlock_iothread();
0e1146a7
KW
893
894 if (ret < 0) {
31194731
HZ
895 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
896 MIGRATION_STATUS_FAILED);
0e1146a7
KW
897 break;
898 }
899
059f896c 900 if (!qemu_file_get_error(s->file)) {
31194731
HZ
901 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
902 MIGRATION_STATUS_COMPLETED);
059f896c
PB
903 break;
904 }
c369f40d
JQ
905 }
906 }
f4410a5d 907
fd45ee2c 908 if (qemu_file_get_error(s->file)) {
31194731
HZ
909 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
910 MIGRATION_STATUS_FAILED);
fd45ee2c
PB
911 break;
912 }
bc72ad67 913 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
0d82d0e8 914 if (current_time >= initial_time + BUFFER_DELAY) {
be7172e2 915 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
77417f10 916 uint64_t time_spent = current_time - initial_time;
0d82d0e8
JQ
917 double bandwidth = transferred_bytes / time_spent;
918 max_size = bandwidth * migrate_max_downtime() / 1000000;
919
7e114f8c
MH
920 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
921 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
922
9013dca5
AK
923 trace_migrate_transferred(transferred_bytes, time_spent,
924 bandwidth, max_size);
90f8ae72
JQ
925 /* if we haven't sent anything, we don't want to recalculate
926 10000 is a small enough number for our purposes */
927 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
928 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
929 }
0d82d0e8 930
1964a397 931 qemu_file_reset_rate_limit(s->file);
0d82d0e8 932 initial_time = current_time;
be7172e2 933 initial_bytes = qemu_ftell(s->file);
0d82d0e8 934 }
a0ff044b 935 if (qemu_file_rate_limit(s->file)) {
0d82d0e8
JQ
936 /* usleep expects microseconds */
937 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
938 }
a3fa1d78
PB
939 }
940
f4410a5d 941 qemu_mutex_lock_iothread();
31194731 942 if (s->state == MIGRATION_STATUS_COMPLETED) {
bc72ad67 943 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
d6ed7312 944 uint64_t transferred_bytes = qemu_ftell(s->file);
a3fa1d78
PB
945 s->total_time = end_time - s->total_time;
946 s->downtime = end_time - start_time;
d6ed7312
PL
947 if (s->total_time) {
948 s->mbps = (((double) transferred_bytes * 8.0) /
949 ((double) s->total_time)) / 1000;
950 }
a3fa1d78
PB
951 runstate_set(RUN_STATE_POSTMIGRATE);
952 } else {
953 if (old_vm_running) {
a3fa1d78 954 vm_start();
dba433c0 955 }
0d82d0e8 956 }
bb1fadc4 957 qemu_bh_schedule(s->cleanup_bh);
dba433c0 958 qemu_mutex_unlock_iothread();
f4410a5d 959
0d82d0e8
JQ
960 return NULL;
961}
962
9848a404 963void migrate_fd_connect(MigrationState *s)
0d82d0e8 964{
cc283e3b
JQ
965 /* This is a best 1st approximation. ns to ms */
966 s->expected_downtime = max_downtime/1000000;
bb1fadc4 967 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
0d82d0e8 968
442773ce
PB
969 qemu_file_set_rate_limit(s->file,
970 s->bandwidth_limit / XFER_LIMIT_RATIO);
971
9287ac27
SH
972 /* Notify before starting migration thread */
973 notifier_list_notify(&migration_state_notifiers, s);
974
8706d2d5 975 migrate_compress_threads_create();
4900116e 976 qemu_thread_create(&s->thread, "migration", migration_thread, s,
bb1fadc4 977 QEMU_THREAD_JOINABLE);
0d82d0e8 978}