]>
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 | * | |
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 AL |
33 | |
34 | /* Migration speed throttling */ | |
35 | static uint32_t max_throttle = (32 << 20); | |
36 | ||
37 | static MigrationState *current_migration; | |
38 | ||
8ca5e801 | 39 | int qemu_start_incoming_migration(const char *uri) |
5bb7910a | 40 | { |
34c9dd8e | 41 | const char *p; |
8ca5e801 | 42 | int ret; |
34c9dd8e AL |
43 | |
44 | if (strstart(uri, "tcp:", &p)) | |
8ca5e801 | 45 | ret = tcp_start_incoming_migration(p); |
065e2813 AL |
46 | #if !defined(WIN32) |
47 | else if (strstart(uri, "exec:", &p)) | |
8ca5e801 | 48 | ret = exec_start_incoming_migration(p); |
4951f65b | 49 | else if (strstart(uri, "unix:", &p)) |
8ca5e801 | 50 | ret = unix_start_incoming_migration(p); |
5ac1fad3 | 51 | else if (strstart(uri, "fd:", &p)) |
8ca5e801 | 52 | ret = fd_start_incoming_migration(p); |
065e2813 | 53 | #endif |
8ca5e801 | 54 | else { |
34c9dd8e | 55 | fprintf(stderr, "unknown migration protocol: %s\n", uri); |
8ca5e801 JQ |
56 | ret = -EPROTONOSUPPORT; |
57 | } | |
58 | return ret; | |
5bb7910a AL |
59 | } |
60 | ||
511c0231 JQ |
61 | void process_incoming_migration(QEMUFile *f) |
62 | { | |
63 | if (qemu_loadvm_state(f) < 0) { | |
64 | fprintf(stderr, "load of migration failed\n"); | |
65 | exit(0); | |
66 | } | |
67 | qemu_announce_self(); | |
68 | DPRINTF("successfully loaded vm state\n"); | |
69 | ||
70 | if (autostart) | |
71 | vm_start(); | |
72 | } | |
73 | ||
b5d17adb | 74 | int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data) |
5bb7910a | 75 | { |
34c9dd8e AL |
76 | MigrationState *s = NULL; |
77 | const char *p; | |
f18c16de LC |
78 | int detach = qdict_get_int(qdict, "detach"); |
79 | const char *uri = qdict_get_str(qdict, "uri"); | |
1302425d JK |
80 | |
81 | if (current_migration && | |
82 | current_migration->get_status(current_migration) == MIG_STATE_ACTIVE) { | |
83 | monitor_printf(mon, "migration already in progress\n"); | |
b5d17adb | 84 | return -1; |
1302425d JK |
85 | } |
86 | ||
b5d17adb | 87 | if (strstart(uri, "tcp:", &p)) { |
f327aa0c | 88 | s = tcp_start_outgoing_migration(mon, p, max_throttle, detach, |
c163b5ca LS |
89 | (int)qdict_get_int(qdict, "blk"), |
90 | (int)qdict_get_int(qdict, "inc")); | |
065e2813 | 91 | #if !defined(WIN32) |
b5d17adb | 92 | } else if (strstart(uri, "exec:", &p)) { |
f327aa0c | 93 | s = exec_start_outgoing_migration(mon, p, max_throttle, detach, |
c163b5ca LS |
94 | (int)qdict_get_int(qdict, "blk"), |
95 | (int)qdict_get_int(qdict, "inc")); | |
b5d17adb | 96 | } else if (strstart(uri, "unix:", &p)) { |
f327aa0c | 97 | s = unix_start_outgoing_migration(mon, p, max_throttle, detach, |
c163b5ca LS |
98 | (int)qdict_get_int(qdict, "blk"), |
99 | (int)qdict_get_int(qdict, "inc")); | |
b5d17adb | 100 | } else if (strstart(uri, "fd:", &p)) { |
c163b5ca LS |
101 | s = fd_start_outgoing_migration(mon, p, max_throttle, detach, |
102 | (int)qdict_get_int(qdict, "blk"), | |
103 | (int)qdict_get_int(qdict, "inc")); | |
065e2813 | 104 | #endif |
b5d17adb | 105 | } else { |
376253ec | 106 | monitor_printf(mon, "unknown migration protocol: %s\n", uri); |
b5d17adb LC |
107 | return -1; |
108 | } | |
34c9dd8e | 109 | |
b5d17adb | 110 | if (s == NULL) { |
376253ec | 111 | monitor_printf(mon, "migration failed\n"); |
b5d17adb LC |
112 | return -1; |
113 | } | |
34c9dd8e | 114 | |
b5d17adb LC |
115 | if (current_migration) { |
116 | current_migration->release(current_migration); | |
34c9dd8e | 117 | } |
b5d17adb LC |
118 | |
119 | current_migration = s; | |
120 | return 0; | |
5bb7910a AL |
121 | } |
122 | ||
ef4b7eee | 123 | int do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data) |
5bb7910a AL |
124 | { |
125 | MigrationState *s = current_migration; | |
126 | ||
127 | if (s) | |
ff8d81d8 | 128 | s->cancel(s); |
ef4b7eee LC |
129 | |
130 | return 0; | |
5bb7910a AL |
131 | } |
132 | ||
ef4b7eee | 133 | int do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data) |
5bb7910a AL |
134 | { |
135 | double d; | |
daa91de2 | 136 | FdMigrationState *s; |
5bb7910a | 137 | |
5667c493 MA |
138 | d = qdict_get_double(qdict, "value"); |
139 | d = MAX(0, MIN(UINT32_MAX, d)); | |
140 | max_throttle = d; | |
daa91de2 | 141 | |
5d39c799 JK |
142 | s = migrate_to_fms(current_migration); |
143 | if (s && s->file) { | |
daa91de2 GC |
144 | qemu_file_set_rate_limit(s->file, max_throttle); |
145 | } | |
ef4b7eee LC |
146 | |
147 | return 0; | |
5bb7910a AL |
148 | } |
149 | ||
a0a3fd60 GC |
150 | /* amount of nanoseconds we are willing to wait for migration to be down. |
151 | * the choice of nanoseconds is because it is the maximum resolution that | |
152 | * get_clock() can achieve. It is an internal measure. All user-visible | |
153 | * units must be in seconds */ | |
154 | static uint64_t max_downtime = 30000000; | |
155 | ||
156 | uint64_t migrate_max_downtime(void) | |
157 | { | |
158 | return max_downtime; | |
159 | } | |
160 | ||
ef4b7eee LC |
161 | int do_migrate_set_downtime(Monitor *mon, const QDict *qdict, |
162 | QObject **ret_data) | |
2ea42952 | 163 | { |
2ea42952 | 164 | double d; |
2ea42952 | 165 | |
b0fbf7d3 MA |
166 | d = qdict_get_double(qdict, "value") * 1e9; |
167 | d = MAX(0, MIN(UINT64_MAX, d)); | |
2ea42952 | 168 | max_downtime = (uint64_t)d; |
ef4b7eee LC |
169 | |
170 | return 0; | |
2ea42952 GC |
171 | } |
172 | ||
c86a6683 LC |
173 | static void migrate_print_status(Monitor *mon, const char *name, |
174 | const QDict *status_dict) | |
5bb7910a | 175 | { |
c86a6683 LC |
176 | QDict *qdict; |
177 | ||
178 | qdict = qobject_to_qdict(qdict_get(status_dict, name)); | |
179 | ||
180 | monitor_printf(mon, "transferred %s: %" PRIu64 " kbytes\n", name, | |
181 | qdict_get_int(qdict, "transferred") >> 10); | |
182 | monitor_printf(mon, "remaining %s: %" PRIu64 " kbytes\n", name, | |
183 | qdict_get_int(qdict, "remaining") >> 10); | |
184 | monitor_printf(mon, "total %s: %" PRIu64 " kbytes\n", name, | |
185 | qdict_get_int(qdict, "total") >> 10); | |
186 | } | |
187 | ||
188 | void do_info_migrate_print(Monitor *mon, const QObject *data) | |
189 | { | |
190 | QDict *qdict; | |
191 | ||
192 | qdict = qobject_to_qdict(data); | |
193 | ||
194 | monitor_printf(mon, "Migration status: %s\n", | |
195 | qdict_get_str(qdict, "status")); | |
196 | ||
197 | if (qdict_haskey(qdict, "ram")) { | |
198 | migrate_print_status(mon, "ram", qdict); | |
199 | } | |
200 | ||
201 | if (qdict_haskey(qdict, "disk")) { | |
202 | migrate_print_status(mon, "disk", qdict); | |
203 | } | |
204 | } | |
205 | ||
206 | static void migrate_put_status(QDict *qdict, const char *name, | |
207 | uint64_t trans, uint64_t rem, uint64_t total) | |
208 | { | |
209 | QObject *obj; | |
210 | ||
211 | obj = qobject_from_jsonf("{ 'transferred': %" PRId64 ", " | |
212 | "'remaining': %" PRId64 ", " | |
213 | "'total': %" PRId64 " }", trans, rem, total); | |
c86a6683 LC |
214 | qdict_put_obj(qdict, name, obj); |
215 | } | |
216 | ||
c86a6683 LC |
217 | void do_info_migrate(Monitor *mon, QObject **ret_data) |
218 | { | |
219 | QDict *qdict; | |
5bb7910a | 220 | MigrationState *s = current_migration; |
376253ec | 221 | |
5bb7910a | 222 | if (s) { |
ff8d81d8 AL |
223 | switch (s->get_status(s)) { |
224 | case MIG_STATE_ACTIVE: | |
c86a6683 LC |
225 | qdict = qdict_new(); |
226 | qdict_put(qdict, "status", qstring_from_str("active")); | |
227 | ||
228 | migrate_put_status(qdict, "ram", ram_bytes_transferred(), | |
229 | ram_bytes_remaining(), ram_bytes_total()); | |
230 | ||
25f23643 | 231 | if (blk_mig_active()) { |
c86a6683 LC |
232 | migrate_put_status(qdict, "disk", blk_mig_bytes_transferred(), |
233 | blk_mig_bytes_remaining(), | |
234 | blk_mig_bytes_total()); | |
25f23643 | 235 | } |
c86a6683 LC |
236 | |
237 | *ret_data = QOBJECT(qdict); | |
ff8d81d8 AL |
238 | break; |
239 | case MIG_STATE_COMPLETED: | |
c86a6683 | 240 | *ret_data = qobject_from_jsonf("{ 'status': 'completed' }"); |
ff8d81d8 AL |
241 | break; |
242 | case MIG_STATE_ERROR: | |
c86a6683 | 243 | *ret_data = qobject_from_jsonf("{ 'status': 'failed' }"); |
ff8d81d8 AL |
244 | break; |
245 | case MIG_STATE_CANCELLED: | |
c86a6683 | 246 | *ret_data = qobject_from_jsonf("{ 'status': 'cancelled' }"); |
ff8d81d8 AL |
247 | break; |
248 | } | |
5bb7910a AL |
249 | } |
250 | } | |
251 | ||
065e2813 AL |
252 | /* shared migration helpers */ |
253 | ||
f327aa0c | 254 | void migrate_fd_monitor_suspend(FdMigrationState *s, Monitor *mon) |
731b0364 | 255 | { |
f327aa0c JK |
256 | s->mon = mon; |
257 | if (monitor_suspend(mon) == 0) { | |
d0f2c4c6 | 258 | DPRINTF("suspending monitor\n"); |
f327aa0c JK |
259 | } else { |
260 | monitor_printf(mon, "terminal does not allow synchronous " | |
cde76ee1 | 261 | "migration, continuing detached\n"); |
f327aa0c | 262 | } |
731b0364 AL |
263 | } |
264 | ||
065e2813 AL |
265 | void migrate_fd_error(FdMigrationState *s) |
266 | { | |
d0f2c4c6 | 267 | DPRINTF("setting error state\n"); |
065e2813 AL |
268 | s->state = MIG_STATE_ERROR; |
269 | migrate_fd_cleanup(s); | |
270 | } | |
271 | ||
41ef56e6 | 272 | int migrate_fd_cleanup(FdMigrationState *s) |
065e2813 | 273 | { |
41ef56e6 AL |
274 | int ret = 0; |
275 | ||
065e2813 AL |
276 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
277 | ||
278 | if (s->file) { | |
d0f2c4c6 | 279 | DPRINTF("closing file\n"); |
41ef56e6 AL |
280 | if (qemu_fclose(s->file) != 0) { |
281 | ret = -1; | |
282 | } | |
5d39c799 | 283 | s->file = NULL; |
065e2813 AL |
284 | } |
285 | ||
286 | if (s->fd != -1) | |
287 | close(s->fd); | |
288 | ||
289 | /* Don't resume monitor until we've flushed all of the buffers */ | |
f327aa0c JK |
290 | if (s->mon) { |
291 | monitor_resume(s->mon); | |
292 | } | |
065e2813 AL |
293 | |
294 | s->fd = -1; | |
41ef56e6 AL |
295 | |
296 | return ret; | |
065e2813 AL |
297 | } |
298 | ||
299 | void migrate_fd_put_notify(void *opaque) | |
300 | { | |
301 | FdMigrationState *s = opaque; | |
302 | ||
303 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); | |
304 | qemu_file_put_notify(s->file); | |
305 | } | |
306 | ||
307 | ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size) | |
308 | { | |
309 | FdMigrationState *s = opaque; | |
310 | ssize_t ret; | |
311 | ||
312 | do { | |
313 | ret = s->write(s, data, size); | |
95b134ea | 314 | } while (ret == -1 && ((s->get_error(s)) == EINTR)); |
065e2813 AL |
315 | |
316 | if (ret == -1) | |
317 | ret = -(s->get_error(s)); | |
318 | ||
319 | if (ret == -EAGAIN) | |
320 | qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s); | |
321 | ||
322 | return ret; | |
323 | } | |
324 | ||
325 | void migrate_fd_connect(FdMigrationState *s) | |
326 | { | |
327 | int ret; | |
328 | ||
329 | s->file = qemu_fopen_ops_buffered(s, | |
330 | s->bandwidth_limit, | |
331 | migrate_fd_put_buffer, | |
332 | migrate_fd_put_ready, | |
333 | migrate_fd_wait_for_unfreeze, | |
334 | migrate_fd_close); | |
335 | ||
d0f2c4c6 | 336 | DPRINTF("beginning savevm\n"); |
f327aa0c | 337 | ret = qemu_savevm_state_begin(s->mon, s->file, s->mig_state.blk, |
c163b5ca | 338 | s->mig_state.shared); |
065e2813 | 339 | if (ret < 0) { |
d0f2c4c6 | 340 | DPRINTF("failed, %d\n", ret); |
065e2813 AL |
341 | migrate_fd_error(s); |
342 | return; | |
343 | } | |
c163b5ca | 344 | |
065e2813 AL |
345 | migrate_fd_put_ready(s); |
346 | } | |
347 | ||
348 | void migrate_fd_put_ready(void *opaque) | |
349 | { | |
350 | FdMigrationState *s = opaque; | |
351 | ||
352 | if (s->state != MIG_STATE_ACTIVE) { | |
d0f2c4c6 | 353 | DPRINTF("put_ready returning because of non-active state\n"); |
065e2813 AL |
354 | return; |
355 | } | |
356 | ||
d0f2c4c6 | 357 | DPRINTF("iterate\n"); |
f327aa0c | 358 | if (qemu_savevm_state_iterate(s->mon, s->file) == 1) { |
b161d123 | 359 | int state; |
eeb34af9 AL |
360 | int old_vm_running = vm_running; |
361 | ||
d0f2c4c6 | 362 | DPRINTF("done iterating\n"); |
065e2813 AL |
363 | vm_stop(0); |
364 | ||
0884657b | 365 | qemu_aio_flush(); |
065e2813 | 366 | bdrv_flush_all(); |
f327aa0c | 367 | if ((qemu_savevm_state_complete(s->mon, s->file)) < 0) { |
eeb34af9 AL |
368 | if (old_vm_running) { |
369 | vm_start(); | |
370 | } | |
b161d123 AL |
371 | state = MIG_STATE_ERROR; |
372 | } else { | |
373 | state = MIG_STATE_COMPLETED; | |
374 | } | |
41ef56e6 AL |
375 | if (migrate_fd_cleanup(s) < 0) { |
376 | if (old_vm_running) { | |
377 | vm_start(); | |
378 | } | |
379 | state = MIG_STATE_ERROR; | |
380 | } | |
b161d123 | 381 | s->state = state; |
065e2813 AL |
382 | } |
383 | } | |
384 | ||
385 | int migrate_fd_get_status(MigrationState *mig_state) | |
386 | { | |
387 | FdMigrationState *s = migrate_to_fms(mig_state); | |
388 | return s->state; | |
389 | } | |
390 | ||
391 | void migrate_fd_cancel(MigrationState *mig_state) | |
392 | { | |
393 | FdMigrationState *s = migrate_to_fms(mig_state); | |
394 | ||
395 | if (s->state != MIG_STATE_ACTIVE) | |
396 | return; | |
397 | ||
d0f2c4c6 | 398 | DPRINTF("cancelling migration\n"); |
065e2813 AL |
399 | |
400 | s->state = MIG_STATE_CANCELLED; | |
f327aa0c | 401 | qemu_savevm_state_cancel(s->mon, s->file); |
065e2813 AL |
402 | |
403 | migrate_fd_cleanup(s); | |
404 | } | |
405 | ||
406 | void migrate_fd_release(MigrationState *mig_state) | |
407 | { | |
408 | FdMigrationState *s = migrate_to_fms(mig_state); | |
409 | ||
d0f2c4c6 | 410 | DPRINTF("releasing state\n"); |
065e2813 AL |
411 | |
412 | if (s->state == MIG_STATE_ACTIVE) { | |
413 | s->state = MIG_STATE_CANCELLED; | |
414 | migrate_fd_cleanup(s); | |
415 | } | |
faa1f8dd | 416 | qemu_free(s); |
065e2813 AL |
417 | } |
418 | ||
419 | void migrate_fd_wait_for_unfreeze(void *opaque) | |
420 | { | |
421 | FdMigrationState *s = opaque; | |
422 | int ret; | |
423 | ||
d0f2c4c6 | 424 | DPRINTF("wait for unfreeze\n"); |
065e2813 AL |
425 | if (s->state != MIG_STATE_ACTIVE) |
426 | return; | |
427 | ||
428 | do { | |
429 | fd_set wfds; | |
430 | ||
431 | FD_ZERO(&wfds); | |
432 | FD_SET(s->fd, &wfds); | |
433 | ||
434 | ret = select(s->fd + 1, NULL, &wfds, NULL, NULL); | |
435 | } while (ret == -1 && (s->get_error(s)) == EINTR); | |
436 | } | |
437 | ||
438 | int migrate_fd_close(void *opaque) | |
439 | { | |
440 | FdMigrationState *s = opaque; | |
e19252d3 UL |
441 | |
442 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); | |
065e2813 AL |
443 | return s->close(s); |
444 | } |