qapi: add test case for deallocating traversal of incomplete structure
[qemu/ar7.git] / migration.c
blob8db1b433f02a6add494eeb70a425e3285337d2fa
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.
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
16 #include "qemu-common.h"
17 #include "migration.h"
18 #include "monitor.h"
19 #include "buffered_file.h"
20 #include "sysemu.h"
21 #include "block.h"
22 #include "qemu_socket.h"
23 #include "block-migration.h"
24 #include "qmp-commands.h"
26 //#define DEBUG_MIGRATION
28 #ifdef DEBUG_MIGRATION
29 #define DPRINTF(fmt, ...) \
30 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
31 #else
32 #define DPRINTF(fmt, ...) \
33 do { } while (0)
34 #endif
36 enum {
37 MIG_STATE_ERROR,
38 MIG_STATE_SETUP,
39 MIG_STATE_CANCELLED,
40 MIG_STATE_ACTIVE,
41 MIG_STATE_COMPLETED,
44 #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
46 static NotifierList migration_state_notifiers =
47 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
49 /* When we add fault tolerance, we could have several
50 migrations at once. For now we don't need to add
51 dynamic creation of migration */
53 static MigrationState *migrate_get_current(void)
55 static MigrationState current_migration = {
56 .state = MIG_STATE_SETUP,
57 .bandwidth_limit = MAX_THROTTLE,
60 return &current_migration;
63 int qemu_start_incoming_migration(const char *uri, Error **errp)
65 const char *p;
66 int ret;
68 if (strstart(uri, "tcp:", &p))
69 ret = tcp_start_incoming_migration(p, errp);
70 #if !defined(WIN32)
71 else if (strstart(uri, "exec:", &p))
72 ret = exec_start_incoming_migration(p);
73 else if (strstart(uri, "unix:", &p))
74 ret = unix_start_incoming_migration(p);
75 else if (strstart(uri, "fd:", &p))
76 ret = fd_start_incoming_migration(p);
77 #endif
78 else {
79 fprintf(stderr, "unknown migration protocol: %s\n", uri);
80 ret = -EPROTONOSUPPORT;
82 return ret;
85 void process_incoming_migration(QEMUFile *f)
87 if (qemu_loadvm_state(f) < 0) {
88 fprintf(stderr, "load of migration failed\n");
89 exit(0);
91 qemu_announce_self();
92 DPRINTF("successfully loaded vm state\n");
94 bdrv_clear_incoming_migration_all();
95 /* Make sure all file formats flush their mutable metadata */
96 bdrv_invalidate_cache_all();
98 if (autostart) {
99 vm_start();
100 } else {
101 runstate_set(RUN_STATE_PRELAUNCH);
105 /* amount of nanoseconds we are willing to wait for migration to be down.
106 * the choice of nanoseconds is because it is the maximum resolution that
107 * get_clock() can achieve. It is an internal measure. All user-visible
108 * units must be in seconds */
109 static uint64_t max_downtime = 30000000;
111 uint64_t migrate_max_downtime(void)
113 return max_downtime;
116 MigrationInfo *qmp_query_migrate(Error **errp)
118 MigrationInfo *info = g_malloc0(sizeof(*info));
119 MigrationState *s = migrate_get_current();
121 switch (s->state) {
122 case MIG_STATE_SETUP:
123 /* no migration has happened ever */
124 break;
125 case MIG_STATE_ACTIVE:
126 info->has_status = true;
127 info->status = g_strdup("active");
129 info->has_ram = true;
130 info->ram = g_malloc0(sizeof(*info->ram));
131 info->ram->transferred = ram_bytes_transferred();
132 info->ram->remaining = ram_bytes_remaining();
133 info->ram->total = ram_bytes_total();
134 info->ram->total_time = qemu_get_clock_ms(rt_clock)
135 - s->total_time;
137 if (blk_mig_active()) {
138 info->has_disk = true;
139 info->disk = g_malloc0(sizeof(*info->disk));
140 info->disk->transferred = blk_mig_bytes_transferred();
141 info->disk->remaining = blk_mig_bytes_remaining();
142 info->disk->total = blk_mig_bytes_total();
144 break;
145 case MIG_STATE_COMPLETED:
146 info->has_status = true;
147 info->status = g_strdup("completed");
149 info->has_ram = true;
150 info->ram = g_malloc0(sizeof(*info->ram));
151 info->ram->transferred = ram_bytes_transferred();
152 info->ram->remaining = 0;
153 info->ram->total = ram_bytes_total();
154 info->ram->total_time = s->total_time;
155 break;
156 case MIG_STATE_ERROR:
157 info->has_status = true;
158 info->status = g_strdup("failed");
159 break;
160 case MIG_STATE_CANCELLED:
161 info->has_status = true;
162 info->status = g_strdup("cancelled");
163 break;
166 return info;
169 /* shared migration helpers */
171 static int migrate_fd_cleanup(MigrationState *s)
173 int ret = 0;
175 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
177 if (s->file) {
178 DPRINTF("closing file\n");
179 ret = qemu_fclose(s->file);
180 s->file = NULL;
183 if (s->fd != -1) {
184 close(s->fd);
185 s->fd = -1;
188 return ret;
191 void migrate_fd_error(MigrationState *s)
193 DPRINTF("setting error state\n");
194 s->state = MIG_STATE_ERROR;
195 notifier_list_notify(&migration_state_notifiers, s);
196 migrate_fd_cleanup(s);
199 static void migrate_fd_completed(MigrationState *s)
201 DPRINTF("setting completed state\n");
202 if (migrate_fd_cleanup(s) < 0) {
203 s->state = MIG_STATE_ERROR;
204 } else {
205 s->state = MIG_STATE_COMPLETED;
206 runstate_set(RUN_STATE_POSTMIGRATE);
208 notifier_list_notify(&migration_state_notifiers, s);
211 static void migrate_fd_put_notify(void *opaque)
213 MigrationState *s = opaque;
215 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
216 qemu_file_put_notify(s->file);
217 if (s->file && qemu_file_get_error(s->file)) {
218 migrate_fd_error(s);
222 static ssize_t migrate_fd_put_buffer(void *opaque, const void *data,
223 size_t size)
225 MigrationState *s = opaque;
226 ssize_t ret;
228 if (s->state != MIG_STATE_ACTIVE) {
229 return -EIO;
232 do {
233 ret = s->write(s, data, size);
234 } while (ret == -1 && ((s->get_error(s)) == EINTR));
236 if (ret == -1)
237 ret = -(s->get_error(s));
239 if (ret == -EAGAIN) {
240 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
243 return ret;
246 static void migrate_fd_put_ready(void *opaque)
248 MigrationState *s = opaque;
249 int ret;
251 if (s->state != MIG_STATE_ACTIVE) {
252 DPRINTF("put_ready returning because of non-active state\n");
253 return;
256 DPRINTF("iterate\n");
257 ret = qemu_savevm_state_iterate(s->file);
258 if (ret < 0) {
259 migrate_fd_error(s);
260 } else if (ret == 1) {
261 int old_vm_running = runstate_is_running();
263 DPRINTF("done iterating\n");
264 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
265 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
267 if (qemu_savevm_state_complete(s->file) < 0) {
268 migrate_fd_error(s);
269 } else {
270 migrate_fd_completed(s);
272 s->total_time = qemu_get_clock_ms(rt_clock) - s->total_time;
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->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 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
323 return s->close(s);
326 void add_migration_state_change_notifier(Notifier *notify)
328 notifier_list_add(&migration_state_notifiers, notify);
331 void remove_migration_state_change_notifier(Notifier *notify)
333 notifier_remove(notify);
336 bool migration_is_active(MigrationState *s)
338 return s->state == MIG_STATE_ACTIVE;
341 bool migration_has_finished(MigrationState *s)
343 return s->state == MIG_STATE_COMPLETED;
346 bool migration_has_failed(MigrationState *s)
348 return (s->state == MIG_STATE_CANCELLED ||
349 s->state == MIG_STATE_ERROR);
352 void migrate_fd_connect(MigrationState *s)
354 int ret;
356 s->state = MIG_STATE_ACTIVE;
357 s->file = qemu_fopen_ops_buffered(s,
358 s->bandwidth_limit,
359 migrate_fd_put_buffer,
360 migrate_fd_put_ready,
361 migrate_fd_wait_for_unfreeze,
362 migrate_fd_close);
364 DPRINTF("beginning savevm\n");
365 ret = qemu_savevm_state_begin(s->file, &s->params);
366 if (ret < 0) {
367 DPRINTF("failed, %d\n", ret);
368 migrate_fd_error(s);
369 return;
371 migrate_fd_put_ready(s);
374 static MigrationState *migrate_init(const MigrationParams *params)
376 MigrationState *s = migrate_get_current();
377 int64_t bandwidth_limit = s->bandwidth_limit;
379 memset(s, 0, sizeof(*s));
380 s->bandwidth_limit = bandwidth_limit;
381 s->params = *params;
383 s->bandwidth_limit = bandwidth_limit;
384 s->state = MIG_STATE_SETUP;
385 s->total_time = qemu_get_clock_ms(rt_clock);
387 return s;
390 static GSList *migration_blockers;
392 void migrate_add_blocker(Error *reason)
394 migration_blockers = g_slist_prepend(migration_blockers, reason);
397 void migrate_del_blocker(Error *reason)
399 migration_blockers = g_slist_remove(migration_blockers, reason);
402 void qmp_migrate(const char *uri, bool has_blk, bool blk,
403 bool has_inc, bool inc, bool has_detach, bool detach,
404 Error **errp)
406 MigrationState *s = migrate_get_current();
407 MigrationParams params;
408 const char *p;
409 int ret;
411 params.blk = blk;
412 params.shared = inc;
414 if (s->state == MIG_STATE_ACTIVE) {
415 error_set(errp, QERR_MIGRATION_ACTIVE);
416 return;
419 if (qemu_savevm_state_blocked(errp)) {
420 return;
423 if (migration_blockers) {
424 *errp = error_copy(migration_blockers->data);
425 return;
428 s = migrate_init(&params);
430 if (strstart(uri, "tcp:", &p)) {
431 ret = tcp_start_outgoing_migration(s, p, errp);
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 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
442 return;
445 if (ret < 0) {
446 if (!error_is_set(errp)) {
447 DPRINTF("migration failed: %s\n", strerror(-ret));
448 /* FIXME: we should return meaningful errors */
449 error_set(errp, QERR_UNDEFINED_ERROR);
451 return;
454 notifier_list_notify(&migration_state_notifiers, s);
457 void qmp_migrate_cancel(Error **errp)
459 migrate_fd_cancel(migrate_get_current());
462 void qmp_migrate_set_speed(int64_t value, Error **errp)
464 MigrationState *s;
466 if (value < 0) {
467 value = 0;
470 s = migrate_get_current();
471 s->bandwidth_limit = value;
472 qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
475 void qmp_migrate_set_downtime(double value, Error **errp)
477 value *= 1e9;
478 value = MAX(0, MIN(UINT64_MAX, value));
479 max_downtime = (uint64_t)value;