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
,
64 (int)qdict_get_int(qdict
, "blk"),
65 (int)qdict_get_int(qdict
, "inc"));
67 else if (strstart(uri
, "exec:", &p
))
68 s
= exec_start_outgoing_migration(p
, max_throttle
, detach
,
69 (int)qdict_get_int(qdict
, "blk"),
70 (int)qdict_get_int(qdict
, "inc"));
71 else if (strstart(uri
, "unix:", &p
))
72 s
= unix_start_outgoing_migration(p
, max_throttle
, detach
,
73 (int)qdict_get_int(qdict
, "blk"),
74 (int)qdict_get_int(qdict
, "inc"));
75 else if (strstart(uri
, "fd:", &p
))
76 s
= fd_start_outgoing_migration(mon
, p
, max_throttle
, detach
,
77 (int)qdict_get_int(qdict
, "blk"),
78 (int)qdict_get_int(qdict
, "inc"));
81 monitor_printf(mon
, "unknown migration protocol: %s\n", uri
);
84 monitor_printf(mon
, "migration failed\n");
86 if (current_migration
)
87 current_migration
->release(current_migration
);
89 current_migration
= s
;
93 void do_migrate_cancel(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
95 MigrationState
*s
= current_migration
;
101 void do_migrate_set_speed(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
106 const char *value
= qdict_get_str(qdict
, "value");
108 d
= strtod(value
, &ptr
);
120 max_throttle
= (uint32_t)d
;
121 s
= migrate_to_fms(current_migration
);
124 qemu_file_set_rate_limit(s
->file
, max_throttle
);
129 /* amount of nanoseconds we are willing to wait for migration to be down.
130 * the choice of nanoseconds is because it is the maximum resolution that
131 * get_clock() can achieve. It is an internal measure. All user-visible
132 * units must be in seconds */
133 static uint64_t max_downtime
= 30000000;
135 uint64_t migrate_max_downtime(void)
140 void do_migrate_set_downtime(Monitor
*mon
, const QDict
*qdict
)
144 const char *value
= qdict_get_str(qdict
, "value");
146 d
= strtod(value
, &ptr
);
147 if (!strcmp(ptr
,"ms")) {
149 } else if (!strcmp(ptr
,"us")) {
151 } else if (!strcmp(ptr
,"ns")) {
153 /* all else considered to be seconds */
157 max_downtime
= (uint64_t)d
;
160 void do_info_migrate(Monitor
*mon
)
162 MigrationState
*s
= current_migration
;
165 monitor_printf(mon
, "Migration status: ");
166 switch (s
->get_status(s
)) {
167 case MIG_STATE_ACTIVE
:
168 monitor_printf(mon
, "active\n");
169 monitor_printf(mon
, "transferred ram: %" PRIu64
" kbytes\n", ram_bytes_transferred() >> 10);
170 monitor_printf(mon
, "remaining ram: %" PRIu64
" kbytes\n", ram_bytes_remaining() >> 10);
171 monitor_printf(mon
, "total ram: %" PRIu64
" kbytes\n", ram_bytes_total() >> 10);
173 case MIG_STATE_COMPLETED
:
174 monitor_printf(mon
, "completed\n");
176 case MIG_STATE_ERROR
:
177 monitor_printf(mon
, "failed\n");
179 case MIG_STATE_CANCELLED
:
180 monitor_printf(mon
, "cancelled\n");
186 /* shared migration helpers */
188 void migrate_fd_monitor_suspend(FdMigrationState
*s
)
190 s
->mon_resume
= cur_mon
;
191 if (monitor_suspend(cur_mon
) == 0)
192 dprintf("suspending monitor\n");
194 monitor_printf(cur_mon
, "terminal does not allow synchronous "
195 "migration, continuing detached\n");
198 void migrate_fd_error(FdMigrationState
*s
)
200 dprintf("setting error state\n");
201 s
->state
= MIG_STATE_ERROR
;
202 migrate_fd_cleanup(s
);
205 void migrate_fd_cleanup(FdMigrationState
*s
)
207 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
210 dprintf("closing file\n");
211 qemu_fclose(s
->file
);
217 /* Don't resume monitor until we've flushed all of the buffers */
219 monitor_resume(s
->mon_resume
);
224 void migrate_fd_put_notify(void *opaque
)
226 FdMigrationState
*s
= opaque
;
228 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
229 qemu_file_put_notify(s
->file
);
232 ssize_t
migrate_fd_put_buffer(void *opaque
, const void *data
, size_t size
)
234 FdMigrationState
*s
= opaque
;
238 ret
= s
->write(s
, data
, size
);
239 } while (ret
== -1 && ((s
->get_error(s
)) == EINTR
));
242 ret
= -(s
->get_error(s
));
245 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, migrate_fd_put_notify
, s
);
250 void migrate_fd_connect(FdMigrationState
*s
)
254 s
->file
= qemu_fopen_ops_buffered(s
,
256 migrate_fd_put_buffer
,
257 migrate_fd_put_ready
,
258 migrate_fd_wait_for_unfreeze
,
261 dprintf("beginning savevm\n");
262 ret
= qemu_savevm_state_begin(s
->file
, s
->mig_state
.blk
,
263 s
->mig_state
.shared
);
265 dprintf("failed, %d\n", ret
);
270 migrate_fd_put_ready(s
);
273 void migrate_fd_put_ready(void *opaque
)
275 FdMigrationState
*s
= opaque
;
277 if (s
->state
!= MIG_STATE_ACTIVE
) {
278 dprintf("put_ready returning because of non-active state\n");
282 dprintf("iterate\n");
283 if (qemu_savevm_state_iterate(s
->file
) == 1) {
285 int old_vm_running
= vm_running
;
287 dprintf("done iterating\n");
292 if ((qemu_savevm_state_complete(s
->file
)) < 0) {
293 if (old_vm_running
) {
296 state
= MIG_STATE_ERROR
;
298 state
= MIG_STATE_COMPLETED
;
300 migrate_fd_cleanup(s
);
305 int migrate_fd_get_status(MigrationState
*mig_state
)
307 FdMigrationState
*s
= migrate_to_fms(mig_state
);
311 void migrate_fd_cancel(MigrationState
*mig_state
)
313 FdMigrationState
*s
= migrate_to_fms(mig_state
);
315 if (s
->state
!= MIG_STATE_ACTIVE
)
318 dprintf("cancelling migration\n");
320 s
->state
= MIG_STATE_CANCELLED
;
322 migrate_fd_cleanup(s
);
325 void migrate_fd_release(MigrationState
*mig_state
)
327 FdMigrationState
*s
= migrate_to_fms(mig_state
);
329 dprintf("releasing state\n");
331 if (s
->state
== MIG_STATE_ACTIVE
) {
332 s
->state
= MIG_STATE_CANCELLED
;
333 migrate_fd_cleanup(s
);
338 void migrate_fd_wait_for_unfreeze(void *opaque
)
340 FdMigrationState
*s
= opaque
;
343 dprintf("wait for unfreeze\n");
344 if (s
->state
!= MIG_STATE_ACTIVE
)
351 FD_SET(s
->fd
, &wfds
);
353 ret
= select(s
->fd
+ 1, NULL
, &wfds
, NULL
, NULL
);
354 } while (ret
== -1 && (s
->get_error(s
)) == EINTR
);
357 int migrate_fd_close(void *opaque
)
359 FdMigrationState
*s
= opaque
;
361 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);