migration: Remove migration cancel() callback
[qemu/kevin.git] / migration.c
blob31b6741c7d0a46c06ac22aff39495e89ad8e56c5
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 if (autostart) {
74 vm_start();
75 } else {
76 runstate_set(RUN_STATE_PRELAUNCH);
80 static MigrationState *migrate_new(Monitor *mon, int64_t bandwidth_limit,
81 int detach, int blk, int inc);
83 int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
85 MigrationState *s = NULL;
86 const char *p;
87 int detach = qdict_get_try_bool(qdict, "detach", 0);
88 int blk = qdict_get_try_bool(qdict, "blk", 0);
89 int inc = qdict_get_try_bool(qdict, "inc", 0);
90 const char *uri = qdict_get_str(qdict, "uri");
91 int ret;
93 if (current_migration &&
94 current_migration->state == MIG_STATE_ACTIVE) {
95 monitor_printf(mon, "migration already in progress\n");
96 return -1;
99 if (qemu_savevm_state_blocked(mon)) {
100 return -1;
103 s = migrate_new(mon, max_throttle, detach, blk, inc);
105 if (strstart(uri, "tcp:", &p)) {
106 ret = tcp_start_outgoing_migration(s, p);
107 #if !defined(WIN32)
108 } else if (strstart(uri, "exec:", &p)) {
109 ret = exec_start_outgoing_migration(s, p);
110 } else if (strstart(uri, "unix:", &p)) {
111 ret = unix_start_outgoing_migration(s, p);
112 } else if (strstart(uri, "fd:", &p)) {
113 ret = fd_start_outgoing_migration(s, p);
114 #endif
115 } else {
116 monitor_printf(mon, "unknown migration protocol: %s\n", uri);
117 ret = -EINVAL;
118 goto free_migrate_state;
121 if (ret < 0) {
122 monitor_printf(mon, "migration failed\n");
123 goto free_migrate_state;
126 g_free(current_migration);
127 current_migration = s;
128 notifier_list_notify(&migration_state_notifiers, NULL);
129 return 0;
130 free_migrate_state:
131 g_free(s);
132 return -1;
135 static void migrate_fd_cancel(MigrationState *s);
137 int do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data)
139 if (current_migration) {
140 migrate_fd_cancel(current_migration);
142 return 0;
145 int do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data)
147 int64_t d;
148 MigrationState *s;
150 d = qdict_get_int(qdict, "value");
151 if (d < 0) {
152 d = 0;
154 max_throttle = d;
156 s = current_migration;
157 if (s && s->file) {
158 qemu_file_set_rate_limit(s->file, max_throttle);
161 return 0;
164 /* amount of nanoseconds we are willing to wait for migration to be down.
165 * the choice of nanoseconds is because it is the maximum resolution that
166 * get_clock() can achieve. It is an internal measure. All user-visible
167 * units must be in seconds */
168 static uint64_t max_downtime = 30000000;
170 uint64_t migrate_max_downtime(void)
172 return max_downtime;
175 int do_migrate_set_downtime(Monitor *mon, const QDict *qdict,
176 QObject **ret_data)
178 double d;
180 d = qdict_get_double(qdict, "value") * 1e9;
181 d = MAX(0, MIN(UINT64_MAX, d));
182 max_downtime = (uint64_t)d;
184 return 0;
187 static void migrate_print_status(Monitor *mon, const char *name,
188 const QDict *status_dict)
190 QDict *qdict;
192 qdict = qobject_to_qdict(qdict_get(status_dict, name));
194 monitor_printf(mon, "transferred %s: %" PRIu64 " kbytes\n", name,
195 qdict_get_int(qdict, "transferred") >> 10);
196 monitor_printf(mon, "remaining %s: %" PRIu64 " kbytes\n", name,
197 qdict_get_int(qdict, "remaining") >> 10);
198 monitor_printf(mon, "total %s: %" PRIu64 " kbytes\n", name,
199 qdict_get_int(qdict, "total") >> 10);
202 void do_info_migrate_print(Monitor *mon, const QObject *data)
204 QDict *qdict;
206 qdict = qobject_to_qdict(data);
208 monitor_printf(mon, "Migration status: %s\n",
209 qdict_get_str(qdict, "status"));
211 if (qdict_haskey(qdict, "ram")) {
212 migrate_print_status(mon, "ram", qdict);
215 if (qdict_haskey(qdict, "disk")) {
216 migrate_print_status(mon, "disk", qdict);
220 static void migrate_put_status(QDict *qdict, const char *name,
221 uint64_t trans, uint64_t rem, uint64_t total)
223 QObject *obj;
225 obj = qobject_from_jsonf("{ 'transferred': %" PRId64 ", "
226 "'remaining': %" PRId64 ", "
227 "'total': %" PRId64 " }", trans, rem, total);
228 qdict_put_obj(qdict, name, obj);
231 void do_info_migrate(Monitor *mon, QObject **ret_data)
233 QDict *qdict;
235 if (current_migration) {
236 MigrationState *s = current_migration;
238 switch (s->state) {
239 case MIG_STATE_SETUP:
240 /* no migration has happened ever */
241 break;
242 case MIG_STATE_ACTIVE:
243 qdict = qdict_new();
244 qdict_put(qdict, "status", qstring_from_str("active"));
246 migrate_put_status(qdict, "ram", ram_bytes_transferred(),
247 ram_bytes_remaining(), ram_bytes_total());
249 if (blk_mig_active()) {
250 migrate_put_status(qdict, "disk", blk_mig_bytes_transferred(),
251 blk_mig_bytes_remaining(),
252 blk_mig_bytes_total());
255 *ret_data = QOBJECT(qdict);
256 break;
257 case MIG_STATE_COMPLETED:
258 *ret_data = qobject_from_jsonf("{ 'status': 'completed' }");
259 break;
260 case MIG_STATE_ERROR:
261 *ret_data = qobject_from_jsonf("{ 'status': 'failed' }");
262 break;
263 case MIG_STATE_CANCELLED:
264 *ret_data = qobject_from_jsonf("{ 'status': 'cancelled' }");
265 break;
270 /* shared migration helpers */
272 static void migrate_fd_monitor_suspend(MigrationState *s, Monitor *mon)
274 s->mon = mon;
275 if (monitor_suspend(mon) == 0) {
276 DPRINTF("suspending monitor\n");
277 } else {
278 monitor_printf(mon, "terminal does not allow synchronous "
279 "migration, continuing detached\n");
283 static int migrate_fd_cleanup(MigrationState *s)
285 int ret = 0;
287 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
289 if (s->file) {
290 DPRINTF("closing file\n");
291 if (qemu_fclose(s->file) != 0) {
292 ret = -1;
294 s->file = NULL;
295 } else {
296 if (s->mon) {
297 monitor_resume(s->mon);
301 if (s->fd != -1) {
302 close(s->fd);
303 s->fd = -1;
306 return ret;
309 void migrate_fd_error(MigrationState *s)
311 DPRINTF("setting error state\n");
312 s->state = MIG_STATE_ERROR;
313 notifier_list_notify(&migration_state_notifiers, NULL);
314 migrate_fd_cleanup(s);
317 static void migrate_fd_completed(MigrationState *s)
319 DPRINTF("setting completed state\n");
320 if (migrate_fd_cleanup(s) < 0) {
321 s->state = MIG_STATE_ERROR;
322 } else {
323 s->state = MIG_STATE_COMPLETED;
324 runstate_set(RUN_STATE_POSTMIGRATE);
326 notifier_list_notify(&migration_state_notifiers, NULL);
329 static void migrate_fd_put_notify(void *opaque)
331 MigrationState *s = opaque;
333 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
334 qemu_file_put_notify(s->file);
335 if (qemu_file_get_error(s->file)) {
336 migrate_fd_error(s);
340 static ssize_t migrate_fd_put_buffer(void *opaque, const void *data,
341 size_t size)
343 MigrationState *s = opaque;
344 ssize_t ret;
346 if (s->state != MIG_STATE_ACTIVE) {
347 return -EIO;
350 do {
351 ret = s->write(s, data, size);
352 } while (ret == -1 && ((s->get_error(s)) == EINTR));
354 if (ret == -1)
355 ret = -(s->get_error(s));
357 if (ret == -EAGAIN) {
358 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
361 return ret;
364 static void migrate_fd_put_ready(void *opaque)
366 MigrationState *s = opaque;
367 int ret;
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 ret = qemu_savevm_state_iterate(s->mon, s->file);
376 if (ret < 0) {
377 migrate_fd_error(s);
378 } else if (ret == 1) {
379 int old_vm_running = runstate_is_running();
381 DPRINTF("done iterating\n");
382 vm_stop(RUN_STATE_FINISH_MIGRATE);
384 if (qemu_savevm_state_complete(s->mon, s->file) < 0) {
385 migrate_fd_error(s);
386 } else {
387 migrate_fd_completed(s);
389 if (s->state != MIG_STATE_COMPLETED) {
390 if (old_vm_running) {
391 vm_start();
397 static void migrate_fd_cancel(MigrationState *s)
399 if (s->state != MIG_STATE_ACTIVE)
400 return;
402 DPRINTF("cancelling migration\n");
404 s->state = MIG_STATE_CANCELLED;
405 notifier_list_notify(&migration_state_notifiers, NULL);
406 qemu_savevm_state_cancel(s->mon, s->file);
408 migrate_fd_cleanup(s);
411 static void migrate_fd_wait_for_unfreeze(void *opaque)
413 MigrationState *s = opaque;
414 int ret;
416 DPRINTF("wait for unfreeze\n");
417 if (s->state != MIG_STATE_ACTIVE)
418 return;
420 do {
421 fd_set wfds;
423 FD_ZERO(&wfds);
424 FD_SET(s->fd, &wfds);
426 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
427 } while (ret == -1 && (s->get_error(s)) == EINTR);
429 if (ret == -1) {
430 qemu_file_set_error(s->file, -s->get_error(s));
434 static int migrate_fd_close(void *opaque)
436 MigrationState *s = opaque;
438 if (s->mon) {
439 monitor_resume(s->mon);
441 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
442 return s->close(s);
445 void add_migration_state_change_notifier(Notifier *notify)
447 notifier_list_add(&migration_state_notifiers, notify);
450 void remove_migration_state_change_notifier(Notifier *notify)
452 notifier_list_remove(&migration_state_notifiers, notify);
455 int get_migration_state(void)
457 if (current_migration) {
458 return current_migration->state;
459 } else {
460 return MIG_STATE_ERROR;
464 void migrate_fd_connect(MigrationState *s)
466 int ret;
468 s->state = MIG_STATE_ACTIVE;
469 s->file = qemu_fopen_ops_buffered(s,
470 s->bandwidth_limit,
471 migrate_fd_put_buffer,
472 migrate_fd_put_ready,
473 migrate_fd_wait_for_unfreeze,
474 migrate_fd_close);
476 DPRINTF("beginning savevm\n");
477 ret = qemu_savevm_state_begin(s->mon, s->file, s->blk, s->shared);
478 if (ret < 0) {
479 DPRINTF("failed, %d\n", ret);
480 migrate_fd_error(s);
481 return;
483 migrate_fd_put_ready(s);
486 static MigrationState *migrate_new(Monitor *mon, int64_t bandwidth_limit,
487 int detach, int blk, int inc)
489 MigrationState *s = g_malloc0(sizeof(*s));
491 s->blk = blk;
492 s->shared = inc;
493 s->mon = NULL;
494 s->bandwidth_limit = bandwidth_limit;
495 s->state = MIG_STATE_SETUP;
497 if (!detach) {
498 migrate_fd_monitor_suspend(s, mon);
501 return s;