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