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
->get_status(current_migration
) == 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 if (current_migration
) {
127 current_migration
->release(current_migration
);
130 current_migration
= s
;
131 notifier_list_notify(&migration_state_notifiers
, NULL
);
138 int do_migrate_cancel(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
140 MigrationState
*s
= current_migration
;
142 if (s
&& s
->get_status(s
) == MIG_STATE_ACTIVE
) {
148 int do_migrate_set_speed(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
153 d
= qdict_get_int(qdict
, "value");
159 s
= current_migration
;
161 qemu_file_set_rate_limit(s
->file
, max_throttle
);
167 /* amount of nanoseconds we are willing to wait for migration to be down.
168 * the choice of nanoseconds is because it is the maximum resolution that
169 * get_clock() can achieve. It is an internal measure. All user-visible
170 * units must be in seconds */
171 static uint64_t max_downtime
= 30000000;
173 uint64_t migrate_max_downtime(void)
178 int do_migrate_set_downtime(Monitor
*mon
, const QDict
*qdict
,
183 d
= qdict_get_double(qdict
, "value") * 1e9
;
184 d
= MAX(0, MIN(UINT64_MAX
, d
));
185 max_downtime
= (uint64_t)d
;
190 static void migrate_print_status(Monitor
*mon
, const char *name
,
191 const QDict
*status_dict
)
195 qdict
= qobject_to_qdict(qdict_get(status_dict
, name
));
197 monitor_printf(mon
, "transferred %s: %" PRIu64
" kbytes\n", name
,
198 qdict_get_int(qdict
, "transferred") >> 10);
199 monitor_printf(mon
, "remaining %s: %" PRIu64
" kbytes\n", name
,
200 qdict_get_int(qdict
, "remaining") >> 10);
201 monitor_printf(mon
, "total %s: %" PRIu64
" kbytes\n", name
,
202 qdict_get_int(qdict
, "total") >> 10);
205 void do_info_migrate_print(Monitor
*mon
, const QObject
*data
)
209 qdict
= qobject_to_qdict(data
);
211 monitor_printf(mon
, "Migration status: %s\n",
212 qdict_get_str(qdict
, "status"));
214 if (qdict_haskey(qdict
, "ram")) {
215 migrate_print_status(mon
, "ram", qdict
);
218 if (qdict_haskey(qdict
, "disk")) {
219 migrate_print_status(mon
, "disk", qdict
);
223 static void migrate_put_status(QDict
*qdict
, const char *name
,
224 uint64_t trans
, uint64_t rem
, uint64_t total
)
228 obj
= qobject_from_jsonf("{ 'transferred': %" PRId64
", "
229 "'remaining': %" PRId64
", "
230 "'total': %" PRId64
" }", trans
, rem
, total
);
231 qdict_put_obj(qdict
, name
, obj
);
234 void do_info_migrate(Monitor
*mon
, QObject
**ret_data
)
238 if (current_migration
) {
239 MigrationState
*s
= current_migration
;
241 switch (s
->get_status(current_migration
)) {
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_put_notify(void *opaque
)
319 MigrationState
*s
= opaque
;
321 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
322 qemu_file_put_notify(s
->file
);
323 if (qemu_file_get_error(s
->file
)) {
328 static ssize_t
migrate_fd_put_buffer(void *opaque
, const void *data
,
331 MigrationState
*s
= opaque
;
334 if (s
->state
!= MIG_STATE_ACTIVE
) {
339 ret
= s
->write(s
, data
, size
);
340 } while (ret
== -1 && ((s
->get_error(s
)) == EINTR
));
343 ret
= -(s
->get_error(s
));
345 if (ret
== -EAGAIN
) {
346 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, migrate_fd_put_notify
, s
);
352 static void migrate_fd_put_ready(void *opaque
)
354 MigrationState
*s
= opaque
;
357 if (s
->state
!= MIG_STATE_ACTIVE
) {
358 DPRINTF("put_ready returning because of non-active state\n");
362 DPRINTF("iterate\n");
363 ret
= qemu_savevm_state_iterate(s
->mon
, s
->file
);
366 } else if (ret
== 1) {
367 int old_vm_running
= runstate_is_running();
369 DPRINTF("done iterating\n");
370 vm_stop(RUN_STATE_FINISH_MIGRATE
);
372 if ((qemu_savevm_state_complete(s
->mon
, s
->file
)) < 0) {
373 if (old_vm_running
) {
376 s
->state
= MIG_STATE_ERROR
;
378 if (migrate_fd_cleanup(s
) < 0) {
379 if (old_vm_running
) {
382 s
->state
= MIG_STATE_ERROR
;
384 if (s
->state
== MIG_STATE_ACTIVE
) {
385 s
->state
= MIG_STATE_COMPLETED
;
386 runstate_set(RUN_STATE_POSTMIGRATE
);
388 notifier_list_notify(&migration_state_notifiers
, NULL
);
392 static int migrate_fd_get_status(MigrationState
*s
)
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_release(MigrationState
*s
)
414 DPRINTF("releasing state\n");
416 if (s
->state
== MIG_STATE_ACTIVE
) {
417 s
->state
= MIG_STATE_CANCELLED
;
418 notifier_list_notify(&migration_state_notifiers
, NULL
);
419 migrate_fd_cleanup(s
);
424 static void migrate_fd_wait_for_unfreeze(void *opaque
)
426 MigrationState
*s
= opaque
;
429 DPRINTF("wait for unfreeze\n");
430 if (s
->state
!= MIG_STATE_ACTIVE
)
437 FD_SET(s
->fd
, &wfds
);
439 ret
= select(s
->fd
+ 1, NULL
, &wfds
, NULL
, NULL
);
440 } while (ret
== -1 && (s
->get_error(s
)) == EINTR
);
443 qemu_file_set_error(s
->file
, -s
->get_error(s
));
447 static int migrate_fd_close(void *opaque
)
449 MigrationState
*s
= opaque
;
452 monitor_resume(s
->mon
);
454 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
458 void add_migration_state_change_notifier(Notifier
*notify
)
460 notifier_list_add(&migration_state_notifiers
, notify
);
463 void remove_migration_state_change_notifier(Notifier
*notify
)
465 notifier_list_remove(&migration_state_notifiers
, notify
);
468 int get_migration_state(void)
470 if (current_migration
) {
471 return migrate_fd_get_status(current_migration
);
473 return MIG_STATE_ERROR
;
477 void migrate_fd_connect(MigrationState
*s
)
481 s
->file
= qemu_fopen_ops_buffered(s
,
483 migrate_fd_put_buffer
,
484 migrate_fd_put_ready
,
485 migrate_fd_wait_for_unfreeze
,
488 DPRINTF("beginning savevm\n");
489 ret
= qemu_savevm_state_begin(s
->mon
, s
->file
, s
->blk
, s
->shared
);
491 DPRINTF("failed, %d\n", ret
);
495 migrate_fd_put_ready(s
);
498 static MigrationState
*migrate_new(Monitor
*mon
, int64_t bandwidth_limit
,
499 int detach
, int blk
, int inc
)
501 MigrationState
*s
= g_malloc0(sizeof(*s
));
503 s
->cancel
= migrate_fd_cancel
;
504 s
->get_status
= migrate_fd_get_status
;
505 s
->release
= migrate_fd_release
;
509 s
->bandwidth_limit
= bandwidth_limit
;
510 s
->state
= MIG_STATE_ACTIVE
;
513 migrate_fd_monitor_suspend(s
, mon
);