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