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