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