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 int qemu_start_incoming_migration(const char *uri
)
44 if (strstart(uri
, "tcp:", &p
))
45 ret
= tcp_start_incoming_migration(p
);
47 else if (strstart(uri
, "exec:", &p
))
48 ret
= exec_start_incoming_migration(p
);
49 else if (strstart(uri
, "unix:", &p
))
50 ret
= unix_start_incoming_migration(p
);
51 else if (strstart(uri
, "fd:", &p
))
52 ret
= fd_start_incoming_migration(p
);
55 fprintf(stderr
, "unknown migration protocol: %s\n", uri
);
56 ret
= -EPROTONOSUPPORT
;
61 void process_incoming_migration(QEMUFile
*f
)
63 if (qemu_loadvm_state(f
) < 0) {
64 fprintf(stderr
, "load of migration failed\n");
68 DPRINTF("successfully loaded vm state\n");
70 incoming_expected
= false;
76 int do_migrate(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
78 MigrationState
*s
= NULL
;
80 int detach
= qdict_get_try_bool(qdict
, "detach", 0);
81 int blk
= qdict_get_try_bool(qdict
, "blk", 0);
82 int inc
= qdict_get_try_bool(qdict
, "inc", 0);
83 const char *uri
= qdict_get_str(qdict
, "uri");
85 if (current_migration
&&
86 current_migration
->get_status(current_migration
) == MIG_STATE_ACTIVE
) {
87 monitor_printf(mon
, "migration already in progress\n");
91 if (qemu_savevm_state_blocked(mon
)) {
95 if (strstart(uri
, "tcp:", &p
)) {
96 s
= tcp_start_outgoing_migration(mon
, p
, max_throttle
, detach
,
99 } else if (strstart(uri
, "exec:", &p
)) {
100 s
= exec_start_outgoing_migration(mon
, p
, max_throttle
, detach
,
102 } else if (strstart(uri
, "unix:", &p
)) {
103 s
= unix_start_outgoing_migration(mon
, p
, max_throttle
, detach
,
105 } else if (strstart(uri
, "fd:", &p
)) {
106 s
= fd_start_outgoing_migration(mon
, p
, max_throttle
, detach
,
110 monitor_printf(mon
, "unknown migration protocol: %s\n", uri
);
115 monitor_printf(mon
, "migration failed\n");
119 if (current_migration
) {
120 current_migration
->release(current_migration
);
123 current_migration
= s
;
127 int do_migrate_cancel(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
129 MigrationState
*s
= current_migration
;
137 int do_migrate_set_speed(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
142 d
= qdict_get_int(qdict
, "value");
148 s
= migrate_to_fms(current_migration
);
150 qemu_file_set_rate_limit(s
->file
, max_throttle
);
156 /* amount of nanoseconds we are willing to wait for migration to be down.
157 * the choice of nanoseconds is because it is the maximum resolution that
158 * get_clock() can achieve. It is an internal measure. All user-visible
159 * units must be in seconds */
160 static uint64_t max_downtime
= 30000000;
162 uint64_t migrate_max_downtime(void)
167 int do_migrate_set_downtime(Monitor
*mon
, const QDict
*qdict
,
172 d
= qdict_get_double(qdict
, "value") * 1e9
;
173 d
= MAX(0, MIN(UINT64_MAX
, d
));
174 max_downtime
= (uint64_t)d
;
179 static void migrate_print_status(Monitor
*mon
, const char *name
,
180 const QDict
*status_dict
)
184 qdict
= qobject_to_qdict(qdict_get(status_dict
, name
));
186 monitor_printf(mon
, "transferred %s: %" PRIu64
" kbytes\n", name
,
187 qdict_get_int(qdict
, "transferred") >> 10);
188 monitor_printf(mon
, "remaining %s: %" PRIu64
" kbytes\n", name
,
189 qdict_get_int(qdict
, "remaining") >> 10);
190 monitor_printf(mon
, "total %s: %" PRIu64
" kbytes\n", name
,
191 qdict_get_int(qdict
, "total") >> 10);
194 void do_info_migrate_print(Monitor
*mon
, const QObject
*data
)
198 qdict
= qobject_to_qdict(data
);
200 monitor_printf(mon
, "Migration status: %s\n",
201 qdict_get_str(qdict
, "status"));
203 if (qdict_haskey(qdict
, "ram")) {
204 migrate_print_status(mon
, "ram", qdict
);
207 if (qdict_haskey(qdict
, "disk")) {
208 migrate_print_status(mon
, "disk", qdict
);
212 static void migrate_put_status(QDict
*qdict
, const char *name
,
213 uint64_t trans
, uint64_t rem
, uint64_t total
)
217 obj
= qobject_from_jsonf("{ 'transferred': %" PRId64
", "
218 "'remaining': %" PRId64
", "
219 "'total': %" PRId64
" }", trans
, rem
, total
);
220 qdict_put_obj(qdict
, name
, obj
);
223 void do_info_migrate(Monitor
*mon
, QObject
**ret_data
)
226 MigrationState
*s
= current_migration
;
229 switch (s
->get_status(s
)) {
230 case MIG_STATE_ACTIVE
:
232 qdict_put(qdict
, "status", qstring_from_str("active"));
234 migrate_put_status(qdict
, "ram", ram_bytes_transferred(),
235 ram_bytes_remaining(), ram_bytes_total());
237 if (blk_mig_active()) {
238 migrate_put_status(qdict
, "disk", blk_mig_bytes_transferred(),
239 blk_mig_bytes_remaining(),
240 blk_mig_bytes_total());
243 *ret_data
= QOBJECT(qdict
);
245 case MIG_STATE_COMPLETED
:
246 *ret_data
= qobject_from_jsonf("{ 'status': 'completed' }");
248 case MIG_STATE_ERROR
:
249 *ret_data
= qobject_from_jsonf("{ 'status': 'failed' }");
251 case MIG_STATE_CANCELLED
:
252 *ret_data
= qobject_from_jsonf("{ 'status': 'cancelled' }");
258 /* shared migration helpers */
260 void migrate_fd_monitor_suspend(FdMigrationState
*s
, Monitor
*mon
)
263 if (monitor_suspend(mon
) == 0) {
264 DPRINTF("suspending monitor\n");
266 monitor_printf(mon
, "terminal does not allow synchronous "
267 "migration, continuing detached\n");
271 void migrate_fd_error(FdMigrationState
*s
)
273 DPRINTF("setting error state\n");
274 s
->state
= MIG_STATE_ERROR
;
275 migrate_fd_cleanup(s
);
278 int migrate_fd_cleanup(FdMigrationState
*s
)
282 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
285 DPRINTF("closing file\n");
286 if (qemu_fclose(s
->file
) != 0) {
295 /* Don't resume monitor until we've flushed all of the buffers */
297 monitor_resume(s
->mon
);
305 void migrate_fd_put_notify(void *opaque
)
307 FdMigrationState
*s
= opaque
;
309 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
310 qemu_file_put_notify(s
->file
);
313 ssize_t
migrate_fd_put_buffer(void *opaque
, const void *data
, size_t size
)
315 FdMigrationState
*s
= opaque
;
319 ret
= s
->write(s
, data
, size
);
320 } while (ret
== -1 && ((s
->get_error(s
)) == EINTR
));
323 ret
= -(s
->get_error(s
));
325 if (ret
== -EAGAIN
) {
326 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, migrate_fd_put_notify
, s
);
327 } else if (ret
< 0) {
329 monitor_resume(s
->mon
);
331 s
->state
= MIG_STATE_ERROR
;
337 void migrate_fd_connect(FdMigrationState
*s
)
341 s
->file
= qemu_fopen_ops_buffered(s
,
343 migrate_fd_put_buffer
,
344 migrate_fd_put_ready
,
345 migrate_fd_wait_for_unfreeze
,
348 DPRINTF("beginning savevm\n");
349 ret
= qemu_savevm_state_begin(s
->mon
, s
->file
, s
->mig_state
.blk
,
350 s
->mig_state
.shared
);
352 DPRINTF("failed, %d\n", ret
);
357 migrate_fd_put_ready(s
);
360 void migrate_fd_put_ready(void *opaque
)
362 FdMigrationState
*s
= opaque
;
364 if (s
->state
!= MIG_STATE_ACTIVE
) {
365 DPRINTF("put_ready returning because of non-active state\n");
369 DPRINTF("iterate\n");
370 if (qemu_savevm_state_iterate(s
->mon
, s
->file
) == 1) {
372 int old_vm_running
= vm_running
;
374 DPRINTF("done iterating\n");
377 if ((qemu_savevm_state_complete(s
->mon
, s
->file
)) < 0) {
378 if (old_vm_running
) {
381 state
= MIG_STATE_ERROR
;
383 state
= MIG_STATE_COMPLETED
;
385 if (migrate_fd_cleanup(s
) < 0) {
386 if (old_vm_running
) {
389 state
= MIG_STATE_ERROR
;
395 int migrate_fd_get_status(MigrationState
*mig_state
)
397 FdMigrationState
*s
= migrate_to_fms(mig_state
);
401 void migrate_fd_cancel(MigrationState
*mig_state
)
403 FdMigrationState
*s
= migrate_to_fms(mig_state
);
405 if (s
->state
!= MIG_STATE_ACTIVE
)
408 DPRINTF("cancelling migration\n");
410 s
->state
= MIG_STATE_CANCELLED
;
411 qemu_savevm_state_cancel(s
->mon
, s
->file
);
413 migrate_fd_cleanup(s
);
416 void migrate_fd_release(MigrationState
*mig_state
)
418 FdMigrationState
*s
= migrate_to_fms(mig_state
);
420 DPRINTF("releasing state\n");
422 if (s
->state
== MIG_STATE_ACTIVE
) {
423 s
->state
= MIG_STATE_CANCELLED
;
424 migrate_fd_cleanup(s
);
429 void migrate_fd_wait_for_unfreeze(void *opaque
)
431 FdMigrationState
*s
= opaque
;
434 DPRINTF("wait for unfreeze\n");
435 if (s
->state
!= MIG_STATE_ACTIVE
)
442 FD_SET(s
->fd
, &wfds
);
444 ret
= select(s
->fd
+ 1, NULL
, &wfds
, NULL
, NULL
);
445 } while (ret
== -1 && (s
->get_error(s
)) == EINTR
);
448 int migrate_fd_close(void *opaque
)
450 FdMigrationState
*s
= opaque
;
452 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);