]>
Commit | Line | Data |
---|---|---|
34c9dd8e AL |
1 | /* |
2 | * QEMU live migration | |
3 | * | |
4 | * Copyright IBM, Corp. 2008 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <[email protected]> | |
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 | 31 | static int socket_errno(MigrationState *s) |
34c9dd8e | 32 | { |
8ad9fa5d | 33 | return socket_error(); |
34c9dd8e AL |
34 | } |
35 | ||
22f00a44 | 36 | static 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 | 41 | static int tcp_close(MigrationState *s) |
34c9dd8e | 42 | { |
61a5872f | 43 | int r = 0; |
d0f2c4c6 | 44 | DPRINTF("tcp_close\n"); |
34c9dd8e | 45 | if (s->fd != -1) { |
61a5872f EH |
46 | if (close(s->fd) < 0) { |
47 | r = -errno; | |
48 | } | |
ff8d81d8 | 49 | s->fd = -1; |
34c9dd8e | 50 | } |
61a5872f | 51 | return r; |
34c9dd8e AL |
52 | } |
53 | ||
34c9dd8e AL |
54 | static void tcp_wait_for_connect(void *opaque) |
55 | { | |
22f00a44 | 56 | MigrationState *s = opaque; |
34c9dd8e | 57 | int val, ret; |
4761a48b | 58 | socklen_t valsize = sizeof(val); |
34c9dd8e | 59 | |
d0f2c4c6 | 60 | DPRINTF("connect completed\n"); |
34c9dd8e | 61 | do { |
0a656f5f | 62 | ret = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, (void *) &val, &valsize); |
efab4718 | 63 | } while (ret == -1 && (socket_error()) == EINTR); |
34c9dd8e AL |
64 | |
65 | if (ret < 0) { | |
065e2813 | 66 | migrate_fd_error(s); |
34c9dd8e AL |
67 | return; |
68 | } | |
69 | ||
70 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); | |
71 | ||
72 | if (val == 0) | |
065e2813 | 73 | migrate_fd_connect(s); |
34c9dd8e | 74 | else { |
d0f2c4c6 | 75 | DPRINTF("error connecting %d\n", val); |
065e2813 | 76 | migrate_fd_error(s); |
34c9dd8e | 77 | } |
34c9dd8e AL |
78 | } |
79 | ||
07af4452 | 80 | int tcp_start_outgoing_migration(MigrationState *s, const char *host_port) |
34c9dd8e AL |
81 | { |
82 | struct sockaddr_in addr; | |
34c9dd8e AL |
83 | int ret; |
84 | ||
07af4452 JQ |
85 | ret = parse_host_port(&addr, host_port); |
86 | if (ret < 0) { | |
87 | return ret; | |
88 | } | |
ee86c61f | 89 | |
065e2813 AL |
90 | s->get_error = socket_errno; |
91 | s->write = socket_write; | |
92 | s->close = tcp_close; | |
34c9dd8e | 93 | |
40ff6d7e | 94 | s->fd = qemu_socket(PF_INET, SOCK_STREAM, 0); |
34c9dd8e | 95 | if (s->fd == -1) { |
ee86c61f | 96 | DPRINTF("Unable to open socket"); |
8414ff3b | 97 | return -socket_error(); |
34c9dd8e AL |
98 | } |
99 | ||
17e90973 | 100 | socket_set_nonblock(s->fd); |
34c9dd8e | 101 | |
34c9dd8e AL |
102 | do { |
103 | ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr)); | |
8414ff3b JQ |
104 | if (ret == -1) { |
105 | ret = -socket_error(); | |
106 | } | |
107 | if (ret == -EINPROGRESS || ret == -EWOULDBLOCK) { | |
34c9dd8e | 108 | qemu_set_fd_handler2(s->fd, NULL, NULL, tcp_wait_for_connect, s); |
8414ff3b JQ |
109 | return 0; |
110 | } | |
34c9dd8e AL |
111 | } while (ret == -EINTR); |
112 | ||
8414ff3b | 113 | if (ret < 0) { |
d0f2c4c6 | 114 | DPRINTF("connect failed\n"); |
304e3a7c | 115 | migrate_fd_error(s); |
8414ff3b JQ |
116 | return ret; |
117 | } | |
118 | migrate_fd_connect(s); | |
07af4452 | 119 | return 0; |
34c9dd8e AL |
120 | } |
121 | ||
122 | static void tcp_accept_incoming_migration(void *opaque) | |
123 | { | |
124 | struct sockaddr_in addr; | |
125 | socklen_t addrlen = sizeof(addr); | |
e0efb993 | 126 | int s = (intptr_t)opaque; |
34c9dd8e | 127 | QEMUFile *f; |
511c0231 | 128 | int c; |
34c9dd8e AL |
129 | |
130 | do { | |
40ff6d7e | 131 | c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen); |
c1d36665 | 132 | } while (c == -1 && socket_error() == EINTR); |
34c9dd8e | 133 | |
d0f2c4c6 | 134 | DPRINTF("accepted migration\n"); |
34c9dd8e AL |
135 | |
136 | if (c == -1) { | |
137 | fprintf(stderr, "could not accept migration connection\n"); | |
d092c108 | 138 | goto out2; |
34c9dd8e AL |
139 | } |
140 | ||
c1d36665 | 141 | f = qemu_fopen_socket(c); |
34c9dd8e AL |
142 | if (f == NULL) { |
143 | fprintf(stderr, "could not qemu_fopen socket\n"); | |
144 | goto out; | |
145 | } | |
146 | ||
511c0231 | 147 | process_incoming_migration(f); |
34c9dd8e AL |
148 | qemu_fclose(f); |
149 | out: | |
d092c108 SH |
150 | close(c); |
151 | out2: | |
cfaf6d36 JQ |
152 | qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL); |
153 | close(s); | |
34c9dd8e AL |
154 | } |
155 | ||
156 | int tcp_start_incoming_migration(const char *host_port) | |
157 | { | |
158 | struct sockaddr_in addr; | |
159 | int val; | |
160 | int s; | |
161 | ||
ee86c61f JQ |
162 | DPRINTF("Attempting to start an incoming migration\n"); |
163 | ||
34c9dd8e AL |
164 | if (parse_host_port(&addr, host_port) < 0) { |
165 | fprintf(stderr, "invalid host/port combination: %s\n", host_port); | |
166 | return -EINVAL; | |
167 | } | |
168 | ||
40ff6d7e | 169 | s = qemu_socket(PF_INET, SOCK_STREAM, 0); |
ee86c61f | 170 | if (s == -1) { |
c1d36665 | 171 | return -socket_error(); |
ee86c61f | 172 | } |
34c9dd8e AL |
173 | |
174 | val = 1; | |
175 | setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); | |
176 | ||
ee86c61f | 177 | if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) { |
34c9dd8e | 178 | goto err; |
ee86c61f JQ |
179 | } |
180 | if (listen(s, 1) == -1) { | |
34c9dd8e | 181 | goto err; |
ee86c61f | 182 | } |
34c9dd8e AL |
183 | |
184 | qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL, | |
e0efb993 | 185 | (void *)(intptr_t)s); |
34c9dd8e AL |
186 | |
187 | return 0; | |
188 | ||
189 | err: | |
190 | close(s); | |
c1d36665 | 191 | return -socket_error(); |
34c9dd8e | 192 | } |