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