PPC: Disable non-440 CPUs for ppcemb target
[qemu/ar7.git] / migration.c
blobbdca72e0085a307062ec788b99255ace2354373b
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 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 static void migrate_print_status(Monitor *mon, const char *name,
111 const QDict *status_dict)
113 QDict *qdict;
115 qdict = qobject_to_qdict(qdict_get(status_dict, name));
117 monitor_printf(mon, "transferred %s: %" PRIu64 " kbytes\n", name,
118 qdict_get_int(qdict, "transferred") >> 10);
119 monitor_printf(mon, "remaining %s: %" PRIu64 " kbytes\n", name,
120 qdict_get_int(qdict, "remaining") >> 10);
121 monitor_printf(mon, "total %s: %" PRIu64 " kbytes\n", name,
122 qdict_get_int(qdict, "total") >> 10);
125 void do_info_migrate_print(Monitor *mon, const QObject *data)
127 QDict *qdict;
129 qdict = qobject_to_qdict(data);
131 monitor_printf(mon, "Migration status: %s\n",
132 qdict_get_str(qdict, "status"));
134 if (qdict_haskey(qdict, "ram")) {
135 migrate_print_status(mon, "ram", qdict);
138 if (qdict_haskey(qdict, "disk")) {
139 migrate_print_status(mon, "disk", qdict);
143 static void migrate_put_status(QDict *qdict, const char *name,
144 uint64_t trans, uint64_t rem, uint64_t total)
146 QObject *obj;
148 obj = qobject_from_jsonf("{ 'transferred': %" PRId64 ", "
149 "'remaining': %" PRId64 ", "
150 "'total': %" PRId64 " }", trans, rem, total);
151 qdict_put_obj(qdict, name, obj);
154 void do_info_migrate(Monitor *mon, QObject **ret_data)
156 QDict *qdict;
157 MigrationState *s = migrate_get_current();
159 switch (s->state) {
160 case MIG_STATE_SETUP:
161 /* no migration has happened ever */
162 break;
163 case MIG_STATE_ACTIVE:
164 qdict = qdict_new();
165 qdict_put(qdict, "status", qstring_from_str("active"));
167 migrate_put_status(qdict, "ram", ram_bytes_transferred(),
168 ram_bytes_remaining(), ram_bytes_total());
170 if (blk_mig_active()) {
171 migrate_put_status(qdict, "disk", blk_mig_bytes_transferred(),
172 blk_mig_bytes_remaining(),
173 blk_mig_bytes_total());
176 *ret_data = QOBJECT(qdict);
177 break;
178 case MIG_STATE_COMPLETED:
179 *ret_data = qobject_from_jsonf("{ 'status': 'completed' }");
180 break;
181 case MIG_STATE_ERROR:
182 *ret_data = qobject_from_jsonf("{ 'status': 'failed' }");
183 break;
184 case MIG_STATE_CANCELLED:
185 *ret_data = qobject_from_jsonf("{ 'status': 'cancelled' }");
186 break;
190 /* shared migration helpers */
192 static void migrate_fd_monitor_suspend(MigrationState *s, Monitor *mon)
194 s->mon = mon;
195 if (monitor_suspend(mon) == 0) {
196 DPRINTF("suspending monitor\n");
197 } else {
198 monitor_printf(mon, "terminal does not allow synchronous "
199 "migration, continuing detached\n");
203 static int migrate_fd_cleanup(MigrationState *s)
205 int ret = 0;
207 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
209 if (s->file) {
210 DPRINTF("closing file\n");
211 if (qemu_fclose(s->file) != 0) {
212 ret = -1;
214 s->file = NULL;
215 } else {
216 if (s->mon) {
217 monitor_resume(s->mon);
221 if (s->fd != -1) {
222 close(s->fd);
223 s->fd = -1;
226 return ret;
229 void migrate_fd_error(MigrationState *s)
231 DPRINTF("setting error state\n");
232 s->state = MIG_STATE_ERROR;
233 notifier_list_notify(&migration_state_notifiers, s);
234 migrate_fd_cleanup(s);
237 static void migrate_fd_completed(MigrationState *s)
239 DPRINTF("setting completed state\n");
240 if (migrate_fd_cleanup(s) < 0) {
241 s->state = MIG_STATE_ERROR;
242 } else {
243 s->state = MIG_STATE_COMPLETED;
244 runstate_set(RUN_STATE_POSTMIGRATE);
246 notifier_list_notify(&migration_state_notifiers, s);
249 static void migrate_fd_put_notify(void *opaque)
251 MigrationState *s = opaque;
253 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
254 qemu_file_put_notify(s->file);
255 if (qemu_file_get_error(s->file)) {
256 migrate_fd_error(s);
260 static ssize_t migrate_fd_put_buffer(void *opaque, const void *data,
261 size_t size)
263 MigrationState *s = opaque;
264 ssize_t ret;
266 if (s->state != MIG_STATE_ACTIVE) {
267 return -EIO;
270 do {
271 ret = s->write(s, data, size);
272 } while (ret == -1 && ((s->get_error(s)) == EINTR));
274 if (ret == -1)
275 ret = -(s->get_error(s));
277 if (ret == -EAGAIN) {
278 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
281 return ret;
284 static void migrate_fd_put_ready(void *opaque)
286 MigrationState *s = opaque;
287 int ret;
289 if (s->state != MIG_STATE_ACTIVE) {
290 DPRINTF("put_ready returning because of non-active state\n");
291 return;
294 DPRINTF("iterate\n");
295 ret = qemu_savevm_state_iterate(s->mon, s->file);
296 if (ret < 0) {
297 migrate_fd_error(s);
298 } else if (ret == 1) {
299 int old_vm_running = runstate_is_running();
301 DPRINTF("done iterating\n");
302 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
304 if (qemu_savevm_state_complete(s->mon, s->file) < 0) {
305 migrate_fd_error(s);
306 } else {
307 migrate_fd_completed(s);
309 if (s->state != MIG_STATE_COMPLETED) {
310 if (old_vm_running) {
311 vm_start();
317 static void migrate_fd_cancel(MigrationState *s)
319 if (s->state != MIG_STATE_ACTIVE)
320 return;
322 DPRINTF("cancelling migration\n");
324 s->state = MIG_STATE_CANCELLED;
325 notifier_list_notify(&migration_state_notifiers, s);
326 qemu_savevm_state_cancel(s->mon, s->file);
328 migrate_fd_cleanup(s);
331 static void migrate_fd_wait_for_unfreeze(void *opaque)
333 MigrationState *s = opaque;
334 int ret;
336 DPRINTF("wait for unfreeze\n");
337 if (s->state != MIG_STATE_ACTIVE)
338 return;
340 do {
341 fd_set wfds;
343 FD_ZERO(&wfds);
344 FD_SET(s->fd, &wfds);
346 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
347 } while (ret == -1 && (s->get_error(s)) == EINTR);
349 if (ret == -1) {
350 qemu_file_set_error(s->file, -s->get_error(s));
354 static int migrate_fd_close(void *opaque)
356 MigrationState *s = opaque;
358 if (s->mon) {
359 monitor_resume(s->mon);
361 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
362 return s->close(s);
365 void add_migration_state_change_notifier(Notifier *notify)
367 notifier_list_add(&migration_state_notifiers, notify);
370 void remove_migration_state_change_notifier(Notifier *notify)
372 notifier_list_remove(&migration_state_notifiers, notify);
375 bool migration_has_finished(MigrationState *s)
377 return s->state == MIG_STATE_COMPLETED;
380 void migrate_fd_connect(MigrationState *s)
382 int ret;
384 s->state = MIG_STATE_ACTIVE;
385 s->file = qemu_fopen_ops_buffered(s,
386 s->bandwidth_limit,
387 migrate_fd_put_buffer,
388 migrate_fd_put_ready,
389 migrate_fd_wait_for_unfreeze,
390 migrate_fd_close);
392 DPRINTF("beginning savevm\n");
393 ret = qemu_savevm_state_begin(s->mon, s->file, s->blk, s->shared);
394 if (ret < 0) {
395 DPRINTF("failed, %d\n", ret);
396 migrate_fd_error(s);
397 return;
399 migrate_fd_put_ready(s);
402 static MigrationState *migrate_init(Monitor *mon, int detach, int blk, int inc)
404 MigrationState *s = migrate_get_current();
405 int64_t bandwidth_limit = s->bandwidth_limit;
407 memset(s, 0, sizeof(*s));
408 s->bandwidth_limit = bandwidth_limit;
409 s->blk = blk;
410 s->shared = inc;
411 s->mon = NULL;
412 s->bandwidth_limit = bandwidth_limit;
413 s->state = MIG_STATE_SETUP;
415 if (!detach) {
416 migrate_fd_monitor_suspend(s, mon);
419 return s;
422 int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
424 MigrationState *s = migrate_get_current();
425 const char *p;
426 int detach = qdict_get_try_bool(qdict, "detach", 0);
427 int blk = qdict_get_try_bool(qdict, "blk", 0);
428 int inc = qdict_get_try_bool(qdict, "inc", 0);
429 const char *uri = qdict_get_str(qdict, "uri");
430 int ret;
432 if (s->state == MIG_STATE_ACTIVE) {
433 monitor_printf(mon, "migration already in progress\n");
434 return -1;
437 if (qemu_savevm_state_blocked(mon)) {
438 return -1;
441 s = migrate_init(mon, detach, blk, inc);
443 if (strstart(uri, "tcp:", &p)) {
444 ret = tcp_start_outgoing_migration(s, p);
445 #if !defined(WIN32)
446 } else if (strstart(uri, "exec:", &p)) {
447 ret = exec_start_outgoing_migration(s, p);
448 } else if (strstart(uri, "unix:", &p)) {
449 ret = unix_start_outgoing_migration(s, p);
450 } else if (strstart(uri, "fd:", &p)) {
451 ret = fd_start_outgoing_migration(s, p);
452 #endif
453 } else {
454 monitor_printf(mon, "unknown migration protocol: %s\n", uri);
455 ret = -EINVAL;
458 if (ret < 0) {
459 monitor_printf(mon, "migration failed: %s\n", strerror(-ret));
460 return ret;
463 notifier_list_notify(&migration_state_notifiers, s);
464 return 0;
467 int do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data)
469 migrate_fd_cancel(migrate_get_current());
470 return 0;
473 int do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data)
475 int64_t d;
476 MigrationState *s;
478 d = qdict_get_int(qdict, "value");
479 if (d < 0) {
480 d = 0;
483 s = migrate_get_current();
484 s->bandwidth_limit = d;
485 qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
487 return 0;
490 int do_migrate_set_downtime(Monitor *mon, const QDict *qdict,
491 QObject **ret_data)
493 double d;
495 d = qdict_get_double(qdict, "value") * 1e9;
496 d = MAX(0, MIN(UINT64_MAX, d));
497 max_downtime = (uint64_t)d;
499 return 0;