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