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