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