block: iostatus: Drop BDRV_IOS_INVAL
[qemu/ar7.git] / migration.c
blob6c70f72ce3c598461e9d6af67931b55365e999f8
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 "qmp-commands.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 enum {
35 MIG_STATE_ERROR,
36 MIG_STATE_SETUP,
37 MIG_STATE_CANCELLED,
38 MIG_STATE_ACTIVE,
39 MIG_STATE_COMPLETED,
42 #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
44 static NotifierList migration_state_notifiers =
45 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
47 /* When we add fault tolerance, we could have several
48 migrations at once. For now we don't need to add
49 dynamic creation of migration */
51 static MigrationState *migrate_get_current(void)
53 static MigrationState current_migration = {
54 .state = MIG_STATE_SETUP,
55 .bandwidth_limit = MAX_THROTTLE,
58 return &current_migration;
61 int qemu_start_incoming_migration(const char *uri)
63 const char *p;
64 int ret;
66 if (strstart(uri, "tcp:", &p))
67 ret = tcp_start_incoming_migration(p);
68 #if !defined(WIN32)
69 else if (strstart(uri, "exec:", &p))
70 ret = exec_start_incoming_migration(p);
71 else if (strstart(uri, "unix:", &p))
72 ret = unix_start_incoming_migration(p);
73 else if (strstart(uri, "fd:", &p))
74 ret = fd_start_incoming_migration(p);
75 #endif
76 else {
77 fprintf(stderr, "unknown migration protocol: %s\n", uri);
78 ret = -EPROTONOSUPPORT;
80 return ret;
83 void process_incoming_migration(QEMUFile *f)
85 if (qemu_loadvm_state(f) < 0) {
86 fprintf(stderr, "load of migration failed\n");
87 exit(0);
89 qemu_announce_self();
90 DPRINTF("successfully loaded vm state\n");
92 if (autostart) {
93 vm_start();
94 } else {
95 runstate_set(RUN_STATE_PRELAUNCH);
99 /* amount of nanoseconds we are willing to wait for migration to be down.
100 * the choice of nanoseconds is because it is the maximum resolution that
101 * get_clock() can achieve. It is an internal measure. All user-visible
102 * units must be in seconds */
103 static uint64_t max_downtime = 30000000;
105 uint64_t migrate_max_downtime(void)
107 return max_downtime;
110 MigrationInfo *qmp_query_migrate(Error **errp)
112 MigrationInfo *info = g_malloc0(sizeof(*info));
113 MigrationState *s = migrate_get_current();
115 switch (s->state) {
116 case MIG_STATE_SETUP:
117 /* no migration has happened ever */
118 break;
119 case MIG_STATE_ACTIVE:
120 info->has_status = true;
121 info->status = g_strdup("active");
123 info->has_ram = true;
124 info->ram = g_malloc0(sizeof(*info->ram));
125 info->ram->transferred = ram_bytes_transferred();
126 info->ram->remaining = ram_bytes_remaining();
127 info->ram->total = ram_bytes_total();
129 if (blk_mig_active()) {
130 info->has_disk = true;
131 info->disk = g_malloc0(sizeof(*info->disk));
132 info->disk->transferred = blk_mig_bytes_transferred();
133 info->disk->remaining = blk_mig_bytes_remaining();
134 info->disk->total = blk_mig_bytes_total();
136 break;
137 case MIG_STATE_COMPLETED:
138 info->has_status = true;
139 info->status = g_strdup("completed");
140 break;
141 case MIG_STATE_ERROR:
142 info->has_status = true;
143 info->status = g_strdup("failed");
144 break;
145 case MIG_STATE_CANCELLED:
146 info->has_status = true;
147 info->status = g_strdup("cancelled");
148 break;
151 return info;
154 /* shared migration helpers */
156 static void migrate_fd_monitor_suspend(MigrationState *s, Monitor *mon)
158 s->mon = mon;
159 if (monitor_suspend(mon) == 0) {
160 DPRINTF("suspending monitor\n");
161 } else {
162 monitor_printf(mon, "terminal does not allow synchronous "
163 "migration, continuing detached\n");
167 static int migrate_fd_cleanup(MigrationState *s)
169 int ret = 0;
171 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
173 if (s->file) {
174 DPRINTF("closing file\n");
175 if (qemu_fclose(s->file) != 0) {
176 ret = -1;
178 s->file = NULL;
179 } else {
180 if (s->mon) {
181 monitor_resume(s->mon);
185 if (s->fd != -1) {
186 close(s->fd);
187 s->fd = -1;
190 return ret;
193 void migrate_fd_error(MigrationState *s)
195 DPRINTF("setting error state\n");
196 s->state = MIG_STATE_ERROR;
197 notifier_list_notify(&migration_state_notifiers, s);
198 migrate_fd_cleanup(s);
201 static void migrate_fd_completed(MigrationState *s)
203 DPRINTF("setting completed state\n");
204 if (migrate_fd_cleanup(s) < 0) {
205 s->state = MIG_STATE_ERROR;
206 } else {
207 s->state = MIG_STATE_COMPLETED;
208 runstate_set(RUN_STATE_POSTMIGRATE);
210 notifier_list_notify(&migration_state_notifiers, s);
213 static void migrate_fd_put_notify(void *opaque)
215 MigrationState *s = opaque;
217 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
218 qemu_file_put_notify(s->file);
219 if (qemu_file_get_error(s->file)) {
220 migrate_fd_error(s);
224 static ssize_t migrate_fd_put_buffer(void *opaque, const void *data,
225 size_t size)
227 MigrationState *s = opaque;
228 ssize_t ret;
230 if (s->state != MIG_STATE_ACTIVE) {
231 return -EIO;
234 do {
235 ret = s->write(s, data, size);
236 } while (ret == -1 && ((s->get_error(s)) == EINTR));
238 if (ret == -1)
239 ret = -(s->get_error(s));
241 if (ret == -EAGAIN) {
242 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
245 return ret;
248 static void migrate_fd_put_ready(void *opaque)
250 MigrationState *s = opaque;
251 int ret;
253 if (s->state != MIG_STATE_ACTIVE) {
254 DPRINTF("put_ready returning because of non-active state\n");
255 return;
258 DPRINTF("iterate\n");
259 ret = qemu_savevm_state_iterate(s->mon, s->file);
260 if (ret < 0) {
261 migrate_fd_error(s);
262 } else if (ret == 1) {
263 int old_vm_running = runstate_is_running();
265 DPRINTF("done iterating\n");
266 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
268 if (qemu_savevm_state_complete(s->mon, s->file) < 0) {
269 migrate_fd_error(s);
270 } else {
271 migrate_fd_completed(s);
273 if (s->state != MIG_STATE_COMPLETED) {
274 if (old_vm_running) {
275 vm_start();
281 static void migrate_fd_cancel(MigrationState *s)
283 if (s->state != MIG_STATE_ACTIVE)
284 return;
286 DPRINTF("cancelling migration\n");
288 s->state = MIG_STATE_CANCELLED;
289 notifier_list_notify(&migration_state_notifiers, s);
290 qemu_savevm_state_cancel(s->mon, s->file);
292 migrate_fd_cleanup(s);
295 static void migrate_fd_wait_for_unfreeze(void *opaque)
297 MigrationState *s = opaque;
298 int ret;
300 DPRINTF("wait for unfreeze\n");
301 if (s->state != MIG_STATE_ACTIVE)
302 return;
304 do {
305 fd_set wfds;
307 FD_ZERO(&wfds);
308 FD_SET(s->fd, &wfds);
310 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
311 } while (ret == -1 && (s->get_error(s)) == EINTR);
313 if (ret == -1) {
314 qemu_file_set_error(s->file, -s->get_error(s));
318 static int migrate_fd_close(void *opaque)
320 MigrationState *s = opaque;
322 if (s->mon) {
323 monitor_resume(s->mon);
325 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
326 return s->close(s);
329 void add_migration_state_change_notifier(Notifier *notify)
331 notifier_list_add(&migration_state_notifiers, notify);
334 void remove_migration_state_change_notifier(Notifier *notify)
336 notifier_list_remove(&migration_state_notifiers, notify);
339 bool migration_has_finished(MigrationState *s)
341 return s->state == MIG_STATE_COMPLETED;
344 void migrate_fd_connect(MigrationState *s)
346 int ret;
348 s->state = MIG_STATE_ACTIVE;
349 s->file = qemu_fopen_ops_buffered(s,
350 s->bandwidth_limit,
351 migrate_fd_put_buffer,
352 migrate_fd_put_ready,
353 migrate_fd_wait_for_unfreeze,
354 migrate_fd_close);
356 DPRINTF("beginning savevm\n");
357 ret = qemu_savevm_state_begin(s->mon, s->file, s->blk, s->shared);
358 if (ret < 0) {
359 DPRINTF("failed, %d\n", ret);
360 migrate_fd_error(s);
361 return;
363 migrate_fd_put_ready(s);
366 static MigrationState *migrate_init(Monitor *mon, int detach, int blk, int inc)
368 MigrationState *s = migrate_get_current();
369 int64_t bandwidth_limit = s->bandwidth_limit;
371 memset(s, 0, sizeof(*s));
372 s->bandwidth_limit = bandwidth_limit;
373 s->blk = blk;
374 s->shared = inc;
375 s->mon = NULL;
376 s->bandwidth_limit = bandwidth_limit;
377 s->state = MIG_STATE_SETUP;
379 if (!detach) {
380 migrate_fd_monitor_suspend(s, mon);
383 return s;
386 int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
388 MigrationState *s = migrate_get_current();
389 const char *p;
390 int detach = qdict_get_try_bool(qdict, "detach", 0);
391 int blk = qdict_get_try_bool(qdict, "blk", 0);
392 int inc = qdict_get_try_bool(qdict, "inc", 0);
393 const char *uri = qdict_get_str(qdict, "uri");
394 int ret;
396 if (s->state == MIG_STATE_ACTIVE) {
397 monitor_printf(mon, "migration already in progress\n");
398 return -1;
401 if (qemu_savevm_state_blocked(mon)) {
402 return -1;
405 s = migrate_init(mon, detach, blk, inc);
407 if (strstart(uri, "tcp:", &p)) {
408 ret = tcp_start_outgoing_migration(s, p);
409 #if !defined(WIN32)
410 } else if (strstart(uri, "exec:", &p)) {
411 ret = exec_start_outgoing_migration(s, p);
412 } else if (strstart(uri, "unix:", &p)) {
413 ret = unix_start_outgoing_migration(s, p);
414 } else if (strstart(uri, "fd:", &p)) {
415 ret = fd_start_outgoing_migration(s, p);
416 #endif
417 } else {
418 monitor_printf(mon, "unknown migration protocol: %s\n", uri);
419 ret = -EINVAL;
422 if (ret < 0) {
423 monitor_printf(mon, "migration failed: %s\n", strerror(-ret));
424 return ret;
427 notifier_list_notify(&migration_state_notifiers, s);
428 return 0;
431 int do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data)
433 migrate_fd_cancel(migrate_get_current());
434 return 0;
437 int do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data)
439 int64_t d;
440 MigrationState *s;
442 d = qdict_get_int(qdict, "value");
443 if (d < 0) {
444 d = 0;
447 s = migrate_get_current();
448 s->bandwidth_limit = d;
449 qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
451 return 0;
454 int do_migrate_set_downtime(Monitor *mon, const QDict *qdict,
455 QObject **ret_data)
457 double d;
459 d = qdict_get_double(qdict, "value") * 1e9;
460 d = MAX(0, MIN(UINT64_MAX, d));
461 max_downtime = (uint64_t)d;
463 return 0;