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