]> git.proxmox.com Git - mirror_qemu.git/blame - migration/migration.c
Merge remote-tracking branch 'remotes/stefanha/tags/net-pull-request' into staging
[mirror_qemu.git] / migration / 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 28
7dc688ed 29enum {
29ae8a41
MH
30 MIG_STATE_ERROR = -1,
31 MIG_STATE_NONE,
7dc688ed 32 MIG_STATE_SETUP,
51cf4c1a 33 MIG_STATE_CANCELLING,
7dc688ed
JQ
34 MIG_STATE_CANCELLED,
35 MIG_STATE_ACTIVE,
36 MIG_STATE_COMPLETED,
37};
5bb7910a 38
d0ae46c1 39#define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
5bb7910a 40
5b4e1eb7
JQ
41/* Amount of time to allocate to each "chunk" of bandwidth-throttled
42 * data. */
43#define BUFFER_DELAY 100
44#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
45
17ad9b35
OW
46/* Migration XBZRLE default cache size */
47#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
48
99a0db9b
GH
49static NotifierList migration_state_notifiers =
50 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
51
17549e84
JQ
52/* When we add fault tolerance, we could have several
53 migrations at once. For now we don't need to add
54 dynamic creation of migration */
55
859bc756 56MigrationState *migrate_get_current(void)
17549e84
JQ
57{
58 static MigrationState current_migration = {
29ae8a41 59 .state = MIG_STATE_NONE,
d0ae46c1 60 .bandwidth_limit = MAX_THROTTLE,
17ad9b35 61 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
7e114f8c 62 .mbps = -1,
17549e84
JQ
63 };
64
65 return &current_migration;
66}
67
43eaae28 68void qemu_start_incoming_migration(const char *uri, Error **errp)
5bb7910a 69{
34c9dd8e
AL
70 const char *p;
71
72 if (strstart(uri, "tcp:", &p))
43eaae28 73 tcp_start_incoming_migration(p, errp);
2da776db 74#ifdef CONFIG_RDMA
41310c68 75 else if (strstart(uri, "rdma:", &p))
2da776db
MH
76 rdma_start_incoming_migration(p, errp);
77#endif
065e2813
AL
78#if !defined(WIN32)
79 else if (strstart(uri, "exec:", &p))
43eaae28 80 exec_start_incoming_migration(p, errp);
4951f65b 81 else if (strstart(uri, "unix:", &p))
43eaae28 82 unix_start_incoming_migration(p, errp);
5ac1fad3 83 else if (strstart(uri, "fd:", &p))
43eaae28 84 fd_start_incoming_migration(p, errp);
065e2813 85#endif
8ca5e801 86 else {
312fd5f2 87 error_setg(errp, "unknown migration protocol: %s", uri);
8ca5e801 88 }
5bb7910a
AL
89}
90
82a4da79 91static void process_incoming_migration_co(void *opaque)
511c0231 92{
82a4da79 93 QEMUFile *f = opaque;
5a8a30db 94 Error *local_err = NULL;
1c12e1f5
PB
95 int ret;
96
97 ret = qemu_loadvm_state(f);
98 qemu_fclose(f);
905f26f2 99 free_xbzrle_decoded_buf();
1c12e1f5 100 if (ret < 0) {
db80face 101 error_report("load of migration failed: %s", strerror(-ret));
4aead692 102 exit(EXIT_FAILURE);
511c0231
JQ
103 }
104 qemu_announce_self();
511c0231 105
0f15423c 106 /* Make sure all file formats flush their mutable metadata */
5a8a30db
KW
107 bdrv_invalidate_cache_all(&local_err);
108 if (local_err) {
109 qerror_report_err(local_err);
110 error_free(local_err);
111 exit(EXIT_FAILURE);
112 }
0f15423c 113
f5bbfba1 114 if (autostart) {
511c0231 115 vm_start();
f5bbfba1 116 } else {
29ed72f1 117 runstate_set(RUN_STATE_PAUSED);
f5bbfba1 118 }
511c0231
JQ
119}
120
82a4da79
PB
121void process_incoming_migration(QEMUFile *f)
122{
123 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
124 int fd = qemu_get_fd(f);
125
126 assert(fd != -1);
f9e8cacc 127 qemu_set_nonblock(fd);
82a4da79
PB
128 qemu_coroutine_enter(co, f);
129}
130
a0a3fd60
GC
131/* amount of nanoseconds we are willing to wait for migration to be down.
132 * the choice of nanoseconds is because it is the maximum resolution that
133 * get_clock() can achieve. It is an internal measure. All user-visible
134 * units must be in seconds */
f7cd55a0 135static uint64_t max_downtime = 300000000;
a0a3fd60
GC
136
137uint64_t migrate_max_downtime(void)
138{
139 return max_downtime;
140}
141
bbf6da32
OW
142MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
143{
144 MigrationCapabilityStatusList *head = NULL;
145 MigrationCapabilityStatusList *caps;
146 MigrationState *s = migrate_get_current();
147 int i;
148
387eedeb 149 caps = NULL; /* silence compiler warning */
bbf6da32
OW
150 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
151 if (head == NULL) {
152 head = g_malloc0(sizeof(*caps));
153 caps = head;
154 } else {
155 caps->next = g_malloc0(sizeof(*caps));
156 caps = caps->next;
157 }
158 caps->value =
159 g_malloc(sizeof(*caps->value));
160 caps->value->capability = i;
161 caps->value->state = s->enabled_capabilities[i];
162 }
163
164 return head;
165}
166
f36d55af
OW
167static void get_xbzrle_cache_stats(MigrationInfo *info)
168{
169 if (migrate_use_xbzrle()) {
170 info->has_xbzrle_cache = true;
171 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
172 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
173 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
174 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
175 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
8bc39233 176 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
f36d55af
OW
177 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
178 }
179}
180
791e7c82 181MigrationInfo *qmp_query_migrate(Error **errp)
5bb7910a 182{
791e7c82 183 MigrationInfo *info = g_malloc0(sizeof(*info));
17549e84
JQ
184 MigrationState *s = migrate_get_current();
185
186 switch (s->state) {
29ae8a41 187 case MIG_STATE_NONE:
17549e84
JQ
188 /* no migration has happened ever */
189 break;
29ae8a41
MH
190 case MIG_STATE_SETUP:
191 info->has_status = true;
192 info->status = g_strdup("setup");
ed4fbd10 193 info->has_total_time = false;
29ae8a41 194 break;
17549e84 195 case MIG_STATE_ACTIVE:
51cf4c1a 196 case MIG_STATE_CANCELLING:
791e7c82
LC
197 info->has_status = true;
198 info->status = g_strdup("active");
7aa939af 199 info->has_total_time = true;
bc72ad67 200 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
7aa939af 201 - s->total_time;
2c52ddf1
JQ
202 info->has_expected_downtime = true;
203 info->expected_downtime = s->expected_downtime;
ed4fbd10
MH
204 info->has_setup_time = true;
205 info->setup_time = s->setup_time;
17549e84 206
791e7c82
LC
207 info->has_ram = true;
208 info->ram = g_malloc0(sizeof(*info->ram));
209 info->ram->transferred = ram_bytes_transferred();
210 info->ram->remaining = ram_bytes_remaining();
211 info->ram->total = ram_bytes_total();
004d4c10 212 info->ram->duplicate = dup_mig_pages_transferred();
f1c72795 213 info->ram->skipped = skipped_mig_pages_transferred();
004d4c10
OW
214 info->ram->normal = norm_mig_pages_transferred();
215 info->ram->normal_bytes = norm_mig_bytes_transferred();
8d017193 216 info->ram->dirty_pages_rate = s->dirty_pages_rate;
7e114f8c 217 info->ram->mbps = s->mbps;
58570ed8 218 info->ram->dirty_sync_count = s->dirty_sync_count;
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;
58570ed8 252 info->ram->dirty_sync_count = s->dirty_sync_count;
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
51cf4c1a
Z
285static void migrate_set_state(MigrationState *s, int old_state, int new_state)
286{
287 if (atomic_cmpxchg(&s->state, old_state, new_state) == new_state) {
288 trace_migrate_set_state(new_state);
289 }
290}
291
bb1fadc4 292static void migrate_fd_cleanup(void *opaque)
065e2813 293{
bb1fadc4
PB
294 MigrationState *s = opaque;
295
296 qemu_bh_delete(s->cleanup_bh);
297 s->cleanup_bh = NULL;
298
065e2813 299 if (s->file) {
9013dca5 300 trace_migrate_fd_cleanup();
404a7c05
PB
301 qemu_mutex_unlock_iothread();
302 qemu_thread_join(&s->thread);
303 qemu_mutex_lock_iothread();
304
6f190a06
PB
305 qemu_fclose(s->file);
306 s->file = NULL;
065e2813
AL
307 }
308
a3fa1d78 309 assert(s->state != MIG_STATE_ACTIVE);
7a2c1721 310
a3fa1d78 311 if (s->state != MIG_STATE_COMPLETED) {
7a2c1721 312 qemu_savevm_state_cancel();
51cf4c1a
Z
313 if (s->state == MIG_STATE_CANCELLING) {
314 migrate_set_state(s, MIG_STATE_CANCELLING, MIG_STATE_CANCELLED);
315 }
7a2c1721 316 }
a3fa1d78
PB
317
318 notifier_list_notify(&migration_state_notifiers, s);
065e2813
AL
319}
320
8b6b99b3 321void migrate_fd_error(MigrationState *s)
065e2813 322{
9013dca5 323 trace_migrate_fd_error();
bb1fadc4
PB
324 assert(s->file == NULL);
325 s->state = MIG_STATE_ERROR;
326 trace_migrate_set_state(MIG_STATE_ERROR);
327 notifier_list_notify(&migration_state_notifiers, s);
458cf28e
JQ
328}
329
0edda1c4 330static void migrate_fd_cancel(MigrationState *s)
065e2813 331{
6f2b811a 332 int old_state ;
a26ba26e 333 QEMUFile *f = migrate_get_current()->file;
9013dca5 334 trace_migrate_fd_cancel();
065e2813 335
6f2b811a
Z
336 do {
337 old_state = s->state;
338 if (old_state != MIG_STATE_SETUP && old_state != MIG_STATE_ACTIVE) {
339 break;
340 }
51cf4c1a
Z
341 migrate_set_state(s, old_state, MIG_STATE_CANCELLING);
342 } while (s->state != MIG_STATE_CANCELLING);
a26ba26e
DDAG
343
344 /*
345 * If we're unlucky the migration code might be stuck somewhere in a
346 * send/write while the network has failed and is waiting to timeout;
347 * if we've got shutdown(2) available then we can force it to quit.
348 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
349 * called in a bh, so there is no race against this cancel.
350 */
351 if (s->state == MIG_STATE_CANCELLING && f) {
352 qemu_file_shutdown(f);
353 }
065e2813
AL
354}
355
99a0db9b
GH
356void add_migration_state_change_notifier(Notifier *notify)
357{
358 notifier_list_add(&migration_state_notifiers, notify);
359}
360
361void remove_migration_state_change_notifier(Notifier *notify)
362{
31552529 363 notifier_remove(notify);
99a0db9b
GH
364}
365
02edd2e7 366bool migration_in_setup(MigrationState *s)
afe2df69 367{
02edd2e7 368 return s->state == MIG_STATE_SETUP;
afe2df69
GH
369}
370
7073693b 371bool migration_has_finished(MigrationState *s)
99a0db9b 372{
7073693b 373 return s->state == MIG_STATE_COMPLETED;
99a0db9b 374}
0edda1c4 375
afe2df69
GH
376bool migration_has_failed(MigrationState *s)
377{
378 return (s->state == MIG_STATE_CANCELLED ||
379 s->state == MIG_STATE_ERROR);
380}
381
6607ae23 382static MigrationState *migrate_init(const MigrationParams *params)
0edda1c4 383{
17549e84 384 MigrationState *s = migrate_get_current();
d0ae46c1 385 int64_t bandwidth_limit = s->bandwidth_limit;
bbf6da32 386 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
17ad9b35 387 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
bbf6da32
OW
388
389 memcpy(enabled_capabilities, s->enabled_capabilities,
390 sizeof(enabled_capabilities));
0edda1c4 391
17549e84 392 memset(s, 0, sizeof(*s));
6607ae23 393 s->params = *params;
bbf6da32
OW
394 memcpy(s->enabled_capabilities, enabled_capabilities,
395 sizeof(enabled_capabilities));
17ad9b35 396 s->xbzrle_cache_size = xbzrle_cache_size;
1299c631 397
0edda1c4 398 s->bandwidth_limit = bandwidth_limit;
d5934dde 399 s->state = MIG_STATE_SETUP;
c09e5bb1 400 trace_migrate_set_state(MIG_STATE_SETUP);
0edda1c4 401
bc72ad67 402 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
0edda1c4
JQ
403 return s;
404}
cab30143 405
fa2756b7
AL
406static GSList *migration_blockers;
407
408void migrate_add_blocker(Error *reason)
409{
410 migration_blockers = g_slist_prepend(migration_blockers, reason);
411}
412
413void migrate_del_blocker(Error *reason)
414{
415 migration_blockers = g_slist_remove(migration_blockers, reason);
416}
417
e1c37d0e
LC
418void qmp_migrate(const char *uri, bool has_blk, bool blk,
419 bool has_inc, bool inc, bool has_detach, bool detach,
420 Error **errp)
cab30143 421{
be7059cd 422 Error *local_err = NULL;
17549e84 423 MigrationState *s = migrate_get_current();
6607ae23 424 MigrationParams params;
cab30143 425 const char *p;
cab30143 426
8c0426ae
PP
427 params.blk = has_blk && blk;
428 params.shared = has_inc && inc;
6607ae23 429
51cf4c1a
Z
430 if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP ||
431 s->state == MIG_STATE_CANCELLING) {
e1c37d0e
LC
432 error_set(errp, QERR_MIGRATION_ACTIVE);
433 return;
cab30143
JQ
434 }
435
ca99993a
DDAG
436 if (runstate_check(RUN_STATE_INMIGRATE)) {
437 error_setg(errp, "Guest is waiting for an incoming migration");
438 return;
439 }
440
e1c37d0e
LC
441 if (qemu_savevm_state_blocked(errp)) {
442 return;
cab30143
JQ
443 }
444
fa2756b7 445 if (migration_blockers) {
e1c37d0e
LC
446 *errp = error_copy(migration_blockers->data);
447 return;
fa2756b7
AL
448 }
449
6607ae23 450 s = migrate_init(&params);
cab30143
JQ
451
452 if (strstart(uri, "tcp:", &p)) {
f37afb5a 453 tcp_start_outgoing_migration(s, p, &local_err);
2da776db 454#ifdef CONFIG_RDMA
41310c68 455 } else if (strstart(uri, "rdma:", &p)) {
2da776db
MH
456 rdma_start_outgoing_migration(s, p, &local_err);
457#endif
cab30143
JQ
458#if !defined(WIN32)
459 } else if (strstart(uri, "exec:", &p)) {
f37afb5a 460 exec_start_outgoing_migration(s, p, &local_err);
cab30143 461 } else if (strstart(uri, "unix:", &p)) {
f37afb5a 462 unix_start_outgoing_migration(s, p, &local_err);
cab30143 463 } else if (strstart(uri, "fd:", &p)) {
f37afb5a 464 fd_start_outgoing_migration(s, p, &local_err);
cab30143 465#endif
99a0db9b 466 } else {
e1c37d0e 467 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
c9501142 468 s->state = MIG_STATE_ERROR;
e1c37d0e 469 return;
cab30143
JQ
470 }
471
f37afb5a 472 if (local_err) {
342ab8d1 473 migrate_fd_error(s);
f37afb5a 474 error_propagate(errp, local_err);
e1c37d0e 475 return;
1299c631 476 }
cab30143
JQ
477}
478
6cdedb07 479void qmp_migrate_cancel(Error **errp)
cab30143 480{
17549e84 481 migrate_fd_cancel(migrate_get_current());
cab30143
JQ
482}
483
9e1ba4cc
OW
484void qmp_migrate_set_cache_size(int64_t value, Error **errp)
485{
486 MigrationState *s = migrate_get_current();
c91e681a 487 int64_t new_size;
9e1ba4cc
OW
488
489 /* Check for truncation */
490 if (value != (size_t)value) {
491 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
492 "exceeding address space");
493 return;
494 }
495
a5615b14
OW
496 /* Cache should not be larger than guest ram size */
497 if (value > ram_bytes_total()) {
498 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
499 "exceeds guest ram size ");
500 return;
501 }
502
c91e681a
OW
503 new_size = xbzrle_cache_resize(value);
504 if (new_size < 0) {
505 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
506 "is smaller than page size");
507 return;
508 }
509
510 s->xbzrle_cache_size = new_size;
9e1ba4cc
OW
511}
512
513int64_t qmp_query_migrate_cache_size(Error **errp)
514{
515 return migrate_xbzrle_cache_size();
516}
517
3dc85383 518void qmp_migrate_set_speed(int64_t value, Error **errp)
cab30143 519{
cab30143
JQ
520 MigrationState *s;
521
3dc85383
LC
522 if (value < 0) {
523 value = 0;
99a0db9b 524 }
442773ce
PB
525 if (value > SIZE_MAX) {
526 value = SIZE_MAX;
527 }
cab30143 528
17549e84 529 s = migrate_get_current();
3dc85383 530 s->bandwidth_limit = value;
442773ce
PB
531 if (s->file) {
532 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
533 }
cab30143
JQ
534}
535
4f0a993b 536void qmp_migrate_set_downtime(double value, Error **errp)
cab30143 537{
4f0a993b
LC
538 value *= 1e9;
539 value = MAX(0, MIN(UINT64_MAX, value));
540 max_downtime = (uint64_t)value;
99a0db9b 541}
17ad9b35 542
60d9222c
MH
543bool migrate_rdma_pin_all(void)
544{
545 MigrationState *s;
546
547 s = migrate_get_current();
548
41310c68 549 return s->enabled_capabilities[MIGRATION_CAPABILITY_RDMA_PIN_ALL];
60d9222c
MH
550}
551
bde1e2ec
CV
552bool migrate_auto_converge(void)
553{
554 MigrationState *s;
555
556 s = migrate_get_current();
557
558 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
559}
560
323004a3
PL
561bool migrate_zero_blocks(void)
562{
563 MigrationState *s;
564
565 s = migrate_get_current();
566
567 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
568}
569
17ad9b35
OW
570int migrate_use_xbzrle(void)
571{
572 MigrationState *s;
573
574 s = migrate_get_current();
575
576 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
577}
578
579int64_t migrate_xbzrle_cache_size(void)
580{
581 MigrationState *s;
582
583 s = migrate_get_current();
584
585 return s->xbzrle_cache_size;
586}
0d82d0e8
JQ
587
588/* migration thread support */
589
5f496a1b 590static void *migration_thread(void *opaque)
0d82d0e8 591{
9848a404 592 MigrationState *s = opaque;
bc72ad67
AB
593 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
594 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
be7172e2 595 int64_t initial_bytes = 0;
0d82d0e8 596 int64_t max_size = 0;
a3fa1d78
PB
597 int64_t start_time = initial_time;
598 bool old_vm_running = false;
76f5933a 599
dba433c0 600 qemu_savevm_state_begin(s->file, &s->params);
0d82d0e8 601
bc72ad67 602 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
29ae8a41
MH
603 migrate_set_state(s, MIG_STATE_SETUP, MIG_STATE_ACTIVE);
604
dba433c0 605 while (s->state == MIG_STATE_ACTIVE) {
a3e879cd 606 int64_t current_time;
c369f40d 607 uint64_t pending_size;
0d82d0e8 608
a0ff044b 609 if (!qemu_file_rate_limit(s->file)) {
c369f40d 610 pending_size = qemu_savevm_state_pending(s->file, max_size);
9013dca5 611 trace_migrate_pending(pending_size, max_size);
b22ff1fb 612 if (pending_size && pending_size >= max_size) {
dba433c0 613 qemu_savevm_state_iterate(s->file);
c369f40d 614 } else {
0e1146a7
KW
615 int ret;
616
32c835ba 617 qemu_mutex_lock_iothread();
bc72ad67 618 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
c369f40d 619 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
a3fa1d78 620 old_vm_running = runstate_is_running();
0e1146a7
KW
621
622 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
623 if (ret >= 0) {
40596834 624 qemu_file_set_rate_limit(s->file, INT64_MAX);
0e1146a7
KW
625 qemu_savevm_state_complete(s->file);
626 }
32c835ba 627 qemu_mutex_unlock_iothread();
0e1146a7
KW
628
629 if (ret < 0) {
d58f574b 630 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR);
0e1146a7
KW
631 break;
632 }
633
059f896c 634 if (!qemu_file_get_error(s->file)) {
d58f574b 635 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_COMPLETED);
059f896c
PB
636 break;
637 }
c369f40d
JQ
638 }
639 }
f4410a5d 640
fd45ee2c 641 if (qemu_file_get_error(s->file)) {
d58f574b 642 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR);
fd45ee2c
PB
643 break;
644 }
bc72ad67 645 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
0d82d0e8 646 if (current_time >= initial_time + BUFFER_DELAY) {
be7172e2 647 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
77417f10 648 uint64_t time_spent = current_time - initial_time;
0d82d0e8
JQ
649 double bandwidth = transferred_bytes / time_spent;
650 max_size = bandwidth * migrate_max_downtime() / 1000000;
651
7e114f8c
MH
652 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
653 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
654
9013dca5
AK
655 trace_migrate_transferred(transferred_bytes, time_spent,
656 bandwidth, max_size);
90f8ae72
JQ
657 /* if we haven't sent anything, we don't want to recalculate
658 10000 is a small enough number for our purposes */
659 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
660 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
661 }
0d82d0e8 662
1964a397 663 qemu_file_reset_rate_limit(s->file);
0d82d0e8 664 initial_time = current_time;
be7172e2 665 initial_bytes = qemu_ftell(s->file);
0d82d0e8 666 }
a0ff044b 667 if (qemu_file_rate_limit(s->file)) {
0d82d0e8
JQ
668 /* usleep expects microseconds */
669 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
670 }
a3fa1d78
PB
671 }
672
f4410a5d 673 qemu_mutex_lock_iothread();
a3fa1d78 674 if (s->state == MIG_STATE_COMPLETED) {
bc72ad67 675 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
d6ed7312 676 uint64_t transferred_bytes = qemu_ftell(s->file);
a3fa1d78
PB
677 s->total_time = end_time - s->total_time;
678 s->downtime = end_time - start_time;
d6ed7312
PL
679 if (s->total_time) {
680 s->mbps = (((double) transferred_bytes * 8.0) /
681 ((double) s->total_time)) / 1000;
682 }
a3fa1d78
PB
683 runstate_set(RUN_STATE_POSTMIGRATE);
684 } else {
685 if (old_vm_running) {
a3fa1d78 686 vm_start();
dba433c0 687 }
0d82d0e8 688 }
bb1fadc4 689 qemu_bh_schedule(s->cleanup_bh);
dba433c0 690 qemu_mutex_unlock_iothread();
f4410a5d 691
0d82d0e8
JQ
692 return NULL;
693}
694
9848a404 695void migrate_fd_connect(MigrationState *s)
0d82d0e8 696{
29ae8a41
MH
697 s->state = MIG_STATE_SETUP;
698 trace_migrate_set_state(MIG_STATE_SETUP);
c09e5bb1 699
cc283e3b
JQ
700 /* This is a best 1st approximation. ns to ms */
701 s->expected_downtime = max_downtime/1000000;
bb1fadc4 702 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
0d82d0e8 703
442773ce
PB
704 qemu_file_set_rate_limit(s->file,
705 s->bandwidth_limit / XFER_LIMIT_RATIO);
706
9287ac27
SH
707 /* Notify before starting migration thread */
708 notifier_list_notify(&migration_state_notifiers, s);
709
4900116e 710 qemu_thread_create(&s->thread, "migration", migration_thread, s,
bb1fadc4 711 QEMU_THREAD_JOINABLE);
0d82d0e8 712}