4 * Copyright IBM, Corp. 2008
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, ...) \
43 static void tcp_cleanup(FdMigrationState *s)
45 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
48 dprintf("closing file\n");
55 /* Don't resume monitor until we've flushed all of the buffers */
64 static void tcp_error(FdMigrationState *s)
66 dprintf("setting error state\n");
67 s->state = MIG_STATE_ERROR;
71 static void fd_put_notify(void *opaque)
73 FdMigrationState *s = opaque;
75 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
76 qemu_file_put_notify(s->file);
79 static ssize_t fd_put_buffer(void *opaque, const void *data, size_t size)
81 FdMigrationState *s = opaque;
85 ret = send(s->fd, data, size, 0);
86 } while (ret == -1 && (socket_error() == EINTR || socket_error() == EWOULDBLOCK));
89 ret = -socket_error();
92 qemu_set_fd_handler2(s->fd, NULL, NULL, fd_put_notify, s);
97 static int fd_close(void *opaque)
99 FdMigrationState *s = opaque;
100 dprintf("fd_close\n");
108 static void fd_wait_for_unfreeze(void *opaque)
110 FdMigrationState *s = opaque;
113 dprintf("wait for unfreeze\n");
114 if (s->state != MIG_STATE_ACTIVE)
121 FD_SET(s->fd, &wfds);
123 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
124 } while (ret == -1 && socket_error() == EINTR);
127 static void fd_put_ready(void *opaque)
129 FdMigrationState *s = opaque;
131 if (s->state != MIG_STATE_ACTIVE) {
132 dprintf("put_ready returning because of non-active state\n");
136 dprintf("iterate\n");
137 if (qemu_savevm_state_iterate(s->file) == 1) {
138 dprintf("done iterating\n");
142 qemu_savevm_state_complete(s->file);
143 s->state = MIG_STATE_COMPLETED;
148 static void tcp_connect_migrate(FdMigrationState *s)
152 s->file = qemu_fopen_ops_buffered(s,
156 fd_wait_for_unfreeze,
159 dprintf("beginning savevm\n");
160 ret = qemu_savevm_state_begin(s->file);
162 dprintf("failed, %d\n", ret);
170 static void tcp_wait_for_connect(void *opaque)
172 FdMigrationState *s = opaque;
174 socklen_t valsize = sizeof(val);
176 dprintf("connect completed\n");
178 ret = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, &val, &valsize);
179 } while (ret == -1 && socket_error() == EINTR);
186 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
189 tcp_connect_migrate(s);
191 dprintf("error connecting %d\n", val);
196 static FdMigrationState *to_fms(MigrationState *mig_state)
198 return container_of(mig_state, FdMigrationState, mig_state);
201 static int tcp_get_status(MigrationState *mig_state)
203 FdMigrationState *s = to_fms(mig_state);
208 static void tcp_cancel(MigrationState *mig_state)
210 FdMigrationState *s = to_fms(mig_state);
212 if (s->state != MIG_STATE_ACTIVE)
215 dprintf("cancelling migration\n");
217 s->state = MIG_STATE_CANCELLED;
222 static void tcp_release(MigrationState *mig_state)
224 FdMigrationState *s = to_fms(mig_state);
226 dprintf("releasing state\n");
228 if (s->state == MIG_STATE_ACTIVE) {
229 s->state = MIG_STATE_CANCELLED;
235 MigrationState *tcp_start_outgoing_migration(const char *host_port,
236 int64_t bandwidth_limit,
239 struct sockaddr_in addr;
243 if (parse_host_port(&addr, host_port) < 0)
246 s = qemu_mallocz(sizeof(*s));
250 s->mig_state.cancel = tcp_cancel;
251 s->mig_state.get_status = tcp_get_status;
252 s->mig_state.release = tcp_release;
254 s->state = MIG_STATE_ACTIVE;
256 s->bandwidth_limit = bandwidth_limit;
257 s->fd = socket(PF_INET, SOCK_STREAM, 0);
263 socket_set_nonblock(s->fd);
265 if (s->detach == 1) {
266 dprintf("detaching from monitor\n");
272 ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr));
274 ret = -socket_error();
276 if (ret == -EINPROGRESS || ret == -EWOULDBLOCK)
277 qemu_set_fd_handler2(s->fd, NULL, NULL, tcp_wait_for_connect, s);
278 } while (ret == -EINTR);
280 if (ret < 0 && ret != -EINPROGRESS && ret != -EWOULDBLOCK) {
281 dprintf("connect failed\n");
286 tcp_connect_migrate(s);
288 return &s->mig_state;
291 static void tcp_accept_incoming_migration(void *opaque)
293 struct sockaddr_in addr;
294 socklen_t addrlen = sizeof(addr);
295 int s = (unsigned long)opaque;
300 c = accept(s, (struct sockaddr *)&addr, &addrlen);
301 } while (c == -1 && socket_error() == EINTR);
303 dprintf("accepted migration\n");
306 fprintf(stderr, "could not accept migration connection\n");
310 f = qemu_fopen_socket(c);
312 fprintf(stderr, "could not qemu_fopen socket\n");
316 vm_stop(0); /* just in case */
317 ret = qemu_loadvm_state(f);
319 fprintf(stderr, "load of migration failed\n");
322 qemu_announce_self();
323 dprintf("successfully loaded vm state\n");
325 /* we've successfully migrated, close the server socket */
326 qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
337 int tcp_start_incoming_migration(const char *host_port)
339 struct sockaddr_in addr;
343 if (parse_host_port(&addr, host_port) < 0) {
344 fprintf(stderr, "invalid host/port combination: %s\n", host_port);
348 s = socket(PF_INET, SOCK_STREAM, 0);
350 return -socket_error();
353 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
355 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1)
358 if (listen(s, 1) == -1)
361 qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL,
362 (void *)(unsigned long)s);
368 return -socket_error();