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