]>
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 | * | |
12 | */ | |
13 | ||
14 | #include "qemu-common.h" | |
15 | #include "migration.h" | |
376253ec | 16 | #include "monitor.h" |
065e2813 AL |
17 | #include "buffered_file.h" |
18 | #include "sysemu.h" | |
19 | #include "block.h" | |
20 | #include "qemu_socket.h" | |
25f23643 | 21 | #include "block-migration.h" |
c86a6683 | 22 | #include "qemu-objects.h" |
065e2813 AL |
23 | |
24 | //#define DEBUG_MIGRATION | |
25 | ||
26 | #ifdef DEBUG_MIGRATION | |
d0f2c4c6 | 27 | #define DPRINTF(fmt, ...) \ |
065e2813 AL |
28 | do { printf("migration: " fmt, ## __VA_ARGS__); } while (0) |
29 | #else | |
d0f2c4c6 | 30 | #define DPRINTF(fmt, ...) \ |
065e2813 AL |
31 | do { } while (0) |
32 | #endif | |
5bb7910a | 33 | |
7dc688ed JQ |
34 | enum { |
35 | MIG_STATE_ERROR, | |
36 | MIG_STATE_SETUP, | |
37 | MIG_STATE_CANCELLED, | |
38 | MIG_STATE_ACTIVE, | |
39 | MIG_STATE_COMPLETED, | |
40 | }; | |
5bb7910a | 41 | |
d0ae46c1 | 42 | #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */ |
5bb7910a | 43 | |
99a0db9b GH |
44 | static NotifierList migration_state_notifiers = |
45 | NOTIFIER_LIST_INITIALIZER(migration_state_notifiers); | |
46 | ||
17549e84 JQ |
47 | /* When we add fault tolerance, we could have several |
48 | migrations at once. For now we don't need to add | |
49 | dynamic creation of migration */ | |
50 | ||
51 | static MigrationState *migrate_get_current(void) | |
52 | { | |
53 | static MigrationState current_migration = { | |
54 | .state = MIG_STATE_SETUP, | |
d0ae46c1 | 55 | .bandwidth_limit = MAX_THROTTLE, |
17549e84 JQ |
56 | }; |
57 | ||
58 | return ¤t_migration; | |
59 | } | |
60 | ||
8ca5e801 | 61 | int qemu_start_incoming_migration(const char *uri) |
5bb7910a | 62 | { |
34c9dd8e | 63 | const char *p; |
8ca5e801 | 64 | int ret; |
34c9dd8e AL |
65 | |
66 | if (strstart(uri, "tcp:", &p)) | |
8ca5e801 | 67 | ret = tcp_start_incoming_migration(p); |
065e2813 AL |
68 | #if !defined(WIN32) |
69 | else if (strstart(uri, "exec:", &p)) | |
8ca5e801 | 70 | ret = exec_start_incoming_migration(p); |
4951f65b | 71 | else if (strstart(uri, "unix:", &p)) |
8ca5e801 | 72 | ret = unix_start_incoming_migration(p); |
5ac1fad3 | 73 | else if (strstart(uri, "fd:", &p)) |
8ca5e801 | 74 | ret = fd_start_incoming_migration(p); |
065e2813 | 75 | #endif |
8ca5e801 | 76 | else { |
34c9dd8e | 77 | fprintf(stderr, "unknown migration protocol: %s\n", uri); |
8ca5e801 JQ |
78 | ret = -EPROTONOSUPPORT; |
79 | } | |
80 | return ret; | |
5bb7910a AL |
81 | } |
82 | ||
511c0231 JQ |
83 | void process_incoming_migration(QEMUFile *f) |
84 | { | |
85 | if (qemu_loadvm_state(f) < 0) { | |
86 | fprintf(stderr, "load of migration failed\n"); | |
87 | exit(0); | |
88 | } | |
89 | qemu_announce_self(); | |
90 | DPRINTF("successfully loaded vm state\n"); | |
91 | ||
f5bbfba1 | 92 | if (autostart) { |
511c0231 | 93 | vm_start(); |
f5bbfba1 | 94 | } else { |
0461d5a6 | 95 | runstate_set(RUN_STATE_PRELAUNCH); |
f5bbfba1 | 96 | } |
511c0231 JQ |
97 | } |
98 | ||
a0a3fd60 GC |
99 | /* amount of nanoseconds we are willing to wait for migration to be down. |
100 | * the choice of nanoseconds is because it is the maximum resolution that | |
101 | * get_clock() can achieve. It is an internal measure. All user-visible | |
102 | * units must be in seconds */ | |
103 | static uint64_t max_downtime = 30000000; | |
104 | ||
105 | uint64_t migrate_max_downtime(void) | |
106 | { | |
107 | return max_downtime; | |
108 | } | |
109 | ||
c86a6683 LC |
110 | static void migrate_print_status(Monitor *mon, const char *name, |
111 | const QDict *status_dict) | |
5bb7910a | 112 | { |
c86a6683 LC |
113 | QDict *qdict; |
114 | ||
115 | qdict = qobject_to_qdict(qdict_get(status_dict, name)); | |
116 | ||
117 | monitor_printf(mon, "transferred %s: %" PRIu64 " kbytes\n", name, | |
118 | qdict_get_int(qdict, "transferred") >> 10); | |
119 | monitor_printf(mon, "remaining %s: %" PRIu64 " kbytes\n", name, | |
120 | qdict_get_int(qdict, "remaining") >> 10); | |
121 | monitor_printf(mon, "total %s: %" PRIu64 " kbytes\n", name, | |
122 | qdict_get_int(qdict, "total") >> 10); | |
123 | } | |
124 | ||
125 | void do_info_migrate_print(Monitor *mon, const QObject *data) | |
126 | { | |
127 | QDict *qdict; | |
128 | ||
129 | qdict = qobject_to_qdict(data); | |
130 | ||
131 | monitor_printf(mon, "Migration status: %s\n", | |
132 | qdict_get_str(qdict, "status")); | |
133 | ||
134 | if (qdict_haskey(qdict, "ram")) { | |
135 | migrate_print_status(mon, "ram", qdict); | |
136 | } | |
137 | ||
138 | if (qdict_haskey(qdict, "disk")) { | |
139 | migrate_print_status(mon, "disk", qdict); | |
140 | } | |
141 | } | |
142 | ||
143 | static void migrate_put_status(QDict *qdict, const char *name, | |
144 | uint64_t trans, uint64_t rem, uint64_t total) | |
145 | { | |
146 | QObject *obj; | |
147 | ||
148 | obj = qobject_from_jsonf("{ 'transferred': %" PRId64 ", " | |
149 | "'remaining': %" PRId64 ", " | |
150 | "'total': %" PRId64 " }", trans, rem, total); | |
c86a6683 LC |
151 | qdict_put_obj(qdict, name, obj); |
152 | } | |
153 | ||
c86a6683 LC |
154 | void do_info_migrate(Monitor *mon, QObject **ret_data) |
155 | { | |
156 | QDict *qdict; | |
17549e84 JQ |
157 | MigrationState *s = migrate_get_current(); |
158 | ||
159 | switch (s->state) { | |
160 | case MIG_STATE_SETUP: | |
161 | /* no migration has happened ever */ | |
162 | break; | |
163 | case MIG_STATE_ACTIVE: | |
164 | qdict = qdict_new(); | |
165 | qdict_put(qdict, "status", qstring_from_str("active")); | |
166 | ||
167 | migrate_put_status(qdict, "ram", ram_bytes_transferred(), | |
168 | ram_bytes_remaining(), ram_bytes_total()); | |
169 | ||
170 | if (blk_mig_active()) { | |
171 | migrate_put_status(qdict, "disk", blk_mig_bytes_transferred(), | |
172 | blk_mig_bytes_remaining(), | |
173 | blk_mig_bytes_total()); | |
ff8d81d8 | 174 | } |
17549e84 JQ |
175 | |
176 | *ret_data = QOBJECT(qdict); | |
177 | break; | |
178 | case MIG_STATE_COMPLETED: | |
179 | *ret_data = qobject_from_jsonf("{ 'status': 'completed' }"); | |
180 | break; | |
181 | case MIG_STATE_ERROR: | |
182 | *ret_data = qobject_from_jsonf("{ 'status': 'failed' }"); | |
183 | break; | |
184 | case MIG_STATE_CANCELLED: | |
185 | *ret_data = qobject_from_jsonf("{ 'status': 'cancelled' }"); | |
186 | break; | |
5bb7910a AL |
187 | } |
188 | } | |
189 | ||
065e2813 AL |
190 | /* shared migration helpers */ |
191 | ||
0edda1c4 | 192 | static void migrate_fd_monitor_suspend(MigrationState *s, Monitor *mon) |
731b0364 | 193 | { |
f327aa0c JK |
194 | s->mon = mon; |
195 | if (monitor_suspend(mon) == 0) { | |
d0f2c4c6 | 196 | DPRINTF("suspending monitor\n"); |
f327aa0c JK |
197 | } else { |
198 | monitor_printf(mon, "terminal does not allow synchronous " | |
cde76ee1 | 199 | "migration, continuing detached\n"); |
f327aa0c | 200 | } |
731b0364 AL |
201 | } |
202 | ||
8b6b99b3 | 203 | static int migrate_fd_cleanup(MigrationState *s) |
065e2813 | 204 | { |
41ef56e6 AL |
205 | int ret = 0; |
206 | ||
065e2813 AL |
207 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
208 | ||
209 | if (s->file) { | |
d0f2c4c6 | 210 | DPRINTF("closing file\n"); |
41ef56e6 AL |
211 | if (qemu_fclose(s->file) != 0) { |
212 | ret = -1; | |
213 | } | |
5d39c799 | 214 | s->file = NULL; |
84ec6552 JK |
215 | } else { |
216 | if (s->mon) { | |
217 | monitor_resume(s->mon); | |
218 | } | |
065e2813 AL |
219 | } |
220 | ||
84ec6552 | 221 | if (s->fd != -1) { |
065e2813 | 222 | close(s->fd); |
84ec6552 | 223 | s->fd = -1; |
f327aa0c | 224 | } |
065e2813 | 225 | |
41ef56e6 | 226 | return ret; |
065e2813 AL |
227 | } |
228 | ||
8b6b99b3 | 229 | void migrate_fd_error(MigrationState *s) |
065e2813 | 230 | { |
8b6b99b3 JQ |
231 | DPRINTF("setting error state\n"); |
232 | s->state = MIG_STATE_ERROR; | |
e0eb7390 | 233 | notifier_list_notify(&migration_state_notifiers, s); |
8b6b99b3 JQ |
234 | migrate_fd_cleanup(s); |
235 | } | |
236 | ||
458cf28e JQ |
237 | static void migrate_fd_completed(MigrationState *s) |
238 | { | |
239 | DPRINTF("setting completed state\n"); | |
240 | if (migrate_fd_cleanup(s) < 0) { | |
241 | s->state = MIG_STATE_ERROR; | |
242 | } else { | |
243 | s->state = MIG_STATE_COMPLETED; | |
244 | runstate_set(RUN_STATE_POSTMIGRATE); | |
245 | } | |
e0eb7390 | 246 | notifier_list_notify(&migration_state_notifiers, s); |
458cf28e JQ |
247 | } |
248 | ||
8b6b99b3 | 249 | static void migrate_fd_put_notify(void *opaque) |
065e2813 | 250 | { |
22f00a44 | 251 | MigrationState *s = opaque; |
065e2813 AL |
252 | |
253 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); | |
254 | qemu_file_put_notify(s->file); | |
624b9cc2 | 255 | if (qemu_file_get_error(s->file)) { |
2350e13c YT |
256 | migrate_fd_error(s); |
257 | } | |
065e2813 AL |
258 | } |
259 | ||
8b6b99b3 JQ |
260 | static ssize_t migrate_fd_put_buffer(void *opaque, const void *data, |
261 | size_t size) | |
065e2813 | 262 | { |
22f00a44 | 263 | MigrationState *s = opaque; |
065e2813 AL |
264 | ssize_t ret; |
265 | ||
fdbecb5d JQ |
266 | if (s->state != MIG_STATE_ACTIVE) { |
267 | return -EIO; | |
268 | } | |
269 | ||
065e2813 AL |
270 | do { |
271 | ret = s->write(s, data, size); | |
95b134ea | 272 | } while (ret == -1 && ((s->get_error(s)) == EINTR)); |
065e2813 AL |
273 | |
274 | if (ret == -1) | |
275 | ret = -(s->get_error(s)); | |
276 | ||
e447b1a6 | 277 | if (ret == -EAGAIN) { |
065e2813 | 278 | qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s); |
e447b1a6 | 279 | } |
065e2813 AL |
280 | |
281 | return ret; | |
282 | } | |
283 | ||
8b6b99b3 | 284 | static void migrate_fd_put_ready(void *opaque) |
065e2813 | 285 | { |
22f00a44 | 286 | MigrationState *s = opaque; |
065e2813 AL |
287 | int ret; |
288 | ||
065e2813 | 289 | if (s->state != MIG_STATE_ACTIVE) { |
d0f2c4c6 | 290 | DPRINTF("put_ready returning because of non-active state\n"); |
065e2813 AL |
291 | return; |
292 | } | |
293 | ||
d0f2c4c6 | 294 | DPRINTF("iterate\n"); |
39346385 JQ |
295 | ret = qemu_savevm_state_iterate(s->mon, s->file); |
296 | if (ret < 0) { | |
297 | migrate_fd_error(s); | |
298 | } else if (ret == 1) { | |
1354869c | 299 | int old_vm_running = runstate_is_running(); |
eeb34af9 | 300 | |
d0f2c4c6 | 301 | DPRINTF("done iterating\n"); |
8a9236f1 | 302 | vm_stop_force_state(RUN_STATE_FINISH_MIGRATE); |
065e2813 | 303 | |
67afff79 JQ |
304 | if (qemu_savevm_state_complete(s->mon, s->file) < 0) { |
305 | migrate_fd_error(s); | |
b161d123 | 306 | } else { |
458cf28e | 307 | migrate_fd_completed(s); |
b161d123 | 308 | } |
48a2f4d6 | 309 | if (s->state != MIG_STATE_COMPLETED) { |
41ef56e6 AL |
310 | if (old_vm_running) { |
311 | vm_start(); | |
312 | } | |
f5bbfba1 | 313 | } |
065e2813 AL |
314 | } |
315 | } | |
316 | ||
0edda1c4 | 317 | static void migrate_fd_cancel(MigrationState *s) |
065e2813 | 318 | { |
065e2813 AL |
319 | if (s->state != MIG_STATE_ACTIVE) |
320 | return; | |
321 | ||
d0f2c4c6 | 322 | DPRINTF("cancelling migration\n"); |
065e2813 AL |
323 | |
324 | s->state = MIG_STATE_CANCELLED; | |
e0eb7390 | 325 | notifier_list_notify(&migration_state_notifiers, s); |
f327aa0c | 326 | qemu_savevm_state_cancel(s->mon, s->file); |
065e2813 AL |
327 | |
328 | migrate_fd_cleanup(s); | |
329 | } | |
330 | ||
8b6b99b3 | 331 | static void migrate_fd_wait_for_unfreeze(void *opaque) |
065e2813 | 332 | { |
22f00a44 | 333 | MigrationState *s = opaque; |
065e2813 AL |
334 | int ret; |
335 | ||
d0f2c4c6 | 336 | DPRINTF("wait for unfreeze\n"); |
065e2813 AL |
337 | if (s->state != MIG_STATE_ACTIVE) |
338 | return; | |
339 | ||
340 | do { | |
341 | fd_set wfds; | |
342 | ||
343 | FD_ZERO(&wfds); | |
344 | FD_SET(s->fd, &wfds); | |
345 | ||
346 | ret = select(s->fd + 1, NULL, &wfds, NULL, NULL); | |
347 | } while (ret == -1 && (s->get_error(s)) == EINTR); | |
af509450 JQ |
348 | |
349 | if (ret == -1) { | |
dcd1d224 | 350 | qemu_file_set_error(s->file, -s->get_error(s)); |
af509450 | 351 | } |
065e2813 AL |
352 | } |
353 | ||
8b6b99b3 | 354 | static int migrate_fd_close(void *opaque) |
065e2813 | 355 | { |
22f00a44 | 356 | MigrationState *s = opaque; |
e19252d3 | 357 | |
84ec6552 JK |
358 | if (s->mon) { |
359 | monitor_resume(s->mon); | |
360 | } | |
e19252d3 | 361 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
065e2813 AL |
362 | return s->close(s); |
363 | } | |
99a0db9b GH |
364 | |
365 | void add_migration_state_change_notifier(Notifier *notify) | |
366 | { | |
367 | notifier_list_add(&migration_state_notifiers, notify); | |
368 | } | |
369 | ||
370 | void remove_migration_state_change_notifier(Notifier *notify) | |
371 | { | |
372 | notifier_list_remove(&migration_state_notifiers, notify); | |
373 | } | |
374 | ||
7073693b | 375 | bool migration_has_finished(MigrationState *s) |
99a0db9b | 376 | { |
7073693b | 377 | return s->state == MIG_STATE_COMPLETED; |
99a0db9b | 378 | } |
0edda1c4 | 379 | |
8b6b99b3 | 380 | void migrate_fd_connect(MigrationState *s) |
99a0db9b | 381 | { |
8b6b99b3 JQ |
382 | int ret; |
383 | ||
d5934dde | 384 | s->state = MIG_STATE_ACTIVE; |
8b6b99b3 JQ |
385 | s->file = qemu_fopen_ops_buffered(s, |
386 | s->bandwidth_limit, | |
387 | migrate_fd_put_buffer, | |
388 | migrate_fd_put_ready, | |
389 | migrate_fd_wait_for_unfreeze, | |
390 | migrate_fd_close); | |
391 | ||
392 | DPRINTF("beginning savevm\n"); | |
393 | ret = qemu_savevm_state_begin(s->mon, s->file, s->blk, s->shared); | |
394 | if (ret < 0) { | |
395 | DPRINTF("failed, %d\n", ret); | |
396 | migrate_fd_error(s); | |
397 | return; | |
398 | } | |
399 | migrate_fd_put_ready(s); | |
400 | } | |
401 | ||
d0ae46c1 | 402 | static MigrationState *migrate_init(Monitor *mon, int detach, int blk, int inc) |
0edda1c4 | 403 | { |
17549e84 | 404 | MigrationState *s = migrate_get_current(); |
d0ae46c1 | 405 | int64_t bandwidth_limit = s->bandwidth_limit; |
0edda1c4 | 406 | |
17549e84 | 407 | memset(s, 0, sizeof(*s)); |
d0ae46c1 | 408 | s->bandwidth_limit = bandwidth_limit; |
0edda1c4 JQ |
409 | s->blk = blk; |
410 | s->shared = inc; | |
411 | s->mon = NULL; | |
412 | s->bandwidth_limit = bandwidth_limit; | |
d5934dde | 413 | s->state = MIG_STATE_SETUP; |
0edda1c4 JQ |
414 | |
415 | if (!detach) { | |
416 | migrate_fd_monitor_suspend(s, mon); | |
417 | } | |
418 | ||
419 | return s; | |
420 | } | |
cab30143 JQ |
421 | |
422 | int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data) | |
423 | { | |
17549e84 | 424 | MigrationState *s = migrate_get_current(); |
cab30143 JQ |
425 | const char *p; |
426 | int detach = qdict_get_try_bool(qdict, "detach", 0); | |
427 | int blk = qdict_get_try_bool(qdict, "blk", 0); | |
428 | int inc = qdict_get_try_bool(qdict, "inc", 0); | |
429 | const char *uri = qdict_get_str(qdict, "uri"); | |
430 | int ret; | |
431 | ||
17549e84 | 432 | if (s->state == MIG_STATE_ACTIVE) { |
cab30143 JQ |
433 | monitor_printf(mon, "migration already in progress\n"); |
434 | return -1; | |
435 | } | |
436 | ||
437 | if (qemu_savevm_state_blocked(mon)) { | |
438 | return -1; | |
439 | } | |
440 | ||
d0ae46c1 | 441 | s = migrate_init(mon, detach, blk, inc); |
cab30143 JQ |
442 | |
443 | if (strstart(uri, "tcp:", &p)) { | |
444 | ret = tcp_start_outgoing_migration(s, p); | |
445 | #if !defined(WIN32) | |
446 | } else if (strstart(uri, "exec:", &p)) { | |
447 | ret = exec_start_outgoing_migration(s, p); | |
448 | } else if (strstart(uri, "unix:", &p)) { | |
449 | ret = unix_start_outgoing_migration(s, p); | |
450 | } else if (strstart(uri, "fd:", &p)) { | |
451 | ret = fd_start_outgoing_migration(s, p); | |
452 | #endif | |
99a0db9b | 453 | } else { |
cab30143 JQ |
454 | monitor_printf(mon, "unknown migration protocol: %s\n", uri); |
455 | ret = -EINVAL; | |
cab30143 JQ |
456 | } |
457 | ||
458 | if (ret < 0) { | |
17549e84 JQ |
459 | monitor_printf(mon, "migration failed: %s\n", strerror(-ret)); |
460 | return ret; | |
cab30143 JQ |
461 | } |
462 | ||
e0eb7390 | 463 | notifier_list_notify(&migration_state_notifiers, s); |
cab30143 | 464 | return 0; |
cab30143 JQ |
465 | } |
466 | ||
467 | int do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data) | |
468 | { | |
17549e84 | 469 | migrate_fd_cancel(migrate_get_current()); |
cab30143 JQ |
470 | return 0; |
471 | } | |
472 | ||
473 | int do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data) | |
474 | { | |
475 | int64_t d; | |
476 | MigrationState *s; | |
477 | ||
478 | d = qdict_get_int(qdict, "value"); | |
479 | if (d < 0) { | |
480 | d = 0; | |
99a0db9b | 481 | } |
cab30143 | 482 | |
17549e84 | 483 | s = migrate_get_current(); |
d0ae46c1 JQ |
484 | s->bandwidth_limit = d; |
485 | qemu_file_set_rate_limit(s->file, s->bandwidth_limit); | |
cab30143 JQ |
486 | |
487 | return 0; | |
488 | } | |
489 | ||
490 | int do_migrate_set_downtime(Monitor *mon, const QDict *qdict, | |
491 | QObject **ret_data) | |
492 | { | |
493 | double d; | |
494 | ||
495 | d = qdict_get_double(qdict, "value") * 1e9; | |
496 | d = MAX(0, MIN(UINT64_MAX, d)); | |
497 | max_downtime = (uint64_t)d; | |
498 | ||
499 | return 0; | |
99a0db9b | 500 | } |