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