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 int do_migrate(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
82 MigrationState
*s
= NULL
;
84 int detach
= qdict_get_try_bool(qdict
, "detach", 0);
85 int blk
= qdict_get_try_bool(qdict
, "blk", 0);
86 int inc
= qdict_get_try_bool(qdict
, "inc", 0);
87 const char *uri
= qdict_get_str(qdict
, "uri");
89 if (current_migration
&&
90 current_migration
->get_status(current_migration
) == MIG_STATE_ACTIVE
) {
91 monitor_printf(mon
, "migration already in progress\n");
95 if (qemu_savevm_state_blocked(mon
)) {
99 if (strstart(uri
, "tcp:", &p
)) {
100 s
= tcp_start_outgoing_migration(mon
, p
, max_throttle
, detach
,
103 } else if (strstart(uri
, "exec:", &p
)) {
104 s
= exec_start_outgoing_migration(mon
, p
, max_throttle
, detach
,
106 } else if (strstart(uri
, "unix:", &p
)) {
107 s
= unix_start_outgoing_migration(mon
, p
, max_throttle
, detach
,
109 } else if (strstart(uri
, "fd:", &p
)) {
110 s
= fd_start_outgoing_migration(mon
, p
, max_throttle
, detach
,
114 monitor_printf(mon
, "unknown migration protocol: %s\n", uri
);
119 monitor_printf(mon
, "migration failed\n");
123 if (current_migration
) {
124 current_migration
->release(current_migration
);
127 current_migration
= s
;
128 notifier_list_notify(&migration_state_notifiers
, NULL
);
132 int do_migrate_cancel(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
134 MigrationState
*s
= current_migration
;
136 if (s
&& s
->get_status(s
) == MIG_STATE_ACTIVE
) {
142 int do_migrate_set_speed(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
147 d
= qdict_get_int(qdict
, "value");
153 s
= current_migration
;
155 qemu_file_set_rate_limit(s
->file
, max_throttle
);
161 /* amount of nanoseconds we are willing to wait for migration to be down.
162 * the choice of nanoseconds is because it is the maximum resolution that
163 * get_clock() can achieve. It is an internal measure. All user-visible
164 * units must be in seconds */
165 static uint64_t max_downtime
= 30000000;
167 uint64_t migrate_max_downtime(void)
172 int do_migrate_set_downtime(Monitor
*mon
, const QDict
*qdict
,
177 d
= qdict_get_double(qdict
, "value") * 1e9
;
178 d
= MAX(0, MIN(UINT64_MAX
, d
));
179 max_downtime
= (uint64_t)d
;
184 static void migrate_print_status(Monitor
*mon
, const char *name
,
185 const QDict
*status_dict
)
189 qdict
= qobject_to_qdict(qdict_get(status_dict
, name
));
191 monitor_printf(mon
, "transferred %s: %" PRIu64
" kbytes\n", name
,
192 qdict_get_int(qdict
, "transferred") >> 10);
193 monitor_printf(mon
, "remaining %s: %" PRIu64
" kbytes\n", name
,
194 qdict_get_int(qdict
, "remaining") >> 10);
195 monitor_printf(mon
, "total %s: %" PRIu64
" kbytes\n", name
,
196 qdict_get_int(qdict
, "total") >> 10);
199 void do_info_migrate_print(Monitor
*mon
, const QObject
*data
)
203 qdict
= qobject_to_qdict(data
);
205 monitor_printf(mon
, "Migration status: %s\n",
206 qdict_get_str(qdict
, "status"));
208 if (qdict_haskey(qdict
, "ram")) {
209 migrate_print_status(mon
, "ram", qdict
);
212 if (qdict_haskey(qdict
, "disk")) {
213 migrate_print_status(mon
, "disk", qdict
);
217 static void migrate_put_status(QDict
*qdict
, const char *name
,
218 uint64_t trans
, uint64_t rem
, uint64_t total
)
222 obj
= qobject_from_jsonf("{ 'transferred': %" PRId64
", "
223 "'remaining': %" PRId64
", "
224 "'total': %" PRId64
" }", trans
, rem
, total
);
225 qdict_put_obj(qdict
, name
, obj
);
228 void do_info_migrate(Monitor
*mon
, QObject
**ret_data
)
232 if (current_migration
) {
233 MigrationState
*s
= current_migration
;
235 switch (s
->get_status(current_migration
)) {
236 case MIG_STATE_ACTIVE
:
238 qdict_put(qdict
, "status", qstring_from_str("active"));
240 migrate_put_status(qdict
, "ram", ram_bytes_transferred(),
241 ram_bytes_remaining(), ram_bytes_total());
243 if (blk_mig_active()) {
244 migrate_put_status(qdict
, "disk", blk_mig_bytes_transferred(),
245 blk_mig_bytes_remaining(),
246 blk_mig_bytes_total());
249 *ret_data
= QOBJECT(qdict
);
251 case MIG_STATE_COMPLETED
:
252 *ret_data
= qobject_from_jsonf("{ 'status': 'completed' }");
254 case MIG_STATE_ERROR
:
255 *ret_data
= qobject_from_jsonf("{ 'status': 'failed' }");
257 case MIG_STATE_CANCELLED
:
258 *ret_data
= qobject_from_jsonf("{ 'status': 'cancelled' }");
264 /* shared migration helpers */
266 void migrate_fd_monitor_suspend(MigrationState
*s
, Monitor
*mon
)
269 if (monitor_suspend(mon
) == 0) {
270 DPRINTF("suspending monitor\n");
272 monitor_printf(mon
, "terminal does not allow synchronous "
273 "migration, continuing detached\n");
277 void migrate_fd_error(MigrationState
*s
)
279 DPRINTF("setting error state\n");
280 s
->state
= MIG_STATE_ERROR
;
281 notifier_list_notify(&migration_state_notifiers
, NULL
);
282 migrate_fd_cleanup(s
);
285 int migrate_fd_cleanup(MigrationState
*s
)
289 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
292 DPRINTF("closing file\n");
293 if (qemu_fclose(s
->file
) != 0) {
299 monitor_resume(s
->mon
);
311 void migrate_fd_put_notify(void *opaque
)
313 MigrationState
*s
= opaque
;
315 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
316 qemu_file_put_notify(s
->file
);
317 if (qemu_file_get_error(s
->file
)) {
322 ssize_t
migrate_fd_put_buffer(void *opaque
, const void *data
, size_t size
)
324 MigrationState
*s
= opaque
;
327 if (s
->state
!= MIG_STATE_ACTIVE
) {
332 ret
= s
->write(s
, data
, size
);
333 } while (ret
== -1 && ((s
->get_error(s
)) == EINTR
));
336 ret
= -(s
->get_error(s
));
338 if (ret
== -EAGAIN
) {
339 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, migrate_fd_put_notify
, s
);
345 void migrate_fd_connect(MigrationState
*s
)
349 s
->file
= qemu_fopen_ops_buffered(s
,
351 migrate_fd_put_buffer
,
352 migrate_fd_put_ready
,
353 migrate_fd_wait_for_unfreeze
,
356 DPRINTF("beginning savevm\n");
357 ret
= qemu_savevm_state_begin(s
->mon
, s
->file
, s
->blk
, s
->shared
);
359 DPRINTF("failed, %d\n", ret
);
364 migrate_fd_put_ready(s
);
367 void migrate_fd_put_ready(void *opaque
)
369 MigrationState
*s
= opaque
;
372 if (s
->state
!= MIG_STATE_ACTIVE
) {
373 DPRINTF("put_ready returning because of non-active state\n");
377 DPRINTF("iterate\n");
378 ret
= qemu_savevm_state_iterate(s
->mon
, s
->file
);
381 } else if (ret
== 1) {
382 int old_vm_running
= runstate_is_running();
384 DPRINTF("done iterating\n");
385 vm_stop(RUN_STATE_FINISH_MIGRATE
);
387 if ((qemu_savevm_state_complete(s
->mon
, s
->file
)) < 0) {
388 if (old_vm_running
) {
391 s
->state
= MIG_STATE_ERROR
;
393 if (migrate_fd_cleanup(s
) < 0) {
394 if (old_vm_running
) {
397 s
->state
= MIG_STATE_ERROR
;
399 if (s
->state
== MIG_STATE_ACTIVE
) {
400 s
->state
= MIG_STATE_COMPLETED
;
401 runstate_set(RUN_STATE_POSTMIGRATE
);
403 notifier_list_notify(&migration_state_notifiers
, NULL
);
407 int migrate_fd_get_status(MigrationState
*s
)
412 void migrate_fd_cancel(MigrationState
*s
)
414 if (s
->state
!= MIG_STATE_ACTIVE
)
417 DPRINTF("cancelling migration\n");
419 s
->state
= MIG_STATE_CANCELLED
;
420 notifier_list_notify(&migration_state_notifiers
, NULL
);
421 qemu_savevm_state_cancel(s
->mon
, s
->file
);
423 migrate_fd_cleanup(s
);
426 void migrate_fd_release(MigrationState
*s
)
429 DPRINTF("releasing state\n");
431 if (s
->state
== MIG_STATE_ACTIVE
) {
432 s
->state
= MIG_STATE_CANCELLED
;
433 notifier_list_notify(&migration_state_notifiers
, NULL
);
434 migrate_fd_cleanup(s
);
439 void migrate_fd_wait_for_unfreeze(void *opaque
)
441 MigrationState
*s
= opaque
;
444 DPRINTF("wait for unfreeze\n");
445 if (s
->state
!= MIG_STATE_ACTIVE
)
452 FD_SET(s
->fd
, &wfds
);
454 ret
= select(s
->fd
+ 1, NULL
, &wfds
, NULL
, NULL
);
455 } while (ret
== -1 && (s
->get_error(s
)) == EINTR
);
458 qemu_file_set_error(s
->file
, -s
->get_error(s
));
462 int migrate_fd_close(void *opaque
)
464 MigrationState
*s
= opaque
;
467 monitor_resume(s
->mon
);
469 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
473 void add_migration_state_change_notifier(Notifier
*notify
)
475 notifier_list_add(&migration_state_notifiers
, notify
);
478 void remove_migration_state_change_notifier(Notifier
*notify
)
480 notifier_list_remove(&migration_state_notifiers
, notify
);
483 int get_migration_state(void)
485 if (current_migration
) {
486 return migrate_fd_get_status(current_migration
);
488 return MIG_STATE_ERROR
;