]> git.proxmox.com Git - mirror_qemu.git/blame - migration.c
rules.mak: fix $(obj) to a real relative path
[mirror_qemu.git] / 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
AL
28
29//#define DEBUG_MIGRATION
30
31#ifdef DEBUG_MIGRATION
d0f2c4c6 32#define DPRINTF(fmt, ...) \
065e2813
AL
33 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
34#else
d0f2c4c6 35#define DPRINTF(fmt, ...) \
065e2813
AL
36 do { } while (0)
37#endif
5bb7910a 38
7dc688ed 39enum {
29ae8a41
MH
40 MIG_STATE_ERROR = -1,
41 MIG_STATE_NONE,
7dc688ed 42 MIG_STATE_SETUP,
51cf4c1a 43 MIG_STATE_CANCELLING,
7dc688ed
JQ
44 MIG_STATE_CANCELLED,
45 MIG_STATE_ACTIVE,
46 MIG_STATE_COMPLETED,
47};
5bb7910a 48
d0ae46c1 49#define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
5bb7910a 50
5b4e1eb7
JQ
51/* Amount of time to allocate to each "chunk" of bandwidth-throttled
52 * data. */
53#define BUFFER_DELAY 100
54#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
55
17ad9b35
OW
56/* Migration XBZRLE default cache size */
57#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
58
99a0db9b
GH
59static NotifierList migration_state_notifiers =
60 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
61
17549e84
JQ
62/* When we add fault tolerance, we could have several
63 migrations at once. For now we don't need to add
64 dynamic creation of migration */
65
859bc756 66MigrationState *migrate_get_current(void)
17549e84
JQ
67{
68 static MigrationState current_migration = {
29ae8a41 69 .state = MIG_STATE_NONE,
d0ae46c1 70 .bandwidth_limit = MAX_THROTTLE,
17ad9b35 71 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
7e114f8c 72 .mbps = -1,
17549e84
JQ
73 };
74
75 return &current_migration;
76}
77
43eaae28 78void qemu_start_incoming_migration(const char *uri, Error **errp)
5bb7910a 79{
34c9dd8e
AL
80 const char *p;
81
82 if (strstart(uri, "tcp:", &p))
43eaae28 83 tcp_start_incoming_migration(p, errp);
2da776db
MH
84#ifdef CONFIG_RDMA
85 else if (strstart(uri, "x-rdma:", &p))
86 rdma_start_incoming_migration(p, errp);
87#endif
065e2813
AL
88#if !defined(WIN32)
89 else if (strstart(uri, "exec:", &p))
43eaae28 90 exec_start_incoming_migration(p, errp);
4951f65b 91 else if (strstart(uri, "unix:", &p))
43eaae28 92 unix_start_incoming_migration(p, errp);
5ac1fad3 93 else if (strstart(uri, "fd:", &p))
43eaae28 94 fd_start_incoming_migration(p, errp);
065e2813 95#endif
8ca5e801 96 else {
312fd5f2 97 error_setg(errp, "unknown migration protocol: %s", uri);
8ca5e801 98 }
5bb7910a
AL
99}
100
82a4da79 101static void process_incoming_migration_co(void *opaque)
511c0231 102{
82a4da79 103 QEMUFile *f = opaque;
1c12e1f5
PB
104 int ret;
105
106 ret = qemu_loadvm_state(f);
107 qemu_fclose(f);
905f26f2 108 free_xbzrle_decoded_buf();
1c12e1f5 109 if (ret < 0) {
511c0231 110 fprintf(stderr, "load of migration failed\n");
4aead692 111 exit(EXIT_FAILURE);
511c0231
JQ
112 }
113 qemu_announce_self();
114 DPRINTF("successfully loaded vm state\n");
115
901862cb 116 bdrv_clear_incoming_migration_all();
0f15423c
AL
117 /* Make sure all file formats flush their mutable metadata */
118 bdrv_invalidate_cache_all();
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 */
141static uint64_t max_downtime = 30000000;
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();
182 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
183 }
184}
185
791e7c82 186MigrationInfo *qmp_query_migrate(Error **errp)
5bb7910a 187{
791e7c82 188 MigrationInfo *info = g_malloc0(sizeof(*info));
17549e84
JQ
189 MigrationState *s = migrate_get_current();
190
191 switch (s->state) {
29ae8a41 192 case MIG_STATE_NONE:
17549e84
JQ
193 /* no migration has happened ever */
194 break;
29ae8a41
MH
195 case MIG_STATE_SETUP:
196 info->has_status = true;
197 info->status = g_strdup("setup");
ed4fbd10 198 info->has_total_time = false;
29ae8a41 199 break;
17549e84 200 case MIG_STATE_ACTIVE:
51cf4c1a 201 case MIG_STATE_CANCELLING:
791e7c82
LC
202 info->has_status = true;
203 info->status = g_strdup("active");
7aa939af 204 info->has_total_time = true;
bc72ad67 205 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
7aa939af 206 - s->total_time;
2c52ddf1
JQ
207 info->has_expected_downtime = true;
208 info->expected_downtime = s->expected_downtime;
ed4fbd10
MH
209 info->has_setup_time = true;
210 info->setup_time = s->setup_time;
17549e84 211
791e7c82
LC
212 info->has_ram = true;
213 info->ram = g_malloc0(sizeof(*info->ram));
214 info->ram->transferred = ram_bytes_transferred();
215 info->ram->remaining = ram_bytes_remaining();
216 info->ram->total = ram_bytes_total();
004d4c10 217 info->ram->duplicate = dup_mig_pages_transferred();
f1c72795 218 info->ram->skipped = skipped_mig_pages_transferred();
004d4c10
OW
219 info->ram->normal = norm_mig_pages_transferred();
220 info->ram->normal_bytes = norm_mig_bytes_transferred();
8d017193 221 info->ram->dirty_pages_rate = s->dirty_pages_rate;
7e114f8c 222 info->ram->mbps = s->mbps;
8d017193 223
17549e84 224 if (blk_mig_active()) {
791e7c82
LC
225 info->has_disk = true;
226 info->disk = g_malloc0(sizeof(*info->disk));
227 info->disk->transferred = blk_mig_bytes_transferred();
228 info->disk->remaining = blk_mig_bytes_remaining();
229 info->disk->total = blk_mig_bytes_total();
ff8d81d8 230 }
f36d55af
OW
231
232 get_xbzrle_cache_stats(info);
17549e84
JQ
233 break;
234 case MIG_STATE_COMPLETED:
f36d55af
OW
235 get_xbzrle_cache_stats(info);
236
791e7c82
LC
237 info->has_status = true;
238 info->status = g_strdup("completed");
00c14997 239 info->has_total_time = true;
7aa939af 240 info->total_time = s->total_time;
9c5a9fcf
JQ
241 info->has_downtime = true;
242 info->downtime = s->downtime;
ed4fbd10
MH
243 info->has_setup_time = true;
244 info->setup_time = s->setup_time;
d5f8a570
JQ
245
246 info->has_ram = true;
247 info->ram = g_malloc0(sizeof(*info->ram));
248 info->ram->transferred = ram_bytes_transferred();
249 info->ram->remaining = 0;
250 info->ram->total = ram_bytes_total();
004d4c10 251 info->ram->duplicate = dup_mig_pages_transferred();
f1c72795 252 info->ram->skipped = skipped_mig_pages_transferred();
004d4c10
OW
253 info->ram->normal = norm_mig_pages_transferred();
254 info->ram->normal_bytes = norm_mig_bytes_transferred();
7e114f8c 255 info->ram->mbps = s->mbps;
17549e84
JQ
256 break;
257 case MIG_STATE_ERROR:
791e7c82
LC
258 info->has_status = true;
259 info->status = g_strdup("failed");
17549e84
JQ
260 break;
261 case MIG_STATE_CANCELLED:
791e7c82
LC
262 info->has_status = true;
263 info->status = g_strdup("cancelled");
17549e84 264 break;
5bb7910a 265 }
791e7c82
LC
266
267 return info;
5bb7910a
AL
268}
269
00458433
OW
270void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
271 Error **errp)
272{
273 MigrationState *s = migrate_get_current();
274 MigrationCapabilityStatusList *cap;
275
29ae8a41 276 if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP) {
00458433
OW
277 error_set(errp, QERR_MIGRATION_ACTIVE);
278 return;
279 }
280
281 for (cap = params; cap; cap = cap->next) {
282 s->enabled_capabilities[cap->value->capability] = cap->value->state;
283 }
284}
285
065e2813
AL
286/* shared migration helpers */
287
51cf4c1a
Z
288static void migrate_set_state(MigrationState *s, int old_state, int new_state)
289{
290 if (atomic_cmpxchg(&s->state, old_state, new_state) == new_state) {
291 trace_migrate_set_state(new_state);
292 }
293}
294
bb1fadc4 295static void migrate_fd_cleanup(void *opaque)
065e2813 296{
bb1fadc4
PB
297 MigrationState *s = opaque;
298
299 qemu_bh_delete(s->cleanup_bh);
300 s->cleanup_bh = NULL;
301
065e2813 302 if (s->file) {
d0f2c4c6 303 DPRINTF("closing file\n");
404a7c05
PB
304 qemu_mutex_unlock_iothread();
305 qemu_thread_join(&s->thread);
306 qemu_mutex_lock_iothread();
307
6f190a06
PB
308 qemu_fclose(s->file);
309 s->file = NULL;
065e2813
AL
310 }
311
a3fa1d78 312 assert(s->state != MIG_STATE_ACTIVE);
7a2c1721 313
a3fa1d78 314 if (s->state != MIG_STATE_COMPLETED) {
7a2c1721 315 qemu_savevm_state_cancel();
51cf4c1a
Z
316 if (s->state == MIG_STATE_CANCELLING) {
317 migrate_set_state(s, MIG_STATE_CANCELLING, MIG_STATE_CANCELLED);
318 }
7a2c1721 319 }
a3fa1d78
PB
320
321 notifier_list_notify(&migration_state_notifiers, s);
065e2813
AL
322}
323
8b6b99b3 324void migrate_fd_error(MigrationState *s)
065e2813 325{
8b6b99b3 326 DPRINTF("setting error state\n");
bb1fadc4
PB
327 assert(s->file == NULL);
328 s->state = MIG_STATE_ERROR;
329 trace_migrate_set_state(MIG_STATE_ERROR);
330 notifier_list_notify(&migration_state_notifiers, s);
458cf28e
JQ
331}
332
0edda1c4 333static void migrate_fd_cancel(MigrationState *s)
065e2813 334{
6f2b811a 335 int old_state ;
d0f2c4c6 336 DPRINTF("cancelling migration\n");
065e2813 337
6f2b811a
Z
338 do {
339 old_state = s->state;
340 if (old_state != MIG_STATE_SETUP && old_state != MIG_STATE_ACTIVE) {
341 break;
342 }
51cf4c1a
Z
343 migrate_set_state(s, old_state, MIG_STATE_CANCELLING);
344 } while (s->state != MIG_STATE_CANCELLING);
065e2813
AL
345}
346
99a0db9b
GH
347void add_migration_state_change_notifier(Notifier *notify)
348{
349 notifier_list_add(&migration_state_notifiers, notify);
350}
351
352void remove_migration_state_change_notifier(Notifier *notify)
353{
31552529 354 notifier_remove(notify);
99a0db9b
GH
355}
356
02edd2e7 357bool migration_in_setup(MigrationState *s)
afe2df69 358{
02edd2e7 359 return s->state == MIG_STATE_SETUP;
afe2df69
GH
360}
361
7073693b 362bool migration_has_finished(MigrationState *s)
99a0db9b 363{
7073693b 364 return s->state == MIG_STATE_COMPLETED;
99a0db9b 365}
0edda1c4 366
afe2df69
GH
367bool migration_has_failed(MigrationState *s)
368{
369 return (s->state == MIG_STATE_CANCELLED ||
370 s->state == MIG_STATE_ERROR);
371}
372
6607ae23 373static MigrationState *migrate_init(const MigrationParams *params)
0edda1c4 374{
17549e84 375 MigrationState *s = migrate_get_current();
d0ae46c1 376 int64_t bandwidth_limit = s->bandwidth_limit;
bbf6da32 377 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
17ad9b35 378 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
bbf6da32
OW
379
380 memcpy(enabled_capabilities, s->enabled_capabilities,
381 sizeof(enabled_capabilities));
0edda1c4 382
17549e84 383 memset(s, 0, sizeof(*s));
6607ae23 384 s->params = *params;
bbf6da32
OW
385 memcpy(s->enabled_capabilities, enabled_capabilities,
386 sizeof(enabled_capabilities));
17ad9b35 387 s->xbzrle_cache_size = xbzrle_cache_size;
1299c631 388
0edda1c4 389 s->bandwidth_limit = bandwidth_limit;
d5934dde 390 s->state = MIG_STATE_SETUP;
c09e5bb1 391 trace_migrate_set_state(MIG_STATE_SETUP);
0edda1c4 392
bc72ad67 393 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
0edda1c4
JQ
394 return s;
395}
cab30143 396
fa2756b7
AL
397static GSList *migration_blockers;
398
399void migrate_add_blocker(Error *reason)
400{
401 migration_blockers = g_slist_prepend(migration_blockers, reason);
402}
403
404void migrate_del_blocker(Error *reason)
405{
406 migration_blockers = g_slist_remove(migration_blockers, reason);
407}
408
e1c37d0e
LC
409void qmp_migrate(const char *uri, bool has_blk, bool blk,
410 bool has_inc, bool inc, bool has_detach, bool detach,
411 Error **errp)
cab30143 412{
be7059cd 413 Error *local_err = NULL;
17549e84 414 MigrationState *s = migrate_get_current();
6607ae23 415 MigrationParams params;
cab30143 416 const char *p;
cab30143 417
8c0426ae
PP
418 params.blk = has_blk && blk;
419 params.shared = has_inc && inc;
6607ae23 420
51cf4c1a
Z
421 if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP ||
422 s->state == MIG_STATE_CANCELLING) {
e1c37d0e
LC
423 error_set(errp, QERR_MIGRATION_ACTIVE);
424 return;
cab30143
JQ
425 }
426
e1c37d0e
LC
427 if (qemu_savevm_state_blocked(errp)) {
428 return;
cab30143
JQ
429 }
430
fa2756b7 431 if (migration_blockers) {
e1c37d0e
LC
432 *errp = error_copy(migration_blockers->data);
433 return;
fa2756b7
AL
434 }
435
6607ae23 436 s = migrate_init(&params);
cab30143
JQ
437
438 if (strstart(uri, "tcp:", &p)) {
f37afb5a 439 tcp_start_outgoing_migration(s, p, &local_err);
2da776db
MH
440#ifdef CONFIG_RDMA
441 } else if (strstart(uri, "x-rdma:", &p)) {
442 rdma_start_outgoing_migration(s, p, &local_err);
443#endif
cab30143
JQ
444#if !defined(WIN32)
445 } else if (strstart(uri, "exec:", &p)) {
f37afb5a 446 exec_start_outgoing_migration(s, p, &local_err);
cab30143 447 } else if (strstart(uri, "unix:", &p)) {
f37afb5a 448 unix_start_outgoing_migration(s, p, &local_err);
cab30143 449 } else if (strstart(uri, "fd:", &p)) {
f37afb5a 450 fd_start_outgoing_migration(s, p, &local_err);
cab30143 451#endif
99a0db9b 452 } else {
e1c37d0e 453 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
c9501142 454 s->state = MIG_STATE_ERROR;
e1c37d0e 455 return;
cab30143
JQ
456 }
457
f37afb5a 458 if (local_err) {
342ab8d1 459 migrate_fd_error(s);
f37afb5a 460 error_propagate(errp, local_err);
e1c37d0e 461 return;
1299c631 462 }
cab30143
JQ
463}
464
6cdedb07 465void qmp_migrate_cancel(Error **errp)
cab30143 466{
17549e84 467 migrate_fd_cancel(migrate_get_current());
cab30143
JQ
468}
469
9e1ba4cc
OW
470void qmp_migrate_set_cache_size(int64_t value, Error **errp)
471{
472 MigrationState *s = migrate_get_current();
c91e681a 473 int64_t new_size;
9e1ba4cc
OW
474
475 /* Check for truncation */
476 if (value != (size_t)value) {
477 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
478 "exceeding address space");
479 return;
480 }
481
a5615b14
OW
482 /* Cache should not be larger than guest ram size */
483 if (value > ram_bytes_total()) {
484 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
485 "exceeds guest ram size ");
486 return;
487 }
488
c91e681a
OW
489 new_size = xbzrle_cache_resize(value);
490 if (new_size < 0) {
491 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
492 "is smaller than page size");
493 return;
494 }
495
496 s->xbzrle_cache_size = new_size;
9e1ba4cc
OW
497}
498
499int64_t qmp_query_migrate_cache_size(Error **errp)
500{
501 return migrate_xbzrle_cache_size();
502}
503
3dc85383 504void qmp_migrate_set_speed(int64_t value, Error **errp)
cab30143 505{
cab30143
JQ
506 MigrationState *s;
507
3dc85383
LC
508 if (value < 0) {
509 value = 0;
99a0db9b 510 }
442773ce
PB
511 if (value > SIZE_MAX) {
512 value = SIZE_MAX;
513 }
cab30143 514
17549e84 515 s = migrate_get_current();
3dc85383 516 s->bandwidth_limit = value;
442773ce
PB
517 if (s->file) {
518 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
519 }
cab30143
JQ
520}
521
4f0a993b 522void qmp_migrate_set_downtime(double value, Error **errp)
cab30143 523{
4f0a993b
LC
524 value *= 1e9;
525 value = MAX(0, MIN(UINT64_MAX, value));
526 max_downtime = (uint64_t)value;
99a0db9b 527}
17ad9b35 528
60d9222c
MH
529bool migrate_rdma_pin_all(void)
530{
531 MigrationState *s;
532
533 s = migrate_get_current();
534
535 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_RDMA_PIN_ALL];
536}
537
bde1e2ec
CV
538bool migrate_auto_converge(void)
539{
540 MigrationState *s;
541
542 s = migrate_get_current();
543
544 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
545}
546
323004a3
PL
547bool migrate_zero_blocks(void)
548{
549 MigrationState *s;
550
551 s = migrate_get_current();
552
553 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
554}
555
17ad9b35
OW
556int migrate_use_xbzrle(void)
557{
558 MigrationState *s;
559
560 s = migrate_get_current();
561
562 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
563}
564
565int64_t migrate_xbzrle_cache_size(void)
566{
567 MigrationState *s;
568
569 s = migrate_get_current();
570
571 return s->xbzrle_cache_size;
572}
0d82d0e8
JQ
573
574/* migration thread support */
575
5f496a1b 576static void *migration_thread(void *opaque)
0d82d0e8 577{
9848a404 578 MigrationState *s = opaque;
bc72ad67
AB
579 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
580 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
be7172e2 581 int64_t initial_bytes = 0;
0d82d0e8 582 int64_t max_size = 0;
a3fa1d78
PB
583 int64_t start_time = initial_time;
584 bool old_vm_running = false;
76f5933a 585
76f5933a 586 DPRINTF("beginning savevm\n");
dba433c0 587 qemu_savevm_state_begin(s->file, &s->params);
0d82d0e8 588
bc72ad67 589 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
29ae8a41
MH
590 migrate_set_state(s, MIG_STATE_SETUP, MIG_STATE_ACTIVE);
591
ed4fbd10
MH
592 DPRINTF("setup complete\n");
593
dba433c0 594 while (s->state == MIG_STATE_ACTIVE) {
a3e879cd 595 int64_t current_time;
c369f40d 596 uint64_t pending_size;
0d82d0e8 597
a0ff044b 598 if (!qemu_file_rate_limit(s->file)) {
c369f40d
JQ
599 DPRINTF("iterate\n");
600 pending_size = qemu_savevm_state_pending(s->file, max_size);
5016e2df
CD
601 DPRINTF("pending size %" PRIu64 " max %" PRIu64 "\n",
602 pending_size, max_size);
b22ff1fb 603 if (pending_size && pending_size >= max_size) {
dba433c0 604 qemu_savevm_state_iterate(s->file);
c369f40d 605 } else {
0e1146a7
KW
606 int ret;
607
c369f40d 608 DPRINTF("done iterating\n");
32c835ba 609 qemu_mutex_lock_iothread();
bc72ad67 610 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
c369f40d 611 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
a3fa1d78 612 old_vm_running = runstate_is_running();
0e1146a7
KW
613
614 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
615 if (ret >= 0) {
40596834 616 qemu_file_set_rate_limit(s->file, INT64_MAX);
0e1146a7
KW
617 qemu_savevm_state_complete(s->file);
618 }
32c835ba 619 qemu_mutex_unlock_iothread();
0e1146a7
KW
620
621 if (ret < 0) {
d58f574b 622 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR);
0e1146a7
KW
623 break;
624 }
625
059f896c 626 if (!qemu_file_get_error(s->file)) {
d58f574b 627 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_COMPLETED);
059f896c
PB
628 break;
629 }
c369f40d
JQ
630 }
631 }
f4410a5d 632
fd45ee2c 633 if (qemu_file_get_error(s->file)) {
d58f574b 634 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR);
fd45ee2c
PB
635 break;
636 }
bc72ad67 637 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
0d82d0e8 638 if (current_time >= initial_time + BUFFER_DELAY) {
be7172e2 639 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
77417f10 640 uint64_t time_spent = current_time - initial_time;
0d82d0e8
JQ
641 double bandwidth = transferred_bytes / time_spent;
642 max_size = bandwidth * migrate_max_downtime() / 1000000;
643
7e114f8c
MH
644 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
645 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
646
0d82d0e8
JQ
647 DPRINTF("transferred %" PRIu64 " time_spent %" PRIu64
648 " bandwidth %g max_size %" PRId64 "\n",
649 transferred_bytes, time_spent, bandwidth, max_size);
90f8ae72
JQ
650 /* if we haven't sent anything, we don't want to recalculate
651 10000 is a small enough number for our purposes */
652 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
653 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
654 }
0d82d0e8 655
1964a397 656 qemu_file_reset_rate_limit(s->file);
0d82d0e8 657 initial_time = current_time;
be7172e2 658 initial_bytes = qemu_ftell(s->file);
0d82d0e8 659 }
a0ff044b 660 if (qemu_file_rate_limit(s->file)) {
0d82d0e8
JQ
661 /* usleep expects microseconds */
662 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
663 }
a3fa1d78
PB
664 }
665
f4410a5d 666 qemu_mutex_lock_iothread();
a3fa1d78 667 if (s->state == MIG_STATE_COMPLETED) {
bc72ad67 668 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
a3fa1d78
PB
669 s->total_time = end_time - s->total_time;
670 s->downtime = end_time - start_time;
671 runstate_set(RUN_STATE_POSTMIGRATE);
672 } else {
673 if (old_vm_running) {
a3fa1d78 674 vm_start();
dba433c0 675 }
0d82d0e8 676 }
bb1fadc4 677 qemu_bh_schedule(s->cleanup_bh);
dba433c0 678 qemu_mutex_unlock_iothread();
f4410a5d 679
0d82d0e8
JQ
680 return NULL;
681}
682
9848a404 683void migrate_fd_connect(MigrationState *s)
0d82d0e8 684{
29ae8a41
MH
685 s->state = MIG_STATE_SETUP;
686 trace_migrate_set_state(MIG_STATE_SETUP);
c09e5bb1 687
cc283e3b
JQ
688 /* This is a best 1st approximation. ns to ms */
689 s->expected_downtime = max_downtime/1000000;
bb1fadc4 690 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
0d82d0e8 691
442773ce
PB
692 qemu_file_set_rate_limit(s->file,
693 s->bandwidth_limit / XFER_LIMIT_RATIO);
694
9287ac27
SH
695 /* Notify before starting migration thread */
696 notifier_list_notify(&migration_state_notifiers, s);
697
5f496a1b 698 qemu_thread_create(&s->thread, migration_thread, s,
bb1fadc4 699 QEMU_THREAD_JOINABLE);
0d82d0e8 700}