]> git.proxmox.com Git - qemu.git/blame - migration.c
qdev-properties-system.c: Allow vlan or netdev for -device, not both
[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
387eedeb 153 caps = NULL; /* silence compiler warning */
bbf6da32
OW
154 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
155 if (head == NULL) {
156 head = g_malloc0(sizeof(*caps));
157 caps = head;
158 } else {
159 caps->next = g_malloc0(sizeof(*caps));
160 caps = caps->next;
161 }
162 caps->value =
163 g_malloc(sizeof(*caps->value));
164 caps->value->capability = i;
165 caps->value->state = s->enabled_capabilities[i];
166 }
167
168 return head;
169}
170
f36d55af
OW
171static void get_xbzrle_cache_stats(MigrationInfo *info)
172{
173 if (migrate_use_xbzrle()) {
174 info->has_xbzrle_cache = true;
175 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
176 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
177 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
178 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
179 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
180 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
181 }
182}
183
791e7c82 184MigrationInfo *qmp_query_migrate(Error **errp)
5bb7910a 185{
791e7c82 186 MigrationInfo *info = g_malloc0(sizeof(*info));
17549e84
JQ
187 MigrationState *s = migrate_get_current();
188
189 switch (s->state) {
29ae8a41 190 case MIG_STATE_NONE:
17549e84
JQ
191 /* no migration has happened ever */
192 break;
29ae8a41
MH
193 case MIG_STATE_SETUP:
194 info->has_status = true;
195 info->status = g_strdup("setup");
ed4fbd10 196 info->has_total_time = false;
29ae8a41 197 break;
17549e84 198 case MIG_STATE_ACTIVE:
791e7c82
LC
199 info->has_status = true;
200 info->status = g_strdup("active");
7aa939af 201 info->has_total_time = true;
bc72ad67 202 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
7aa939af 203 - s->total_time;
2c52ddf1
JQ
204 info->has_expected_downtime = true;
205 info->expected_downtime = s->expected_downtime;
ed4fbd10
MH
206 info->has_setup_time = true;
207 info->setup_time = s->setup_time;
17549e84 208
791e7c82
LC
209 info->has_ram = true;
210 info->ram = g_malloc0(sizeof(*info->ram));
211 info->ram->transferred = ram_bytes_transferred();
212 info->ram->remaining = ram_bytes_remaining();
213 info->ram->total = ram_bytes_total();
004d4c10 214 info->ram->duplicate = dup_mig_pages_transferred();
f1c72795 215 info->ram->skipped = skipped_mig_pages_transferred();
004d4c10
OW
216 info->ram->normal = norm_mig_pages_transferred();
217 info->ram->normal_bytes = norm_mig_bytes_transferred();
8d017193 218 info->ram->dirty_pages_rate = s->dirty_pages_rate;
7e114f8c 219 info->ram->mbps = s->mbps;
8d017193 220
17549e84 221 if (blk_mig_active()) {
791e7c82
LC
222 info->has_disk = true;
223 info->disk = g_malloc0(sizeof(*info->disk));
224 info->disk->transferred = blk_mig_bytes_transferred();
225 info->disk->remaining = blk_mig_bytes_remaining();
226 info->disk->total = blk_mig_bytes_total();
ff8d81d8 227 }
f36d55af
OW
228
229 get_xbzrle_cache_stats(info);
17549e84
JQ
230 break;
231 case MIG_STATE_COMPLETED:
f36d55af
OW
232 get_xbzrle_cache_stats(info);
233
791e7c82
LC
234 info->has_status = true;
235 info->status = g_strdup("completed");
00c14997 236 info->has_total_time = true;
7aa939af 237 info->total_time = s->total_time;
9c5a9fcf
JQ
238 info->has_downtime = true;
239 info->downtime = s->downtime;
ed4fbd10
MH
240 info->has_setup_time = true;
241 info->setup_time = s->setup_time;
d5f8a570
JQ
242
243 info->has_ram = true;
244 info->ram = g_malloc0(sizeof(*info->ram));
245 info->ram->transferred = ram_bytes_transferred();
246 info->ram->remaining = 0;
247 info->ram->total = ram_bytes_total();
004d4c10 248 info->ram->duplicate = dup_mig_pages_transferred();
f1c72795 249 info->ram->skipped = skipped_mig_pages_transferred();
004d4c10
OW
250 info->ram->normal = norm_mig_pages_transferred();
251 info->ram->normal_bytes = norm_mig_bytes_transferred();
7e114f8c 252 info->ram->mbps = s->mbps;
17549e84
JQ
253 break;
254 case MIG_STATE_ERROR:
791e7c82
LC
255 info->has_status = true;
256 info->status = g_strdup("failed");
17549e84
JQ
257 break;
258 case MIG_STATE_CANCELLED:
791e7c82
LC
259 info->has_status = true;
260 info->status = g_strdup("cancelled");
17549e84 261 break;
5bb7910a 262 }
791e7c82
LC
263
264 return info;
5bb7910a
AL
265}
266
00458433
OW
267void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
268 Error **errp)
269{
270 MigrationState *s = migrate_get_current();
271 MigrationCapabilityStatusList *cap;
272
29ae8a41 273 if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP) {
00458433
OW
274 error_set(errp, QERR_MIGRATION_ACTIVE);
275 return;
276 }
277
278 for (cap = params; cap; cap = cap->next) {
279 s->enabled_capabilities[cap->value->capability] = cap->value->state;
280 }
281}
282
065e2813
AL
283/* shared migration helpers */
284
bb1fadc4 285static void migrate_fd_cleanup(void *opaque)
065e2813 286{
bb1fadc4
PB
287 MigrationState *s = opaque;
288
289 qemu_bh_delete(s->cleanup_bh);
290 s->cleanup_bh = NULL;
291
065e2813 292 if (s->file) {
d0f2c4c6 293 DPRINTF("closing file\n");
404a7c05
PB
294 qemu_mutex_unlock_iothread();
295 qemu_thread_join(&s->thread);
296 qemu_mutex_lock_iothread();
297
6f190a06
PB
298 qemu_fclose(s->file);
299 s->file = NULL;
065e2813
AL
300 }
301
a3fa1d78 302 assert(s->state != MIG_STATE_ACTIVE);
7a2c1721 303
a3fa1d78 304 if (s->state != MIG_STATE_COMPLETED) {
7a2c1721
PB
305 qemu_savevm_state_cancel();
306 }
a3fa1d78
PB
307
308 notifier_list_notify(&migration_state_notifiers, s);
065e2813
AL
309}
310
d58f574b 311static void migrate_set_state(MigrationState *s, int old_state, int new_state)
f4410a5d 312{
d58f574b 313 if (atomic_cmpxchg(&s->state, old_state, new_state) == new_state) {
f4410a5d
PB
314 trace_migrate_set_state(new_state);
315 }
316}
317
8b6b99b3 318void migrate_fd_error(MigrationState *s)
065e2813 319{
8b6b99b3 320 DPRINTF("setting error state\n");
bb1fadc4
PB
321 assert(s->file == NULL);
322 s->state = MIG_STATE_ERROR;
323 trace_migrate_set_state(MIG_STATE_ERROR);
324 notifier_list_notify(&migration_state_notifiers, s);
458cf28e
JQ
325}
326
0edda1c4 327static void migrate_fd_cancel(MigrationState *s)
065e2813 328{
d0f2c4c6 329 DPRINTF("cancelling migration\n");
065e2813 330
d58f574b 331 migrate_set_state(s, s->state, MIG_STATE_CANCELLED);
065e2813
AL
332}
333
99a0db9b
GH
334void add_migration_state_change_notifier(Notifier *notify)
335{
336 notifier_list_add(&migration_state_notifiers, notify);
337}
338
339void remove_migration_state_change_notifier(Notifier *notify)
340{
31552529 341 notifier_remove(notify);
99a0db9b
GH
342}
343
02edd2e7 344bool migration_in_setup(MigrationState *s)
afe2df69 345{
02edd2e7 346 return s->state == MIG_STATE_SETUP;
afe2df69
GH
347}
348
7073693b 349bool migration_has_finished(MigrationState *s)
99a0db9b 350{
7073693b 351 return s->state == MIG_STATE_COMPLETED;
99a0db9b 352}
0edda1c4 353
afe2df69
GH
354bool migration_has_failed(MigrationState *s)
355{
356 return (s->state == MIG_STATE_CANCELLED ||
357 s->state == MIG_STATE_ERROR);
358}
359
6607ae23 360static MigrationState *migrate_init(const MigrationParams *params)
0edda1c4 361{
17549e84 362 MigrationState *s = migrate_get_current();
d0ae46c1 363 int64_t bandwidth_limit = s->bandwidth_limit;
bbf6da32 364 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
17ad9b35 365 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
bbf6da32
OW
366
367 memcpy(enabled_capabilities, s->enabled_capabilities,
368 sizeof(enabled_capabilities));
0edda1c4 369
17549e84 370 memset(s, 0, sizeof(*s));
6607ae23 371 s->params = *params;
bbf6da32
OW
372 memcpy(s->enabled_capabilities, enabled_capabilities,
373 sizeof(enabled_capabilities));
17ad9b35 374 s->xbzrle_cache_size = xbzrle_cache_size;
1299c631 375
0edda1c4 376 s->bandwidth_limit = bandwidth_limit;
d5934dde 377 s->state = MIG_STATE_SETUP;
c09e5bb1 378 trace_migrate_set_state(MIG_STATE_SETUP);
0edda1c4 379
bc72ad67 380 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
0edda1c4
JQ
381 return s;
382}
cab30143 383
fa2756b7
AL
384static GSList *migration_blockers;
385
386void migrate_add_blocker(Error *reason)
387{
388 migration_blockers = g_slist_prepend(migration_blockers, reason);
389}
390
391void migrate_del_blocker(Error *reason)
392{
393 migration_blockers = g_slist_remove(migration_blockers, reason);
394}
395
e1c37d0e
LC
396void qmp_migrate(const char *uri, bool has_blk, bool blk,
397 bool has_inc, bool inc, bool has_detach, bool detach,
398 Error **errp)
cab30143 399{
be7059cd 400 Error *local_err = NULL;
17549e84 401 MigrationState *s = migrate_get_current();
6607ae23 402 MigrationParams params;
cab30143 403 const char *p;
cab30143 404
8c0426ae
PP
405 params.blk = has_blk && blk;
406 params.shared = has_inc && inc;
6607ae23 407
29ae8a41 408 if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP) {
e1c37d0e
LC
409 error_set(errp, QERR_MIGRATION_ACTIVE);
410 return;
cab30143
JQ
411 }
412
e1c37d0e
LC
413 if (qemu_savevm_state_blocked(errp)) {
414 return;
cab30143
JQ
415 }
416
fa2756b7 417 if (migration_blockers) {
e1c37d0e
LC
418 *errp = error_copy(migration_blockers->data);
419 return;
fa2756b7
AL
420 }
421
6607ae23 422 s = migrate_init(&params);
cab30143
JQ
423
424 if (strstart(uri, "tcp:", &p)) {
f37afb5a 425 tcp_start_outgoing_migration(s, p, &local_err);
2da776db
MH
426#ifdef CONFIG_RDMA
427 } else if (strstart(uri, "x-rdma:", &p)) {
428 rdma_start_outgoing_migration(s, p, &local_err);
429#endif
cab30143
JQ
430#if !defined(WIN32)
431 } else if (strstart(uri, "exec:", &p)) {
f37afb5a 432 exec_start_outgoing_migration(s, p, &local_err);
cab30143 433 } else if (strstart(uri, "unix:", &p)) {
f37afb5a 434 unix_start_outgoing_migration(s, p, &local_err);
cab30143 435 } else if (strstart(uri, "fd:", &p)) {
f37afb5a 436 fd_start_outgoing_migration(s, p, &local_err);
cab30143 437#endif
99a0db9b 438 } else {
e1c37d0e
LC
439 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
440 return;
cab30143
JQ
441 }
442
f37afb5a 443 if (local_err) {
342ab8d1 444 migrate_fd_error(s);
f37afb5a 445 error_propagate(errp, local_err);
e1c37d0e 446 return;
1299c631 447 }
cab30143
JQ
448}
449
6cdedb07 450void qmp_migrate_cancel(Error **errp)
cab30143 451{
17549e84 452 migrate_fd_cancel(migrate_get_current());
cab30143
JQ
453}
454
9e1ba4cc
OW
455void qmp_migrate_set_cache_size(int64_t value, Error **errp)
456{
457 MigrationState *s = migrate_get_current();
458
459 /* Check for truncation */
460 if (value != (size_t)value) {
461 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
462 "exceeding address space");
463 return;
464 }
465
466 s->xbzrle_cache_size = xbzrle_cache_resize(value);
467}
468
469int64_t qmp_query_migrate_cache_size(Error **errp)
470{
471 return migrate_xbzrle_cache_size();
472}
473
3dc85383 474void qmp_migrate_set_speed(int64_t value, Error **errp)
cab30143 475{
cab30143
JQ
476 MigrationState *s;
477
3dc85383
LC
478 if (value < 0) {
479 value = 0;
99a0db9b 480 }
442773ce
PB
481 if (value > SIZE_MAX) {
482 value = SIZE_MAX;
483 }
cab30143 484
17549e84 485 s = migrate_get_current();
3dc85383 486 s->bandwidth_limit = value;
442773ce
PB
487 if (s->file) {
488 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
489 }
cab30143
JQ
490}
491
4f0a993b 492void qmp_migrate_set_downtime(double value, Error **errp)
cab30143 493{
4f0a993b
LC
494 value *= 1e9;
495 value = MAX(0, MIN(UINT64_MAX, value));
496 max_downtime = (uint64_t)value;
99a0db9b 497}
17ad9b35 498
60d9222c
MH
499bool migrate_rdma_pin_all(void)
500{
501 MigrationState *s;
502
503 s = migrate_get_current();
504
505 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_RDMA_PIN_ALL];
506}
507
bde1e2ec
CV
508bool migrate_auto_converge(void)
509{
510 MigrationState *s;
511
512 s = migrate_get_current();
513
514 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
515}
516
323004a3
PL
517bool migrate_zero_blocks(void)
518{
519 MigrationState *s;
520
521 s = migrate_get_current();
522
523 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
524}
525
17ad9b35
OW
526int migrate_use_xbzrle(void)
527{
528 MigrationState *s;
529
530 s = migrate_get_current();
531
532 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
533}
534
535int64_t migrate_xbzrle_cache_size(void)
536{
537 MigrationState *s;
538
539 s = migrate_get_current();
540
541 return s->xbzrle_cache_size;
542}
0d82d0e8
JQ
543
544/* migration thread support */
545
5f496a1b 546static void *migration_thread(void *opaque)
0d82d0e8 547{
9848a404 548 MigrationState *s = opaque;
bc72ad67
AB
549 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
550 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
be7172e2 551 int64_t initial_bytes = 0;
0d82d0e8 552 int64_t max_size = 0;
a3fa1d78
PB
553 int64_t start_time = initial_time;
554 bool old_vm_running = false;
76f5933a 555
76f5933a 556 DPRINTF("beginning savevm\n");
dba433c0 557 qemu_savevm_state_begin(s->file, &s->params);
0d82d0e8 558
bc72ad67 559 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
29ae8a41
MH
560 migrate_set_state(s, MIG_STATE_SETUP, MIG_STATE_ACTIVE);
561
ed4fbd10
MH
562 DPRINTF("setup complete\n");
563
dba433c0 564 while (s->state == MIG_STATE_ACTIVE) {
a3e879cd 565 int64_t current_time;
c369f40d 566 uint64_t pending_size;
0d82d0e8 567
a0ff044b 568 if (!qemu_file_rate_limit(s->file)) {
c369f40d
JQ
569 DPRINTF("iterate\n");
570 pending_size = qemu_savevm_state_pending(s->file, max_size);
5016e2df
CD
571 DPRINTF("pending size %" PRIu64 " max %" PRIu64 "\n",
572 pending_size, max_size);
b22ff1fb 573 if (pending_size && pending_size >= max_size) {
dba433c0 574 qemu_savevm_state_iterate(s->file);
c369f40d 575 } else {
0e1146a7
KW
576 int ret;
577
c369f40d 578 DPRINTF("done iterating\n");
32c835ba 579 qemu_mutex_lock_iothread();
bc72ad67 580 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
c369f40d 581 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
a3fa1d78 582 old_vm_running = runstate_is_running();
0e1146a7
KW
583
584 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
585 if (ret >= 0) {
586 qemu_file_set_rate_limit(s->file, INT_MAX);
587 qemu_savevm_state_complete(s->file);
588 }
32c835ba 589 qemu_mutex_unlock_iothread();
0e1146a7
KW
590
591 if (ret < 0) {
d58f574b 592 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR);
0e1146a7
KW
593 break;
594 }
595
059f896c 596 if (!qemu_file_get_error(s->file)) {
d58f574b 597 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_COMPLETED);
059f896c
PB
598 break;
599 }
c369f40d
JQ
600 }
601 }
f4410a5d 602
fd45ee2c 603 if (qemu_file_get_error(s->file)) {
d58f574b 604 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR);
fd45ee2c
PB
605 break;
606 }
bc72ad67 607 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
0d82d0e8 608 if (current_time >= initial_time + BUFFER_DELAY) {
be7172e2 609 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
77417f10 610 uint64_t time_spent = current_time - initial_time;
0d82d0e8
JQ
611 double bandwidth = transferred_bytes / time_spent;
612 max_size = bandwidth * migrate_max_downtime() / 1000000;
613
7e114f8c
MH
614 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
615 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
616
0d82d0e8
JQ
617 DPRINTF("transferred %" PRIu64 " time_spent %" PRIu64
618 " bandwidth %g max_size %" PRId64 "\n",
619 transferred_bytes, time_spent, bandwidth, max_size);
90f8ae72
JQ
620 /* if we haven't sent anything, we don't want to recalculate
621 10000 is a small enough number for our purposes */
622 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
623 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
624 }
0d82d0e8 625
1964a397 626 qemu_file_reset_rate_limit(s->file);
0d82d0e8 627 initial_time = current_time;
be7172e2 628 initial_bytes = qemu_ftell(s->file);
0d82d0e8 629 }
a0ff044b 630 if (qemu_file_rate_limit(s->file)) {
0d82d0e8
JQ
631 /* usleep expects microseconds */
632 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
633 }
a3fa1d78
PB
634 }
635
f4410a5d 636 qemu_mutex_lock_iothread();
a3fa1d78 637 if (s->state == MIG_STATE_COMPLETED) {
bc72ad67 638 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
a3fa1d78
PB
639 s->total_time = end_time - s->total_time;
640 s->downtime = end_time - start_time;
641 runstate_set(RUN_STATE_POSTMIGRATE);
642 } else {
643 if (old_vm_running) {
a3fa1d78 644 vm_start();
dba433c0 645 }
0d82d0e8 646 }
bb1fadc4 647 qemu_bh_schedule(s->cleanup_bh);
dba433c0 648 qemu_mutex_unlock_iothread();
f4410a5d 649
0d82d0e8
JQ
650 return NULL;
651}
652
9848a404 653void migrate_fd_connect(MigrationState *s)
0d82d0e8 654{
29ae8a41
MH
655 s->state = MIG_STATE_SETUP;
656 trace_migrate_set_state(MIG_STATE_SETUP);
c09e5bb1 657
cc283e3b
JQ
658 /* This is a best 1st approximation. ns to ms */
659 s->expected_downtime = max_downtime/1000000;
bb1fadc4 660 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
0d82d0e8 661
442773ce
PB
662 qemu_file_set_rate_limit(s->file,
663 s->bandwidth_limit / XFER_LIMIT_RATIO);
664
9287ac27
SH
665 /* Notify before starting migration thread */
666 notifier_list_notify(&migration_state_notifiers, s);
667
5f496a1b 668 qemu_thread_create(&s->thread, migration_thread, s,
bb1fadc4 669 QEMU_THREAD_JOINABLE);
0d82d0e8 670}