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
);
48 fprintf(stderr
, "unknown migration protocol: %s\n", uri
);
51 void do_migrate(int detach
, const char *uri
)
53 MigrationState
*s
= NULL
;
56 if (strstart(uri
, "tcp:", &p
))
57 s
= tcp_start_outgoing_migration(p
, max_throttle
, detach
);
59 else if (strstart(uri
, "exec:", &p
))
60 s
= exec_start_outgoing_migration(p
, max_throttle
, detach
);
63 term_printf("unknown migration protocol: %s\n", uri
);
66 term_printf("migration failed\n");
68 if (current_migration
)
69 current_migration
->release(current_migration
);
71 current_migration
= s
;
75 void do_migrate_cancel(void)
77 MigrationState
*s
= current_migration
;
83 void do_migrate_set_speed(const char *value
)
88 d
= strtod(value
, &ptr
);
100 max_throttle
= (uint32_t)d
;
103 void do_info_migrate(void)
105 MigrationState
*s
= current_migration
;
108 term_printf("Migration status: ");
109 switch (s
->get_status(s
)) {
110 case MIG_STATE_ACTIVE
:
111 term_printf("active\n");
113 case MIG_STATE_COMPLETED
:
114 term_printf("completed\n");
116 case MIG_STATE_ERROR
:
117 term_printf("failed\n");
119 case MIG_STATE_CANCELLED
:
120 term_printf("cancelled\n");
126 /* shared migration helpers */
128 void migrate_fd_error(FdMigrationState
*s
)
130 dprintf("setting error state\n");
131 s
->state
= MIG_STATE_ERROR
;
132 migrate_fd_cleanup(s
);
135 void migrate_fd_cleanup(FdMigrationState
*s
)
137 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
140 dprintf("closing file\n");
141 qemu_fclose(s
->file
);
147 /* Don't resume monitor until we've flushed all of the buffers */
148 if (s
->detach
== 2) {
156 void migrate_fd_put_notify(void *opaque
)
158 FdMigrationState
*s
= opaque
;
160 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
161 qemu_file_put_notify(s
->file
);
164 ssize_t
migrate_fd_put_buffer(void *opaque
, const void *data
, size_t size
)
166 FdMigrationState
*s
= opaque
;
170 ret
= s
->write(s
, data
, size
);
171 } while (ret
== -1 && ((s
->get_error(s
)) == EINTR
));
174 ret
= -(s
->get_error(s
));
177 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, migrate_fd_put_notify
, s
);
182 void migrate_fd_connect(FdMigrationState
*s
)
186 s
->file
= qemu_fopen_ops_buffered(s
,
188 migrate_fd_put_buffer
,
189 migrate_fd_put_ready
,
190 migrate_fd_wait_for_unfreeze
,
193 dprintf("beginning savevm\n");
194 ret
= qemu_savevm_state_begin(s
->file
);
196 dprintf("failed, %d\n", ret
);
201 migrate_fd_put_ready(s
);
204 void migrate_fd_put_ready(void *opaque
)
206 FdMigrationState
*s
= opaque
;
208 if (s
->state
!= MIG_STATE_ACTIVE
) {
209 dprintf("put_ready returning because of non-active state\n");
213 dprintf("iterate\n");
214 if (qemu_savevm_state_iterate(s
->file
) == 1) {
216 int old_vm_running
= vm_running
;
218 dprintf("done iterating\n");
223 if ((qemu_savevm_state_complete(s
->file
)) < 0) {
224 if (old_vm_running
) {
227 state
= MIG_STATE_ERROR
;
229 state
= MIG_STATE_COMPLETED
;
231 migrate_fd_cleanup(s
);
236 int migrate_fd_get_status(MigrationState
*mig_state
)
238 FdMigrationState
*s
= migrate_to_fms(mig_state
);
242 void migrate_fd_cancel(MigrationState
*mig_state
)
244 FdMigrationState
*s
= migrate_to_fms(mig_state
);
246 if (s
->state
!= MIG_STATE_ACTIVE
)
249 dprintf("cancelling migration\n");
251 s
->state
= MIG_STATE_CANCELLED
;
253 migrate_fd_cleanup(s
);
256 void migrate_fd_release(MigrationState
*mig_state
)
258 FdMigrationState
*s
= migrate_to_fms(mig_state
);
260 dprintf("releasing state\n");
262 if (s
->state
== MIG_STATE_ACTIVE
) {
263 s
->state
= MIG_STATE_CANCELLED
;
264 migrate_fd_cleanup(s
);
269 void migrate_fd_wait_for_unfreeze(void *opaque
)
271 FdMigrationState
*s
= opaque
;
274 dprintf("wait for unfreeze\n");
275 if (s
->state
!= MIG_STATE_ACTIVE
)
282 FD_SET(s
->fd
, &wfds
);
284 ret
= select(s
->fd
+ 1, NULL
, &wfds
, NULL
, NULL
);
285 } while (ret
== -1 && (s
->get_error(s
)) == EINTR
);
288 int migrate_fd_close(void *opaque
)
290 FdMigrationState
*s
= opaque
;
292 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);