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