migration: Move exported functions to the end of the file
[qemu.git] / migration.c
blobc852f2e9dd7408300161bbdbf547f066eab719b6
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 /* amount of nanoseconds we are willing to wait for migration to be down.
81 * the choice of nanoseconds is because it is the maximum resolution that
82 * get_clock() can achieve. It is an internal measure. All user-visible
83 * units must be in seconds */
84 static uint64_t max_downtime = 30000000;
86 uint64_t migrate_max_downtime(void)
88 return max_downtime;
91 static void migrate_print_status(Monitor *mon, const char *name,
92 const QDict *status_dict)
94 QDict *qdict;
96 qdict = qobject_to_qdict(qdict_get(status_dict, name));
98 monitor_printf(mon, "transferred %s: %" PRIu64 " kbytes\n", name,
99 qdict_get_int(qdict, "transferred") >> 10);
100 monitor_printf(mon, "remaining %s: %" PRIu64 " kbytes\n", name,
101 qdict_get_int(qdict, "remaining") >> 10);
102 monitor_printf(mon, "total %s: %" PRIu64 " kbytes\n", name,
103 qdict_get_int(qdict, "total") >> 10);
106 void do_info_migrate_print(Monitor *mon, const QObject *data)
108 QDict *qdict;
110 qdict = qobject_to_qdict(data);
112 monitor_printf(mon, "Migration status: %s\n",
113 qdict_get_str(qdict, "status"));
115 if (qdict_haskey(qdict, "ram")) {
116 migrate_print_status(mon, "ram", qdict);
119 if (qdict_haskey(qdict, "disk")) {
120 migrate_print_status(mon, "disk", qdict);
124 static void migrate_put_status(QDict *qdict, const char *name,
125 uint64_t trans, uint64_t rem, uint64_t total)
127 QObject *obj;
129 obj = qobject_from_jsonf("{ 'transferred': %" PRId64 ", "
130 "'remaining': %" PRId64 ", "
131 "'total': %" PRId64 " }", trans, rem, total);
132 qdict_put_obj(qdict, name, obj);
135 void do_info_migrate(Monitor *mon, QObject **ret_data)
137 QDict *qdict;
139 if (current_migration) {
140 MigrationState *s = current_migration;
142 switch (s->state) {
143 case MIG_STATE_SETUP:
144 /* no migration has happened ever */
145 break;
146 case MIG_STATE_ACTIVE:
147 qdict = qdict_new();
148 qdict_put(qdict, "status", qstring_from_str("active"));
150 migrate_put_status(qdict, "ram", ram_bytes_transferred(),
151 ram_bytes_remaining(), ram_bytes_total());
153 if (blk_mig_active()) {
154 migrate_put_status(qdict, "disk", blk_mig_bytes_transferred(),
155 blk_mig_bytes_remaining(),
156 blk_mig_bytes_total());
159 *ret_data = QOBJECT(qdict);
160 break;
161 case MIG_STATE_COMPLETED:
162 *ret_data = qobject_from_jsonf("{ 'status': 'completed' }");
163 break;
164 case MIG_STATE_ERROR:
165 *ret_data = qobject_from_jsonf("{ 'status': 'failed' }");
166 break;
167 case MIG_STATE_CANCELLED:
168 *ret_data = qobject_from_jsonf("{ 'status': 'cancelled' }");
169 break;
174 /* shared migration helpers */
176 static void migrate_fd_monitor_suspend(MigrationState *s, Monitor *mon)
178 s->mon = mon;
179 if (monitor_suspend(mon) == 0) {
180 DPRINTF("suspending monitor\n");
181 } else {
182 monitor_printf(mon, "terminal does not allow synchronous "
183 "migration, continuing detached\n");
187 static int migrate_fd_cleanup(MigrationState *s)
189 int ret = 0;
191 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
193 if (s->file) {
194 DPRINTF("closing file\n");
195 if (qemu_fclose(s->file) != 0) {
196 ret = -1;
198 s->file = NULL;
199 } else {
200 if (s->mon) {
201 monitor_resume(s->mon);
205 if (s->fd != -1) {
206 close(s->fd);
207 s->fd = -1;
210 return ret;
213 void migrate_fd_error(MigrationState *s)
215 DPRINTF("setting error state\n");
216 s->state = MIG_STATE_ERROR;
217 notifier_list_notify(&migration_state_notifiers, NULL);
218 migrate_fd_cleanup(s);
221 static void migrate_fd_completed(MigrationState *s)
223 DPRINTF("setting completed state\n");
224 if (migrate_fd_cleanup(s) < 0) {
225 s->state = MIG_STATE_ERROR;
226 } else {
227 s->state = MIG_STATE_COMPLETED;
228 runstate_set(RUN_STATE_POSTMIGRATE);
230 notifier_list_notify(&migration_state_notifiers, NULL);
233 static void migrate_fd_put_notify(void *opaque)
235 MigrationState *s = opaque;
237 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
238 qemu_file_put_notify(s->file);
239 if (qemu_file_get_error(s->file)) {
240 migrate_fd_error(s);
244 static ssize_t migrate_fd_put_buffer(void *opaque, const void *data,
245 size_t size)
247 MigrationState *s = opaque;
248 ssize_t ret;
250 if (s->state != MIG_STATE_ACTIVE) {
251 return -EIO;
254 do {
255 ret = s->write(s, data, size);
256 } while (ret == -1 && ((s->get_error(s)) == EINTR));
258 if (ret == -1)
259 ret = -(s->get_error(s));
261 if (ret == -EAGAIN) {
262 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
265 return ret;
268 static void migrate_fd_put_ready(void *opaque)
270 MigrationState *s = opaque;
271 int ret;
273 if (s->state != MIG_STATE_ACTIVE) {
274 DPRINTF("put_ready returning because of non-active state\n");
275 return;
278 DPRINTF("iterate\n");
279 ret = qemu_savevm_state_iterate(s->mon, s->file);
280 if (ret < 0) {
281 migrate_fd_error(s);
282 } else if (ret == 1) {
283 int old_vm_running = runstate_is_running();
285 DPRINTF("done iterating\n");
286 vm_stop(RUN_STATE_FINISH_MIGRATE);
288 if (qemu_savevm_state_complete(s->mon, s->file) < 0) {
289 migrate_fd_error(s);
290 } else {
291 migrate_fd_completed(s);
293 if (s->state != MIG_STATE_COMPLETED) {
294 if (old_vm_running) {
295 vm_start();
301 static void migrate_fd_cancel(MigrationState *s)
303 if (s->state != MIG_STATE_ACTIVE)
304 return;
306 DPRINTF("cancelling migration\n");
308 s->state = MIG_STATE_CANCELLED;
309 notifier_list_notify(&migration_state_notifiers, NULL);
310 qemu_savevm_state_cancel(s->mon, s->file);
312 migrate_fd_cleanup(s);
315 static void migrate_fd_wait_for_unfreeze(void *opaque)
317 MigrationState *s = opaque;
318 int ret;
320 DPRINTF("wait for unfreeze\n");
321 if (s->state != MIG_STATE_ACTIVE)
322 return;
324 do {
325 fd_set wfds;
327 FD_ZERO(&wfds);
328 FD_SET(s->fd, &wfds);
330 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
331 } while (ret == -1 && (s->get_error(s)) == EINTR);
333 if (ret == -1) {
334 qemu_file_set_error(s->file, -s->get_error(s));
338 static int migrate_fd_close(void *opaque)
340 MigrationState *s = opaque;
342 if (s->mon) {
343 monitor_resume(s->mon);
345 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
346 return s->close(s);
349 void add_migration_state_change_notifier(Notifier *notify)
351 notifier_list_add(&migration_state_notifiers, notify);
354 void remove_migration_state_change_notifier(Notifier *notify)
356 notifier_list_remove(&migration_state_notifiers, notify);
359 int get_migration_state(void)
361 if (current_migration) {
362 return current_migration->state;
363 } else {
364 return MIG_STATE_ERROR;
368 void migrate_fd_connect(MigrationState *s)
370 int ret;
372 s->state = MIG_STATE_ACTIVE;
373 s->file = qemu_fopen_ops_buffered(s,
374 s->bandwidth_limit,
375 migrate_fd_put_buffer,
376 migrate_fd_put_ready,
377 migrate_fd_wait_for_unfreeze,
378 migrate_fd_close);
380 DPRINTF("beginning savevm\n");
381 ret = qemu_savevm_state_begin(s->mon, s->file, s->blk, s->shared);
382 if (ret < 0) {
383 DPRINTF("failed, %d\n", ret);
384 migrate_fd_error(s);
385 return;
387 migrate_fd_put_ready(s);
390 static MigrationState *migrate_new(Monitor *mon, int64_t bandwidth_limit,
391 int detach, int blk, int inc)
393 MigrationState *s = g_malloc0(sizeof(*s));
395 s->blk = blk;
396 s->shared = inc;
397 s->mon = NULL;
398 s->bandwidth_limit = bandwidth_limit;
399 s->state = MIG_STATE_SETUP;
401 if (!detach) {
402 migrate_fd_monitor_suspend(s, mon);
405 return s;
408 int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
410 MigrationState *s = NULL;
411 const char *p;
412 int detach = qdict_get_try_bool(qdict, "detach", 0);
413 int blk = qdict_get_try_bool(qdict, "blk", 0);
414 int inc = qdict_get_try_bool(qdict, "inc", 0);
415 const char *uri = qdict_get_str(qdict, "uri");
416 int ret;
418 if (current_migration &&
419 current_migration->state == MIG_STATE_ACTIVE) {
420 monitor_printf(mon, "migration already in progress\n");
421 return -1;
424 if (qemu_savevm_state_blocked(mon)) {
425 return -1;
428 s = migrate_new(mon, max_throttle, detach, blk, inc);
430 if (strstart(uri, "tcp:", &p)) {
431 ret = tcp_start_outgoing_migration(s, p);
432 #if !defined(WIN32)
433 } else if (strstart(uri, "exec:", &p)) {
434 ret = exec_start_outgoing_migration(s, p);
435 } else if (strstart(uri, "unix:", &p)) {
436 ret = unix_start_outgoing_migration(s, p);
437 } else if (strstart(uri, "fd:", &p)) {
438 ret = fd_start_outgoing_migration(s, p);
439 #endif
440 } else {
441 monitor_printf(mon, "unknown migration protocol: %s\n", uri);
442 ret = -EINVAL;
443 goto free_migrate_state;
446 if (ret < 0) {
447 monitor_printf(mon, "migration failed\n");
448 goto free_migrate_state;
451 g_free(current_migration);
452 current_migration = s;
453 notifier_list_notify(&migration_state_notifiers, NULL);
454 return 0;
455 free_migrate_state:
456 g_free(s);
457 return -1;
460 int do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data)
462 if (current_migration) {
463 migrate_fd_cancel(current_migration);
465 return 0;
468 int do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data)
470 int64_t d;
471 MigrationState *s;
473 d = qdict_get_int(qdict, "value");
474 if (d < 0) {
475 d = 0;
477 max_throttle = d;
479 s = current_migration;
480 if (s && s->file) {
481 qemu_file_set_rate_limit(s->file, max_throttle);
484 return 0;
487 int do_migrate_set_downtime(Monitor *mon, const QDict *qdict,
488 QObject **ret_data)
490 double d;
492 d = qdict_get_double(qdict, "value") * 1e9;
493 d = MAX(0, MIN(UINT64_MAX, d));
494 max_downtime = (uint64_t)d;
496 return 0;