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"
19 #include "buffered_file.h"
22 //#define DEBUG_MIGRATION_TCP
24 #ifdef DEBUG_MIGRATION_TCP
25 #define dprintf(fmt, ...) \
26 do { printf("migration-tcp: " fmt, ## __VA_ARGS__); } while (0)
28 #define dprintf(fmt, ...) \
32 static int socket_errno(FdMigrationState *s)
34 return socket_error();
37 static int socket_write(FdMigrationState *s, const void * buf, size_t size)
39 return send(s->fd, buf, size, 0);
42 static int tcp_close(FdMigrationState *s)
44 dprintf("tcp_close\n");
53 static void tcp_wait_for_connect(void *opaque)
55 FdMigrationState *s = opaque;
57 socklen_t valsize = sizeof(val);
59 dprintf("connect completed\n");
61 ret = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, (void *) &val, &valsize);
62 } while (ret == -1 && (s->get_error(s)) == EINTR);
69 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
72 migrate_fd_connect(s);
74 dprintf("error connecting %d\n", val);
79 MigrationState *tcp_start_outgoing_migration(const char *host_port,
80 int64_t bandwidth_limit,
85 struct sockaddr_in addr;
89 if (parse_host_port(&addr, host_port) < 0)
92 s = qemu_mallocz(sizeof(*s));
94 s->get_error = socket_errno;
95 s->write = socket_write;
97 s->mig_state.cancel = migrate_fd_cancel;
98 s->mig_state.get_status = migrate_fd_get_status;
99 s->mig_state.release = migrate_fd_release;
101 s->mig_state.blk = blk;
102 s->mig_state.shared = inc;
104 s->state = MIG_STATE_ACTIVE;
105 s->mon_resume = NULL;
106 s->bandwidth_limit = bandwidth_limit;
107 s->fd = socket(PF_INET, SOCK_STREAM, 0);
113 socket_set_nonblock(s->fd);
116 migrate_fd_monitor_suspend(s);
119 ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr));
121 ret = -(s->get_error(s));
123 if (ret == -EINPROGRESS || ret == -EWOULDBLOCK)
124 qemu_set_fd_handler2(s->fd, NULL, NULL, tcp_wait_for_connect, s);
125 } while (ret == -EINTR);
127 if (ret < 0 && ret != -EINPROGRESS && ret != -EWOULDBLOCK) {
128 dprintf("connect failed\n");
133 migrate_fd_connect(s);
135 return &s->mig_state;
138 static void tcp_accept_incoming_migration(void *opaque)
140 struct sockaddr_in addr;
141 socklen_t addrlen = sizeof(addr);
142 int s = (unsigned long)opaque;
147 c = accept(s, (struct sockaddr *)&addr, &addrlen);
148 } while (c == -1 && socket_error() == EINTR);
150 dprintf("accepted migration\n");
153 fprintf(stderr, "could not accept migration connection\n");
157 f = qemu_fopen_socket(c);
159 fprintf(stderr, "could not qemu_fopen socket\n");
163 ret = qemu_loadvm_state(f);
165 fprintf(stderr, "load of migration failed\n");
168 qemu_announce_self();
169 dprintf("successfully loaded vm state\n");
171 /* we've successfully migrated, close the server socket */
172 qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
183 int tcp_start_incoming_migration(const char *host_port)
185 struct sockaddr_in addr;
189 if (parse_host_port(&addr, host_port) < 0) {
190 fprintf(stderr, "invalid host/port combination: %s\n", host_port);
194 s = socket(PF_INET, SOCK_STREAM, 0);
196 return -socket_error();
199 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
201 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1)
204 if (listen(s, 1) == -1)
207 qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL,
208 (void *)(unsigned long)s);
214 return -socket_error();