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