lsi53c895a: fix Phase Mismatch Jump
[qemu/stefanha.git] / migration.c
blobb49964c5e3a3c81a4b8b8d87be8c7923d0f4d21b
1 /*
2 * QEMU live migration
4 * Copyright IBM, Corp. 2008
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
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"
24 //#define DEBUG_MIGRATION
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
34 /* Migration speed throttling */
35 static uint32_t max_throttle = (32 << 20);
37 static MigrationState *current_migration;
39 int qemu_start_incoming_migration(const char *uri)
41 const char *p;
42 int ret;
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;
58 return ret;
61 void process_incoming_migration(QEMUFile *f)
63 if (qemu_loadvm_state(f) < 0) {
64 fprintf(stderr, "load of migration failed\n");
65 exit(0);
67 qemu_announce_self();
68 DPRINTF("successfully loaded vm state\n");
70 if (autostart)
71 vm_start();
74 int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
76 MigrationState *s = NULL;
77 const char *p;
78 int detach = qdict_get_int(qdict, "detach");
79 const char *uri = qdict_get_str(qdict, "uri");
81 if (current_migration &&
82 current_migration->get_status(current_migration) == MIG_STATE_ACTIVE) {
83 monitor_printf(mon, "migration already in progress\n");
84 return -1;
87 if (strstart(uri, "tcp:", &p)) {
88 s = tcp_start_outgoing_migration(mon, p, max_throttle, detach,
89 (int)qdict_get_int(qdict, "blk"),
90 (int)qdict_get_int(qdict, "inc"));
91 #if !defined(WIN32)
92 } else if (strstart(uri, "exec:", &p)) {
93 s = exec_start_outgoing_migration(mon, p, max_throttle, detach,
94 (int)qdict_get_int(qdict, "blk"),
95 (int)qdict_get_int(qdict, "inc"));
96 } else if (strstart(uri, "unix:", &p)) {
97 s = unix_start_outgoing_migration(mon, p, max_throttle, detach,
98 (int)qdict_get_int(qdict, "blk"),
99 (int)qdict_get_int(qdict, "inc"));
100 } else if (strstart(uri, "fd:", &p)) {
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"));
104 #endif
105 } else {
106 monitor_printf(mon, "unknown migration protocol: %s\n", uri);
107 return -1;
110 if (s == NULL) {
111 monitor_printf(mon, "migration failed\n");
112 return -1;
115 if (current_migration) {
116 current_migration->release(current_migration);
119 current_migration = s;
120 return 0;
123 int do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data)
125 MigrationState *s = current_migration;
127 if (s)
128 s->cancel(s);
130 return 0;
133 int do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data)
135 double d;
136 FdMigrationState *s;
138 d = qdict_get_double(qdict, "value");
139 d = MAX(0, MIN(UINT32_MAX, d));
140 max_throttle = d;
142 s = migrate_to_fms(current_migration);
143 if (s && s->file) {
144 qemu_file_set_rate_limit(s->file, max_throttle);
147 return 0;
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;
156 uint64_t migrate_max_downtime(void)
158 return max_downtime;
161 int do_migrate_set_downtime(Monitor *mon, const QDict *qdict,
162 QObject **ret_data)
164 double d;
166 d = qdict_get_double(qdict, "value") * 1e9;
167 d = MAX(0, MIN(UINT64_MAX, d));
168 max_downtime = (uint64_t)d;
170 return 0;
173 static void migrate_print_status(Monitor *mon, const char *name,
174 const QDict *status_dict)
176 QDict *qdict;
178 qdict = qobject_to_qdict(qdict_get(status_dict, name));
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);
188 void do_info_migrate_print(Monitor *mon, const QObject *data)
190 QDict *qdict;
192 qdict = qobject_to_qdict(data);
194 monitor_printf(mon, "Migration status: %s\n",
195 qdict_get_str(qdict, "status"));
197 if (qdict_haskey(qdict, "ram")) {
198 migrate_print_status(mon, "ram", qdict);
201 if (qdict_haskey(qdict, "disk")) {
202 migrate_print_status(mon, "disk", qdict);
206 static void migrate_put_status(QDict *qdict, const char *name,
207 uint64_t trans, uint64_t rem, uint64_t total)
209 QObject *obj;
211 obj = qobject_from_jsonf("{ 'transferred': %" PRId64 ", "
212 "'remaining': %" PRId64 ", "
213 "'total': %" PRId64 " }", trans, rem, total);
214 qdict_put_obj(qdict, name, obj);
217 void do_info_migrate(Monitor *mon, QObject **ret_data)
219 QDict *qdict;
220 MigrationState *s = current_migration;
222 if (s) {
223 switch (s->get_status(s)) {
224 case MIG_STATE_ACTIVE:
225 qdict = qdict_new();
226 qdict_put(qdict, "status", qstring_from_str("active"));
228 migrate_put_status(qdict, "ram", ram_bytes_transferred(),
229 ram_bytes_remaining(), ram_bytes_total());
231 if (blk_mig_active()) {
232 migrate_put_status(qdict, "disk", blk_mig_bytes_transferred(),
233 blk_mig_bytes_remaining(),
234 blk_mig_bytes_total());
237 *ret_data = QOBJECT(qdict);
238 break;
239 case MIG_STATE_COMPLETED:
240 *ret_data = qobject_from_jsonf("{ 'status': 'completed' }");
241 break;
242 case MIG_STATE_ERROR:
243 *ret_data = qobject_from_jsonf("{ 'status': 'failed' }");
244 break;
245 case MIG_STATE_CANCELLED:
246 *ret_data = qobject_from_jsonf("{ 'status': 'cancelled' }");
247 break;
252 /* shared migration helpers */
254 void migrate_fd_monitor_suspend(FdMigrationState *s, Monitor *mon)
256 s->mon = mon;
257 if (monitor_suspend(mon) == 0) {
258 DPRINTF("suspending monitor\n");
259 } else {
260 monitor_printf(mon, "terminal does not allow synchronous "
261 "migration, continuing detached\n");
265 void migrate_fd_error(FdMigrationState *s)
267 DPRINTF("setting error state\n");
268 s->state = MIG_STATE_ERROR;
269 migrate_fd_cleanup(s);
272 int migrate_fd_cleanup(FdMigrationState *s)
274 int ret = 0;
276 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
278 if (s->file) {
279 DPRINTF("closing file\n");
280 if (qemu_fclose(s->file) != 0) {
281 ret = -1;
283 s->file = NULL;
286 if (s->fd != -1)
287 close(s->fd);
289 /* Don't resume monitor until we've flushed all of the buffers */
290 if (s->mon) {
291 monitor_resume(s->mon);
294 s->fd = -1;
296 return ret;
299 void migrate_fd_put_notify(void *opaque)
301 FdMigrationState *s = opaque;
303 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
304 qemu_file_put_notify(s->file);
307 ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size)
309 FdMigrationState *s = opaque;
310 ssize_t ret;
312 do {
313 ret = s->write(s, data, size);
314 } while (ret == -1 && ((s->get_error(s)) == EINTR));
316 if (ret == -1)
317 ret = -(s->get_error(s));
319 if (ret == -EAGAIN)
320 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
322 return ret;
325 void migrate_fd_connect(FdMigrationState *s)
327 int ret;
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);
336 DPRINTF("beginning savevm\n");
337 ret = qemu_savevm_state_begin(s->mon, s->file, s->mig_state.blk,
338 s->mig_state.shared);
339 if (ret < 0) {
340 DPRINTF("failed, %d\n", ret);
341 migrate_fd_error(s);
342 return;
345 migrate_fd_put_ready(s);
348 void migrate_fd_put_ready(void *opaque)
350 FdMigrationState *s = opaque;
352 if (s->state != MIG_STATE_ACTIVE) {
353 DPRINTF("put_ready returning because of non-active state\n");
354 return;
357 DPRINTF("iterate\n");
358 if (qemu_savevm_state_iterate(s->mon, s->file) == 1) {
359 int state;
360 int old_vm_running = vm_running;
362 DPRINTF("done iterating\n");
363 vm_stop(0);
365 qemu_aio_flush();
366 bdrv_flush_all();
367 if ((qemu_savevm_state_complete(s->mon, s->file)) < 0) {
368 if (old_vm_running) {
369 vm_start();
371 state = MIG_STATE_ERROR;
372 } else {
373 state = MIG_STATE_COMPLETED;
375 if (migrate_fd_cleanup(s) < 0) {
376 if (old_vm_running) {
377 vm_start();
379 state = MIG_STATE_ERROR;
381 s->state = state;
385 int migrate_fd_get_status(MigrationState *mig_state)
387 FdMigrationState *s = migrate_to_fms(mig_state);
388 return s->state;
391 void migrate_fd_cancel(MigrationState *mig_state)
393 FdMigrationState *s = migrate_to_fms(mig_state);
395 if (s->state != MIG_STATE_ACTIVE)
396 return;
398 DPRINTF("cancelling migration\n");
400 s->state = MIG_STATE_CANCELLED;
401 qemu_savevm_state_cancel(s->mon, s->file);
403 migrate_fd_cleanup(s);
406 void migrate_fd_release(MigrationState *mig_state)
408 FdMigrationState *s = migrate_to_fms(mig_state);
410 DPRINTF("releasing state\n");
412 if (s->state == MIG_STATE_ACTIVE) {
413 s->state = MIG_STATE_CANCELLED;
414 migrate_fd_cleanup(s);
416 qemu_free(s);
419 void migrate_fd_wait_for_unfreeze(void *opaque)
421 FdMigrationState *s = opaque;
422 int ret;
424 DPRINTF("wait for unfreeze\n");
425 if (s->state != MIG_STATE_ACTIVE)
426 return;
428 do {
429 fd_set wfds;
431 FD_ZERO(&wfds);
432 FD_SET(s->fd, &wfds);
434 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
435 } while (ret == -1 && (s->get_error(s)) == EINTR);
438 int migrate_fd_close(void *opaque)
440 FdMigrationState *s = opaque;
442 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
443 return s->close(s);