4 * Copyright IBM, Corp. 2008
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"
17 #include "buffered_file.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)
30 #define DPRINTF(fmt, ...) \
34 #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
36 static NotifierList migration_state_notifiers
=
37 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers
);
39 /* When we add fault tolerance, we could have several
40 migrations at once. For now we don't need to add
41 dynamic creation of migration */
43 static MigrationState
*migrate_get_current(void)
45 static MigrationState current_migration
= {
46 .state
= MIG_STATE_SETUP
,
47 .bandwidth_limit
= MAX_THROTTLE
,
50 return ¤t_migration
;
53 int qemu_start_incoming_migration(const char *uri
)
58 if (strstart(uri
, "tcp:", &p
))
59 ret
= tcp_start_incoming_migration(p
);
61 else if (strstart(uri
, "exec:", &p
))
62 ret
= exec_start_incoming_migration(p
);
63 else if (strstart(uri
, "unix:", &p
))
64 ret
= unix_start_incoming_migration(p
);
65 else if (strstart(uri
, "fd:", &p
))
66 ret
= fd_start_incoming_migration(p
);
69 fprintf(stderr
, "unknown migration protocol: %s\n", uri
);
70 ret
= -EPROTONOSUPPORT
;
75 void process_incoming_migration(QEMUFile
*f
)
77 if (qemu_loadvm_state(f
) < 0) {
78 fprintf(stderr
, "load of migration failed\n");
82 DPRINTF("successfully loaded vm state\n");
87 runstate_set(RUN_STATE_PRELAUNCH
);
91 /* amount of nanoseconds we are willing to wait for migration to be down.
92 * the choice of nanoseconds is because it is the maximum resolution that
93 * get_clock() can achieve. It is an internal measure. All user-visible
94 * units must be in seconds */
95 static uint64_t max_downtime
= 30000000;
97 uint64_t migrate_max_downtime(void)
102 static void migrate_print_status(Monitor
*mon
, const char *name
,
103 const QDict
*status_dict
)
107 qdict
= qobject_to_qdict(qdict_get(status_dict
, name
));
109 monitor_printf(mon
, "transferred %s: %" PRIu64
" kbytes\n", name
,
110 qdict_get_int(qdict
, "transferred") >> 10);
111 monitor_printf(mon
, "remaining %s: %" PRIu64
" kbytes\n", name
,
112 qdict_get_int(qdict
, "remaining") >> 10);
113 monitor_printf(mon
, "total %s: %" PRIu64
" kbytes\n", name
,
114 qdict_get_int(qdict
, "total") >> 10);
117 void do_info_migrate_print(Monitor
*mon
, const QObject
*data
)
121 qdict
= qobject_to_qdict(data
);
123 monitor_printf(mon
, "Migration status: %s\n",
124 qdict_get_str(qdict
, "status"));
126 if (qdict_haskey(qdict
, "ram")) {
127 migrate_print_status(mon
, "ram", qdict
);
130 if (qdict_haskey(qdict
, "disk")) {
131 migrate_print_status(mon
, "disk", qdict
);
135 static void migrate_put_status(QDict
*qdict
, const char *name
,
136 uint64_t trans
, uint64_t rem
, uint64_t total
)
140 obj
= qobject_from_jsonf("{ 'transferred': %" PRId64
", "
141 "'remaining': %" PRId64
", "
142 "'total': %" PRId64
" }", trans
, rem
, total
);
143 qdict_put_obj(qdict
, name
, obj
);
146 void do_info_migrate(Monitor
*mon
, QObject
**ret_data
)
149 MigrationState
*s
= migrate_get_current();
152 case MIG_STATE_SETUP
:
153 /* no migration has happened ever */
155 case MIG_STATE_ACTIVE
:
157 qdict_put(qdict
, "status", qstring_from_str("active"));
159 migrate_put_status(qdict
, "ram", ram_bytes_transferred(),
160 ram_bytes_remaining(), ram_bytes_total());
162 if (blk_mig_active()) {
163 migrate_put_status(qdict
, "disk", blk_mig_bytes_transferred(),
164 blk_mig_bytes_remaining(),
165 blk_mig_bytes_total());
168 *ret_data
= QOBJECT(qdict
);
170 case MIG_STATE_COMPLETED
:
171 *ret_data
= qobject_from_jsonf("{ 'status': 'completed' }");
173 case MIG_STATE_ERROR
:
174 *ret_data
= qobject_from_jsonf("{ 'status': 'failed' }");
176 case MIG_STATE_CANCELLED
:
177 *ret_data
= qobject_from_jsonf("{ 'status': 'cancelled' }");
182 /* shared migration helpers */
184 static void migrate_fd_monitor_suspend(MigrationState
*s
, Monitor
*mon
)
187 if (monitor_suspend(mon
) == 0) {
188 DPRINTF("suspending monitor\n");
190 monitor_printf(mon
, "terminal does not allow synchronous "
191 "migration, continuing detached\n");
195 static int migrate_fd_cleanup(MigrationState
*s
)
199 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
202 DPRINTF("closing file\n");
203 if (qemu_fclose(s
->file
) != 0) {
209 monitor_resume(s
->mon
);
221 void migrate_fd_error(MigrationState
*s
)
223 DPRINTF("setting error state\n");
224 s
->state
= MIG_STATE_ERROR
;
225 notifier_list_notify(&migration_state_notifiers
, s
);
226 migrate_fd_cleanup(s
);
229 static void migrate_fd_completed(MigrationState
*s
)
231 DPRINTF("setting completed state\n");
232 if (migrate_fd_cleanup(s
) < 0) {
233 s
->state
= MIG_STATE_ERROR
;
235 s
->state
= MIG_STATE_COMPLETED
;
236 runstate_set(RUN_STATE_POSTMIGRATE
);
238 notifier_list_notify(&migration_state_notifiers
, s
);
241 static void migrate_fd_put_notify(void *opaque
)
243 MigrationState
*s
= opaque
;
245 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
246 qemu_file_put_notify(s
->file
);
247 if (qemu_file_get_error(s
->file
)) {
252 static ssize_t
migrate_fd_put_buffer(void *opaque
, const void *data
,
255 MigrationState
*s
= opaque
;
258 if (s
->state
!= MIG_STATE_ACTIVE
) {
263 ret
= s
->write(s
, data
, size
);
264 } while (ret
== -1 && ((s
->get_error(s
)) == EINTR
));
267 ret
= -(s
->get_error(s
));
269 if (ret
== -EAGAIN
) {
270 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, migrate_fd_put_notify
, s
);
276 static void migrate_fd_put_ready(void *opaque
)
278 MigrationState
*s
= opaque
;
281 if (s
->state
!= MIG_STATE_ACTIVE
) {
282 DPRINTF("put_ready returning because of non-active state\n");
286 DPRINTF("iterate\n");
287 ret
= qemu_savevm_state_iterate(s
->mon
, s
->file
);
290 } else if (ret
== 1) {
291 int old_vm_running
= runstate_is_running();
293 DPRINTF("done iterating\n");
294 vm_stop(RUN_STATE_FINISH_MIGRATE
);
296 if (qemu_savevm_state_complete(s
->mon
, s
->file
) < 0) {
299 migrate_fd_completed(s
);
301 if (s
->state
!= MIG_STATE_COMPLETED
) {
302 if (old_vm_running
) {
309 static void migrate_fd_cancel(MigrationState
*s
)
311 if (s
->state
!= MIG_STATE_ACTIVE
)
314 DPRINTF("cancelling migration\n");
316 s
->state
= MIG_STATE_CANCELLED
;
317 notifier_list_notify(&migration_state_notifiers
, s
);
318 qemu_savevm_state_cancel(s
->mon
, s
->file
);
320 migrate_fd_cleanup(s
);
323 static void migrate_fd_wait_for_unfreeze(void *opaque
)
325 MigrationState
*s
= opaque
;
328 DPRINTF("wait for unfreeze\n");
329 if (s
->state
!= MIG_STATE_ACTIVE
)
336 FD_SET(s
->fd
, &wfds
);
338 ret
= select(s
->fd
+ 1, NULL
, &wfds
, NULL
, NULL
);
339 } while (ret
== -1 && (s
->get_error(s
)) == EINTR
);
342 qemu_file_set_error(s
->file
, -s
->get_error(s
));
346 static int migrate_fd_close(void *opaque
)
348 MigrationState
*s
= opaque
;
351 monitor_resume(s
->mon
);
353 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
357 void add_migration_state_change_notifier(Notifier
*notify
)
359 notifier_list_add(&migration_state_notifiers
, notify
);
362 void remove_migration_state_change_notifier(Notifier
*notify
)
364 notifier_list_remove(&migration_state_notifiers
, notify
);
367 int get_migration_state(void)
369 return migrate_get_current()->state
;
372 void migrate_fd_connect(MigrationState
*s
)
376 s
->state
= MIG_STATE_ACTIVE
;
377 s
->file
= qemu_fopen_ops_buffered(s
,
379 migrate_fd_put_buffer
,
380 migrate_fd_put_ready
,
381 migrate_fd_wait_for_unfreeze
,
384 DPRINTF("beginning savevm\n");
385 ret
= qemu_savevm_state_begin(s
->mon
, s
->file
, s
->blk
, s
->shared
);
387 DPRINTF("failed, %d\n", ret
);
391 migrate_fd_put_ready(s
);
394 static MigrationState
*migrate_init(Monitor
*mon
, int detach
, int blk
, int inc
)
396 MigrationState
*s
= migrate_get_current();
397 int64_t bandwidth_limit
= s
->bandwidth_limit
;
399 memset(s
, 0, sizeof(*s
));
400 s
->bandwidth_limit
= bandwidth_limit
;
404 s
->bandwidth_limit
= bandwidth_limit
;
405 s
->state
= MIG_STATE_SETUP
;
408 migrate_fd_monitor_suspend(s
, mon
);
414 int do_migrate(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
416 MigrationState
*s
= migrate_get_current();
418 int detach
= qdict_get_try_bool(qdict
, "detach", 0);
419 int blk
= qdict_get_try_bool(qdict
, "blk", 0);
420 int inc
= qdict_get_try_bool(qdict
, "inc", 0);
421 const char *uri
= qdict_get_str(qdict
, "uri");
424 if (s
->state
== MIG_STATE_ACTIVE
) {
425 monitor_printf(mon
, "migration already in progress\n");
429 if (qemu_savevm_state_blocked(mon
)) {
433 s
= migrate_init(mon
, detach
, blk
, inc
);
435 if (strstart(uri
, "tcp:", &p
)) {
436 ret
= tcp_start_outgoing_migration(s
, p
);
438 } else if (strstart(uri
, "exec:", &p
)) {
439 ret
= exec_start_outgoing_migration(s
, p
);
440 } else if (strstart(uri
, "unix:", &p
)) {
441 ret
= unix_start_outgoing_migration(s
, p
);
442 } else if (strstart(uri
, "fd:", &p
)) {
443 ret
= fd_start_outgoing_migration(s
, p
);
446 monitor_printf(mon
, "unknown migration protocol: %s\n", uri
);
451 monitor_printf(mon
, "migration failed: %s\n", strerror(-ret
));
455 notifier_list_notify(&migration_state_notifiers
, s
);
459 int do_migrate_cancel(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
461 migrate_fd_cancel(migrate_get_current());
465 int do_migrate_set_speed(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
470 d
= qdict_get_int(qdict
, "value");
475 s
= migrate_get_current();
476 s
->bandwidth_limit
= d
;
477 qemu_file_set_rate_limit(s
->file
, s
->bandwidth_limit
);
482 int do_migrate_set_downtime(Monitor
*mon
, const QDict
*qdict
,
487 d
= qdict_get_double(qdict
, "value") * 1e9
;
488 d
= MAX(0, MIN(UINT64_MAX
, d
));
489 max_downtime
= (uint64_t)d
;