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" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 17 | #include "qemu/error-report.h" |
Alex Bligh | 6a1751b | 2013-08-21 16:02:47 +0100 | [diff] [blame] | 18 | #include "qemu/main-loop.h" |
Paolo Bonzini | caf71f8 | 2012-12-17 18:19:50 +0100 | [diff] [blame] | 19 | #include "migration/migration.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" |
Markus Armbruster | cc7a8ea | 2015-03-17 17:22:46 +0100 | [diff] [blame] | 23 | #include "qapi/qmp/qerror.h" |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 24 | #include "qapi/util.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 25 | #include "qemu/sockets.h" |
Paolo Bonzini | ab28bd2 | 2015-07-09 08:55:38 +0200 | [diff] [blame] | 26 | #include "qemu/rcu.h" |
Paolo Bonzini | caf71f8 | 2012-12-17 18:19:50 +0100 | [diff] [blame] | 27 | #include "migration/block.h" |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 28 | #include "migration/postcopy-ram.h" |
Juan Quintela | 766bd17 | 2012-07-23 05:45:29 +0200 | [diff] [blame] | 29 | #include "qemu/thread.h" |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 30 | #include "qmp-commands.h" |
Kazuya Saito | c09e5bb | 2013-02-22 17:36:19 +0100 | [diff] [blame] | 31 | #include "trace.h" |
Juan Quintela | 598cd2b | 2015-05-20 12:16:15 +0200 | [diff] [blame] | 32 | #include "qapi-event.h" |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 33 | #include "qom/cpu.h" |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 34 | #include "exec/memory.h" |
| 35 | #include "exec/address-spaces.h" |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 36 | |
Jason J. Herne | dc32562 | 2015-09-08 13:12:37 -0400 | [diff] [blame] | 37 | #define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */ |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 38 | |
Juan Quintela | 5b4e1eb | 2012-12-19 10:40:48 +0100 | [diff] [blame] | 39 | /* Amount of time to allocate to each "chunk" of bandwidth-throttled |
| 40 | * data. */ |
| 41 | #define BUFFER_DELAY 100 |
| 42 | #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY) |
| 43 | |
Liang Li | 8706d2d | 2015-03-23 16:32:17 +0800 | [diff] [blame] | 44 | /* Default compression thread count */ |
| 45 | #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8 |
Liang Li | 3fcb38c | 2015-03-23 16:32:18 +0800 | [diff] [blame] | 46 | /* Default decompression thread count, usually decompression is at |
| 47 | * least 4 times as fast as compression.*/ |
| 48 | #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2 |
Liang Li | 8706d2d | 2015-03-23 16:32:17 +0800 | [diff] [blame] | 49 | /*0: means nocompress, 1: best speed, ... 9: best compress ratio */ |
| 50 | #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1 |
Jason J. Herne | 1626fee | 2015-09-08 13:12:34 -0400 | [diff] [blame] | 51 | /* Define default autoconverge cpu throttle migration parameters */ |
| 52 | #define DEFAULT_MIGRATE_X_CPU_THROTTLE_INITIAL 20 |
| 53 | #define DEFAULT_MIGRATE_X_CPU_THROTTLE_INCREMENT 10 |
Liang Li | 8706d2d | 2015-03-23 16:32:17 +0800 | [diff] [blame] | 54 | |
Orit Wasserman | 17ad9b3 | 2012-08-06 21:42:53 +0300 | [diff] [blame] | 55 | /* Migration XBZRLE default cache size */ |
| 56 | #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024) |
| 57 | |
Gerd Hoffmann | 99a0db9 | 2010-12-13 17:30:12 +0100 | [diff] [blame] | 58 | static NotifierList migration_state_notifiers = |
| 59 | NOTIFIER_LIST_INITIALIZER(migration_state_notifiers); |
| 60 | |
Dr. David Alan Gilbert | adde220 | 2015-02-19 11:40:27 +0000 | [diff] [blame] | 61 | static bool deferred_incoming; |
| 62 | |
Dr. David Alan Gilbert | 093e3c4 | 2015-11-05 18:10:52 +0000 | [diff] [blame] | 63 | /* |
| 64 | * Current state of incoming postcopy; note this is not part of |
| 65 | * MigrationIncomingState since it's state is used during cleanup |
| 66 | * at the end as MIS is being freed. |
| 67 | */ |
| 68 | static PostcopyState incoming_postcopy_state; |
| 69 | |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 70 | /* When we add fault tolerance, we could have several |
| 71 | migrations at once. For now we don't need to add |
| 72 | dynamic creation of migration */ |
| 73 | |
Dr. David Alan Gilbert | bca7856 | 2015-05-21 13:24:14 +0100 | [diff] [blame] | 74 | /* For outgoing */ |
Juan Quintela | 859bc75 | 2012-08-13 09:42:49 +0200 | [diff] [blame] | 75 | MigrationState *migrate_get_current(void) |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 76 | { |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 77 | static bool once; |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 78 | static MigrationState current_migration = { |
zhanghailiang | 3119473 | 2015-03-13 16:08:38 +0800 | [diff] [blame] | 79 | .state = MIGRATION_STATUS_NONE, |
Juan Quintela | d0ae46c | 2011-02-23 00:33:19 +0100 | [diff] [blame] | 80 | .bandwidth_limit = MAX_THROTTLE, |
Orit Wasserman | 17ad9b3 | 2012-08-06 21:42:53 +0300 | [diff] [blame] | 81 | .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE, |
Michael R. Hines | 7e114f8 | 2013-06-25 21:35:30 -0400 | [diff] [blame] | 82 | .mbps = -1, |
Liang Li | 43c60a8 | 2015-03-23 16:32:27 +0800 | [diff] [blame] | 83 | .parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = |
| 84 | DEFAULT_MIGRATE_COMPRESS_LEVEL, |
| 85 | .parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = |
| 86 | DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT, |
| 87 | .parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] = |
| 88 | DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT, |
Jason J. Herne | 1626fee | 2015-09-08 13:12:34 -0400 | [diff] [blame] | 89 | .parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] = |
| 90 | DEFAULT_MIGRATE_X_CPU_THROTTLE_INITIAL, |
| 91 | .parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] = |
| 92 | DEFAULT_MIGRATE_X_CPU_THROTTLE_INCREMENT, |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 93 | }; |
| 94 | |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 95 | if (!once) { |
| 96 | qemu_mutex_init(¤t_migration.src_page_req_mutex); |
| 97 | once = true; |
| 98 | } |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 99 | return ¤t_migration; |
| 100 | } |
| 101 | |
Dr. David Alan Gilbert | bca7856 | 2015-05-21 13:24:14 +0100 | [diff] [blame] | 102 | /* For incoming */ |
| 103 | static MigrationIncomingState *mis_current; |
| 104 | |
| 105 | MigrationIncomingState *migration_incoming_get_current(void) |
| 106 | { |
| 107 | return mis_current; |
| 108 | } |
| 109 | |
| 110 | MigrationIncomingState *migration_incoming_state_new(QEMUFile* f) |
| 111 | { |
Markus Armbruster | 97f3ad3 | 2015-09-14 13:51:31 +0200 | [diff] [blame] | 112 | mis_current = g_new0(MigrationIncomingState, 1); |
Dr. David Alan Gilbert | 42e2aa5 | 2015-11-05 18:10:34 +0000 | [diff] [blame] | 113 | mis_current->from_src_file = f; |
Dr. David Alan Gilbert | 1a8f46f | 2015-05-21 13:24:16 +0100 | [diff] [blame] | 114 | QLIST_INIT(&mis_current->loadvm_handlers); |
Dr. David Alan Gilbert | 6decec9 | 2015-11-05 18:10:47 +0000 | [diff] [blame] | 115 | qemu_mutex_init(&mis_current->rp_mutex); |
Dr. David Alan Gilbert | 7b89bf2 | 2015-11-05 18:10:50 +0000 | [diff] [blame] | 116 | qemu_event_init(&mis_current->main_thread_load_event, false); |
Dr. David Alan Gilbert | bca7856 | 2015-05-21 13:24:14 +0100 | [diff] [blame] | 117 | |
| 118 | return mis_current; |
| 119 | } |
| 120 | |
| 121 | void migration_incoming_state_destroy(void) |
| 122 | { |
Dr. David Alan Gilbert | 7b89bf2 | 2015-11-05 18:10:50 +0000 | [diff] [blame] | 123 | qemu_event_destroy(&mis_current->main_thread_load_event); |
Dr. David Alan Gilbert | 1a8f46f | 2015-05-21 13:24:16 +0100 | [diff] [blame] | 124 | loadvm_free_handlers(mis_current); |
Dr. David Alan Gilbert | bca7856 | 2015-05-21 13:24:14 +0100 | [diff] [blame] | 125 | g_free(mis_current); |
| 126 | mis_current = NULL; |
| 127 | } |
| 128 | |
Juan Quintela | df4b102 | 2014-10-08 10:58:10 +0200 | [diff] [blame] | 129 | |
| 130 | typedef struct { |
Juan Quintela | 13d1681 | 2014-10-08 13:58:24 +0200 | [diff] [blame] | 131 | bool optional; |
Juan Quintela | df4b102 | 2014-10-08 10:58:10 +0200 | [diff] [blame] | 132 | uint32_t size; |
| 133 | uint8_t runstate[100]; |
Juan Quintela | 172c435 | 2015-07-08 13:56:26 +0200 | [diff] [blame] | 134 | RunState state; |
| 135 | bool received; |
Juan Quintela | df4b102 | 2014-10-08 10:58:10 +0200 | [diff] [blame] | 136 | } GlobalState; |
| 137 | |
| 138 | static GlobalState global_state; |
| 139 | |
Juan Quintela | 560d027 | 2015-07-15 09:53:46 +0200 | [diff] [blame] | 140 | int global_state_store(void) |
Juan Quintela | df4b102 | 2014-10-08 10:58:10 +0200 | [diff] [blame] | 141 | { |
| 142 | if (!runstate_store((char *)global_state.runstate, |
| 143 | sizeof(global_state.runstate))) { |
| 144 | error_report("runstate name too big: %s", global_state.runstate); |
| 145 | trace_migrate_state_too_big(); |
| 146 | return -EINVAL; |
| 147 | } |
| 148 | return 0; |
| 149 | } |
| 150 | |
Anthony PERARD | c69adea | 2015-08-03 15:29:19 +0100 | [diff] [blame] | 151 | void global_state_store_running(void) |
| 152 | { |
| 153 | const char *state = RunState_lookup[RUN_STATE_RUNNING]; |
| 154 | strncpy((char *)global_state.runstate, |
| 155 | state, sizeof(global_state.runstate)); |
| 156 | } |
| 157 | |
Juan Quintela | 172c435 | 2015-07-08 13:56:26 +0200 | [diff] [blame] | 158 | static bool global_state_received(void) |
Juan Quintela | df4b102 | 2014-10-08 10:58:10 +0200 | [diff] [blame] | 159 | { |
Juan Quintela | 172c435 | 2015-07-08 13:56:26 +0200 | [diff] [blame] | 160 | return global_state.received; |
| 161 | } |
| 162 | |
| 163 | static RunState global_state_get_runstate(void) |
| 164 | { |
| 165 | return global_state.state; |
Juan Quintela | df4b102 | 2014-10-08 10:58:10 +0200 | [diff] [blame] | 166 | } |
| 167 | |
Juan Quintela | 13d1681 | 2014-10-08 13:58:24 +0200 | [diff] [blame] | 168 | void global_state_set_optional(void) |
| 169 | { |
| 170 | global_state.optional = true; |
| 171 | } |
| 172 | |
| 173 | static bool global_state_needed(void *opaque) |
| 174 | { |
| 175 | GlobalState *s = opaque; |
| 176 | char *runstate = (char *)s->runstate; |
| 177 | |
| 178 | /* If it is not optional, it is mandatory */ |
| 179 | |
| 180 | if (s->optional == false) { |
| 181 | return true; |
| 182 | } |
| 183 | |
| 184 | /* If state is running or paused, it is not needed */ |
| 185 | |
| 186 | if (strcmp(runstate, "running") == 0 || |
| 187 | strcmp(runstate, "paused") == 0) { |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | /* for any other state it is needed */ |
| 192 | return true; |
| 193 | } |
| 194 | |
Juan Quintela | df4b102 | 2014-10-08 10:58:10 +0200 | [diff] [blame] | 195 | static int global_state_post_load(void *opaque, int version_id) |
| 196 | { |
| 197 | GlobalState *s = opaque; |
Juan Quintela | 172c435 | 2015-07-08 13:56:26 +0200 | [diff] [blame] | 198 | Error *local_err = NULL; |
| 199 | int r; |
Juan Quintela | df4b102 | 2014-10-08 10:58:10 +0200 | [diff] [blame] | 200 | char *runstate = (char *)s->runstate; |
| 201 | |
Juan Quintela | 172c435 | 2015-07-08 13:56:26 +0200 | [diff] [blame] | 202 | s->received = true; |
Juan Quintela | df4b102 | 2014-10-08 10:58:10 +0200 | [diff] [blame] | 203 | trace_migrate_global_state_post_load(runstate); |
| 204 | |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 205 | r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE__MAX, |
Juan Quintela | df4b102 | 2014-10-08 10:58:10 +0200 | [diff] [blame] | 206 | -1, &local_err); |
| 207 | |
Juan Quintela | 172c435 | 2015-07-08 13:56:26 +0200 | [diff] [blame] | 208 | if (r == -1) { |
| 209 | if (local_err) { |
| 210 | error_report_err(local_err); |
Juan Quintela | df4b102 | 2014-10-08 10:58:10 +0200 | [diff] [blame] | 211 | } |
Juan Quintela | 172c435 | 2015-07-08 13:56:26 +0200 | [diff] [blame] | 212 | return -EINVAL; |
Juan Quintela | df4b102 | 2014-10-08 10:58:10 +0200 | [diff] [blame] | 213 | } |
Juan Quintela | 172c435 | 2015-07-08 13:56:26 +0200 | [diff] [blame] | 214 | s->state = r; |
Juan Quintela | df4b102 | 2014-10-08 10:58:10 +0200 | [diff] [blame] | 215 | |
Juan Quintela | 172c435 | 2015-07-08 13:56:26 +0200 | [diff] [blame] | 216 | return 0; |
Juan Quintela | df4b102 | 2014-10-08 10:58:10 +0200 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static void global_state_pre_save(void *opaque) |
| 220 | { |
| 221 | GlobalState *s = opaque; |
| 222 | |
| 223 | trace_migrate_global_state_pre_save((char *)s->runstate); |
| 224 | s->size = strlen((char *)s->runstate) + 1; |
| 225 | } |
| 226 | |
| 227 | static const VMStateDescription vmstate_globalstate = { |
| 228 | .name = "globalstate", |
| 229 | .version_id = 1, |
| 230 | .minimum_version_id = 1, |
| 231 | .post_load = global_state_post_load, |
| 232 | .pre_save = global_state_pre_save, |
Juan Quintela | 13d1681 | 2014-10-08 13:58:24 +0200 | [diff] [blame] | 233 | .needed = global_state_needed, |
Juan Quintela | df4b102 | 2014-10-08 10:58:10 +0200 | [diff] [blame] | 234 | .fields = (VMStateField[]) { |
| 235 | VMSTATE_UINT32(size, GlobalState), |
| 236 | VMSTATE_BUFFER(runstate, GlobalState), |
| 237 | VMSTATE_END_OF_LIST() |
| 238 | }, |
| 239 | }; |
| 240 | |
| 241 | void register_global_state(void) |
| 242 | { |
| 243 | /* We would use it independently that we receive it */ |
| 244 | strcpy((char *)&global_state.runstate, ""); |
Juan Quintela | 172c435 | 2015-07-08 13:56:26 +0200 | [diff] [blame] | 245 | global_state.received = false; |
Juan Quintela | df4b102 | 2014-10-08 10:58:10 +0200 | [diff] [blame] | 246 | vmstate_register(NULL, 0, &vmstate_globalstate, &global_state); |
| 247 | } |
| 248 | |
Juan Quintela | b05dc72 | 2015-07-07 14:44:05 +0200 | [diff] [blame] | 249 | static void migrate_generate_event(int new_state) |
| 250 | { |
| 251 | if (migrate_use_events()) { |
| 252 | qapi_event_send_migration(new_state, &error_abort); |
Juan Quintela | b05dc72 | 2015-07-07 14:44:05 +0200 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
Dr. David Alan Gilbert | adde220 | 2015-02-19 11:40:27 +0000 | [diff] [blame] | 256 | /* |
| 257 | * Called on -incoming with a defer: uri. |
| 258 | * The migration can be started later after any parameters have been |
| 259 | * changed. |
| 260 | */ |
| 261 | static void deferred_incoming_migration(Error **errp) |
| 262 | { |
| 263 | if (deferred_incoming) { |
| 264 | error_setg(errp, "Incoming migration already deferred"); |
| 265 | } |
| 266 | deferred_incoming = true; |
| 267 | } |
| 268 | |
Dr. David Alan Gilbert | 1e2d90e | 2015-11-05 18:11:07 +0000 | [diff] [blame] | 269 | /* Request a range of pages from the source VM at the given |
| 270 | * start address. |
| 271 | * rbname: Name of the RAMBlock to request the page in, if NULL it's the same |
| 272 | * as the last request (a name must have been given previously) |
| 273 | * Start: Address offset within the RB |
| 274 | * Len: Length in bytes required - must be a multiple of pagesize |
| 275 | */ |
| 276 | void migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname, |
| 277 | ram_addr_t start, size_t len) |
| 278 | { |
| 279 | uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname upto 256 */ |
| 280 | size_t msglen = 12; /* start + len */ |
| 281 | |
| 282 | *(uint64_t *)bufc = cpu_to_be64((uint64_t)start); |
| 283 | *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len); |
| 284 | |
| 285 | if (rbname) { |
| 286 | int rbname_len = strlen(rbname); |
| 287 | assert(rbname_len < 256); |
| 288 | |
| 289 | bufc[msglen++] = rbname_len; |
| 290 | memcpy(bufc + msglen, rbname, rbname_len); |
| 291 | msglen += rbname_len; |
| 292 | migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES_ID, msglen, bufc); |
| 293 | } else { |
| 294 | migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES, msglen, bufc); |
| 295 | } |
| 296 | } |
| 297 | |
Paolo Bonzini | 43eaae2 | 2012-10-02 18:21:18 +0200 | [diff] [blame] | 298 | void qemu_start_incoming_migration(const char *uri, Error **errp) |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 299 | { |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 300 | const char *p; |
| 301 | |
Juan Quintela | 7cf1fe6 | 2015-05-20 17:15:42 +0200 | [diff] [blame] | 302 | qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort); |
Dr. David Alan Gilbert | adde220 | 2015-02-19 11:40:27 +0000 | [diff] [blame] | 303 | if (!strcmp(uri, "defer")) { |
| 304 | deferred_incoming_migration(errp); |
| 305 | } else if (strstart(uri, "tcp:", &p)) { |
Paolo Bonzini | 43eaae2 | 2012-10-02 18:21:18 +0200 | [diff] [blame] | 306 | tcp_start_incoming_migration(p, errp); |
Michael R. Hines | 2da776d | 2013-07-22 10:01:54 -0400 | [diff] [blame] | 307 | #ifdef CONFIG_RDMA |
Dr. David Alan Gilbert | adde220 | 2015-02-19 11:40:27 +0000 | [diff] [blame] | 308 | } else if (strstart(uri, "rdma:", &p)) { |
Michael R. Hines | 2da776d | 2013-07-22 10:01:54 -0400 | [diff] [blame] | 309 | rdma_start_incoming_migration(p, errp); |
| 310 | #endif |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 311 | #if !defined(WIN32) |
Dr. David Alan Gilbert | adde220 | 2015-02-19 11:40:27 +0000 | [diff] [blame] | 312 | } else if (strstart(uri, "exec:", &p)) { |
Paolo Bonzini | 43eaae2 | 2012-10-02 18:21:18 +0200 | [diff] [blame] | 313 | exec_start_incoming_migration(p, errp); |
Dr. David Alan Gilbert | adde220 | 2015-02-19 11:40:27 +0000 | [diff] [blame] | 314 | } else if (strstart(uri, "unix:", &p)) { |
Paolo Bonzini | 43eaae2 | 2012-10-02 18:21:18 +0200 | [diff] [blame] | 315 | unix_start_incoming_migration(p, errp); |
Dr. David Alan Gilbert | adde220 | 2015-02-19 11:40:27 +0000 | [diff] [blame] | 316 | } else if (strstart(uri, "fd:", &p)) { |
Paolo Bonzini | 43eaae2 | 2012-10-02 18:21:18 +0200 | [diff] [blame] | 317 | fd_start_incoming_migration(p, errp); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 318 | #endif |
Dr. David Alan Gilbert | adde220 | 2015-02-19 11:40:27 +0000 | [diff] [blame] | 319 | } else { |
Markus Armbruster | 312fd5f | 2013-02-08 21:22:16 +0100 | [diff] [blame] | 320 | error_setg(errp, "unknown migration protocol: %s", uri); |
Juan Quintela | 8ca5e80 | 2010-06-09 14:10:54 +0200 | [diff] [blame] | 321 | } |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Paolo Bonzini | 82a4da7 | 2012-08-07 10:57:43 +0200 | [diff] [blame] | 324 | static void process_incoming_migration_co(void *opaque) |
Juan Quintela | 511c023 | 2010-06-09 14:10:55 +0200 | [diff] [blame] | 325 | { |
Paolo Bonzini | 82a4da7 | 2012-08-07 10:57:43 +0200 | [diff] [blame] | 326 | QEMUFile *f = opaque; |
Kevin Wolf | 5a8a30d | 2014-03-12 15:59:16 +0100 | [diff] [blame] | 327 | Error *local_err = NULL; |
Dr. David Alan Gilbert | e9bef23 | 2015-11-05 18:11:21 +0000 | [diff] [blame] | 328 | MigrationIncomingState *mis; |
| 329 | PostcopyState ps; |
Paolo Bonzini | 1c12e1f | 2012-08-07 10:51:51 +0200 | [diff] [blame] | 330 | int ret; |
| 331 | |
Dr. David Alan Gilbert | e9bef23 | 2015-11-05 18:11:21 +0000 | [diff] [blame] | 332 | mis = migration_incoming_state_new(f); |
Dr. David Alan Gilbert | 093e3c4 | 2015-11-05 18:10:52 +0000 | [diff] [blame] | 333 | postcopy_state_set(POSTCOPY_INCOMING_NONE); |
Juan Quintela | 7cf1fe6 | 2015-05-20 17:15:42 +0200 | [diff] [blame] | 334 | migrate_generate_event(MIGRATION_STATUS_ACTIVE); |
Dr. David Alan Gilbert | e9bef23 | 2015-11-05 18:11:21 +0000 | [diff] [blame] | 335 | |
Paolo Bonzini | 1c12e1f | 2012-08-07 10:51:51 +0200 | [diff] [blame] | 336 | ret = qemu_loadvm_state(f); |
Dr. David Alan Gilbert | bca7856 | 2015-05-21 13:24:14 +0100 | [diff] [blame] | 337 | |
Dr. David Alan Gilbert | e9bef23 | 2015-11-05 18:11:21 +0000 | [diff] [blame] | 338 | ps = postcopy_state_get(); |
| 339 | trace_process_incoming_migration_co_end(ret, ps); |
| 340 | if (ps != POSTCOPY_INCOMING_NONE) { |
| 341 | if (ps == POSTCOPY_INCOMING_ADVISE) { |
| 342 | /* |
| 343 | * Where a migration had postcopy enabled (and thus went to advise) |
| 344 | * but managed to complete within the precopy period, we can use |
| 345 | * the normal exit. |
| 346 | */ |
| 347 | postcopy_ram_incoming_cleanup(mis); |
| 348 | } else if (ret >= 0) { |
| 349 | /* |
| 350 | * Postcopy was started, cleanup should happen at the end of the |
| 351 | * postcopy thread. |
| 352 | */ |
| 353 | trace_process_incoming_migration_co_postcopy_end_main(); |
| 354 | return; |
| 355 | } |
| 356 | /* Else if something went wrong then just fall out of the normal exit */ |
| 357 | } |
| 358 | |
Paolo Bonzini | 1c12e1f | 2012-08-07 10:51:51 +0200 | [diff] [blame] | 359 | qemu_fclose(f); |
Gonglei (Arei) | 905f26f | 2014-01-30 20:08:35 +0200 | [diff] [blame] | 360 | free_xbzrle_decoded_buf(); |
Dr. David Alan Gilbert | bca7856 | 2015-05-21 13:24:14 +0100 | [diff] [blame] | 361 | migration_incoming_state_destroy(); |
| 362 | |
Paolo Bonzini | 1c12e1f | 2012-08-07 10:51:51 +0200 | [diff] [blame] | 363 | if (ret < 0) { |
Juan Quintela | 7cf1fe6 | 2015-05-20 17:15:42 +0200 | [diff] [blame] | 364 | migrate_generate_event(MIGRATION_STATUS_FAILED); |
Peter Lieven | db80fac | 2014-06-10 11:29:16 +0200 | [diff] [blame] | 365 | error_report("load of migration failed: %s", strerror(-ret)); |
Liang Li | 3fcb38c | 2015-03-23 16:32:18 +0800 | [diff] [blame] | 366 | migrate_decompress_threads_join(); |
Eric Blake | 4aead69 | 2013-04-16 15:50:41 -0600 | [diff] [blame] | 367 | exit(EXIT_FAILURE); |
Juan Quintela | 511c023 | 2010-06-09 14:10:55 +0200 | [diff] [blame] | 368 | } |
Juan Quintela | 511c023 | 2010-06-09 14:10:55 +0200 | [diff] [blame] | 369 | |
Anthony Liguori | 0f15423 | 2011-11-14 15:09:45 -0600 | [diff] [blame] | 370 | /* Make sure all file formats flush their mutable metadata */ |
Kevin Wolf | 5a8a30d | 2014-03-12 15:59:16 +0100 | [diff] [blame] | 371 | bdrv_invalidate_cache_all(&local_err); |
| 372 | if (local_err) { |
Dr. David Alan Gilbert | ed1f3e0 | 2015-10-13 12:21:27 +0100 | [diff] [blame] | 373 | migrate_generate_event(MIGRATION_STATUS_FAILED); |
Markus Armbruster | 97baf9d | 2015-02-18 19:21:52 +0100 | [diff] [blame] | 374 | error_report_err(local_err); |
Liang Li | 3fcb38c | 2015-03-23 16:32:18 +0800 | [diff] [blame] | 375 | migrate_decompress_threads_join(); |
Kevin Wolf | 5a8a30d | 2014-03-12 15:59:16 +0100 | [diff] [blame] | 376 | exit(EXIT_FAILURE); |
| 377 | } |
Anthony Liguori | 0f15423 | 2011-11-14 15:09:45 -0600 | [diff] [blame] | 378 | |
Amit Shah | 92e3762 | 2015-10-14 17:37:19 +0530 | [diff] [blame] | 379 | /* |
| 380 | * This must happen after all error conditions are dealt with and |
| 381 | * we're sure the VM is going to be running on this host. |
| 382 | */ |
| 383 | qemu_announce_self(); |
| 384 | |
Juan Quintela | 172c435 | 2015-07-08 13:56:26 +0200 | [diff] [blame] | 385 | /* If global state section was not received or we are in running |
| 386 | state, we need to obey autostart. Any other state is set with |
| 387 | runstate_set. */ |
Juan Quintela | df4b102 | 2014-10-08 10:58:10 +0200 | [diff] [blame] | 388 | |
Juan Quintela | 172c435 | 2015-07-08 13:56:26 +0200 | [diff] [blame] | 389 | if (!global_state_received() || |
| 390 | global_state_get_runstate() == RUN_STATE_RUNNING) { |
Juan Quintela | df4b102 | 2014-10-08 10:58:10 +0200 | [diff] [blame] | 391 | if (autostart) { |
| 392 | vm_start(); |
| 393 | } else { |
| 394 | runstate_set(RUN_STATE_PAUSED); |
| 395 | } |
Juan Quintela | 172c435 | 2015-07-08 13:56:26 +0200 | [diff] [blame] | 396 | } else { |
| 397 | runstate_set(global_state_get_runstate()); |
Luiz Capitulino | f5bbfba | 2011-07-29 15:04:45 -0300 | [diff] [blame] | 398 | } |
Liang Li | 3fcb38c | 2015-03-23 16:32:18 +0800 | [diff] [blame] | 399 | migrate_decompress_threads_join(); |
Dr. David Alan Gilbert | ed1f3e0 | 2015-10-13 12:21:27 +0100 | [diff] [blame] | 400 | /* |
| 401 | * This must happen after any state changes since as soon as an external |
| 402 | * observer sees this event they might start to prod at the VM assuming |
| 403 | * it's ready to use. |
| 404 | */ |
| 405 | migrate_generate_event(MIGRATION_STATUS_COMPLETED); |
Juan Quintela | 511c023 | 2010-06-09 14:10:55 +0200 | [diff] [blame] | 406 | } |
| 407 | |
Paolo Bonzini | 82a4da7 | 2012-08-07 10:57:43 +0200 | [diff] [blame] | 408 | void process_incoming_migration(QEMUFile *f) |
| 409 | { |
| 410 | Coroutine *co = qemu_coroutine_create(process_incoming_migration_co); |
| 411 | int fd = qemu_get_fd(f); |
| 412 | |
| 413 | assert(fd != -1); |
Liang Li | 3fcb38c | 2015-03-23 16:32:18 +0800 | [diff] [blame] | 414 | migrate_decompress_threads_create(); |
Stefan Hajnoczi | f9e8cac | 2013-03-27 10:10:43 +0100 | [diff] [blame] | 415 | qemu_set_nonblock(fd); |
Paolo Bonzini | 82a4da7 | 2012-08-07 10:57:43 +0200 | [diff] [blame] | 416 | qemu_coroutine_enter(co, f); |
| 417 | } |
| 418 | |
Dr. David Alan Gilbert | 6decec9 | 2015-11-05 18:10:47 +0000 | [diff] [blame] | 419 | /* |
| 420 | * Send a message on the return channel back to the source |
| 421 | * of the migration. |
| 422 | */ |
| 423 | void migrate_send_rp_message(MigrationIncomingState *mis, |
| 424 | enum mig_rp_message_type message_type, |
| 425 | uint16_t len, void *data) |
| 426 | { |
| 427 | trace_migrate_send_rp_message((int)message_type, len); |
| 428 | qemu_mutex_lock(&mis->rp_mutex); |
| 429 | qemu_put_be16(mis->to_src_file, (unsigned int)message_type); |
| 430 | qemu_put_be16(mis->to_src_file, len); |
| 431 | qemu_put_buffer(mis->to_src_file, data, len); |
| 432 | qemu_fflush(mis->to_src_file); |
| 433 | qemu_mutex_unlock(&mis->rp_mutex); |
| 434 | } |
| 435 | |
| 436 | /* |
| 437 | * Send a 'SHUT' message on the return channel with the given value |
| 438 | * to indicate that we've finished with the RP. Non-0 value indicates |
| 439 | * error. |
| 440 | */ |
| 441 | void migrate_send_rp_shut(MigrationIncomingState *mis, |
| 442 | uint32_t value) |
| 443 | { |
| 444 | uint32_t buf; |
| 445 | |
| 446 | buf = cpu_to_be32(value); |
| 447 | migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf); |
| 448 | } |
| 449 | |
| 450 | /* |
| 451 | * Send a 'PONG' message on the return channel with the given value |
| 452 | * (normally in response to a 'PING') |
| 453 | */ |
| 454 | void migrate_send_rp_pong(MigrationIncomingState *mis, |
| 455 | uint32_t value) |
| 456 | { |
| 457 | uint32_t buf; |
| 458 | |
| 459 | buf = cpu_to_be32(value); |
| 460 | migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf); |
| 461 | } |
| 462 | |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 463 | /* amount of nanoseconds we are willing to wait for migration to be down. |
| 464 | * the choice of nanoseconds is because it is the maximum resolution that |
| 465 | * get_clock() can achieve. It is an internal measure. All user-visible |
| 466 | * units must be in seconds */ |
Alexey Kardashevskiy | f7cd55a | 2014-03-27 14:57:26 +1100 | [diff] [blame] | 467 | static uint64_t max_downtime = 300000000; |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 468 | |
| 469 | uint64_t migrate_max_downtime(void) |
| 470 | { |
| 471 | return max_downtime; |
| 472 | } |
| 473 | |
Orit Wasserman | bbf6da3 | 2012-08-06 21:42:47 +0300 | [diff] [blame] | 474 | MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp) |
| 475 | { |
| 476 | MigrationCapabilityStatusList *head = NULL; |
| 477 | MigrationCapabilityStatusList *caps; |
| 478 | MigrationState *s = migrate_get_current(); |
| 479 | int i; |
| 480 | |
Michael Tokarev | 387eede | 2013-10-05 13:18:28 +0400 | [diff] [blame] | 481 | caps = NULL; /* silence compiler warning */ |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 482 | for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) { |
Orit Wasserman | bbf6da3 | 2012-08-06 21:42:47 +0300 | [diff] [blame] | 483 | if (head == NULL) { |
| 484 | head = g_malloc0(sizeof(*caps)); |
| 485 | caps = head; |
| 486 | } else { |
| 487 | caps->next = g_malloc0(sizeof(*caps)); |
| 488 | caps = caps->next; |
| 489 | } |
| 490 | caps->value = |
| 491 | g_malloc(sizeof(*caps->value)); |
| 492 | caps->value->capability = i; |
| 493 | caps->value->state = s->enabled_capabilities[i]; |
| 494 | } |
| 495 | |
| 496 | return head; |
| 497 | } |
| 498 | |
Liang Li | 85de832 | 2015-03-23 16:32:28 +0800 | [diff] [blame] | 499 | MigrationParameters *qmp_query_migrate_parameters(Error **errp) |
| 500 | { |
| 501 | MigrationParameters *params; |
| 502 | MigrationState *s = migrate_get_current(); |
| 503 | |
| 504 | params = g_malloc0(sizeof(*params)); |
| 505 | params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL]; |
| 506 | params->compress_threads = |
| 507 | s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS]; |
| 508 | params->decompress_threads = |
| 509 | s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS]; |
Jason J. Herne | 1626fee | 2015-09-08 13:12:34 -0400 | [diff] [blame] | 510 | params->x_cpu_throttle_initial = |
| 511 | s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL]; |
| 512 | params->x_cpu_throttle_increment = |
| 513 | s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT]; |
Liang Li | 85de832 | 2015-03-23 16:32:28 +0800 | [diff] [blame] | 514 | |
| 515 | return params; |
| 516 | } |
| 517 | |
Dr. David Alan Gilbert | f6844b9 | 2015-11-05 18:10:48 +0000 | [diff] [blame] | 518 | /* |
| 519 | * Return true if we're already in the middle of a migration |
| 520 | * (i.e. any of the active or setup states) |
| 521 | */ |
| 522 | static bool migration_is_setup_or_active(int state) |
| 523 | { |
| 524 | switch (state) { |
| 525 | case MIGRATION_STATUS_ACTIVE: |
Dr. David Alan Gilbert | 9ec055a | 2015-11-05 18:10:58 +0000 | [diff] [blame] | 526 | case MIGRATION_STATUS_POSTCOPY_ACTIVE: |
Dr. David Alan Gilbert | f6844b9 | 2015-11-05 18:10:48 +0000 | [diff] [blame] | 527 | case MIGRATION_STATUS_SETUP: |
| 528 | return true; |
| 529 | |
| 530 | default: |
| 531 | return false; |
| 532 | |
| 533 | } |
| 534 | } |
| 535 | |
Orit Wasserman | f36d55a | 2012-08-06 21:42:57 +0300 | [diff] [blame] | 536 | static void get_xbzrle_cache_stats(MigrationInfo *info) |
| 537 | { |
| 538 | if (migrate_use_xbzrle()) { |
| 539 | info->has_xbzrle_cache = true; |
| 540 | info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache)); |
| 541 | info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size(); |
| 542 | info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred(); |
| 543 | info->xbzrle_cache->pages = xbzrle_mig_pages_transferred(); |
| 544 | info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss(); |
ChenLiang | 8bc3923 | 2014-04-04 17:57:56 +0800 | [diff] [blame] | 545 | info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate(); |
Orit Wasserman | f36d55a | 2012-08-06 21:42:57 +0300 | [diff] [blame] | 546 | info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow(); |
| 547 | } |
| 548 | } |
| 549 | |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 550 | MigrationInfo *qmp_query_migrate(Error **errp) |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 551 | { |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 552 | MigrationInfo *info = g_malloc0(sizeof(*info)); |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 553 | MigrationState *s = migrate_get_current(); |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 554 | |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 555 | switch (s->state) { |
zhanghailiang | 3119473 | 2015-03-13 16:08:38 +0800 | [diff] [blame] | 556 | case MIGRATION_STATUS_NONE: |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 557 | /* no migration has happened ever */ |
| 558 | break; |
zhanghailiang | 3119473 | 2015-03-13 16:08:38 +0800 | [diff] [blame] | 559 | case MIGRATION_STATUS_SETUP: |
Michael R. Hines | 29ae8a4 | 2013-07-22 10:01:57 -0400 | [diff] [blame] | 560 | info->has_status = true; |
Michael R. Hines | ed4fbd1 | 2013-07-22 10:01:58 -0400 | [diff] [blame] | 561 | info->has_total_time = false; |
Michael R. Hines | 29ae8a4 | 2013-07-22 10:01:57 -0400 | [diff] [blame] | 562 | break; |
zhanghailiang | 3119473 | 2015-03-13 16:08:38 +0800 | [diff] [blame] | 563 | case MIGRATION_STATUS_ACTIVE: |
| 564 | case MIGRATION_STATUS_CANCELLING: |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 565 | info->has_status = true; |
Juan Quintela | 7aa939a | 2012-08-18 13:17:10 +0200 | [diff] [blame] | 566 | info->has_total_time = true; |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 567 | info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) |
Juan Quintela | 7aa939a | 2012-08-18 13:17:10 +0200 | [diff] [blame] | 568 | - s->total_time; |
Juan Quintela | 2c52ddf | 2012-08-13 09:53:12 +0200 | [diff] [blame] | 569 | info->has_expected_downtime = true; |
| 570 | info->expected_downtime = s->expected_downtime; |
Michael R. Hines | ed4fbd1 | 2013-07-22 10:01:58 -0400 | [diff] [blame] | 571 | info->has_setup_time = true; |
| 572 | info->setup_time = s->setup_time; |
Luiz Capitulino | c86a668 | 2009-12-10 17:16:05 -0200 | [diff] [blame] | 573 | |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 574 | info->has_ram = true; |
| 575 | info->ram = g_malloc0(sizeof(*info->ram)); |
| 576 | info->ram->transferred = ram_bytes_transferred(); |
| 577 | info->ram->remaining = ram_bytes_remaining(); |
| 578 | info->ram->total = ram_bytes_total(); |
Orit Wasserman | 004d4c1 | 2012-08-06 21:42:56 +0300 | [diff] [blame] | 579 | info->ram->duplicate = dup_mig_pages_transferred(); |
Peter Lieven | f1c7279 | 2013-03-26 10:58:37 +0100 | [diff] [blame] | 580 | info->ram->skipped = skipped_mig_pages_transferred(); |
Orit Wasserman | 004d4c1 | 2012-08-06 21:42:56 +0300 | [diff] [blame] | 581 | info->ram->normal = norm_mig_pages_transferred(); |
| 582 | info->ram->normal_bytes = norm_mig_bytes_transferred(); |
Juan Quintela | 8d01719 | 2012-08-13 12:31:25 +0200 | [diff] [blame] | 583 | info->ram->dirty_pages_rate = s->dirty_pages_rate; |
Michael R. Hines | 7e114f8 | 2013-06-25 21:35:30 -0400 | [diff] [blame] | 584 | info->ram->mbps = s->mbps; |
ChenLiang | 58570ed | 2014-04-04 17:57:55 +0800 | [diff] [blame] | 585 | info->ram->dirty_sync_count = s->dirty_sync_count; |
Juan Quintela | 8d01719 | 2012-08-13 12:31:25 +0200 | [diff] [blame] | 586 | |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 587 | if (blk_mig_active()) { |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 588 | info->has_disk = true; |
| 589 | info->disk = g_malloc0(sizeof(*info->disk)); |
| 590 | info->disk->transferred = blk_mig_bytes_transferred(); |
| 591 | info->disk->remaining = blk_mig_bytes_remaining(); |
| 592 | info->disk->total = blk_mig_bytes_total(); |
aliguori | ff8d81d | 2008-10-24 22:10:31 +0000 | [diff] [blame] | 593 | } |
Orit Wasserman | f36d55a | 2012-08-06 21:42:57 +0300 | [diff] [blame] | 594 | |
Jason J. Herne | 4782893 | 2015-09-08 13:12:36 -0400 | [diff] [blame] | 595 | if (cpu_throttle_active()) { |
| 596 | info->has_x_cpu_throttle_percentage = true; |
| 597 | info->x_cpu_throttle_percentage = cpu_throttle_get_percentage(); |
| 598 | } |
| 599 | |
Orit Wasserman | f36d55a | 2012-08-06 21:42:57 +0300 | [diff] [blame] | 600 | get_xbzrle_cache_stats(info); |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 601 | break; |
Dr. David Alan Gilbert | 9ec055a | 2015-11-05 18:10:58 +0000 | [diff] [blame] | 602 | case MIGRATION_STATUS_POSTCOPY_ACTIVE: |
| 603 | /* Mostly the same as active; TODO add some postcopy stats */ |
| 604 | info->has_status = true; |
| 605 | info->has_total_time = true; |
| 606 | info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) |
| 607 | - s->total_time; |
| 608 | info->has_expected_downtime = true; |
| 609 | info->expected_downtime = s->expected_downtime; |
| 610 | info->has_setup_time = true; |
| 611 | info->setup_time = s->setup_time; |
| 612 | |
| 613 | info->has_ram = true; |
| 614 | info->ram = g_malloc0(sizeof(*info->ram)); |
| 615 | info->ram->transferred = ram_bytes_transferred(); |
| 616 | info->ram->remaining = ram_bytes_remaining(); |
| 617 | info->ram->total = ram_bytes_total(); |
| 618 | info->ram->duplicate = dup_mig_pages_transferred(); |
| 619 | info->ram->skipped = skipped_mig_pages_transferred(); |
| 620 | info->ram->normal = norm_mig_pages_transferred(); |
| 621 | info->ram->normal_bytes = norm_mig_bytes_transferred(); |
| 622 | info->ram->dirty_pages_rate = s->dirty_pages_rate; |
| 623 | info->ram->mbps = s->mbps; |
| 624 | |
| 625 | if (blk_mig_active()) { |
| 626 | info->has_disk = true; |
| 627 | info->disk = g_malloc0(sizeof(*info->disk)); |
| 628 | info->disk->transferred = blk_mig_bytes_transferred(); |
| 629 | info->disk->remaining = blk_mig_bytes_remaining(); |
| 630 | info->disk->total = blk_mig_bytes_total(); |
| 631 | } |
| 632 | |
| 633 | get_xbzrle_cache_stats(info); |
| 634 | break; |
zhanghailiang | 3119473 | 2015-03-13 16:08:38 +0800 | [diff] [blame] | 635 | case MIGRATION_STATUS_COMPLETED: |
Orit Wasserman | f36d55a | 2012-08-06 21:42:57 +0300 | [diff] [blame] | 636 | get_xbzrle_cache_stats(info); |
| 637 | |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 638 | info->has_status = true; |
Pawit Pornkitprasan | 00c1499 | 2013-07-19 11:23:45 +0900 | [diff] [blame] | 639 | info->has_total_time = true; |
Juan Quintela | 7aa939a | 2012-08-18 13:17:10 +0200 | [diff] [blame] | 640 | info->total_time = s->total_time; |
Juan Quintela | 9c5a9fc | 2012-08-13 09:35:16 +0200 | [diff] [blame] | 641 | info->has_downtime = true; |
| 642 | info->downtime = s->downtime; |
Michael R. Hines | ed4fbd1 | 2013-07-22 10:01:58 -0400 | [diff] [blame] | 643 | info->has_setup_time = true; |
| 644 | info->setup_time = s->setup_time; |
Juan Quintela | d5f8a57 | 2012-05-21 22:01:07 +0200 | [diff] [blame] | 645 | |
| 646 | info->has_ram = true; |
| 647 | info->ram = g_malloc0(sizeof(*info->ram)); |
| 648 | info->ram->transferred = ram_bytes_transferred(); |
| 649 | info->ram->remaining = 0; |
| 650 | info->ram->total = ram_bytes_total(); |
Orit Wasserman | 004d4c1 | 2012-08-06 21:42:56 +0300 | [diff] [blame] | 651 | info->ram->duplicate = dup_mig_pages_transferred(); |
Peter Lieven | f1c7279 | 2013-03-26 10:58:37 +0100 | [diff] [blame] | 652 | info->ram->skipped = skipped_mig_pages_transferred(); |
Orit Wasserman | 004d4c1 | 2012-08-06 21:42:56 +0300 | [diff] [blame] | 653 | info->ram->normal = norm_mig_pages_transferred(); |
| 654 | info->ram->normal_bytes = norm_mig_bytes_transferred(); |
Michael R. Hines | 7e114f8 | 2013-06-25 21:35:30 -0400 | [diff] [blame] | 655 | info->ram->mbps = s->mbps; |
ChenLiang | 58570ed | 2014-04-04 17:57:55 +0800 | [diff] [blame] | 656 | info->ram->dirty_sync_count = s->dirty_sync_count; |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 657 | break; |
zhanghailiang | 3119473 | 2015-03-13 16:08:38 +0800 | [diff] [blame] | 658 | case MIGRATION_STATUS_FAILED: |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 659 | info->has_status = true; |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 660 | break; |
zhanghailiang | 3119473 | 2015-03-13 16:08:38 +0800 | [diff] [blame] | 661 | case MIGRATION_STATUS_CANCELLED: |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 662 | info->has_status = true; |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 663 | break; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 664 | } |
zhanghailiang | cde63fb | 2015-03-13 16:08:41 +0800 | [diff] [blame] | 665 | info->status = s->state; |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 666 | |
| 667 | return info; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Orit Wasserman | 0045843 | 2012-08-06 21:42:48 +0300 | [diff] [blame] | 670 | void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params, |
| 671 | Error **errp) |
| 672 | { |
| 673 | MigrationState *s = migrate_get_current(); |
| 674 | MigrationCapabilityStatusList *cap; |
| 675 | |
Dr. David Alan Gilbert | f6844b9 | 2015-11-05 18:10:48 +0000 | [diff] [blame] | 676 | if (migration_is_setup_or_active(s->state)) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 677 | error_setg(errp, QERR_MIGRATION_ACTIVE); |
Orit Wasserman | 0045843 | 2012-08-06 21:42:48 +0300 | [diff] [blame] | 678 | return; |
| 679 | } |
| 680 | |
| 681 | for (cap = params; cap; cap = cap->next) { |
| 682 | s->enabled_capabilities[cap->value->capability] = cap->value->state; |
| 683 | } |
Dr. David Alan Gilbert | 53dd370 | 2015-11-05 18:10:51 +0000 | [diff] [blame] | 684 | |
| 685 | if (migrate_postcopy_ram()) { |
| 686 | if (migrate_use_compression()) { |
| 687 | /* The decompression threads asynchronously write into RAM |
| 688 | * rather than use the atomic copies needed to avoid |
| 689 | * userfaulting. It should be possible to fix the decompression |
| 690 | * threads for compatibility in future. |
| 691 | */ |
| 692 | error_report("Postcopy is not currently compatible with " |
| 693 | "compression"); |
| 694 | s->enabled_capabilities[MIGRATION_CAPABILITY_X_POSTCOPY_RAM] = |
| 695 | false; |
| 696 | } |
| 697 | } |
Orit Wasserman | 0045843 | 2012-08-06 21:42:48 +0300 | [diff] [blame] | 698 | } |
| 699 | |
Liang Li | 85de832 | 2015-03-23 16:32:28 +0800 | [diff] [blame] | 700 | void qmp_migrate_set_parameters(bool has_compress_level, |
| 701 | int64_t compress_level, |
| 702 | bool has_compress_threads, |
| 703 | int64_t compress_threads, |
| 704 | bool has_decompress_threads, |
Jason J. Herne | 1626fee | 2015-09-08 13:12:34 -0400 | [diff] [blame] | 705 | int64_t decompress_threads, |
| 706 | bool has_x_cpu_throttle_initial, |
| 707 | int64_t x_cpu_throttle_initial, |
| 708 | bool has_x_cpu_throttle_increment, |
| 709 | int64_t x_cpu_throttle_increment, Error **errp) |
Liang Li | 85de832 | 2015-03-23 16:32:28 +0800 | [diff] [blame] | 710 | { |
| 711 | MigrationState *s = migrate_get_current(); |
| 712 | |
| 713 | if (has_compress_level && (compress_level < 0 || compress_level > 9)) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 714 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level", |
| 715 | "is invalid, it should be in the range of 0 to 9"); |
Liang Li | 85de832 | 2015-03-23 16:32:28 +0800 | [diff] [blame] | 716 | return; |
| 717 | } |
| 718 | if (has_compress_threads && |
| 719 | (compress_threads < 1 || compress_threads > 255)) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 720 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
| 721 | "compress_threads", |
| 722 | "is invalid, it should be in the range of 1 to 255"); |
Liang Li | 85de832 | 2015-03-23 16:32:28 +0800 | [diff] [blame] | 723 | return; |
| 724 | } |
| 725 | if (has_decompress_threads && |
| 726 | (decompress_threads < 1 || decompress_threads > 255)) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 727 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
| 728 | "decompress_threads", |
| 729 | "is invalid, it should be in the range of 1 to 255"); |
Liang Li | 85de832 | 2015-03-23 16:32:28 +0800 | [diff] [blame] | 730 | return; |
| 731 | } |
Jason J. Herne | 1626fee | 2015-09-08 13:12:34 -0400 | [diff] [blame] | 732 | if (has_x_cpu_throttle_initial && |
| 733 | (x_cpu_throttle_initial < 1 || x_cpu_throttle_initial > 99)) { |
| 734 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
| 735 | "x_cpu_throttle_initial", |
| 736 | "an integer in the range of 1 to 99"); |
| 737 | } |
| 738 | if (has_x_cpu_throttle_increment && |
| 739 | (x_cpu_throttle_increment < 1 || x_cpu_throttle_increment > 99)) { |
| 740 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
| 741 | "x_cpu_throttle_increment", |
| 742 | "an integer in the range of 1 to 99"); |
| 743 | } |
Liang Li | 85de832 | 2015-03-23 16:32:28 +0800 | [diff] [blame] | 744 | |
| 745 | if (has_compress_level) { |
| 746 | s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level; |
| 747 | } |
| 748 | if (has_compress_threads) { |
| 749 | s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads; |
| 750 | } |
| 751 | if (has_decompress_threads) { |
| 752 | s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] = |
| 753 | decompress_threads; |
| 754 | } |
Jason J. Herne | 1626fee | 2015-09-08 13:12:34 -0400 | [diff] [blame] | 755 | if (has_x_cpu_throttle_initial) { |
| 756 | s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] = |
| 757 | x_cpu_throttle_initial; |
| 758 | } |
| 759 | |
| 760 | if (has_x_cpu_throttle_increment) { |
| 761 | s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] = |
| 762 | x_cpu_throttle_increment; |
| 763 | } |
Liang Li | 85de832 | 2015-03-23 16:32:28 +0800 | [diff] [blame] | 764 | } |
| 765 | |
Dr. David Alan Gilbert | 4886a1b | 2015-11-05 18:10:56 +0000 | [diff] [blame] | 766 | void qmp_migrate_start_postcopy(Error **errp) |
| 767 | { |
| 768 | MigrationState *s = migrate_get_current(); |
| 769 | |
| 770 | if (!migrate_postcopy_ram()) { |
Dr. David Alan Gilbert | a54d340 | 2015-11-12 11:34:44 +0000 | [diff] [blame] | 771 | error_setg(errp, "Enable postcopy with migrate_set_capability before" |
Dr. David Alan Gilbert | 4886a1b | 2015-11-05 18:10:56 +0000 | [diff] [blame] | 772 | " the start of migration"); |
| 773 | return; |
| 774 | } |
| 775 | |
| 776 | if (s->state == MIGRATION_STATUS_NONE) { |
| 777 | error_setg(errp, "Postcopy must be started after migration has been" |
| 778 | " started"); |
| 779 | return; |
| 780 | } |
| 781 | /* |
| 782 | * we don't error if migration has finished since that would be racy |
| 783 | * with issuing this command. |
| 784 | */ |
| 785 | atomic_set(&s->start_postcopy, true); |
| 786 | } |
| 787 | |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 788 | /* shared migration helpers */ |
| 789 | |
zhanghailiang | 48781e5 | 2015-12-16 11:47:33 +0000 | [diff] [blame^] | 790 | void migrate_set_state(int *state, int old_state, int new_state) |
Zhanghaoyu (A) | 51cf4c1 | 2013-11-07 11:01:15 +0000 | [diff] [blame] | 791 | { |
zhanghailiang | 48781e5 | 2015-12-16 11:47:33 +0000 | [diff] [blame^] | 792 | if (atomic_cmpxchg(state, old_state, new_state) == old_state) { |
Juan Quintela | 4ba4bc5 | 2015-07-08 13:58:27 +0200 | [diff] [blame] | 793 | trace_migrate_set_state(new_state); |
Juan Quintela | b05dc72 | 2015-07-07 14:44:05 +0200 | [diff] [blame] | 794 | migrate_generate_event(new_state); |
Zhanghaoyu (A) | 51cf4c1 | 2013-11-07 11:01:15 +0000 | [diff] [blame] | 795 | } |
| 796 | } |
| 797 | |
Paolo Bonzini | bb1fadc | 2013-02-22 17:36:21 +0100 | [diff] [blame] | 798 | static void migrate_fd_cleanup(void *opaque) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 799 | { |
Paolo Bonzini | bb1fadc | 2013-02-22 17:36:21 +0100 | [diff] [blame] | 800 | MigrationState *s = opaque; |
| 801 | |
| 802 | qemu_bh_delete(s->cleanup_bh); |
| 803 | s->cleanup_bh = NULL; |
| 804 | |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 805 | flush_page_queue(s); |
| 806 | |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 807 | if (s->file) { |
Alexey Kardashevskiy | 9013dca | 2014-03-11 10:42:29 +1100 | [diff] [blame] | 808 | trace_migrate_fd_cleanup(); |
Paolo Bonzini | 404a7c0 | 2013-02-22 17:36:46 +0100 | [diff] [blame] | 809 | qemu_mutex_unlock_iothread(); |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 810 | if (s->migration_thread_running) { |
| 811 | qemu_thread_join(&s->thread); |
| 812 | s->migration_thread_running = false; |
| 813 | } |
Paolo Bonzini | 404a7c0 | 2013-02-22 17:36:46 +0100 | [diff] [blame] | 814 | qemu_mutex_lock_iothread(); |
| 815 | |
Liang Li | 8706d2d | 2015-03-23 16:32:17 +0800 | [diff] [blame] | 816 | migrate_compress_threads_join(); |
Paolo Bonzini | 6f190a0 | 2013-02-22 17:36:48 +0100 | [diff] [blame] | 817 | qemu_fclose(s->file); |
| 818 | s->file = NULL; |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Dr. David Alan Gilbert | 9ec055a | 2015-11-05 18:10:58 +0000 | [diff] [blame] | 821 | assert((s->state != MIGRATION_STATUS_ACTIVE) && |
| 822 | (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE)); |
Paolo Bonzini | 7a2c172 | 2013-02-22 17:36:09 +0100 | [diff] [blame] | 823 | |
Liang Li | 94f5a43 | 2015-11-02 15:37:00 +0800 | [diff] [blame] | 824 | if (s->state == MIGRATION_STATUS_CANCELLING) { |
zhanghailiang | 48781e5 | 2015-12-16 11:47:33 +0000 | [diff] [blame^] | 825 | migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING, |
Liang Li | 94f5a43 | 2015-11-02 15:37:00 +0800 | [diff] [blame] | 826 | MIGRATION_STATUS_CANCELLED); |
Paolo Bonzini | 7a2c172 | 2013-02-22 17:36:09 +0100 | [diff] [blame] | 827 | } |
Paolo Bonzini | a3fa1d7 | 2013-02-22 17:36:18 +0100 | [diff] [blame] | 828 | |
| 829 | notifier_list_notify(&migration_state_notifiers, s); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Juan Quintela | 8b6b99b | 2011-09-11 20:28:22 +0200 | [diff] [blame] | 832 | void migrate_fd_error(MigrationState *s) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 833 | { |
Alexey Kardashevskiy | 9013dca | 2014-03-11 10:42:29 +1100 | [diff] [blame] | 834 | trace_migrate_fd_error(); |
Paolo Bonzini | bb1fadc | 2013-02-22 17:36:21 +0100 | [diff] [blame] | 835 | assert(s->file == NULL); |
zhanghailiang | 48781e5 | 2015-12-16 11:47:33 +0000 | [diff] [blame^] | 836 | migrate_set_state(&s->state, MIGRATION_STATUS_SETUP, |
| 837 | MIGRATION_STATUS_FAILED); |
Paolo Bonzini | bb1fadc | 2013-02-22 17:36:21 +0100 | [diff] [blame] | 838 | notifier_list_notify(&migration_state_notifiers, s); |
Juan Quintela | 458cf28 | 2011-02-22 23:32:54 +0100 | [diff] [blame] | 839 | } |
| 840 | |
Juan Quintela | 0edda1c | 2010-05-11 16:28:39 +0200 | [diff] [blame] | 841 | static void migrate_fd_cancel(MigrationState *s) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 842 | { |
Zhanghaoyu (A) | 6f2b811 | 2013-11-07 08:21:23 +0000 | [diff] [blame] | 843 | int old_state ; |
Dr. David Alan Gilbert | a26ba26 | 2015-01-08 11:11:32 +0000 | [diff] [blame] | 844 | QEMUFile *f = migrate_get_current()->file; |
Alexey Kardashevskiy | 9013dca | 2014-03-11 10:42:29 +1100 | [diff] [blame] | 845 | trace_migrate_fd_cancel(); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 846 | |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 847 | if (s->rp_state.from_dst_file) { |
| 848 | /* shutdown the rp socket, so causing the rp thread to shutdown */ |
| 849 | qemu_file_shutdown(s->rp_state.from_dst_file); |
| 850 | } |
| 851 | |
Zhanghaoyu (A) | 6f2b811 | 2013-11-07 08:21:23 +0000 | [diff] [blame] | 852 | do { |
| 853 | old_state = s->state; |
Dr. David Alan Gilbert | f6844b9 | 2015-11-05 18:10:48 +0000 | [diff] [blame] | 854 | if (!migration_is_setup_or_active(old_state)) { |
Zhanghaoyu (A) | 6f2b811 | 2013-11-07 08:21:23 +0000 | [diff] [blame] | 855 | break; |
| 856 | } |
zhanghailiang | 48781e5 | 2015-12-16 11:47:33 +0000 | [diff] [blame^] | 857 | migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING); |
zhanghailiang | 3119473 | 2015-03-13 16:08:38 +0800 | [diff] [blame] | 858 | } while (s->state != MIGRATION_STATUS_CANCELLING); |
Dr. David Alan Gilbert | a26ba26 | 2015-01-08 11:11:32 +0000 | [diff] [blame] | 859 | |
| 860 | /* |
| 861 | * If we're unlucky the migration code might be stuck somewhere in a |
| 862 | * send/write while the network has failed and is waiting to timeout; |
| 863 | * if we've got shutdown(2) available then we can force it to quit. |
| 864 | * The outgoing qemu file gets closed in migrate_fd_cleanup that is |
| 865 | * called in a bh, so there is no race against this cancel. |
| 866 | */ |
zhanghailiang | 3119473 | 2015-03-13 16:08:38 +0800 | [diff] [blame] | 867 | if (s->state == MIGRATION_STATUS_CANCELLING && f) { |
Dr. David Alan Gilbert | a26ba26 | 2015-01-08 11:11:32 +0000 | [diff] [blame] | 868 | qemu_file_shutdown(f); |
| 869 | } |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Gerd Hoffmann | 99a0db9 | 2010-12-13 17:30:12 +0100 | [diff] [blame] | 872 | void add_migration_state_change_notifier(Notifier *notify) |
| 873 | { |
| 874 | notifier_list_add(&migration_state_notifiers, notify); |
| 875 | } |
| 876 | |
| 877 | void remove_migration_state_change_notifier(Notifier *notify) |
| 878 | { |
Paolo Bonzini | 3155252 | 2012-01-13 17:34:01 +0100 | [diff] [blame] | 879 | notifier_remove(notify); |
Gerd Hoffmann | 99a0db9 | 2010-12-13 17:30:12 +0100 | [diff] [blame] | 880 | } |
| 881 | |
Stefan Hajnoczi | 02edd2e | 2013-07-29 15:01:58 +0200 | [diff] [blame] | 882 | bool migration_in_setup(MigrationState *s) |
Gerd Hoffmann | afe2df6 | 2011-10-25 13:50:11 +0200 | [diff] [blame] | 883 | { |
zhanghailiang | 3119473 | 2015-03-13 16:08:38 +0800 | [diff] [blame] | 884 | return s->state == MIGRATION_STATUS_SETUP; |
Gerd Hoffmann | afe2df6 | 2011-10-25 13:50:11 +0200 | [diff] [blame] | 885 | } |
| 886 | |
Juan Quintela | 7073693 | 2011-02-23 00:43:59 +0100 | [diff] [blame] | 887 | bool migration_has_finished(MigrationState *s) |
Gerd Hoffmann | 99a0db9 | 2010-12-13 17:30:12 +0100 | [diff] [blame] | 888 | { |
zhanghailiang | 3119473 | 2015-03-13 16:08:38 +0800 | [diff] [blame] | 889 | return s->state == MIGRATION_STATUS_COMPLETED; |
Gerd Hoffmann | 99a0db9 | 2010-12-13 17:30:12 +0100 | [diff] [blame] | 890 | } |
Juan Quintela | 0edda1c | 2010-05-11 16:28:39 +0200 | [diff] [blame] | 891 | |
Gerd Hoffmann | afe2df6 | 2011-10-25 13:50:11 +0200 | [diff] [blame] | 892 | bool migration_has_failed(MigrationState *s) |
| 893 | { |
zhanghailiang | 3119473 | 2015-03-13 16:08:38 +0800 | [diff] [blame] | 894 | return (s->state == MIGRATION_STATUS_CANCELLED || |
| 895 | s->state == MIGRATION_STATUS_FAILED); |
Gerd Hoffmann | afe2df6 | 2011-10-25 13:50:11 +0200 | [diff] [blame] | 896 | } |
| 897 | |
Dr. David Alan Gilbert | 9ec055a | 2015-11-05 18:10:58 +0000 | [diff] [blame] | 898 | bool migration_in_postcopy(MigrationState *s) |
| 899 | { |
| 900 | return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE); |
| 901 | } |
| 902 | |
Dr. David Alan Gilbert | aefeb18 | 2015-11-05 18:10:40 +0000 | [diff] [blame] | 903 | MigrationState *migrate_init(const MigrationParams *params) |
Juan Quintela | 0edda1c | 2010-05-11 16:28:39 +0200 | [diff] [blame] | 904 | { |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 905 | MigrationState *s = migrate_get_current(); |
Orit Wasserman | bbf6da3 | 2012-08-06 21:42:47 +0300 | [diff] [blame] | 906 | |
Dr. David Alan Gilbert | 389775d | 2015-11-12 15:38:27 +0000 | [diff] [blame] | 907 | /* |
| 908 | * Reinitialise all migration state, except |
| 909 | * parameters/capabilities that the user set, and |
| 910 | * locks. |
| 911 | */ |
| 912 | s->bytes_xfer = 0; |
| 913 | s->xfer_limit = 0; |
| 914 | s->cleanup_bh = 0; |
| 915 | s->file = NULL; |
| 916 | s->state = MIGRATION_STATUS_NONE; |
Isaku Yamahata | 6607ae2 | 2012-06-19 18:43:09 +0300 | [diff] [blame] | 917 | s->params = *params; |
Dr. David Alan Gilbert | 389775d | 2015-11-12 15:38:27 +0000 | [diff] [blame] | 918 | s->rp_state.from_dst_file = NULL; |
| 919 | s->rp_state.error = false; |
| 920 | s->mbps = 0.0; |
| 921 | s->downtime = 0; |
| 922 | s->expected_downtime = 0; |
| 923 | s->dirty_pages_rate = 0; |
| 924 | s->dirty_bytes_rate = 0; |
| 925 | s->setup_time = 0; |
| 926 | s->dirty_sync_count = 0; |
| 927 | s->start_postcopy = false; |
| 928 | s->migration_thread_running = false; |
| 929 | s->last_req_rb = NULL; |
Juan Quintela | 1299c63 | 2011-11-09 21:29:01 +0100 | [diff] [blame] | 930 | |
zhanghailiang | 48781e5 | 2015-12-16 11:47:33 +0000 | [diff] [blame^] | 931 | migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP); |
Juan Quintela | 0edda1c | 2010-05-11 16:28:39 +0200 | [diff] [blame] | 932 | |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 933 | QSIMPLEQ_INIT(&s->src_page_requests); |
| 934 | |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 935 | s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
Juan Quintela | 0edda1c | 2010-05-11 16:28:39 +0200 | [diff] [blame] | 936 | return s; |
| 937 | } |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 938 | |
Anthony Liguori | fa2756b | 2011-11-14 15:09:43 -0600 | [diff] [blame] | 939 | static GSList *migration_blockers; |
| 940 | |
| 941 | void migrate_add_blocker(Error *reason) |
| 942 | { |
| 943 | migration_blockers = g_slist_prepend(migration_blockers, reason); |
| 944 | } |
| 945 | |
| 946 | void migrate_del_blocker(Error *reason) |
| 947 | { |
| 948 | migration_blockers = g_slist_remove(migration_blockers, reason); |
| 949 | } |
| 950 | |
Dr. David Alan Gilbert | bf1ae1f | 2015-02-19 11:40:28 +0000 | [diff] [blame] | 951 | void qmp_migrate_incoming(const char *uri, Error **errp) |
| 952 | { |
| 953 | Error *local_err = NULL; |
Dr. David Alan Gilbert | 4debb5f | 2015-02-26 14:54:41 +0000 | [diff] [blame] | 954 | static bool once = true; |
Dr. David Alan Gilbert | bf1ae1f | 2015-02-19 11:40:28 +0000 | [diff] [blame] | 955 | |
| 956 | if (!deferred_incoming) { |
Dr. David Alan Gilbert | 4debb5f | 2015-02-26 14:54:41 +0000 | [diff] [blame] | 957 | error_setg(errp, "For use with '-incoming defer'"); |
Dr. David Alan Gilbert | bf1ae1f | 2015-02-19 11:40:28 +0000 | [diff] [blame] | 958 | return; |
| 959 | } |
Dr. David Alan Gilbert | 4debb5f | 2015-02-26 14:54:41 +0000 | [diff] [blame] | 960 | if (!once) { |
| 961 | error_setg(errp, "The incoming migration has already been started"); |
| 962 | } |
Dr. David Alan Gilbert | bf1ae1f | 2015-02-19 11:40:28 +0000 | [diff] [blame] | 963 | |
| 964 | qemu_start_incoming_migration(uri, &local_err); |
| 965 | |
| 966 | if (local_err) { |
| 967 | error_propagate(errp, local_err); |
| 968 | return; |
| 969 | } |
| 970 | |
Dr. David Alan Gilbert | 4debb5f | 2015-02-26 14:54:41 +0000 | [diff] [blame] | 971 | once = false; |
Dr. David Alan Gilbert | bf1ae1f | 2015-02-19 11:40:28 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 974 | void qmp_migrate(const char *uri, bool has_blk, bool blk, |
| 975 | bool has_inc, bool inc, bool has_detach, bool detach, |
| 976 | Error **errp) |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 977 | { |
Paolo Bonzini | be7059c | 2012-10-03 14:34:33 +0200 | [diff] [blame] | 978 | Error *local_err = NULL; |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 979 | MigrationState *s = migrate_get_current(); |
Isaku Yamahata | 6607ae2 | 2012-06-19 18:43:09 +0300 | [diff] [blame] | 980 | MigrationParams params; |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 981 | const char *p; |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 982 | |
Pawit Pornkitprasan | 8c0426a | 2013-07-30 08:39:52 +0900 | [diff] [blame] | 983 | params.blk = has_blk && blk; |
| 984 | params.shared = has_inc && inc; |
Isaku Yamahata | 6607ae2 | 2012-06-19 18:43:09 +0300 | [diff] [blame] | 985 | |
Dr. David Alan Gilbert | f6844b9 | 2015-11-05 18:10:48 +0000 | [diff] [blame] | 986 | if (migration_is_setup_or_active(s->state) || |
zhanghailiang | 3119473 | 2015-03-13 16:08:38 +0800 | [diff] [blame] | 987 | s->state == MIGRATION_STATUS_CANCELLING) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 988 | error_setg(errp, QERR_MIGRATION_ACTIVE); |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 989 | return; |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 990 | } |
Dr. David Alan Gilbert | ca99993 | 2014-04-14 17:03:59 +0100 | [diff] [blame] | 991 | if (runstate_check(RUN_STATE_INMIGRATE)) { |
| 992 | error_setg(errp, "Guest is waiting for an incoming migration"); |
| 993 | return; |
| 994 | } |
| 995 | |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 996 | if (qemu_savevm_state_blocked(errp)) { |
| 997 | return; |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 998 | } |
| 999 | |
Anthony Liguori | fa2756b | 2011-11-14 15:09:43 -0600 | [diff] [blame] | 1000 | if (migration_blockers) { |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 1001 | *errp = error_copy(migration_blockers->data); |
| 1002 | return; |
Anthony Liguori | fa2756b | 2011-11-14 15:09:43 -0600 | [diff] [blame] | 1003 | } |
| 1004 | |
Juan Quintela | 656a233 | 2015-07-01 09:32:29 +0200 | [diff] [blame] | 1005 | /* We are starting a new migration, so we want to start in a clean |
| 1006 | state. This change is only needed if previous migration |
| 1007 | failed/was cancelled. We don't use migrate_set_state() because |
| 1008 | we are setting the initial state, not changing it. */ |
| 1009 | s->state = MIGRATION_STATUS_NONE; |
| 1010 | |
Isaku Yamahata | 6607ae2 | 2012-06-19 18:43:09 +0300 | [diff] [blame] | 1011 | s = migrate_init(¶ms); |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 1012 | |
| 1013 | if (strstart(uri, "tcp:", &p)) { |
Paolo Bonzini | f37afb5 | 2012-10-02 10:02:46 +0200 | [diff] [blame] | 1014 | tcp_start_outgoing_migration(s, p, &local_err); |
Michael R. Hines | 2da776d | 2013-07-22 10:01:54 -0400 | [diff] [blame] | 1015 | #ifdef CONFIG_RDMA |
Michael R. Hines | 41310c6 | 2013-12-19 04:52:01 +0800 | [diff] [blame] | 1016 | } else if (strstart(uri, "rdma:", &p)) { |
Michael R. Hines | 2da776d | 2013-07-22 10:01:54 -0400 | [diff] [blame] | 1017 | rdma_start_outgoing_migration(s, p, &local_err); |
| 1018 | #endif |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 1019 | #if !defined(WIN32) |
| 1020 | } else if (strstart(uri, "exec:", &p)) { |
Paolo Bonzini | f37afb5 | 2012-10-02 10:02:46 +0200 | [diff] [blame] | 1021 | exec_start_outgoing_migration(s, p, &local_err); |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 1022 | } else if (strstart(uri, "unix:", &p)) { |
Paolo Bonzini | f37afb5 | 2012-10-02 10:02:46 +0200 | [diff] [blame] | 1023 | unix_start_outgoing_migration(s, p, &local_err); |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 1024 | } else if (strstart(uri, "fd:", &p)) { |
Paolo Bonzini | f37afb5 | 2012-10-02 10:02:46 +0200 | [diff] [blame] | 1025 | fd_start_outgoing_migration(s, p, &local_err); |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 1026 | #endif |
| 1027 | } else { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1028 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri", |
| 1029 | "a valid migration protocol"); |
zhanghailiang | 48781e5 | 2015-12-16 11:47:33 +0000 | [diff] [blame^] | 1030 | migrate_set_state(&s->state, MIGRATION_STATUS_SETUP, |
| 1031 | MIGRATION_STATUS_FAILED); |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 1032 | return; |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 1033 | } |
| 1034 | |
Paolo Bonzini | f37afb5 | 2012-10-02 10:02:46 +0200 | [diff] [blame] | 1035 | if (local_err) { |
Paolo Bonzini | 342ab8d | 2012-10-02 09:59:38 +0200 | [diff] [blame] | 1036 | migrate_fd_error(s); |
Paolo Bonzini | f37afb5 | 2012-10-02 10:02:46 +0200 | [diff] [blame] | 1037 | error_propagate(errp, local_err); |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 1038 | return; |
Juan Quintela | 1299c63 | 2011-11-09 21:29:01 +0100 | [diff] [blame] | 1039 | } |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 1040 | } |
| 1041 | |
Luiz Capitulino | 6cdedb0 | 2011-11-27 22:54:09 -0200 | [diff] [blame] | 1042 | void qmp_migrate_cancel(Error **errp) |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 1043 | { |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 1044 | migrate_fd_cancel(migrate_get_current()); |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 1045 | } |
| 1046 | |
Orit Wasserman | 9e1ba4c | 2012-08-06 21:42:54 +0300 | [diff] [blame] | 1047 | void qmp_migrate_set_cache_size(int64_t value, Error **errp) |
| 1048 | { |
| 1049 | MigrationState *s = migrate_get_current(); |
Orit Wasserman | c91e681 | 2014-01-30 20:08:34 +0200 | [diff] [blame] | 1050 | int64_t new_size; |
Orit Wasserman | 9e1ba4c | 2012-08-06 21:42:54 +0300 | [diff] [blame] | 1051 | |
| 1052 | /* Check for truncation */ |
| 1053 | if (value != (size_t)value) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1054 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size", |
| 1055 | "exceeding address space"); |
Orit Wasserman | 9e1ba4c | 2012-08-06 21:42:54 +0300 | [diff] [blame] | 1056 | return; |
| 1057 | } |
| 1058 | |
Orit Wasserman | a5615b1 | 2014-01-30 20:08:36 +0200 | [diff] [blame] | 1059 | /* Cache should not be larger than guest ram size */ |
| 1060 | if (value > ram_bytes_total()) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1061 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size", |
| 1062 | "exceeds guest ram size "); |
Orit Wasserman | a5615b1 | 2014-01-30 20:08:36 +0200 | [diff] [blame] | 1063 | return; |
| 1064 | } |
| 1065 | |
Orit Wasserman | c91e681 | 2014-01-30 20:08:34 +0200 | [diff] [blame] | 1066 | new_size = xbzrle_cache_resize(value); |
| 1067 | if (new_size < 0) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1068 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size", |
| 1069 | "is smaller than page size"); |
Orit Wasserman | c91e681 | 2014-01-30 20:08:34 +0200 | [diff] [blame] | 1070 | return; |
| 1071 | } |
| 1072 | |
| 1073 | s->xbzrle_cache_size = new_size; |
Orit Wasserman | 9e1ba4c | 2012-08-06 21:42:54 +0300 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | int64_t qmp_query_migrate_cache_size(Error **errp) |
| 1077 | { |
| 1078 | return migrate_xbzrle_cache_size(); |
| 1079 | } |
| 1080 | |
Luiz Capitulino | 3dc8538 | 2011-11-28 11:59:37 -0200 | [diff] [blame] | 1081 | void qmp_migrate_set_speed(int64_t value, Error **errp) |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 1082 | { |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 1083 | MigrationState *s; |
| 1084 | |
Luiz Capitulino | 3dc8538 | 2011-11-28 11:59:37 -0200 | [diff] [blame] | 1085 | if (value < 0) { |
| 1086 | value = 0; |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 1087 | } |
Paolo Bonzini | 442773c | 2013-02-22 17:36:44 +0100 | [diff] [blame] | 1088 | if (value > SIZE_MAX) { |
| 1089 | value = SIZE_MAX; |
| 1090 | } |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 1091 | |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 1092 | s = migrate_get_current(); |
Luiz Capitulino | 3dc8538 | 2011-11-28 11:59:37 -0200 | [diff] [blame] | 1093 | s->bandwidth_limit = value; |
Paolo Bonzini | 442773c | 2013-02-22 17:36:44 +0100 | [diff] [blame] | 1094 | if (s->file) { |
| 1095 | qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO); |
| 1096 | } |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 1097 | } |
| 1098 | |
Luiz Capitulino | 4f0a993 | 2011-11-27 23:18:01 -0200 | [diff] [blame] | 1099 | void qmp_migrate_set_downtime(double value, Error **errp) |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 1100 | { |
Luiz Capitulino | 4f0a993 | 2011-11-27 23:18:01 -0200 | [diff] [blame] | 1101 | value *= 1e9; |
| 1102 | value = MAX(0, MIN(UINT64_MAX, value)); |
| 1103 | max_downtime = (uint64_t)value; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 1104 | } |
Orit Wasserman | 17ad9b3 | 2012-08-06 21:42:53 +0300 | [diff] [blame] | 1105 | |
Dr. David Alan Gilbert | 53dd370 | 2015-11-05 18:10:51 +0000 | [diff] [blame] | 1106 | bool migrate_postcopy_ram(void) |
| 1107 | { |
| 1108 | MigrationState *s; |
| 1109 | |
| 1110 | s = migrate_get_current(); |
| 1111 | |
| 1112 | return s->enabled_capabilities[MIGRATION_CAPABILITY_X_POSTCOPY_RAM]; |
| 1113 | } |
| 1114 | |
Chegu Vinod | bde1e2e | 2013-06-24 03:49:42 -0600 | [diff] [blame] | 1115 | bool migrate_auto_converge(void) |
| 1116 | { |
| 1117 | MigrationState *s; |
| 1118 | |
| 1119 | s = migrate_get_current(); |
| 1120 | |
| 1121 | return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE]; |
| 1122 | } |
| 1123 | |
Peter Lieven | 323004a | 2013-07-18 09:48:50 +0200 | [diff] [blame] | 1124 | bool migrate_zero_blocks(void) |
| 1125 | { |
| 1126 | MigrationState *s; |
| 1127 | |
| 1128 | s = migrate_get_current(); |
| 1129 | |
| 1130 | return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS]; |
| 1131 | } |
| 1132 | |
Liang Li | 8706d2d | 2015-03-23 16:32:17 +0800 | [diff] [blame] | 1133 | bool migrate_use_compression(void) |
| 1134 | { |
Liang Li | dde4e69 | 2015-03-23 16:32:26 +0800 | [diff] [blame] | 1135 | MigrationState *s; |
| 1136 | |
| 1137 | s = migrate_get_current(); |
| 1138 | |
| 1139 | return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS]; |
Liang Li | 8706d2d | 2015-03-23 16:32:17 +0800 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | int migrate_compress_level(void) |
| 1143 | { |
| 1144 | MigrationState *s; |
| 1145 | |
| 1146 | s = migrate_get_current(); |
| 1147 | |
Liang Li | 43c60a8 | 2015-03-23 16:32:27 +0800 | [diff] [blame] | 1148 | return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL]; |
Liang Li | 8706d2d | 2015-03-23 16:32:17 +0800 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | int migrate_compress_threads(void) |
| 1152 | { |
| 1153 | MigrationState *s; |
| 1154 | |
| 1155 | s = migrate_get_current(); |
| 1156 | |
Liang Li | 43c60a8 | 2015-03-23 16:32:27 +0800 | [diff] [blame] | 1157 | return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS]; |
Liang Li | 8706d2d | 2015-03-23 16:32:17 +0800 | [diff] [blame] | 1158 | } |
| 1159 | |
Liang Li | 3fcb38c | 2015-03-23 16:32:18 +0800 | [diff] [blame] | 1160 | int migrate_decompress_threads(void) |
| 1161 | { |
| 1162 | MigrationState *s; |
| 1163 | |
| 1164 | s = migrate_get_current(); |
| 1165 | |
Liang Li | 43c60a8 | 2015-03-23 16:32:27 +0800 | [diff] [blame] | 1166 | return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS]; |
Liang Li | 3fcb38c | 2015-03-23 16:32:18 +0800 | [diff] [blame] | 1167 | } |
| 1168 | |
Juan Quintela | b05dc72 | 2015-07-07 14:44:05 +0200 | [diff] [blame] | 1169 | bool migrate_use_events(void) |
| 1170 | { |
| 1171 | MigrationState *s; |
| 1172 | |
| 1173 | s = migrate_get_current(); |
| 1174 | |
| 1175 | return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS]; |
| 1176 | } |
| 1177 | |
Orit Wasserman | 17ad9b3 | 2012-08-06 21:42:53 +0300 | [diff] [blame] | 1178 | int migrate_use_xbzrle(void) |
| 1179 | { |
| 1180 | MigrationState *s; |
| 1181 | |
| 1182 | s = migrate_get_current(); |
| 1183 | |
| 1184 | return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE]; |
| 1185 | } |
| 1186 | |
| 1187 | int64_t migrate_xbzrle_cache_size(void) |
| 1188 | { |
| 1189 | MigrationState *s; |
| 1190 | |
| 1191 | s = migrate_get_current(); |
| 1192 | |
| 1193 | return s->xbzrle_cache_size; |
| 1194 | } |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 1195 | |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 1196 | /* migration thread support */ |
| 1197 | /* |
| 1198 | * Something bad happened to the RP stream, mark an error |
| 1199 | * The caller shall print or trace something to indicate why |
| 1200 | */ |
| 1201 | static void mark_source_rp_bad(MigrationState *s) |
| 1202 | { |
| 1203 | s->rp_state.error = true; |
| 1204 | } |
| 1205 | |
| 1206 | static struct rp_cmd_args { |
| 1207 | ssize_t len; /* -1 = variable */ |
| 1208 | const char *name; |
| 1209 | } rp_cmd_args[] = { |
| 1210 | [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" }, |
| 1211 | [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" }, |
| 1212 | [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" }, |
Dr. David Alan Gilbert | 1e2d90e | 2015-11-05 18:11:07 +0000 | [diff] [blame] | 1213 | [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" }, |
| 1214 | [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" }, |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 1215 | [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" }, |
| 1216 | }; |
| 1217 | |
| 1218 | /* |
Dr. David Alan Gilbert | 1e2d90e | 2015-11-05 18:11:07 +0000 | [diff] [blame] | 1219 | * Process a request for pages received on the return path, |
| 1220 | * We're allowed to send more than requested (e.g. to round to our page size) |
| 1221 | * and we don't need to send pages that have already been sent. |
| 1222 | */ |
| 1223 | static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname, |
| 1224 | ram_addr_t start, size_t len) |
| 1225 | { |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1226 | long our_host_ps = getpagesize(); |
| 1227 | |
Dr. David Alan Gilbert | 1e2d90e | 2015-11-05 18:11:07 +0000 | [diff] [blame] | 1228 | trace_migrate_handle_rp_req_pages(rbname, start, len); |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1229 | |
| 1230 | /* |
| 1231 | * Since we currently insist on matching page sizes, just sanity check |
| 1232 | * we're being asked for whole host pages. |
| 1233 | */ |
| 1234 | if (start & (our_host_ps-1) || |
| 1235 | (len & (our_host_ps-1))) { |
| 1236 | error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT |
| 1237 | " len: %zd", __func__, start, len); |
| 1238 | mark_source_rp_bad(ms); |
| 1239 | return; |
| 1240 | } |
| 1241 | |
| 1242 | if (ram_save_queue_pages(ms, rbname, start, len)) { |
| 1243 | mark_source_rp_bad(ms); |
| 1244 | } |
Dr. David Alan Gilbert | 1e2d90e | 2015-11-05 18:11:07 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | /* |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 1248 | * Handles messages sent on the return path towards the source VM |
| 1249 | * |
| 1250 | */ |
| 1251 | static void *source_return_path_thread(void *opaque) |
| 1252 | { |
| 1253 | MigrationState *ms = opaque; |
| 1254 | QEMUFile *rp = ms->rp_state.from_dst_file; |
| 1255 | uint16_t header_len, header_type; |
| 1256 | const int max_len = 512; |
| 1257 | uint8_t buf[max_len]; |
| 1258 | uint32_t tmp32, sibling_error; |
Dr. David Alan Gilbert | 1e2d90e | 2015-11-05 18:11:07 +0000 | [diff] [blame] | 1259 | ram_addr_t start = 0; /* =0 to silence warning */ |
| 1260 | size_t len = 0, expected_len; |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 1261 | int res; |
| 1262 | |
| 1263 | trace_source_return_path_thread_entry(); |
| 1264 | while (!ms->rp_state.error && !qemu_file_get_error(rp) && |
| 1265 | migration_is_setup_or_active(ms->state)) { |
| 1266 | trace_source_return_path_thread_loop_top(); |
| 1267 | header_type = qemu_get_be16(rp); |
| 1268 | header_len = qemu_get_be16(rp); |
| 1269 | |
| 1270 | if (header_type >= MIG_RP_MSG_MAX || |
| 1271 | header_type == MIG_RP_MSG_INVALID) { |
| 1272 | error_report("RP: Received invalid message 0x%04x length 0x%04x", |
| 1273 | header_type, header_len); |
| 1274 | mark_source_rp_bad(ms); |
| 1275 | goto out; |
| 1276 | } |
| 1277 | |
| 1278 | if ((rp_cmd_args[header_type].len != -1 && |
| 1279 | header_len != rp_cmd_args[header_type].len) || |
| 1280 | header_len > max_len) { |
| 1281 | error_report("RP: Received '%s' message (0x%04x) with" |
| 1282 | "incorrect length %d expecting %zu", |
| 1283 | rp_cmd_args[header_type].name, header_type, header_len, |
| 1284 | (size_t)rp_cmd_args[header_type].len); |
| 1285 | mark_source_rp_bad(ms); |
| 1286 | goto out; |
| 1287 | } |
| 1288 | |
| 1289 | /* We know we've got a valid header by this point */ |
| 1290 | res = qemu_get_buffer(rp, buf, header_len); |
| 1291 | if (res != header_len) { |
| 1292 | error_report("RP: Failed reading data for message 0x%04x" |
| 1293 | " read %d expected %d", |
| 1294 | header_type, res, header_len); |
| 1295 | mark_source_rp_bad(ms); |
| 1296 | goto out; |
| 1297 | } |
| 1298 | |
| 1299 | /* OK, we have the message and the data */ |
| 1300 | switch (header_type) { |
| 1301 | case MIG_RP_MSG_SHUT: |
| 1302 | sibling_error = be32_to_cpup((uint32_t *)buf); |
| 1303 | trace_source_return_path_thread_shut(sibling_error); |
| 1304 | if (sibling_error) { |
| 1305 | error_report("RP: Sibling indicated error %d", sibling_error); |
| 1306 | mark_source_rp_bad(ms); |
| 1307 | } |
| 1308 | /* |
| 1309 | * We'll let the main thread deal with closing the RP |
| 1310 | * we could do a shutdown(2) on it, but we're the only user |
| 1311 | * anyway, so there's nothing gained. |
| 1312 | */ |
| 1313 | goto out; |
| 1314 | |
| 1315 | case MIG_RP_MSG_PONG: |
| 1316 | tmp32 = be32_to_cpup((uint32_t *)buf); |
| 1317 | trace_source_return_path_thread_pong(tmp32); |
| 1318 | break; |
| 1319 | |
Dr. David Alan Gilbert | 1e2d90e | 2015-11-05 18:11:07 +0000 | [diff] [blame] | 1320 | case MIG_RP_MSG_REQ_PAGES: |
| 1321 | start = be64_to_cpup((uint64_t *)buf); |
| 1322 | len = be32_to_cpup((uint32_t *)(buf + 8)); |
| 1323 | migrate_handle_rp_req_pages(ms, NULL, start, len); |
| 1324 | break; |
| 1325 | |
| 1326 | case MIG_RP_MSG_REQ_PAGES_ID: |
| 1327 | expected_len = 12 + 1; /* header + termination */ |
| 1328 | |
| 1329 | if (header_len >= expected_len) { |
| 1330 | start = be64_to_cpup((uint64_t *)buf); |
| 1331 | len = be32_to_cpup((uint32_t *)(buf + 8)); |
| 1332 | /* Now we expect an idstr */ |
| 1333 | tmp32 = buf[12]; /* Length of the following idstr */ |
| 1334 | buf[13 + tmp32] = '\0'; |
| 1335 | expected_len += tmp32; |
| 1336 | } |
| 1337 | if (header_len != expected_len) { |
| 1338 | error_report("RP: Req_Page_id with length %d expecting %zd", |
| 1339 | header_len, expected_len); |
| 1340 | mark_source_rp_bad(ms); |
| 1341 | goto out; |
| 1342 | } |
| 1343 | migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len); |
| 1344 | break; |
| 1345 | |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 1346 | default: |
| 1347 | break; |
| 1348 | } |
| 1349 | } |
Dr. David Alan Gilbert | 5df5416 | 2015-11-18 11:48:41 +0000 | [diff] [blame] | 1350 | if (qemu_file_get_error(rp)) { |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 1351 | trace_source_return_path_thread_bad_end(); |
| 1352 | mark_source_rp_bad(ms); |
| 1353 | } |
| 1354 | |
| 1355 | trace_source_return_path_thread_end(); |
| 1356 | out: |
| 1357 | ms->rp_state.from_dst_file = NULL; |
| 1358 | qemu_fclose(rp); |
| 1359 | return NULL; |
| 1360 | } |
| 1361 | |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 1362 | static int open_return_path_on_source(MigrationState *ms) |
| 1363 | { |
| 1364 | |
| 1365 | ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->file); |
| 1366 | if (!ms->rp_state.from_dst_file) { |
| 1367 | return -1; |
| 1368 | } |
| 1369 | |
| 1370 | trace_open_return_path_on_source(); |
| 1371 | qemu_thread_create(&ms->rp_state.rp_thread, "return path", |
| 1372 | source_return_path_thread, ms, QEMU_THREAD_JOINABLE); |
| 1373 | |
| 1374 | trace_open_return_path_on_source_continue(); |
| 1375 | |
| 1376 | return 0; |
| 1377 | } |
| 1378 | |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 1379 | /* Returns 0 if the RP was ok, otherwise there was an error on the RP */ |
| 1380 | static int await_return_path_close_on_source(MigrationState *ms) |
| 1381 | { |
| 1382 | /* |
| 1383 | * If this is a normal exit then the destination will send a SHUT and the |
| 1384 | * rp_thread will exit, however if there's an error we need to cause |
| 1385 | * it to exit. |
| 1386 | */ |
| 1387 | if (qemu_file_get_error(ms->file) && ms->rp_state.from_dst_file) { |
| 1388 | /* |
| 1389 | * shutdown(2), if we have it, will cause it to unblock if it's stuck |
| 1390 | * waiting for the destination. |
| 1391 | */ |
| 1392 | qemu_file_shutdown(ms->rp_state.from_dst_file); |
| 1393 | mark_source_rp_bad(ms); |
| 1394 | } |
| 1395 | trace_await_return_path_close_on_source_joining(); |
| 1396 | qemu_thread_join(&ms->rp_state.rp_thread); |
| 1397 | trace_await_return_path_close_on_source_close(); |
| 1398 | return ms->rp_state.error; |
| 1399 | } |
| 1400 | |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 1401 | /* |
| 1402 | * Switch from normal iteration to postcopy |
| 1403 | * Returns non-0 on error |
| 1404 | */ |
| 1405 | static int postcopy_start(MigrationState *ms, bool *old_vm_running) |
| 1406 | { |
| 1407 | int ret; |
| 1408 | const QEMUSizedBuffer *qsb; |
| 1409 | int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
zhanghailiang | 48781e5 | 2015-12-16 11:47:33 +0000 | [diff] [blame^] | 1410 | migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE, |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 1411 | MIGRATION_STATUS_POSTCOPY_ACTIVE); |
| 1412 | |
| 1413 | trace_postcopy_start(); |
| 1414 | qemu_mutex_lock_iothread(); |
| 1415 | trace_postcopy_start_set_run(); |
| 1416 | |
| 1417 | qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER); |
| 1418 | *old_vm_running = runstate_is_running(); |
| 1419 | global_state_store(); |
| 1420 | ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE); |
| 1421 | |
| 1422 | if (ret < 0) { |
| 1423 | goto fail; |
| 1424 | } |
| 1425 | |
| 1426 | /* |
Dr. David Alan Gilbert | 1c0d249 | 2015-11-11 14:02:27 +0000 | [diff] [blame] | 1427 | * Cause any non-postcopiable, but iterative devices to |
| 1428 | * send out their final data. |
| 1429 | */ |
| 1430 | qemu_savevm_state_complete_precopy(ms->file, true); |
| 1431 | |
| 1432 | /* |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 1433 | * in Finish migrate and with the io-lock held everything should |
| 1434 | * be quiet, but we've potentially still got dirty pages and we |
| 1435 | * need to tell the destination to throw any pages it's already received |
| 1436 | * that are dirty |
| 1437 | */ |
| 1438 | if (ram_postcopy_send_discard_bitmap(ms)) { |
| 1439 | error_report("postcopy send discard bitmap failed"); |
| 1440 | goto fail; |
| 1441 | } |
| 1442 | |
| 1443 | /* |
| 1444 | * send rest of state - note things that are doing postcopy |
| 1445 | * will notice we're in POSTCOPY_ACTIVE and not actually |
| 1446 | * wrap their state up here |
| 1447 | */ |
| 1448 | qemu_file_set_rate_limit(ms->file, INT64_MAX); |
| 1449 | /* Ping just for debugging, helps line traces up */ |
| 1450 | qemu_savevm_send_ping(ms->file, 2); |
| 1451 | |
| 1452 | /* |
| 1453 | * While loading the device state we may trigger page transfer |
| 1454 | * requests and the fd must be free to process those, and thus |
| 1455 | * the destination must read the whole device state off the fd before |
| 1456 | * it starts processing it. Unfortunately the ad-hoc migration format |
| 1457 | * doesn't allow the destination to know the size to read without fully |
| 1458 | * parsing it through each devices load-state code (especially the open |
| 1459 | * coded devices that use get/put). |
| 1460 | * So we wrap the device state up in a package with a length at the start; |
| 1461 | * to do this we use a qemu_buf to hold the whole of the device state. |
| 1462 | */ |
| 1463 | QEMUFile *fb = qemu_bufopen("w", NULL); |
| 1464 | if (!fb) { |
| 1465 | error_report("Failed to create buffered file"); |
| 1466 | goto fail; |
| 1467 | } |
| 1468 | |
Dr. David Alan Gilbert | c76201a | 2015-11-05 18:11:18 +0000 | [diff] [blame] | 1469 | /* |
| 1470 | * Make sure the receiver can get incoming pages before we send the rest |
| 1471 | * of the state |
| 1472 | */ |
| 1473 | qemu_savevm_send_postcopy_listen(fb); |
| 1474 | |
Dr. David Alan Gilbert | 1c0d249 | 2015-11-11 14:02:27 +0000 | [diff] [blame] | 1475 | qemu_savevm_state_complete_precopy(fb, false); |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 1476 | qemu_savevm_send_ping(fb, 3); |
| 1477 | |
| 1478 | qemu_savevm_send_postcopy_run(fb); |
| 1479 | |
| 1480 | /* <><> end of stuff going into the package */ |
| 1481 | qsb = qemu_buf_get(fb); |
| 1482 | |
| 1483 | /* Now send that blob */ |
| 1484 | if (qemu_savevm_send_packaged(ms->file, qsb)) { |
| 1485 | goto fail_closefb; |
| 1486 | } |
| 1487 | qemu_fclose(fb); |
| 1488 | ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop; |
| 1489 | |
| 1490 | qemu_mutex_unlock_iothread(); |
| 1491 | |
| 1492 | /* |
| 1493 | * Although this ping is just for debug, it could potentially be |
| 1494 | * used for getting a better measurement of downtime at the source. |
| 1495 | */ |
| 1496 | qemu_savevm_send_ping(ms->file, 4); |
| 1497 | |
| 1498 | ret = qemu_file_get_error(ms->file); |
| 1499 | if (ret) { |
| 1500 | error_report("postcopy_start: Migration stream errored"); |
zhanghailiang | 48781e5 | 2015-12-16 11:47:33 +0000 | [diff] [blame^] | 1501 | migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE, |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 1502 | MIGRATION_STATUS_FAILED); |
| 1503 | } |
| 1504 | |
| 1505 | return ret; |
| 1506 | |
| 1507 | fail_closefb: |
| 1508 | qemu_fclose(fb); |
| 1509 | fail: |
zhanghailiang | 48781e5 | 2015-12-16 11:47:33 +0000 | [diff] [blame^] | 1510 | migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE, |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 1511 | MIGRATION_STATUS_FAILED); |
| 1512 | qemu_mutex_unlock_iothread(); |
| 1513 | return -1; |
| 1514 | } |
| 1515 | |
Dr. David Alan Gilbert | 09f6c85 | 2015-08-13 11:51:31 +0100 | [diff] [blame] | 1516 | /** |
| 1517 | * migration_completion: Used by migration_thread when there's not much left. |
| 1518 | * The caller 'breaks' the loop when this returns. |
| 1519 | * |
| 1520 | * @s: Current migration state |
Dr. David Alan Gilbert | 36f4856 | 2015-11-05 18:10:57 +0000 | [diff] [blame] | 1521 | * @current_active_state: The migration state we expect to be in |
Dr. David Alan Gilbert | 09f6c85 | 2015-08-13 11:51:31 +0100 | [diff] [blame] | 1522 | * @*old_vm_running: Pointer to old_vm_running flag |
| 1523 | * @*start_time: Pointer to time to update |
| 1524 | */ |
Dr. David Alan Gilbert | 36f4856 | 2015-11-05 18:10:57 +0000 | [diff] [blame] | 1525 | static void migration_completion(MigrationState *s, int current_active_state, |
| 1526 | bool *old_vm_running, |
Dr. David Alan Gilbert | 09f6c85 | 2015-08-13 11:51:31 +0100 | [diff] [blame] | 1527 | int64_t *start_time) |
| 1528 | { |
| 1529 | int ret; |
| 1530 | |
Dr. David Alan Gilbert | b10ac0c | 2015-11-05 18:11:06 +0000 | [diff] [blame] | 1531 | if (s->state == MIGRATION_STATUS_ACTIVE) { |
| 1532 | qemu_mutex_lock_iothread(); |
| 1533 | *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
| 1534 | qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER); |
| 1535 | *old_vm_running = runstate_is_running(); |
| 1536 | ret = global_state_store(); |
Dr. David Alan Gilbert | 09f6c85 | 2015-08-13 11:51:31 +0100 | [diff] [blame] | 1537 | |
Dr. David Alan Gilbert | b10ac0c | 2015-11-05 18:11:06 +0000 | [diff] [blame] | 1538 | if (!ret) { |
| 1539 | ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE); |
| 1540 | if (ret >= 0) { |
| 1541 | qemu_file_set_rate_limit(s->file, INT64_MAX); |
Dr. David Alan Gilbert | 1c0d249 | 2015-11-11 14:02:27 +0000 | [diff] [blame] | 1542 | qemu_savevm_state_complete_precopy(s->file, false); |
Dr. David Alan Gilbert | b10ac0c | 2015-11-05 18:11:06 +0000 | [diff] [blame] | 1543 | } |
Dr. David Alan Gilbert | 09f6c85 | 2015-08-13 11:51:31 +0100 | [diff] [blame] | 1544 | } |
Dr. David Alan Gilbert | b10ac0c | 2015-11-05 18:11:06 +0000 | [diff] [blame] | 1545 | qemu_mutex_unlock_iothread(); |
Dr. David Alan Gilbert | 09f6c85 | 2015-08-13 11:51:31 +0100 | [diff] [blame] | 1546 | |
Dr. David Alan Gilbert | b10ac0c | 2015-11-05 18:11:06 +0000 | [diff] [blame] | 1547 | if (ret < 0) { |
| 1548 | goto fail; |
| 1549 | } |
| 1550 | } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) { |
| 1551 | trace_migration_completion_postcopy_end(); |
| 1552 | |
| 1553 | qemu_savevm_state_complete_postcopy(s->file); |
| 1554 | trace_migration_completion_postcopy_end_after_complete(); |
| 1555 | } |
| 1556 | |
| 1557 | /* |
| 1558 | * If rp was opened we must clean up the thread before |
| 1559 | * cleaning everything else up (since if there are no failures |
| 1560 | * it will wait for the destination to send it's status in |
| 1561 | * a SHUT command). |
| 1562 | * Postcopy opens rp if enabled (even if it's not avtivated) |
| 1563 | */ |
| 1564 | if (migrate_postcopy_ram()) { |
| 1565 | int rp_error; |
| 1566 | trace_migration_completion_postcopy_end_before_rp(); |
| 1567 | rp_error = await_return_path_close_on_source(s); |
| 1568 | trace_migration_completion_postcopy_end_after_rp(rp_error); |
| 1569 | if (rp_error) { |
| 1570 | goto fail; |
| 1571 | } |
Dr. David Alan Gilbert | 09f6c85 | 2015-08-13 11:51:31 +0100 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | if (qemu_file_get_error(s->file)) { |
| 1575 | trace_migration_completion_file_err(); |
| 1576 | goto fail; |
| 1577 | } |
| 1578 | |
zhanghailiang | 48781e5 | 2015-12-16 11:47:33 +0000 | [diff] [blame^] | 1579 | migrate_set_state(&s->state, current_active_state, |
| 1580 | MIGRATION_STATUS_COMPLETED); |
Dr. David Alan Gilbert | 09f6c85 | 2015-08-13 11:51:31 +0100 | [diff] [blame] | 1581 | return; |
| 1582 | |
| 1583 | fail: |
zhanghailiang | 48781e5 | 2015-12-16 11:47:33 +0000 | [diff] [blame^] | 1584 | migrate_set_state(&s->state, current_active_state, |
| 1585 | MIGRATION_STATUS_FAILED); |
Dr. David Alan Gilbert | 09f6c85 | 2015-08-13 11:51:31 +0100 | [diff] [blame] | 1586 | } |
| 1587 | |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 1588 | /* |
| 1589 | * Master migration thread on the source VM. |
| 1590 | * It drives the migration and pumps the data down the outgoing channel. |
| 1591 | */ |
Juan Quintela | 5f496a1 | 2013-02-22 17:36:30 +0100 | [diff] [blame] | 1592 | static void *migration_thread(void *opaque) |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 1593 | { |
Juan Quintela | 9848a40 | 2012-12-19 09:55:50 +0100 | [diff] [blame] | 1594 | MigrationState *s = opaque; |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 1595 | /* Used by the bandwidth calcs, updated later */ |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 1596 | int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
| 1597 | int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST); |
Paolo Bonzini | be7172e | 2013-02-22 17:36:43 +0100 | [diff] [blame] | 1598 | int64_t initial_bytes = 0; |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 1599 | int64_t max_size = 0; |
Paolo Bonzini | a3fa1d7 | 2013-02-22 17:36:18 +0100 | [diff] [blame] | 1600 | int64_t start_time = initial_time; |
Liang Li | 94f5a43 | 2015-11-02 15:37:00 +0800 | [diff] [blame] | 1601 | int64_t end_time; |
Paolo Bonzini | a3fa1d7 | 2013-02-22 17:36:18 +0100 | [diff] [blame] | 1602 | bool old_vm_running = false; |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 1603 | bool entered_postcopy = false; |
| 1604 | /* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */ |
| 1605 | enum MigrationStatus current_active_state = MIGRATION_STATUS_ACTIVE; |
Juan Quintela | 76f5933 | 2012-10-03 20:16:24 +0200 | [diff] [blame] | 1606 | |
Paolo Bonzini | ab28bd2 | 2015-07-09 08:55:38 +0200 | [diff] [blame] | 1607 | rcu_register_thread(); |
| 1608 | |
Dr. David Alan Gilbert | f796baa | 2015-05-21 13:24:12 +0100 | [diff] [blame] | 1609 | qemu_savevm_state_header(s->file); |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 1610 | |
| 1611 | if (migrate_postcopy_ram()) { |
| 1612 | /* Now tell the dest that it should open its end so it can reply */ |
| 1613 | qemu_savevm_send_open_return_path(s->file); |
| 1614 | |
| 1615 | /* And do a ping that will make stuff easier to debug */ |
| 1616 | qemu_savevm_send_ping(s->file, 1); |
| 1617 | |
| 1618 | /* |
| 1619 | * Tell the destination that we *might* want to do postcopy later; |
| 1620 | * if the other end can't do postcopy it should fail now, nice and |
| 1621 | * early. |
| 1622 | */ |
| 1623 | qemu_savevm_send_postcopy_advise(s->file); |
| 1624 | } |
| 1625 | |
Paolo Bonzini | dba433c | 2013-02-22 17:36:17 +0100 | [diff] [blame] | 1626 | qemu_savevm_state_begin(s->file, &s->params); |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 1627 | |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 1628 | s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start; |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 1629 | current_active_state = MIGRATION_STATUS_ACTIVE; |
zhanghailiang | 48781e5 | 2015-12-16 11:47:33 +0000 | [diff] [blame^] | 1630 | migrate_set_state(&s->state, MIGRATION_STATUS_SETUP, |
| 1631 | MIGRATION_STATUS_ACTIVE); |
Michael R. Hines | 29ae8a4 | 2013-07-22 10:01:57 -0400 | [diff] [blame] | 1632 | |
Dr. David Alan Gilbert | 9ec055a | 2015-11-05 18:10:58 +0000 | [diff] [blame] | 1633 | trace_migration_thread_setup_complete(); |
| 1634 | |
| 1635 | while (s->state == MIGRATION_STATUS_ACTIVE || |
| 1636 | s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) { |
Juan Quintela | a3e879c | 2013-02-01 12:39:08 +0100 | [diff] [blame] | 1637 | int64_t current_time; |
Juan Quintela | c369f40 | 2012-10-03 20:33:34 +0200 | [diff] [blame] | 1638 | uint64_t pending_size; |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 1639 | |
Paolo Bonzini | a0ff044 | 2013-02-22 17:36:35 +0100 | [diff] [blame] | 1640 | if (!qemu_file_rate_limit(s->file)) { |
Dr. David Alan Gilbert | c31b098 | 2015-11-05 18:10:54 +0000 | [diff] [blame] | 1641 | uint64_t pend_post, pend_nonpost; |
| 1642 | |
| 1643 | qemu_savevm_state_pending(s->file, max_size, &pend_nonpost, |
| 1644 | &pend_post); |
| 1645 | pending_size = pend_nonpost + pend_post; |
| 1646 | trace_migrate_pending(pending_size, max_size, |
| 1647 | pend_post, pend_nonpost); |
Juan Quintela | b22ff1f | 2012-10-17 21:06:31 +0200 | [diff] [blame] | 1648 | if (pending_size && pending_size >= max_size) { |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 1649 | /* Still a significant amount to transfer */ |
| 1650 | |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 1651 | if (migrate_postcopy_ram() && |
| 1652 | s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE && |
| 1653 | pend_nonpost <= max_size && |
| 1654 | atomic_read(&s->start_postcopy)) { |
| 1655 | |
| 1656 | if (!postcopy_start(s, &old_vm_running)) { |
| 1657 | current_active_state = MIGRATION_STATUS_POSTCOPY_ACTIVE; |
| 1658 | entered_postcopy = true; |
| 1659 | } |
| 1660 | |
| 1661 | continue; |
| 1662 | } |
| 1663 | /* Just another iteration step */ |
Dr. David Alan Gilbert | 35ecd94 | 2015-11-05 18:11:14 +0000 | [diff] [blame] | 1664 | qemu_savevm_state_iterate(s->file, entered_postcopy); |
Juan Quintela | c369f40 | 2012-10-03 20:33:34 +0200 | [diff] [blame] | 1665 | } else { |
Dr. David Alan Gilbert | 09f6c85 | 2015-08-13 11:51:31 +0100 | [diff] [blame] | 1666 | trace_migration_thread_low_pending(pending_size); |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 1667 | migration_completion(s, current_active_state, |
Dr. David Alan Gilbert | 36f4856 | 2015-11-05 18:10:57 +0000 | [diff] [blame] | 1668 | &old_vm_running, &start_time); |
Dr. David Alan Gilbert | 09f6c85 | 2015-08-13 11:51:31 +0100 | [diff] [blame] | 1669 | break; |
Juan Quintela | c369f40 | 2012-10-03 20:33:34 +0200 | [diff] [blame] | 1670 | } |
| 1671 | } |
Paolo Bonzini | f4410a5 | 2013-02-22 17:36:20 +0100 | [diff] [blame] | 1672 | |
Paolo Bonzini | fd45ee2 | 2013-02-22 17:36:33 +0100 | [diff] [blame] | 1673 | if (qemu_file_get_error(s->file)) { |
zhanghailiang | 48781e5 | 2015-12-16 11:47:33 +0000 | [diff] [blame^] | 1674 | migrate_set_state(&s->state, current_active_state, |
| 1675 | MIGRATION_STATUS_FAILED); |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 1676 | trace_migration_thread_file_err(); |
Paolo Bonzini | fd45ee2 | 2013-02-22 17:36:33 +0100 | [diff] [blame] | 1677 | break; |
| 1678 | } |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 1679 | current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 1680 | if (current_time >= initial_time + BUFFER_DELAY) { |
Paolo Bonzini | be7172e | 2013-02-22 17:36:43 +0100 | [diff] [blame] | 1681 | uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes; |
Michael Roth | 77417f1 | 2013-05-16 16:25:44 -0500 | [diff] [blame] | 1682 | uint64_t time_spent = current_time - initial_time; |
Paolo Bonzini | a694ee3 | 2015-01-26 12:12:27 +0100 | [diff] [blame] | 1683 | double bandwidth = (double)transferred_bytes / time_spent; |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 1684 | max_size = bandwidth * migrate_max_downtime() / 1000000; |
| 1685 | |
Michael R. Hines | 7e114f8 | 2013-06-25 21:35:30 -0400 | [diff] [blame] | 1686 | s->mbps = time_spent ? (((double) transferred_bytes * 8.0) / |
| 1687 | ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1; |
| 1688 | |
Alexey Kardashevskiy | 9013dca | 2014-03-11 10:42:29 +1100 | [diff] [blame] | 1689 | trace_migrate_transferred(transferred_bytes, time_spent, |
| 1690 | bandwidth, max_size); |
Juan Quintela | 90f8ae7 | 2013-02-01 13:22:37 +0100 | [diff] [blame] | 1691 | /* if we haven't sent anything, we don't want to recalculate |
| 1692 | 10000 is a small enough number for our purposes */ |
| 1693 | if (s->dirty_bytes_rate && transferred_bytes > 10000) { |
| 1694 | s->expected_downtime = s->dirty_bytes_rate / bandwidth; |
| 1695 | } |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 1696 | |
Paolo Bonzini | 1964a39 | 2013-02-22 17:36:45 +0100 | [diff] [blame] | 1697 | qemu_file_reset_rate_limit(s->file); |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 1698 | initial_time = current_time; |
Paolo Bonzini | be7172e | 2013-02-22 17:36:43 +0100 | [diff] [blame] | 1699 | initial_bytes = qemu_ftell(s->file); |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 1700 | } |
Paolo Bonzini | a0ff044 | 2013-02-22 17:36:35 +0100 | [diff] [blame] | 1701 | if (qemu_file_rate_limit(s->file)) { |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 1702 | /* usleep expects microseconds */ |
| 1703 | g_usleep((initial_time + BUFFER_DELAY - current_time)*1000); |
| 1704 | } |
Paolo Bonzini | a3fa1d7 | 2013-02-22 17:36:18 +0100 | [diff] [blame] | 1705 | } |
| 1706 | |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 1707 | trace_migration_thread_after_loop(); |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 1708 | /* If we enabled cpu throttling for auto-converge, turn it off. */ |
| 1709 | cpu_throttle_stop(); |
Liang Li | 94f5a43 | 2015-11-02 15:37:00 +0800 | [diff] [blame] | 1710 | end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 1711 | |
Paolo Bonzini | f4410a5 | 2013-02-22 17:36:20 +0100 | [diff] [blame] | 1712 | qemu_mutex_lock_iothread(); |
Liang Li | ea7415f | 2015-11-02 15:37:01 +0800 | [diff] [blame] | 1713 | qemu_savevm_state_cleanup(); |
zhanghailiang | 3119473 | 2015-03-13 16:08:38 +0800 | [diff] [blame] | 1714 | if (s->state == MIGRATION_STATUS_COMPLETED) { |
Peter Lieven | d6ed731 | 2014-05-12 10:46:00 +0200 | [diff] [blame] | 1715 | uint64_t transferred_bytes = qemu_ftell(s->file); |
Paolo Bonzini | a3fa1d7 | 2013-02-22 17:36:18 +0100 | [diff] [blame] | 1716 | s->total_time = end_time - s->total_time; |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 1717 | if (!entered_postcopy) { |
| 1718 | s->downtime = end_time - start_time; |
| 1719 | } |
Peter Lieven | d6ed731 | 2014-05-12 10:46:00 +0200 | [diff] [blame] | 1720 | if (s->total_time) { |
| 1721 | s->mbps = (((double) transferred_bytes * 8.0) / |
| 1722 | ((double) s->total_time)) / 1000; |
| 1723 | } |
Paolo Bonzini | a3fa1d7 | 2013-02-22 17:36:18 +0100 | [diff] [blame] | 1724 | runstate_set(RUN_STATE_POSTMIGRATE); |
| 1725 | } else { |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 1726 | if (old_vm_running && !entered_postcopy) { |
Paolo Bonzini | a3fa1d7 | 2013-02-22 17:36:18 +0100 | [diff] [blame] | 1727 | vm_start(); |
Paolo Bonzini | dba433c | 2013-02-22 17:36:17 +0100 | [diff] [blame] | 1728 | } |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 1729 | } |
Paolo Bonzini | bb1fadc | 2013-02-22 17:36:21 +0100 | [diff] [blame] | 1730 | qemu_bh_schedule(s->cleanup_bh); |
Paolo Bonzini | dba433c | 2013-02-22 17:36:17 +0100 | [diff] [blame] | 1731 | qemu_mutex_unlock_iothread(); |
Paolo Bonzini | f4410a5 | 2013-02-22 17:36:20 +0100 | [diff] [blame] | 1732 | |
Paolo Bonzini | ab28bd2 | 2015-07-09 08:55:38 +0200 | [diff] [blame] | 1733 | rcu_unregister_thread(); |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 1734 | return NULL; |
| 1735 | } |
| 1736 | |
Juan Quintela | 9848a40 | 2012-12-19 09:55:50 +0100 | [diff] [blame] | 1737 | void migrate_fd_connect(MigrationState *s) |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 1738 | { |
Juan Quintela | cc283e3 | 2013-02-01 11:12:26 +0100 | [diff] [blame] | 1739 | /* This is a best 1st approximation. ns to ms */ |
| 1740 | s->expected_downtime = max_downtime/1000000; |
Paolo Bonzini | bb1fadc | 2013-02-22 17:36:21 +0100 | [diff] [blame] | 1741 | s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s); |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 1742 | |
Paolo Bonzini | 442773c | 2013-02-22 17:36:44 +0100 | [diff] [blame] | 1743 | qemu_file_set_rate_limit(s->file, |
| 1744 | s->bandwidth_limit / XFER_LIMIT_RATIO); |
| 1745 | |
Stefan Hajnoczi | 9287ac2 | 2013-07-29 15:01:57 +0200 | [diff] [blame] | 1746 | /* Notify before starting migration thread */ |
| 1747 | notifier_list_notify(&migration_state_notifiers, s); |
| 1748 | |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 1749 | /* |
| 1750 | * Open the return path; currently for postcopy but other things might |
| 1751 | * also want it. |
| 1752 | */ |
| 1753 | if (migrate_postcopy_ram()) { |
| 1754 | if (open_return_path_on_source(s)) { |
| 1755 | error_report("Unable to open return-path for postcopy"); |
zhanghailiang | 48781e5 | 2015-12-16 11:47:33 +0000 | [diff] [blame^] | 1756 | migrate_set_state(&s->state, MIGRATION_STATUS_SETUP, |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 1757 | MIGRATION_STATUS_FAILED); |
| 1758 | migrate_fd_cleanup(s); |
| 1759 | return; |
| 1760 | } |
| 1761 | } |
| 1762 | |
Liang Li | 8706d2d | 2015-03-23 16:32:17 +0800 | [diff] [blame] | 1763 | migrate_compress_threads_create(); |
Dr. David Alan Gilbert | 4900116 | 2014-01-30 10:20:32 +0000 | [diff] [blame] | 1764 | qemu_thread_create(&s->thread, "migration", migration_thread, s, |
Paolo Bonzini | bb1fadc | 2013-02-22 17:36:21 +0100 | [diff] [blame] | 1765 | QEMU_THREAD_JOINABLE); |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 1766 | s->migration_thread_running = true; |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 1767 | } |
Dr. David Alan Gilbert | 093e3c4 | 2015-11-05 18:10:52 +0000 | [diff] [blame] | 1768 | |
| 1769 | PostcopyState postcopy_state_get(void) |
| 1770 | { |
| 1771 | return atomic_mb_read(&incoming_postcopy_state); |
| 1772 | } |
| 1773 | |
| 1774 | /* Set the state and return the old state */ |
| 1775 | PostcopyState postcopy_state_set(PostcopyState new_state) |
| 1776 | { |
| 1777 | return atomic_xchg(&incoming_postcopy_state, new_state); |
| 1778 | } |
| 1779 | |