pci: Replace pci_find_domain() with more general pci_root_bus_path()
[qemu/ar7.git] / migration.c
bloba704d48669f2bf2d6bcfd941028cdef7f0390814
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 "migration/migration.h"
18 #include "monitor/monitor.h"
19 #include "migration/qemu-file.h"
20 #include "sysemu/sysemu.h"
21 #include "block/block.h"
22 #include "qemu/sockets.h"
23 #include "migration/block.h"
24 #include "qemu/thread.h"
25 #include "qmp-commands.h"
26 #include "trace.h"
28 //#define DEBUG_MIGRATION
30 #ifdef DEBUG_MIGRATION
31 #define DPRINTF(fmt, ...) \
32 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
33 #else
34 #define DPRINTF(fmt, ...) \
35 do { } while (0)
36 #endif
38 enum {
39 MIG_STATE_ERROR,
40 MIG_STATE_SETUP,
41 MIG_STATE_CANCELLED,
42 MIG_STATE_ACTIVE,
43 MIG_STATE_COMPLETED,
46 #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
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)
53 /* Migration XBZRLE default cache size */
54 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
56 static NotifierList migration_state_notifiers =
57 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
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 */
63 MigrationState *migrate_get_current(void)
65 static MigrationState current_migration = {
66 .state = MIG_STATE_SETUP,
67 .bandwidth_limit = MAX_THROTTLE,
68 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
69 .mbps = -1,
72 return &current_migration;
75 void qemu_start_incoming_migration(const char *uri, Error **errp)
77 const char *p;
79 if (strstart(uri, "tcp:", &p))
80 tcp_start_incoming_migration(p, errp);
81 #if !defined(WIN32)
82 else if (strstart(uri, "exec:", &p))
83 exec_start_incoming_migration(p, errp);
84 else if (strstart(uri, "unix:", &p))
85 unix_start_incoming_migration(p, errp);
86 else if (strstart(uri, "fd:", &p))
87 fd_start_incoming_migration(p, errp);
88 #endif
89 else {
90 error_setg(errp, "unknown migration protocol: %s", uri);
94 static void process_incoming_migration_co(void *opaque)
96 QEMUFile *f = opaque;
97 int ret;
99 ret = qemu_loadvm_state(f);
100 qemu_fclose(f);
101 if (ret < 0) {
102 fprintf(stderr, "load of migration failed\n");
103 exit(EXIT_FAILURE);
105 qemu_announce_self();
106 DPRINTF("successfully loaded vm state\n");
108 bdrv_clear_incoming_migration_all();
109 /* Make sure all file formats flush their mutable metadata */
110 bdrv_invalidate_cache_all();
112 if (autostart) {
113 vm_start();
114 } else {
115 runstate_set(RUN_STATE_PAUSED);
119 void process_incoming_migration(QEMUFile *f)
121 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
122 int fd = qemu_get_fd(f);
124 assert(fd != -1);
125 qemu_set_nonblock(fd);
126 qemu_coroutine_enter(co, f);
129 /* amount of nanoseconds we are willing to wait for migration to be down.
130 * the choice of nanoseconds is because it is the maximum resolution that
131 * get_clock() can achieve. It is an internal measure. All user-visible
132 * units must be in seconds */
133 static uint64_t max_downtime = 30000000;
135 uint64_t migrate_max_downtime(void)
137 return max_downtime;
140 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
142 MigrationCapabilityStatusList *head = NULL;
143 MigrationCapabilityStatusList *caps;
144 MigrationState *s = migrate_get_current();
145 int i;
147 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
148 if (head == NULL) {
149 head = g_malloc0(sizeof(*caps));
150 caps = head;
151 } else {
152 caps->next = g_malloc0(sizeof(*caps));
153 caps = caps->next;
155 caps->value =
156 g_malloc(sizeof(*caps->value));
157 caps->value->capability = i;
158 caps->value->state = s->enabled_capabilities[i];
161 return head;
164 static void get_xbzrle_cache_stats(MigrationInfo *info)
166 if (migrate_use_xbzrle()) {
167 info->has_xbzrle_cache = true;
168 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
169 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
170 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
171 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
172 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
173 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
177 MigrationInfo *qmp_query_migrate(Error **errp)
179 MigrationInfo *info = g_malloc0(sizeof(*info));
180 MigrationState *s = migrate_get_current();
182 switch (s->state) {
183 case MIG_STATE_SETUP:
184 /* no migration has happened ever */
185 break;
186 case MIG_STATE_ACTIVE:
187 info->has_status = true;
188 info->status = g_strdup("active");
189 info->has_total_time = true;
190 info->total_time = qemu_get_clock_ms(rt_clock)
191 - s->total_time;
192 info->has_expected_downtime = true;
193 info->expected_downtime = s->expected_downtime;
195 info->has_ram = true;
196 info->ram = g_malloc0(sizeof(*info->ram));
197 info->ram->transferred = ram_bytes_transferred();
198 info->ram->remaining = ram_bytes_remaining();
199 info->ram->total = ram_bytes_total();
200 info->ram->duplicate = dup_mig_pages_transferred();
201 info->ram->skipped = skipped_mig_pages_transferred();
202 info->ram->normal = norm_mig_pages_transferred();
203 info->ram->normal_bytes = norm_mig_bytes_transferred();
204 info->ram->dirty_pages_rate = s->dirty_pages_rate;
205 info->ram->mbps = s->mbps;
207 if (blk_mig_active()) {
208 info->has_disk = true;
209 info->disk = g_malloc0(sizeof(*info->disk));
210 info->disk->transferred = blk_mig_bytes_transferred();
211 info->disk->remaining = blk_mig_bytes_remaining();
212 info->disk->total = blk_mig_bytes_total();
215 get_xbzrle_cache_stats(info);
216 break;
217 case MIG_STATE_COMPLETED:
218 get_xbzrle_cache_stats(info);
220 info->has_status = true;
221 info->status = g_strdup("completed");
222 info->total_time = s->total_time;
223 info->has_downtime = true;
224 info->downtime = s->downtime;
226 info->has_ram = true;
227 info->ram = g_malloc0(sizeof(*info->ram));
228 info->ram->transferred = ram_bytes_transferred();
229 info->ram->remaining = 0;
230 info->ram->total = ram_bytes_total();
231 info->ram->duplicate = dup_mig_pages_transferred();
232 info->ram->skipped = skipped_mig_pages_transferred();
233 info->ram->normal = norm_mig_pages_transferred();
234 info->ram->normal_bytes = norm_mig_bytes_transferred();
235 info->ram->mbps = s->mbps;
236 break;
237 case MIG_STATE_ERROR:
238 info->has_status = true;
239 info->status = g_strdup("failed");
240 break;
241 case MIG_STATE_CANCELLED:
242 info->has_status = true;
243 info->status = g_strdup("cancelled");
244 break;
247 return info;
250 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
251 Error **errp)
253 MigrationState *s = migrate_get_current();
254 MigrationCapabilityStatusList *cap;
256 if (s->state == MIG_STATE_ACTIVE) {
257 error_set(errp, QERR_MIGRATION_ACTIVE);
258 return;
261 for (cap = params; cap; cap = cap->next) {
262 s->enabled_capabilities[cap->value->capability] = cap->value->state;
266 /* shared migration helpers */
268 static void migrate_fd_cleanup(void *opaque)
270 MigrationState *s = opaque;
272 qemu_bh_delete(s->cleanup_bh);
273 s->cleanup_bh = NULL;
275 if (s->file) {
276 DPRINTF("closing file\n");
277 qemu_mutex_unlock_iothread();
278 qemu_thread_join(&s->thread);
279 qemu_mutex_lock_iothread();
281 qemu_fclose(s->file);
282 s->file = NULL;
285 assert(s->state != MIG_STATE_ACTIVE);
287 if (s->state != MIG_STATE_COMPLETED) {
288 qemu_savevm_state_cancel();
291 notifier_list_notify(&migration_state_notifiers, s);
294 static void migrate_finish_set_state(MigrationState *s, int new_state)
296 if (__sync_val_compare_and_swap(&s->state, MIG_STATE_ACTIVE,
297 new_state) == new_state) {
298 trace_migrate_set_state(new_state);
302 void migrate_fd_error(MigrationState *s)
304 DPRINTF("setting error state\n");
305 assert(s->file == NULL);
306 s->state = MIG_STATE_ERROR;
307 trace_migrate_set_state(MIG_STATE_ERROR);
308 notifier_list_notify(&migration_state_notifiers, s);
311 static void migrate_fd_cancel(MigrationState *s)
313 DPRINTF("cancelling migration\n");
315 migrate_finish_set_state(s, MIG_STATE_CANCELLED);
318 void add_migration_state_change_notifier(Notifier *notify)
320 notifier_list_add(&migration_state_notifiers, notify);
323 void remove_migration_state_change_notifier(Notifier *notify)
325 notifier_remove(notify);
328 bool migration_is_active(MigrationState *s)
330 return s->state == MIG_STATE_ACTIVE;
333 bool migration_has_finished(MigrationState *s)
335 return s->state == MIG_STATE_COMPLETED;
338 bool migration_has_failed(MigrationState *s)
340 return (s->state == MIG_STATE_CANCELLED ||
341 s->state == MIG_STATE_ERROR);
344 static MigrationState *migrate_init(const MigrationParams *params)
346 MigrationState *s = migrate_get_current();
347 int64_t bandwidth_limit = s->bandwidth_limit;
348 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
349 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
351 memcpy(enabled_capabilities, s->enabled_capabilities,
352 sizeof(enabled_capabilities));
354 memset(s, 0, sizeof(*s));
355 s->params = *params;
356 memcpy(s->enabled_capabilities, enabled_capabilities,
357 sizeof(enabled_capabilities));
358 s->xbzrle_cache_size = xbzrle_cache_size;
360 s->bandwidth_limit = bandwidth_limit;
361 s->state = MIG_STATE_SETUP;
362 trace_migrate_set_state(MIG_STATE_SETUP);
364 s->total_time = qemu_get_clock_ms(rt_clock);
365 return s;
368 static GSList *migration_blockers;
370 void migrate_add_blocker(Error *reason)
372 migration_blockers = g_slist_prepend(migration_blockers, reason);
375 void migrate_del_blocker(Error *reason)
377 migration_blockers = g_slist_remove(migration_blockers, reason);
380 void qmp_migrate(const char *uri, bool has_blk, bool blk,
381 bool has_inc, bool inc, bool has_detach, bool detach,
382 Error **errp)
384 Error *local_err = NULL;
385 MigrationState *s = migrate_get_current();
386 MigrationParams params;
387 const char *p;
389 params.blk = blk;
390 params.shared = inc;
392 if (s->state == MIG_STATE_ACTIVE) {
393 error_set(errp, QERR_MIGRATION_ACTIVE);
394 return;
397 if (qemu_savevm_state_blocked(errp)) {
398 return;
401 if (migration_blockers) {
402 *errp = error_copy(migration_blockers->data);
403 return;
406 s = migrate_init(&params);
408 if (strstart(uri, "tcp:", &p)) {
409 tcp_start_outgoing_migration(s, p, &local_err);
410 #if !defined(WIN32)
411 } else if (strstart(uri, "exec:", &p)) {
412 exec_start_outgoing_migration(s, p, &local_err);
413 } else if (strstart(uri, "unix:", &p)) {
414 unix_start_outgoing_migration(s, p, &local_err);
415 } else if (strstart(uri, "fd:", &p)) {
416 fd_start_outgoing_migration(s, p, &local_err);
417 #endif
418 } else {
419 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
420 return;
423 if (local_err) {
424 migrate_fd_error(s);
425 error_propagate(errp, local_err);
426 return;
430 void qmp_migrate_cancel(Error **errp)
432 migrate_fd_cancel(migrate_get_current());
435 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
437 MigrationState *s = migrate_get_current();
439 /* Check for truncation */
440 if (value != (size_t)value) {
441 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
442 "exceeding address space");
443 return;
446 s->xbzrle_cache_size = xbzrle_cache_resize(value);
449 int64_t qmp_query_migrate_cache_size(Error **errp)
451 return migrate_xbzrle_cache_size();
454 void qmp_migrate_set_speed(int64_t value, Error **errp)
456 MigrationState *s;
458 if (value < 0) {
459 value = 0;
461 if (value > SIZE_MAX) {
462 value = SIZE_MAX;
465 s = migrate_get_current();
466 s->bandwidth_limit = value;
467 if (s->file) {
468 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
472 void qmp_migrate_set_downtime(double value, Error **errp)
474 value *= 1e9;
475 value = MAX(0, MIN(UINT64_MAX, value));
476 max_downtime = (uint64_t)value;
479 bool migrate_rdma_pin_all(void)
481 MigrationState *s;
483 s = migrate_get_current();
485 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_RDMA_PIN_ALL];
488 int migrate_use_xbzrle(void)
490 MigrationState *s;
492 s = migrate_get_current();
494 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
497 int64_t migrate_xbzrle_cache_size(void)
499 MigrationState *s;
501 s = migrate_get_current();
503 return s->xbzrle_cache_size;
506 /* migration thread support */
508 static void *migration_thread(void *opaque)
510 MigrationState *s = opaque;
511 int64_t initial_time = qemu_get_clock_ms(rt_clock);
512 int64_t initial_bytes = 0;
513 int64_t max_size = 0;
514 int64_t start_time = initial_time;
515 bool old_vm_running = false;
517 DPRINTF("beginning savevm\n");
518 qemu_savevm_state_begin(s->file, &s->params);
520 while (s->state == MIG_STATE_ACTIVE) {
521 int64_t current_time;
522 uint64_t pending_size;
524 if (!qemu_file_rate_limit(s->file)) {
525 DPRINTF("iterate\n");
526 pending_size = qemu_savevm_state_pending(s->file, max_size);
527 DPRINTF("pending size %lu max %lu\n", pending_size, max_size);
528 if (pending_size && pending_size >= max_size) {
529 qemu_savevm_state_iterate(s->file);
530 } else {
531 DPRINTF("done iterating\n");
532 qemu_mutex_lock_iothread();
533 start_time = qemu_get_clock_ms(rt_clock);
534 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
535 old_vm_running = runstate_is_running();
536 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
537 qemu_file_set_rate_limit(s->file, INT_MAX);
538 qemu_savevm_state_complete(s->file);
539 qemu_mutex_unlock_iothread();
540 if (!qemu_file_get_error(s->file)) {
541 migrate_finish_set_state(s, MIG_STATE_COMPLETED);
542 break;
547 if (qemu_file_get_error(s->file)) {
548 migrate_finish_set_state(s, MIG_STATE_ERROR);
549 break;
551 current_time = qemu_get_clock_ms(rt_clock);
552 if (current_time >= initial_time + BUFFER_DELAY) {
553 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
554 uint64_t time_spent = current_time - initial_time;
555 double bandwidth = transferred_bytes / time_spent;
556 max_size = bandwidth * migrate_max_downtime() / 1000000;
558 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
559 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
561 DPRINTF("transferred %" PRIu64 " time_spent %" PRIu64
562 " bandwidth %g max_size %" PRId64 "\n",
563 transferred_bytes, time_spent, bandwidth, max_size);
564 /* if we haven't sent anything, we don't want to recalculate
565 10000 is a small enough number for our purposes */
566 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
567 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
570 qemu_file_reset_rate_limit(s->file);
571 initial_time = current_time;
572 initial_bytes = qemu_ftell(s->file);
574 if (qemu_file_rate_limit(s->file)) {
575 /* usleep expects microseconds */
576 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
580 qemu_mutex_lock_iothread();
581 if (s->state == MIG_STATE_COMPLETED) {
582 int64_t end_time = qemu_get_clock_ms(rt_clock);
583 s->total_time = end_time - s->total_time;
584 s->downtime = end_time - start_time;
585 runstate_set(RUN_STATE_POSTMIGRATE);
586 } else {
587 if (old_vm_running) {
588 vm_start();
591 qemu_bh_schedule(s->cleanup_bh);
592 qemu_mutex_unlock_iothread();
594 return NULL;
597 void migrate_fd_connect(MigrationState *s)
599 s->state = MIG_STATE_ACTIVE;
600 trace_migrate_set_state(MIG_STATE_ACTIVE);
602 /* This is a best 1st approximation. ns to ms */
603 s->expected_downtime = max_downtime/1000000;
604 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
606 qemu_file_set_rate_limit(s->file,
607 s->bandwidth_limit / XFER_LIMIT_RATIO);
609 qemu_thread_create(&s->thread, migration_thread, s,
610 QEMU_THREAD_JOINABLE);
611 notifier_list_notify(&migration_state_notifiers, s);