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
, int detach
, const char *uri
)
57 MigrationState
*s
= NULL
;
60 if (strstart(uri
, "tcp:", &p
))
61 s
= tcp_start_outgoing_migration(p
, max_throttle
, detach
);
63 else if (strstart(uri
, "exec:", &p
))
64 s
= exec_start_outgoing_migration(p
, max_throttle
, detach
);
65 else if (strstart(uri
, "unix:", &p
))
66 s
= unix_start_outgoing_migration(p
, max_throttle
, detach
);
67 else if (strstart(uri
, "fd:", &p
))
68 s
= fd_start_outgoing_migration(mon
, p
, max_throttle
, detach
);
71 monitor_printf(mon
, "unknown migration protocol: %s\n", uri
);
74 monitor_printf(mon
, "migration failed\n");
76 if (current_migration
)
77 current_migration
->release(current_migration
);
79 current_migration
= s
;
83 void do_migrate_cancel(Monitor
*mon
)
85 MigrationState
*s
= current_migration
;
91 void do_migrate_set_speed(Monitor
*mon
, const char *value
)
97 d
= strtod(value
, &ptr
);
109 max_throttle
= (uint32_t)d
;
110 s
= migrate_to_fms(current_migration
);
113 qemu_file_set_rate_limit(s
->file
, max_throttle
);
118 /* amount of nanoseconds we are willing to wait for migration to be down.
119 * the choice of nanoseconds is because it is the maximum resolution that
120 * get_clock() can achieve. It is an internal measure. All user-visible
121 * units must be in seconds */
122 static uint64_t max_downtime
= 30000000;
124 uint64_t migrate_max_downtime(void)
129 void do_migrate_set_downtime(Monitor
*mon
, const char *value
)
134 d
= strtod(value
, &ptr
);
135 if (!strcmp(ptr
,"ms")) {
137 } else if (!strcmp(ptr
,"us")) {
139 } else if (!strcmp(ptr
,"ns")) {
141 /* all else considered to be seconds */
145 max_downtime
= (uint64_t)d
;
148 void do_info_migrate(Monitor
*mon
)
150 MigrationState
*s
= current_migration
;
153 monitor_printf(mon
, "Migration status: ");
154 switch (s
->get_status(s
)) {
155 case MIG_STATE_ACTIVE
:
156 monitor_printf(mon
, "active\n");
157 monitor_printf(mon
, "transferred ram: %" PRIu64
" kbytes\n", ram_bytes_transferred() >> 10);
158 monitor_printf(mon
, "remaining ram: %" PRIu64
" kbytes\n", ram_bytes_remaining() >> 10);
159 monitor_printf(mon
, "total ram: %" PRIu64
" kbytes\n", ram_bytes_total() >> 10);
161 case MIG_STATE_COMPLETED
:
162 monitor_printf(mon
, "completed\n");
164 case MIG_STATE_ERROR
:
165 monitor_printf(mon
, "failed\n");
167 case MIG_STATE_CANCELLED
:
168 monitor_printf(mon
, "cancelled\n");
174 /* shared migration helpers */
176 void migrate_fd_monitor_suspend(FdMigrationState
*s
)
178 s
->mon_resume
= cur_mon
;
179 if (monitor_suspend(cur_mon
) == 0)
180 dprintf("suspending monitor\n");
182 monitor_printf(cur_mon
, "terminal does not allow synchronous "
183 "migration, continuing detached\n");
186 void migrate_fd_error(FdMigrationState
*s
)
188 dprintf("setting error state\n");
189 s
->state
= MIG_STATE_ERROR
;
190 migrate_fd_cleanup(s
);
193 void migrate_fd_cleanup(FdMigrationState
*s
)
195 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
198 dprintf("closing file\n");
199 qemu_fclose(s
->file
);
205 /* Don't resume monitor until we've flushed all of the buffers */
207 monitor_resume(s
->mon_resume
);
212 void migrate_fd_put_notify(void *opaque
)
214 FdMigrationState
*s
= opaque
;
216 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
217 qemu_file_put_notify(s
->file
);
220 ssize_t
migrate_fd_put_buffer(void *opaque
, const void *data
, size_t size
)
222 FdMigrationState
*s
= opaque
;
226 ret
= s
->write(s
, data
, size
);
227 } while (ret
== -1 && ((s
->get_error(s
)) == EINTR
));
230 ret
= -(s
->get_error(s
));
233 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, migrate_fd_put_notify
, s
);
238 void migrate_fd_connect(FdMigrationState
*s
)
242 s
->file
= qemu_fopen_ops_buffered(s
,
244 migrate_fd_put_buffer
,
245 migrate_fd_put_ready
,
246 migrate_fd_wait_for_unfreeze
,
249 dprintf("beginning savevm\n");
250 ret
= qemu_savevm_state_begin(s
->file
);
252 dprintf("failed, %d\n", ret
);
257 migrate_fd_put_ready(s
);
260 void migrate_fd_put_ready(void *opaque
)
262 FdMigrationState
*s
= opaque
;
264 if (s
->state
!= MIG_STATE_ACTIVE
) {
265 dprintf("put_ready returning because of non-active state\n");
269 dprintf("iterate\n");
270 if (qemu_savevm_state_iterate(s
->file
) == 1) {
272 int old_vm_running
= vm_running
;
274 dprintf("done iterating\n");
279 if ((qemu_savevm_state_complete(s
->file
)) < 0) {
280 if (old_vm_running
) {
283 state
= MIG_STATE_ERROR
;
285 state
= MIG_STATE_COMPLETED
;
287 migrate_fd_cleanup(s
);
292 int migrate_fd_get_status(MigrationState
*mig_state
)
294 FdMigrationState
*s
= migrate_to_fms(mig_state
);
298 void migrate_fd_cancel(MigrationState
*mig_state
)
300 FdMigrationState
*s
= migrate_to_fms(mig_state
);
302 if (s
->state
!= MIG_STATE_ACTIVE
)
305 dprintf("cancelling migration\n");
307 s
->state
= MIG_STATE_CANCELLED
;
309 migrate_fd_cleanup(s
);
312 void migrate_fd_release(MigrationState
*mig_state
)
314 FdMigrationState
*s
= migrate_to_fms(mig_state
);
316 dprintf("releasing state\n");
318 if (s
->state
== MIG_STATE_ACTIVE
) {
319 s
->state
= MIG_STATE_CANCELLED
;
320 migrate_fd_cleanup(s
);
325 void migrate_fd_wait_for_unfreeze(void *opaque
)
327 FdMigrationState
*s
= opaque
;
330 dprintf("wait for unfreeze\n");
331 if (s
->state
!= MIG_STATE_ACTIVE
)
338 FD_SET(s
->fd
, &wfds
);
340 ret
= select(s
->fd
+ 1, NULL
, &wfds
, NULL
, NULL
);
341 } while (ret
== -1 && (s
->get_error(s
)) == EINTR
);
344 int migrate_fd_close(void *opaque
)
346 FdMigrationState
*s
= opaque
;
348 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);