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 /* amount of nanoseconds we are willing to wait for migration to be down.
81 * the choice of nanoseconds is because it is the maximum resolution that
82 * get_clock() can achieve. It is an internal measure. All user-visible
83 * units must be in seconds */
84 static uint64_t max_downtime
= 30000000;
86 uint64_t migrate_max_downtime(void)
91 static void migrate_print_status(Monitor
*mon
, const char *name
,
92 const QDict
*status_dict
)
96 qdict
= qobject_to_qdict(qdict_get(status_dict
, name
));
98 monitor_printf(mon
, "transferred %s: %" PRIu64
" kbytes\n", name
,
99 qdict_get_int(qdict
, "transferred") >> 10);
100 monitor_printf(mon
, "remaining %s: %" PRIu64
" kbytes\n", name
,
101 qdict_get_int(qdict
, "remaining") >> 10);
102 monitor_printf(mon
, "total %s: %" PRIu64
" kbytes\n", name
,
103 qdict_get_int(qdict
, "total") >> 10);
106 void do_info_migrate_print(Monitor
*mon
, const QObject
*data
)
110 qdict
= qobject_to_qdict(data
);
112 monitor_printf(mon
, "Migration status: %s\n",
113 qdict_get_str(qdict
, "status"));
115 if (qdict_haskey(qdict
, "ram")) {
116 migrate_print_status(mon
, "ram", qdict
);
119 if (qdict_haskey(qdict
, "disk")) {
120 migrate_print_status(mon
, "disk", qdict
);
124 static void migrate_put_status(QDict
*qdict
, const char *name
,
125 uint64_t trans
, uint64_t rem
, uint64_t total
)
129 obj
= qobject_from_jsonf("{ 'transferred': %" PRId64
", "
130 "'remaining': %" PRId64
", "
131 "'total': %" PRId64
" }", trans
, rem
, total
);
132 qdict_put_obj(qdict
, name
, obj
);
135 void do_info_migrate(Monitor
*mon
, QObject
**ret_data
)
139 if (current_migration
) {
140 MigrationState
*s
= current_migration
;
143 case MIG_STATE_SETUP
:
144 /* no migration has happened ever */
146 case MIG_STATE_ACTIVE
:
148 qdict_put(qdict
, "status", qstring_from_str("active"));
150 migrate_put_status(qdict
, "ram", ram_bytes_transferred(),
151 ram_bytes_remaining(), ram_bytes_total());
153 if (blk_mig_active()) {
154 migrate_put_status(qdict
, "disk", blk_mig_bytes_transferred(),
155 blk_mig_bytes_remaining(),
156 blk_mig_bytes_total());
159 *ret_data
= QOBJECT(qdict
);
161 case MIG_STATE_COMPLETED
:
162 *ret_data
= qobject_from_jsonf("{ 'status': 'completed' }");
164 case MIG_STATE_ERROR
:
165 *ret_data
= qobject_from_jsonf("{ 'status': 'failed' }");
167 case MIG_STATE_CANCELLED
:
168 *ret_data
= qobject_from_jsonf("{ 'status': 'cancelled' }");
174 /* shared migration helpers */
176 static void migrate_fd_monitor_suspend(MigrationState
*s
, Monitor
*mon
)
179 if (monitor_suspend(mon
) == 0) {
180 DPRINTF("suspending monitor\n");
182 monitor_printf(mon
, "terminal does not allow synchronous "
183 "migration, continuing detached\n");
187 static int migrate_fd_cleanup(MigrationState
*s
)
191 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
194 DPRINTF("closing file\n");
195 if (qemu_fclose(s
->file
) != 0) {
201 monitor_resume(s
->mon
);
213 void migrate_fd_error(MigrationState
*s
)
215 DPRINTF("setting error state\n");
216 s
->state
= MIG_STATE_ERROR
;
217 notifier_list_notify(&migration_state_notifiers
, NULL
);
218 migrate_fd_cleanup(s
);
221 static void migrate_fd_completed(MigrationState
*s
)
223 DPRINTF("setting completed state\n");
224 if (migrate_fd_cleanup(s
) < 0) {
225 s
->state
= MIG_STATE_ERROR
;
227 s
->state
= MIG_STATE_COMPLETED
;
228 runstate_set(RUN_STATE_POSTMIGRATE
);
230 notifier_list_notify(&migration_state_notifiers
, NULL
);
233 static void migrate_fd_put_notify(void *opaque
)
235 MigrationState
*s
= opaque
;
237 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
238 qemu_file_put_notify(s
->file
);
239 if (qemu_file_get_error(s
->file
)) {
244 static ssize_t
migrate_fd_put_buffer(void *opaque
, const void *data
,
247 MigrationState
*s
= opaque
;
250 if (s
->state
!= MIG_STATE_ACTIVE
) {
255 ret
= s
->write(s
, data
, size
);
256 } while (ret
== -1 && ((s
->get_error(s
)) == EINTR
));
259 ret
= -(s
->get_error(s
));
261 if (ret
== -EAGAIN
) {
262 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, migrate_fd_put_notify
, s
);
268 static void migrate_fd_put_ready(void *opaque
)
270 MigrationState
*s
= opaque
;
273 if (s
->state
!= MIG_STATE_ACTIVE
) {
274 DPRINTF("put_ready returning because of non-active state\n");
278 DPRINTF("iterate\n");
279 ret
= qemu_savevm_state_iterate(s
->mon
, s
->file
);
282 } else if (ret
== 1) {
283 int old_vm_running
= runstate_is_running();
285 DPRINTF("done iterating\n");
286 vm_stop(RUN_STATE_FINISH_MIGRATE
);
288 if (qemu_savevm_state_complete(s
->mon
, s
->file
) < 0) {
291 migrate_fd_completed(s
);
293 if (s
->state
!= MIG_STATE_COMPLETED
) {
294 if (old_vm_running
) {
301 static void migrate_fd_cancel(MigrationState
*s
)
303 if (s
->state
!= MIG_STATE_ACTIVE
)
306 DPRINTF("cancelling migration\n");
308 s
->state
= MIG_STATE_CANCELLED
;
309 notifier_list_notify(&migration_state_notifiers
, NULL
);
310 qemu_savevm_state_cancel(s
->mon
, s
->file
);
312 migrate_fd_cleanup(s
);
315 static void migrate_fd_wait_for_unfreeze(void *opaque
)
317 MigrationState
*s
= opaque
;
320 DPRINTF("wait for unfreeze\n");
321 if (s
->state
!= MIG_STATE_ACTIVE
)
328 FD_SET(s
->fd
, &wfds
);
330 ret
= select(s
->fd
+ 1, NULL
, &wfds
, NULL
, NULL
);
331 } while (ret
== -1 && (s
->get_error(s
)) == EINTR
);
334 qemu_file_set_error(s
->file
, -s
->get_error(s
));
338 static int migrate_fd_close(void *opaque
)
340 MigrationState
*s
= opaque
;
343 monitor_resume(s
->mon
);
345 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
349 void add_migration_state_change_notifier(Notifier
*notify
)
351 notifier_list_add(&migration_state_notifiers
, notify
);
354 void remove_migration_state_change_notifier(Notifier
*notify
)
356 notifier_list_remove(&migration_state_notifiers
, notify
);
359 int get_migration_state(void)
361 if (current_migration
) {
362 return current_migration
->state
;
364 return MIG_STATE_ERROR
;
368 void migrate_fd_connect(MigrationState
*s
)
372 s
->state
= MIG_STATE_ACTIVE
;
373 s
->file
= qemu_fopen_ops_buffered(s
,
375 migrate_fd_put_buffer
,
376 migrate_fd_put_ready
,
377 migrate_fd_wait_for_unfreeze
,
380 DPRINTF("beginning savevm\n");
381 ret
= qemu_savevm_state_begin(s
->mon
, s
->file
, s
->blk
, s
->shared
);
383 DPRINTF("failed, %d\n", ret
);
387 migrate_fd_put_ready(s
);
390 static MigrationState
*migrate_new(Monitor
*mon
, int64_t bandwidth_limit
,
391 int detach
, int blk
, int inc
)
393 MigrationState
*s
= g_malloc0(sizeof(*s
));
398 s
->bandwidth_limit
= bandwidth_limit
;
399 s
->state
= MIG_STATE_SETUP
;
402 migrate_fd_monitor_suspend(s
, mon
);
408 int do_migrate(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
410 MigrationState
*s
= NULL
;
412 int detach
= qdict_get_try_bool(qdict
, "detach", 0);
413 int blk
= qdict_get_try_bool(qdict
, "blk", 0);
414 int inc
= qdict_get_try_bool(qdict
, "inc", 0);
415 const char *uri
= qdict_get_str(qdict
, "uri");
418 if (current_migration
&&
419 current_migration
->state
== MIG_STATE_ACTIVE
) {
420 monitor_printf(mon
, "migration already in progress\n");
424 if (qemu_savevm_state_blocked(mon
)) {
428 s
= migrate_new(mon
, max_throttle
, detach
, blk
, inc
);
430 if (strstart(uri
, "tcp:", &p
)) {
431 ret
= tcp_start_outgoing_migration(s
, p
);
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
);
441 monitor_printf(mon
, "unknown migration protocol: %s\n", uri
);
443 goto free_migrate_state
;
447 monitor_printf(mon
, "migration failed\n");
448 goto free_migrate_state
;
451 g_free(current_migration
);
452 current_migration
= s
;
453 notifier_list_notify(&migration_state_notifiers
, NULL
);
460 int do_migrate_cancel(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
462 if (current_migration
) {
463 migrate_fd_cancel(current_migration
);
468 int do_migrate_set_speed(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
473 d
= qdict_get_int(qdict
, "value");
479 s
= current_migration
;
481 qemu_file_set_rate_limit(s
->file
, max_throttle
);
487 int do_migrate_set_downtime(Monitor
*mon
, const QDict
*qdict
,
492 d
= qdict_get_double(qdict
, "value") * 1e9
;
493 d
= MAX(0, MIN(UINT64_MAX
, d
));
494 max_downtime
= (uint64_t)d
;