usb: add usb_bus_release function
[qemu-kvm.git] / migration.c
blob8d675b31a1792a0e0ed174b1844354fe9efdcd8c
1 /*
2 * QEMU live migration
4 * Copyright IBM, Corp. 2008
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
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.
16 #include "qemu-common.h"
17 #include "qemu/main-loop.h"
18 #include "migration/migration.h"
19 #include "monitor/monitor.h"
20 #include "migration/qemu-file.h"
21 #include "sysemu/sysemu.h"
22 #include "block/block.h"
23 #include "qemu/sockets.h"
24 #include "migration/block.h"
25 #include "qemu/thread.h"
26 #include "qmp-commands.h"
27 #include "trace.h"
29 enum {
30 MIG_STATE_ERROR = -1,
31 MIG_STATE_NONE,
32 MIG_STATE_SETUP,
33 MIG_STATE_CANCELLING,
34 MIG_STATE_CANCELLED,
35 MIG_STATE_ACTIVE,
36 MIG_STATE_COMPLETED,
39 #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
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)
46 /* Migration XBZRLE default cache size */
47 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
49 static NotifierList migration_state_notifiers =
50 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
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 */
56 MigrationState *migrate_get_current(void)
58 static MigrationState current_migration = {
59 .state = MIG_STATE_NONE,
60 .bandwidth_limit = MAX_THROTTLE,
61 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
62 .mbps = -1,
65 return &current_migration;
68 void qemu_start_incoming_migration(const char *uri, Error **errp)
70 const char *p;
72 if (strstart(uri, "tcp:", &p))
73 tcp_start_incoming_migration(p, errp);
74 #ifdef CONFIG_RDMA
75 else if (strstart(uri, "rdma:", &p))
76 rdma_start_incoming_migration(p, errp);
77 #endif
78 #if !defined(WIN32)
79 else if (strstart(uri, "exec:", &p))
80 exec_start_incoming_migration(p, errp);
81 else if (strstart(uri, "unix:", &p))
82 unix_start_incoming_migration(p, errp);
83 else if (strstart(uri, "fd:", &p))
84 fd_start_incoming_migration(p, errp);
85 #endif
86 else {
87 error_setg(errp, "unknown migration protocol: %s", uri);
91 static void process_incoming_migration_co(void *opaque)
93 QEMUFile *f = opaque;
94 Error *local_err = NULL;
95 int ret;
97 ret = qemu_loadvm_state(f);
98 qemu_fclose(f);
99 free_xbzrle_decoded_buf();
100 if (ret < 0) {
101 error_report("load of migration failed: %s", strerror(-ret));
102 exit(EXIT_FAILURE);
104 qemu_announce_self();
106 bdrv_clear_incoming_migration_all();
107 /* Make sure all file formats flush their mutable metadata */
108 bdrv_invalidate_cache_all(&local_err);
109 if (local_err) {
110 qerror_report_err(local_err);
111 error_free(local_err);
112 exit(EXIT_FAILURE);
115 if (autostart) {
116 vm_start();
117 } else {
118 runstate_set(RUN_STATE_PAUSED);
122 void process_incoming_migration(QEMUFile *f)
124 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
125 int fd = qemu_get_fd(f);
127 assert(fd != -1);
128 qemu_set_nonblock(fd);
129 qemu_coroutine_enter(co, f);
132 /* amount of nanoseconds we are willing to wait for migration to be down.
133 * the choice of nanoseconds is because it is the maximum resolution that
134 * get_clock() can achieve. It is an internal measure. All user-visible
135 * units must be in seconds */
136 static uint64_t max_downtime = 300000000;
138 uint64_t migrate_max_downtime(void)
140 return max_downtime;
143 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
145 MigrationCapabilityStatusList *head = NULL;
146 MigrationCapabilityStatusList *caps;
147 MigrationState *s = migrate_get_current();
148 int i;
150 caps = NULL; /* silence compiler warning */
151 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
152 if (head == NULL) {
153 head = g_malloc0(sizeof(*caps));
154 caps = head;
155 } else {
156 caps->next = g_malloc0(sizeof(*caps));
157 caps = caps->next;
159 caps->value =
160 g_malloc(sizeof(*caps->value));
161 caps->value->capability = i;
162 caps->value->state = s->enabled_capabilities[i];
165 return head;
168 static void get_xbzrle_cache_stats(MigrationInfo *info)
170 if (migrate_use_xbzrle()) {
171 info->has_xbzrle_cache = true;
172 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
173 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
174 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
175 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
176 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
177 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
178 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
182 MigrationInfo *qmp_query_migrate(Error **errp)
184 MigrationInfo *info = g_malloc0(sizeof(*info));
185 MigrationState *s = migrate_get_current();
187 switch (s->state) {
188 case MIG_STATE_NONE:
189 /* no migration has happened ever */
190 break;
191 case MIG_STATE_SETUP:
192 info->has_status = true;
193 info->status = g_strdup("setup");
194 info->has_total_time = false;
195 break;
196 case MIG_STATE_ACTIVE:
197 case MIG_STATE_CANCELLING:
198 info->has_status = true;
199 info->status = g_strdup("active");
200 info->has_total_time = true;
201 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
202 - s->total_time;
203 info->has_expected_downtime = true;
204 info->expected_downtime = s->expected_downtime;
205 info->has_setup_time = true;
206 info->setup_time = s->setup_time;
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();
213 info->ram->duplicate = dup_mig_pages_transferred();
214 info->ram->skipped = skipped_mig_pages_transferred();
215 info->ram->normal = norm_mig_pages_transferred();
216 info->ram->normal_bytes = norm_mig_bytes_transferred();
217 info->ram->dirty_pages_rate = s->dirty_pages_rate;
218 info->ram->mbps = s->mbps;
219 info->ram->dirty_sync_count = s->dirty_sync_count;
221 if (blk_mig_active()) {
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();
229 get_xbzrle_cache_stats(info);
230 break;
231 case MIG_STATE_COMPLETED:
232 get_xbzrle_cache_stats(info);
234 info->has_status = true;
235 info->status = g_strdup("completed");
236 info->has_total_time = true;
237 info->total_time = s->total_time;
238 info->has_downtime = true;
239 info->downtime = s->downtime;
240 info->has_setup_time = true;
241 info->setup_time = s->setup_time;
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();
248 info->ram->duplicate = dup_mig_pages_transferred();
249 info->ram->skipped = skipped_mig_pages_transferred();
250 info->ram->normal = norm_mig_pages_transferred();
251 info->ram->normal_bytes = norm_mig_bytes_transferred();
252 info->ram->mbps = s->mbps;
253 info->ram->dirty_sync_count = s->dirty_sync_count;
254 break;
255 case MIG_STATE_ERROR:
256 info->has_status = true;
257 info->status = g_strdup("failed");
258 break;
259 case MIG_STATE_CANCELLED:
260 info->has_status = true;
261 info->status = g_strdup("cancelled");
262 break;
265 return info;
268 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
269 Error **errp)
271 MigrationState *s = migrate_get_current();
272 MigrationCapabilityStatusList *cap;
274 if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP) {
275 error_set(errp, QERR_MIGRATION_ACTIVE);
276 return;
279 for (cap = params; cap; cap = cap->next) {
280 s->enabled_capabilities[cap->value->capability] = cap->value->state;
284 /* shared migration helpers */
286 static void migrate_set_state(MigrationState *s, int old_state, int new_state)
288 if (atomic_cmpxchg(&s->state, old_state, new_state) == new_state) {
289 trace_migrate_set_state(new_state);
293 static void migrate_fd_cleanup(void *opaque)
295 MigrationState *s = opaque;
297 qemu_bh_delete(s->cleanup_bh);
298 s->cleanup_bh = NULL;
300 if (s->file) {
301 trace_migrate_fd_cleanup();
302 qemu_mutex_unlock_iothread();
303 qemu_thread_join(&s->thread);
304 qemu_mutex_lock_iothread();
306 qemu_fclose(s->file);
307 s->file = NULL;
310 assert(s->state != MIG_STATE_ACTIVE);
312 if (s->state != MIG_STATE_COMPLETED) {
313 qemu_savevm_state_cancel();
314 if (s->state == MIG_STATE_CANCELLING) {
315 migrate_set_state(s, MIG_STATE_CANCELLING, MIG_STATE_CANCELLED);
319 notifier_list_notify(&migration_state_notifiers, s);
322 void migrate_fd_error(MigrationState *s)
324 trace_migrate_fd_error();
325 assert(s->file == NULL);
326 s->state = MIG_STATE_ERROR;
327 trace_migrate_set_state(MIG_STATE_ERROR);
328 notifier_list_notify(&migration_state_notifiers, s);
331 static void migrate_fd_cancel(MigrationState *s)
333 int old_state ;
334 trace_migrate_fd_cancel();
336 do {
337 old_state = s->state;
338 if (old_state != MIG_STATE_SETUP && old_state != MIG_STATE_ACTIVE) {
339 break;
341 migrate_set_state(s, old_state, MIG_STATE_CANCELLING);
342 } while (s->state != MIG_STATE_CANCELLING);
345 void add_migration_state_change_notifier(Notifier *notify)
347 notifier_list_add(&migration_state_notifiers, notify);
350 void remove_migration_state_change_notifier(Notifier *notify)
352 notifier_remove(notify);
355 bool migration_in_setup(MigrationState *s)
357 return s->state == MIG_STATE_SETUP;
360 bool migration_has_finished(MigrationState *s)
362 return s->state == MIG_STATE_COMPLETED;
365 bool migration_has_failed(MigrationState *s)
367 return (s->state == MIG_STATE_CANCELLED ||
368 s->state == MIG_STATE_ERROR);
371 static MigrationState *migrate_init(const MigrationParams *params)
373 MigrationState *s = migrate_get_current();
374 int64_t bandwidth_limit = s->bandwidth_limit;
375 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
376 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
378 memcpy(enabled_capabilities, s->enabled_capabilities,
379 sizeof(enabled_capabilities));
381 memset(s, 0, sizeof(*s));
382 s->params = *params;
383 memcpy(s->enabled_capabilities, enabled_capabilities,
384 sizeof(enabled_capabilities));
385 s->xbzrle_cache_size = xbzrle_cache_size;
387 s->bandwidth_limit = bandwidth_limit;
388 s->state = MIG_STATE_SETUP;
389 trace_migrate_set_state(MIG_STATE_SETUP);
391 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
392 return s;
395 static GSList *migration_blockers;
397 void migrate_add_blocker(Error *reason)
399 migration_blockers = g_slist_prepend(migration_blockers, reason);
402 void migrate_del_blocker(Error *reason)
404 migration_blockers = g_slist_remove(migration_blockers, reason);
407 void qmp_migrate(const char *uri, bool has_blk, bool blk,
408 bool has_inc, bool inc, bool has_detach, bool detach,
409 Error **errp)
411 Error *local_err = NULL;
412 MigrationState *s = migrate_get_current();
413 MigrationParams params;
414 const char *p;
416 params.blk = has_blk && blk;
417 params.shared = has_inc && inc;
419 if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP ||
420 s->state == MIG_STATE_CANCELLING) {
421 error_set(errp, QERR_MIGRATION_ACTIVE);
422 return;
425 if (runstate_check(RUN_STATE_INMIGRATE)) {
426 error_setg(errp, "Guest is waiting for an incoming migration");
427 return;
430 if (qemu_savevm_state_blocked(errp)) {
431 return;
434 if (migration_blockers) {
435 *errp = error_copy(migration_blockers->data);
436 return;
439 s = migrate_init(&params);
441 if (strstart(uri, "tcp:", &p)) {
442 tcp_start_outgoing_migration(s, p, &local_err);
443 #ifdef CONFIG_RDMA
444 } else if (strstart(uri, "rdma:", &p)) {
445 rdma_start_outgoing_migration(s, p, &local_err);
446 #endif
447 #if !defined(WIN32)
448 } else if (strstart(uri, "exec:", &p)) {
449 exec_start_outgoing_migration(s, p, &local_err);
450 } else if (strstart(uri, "unix:", &p)) {
451 unix_start_outgoing_migration(s, p, &local_err);
452 } else if (strstart(uri, "fd:", &p)) {
453 fd_start_outgoing_migration(s, p, &local_err);
454 #endif
455 } else {
456 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
457 s->state = MIG_STATE_ERROR;
458 return;
461 if (local_err) {
462 migrate_fd_error(s);
463 error_propagate(errp, local_err);
464 return;
468 void qmp_migrate_cancel(Error **errp)
470 migrate_fd_cancel(migrate_get_current());
473 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
475 MigrationState *s = migrate_get_current();
476 int64_t new_size;
478 /* Check for truncation */
479 if (value != (size_t)value) {
480 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
481 "exceeding address space");
482 return;
485 /* Cache should not be larger than guest ram size */
486 if (value > ram_bytes_total()) {
487 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
488 "exceeds guest ram size ");
489 return;
492 new_size = xbzrle_cache_resize(value);
493 if (new_size < 0) {
494 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
495 "is smaller than page size");
496 return;
499 s->xbzrle_cache_size = new_size;
502 int64_t qmp_query_migrate_cache_size(Error **errp)
504 return migrate_xbzrle_cache_size();
507 void qmp_migrate_set_speed(int64_t value, Error **errp)
509 MigrationState *s;
511 if (value < 0) {
512 value = 0;
514 if (value > SIZE_MAX) {
515 value = SIZE_MAX;
518 s = migrate_get_current();
519 s->bandwidth_limit = value;
520 if (s->file) {
521 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
525 void qmp_migrate_set_downtime(double value, Error **errp)
527 value *= 1e9;
528 value = MAX(0, MIN(UINT64_MAX, value));
529 max_downtime = (uint64_t)value;
532 bool migrate_rdma_pin_all(void)
534 MigrationState *s;
536 s = migrate_get_current();
538 return s->enabled_capabilities[MIGRATION_CAPABILITY_RDMA_PIN_ALL];
541 bool migrate_auto_converge(void)
543 MigrationState *s;
545 s = migrate_get_current();
547 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
550 bool migrate_zero_blocks(void)
552 MigrationState *s;
554 s = migrate_get_current();
556 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
559 int migrate_use_xbzrle(void)
561 MigrationState *s;
563 s = migrate_get_current();
565 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
568 int64_t migrate_xbzrle_cache_size(void)
570 MigrationState *s;
572 s = migrate_get_current();
574 return s->xbzrle_cache_size;
577 /* migration thread support */
579 static void *migration_thread(void *opaque)
581 MigrationState *s = opaque;
582 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
583 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
584 int64_t initial_bytes = 0;
585 int64_t max_size = 0;
586 int64_t start_time = initial_time;
587 bool old_vm_running = false;
589 qemu_savevm_state_begin(s->file, &s->params);
591 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
592 migrate_set_state(s, MIG_STATE_SETUP, MIG_STATE_ACTIVE);
594 while (s->state == MIG_STATE_ACTIVE) {
595 int64_t current_time;
596 uint64_t pending_size;
598 if (!qemu_file_rate_limit(s->file)) {
599 pending_size = qemu_savevm_state_pending(s->file, max_size);
600 trace_migrate_pending(pending_size, max_size);
601 if (pending_size && pending_size >= max_size) {
602 qemu_savevm_state_iterate(s->file);
603 } else {
604 int ret;
606 qemu_mutex_lock_iothread();
607 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
608 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
609 old_vm_running = runstate_is_running();
611 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
612 if (ret >= 0) {
613 qemu_file_set_rate_limit(s->file, INT64_MAX);
614 qemu_savevm_state_complete(s->file);
616 qemu_mutex_unlock_iothread();
618 if (ret < 0) {
619 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR);
620 break;
623 if (!qemu_file_get_error(s->file)) {
624 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_COMPLETED);
625 break;
630 if (qemu_file_get_error(s->file)) {
631 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR);
632 break;
634 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
635 if (current_time >= initial_time + BUFFER_DELAY) {
636 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
637 uint64_t time_spent = current_time - initial_time;
638 double bandwidth = transferred_bytes / time_spent;
639 max_size = bandwidth * migrate_max_downtime() / 1000000;
641 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
642 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
644 trace_migrate_transferred(transferred_bytes, time_spent,
645 bandwidth, max_size);
646 /* if we haven't sent anything, we don't want to recalculate
647 10000 is a small enough number for our purposes */
648 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
649 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
652 qemu_file_reset_rate_limit(s->file);
653 initial_time = current_time;
654 initial_bytes = qemu_ftell(s->file);
656 if (qemu_file_rate_limit(s->file)) {
657 /* usleep expects microseconds */
658 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
662 qemu_mutex_lock_iothread();
663 if (s->state == MIG_STATE_COMPLETED) {
664 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
665 uint64_t transferred_bytes = qemu_ftell(s->file);
666 s->total_time = end_time - s->total_time;
667 s->downtime = end_time - start_time;
668 if (s->total_time) {
669 s->mbps = (((double) transferred_bytes * 8.0) /
670 ((double) s->total_time)) / 1000;
672 runstate_set(RUN_STATE_POSTMIGRATE);
673 } else {
674 if (old_vm_running) {
675 vm_start();
678 qemu_bh_schedule(s->cleanup_bh);
679 qemu_mutex_unlock_iothread();
681 return NULL;
684 void migrate_fd_connect(MigrationState *s)
686 s->state = MIG_STATE_SETUP;
687 trace_migrate_set_state(MIG_STATE_SETUP);
689 /* This is a best 1st approximation. ns to ms */
690 s->expected_downtime = max_downtime/1000000;
691 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
693 qemu_file_set_rate_limit(s->file,
694 s->bandwidth_limit / XFER_LIMIT_RATIO);
696 /* Notify before starting migration thread */
697 notifier_list_notify(&migration_state_notifiers, s);
699 qemu_thread_create(&s->thread, "migration", migration_thread, s,
700 QEMU_THREAD_JOINABLE);