target-microblaze: Drop unused cpu_mb_close() prototype
[qemu/opensuse.git] / migration.c
blobc69e864fcd77b22441322aa9d7b18f7df6fd5462
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"
27 //#define DEBUG_MIGRATION
29 #ifdef DEBUG_MIGRATION
30 #define DPRINTF(fmt, ...) \
31 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
32 #else
33 #define DPRINTF(fmt, ...) \
34 do { } while (0)
35 #endif
37 enum {
38 MIG_STATE_ERROR,
39 MIG_STATE_SETUP,
40 MIG_STATE_CANCELLED,
41 MIG_STATE_ACTIVE,
42 MIG_STATE_COMPLETED,
45 #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
47 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
48 * data. */
49 #define BUFFER_DELAY 100
50 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
52 /* Migration XBZRLE default cache size */
53 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
55 static NotifierList migration_state_notifiers =
56 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
58 /* When we add fault tolerance, we could have several
59 migrations at once. For now we don't need to add
60 dynamic creation of migration */
62 MigrationState *migrate_get_current(void)
64 static MigrationState current_migration = {
65 .state = MIG_STATE_SETUP,
66 .bandwidth_limit = MAX_THROTTLE,
67 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
70 return &current_migration;
73 void qemu_start_incoming_migration(const char *uri, Error **errp)
75 const char *p;
77 if (strstart(uri, "tcp:", &p))
78 tcp_start_incoming_migration(p, errp);
79 #if !defined(WIN32)
80 else if (strstart(uri, "exec:", &p))
81 exec_start_incoming_migration(p, errp);
82 else if (strstart(uri, "unix:", &p))
83 unix_start_incoming_migration(p, errp);
84 else if (strstart(uri, "fd:", &p))
85 fd_start_incoming_migration(p, errp);
86 #endif
87 else {
88 error_setg(errp, "unknown migration protocol: %s\n", uri);
92 static void process_incoming_migration_co(void *opaque)
94 QEMUFile *f = opaque;
95 int ret;
97 ret = qemu_loadvm_state(f);
98 qemu_set_fd_handler(qemu_get_fd(f), NULL, NULL, NULL);
99 qemu_fclose(f);
100 if (ret < 0) {
101 fprintf(stderr, "load of migration failed\n");
102 exit(0);
104 qemu_announce_self();
105 DPRINTF("successfully loaded vm state\n");
107 bdrv_clear_incoming_migration_all();
108 /* Make sure all file formats flush their mutable metadata */
109 bdrv_invalidate_cache_all();
111 if (autostart) {
112 vm_start();
113 } else {
114 runstate_set(RUN_STATE_PAUSED);
118 static void enter_migration_coroutine(void *opaque)
120 Coroutine *co = opaque;
121 qemu_coroutine_enter(co, NULL);
124 void process_incoming_migration(QEMUFile *f)
126 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
127 int fd = qemu_get_fd(f);
129 assert(fd != -1);
130 socket_set_nonblock(fd);
131 qemu_set_fd_handler(fd, enter_migration_coroutine, NULL, co);
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_SETUP:
190 /* no migration has happened ever */
191 break;
192 case MIG_STATE_ACTIVE:
193 info->has_status = true;
194 info->status = g_strdup("active");
195 info->has_total_time = true;
196 info->total_time = qemu_get_clock_ms(rt_clock)
197 - s->total_time;
198 info->has_expected_downtime = true;
199 info->expected_downtime = s->expected_downtime;
201 info->has_ram = true;
202 info->ram = g_malloc0(sizeof(*info->ram));
203 info->ram->transferred = ram_bytes_transferred();
204 info->ram->remaining = ram_bytes_remaining();
205 info->ram->total = ram_bytes_total();
206 info->ram->duplicate = dup_mig_pages_transferred();
207 info->ram->normal = norm_mig_pages_transferred();
208 info->ram->normal_bytes = norm_mig_bytes_transferred();
209 info->ram->dirty_pages_rate = s->dirty_pages_rate;
212 if (blk_mig_active()) {
213 info->has_disk = true;
214 info->disk = g_malloc0(sizeof(*info->disk));
215 info->disk->transferred = blk_mig_bytes_transferred();
216 info->disk->remaining = blk_mig_bytes_remaining();
217 info->disk->total = blk_mig_bytes_total();
220 get_xbzrle_cache_stats(info);
221 break;
222 case MIG_STATE_COMPLETED:
223 get_xbzrle_cache_stats(info);
225 info->has_status = true;
226 info->status = g_strdup("completed");
227 info->total_time = s->total_time;
228 info->has_downtime = true;
229 info->downtime = s->downtime;
231 info->has_ram = true;
232 info->ram = g_malloc0(sizeof(*info->ram));
233 info->ram->transferred = ram_bytes_transferred();
234 info->ram->remaining = 0;
235 info->ram->total = ram_bytes_total();
236 info->ram->duplicate = dup_mig_pages_transferred();
237 info->ram->normal = norm_mig_pages_transferred();
238 info->ram->normal_bytes = norm_mig_bytes_transferred();
239 break;
240 case MIG_STATE_ERROR:
241 info->has_status = true;
242 info->status = g_strdup("failed");
243 break;
244 case MIG_STATE_CANCELLED:
245 info->has_status = true;
246 info->status = g_strdup("cancelled");
247 break;
250 return info;
253 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
254 Error **errp)
256 MigrationState *s = migrate_get_current();
257 MigrationCapabilityStatusList *cap;
259 if (s->state == MIG_STATE_ACTIVE) {
260 error_set(errp, QERR_MIGRATION_ACTIVE);
261 return;
264 for (cap = params; cap; cap = cap->next) {
265 s->enabled_capabilities[cap->value->capability] = cap->value->state;
269 /* shared migration helpers */
271 static int migrate_fd_cleanup(MigrationState *s)
273 int ret = 0;
275 if (s->file) {
276 DPRINTF("closing file\n");
277 ret = qemu_fclose(s->file);
278 s->file = NULL;
281 assert(s->fd == -1);
282 return ret;
285 void migrate_fd_error(MigrationState *s)
287 DPRINTF("setting error state\n");
288 s->state = MIG_STATE_ERROR;
289 notifier_list_notify(&migration_state_notifiers, s);
290 migrate_fd_cleanup(s);
293 static void migrate_fd_completed(MigrationState *s)
295 DPRINTF("setting completed state\n");
296 if (migrate_fd_cleanup(s) < 0) {
297 s->state = MIG_STATE_ERROR;
298 } else {
299 s->state = MIG_STATE_COMPLETED;
300 runstate_set(RUN_STATE_POSTMIGRATE);
302 notifier_list_notify(&migration_state_notifiers, s);
305 ssize_t migrate_fd_put_buffer(MigrationState *s, const void *data,
306 size_t size)
308 ssize_t ret;
310 if (s->state != MIG_STATE_ACTIVE) {
311 return -EIO;
314 do {
315 ret = s->write(s, data, size);
316 } while (ret == -1 && ((s->get_error(s)) == EINTR));
318 if (ret == -1)
319 ret = -(s->get_error(s));
321 return ret;
324 static void migrate_fd_cancel(MigrationState *s)
326 if (s->state != MIG_STATE_ACTIVE)
327 return;
329 DPRINTF("cancelling migration\n");
331 s->state = MIG_STATE_CANCELLED;
332 notifier_list_notify(&migration_state_notifiers, s);
333 qemu_savevm_state_cancel(s->file);
335 migrate_fd_cleanup(s);
338 int migrate_fd_close(MigrationState *s)
340 int rc = 0;
341 if (s->fd != -1) {
342 rc = s->close(s);
343 s->fd = -1;
345 return rc;
348 void add_migration_state_change_notifier(Notifier *notify)
350 notifier_list_add(&migration_state_notifiers, notify);
353 void remove_migration_state_change_notifier(Notifier *notify)
355 notifier_remove(notify);
358 bool migration_is_active(MigrationState *s)
360 return s->state == MIG_STATE_ACTIVE;
363 bool migration_has_finished(MigrationState *s)
365 return s->state == MIG_STATE_COMPLETED;
368 bool migration_has_failed(MigrationState *s)
370 return (s->state == MIG_STATE_CANCELLED ||
371 s->state == MIG_STATE_ERROR);
374 static MigrationState *migrate_init(const MigrationParams *params)
376 MigrationState *s = migrate_get_current();
377 int64_t bandwidth_limit = s->bandwidth_limit;
378 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
379 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
381 memcpy(enabled_capabilities, s->enabled_capabilities,
382 sizeof(enabled_capabilities));
384 memset(s, 0, sizeof(*s));
385 s->bandwidth_limit = bandwidth_limit;
386 s->params = *params;
387 memcpy(s->enabled_capabilities, enabled_capabilities,
388 sizeof(enabled_capabilities));
389 s->xbzrle_cache_size = xbzrle_cache_size;
391 s->bandwidth_limit = bandwidth_limit;
392 s->state = MIG_STATE_SETUP;
393 s->total_time = qemu_get_clock_ms(rt_clock);
395 return s;
398 static GSList *migration_blockers;
400 void migrate_add_blocker(Error *reason)
402 migration_blockers = g_slist_prepend(migration_blockers, reason);
405 void migrate_del_blocker(Error *reason)
407 migration_blockers = g_slist_remove(migration_blockers, reason);
410 void qmp_migrate(const char *uri, bool has_blk, bool blk,
411 bool has_inc, bool inc, bool has_detach, bool detach,
412 Error **errp)
414 Error *local_err = NULL;
415 MigrationState *s = migrate_get_current();
416 MigrationParams params;
417 const char *p;
419 params.blk = blk;
420 params.shared = inc;
422 if (s->state == MIG_STATE_ACTIVE) {
423 error_set(errp, QERR_MIGRATION_ACTIVE);
424 return;
427 if (qemu_savevm_state_blocked(errp)) {
428 return;
431 if (migration_blockers) {
432 *errp = error_copy(migration_blockers->data);
433 return;
436 s = migrate_init(&params);
438 if (strstart(uri, "tcp:", &p)) {
439 tcp_start_outgoing_migration(s, p, &local_err);
440 #if !defined(WIN32)
441 } else if (strstart(uri, "exec:", &p)) {
442 exec_start_outgoing_migration(s, p, &local_err);
443 } else if (strstart(uri, "unix:", &p)) {
444 unix_start_outgoing_migration(s, p, &local_err);
445 } else if (strstart(uri, "fd:", &p)) {
446 fd_start_outgoing_migration(s, p, &local_err);
447 #endif
448 } else {
449 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
450 return;
453 if (local_err) {
454 migrate_fd_error(s);
455 error_propagate(errp, local_err);
456 return;
460 void qmp_migrate_cancel(Error **errp)
462 migrate_fd_cancel(migrate_get_current());
465 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
467 MigrationState *s = migrate_get_current();
469 /* Check for truncation */
470 if (value != (size_t)value) {
471 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
472 "exceeding address space");
473 return;
476 s->xbzrle_cache_size = xbzrle_cache_resize(value);
479 int64_t qmp_query_migrate_cache_size(Error **errp)
481 return migrate_xbzrle_cache_size();
484 void qmp_migrate_set_speed(int64_t value, Error **errp)
486 MigrationState *s;
488 if (value < 0) {
489 value = 0;
492 s = migrate_get_current();
493 s->bandwidth_limit = value;
494 qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
497 void qmp_migrate_set_downtime(double value, Error **errp)
499 value *= 1e9;
500 value = MAX(0, MIN(UINT64_MAX, value));
501 max_downtime = (uint64_t)value;
504 int migrate_use_xbzrle(void)
506 MigrationState *s;
508 s = migrate_get_current();
510 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
513 int64_t migrate_xbzrle_cache_size(void)
515 MigrationState *s;
517 s = migrate_get_current();
519 return s->xbzrle_cache_size;
522 /* migration thread support */
525 static ssize_t buffered_flush(MigrationState *s)
527 size_t offset = 0;
528 ssize_t ret = 0;
530 DPRINTF("flushing %zu byte(s) of data\n", s->buffer_size);
532 while (s->bytes_xfer < s->xfer_limit && offset < s->buffer_size) {
533 size_t to_send = MIN(s->buffer_size - offset, s->xfer_limit - s->bytes_xfer);
534 ret = migrate_fd_put_buffer(s, s->buffer + offset, to_send);
535 if (ret <= 0) {
536 DPRINTF("error flushing data, %zd\n", ret);
537 break;
538 } else {
539 DPRINTF("flushed %zd byte(s)\n", ret);
540 offset += ret;
541 s->bytes_xfer += ret;
545 DPRINTF("flushed %zu of %zu byte(s)\n", offset, s->buffer_size);
546 memmove(s->buffer, s->buffer + offset, s->buffer_size - offset);
547 s->buffer_size -= offset;
549 if (ret < 0) {
550 return ret;
552 return offset;
555 static int buffered_put_buffer(void *opaque, const uint8_t *buf,
556 int64_t pos, int size)
558 MigrationState *s = opaque;
559 ssize_t error;
561 DPRINTF("putting %d bytes at %" PRId64 "\n", size, pos);
563 error = qemu_file_get_error(s->file);
564 if (error) {
565 DPRINTF("flush when error, bailing: %s\n", strerror(-error));
566 return error;
569 if (size <= 0) {
570 return size;
573 if (size > (s->buffer_capacity - s->buffer_size)) {
574 DPRINTF("increasing buffer capacity from %zu by %zu\n",
575 s->buffer_capacity, size + 1024);
577 s->buffer_capacity += size + 1024;
579 s->buffer = g_realloc(s->buffer, s->buffer_capacity);
582 memcpy(s->buffer + s->buffer_size, buf, size);
583 s->buffer_size += size;
585 return size;
588 static int buffered_close(void *opaque)
590 MigrationState *s = opaque;
591 ssize_t ret = 0;
592 int ret2;
594 DPRINTF("closing\n");
596 s->xfer_limit = INT_MAX;
597 while (!qemu_file_get_error(s->file) && s->buffer_size) {
598 ret = buffered_flush(s);
599 if (ret < 0) {
600 break;
604 ret2 = migrate_fd_close(s);
605 if (ret >= 0) {
606 ret = ret2;
608 ret = migrate_fd_close(s);
609 s->complete = true;
610 return ret;
613 static int buffered_get_fd(void *opaque)
615 MigrationState *s = opaque;
617 return s->fd;
621 * The meaning of the return values is:
622 * 0: We can continue sending
623 * 1: Time to stop
624 * negative: There has been an error
626 static int buffered_rate_limit(void *opaque)
628 MigrationState *s = opaque;
629 int ret;
631 ret = qemu_file_get_error(s->file);
632 if (ret) {
633 return ret;
636 if (s->bytes_xfer > s->xfer_limit) {
637 return 1;
640 return 0;
643 static int64_t buffered_set_rate_limit(void *opaque, int64_t new_rate)
645 MigrationState *s = opaque;
646 if (qemu_file_get_error(s->file)) {
647 goto out;
649 if (new_rate > SIZE_MAX) {
650 new_rate = SIZE_MAX;
653 s->xfer_limit = new_rate / 10;
655 out:
656 return s->xfer_limit;
659 static int64_t buffered_get_rate_limit(void *opaque)
661 MigrationState *s = opaque;
663 return s->xfer_limit;
666 static bool migrate_fd_put_ready(MigrationState *s, uint64_t max_size)
668 int ret;
669 uint64_t pending_size;
670 bool last_round = false;
672 qemu_mutex_lock_iothread();
673 if (s->state != MIG_STATE_ACTIVE) {
674 DPRINTF("put_ready returning because of non-active state\n");
675 qemu_mutex_unlock_iothread();
676 return false;
678 if (s->first_time) {
679 s->first_time = false;
680 DPRINTF("beginning savevm\n");
681 ret = qemu_savevm_state_begin(s->file, &s->params);
682 if (ret < 0) {
683 DPRINTF("failed, %d\n", ret);
684 migrate_fd_error(s);
685 qemu_mutex_unlock_iothread();
686 return false;
690 DPRINTF("iterate\n");
691 pending_size = qemu_savevm_state_pending(s->file, max_size);
692 DPRINTF("pending size %lu max %lu\n", pending_size, max_size);
693 if (pending_size >= max_size) {
694 ret = qemu_savevm_state_iterate(s->file);
695 if (ret < 0) {
696 migrate_fd_error(s);
698 } else {
699 int old_vm_running = runstate_is_running();
700 int64_t start_time, end_time;
702 DPRINTF("done iterating\n");
703 start_time = qemu_get_clock_ms(rt_clock);
704 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
705 if (old_vm_running) {
706 vm_stop(RUN_STATE_FINISH_MIGRATE);
707 } else {
708 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
711 if (qemu_savevm_state_complete(s->file) < 0) {
712 migrate_fd_error(s);
713 } else {
714 migrate_fd_completed(s);
716 end_time = qemu_get_clock_ms(rt_clock);
717 s->total_time = end_time - s->total_time;
718 s->downtime = end_time - start_time;
719 if (s->state != MIG_STATE_COMPLETED) {
720 if (old_vm_running) {
721 vm_start();
724 last_round = true;
726 qemu_mutex_unlock_iothread();
728 return last_round;
731 static void *buffered_file_thread(void *opaque)
733 MigrationState *s = opaque;
734 int64_t initial_time = qemu_get_clock_ms(rt_clock);
735 int64_t max_size = 0;
736 bool last_round = false;
738 while (true) {
739 int64_t current_time = qemu_get_clock_ms(rt_clock);
741 if (s->complete) {
742 break;
744 if (current_time >= initial_time + BUFFER_DELAY) {
745 uint64_t transferred_bytes = s->bytes_xfer;
746 uint64_t time_spent = current_time - initial_time;
747 double bandwidth = transferred_bytes / time_spent;
748 max_size = bandwidth * migrate_max_downtime() / 1000000;
750 DPRINTF("transferred %" PRIu64 " time_spent %" PRIu64
751 " bandwidth %g max_size %" PRId64 "\n",
752 transferred_bytes, time_spent, bandwidth, max_size);
754 s->bytes_xfer = 0;
755 initial_time = current_time;
757 if (!last_round && (s->bytes_xfer >= s->xfer_limit)) {
758 /* usleep expects microseconds */
759 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
761 if (buffered_flush(s) < 0) {
762 break;
765 DPRINTF("file is ready\n");
766 if (s->bytes_xfer < s->xfer_limit) {
767 DPRINTF("notifying client\n");
768 last_round = migrate_fd_put_ready(s, max_size);
772 g_free(s->buffer);
773 return NULL;
776 static const QEMUFileOps buffered_file_ops = {
777 .get_fd = buffered_get_fd,
778 .put_buffer = buffered_put_buffer,
779 .close = buffered_close,
780 .rate_limit = buffered_rate_limit,
781 .get_rate_limit = buffered_get_rate_limit,
782 .set_rate_limit = buffered_set_rate_limit,
785 void migrate_fd_connect(MigrationState *s)
787 s->state = MIG_STATE_ACTIVE;
788 s->bytes_xfer = 0;
789 s->buffer = NULL;
790 s->buffer_size = 0;
791 s->buffer_capacity = 0;
793 s->first_time = true;
795 s->xfer_limit = s->bandwidth_limit / XFER_LIMIT_RATIO;
796 s->complete = false;
798 s->file = qemu_fopen_ops(s, &buffered_file_ops);
800 qemu_thread_create(&s->thread, buffered_file_thread, s,
801 QEMU_THREAD_DETACHED);
802 notifier_list_notify(&migration_state_notifiers, s);