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