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