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