]> git.proxmox.com Git - mirror_qemu.git/blame - migration/migration.c
migration: No need to call trace_migrate_set_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 {
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{
a5c17b5f 512 if (atomic_cmpxchg(&s->state, old_state, new_state) == old_state) {
51cf4c1a
Z
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);
7844337d 552 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
bb1fadc4 553 notifier_list_notify(&migration_state_notifiers, s);
458cf28e
JQ
554}
555
0edda1c4 556static void migrate_fd_cancel(MigrationState *s)
065e2813 557{
6f2b811a 558 int old_state ;
a26ba26e 559 QEMUFile *f = migrate_get_current()->file;
9013dca5 560 trace_migrate_fd_cancel();
065e2813 561
6f2b811a
Z
562 do {
563 old_state = s->state;
31194731
HZ
564 if (old_state != MIGRATION_STATUS_SETUP &&
565 old_state != MIGRATION_STATUS_ACTIVE) {
6f2b811a
Z
566 break;
567 }
31194731
HZ
568 migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
569 } while (s->state != MIGRATION_STATUS_CANCELLING);
a26ba26e
DDAG
570
571 /*
572 * If we're unlucky the migration code might be stuck somewhere in a
573 * send/write while the network has failed and is waiting to timeout;
574 * if we've got shutdown(2) available then we can force it to quit.
575 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
576 * called in a bh, so there is no race against this cancel.
577 */
31194731 578 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
a26ba26e
DDAG
579 qemu_file_shutdown(f);
580 }
065e2813
AL
581}
582
99a0db9b
GH
583void add_migration_state_change_notifier(Notifier *notify)
584{
585 notifier_list_add(&migration_state_notifiers, notify);
586}
587
588void remove_migration_state_change_notifier(Notifier *notify)
589{
31552529 590 notifier_remove(notify);
99a0db9b
GH
591}
592
02edd2e7 593bool migration_in_setup(MigrationState *s)
afe2df69 594{
31194731 595 return s->state == MIGRATION_STATUS_SETUP;
afe2df69
GH
596}
597
7073693b 598bool migration_has_finished(MigrationState *s)
99a0db9b 599{
31194731 600 return s->state == MIGRATION_STATUS_COMPLETED;
99a0db9b 601}
0edda1c4 602
afe2df69
GH
603bool migration_has_failed(MigrationState *s)
604{
31194731
HZ
605 return (s->state == MIGRATION_STATUS_CANCELLED ||
606 s->state == MIGRATION_STATUS_FAILED);
afe2df69
GH
607}
608
6607ae23 609static MigrationState *migrate_init(const MigrationParams *params)
0edda1c4 610{
17549e84 611 MigrationState *s = migrate_get_current();
d0ae46c1 612 int64_t bandwidth_limit = s->bandwidth_limit;
bbf6da32 613 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
17ad9b35 614 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
43c60a81
LL
615 int compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
616 int compress_thread_count =
617 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
618 int decompress_thread_count =
619 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
bbf6da32
OW
620
621 memcpy(enabled_capabilities, s->enabled_capabilities,
622 sizeof(enabled_capabilities));
0edda1c4 623
17549e84 624 memset(s, 0, sizeof(*s));
6607ae23 625 s->params = *params;
bbf6da32
OW
626 memcpy(s->enabled_capabilities, enabled_capabilities,
627 sizeof(enabled_capabilities));
17ad9b35 628 s->xbzrle_cache_size = xbzrle_cache_size;
1299c631 629
43c60a81
LL
630 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
631 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
632 compress_thread_count;
633 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
634 decompress_thread_count;
0edda1c4 635 s->bandwidth_limit = bandwidth_limit;
7844337d 636 migrate_set_state(s, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
0edda1c4 637
bc72ad67 638 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
0edda1c4
JQ
639 return s;
640}
cab30143 641
fa2756b7
AL
642static GSList *migration_blockers;
643
644void migrate_add_blocker(Error *reason)
645{
646 migration_blockers = g_slist_prepend(migration_blockers, reason);
647}
648
649void migrate_del_blocker(Error *reason)
650{
651 migration_blockers = g_slist_remove(migration_blockers, reason);
652}
653
bf1ae1f4
DDAG
654void qmp_migrate_incoming(const char *uri, Error **errp)
655{
656 Error *local_err = NULL;
4debb5f5 657 static bool once = true;
bf1ae1f4
DDAG
658
659 if (!deferred_incoming) {
4debb5f5 660 error_setg(errp, "For use with '-incoming defer'");
bf1ae1f4
DDAG
661 return;
662 }
4debb5f5
DDAG
663 if (!once) {
664 error_setg(errp, "The incoming migration has already been started");
665 }
bf1ae1f4
DDAG
666
667 qemu_start_incoming_migration(uri, &local_err);
668
669 if (local_err) {
670 error_propagate(errp, local_err);
671 return;
672 }
673
4debb5f5 674 once = false;
bf1ae1f4
DDAG
675}
676
e1c37d0e
LC
677void qmp_migrate(const char *uri, bool has_blk, bool blk,
678 bool has_inc, bool inc, bool has_detach, bool detach,
679 Error **errp)
cab30143 680{
be7059cd 681 Error *local_err = NULL;
17549e84 682 MigrationState *s = migrate_get_current();
6607ae23 683 MigrationParams params;
cab30143 684 const char *p;
cab30143 685
8c0426ae
PP
686 params.blk = has_blk && blk;
687 params.shared = has_inc && inc;
6607ae23 688
31194731
HZ
689 if (s->state == MIGRATION_STATUS_ACTIVE ||
690 s->state == MIGRATION_STATUS_SETUP ||
691 s->state == MIGRATION_STATUS_CANCELLING) {
c6bd8c70 692 error_setg(errp, QERR_MIGRATION_ACTIVE);
e1c37d0e 693 return;
cab30143 694 }
ca99993a
DDAG
695 if (runstate_check(RUN_STATE_INMIGRATE)) {
696 error_setg(errp, "Guest is waiting for an incoming migration");
697 return;
698 }
699
e1c37d0e
LC
700 if (qemu_savevm_state_blocked(errp)) {
701 return;
cab30143
JQ
702 }
703
fa2756b7 704 if (migration_blockers) {
e1c37d0e
LC
705 *errp = error_copy(migration_blockers->data);
706 return;
fa2756b7
AL
707 }
708
656a2334
JQ
709 /* We are starting a new migration, so we want to start in a clean
710 state. This change is only needed if previous migration
711 failed/was cancelled. We don't use migrate_set_state() because
712 we are setting the initial state, not changing it. */
713 s->state = MIGRATION_STATUS_NONE;
714
6607ae23 715 s = migrate_init(&params);
cab30143
JQ
716
717 if (strstart(uri, "tcp:", &p)) {
f37afb5a 718 tcp_start_outgoing_migration(s, p, &local_err);
2da776db 719#ifdef CONFIG_RDMA
41310c68 720 } else if (strstart(uri, "rdma:", &p)) {
2da776db
MH
721 rdma_start_outgoing_migration(s, p, &local_err);
722#endif
cab30143
JQ
723#if !defined(WIN32)
724 } else if (strstart(uri, "exec:", &p)) {
f37afb5a 725 exec_start_outgoing_migration(s, p, &local_err);
cab30143 726 } else if (strstart(uri, "unix:", &p)) {
f37afb5a 727 unix_start_outgoing_migration(s, p, &local_err);
cab30143 728 } else if (strstart(uri, "fd:", &p)) {
f37afb5a 729 fd_start_outgoing_migration(s, p, &local_err);
cab30143 730#endif
99a0db9b 731 } else {
c6bd8c70
MA
732 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
733 "a valid migration protocol");
7844337d 734 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
e1c37d0e 735 return;
cab30143
JQ
736 }
737
f37afb5a 738 if (local_err) {
342ab8d1 739 migrate_fd_error(s);
f37afb5a 740 error_propagate(errp, local_err);
e1c37d0e 741 return;
1299c631 742 }
cab30143
JQ
743}
744
6cdedb07 745void qmp_migrate_cancel(Error **errp)
cab30143 746{
17549e84 747 migrate_fd_cancel(migrate_get_current());
cab30143
JQ
748}
749
9e1ba4cc
OW
750void qmp_migrate_set_cache_size(int64_t value, Error **errp)
751{
752 MigrationState *s = migrate_get_current();
c91e681a 753 int64_t new_size;
9e1ba4cc
OW
754
755 /* Check for truncation */
756 if (value != (size_t)value) {
c6bd8c70
MA
757 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
758 "exceeding address space");
9e1ba4cc
OW
759 return;
760 }
761
a5615b14
OW
762 /* Cache should not be larger than guest ram size */
763 if (value > ram_bytes_total()) {
c6bd8c70
MA
764 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
765 "exceeds guest ram size ");
a5615b14
OW
766 return;
767 }
768
c91e681a
OW
769 new_size = xbzrle_cache_resize(value);
770 if (new_size < 0) {
c6bd8c70
MA
771 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
772 "is smaller than page size");
c91e681a
OW
773 return;
774 }
775
776 s->xbzrle_cache_size = new_size;
9e1ba4cc
OW
777}
778
779int64_t qmp_query_migrate_cache_size(Error **errp)
780{
781 return migrate_xbzrle_cache_size();
782}
783
3dc85383 784void qmp_migrate_set_speed(int64_t value, Error **errp)
cab30143 785{
cab30143
JQ
786 MigrationState *s;
787
3dc85383
LC
788 if (value < 0) {
789 value = 0;
99a0db9b 790 }
442773ce
PB
791 if (value > SIZE_MAX) {
792 value = SIZE_MAX;
793 }
cab30143 794
17549e84 795 s = migrate_get_current();
3dc85383 796 s->bandwidth_limit = value;
442773ce
PB
797 if (s->file) {
798 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
799 }
cab30143
JQ
800}
801
4f0a993b 802void qmp_migrate_set_downtime(double value, Error **errp)
cab30143 803{
4f0a993b
LC
804 value *= 1e9;
805 value = MAX(0, MIN(UINT64_MAX, value));
806 max_downtime = (uint64_t)value;
99a0db9b 807}
17ad9b35 808
bde1e2ec
CV
809bool migrate_auto_converge(void)
810{
811 MigrationState *s;
812
813 s = migrate_get_current();
814
815 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
816}
817
323004a3
PL
818bool migrate_zero_blocks(void)
819{
820 MigrationState *s;
821
822 s = migrate_get_current();
823
824 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
825}
826
8706d2d5
LL
827bool migrate_use_compression(void)
828{
dde4e694
LL
829 MigrationState *s;
830
831 s = migrate_get_current();
832
833 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
8706d2d5
LL
834}
835
836int migrate_compress_level(void)
837{
838 MigrationState *s;
839
840 s = migrate_get_current();
841
43c60a81 842 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
8706d2d5
LL
843}
844
845int migrate_compress_threads(void)
846{
847 MigrationState *s;
848
849 s = migrate_get_current();
850
43c60a81 851 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
8706d2d5
LL
852}
853
3fcb38c2
LL
854int migrate_decompress_threads(void)
855{
856 MigrationState *s;
857
858 s = migrate_get_current();
859
43c60a81 860 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
3fcb38c2
LL
861}
862
17ad9b35
OW
863int migrate_use_xbzrle(void)
864{
865 MigrationState *s;
866
867 s = migrate_get_current();
868
869 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
870}
871
872int64_t migrate_xbzrle_cache_size(void)
873{
874 MigrationState *s;
875
876 s = migrate_get_current();
877
878 return s->xbzrle_cache_size;
879}
0d82d0e8
JQ
880
881/* migration thread support */
882
5f496a1b 883static void *migration_thread(void *opaque)
0d82d0e8 884{
9848a404 885 MigrationState *s = opaque;
bc72ad67
AB
886 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
887 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
be7172e2 888 int64_t initial_bytes = 0;
0d82d0e8 889 int64_t max_size = 0;
a3fa1d78
PB
890 int64_t start_time = initial_time;
891 bool old_vm_running = false;
76f5933a 892
f796baa1 893 qemu_savevm_state_header(s->file);
dba433c0 894 qemu_savevm_state_begin(s->file, &s->params);
0d82d0e8 895
bc72ad67 896 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
31194731 897 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
29ae8a41 898
31194731 899 while (s->state == MIGRATION_STATUS_ACTIVE) {
a3e879cd 900 int64_t current_time;
c369f40d 901 uint64_t pending_size;
0d82d0e8 902
a0ff044b 903 if (!qemu_file_rate_limit(s->file)) {
c369f40d 904 pending_size = qemu_savevm_state_pending(s->file, max_size);
9013dca5 905 trace_migrate_pending(pending_size, max_size);
b22ff1fb 906 if (pending_size && pending_size >= max_size) {
dba433c0 907 qemu_savevm_state_iterate(s->file);
c369f40d 908 } else {
0e1146a7
KW
909 int ret;
910
32c835ba 911 qemu_mutex_lock_iothread();
bc72ad67 912 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
c369f40d 913 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
a3fa1d78 914 old_vm_running = runstate_is_running();
0e1146a7 915
df4b1024
JQ
916 ret = global_state_store();
917 if (!ret) {
918 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
919 if (ret >= 0) {
920 qemu_file_set_rate_limit(s->file, INT64_MAX);
921 qemu_savevm_state_complete(s->file);
922 }
0e1146a7 923 }
32c835ba 924 qemu_mutex_unlock_iothread();
0e1146a7
KW
925
926 if (ret < 0) {
31194731
HZ
927 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
928 MIGRATION_STATUS_FAILED);
0e1146a7
KW
929 break;
930 }
931
059f896c 932 if (!qemu_file_get_error(s->file)) {
31194731
HZ
933 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
934 MIGRATION_STATUS_COMPLETED);
059f896c
PB
935 break;
936 }
c369f40d
JQ
937 }
938 }
f4410a5d 939
fd45ee2c 940 if (qemu_file_get_error(s->file)) {
31194731
HZ
941 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
942 MIGRATION_STATUS_FAILED);
fd45ee2c
PB
943 break;
944 }
bc72ad67 945 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
0d82d0e8 946 if (current_time >= initial_time + BUFFER_DELAY) {
be7172e2 947 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
77417f10 948 uint64_t time_spent = current_time - initial_time;
0d82d0e8
JQ
949 double bandwidth = transferred_bytes / time_spent;
950 max_size = bandwidth * migrate_max_downtime() / 1000000;
951
7e114f8c
MH
952 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
953 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
954
9013dca5
AK
955 trace_migrate_transferred(transferred_bytes, time_spent,
956 bandwidth, max_size);
90f8ae72
JQ
957 /* if we haven't sent anything, we don't want to recalculate
958 10000 is a small enough number for our purposes */
959 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
960 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
961 }
0d82d0e8 962
1964a397 963 qemu_file_reset_rate_limit(s->file);
0d82d0e8 964 initial_time = current_time;
be7172e2 965 initial_bytes = qemu_ftell(s->file);
0d82d0e8 966 }
a0ff044b 967 if (qemu_file_rate_limit(s->file)) {
0d82d0e8
JQ
968 /* usleep expects microseconds */
969 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
970 }
a3fa1d78
PB
971 }
972
f4410a5d 973 qemu_mutex_lock_iothread();
31194731 974 if (s->state == MIGRATION_STATUS_COMPLETED) {
bc72ad67 975 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
d6ed7312 976 uint64_t transferred_bytes = qemu_ftell(s->file);
a3fa1d78
PB
977 s->total_time = end_time - s->total_time;
978 s->downtime = end_time - start_time;
d6ed7312
PL
979 if (s->total_time) {
980 s->mbps = (((double) transferred_bytes * 8.0) /
981 ((double) s->total_time)) / 1000;
982 }
a3fa1d78
PB
983 runstate_set(RUN_STATE_POSTMIGRATE);
984 } else {
985 if (old_vm_running) {
a3fa1d78 986 vm_start();
dba433c0 987 }
0d82d0e8 988 }
bb1fadc4 989 qemu_bh_schedule(s->cleanup_bh);
dba433c0 990 qemu_mutex_unlock_iothread();
f4410a5d 991
0d82d0e8
JQ
992 return NULL;
993}
994
9848a404 995void migrate_fd_connect(MigrationState *s)
0d82d0e8 996{
cc283e3b
JQ
997 /* This is a best 1st approximation. ns to ms */
998 s->expected_downtime = max_downtime/1000000;
bb1fadc4 999 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
0d82d0e8 1000
442773ce
PB
1001 qemu_file_set_rate_limit(s->file,
1002 s->bandwidth_limit / XFER_LIMIT_RATIO);
1003
9287ac27
SH
1004 /* Notify before starting migration thread */
1005 notifier_list_notify(&migration_state_notifiers, s);
1006
8706d2d5 1007 migrate_compress_threads_create();
4900116e 1008 qemu_thread_create(&s->thread, "migration", migration_thread, s,
bb1fadc4 1009 QEMU_THREAD_JOINABLE);
0d82d0e8 1010}