aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU live migration |
| 3 | * |
| 4 | * Copyright IBM, Corp. 2008 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Anthony Liguori <aliguori@us.ibm.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 10 | * the COPYING file in the top-level directory. |
| 11 | * |
Paolo Bonzini | 6b620ca | 2012-01-13 17:44:23 +0100 | [diff] [blame] | 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. |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include "qemu-common.h" |
Alex Bligh | 6a1751b | 2013-08-21 16:02:47 +0100 | [diff] [blame] | 17 | #include "qemu/main-loop.h" |
Paolo Bonzini | caf71f8 | 2012-12-17 18:19:50 +0100 | [diff] [blame] | 18 | #include "migration/migration.h" |
Paolo Bonzini | 83c9089 | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 19 | #include "monitor/monitor.h" |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 20 | #include "migration/qemu-file.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 21 | #include "sysemu/sysemu.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 22 | #include "block/block.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 23 | #include "qemu/sockets.h" |
Paolo Bonzini | caf71f8 | 2012-12-17 18:19:50 +0100 | [diff] [blame] | 24 | #include "migration/block.h" |
Juan Quintela | 766bd17 | 2012-07-23 05:45:29 +0200 | [diff] [blame] | 25 | #include "qemu/thread.h" |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 26 | #include "qmp-commands.h" |
Kazuya Saito | c09e5bb | 2013-02-22 17:36:19 +0100 | [diff] [blame] | 27 | #include "trace.h" |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 28 | |
| 29 | //#define DEBUG_MIGRATION |
| 30 | |
| 31 | #ifdef DEBUG_MIGRATION |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 32 | #define DPRINTF(fmt, ...) \ |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 33 | do { printf("migration: " fmt, ## __VA_ARGS__); } while (0) |
| 34 | #else |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 35 | #define DPRINTF(fmt, ...) \ |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 36 | do { } while (0) |
| 37 | #endif |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 38 | |
Juan Quintela | 7dc688e | 2011-02-23 00:48:46 +0100 | [diff] [blame] | 39 | enum { |
Michael R. Hines | 29ae8a4 | 2013-07-22 10:01:57 -0400 | [diff] [blame] | 40 | MIG_STATE_ERROR = -1, |
| 41 | MIG_STATE_NONE, |
Juan Quintela | 7dc688e | 2011-02-23 00:48:46 +0100 | [diff] [blame] | 42 | MIG_STATE_SETUP, |
Zhanghaoyu (A) | 51cf4c1 | 2013-11-07 11:01:15 +0000 | [diff] [blame] | 43 | MIG_STATE_CANCELLING, |
Juan Quintela | 7dc688e | 2011-02-23 00:48:46 +0100 | [diff] [blame] | 44 | MIG_STATE_CANCELLED, |
| 45 | MIG_STATE_ACTIVE, |
| 46 | MIG_STATE_COMPLETED, |
| 47 | }; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 48 | |
Juan Quintela | d0ae46c | 2011-02-23 00:33:19 +0100 | [diff] [blame] | 49 | #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */ |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 50 | |
Juan Quintela | 5b4e1eb | 2012-12-19 10:40:48 +0100 | [diff] [blame] | 51 | /* Amount of time to allocate to each "chunk" of bandwidth-throttled |
| 52 | * data. */ |
| 53 | #define BUFFER_DELAY 100 |
| 54 | #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY) |
| 55 | |
Orit Wasserman | 17ad9b3 | 2012-08-06 21:42:53 +0300 | [diff] [blame] | 56 | /* Migration XBZRLE default cache size */ |
| 57 | #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024) |
| 58 | |
Gerd Hoffmann | 99a0db9 | 2010-12-13 17:30:12 +0100 | [diff] [blame] | 59 | static NotifierList migration_state_notifiers = |
| 60 | NOTIFIER_LIST_INITIALIZER(migration_state_notifiers); |
| 61 | |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 62 | /* When we add fault tolerance, we could have several |
| 63 | migrations at once. For now we don't need to add |
| 64 | dynamic creation of migration */ |
| 65 | |
Juan Quintela | 859bc75 | 2012-08-13 09:42:49 +0200 | [diff] [blame] | 66 | MigrationState *migrate_get_current(void) |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 67 | { |
| 68 | static MigrationState current_migration = { |
Michael R. Hines | 29ae8a4 | 2013-07-22 10:01:57 -0400 | [diff] [blame] | 69 | .state = MIG_STATE_NONE, |
Juan Quintela | d0ae46c | 2011-02-23 00:33:19 +0100 | [diff] [blame] | 70 | .bandwidth_limit = MAX_THROTTLE, |
Orit Wasserman | 17ad9b3 | 2012-08-06 21:42:53 +0300 | [diff] [blame] | 71 | .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE, |
Michael R. Hines | 7e114f8 | 2013-06-25 21:35:30 -0400 | [diff] [blame] | 72 | .mbps = -1, |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | return ¤t_migration; |
| 76 | } |
| 77 | |
Paolo Bonzini | 43eaae2 | 2012-10-02 18:21:18 +0200 | [diff] [blame] | 78 | void qemu_start_incoming_migration(const char *uri, Error **errp) |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 79 | { |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 80 | const char *p; |
| 81 | |
| 82 | if (strstart(uri, "tcp:", &p)) |
Paolo Bonzini | 43eaae2 | 2012-10-02 18:21:18 +0200 | [diff] [blame] | 83 | tcp_start_incoming_migration(p, errp); |
Michael R. Hines | 2da776d | 2013-07-22 10:01:54 -0400 | [diff] [blame] | 84 | #ifdef CONFIG_RDMA |
Michael R. Hines | 41310c6 | 2013-12-19 04:52:01 +0800 | [diff] [blame] | 85 | else if (strstart(uri, "rdma:", &p)) |
Michael R. Hines | 2da776d | 2013-07-22 10:01:54 -0400 | [diff] [blame] | 86 | rdma_start_incoming_migration(p, errp); |
| 87 | #endif |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 88 | #if !defined(WIN32) |
| 89 | else if (strstart(uri, "exec:", &p)) |
Paolo Bonzini | 43eaae2 | 2012-10-02 18:21:18 +0200 | [diff] [blame] | 90 | exec_start_incoming_migration(p, errp); |
Chris Lalancette | 4951f65 | 2009-08-05 17:24:29 +0200 | [diff] [blame] | 91 | else if (strstart(uri, "unix:", &p)) |
Paolo Bonzini | 43eaae2 | 2012-10-02 18:21:18 +0200 | [diff] [blame] | 92 | unix_start_incoming_migration(p, errp); |
Paolo Bonzini | 5ac1fad | 2009-08-18 15:56:25 +0200 | [diff] [blame] | 93 | else if (strstart(uri, "fd:", &p)) |
Paolo Bonzini | 43eaae2 | 2012-10-02 18:21:18 +0200 | [diff] [blame] | 94 | fd_start_incoming_migration(p, errp); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 95 | #endif |
Juan Quintela | 8ca5e80 | 2010-06-09 14:10:54 +0200 | [diff] [blame] | 96 | else { |
Markus Armbruster | 312fd5f | 2013-02-08 21:22:16 +0100 | [diff] [blame] | 97 | error_setg(errp, "unknown migration protocol: %s", uri); |
Juan Quintela | 8ca5e80 | 2010-06-09 14:10:54 +0200 | [diff] [blame] | 98 | } |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Paolo Bonzini | 82a4da7 | 2012-08-07 10:57:43 +0200 | [diff] [blame] | 101 | static void process_incoming_migration_co(void *opaque) |
Juan Quintela | 511c023 | 2010-06-09 14:10:55 +0200 | [diff] [blame] | 102 | { |
Paolo Bonzini | 82a4da7 | 2012-08-07 10:57:43 +0200 | [diff] [blame] | 103 | QEMUFile *f = opaque; |
Paolo Bonzini | 1c12e1f | 2012-08-07 10:51:51 +0200 | [diff] [blame] | 104 | int ret; |
| 105 | |
| 106 | ret = qemu_loadvm_state(f); |
| 107 | qemu_fclose(f); |
Gonglei (Arei) | 905f26f | 2014-01-30 20:08:35 +0200 | [diff] [blame] | 108 | free_xbzrle_decoded_buf(); |
Paolo Bonzini | 1c12e1f | 2012-08-07 10:51:51 +0200 | [diff] [blame] | 109 | if (ret < 0) { |
Juan Quintela | 511c023 | 2010-06-09 14:10:55 +0200 | [diff] [blame] | 110 | fprintf(stderr, "load of migration failed\n"); |
Eric Blake | 4aead69 | 2013-04-16 15:50:41 -0600 | [diff] [blame] | 111 | exit(EXIT_FAILURE); |
Juan Quintela | 511c023 | 2010-06-09 14:10:55 +0200 | [diff] [blame] | 112 | } |
| 113 | qemu_announce_self(); |
| 114 | DPRINTF("successfully loaded vm state\n"); |
| 115 | |
Benoît Canet | 901862c | 2012-03-23 08:36:52 +0100 | [diff] [blame] | 116 | bdrv_clear_incoming_migration_all(); |
Anthony Liguori | 0f15423 | 2011-11-14 15:09:45 -0600 | [diff] [blame] | 117 | /* Make sure all file formats flush their mutable metadata */ |
| 118 | bdrv_invalidate_cache_all(); |
| 119 | |
Luiz Capitulino | f5bbfba | 2011-07-29 15:04:45 -0300 | [diff] [blame] | 120 | if (autostart) { |
Juan Quintela | 511c023 | 2010-06-09 14:10:55 +0200 | [diff] [blame] | 121 | vm_start(); |
Luiz Capitulino | f5bbfba | 2011-07-29 15:04:45 -0300 | [diff] [blame] | 122 | } else { |
Paolo Bonzini | 29ed72f | 2012-10-19 16:45:24 +0200 | [diff] [blame] | 123 | runstate_set(RUN_STATE_PAUSED); |
Luiz Capitulino | f5bbfba | 2011-07-29 15:04:45 -0300 | [diff] [blame] | 124 | } |
Juan Quintela | 511c023 | 2010-06-09 14:10:55 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Paolo Bonzini | 82a4da7 | 2012-08-07 10:57:43 +0200 | [diff] [blame] | 127 | void process_incoming_migration(QEMUFile *f) |
| 128 | { |
| 129 | Coroutine *co = qemu_coroutine_create(process_incoming_migration_co); |
| 130 | int fd = qemu_get_fd(f); |
| 131 | |
| 132 | assert(fd != -1); |
Stefan Hajnoczi | f9e8cac | 2013-03-27 10:10:43 +0100 | [diff] [blame] | 133 | qemu_set_nonblock(fd); |
Paolo Bonzini | 82a4da7 | 2012-08-07 10:57:43 +0200 | [diff] [blame] | 134 | qemu_coroutine_enter(co, f); |
| 135 | } |
| 136 | |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 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 = 30000000; |
| 142 | |
| 143 | uint64_t migrate_max_downtime(void) |
| 144 | { |
| 145 | return max_downtime; |
| 146 | } |
| 147 | |
Orit Wasserman | bbf6da3 | 2012-08-06 21:42:47 +0300 | [diff] [blame] | 148 | MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp) |
| 149 | { |
| 150 | MigrationCapabilityStatusList *head = NULL; |
| 151 | MigrationCapabilityStatusList *caps; |
| 152 | MigrationState *s = migrate_get_current(); |
| 153 | int i; |
| 154 | |
Michael Tokarev | 387eede | 2013-10-05 13:18:28 +0400 | [diff] [blame] | 155 | caps = NULL; /* silence compiler warning */ |
Orit Wasserman | bbf6da3 | 2012-08-06 21:42:47 +0300 | [diff] [blame] | 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; |
| 163 | } |
| 164 | caps->value = |
| 165 | g_malloc(sizeof(*caps->value)); |
| 166 | caps->value->capability = i; |
| 167 | caps->value->state = s->enabled_capabilities[i]; |
| 168 | } |
| 169 | |
| 170 | return head; |
| 171 | } |
| 172 | |
Orit Wasserman | f36d55a | 2012-08-06 21:42:57 +0300 | [diff] [blame] | 173 | static void get_xbzrle_cache_stats(MigrationInfo *info) |
| 174 | { |
| 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->overflow = xbzrle_mig_pages_overflow(); |
| 183 | } |
| 184 | } |
| 185 | |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 186 | MigrationInfo *qmp_query_migrate(Error **errp) |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 187 | { |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 188 | MigrationInfo *info = g_malloc0(sizeof(*info)); |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 189 | MigrationState *s = migrate_get_current(); |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 190 | |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 191 | switch (s->state) { |
Michael R. Hines | 29ae8a4 | 2013-07-22 10:01:57 -0400 | [diff] [blame] | 192 | case MIG_STATE_NONE: |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 193 | /* no migration has happened ever */ |
| 194 | break; |
Michael R. Hines | 29ae8a4 | 2013-07-22 10:01:57 -0400 | [diff] [blame] | 195 | case MIG_STATE_SETUP: |
| 196 | info->has_status = true; |
| 197 | info->status = g_strdup("setup"); |
Michael R. Hines | ed4fbd1 | 2013-07-22 10:01:58 -0400 | [diff] [blame] | 198 | info->has_total_time = false; |
Michael R. Hines | 29ae8a4 | 2013-07-22 10:01:57 -0400 | [diff] [blame] | 199 | break; |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 200 | case MIG_STATE_ACTIVE: |
Zhanghaoyu (A) | 51cf4c1 | 2013-11-07 11:01:15 +0000 | [diff] [blame] | 201 | case MIG_STATE_CANCELLING: |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 202 | info->has_status = true; |
| 203 | info->status = g_strdup("active"); |
Juan Quintela | 7aa939a | 2012-08-18 13:17:10 +0200 | [diff] [blame] | 204 | info->has_total_time = true; |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 205 | info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) |
Juan Quintela | 7aa939a | 2012-08-18 13:17:10 +0200 | [diff] [blame] | 206 | - s->total_time; |
Juan Quintela | 2c52ddf | 2012-08-13 09:53:12 +0200 | [diff] [blame] | 207 | info->has_expected_downtime = true; |
| 208 | info->expected_downtime = s->expected_downtime; |
Michael R. Hines | ed4fbd1 | 2013-07-22 10:01:58 -0400 | [diff] [blame] | 209 | info->has_setup_time = true; |
| 210 | info->setup_time = s->setup_time; |
Luiz Capitulino | c86a668 | 2009-12-10 17:16:05 -0200 | [diff] [blame] | 211 | |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 212 | info->has_ram = true; |
| 213 | info->ram = g_malloc0(sizeof(*info->ram)); |
| 214 | info->ram->transferred = ram_bytes_transferred(); |
| 215 | info->ram->remaining = ram_bytes_remaining(); |
| 216 | info->ram->total = ram_bytes_total(); |
Orit Wasserman | 004d4c1 | 2012-08-06 21:42:56 +0300 | [diff] [blame] | 217 | info->ram->duplicate = dup_mig_pages_transferred(); |
Peter Lieven | f1c7279 | 2013-03-26 10:58:37 +0100 | [diff] [blame] | 218 | info->ram->skipped = skipped_mig_pages_transferred(); |
Orit Wasserman | 004d4c1 | 2012-08-06 21:42:56 +0300 | [diff] [blame] | 219 | info->ram->normal = norm_mig_pages_transferred(); |
| 220 | info->ram->normal_bytes = norm_mig_bytes_transferred(); |
Juan Quintela | 8d01719 | 2012-08-13 12:31:25 +0200 | [diff] [blame] | 221 | info->ram->dirty_pages_rate = s->dirty_pages_rate; |
Michael R. Hines | 7e114f8 | 2013-06-25 21:35:30 -0400 | [diff] [blame] | 222 | info->ram->mbps = s->mbps; |
Juan Quintela | 8d01719 | 2012-08-13 12:31:25 +0200 | [diff] [blame] | 223 | |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 224 | if (blk_mig_active()) { |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 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(); |
aliguori | ff8d81d | 2008-10-24 22:10:31 +0000 | [diff] [blame] | 230 | } |
Orit Wasserman | f36d55a | 2012-08-06 21:42:57 +0300 | [diff] [blame] | 231 | |
| 232 | get_xbzrle_cache_stats(info); |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 233 | break; |
| 234 | case MIG_STATE_COMPLETED: |
Orit Wasserman | f36d55a | 2012-08-06 21:42:57 +0300 | [diff] [blame] | 235 | get_xbzrle_cache_stats(info); |
| 236 | |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 237 | info->has_status = true; |
| 238 | info->status = g_strdup("completed"); |
Pawit Pornkitprasan | 00c1499 | 2013-07-19 11:23:45 +0900 | [diff] [blame] | 239 | info->has_total_time = true; |
Juan Quintela | 7aa939a | 2012-08-18 13:17:10 +0200 | [diff] [blame] | 240 | info->total_time = s->total_time; |
Juan Quintela | 9c5a9fc | 2012-08-13 09:35:16 +0200 | [diff] [blame] | 241 | info->has_downtime = true; |
| 242 | info->downtime = s->downtime; |
Michael R. Hines | ed4fbd1 | 2013-07-22 10:01:58 -0400 | [diff] [blame] | 243 | info->has_setup_time = true; |
| 244 | info->setup_time = s->setup_time; |
Juan Quintela | d5f8a57 | 2012-05-21 22:01:07 +0200 | [diff] [blame] | 245 | |
| 246 | info->has_ram = true; |
| 247 | info->ram = g_malloc0(sizeof(*info->ram)); |
| 248 | info->ram->transferred = ram_bytes_transferred(); |
| 249 | info->ram->remaining = 0; |
| 250 | info->ram->total = ram_bytes_total(); |
Orit Wasserman | 004d4c1 | 2012-08-06 21:42:56 +0300 | [diff] [blame] | 251 | info->ram->duplicate = dup_mig_pages_transferred(); |
Peter Lieven | f1c7279 | 2013-03-26 10:58:37 +0100 | [diff] [blame] | 252 | info->ram->skipped = skipped_mig_pages_transferred(); |
Orit Wasserman | 004d4c1 | 2012-08-06 21:42:56 +0300 | [diff] [blame] | 253 | info->ram->normal = norm_mig_pages_transferred(); |
| 254 | info->ram->normal_bytes = norm_mig_bytes_transferred(); |
Michael R. Hines | 7e114f8 | 2013-06-25 21:35:30 -0400 | [diff] [blame] | 255 | info->ram->mbps = s->mbps; |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 256 | break; |
| 257 | case MIG_STATE_ERROR: |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 258 | info->has_status = true; |
| 259 | info->status = g_strdup("failed"); |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 260 | break; |
| 261 | case MIG_STATE_CANCELLED: |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 262 | info->has_status = true; |
| 263 | info->status = g_strdup("cancelled"); |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 264 | break; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 265 | } |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 266 | |
| 267 | return info; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Orit Wasserman | 0045843 | 2012-08-06 21:42:48 +0300 | [diff] [blame] | 270 | void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params, |
| 271 | Error **errp) |
| 272 | { |
| 273 | MigrationState *s = migrate_get_current(); |
| 274 | MigrationCapabilityStatusList *cap; |
| 275 | |
Michael R. Hines | 29ae8a4 | 2013-07-22 10:01:57 -0400 | [diff] [blame] | 276 | if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP) { |
Orit Wasserman | 0045843 | 2012-08-06 21:42:48 +0300 | [diff] [blame] | 277 | error_set(errp, QERR_MIGRATION_ACTIVE); |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | for (cap = params; cap; cap = cap->next) { |
| 282 | s->enabled_capabilities[cap->value->capability] = cap->value->state; |
| 283 | } |
| 284 | } |
| 285 | |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 286 | /* shared migration helpers */ |
| 287 | |
Zhanghaoyu (A) | 51cf4c1 | 2013-11-07 11:01:15 +0000 | [diff] [blame] | 288 | static void migrate_set_state(MigrationState *s, int old_state, int new_state) |
| 289 | { |
| 290 | if (atomic_cmpxchg(&s->state, old_state, new_state) == new_state) { |
| 291 | trace_migrate_set_state(new_state); |
| 292 | } |
| 293 | } |
| 294 | |
Paolo Bonzini | bb1fadc | 2013-02-22 17:36:21 +0100 | [diff] [blame] | 295 | static void migrate_fd_cleanup(void *opaque) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 296 | { |
Paolo Bonzini | bb1fadc | 2013-02-22 17:36:21 +0100 | [diff] [blame] | 297 | MigrationState *s = opaque; |
| 298 | |
| 299 | qemu_bh_delete(s->cleanup_bh); |
| 300 | s->cleanup_bh = NULL; |
| 301 | |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 302 | if (s->file) { |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 303 | DPRINTF("closing file\n"); |
Paolo Bonzini | 404a7c0 | 2013-02-22 17:36:46 +0100 | [diff] [blame] | 304 | qemu_mutex_unlock_iothread(); |
| 305 | qemu_thread_join(&s->thread); |
| 306 | qemu_mutex_lock_iothread(); |
| 307 | |
Paolo Bonzini | 6f190a0 | 2013-02-22 17:36:48 +0100 | [diff] [blame] | 308 | qemu_fclose(s->file); |
| 309 | s->file = NULL; |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Paolo Bonzini | a3fa1d7 | 2013-02-22 17:36:18 +0100 | [diff] [blame] | 312 | assert(s->state != MIG_STATE_ACTIVE); |
Paolo Bonzini | 7a2c172 | 2013-02-22 17:36:09 +0100 | [diff] [blame] | 313 | |
Paolo Bonzini | a3fa1d7 | 2013-02-22 17:36:18 +0100 | [diff] [blame] | 314 | if (s->state != MIG_STATE_COMPLETED) { |
Paolo Bonzini | 7a2c172 | 2013-02-22 17:36:09 +0100 | [diff] [blame] | 315 | qemu_savevm_state_cancel(); |
Zhanghaoyu (A) | 51cf4c1 | 2013-11-07 11:01:15 +0000 | [diff] [blame] | 316 | if (s->state == MIG_STATE_CANCELLING) { |
| 317 | migrate_set_state(s, MIG_STATE_CANCELLING, MIG_STATE_CANCELLED); |
| 318 | } |
Paolo Bonzini | 7a2c172 | 2013-02-22 17:36:09 +0100 | [diff] [blame] | 319 | } |
Paolo Bonzini | a3fa1d7 | 2013-02-22 17:36:18 +0100 | [diff] [blame] | 320 | |
| 321 | notifier_list_notify(&migration_state_notifiers, s); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Juan Quintela | 8b6b99b | 2011-09-11 20:28:22 +0200 | [diff] [blame] | 324 | void migrate_fd_error(MigrationState *s) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 325 | { |
Juan Quintela | 8b6b99b | 2011-09-11 20:28:22 +0200 | [diff] [blame] | 326 | DPRINTF("setting error state\n"); |
Paolo Bonzini | bb1fadc | 2013-02-22 17:36:21 +0100 | [diff] [blame] | 327 | assert(s->file == NULL); |
| 328 | s->state = MIG_STATE_ERROR; |
| 329 | trace_migrate_set_state(MIG_STATE_ERROR); |
| 330 | notifier_list_notify(&migration_state_notifiers, s); |
Juan Quintela | 458cf28 | 2011-02-22 23:32:54 +0100 | [diff] [blame] | 331 | } |
| 332 | |
Juan Quintela | 0edda1c | 2010-05-11 16:28:39 +0200 | [diff] [blame] | 333 | static void migrate_fd_cancel(MigrationState *s) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 334 | { |
Zhanghaoyu (A) | 6f2b811 | 2013-11-07 08:21:23 +0000 | [diff] [blame] | 335 | int old_state ; |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 336 | DPRINTF("cancelling migration\n"); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 337 | |
Zhanghaoyu (A) | 6f2b811 | 2013-11-07 08:21:23 +0000 | [diff] [blame] | 338 | do { |
| 339 | old_state = s->state; |
| 340 | if (old_state != MIG_STATE_SETUP && old_state != MIG_STATE_ACTIVE) { |
| 341 | break; |
| 342 | } |
Zhanghaoyu (A) | 51cf4c1 | 2013-11-07 11:01:15 +0000 | [diff] [blame] | 343 | migrate_set_state(s, old_state, MIG_STATE_CANCELLING); |
| 344 | } while (s->state != MIG_STATE_CANCELLING); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Gerd Hoffmann | 99a0db9 | 2010-12-13 17:30:12 +0100 | [diff] [blame] | 347 | void add_migration_state_change_notifier(Notifier *notify) |
| 348 | { |
| 349 | notifier_list_add(&migration_state_notifiers, notify); |
| 350 | } |
| 351 | |
| 352 | void remove_migration_state_change_notifier(Notifier *notify) |
| 353 | { |
Paolo Bonzini | 3155252 | 2012-01-13 17:34:01 +0100 | [diff] [blame] | 354 | notifier_remove(notify); |
Gerd Hoffmann | 99a0db9 | 2010-12-13 17:30:12 +0100 | [diff] [blame] | 355 | } |
| 356 | |
Stefan Hajnoczi | 02edd2e | 2013-07-29 15:01:58 +0200 | [diff] [blame] | 357 | bool migration_in_setup(MigrationState *s) |
Gerd Hoffmann | afe2df6 | 2011-10-25 13:50:11 +0200 | [diff] [blame] | 358 | { |
Stefan Hajnoczi | 02edd2e | 2013-07-29 15:01:58 +0200 | [diff] [blame] | 359 | return s->state == MIG_STATE_SETUP; |
Gerd Hoffmann | afe2df6 | 2011-10-25 13:50:11 +0200 | [diff] [blame] | 360 | } |
| 361 | |
Juan Quintela | 7073693 | 2011-02-23 00:43:59 +0100 | [diff] [blame] | 362 | bool migration_has_finished(MigrationState *s) |
Gerd Hoffmann | 99a0db9 | 2010-12-13 17:30:12 +0100 | [diff] [blame] | 363 | { |
Juan Quintela | 7073693 | 2011-02-23 00:43:59 +0100 | [diff] [blame] | 364 | return s->state == MIG_STATE_COMPLETED; |
Gerd Hoffmann | 99a0db9 | 2010-12-13 17:30:12 +0100 | [diff] [blame] | 365 | } |
Juan Quintela | 0edda1c | 2010-05-11 16:28:39 +0200 | [diff] [blame] | 366 | |
Gerd Hoffmann | afe2df6 | 2011-10-25 13:50:11 +0200 | [diff] [blame] | 367 | bool migration_has_failed(MigrationState *s) |
| 368 | { |
| 369 | return (s->state == MIG_STATE_CANCELLED || |
| 370 | s->state == MIG_STATE_ERROR); |
| 371 | } |
| 372 | |
Isaku Yamahata | 6607ae2 | 2012-06-19 18:43:09 +0300 | [diff] [blame] | 373 | static MigrationState *migrate_init(const MigrationParams *params) |
Juan Quintela | 0edda1c | 2010-05-11 16:28:39 +0200 | [diff] [blame] | 374 | { |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 375 | MigrationState *s = migrate_get_current(); |
Juan Quintela | d0ae46c | 2011-02-23 00:33:19 +0100 | [diff] [blame] | 376 | int64_t bandwidth_limit = s->bandwidth_limit; |
Orit Wasserman | bbf6da3 | 2012-08-06 21:42:47 +0300 | [diff] [blame] | 377 | bool enabled_capabilities[MIGRATION_CAPABILITY_MAX]; |
Orit Wasserman | 17ad9b3 | 2012-08-06 21:42:53 +0300 | [diff] [blame] | 378 | int64_t xbzrle_cache_size = s->xbzrle_cache_size; |
Orit Wasserman | bbf6da3 | 2012-08-06 21:42:47 +0300 | [diff] [blame] | 379 | |
| 380 | memcpy(enabled_capabilities, s->enabled_capabilities, |
| 381 | sizeof(enabled_capabilities)); |
Juan Quintela | 0edda1c | 2010-05-11 16:28:39 +0200 | [diff] [blame] | 382 | |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 383 | memset(s, 0, sizeof(*s)); |
Isaku Yamahata | 6607ae2 | 2012-06-19 18:43:09 +0300 | [diff] [blame] | 384 | s->params = *params; |
Orit Wasserman | bbf6da3 | 2012-08-06 21:42:47 +0300 | [diff] [blame] | 385 | memcpy(s->enabled_capabilities, enabled_capabilities, |
| 386 | sizeof(enabled_capabilities)); |
Orit Wasserman | 17ad9b3 | 2012-08-06 21:42:53 +0300 | [diff] [blame] | 387 | s->xbzrle_cache_size = xbzrle_cache_size; |
Juan Quintela | 1299c63 | 2011-11-09 21:29:01 +0100 | [diff] [blame] | 388 | |
Juan Quintela | 0edda1c | 2010-05-11 16:28:39 +0200 | [diff] [blame] | 389 | s->bandwidth_limit = bandwidth_limit; |
Juan Quintela | d5934dd | 2010-05-11 23:01:53 +0200 | [diff] [blame] | 390 | s->state = MIG_STATE_SETUP; |
Kazuya Saito | c09e5bb | 2013-02-22 17:36:19 +0100 | [diff] [blame] | 391 | trace_migrate_set_state(MIG_STATE_SETUP); |
Juan Quintela | 0edda1c | 2010-05-11 16:28:39 +0200 | [diff] [blame] | 392 | |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 393 | s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
Juan Quintela | 0edda1c | 2010-05-11 16:28:39 +0200 | [diff] [blame] | 394 | return s; |
| 395 | } |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 396 | |
Anthony Liguori | fa2756b | 2011-11-14 15:09:43 -0600 | [diff] [blame] | 397 | static GSList *migration_blockers; |
| 398 | |
| 399 | void migrate_add_blocker(Error *reason) |
| 400 | { |
| 401 | migration_blockers = g_slist_prepend(migration_blockers, reason); |
| 402 | } |
| 403 | |
| 404 | void migrate_del_blocker(Error *reason) |
| 405 | { |
| 406 | migration_blockers = g_slist_remove(migration_blockers, reason); |
| 407 | } |
| 408 | |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 409 | void qmp_migrate(const char *uri, bool has_blk, bool blk, |
| 410 | bool has_inc, bool inc, bool has_detach, bool detach, |
| 411 | Error **errp) |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 412 | { |
Paolo Bonzini | be7059c | 2012-10-03 14:34:33 +0200 | [diff] [blame] | 413 | Error *local_err = NULL; |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 414 | MigrationState *s = migrate_get_current(); |
Isaku Yamahata | 6607ae2 | 2012-06-19 18:43:09 +0300 | [diff] [blame] | 415 | MigrationParams params; |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 416 | const char *p; |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 417 | |
Pawit Pornkitprasan | 8c0426a | 2013-07-30 08:39:52 +0900 | [diff] [blame] | 418 | params.blk = has_blk && blk; |
| 419 | params.shared = has_inc && inc; |
Isaku Yamahata | 6607ae2 | 2012-06-19 18:43:09 +0300 | [diff] [blame] | 420 | |
Zhanghaoyu (A) | 51cf4c1 | 2013-11-07 11:01:15 +0000 | [diff] [blame] | 421 | if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP || |
| 422 | s->state == MIG_STATE_CANCELLING) { |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 423 | error_set(errp, QERR_MIGRATION_ACTIVE); |
| 424 | return; |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 425 | } |
| 426 | |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 427 | if (qemu_savevm_state_blocked(errp)) { |
| 428 | return; |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 429 | } |
| 430 | |
Anthony Liguori | fa2756b | 2011-11-14 15:09:43 -0600 | [diff] [blame] | 431 | if (migration_blockers) { |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 432 | *errp = error_copy(migration_blockers->data); |
| 433 | return; |
Anthony Liguori | fa2756b | 2011-11-14 15:09:43 -0600 | [diff] [blame] | 434 | } |
| 435 | |
Isaku Yamahata | 6607ae2 | 2012-06-19 18:43:09 +0300 | [diff] [blame] | 436 | s = migrate_init(¶ms); |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 437 | |
| 438 | if (strstart(uri, "tcp:", &p)) { |
Paolo Bonzini | f37afb5 | 2012-10-02 10:02:46 +0200 | [diff] [blame] | 439 | tcp_start_outgoing_migration(s, p, &local_err); |
Michael R. Hines | 2da776d | 2013-07-22 10:01:54 -0400 | [diff] [blame] | 440 | #ifdef CONFIG_RDMA |
Michael R. Hines | 41310c6 | 2013-12-19 04:52:01 +0800 | [diff] [blame] | 441 | } else if (strstart(uri, "rdma:", &p)) { |
Michael R. Hines | 2da776d | 2013-07-22 10:01:54 -0400 | [diff] [blame] | 442 | rdma_start_outgoing_migration(s, p, &local_err); |
| 443 | #endif |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 444 | #if !defined(WIN32) |
| 445 | } else if (strstart(uri, "exec:", &p)) { |
Paolo Bonzini | f37afb5 | 2012-10-02 10:02:46 +0200 | [diff] [blame] | 446 | exec_start_outgoing_migration(s, p, &local_err); |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 447 | } else if (strstart(uri, "unix:", &p)) { |
Paolo Bonzini | f37afb5 | 2012-10-02 10:02:46 +0200 | [diff] [blame] | 448 | unix_start_outgoing_migration(s, p, &local_err); |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 449 | } else if (strstart(uri, "fd:", &p)) { |
Paolo Bonzini | f37afb5 | 2012-10-02 10:02:46 +0200 | [diff] [blame] | 450 | fd_start_outgoing_migration(s, p, &local_err); |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 451 | #endif |
| 452 | } else { |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 453 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol"); |
Luiz Capitulino | c950114 | 2013-12-29 22:39:58 -0500 | [diff] [blame] | 454 | s->state = MIG_STATE_ERROR; |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 455 | return; |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 456 | } |
| 457 | |
Paolo Bonzini | f37afb5 | 2012-10-02 10:02:46 +0200 | [diff] [blame] | 458 | if (local_err) { |
Paolo Bonzini | 342ab8d | 2012-10-02 09:59:38 +0200 | [diff] [blame] | 459 | migrate_fd_error(s); |
Paolo Bonzini | f37afb5 | 2012-10-02 10:02:46 +0200 | [diff] [blame] | 460 | error_propagate(errp, local_err); |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 461 | return; |
Juan Quintela | 1299c63 | 2011-11-09 21:29:01 +0100 | [diff] [blame] | 462 | } |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 463 | } |
| 464 | |
Luiz Capitulino | 6cdedb0 | 2011-11-27 22:54:09 -0200 | [diff] [blame] | 465 | void qmp_migrate_cancel(Error **errp) |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 466 | { |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 467 | migrate_fd_cancel(migrate_get_current()); |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 468 | } |
| 469 | |
Orit Wasserman | 9e1ba4c | 2012-08-06 21:42:54 +0300 | [diff] [blame] | 470 | void qmp_migrate_set_cache_size(int64_t value, Error **errp) |
| 471 | { |
| 472 | MigrationState *s = migrate_get_current(); |
Orit Wasserman | c91e681 | 2014-01-30 20:08:34 +0200 | [diff] [blame] | 473 | int64_t new_size; |
Orit Wasserman | 9e1ba4c | 2012-08-06 21:42:54 +0300 | [diff] [blame] | 474 | |
| 475 | /* Check for truncation */ |
| 476 | if (value != (size_t)value) { |
| 477 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size", |
| 478 | "exceeding address space"); |
| 479 | return; |
| 480 | } |
| 481 | |
Orit Wasserman | a5615b1 | 2014-01-30 20:08:36 +0200 | [diff] [blame] | 482 | /* Cache should not be larger than guest ram size */ |
| 483 | if (value > ram_bytes_total()) { |
| 484 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size", |
| 485 | "exceeds guest ram size "); |
| 486 | return; |
| 487 | } |
| 488 | |
Orit Wasserman | c91e681 | 2014-01-30 20:08:34 +0200 | [diff] [blame] | 489 | new_size = xbzrle_cache_resize(value); |
| 490 | if (new_size < 0) { |
| 491 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size", |
| 492 | "is smaller than page size"); |
| 493 | return; |
| 494 | } |
| 495 | |
| 496 | s->xbzrle_cache_size = new_size; |
Orit Wasserman | 9e1ba4c | 2012-08-06 21:42:54 +0300 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | int64_t qmp_query_migrate_cache_size(Error **errp) |
| 500 | { |
| 501 | return migrate_xbzrle_cache_size(); |
| 502 | } |
| 503 | |
Luiz Capitulino | 3dc8538 | 2011-11-28 11:59:37 -0200 | [diff] [blame] | 504 | void qmp_migrate_set_speed(int64_t value, Error **errp) |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 505 | { |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 506 | MigrationState *s; |
| 507 | |
Luiz Capitulino | 3dc8538 | 2011-11-28 11:59:37 -0200 | [diff] [blame] | 508 | if (value < 0) { |
| 509 | value = 0; |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 510 | } |
Paolo Bonzini | 442773c | 2013-02-22 17:36:44 +0100 | [diff] [blame] | 511 | if (value > SIZE_MAX) { |
| 512 | value = SIZE_MAX; |
| 513 | } |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 514 | |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 515 | s = migrate_get_current(); |
Luiz Capitulino | 3dc8538 | 2011-11-28 11:59:37 -0200 | [diff] [blame] | 516 | s->bandwidth_limit = value; |
Paolo Bonzini | 442773c | 2013-02-22 17:36:44 +0100 | [diff] [blame] | 517 | if (s->file) { |
| 518 | qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO); |
| 519 | } |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 520 | } |
| 521 | |
Luiz Capitulino | 4f0a993 | 2011-11-27 23:18:01 -0200 | [diff] [blame] | 522 | void qmp_migrate_set_downtime(double value, Error **errp) |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 523 | { |
Luiz Capitulino | 4f0a993 | 2011-11-27 23:18:01 -0200 | [diff] [blame] | 524 | value *= 1e9; |
| 525 | value = MAX(0, MIN(UINT64_MAX, value)); |
| 526 | max_downtime = (uint64_t)value; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 527 | } |
Orit Wasserman | 17ad9b3 | 2012-08-06 21:42:53 +0300 | [diff] [blame] | 528 | |
Michael R. Hines | 60d9222 | 2013-06-25 21:35:36 -0400 | [diff] [blame] | 529 | bool migrate_rdma_pin_all(void) |
| 530 | { |
| 531 | MigrationState *s; |
| 532 | |
| 533 | s = migrate_get_current(); |
| 534 | |
Michael R. Hines | 41310c6 | 2013-12-19 04:52:01 +0800 | [diff] [blame] | 535 | return s->enabled_capabilities[MIGRATION_CAPABILITY_RDMA_PIN_ALL]; |
Michael R. Hines | 60d9222 | 2013-06-25 21:35:36 -0400 | [diff] [blame] | 536 | } |
| 537 | |
Chegu Vinod | bde1e2e | 2013-06-24 03:49:42 -0600 | [diff] [blame] | 538 | bool migrate_auto_converge(void) |
| 539 | { |
| 540 | MigrationState *s; |
| 541 | |
| 542 | s = migrate_get_current(); |
| 543 | |
| 544 | return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE]; |
| 545 | } |
| 546 | |
Peter Lieven | 323004a | 2013-07-18 09:48:50 +0200 | [diff] [blame] | 547 | bool migrate_zero_blocks(void) |
| 548 | { |
| 549 | MigrationState *s; |
| 550 | |
| 551 | s = migrate_get_current(); |
| 552 | |
| 553 | return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS]; |
| 554 | } |
| 555 | |
Orit Wasserman | 17ad9b3 | 2012-08-06 21:42:53 +0300 | [diff] [blame] | 556 | int migrate_use_xbzrle(void) |
| 557 | { |
| 558 | MigrationState *s; |
| 559 | |
| 560 | s = migrate_get_current(); |
| 561 | |
| 562 | return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE]; |
| 563 | } |
| 564 | |
| 565 | int64_t migrate_xbzrle_cache_size(void) |
| 566 | { |
| 567 | MigrationState *s; |
| 568 | |
| 569 | s = migrate_get_current(); |
| 570 | |
| 571 | return s->xbzrle_cache_size; |
| 572 | } |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 573 | |
| 574 | /* migration thread support */ |
| 575 | |
Juan Quintela | 5f496a1 | 2013-02-22 17:36:30 +0100 | [diff] [blame] | 576 | static void *migration_thread(void *opaque) |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 577 | { |
Juan Quintela | 9848a40 | 2012-12-19 09:55:50 +0100 | [diff] [blame] | 578 | MigrationState *s = opaque; |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 579 | int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
| 580 | int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST); |
Paolo Bonzini | be7172e | 2013-02-22 17:36:43 +0100 | [diff] [blame] | 581 | int64_t initial_bytes = 0; |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 582 | int64_t max_size = 0; |
Paolo Bonzini | a3fa1d7 | 2013-02-22 17:36:18 +0100 | [diff] [blame] | 583 | int64_t start_time = initial_time; |
| 584 | bool old_vm_running = false; |
Juan Quintela | 76f5933 | 2012-10-03 20:16:24 +0200 | [diff] [blame] | 585 | |
Juan Quintela | 76f5933 | 2012-10-03 20:16:24 +0200 | [diff] [blame] | 586 | DPRINTF("beginning savevm\n"); |
Paolo Bonzini | dba433c | 2013-02-22 17:36:17 +0100 | [diff] [blame] | 587 | qemu_savevm_state_begin(s->file, &s->params); |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 588 | |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 589 | s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start; |
Michael R. Hines | 29ae8a4 | 2013-07-22 10:01:57 -0400 | [diff] [blame] | 590 | migrate_set_state(s, MIG_STATE_SETUP, MIG_STATE_ACTIVE); |
| 591 | |
Michael R. Hines | ed4fbd1 | 2013-07-22 10:01:58 -0400 | [diff] [blame] | 592 | DPRINTF("setup complete\n"); |
| 593 | |
Paolo Bonzini | dba433c | 2013-02-22 17:36:17 +0100 | [diff] [blame] | 594 | while (s->state == MIG_STATE_ACTIVE) { |
Juan Quintela | a3e879c | 2013-02-01 12:39:08 +0100 | [diff] [blame] | 595 | int64_t current_time; |
Juan Quintela | c369f40 | 2012-10-03 20:33:34 +0200 | [diff] [blame] | 596 | uint64_t pending_size; |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 597 | |
Paolo Bonzini | a0ff044 | 2013-02-22 17:36:35 +0100 | [diff] [blame] | 598 | if (!qemu_file_rate_limit(s->file)) { |
Juan Quintela | c369f40 | 2012-10-03 20:33:34 +0200 | [diff] [blame] | 599 | DPRINTF("iterate\n"); |
| 600 | pending_size = qemu_savevm_state_pending(s->file, max_size); |
Christoffer Dall | 5016e2d | 2013-08-23 10:34:16 -0700 | [diff] [blame] | 601 | DPRINTF("pending size %" PRIu64 " max %" PRIu64 "\n", |
| 602 | pending_size, max_size); |
Juan Quintela | b22ff1f | 2012-10-17 21:06:31 +0200 | [diff] [blame] | 603 | if (pending_size && pending_size >= max_size) { |
Paolo Bonzini | dba433c | 2013-02-22 17:36:17 +0100 | [diff] [blame] | 604 | qemu_savevm_state_iterate(s->file); |
Juan Quintela | c369f40 | 2012-10-03 20:33:34 +0200 | [diff] [blame] | 605 | } else { |
Kevin Wolf | 0e1146a | 2013-07-05 13:54:55 +0200 | [diff] [blame] | 606 | int ret; |
| 607 | |
Juan Quintela | c369f40 | 2012-10-03 20:33:34 +0200 | [diff] [blame] | 608 | DPRINTF("done iterating\n"); |
Paolo Bonzini | 32c835b | 2013-02-22 17:36:27 +0100 | [diff] [blame] | 609 | qemu_mutex_lock_iothread(); |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 610 | start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
Juan Quintela | c369f40 | 2012-10-03 20:33:34 +0200 | [diff] [blame] | 611 | qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER); |
Paolo Bonzini | a3fa1d7 | 2013-02-22 17:36:18 +0100 | [diff] [blame] | 612 | old_vm_running = runstate_is_running(); |
Kevin Wolf | 0e1146a | 2013-07-05 13:54:55 +0200 | [diff] [blame] | 613 | |
| 614 | ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE); |
| 615 | if (ret >= 0) { |
Matthew Garrett | 4059683 | 2013-11-25 14:42:43 -0500 | [diff] [blame] | 616 | qemu_file_set_rate_limit(s->file, INT64_MAX); |
Kevin Wolf | 0e1146a | 2013-07-05 13:54:55 +0200 | [diff] [blame] | 617 | qemu_savevm_state_complete(s->file); |
| 618 | } |
Paolo Bonzini | 32c835b | 2013-02-22 17:36:27 +0100 | [diff] [blame] | 619 | qemu_mutex_unlock_iothread(); |
Kevin Wolf | 0e1146a | 2013-07-05 13:54:55 +0200 | [diff] [blame] | 620 | |
| 621 | if (ret < 0) { |
Michael R. Hines | d58f574 | 2013-07-22 10:01:56 -0400 | [diff] [blame] | 622 | migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR); |
Kevin Wolf | 0e1146a | 2013-07-05 13:54:55 +0200 | [diff] [blame] | 623 | break; |
| 624 | } |
| 625 | |
Paolo Bonzini | 059f896 | 2013-02-22 17:36:32 +0100 | [diff] [blame] | 626 | if (!qemu_file_get_error(s->file)) { |
Michael R. Hines | d58f574 | 2013-07-22 10:01:56 -0400 | [diff] [blame] | 627 | migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_COMPLETED); |
Paolo Bonzini | 059f896 | 2013-02-22 17:36:32 +0100 | [diff] [blame] | 628 | break; |
| 629 | } |
Juan Quintela | c369f40 | 2012-10-03 20:33:34 +0200 | [diff] [blame] | 630 | } |
| 631 | } |
Paolo Bonzini | f4410a5 | 2013-02-22 17:36:20 +0100 | [diff] [blame] | 632 | |
Paolo Bonzini | fd45ee2 | 2013-02-22 17:36:33 +0100 | [diff] [blame] | 633 | if (qemu_file_get_error(s->file)) { |
Michael R. Hines | d58f574 | 2013-07-22 10:01:56 -0400 | [diff] [blame] | 634 | migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR); |
Paolo Bonzini | fd45ee2 | 2013-02-22 17:36:33 +0100 | [diff] [blame] | 635 | break; |
| 636 | } |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 637 | current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 638 | if (current_time >= initial_time + BUFFER_DELAY) { |
Paolo Bonzini | be7172e | 2013-02-22 17:36:43 +0100 | [diff] [blame] | 639 | uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes; |
Michael Roth | 77417f1 | 2013-05-16 16:25:44 -0500 | [diff] [blame] | 640 | uint64_t time_spent = current_time - initial_time; |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 641 | double bandwidth = transferred_bytes / time_spent; |
| 642 | max_size = bandwidth * migrate_max_downtime() / 1000000; |
| 643 | |
Michael R. Hines | 7e114f8 | 2013-06-25 21:35:30 -0400 | [diff] [blame] | 644 | s->mbps = time_spent ? (((double) transferred_bytes * 8.0) / |
| 645 | ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1; |
| 646 | |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 647 | DPRINTF("transferred %" PRIu64 " time_spent %" PRIu64 |
| 648 | " bandwidth %g max_size %" PRId64 "\n", |
| 649 | transferred_bytes, time_spent, bandwidth, max_size); |
Juan Quintela | 90f8ae7 | 2013-02-01 13:22:37 +0100 | [diff] [blame] | 650 | /* if we haven't sent anything, we don't want to recalculate |
| 651 | 10000 is a small enough number for our purposes */ |
| 652 | if (s->dirty_bytes_rate && transferred_bytes > 10000) { |
| 653 | s->expected_downtime = s->dirty_bytes_rate / bandwidth; |
| 654 | } |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 655 | |
Paolo Bonzini | 1964a39 | 2013-02-22 17:36:45 +0100 | [diff] [blame] | 656 | qemu_file_reset_rate_limit(s->file); |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 657 | initial_time = current_time; |
Paolo Bonzini | be7172e | 2013-02-22 17:36:43 +0100 | [diff] [blame] | 658 | initial_bytes = qemu_ftell(s->file); |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 659 | } |
Paolo Bonzini | a0ff044 | 2013-02-22 17:36:35 +0100 | [diff] [blame] | 660 | if (qemu_file_rate_limit(s->file)) { |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 661 | /* usleep expects microseconds */ |
| 662 | g_usleep((initial_time + BUFFER_DELAY - current_time)*1000); |
| 663 | } |
Paolo Bonzini | a3fa1d7 | 2013-02-22 17:36:18 +0100 | [diff] [blame] | 664 | } |
| 665 | |
Paolo Bonzini | f4410a5 | 2013-02-22 17:36:20 +0100 | [diff] [blame] | 666 | qemu_mutex_lock_iothread(); |
Paolo Bonzini | a3fa1d7 | 2013-02-22 17:36:18 +0100 | [diff] [blame] | 667 | if (s->state == MIG_STATE_COMPLETED) { |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 668 | int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
Paolo Bonzini | a3fa1d7 | 2013-02-22 17:36:18 +0100 | [diff] [blame] | 669 | s->total_time = end_time - s->total_time; |
| 670 | s->downtime = end_time - start_time; |
| 671 | runstate_set(RUN_STATE_POSTMIGRATE); |
| 672 | } else { |
| 673 | if (old_vm_running) { |
Paolo Bonzini | a3fa1d7 | 2013-02-22 17:36:18 +0100 | [diff] [blame] | 674 | vm_start(); |
Paolo Bonzini | dba433c | 2013-02-22 17:36:17 +0100 | [diff] [blame] | 675 | } |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 676 | } |
Paolo Bonzini | bb1fadc | 2013-02-22 17:36:21 +0100 | [diff] [blame] | 677 | qemu_bh_schedule(s->cleanup_bh); |
Paolo Bonzini | dba433c | 2013-02-22 17:36:17 +0100 | [diff] [blame] | 678 | qemu_mutex_unlock_iothread(); |
Paolo Bonzini | f4410a5 | 2013-02-22 17:36:20 +0100 | [diff] [blame] | 679 | |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 680 | return NULL; |
| 681 | } |
| 682 | |
Juan Quintela | 9848a40 | 2012-12-19 09:55:50 +0100 | [diff] [blame] | 683 | void migrate_fd_connect(MigrationState *s) |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 684 | { |
Michael R. Hines | 29ae8a4 | 2013-07-22 10:01:57 -0400 | [diff] [blame] | 685 | s->state = MIG_STATE_SETUP; |
| 686 | trace_migrate_set_state(MIG_STATE_SETUP); |
Kazuya Saito | c09e5bb | 2013-02-22 17:36:19 +0100 | [diff] [blame] | 687 | |
Juan Quintela | cc283e3 | 2013-02-01 11:12:26 +0100 | [diff] [blame] | 688 | /* This is a best 1st approximation. ns to ms */ |
| 689 | s->expected_downtime = max_downtime/1000000; |
Paolo Bonzini | bb1fadc | 2013-02-22 17:36:21 +0100 | [diff] [blame] | 690 | s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s); |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 691 | |
Paolo Bonzini | 442773c | 2013-02-22 17:36:44 +0100 | [diff] [blame] | 692 | qemu_file_set_rate_limit(s->file, |
| 693 | s->bandwidth_limit / XFER_LIMIT_RATIO); |
| 694 | |
Stefan Hajnoczi | 9287ac2 | 2013-07-29 15:01:57 +0200 | [diff] [blame] | 695 | /* Notify before starting migration thread */ |
| 696 | notifier_list_notify(&migration_state_notifiers, s); |
| 697 | |
Juan Quintela | 5f496a1 | 2013-02-22 17:36:30 +0100 | [diff] [blame] | 698 | qemu_thread_create(&s->thread, migration_thread, s, |
Paolo Bonzini | bb1fadc | 2013-02-22 17:36:21 +0100 | [diff] [blame] | 699 | QEMU_THREAD_JOINABLE); |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 700 | } |