]>
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" | |
6a1751b7 | 17 | #include "qemu/main-loop.h" |
caf71f86 | 18 | #include "migration/migration.h" |
83c9089e | 19 | #include "monitor/monitor.h" |
0d82d0e8 | 20 | #include "migration/qemu-file.h" |
9c17d615 | 21 | #include "sysemu/sysemu.h" |
737e150e | 22 | #include "block/block.h" |
1de7afc9 | 23 | #include "qemu/sockets.h" |
caf71f86 | 24 | #include "migration/block.h" |
766bd176 | 25 | #include "qemu/thread.h" |
791e7c82 | 26 | #include "qmp-commands.h" |
c09e5bb1 | 27 | #include "trace.h" |
065e2813 | 28 | |
d0ae46c1 | 29 | #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */ |
5bb7910a | 30 | |
5b4e1eb7 JQ |
31 | /* Amount of time to allocate to each "chunk" of bandwidth-throttled |
32 | * data. */ | |
33 | #define BUFFER_DELAY 100 | |
34 | #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY) | |
35 | ||
8706d2d5 LL |
36 | /* Default compression thread count */ |
37 | #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8 | |
3fcb38c2 LL |
38 | /* Default decompression thread count, usually decompression is at |
39 | * least 4 times as fast as compression.*/ | |
40 | #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2 | |
8706d2d5 LL |
41 | /*0: means nocompress, 1: best speed, ... 9: best compress ratio */ |
42 | #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1 | |
43 | ||
17ad9b35 OW |
44 | /* Migration XBZRLE default cache size */ |
45 | #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024) | |
46 | ||
99a0db9b GH |
47 | static NotifierList migration_state_notifiers = |
48 | NOTIFIER_LIST_INITIALIZER(migration_state_notifiers); | |
49 | ||
adde220a DDAG |
50 | static bool deferred_incoming; |
51 | ||
17549e84 JQ |
52 | /* When we add fault tolerance, we could have several |
53 | migrations at once. For now we don't need to add | |
54 | dynamic creation of migration */ | |
55 | ||
859bc756 | 56 | MigrationState *migrate_get_current(void) |
17549e84 JQ |
57 | { |
58 | static MigrationState current_migration = { | |
31194731 | 59 | .state = MIGRATION_STATUS_NONE, |
d0ae46c1 | 60 | .bandwidth_limit = MAX_THROTTLE, |
17ad9b35 | 61 | .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE, |
7e114f8c | 62 | .mbps = -1, |
8706d2d5 | 63 | .compress_thread_count = DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT, |
3fcb38c2 | 64 | .decompress_thread_count = DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT, |
8706d2d5 | 65 | .compress_level = DEFAULT_MIGRATE_COMPRESS_LEVEL, |
17549e84 JQ |
66 | }; |
67 | ||
68 | return ¤t_migration; | |
69 | } | |
70 | ||
adde220a DDAG |
71 | /* |
72 | * Called on -incoming with a defer: uri. | |
73 | * The migration can be started later after any parameters have been | |
74 | * changed. | |
75 | */ | |
76 | static void deferred_incoming_migration(Error **errp) | |
77 | { | |
78 | if (deferred_incoming) { | |
79 | error_setg(errp, "Incoming migration already deferred"); | |
80 | } | |
81 | deferred_incoming = true; | |
82 | } | |
83 | ||
43eaae28 | 84 | void qemu_start_incoming_migration(const char *uri, Error **errp) |
5bb7910a | 85 | { |
34c9dd8e AL |
86 | const char *p; |
87 | ||
adde220a DDAG |
88 | if (!strcmp(uri, "defer")) { |
89 | deferred_incoming_migration(errp); | |
90 | } else if (strstart(uri, "tcp:", &p)) { | |
43eaae28 | 91 | tcp_start_incoming_migration(p, errp); |
2da776db | 92 | #ifdef CONFIG_RDMA |
adde220a | 93 | } else if (strstart(uri, "rdma:", &p)) { |
2da776db MH |
94 | rdma_start_incoming_migration(p, errp); |
95 | #endif | |
065e2813 | 96 | #if !defined(WIN32) |
adde220a | 97 | } else if (strstart(uri, "exec:", &p)) { |
43eaae28 | 98 | exec_start_incoming_migration(p, errp); |
adde220a | 99 | } else if (strstart(uri, "unix:", &p)) { |
43eaae28 | 100 | unix_start_incoming_migration(p, errp); |
adde220a | 101 | } else if (strstart(uri, "fd:", &p)) { |
43eaae28 | 102 | fd_start_incoming_migration(p, errp); |
065e2813 | 103 | #endif |
adde220a | 104 | } else { |
312fd5f2 | 105 | error_setg(errp, "unknown migration protocol: %s", uri); |
8ca5e801 | 106 | } |
5bb7910a AL |
107 | } |
108 | ||
82a4da79 | 109 | static void process_incoming_migration_co(void *opaque) |
511c0231 | 110 | { |
82a4da79 | 111 | QEMUFile *f = opaque; |
5a8a30db | 112 | Error *local_err = NULL; |
1c12e1f5 PB |
113 | int ret; |
114 | ||
115 | ret = qemu_loadvm_state(f); | |
116 | qemu_fclose(f); | |
905f26f2 | 117 | free_xbzrle_decoded_buf(); |
1c12e1f5 | 118 | if (ret < 0) { |
db80face | 119 | error_report("load of migration failed: %s", strerror(-ret)); |
3fcb38c2 | 120 | migrate_decompress_threads_join(); |
4aead692 | 121 | exit(EXIT_FAILURE); |
511c0231 JQ |
122 | } |
123 | qemu_announce_self(); | |
511c0231 | 124 | |
0f15423c | 125 | /* Make sure all file formats flush their mutable metadata */ |
5a8a30db KW |
126 | bdrv_invalidate_cache_all(&local_err); |
127 | if (local_err) { | |
97baf9d9 | 128 | error_report_err(local_err); |
3fcb38c2 | 129 | migrate_decompress_threads_join(); |
5a8a30db KW |
130 | exit(EXIT_FAILURE); |
131 | } | |
0f15423c | 132 | |
f5bbfba1 | 133 | if (autostart) { |
511c0231 | 134 | vm_start(); |
f5bbfba1 | 135 | } else { |
29ed72f1 | 136 | runstate_set(RUN_STATE_PAUSED); |
f5bbfba1 | 137 | } |
3fcb38c2 | 138 | migrate_decompress_threads_join(); |
511c0231 JQ |
139 | } |
140 | ||
82a4da79 PB |
141 | void process_incoming_migration(QEMUFile *f) |
142 | { | |
143 | Coroutine *co = qemu_coroutine_create(process_incoming_migration_co); | |
144 | int fd = qemu_get_fd(f); | |
145 | ||
146 | assert(fd != -1); | |
3fcb38c2 | 147 | migrate_decompress_threads_create(); |
f9e8cacc | 148 | qemu_set_nonblock(fd); |
82a4da79 PB |
149 | qemu_coroutine_enter(co, f); |
150 | } | |
151 | ||
a0a3fd60 GC |
152 | /* amount of nanoseconds we are willing to wait for migration to be down. |
153 | * the choice of nanoseconds is because it is the maximum resolution that | |
154 | * get_clock() can achieve. It is an internal measure. All user-visible | |
155 | * units must be in seconds */ | |
f7cd55a0 | 156 | static uint64_t max_downtime = 300000000; |
a0a3fd60 GC |
157 | |
158 | uint64_t migrate_max_downtime(void) | |
159 | { | |
160 | return max_downtime; | |
161 | } | |
162 | ||
bbf6da32 OW |
163 | MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp) |
164 | { | |
165 | MigrationCapabilityStatusList *head = NULL; | |
166 | MigrationCapabilityStatusList *caps; | |
167 | MigrationState *s = migrate_get_current(); | |
168 | int i; | |
169 | ||
387eedeb | 170 | caps = NULL; /* silence compiler warning */ |
bbf6da32 OW |
171 | for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) { |
172 | if (head == NULL) { | |
173 | head = g_malloc0(sizeof(*caps)); | |
174 | caps = head; | |
175 | } else { | |
176 | caps->next = g_malloc0(sizeof(*caps)); | |
177 | caps = caps->next; | |
178 | } | |
179 | caps->value = | |
180 | g_malloc(sizeof(*caps->value)); | |
181 | caps->value->capability = i; | |
182 | caps->value->state = s->enabled_capabilities[i]; | |
183 | } | |
184 | ||
185 | return head; | |
186 | } | |
187 | ||
f36d55af OW |
188 | static void get_xbzrle_cache_stats(MigrationInfo *info) |
189 | { | |
190 | if (migrate_use_xbzrle()) { | |
191 | info->has_xbzrle_cache = true; | |
192 | info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache)); | |
193 | info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size(); | |
194 | info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred(); | |
195 | info->xbzrle_cache->pages = xbzrle_mig_pages_transferred(); | |
196 | info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss(); | |
8bc39233 | 197 | info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate(); |
f36d55af OW |
198 | info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow(); |
199 | } | |
200 | } | |
201 | ||
791e7c82 | 202 | MigrationInfo *qmp_query_migrate(Error **errp) |
5bb7910a | 203 | { |
791e7c82 | 204 | MigrationInfo *info = g_malloc0(sizeof(*info)); |
17549e84 JQ |
205 | MigrationState *s = migrate_get_current(); |
206 | ||
207 | switch (s->state) { | |
31194731 | 208 | case MIGRATION_STATUS_NONE: |
17549e84 JQ |
209 | /* no migration has happened ever */ |
210 | break; | |
31194731 | 211 | case MIGRATION_STATUS_SETUP: |
29ae8a41 | 212 | info->has_status = true; |
ed4fbd10 | 213 | info->has_total_time = false; |
29ae8a41 | 214 | break; |
31194731 HZ |
215 | case MIGRATION_STATUS_ACTIVE: |
216 | case MIGRATION_STATUS_CANCELLING: | |
791e7c82 | 217 | info->has_status = true; |
7aa939af | 218 | info->has_total_time = true; |
bc72ad67 | 219 | info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) |
7aa939af | 220 | - s->total_time; |
2c52ddf1 JQ |
221 | info->has_expected_downtime = true; |
222 | info->expected_downtime = s->expected_downtime; | |
ed4fbd10 MH |
223 | info->has_setup_time = true; |
224 | info->setup_time = s->setup_time; | |
17549e84 | 225 | |
791e7c82 LC |
226 | info->has_ram = true; |
227 | info->ram = g_malloc0(sizeof(*info->ram)); | |
228 | info->ram->transferred = ram_bytes_transferred(); | |
229 | info->ram->remaining = ram_bytes_remaining(); | |
230 | info->ram->total = ram_bytes_total(); | |
004d4c10 | 231 | info->ram->duplicate = dup_mig_pages_transferred(); |
f1c72795 | 232 | info->ram->skipped = skipped_mig_pages_transferred(); |
004d4c10 OW |
233 | info->ram->normal = norm_mig_pages_transferred(); |
234 | info->ram->normal_bytes = norm_mig_bytes_transferred(); | |
8d017193 | 235 | info->ram->dirty_pages_rate = s->dirty_pages_rate; |
7e114f8c | 236 | info->ram->mbps = s->mbps; |
58570ed8 | 237 | info->ram->dirty_sync_count = s->dirty_sync_count; |
8d017193 | 238 | |
17549e84 | 239 | if (blk_mig_active()) { |
791e7c82 LC |
240 | info->has_disk = true; |
241 | info->disk = g_malloc0(sizeof(*info->disk)); | |
242 | info->disk->transferred = blk_mig_bytes_transferred(); | |
243 | info->disk->remaining = blk_mig_bytes_remaining(); | |
244 | info->disk->total = blk_mig_bytes_total(); | |
ff8d81d8 | 245 | } |
f36d55af OW |
246 | |
247 | get_xbzrle_cache_stats(info); | |
17549e84 | 248 | break; |
31194731 | 249 | case MIGRATION_STATUS_COMPLETED: |
f36d55af OW |
250 | get_xbzrle_cache_stats(info); |
251 | ||
791e7c82 | 252 | info->has_status = true; |
00c14997 | 253 | info->has_total_time = true; |
7aa939af | 254 | info->total_time = s->total_time; |
9c5a9fcf JQ |
255 | info->has_downtime = true; |
256 | info->downtime = s->downtime; | |
ed4fbd10 MH |
257 | info->has_setup_time = true; |
258 | info->setup_time = s->setup_time; | |
d5f8a570 JQ |
259 | |
260 | info->has_ram = true; | |
261 | info->ram = g_malloc0(sizeof(*info->ram)); | |
262 | info->ram->transferred = ram_bytes_transferred(); | |
263 | info->ram->remaining = 0; | |
264 | info->ram->total = ram_bytes_total(); | |
004d4c10 | 265 | info->ram->duplicate = dup_mig_pages_transferred(); |
f1c72795 | 266 | info->ram->skipped = skipped_mig_pages_transferred(); |
004d4c10 OW |
267 | info->ram->normal = norm_mig_pages_transferred(); |
268 | info->ram->normal_bytes = norm_mig_bytes_transferred(); | |
7e114f8c | 269 | info->ram->mbps = s->mbps; |
58570ed8 | 270 | info->ram->dirty_sync_count = s->dirty_sync_count; |
17549e84 | 271 | break; |
31194731 | 272 | case MIGRATION_STATUS_FAILED: |
791e7c82 | 273 | info->has_status = true; |
17549e84 | 274 | break; |
31194731 | 275 | case MIGRATION_STATUS_CANCELLED: |
791e7c82 | 276 | info->has_status = true; |
17549e84 | 277 | break; |
5bb7910a | 278 | } |
cde63fbe | 279 | info->status = s->state; |
791e7c82 LC |
280 | |
281 | return info; | |
5bb7910a AL |
282 | } |
283 | ||
00458433 OW |
284 | void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params, |
285 | Error **errp) | |
286 | { | |
287 | MigrationState *s = migrate_get_current(); | |
288 | MigrationCapabilityStatusList *cap; | |
289 | ||
31194731 HZ |
290 | if (s->state == MIGRATION_STATUS_ACTIVE || |
291 | s->state == MIGRATION_STATUS_SETUP) { | |
00458433 OW |
292 | error_set(errp, QERR_MIGRATION_ACTIVE); |
293 | return; | |
294 | } | |
295 | ||
296 | for (cap = params; cap; cap = cap->next) { | |
297 | s->enabled_capabilities[cap->value->capability] = cap->value->state; | |
298 | } | |
299 | } | |
300 | ||
065e2813 AL |
301 | /* shared migration helpers */ |
302 | ||
51cf4c1a Z |
303 | static void migrate_set_state(MigrationState *s, int old_state, int new_state) |
304 | { | |
305 | if (atomic_cmpxchg(&s->state, old_state, new_state) == new_state) { | |
306 | trace_migrate_set_state(new_state); | |
307 | } | |
308 | } | |
309 | ||
bb1fadc4 | 310 | static void migrate_fd_cleanup(void *opaque) |
065e2813 | 311 | { |
bb1fadc4 PB |
312 | MigrationState *s = opaque; |
313 | ||
314 | qemu_bh_delete(s->cleanup_bh); | |
315 | s->cleanup_bh = NULL; | |
316 | ||
065e2813 | 317 | if (s->file) { |
9013dca5 | 318 | trace_migrate_fd_cleanup(); |
404a7c05 PB |
319 | qemu_mutex_unlock_iothread(); |
320 | qemu_thread_join(&s->thread); | |
321 | qemu_mutex_lock_iothread(); | |
322 | ||
8706d2d5 | 323 | migrate_compress_threads_join(); |
6f190a06 PB |
324 | qemu_fclose(s->file); |
325 | s->file = NULL; | |
065e2813 AL |
326 | } |
327 | ||
31194731 | 328 | assert(s->state != MIGRATION_STATUS_ACTIVE); |
7a2c1721 | 329 | |
31194731 | 330 | if (s->state != MIGRATION_STATUS_COMPLETED) { |
7a2c1721 | 331 | qemu_savevm_state_cancel(); |
31194731 HZ |
332 | if (s->state == MIGRATION_STATUS_CANCELLING) { |
333 | migrate_set_state(s, MIGRATION_STATUS_CANCELLING, | |
334 | MIGRATION_STATUS_CANCELLED); | |
51cf4c1a | 335 | } |
7a2c1721 | 336 | } |
a3fa1d78 PB |
337 | |
338 | notifier_list_notify(&migration_state_notifiers, s); | |
065e2813 AL |
339 | } |
340 | ||
8b6b99b3 | 341 | void migrate_fd_error(MigrationState *s) |
065e2813 | 342 | { |
9013dca5 | 343 | trace_migrate_fd_error(); |
bb1fadc4 | 344 | assert(s->file == NULL); |
31194731 HZ |
345 | s->state = MIGRATION_STATUS_FAILED; |
346 | trace_migrate_set_state(MIGRATION_STATUS_FAILED); | |
bb1fadc4 | 347 | notifier_list_notify(&migration_state_notifiers, s); |
458cf28e JQ |
348 | } |
349 | ||
0edda1c4 | 350 | static void migrate_fd_cancel(MigrationState *s) |
065e2813 | 351 | { |
6f2b811a | 352 | int old_state ; |
a26ba26e | 353 | QEMUFile *f = migrate_get_current()->file; |
9013dca5 | 354 | trace_migrate_fd_cancel(); |
065e2813 | 355 | |
6f2b811a Z |
356 | do { |
357 | old_state = s->state; | |
31194731 HZ |
358 | if (old_state != MIGRATION_STATUS_SETUP && |
359 | old_state != MIGRATION_STATUS_ACTIVE) { | |
6f2b811a Z |
360 | break; |
361 | } | |
31194731 HZ |
362 | migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING); |
363 | } while (s->state != MIGRATION_STATUS_CANCELLING); | |
a26ba26e DDAG |
364 | |
365 | /* | |
366 | * If we're unlucky the migration code might be stuck somewhere in a | |
367 | * send/write while the network has failed and is waiting to timeout; | |
368 | * if we've got shutdown(2) available then we can force it to quit. | |
369 | * The outgoing qemu file gets closed in migrate_fd_cleanup that is | |
370 | * called in a bh, so there is no race against this cancel. | |
371 | */ | |
31194731 | 372 | if (s->state == MIGRATION_STATUS_CANCELLING && f) { |
a26ba26e DDAG |
373 | qemu_file_shutdown(f); |
374 | } | |
065e2813 AL |
375 | } |
376 | ||
99a0db9b GH |
377 | void add_migration_state_change_notifier(Notifier *notify) |
378 | { | |
379 | notifier_list_add(&migration_state_notifiers, notify); | |
380 | } | |
381 | ||
382 | void remove_migration_state_change_notifier(Notifier *notify) | |
383 | { | |
31552529 | 384 | notifier_remove(notify); |
99a0db9b GH |
385 | } |
386 | ||
02edd2e7 | 387 | bool migration_in_setup(MigrationState *s) |
afe2df69 | 388 | { |
31194731 | 389 | return s->state == MIGRATION_STATUS_SETUP; |
afe2df69 GH |
390 | } |
391 | ||
7073693b | 392 | bool migration_has_finished(MigrationState *s) |
99a0db9b | 393 | { |
31194731 | 394 | return s->state == MIGRATION_STATUS_COMPLETED; |
99a0db9b | 395 | } |
0edda1c4 | 396 | |
afe2df69 GH |
397 | bool migration_has_failed(MigrationState *s) |
398 | { | |
31194731 HZ |
399 | return (s->state == MIGRATION_STATUS_CANCELLED || |
400 | s->state == MIGRATION_STATUS_FAILED); | |
afe2df69 GH |
401 | } |
402 | ||
6607ae23 | 403 | static MigrationState *migrate_init(const MigrationParams *params) |
0edda1c4 | 404 | { |
17549e84 | 405 | MigrationState *s = migrate_get_current(); |
d0ae46c1 | 406 | int64_t bandwidth_limit = s->bandwidth_limit; |
bbf6da32 | 407 | bool enabled_capabilities[MIGRATION_CAPABILITY_MAX]; |
17ad9b35 | 408 | int64_t xbzrle_cache_size = s->xbzrle_cache_size; |
8706d2d5 LL |
409 | int compress_level = s->compress_level; |
410 | int compress_thread_count = s->compress_thread_count; | |
3fcb38c2 | 411 | int decompress_thread_count = s->decompress_thread_count; |
bbf6da32 OW |
412 | |
413 | memcpy(enabled_capabilities, s->enabled_capabilities, | |
414 | sizeof(enabled_capabilities)); | |
0edda1c4 | 415 | |
17549e84 | 416 | memset(s, 0, sizeof(*s)); |
6607ae23 | 417 | s->params = *params; |
bbf6da32 OW |
418 | memcpy(s->enabled_capabilities, enabled_capabilities, |
419 | sizeof(enabled_capabilities)); | |
17ad9b35 | 420 | s->xbzrle_cache_size = xbzrle_cache_size; |
1299c631 | 421 | |
8706d2d5 LL |
422 | s->compress_level = compress_level; |
423 | s->compress_thread_count = compress_thread_count; | |
3fcb38c2 | 424 | s->decompress_thread_count = decompress_thread_count; |
0edda1c4 | 425 | s->bandwidth_limit = bandwidth_limit; |
31194731 HZ |
426 | s->state = MIGRATION_STATUS_SETUP; |
427 | trace_migrate_set_state(MIGRATION_STATUS_SETUP); | |
0edda1c4 | 428 | |
bc72ad67 | 429 | s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
0edda1c4 JQ |
430 | return s; |
431 | } | |
cab30143 | 432 | |
fa2756b7 AL |
433 | static GSList *migration_blockers; |
434 | ||
435 | void migrate_add_blocker(Error *reason) | |
436 | { | |
437 | migration_blockers = g_slist_prepend(migration_blockers, reason); | |
438 | } | |
439 | ||
440 | void migrate_del_blocker(Error *reason) | |
441 | { | |
442 | migration_blockers = g_slist_remove(migration_blockers, reason); | |
443 | } | |
444 | ||
bf1ae1f4 DDAG |
445 | void qmp_migrate_incoming(const char *uri, Error **errp) |
446 | { | |
447 | Error *local_err = NULL; | |
4debb5f5 | 448 | static bool once = true; |
bf1ae1f4 DDAG |
449 | |
450 | if (!deferred_incoming) { | |
4debb5f5 | 451 | error_setg(errp, "For use with '-incoming defer'"); |
bf1ae1f4 DDAG |
452 | return; |
453 | } | |
4debb5f5 DDAG |
454 | if (!once) { |
455 | error_setg(errp, "The incoming migration has already been started"); | |
456 | } | |
bf1ae1f4 DDAG |
457 | |
458 | qemu_start_incoming_migration(uri, &local_err); | |
459 | ||
460 | if (local_err) { | |
461 | error_propagate(errp, local_err); | |
462 | return; | |
463 | } | |
464 | ||
4debb5f5 | 465 | once = false; |
bf1ae1f4 DDAG |
466 | } |
467 | ||
e1c37d0e LC |
468 | void qmp_migrate(const char *uri, bool has_blk, bool blk, |
469 | bool has_inc, bool inc, bool has_detach, bool detach, | |
470 | Error **errp) | |
cab30143 | 471 | { |
be7059cd | 472 | Error *local_err = NULL; |
17549e84 | 473 | MigrationState *s = migrate_get_current(); |
6607ae23 | 474 | MigrationParams params; |
cab30143 | 475 | const char *p; |
cab30143 | 476 | |
8c0426ae PP |
477 | params.blk = has_blk && blk; |
478 | params.shared = has_inc && inc; | |
6607ae23 | 479 | |
31194731 HZ |
480 | if (s->state == MIGRATION_STATUS_ACTIVE || |
481 | s->state == MIGRATION_STATUS_SETUP || | |
482 | s->state == MIGRATION_STATUS_CANCELLING) { | |
e1c37d0e LC |
483 | error_set(errp, QERR_MIGRATION_ACTIVE); |
484 | return; | |
cab30143 JQ |
485 | } |
486 | ||
ca99993a DDAG |
487 | if (runstate_check(RUN_STATE_INMIGRATE)) { |
488 | error_setg(errp, "Guest is waiting for an incoming migration"); | |
489 | return; | |
490 | } | |
491 | ||
e1c37d0e LC |
492 | if (qemu_savevm_state_blocked(errp)) { |
493 | return; | |
cab30143 JQ |
494 | } |
495 | ||
fa2756b7 | 496 | if (migration_blockers) { |
e1c37d0e LC |
497 | *errp = error_copy(migration_blockers->data); |
498 | return; | |
fa2756b7 AL |
499 | } |
500 | ||
6607ae23 | 501 | s = migrate_init(¶ms); |
cab30143 JQ |
502 | |
503 | if (strstart(uri, "tcp:", &p)) { | |
f37afb5a | 504 | tcp_start_outgoing_migration(s, p, &local_err); |
2da776db | 505 | #ifdef CONFIG_RDMA |
41310c68 | 506 | } else if (strstart(uri, "rdma:", &p)) { |
2da776db MH |
507 | rdma_start_outgoing_migration(s, p, &local_err); |
508 | #endif | |
cab30143 JQ |
509 | #if !defined(WIN32) |
510 | } else if (strstart(uri, "exec:", &p)) { | |
f37afb5a | 511 | exec_start_outgoing_migration(s, p, &local_err); |
cab30143 | 512 | } else if (strstart(uri, "unix:", &p)) { |
f37afb5a | 513 | unix_start_outgoing_migration(s, p, &local_err); |
cab30143 | 514 | } else if (strstart(uri, "fd:", &p)) { |
f37afb5a | 515 | fd_start_outgoing_migration(s, p, &local_err); |
cab30143 | 516 | #endif |
99a0db9b | 517 | } else { |
e1c37d0e | 518 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol"); |
31194731 | 519 | s->state = MIGRATION_STATUS_FAILED; |
e1c37d0e | 520 | return; |
cab30143 JQ |
521 | } |
522 | ||
f37afb5a | 523 | if (local_err) { |
342ab8d1 | 524 | migrate_fd_error(s); |
f37afb5a | 525 | error_propagate(errp, local_err); |
e1c37d0e | 526 | return; |
1299c631 | 527 | } |
cab30143 JQ |
528 | } |
529 | ||
6cdedb07 | 530 | void qmp_migrate_cancel(Error **errp) |
cab30143 | 531 | { |
17549e84 | 532 | migrate_fd_cancel(migrate_get_current()); |
cab30143 JQ |
533 | } |
534 | ||
9e1ba4cc OW |
535 | void qmp_migrate_set_cache_size(int64_t value, Error **errp) |
536 | { | |
537 | MigrationState *s = migrate_get_current(); | |
c91e681a | 538 | int64_t new_size; |
9e1ba4cc OW |
539 | |
540 | /* Check for truncation */ | |
541 | if (value != (size_t)value) { | |
542 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size", | |
543 | "exceeding address space"); | |
544 | return; | |
545 | } | |
546 | ||
a5615b14 OW |
547 | /* Cache should not be larger than guest ram size */ |
548 | if (value > ram_bytes_total()) { | |
549 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size", | |
550 | "exceeds guest ram size "); | |
551 | return; | |
552 | } | |
553 | ||
c91e681a OW |
554 | new_size = xbzrle_cache_resize(value); |
555 | if (new_size < 0) { | |
556 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size", | |
557 | "is smaller than page size"); | |
558 | return; | |
559 | } | |
560 | ||
561 | s->xbzrle_cache_size = new_size; | |
9e1ba4cc OW |
562 | } |
563 | ||
564 | int64_t qmp_query_migrate_cache_size(Error **errp) | |
565 | { | |
566 | return migrate_xbzrle_cache_size(); | |
567 | } | |
568 | ||
3dc85383 | 569 | void qmp_migrate_set_speed(int64_t value, Error **errp) |
cab30143 | 570 | { |
cab30143 JQ |
571 | MigrationState *s; |
572 | ||
3dc85383 LC |
573 | if (value < 0) { |
574 | value = 0; | |
99a0db9b | 575 | } |
442773ce PB |
576 | if (value > SIZE_MAX) { |
577 | value = SIZE_MAX; | |
578 | } | |
cab30143 | 579 | |
17549e84 | 580 | s = migrate_get_current(); |
3dc85383 | 581 | s->bandwidth_limit = value; |
442773ce PB |
582 | if (s->file) { |
583 | qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO); | |
584 | } | |
cab30143 JQ |
585 | } |
586 | ||
4f0a993b | 587 | void qmp_migrate_set_downtime(double value, Error **errp) |
cab30143 | 588 | { |
4f0a993b LC |
589 | value *= 1e9; |
590 | value = MAX(0, MIN(UINT64_MAX, value)); | |
591 | max_downtime = (uint64_t)value; | |
99a0db9b | 592 | } |
17ad9b35 | 593 | |
bde1e2ec CV |
594 | bool migrate_auto_converge(void) |
595 | { | |
596 | MigrationState *s; | |
597 | ||
598 | s = migrate_get_current(); | |
599 | ||
600 | return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE]; | |
601 | } | |
602 | ||
323004a3 PL |
603 | bool migrate_zero_blocks(void) |
604 | { | |
605 | MigrationState *s; | |
606 | ||
607 | s = migrate_get_current(); | |
608 | ||
609 | return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS]; | |
610 | } | |
611 | ||
8706d2d5 LL |
612 | bool migrate_use_compression(void) |
613 | { | |
614 | /* Disable compression before the patch series are applied */ | |
615 | return false; | |
616 | } | |
617 | ||
618 | int migrate_compress_level(void) | |
619 | { | |
620 | MigrationState *s; | |
621 | ||
622 | s = migrate_get_current(); | |
623 | ||
624 | return s->compress_level; | |
625 | } | |
626 | ||
627 | int migrate_compress_threads(void) | |
628 | { | |
629 | MigrationState *s; | |
630 | ||
631 | s = migrate_get_current(); | |
632 | ||
633 | return s->compress_thread_count; | |
634 | } | |
635 | ||
3fcb38c2 LL |
636 | int migrate_decompress_threads(void) |
637 | { | |
638 | MigrationState *s; | |
639 | ||
640 | s = migrate_get_current(); | |
641 | ||
642 | return s->decompress_thread_count; | |
643 | } | |
644 | ||
17ad9b35 OW |
645 | int migrate_use_xbzrle(void) |
646 | { | |
647 | MigrationState *s; | |
648 | ||
649 | s = migrate_get_current(); | |
650 | ||
651 | return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE]; | |
652 | } | |
653 | ||
654 | int64_t migrate_xbzrle_cache_size(void) | |
655 | { | |
656 | MigrationState *s; | |
657 | ||
658 | s = migrate_get_current(); | |
659 | ||
660 | return s->xbzrle_cache_size; | |
661 | } | |
0d82d0e8 JQ |
662 | |
663 | /* migration thread support */ | |
664 | ||
5f496a1b | 665 | static void *migration_thread(void *opaque) |
0d82d0e8 | 666 | { |
9848a404 | 667 | MigrationState *s = opaque; |
bc72ad67 AB |
668 | int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
669 | int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST); | |
be7172e2 | 670 | int64_t initial_bytes = 0; |
0d82d0e8 | 671 | int64_t max_size = 0; |
a3fa1d78 PB |
672 | int64_t start_time = initial_time; |
673 | bool old_vm_running = false; | |
76f5933a | 674 | |
dba433c0 | 675 | qemu_savevm_state_begin(s->file, &s->params); |
0d82d0e8 | 676 | |
bc72ad67 | 677 | s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start; |
31194731 | 678 | migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE); |
29ae8a41 | 679 | |
31194731 | 680 | while (s->state == MIGRATION_STATUS_ACTIVE) { |
a3e879cd | 681 | int64_t current_time; |
c369f40d | 682 | uint64_t pending_size; |
0d82d0e8 | 683 | |
a0ff044b | 684 | if (!qemu_file_rate_limit(s->file)) { |
c369f40d | 685 | pending_size = qemu_savevm_state_pending(s->file, max_size); |
9013dca5 | 686 | trace_migrate_pending(pending_size, max_size); |
b22ff1fb | 687 | if (pending_size && pending_size >= max_size) { |
dba433c0 | 688 | qemu_savevm_state_iterate(s->file); |
c369f40d | 689 | } else { |
0e1146a7 KW |
690 | int ret; |
691 | ||
32c835ba | 692 | qemu_mutex_lock_iothread(); |
bc72ad67 | 693 | start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
c369f40d | 694 | qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER); |
a3fa1d78 | 695 | old_vm_running = runstate_is_running(); |
0e1146a7 KW |
696 | |
697 | ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE); | |
698 | if (ret >= 0) { | |
40596834 | 699 | qemu_file_set_rate_limit(s->file, INT64_MAX); |
0e1146a7 KW |
700 | qemu_savevm_state_complete(s->file); |
701 | } | |
32c835ba | 702 | qemu_mutex_unlock_iothread(); |
0e1146a7 KW |
703 | |
704 | if (ret < 0) { | |
31194731 HZ |
705 | migrate_set_state(s, MIGRATION_STATUS_ACTIVE, |
706 | MIGRATION_STATUS_FAILED); | |
0e1146a7 KW |
707 | break; |
708 | } | |
709 | ||
059f896c | 710 | if (!qemu_file_get_error(s->file)) { |
31194731 HZ |
711 | migrate_set_state(s, MIGRATION_STATUS_ACTIVE, |
712 | MIGRATION_STATUS_COMPLETED); | |
059f896c PB |
713 | break; |
714 | } | |
c369f40d JQ |
715 | } |
716 | } | |
f4410a5d | 717 | |
fd45ee2c | 718 | if (qemu_file_get_error(s->file)) { |
31194731 HZ |
719 | migrate_set_state(s, MIGRATION_STATUS_ACTIVE, |
720 | MIGRATION_STATUS_FAILED); | |
fd45ee2c PB |
721 | break; |
722 | } | |
bc72ad67 | 723 | current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
0d82d0e8 | 724 | if (current_time >= initial_time + BUFFER_DELAY) { |
be7172e2 | 725 | uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes; |
77417f10 | 726 | uint64_t time_spent = current_time - initial_time; |
0d82d0e8 JQ |
727 | double bandwidth = transferred_bytes / time_spent; |
728 | max_size = bandwidth * migrate_max_downtime() / 1000000; | |
729 | ||
7e114f8c MH |
730 | s->mbps = time_spent ? (((double) transferred_bytes * 8.0) / |
731 | ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1; | |
732 | ||
9013dca5 AK |
733 | trace_migrate_transferred(transferred_bytes, time_spent, |
734 | bandwidth, max_size); | |
90f8ae72 JQ |
735 | /* if we haven't sent anything, we don't want to recalculate |
736 | 10000 is a small enough number for our purposes */ | |
737 | if (s->dirty_bytes_rate && transferred_bytes > 10000) { | |
738 | s->expected_downtime = s->dirty_bytes_rate / bandwidth; | |
739 | } | |
0d82d0e8 | 740 | |
1964a397 | 741 | qemu_file_reset_rate_limit(s->file); |
0d82d0e8 | 742 | initial_time = current_time; |
be7172e2 | 743 | initial_bytes = qemu_ftell(s->file); |
0d82d0e8 | 744 | } |
a0ff044b | 745 | if (qemu_file_rate_limit(s->file)) { |
0d82d0e8 JQ |
746 | /* usleep expects microseconds */ |
747 | g_usleep((initial_time + BUFFER_DELAY - current_time)*1000); | |
748 | } | |
a3fa1d78 PB |
749 | } |
750 | ||
f4410a5d | 751 | qemu_mutex_lock_iothread(); |
31194731 | 752 | if (s->state == MIGRATION_STATUS_COMPLETED) { |
bc72ad67 | 753 | int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
d6ed7312 | 754 | uint64_t transferred_bytes = qemu_ftell(s->file); |
a3fa1d78 PB |
755 | s->total_time = end_time - s->total_time; |
756 | s->downtime = end_time - start_time; | |
d6ed7312 PL |
757 | if (s->total_time) { |
758 | s->mbps = (((double) transferred_bytes * 8.0) / | |
759 | ((double) s->total_time)) / 1000; | |
760 | } | |
a3fa1d78 PB |
761 | runstate_set(RUN_STATE_POSTMIGRATE); |
762 | } else { | |
763 | if (old_vm_running) { | |
a3fa1d78 | 764 | vm_start(); |
dba433c0 | 765 | } |
0d82d0e8 | 766 | } |
bb1fadc4 | 767 | qemu_bh_schedule(s->cleanup_bh); |
dba433c0 | 768 | qemu_mutex_unlock_iothread(); |
f4410a5d | 769 | |
0d82d0e8 JQ |
770 | return NULL; |
771 | } | |
772 | ||
9848a404 | 773 | void migrate_fd_connect(MigrationState *s) |
0d82d0e8 | 774 | { |
31194731 HZ |
775 | s->state = MIGRATION_STATUS_SETUP; |
776 | trace_migrate_set_state(MIGRATION_STATUS_SETUP); | |
c09e5bb1 | 777 | |
cc283e3b JQ |
778 | /* This is a best 1st approximation. ns to ms */ |
779 | s->expected_downtime = max_downtime/1000000; | |
bb1fadc4 | 780 | s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s); |
0d82d0e8 | 781 | |
442773ce PB |
782 | qemu_file_set_rate_limit(s->file, |
783 | s->bandwidth_limit / XFER_LIMIT_RATIO); | |
784 | ||
9287ac27 SH |
785 | /* Notify before starting migration thread */ |
786 | notifier_list_notify(&migration_state_notifiers, s); | |
787 | ||
8706d2d5 | 788 | migrate_compress_threads_create(); |
4900116e | 789 | qemu_thread_create(&s->thread, "migration", migration_thread, s, |
bb1fadc4 | 790 | QEMU_THREAD_JOINABLE); |
0d82d0e8 | 791 | } |