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 uint32_t max_throttle
= (32 << 20);
37 static MigrationState
*current_migration
;
39 void qemu_start_incoming_migration(const char *uri
)
43 if (strstart(uri
, "tcp:", &p
))
44 tcp_start_incoming_migration(p
);
46 else if (strstart(uri
, "exec:", &p
))
47 exec_start_incoming_migration(p
);
48 else if (strstart(uri
, "unix:", &p
))
49 unix_start_incoming_migration(p
);
50 else if (strstart(uri
, "fd:", &p
))
51 fd_start_incoming_migration(p
);
54 fprintf(stderr
, "unknown migration protocol: %s\n", uri
);
57 void do_migrate(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
59 MigrationState
*s
= NULL
;
61 int detach
= qdict_get_int(qdict
, "detach");
62 const char *uri
= qdict_get_str(qdict
, "uri");
64 if (current_migration
&&
65 current_migration
->get_status(current_migration
) == MIG_STATE_ACTIVE
) {
66 monitor_printf(mon
, "migration already in progress\n");
70 if (strstart(uri
, "tcp:", &p
))
71 s
= tcp_start_outgoing_migration(mon
, p
, max_throttle
, detach
,
72 (int)qdict_get_int(qdict
, "blk"),
73 (int)qdict_get_int(qdict
, "inc"));
75 else if (strstart(uri
, "exec:", &p
))
76 s
= exec_start_outgoing_migration(mon
, p
, max_throttle
, detach
,
77 (int)qdict_get_int(qdict
, "blk"),
78 (int)qdict_get_int(qdict
, "inc"));
79 else if (strstart(uri
, "unix:", &p
))
80 s
= unix_start_outgoing_migration(mon
, p
, max_throttle
, detach
,
81 (int)qdict_get_int(qdict
, "blk"),
82 (int)qdict_get_int(qdict
, "inc"));
83 else if (strstart(uri
, "fd:", &p
))
84 s
= fd_start_outgoing_migration(mon
, p
, max_throttle
, detach
,
85 (int)qdict_get_int(qdict
, "blk"),
86 (int)qdict_get_int(qdict
, "inc"));
89 monitor_printf(mon
, "unknown migration protocol: %s\n", uri
);
92 monitor_printf(mon
, "migration failed\n");
94 if (current_migration
)
95 current_migration
->release(current_migration
);
97 current_migration
= s
;
101 int do_migrate_cancel(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
103 MigrationState
*s
= current_migration
;
111 int do_migrate_set_speed(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
116 d
= qdict_get_double(qdict
, "value");
117 d
= MAX(0, MIN(UINT32_MAX
, d
));
120 s
= migrate_to_fms(current_migration
);
122 qemu_file_set_rate_limit(s
->file
, max_throttle
);
128 /* amount of nanoseconds we are willing to wait for migration to be down.
129 * the choice of nanoseconds is because it is the maximum resolution that
130 * get_clock() can achieve. It is an internal measure. All user-visible
131 * units must be in seconds */
132 static uint64_t max_downtime
= 30000000;
134 uint64_t migrate_max_downtime(void)
139 int do_migrate_set_downtime(Monitor
*mon
, const QDict
*qdict
,
144 d
= qdict_get_double(qdict
, "value") * 1e9
;
145 d
= MAX(0, MIN(UINT64_MAX
, d
));
146 max_downtime
= (uint64_t)d
;
151 static void migrate_print_status(Monitor
*mon
, const char *name
,
152 const QDict
*status_dict
)
156 qdict
= qobject_to_qdict(qdict_get(status_dict
, name
));
158 monitor_printf(mon
, "transferred %s: %" PRIu64
" kbytes\n", name
,
159 qdict_get_int(qdict
, "transferred") >> 10);
160 monitor_printf(mon
, "remaining %s: %" PRIu64
" kbytes\n", name
,
161 qdict_get_int(qdict
, "remaining") >> 10);
162 monitor_printf(mon
, "total %s: %" PRIu64
" kbytes\n", name
,
163 qdict_get_int(qdict
, "total") >> 10);
166 void do_info_migrate_print(Monitor
*mon
, const QObject
*data
)
170 qdict
= qobject_to_qdict(data
);
172 monitor_printf(mon
, "Migration status: %s\n",
173 qdict_get_str(qdict
, "status"));
175 if (qdict_haskey(qdict
, "ram")) {
176 migrate_print_status(mon
, "ram", qdict
);
179 if (qdict_haskey(qdict
, "disk")) {
180 migrate_print_status(mon
, "disk", qdict
);
184 static void migrate_put_status(QDict
*qdict
, const char *name
,
185 uint64_t trans
, uint64_t rem
, uint64_t total
)
189 obj
= qobject_from_jsonf("{ 'transferred': %" PRId64
", "
190 "'remaining': %" PRId64
", "
191 "'total': %" PRId64
" }", trans
, rem
, total
);
192 qdict_put_obj(qdict
, name
, obj
);
196 * do_info_migrate(): Migration status
198 * Return a QDict. If migration is active there will be another
199 * QDict with RAM migration status and if block migration is active
200 * another one with block migration status.
202 * The main QDict contains the following:
204 * - "status": migration status
205 * - "ram": only present if "status" is "active", it is a QDict with the
206 * following RAM information (in bytes):
207 * - "transferred": amount transferred
208 * - "remaining": amount remaining
210 * - "disk": only present if "status" is "active" and it is a block migration,
211 * it is a QDict with the following disk information (in bytes):
212 * - "transferred": amount transferred
213 * - "remaining": amount remaining
218 * 1. Migration is "completed":
220 * { "status": "completed" }
222 * 2. Migration is "active" and it is not a block migration:
224 * { "status": "active",
225 * "ram": { "transferred": 123, "remaining": 123, "total": 246 } }
227 * 3. Migration is "active" and it is a block migration:
229 * { "status": "active",
230 * "ram": { "total": 1057024, "remaining": 1053304, "transferred": 3720 },
231 * "disk": { "total": 20971520, "remaining": 20880384, "transferred": 91136 }}
233 void do_info_migrate(Monitor
*mon
, QObject
**ret_data
)
236 MigrationState
*s
= current_migration
;
239 switch (s
->get_status(s
)) {
240 case MIG_STATE_ACTIVE
:
242 qdict_put(qdict
, "status", qstring_from_str("active"));
244 migrate_put_status(qdict
, "ram", ram_bytes_transferred(),
245 ram_bytes_remaining(), ram_bytes_total());
247 if (blk_mig_active()) {
248 migrate_put_status(qdict
, "disk", blk_mig_bytes_transferred(),
249 blk_mig_bytes_remaining(),
250 blk_mig_bytes_total());
253 *ret_data
= QOBJECT(qdict
);
255 case MIG_STATE_COMPLETED
:
256 *ret_data
= qobject_from_jsonf("{ 'status': 'completed' }");
258 case MIG_STATE_ERROR
:
259 *ret_data
= qobject_from_jsonf("{ 'status': 'failed' }");
261 case MIG_STATE_CANCELLED
:
262 *ret_data
= qobject_from_jsonf("{ 'status': 'cancelled' }");
268 /* shared migration helpers */
270 void migrate_fd_monitor_suspend(FdMigrationState
*s
, Monitor
*mon
)
273 if (monitor_suspend(mon
) == 0) {
274 DPRINTF("suspending monitor\n");
276 monitor_printf(mon
, "terminal does not allow synchronous "
277 "migration, continuing detached\n");
281 void migrate_fd_error(FdMigrationState
*s
)
283 DPRINTF("setting error state\n");
284 s
->state
= MIG_STATE_ERROR
;
285 migrate_fd_cleanup(s
);
288 void migrate_fd_cleanup(FdMigrationState
*s
)
290 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
293 DPRINTF("closing file\n");
294 qemu_fclose(s
->file
);
301 /* Don't resume monitor until we've flushed all of the buffers */
303 monitor_resume(s
->mon
);
309 void migrate_fd_put_notify(void *opaque
)
311 FdMigrationState
*s
= opaque
;
313 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
314 qemu_file_put_notify(s
->file
);
317 ssize_t
migrate_fd_put_buffer(void *opaque
, const void *data
, size_t size
)
319 FdMigrationState
*s
= opaque
;
323 ret
= s
->write(s
, data
, size
);
324 } while (ret
== -1 && ((s
->get_error(s
)) == EINTR
));
327 ret
= -(s
->get_error(s
));
330 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, migrate_fd_put_notify
, s
);
335 void migrate_fd_connect(FdMigrationState
*s
)
339 s
->file
= qemu_fopen_ops_buffered(s
,
341 migrate_fd_put_buffer
,
342 migrate_fd_put_ready
,
343 migrate_fd_wait_for_unfreeze
,
346 DPRINTF("beginning savevm\n");
347 ret
= qemu_savevm_state_begin(s
->mon
, s
->file
, s
->mig_state
.blk
,
348 s
->mig_state
.shared
);
350 DPRINTF("failed, %d\n", ret
);
355 migrate_fd_put_ready(s
);
358 void migrate_fd_put_ready(void *opaque
)
360 FdMigrationState
*s
= opaque
;
362 if (s
->state
!= MIG_STATE_ACTIVE
) {
363 DPRINTF("put_ready returning because of non-active state\n");
367 DPRINTF("iterate\n");
368 if (qemu_savevm_state_iterate(s
->mon
, s
->file
) == 1) {
370 int old_vm_running
= vm_running
;
372 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 migrate_fd_cleanup(s
);
390 int migrate_fd_get_status(MigrationState
*mig_state
)
392 FdMigrationState
*s
= migrate_to_fms(mig_state
);
396 void migrate_fd_cancel(MigrationState
*mig_state
)
398 FdMigrationState
*s
= migrate_to_fms(mig_state
);
400 if (s
->state
!= MIG_STATE_ACTIVE
)
403 DPRINTF("cancelling migration\n");
405 s
->state
= MIG_STATE_CANCELLED
;
406 qemu_savevm_state_cancel(s
->mon
, s
->file
);
408 migrate_fd_cleanup(s
);
411 void migrate_fd_release(MigrationState
*mig_state
)
413 FdMigrationState
*s
= migrate_to_fms(mig_state
);
415 DPRINTF("releasing state\n");
417 if (s
->state
== MIG_STATE_ACTIVE
) {
418 s
->state
= MIG_STATE_CANCELLED
;
419 migrate_fd_cleanup(s
);
424 void migrate_fd_wait_for_unfreeze(void *opaque
)
426 FdMigrationState
*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 int migrate_fd_close(void *opaque
)
445 FdMigrationState
*s
= opaque
;
447 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);