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