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 void do_migrate_cancel(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
103 MigrationState
*s
= current_migration
;
109 void do_migrate_set_speed(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
114 const char *value
= qdict_get_str(qdict
, "value");
116 d
= strtod(value
, &ptr
);
128 max_throttle
= (uint32_t)d
;
130 s
= migrate_to_fms(current_migration
);
132 qemu_file_set_rate_limit(s
->file
, max_throttle
);
136 /* amount of nanoseconds we are willing to wait for migration to be down.
137 * the choice of nanoseconds is because it is the maximum resolution that
138 * get_clock() can achieve. It is an internal measure. All user-visible
139 * units must be in seconds */
140 static uint64_t max_downtime
= 30000000;
142 uint64_t migrate_max_downtime(void)
147 void do_migrate_set_downtime(Monitor
*mon
, const QDict
*qdict
)
151 const char *value
= qdict_get_str(qdict
, "value");
153 d
= strtod(value
, &ptr
);
154 if (!strcmp(ptr
,"ms")) {
156 } else if (!strcmp(ptr
,"us")) {
158 } else if (!strcmp(ptr
,"ns")) {
160 /* all else considered to be seconds */
164 max_downtime
= (uint64_t)d
;
167 static void migrate_print_status(Monitor
*mon
, const char *name
,
168 const QDict
*status_dict
)
172 qdict
= qobject_to_qdict(qdict_get(status_dict
, name
));
174 monitor_printf(mon
, "transferred %s: %" PRIu64
" kbytes\n", name
,
175 qdict_get_int(qdict
, "transferred") >> 10);
176 monitor_printf(mon
, "remaining %s: %" PRIu64
" kbytes\n", name
,
177 qdict_get_int(qdict
, "remaining") >> 10);
178 monitor_printf(mon
, "total %s: %" PRIu64
" kbytes\n", name
,
179 qdict_get_int(qdict
, "total") >> 10);
182 void do_info_migrate_print(Monitor
*mon
, const QObject
*data
)
186 qdict
= qobject_to_qdict(data
);
188 monitor_printf(mon
, "Migration status: %s\n",
189 qdict_get_str(qdict
, "status"));
191 if (qdict_haskey(qdict
, "ram")) {
192 migrate_print_status(mon
, "ram", qdict
);
195 if (qdict_haskey(qdict
, "disk")) {
196 migrate_print_status(mon
, "disk", qdict
);
200 static void migrate_put_status(QDict
*qdict
, const char *name
,
201 uint64_t trans
, uint64_t rem
, uint64_t total
)
205 obj
= qobject_from_jsonf("{ 'transferred': %" PRId64
", "
206 "'remaining': %" PRId64
", "
207 "'total': %" PRId64
" }", trans
, rem
, total
);
210 qdict_put_obj(qdict
, name
, obj
);
214 * do_info_migrate(): Migration status
216 * Return a QDict. If migration is active there will be another
217 * QDict with RAM migration status and if block migration is active
218 * another one with block migration status.
220 * The main QDict contains the following:
222 * - "status": migration status
223 * - "ram": only present if "status" is "active", it is a QDict with the
224 * following RAM information (in bytes):
225 * - "transferred": amount transferred
226 * - "remaining": amount remaining
228 * - "disk": only present if "status" is "active" and it is a block migration,
229 * it is a QDict with the following disk information (in bytes):
230 * - "transferred": amount transferred
231 * - "remaining": amount remaining
236 * 1. Migration is "completed":
238 * { "status": "completed" }
240 * 2. Migration is "active" and it is not a block migration:
242 * { "status": "active",
243 * "ram": { "transferred": 123, "remaining": 123, "total": 246 } }
245 * 3. Migration is "active" and it is a block migration:
247 * { "status": "active",
248 * "ram": { "total": 1057024, "remaining": 1053304, "transferred": 3720 },
249 * "disk": { "total": 20971520, "remaining": 20880384, "transferred": 91136 }}
251 void do_info_migrate(Monitor
*mon
, QObject
**ret_data
)
254 MigrationState
*s
= current_migration
;
257 switch (s
->get_status(s
)) {
258 case MIG_STATE_ACTIVE
:
260 qdict_put(qdict
, "status", qstring_from_str("active"));
262 migrate_put_status(qdict
, "ram", ram_bytes_transferred(),
263 ram_bytes_remaining(), ram_bytes_total());
265 if (blk_mig_active()) {
266 migrate_put_status(qdict
, "disk", blk_mig_bytes_transferred(),
267 blk_mig_bytes_remaining(),
268 blk_mig_bytes_total());
271 *ret_data
= QOBJECT(qdict
);
273 case MIG_STATE_COMPLETED
:
274 *ret_data
= qobject_from_jsonf("{ 'status': 'completed' }");
276 case MIG_STATE_ERROR
:
277 *ret_data
= qobject_from_jsonf("{ 'status': 'failed' }");
279 case MIG_STATE_CANCELLED
:
280 *ret_data
= qobject_from_jsonf("{ 'status': 'cancelled' }");
283 assert(*ret_data
!= NULL
);
287 /* shared migration helpers */
289 void migrate_fd_monitor_suspend(FdMigrationState
*s
, Monitor
*mon
)
292 if (monitor_suspend(mon
) == 0) {
293 dprintf("suspending monitor\n");
295 monitor_printf(mon
, "terminal does not allow synchronous "
296 "migration, continuing detached\n");
300 void migrate_fd_error(FdMigrationState
*s
)
302 dprintf("setting error state\n");
303 s
->state
= MIG_STATE_ERROR
;
304 migrate_fd_cleanup(s
);
307 void migrate_fd_cleanup(FdMigrationState
*s
)
309 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
312 dprintf("closing file\n");
313 qemu_fclose(s
->file
);
320 /* Don't resume monitor until we've flushed all of the buffers */
322 monitor_resume(s
->mon
);
328 void migrate_fd_put_notify(void *opaque
)
330 FdMigrationState
*s
= opaque
;
332 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
333 qemu_file_put_notify(s
->file
);
336 ssize_t
migrate_fd_put_buffer(void *opaque
, const void *data
, size_t size
)
338 FdMigrationState
*s
= opaque
;
342 ret
= s
->write(s
, data
, size
);
343 } while (ret
== -1 && ((s
->get_error(s
)) == EINTR
));
346 ret
= -(s
->get_error(s
));
349 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, migrate_fd_put_notify
, s
);
354 void migrate_fd_connect(FdMigrationState
*s
)
358 s
->file
= qemu_fopen_ops_buffered(s
,
360 migrate_fd_put_buffer
,
361 migrate_fd_put_ready
,
362 migrate_fd_wait_for_unfreeze
,
365 dprintf("beginning savevm\n");
366 ret
= qemu_savevm_state_begin(s
->mon
, s
->file
, s
->mig_state
.blk
,
367 s
->mig_state
.shared
);
369 dprintf("failed, %d\n", ret
);
374 migrate_fd_put_ready(s
);
377 void migrate_fd_put_ready(void *opaque
)
379 FdMigrationState
*s
= opaque
;
381 if (s
->state
!= MIG_STATE_ACTIVE
) {
382 dprintf("put_ready returning because of non-active state\n");
386 dprintf("iterate\n");
387 if (qemu_savevm_state_iterate(s
->mon
, s
->file
) == 1) {
389 int old_vm_running
= vm_running
;
391 dprintf("done iterating\n");
396 if ((qemu_savevm_state_complete(s
->mon
, s
->file
)) < 0) {
397 if (old_vm_running
) {
400 state
= MIG_STATE_ERROR
;
402 state
= MIG_STATE_COMPLETED
;
404 migrate_fd_cleanup(s
);
409 int migrate_fd_get_status(MigrationState
*mig_state
)
411 FdMigrationState
*s
= migrate_to_fms(mig_state
);
415 void migrate_fd_cancel(MigrationState
*mig_state
)
417 FdMigrationState
*s
= migrate_to_fms(mig_state
);
419 if (s
->state
!= MIG_STATE_ACTIVE
)
422 dprintf("cancelling migration\n");
424 s
->state
= MIG_STATE_CANCELLED
;
425 qemu_savevm_state_cancel(s
->mon
, s
->file
);
427 migrate_fd_cleanup(s
);
430 void migrate_fd_release(MigrationState
*mig_state
)
432 FdMigrationState
*s
= migrate_to_fms(mig_state
);
434 dprintf("releasing state\n");
436 if (s
->state
== MIG_STATE_ACTIVE
) {
437 s
->state
= MIG_STATE_CANCELLED
;
438 migrate_fd_cleanup(s
);
443 void migrate_fd_wait_for_unfreeze(void *opaque
)
445 FdMigrationState
*s
= opaque
;
448 dprintf("wait for unfreeze\n");
449 if (s
->state
!= MIG_STATE_ACTIVE
)
456 FD_SET(s
->fd
, &wfds
);
458 ret
= select(s
->fd
+ 1, NULL
, &wfds
, NULL
, NULL
);
459 } while (ret
== -1 && (s
->get_error(s
)) == EINTR
);
462 int migrate_fd_close(void *opaque
)
464 FdMigrationState
*s
= opaque
;
466 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);