vhost: fix mem_sections memory corruption
[qemu/opensuse.git] / migration-tcp.c
blobcf6a9b83d69f50b15ca2094183080ad3f372fdd2
1 /*
2 * QEMU live migration
4 * Copyright IBM, Corp. 2008
6 * Authors:
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"
18 #include "buffered_file.h"
19 #include "block.h"
21 //#define DEBUG_MIGRATION_TCP
23 #ifdef DEBUG_MIGRATION_TCP
24 #define DPRINTF(fmt, ...) \
25 do { printf("migration-tcp: " fmt, ## __VA_ARGS__); } while (0)
26 #else
27 #define DPRINTF(fmt, ...) \
28 do { } while (0)
29 #endif
31 static int socket_errno(MigrationState *s)
33 return socket_error();
36 static int socket_write(MigrationState *s, const void * buf, size_t size)
38 return send(s->fd, buf, size, 0);
41 static int tcp_close(MigrationState *s)
43 int r = 0;
44 DPRINTF("tcp_close\n");
45 if (s->fd != -1) {
46 if (close(s->fd) < 0) {
47 r = -errno;
49 s->fd = -1;
51 return r;
54 static void tcp_wait_for_connect(void *opaque)
56 MigrationState *s = opaque;
57 int val, ret;
58 socklen_t valsize = sizeof(val);
60 DPRINTF("connect completed\n");
61 do {
62 ret = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, (void *) &val, &valsize);
63 } while (ret == -1 && (socket_error()) == EINTR);
65 if (ret < 0) {
66 migrate_fd_error(s);
67 return;
70 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
72 if (val == 0)
73 migrate_fd_connect(s);
74 else {
75 DPRINTF("error connecting %d\n", val);
76 migrate_fd_error(s);
80 int tcp_start_outgoing_migration(MigrationState *s, const char *host_port)
82 struct sockaddr_in addr;
83 int ret;
85 ret = parse_host_port(&addr, host_port);
86 if (ret < 0) {
87 return ret;
90 s->get_error = socket_errno;
91 s->write = socket_write;
92 s->close = tcp_close;
94 s->fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
95 if (s->fd == -1) {
96 DPRINTF("Unable to open socket");
97 return -socket_error();
100 socket_set_nonblock(s->fd);
102 do {
103 ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr));
104 if (ret == -1) {
105 ret = -socket_error();
107 if (ret == -EINPROGRESS || ret == -EWOULDBLOCK) {
108 qemu_set_fd_handler2(s->fd, NULL, NULL, tcp_wait_for_connect, s);
109 return 0;
111 } while (ret == -EINTR);
113 if (ret < 0) {
114 DPRINTF("connect failed\n");
115 migrate_fd_error(s);
116 return ret;
118 migrate_fd_connect(s);
119 return 0;
122 static void tcp_accept_incoming_migration(void *opaque)
124 struct sockaddr_in addr;
125 socklen_t addrlen = sizeof(addr);
126 int s = (intptr_t)opaque;
127 QEMUFile *f;
128 int c;
130 do {
131 c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
132 } while (c == -1 && socket_error() == EINTR);
134 DPRINTF("accepted migration\n");
136 if (c == -1) {
137 fprintf(stderr, "could not accept migration connection\n");
138 goto out2;
141 f = qemu_fopen_socket(c);
142 if (f == NULL) {
143 fprintf(stderr, "could not qemu_fopen socket\n");
144 goto out;
147 process_incoming_migration(f);
148 qemu_fclose(f);
149 out:
150 close(c);
151 out2:
152 qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
153 close(s);
156 int tcp_start_incoming_migration(const char *host_port)
158 struct sockaddr_in addr;
159 int val;
160 int s;
162 DPRINTF("Attempting to start an incoming migration\n");
164 if (parse_host_port(&addr, host_port) < 0) {
165 fprintf(stderr, "invalid host/port combination: %s\n", host_port);
166 return -EINVAL;
169 s = qemu_socket(PF_INET, SOCK_STREAM, 0);
170 if (s == -1) {
171 return -socket_error();
174 val = 1;
175 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
177 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
178 goto err;
180 if (listen(s, 1) == -1) {
181 goto err;
184 qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL,
185 (void *)(intptr_t)s);
187 return 0;
189 err:
190 close(s);
191 return -socket_error();