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 (strstart(uri
, "tcp:", &p
))
63 s
= tcp_start_outgoing_migration(p
, max_throttle
, detach
);
65 else if (strstart(uri
, "exec:", &p
))
66 s
= exec_start_outgoing_migration(p
, max_throttle
, detach
);
67 else if (strstart(uri
, "unix:", &p
))
68 s
= unix_start_outgoing_migration(p
, max_throttle
, detach
);
69 else if (strstart(uri
, "fd:", &p
))
70 s
= fd_start_outgoing_migration(mon
, p
, max_throttle
, detach
);
73 monitor_printf(mon
, "unknown migration protocol: %s\n", uri
);
76 monitor_printf(mon
, "migration failed\n");
78 if (current_migration
)
79 current_migration
->release(current_migration
);
81 current_migration
= s
;
85 void do_migrate_cancel(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
87 MigrationState
*s
= current_migration
;
93 void do_migrate_set_speed(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
98 const char *value
= qdict_get_str(qdict
, "value");
100 d
= strtod(value
, &ptr
);
112 max_throttle
= (uint32_t)d
;
113 s
= migrate_to_fms(current_migration
);
116 qemu_file_set_rate_limit(s
->file
, max_throttle
);
121 /* amount of nanoseconds we are willing to wait for migration to be down.
122 * the choice of nanoseconds is because it is the maximum resolution that
123 * get_clock() can achieve. It is an internal measure. All user-visible
124 * units must be in seconds */
125 static uint64_t max_downtime
= 30000000;
127 uint64_t migrate_max_downtime(void)
132 void do_migrate_set_downtime(Monitor
*mon
, const QDict
*qdict
)
136 const char *value
= qdict_get_str(qdict
, "value");
138 d
= strtod(value
, &ptr
);
139 if (!strcmp(ptr
,"ms")) {
141 } else if (!strcmp(ptr
,"us")) {
143 } else if (!strcmp(ptr
,"ns")) {
145 /* all else considered to be seconds */
149 max_downtime
= (uint64_t)d
;
152 void do_info_migrate(Monitor
*mon
)
154 MigrationState
*s
= current_migration
;
157 monitor_printf(mon
, "Migration status: ");
158 switch (s
->get_status(s
)) {
159 case MIG_STATE_ACTIVE
:
160 monitor_printf(mon
, "active\n");
161 monitor_printf(mon
, "transferred ram: %" PRIu64
" kbytes\n", ram_bytes_transferred() >> 10);
162 monitor_printf(mon
, "remaining ram: %" PRIu64
" kbytes\n", ram_bytes_remaining() >> 10);
163 monitor_printf(mon
, "total ram: %" PRIu64
" kbytes\n", ram_bytes_total() >> 10);
165 case MIG_STATE_COMPLETED
:
166 monitor_printf(mon
, "completed\n");
168 case MIG_STATE_ERROR
:
169 monitor_printf(mon
, "failed\n");
171 case MIG_STATE_CANCELLED
:
172 monitor_printf(mon
, "cancelled\n");
178 /* shared migration helpers */
180 void migrate_fd_monitor_suspend(FdMigrationState
*s
)
182 s
->mon_resume
= cur_mon
;
183 if (monitor_suspend(cur_mon
) == 0)
184 dprintf("suspending monitor\n");
186 monitor_printf(cur_mon
, "terminal does not allow synchronous "
187 "migration, continuing detached\n");
190 void migrate_fd_error(FdMigrationState
*s
)
192 dprintf("setting error state\n");
193 s
->state
= MIG_STATE_ERROR
;
194 migrate_fd_cleanup(s
);
197 void migrate_fd_cleanup(FdMigrationState
*s
)
199 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
202 dprintf("closing file\n");
203 qemu_fclose(s
->file
);
209 /* Don't resume monitor until we've flushed all of the buffers */
211 monitor_resume(s
->mon_resume
);
216 void migrate_fd_put_notify(void *opaque
)
218 FdMigrationState
*s
= opaque
;
220 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
221 qemu_file_put_notify(s
->file
);
224 ssize_t
migrate_fd_put_buffer(void *opaque
, const void *data
, size_t size
)
226 FdMigrationState
*s
= opaque
;
230 ret
= s
->write(s
, data
, size
);
231 } while (ret
== -1 && ((s
->get_error(s
)) == EINTR
));
234 ret
= -(s
->get_error(s
));
237 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, migrate_fd_put_notify
, s
);
242 void migrate_fd_connect(FdMigrationState
*s
)
246 s
->file
= qemu_fopen_ops_buffered(s
,
248 migrate_fd_put_buffer
,
249 migrate_fd_put_ready
,
250 migrate_fd_wait_for_unfreeze
,
253 dprintf("beginning savevm\n");
254 ret
= qemu_savevm_state_begin(s
->file
);
256 dprintf("failed, %d\n", ret
);
261 migrate_fd_put_ready(s
);
264 void migrate_fd_put_ready(void *opaque
)
266 FdMigrationState
*s
= opaque
;
268 if (s
->state
!= MIG_STATE_ACTIVE
) {
269 dprintf("put_ready returning because of non-active state\n");
273 dprintf("iterate\n");
274 if (qemu_savevm_state_iterate(s
->file
) == 1) {
276 int old_vm_running
= vm_running
;
278 dprintf("done iterating\n");
283 if ((qemu_savevm_state_complete(s
->file
)) < 0) {
284 if (old_vm_running
) {
287 state
= MIG_STATE_ERROR
;
289 state
= MIG_STATE_COMPLETED
;
291 migrate_fd_cleanup(s
);
296 int migrate_fd_get_status(MigrationState
*mig_state
)
298 FdMigrationState
*s
= migrate_to_fms(mig_state
);
302 void migrate_fd_cancel(MigrationState
*mig_state
)
304 FdMigrationState
*s
= migrate_to_fms(mig_state
);
306 if (s
->state
!= MIG_STATE_ACTIVE
)
309 dprintf("cancelling migration\n");
311 s
->state
= MIG_STATE_CANCELLED
;
313 migrate_fd_cleanup(s
);
316 void migrate_fd_release(MigrationState
*mig_state
)
318 FdMigrationState
*s
= migrate_to_fms(mig_state
);
320 dprintf("releasing state\n");
322 if (s
->state
== MIG_STATE_ACTIVE
) {
323 s
->state
= MIG_STATE_CANCELLED
;
324 migrate_fd_cleanup(s
);
329 void migrate_fd_wait_for_unfreeze(void *opaque
)
331 FdMigrationState
*s
= opaque
;
334 dprintf("wait for unfreeze\n");
335 if (s
->state
!= MIG_STATE_ACTIVE
)
342 FD_SET(s
->fd
, &wfds
);
344 ret
= select(s
->fd
+ 1, NULL
, &wfds
, NULL
, NULL
);
345 } while (ret
== -1 && (s
->get_error(s
)) == EINTR
);
348 int migrate_fd_close(void *opaque
)
350 FdMigrationState
*s
= opaque
;
352 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);