pcnet-pci: mark I/O and MMIO as LITTLE_ENDIAN
[qemu/ar7.git] / migration.c
blob200d404547885ea2c9bbbb5ca7c07ca5f743e1cd
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 //#define DEBUG_MIGRATION
31 #ifdef DEBUG_MIGRATION
32 #define DPRINTF(fmt, ...) \
33 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
34 #else
35 #define DPRINTF(fmt, ...) \
36 do { } while (0)
37 #endif
39 enum {
40 MIG_STATE_ERROR = -1,
41 MIG_STATE_NONE,
42 MIG_STATE_SETUP,
43 MIG_STATE_CANCELLED,
44 MIG_STATE_ACTIVE,
45 MIG_STATE_COMPLETED,
48 #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
50 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
51 * data. */
52 #define BUFFER_DELAY 100
53 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
55 /* Migration XBZRLE default cache size */
56 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
58 static NotifierList migration_state_notifiers =
59 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
61 /* When we add fault tolerance, we could have several
62 migrations at once. For now we don't need to add
63 dynamic creation of migration */
65 MigrationState *migrate_get_current(void)
67 static MigrationState current_migration = {
68 .state = MIG_STATE_NONE,
69 .bandwidth_limit = MAX_THROTTLE,
70 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
71 .mbps = -1,
74 return &current_migration;
77 void qemu_start_incoming_migration(const char *uri, Error **errp)
79 const char *p;
81 if (strstart(uri, "tcp:", &p))
82 tcp_start_incoming_migration(p, errp);
83 #ifdef CONFIG_RDMA
84 else if (strstart(uri, "x-rdma:", &p))
85 rdma_start_incoming_migration(p, errp);
86 #endif
87 #if !defined(WIN32)
88 else if (strstart(uri, "exec:", &p))
89 exec_start_incoming_migration(p, errp);
90 else if (strstart(uri, "unix:", &p))
91 unix_start_incoming_migration(p, errp);
92 else if (strstart(uri, "fd:", &p))
93 fd_start_incoming_migration(p, errp);
94 #endif
95 else {
96 error_setg(errp, "unknown migration protocol: %s", uri);
100 static void process_incoming_migration_co(void *opaque)
102 QEMUFile *f = opaque;
103 int ret;
105 ret = qemu_loadvm_state(f);
106 qemu_fclose(f);
107 if (ret < 0) {
108 fprintf(stderr, "load of migration failed\n");
109 exit(EXIT_FAILURE);
111 qemu_announce_self();
112 DPRINTF("successfully loaded vm state\n");
114 bdrv_clear_incoming_migration_all();
115 /* Make sure all file formats flush their mutable metadata */
116 bdrv_invalidate_cache_all();
118 if (autostart) {
119 vm_start();
120 } else {
121 runstate_set(RUN_STATE_PAUSED);
125 void process_incoming_migration(QEMUFile *f)
127 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
128 int fd = qemu_get_fd(f);
130 assert(fd != -1);
131 qemu_set_nonblock(fd);
132 qemu_coroutine_enter(co, f);
135 /* amount of nanoseconds we are willing to wait for migration to be down.
136 * the choice of nanoseconds is because it is the maximum resolution that
137 * get_clock() can achieve. It is an internal measure. All user-visible
138 * units must be in seconds */
139 static uint64_t max_downtime = 30000000;
141 uint64_t migrate_max_downtime(void)
143 return max_downtime;
146 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
148 MigrationCapabilityStatusList *head = NULL;
149 MigrationCapabilityStatusList *caps;
150 MigrationState *s = migrate_get_current();
151 int i;
153 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
154 if (head == NULL) {
155 head = g_malloc0(sizeof(*caps));
156 caps = head;
157 } else {
158 caps->next = g_malloc0(sizeof(*caps));
159 caps = caps->next;
161 caps->value =
162 g_malloc(sizeof(*caps->value));
163 caps->value->capability = i;
164 caps->value->state = s->enabled_capabilities[i];
167 return head;
170 static void get_xbzrle_cache_stats(MigrationInfo *info)
172 if (migrate_use_xbzrle()) {
173 info->has_xbzrle_cache = true;
174 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
175 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
176 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
177 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
178 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
179 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
183 MigrationInfo *qmp_query_migrate(Error **errp)
185 MigrationInfo *info = g_malloc0(sizeof(*info));
186 MigrationState *s = migrate_get_current();
188 switch (s->state) {
189 case MIG_STATE_NONE:
190 /* no migration has happened ever */
191 break;
192 case MIG_STATE_SETUP:
193 info->has_status = true;
194 info->status = g_strdup("setup");
195 info->has_total_time = false;
196 break;
197 case MIG_STATE_ACTIVE:
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;
220 if (blk_mig_active()) {
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();
228 get_xbzrle_cache_stats(info);
229 break;
230 case MIG_STATE_COMPLETED:
231 get_xbzrle_cache_stats(info);
233 info->has_status = true;
234 info->status = g_strdup("completed");
235 info->has_total_time = true;
236 info->total_time = s->total_time;
237 info->has_downtime = true;
238 info->downtime = s->downtime;
239 info->has_setup_time = true;
240 info->setup_time = s->setup_time;
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();
247 info->ram->duplicate = dup_mig_pages_transferred();
248 info->ram->skipped = skipped_mig_pages_transferred();
249 info->ram->normal = norm_mig_pages_transferred();
250 info->ram->normal_bytes = norm_mig_bytes_transferred();
251 info->ram->mbps = s->mbps;
252 break;
253 case MIG_STATE_ERROR:
254 info->has_status = true;
255 info->status = g_strdup("failed");
256 break;
257 case MIG_STATE_CANCELLED:
258 info->has_status = true;
259 info->status = g_strdup("cancelled");
260 break;
263 return info;
266 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
267 Error **errp)
269 MigrationState *s = migrate_get_current();
270 MigrationCapabilityStatusList *cap;
272 if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP) {
273 error_set(errp, QERR_MIGRATION_ACTIVE);
274 return;
277 for (cap = params; cap; cap = cap->next) {
278 s->enabled_capabilities[cap->value->capability] = cap->value->state;
282 /* shared migration helpers */
284 static void migrate_fd_cleanup(void *opaque)
286 MigrationState *s = opaque;
288 qemu_bh_delete(s->cleanup_bh);
289 s->cleanup_bh = NULL;
291 if (s->file) {
292 DPRINTF("closing file\n");
293 qemu_mutex_unlock_iothread();
294 qemu_thread_join(&s->thread);
295 qemu_mutex_lock_iothread();
297 qemu_fclose(s->file);
298 s->file = NULL;
301 assert(s->state != MIG_STATE_ACTIVE);
303 if (s->state != MIG_STATE_COMPLETED) {
304 qemu_savevm_state_cancel();
307 notifier_list_notify(&migration_state_notifiers, s);
310 static void migrate_set_state(MigrationState *s, int old_state, int new_state)
312 if (atomic_cmpxchg(&s->state, old_state, new_state) == new_state) {
313 trace_migrate_set_state(new_state);
317 void migrate_fd_error(MigrationState *s)
319 DPRINTF("setting error state\n");
320 assert(s->file == NULL);
321 s->state = MIG_STATE_ERROR;
322 trace_migrate_set_state(MIG_STATE_ERROR);
323 notifier_list_notify(&migration_state_notifiers, s);
326 static void migrate_fd_cancel(MigrationState *s)
328 DPRINTF("cancelling migration\n");
330 migrate_set_state(s, s->state, MIG_STATE_CANCELLED);
333 void add_migration_state_change_notifier(Notifier *notify)
335 notifier_list_add(&migration_state_notifiers, notify);
338 void remove_migration_state_change_notifier(Notifier *notify)
340 notifier_remove(notify);
343 bool migration_in_setup(MigrationState *s)
345 return s->state == MIG_STATE_SETUP;
348 bool migration_has_finished(MigrationState *s)
350 return s->state == MIG_STATE_COMPLETED;
353 bool migration_has_failed(MigrationState *s)
355 return (s->state == MIG_STATE_CANCELLED ||
356 s->state == MIG_STATE_ERROR);
359 static MigrationState *migrate_init(const MigrationParams *params)
361 MigrationState *s = migrate_get_current();
362 int64_t bandwidth_limit = s->bandwidth_limit;
363 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
364 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
366 memcpy(enabled_capabilities, s->enabled_capabilities,
367 sizeof(enabled_capabilities));
369 memset(s, 0, sizeof(*s));
370 s->params = *params;
371 memcpy(s->enabled_capabilities, enabled_capabilities,
372 sizeof(enabled_capabilities));
373 s->xbzrle_cache_size = xbzrle_cache_size;
375 s->bandwidth_limit = bandwidth_limit;
376 s->state = MIG_STATE_SETUP;
377 trace_migrate_set_state(MIG_STATE_SETUP);
379 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
380 return s;
383 static GSList *migration_blockers;
385 void migrate_add_blocker(Error *reason)
387 migration_blockers = g_slist_prepend(migration_blockers, reason);
390 void migrate_del_blocker(Error *reason)
392 migration_blockers = g_slist_remove(migration_blockers, reason);
395 void qmp_migrate(const char *uri, bool has_blk, bool blk,
396 bool has_inc, bool inc, bool has_detach, bool detach,
397 Error **errp)
399 Error *local_err = NULL;
400 MigrationState *s = migrate_get_current();
401 MigrationParams params;
402 const char *p;
404 params.blk = has_blk && blk;
405 params.shared = has_inc && inc;
407 if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP) {
408 error_set(errp, QERR_MIGRATION_ACTIVE);
409 return;
412 if (qemu_savevm_state_blocked(errp)) {
413 return;
416 if (migration_blockers) {
417 *errp = error_copy(migration_blockers->data);
418 return;
421 s = migrate_init(&params);
423 if (strstart(uri, "tcp:", &p)) {
424 tcp_start_outgoing_migration(s, p, &local_err);
425 #ifdef CONFIG_RDMA
426 } else if (strstart(uri, "x-rdma:", &p)) {
427 rdma_start_outgoing_migration(s, p, &local_err);
428 #endif
429 #if !defined(WIN32)
430 } else if (strstart(uri, "exec:", &p)) {
431 exec_start_outgoing_migration(s, p, &local_err);
432 } else if (strstart(uri, "unix:", &p)) {
433 unix_start_outgoing_migration(s, p, &local_err);
434 } else if (strstart(uri, "fd:", &p)) {
435 fd_start_outgoing_migration(s, p, &local_err);
436 #endif
437 } else {
438 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
439 return;
442 if (local_err) {
443 migrate_fd_error(s);
444 error_propagate(errp, local_err);
445 return;
449 void qmp_migrate_cancel(Error **errp)
451 migrate_fd_cancel(migrate_get_current());
454 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
456 MigrationState *s = migrate_get_current();
458 /* Check for truncation */
459 if (value != (size_t)value) {
460 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
461 "exceeding address space");
462 return;
465 s->xbzrle_cache_size = xbzrle_cache_resize(value);
468 int64_t qmp_query_migrate_cache_size(Error **errp)
470 return migrate_xbzrle_cache_size();
473 void qmp_migrate_set_speed(int64_t value, Error **errp)
475 MigrationState *s;
477 if (value < 0) {
478 value = 0;
480 if (value > SIZE_MAX) {
481 value = SIZE_MAX;
484 s = migrate_get_current();
485 s->bandwidth_limit = value;
486 if (s->file) {
487 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
491 void qmp_migrate_set_downtime(double value, Error **errp)
493 value *= 1e9;
494 value = MAX(0, MIN(UINT64_MAX, value));
495 max_downtime = (uint64_t)value;
498 bool migrate_rdma_pin_all(void)
500 MigrationState *s;
502 s = migrate_get_current();
504 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_RDMA_PIN_ALL];
507 bool migrate_auto_converge(void)
509 MigrationState *s;
511 s = migrate_get_current();
513 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
516 bool migrate_zero_blocks(void)
518 MigrationState *s;
520 s = migrate_get_current();
522 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
525 int migrate_use_xbzrle(void)
527 MigrationState *s;
529 s = migrate_get_current();
531 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
534 int64_t migrate_xbzrle_cache_size(void)
536 MigrationState *s;
538 s = migrate_get_current();
540 return s->xbzrle_cache_size;
543 /* migration thread support */
545 static void *migration_thread(void *opaque)
547 MigrationState *s = opaque;
548 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
549 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
550 int64_t initial_bytes = 0;
551 int64_t max_size = 0;
552 int64_t start_time = initial_time;
553 bool old_vm_running = false;
555 DPRINTF("beginning savevm\n");
556 qemu_savevm_state_begin(s->file, &s->params);
558 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
559 migrate_set_state(s, MIG_STATE_SETUP, MIG_STATE_ACTIVE);
561 DPRINTF("setup complete\n");
563 while (s->state == MIG_STATE_ACTIVE) {
564 int64_t current_time;
565 uint64_t pending_size;
567 if (!qemu_file_rate_limit(s->file)) {
568 DPRINTF("iterate\n");
569 pending_size = qemu_savevm_state_pending(s->file, max_size);
570 DPRINTF("pending size %lu max %lu\n", pending_size, max_size);
571 if (pending_size && pending_size >= max_size) {
572 qemu_savevm_state_iterate(s->file);
573 } else {
574 int ret;
576 DPRINTF("done iterating\n");
577 qemu_mutex_lock_iothread();
578 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
579 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
580 old_vm_running = runstate_is_running();
582 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
583 if (ret >= 0) {
584 qemu_file_set_rate_limit(s->file, INT_MAX);
585 qemu_savevm_state_complete(s->file);
587 qemu_mutex_unlock_iothread();
589 if (ret < 0) {
590 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR);
591 break;
594 if (!qemu_file_get_error(s->file)) {
595 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_COMPLETED);
596 break;
601 if (qemu_file_get_error(s->file)) {
602 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR);
603 break;
605 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
606 if (current_time >= initial_time + BUFFER_DELAY) {
607 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
608 uint64_t time_spent = current_time - initial_time;
609 double bandwidth = transferred_bytes / time_spent;
610 max_size = bandwidth * migrate_max_downtime() / 1000000;
612 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
613 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
615 DPRINTF("transferred %" PRIu64 " time_spent %" PRIu64
616 " bandwidth %g max_size %" PRId64 "\n",
617 transferred_bytes, time_spent, bandwidth, max_size);
618 /* if we haven't sent anything, we don't want to recalculate
619 10000 is a small enough number for our purposes */
620 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
621 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
624 qemu_file_reset_rate_limit(s->file);
625 initial_time = current_time;
626 initial_bytes = qemu_ftell(s->file);
628 if (qemu_file_rate_limit(s->file)) {
629 /* usleep expects microseconds */
630 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
634 qemu_mutex_lock_iothread();
635 if (s->state == MIG_STATE_COMPLETED) {
636 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
637 s->total_time = end_time - s->total_time;
638 s->downtime = end_time - start_time;
639 runstate_set(RUN_STATE_POSTMIGRATE);
640 } else {
641 if (old_vm_running) {
642 vm_start();
645 qemu_bh_schedule(s->cleanup_bh);
646 qemu_mutex_unlock_iothread();
648 return NULL;
651 void migrate_fd_connect(MigrationState *s)
653 s->state = MIG_STATE_SETUP;
654 trace_migrate_set_state(MIG_STATE_SETUP);
656 /* This is a best 1st approximation. ns to ms */
657 s->expected_downtime = max_downtime/1000000;
658 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
660 qemu_file_set_rate_limit(s->file,
661 s->bandwidth_limit / XFER_LIMIT_RATIO);
663 /* Notify before starting migration thread */
664 notifier_list_notify(&migration_state_notifiers, s);
666 qemu_thread_create(&s->thread, migration_thread, s,
667 QEMU_THREAD_JOINABLE);