seccomp: libseccomp version varying according to arch
[qemu/ar7.git] / migration / migration.c
blobbc424907f3c001f1509c8dcbe9adc3654146d2dc
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 MAX_THROTTLE (32 << 20) /* Migration speed throttling */
31 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
32 * data. */
33 #define BUFFER_DELAY 100
34 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
36 /* Migration XBZRLE default cache size */
37 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
39 static NotifierList migration_state_notifiers =
40 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
42 static bool deferred_incoming;
44 /* When we add fault tolerance, we could have several
45 migrations at once. For now we don't need to add
46 dynamic creation of migration */
48 MigrationState *migrate_get_current(void)
50 static MigrationState current_migration = {
51 .state = MIGRATION_STATUS_NONE,
52 .bandwidth_limit = MAX_THROTTLE,
53 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
54 .mbps = -1,
57 return &current_migration;
61 * Called on -incoming with a defer: uri.
62 * The migration can be started later after any parameters have been
63 * changed.
65 static void deferred_incoming_migration(Error **errp)
67 if (deferred_incoming) {
68 error_setg(errp, "Incoming migration already deferred");
70 deferred_incoming = true;
73 void qemu_start_incoming_migration(const char *uri, Error **errp)
75 const char *p;
77 if (!strcmp(uri, "defer")) {
78 deferred_incoming_migration(errp);
79 } else if (strstart(uri, "tcp:", &p)) {
80 tcp_start_incoming_migration(p, errp);
81 #ifdef CONFIG_RDMA
82 } else if (strstart(uri, "rdma:", &p)) {
83 rdma_start_incoming_migration(p, errp);
84 #endif
85 #if !defined(WIN32)
86 } else if (strstart(uri, "exec:", &p)) {
87 exec_start_incoming_migration(p, errp);
88 } else if (strstart(uri, "unix:", &p)) {
89 unix_start_incoming_migration(p, errp);
90 } else if (strstart(uri, "fd:", &p)) {
91 fd_start_incoming_migration(p, errp);
92 #endif
93 } else {
94 error_setg(errp, "unknown migration protocol: %s", uri);
98 static void process_incoming_migration_co(void *opaque)
100 QEMUFile *f = opaque;
101 Error *local_err = NULL;
102 int ret;
104 ret = qemu_loadvm_state(f);
105 qemu_fclose(f);
106 free_xbzrle_decoded_buf();
107 if (ret < 0) {
108 error_report("load of migration failed: %s", strerror(-ret));
109 exit(EXIT_FAILURE);
111 qemu_announce_self();
113 /* Make sure all file formats flush their mutable metadata */
114 bdrv_invalidate_cache_all(&local_err);
115 if (local_err) {
116 error_report_err(local_err);
117 exit(EXIT_FAILURE);
120 if (autostart) {
121 vm_start();
122 } else {
123 runstate_set(RUN_STATE_PAUSED);
127 void process_incoming_migration(QEMUFile *f)
129 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
130 int fd = qemu_get_fd(f);
132 assert(fd != -1);
133 qemu_set_nonblock(fd);
134 qemu_coroutine_enter(co, f);
137 /* amount of nanoseconds we are willing to wait for migration to be down.
138 * the choice of nanoseconds is because it is the maximum resolution that
139 * get_clock() can achieve. It is an internal measure. All user-visible
140 * units must be in seconds */
141 static uint64_t max_downtime = 300000000;
143 uint64_t migrate_max_downtime(void)
145 return max_downtime;
148 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
150 MigrationCapabilityStatusList *head = NULL;
151 MigrationCapabilityStatusList *caps;
152 MigrationState *s = migrate_get_current();
153 int i;
155 caps = NULL; /* silence compiler warning */
156 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
157 if (head == NULL) {
158 head = g_malloc0(sizeof(*caps));
159 caps = head;
160 } else {
161 caps->next = g_malloc0(sizeof(*caps));
162 caps = caps->next;
164 caps->value =
165 g_malloc(sizeof(*caps->value));
166 caps->value->capability = i;
167 caps->value->state = s->enabled_capabilities[i];
170 return head;
173 static void get_xbzrle_cache_stats(MigrationInfo *info)
175 if (migrate_use_xbzrle()) {
176 info->has_xbzrle_cache = true;
177 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
178 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
179 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
180 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
181 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
182 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
183 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
187 MigrationInfo *qmp_query_migrate(Error **errp)
189 MigrationInfo *info = g_malloc0(sizeof(*info));
190 MigrationState *s = migrate_get_current();
192 switch (s->state) {
193 case MIGRATION_STATUS_NONE:
194 /* no migration has happened ever */
195 break;
196 case MIGRATION_STATUS_SETUP:
197 info->has_status = true;
198 info->has_total_time = false;
199 break;
200 case MIGRATION_STATUS_ACTIVE:
201 case MIGRATION_STATUS_CANCELLING:
202 info->has_status = true;
203 info->has_total_time = true;
204 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
205 - s->total_time;
206 info->has_expected_downtime = true;
207 info->expected_downtime = s->expected_downtime;
208 info->has_setup_time = true;
209 info->setup_time = s->setup_time;
211 info->has_ram = true;
212 info->ram = g_malloc0(sizeof(*info->ram));
213 info->ram->transferred = ram_bytes_transferred();
214 info->ram->remaining = ram_bytes_remaining();
215 info->ram->total = ram_bytes_total();
216 info->ram->duplicate = dup_mig_pages_transferred();
217 info->ram->skipped = skipped_mig_pages_transferred();
218 info->ram->normal = norm_mig_pages_transferred();
219 info->ram->normal_bytes = norm_mig_bytes_transferred();
220 info->ram->dirty_pages_rate = s->dirty_pages_rate;
221 info->ram->mbps = s->mbps;
222 info->ram->dirty_sync_count = s->dirty_sync_count;
224 if (blk_mig_active()) {
225 info->has_disk = true;
226 info->disk = g_malloc0(sizeof(*info->disk));
227 info->disk->transferred = blk_mig_bytes_transferred();
228 info->disk->remaining = blk_mig_bytes_remaining();
229 info->disk->total = blk_mig_bytes_total();
232 get_xbzrle_cache_stats(info);
233 break;
234 case MIGRATION_STATUS_COMPLETED:
235 get_xbzrle_cache_stats(info);
237 info->has_status = true;
238 info->has_total_time = true;
239 info->total_time = s->total_time;
240 info->has_downtime = true;
241 info->downtime = s->downtime;
242 info->has_setup_time = true;
243 info->setup_time = s->setup_time;
245 info->has_ram = true;
246 info->ram = g_malloc0(sizeof(*info->ram));
247 info->ram->transferred = ram_bytes_transferred();
248 info->ram->remaining = 0;
249 info->ram->total = ram_bytes_total();
250 info->ram->duplicate = dup_mig_pages_transferred();
251 info->ram->skipped = skipped_mig_pages_transferred();
252 info->ram->normal = norm_mig_pages_transferred();
253 info->ram->normal_bytes = norm_mig_bytes_transferred();
254 info->ram->mbps = s->mbps;
255 info->ram->dirty_sync_count = s->dirty_sync_count;
256 break;
257 case MIGRATION_STATUS_FAILED:
258 info->has_status = true;
259 break;
260 case MIGRATION_STATUS_CANCELLED:
261 info->has_status = true;
262 break;
264 info->status = s->state;
266 return info;
269 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
270 Error **errp)
272 MigrationState *s = migrate_get_current();
273 MigrationCapabilityStatusList *cap;
275 if (s->state == MIGRATION_STATUS_ACTIVE ||
276 s->state == MIGRATION_STATUS_SETUP) {
277 error_set(errp, QERR_MIGRATION_ACTIVE);
278 return;
281 for (cap = params; cap; cap = cap->next) {
282 s->enabled_capabilities[cap->value->capability] = cap->value->state;
286 /* shared migration helpers */
288 static void migrate_set_state(MigrationState *s, int old_state, int new_state)
290 if (atomic_cmpxchg(&s->state, old_state, new_state) == new_state) {
291 trace_migrate_set_state(new_state);
295 static void migrate_fd_cleanup(void *opaque)
297 MigrationState *s = opaque;
299 qemu_bh_delete(s->cleanup_bh);
300 s->cleanup_bh = NULL;
302 if (s->file) {
303 trace_migrate_fd_cleanup();
304 qemu_mutex_unlock_iothread();
305 qemu_thread_join(&s->thread);
306 qemu_mutex_lock_iothread();
308 qemu_fclose(s->file);
309 s->file = NULL;
312 assert(s->state != MIGRATION_STATUS_ACTIVE);
314 if (s->state != MIGRATION_STATUS_COMPLETED) {
315 qemu_savevm_state_cancel();
316 if (s->state == MIGRATION_STATUS_CANCELLING) {
317 migrate_set_state(s, MIGRATION_STATUS_CANCELLING,
318 MIGRATION_STATUS_CANCELLED);
322 notifier_list_notify(&migration_state_notifiers, s);
325 void migrate_fd_error(MigrationState *s)
327 trace_migrate_fd_error();
328 assert(s->file == NULL);
329 s->state = MIGRATION_STATUS_FAILED;
330 trace_migrate_set_state(MIGRATION_STATUS_FAILED);
331 notifier_list_notify(&migration_state_notifiers, s);
334 static void migrate_fd_cancel(MigrationState *s)
336 int old_state ;
337 QEMUFile *f = migrate_get_current()->file;
338 trace_migrate_fd_cancel();
340 do {
341 old_state = s->state;
342 if (old_state != MIGRATION_STATUS_SETUP &&
343 old_state != MIGRATION_STATUS_ACTIVE) {
344 break;
346 migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
347 } while (s->state != MIGRATION_STATUS_CANCELLING);
350 * If we're unlucky the migration code might be stuck somewhere in a
351 * send/write while the network has failed and is waiting to timeout;
352 * if we've got shutdown(2) available then we can force it to quit.
353 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
354 * called in a bh, so there is no race against this cancel.
356 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
357 qemu_file_shutdown(f);
361 void add_migration_state_change_notifier(Notifier *notify)
363 notifier_list_add(&migration_state_notifiers, notify);
366 void remove_migration_state_change_notifier(Notifier *notify)
368 notifier_remove(notify);
371 bool migration_in_setup(MigrationState *s)
373 return s->state == MIGRATION_STATUS_SETUP;
376 bool migration_has_finished(MigrationState *s)
378 return s->state == MIGRATION_STATUS_COMPLETED;
381 bool migration_has_failed(MigrationState *s)
383 return (s->state == MIGRATION_STATUS_CANCELLED ||
384 s->state == MIGRATION_STATUS_FAILED);
387 static MigrationState *migrate_init(const MigrationParams *params)
389 MigrationState *s = migrate_get_current();
390 int64_t bandwidth_limit = s->bandwidth_limit;
391 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
392 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
394 memcpy(enabled_capabilities, s->enabled_capabilities,
395 sizeof(enabled_capabilities));
397 memset(s, 0, sizeof(*s));
398 s->params = *params;
399 memcpy(s->enabled_capabilities, enabled_capabilities,
400 sizeof(enabled_capabilities));
401 s->xbzrle_cache_size = xbzrle_cache_size;
403 s->bandwidth_limit = bandwidth_limit;
404 s->state = MIGRATION_STATUS_SETUP;
405 trace_migrate_set_state(MIGRATION_STATUS_SETUP);
407 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
408 return s;
411 static GSList *migration_blockers;
413 void migrate_add_blocker(Error *reason)
415 migration_blockers = g_slist_prepend(migration_blockers, reason);
418 void migrate_del_blocker(Error *reason)
420 migration_blockers = g_slist_remove(migration_blockers, reason);
423 void qmp_migrate_incoming(const char *uri, Error **errp)
425 Error *local_err = NULL;
426 static bool once = true;
428 if (!deferred_incoming) {
429 error_setg(errp, "For use with '-incoming defer'");
430 return;
432 if (!once) {
433 error_setg(errp, "The incoming migration has already been started");
436 qemu_start_incoming_migration(uri, &local_err);
438 if (local_err) {
439 error_propagate(errp, local_err);
440 return;
443 once = false;
446 void qmp_migrate(const char *uri, bool has_blk, bool blk,
447 bool has_inc, bool inc, bool has_detach, bool detach,
448 Error **errp)
450 Error *local_err = NULL;
451 MigrationState *s = migrate_get_current();
452 MigrationParams params;
453 const char *p;
455 params.blk = has_blk && blk;
456 params.shared = has_inc && inc;
458 if (s->state == MIGRATION_STATUS_ACTIVE ||
459 s->state == MIGRATION_STATUS_SETUP ||
460 s->state == MIGRATION_STATUS_CANCELLING) {
461 error_set(errp, QERR_MIGRATION_ACTIVE);
462 return;
465 if (runstate_check(RUN_STATE_INMIGRATE)) {
466 error_setg(errp, "Guest is waiting for an incoming migration");
467 return;
470 if (qemu_savevm_state_blocked(errp)) {
471 return;
474 if (migration_blockers) {
475 *errp = error_copy(migration_blockers->data);
476 return;
479 s = migrate_init(&params);
481 if (strstart(uri, "tcp:", &p)) {
482 tcp_start_outgoing_migration(s, p, &local_err);
483 #ifdef CONFIG_RDMA
484 } else if (strstart(uri, "rdma:", &p)) {
485 rdma_start_outgoing_migration(s, p, &local_err);
486 #endif
487 #if !defined(WIN32)
488 } else if (strstart(uri, "exec:", &p)) {
489 exec_start_outgoing_migration(s, p, &local_err);
490 } else if (strstart(uri, "unix:", &p)) {
491 unix_start_outgoing_migration(s, p, &local_err);
492 } else if (strstart(uri, "fd:", &p)) {
493 fd_start_outgoing_migration(s, p, &local_err);
494 #endif
495 } else {
496 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
497 s->state = MIGRATION_STATUS_FAILED;
498 return;
501 if (local_err) {
502 migrate_fd_error(s);
503 error_propagate(errp, local_err);
504 return;
508 void qmp_migrate_cancel(Error **errp)
510 migrate_fd_cancel(migrate_get_current());
513 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
515 MigrationState *s = migrate_get_current();
516 int64_t new_size;
518 /* Check for truncation */
519 if (value != (size_t)value) {
520 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
521 "exceeding address space");
522 return;
525 /* Cache should not be larger than guest ram size */
526 if (value > ram_bytes_total()) {
527 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
528 "exceeds guest ram size ");
529 return;
532 new_size = xbzrle_cache_resize(value);
533 if (new_size < 0) {
534 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
535 "is smaller than page size");
536 return;
539 s->xbzrle_cache_size = new_size;
542 int64_t qmp_query_migrate_cache_size(Error **errp)
544 return migrate_xbzrle_cache_size();
547 void qmp_migrate_set_speed(int64_t value, Error **errp)
549 MigrationState *s;
551 if (value < 0) {
552 value = 0;
554 if (value > SIZE_MAX) {
555 value = SIZE_MAX;
558 s = migrate_get_current();
559 s->bandwidth_limit = value;
560 if (s->file) {
561 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
565 void qmp_migrate_set_downtime(double value, Error **errp)
567 value *= 1e9;
568 value = MAX(0, MIN(UINT64_MAX, value));
569 max_downtime = (uint64_t)value;
572 bool migrate_auto_converge(void)
574 MigrationState *s;
576 s = migrate_get_current();
578 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
581 bool migrate_zero_blocks(void)
583 MigrationState *s;
585 s = migrate_get_current();
587 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
590 int migrate_use_xbzrle(void)
592 MigrationState *s;
594 s = migrate_get_current();
596 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
599 int64_t migrate_xbzrle_cache_size(void)
601 MigrationState *s;
603 s = migrate_get_current();
605 return s->xbzrle_cache_size;
608 /* migration thread support */
610 static void *migration_thread(void *opaque)
612 MigrationState *s = opaque;
613 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
614 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
615 int64_t initial_bytes = 0;
616 int64_t max_size = 0;
617 int64_t start_time = initial_time;
618 bool old_vm_running = false;
620 qemu_savevm_state_begin(s->file, &s->params);
622 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
623 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
625 while (s->state == MIGRATION_STATUS_ACTIVE) {
626 int64_t current_time;
627 uint64_t pending_size;
629 if (!qemu_file_rate_limit(s->file)) {
630 pending_size = qemu_savevm_state_pending(s->file, max_size);
631 trace_migrate_pending(pending_size, max_size);
632 if (pending_size && pending_size >= max_size) {
633 qemu_savevm_state_iterate(s->file);
634 } else {
635 int ret;
637 qemu_mutex_lock_iothread();
638 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
639 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
640 old_vm_running = runstate_is_running();
642 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
643 if (ret >= 0) {
644 qemu_file_set_rate_limit(s->file, INT64_MAX);
645 qemu_savevm_state_complete(s->file);
647 qemu_mutex_unlock_iothread();
649 if (ret < 0) {
650 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
651 MIGRATION_STATUS_FAILED);
652 break;
655 if (!qemu_file_get_error(s->file)) {
656 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
657 MIGRATION_STATUS_COMPLETED);
658 break;
663 if (qemu_file_get_error(s->file)) {
664 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
665 MIGRATION_STATUS_FAILED);
666 break;
668 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
669 if (current_time >= initial_time + BUFFER_DELAY) {
670 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
671 uint64_t time_spent = current_time - initial_time;
672 double bandwidth = transferred_bytes / time_spent;
673 max_size = bandwidth * migrate_max_downtime() / 1000000;
675 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
676 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
678 trace_migrate_transferred(transferred_bytes, time_spent,
679 bandwidth, max_size);
680 /* if we haven't sent anything, we don't want to recalculate
681 10000 is a small enough number for our purposes */
682 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
683 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
686 qemu_file_reset_rate_limit(s->file);
687 initial_time = current_time;
688 initial_bytes = qemu_ftell(s->file);
690 if (qemu_file_rate_limit(s->file)) {
691 /* usleep expects microseconds */
692 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
696 qemu_mutex_lock_iothread();
697 if (s->state == MIGRATION_STATUS_COMPLETED) {
698 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
699 uint64_t transferred_bytes = qemu_ftell(s->file);
700 s->total_time = end_time - s->total_time;
701 s->downtime = end_time - start_time;
702 if (s->total_time) {
703 s->mbps = (((double) transferred_bytes * 8.0) /
704 ((double) s->total_time)) / 1000;
706 runstate_set(RUN_STATE_POSTMIGRATE);
707 } else {
708 if (old_vm_running) {
709 vm_start();
712 qemu_bh_schedule(s->cleanup_bh);
713 qemu_mutex_unlock_iothread();
715 return NULL;
718 void migrate_fd_connect(MigrationState *s)
720 s->state = MIGRATION_STATUS_SETUP;
721 trace_migrate_set_state(MIGRATION_STATUS_SETUP);
723 /* This is a best 1st approximation. ns to ms */
724 s->expected_downtime = max_downtime/1000000;
725 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
727 qemu_file_set_rate_limit(s->file,
728 s->bandwidth_limit / XFER_LIMIT_RATIO);
730 /* Notify before starting migration thread */
731 notifier_list_notify(&migration_state_notifiers, s);
733 qemu_thread_create(&s->thread, "migration", migration_thread, s,
734 QEMU_THREAD_JOINABLE);