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