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
);
50 fprintf(stderr
, "unknown migration protocol: %s\n", uri
);
53 void do_migrate(Monitor
*mon
, int detach
, const char *uri
)
55 MigrationState
*s
= NULL
;
58 if (strstart(uri
, "tcp:", &p
))
59 s
= tcp_start_outgoing_migration(p
, max_throttle
, detach
);
61 else if (strstart(uri
, "exec:", &p
))
62 s
= exec_start_outgoing_migration(p
, max_throttle
, detach
);
63 else if (strstart(uri
, "unix:", &p
))
64 s
= unix_start_outgoing_migration(p
, max_throttle
, detach
);
67 monitor_printf(mon
, "unknown migration protocol: %s\n", uri
);
70 monitor_printf(mon
, "migration failed\n");
72 if (current_migration
)
73 current_migration
->release(current_migration
);
75 current_migration
= s
;
79 void do_migrate_cancel(Monitor
*mon
)
81 MigrationState
*s
= current_migration
;
87 void do_migrate_set_speed(Monitor
*mon
, const char *value
)
93 d
= strtod(value
, &ptr
);
105 max_throttle
= (uint32_t)d
;
106 s
= migrate_to_fms(current_migration
);
109 qemu_file_set_rate_limit(s
->file
, max_throttle
);
114 /* amount of nanoseconds we are willing to wait for migration to be down.
115 * the choice of nanoseconds is because it is the maximum resolution that
116 * get_clock() can achieve. It is an internal measure. All user-visible
117 * units must be in seconds */
118 static uint64_t max_downtime
= 30000000;
120 uint64_t migrate_max_downtime(void)
125 void do_migrate_set_downtime(Monitor
*mon
, const char *value
)
130 d
= strtod(value
, &ptr
);
131 if (!strcmp(ptr
,"ms")) {
133 } else if (!strcmp(ptr
,"us")) {
135 } else if (!strcmp(ptr
,"ns")) {
137 /* all else considered to be seconds */
141 max_downtime
= (uint64_t)d
;
144 void do_info_migrate(Monitor
*mon
)
146 MigrationState
*s
= current_migration
;
149 monitor_printf(mon
, "Migration status: ");
150 switch (s
->get_status(s
)) {
151 case MIG_STATE_ACTIVE
:
152 monitor_printf(mon
, "active\n");
153 monitor_printf(mon
, "transferred ram: %" PRIu64
" kbytes\n", ram_bytes_transferred() >> 10);
154 monitor_printf(mon
, "remaining ram: %" PRIu64
" kbytes\n", ram_bytes_remaining() >> 10);
155 monitor_printf(mon
, "total ram: %" PRIu64
" kbytes\n", ram_bytes_total() >> 10);
157 case MIG_STATE_COMPLETED
:
158 monitor_printf(mon
, "completed\n");
160 case MIG_STATE_ERROR
:
161 monitor_printf(mon
, "failed\n");
163 case MIG_STATE_CANCELLED
:
164 monitor_printf(mon
, "cancelled\n");
170 /* shared migration helpers */
172 void migrate_fd_monitor_suspend(FdMigrationState
*s
)
174 s
->mon_resume
= cur_mon
;
175 if (monitor_suspend(cur_mon
) == 0)
176 dprintf("suspending monitor\n");
178 monitor_printf(cur_mon
, "terminal does not allow synchronous "
179 "migration, continuing detached\n");
182 void migrate_fd_error(FdMigrationState
*s
)
184 dprintf("setting error state\n");
185 s
->state
= MIG_STATE_ERROR
;
186 migrate_fd_cleanup(s
);
189 void migrate_fd_cleanup(FdMigrationState
*s
)
191 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
194 dprintf("closing file\n");
195 qemu_fclose(s
->file
);
201 /* Don't resume monitor until we've flushed all of the buffers */
203 monitor_resume(s
->mon_resume
);
208 void migrate_fd_put_notify(void *opaque
)
210 FdMigrationState
*s
= opaque
;
212 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
213 qemu_file_put_notify(s
->file
);
216 ssize_t
migrate_fd_put_buffer(void *opaque
, const void *data
, size_t size
)
218 FdMigrationState
*s
= opaque
;
222 ret
= s
->write(s
, data
, size
);
223 } while (ret
== -1 && ((s
->get_error(s
)) == EINTR
));
226 ret
= -(s
->get_error(s
));
229 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, migrate_fd_put_notify
, s
);
234 void migrate_fd_connect(FdMigrationState
*s
)
238 s
->file
= qemu_fopen_ops_buffered(s
,
240 migrate_fd_put_buffer
,
241 migrate_fd_put_ready
,
242 migrate_fd_wait_for_unfreeze
,
245 dprintf("beginning savevm\n");
246 ret
= qemu_savevm_state_begin(s
->file
);
248 dprintf("failed, %d\n", ret
);
253 migrate_fd_put_ready(s
);
256 void migrate_fd_put_ready(void *opaque
)
258 FdMigrationState
*s
= opaque
;
260 if (s
->state
!= MIG_STATE_ACTIVE
) {
261 dprintf("put_ready returning because of non-active state\n");
265 dprintf("iterate\n");
266 if (qemu_savevm_state_iterate(s
->file
) == 1) {
268 int old_vm_running
= vm_running
;
270 dprintf("done iterating\n");
275 if ((qemu_savevm_state_complete(s
->file
)) < 0) {
276 if (old_vm_running
) {
279 state
= MIG_STATE_ERROR
;
281 state
= MIG_STATE_COMPLETED
;
283 migrate_fd_cleanup(s
);
288 int migrate_fd_get_status(MigrationState
*mig_state
)
290 FdMigrationState
*s
= migrate_to_fms(mig_state
);
294 void migrate_fd_cancel(MigrationState
*mig_state
)
296 FdMigrationState
*s
= migrate_to_fms(mig_state
);
298 if (s
->state
!= MIG_STATE_ACTIVE
)
301 dprintf("cancelling migration\n");
303 s
->state
= MIG_STATE_CANCELLED
;
305 migrate_fd_cleanup(s
);
308 void migrate_fd_release(MigrationState
*mig_state
)
310 FdMigrationState
*s
= migrate_to_fms(mig_state
);
312 dprintf("releasing state\n");
314 if (s
->state
== MIG_STATE_ACTIVE
) {
315 s
->state
= MIG_STATE_CANCELLED
;
316 migrate_fd_cleanup(s
);
321 void migrate_fd_wait_for_unfreeze(void *opaque
)
323 FdMigrationState
*s
= opaque
;
326 dprintf("wait for unfreeze\n");
327 if (s
->state
!= MIG_STATE_ACTIVE
)
334 FD_SET(s
->fd
, &wfds
);
336 ret
= select(s
->fd
+ 1, NULL
, &wfds
, NULL
, NULL
);
337 } while (ret
== -1 && (s
->get_error(s
)) == EINTR
);
340 int migrate_fd_close(void *opaque
)
342 FdMigrationState
*s
= opaque
;
344 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);