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