SPARC: Add asr17 register support
[qemu/agraf.git] / migration.c
blobd593b1df5dd8b7a253b034cf57a407005ed10d5a
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 int64_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 incoming_expected = false;
72 if (autostart)
73 vm_start();
76 int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
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");
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;
91 if (qemu_savevm_state_blocked(mon)) {
92 return -1;
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;
114 if (s == NULL) {
115 monitor_printf(mon, "migration failed\n");
116 return -1;
119 if (current_migration) {
120 current_migration->release(current_migration);
123 current_migration = s;
124 return 0;
127 int do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data)
129 MigrationState *s = current_migration;
131 if (s)
132 s->cancel(s);
134 return 0;
137 int do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data)
139 int64_t d;
140 FdMigrationState *s;
142 d = qdict_get_int(qdict, "value");
143 if (d < 0) {
144 d = 0;
146 max_throttle = d;
148 s = migrate_to_fms(current_migration);
149 if (s && s->file) {
150 qemu_file_set_rate_limit(s->file, max_throttle);
153 return 0;
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;
162 uint64_t migrate_max_downtime(void)
164 return max_downtime;
167 int do_migrate_set_downtime(Monitor *mon, const QDict *qdict,
168 QObject **ret_data)
170 double d;
172 d = qdict_get_double(qdict, "value") * 1e9;
173 d = MAX(0, MIN(UINT64_MAX, d));
174 max_downtime = (uint64_t)d;
176 return 0;
179 static void migrate_print_status(Monitor *mon, const char *name,
180 const QDict *status_dict)
182 QDict *qdict;
184 qdict = qobject_to_qdict(qdict_get(status_dict, name));
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);
194 void do_info_migrate_print(Monitor *mon, const QObject *data)
196 QDict *qdict;
198 qdict = qobject_to_qdict(data);
200 monitor_printf(mon, "Migration status: %s\n",
201 qdict_get_str(qdict, "status"));
203 if (qdict_haskey(qdict, "ram")) {
204 migrate_print_status(mon, "ram", qdict);
207 if (qdict_haskey(qdict, "disk")) {
208 migrate_print_status(mon, "disk", qdict);
212 static void migrate_put_status(QDict *qdict, const char *name,
213 uint64_t trans, uint64_t rem, uint64_t total)
215 QObject *obj;
217 obj = qobject_from_jsonf("{ 'transferred': %" PRId64 ", "
218 "'remaining': %" PRId64 ", "
219 "'total': %" PRId64 " }", trans, rem, total);
220 qdict_put_obj(qdict, name, obj);
223 void do_info_migrate(Monitor *mon, QObject **ret_data)
225 QDict *qdict;
226 MigrationState *s = current_migration;
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"));
234 migrate_put_status(qdict, "ram", ram_bytes_transferred(),
235 ram_bytes_remaining(), ram_bytes_total());
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());
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;
258 /* shared migration helpers */
260 void migrate_fd_monitor_suspend(FdMigrationState *s, Monitor *mon)
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");
271 void migrate_fd_error(FdMigrationState *s)
273 DPRINTF("setting error state\n");
274 s->state = MIG_STATE_ERROR;
275 migrate_fd_cleanup(s);
278 int migrate_fd_cleanup(FdMigrationState *s)
280 int ret = 0;
282 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
284 if (s->file) {
285 DPRINTF("closing file\n");
286 if (qemu_fclose(s->file) != 0) {
287 ret = -1;
289 s->file = NULL;
292 if (s->fd != -1)
293 close(s->fd);
295 /* Don't resume monitor until we've flushed all of the buffers */
296 if (s->mon) {
297 monitor_resume(s->mon);
300 s->fd = -1;
302 return ret;
305 void migrate_fd_put_notify(void *opaque)
307 FdMigrationState *s = opaque;
309 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
310 qemu_file_put_notify(s->file);
313 ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size)
315 FdMigrationState *s = opaque;
316 ssize_t ret;
318 do {
319 ret = s->write(s, data, size);
320 } while (ret == -1 && ((s->get_error(s)) == EINTR));
322 if (ret == -1)
323 ret = -(s->get_error(s));
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);
331 s->state = MIG_STATE_ERROR;
334 return ret;
337 void migrate_fd_connect(FdMigrationState *s)
339 int ret;
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);
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;
357 migrate_fd_put_ready(s);
360 void migrate_fd_put_ready(void *opaque)
362 FdMigrationState *s = opaque;
364 if (s->state != MIG_STATE_ACTIVE) {
365 DPRINTF("put_ready returning because of non-active state\n");
366 return;
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;
374 DPRINTF("done iterating\n");
375 vm_stop(0);
377 if ((qemu_savevm_state_complete(s->mon, s->file)) < 0) {
378 if (old_vm_running) {
379 vm_start();
381 state = MIG_STATE_ERROR;
382 } else {
383 state = MIG_STATE_COMPLETED;
385 if (migrate_fd_cleanup(s) < 0) {
386 if (old_vm_running) {
387 vm_start();
389 state = MIG_STATE_ERROR;
391 s->state = state;
395 int migrate_fd_get_status(MigrationState *mig_state)
397 FdMigrationState *s = migrate_to_fms(mig_state);
398 return s->state;
401 void migrate_fd_cancel(MigrationState *mig_state)
403 FdMigrationState *s = migrate_to_fms(mig_state);
405 if (s->state != MIG_STATE_ACTIVE)
406 return;
408 DPRINTF("cancelling migration\n");
410 s->state = MIG_STATE_CANCELLED;
411 qemu_savevm_state_cancel(s->mon, s->file);
413 migrate_fd_cleanup(s);
416 void migrate_fd_release(MigrationState *mig_state)
418 FdMigrationState *s = migrate_to_fms(mig_state);
420 DPRINTF("releasing state\n");
422 if (s->state == MIG_STATE_ACTIVE) {
423 s->state = MIG_STATE_CANCELLED;
424 migrate_fd_cleanup(s);
426 qemu_free(s);
429 void migrate_fd_wait_for_unfreeze(void *opaque)
431 FdMigrationState *s = opaque;
432 int ret;
434 DPRINTF("wait for unfreeze\n");
435 if (s->state != MIG_STATE_ACTIVE)
436 return;
438 do {
439 fd_set wfds;
441 FD_ZERO(&wfds);
442 FD_SET(s->fd, &wfds);
444 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
445 } while (ret == -1 && (s->get_error(s)) == EINTR);
448 int migrate_fd_close(void *opaque)
450 FdMigrationState *s = opaque;
452 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
453 return s->close(s);