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 d
= qdict_get_double(qdict
, "value");
115 d
= MAX(0, MIN(UINT32_MAX
, d
));
118 s
= migrate_to_fms(current_migration
);
120 qemu_file_set_rate_limit(s
->file
, max_throttle
);
124 /* amount of nanoseconds we are willing to wait for migration to be down.
125 * the choice of nanoseconds is because it is the maximum resolution that
126 * get_clock() can achieve. It is an internal measure. All user-visible
127 * units must be in seconds */
128 static uint64_t max_downtime
= 30000000;
130 uint64_t migrate_max_downtime(void)
135 void do_migrate_set_downtime(Monitor
*mon
, const QDict
*qdict
,
140 d
= qdict_get_double(qdict
, "value") * 1e9
;
141 d
= MAX(0, MIN(UINT64_MAX
, d
));
142 max_downtime
= (uint64_t)d
;
145 static void migrate_print_status(Monitor
*mon
, const char *name
,
146 const QDict
*status_dict
)
150 qdict
= qobject_to_qdict(qdict_get(status_dict
, name
));
152 monitor_printf(mon
, "transferred %s: %" PRIu64
" kbytes\n", name
,
153 qdict_get_int(qdict
, "transferred") >> 10);
154 monitor_printf(mon
, "remaining %s: %" PRIu64
" kbytes\n", name
,
155 qdict_get_int(qdict
, "remaining") >> 10);
156 monitor_printf(mon
, "total %s: %" PRIu64
" kbytes\n", name
,
157 qdict_get_int(qdict
, "total") >> 10);
160 void do_info_migrate_print(Monitor
*mon
, const QObject
*data
)
164 qdict
= qobject_to_qdict(data
);
166 monitor_printf(mon
, "Migration status: %s\n",
167 qdict_get_str(qdict
, "status"));
169 if (qdict_haskey(qdict
, "ram")) {
170 migrate_print_status(mon
, "ram", qdict
);
173 if (qdict_haskey(qdict
, "disk")) {
174 migrate_print_status(mon
, "disk", qdict
);
178 static void migrate_put_status(QDict
*qdict
, const char *name
,
179 uint64_t trans
, uint64_t rem
, uint64_t total
)
183 obj
= qobject_from_jsonf("{ 'transferred': %" PRId64
", "
184 "'remaining': %" PRId64
", "
185 "'total': %" PRId64
" }", trans
, rem
, total
);
186 qdict_put_obj(qdict
, name
, obj
);
190 * do_info_migrate(): Migration status
192 * Return a QDict. If migration is active there will be another
193 * QDict with RAM migration status and if block migration is active
194 * another one with block migration status.
196 * The main QDict contains the following:
198 * - "status": migration status
199 * - "ram": only present if "status" is "active", it is a QDict with the
200 * following RAM information (in bytes):
201 * - "transferred": amount transferred
202 * - "remaining": amount remaining
204 * - "disk": only present if "status" is "active" and it is a block migration,
205 * it is a QDict with the following disk information (in bytes):
206 * - "transferred": amount transferred
207 * - "remaining": amount remaining
212 * 1. Migration is "completed":
214 * { "status": "completed" }
216 * 2. Migration is "active" and it is not a block migration:
218 * { "status": "active",
219 * "ram": { "transferred": 123, "remaining": 123, "total": 246 } }
221 * 3. Migration is "active" and it is a block migration:
223 * { "status": "active",
224 * "ram": { "total": 1057024, "remaining": 1053304, "transferred": 3720 },
225 * "disk": { "total": 20971520, "remaining": 20880384, "transferred": 91136 }}
227 void do_info_migrate(Monitor
*mon
, QObject
**ret_data
)
230 MigrationState
*s
= current_migration
;
233 switch (s
->get_status(s
)) {
234 case MIG_STATE_ACTIVE
:
236 qdict_put(qdict
, "status", qstring_from_str("active"));
238 migrate_put_status(qdict
, "ram", ram_bytes_transferred(),
239 ram_bytes_remaining(), ram_bytes_total());
241 if (blk_mig_active()) {
242 migrate_put_status(qdict
, "disk", blk_mig_bytes_transferred(),
243 blk_mig_bytes_remaining(),
244 blk_mig_bytes_total());
247 *ret_data
= QOBJECT(qdict
);
249 case MIG_STATE_COMPLETED
:
250 *ret_data
= qobject_from_jsonf("{ 'status': 'completed' }");
252 case MIG_STATE_ERROR
:
253 *ret_data
= qobject_from_jsonf("{ 'status': 'failed' }");
255 case MIG_STATE_CANCELLED
:
256 *ret_data
= qobject_from_jsonf("{ 'status': 'cancelled' }");
262 /* shared migration helpers */
264 void migrate_fd_monitor_suspend(FdMigrationState
*s
, Monitor
*mon
)
267 if (monitor_suspend(mon
) == 0) {
268 DPRINTF("suspending monitor\n");
270 monitor_printf(mon
, "terminal does not allow synchronous "
271 "migration, continuing detached\n");
275 void migrate_fd_error(FdMigrationState
*s
)
277 DPRINTF("setting error state\n");
278 s
->state
= MIG_STATE_ERROR
;
279 migrate_fd_cleanup(s
);
282 void migrate_fd_cleanup(FdMigrationState
*s
)
284 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
287 DPRINTF("closing file\n");
288 qemu_fclose(s
->file
);
295 /* Don't resume monitor until we've flushed all of the buffers */
297 monitor_resume(s
->mon
);
303 void migrate_fd_put_notify(void *opaque
)
305 FdMigrationState
*s
= opaque
;
307 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
308 qemu_file_put_notify(s
->file
);
311 ssize_t
migrate_fd_put_buffer(void *opaque
, const void *data
, size_t size
)
313 FdMigrationState
*s
= opaque
;
317 ret
= s
->write(s
, data
, size
);
318 } while (ret
== -1 && ((s
->get_error(s
)) == EINTR
));
321 ret
= -(s
->get_error(s
));
324 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, migrate_fd_put_notify
, s
);
329 void migrate_fd_connect(FdMigrationState
*s
)
333 s
->file
= qemu_fopen_ops_buffered(s
,
335 migrate_fd_put_buffer
,
336 migrate_fd_put_ready
,
337 migrate_fd_wait_for_unfreeze
,
340 DPRINTF("beginning savevm\n");
341 ret
= qemu_savevm_state_begin(s
->mon
, s
->file
, s
->mig_state
.blk
,
342 s
->mig_state
.shared
);
344 DPRINTF("failed, %d\n", ret
);
349 migrate_fd_put_ready(s
);
352 void migrate_fd_put_ready(void *opaque
)
354 FdMigrationState
*s
= opaque
;
356 if (s
->state
!= MIG_STATE_ACTIVE
) {
357 DPRINTF("put_ready returning because of non-active state\n");
361 DPRINTF("iterate\n");
362 if (qemu_savevm_state_iterate(s
->mon
, s
->file
) == 1) {
364 int old_vm_running
= vm_running
;
366 DPRINTF("done iterating\n");
371 if ((qemu_savevm_state_complete(s
->mon
, s
->file
)) < 0) {
372 if (old_vm_running
) {
375 state
= MIG_STATE_ERROR
;
377 state
= MIG_STATE_COMPLETED
;
379 migrate_fd_cleanup(s
);
384 int migrate_fd_get_status(MigrationState
*mig_state
)
386 FdMigrationState
*s
= migrate_to_fms(mig_state
);
390 void migrate_fd_cancel(MigrationState
*mig_state
)
392 FdMigrationState
*s
= migrate_to_fms(mig_state
);
394 if (s
->state
!= MIG_STATE_ACTIVE
)
397 DPRINTF("cancelling migration\n");
399 s
->state
= MIG_STATE_CANCELLED
;
400 qemu_savevm_state_cancel(s
->mon
, s
->file
);
402 migrate_fd_cleanup(s
);
405 void migrate_fd_release(MigrationState
*mig_state
)
407 FdMigrationState
*s
= migrate_to_fms(mig_state
);
409 DPRINTF("releasing state\n");
411 if (s
->state
== MIG_STATE_ACTIVE
) {
412 s
->state
= MIG_STATE_CANCELLED
;
413 migrate_fd_cleanup(s
);
418 void migrate_fd_wait_for_unfreeze(void *opaque
)
420 FdMigrationState
*s
= opaque
;
423 DPRINTF("wait for unfreeze\n");
424 if (s
->state
!= MIG_STATE_ACTIVE
)
431 FD_SET(s
->fd
, &wfds
);
433 ret
= select(s
->fd
+ 1, NULL
, &wfds
, NULL
, NULL
);
434 } while (ret
== -1 && (s
->get_error(s
)) == EINTR
);
437 int migrate_fd_close(void *opaque
)
439 FdMigrationState
*s
= opaque
;
441 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);