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 "qemu_socket.h"
16 #include "migration.h"
17 #include "qemu-char.h"
20 #include "buffered_file.h"
23 //#define DEBUG_MIGRATION_TCP
25 typedef struct FdMigrationState
27 MigrationState mig_state
;
29 int64_t bandwidth_limit
;
35 #ifdef DEBUG_MIGRATION_TCP
36 #define dprintf(fmt, ...) \
37 do { printf("migration-tcp: " fmt, ## __VA_ARGS__); } while (0)
39 #define dprintf(fmt, ...) \
45 static void tcp_cleanup(FdMigrationState
*s
)
52 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
56 dprintf("closing file\n");
66 static void tcp_error(FdMigrationState
*s
)
68 dprintf("setting error state\n");
69 s
->state
= MIG_STATE_ERROR
;
73 static void fd_put_notify(void *opaque
)
75 FdMigrationState
*s
= opaque
;
77 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
78 qemu_file_put_notify(s
->file
);
81 static ssize_t
fd_put_buffer(void *opaque
, const void *data
, size_t size
)
83 FdMigrationState
*s
= opaque
;
87 ret
= send(s
->fd
, data
, size
, 0);
88 } while (ret
== -1 && (socket_error() == EINTR
|| socket_error() == EWOULDBLOCK
));
91 ret
= -socket_error();
94 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, fd_put_notify
, s
);
99 static int fd_close(void *opaque
)
101 FdMigrationState
*s
= opaque
;
102 dprintf("fd_close\n");
110 static void fd_wait_for_unfreeze(void *opaque
)
112 FdMigrationState
*s
= opaque
;
115 dprintf("wait for unfreeze\n");
116 if (s
->state
!= MIG_STATE_ACTIVE
)
123 FD_SET(s
->fd
, &wfds
);
125 ret
= select(s
->fd
+ 1, NULL
, &wfds
, NULL
, NULL
);
126 } while (ret
== -1 && socket_error() == EINTR
);
129 static void fd_put_ready(void *opaque
)
131 FdMigrationState
*s
= opaque
;
133 if (s
->state
!= MIG_STATE_ACTIVE
) {
134 dprintf("put_ready returning because of non-active state\n");
138 dprintf("iterate\n");
139 if (qemu_savevm_state_iterate(s
->file
) == 1) {
140 dprintf("done iterating\n");
144 qemu_savevm_state_complete(s
->file
);
145 s
->state
= MIG_STATE_COMPLETED
;
150 static void tcp_connect_migrate(FdMigrationState
*s
)
154 s
->file
= qemu_fopen_ops_buffered(s
,
158 fd_wait_for_unfreeze
,
161 dprintf("beginning savevm\n");
162 ret
= qemu_savevm_state_begin(s
->file
);
164 dprintf("failed, %d\n", ret
);
172 static void tcp_wait_for_connect(void *opaque
)
174 FdMigrationState
*s
= opaque
;
176 socklen_t valsize
= sizeof(val
);
178 dprintf("connect completed\n");
180 ret
= getsockopt(s
->fd
, SOL_SOCKET
, SO_ERROR
, &val
, &valsize
);
181 } while (ret
== -1 && socket_error() == EINTR
);
188 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
191 tcp_connect_migrate(s
);
193 dprintf("error connecting %d\n", val
);
198 static FdMigrationState
*to_fms(MigrationState
*mig_state
)
200 return container_of(mig_state
, FdMigrationState
, mig_state
);
203 static int tcp_get_status(MigrationState
*mig_state
)
205 FdMigrationState
*s
= to_fms(mig_state
);
210 static void tcp_cancel(MigrationState
*mig_state
)
212 FdMigrationState
*s
= to_fms(mig_state
);
214 if (s
->state
!= MIG_STATE_ACTIVE
)
217 dprintf("cancelling migration\n");
219 s
->state
= MIG_STATE_CANCELLED
;
224 static void tcp_release(MigrationState
*mig_state
)
226 FdMigrationState
*s
= to_fms(mig_state
);
228 dprintf("releasing state\n");
230 if (s
->state
== MIG_STATE_ACTIVE
) {
231 s
->state
= MIG_STATE_CANCELLED
;
237 MigrationState
*tcp_start_outgoing_migration(const char *host_port
,
238 int64_t bandwidth_limit
,
241 struct sockaddr_in addr
;
245 if (parse_host_port(&addr
, host_port
) < 0)
248 s
= qemu_mallocz(sizeof(*s
));
252 s
->mig_state
.cancel
= tcp_cancel
;
253 s
->mig_state
.get_status
= tcp_get_status
;
254 s
->mig_state
.release
= tcp_release
;
256 s
->state
= MIG_STATE_ACTIVE
;
258 s
->bandwidth_limit
= bandwidth_limit
;
259 s
->fd
= socket(PF_INET
, SOCK_STREAM
, 0);
265 socket_set_nonblock(s
->fd
);
267 if (s
->detach
== 1) {
268 dprintf("detaching from monitor\n");
274 ret
= connect(s
->fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
276 ret
= -socket_error();
278 if (ret
== -EINPROGRESS
|| ret
== -EWOULDBLOCK
)
279 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, tcp_wait_for_connect
, s
);
280 } while (ret
== -EINTR
);
282 if (ret
< 0 && ret
!= -EINPROGRESS
&& ret
!= -EWOULDBLOCK
) {
283 dprintf("connect failed\n");
288 tcp_connect_migrate(s
);
290 return &s
->mig_state
;
293 static void tcp_accept_incoming_migration(void *opaque
)
295 struct sockaddr_in addr
;
296 socklen_t addrlen
= sizeof(addr
);
297 int s
= (unsigned long)opaque
;
302 c
= accept(s
, (struct sockaddr
*)&addr
, &addrlen
);
303 } while (c
== -1 && socket_error() == EINTR
);
305 dprintf("accepted migration\n");
308 fprintf(stderr
, "could not accept migration connection\n");
312 f
= qemu_fopen_socket(c
);
314 fprintf(stderr
, "could not qemu_fopen socket\n");
318 vm_stop(0); /* just in case */
319 ret
= qemu_loadvm_state(f
);
321 fprintf(stderr
, "load of migration failed\n");
324 qemu_announce_self();
325 dprintf("successfully loaded vm state\n");
327 /* we've successfully migrated, close the server socket */
328 qemu_set_fd_handler2(s
, NULL
, NULL
, NULL
, NULL
);
339 int tcp_start_incoming_migration(const char *host_port
)
341 struct sockaddr_in addr
;
345 if (parse_host_port(&addr
, host_port
) < 0) {
346 fprintf(stderr
, "invalid host/port combination: %s\n", host_port
);
350 s
= socket(PF_INET
, SOCK_STREAM
, 0);
352 return -socket_error();
355 setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
357 if (bind(s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == -1)
360 if (listen(s
, 1) == -1)
363 qemu_set_fd_handler2(s
, NULL
, tcp_accept_incoming_migration
, NULL
,
364 (void *)(unsigned long)s
);
370 return -socket_error();