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