RunState: Add additional states
[qemu.git] / migration.c
blobf2499cfdd485d3268ec6b80225571b942db6f306
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 static NotifierList migration_state_notifiers =
40 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
42 int qemu_start_incoming_migration(const char *uri)
44 const char *p;
45 int ret;
47 if (strstart(uri, "tcp:", &p))
48 ret = tcp_start_incoming_migration(p);
49 #if !defined(WIN32)
50 else if (strstart(uri, "exec:", &p))
51 ret = exec_start_incoming_migration(p);
52 else if (strstart(uri, "unix:", &p))
53 ret = unix_start_incoming_migration(p);
54 else if (strstart(uri, "fd:", &p))
55 ret = fd_start_incoming_migration(p);
56 #endif
57 else {
58 fprintf(stderr, "unknown migration protocol: %s\n", uri);
59 ret = -EPROTONOSUPPORT;
61 return ret;
64 void process_incoming_migration(QEMUFile *f)
66 if (qemu_loadvm_state(f) < 0) {
67 fprintf(stderr, "load of migration failed\n");
68 exit(0);
70 qemu_announce_self();
71 DPRINTF("successfully loaded vm state\n");
73 incoming_expected = false;
75 if (autostart) {
76 vm_start();
77 } else {
78 runstate_set(RSTATE_PRE_LAUNCH);
82 int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
84 MigrationState *s = NULL;
85 const char *p;
86 int detach = qdict_get_try_bool(qdict, "detach", 0);
87 int blk = qdict_get_try_bool(qdict, "blk", 0);
88 int inc = qdict_get_try_bool(qdict, "inc", 0);
89 const char *uri = qdict_get_str(qdict, "uri");
91 if (current_migration &&
92 current_migration->get_status(current_migration) == MIG_STATE_ACTIVE) {
93 monitor_printf(mon, "migration already in progress\n");
94 return -1;
97 if (qemu_savevm_state_blocked(mon)) {
98 return -1;
101 if (strstart(uri, "tcp:", &p)) {
102 s = tcp_start_outgoing_migration(mon, p, max_throttle, detach,
103 blk, inc);
104 #if !defined(WIN32)
105 } else if (strstart(uri, "exec:", &p)) {
106 s = exec_start_outgoing_migration(mon, p, max_throttle, detach,
107 blk, inc);
108 } else if (strstart(uri, "unix:", &p)) {
109 s = unix_start_outgoing_migration(mon, p, max_throttle, detach,
110 blk, inc);
111 } else if (strstart(uri, "fd:", &p)) {
112 s = fd_start_outgoing_migration(mon, p, max_throttle, detach,
113 blk, inc);
114 #endif
115 } else {
116 monitor_printf(mon, "unknown migration protocol: %s\n", uri);
117 return -1;
120 if (s == NULL) {
121 monitor_printf(mon, "migration failed\n");
122 return -1;
125 if (current_migration) {
126 current_migration->release(current_migration);
129 current_migration = s;
130 notifier_list_notify(&migration_state_notifiers, NULL);
131 return 0;
134 int do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data)
136 MigrationState *s = current_migration;
138 if (s)
139 s->cancel(s);
141 return 0;
144 int do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data)
146 int64_t d;
147 FdMigrationState *s;
149 d = qdict_get_int(qdict, "value");
150 if (d < 0) {
151 d = 0;
153 max_throttle = d;
155 s = migrate_to_fms(current_migration);
156 if (s && s->file) {
157 qemu_file_set_rate_limit(s->file, max_throttle);
160 return 0;
163 /* amount of nanoseconds we are willing to wait for migration to be down.
164 * the choice of nanoseconds is because it is the maximum resolution that
165 * get_clock() can achieve. It is an internal measure. All user-visible
166 * units must be in seconds */
167 static uint64_t max_downtime = 30000000;
169 uint64_t migrate_max_downtime(void)
171 return max_downtime;
174 int do_migrate_set_downtime(Monitor *mon, const QDict *qdict,
175 QObject **ret_data)
177 double d;
179 d = qdict_get_double(qdict, "value") * 1e9;
180 d = MAX(0, MIN(UINT64_MAX, d));
181 max_downtime = (uint64_t)d;
183 return 0;
186 static void migrate_print_status(Monitor *mon, const char *name,
187 const QDict *status_dict)
189 QDict *qdict;
191 qdict = qobject_to_qdict(qdict_get(status_dict, name));
193 monitor_printf(mon, "transferred %s: %" PRIu64 " kbytes\n", name,
194 qdict_get_int(qdict, "transferred") >> 10);
195 monitor_printf(mon, "remaining %s: %" PRIu64 " kbytes\n", name,
196 qdict_get_int(qdict, "remaining") >> 10);
197 monitor_printf(mon, "total %s: %" PRIu64 " kbytes\n", name,
198 qdict_get_int(qdict, "total") >> 10);
201 void do_info_migrate_print(Monitor *mon, const QObject *data)
203 QDict *qdict;
205 qdict = qobject_to_qdict(data);
207 monitor_printf(mon, "Migration status: %s\n",
208 qdict_get_str(qdict, "status"));
210 if (qdict_haskey(qdict, "ram")) {
211 migrate_print_status(mon, "ram", qdict);
214 if (qdict_haskey(qdict, "disk")) {
215 migrate_print_status(mon, "disk", qdict);
219 static void migrate_put_status(QDict *qdict, const char *name,
220 uint64_t trans, uint64_t rem, uint64_t total)
222 QObject *obj;
224 obj = qobject_from_jsonf("{ 'transferred': %" PRId64 ", "
225 "'remaining': %" PRId64 ", "
226 "'total': %" PRId64 " }", trans, rem, total);
227 qdict_put_obj(qdict, name, obj);
230 void do_info_migrate(Monitor *mon, QObject **ret_data)
232 QDict *qdict;
233 MigrationState *s = current_migration;
235 if (s) {
236 switch (s->get_status(s)) {
237 case MIG_STATE_ACTIVE:
238 qdict = qdict_new();
239 qdict_put(qdict, "status", qstring_from_str("active"));
241 migrate_put_status(qdict, "ram", ram_bytes_transferred(),
242 ram_bytes_remaining(), ram_bytes_total());
244 if (blk_mig_active()) {
245 migrate_put_status(qdict, "disk", blk_mig_bytes_transferred(),
246 blk_mig_bytes_remaining(),
247 blk_mig_bytes_total());
250 *ret_data = QOBJECT(qdict);
251 break;
252 case MIG_STATE_COMPLETED:
253 *ret_data = qobject_from_jsonf("{ 'status': 'completed' }");
254 break;
255 case MIG_STATE_ERROR:
256 *ret_data = qobject_from_jsonf("{ 'status': 'failed' }");
257 break;
258 case MIG_STATE_CANCELLED:
259 *ret_data = qobject_from_jsonf("{ 'status': 'cancelled' }");
260 break;
265 /* shared migration helpers */
267 void migrate_fd_monitor_suspend(FdMigrationState *s, Monitor *mon)
269 s->mon = mon;
270 if (monitor_suspend(mon) == 0) {
271 DPRINTF("suspending monitor\n");
272 } else {
273 monitor_printf(mon, "terminal does not allow synchronous "
274 "migration, continuing detached\n");
278 void migrate_fd_error(FdMigrationState *s)
280 DPRINTF("setting error state\n");
281 s->state = MIG_STATE_ERROR;
282 notifier_list_notify(&migration_state_notifiers, NULL);
283 migrate_fd_cleanup(s);
286 int migrate_fd_cleanup(FdMigrationState *s)
288 int ret = 0;
290 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
292 if (s->file) {
293 DPRINTF("closing file\n");
294 if (qemu_fclose(s->file) != 0) {
295 ret = -1;
297 s->file = NULL;
298 } else {
299 if (s->mon) {
300 monitor_resume(s->mon);
304 if (s->fd != -1) {
305 close(s->fd);
306 s->fd = -1;
309 return ret;
312 void migrate_fd_put_notify(void *opaque)
314 FdMigrationState *s = opaque;
316 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
317 qemu_file_put_notify(s->file);
320 ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size)
322 FdMigrationState *s = opaque;
323 ssize_t ret;
325 do {
326 ret = s->write(s, data, size);
327 } while (ret == -1 && ((s->get_error(s)) == EINTR));
329 if (ret == -1)
330 ret = -(s->get_error(s));
332 if (ret == -EAGAIN) {
333 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
334 } else if (ret < 0) {
335 s->state = MIG_STATE_ERROR;
336 notifier_list_notify(&migration_state_notifiers, NULL);
339 return ret;
342 void migrate_fd_connect(FdMigrationState *s)
344 int ret;
346 s->file = qemu_fopen_ops_buffered(s,
347 s->bandwidth_limit,
348 migrate_fd_put_buffer,
349 migrate_fd_put_ready,
350 migrate_fd_wait_for_unfreeze,
351 migrate_fd_close);
353 DPRINTF("beginning savevm\n");
354 ret = qemu_savevm_state_begin(s->mon, s->file, s->mig_state.blk,
355 s->mig_state.shared);
356 if (ret < 0) {
357 DPRINTF("failed, %d\n", ret);
358 migrate_fd_error(s);
359 return;
362 migrate_fd_put_ready(s);
365 void migrate_fd_put_ready(void *opaque)
367 FdMigrationState *s = opaque;
369 if (s->state != MIG_STATE_ACTIVE) {
370 DPRINTF("put_ready returning because of non-active state\n");
371 return;
374 DPRINTF("iterate\n");
375 if (qemu_savevm_state_iterate(s->mon, s->file) == 1) {
376 int state;
377 int old_vm_running = vm_running;
379 DPRINTF("done iterating\n");
380 vm_stop(RSTATE_PRE_MIGRATE);
382 if ((qemu_savevm_state_complete(s->mon, s->file)) < 0) {
383 if (old_vm_running) {
384 vm_start();
386 state = MIG_STATE_ERROR;
387 } else {
388 state = MIG_STATE_COMPLETED;
390 if (migrate_fd_cleanup(s) < 0) {
391 if (old_vm_running) {
392 vm_start();
394 state = MIG_STATE_ERROR;
396 if (state == MIG_STATE_COMPLETED) {
397 runstate_set(RSTATE_POST_MIGRATE);
399 s->state = state;
400 notifier_list_notify(&migration_state_notifiers, NULL);
404 int migrate_fd_get_status(MigrationState *mig_state)
406 FdMigrationState *s = migrate_to_fms(mig_state);
407 return s->state;
410 void migrate_fd_cancel(MigrationState *mig_state)
412 FdMigrationState *s = migrate_to_fms(mig_state);
414 if (s->state != MIG_STATE_ACTIVE)
415 return;
417 DPRINTF("cancelling migration\n");
419 s->state = MIG_STATE_CANCELLED;
420 notifier_list_notify(&migration_state_notifiers, NULL);
421 qemu_savevm_state_cancel(s->mon, s->file);
423 migrate_fd_cleanup(s);
426 void migrate_fd_release(MigrationState *mig_state)
428 FdMigrationState *s = migrate_to_fms(mig_state);
430 DPRINTF("releasing state\n");
432 if (s->state == MIG_STATE_ACTIVE) {
433 s->state = MIG_STATE_CANCELLED;
434 notifier_list_notify(&migration_state_notifiers, NULL);
435 migrate_fd_cleanup(s);
437 g_free(s);
440 void migrate_fd_wait_for_unfreeze(void *opaque)
442 FdMigrationState *s = opaque;
443 int ret;
445 DPRINTF("wait for unfreeze\n");
446 if (s->state != MIG_STATE_ACTIVE)
447 return;
449 do {
450 fd_set wfds;
452 FD_ZERO(&wfds);
453 FD_SET(s->fd, &wfds);
455 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
456 } while (ret == -1 && (s->get_error(s)) == EINTR);
459 int migrate_fd_close(void *opaque)
461 FdMigrationState *s = opaque;
463 if (s->mon) {
464 monitor_resume(s->mon);
466 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
467 return s->close(s);
470 void add_migration_state_change_notifier(Notifier *notify)
472 notifier_list_add(&migration_state_notifiers, notify);
475 void remove_migration_state_change_notifier(Notifier *notify)
477 notifier_list_remove(&migration_state_notifiers, notify);
480 int get_migration_state(void)
482 if (current_migration) {
483 return migrate_fd_get_status(current_migration);
484 } else {
485 return MIG_STATE_ERROR;