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