]>
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" | |
caf71f86 | 17 | #include "migration/migration.h" |
83c9089e | 18 | #include "monitor/monitor.h" |
0d82d0e8 | 19 | #include "migration/qemu-file.h" |
9c17d615 | 20 | #include "sysemu/sysemu.h" |
737e150e | 21 | #include "block/block.h" |
1de7afc9 | 22 | #include "qemu/sockets.h" |
caf71f86 | 23 | #include "migration/block.h" |
766bd176 | 24 | #include "qemu/thread.h" |
791e7c82 | 25 | #include "qmp-commands.h" |
065e2813 AL |
26 | |
27 | //#define DEBUG_MIGRATION | |
28 | ||
29 | #ifdef DEBUG_MIGRATION | |
d0f2c4c6 | 30 | #define DPRINTF(fmt, ...) \ |
065e2813 AL |
31 | do { printf("migration: " fmt, ## __VA_ARGS__); } while (0) |
32 | #else | |
d0f2c4c6 | 33 | #define DPRINTF(fmt, ...) \ |
065e2813 AL |
34 | do { } while (0) |
35 | #endif | |
5bb7910a | 36 | |
7dc688ed JQ |
37 | enum { |
38 | MIG_STATE_ERROR, | |
39 | MIG_STATE_SETUP, | |
40 | MIG_STATE_CANCELLED, | |
41 | MIG_STATE_ACTIVE, | |
42 | MIG_STATE_COMPLETED, | |
43 | }; | |
5bb7910a | 44 | |
d0ae46c1 | 45 | #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */ |
5bb7910a | 46 | |
5b4e1eb7 JQ |
47 | /* Amount of time to allocate to each "chunk" of bandwidth-throttled |
48 | * data. */ | |
49 | #define BUFFER_DELAY 100 | |
50 | #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY) | |
51 | ||
17ad9b35 OW |
52 | /* Migration XBZRLE default cache size */ |
53 | #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024) | |
54 | ||
99a0db9b GH |
55 | static NotifierList migration_state_notifiers = |
56 | NOTIFIER_LIST_INITIALIZER(migration_state_notifiers); | |
57 | ||
17549e84 JQ |
58 | /* When we add fault tolerance, we could have several |
59 | migrations at once. For now we don't need to add | |
60 | dynamic creation of migration */ | |
61 | ||
859bc756 | 62 | MigrationState *migrate_get_current(void) |
17549e84 JQ |
63 | { |
64 | static MigrationState current_migration = { | |
65 | .state = MIG_STATE_SETUP, | |
d0ae46c1 | 66 | .bandwidth_limit = MAX_THROTTLE, |
17ad9b35 | 67 | .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE, |
17549e84 JQ |
68 | }; |
69 | ||
70 | return ¤t_migration; | |
71 | } | |
72 | ||
43eaae28 | 73 | void qemu_start_incoming_migration(const char *uri, Error **errp) |
5bb7910a | 74 | { |
34c9dd8e AL |
75 | const char *p; |
76 | ||
77 | if (strstart(uri, "tcp:", &p)) | |
43eaae28 | 78 | tcp_start_incoming_migration(p, errp); |
065e2813 AL |
79 | #if !defined(WIN32) |
80 | else if (strstart(uri, "exec:", &p)) | |
43eaae28 | 81 | exec_start_incoming_migration(p, errp); |
4951f65b | 82 | else if (strstart(uri, "unix:", &p)) |
43eaae28 | 83 | unix_start_incoming_migration(p, errp); |
5ac1fad3 | 84 | else if (strstart(uri, "fd:", &p)) |
43eaae28 | 85 | fd_start_incoming_migration(p, errp); |
065e2813 | 86 | #endif |
8ca5e801 | 87 | else { |
43eaae28 | 88 | error_setg(errp, "unknown migration protocol: %s\n", uri); |
8ca5e801 | 89 | } |
5bb7910a AL |
90 | } |
91 | ||
82a4da79 | 92 | static void process_incoming_migration_co(void *opaque) |
511c0231 | 93 | { |
82a4da79 | 94 | QEMUFile *f = opaque; |
1c12e1f5 PB |
95 | int ret; |
96 | ||
97 | ret = qemu_loadvm_state(f); | |
82a4da79 | 98 | qemu_set_fd_handler(qemu_get_fd(f), NULL, NULL, NULL); |
1c12e1f5 PB |
99 | qemu_fclose(f); |
100 | if (ret < 0) { | |
511c0231 JQ |
101 | fprintf(stderr, "load of migration failed\n"); |
102 | exit(0); | |
103 | } | |
104 | qemu_announce_self(); | |
105 | DPRINTF("successfully loaded vm state\n"); | |
106 | ||
901862cb | 107 | bdrv_clear_incoming_migration_all(); |
0f15423c AL |
108 | /* Make sure all file formats flush their mutable metadata */ |
109 | bdrv_invalidate_cache_all(); | |
110 | ||
f5bbfba1 | 111 | if (autostart) { |
511c0231 | 112 | vm_start(); |
f5bbfba1 | 113 | } else { |
29ed72f1 | 114 | runstate_set(RUN_STATE_PAUSED); |
f5bbfba1 | 115 | } |
511c0231 JQ |
116 | } |
117 | ||
82a4da79 PB |
118 | static void enter_migration_coroutine(void *opaque) |
119 | { | |
120 | Coroutine *co = opaque; | |
121 | qemu_coroutine_enter(co, NULL); | |
122 | } | |
123 | ||
124 | void process_incoming_migration(QEMUFile *f) | |
125 | { | |
126 | Coroutine *co = qemu_coroutine_create(process_incoming_migration_co); | |
127 | int fd = qemu_get_fd(f); | |
128 | ||
129 | assert(fd != -1); | |
130 | socket_set_nonblock(fd); | |
131 | qemu_set_fd_handler(fd, enter_migration_coroutine, NULL, co); | |
132 | qemu_coroutine_enter(co, f); | |
133 | } | |
134 | ||
a0a3fd60 GC |
135 | /* amount of nanoseconds we are willing to wait for migration to be down. |
136 | * the choice of nanoseconds is because it is the maximum resolution that | |
137 | * get_clock() can achieve. It is an internal measure. All user-visible | |
138 | * units must be in seconds */ | |
139 | static uint64_t max_downtime = 30000000; | |
140 | ||
141 | uint64_t migrate_max_downtime(void) | |
142 | { | |
143 | return max_downtime; | |
144 | } | |
145 | ||
bbf6da32 OW |
146 | MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp) |
147 | { | |
148 | MigrationCapabilityStatusList *head = NULL; | |
149 | MigrationCapabilityStatusList *caps; | |
150 | MigrationState *s = migrate_get_current(); | |
151 | int i; | |
152 | ||
153 | for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) { | |
154 | if (head == NULL) { | |
155 | head = g_malloc0(sizeof(*caps)); | |
156 | caps = head; | |
157 | } else { | |
158 | caps->next = g_malloc0(sizeof(*caps)); | |
159 | caps = caps->next; | |
160 | } | |
161 | caps->value = | |
162 | g_malloc(sizeof(*caps->value)); | |
163 | caps->value->capability = i; | |
164 | caps->value->state = s->enabled_capabilities[i]; | |
165 | } | |
166 | ||
167 | return head; | |
168 | } | |
169 | ||
f36d55af OW |
170 | static void get_xbzrle_cache_stats(MigrationInfo *info) |
171 | { | |
172 | if (migrate_use_xbzrle()) { | |
173 | info->has_xbzrle_cache = true; | |
174 | info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache)); | |
175 | info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size(); | |
176 | info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred(); | |
177 | info->xbzrle_cache->pages = xbzrle_mig_pages_transferred(); | |
178 | info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss(); | |
179 | info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow(); | |
180 | } | |
181 | } | |
182 | ||
791e7c82 | 183 | MigrationInfo *qmp_query_migrate(Error **errp) |
5bb7910a | 184 | { |
791e7c82 | 185 | MigrationInfo *info = g_malloc0(sizeof(*info)); |
17549e84 JQ |
186 | MigrationState *s = migrate_get_current(); |
187 | ||
188 | switch (s->state) { | |
189 | case MIG_STATE_SETUP: | |
190 | /* no migration has happened ever */ | |
191 | break; | |
192 | case MIG_STATE_ACTIVE: | |
791e7c82 LC |
193 | info->has_status = true; |
194 | info->status = g_strdup("active"); | |
7aa939af JQ |
195 | info->has_total_time = true; |
196 | info->total_time = qemu_get_clock_ms(rt_clock) | |
197 | - s->total_time; | |
2c52ddf1 JQ |
198 | info->has_expected_downtime = true; |
199 | info->expected_downtime = s->expected_downtime; | |
17549e84 | 200 | |
791e7c82 LC |
201 | info->has_ram = true; |
202 | info->ram = g_malloc0(sizeof(*info->ram)); | |
203 | info->ram->transferred = ram_bytes_transferred(); | |
204 | info->ram->remaining = ram_bytes_remaining(); | |
205 | info->ram->total = ram_bytes_total(); | |
004d4c10 OW |
206 | info->ram->duplicate = dup_mig_pages_transferred(); |
207 | info->ram->normal = norm_mig_pages_transferred(); | |
208 | info->ram->normal_bytes = norm_mig_bytes_transferred(); | |
8d017193 JQ |
209 | info->ram->dirty_pages_rate = s->dirty_pages_rate; |
210 | ||
17549e84 JQ |
211 | |
212 | if (blk_mig_active()) { | |
791e7c82 LC |
213 | info->has_disk = true; |
214 | info->disk = g_malloc0(sizeof(*info->disk)); | |
215 | info->disk->transferred = blk_mig_bytes_transferred(); | |
216 | info->disk->remaining = blk_mig_bytes_remaining(); | |
217 | info->disk->total = blk_mig_bytes_total(); | |
ff8d81d8 | 218 | } |
f36d55af OW |
219 | |
220 | get_xbzrle_cache_stats(info); | |
17549e84 JQ |
221 | break; |
222 | case MIG_STATE_COMPLETED: | |
f36d55af OW |
223 | get_xbzrle_cache_stats(info); |
224 | ||
791e7c82 LC |
225 | info->has_status = true; |
226 | info->status = g_strdup("completed"); | |
7aa939af | 227 | info->total_time = s->total_time; |
9c5a9fcf JQ |
228 | info->has_downtime = true; |
229 | info->downtime = s->downtime; | |
d5f8a570 JQ |
230 | |
231 | info->has_ram = true; | |
232 | info->ram = g_malloc0(sizeof(*info->ram)); | |
233 | info->ram->transferred = ram_bytes_transferred(); | |
234 | info->ram->remaining = 0; | |
235 | info->ram->total = ram_bytes_total(); | |
004d4c10 OW |
236 | info->ram->duplicate = dup_mig_pages_transferred(); |
237 | info->ram->normal = norm_mig_pages_transferred(); | |
238 | info->ram->normal_bytes = norm_mig_bytes_transferred(); | |
17549e84 JQ |
239 | break; |
240 | case MIG_STATE_ERROR: | |
791e7c82 LC |
241 | info->has_status = true; |
242 | info->status = g_strdup("failed"); | |
17549e84 JQ |
243 | break; |
244 | case MIG_STATE_CANCELLED: | |
791e7c82 LC |
245 | info->has_status = true; |
246 | info->status = g_strdup("cancelled"); | |
17549e84 | 247 | break; |
5bb7910a | 248 | } |
791e7c82 LC |
249 | |
250 | return info; | |
5bb7910a AL |
251 | } |
252 | ||
00458433 OW |
253 | void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params, |
254 | Error **errp) | |
255 | { | |
256 | MigrationState *s = migrate_get_current(); | |
257 | MigrationCapabilityStatusList *cap; | |
258 | ||
259 | if (s->state == MIG_STATE_ACTIVE) { | |
260 | error_set(errp, QERR_MIGRATION_ACTIVE); | |
261 | return; | |
262 | } | |
263 | ||
264 | for (cap = params; cap; cap = cap->next) { | |
265 | s->enabled_capabilities[cap->value->capability] = cap->value->state; | |
266 | } | |
267 | } | |
268 | ||
065e2813 AL |
269 | /* shared migration helpers */ |
270 | ||
8b6b99b3 | 271 | static int migrate_fd_cleanup(MigrationState *s) |
065e2813 | 272 | { |
41ef56e6 AL |
273 | int ret = 0; |
274 | ||
065e2813 | 275 | if (s->file) { |
d0f2c4c6 | 276 | DPRINTF("closing file\n"); |
a6d34a94 | 277 | ret = qemu_fclose(s->file); |
5d39c799 | 278 | s->file = NULL; |
065e2813 AL |
279 | } |
280 | ||
24ea1e4b | 281 | assert(s->fd == -1); |
41ef56e6 | 282 | return ret; |
065e2813 AL |
283 | } |
284 | ||
8b6b99b3 | 285 | void migrate_fd_error(MigrationState *s) |
065e2813 | 286 | { |
8b6b99b3 JQ |
287 | DPRINTF("setting error state\n"); |
288 | s->state = MIG_STATE_ERROR; | |
e0eb7390 | 289 | notifier_list_notify(&migration_state_notifiers, s); |
8b6b99b3 JQ |
290 | migrate_fd_cleanup(s); |
291 | } | |
292 | ||
458cf28e JQ |
293 | static void migrate_fd_completed(MigrationState *s) |
294 | { | |
295 | DPRINTF("setting completed state\n"); | |
296 | if (migrate_fd_cleanup(s) < 0) { | |
297 | s->state = MIG_STATE_ERROR; | |
298 | } else { | |
299 | s->state = MIG_STATE_COMPLETED; | |
300 | runstate_set(RUN_STATE_POSTMIGRATE); | |
301 | } | |
e0eb7390 | 302 | notifier_list_notify(&migration_state_notifiers, s); |
458cf28e JQ |
303 | } |
304 | ||
b9c961a8 PB |
305 | static ssize_t migrate_fd_put_buffer(MigrationState *s, const void *data, |
306 | size_t size) | |
065e2813 | 307 | { |
065e2813 AL |
308 | ssize_t ret; |
309 | ||
fdbecb5d JQ |
310 | if (s->state != MIG_STATE_ACTIVE) { |
311 | return -EIO; | |
312 | } | |
313 | ||
065e2813 AL |
314 | do { |
315 | ret = s->write(s, data, size); | |
95b134ea | 316 | } while (ret == -1 && ((s->get_error(s)) == EINTR)); |
065e2813 AL |
317 | |
318 | if (ret == -1) | |
319 | ret = -(s->get_error(s)); | |
320 | ||
065e2813 AL |
321 | return ret; |
322 | } | |
323 | ||
0edda1c4 | 324 | static void migrate_fd_cancel(MigrationState *s) |
065e2813 | 325 | { |
065e2813 AL |
326 | if (s->state != MIG_STATE_ACTIVE) |
327 | return; | |
328 | ||
d0f2c4c6 | 329 | DPRINTF("cancelling migration\n"); |
065e2813 AL |
330 | |
331 | s->state = MIG_STATE_CANCELLED; | |
e0eb7390 | 332 | notifier_list_notify(&migration_state_notifiers, s); |
6522773f | 333 | qemu_savevm_state_cancel(); |
065e2813 AL |
334 | |
335 | migrate_fd_cleanup(s); | |
336 | } | |
337 | ||
11c76741 | 338 | int migrate_fd_close(MigrationState *s) |
065e2813 | 339 | { |
8dc592e6 PB |
340 | int rc = 0; |
341 | if (s->fd != -1) { | |
8dc592e6 PB |
342 | rc = s->close(s); |
343 | s->fd = -1; | |
344 | } | |
345 | return rc; | |
065e2813 | 346 | } |
99a0db9b GH |
347 | |
348 | void add_migration_state_change_notifier(Notifier *notify) | |
349 | { | |
350 | notifier_list_add(&migration_state_notifiers, notify); | |
351 | } | |
352 | ||
353 | void remove_migration_state_change_notifier(Notifier *notify) | |
354 | { | |
31552529 | 355 | notifier_remove(notify); |
99a0db9b GH |
356 | } |
357 | ||
afe2df69 GH |
358 | bool migration_is_active(MigrationState *s) |
359 | { | |
360 | return s->state == MIG_STATE_ACTIVE; | |
361 | } | |
362 | ||
7073693b | 363 | bool migration_has_finished(MigrationState *s) |
99a0db9b | 364 | { |
7073693b | 365 | return s->state == MIG_STATE_COMPLETED; |
99a0db9b | 366 | } |
0edda1c4 | 367 | |
afe2df69 GH |
368 | bool migration_has_failed(MigrationState *s) |
369 | { | |
370 | return (s->state == MIG_STATE_CANCELLED || | |
371 | s->state == MIG_STATE_ERROR); | |
372 | } | |
373 | ||
6607ae23 | 374 | static MigrationState *migrate_init(const MigrationParams *params) |
0edda1c4 | 375 | { |
17549e84 | 376 | MigrationState *s = migrate_get_current(); |
d0ae46c1 | 377 | int64_t bandwidth_limit = s->bandwidth_limit; |
bbf6da32 | 378 | bool enabled_capabilities[MIGRATION_CAPABILITY_MAX]; |
17ad9b35 | 379 | int64_t xbzrle_cache_size = s->xbzrle_cache_size; |
bbf6da32 OW |
380 | |
381 | memcpy(enabled_capabilities, s->enabled_capabilities, | |
382 | sizeof(enabled_capabilities)); | |
0edda1c4 | 383 | |
17549e84 | 384 | memset(s, 0, sizeof(*s)); |
d0ae46c1 | 385 | s->bandwidth_limit = bandwidth_limit; |
6607ae23 | 386 | s->params = *params; |
bbf6da32 OW |
387 | memcpy(s->enabled_capabilities, enabled_capabilities, |
388 | sizeof(enabled_capabilities)); | |
17ad9b35 | 389 | s->xbzrle_cache_size = xbzrle_cache_size; |
1299c631 | 390 | |
0edda1c4 | 391 | s->bandwidth_limit = bandwidth_limit; |
d5934dde | 392 | s->state = MIG_STATE_SETUP; |
d5f8a570 | 393 | s->total_time = qemu_get_clock_ms(rt_clock); |
0edda1c4 | 394 | |
0edda1c4 JQ |
395 | return s; |
396 | } | |
cab30143 | 397 | |
fa2756b7 AL |
398 | static GSList *migration_blockers; |
399 | ||
400 | void migrate_add_blocker(Error *reason) | |
401 | { | |
402 | migration_blockers = g_slist_prepend(migration_blockers, reason); | |
403 | } | |
404 | ||
405 | void migrate_del_blocker(Error *reason) | |
406 | { | |
407 | migration_blockers = g_slist_remove(migration_blockers, reason); | |
408 | } | |
409 | ||
e1c37d0e LC |
410 | void qmp_migrate(const char *uri, bool has_blk, bool blk, |
411 | bool has_inc, bool inc, bool has_detach, bool detach, | |
412 | Error **errp) | |
cab30143 | 413 | { |
be7059cd | 414 | Error *local_err = NULL; |
17549e84 | 415 | MigrationState *s = migrate_get_current(); |
6607ae23 | 416 | MigrationParams params; |
cab30143 | 417 | const char *p; |
cab30143 | 418 | |
6607ae23 IY |
419 | params.blk = blk; |
420 | params.shared = inc; | |
421 | ||
17549e84 | 422 | if (s->state == MIG_STATE_ACTIVE) { |
e1c37d0e LC |
423 | error_set(errp, QERR_MIGRATION_ACTIVE); |
424 | return; | |
cab30143 JQ |
425 | } |
426 | ||
e1c37d0e LC |
427 | if (qemu_savevm_state_blocked(errp)) { |
428 | return; | |
cab30143 JQ |
429 | } |
430 | ||
fa2756b7 | 431 | if (migration_blockers) { |
e1c37d0e LC |
432 | *errp = error_copy(migration_blockers->data); |
433 | return; | |
fa2756b7 AL |
434 | } |
435 | ||
6607ae23 | 436 | s = migrate_init(¶ms); |
cab30143 JQ |
437 | |
438 | if (strstart(uri, "tcp:", &p)) { | |
f37afb5a | 439 | tcp_start_outgoing_migration(s, p, &local_err); |
cab30143 JQ |
440 | #if !defined(WIN32) |
441 | } else if (strstart(uri, "exec:", &p)) { | |
f37afb5a | 442 | exec_start_outgoing_migration(s, p, &local_err); |
cab30143 | 443 | } else if (strstart(uri, "unix:", &p)) { |
f37afb5a | 444 | unix_start_outgoing_migration(s, p, &local_err); |
cab30143 | 445 | } else if (strstart(uri, "fd:", &p)) { |
f37afb5a | 446 | fd_start_outgoing_migration(s, p, &local_err); |
cab30143 | 447 | #endif |
99a0db9b | 448 | } else { |
e1c37d0e LC |
449 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol"); |
450 | return; | |
cab30143 JQ |
451 | } |
452 | ||
f37afb5a | 453 | if (local_err) { |
342ab8d1 | 454 | migrate_fd_error(s); |
f37afb5a | 455 | error_propagate(errp, local_err); |
e1c37d0e | 456 | return; |
1299c631 | 457 | } |
cab30143 JQ |
458 | } |
459 | ||
6cdedb07 | 460 | void qmp_migrate_cancel(Error **errp) |
cab30143 | 461 | { |
17549e84 | 462 | migrate_fd_cancel(migrate_get_current()); |
cab30143 JQ |
463 | } |
464 | ||
9e1ba4cc OW |
465 | void qmp_migrate_set_cache_size(int64_t value, Error **errp) |
466 | { | |
467 | MigrationState *s = migrate_get_current(); | |
468 | ||
469 | /* Check for truncation */ | |
470 | if (value != (size_t)value) { | |
471 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size", | |
472 | "exceeding address space"); | |
473 | return; | |
474 | } | |
475 | ||
476 | s->xbzrle_cache_size = xbzrle_cache_resize(value); | |
477 | } | |
478 | ||
479 | int64_t qmp_query_migrate_cache_size(Error **errp) | |
480 | { | |
481 | return migrate_xbzrle_cache_size(); | |
482 | } | |
483 | ||
3dc85383 | 484 | void qmp_migrate_set_speed(int64_t value, Error **errp) |
cab30143 | 485 | { |
cab30143 JQ |
486 | MigrationState *s; |
487 | ||
3dc85383 LC |
488 | if (value < 0) { |
489 | value = 0; | |
99a0db9b | 490 | } |
cab30143 | 491 | |
17549e84 | 492 | s = migrate_get_current(); |
3dc85383 | 493 | s->bandwidth_limit = value; |
d0ae46c1 | 494 | qemu_file_set_rate_limit(s->file, s->bandwidth_limit); |
cab30143 JQ |
495 | } |
496 | ||
4f0a993b | 497 | void qmp_migrate_set_downtime(double value, Error **errp) |
cab30143 | 498 | { |
4f0a993b LC |
499 | value *= 1e9; |
500 | value = MAX(0, MIN(UINT64_MAX, value)); | |
501 | max_downtime = (uint64_t)value; | |
99a0db9b | 502 | } |
17ad9b35 OW |
503 | |
504 | int migrate_use_xbzrle(void) | |
505 | { | |
506 | MigrationState *s; | |
507 | ||
508 | s = migrate_get_current(); | |
509 | ||
510 | return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE]; | |
511 | } | |
512 | ||
513 | int64_t migrate_xbzrle_cache_size(void) | |
514 | { | |
515 | MigrationState *s; | |
516 | ||
517 | s = migrate_get_current(); | |
518 | ||
519 | return s->xbzrle_cache_size; | |
520 | } | |
0d82d0e8 JQ |
521 | |
522 | /* migration thread support */ | |
523 | ||
9848a404 JQ |
524 | |
525 | static ssize_t buffered_flush(MigrationState *s) | |
0d82d0e8 JQ |
526 | { |
527 | size_t offset = 0; | |
528 | ssize_t ret = 0; | |
529 | ||
530 | DPRINTF("flushing %zu byte(s) of data\n", s->buffer_size); | |
531 | ||
532 | while (s->bytes_xfer < s->xfer_limit && offset < s->buffer_size) { | |
533 | size_t to_send = MIN(s->buffer_size - offset, s->xfer_limit - s->bytes_xfer); | |
9848a404 | 534 | ret = migrate_fd_put_buffer(s, s->buffer + offset, to_send); |
0d82d0e8 JQ |
535 | if (ret <= 0) { |
536 | DPRINTF("error flushing data, %zd\n", ret); | |
537 | break; | |
538 | } else { | |
539 | DPRINTF("flushed %zd byte(s)\n", ret); | |
540 | offset += ret; | |
541 | s->bytes_xfer += ret; | |
542 | } | |
543 | } | |
544 | ||
545 | DPRINTF("flushed %zu of %zu byte(s)\n", offset, s->buffer_size); | |
546 | memmove(s->buffer, s->buffer + offset, s->buffer_size - offset); | |
547 | s->buffer_size -= offset; | |
548 | ||
549 | if (ret < 0) { | |
550 | return ret; | |
551 | } | |
552 | return offset; | |
553 | } | |
554 | ||
555 | static int buffered_put_buffer(void *opaque, const uint8_t *buf, | |
556 | int64_t pos, int size) | |
557 | { | |
9848a404 | 558 | MigrationState *s = opaque; |
0d82d0e8 JQ |
559 | ssize_t error; |
560 | ||
561 | DPRINTF("putting %d bytes at %" PRId64 "\n", size, pos); | |
562 | ||
563 | error = qemu_file_get_error(s->file); | |
564 | if (error) { | |
565 | DPRINTF("flush when error, bailing: %s\n", strerror(-error)); | |
566 | return error; | |
567 | } | |
568 | ||
569 | if (size <= 0) { | |
570 | return size; | |
571 | } | |
572 | ||
573 | if (size > (s->buffer_capacity - s->buffer_size)) { | |
574 | DPRINTF("increasing buffer capacity from %zu by %zu\n", | |
575 | s->buffer_capacity, size + 1024); | |
576 | ||
577 | s->buffer_capacity += size + 1024; | |
578 | ||
579 | s->buffer = g_realloc(s->buffer, s->buffer_capacity); | |
580 | } | |
581 | ||
582 | memcpy(s->buffer + s->buffer_size, buf, size); | |
583 | s->buffer_size += size; | |
584 | ||
585 | return size; | |
586 | } | |
587 | ||
588 | static int buffered_close(void *opaque) | |
589 | { | |
9848a404 | 590 | MigrationState *s = opaque; |
0d82d0e8 JQ |
591 | ssize_t ret = 0; |
592 | int ret2; | |
593 | ||
594 | DPRINTF("closing\n"); | |
595 | ||
596 | s->xfer_limit = INT_MAX; | |
597 | while (!qemu_file_get_error(s->file) && s->buffer_size) { | |
598 | ret = buffered_flush(s); | |
599 | if (ret < 0) { | |
600 | break; | |
601 | } | |
602 | } | |
603 | ||
9848a404 | 604 | ret2 = migrate_fd_close(s); |
0d82d0e8 JQ |
605 | if (ret >= 0) { |
606 | ret = ret2; | |
607 | } | |
9848a404 | 608 | s->complete = true; |
0d82d0e8 JQ |
609 | return ret; |
610 | } | |
611 | ||
612 | static int buffered_get_fd(void *opaque) | |
613 | { | |
9848a404 | 614 | MigrationState *s = opaque; |
0d82d0e8 | 615 | |
9848a404 | 616 | return s->fd; |
0d82d0e8 JQ |
617 | } |
618 | ||
619 | /* | |
620 | * The meaning of the return values is: | |
621 | * 0: We can continue sending | |
622 | * 1: Time to stop | |
623 | * negative: There has been an error | |
624 | */ | |
625 | static int buffered_rate_limit(void *opaque) | |
626 | { | |
9848a404 | 627 | MigrationState *s = opaque; |
0d82d0e8 JQ |
628 | int ret; |
629 | ||
630 | ret = qemu_file_get_error(s->file); | |
631 | if (ret) { | |
632 | return ret; | |
633 | } | |
634 | ||
1e973051 | 635 | if (s->bytes_xfer >= s->xfer_limit) { |
0d82d0e8 JQ |
636 | return 1; |
637 | } | |
638 | ||
639 | return 0; | |
640 | } | |
641 | ||
642 | static int64_t buffered_set_rate_limit(void *opaque, int64_t new_rate) | |
643 | { | |
9848a404 | 644 | MigrationState *s = opaque; |
0d82d0e8 JQ |
645 | if (qemu_file_get_error(s->file)) { |
646 | goto out; | |
647 | } | |
648 | if (new_rate > SIZE_MAX) { | |
649 | new_rate = SIZE_MAX; | |
650 | } | |
651 | ||
f65a8747 | 652 | s->xfer_limit = new_rate / XFER_LIMIT_RATIO; |
0d82d0e8 JQ |
653 | |
654 | out: | |
655 | return s->xfer_limit; | |
656 | } | |
657 | ||
658 | static int64_t buffered_get_rate_limit(void *opaque) | |
659 | { | |
9848a404 | 660 | MigrationState *s = opaque; |
0d82d0e8 JQ |
661 | |
662 | return s->xfer_limit; | |
663 | } | |
664 | ||
0d82d0e8 JQ |
665 | static void *buffered_file_thread(void *opaque) |
666 | { | |
9848a404 | 667 | MigrationState *s = opaque; |
0d82d0e8 JQ |
668 | int64_t initial_time = qemu_get_clock_ms(rt_clock); |
669 | int64_t max_size = 0; | |
670 | bool last_round = false; | |
76f5933a JQ |
671 | int ret; |
672 | ||
673 | qemu_mutex_lock_iothread(); | |
674 | DPRINTF("beginning savevm\n"); | |
675 | ret = qemu_savevm_state_begin(s->file, &s->params); | |
676 | if (ret < 0) { | |
677 | DPRINTF("failed, %d\n", ret); | |
678 | qemu_mutex_unlock_iothread(); | |
679 | goto out; | |
680 | } | |
681 | qemu_mutex_unlock_iothread(); | |
0d82d0e8 JQ |
682 | |
683 | while (true) { | |
684 | int64_t current_time = qemu_get_clock_ms(rt_clock); | |
c369f40d | 685 | uint64_t pending_size; |
0d82d0e8 | 686 | |
7de6a690 JQ |
687 | qemu_mutex_lock_iothread(); |
688 | if (s->state != MIG_STATE_ACTIVE) { | |
689 | DPRINTF("put_ready returning because of non-active state\n"); | |
690 | qemu_mutex_unlock_iothread(); | |
691 | break; | |
692 | } | |
9848a404 | 693 | if (s->complete) { |
7de6a690 | 694 | qemu_mutex_unlock_iothread(); |
0d82d0e8 JQ |
695 | break; |
696 | } | |
c369f40d JQ |
697 | if (s->bytes_xfer < s->xfer_limit) { |
698 | DPRINTF("iterate\n"); | |
699 | pending_size = qemu_savevm_state_pending(s->file, max_size); | |
700 | DPRINTF("pending size %lu max %lu\n", pending_size, max_size); | |
b22ff1fb | 701 | if (pending_size && pending_size >= max_size) { |
c369f40d JQ |
702 | ret = qemu_savevm_state_iterate(s->file); |
703 | if (ret < 0) { | |
704 | qemu_mutex_unlock_iothread(); | |
705 | break; | |
706 | } | |
707 | } else { | |
708 | int old_vm_running = runstate_is_running(); | |
709 | int64_t start_time, end_time; | |
710 | ||
711 | DPRINTF("done iterating\n"); | |
712 | start_time = qemu_get_clock_ms(rt_clock); | |
713 | qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER); | |
714 | if (old_vm_running) { | |
715 | vm_stop(RUN_STATE_FINISH_MIGRATE); | |
716 | } else { | |
717 | vm_stop_force_state(RUN_STATE_FINISH_MIGRATE); | |
718 | } | |
719 | ret = qemu_savevm_state_complete(s->file); | |
720 | if (ret < 0) { | |
721 | qemu_mutex_unlock_iothread(); | |
722 | break; | |
723 | } else { | |
724 | migrate_fd_completed(s); | |
725 | } | |
726 | end_time = qemu_get_clock_ms(rt_clock); | |
727 | s->total_time = end_time - s->total_time; | |
728 | s->downtime = end_time - start_time; | |
729 | if (s->state != MIG_STATE_COMPLETED) { | |
730 | if (old_vm_running) { | |
731 | vm_start(); | |
732 | } | |
733 | } | |
734 | last_round = true; | |
735 | } | |
736 | } | |
7de6a690 | 737 | qemu_mutex_unlock_iothread(); |
0d82d0e8 JQ |
738 | if (current_time >= initial_time + BUFFER_DELAY) { |
739 | uint64_t transferred_bytes = s->bytes_xfer; | |
740 | uint64_t time_spent = current_time - initial_time; | |
741 | double bandwidth = transferred_bytes / time_spent; | |
742 | max_size = bandwidth * migrate_max_downtime() / 1000000; | |
743 | ||
744 | DPRINTF("transferred %" PRIu64 " time_spent %" PRIu64 | |
745 | " bandwidth %g max_size %" PRId64 "\n", | |
746 | transferred_bytes, time_spent, bandwidth, max_size); | |
747 | ||
748 | s->bytes_xfer = 0; | |
749 | initial_time = current_time; | |
750 | } | |
751 | if (!last_round && (s->bytes_xfer >= s->xfer_limit)) { | |
752 | /* usleep expects microseconds */ | |
753 | g_usleep((initial_time + BUFFER_DELAY - current_time)*1000); | |
754 | } | |
f7b67be3 JQ |
755 | ret = buffered_flush(s); |
756 | if (ret < 0) { | |
0d82d0e8 JQ |
757 | break; |
758 | } | |
0d82d0e8 JQ |
759 | } |
760 | ||
76f5933a JQ |
761 | out: |
762 | if (ret < 0) { | |
763 | migrate_fd_error(s); | |
764 | } | |
0d82d0e8 | 765 | g_free(s->buffer); |
0d82d0e8 JQ |
766 | return NULL; |
767 | } | |
768 | ||
769 | static const QEMUFileOps buffered_file_ops = { | |
770 | .get_fd = buffered_get_fd, | |
771 | .put_buffer = buffered_put_buffer, | |
772 | .close = buffered_close, | |
773 | .rate_limit = buffered_rate_limit, | |
774 | .get_rate_limit = buffered_get_rate_limit, | |
775 | .set_rate_limit = buffered_set_rate_limit, | |
776 | }; | |
777 | ||
9848a404 | 778 | void migrate_fd_connect(MigrationState *s) |
0d82d0e8 | 779 | { |
9848a404 JQ |
780 | s->state = MIG_STATE_ACTIVE; |
781 | s->bytes_xfer = 0; | |
782 | s->buffer = NULL; | |
783 | s->buffer_size = 0; | |
784 | s->buffer_capacity = 0; | |
0d82d0e8 | 785 | |
9848a404 JQ |
786 | s->xfer_limit = s->bandwidth_limit / XFER_LIMIT_RATIO; |
787 | s->complete = false; | |
0d82d0e8 JQ |
788 | |
789 | s->file = qemu_fopen_ops(s, &buffered_file_ops); | |
790 | ||
0d82d0e8 JQ |
791 | qemu_thread_create(&s->thread, buffered_file_thread, s, |
792 | QEMU_THREAD_DETACHED); | |
0d3b26f5 | 793 | notifier_list_notify(&migration_state_notifiers, s); |
0d82d0e8 | 794 | } |