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 /* 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
)
47 if (strstart(uri
, "tcp:", &p
))
48 ret
= tcp_start_incoming_migration(p
);
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
);
58 fprintf(stderr
, "unknown migration protocol: %s\n", uri
);
59 ret
= -EPROTONOSUPPORT
;
64 void process_incoming_migration(QEMUFile
*f
)
66 if (qemu_loadvm_state(f
) < 0) {
67 fprintf(stderr
, "load of migration failed\n");
71 DPRINTF("successfully loaded vm state\n");
76 runstate_set(RUN_STATE_PRELAUNCH
);
80 static MigrationState
*migrate_new(Monitor
*mon
, int64_t bandwidth_limit
,
81 int detach
, int blk
, int inc
);
83 int do_migrate(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
85 MigrationState
*s
= NULL
;
87 int detach
= qdict_get_try_bool(qdict
, "detach", 0);
88 int blk
= qdict_get_try_bool(qdict
, "blk", 0);
89 int inc
= qdict_get_try_bool(qdict
, "inc", 0);
90 const char *uri
= qdict_get_str(qdict
, "uri");
93 if (current_migration
&&
94 current_migration
->state
== MIG_STATE_ACTIVE
) {
95 monitor_printf(mon
, "migration already in progress\n");
99 if (qemu_savevm_state_blocked(mon
)) {
103 s
= migrate_new(mon
, max_throttle
, detach
, blk
, inc
);
105 if (strstart(uri
, "tcp:", &p
)) {
106 ret
= tcp_start_outgoing_migration(s
, p
);
108 } else if (strstart(uri
, "exec:", &p
)) {
109 ret
= exec_start_outgoing_migration(s
, p
);
110 } else if (strstart(uri
, "unix:", &p
)) {
111 ret
= unix_start_outgoing_migration(s
, p
);
112 } else if (strstart(uri
, "fd:", &p
)) {
113 ret
= fd_start_outgoing_migration(s
, p
);
116 monitor_printf(mon
, "unknown migration protocol: %s\n", uri
);
118 goto free_migrate_state
;
122 monitor_printf(mon
, "migration failed\n");
123 goto free_migrate_state
;
126 g_free(current_migration
);
127 current_migration
= s
;
128 notifier_list_notify(&migration_state_notifiers
, NULL
);
135 static void migrate_fd_cancel(MigrationState
*s
);
137 int do_migrate_cancel(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
139 if (current_migration
) {
140 migrate_fd_cancel(current_migration
);
145 int do_migrate_set_speed(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
150 d
= qdict_get_int(qdict
, "value");
156 s
= current_migration
;
158 qemu_file_set_rate_limit(s
->file
, max_throttle
);
164 /* amount of nanoseconds we are willing to wait for migration to be down.
165 * the choice of nanoseconds is because it is the maximum resolution that
166 * get_clock() can achieve. It is an internal measure. All user-visible
167 * units must be in seconds */
168 static uint64_t max_downtime
= 30000000;
170 uint64_t migrate_max_downtime(void)
175 int do_migrate_set_downtime(Monitor
*mon
, const QDict
*qdict
,
180 d
= qdict_get_double(qdict
, "value") * 1e9
;
181 d
= MAX(0, MIN(UINT64_MAX
, d
));
182 max_downtime
= (uint64_t)d
;
187 static void migrate_print_status(Monitor
*mon
, const char *name
,
188 const QDict
*status_dict
)
192 qdict
= qobject_to_qdict(qdict_get(status_dict
, name
));
194 monitor_printf(mon
, "transferred %s: %" PRIu64
" kbytes\n", name
,
195 qdict_get_int(qdict
, "transferred") >> 10);
196 monitor_printf(mon
, "remaining %s: %" PRIu64
" kbytes\n", name
,
197 qdict_get_int(qdict
, "remaining") >> 10);
198 monitor_printf(mon
, "total %s: %" PRIu64
" kbytes\n", name
,
199 qdict_get_int(qdict
, "total") >> 10);
202 void do_info_migrate_print(Monitor
*mon
, const QObject
*data
)
206 qdict
= qobject_to_qdict(data
);
208 monitor_printf(mon
, "Migration status: %s\n",
209 qdict_get_str(qdict
, "status"));
211 if (qdict_haskey(qdict
, "ram")) {
212 migrate_print_status(mon
, "ram", qdict
);
215 if (qdict_haskey(qdict
, "disk")) {
216 migrate_print_status(mon
, "disk", qdict
);
220 static void migrate_put_status(QDict
*qdict
, const char *name
,
221 uint64_t trans
, uint64_t rem
, uint64_t total
)
225 obj
= qobject_from_jsonf("{ 'transferred': %" PRId64
", "
226 "'remaining': %" PRId64
", "
227 "'total': %" PRId64
" }", trans
, rem
, total
);
228 qdict_put_obj(qdict
, name
, obj
);
231 void do_info_migrate(Monitor
*mon
, QObject
**ret_data
)
235 if (current_migration
) {
236 MigrationState
*s
= current_migration
;
239 case MIG_STATE_SETUP
:
240 /* no migration has happened ever */
242 case MIG_STATE_ACTIVE
:
244 qdict_put(qdict
, "status", qstring_from_str("active"));
246 migrate_put_status(qdict
, "ram", ram_bytes_transferred(),
247 ram_bytes_remaining(), ram_bytes_total());
249 if (blk_mig_active()) {
250 migrate_put_status(qdict
, "disk", blk_mig_bytes_transferred(),
251 blk_mig_bytes_remaining(),
252 blk_mig_bytes_total());
255 *ret_data
= QOBJECT(qdict
);
257 case MIG_STATE_COMPLETED
:
258 *ret_data
= qobject_from_jsonf("{ 'status': 'completed' }");
260 case MIG_STATE_ERROR
:
261 *ret_data
= qobject_from_jsonf("{ 'status': 'failed' }");
263 case MIG_STATE_CANCELLED
:
264 *ret_data
= qobject_from_jsonf("{ 'status': 'cancelled' }");
270 /* shared migration helpers */
272 static void migrate_fd_monitor_suspend(MigrationState
*s
, Monitor
*mon
)
275 if (monitor_suspend(mon
) == 0) {
276 DPRINTF("suspending monitor\n");
278 monitor_printf(mon
, "terminal does not allow synchronous "
279 "migration, continuing detached\n");
283 static int migrate_fd_cleanup(MigrationState
*s
)
287 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
290 DPRINTF("closing file\n");
291 if (qemu_fclose(s
->file
) != 0) {
297 monitor_resume(s
->mon
);
309 void migrate_fd_error(MigrationState
*s
)
311 DPRINTF("setting error state\n");
312 s
->state
= MIG_STATE_ERROR
;
313 notifier_list_notify(&migration_state_notifiers
, NULL
);
314 migrate_fd_cleanup(s
);
317 static void migrate_fd_completed(MigrationState
*s
)
319 DPRINTF("setting completed state\n");
320 if (migrate_fd_cleanup(s
) < 0) {
321 s
->state
= MIG_STATE_ERROR
;
323 s
->state
= MIG_STATE_COMPLETED
;
324 runstate_set(RUN_STATE_POSTMIGRATE
);
326 notifier_list_notify(&migration_state_notifiers
, NULL
);
329 static void migrate_fd_put_notify(void *opaque
)
331 MigrationState
*s
= opaque
;
333 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
334 qemu_file_put_notify(s
->file
);
335 if (qemu_file_get_error(s
->file
)) {
340 static ssize_t
migrate_fd_put_buffer(void *opaque
, const void *data
,
343 MigrationState
*s
= opaque
;
346 if (s
->state
!= MIG_STATE_ACTIVE
) {
351 ret
= s
->write(s
, data
, size
);
352 } while (ret
== -1 && ((s
->get_error(s
)) == EINTR
));
355 ret
= -(s
->get_error(s
));
357 if (ret
== -EAGAIN
) {
358 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, migrate_fd_put_notify
, s
);
364 static void migrate_fd_put_ready(void *opaque
)
366 MigrationState
*s
= opaque
;
369 if (s
->state
!= MIG_STATE_ACTIVE
) {
370 DPRINTF("put_ready returning because of non-active state\n");
374 DPRINTF("iterate\n");
375 ret
= qemu_savevm_state_iterate(s
->mon
, s
->file
);
378 } else if (ret
== 1) {
379 int old_vm_running
= runstate_is_running();
381 DPRINTF("done iterating\n");
382 vm_stop(RUN_STATE_FINISH_MIGRATE
);
384 if (qemu_savevm_state_complete(s
->mon
, s
->file
) < 0) {
387 migrate_fd_completed(s
);
389 if (s
->state
!= MIG_STATE_COMPLETED
) {
390 if (old_vm_running
) {
397 static void migrate_fd_cancel(MigrationState
*s
)
399 if (s
->state
!= MIG_STATE_ACTIVE
)
402 DPRINTF("cancelling migration\n");
404 s
->state
= MIG_STATE_CANCELLED
;
405 notifier_list_notify(&migration_state_notifiers
, NULL
);
406 qemu_savevm_state_cancel(s
->mon
, s
->file
);
408 migrate_fd_cleanup(s
);
411 static void migrate_fd_wait_for_unfreeze(void *opaque
)
413 MigrationState
*s
= opaque
;
416 DPRINTF("wait for unfreeze\n");
417 if (s
->state
!= MIG_STATE_ACTIVE
)
424 FD_SET(s
->fd
, &wfds
);
426 ret
= select(s
->fd
+ 1, NULL
, &wfds
, NULL
, NULL
);
427 } while (ret
== -1 && (s
->get_error(s
)) == EINTR
);
430 qemu_file_set_error(s
->file
, -s
->get_error(s
));
434 static int migrate_fd_close(void *opaque
)
436 MigrationState
*s
= opaque
;
439 monitor_resume(s
->mon
);
441 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
445 void add_migration_state_change_notifier(Notifier
*notify
)
447 notifier_list_add(&migration_state_notifiers
, notify
);
450 void remove_migration_state_change_notifier(Notifier
*notify
)
452 notifier_list_remove(&migration_state_notifiers
, notify
);
455 int get_migration_state(void)
457 if (current_migration
) {
458 return current_migration
->state
;
460 return MIG_STATE_ERROR
;
464 void migrate_fd_connect(MigrationState
*s
)
468 s
->state
= MIG_STATE_ACTIVE
;
469 s
->file
= qemu_fopen_ops_buffered(s
,
471 migrate_fd_put_buffer
,
472 migrate_fd_put_ready
,
473 migrate_fd_wait_for_unfreeze
,
476 DPRINTF("beginning savevm\n");
477 ret
= qemu_savevm_state_begin(s
->mon
, s
->file
, s
->blk
, s
->shared
);
479 DPRINTF("failed, %d\n", ret
);
483 migrate_fd_put_ready(s
);
486 static MigrationState
*migrate_new(Monitor
*mon
, int64_t bandwidth_limit
,
487 int detach
, int blk
, int inc
)
489 MigrationState
*s
= g_malloc0(sizeof(*s
));
494 s
->bandwidth_limit
= bandwidth_limit
;
495 s
->state
= MIG_STATE_SETUP
;
498 migrate_fd_monitor_suspend(s
, mon
);