]>
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 | ||
16 | #include "qemu-common.h" | |
d49b6836 | 17 | #include "qemu/error-report.h" |
6a1751b7 | 18 | #include "qemu/main-loop.h" |
caf71f86 | 19 | #include "migration/migration.h" |
0d82d0e8 | 20 | #include "migration/qemu-file.h" |
9c17d615 | 21 | #include "sysemu/sysemu.h" |
737e150e | 22 | #include "block/block.h" |
cc7a8ea7 | 23 | #include "qapi/qmp/qerror.h" |
1de7afc9 | 24 | #include "qemu/sockets.h" |
ab28bd23 | 25 | #include "qemu/rcu.h" |
caf71f86 | 26 | #include "migration/block.h" |
766bd176 | 27 | #include "qemu/thread.h" |
791e7c82 | 28 | #include "qmp-commands.h" |
c09e5bb1 | 29 | #include "trace.h" |
df4b1024 | 30 | #include "qapi/util.h" |
598cd2bd | 31 | #include "qapi-event.h" |
070afca2 | 32 | #include "qom/cpu.h" |
065e2813 | 33 | |
dc325627 | 34 | #define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */ |
5bb7910a | 35 | |
5b4e1eb7 JQ |
36 | /* Amount of time to allocate to each "chunk" of bandwidth-throttled |
37 | * data. */ | |
38 | #define BUFFER_DELAY 100 | |
39 | #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY) | |
40 | ||
8706d2d5 LL |
41 | /* Default compression thread count */ |
42 | #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8 | |
3fcb38c2 LL |
43 | /* Default decompression thread count, usually decompression is at |
44 | * least 4 times as fast as compression.*/ | |
45 | #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2 | |
8706d2d5 LL |
46 | /*0: means nocompress, 1: best speed, ... 9: best compress ratio */ |
47 | #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1 | |
1626fee3 JH |
48 | /* Define default autoconverge cpu throttle migration parameters */ |
49 | #define DEFAULT_MIGRATE_X_CPU_THROTTLE_INITIAL 20 | |
50 | #define DEFAULT_MIGRATE_X_CPU_THROTTLE_INCREMENT 10 | |
8706d2d5 | 51 | |
17ad9b35 OW |
52 | /* Migration XBZRLE default cache size */ |
53 | #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024) | |
54 | ||
99a0db9b GH |
55 | static NotifierList migration_state_notifiers = |
56 | NOTIFIER_LIST_INITIALIZER(migration_state_notifiers); | |
57 | ||
adde220a DDAG |
58 | static bool deferred_incoming; |
59 | ||
093e3c42 DDAG |
60 | /* |
61 | * Current state of incoming postcopy; note this is not part of | |
62 | * MigrationIncomingState since it's state is used during cleanup | |
63 | * at the end as MIS is being freed. | |
64 | */ | |
65 | static PostcopyState incoming_postcopy_state; | |
66 | ||
17549e84 JQ |
67 | /* When we add fault tolerance, we could have several |
68 | migrations at once. For now we don't need to add | |
69 | dynamic creation of migration */ | |
70 | ||
bca7856a | 71 | /* For outgoing */ |
859bc756 | 72 | MigrationState *migrate_get_current(void) |
17549e84 JQ |
73 | { |
74 | static MigrationState current_migration = { | |
31194731 | 75 | .state = MIGRATION_STATUS_NONE, |
d0ae46c1 | 76 | .bandwidth_limit = MAX_THROTTLE, |
17ad9b35 | 77 | .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE, |
7e114f8c | 78 | .mbps = -1, |
43c60a81 LL |
79 | .parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = |
80 | DEFAULT_MIGRATE_COMPRESS_LEVEL, | |
81 | .parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = | |
82 | DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT, | |
83 | .parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] = | |
84 | DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT, | |
1626fee3 JH |
85 | .parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] = |
86 | DEFAULT_MIGRATE_X_CPU_THROTTLE_INITIAL, | |
87 | .parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] = | |
88 | DEFAULT_MIGRATE_X_CPU_THROTTLE_INCREMENT, | |
17549e84 JQ |
89 | }; |
90 | ||
91 | return ¤t_migration; | |
92 | } | |
93 | ||
bca7856a DDAG |
94 | /* For incoming */ |
95 | static MigrationIncomingState *mis_current; | |
96 | ||
97 | MigrationIncomingState *migration_incoming_get_current(void) | |
98 | { | |
99 | return mis_current; | |
100 | } | |
101 | ||
102 | MigrationIncomingState *migration_incoming_state_new(QEMUFile* f) | |
103 | { | |
97f3ad35 | 104 | mis_current = g_new0(MigrationIncomingState, 1); |
42e2aa56 | 105 | mis_current->from_src_file = f; |
1a8f46f8 | 106 | QLIST_INIT(&mis_current->loadvm_handlers); |
6decec93 | 107 | qemu_mutex_init(&mis_current->rp_mutex); |
7b89bf27 | 108 | qemu_event_init(&mis_current->main_thread_load_event, false); |
bca7856a DDAG |
109 | |
110 | return mis_current; | |
111 | } | |
112 | ||
113 | void migration_incoming_state_destroy(void) | |
114 | { | |
7b89bf27 | 115 | qemu_event_destroy(&mis_current->main_thread_load_event); |
1a8f46f8 | 116 | loadvm_free_handlers(mis_current); |
bca7856a DDAG |
117 | g_free(mis_current); |
118 | mis_current = NULL; | |
119 | } | |
120 | ||
df4b1024 JQ |
121 | |
122 | typedef struct { | |
13d16814 | 123 | bool optional; |
df4b1024 JQ |
124 | uint32_t size; |
125 | uint8_t runstate[100]; | |
172c4356 JQ |
126 | RunState state; |
127 | bool received; | |
df4b1024 JQ |
128 | } GlobalState; |
129 | ||
130 | static GlobalState global_state; | |
131 | ||
560d027b | 132 | int global_state_store(void) |
df4b1024 JQ |
133 | { |
134 | if (!runstate_store((char *)global_state.runstate, | |
135 | sizeof(global_state.runstate))) { | |
136 | error_report("runstate name too big: %s", global_state.runstate); | |
137 | trace_migrate_state_too_big(); | |
138 | return -EINVAL; | |
139 | } | |
140 | return 0; | |
141 | } | |
142 | ||
c69adea4 AP |
143 | void global_state_store_running(void) |
144 | { | |
145 | const char *state = RunState_lookup[RUN_STATE_RUNNING]; | |
146 | strncpy((char *)global_state.runstate, | |
147 | state, sizeof(global_state.runstate)); | |
148 | } | |
149 | ||
172c4356 | 150 | static bool global_state_received(void) |
df4b1024 | 151 | { |
172c4356 JQ |
152 | return global_state.received; |
153 | } | |
154 | ||
155 | static RunState global_state_get_runstate(void) | |
156 | { | |
157 | return global_state.state; | |
df4b1024 JQ |
158 | } |
159 | ||
13d16814 JQ |
160 | void global_state_set_optional(void) |
161 | { | |
162 | global_state.optional = true; | |
163 | } | |
164 | ||
165 | static bool global_state_needed(void *opaque) | |
166 | { | |
167 | GlobalState *s = opaque; | |
168 | char *runstate = (char *)s->runstate; | |
169 | ||
170 | /* If it is not optional, it is mandatory */ | |
171 | ||
172 | if (s->optional == false) { | |
173 | return true; | |
174 | } | |
175 | ||
176 | /* If state is running or paused, it is not needed */ | |
177 | ||
178 | if (strcmp(runstate, "running") == 0 || | |
179 | strcmp(runstate, "paused") == 0) { | |
180 | return false; | |
181 | } | |
182 | ||
183 | /* for any other state it is needed */ | |
184 | return true; | |
185 | } | |
186 | ||
df4b1024 JQ |
187 | static int global_state_post_load(void *opaque, int version_id) |
188 | { | |
189 | GlobalState *s = opaque; | |
172c4356 JQ |
190 | Error *local_err = NULL; |
191 | int r; | |
df4b1024 JQ |
192 | char *runstate = (char *)s->runstate; |
193 | ||
172c4356 | 194 | s->received = true; |
df4b1024 JQ |
195 | trace_migrate_global_state_post_load(runstate); |
196 | ||
172c4356 | 197 | r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE_MAX, |
df4b1024 JQ |
198 | -1, &local_err); |
199 | ||
172c4356 JQ |
200 | if (r == -1) { |
201 | if (local_err) { | |
202 | error_report_err(local_err); | |
df4b1024 | 203 | } |
172c4356 | 204 | return -EINVAL; |
df4b1024 | 205 | } |
172c4356 | 206 | s->state = r; |
df4b1024 | 207 | |
172c4356 | 208 | return 0; |
df4b1024 JQ |
209 | } |
210 | ||
211 | static void global_state_pre_save(void *opaque) | |
212 | { | |
213 | GlobalState *s = opaque; | |
214 | ||
215 | trace_migrate_global_state_pre_save((char *)s->runstate); | |
216 | s->size = strlen((char *)s->runstate) + 1; | |
217 | } | |
218 | ||
219 | static const VMStateDescription vmstate_globalstate = { | |
220 | .name = "globalstate", | |
221 | .version_id = 1, | |
222 | .minimum_version_id = 1, | |
223 | .post_load = global_state_post_load, | |
224 | .pre_save = global_state_pre_save, | |
13d16814 | 225 | .needed = global_state_needed, |
df4b1024 JQ |
226 | .fields = (VMStateField[]) { |
227 | VMSTATE_UINT32(size, GlobalState), | |
228 | VMSTATE_BUFFER(runstate, GlobalState), | |
229 | VMSTATE_END_OF_LIST() | |
230 | }, | |
231 | }; | |
232 | ||
233 | void register_global_state(void) | |
234 | { | |
235 | /* We would use it independently that we receive it */ | |
236 | strcpy((char *)&global_state.runstate, ""); | |
172c4356 | 237 | global_state.received = false; |
df4b1024 JQ |
238 | vmstate_register(NULL, 0, &vmstate_globalstate, &global_state); |
239 | } | |
240 | ||
b05dc723 JQ |
241 | static void migrate_generate_event(int new_state) |
242 | { | |
243 | if (migrate_use_events()) { | |
244 | qapi_event_send_migration(new_state, &error_abort); | |
b05dc723 JQ |
245 | } |
246 | } | |
247 | ||
adde220a DDAG |
248 | /* |
249 | * Called on -incoming with a defer: uri. | |
250 | * The migration can be started later after any parameters have been | |
251 | * changed. | |
252 | */ | |
253 | static void deferred_incoming_migration(Error **errp) | |
254 | { | |
255 | if (deferred_incoming) { | |
256 | error_setg(errp, "Incoming migration already deferred"); | |
257 | } | |
258 | deferred_incoming = true; | |
259 | } | |
260 | ||
43eaae28 | 261 | void qemu_start_incoming_migration(const char *uri, Error **errp) |
5bb7910a | 262 | { |
34c9dd8e AL |
263 | const char *p; |
264 | ||
7cf1fe6d | 265 | qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort); |
adde220a DDAG |
266 | if (!strcmp(uri, "defer")) { |
267 | deferred_incoming_migration(errp); | |
268 | } else if (strstart(uri, "tcp:", &p)) { | |
43eaae28 | 269 | tcp_start_incoming_migration(p, errp); |
2da776db | 270 | #ifdef CONFIG_RDMA |
adde220a | 271 | } else if (strstart(uri, "rdma:", &p)) { |
2da776db MH |
272 | rdma_start_incoming_migration(p, errp); |
273 | #endif | |
065e2813 | 274 | #if !defined(WIN32) |
adde220a | 275 | } else if (strstart(uri, "exec:", &p)) { |
43eaae28 | 276 | exec_start_incoming_migration(p, errp); |
adde220a | 277 | } else if (strstart(uri, "unix:", &p)) { |
43eaae28 | 278 | unix_start_incoming_migration(p, errp); |
adde220a | 279 | } else if (strstart(uri, "fd:", &p)) { |
43eaae28 | 280 | fd_start_incoming_migration(p, errp); |
065e2813 | 281 | #endif |
adde220a | 282 | } else { |
312fd5f2 | 283 | error_setg(errp, "unknown migration protocol: %s", uri); |
8ca5e801 | 284 | } |
5bb7910a AL |
285 | } |
286 | ||
82a4da79 | 287 | static void process_incoming_migration_co(void *opaque) |
511c0231 | 288 | { |
82a4da79 | 289 | QEMUFile *f = opaque; |
5a8a30db | 290 | Error *local_err = NULL; |
1c12e1f5 PB |
291 | int ret; |
292 | ||
bca7856a | 293 | migration_incoming_state_new(f); |
093e3c42 | 294 | postcopy_state_set(POSTCOPY_INCOMING_NONE); |
7cf1fe6d | 295 | migrate_generate_event(MIGRATION_STATUS_ACTIVE); |
1c12e1f5 | 296 | ret = qemu_loadvm_state(f); |
bca7856a | 297 | |
1c12e1f5 | 298 | qemu_fclose(f); |
905f26f2 | 299 | free_xbzrle_decoded_buf(); |
bca7856a DDAG |
300 | migration_incoming_state_destroy(); |
301 | ||
1c12e1f5 | 302 | if (ret < 0) { |
7cf1fe6d | 303 | migrate_generate_event(MIGRATION_STATUS_FAILED); |
db80face | 304 | error_report("load of migration failed: %s", strerror(-ret)); |
3fcb38c2 | 305 | migrate_decompress_threads_join(); |
4aead692 | 306 | exit(EXIT_FAILURE); |
511c0231 | 307 | } |
511c0231 | 308 | |
0f15423c | 309 | /* Make sure all file formats flush their mutable metadata */ |
5a8a30db KW |
310 | bdrv_invalidate_cache_all(&local_err); |
311 | if (local_err) { | |
ed1f3e00 | 312 | migrate_generate_event(MIGRATION_STATUS_FAILED); |
97baf9d9 | 313 | error_report_err(local_err); |
3fcb38c2 | 314 | migrate_decompress_threads_join(); |
5a8a30db KW |
315 | exit(EXIT_FAILURE); |
316 | } | |
0f15423c | 317 | |
92e37622 AS |
318 | /* |
319 | * This must happen after all error conditions are dealt with and | |
320 | * we're sure the VM is going to be running on this host. | |
321 | */ | |
322 | qemu_announce_self(); | |
323 | ||
172c4356 JQ |
324 | /* If global state section was not received or we are in running |
325 | state, we need to obey autostart. Any other state is set with | |
326 | runstate_set. */ | |
df4b1024 | 327 | |
172c4356 JQ |
328 | if (!global_state_received() || |
329 | global_state_get_runstate() == RUN_STATE_RUNNING) { | |
df4b1024 JQ |
330 | if (autostart) { |
331 | vm_start(); | |
332 | } else { | |
333 | runstate_set(RUN_STATE_PAUSED); | |
334 | } | |
172c4356 JQ |
335 | } else { |
336 | runstate_set(global_state_get_runstate()); | |
f5bbfba1 | 337 | } |
3fcb38c2 | 338 | migrate_decompress_threads_join(); |
ed1f3e00 DDAG |
339 | /* |
340 | * This must happen after any state changes since as soon as an external | |
341 | * observer sees this event they might start to prod at the VM assuming | |
342 | * it's ready to use. | |
343 | */ | |
344 | migrate_generate_event(MIGRATION_STATUS_COMPLETED); | |
511c0231 JQ |
345 | } |
346 | ||
82a4da79 PB |
347 | void process_incoming_migration(QEMUFile *f) |
348 | { | |
349 | Coroutine *co = qemu_coroutine_create(process_incoming_migration_co); | |
350 | int fd = qemu_get_fd(f); | |
351 | ||
352 | assert(fd != -1); | |
3fcb38c2 | 353 | migrate_decompress_threads_create(); |
f9e8cacc | 354 | qemu_set_nonblock(fd); |
82a4da79 PB |
355 | qemu_coroutine_enter(co, f); |
356 | } | |
357 | ||
6decec93 DDAG |
358 | /* |
359 | * Send a message on the return channel back to the source | |
360 | * of the migration. | |
361 | */ | |
362 | void migrate_send_rp_message(MigrationIncomingState *mis, | |
363 | enum mig_rp_message_type message_type, | |
364 | uint16_t len, void *data) | |
365 | { | |
366 | trace_migrate_send_rp_message((int)message_type, len); | |
367 | qemu_mutex_lock(&mis->rp_mutex); | |
368 | qemu_put_be16(mis->to_src_file, (unsigned int)message_type); | |
369 | qemu_put_be16(mis->to_src_file, len); | |
370 | qemu_put_buffer(mis->to_src_file, data, len); | |
371 | qemu_fflush(mis->to_src_file); | |
372 | qemu_mutex_unlock(&mis->rp_mutex); | |
373 | } | |
374 | ||
375 | /* | |
376 | * Send a 'SHUT' message on the return channel with the given value | |
377 | * to indicate that we've finished with the RP. Non-0 value indicates | |
378 | * error. | |
379 | */ | |
380 | void migrate_send_rp_shut(MigrationIncomingState *mis, | |
381 | uint32_t value) | |
382 | { | |
383 | uint32_t buf; | |
384 | ||
385 | buf = cpu_to_be32(value); | |
386 | migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf); | |
387 | } | |
388 | ||
389 | /* | |
390 | * Send a 'PONG' message on the return channel with the given value | |
391 | * (normally in response to a 'PING') | |
392 | */ | |
393 | void migrate_send_rp_pong(MigrationIncomingState *mis, | |
394 | uint32_t value) | |
395 | { | |
396 | uint32_t buf; | |
397 | ||
398 | buf = cpu_to_be32(value); | |
399 | migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf); | |
400 | } | |
401 | ||
a0a3fd60 GC |
402 | /* amount of nanoseconds we are willing to wait for migration to be down. |
403 | * the choice of nanoseconds is because it is the maximum resolution that | |
404 | * get_clock() can achieve. It is an internal measure. All user-visible | |
405 | * units must be in seconds */ | |
f7cd55a0 | 406 | static uint64_t max_downtime = 300000000; |
a0a3fd60 GC |
407 | |
408 | uint64_t migrate_max_downtime(void) | |
409 | { | |
410 | return max_downtime; | |
411 | } | |
412 | ||
bbf6da32 OW |
413 | MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp) |
414 | { | |
415 | MigrationCapabilityStatusList *head = NULL; | |
416 | MigrationCapabilityStatusList *caps; | |
417 | MigrationState *s = migrate_get_current(); | |
418 | int i; | |
419 | ||
387eedeb | 420 | caps = NULL; /* silence compiler warning */ |
bbf6da32 OW |
421 | for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) { |
422 | if (head == NULL) { | |
423 | head = g_malloc0(sizeof(*caps)); | |
424 | caps = head; | |
425 | } else { | |
426 | caps->next = g_malloc0(sizeof(*caps)); | |
427 | caps = caps->next; | |
428 | } | |
429 | caps->value = | |
430 | g_malloc(sizeof(*caps->value)); | |
431 | caps->value->capability = i; | |
432 | caps->value->state = s->enabled_capabilities[i]; | |
433 | } | |
434 | ||
435 | return head; | |
436 | } | |
437 | ||
85de8323 LL |
438 | MigrationParameters *qmp_query_migrate_parameters(Error **errp) |
439 | { | |
440 | MigrationParameters *params; | |
441 | MigrationState *s = migrate_get_current(); | |
442 | ||
443 | params = g_malloc0(sizeof(*params)); | |
444 | params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL]; | |
445 | params->compress_threads = | |
446 | s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS]; | |
447 | params->decompress_threads = | |
448 | s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS]; | |
1626fee3 JH |
449 | params->x_cpu_throttle_initial = |
450 | s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL]; | |
451 | params->x_cpu_throttle_increment = | |
452 | s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT]; | |
85de8323 LL |
453 | |
454 | return params; | |
455 | } | |
456 | ||
f6844b99 DDAG |
457 | /* |
458 | * Return true if we're already in the middle of a migration | |
459 | * (i.e. any of the active or setup states) | |
460 | */ | |
461 | static bool migration_is_setup_or_active(int state) | |
462 | { | |
463 | switch (state) { | |
464 | case MIGRATION_STATUS_ACTIVE: | |
465 | case MIGRATION_STATUS_SETUP: | |
466 | return true; | |
467 | ||
468 | default: | |
469 | return false; | |
470 | ||
471 | } | |
472 | } | |
473 | ||
f36d55af OW |
474 | static void get_xbzrle_cache_stats(MigrationInfo *info) |
475 | { | |
476 | if (migrate_use_xbzrle()) { | |
477 | info->has_xbzrle_cache = true; | |
478 | info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache)); | |
479 | info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size(); | |
480 | info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred(); | |
481 | info->xbzrle_cache->pages = xbzrle_mig_pages_transferred(); | |
482 | info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss(); | |
8bc39233 | 483 | info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate(); |
f36d55af OW |
484 | info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow(); |
485 | } | |
486 | } | |
487 | ||
791e7c82 | 488 | MigrationInfo *qmp_query_migrate(Error **errp) |
5bb7910a | 489 | { |
791e7c82 | 490 | MigrationInfo *info = g_malloc0(sizeof(*info)); |
17549e84 JQ |
491 | MigrationState *s = migrate_get_current(); |
492 | ||
493 | switch (s->state) { | |
31194731 | 494 | case MIGRATION_STATUS_NONE: |
17549e84 JQ |
495 | /* no migration has happened ever */ |
496 | break; | |
31194731 | 497 | case MIGRATION_STATUS_SETUP: |
29ae8a41 | 498 | info->has_status = true; |
ed4fbd10 | 499 | info->has_total_time = false; |
29ae8a41 | 500 | break; |
31194731 HZ |
501 | case MIGRATION_STATUS_ACTIVE: |
502 | case MIGRATION_STATUS_CANCELLING: | |
791e7c82 | 503 | info->has_status = true; |
7aa939af | 504 | info->has_total_time = true; |
bc72ad67 | 505 | info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) |
7aa939af | 506 | - s->total_time; |
2c52ddf1 JQ |
507 | info->has_expected_downtime = true; |
508 | info->expected_downtime = s->expected_downtime; | |
ed4fbd10 MH |
509 | info->has_setup_time = true; |
510 | info->setup_time = s->setup_time; | |
17549e84 | 511 | |
791e7c82 LC |
512 | info->has_ram = true; |
513 | info->ram = g_malloc0(sizeof(*info->ram)); | |
514 | info->ram->transferred = ram_bytes_transferred(); | |
515 | info->ram->remaining = ram_bytes_remaining(); | |
516 | info->ram->total = ram_bytes_total(); | |
004d4c10 | 517 | info->ram->duplicate = dup_mig_pages_transferred(); |
f1c72795 | 518 | info->ram->skipped = skipped_mig_pages_transferred(); |
004d4c10 OW |
519 | info->ram->normal = norm_mig_pages_transferred(); |
520 | info->ram->normal_bytes = norm_mig_bytes_transferred(); | |
8d017193 | 521 | info->ram->dirty_pages_rate = s->dirty_pages_rate; |
7e114f8c | 522 | info->ram->mbps = s->mbps; |
58570ed8 | 523 | info->ram->dirty_sync_count = s->dirty_sync_count; |
8d017193 | 524 | |
17549e84 | 525 | if (blk_mig_active()) { |
791e7c82 LC |
526 | info->has_disk = true; |
527 | info->disk = g_malloc0(sizeof(*info->disk)); | |
528 | info->disk->transferred = blk_mig_bytes_transferred(); | |
529 | info->disk->remaining = blk_mig_bytes_remaining(); | |
530 | info->disk->total = blk_mig_bytes_total(); | |
ff8d81d8 | 531 | } |
f36d55af | 532 | |
4782893e JH |
533 | if (cpu_throttle_active()) { |
534 | info->has_x_cpu_throttle_percentage = true; | |
535 | info->x_cpu_throttle_percentage = cpu_throttle_get_percentage(); | |
536 | } | |
537 | ||
f36d55af | 538 | get_xbzrle_cache_stats(info); |
17549e84 | 539 | break; |
31194731 | 540 | case MIGRATION_STATUS_COMPLETED: |
f36d55af OW |
541 | get_xbzrle_cache_stats(info); |
542 | ||
791e7c82 | 543 | info->has_status = true; |
00c14997 | 544 | info->has_total_time = true; |
7aa939af | 545 | info->total_time = s->total_time; |
9c5a9fcf JQ |
546 | info->has_downtime = true; |
547 | info->downtime = s->downtime; | |
ed4fbd10 MH |
548 | info->has_setup_time = true; |
549 | info->setup_time = s->setup_time; | |
d5f8a570 JQ |
550 | |
551 | info->has_ram = true; | |
552 | info->ram = g_malloc0(sizeof(*info->ram)); | |
553 | info->ram->transferred = ram_bytes_transferred(); | |
554 | info->ram->remaining = 0; | |
555 | info->ram->total = ram_bytes_total(); | |
004d4c10 | 556 | info->ram->duplicate = dup_mig_pages_transferred(); |
f1c72795 | 557 | info->ram->skipped = skipped_mig_pages_transferred(); |
004d4c10 OW |
558 | info->ram->normal = norm_mig_pages_transferred(); |
559 | info->ram->normal_bytes = norm_mig_bytes_transferred(); | |
7e114f8c | 560 | info->ram->mbps = s->mbps; |
58570ed8 | 561 | info->ram->dirty_sync_count = s->dirty_sync_count; |
17549e84 | 562 | break; |
31194731 | 563 | case MIGRATION_STATUS_FAILED: |
791e7c82 | 564 | info->has_status = true; |
17549e84 | 565 | break; |
31194731 | 566 | case MIGRATION_STATUS_CANCELLED: |
791e7c82 | 567 | info->has_status = true; |
17549e84 | 568 | break; |
5bb7910a | 569 | } |
cde63fbe | 570 | info->status = s->state; |
791e7c82 LC |
571 | |
572 | return info; | |
5bb7910a AL |
573 | } |
574 | ||
00458433 OW |
575 | void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params, |
576 | Error **errp) | |
577 | { | |
578 | MigrationState *s = migrate_get_current(); | |
579 | MigrationCapabilityStatusList *cap; | |
580 | ||
f6844b99 | 581 | if (migration_is_setup_or_active(s->state)) { |
c6bd8c70 | 582 | error_setg(errp, QERR_MIGRATION_ACTIVE); |
00458433 OW |
583 | return; |
584 | } | |
585 | ||
586 | for (cap = params; cap; cap = cap->next) { | |
587 | s->enabled_capabilities[cap->value->capability] = cap->value->state; | |
588 | } | |
53dd370c DDAG |
589 | |
590 | if (migrate_postcopy_ram()) { | |
591 | if (migrate_use_compression()) { | |
592 | /* The decompression threads asynchronously write into RAM | |
593 | * rather than use the atomic copies needed to avoid | |
594 | * userfaulting. It should be possible to fix the decompression | |
595 | * threads for compatibility in future. | |
596 | */ | |
597 | error_report("Postcopy is not currently compatible with " | |
598 | "compression"); | |
599 | s->enabled_capabilities[MIGRATION_CAPABILITY_X_POSTCOPY_RAM] = | |
600 | false; | |
601 | } | |
602 | } | |
00458433 OW |
603 | } |
604 | ||
85de8323 LL |
605 | void qmp_migrate_set_parameters(bool has_compress_level, |
606 | int64_t compress_level, | |
607 | bool has_compress_threads, | |
608 | int64_t compress_threads, | |
609 | bool has_decompress_threads, | |
1626fee3 JH |
610 | int64_t decompress_threads, |
611 | bool has_x_cpu_throttle_initial, | |
612 | int64_t x_cpu_throttle_initial, | |
613 | bool has_x_cpu_throttle_increment, | |
614 | int64_t x_cpu_throttle_increment, Error **errp) | |
85de8323 LL |
615 | { |
616 | MigrationState *s = migrate_get_current(); | |
617 | ||
618 | if (has_compress_level && (compress_level < 0 || compress_level > 9)) { | |
c6bd8c70 MA |
619 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level", |
620 | "is invalid, it should be in the range of 0 to 9"); | |
85de8323 LL |
621 | return; |
622 | } | |
623 | if (has_compress_threads && | |
624 | (compress_threads < 1 || compress_threads > 255)) { | |
c6bd8c70 MA |
625 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
626 | "compress_threads", | |
627 | "is invalid, it should be in the range of 1 to 255"); | |
85de8323 LL |
628 | return; |
629 | } | |
630 | if (has_decompress_threads && | |
631 | (decompress_threads < 1 || decompress_threads > 255)) { | |
c6bd8c70 MA |
632 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
633 | "decompress_threads", | |
634 | "is invalid, it should be in the range of 1 to 255"); | |
85de8323 LL |
635 | return; |
636 | } | |
1626fee3 JH |
637 | if (has_x_cpu_throttle_initial && |
638 | (x_cpu_throttle_initial < 1 || x_cpu_throttle_initial > 99)) { | |
639 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, | |
640 | "x_cpu_throttle_initial", | |
641 | "an integer in the range of 1 to 99"); | |
642 | } | |
643 | if (has_x_cpu_throttle_increment && | |
644 | (x_cpu_throttle_increment < 1 || x_cpu_throttle_increment > 99)) { | |
645 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, | |
646 | "x_cpu_throttle_increment", | |
647 | "an integer in the range of 1 to 99"); | |
648 | } | |
85de8323 LL |
649 | |
650 | if (has_compress_level) { | |
651 | s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level; | |
652 | } | |
653 | if (has_compress_threads) { | |
654 | s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads; | |
655 | } | |
656 | if (has_decompress_threads) { | |
657 | s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] = | |
658 | decompress_threads; | |
659 | } | |
1626fee3 JH |
660 | if (has_x_cpu_throttle_initial) { |
661 | s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] = | |
662 | x_cpu_throttle_initial; | |
663 | } | |
664 | ||
665 | if (has_x_cpu_throttle_increment) { | |
666 | s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] = | |
667 | x_cpu_throttle_increment; | |
668 | } | |
85de8323 LL |
669 | } |
670 | ||
4886a1bc DDAG |
671 | void qmp_migrate_start_postcopy(Error **errp) |
672 | { | |
673 | MigrationState *s = migrate_get_current(); | |
674 | ||
675 | if (!migrate_postcopy_ram()) { | |
676 | error_setg(errp, "Enable postcopy with migration_set_capability before" | |
677 | " the start of migration"); | |
678 | return; | |
679 | } | |
680 | ||
681 | if (s->state == MIGRATION_STATUS_NONE) { | |
682 | error_setg(errp, "Postcopy must be started after migration has been" | |
683 | " started"); | |
684 | return; | |
685 | } | |
686 | /* | |
687 | * we don't error if migration has finished since that would be racy | |
688 | * with issuing this command. | |
689 | */ | |
690 | atomic_set(&s->start_postcopy, true); | |
691 | } | |
692 | ||
065e2813 AL |
693 | /* shared migration helpers */ |
694 | ||
51cf4c1a Z |
695 | static void migrate_set_state(MigrationState *s, int old_state, int new_state) |
696 | { | |
a5c17b5f | 697 | if (atomic_cmpxchg(&s->state, old_state, new_state) == old_state) { |
4ba4bc5e | 698 | trace_migrate_set_state(new_state); |
b05dc723 | 699 | migrate_generate_event(new_state); |
51cf4c1a Z |
700 | } |
701 | } | |
702 | ||
bb1fadc4 | 703 | static void migrate_fd_cleanup(void *opaque) |
065e2813 | 704 | { |
bb1fadc4 PB |
705 | MigrationState *s = opaque; |
706 | ||
707 | qemu_bh_delete(s->cleanup_bh); | |
708 | s->cleanup_bh = NULL; | |
709 | ||
065e2813 | 710 | if (s->file) { |
9013dca5 | 711 | trace_migrate_fd_cleanup(); |
404a7c05 PB |
712 | qemu_mutex_unlock_iothread(); |
713 | qemu_thread_join(&s->thread); | |
714 | qemu_mutex_lock_iothread(); | |
715 | ||
8706d2d5 | 716 | migrate_compress_threads_join(); |
6f190a06 PB |
717 | qemu_fclose(s->file); |
718 | s->file = NULL; | |
065e2813 AL |
719 | } |
720 | ||
31194731 | 721 | assert(s->state != MIGRATION_STATUS_ACTIVE); |
7a2c1721 | 722 | |
94f5a437 LL |
723 | if (s->state == MIGRATION_STATUS_CANCELLING) { |
724 | migrate_set_state(s, MIGRATION_STATUS_CANCELLING, | |
725 | MIGRATION_STATUS_CANCELLED); | |
7a2c1721 | 726 | } |
a3fa1d78 PB |
727 | |
728 | notifier_list_notify(&migration_state_notifiers, s); | |
065e2813 AL |
729 | } |
730 | ||
8b6b99b3 | 731 | void migrate_fd_error(MigrationState *s) |
065e2813 | 732 | { |
9013dca5 | 733 | trace_migrate_fd_error(); |
bb1fadc4 | 734 | assert(s->file == NULL); |
7844337d | 735 | migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED); |
bb1fadc4 | 736 | notifier_list_notify(&migration_state_notifiers, s); |
458cf28e JQ |
737 | } |
738 | ||
0edda1c4 | 739 | static void migrate_fd_cancel(MigrationState *s) |
065e2813 | 740 | { |
6f2b811a | 741 | int old_state ; |
a26ba26e | 742 | QEMUFile *f = migrate_get_current()->file; |
9013dca5 | 743 | trace_migrate_fd_cancel(); |
065e2813 | 744 | |
70b20477 DDAG |
745 | if (s->rp_state.from_dst_file) { |
746 | /* shutdown the rp socket, so causing the rp thread to shutdown */ | |
747 | qemu_file_shutdown(s->rp_state.from_dst_file); | |
748 | } | |
749 | ||
6f2b811a Z |
750 | do { |
751 | old_state = s->state; | |
f6844b99 | 752 | if (!migration_is_setup_or_active(old_state)) { |
6f2b811a Z |
753 | break; |
754 | } | |
31194731 HZ |
755 | migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING); |
756 | } while (s->state != MIGRATION_STATUS_CANCELLING); | |
a26ba26e DDAG |
757 | |
758 | /* | |
759 | * If we're unlucky the migration code might be stuck somewhere in a | |
760 | * send/write while the network has failed and is waiting to timeout; | |
761 | * if we've got shutdown(2) available then we can force it to quit. | |
762 | * The outgoing qemu file gets closed in migrate_fd_cleanup that is | |
763 | * called in a bh, so there is no race against this cancel. | |
764 | */ | |
31194731 | 765 | if (s->state == MIGRATION_STATUS_CANCELLING && f) { |
a26ba26e DDAG |
766 | qemu_file_shutdown(f); |
767 | } | |
065e2813 AL |
768 | } |
769 | ||
99a0db9b GH |
770 | void add_migration_state_change_notifier(Notifier *notify) |
771 | { | |
772 | notifier_list_add(&migration_state_notifiers, notify); | |
773 | } | |
774 | ||
775 | void remove_migration_state_change_notifier(Notifier *notify) | |
776 | { | |
31552529 | 777 | notifier_remove(notify); |
99a0db9b GH |
778 | } |
779 | ||
02edd2e7 | 780 | bool migration_in_setup(MigrationState *s) |
afe2df69 | 781 | { |
31194731 | 782 | return s->state == MIGRATION_STATUS_SETUP; |
afe2df69 GH |
783 | } |
784 | ||
7073693b | 785 | bool migration_has_finished(MigrationState *s) |
99a0db9b | 786 | { |
31194731 | 787 | return s->state == MIGRATION_STATUS_COMPLETED; |
99a0db9b | 788 | } |
0edda1c4 | 789 | |
afe2df69 GH |
790 | bool migration_has_failed(MigrationState *s) |
791 | { | |
31194731 HZ |
792 | return (s->state == MIGRATION_STATUS_CANCELLED || |
793 | s->state == MIGRATION_STATUS_FAILED); | |
afe2df69 GH |
794 | } |
795 | ||
aefeb18b | 796 | MigrationState *migrate_init(const MigrationParams *params) |
0edda1c4 | 797 | { |
17549e84 | 798 | MigrationState *s = migrate_get_current(); |
d0ae46c1 | 799 | int64_t bandwidth_limit = s->bandwidth_limit; |
bbf6da32 | 800 | bool enabled_capabilities[MIGRATION_CAPABILITY_MAX]; |
17ad9b35 | 801 | int64_t xbzrle_cache_size = s->xbzrle_cache_size; |
43c60a81 LL |
802 | int compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL]; |
803 | int compress_thread_count = | |
804 | s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS]; | |
805 | int decompress_thread_count = | |
806 | s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS]; | |
1626fee3 JH |
807 | int x_cpu_throttle_initial = |
808 | s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL]; | |
809 | int x_cpu_throttle_increment = | |
810 | s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT]; | |
bbf6da32 OW |
811 | |
812 | memcpy(enabled_capabilities, s->enabled_capabilities, | |
813 | sizeof(enabled_capabilities)); | |
0edda1c4 | 814 | |
17549e84 | 815 | memset(s, 0, sizeof(*s)); |
6607ae23 | 816 | s->params = *params; |
bbf6da32 OW |
817 | memcpy(s->enabled_capabilities, enabled_capabilities, |
818 | sizeof(enabled_capabilities)); | |
17ad9b35 | 819 | s->xbzrle_cache_size = xbzrle_cache_size; |
1299c631 | 820 | |
43c60a81 LL |
821 | s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level; |
822 | s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = | |
823 | compress_thread_count; | |
824 | s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] = | |
825 | decompress_thread_count; | |
1626fee3 JH |
826 | s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] = |
827 | x_cpu_throttle_initial; | |
828 | s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] = | |
829 | x_cpu_throttle_increment; | |
0edda1c4 | 830 | s->bandwidth_limit = bandwidth_limit; |
7844337d | 831 | migrate_set_state(s, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP); |
0edda1c4 | 832 | |
bc72ad67 | 833 | s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
0edda1c4 JQ |
834 | return s; |
835 | } | |
cab30143 | 836 | |
fa2756b7 AL |
837 | static GSList *migration_blockers; |
838 | ||
839 | void migrate_add_blocker(Error *reason) | |
840 | { | |
841 | migration_blockers = g_slist_prepend(migration_blockers, reason); | |
842 | } | |
843 | ||
844 | void migrate_del_blocker(Error *reason) | |
845 | { | |
846 | migration_blockers = g_slist_remove(migration_blockers, reason); | |
847 | } | |
848 | ||
bf1ae1f4 DDAG |
849 | void qmp_migrate_incoming(const char *uri, Error **errp) |
850 | { | |
851 | Error *local_err = NULL; | |
4debb5f5 | 852 | static bool once = true; |
bf1ae1f4 DDAG |
853 | |
854 | if (!deferred_incoming) { | |
4debb5f5 | 855 | error_setg(errp, "For use with '-incoming defer'"); |
bf1ae1f4 DDAG |
856 | return; |
857 | } | |
4debb5f5 DDAG |
858 | if (!once) { |
859 | error_setg(errp, "The incoming migration has already been started"); | |
860 | } | |
bf1ae1f4 DDAG |
861 | |
862 | qemu_start_incoming_migration(uri, &local_err); | |
863 | ||
864 | if (local_err) { | |
865 | error_propagate(errp, local_err); | |
866 | return; | |
867 | } | |
868 | ||
4debb5f5 | 869 | once = false; |
bf1ae1f4 DDAG |
870 | } |
871 | ||
e1c37d0e LC |
872 | void qmp_migrate(const char *uri, bool has_blk, bool blk, |
873 | bool has_inc, bool inc, bool has_detach, bool detach, | |
874 | Error **errp) | |
cab30143 | 875 | { |
be7059cd | 876 | Error *local_err = NULL; |
17549e84 | 877 | MigrationState *s = migrate_get_current(); |
6607ae23 | 878 | MigrationParams params; |
cab30143 | 879 | const char *p; |
cab30143 | 880 | |
8c0426ae PP |
881 | params.blk = has_blk && blk; |
882 | params.shared = has_inc && inc; | |
6607ae23 | 883 | |
f6844b99 | 884 | if (migration_is_setup_or_active(s->state) || |
31194731 | 885 | s->state == MIGRATION_STATUS_CANCELLING) { |
c6bd8c70 | 886 | error_setg(errp, QERR_MIGRATION_ACTIVE); |
e1c37d0e | 887 | return; |
cab30143 | 888 | } |
ca99993a DDAG |
889 | if (runstate_check(RUN_STATE_INMIGRATE)) { |
890 | error_setg(errp, "Guest is waiting for an incoming migration"); | |
891 | return; | |
892 | } | |
893 | ||
e1c37d0e LC |
894 | if (qemu_savevm_state_blocked(errp)) { |
895 | return; | |
cab30143 JQ |
896 | } |
897 | ||
fa2756b7 | 898 | if (migration_blockers) { |
e1c37d0e LC |
899 | *errp = error_copy(migration_blockers->data); |
900 | return; | |
fa2756b7 AL |
901 | } |
902 | ||
656a2334 JQ |
903 | /* We are starting a new migration, so we want to start in a clean |
904 | state. This change is only needed if previous migration | |
905 | failed/was cancelled. We don't use migrate_set_state() because | |
906 | we are setting the initial state, not changing it. */ | |
907 | s->state = MIGRATION_STATUS_NONE; | |
908 | ||
6607ae23 | 909 | s = migrate_init(¶ms); |
cab30143 JQ |
910 | |
911 | if (strstart(uri, "tcp:", &p)) { | |
f37afb5a | 912 | tcp_start_outgoing_migration(s, p, &local_err); |
2da776db | 913 | #ifdef CONFIG_RDMA |
41310c68 | 914 | } else if (strstart(uri, "rdma:", &p)) { |
2da776db MH |
915 | rdma_start_outgoing_migration(s, p, &local_err); |
916 | #endif | |
cab30143 JQ |
917 | #if !defined(WIN32) |
918 | } else if (strstart(uri, "exec:", &p)) { | |
f37afb5a | 919 | exec_start_outgoing_migration(s, p, &local_err); |
cab30143 | 920 | } else if (strstart(uri, "unix:", &p)) { |
f37afb5a | 921 | unix_start_outgoing_migration(s, p, &local_err); |
cab30143 | 922 | } else if (strstart(uri, "fd:", &p)) { |
f37afb5a | 923 | fd_start_outgoing_migration(s, p, &local_err); |
cab30143 | 924 | #endif |
99a0db9b | 925 | } else { |
c6bd8c70 MA |
926 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri", |
927 | "a valid migration protocol"); | |
7844337d | 928 | migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED); |
e1c37d0e | 929 | return; |
cab30143 JQ |
930 | } |
931 | ||
f37afb5a | 932 | if (local_err) { |
342ab8d1 | 933 | migrate_fd_error(s); |
f37afb5a | 934 | error_propagate(errp, local_err); |
e1c37d0e | 935 | return; |
1299c631 | 936 | } |
cab30143 JQ |
937 | } |
938 | ||
6cdedb07 | 939 | void qmp_migrate_cancel(Error **errp) |
cab30143 | 940 | { |
17549e84 | 941 | migrate_fd_cancel(migrate_get_current()); |
cab30143 JQ |
942 | } |
943 | ||
9e1ba4cc OW |
944 | void qmp_migrate_set_cache_size(int64_t value, Error **errp) |
945 | { | |
946 | MigrationState *s = migrate_get_current(); | |
c91e681a | 947 | int64_t new_size; |
9e1ba4cc OW |
948 | |
949 | /* Check for truncation */ | |
950 | if (value != (size_t)value) { | |
c6bd8c70 MA |
951 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size", |
952 | "exceeding address space"); | |
9e1ba4cc OW |
953 | return; |
954 | } | |
955 | ||
a5615b14 OW |
956 | /* Cache should not be larger than guest ram size */ |
957 | if (value > ram_bytes_total()) { | |
c6bd8c70 MA |
958 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size", |
959 | "exceeds guest ram size "); | |
a5615b14 OW |
960 | return; |
961 | } | |
962 | ||
c91e681a OW |
963 | new_size = xbzrle_cache_resize(value); |
964 | if (new_size < 0) { | |
c6bd8c70 MA |
965 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size", |
966 | "is smaller than page size"); | |
c91e681a OW |
967 | return; |
968 | } | |
969 | ||
970 | s->xbzrle_cache_size = new_size; | |
9e1ba4cc OW |
971 | } |
972 | ||
973 | int64_t qmp_query_migrate_cache_size(Error **errp) | |
974 | { | |
975 | return migrate_xbzrle_cache_size(); | |
976 | } | |
977 | ||
3dc85383 | 978 | void qmp_migrate_set_speed(int64_t value, Error **errp) |
cab30143 | 979 | { |
cab30143 JQ |
980 | MigrationState *s; |
981 | ||
3dc85383 LC |
982 | if (value < 0) { |
983 | value = 0; | |
99a0db9b | 984 | } |
442773ce PB |
985 | if (value > SIZE_MAX) { |
986 | value = SIZE_MAX; | |
987 | } | |
cab30143 | 988 | |
17549e84 | 989 | s = migrate_get_current(); |
3dc85383 | 990 | s->bandwidth_limit = value; |
442773ce PB |
991 | if (s->file) { |
992 | qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO); | |
993 | } | |
cab30143 JQ |
994 | } |
995 | ||
4f0a993b | 996 | void qmp_migrate_set_downtime(double value, Error **errp) |
cab30143 | 997 | { |
4f0a993b LC |
998 | value *= 1e9; |
999 | value = MAX(0, MIN(UINT64_MAX, value)); | |
1000 | max_downtime = (uint64_t)value; | |
99a0db9b | 1001 | } |
17ad9b35 | 1002 | |
53dd370c DDAG |
1003 | bool migrate_postcopy_ram(void) |
1004 | { | |
1005 | MigrationState *s; | |
1006 | ||
1007 | s = migrate_get_current(); | |
1008 | ||
1009 | return s->enabled_capabilities[MIGRATION_CAPABILITY_X_POSTCOPY_RAM]; | |
1010 | } | |
1011 | ||
bde1e2ec CV |
1012 | bool migrate_auto_converge(void) |
1013 | { | |
1014 | MigrationState *s; | |
1015 | ||
1016 | s = migrate_get_current(); | |
1017 | ||
1018 | return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE]; | |
1019 | } | |
1020 | ||
323004a3 PL |
1021 | bool migrate_zero_blocks(void) |
1022 | { | |
1023 | MigrationState *s; | |
1024 | ||
1025 | s = migrate_get_current(); | |
1026 | ||
1027 | return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS]; | |
1028 | } | |
1029 | ||
8706d2d5 LL |
1030 | bool migrate_use_compression(void) |
1031 | { | |
dde4e694 LL |
1032 | MigrationState *s; |
1033 | ||
1034 | s = migrate_get_current(); | |
1035 | ||
1036 | return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS]; | |
8706d2d5 LL |
1037 | } |
1038 | ||
1039 | int migrate_compress_level(void) | |
1040 | { | |
1041 | MigrationState *s; | |
1042 | ||
1043 | s = migrate_get_current(); | |
1044 | ||
43c60a81 | 1045 | return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL]; |
8706d2d5 LL |
1046 | } |
1047 | ||
1048 | int migrate_compress_threads(void) | |
1049 | { | |
1050 | MigrationState *s; | |
1051 | ||
1052 | s = migrate_get_current(); | |
1053 | ||
43c60a81 | 1054 | return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS]; |
8706d2d5 LL |
1055 | } |
1056 | ||
3fcb38c2 LL |
1057 | int migrate_decompress_threads(void) |
1058 | { | |
1059 | MigrationState *s; | |
1060 | ||
1061 | s = migrate_get_current(); | |
1062 | ||
43c60a81 | 1063 | return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS]; |
3fcb38c2 LL |
1064 | } |
1065 | ||
b05dc723 JQ |
1066 | bool migrate_use_events(void) |
1067 | { | |
1068 | MigrationState *s; | |
1069 | ||
1070 | s = migrate_get_current(); | |
1071 | ||
1072 | return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS]; | |
1073 | } | |
1074 | ||
17ad9b35 OW |
1075 | int migrate_use_xbzrle(void) |
1076 | { | |
1077 | MigrationState *s; | |
1078 | ||
1079 | s = migrate_get_current(); | |
1080 | ||
1081 | return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE]; | |
1082 | } | |
1083 | ||
1084 | int64_t migrate_xbzrle_cache_size(void) | |
1085 | { | |
1086 | MigrationState *s; | |
1087 | ||
1088 | s = migrate_get_current(); | |
1089 | ||
1090 | return s->xbzrle_cache_size; | |
1091 | } | |
0d82d0e8 | 1092 | |
70b20477 DDAG |
1093 | /* migration thread support */ |
1094 | /* | |
1095 | * Something bad happened to the RP stream, mark an error | |
1096 | * The caller shall print or trace something to indicate why | |
1097 | */ | |
1098 | static void mark_source_rp_bad(MigrationState *s) | |
1099 | { | |
1100 | s->rp_state.error = true; | |
1101 | } | |
1102 | ||
1103 | static struct rp_cmd_args { | |
1104 | ssize_t len; /* -1 = variable */ | |
1105 | const char *name; | |
1106 | } rp_cmd_args[] = { | |
1107 | [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" }, | |
1108 | [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" }, | |
1109 | [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" }, | |
1110 | [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" }, | |
1111 | }; | |
1112 | ||
1113 | /* | |
1114 | * Handles messages sent on the return path towards the source VM | |
1115 | * | |
1116 | */ | |
1117 | static void *source_return_path_thread(void *opaque) | |
1118 | { | |
1119 | MigrationState *ms = opaque; | |
1120 | QEMUFile *rp = ms->rp_state.from_dst_file; | |
1121 | uint16_t header_len, header_type; | |
1122 | const int max_len = 512; | |
1123 | uint8_t buf[max_len]; | |
1124 | uint32_t tmp32, sibling_error; | |
1125 | int res; | |
1126 | ||
1127 | trace_source_return_path_thread_entry(); | |
1128 | while (!ms->rp_state.error && !qemu_file_get_error(rp) && | |
1129 | migration_is_setup_or_active(ms->state)) { | |
1130 | trace_source_return_path_thread_loop_top(); | |
1131 | header_type = qemu_get_be16(rp); | |
1132 | header_len = qemu_get_be16(rp); | |
1133 | ||
1134 | if (header_type >= MIG_RP_MSG_MAX || | |
1135 | header_type == MIG_RP_MSG_INVALID) { | |
1136 | error_report("RP: Received invalid message 0x%04x length 0x%04x", | |
1137 | header_type, header_len); | |
1138 | mark_source_rp_bad(ms); | |
1139 | goto out; | |
1140 | } | |
1141 | ||
1142 | if ((rp_cmd_args[header_type].len != -1 && | |
1143 | header_len != rp_cmd_args[header_type].len) || | |
1144 | header_len > max_len) { | |
1145 | error_report("RP: Received '%s' message (0x%04x) with" | |
1146 | "incorrect length %d expecting %zu", | |
1147 | rp_cmd_args[header_type].name, header_type, header_len, | |
1148 | (size_t)rp_cmd_args[header_type].len); | |
1149 | mark_source_rp_bad(ms); | |
1150 | goto out; | |
1151 | } | |
1152 | ||
1153 | /* We know we've got a valid header by this point */ | |
1154 | res = qemu_get_buffer(rp, buf, header_len); | |
1155 | if (res != header_len) { | |
1156 | error_report("RP: Failed reading data for message 0x%04x" | |
1157 | " read %d expected %d", | |
1158 | header_type, res, header_len); | |
1159 | mark_source_rp_bad(ms); | |
1160 | goto out; | |
1161 | } | |
1162 | ||
1163 | /* OK, we have the message and the data */ | |
1164 | switch (header_type) { | |
1165 | case MIG_RP_MSG_SHUT: | |
1166 | sibling_error = be32_to_cpup((uint32_t *)buf); | |
1167 | trace_source_return_path_thread_shut(sibling_error); | |
1168 | if (sibling_error) { | |
1169 | error_report("RP: Sibling indicated error %d", sibling_error); | |
1170 | mark_source_rp_bad(ms); | |
1171 | } | |
1172 | /* | |
1173 | * We'll let the main thread deal with closing the RP | |
1174 | * we could do a shutdown(2) on it, but we're the only user | |
1175 | * anyway, so there's nothing gained. | |
1176 | */ | |
1177 | goto out; | |
1178 | ||
1179 | case MIG_RP_MSG_PONG: | |
1180 | tmp32 = be32_to_cpup((uint32_t *)buf); | |
1181 | trace_source_return_path_thread_pong(tmp32); | |
1182 | break; | |
1183 | ||
1184 | default: | |
1185 | break; | |
1186 | } | |
1187 | } | |
1188 | if (rp && qemu_file_get_error(rp)) { | |
1189 | trace_source_return_path_thread_bad_end(); | |
1190 | mark_source_rp_bad(ms); | |
1191 | } | |
1192 | ||
1193 | trace_source_return_path_thread_end(); | |
1194 | out: | |
1195 | ms->rp_state.from_dst_file = NULL; | |
1196 | qemu_fclose(rp); | |
1197 | return NULL; | |
1198 | } | |
1199 | ||
1200 | __attribute__ (( unused )) /* Until later in patch series */ | |
1201 | static int open_return_path_on_source(MigrationState *ms) | |
1202 | { | |
1203 | ||
1204 | ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->file); | |
1205 | if (!ms->rp_state.from_dst_file) { | |
1206 | return -1; | |
1207 | } | |
1208 | ||
1209 | trace_open_return_path_on_source(); | |
1210 | qemu_thread_create(&ms->rp_state.rp_thread, "return path", | |
1211 | source_return_path_thread, ms, QEMU_THREAD_JOINABLE); | |
1212 | ||
1213 | trace_open_return_path_on_source_continue(); | |
1214 | ||
1215 | return 0; | |
1216 | } | |
1217 | ||
1218 | __attribute__ (( unused )) /* Until later in patch series */ | |
1219 | /* Returns 0 if the RP was ok, otherwise there was an error on the RP */ | |
1220 | static int await_return_path_close_on_source(MigrationState *ms) | |
1221 | { | |
1222 | /* | |
1223 | * If this is a normal exit then the destination will send a SHUT and the | |
1224 | * rp_thread will exit, however if there's an error we need to cause | |
1225 | * it to exit. | |
1226 | */ | |
1227 | if (qemu_file_get_error(ms->file) && ms->rp_state.from_dst_file) { | |
1228 | /* | |
1229 | * shutdown(2), if we have it, will cause it to unblock if it's stuck | |
1230 | * waiting for the destination. | |
1231 | */ | |
1232 | qemu_file_shutdown(ms->rp_state.from_dst_file); | |
1233 | mark_source_rp_bad(ms); | |
1234 | } | |
1235 | trace_await_return_path_close_on_source_joining(); | |
1236 | qemu_thread_join(&ms->rp_state.rp_thread); | |
1237 | trace_await_return_path_close_on_source_close(); | |
1238 | return ms->rp_state.error; | |
1239 | } | |
1240 | ||
09f6c85e DDAG |
1241 | /** |
1242 | * migration_completion: Used by migration_thread when there's not much left. | |
1243 | * The caller 'breaks' the loop when this returns. | |
1244 | * | |
1245 | * @s: Current migration state | |
36f48567 | 1246 | * @current_active_state: The migration state we expect to be in |
09f6c85e DDAG |
1247 | * @*old_vm_running: Pointer to old_vm_running flag |
1248 | * @*start_time: Pointer to time to update | |
1249 | */ | |
36f48567 DDAG |
1250 | static void migration_completion(MigrationState *s, int current_active_state, |
1251 | bool *old_vm_running, | |
09f6c85e DDAG |
1252 | int64_t *start_time) |
1253 | { | |
1254 | int ret; | |
1255 | ||
1256 | qemu_mutex_lock_iothread(); | |
1257 | *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); | |
1258 | qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER); | |
1259 | *old_vm_running = runstate_is_running(); | |
1260 | ||
1261 | ret = global_state_store(); | |
1262 | if (!ret) { | |
1263 | ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE); | |
1264 | if (ret >= 0) { | |
1265 | qemu_file_set_rate_limit(s->file, INT64_MAX); | |
a3e06c3d | 1266 | qemu_savevm_state_complete_precopy(s->file); |
09f6c85e DDAG |
1267 | } |
1268 | } | |
1269 | qemu_mutex_unlock_iothread(); | |
1270 | ||
1271 | if (ret < 0) { | |
1272 | goto fail; | |
1273 | } | |
1274 | ||
1275 | if (qemu_file_get_error(s->file)) { | |
1276 | trace_migration_completion_file_err(); | |
1277 | goto fail; | |
1278 | } | |
1279 | ||
36f48567 | 1280 | migrate_set_state(s, current_active_state, MIGRATION_STATUS_COMPLETED); |
09f6c85e DDAG |
1281 | return; |
1282 | ||
1283 | fail: | |
36f48567 | 1284 | migrate_set_state(s, current_active_state, MIGRATION_STATUS_FAILED); |
09f6c85e DDAG |
1285 | } |
1286 | ||
70b20477 DDAG |
1287 | /* |
1288 | * Master migration thread on the source VM. | |
1289 | * It drives the migration and pumps the data down the outgoing channel. | |
1290 | */ | |
5f496a1b | 1291 | static void *migration_thread(void *opaque) |
0d82d0e8 | 1292 | { |
9848a404 | 1293 | MigrationState *s = opaque; |
bc72ad67 AB |
1294 | int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
1295 | int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST); | |
be7172e2 | 1296 | int64_t initial_bytes = 0; |
0d82d0e8 | 1297 | int64_t max_size = 0; |
a3fa1d78 | 1298 | int64_t start_time = initial_time; |
94f5a437 | 1299 | int64_t end_time; |
a3fa1d78 | 1300 | bool old_vm_running = false; |
76f5933a | 1301 | |
ab28bd23 PB |
1302 | rcu_register_thread(); |
1303 | ||
f796baa1 | 1304 | qemu_savevm_state_header(s->file); |
dba433c0 | 1305 | qemu_savevm_state_begin(s->file, &s->params); |
0d82d0e8 | 1306 | |
bc72ad67 | 1307 | s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start; |
31194731 | 1308 | migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE); |
29ae8a41 | 1309 | |
31194731 | 1310 | while (s->state == MIGRATION_STATUS_ACTIVE) { |
a3e879cd | 1311 | int64_t current_time; |
c369f40d | 1312 | uint64_t pending_size; |
0d82d0e8 | 1313 | |
a0ff044b | 1314 | if (!qemu_file_rate_limit(s->file)) { |
c31b098f DDAG |
1315 | uint64_t pend_post, pend_nonpost; |
1316 | ||
1317 | qemu_savevm_state_pending(s->file, max_size, &pend_nonpost, | |
1318 | &pend_post); | |
1319 | pending_size = pend_nonpost + pend_post; | |
1320 | trace_migrate_pending(pending_size, max_size, | |
1321 | pend_post, pend_nonpost); | |
b22ff1fb | 1322 | if (pending_size && pending_size >= max_size) { |
dba433c0 | 1323 | qemu_savevm_state_iterate(s->file); |
c369f40d | 1324 | } else { |
09f6c85e | 1325 | trace_migration_thread_low_pending(pending_size); |
36f48567 DDAG |
1326 | migration_completion(s, MIGRATION_STATUS_ACTIVE, |
1327 | &old_vm_running, &start_time); | |
09f6c85e | 1328 | break; |
c369f40d JQ |
1329 | } |
1330 | } | |
f4410a5d | 1331 | |
fd45ee2c | 1332 | if (qemu_file_get_error(s->file)) { |
31194731 HZ |
1333 | migrate_set_state(s, MIGRATION_STATUS_ACTIVE, |
1334 | MIGRATION_STATUS_FAILED); | |
fd45ee2c PB |
1335 | break; |
1336 | } | |
bc72ad67 | 1337 | current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
0d82d0e8 | 1338 | if (current_time >= initial_time + BUFFER_DELAY) { |
be7172e2 | 1339 | uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes; |
77417f10 | 1340 | uint64_t time_spent = current_time - initial_time; |
0d82d0e8 JQ |
1341 | double bandwidth = transferred_bytes / time_spent; |
1342 | max_size = bandwidth * migrate_max_downtime() / 1000000; | |
1343 | ||
7e114f8c MH |
1344 | s->mbps = time_spent ? (((double) transferred_bytes * 8.0) / |
1345 | ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1; | |
1346 | ||
9013dca5 AK |
1347 | trace_migrate_transferred(transferred_bytes, time_spent, |
1348 | bandwidth, max_size); | |
90f8ae72 JQ |
1349 | /* if we haven't sent anything, we don't want to recalculate |
1350 | 10000 is a small enough number for our purposes */ | |
1351 | if (s->dirty_bytes_rate && transferred_bytes > 10000) { | |
1352 | s->expected_downtime = s->dirty_bytes_rate / bandwidth; | |
1353 | } | |
0d82d0e8 | 1354 | |
1964a397 | 1355 | qemu_file_reset_rate_limit(s->file); |
0d82d0e8 | 1356 | initial_time = current_time; |
be7172e2 | 1357 | initial_bytes = qemu_ftell(s->file); |
0d82d0e8 | 1358 | } |
a0ff044b | 1359 | if (qemu_file_rate_limit(s->file)) { |
0d82d0e8 JQ |
1360 | /* usleep expects microseconds */ |
1361 | g_usleep((initial_time + BUFFER_DELAY - current_time)*1000); | |
1362 | } | |
a3fa1d78 PB |
1363 | } |
1364 | ||
070afca2 JH |
1365 | /* If we enabled cpu throttling for auto-converge, turn it off. */ |
1366 | cpu_throttle_stop(); | |
94f5a437 | 1367 | end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
070afca2 | 1368 | |
f4410a5d | 1369 | qemu_mutex_lock_iothread(); |
ea7415fa | 1370 | qemu_savevm_state_cleanup(); |
31194731 | 1371 | if (s->state == MIGRATION_STATUS_COMPLETED) { |
d6ed7312 | 1372 | uint64_t transferred_bytes = qemu_ftell(s->file); |
a3fa1d78 PB |
1373 | s->total_time = end_time - s->total_time; |
1374 | s->downtime = end_time - start_time; | |
d6ed7312 PL |
1375 | if (s->total_time) { |
1376 | s->mbps = (((double) transferred_bytes * 8.0) / | |
1377 | ((double) s->total_time)) / 1000; | |
1378 | } | |
a3fa1d78 PB |
1379 | runstate_set(RUN_STATE_POSTMIGRATE); |
1380 | } else { | |
1381 | if (old_vm_running) { | |
a3fa1d78 | 1382 | vm_start(); |
dba433c0 | 1383 | } |
0d82d0e8 | 1384 | } |
bb1fadc4 | 1385 | qemu_bh_schedule(s->cleanup_bh); |
dba433c0 | 1386 | qemu_mutex_unlock_iothread(); |
f4410a5d | 1387 | |
ab28bd23 | 1388 | rcu_unregister_thread(); |
0d82d0e8 JQ |
1389 | return NULL; |
1390 | } | |
1391 | ||
9848a404 | 1392 | void migrate_fd_connect(MigrationState *s) |
0d82d0e8 | 1393 | { |
cc283e3b JQ |
1394 | /* This is a best 1st approximation. ns to ms */ |
1395 | s->expected_downtime = max_downtime/1000000; | |
bb1fadc4 | 1396 | s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s); |
0d82d0e8 | 1397 | |
442773ce PB |
1398 | qemu_file_set_rate_limit(s->file, |
1399 | s->bandwidth_limit / XFER_LIMIT_RATIO); | |
1400 | ||
9287ac27 SH |
1401 | /* Notify before starting migration thread */ |
1402 | notifier_list_notify(&migration_state_notifiers, s); | |
1403 | ||
8706d2d5 | 1404 | migrate_compress_threads_create(); |
4900116e | 1405 | qemu_thread_create(&s->thread, "migration", migration_thread, s, |
bb1fadc4 | 1406 | QEMU_THREAD_JOINABLE); |
0d82d0e8 | 1407 | } |
093e3c42 DDAG |
1408 | |
1409 | PostcopyState postcopy_state_get(void) | |
1410 | { | |
1411 | return atomic_mb_read(&incoming_postcopy_state); | |
1412 | } | |
1413 | ||
1414 | /* Set the state and return the old state */ | |
1415 | PostcopyState postcopy_state_set(PostcopyState new_state) | |
1416 | { | |
1417 | return atomic_xchg(&incoming_postcopy_state, new_state); | |
1418 | } | |
1419 |