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