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