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