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"
22 //#define DEBUG_MIGRATION
24 #ifdef DEBUG_MIGRATION
25 #define dprintf(fmt, ...) \
26 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
28 #define dprintf(fmt, ...) \
32 /* Migration speed throttling */
33 static uint32_t max_throttle
= (32 << 20);
35 static MigrationState
*current_migration
;
37 void qemu_start_incoming_migration(const char *uri
)
41 if (strstart(uri
, "tcp:", &p
))
42 tcp_start_incoming_migration(p
);
44 else if (strstart(uri
, "exec:", &p
))
45 exec_start_incoming_migration(p
);
46 else if (strstart(uri
, "unix:", &p
))
47 unix_start_incoming_migration(p
);
48 else if (strstart(uri
, "fd:", &p
))
49 fd_start_incoming_migration(p
);
52 fprintf(stderr
, "unknown migration protocol: %s\n", uri
);
55 void do_migrate(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
57 MigrationState
*s
= NULL
;
59 int detach
= qdict_get_int(qdict
, "detach");
60 const char *uri
= qdict_get_str(qdict
, "uri");
62 if (current_migration
&&
63 current_migration
->get_status(current_migration
) == MIG_STATE_ACTIVE
) {
64 monitor_printf(mon
, "migration already in progress\n");
68 if (strstart(uri
, "tcp:", &p
))
69 s
= tcp_start_outgoing_migration(p
, max_throttle
, detach
,
70 (int)qdict_get_int(qdict
, "blk"),
71 (int)qdict_get_int(qdict
, "inc"));
73 else if (strstart(uri
, "exec:", &p
))
74 s
= exec_start_outgoing_migration(p
, max_throttle
, detach
,
75 (int)qdict_get_int(qdict
, "blk"),
76 (int)qdict_get_int(qdict
, "inc"));
77 else if (strstart(uri
, "unix:", &p
))
78 s
= unix_start_outgoing_migration(p
, max_throttle
, detach
,
79 (int)qdict_get_int(qdict
, "blk"),
80 (int)qdict_get_int(qdict
, "inc"));
81 else if (strstart(uri
, "fd:", &p
))
82 s
= fd_start_outgoing_migration(mon
, p
, max_throttle
, detach
,
83 (int)qdict_get_int(qdict
, "blk"),
84 (int)qdict_get_int(qdict
, "inc"));
87 monitor_printf(mon
, "unknown migration protocol: %s\n", uri
);
90 monitor_printf(mon
, "migration failed\n");
92 if (current_migration
)
93 current_migration
->release(current_migration
);
95 current_migration
= s
;
99 void do_migrate_cancel(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
101 MigrationState
*s
= current_migration
;
107 void do_migrate_set_speed(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
112 const char *value
= qdict_get_str(qdict
, "value");
114 d
= strtod(value
, &ptr
);
126 max_throttle
= (uint32_t)d
;
128 s
= migrate_to_fms(current_migration
);
130 qemu_file_set_rate_limit(s
->file
, max_throttle
);
134 /* amount of nanoseconds we are willing to wait for migration to be down.
135 * the choice of nanoseconds is because it is the maximum resolution that
136 * get_clock() can achieve. It is an internal measure. All user-visible
137 * units must be in seconds */
138 static uint64_t max_downtime
= 30000000;
140 uint64_t migrate_max_downtime(void)
145 void do_migrate_set_downtime(Monitor
*mon
, const QDict
*qdict
)
149 const char *value
= qdict_get_str(qdict
, "value");
151 d
= strtod(value
, &ptr
);
152 if (!strcmp(ptr
,"ms")) {
154 } else if (!strcmp(ptr
,"us")) {
156 } else if (!strcmp(ptr
,"ns")) {
158 /* all else considered to be seconds */
162 max_downtime
= (uint64_t)d
;
165 void do_info_migrate(Monitor
*mon
)
167 MigrationState
*s
= current_migration
;
170 monitor_printf(mon
, "Migration status: ");
171 switch (s
->get_status(s
)) {
172 case MIG_STATE_ACTIVE
:
173 monitor_printf(mon
, "active\n");
174 monitor_printf(mon
, "transferred ram: %" PRIu64
" kbytes\n", ram_bytes_transferred() >> 10);
175 monitor_printf(mon
, "remaining ram: %" PRIu64
" kbytes\n", ram_bytes_remaining() >> 10);
176 monitor_printf(mon
, "total ram: %" PRIu64
" kbytes\n", ram_bytes_total() >> 10);
178 case MIG_STATE_COMPLETED
:
179 monitor_printf(mon
, "completed\n");
181 case MIG_STATE_ERROR
:
182 monitor_printf(mon
, "failed\n");
184 case MIG_STATE_CANCELLED
:
185 monitor_printf(mon
, "cancelled\n");
191 /* shared migration helpers */
193 void migrate_fd_monitor_suspend(FdMigrationState
*s
)
195 s
->mon_resume
= cur_mon
;
196 if (monitor_suspend(cur_mon
) == 0)
197 dprintf("suspending monitor\n");
199 monitor_printf(cur_mon
, "terminal does not allow synchronous "
200 "migration, continuing detached\n");
203 void migrate_fd_error(FdMigrationState
*s
)
205 dprintf("setting error state\n");
206 s
->state
= MIG_STATE_ERROR
;
207 migrate_fd_cleanup(s
);
210 void migrate_fd_cleanup(FdMigrationState
*s
)
212 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
215 dprintf("closing file\n");
216 qemu_fclose(s
->file
);
223 /* Don't resume monitor until we've flushed all of the buffers */
225 monitor_resume(s
->mon_resume
);
230 void migrate_fd_put_notify(void *opaque
)
232 FdMigrationState
*s
= opaque
;
234 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
235 qemu_file_put_notify(s
->file
);
238 ssize_t
migrate_fd_put_buffer(void *opaque
, const void *data
, size_t size
)
240 FdMigrationState
*s
= opaque
;
244 ret
= s
->write(s
, data
, size
);
245 } while (ret
== -1 && ((s
->get_error(s
)) == EINTR
));
248 ret
= -(s
->get_error(s
));
251 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, migrate_fd_put_notify
, s
);
256 void migrate_fd_connect(FdMigrationState
*s
)
260 s
->file
= qemu_fopen_ops_buffered(s
,
262 migrate_fd_put_buffer
,
263 migrate_fd_put_ready
,
264 migrate_fd_wait_for_unfreeze
,
267 dprintf("beginning savevm\n");
268 ret
= qemu_savevm_state_begin(s
->file
, s
->mig_state
.blk
,
269 s
->mig_state
.shared
);
271 dprintf("failed, %d\n", ret
);
276 migrate_fd_put_ready(s
);
279 void migrate_fd_put_ready(void *opaque
)
281 FdMigrationState
*s
= opaque
;
283 if (s
->state
!= MIG_STATE_ACTIVE
) {
284 dprintf("put_ready returning because of non-active state\n");
288 dprintf("iterate\n");
289 if (qemu_savevm_state_iterate(s
->file
) == 1) {
291 int old_vm_running
= vm_running
;
293 dprintf("done iterating\n");
298 if ((qemu_savevm_state_complete(s
->file
)) < 0) {
299 if (old_vm_running
) {
302 state
= MIG_STATE_ERROR
;
304 state
= MIG_STATE_COMPLETED
;
306 migrate_fd_cleanup(s
);
311 int migrate_fd_get_status(MigrationState
*mig_state
)
313 FdMigrationState
*s
= migrate_to_fms(mig_state
);
317 void migrate_fd_cancel(MigrationState
*mig_state
)
319 FdMigrationState
*s
= migrate_to_fms(mig_state
);
321 if (s
->state
!= MIG_STATE_ACTIVE
)
324 dprintf("cancelling migration\n");
326 s
->state
= MIG_STATE_CANCELLED
;
327 qemu_savevm_state_cancel(s
->file
);
329 migrate_fd_cleanup(s
);
332 void migrate_fd_release(MigrationState
*mig_state
)
334 FdMigrationState
*s
= migrate_to_fms(mig_state
);
336 dprintf("releasing state\n");
338 if (s
->state
== MIG_STATE_ACTIVE
) {
339 s
->state
= MIG_STATE_CANCELLED
;
340 migrate_fd_cleanup(s
);
345 void migrate_fd_wait_for_unfreeze(void *opaque
)
347 FdMigrationState
*s
= opaque
;
350 dprintf("wait for unfreeze\n");
351 if (s
->state
!= MIG_STATE_ACTIVE
)
358 FD_SET(s
->fd
, &wfds
);
360 ret
= select(s
->fd
+ 1, NULL
, &wfds
, NULL
, NULL
);
361 } while (ret
== -1 && (s
->get_error(s
)) == EINTR
);
364 int migrate_fd_close(void *opaque
)
366 FdMigrationState
*s
= opaque
;
368 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);