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