]> git.proxmox.com Git - qemu.git/blame - migration-tcp.c
migration: make migration-{tcp,unix} consistent
[qemu.git] / migration-tcp.c
CommitLineData
34c9dd8e
AL
1/*
2 * QEMU live migration
3 *
4 * Copyright IBM, Corp. 2008
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
14#include "qemu-common.h"
15#include "qemu_socket.h"
16#include "migration.h"
17#include "qemu-char.h"
34c9dd8e
AL
18#include "buffered_file.h"
19#include "block.h"
20
21//#define DEBUG_MIGRATION_TCP
22
34c9dd8e 23#ifdef DEBUG_MIGRATION_TCP
d0f2c4c6 24#define DPRINTF(fmt, ...) \
34c9dd8e
AL
25 do { printf("migration-tcp: " fmt, ## __VA_ARGS__); } while (0)
26#else
d0f2c4c6 27#define DPRINTF(fmt, ...) \
34c9dd8e
AL
28 do { } while (0)
29#endif
30
22f00a44 31static int socket_errno(MigrationState *s)
34c9dd8e 32{
8ad9fa5d 33 return socket_error();
34c9dd8e
AL
34}
35
22f00a44 36static int socket_write(MigrationState *s, const void * buf, size_t size)
34c9dd8e 37{
065e2813 38 return send(s->fd, buf, size, 0);
34c9dd8e
AL
39}
40
22f00a44 41static int tcp_close(MigrationState *s)
34c9dd8e 42{
d0f2c4c6 43 DPRINTF("tcp_close\n");
34c9dd8e 44 if (s->fd != -1) {
ff8d81d8
AL
45 close(s->fd);
46 s->fd = -1;
34c9dd8e
AL
47 }
48 return 0;
49}
50
34c9dd8e
AL
51static void tcp_wait_for_connect(void *opaque)
52{
22f00a44 53 MigrationState *s = opaque;
34c9dd8e 54 int val, ret;
4761a48b 55 socklen_t valsize = sizeof(val);
34c9dd8e 56
d0f2c4c6 57 DPRINTF("connect completed\n");
34c9dd8e 58 do {
0a656f5f 59 ret = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, (void *) &val, &valsize);
efab4718 60 } while (ret == -1 && (socket_error()) == EINTR);
34c9dd8e
AL
61
62 if (ret < 0) {
065e2813 63 migrate_fd_error(s);
34c9dd8e
AL
64 return;
65 }
66
67 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
68
69 if (val == 0)
065e2813 70 migrate_fd_connect(s);
34c9dd8e 71 else {
d0f2c4c6 72 DPRINTF("error connecting %d\n", val);
065e2813 73 migrate_fd_error(s);
34c9dd8e 74 }
34c9dd8e
AL
75}
76
07af4452 77int tcp_start_outgoing_migration(MigrationState *s, const char *host_port)
34c9dd8e
AL
78{
79 struct sockaddr_in addr;
34c9dd8e
AL
80 int ret;
81
07af4452
JQ
82 ret = parse_host_port(&addr, host_port);
83 if (ret < 0) {
84 return ret;
85 }
ee86c61f 86
065e2813
AL
87 s->get_error = socket_errno;
88 s->write = socket_write;
89 s->close = tcp_close;
34c9dd8e 90
40ff6d7e 91 s->fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
34c9dd8e 92 if (s->fd == -1) {
ee86c61f 93 DPRINTF("Unable to open socket");
8414ff3b 94 return -socket_error();
34c9dd8e
AL
95 }
96
17e90973 97 socket_set_nonblock(s->fd);
34c9dd8e 98
34c9dd8e
AL
99 do {
100 ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr));
8414ff3b
JQ
101 if (ret == -1) {
102 ret = -socket_error();
103 }
104 if (ret == -EINPROGRESS || ret == -EWOULDBLOCK) {
34c9dd8e 105 qemu_set_fd_handler2(s->fd, NULL, NULL, tcp_wait_for_connect, s);
8414ff3b
JQ
106 return 0;
107 }
34c9dd8e
AL
108 } while (ret == -EINTR);
109
8414ff3b 110 if (ret < 0) {
d0f2c4c6 111 DPRINTF("connect failed\n");
304e3a7c 112 migrate_fd_error(s);
8414ff3b
JQ
113 return ret;
114 }
115 migrate_fd_connect(s);
07af4452 116 return 0;
34c9dd8e
AL
117}
118
119static void tcp_accept_incoming_migration(void *opaque)
120{
121 struct sockaddr_in addr;
122 socklen_t addrlen = sizeof(addr);
e0efb993 123 int s = (intptr_t)opaque;
34c9dd8e 124 QEMUFile *f;
511c0231 125 int c;
34c9dd8e
AL
126
127 do {
40ff6d7e 128 c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
c1d36665 129 } while (c == -1 && socket_error() == EINTR);
34c9dd8e 130
d0f2c4c6 131 DPRINTF("accepted migration\n");
34c9dd8e
AL
132
133 if (c == -1) {
134 fprintf(stderr, "could not accept migration connection\n");
d092c108 135 goto out2;
34c9dd8e
AL
136 }
137
c1d36665 138 f = qemu_fopen_socket(c);
34c9dd8e
AL
139 if (f == NULL) {
140 fprintf(stderr, "could not qemu_fopen socket\n");
141 goto out;
142 }
143
511c0231 144 process_incoming_migration(f);
34c9dd8e
AL
145 qemu_fclose(f);
146out:
d092c108
SH
147 close(c);
148out2:
cfaf6d36
JQ
149 qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
150 close(s);
34c9dd8e
AL
151}
152
153int tcp_start_incoming_migration(const char *host_port)
154{
155 struct sockaddr_in addr;
156 int val;
157 int s;
158
ee86c61f
JQ
159 DPRINTF("Attempting to start an incoming migration\n");
160
34c9dd8e
AL
161 if (parse_host_port(&addr, host_port) < 0) {
162 fprintf(stderr, "invalid host/port combination: %s\n", host_port);
163 return -EINVAL;
164 }
165
40ff6d7e 166 s = qemu_socket(PF_INET, SOCK_STREAM, 0);
ee86c61f 167 if (s == -1) {
c1d36665 168 return -socket_error();
ee86c61f 169 }
34c9dd8e
AL
170
171 val = 1;
172 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
173
ee86c61f 174 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
34c9dd8e 175 goto err;
ee86c61f
JQ
176 }
177 if (listen(s, 1) == -1) {
34c9dd8e 178 goto err;
ee86c61f 179 }
34c9dd8e
AL
180
181 qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL,
e0efb993 182 (void *)(intptr_t)s);
34c9dd8e
AL
183
184 return 0;
185
186err:
187 close(s);
c1d36665 188 return -socket_error();
34c9dd8e 189}