blob: fb21e20a54caacb8e612f118966bfc7e77f954da [file] [log] [blame]
Mark McLoughlin42281ac2009-11-25 18:48:56 +00001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Mark McLoughlin42281ac2009-11-25 18:48:56 +000024#include "config-host.h"
25
Paolo Bonzini1422e322012-10-24 08:43:34 +020026#include "net/net.h"
Paolo Bonzinia245fc12012-09-17 18:43:51 +020027#include "clients.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010028#include "monitor/monitor.h"
Mark McLoughlin42281ac2009-11-25 18:48:56 +000029#include "qemu-common.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010030#include "qemu/error-report.h"
31#include "qemu/option.h"
32#include "qemu/sockets.h"
33#include "qemu/iov.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010034#include "qemu/main-loop.h"
Mark McLoughlin42281ac2009-11-25 18:48:56 +000035
36typedef struct NetSocketState {
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010037 NetClientState nc;
Zhi Yong Wu011de2b2012-07-20 14:25:53 +010038 int listen_fd;
Mark McLoughlin42281ac2009-11-25 18:48:56 +000039 int fd;
40 int state; /* 0 = getting length, 1 = getting data */
41 unsigned int index;
42 unsigned int packet_len;
Stefan Hajnoczi45a7f542012-08-20 10:14:35 +010043 unsigned int send_index; /* number of bytes sent (only SOCK_STREAM) */
Scott Feldmand32fcad2013-03-18 11:43:44 -070044 uint8_t buf[NET_BUFSIZE];
Mark McLoughlin42281ac2009-11-25 18:48:56 +000045 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
Stefan Hajnoczi863f6782012-08-20 10:21:54 +010046 IOHandler *send_fn; /* differs between SOCK_STREAM/SOCK_DGRAM */
47 bool read_poll; /* waiting to receive data? */
48 bool write_poll; /* waiting to transmit data? */
Mark McLoughlin42281ac2009-11-25 18:48:56 +000049} NetSocketState;
50
Zhi Yong Wu011de2b2012-07-20 14:25:53 +010051static void net_socket_accept(void *opaque);
Stefan Hajnoczi863f6782012-08-20 10:21:54 +010052static void net_socket_writable(void *opaque);
53
54/* Only read packets from socket when peer can receive them */
55static int net_socket_can_send(void *opaque)
56{
57 NetSocketState *s = opaque;
58
59 return qemu_can_send_packet(&s->nc);
60}
61
62static void net_socket_update_fd_handler(NetSocketState *s)
63{
64 qemu_set_fd_handler2(s->fd,
65 s->read_poll ? net_socket_can_send : NULL,
66 s->read_poll ? s->send_fn : NULL,
67 s->write_poll ? net_socket_writable : NULL,
68 s);
69}
70
71static void net_socket_read_poll(NetSocketState *s, bool enable)
72{
73 s->read_poll = enable;
74 net_socket_update_fd_handler(s);
75}
76
77static void net_socket_write_poll(NetSocketState *s, bool enable)
78{
79 s->write_poll = enable;
80 net_socket_update_fd_handler(s);
81}
82
83static void net_socket_writable(void *opaque)
84{
85 NetSocketState *s = opaque;
86
87 net_socket_write_poll(s, false);
88
89 qemu_flush_queued_packets(&s->nc);
90}
Mark McLoughlin42281ac2009-11-25 18:48:56 +000091
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010092static ssize_t net_socket_receive(NetClientState *nc, const uint8_t *buf, size_t size)
Mark McLoughlin42281ac2009-11-25 18:48:56 +000093{
Mark McLoughlin564f63e2009-11-25 18:49:08 +000094 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
Stefan Hajnoczi45a7f542012-08-20 10:14:35 +010095 uint32_t len = htonl(size);
96 struct iovec iov[] = {
97 {
98 .iov_base = &len,
99 .iov_len = sizeof(len),
100 }, {
101 .iov_base = (void *)buf,
102 .iov_len = size,
103 },
104 };
105 size_t remaining;
106 ssize_t ret;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000107
Stefan Hajnoczi45a7f542012-08-20 10:14:35 +0100108 remaining = iov_size(iov, 2) - s->send_index;
109 ret = iov_send(s->fd, iov, 2, s->send_index, remaining);
110
111 if (ret == -1 && errno == EAGAIN) {
112 ret = 0; /* handled further down */
113 }
114 if (ret == -1) {
115 s->send_index = 0;
116 return -errno;
117 }
118 if (ret < (ssize_t)remaining) {
119 s->send_index += ret;
120 net_socket_write_poll(s, true);
121 return 0;
122 }
123 s->send_index = 0;
124 return size;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000125}
126
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100127static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf, size_t size)
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000128{
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000129 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
Stefan Hajnoczi213fd502012-08-20 10:28:53 +0100130 ssize_t ret;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000131
Stefan Hajnoczi213fd502012-08-20 10:28:53 +0100132 do {
Stefan Weil73062df2012-09-22 21:13:28 +0200133 ret = qemu_sendto(s->fd, buf, size, 0,
134 (struct sockaddr *)&s->dgram_dst,
135 sizeof(s->dgram_dst));
Stefan Hajnoczi213fd502012-08-20 10:28:53 +0100136 } while (ret == -1 && errno == EINTR);
137
138 if (ret == -1 && errno == EAGAIN) {
139 net_socket_write_poll(s, true);
140 return 0;
141 }
142 return ret;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000143}
144
145static void net_socket_send(void *opaque)
146{
147 NetSocketState *s = opaque;
148 int size, err;
149 unsigned l;
Scott Feldmand32fcad2013-03-18 11:43:44 -0700150 uint8_t buf1[NET_BUFSIZE];
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000151 const uint8_t *buf;
152
Blue Swirl00aa0042011-07-23 20:04:29 +0000153 size = qemu_recv(s->fd, buf1, sizeof(buf1), 0);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000154 if (size < 0) {
155 err = socket_error();
156 if (err != EWOULDBLOCK)
157 goto eoc;
158 } else if (size == 0) {
159 /* end of connection */
160 eoc:
Stefan Hajnoczi863f6782012-08-20 10:21:54 +0100161 net_socket_read_poll(s, false);
162 net_socket_write_poll(s, false);
Zhi Yong Wu011de2b2012-07-20 14:25:53 +0100163 if (s->listen_fd != -1) {
164 qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
165 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000166 closesocket(s->fd);
Zhi Yong Wu011de2b2012-07-20 14:25:53 +0100167
168 s->fd = -1;
169 s->state = 0;
170 s->index = 0;
171 s->packet_len = 0;
172 s->nc.link_down = true;
173 memset(s->buf, 0, sizeof(s->buf));
174 memset(s->nc.info_str, 0, sizeof(s->nc.info_str));
175
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000176 return;
177 }
178 buf = buf1;
179 while (size > 0) {
180 /* reassemble a packet from the network */
181 switch(s->state) {
182 case 0:
183 l = 4 - s->index;
184 if (l > size)
185 l = size;
186 memcpy(s->buf + s->index, buf, l);
187 buf += l;
188 size -= l;
189 s->index += l;
190 if (s->index == 4) {
191 /* got length */
192 s->packet_len = ntohl(*(uint32_t *)s->buf);
193 s->index = 0;
194 s->state = 1;
195 }
196 break;
197 case 1:
198 l = s->packet_len - s->index;
199 if (l > size)
200 l = size;
201 if (s->index + l <= sizeof(s->buf)) {
202 memcpy(s->buf + s->index, buf, l);
203 } else {
204 fprintf(stderr, "serious error: oversized packet received,"
205 "connection terminated.\n");
206 s->state = 0;
207 goto eoc;
208 }
209
210 s->index += l;
211 buf += l;
212 size -= l;
213 if (s->index >= s->packet_len) {
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000214 qemu_send_packet(&s->nc, s->buf, s->packet_len);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000215 s->index = 0;
216 s->state = 0;
217 }
218 break;
219 }
220 }
221}
222
223static void net_socket_send_dgram(void *opaque)
224{
225 NetSocketState *s = opaque;
226 int size;
227
Blue Swirl00aa0042011-07-23 20:04:29 +0000228 size = qemu_recv(s->fd, s->buf, sizeof(s->buf), 0);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000229 if (size < 0)
230 return;
231 if (size == 0) {
232 /* end of connection */
Stefan Hajnoczi863f6782012-08-20 10:21:54 +0100233 net_socket_read_poll(s, false);
234 net_socket_write_poll(s, false);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000235 return;
236 }
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000237 qemu_send_packet(&s->nc, s->buf, size);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000238}
239
Mike Ryan3a75e742010-12-01 11:16:47 -0800240static int net_socket_mcast_create(struct sockaddr_in *mcastaddr, struct in_addr *localaddr)
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000241{
242 struct ip_mreq imr;
243 int fd;
244 int val, ret;
Brad23ddf2b2011-08-07 11:06:43 +0000245#ifdef __OpenBSD__
246 unsigned char loop;
247#else
248 int loop;
249#endif
250
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000251 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000252 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) "
253 "does not contain a multicast address\n",
254 inet_ntoa(mcastaddr->sin_addr),
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000255 (int)ntohl(mcastaddr->sin_addr.s_addr));
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000256 return -1;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000257
258 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100259 fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000260 if (fd < 0) {
261 perror("socket(PF_INET, SOCK_DGRAM)");
262 return -1;
263 }
264
Sebastian Ottlikbcbe92f2013-10-02 12:23:14 +0200265 /* Allow multiple sockets to bind the same multicast ip and port by setting
266 * SO_REUSEADDR. This is the only situation where SO_REUSEADDR should be set
267 * on windows. Use socket_set_fast_reuse otherwise as it sets SO_REUSEADDR
268 * only on posix systems.
269 */
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000270 val = 1;
Stefan Weil9957fc72013-03-08 19:58:32 +0100271 ret = qemu_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000272 if (ret < 0) {
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000273 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
274 goto fail;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000275 }
276
277 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
278 if (ret < 0) {
279 perror("bind");
280 goto fail;
281 }
282
283 /* Add host to multicast group */
284 imr.imr_multiaddr = mcastaddr->sin_addr;
Mike Ryan3a75e742010-12-01 11:16:47 -0800285 if (localaddr) {
286 imr.imr_interface = *localaddr;
287 } else {
288 imr.imr_interface.s_addr = htonl(INADDR_ANY);
289 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000290
Stefan Weil9957fc72013-03-08 19:58:32 +0100291 ret = qemu_setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
292 &imr, sizeof(struct ip_mreq));
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000293 if (ret < 0) {
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000294 perror("setsockopt(IP_ADD_MEMBERSHIP)");
295 goto fail;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000296 }
297
298 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
Brad23ddf2b2011-08-07 11:06:43 +0000299 loop = 1;
Stefan Weil9957fc72013-03-08 19:58:32 +0100300 ret = qemu_setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
301 &loop, sizeof(loop));
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000302 if (ret < 0) {
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000303 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
304 goto fail;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000305 }
306
Mike Ryan3a75e742010-12-01 11:16:47 -0800307 /* If a bind address is given, only send packets from that address */
308 if (localaddr != NULL) {
Stefan Weil9957fc72013-03-08 19:58:32 +0100309 ret = qemu_setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
310 localaddr, sizeof(*localaddr));
Mike Ryan3a75e742010-12-01 11:16:47 -0800311 if (ret < 0) {
312 perror("setsockopt(IP_MULTICAST_IF)");
313 goto fail;
314 }
315 }
316
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100317 qemu_set_nonblock(fd);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000318 return fd;
319fail:
320 if (fd >= 0)
321 closesocket(fd);
322 return -1;
323}
324
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100325static void net_socket_cleanup(NetClientState *nc)
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000326{
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000327 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
Zhi Yong Wu011de2b2012-07-20 14:25:53 +0100328 if (s->fd != -1) {
Stefan Hajnoczi863f6782012-08-20 10:21:54 +0100329 net_socket_read_poll(s, false);
330 net_socket_write_poll(s, false);
Zhi Yong Wu011de2b2012-07-20 14:25:53 +0100331 close(s->fd);
332 s->fd = -1;
333 }
334 if (s->listen_fd != -1) {
335 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
336 closesocket(s->listen_fd);
337 s->listen_fd = -1;
338 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000339}
340
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000341static NetClientInfo net_dgram_socket_info = {
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200342 .type = NET_CLIENT_OPTIONS_KIND_SOCKET,
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000343 .size = sizeof(NetSocketState),
344 .receive = net_socket_receive_dgram,
345 .cleanup = net_socket_cleanup,
346};
347
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100348static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000349 const char *model,
350 const char *name,
351 int fd, int is_connected)
352{
353 struct sockaddr_in saddr;
354 int newfd;
355 socklen_t saddr_len;
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100356 NetClientState *nc;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000357 NetSocketState *s;
358
359 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
360 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
361 * by ONLY ONE process: we must "clone" this dgram socket --jjo
362 */
363
364 if (is_connected) {
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000365 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
366 /* must be bound */
367 if (saddr.sin_addr.s_addr == 0) {
368 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, "
369 "cannot setup multicast dst addr\n", fd);
Stefan Hajnoczie5d1fca2011-12-07 15:01:49 +0000370 goto err;
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000371 }
372 /* clone dgram socket */
373 newfd = net_socket_mcast_create(&saddr, NULL);
374 if (newfd < 0) {
375 /* error already reported by net_socket_mcast_create() */
Stefan Hajnoczie5d1fca2011-12-07 15:01:49 +0000376 goto err;
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000377 }
378 /* clone newfd to fd, close newfd */
379 dup2(newfd, fd);
380 close(newfd);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000381
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000382 } else {
383 fprintf(stderr,
384 "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
385 fd, strerror(errno));
Stefan Hajnoczie5d1fca2011-12-07 15:01:49 +0000386 goto err;
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000387 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000388 }
389
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100390 nc = qemu_new_net_client(&net_dgram_socket_info, peer, model, name);
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000391
392 snprintf(nc->info_str, sizeof(nc->info_str),
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000393 "socket: fd=%d (%s mcast=%s:%d)",
394 fd, is_connected ? "cloned" : "",
395 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000396
397 s = DO_UPCAST(NetSocketState, nc, nc);
398
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000399 s->fd = fd;
Zhi Yong Wu011de2b2012-07-20 14:25:53 +0100400 s->listen_fd = -1;
Stefan Hajnoczi863f6782012-08-20 10:21:54 +0100401 s->send_fn = net_socket_send_dgram;
402 net_socket_read_poll(s, true);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000403
404 /* mcast: save bound address as dst */
Zhi Yong Wue34cde32012-07-20 14:25:52 +0100405 if (is_connected) {
406 s->dgram_dst = saddr;
407 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000408
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000409 return s;
Stefan Hajnoczie5d1fca2011-12-07 15:01:49 +0000410
411err:
412 closesocket(fd);
413 return NULL;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000414}
415
416static void net_socket_connect(void *opaque)
417{
418 NetSocketState *s = opaque;
Stefan Hajnoczi863f6782012-08-20 10:21:54 +0100419 s->send_fn = net_socket_send;
420 net_socket_read_poll(s, true);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000421}
422
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000423static NetClientInfo net_socket_info = {
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200424 .type = NET_CLIENT_OPTIONS_KIND_SOCKET,
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000425 .size = sizeof(NetSocketState),
426 .receive = net_socket_receive,
427 .cleanup = net_socket_cleanup,
428};
429
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100430static NetSocketState *net_socket_fd_init_stream(NetClientState *peer,
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000431 const char *model,
432 const char *name,
433 int fd, int is_connected)
434{
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100435 NetClientState *nc;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000436 NetSocketState *s;
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000437
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100438 nc = qemu_new_net_client(&net_socket_info, peer, model, name);
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000439
440 snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d", fd);
441
442 s = DO_UPCAST(NetSocketState, nc, nc);
443
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000444 s->fd = fd;
Zhi Yong Wu011de2b2012-07-20 14:25:53 +0100445 s->listen_fd = -1;
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000446
Stefan Hajnoczi20048d02013-02-27 15:05:47 +0100447 /* Disable Nagle algorithm on TCP sockets to reduce latency */
448 socket_set_nodelay(fd);
449
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000450 if (is_connected) {
451 net_socket_connect(s);
452 } else {
453 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
454 }
455 return s;
456}
457
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100458static NetSocketState *net_socket_fd_init(NetClientState *peer,
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000459 const char *model, const char *name,
460 int fd, int is_connected)
461{
462 int so_type = -1, optlen=sizeof(so_type);
463
464 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
465 (socklen_t *)&optlen)< 0) {
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000466 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n",
467 fd);
Stefan Hajnoczie5d1fca2011-12-07 15:01:49 +0000468 closesocket(fd);
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000469 return NULL;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000470 }
471 switch(so_type) {
472 case SOCK_DGRAM:
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100473 return net_socket_fd_init_dgram(peer, model, name, fd, is_connected);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000474 case SOCK_STREAM:
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100475 return net_socket_fd_init_stream(peer, model, name, fd, is_connected);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000476 default:
477 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
478 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100479 return net_socket_fd_init_stream(peer, model, name, fd, is_connected);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000480 }
481 return NULL;
482}
483
484static void net_socket_accept(void *opaque)
485{
Zhi Yong Wu011de2b2012-07-20 14:25:53 +0100486 NetSocketState *s = opaque;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000487 struct sockaddr_in saddr;
488 socklen_t len;
489 int fd;
490
491 for(;;) {
492 len = sizeof(saddr);
Zhi Yong Wu011de2b2012-07-20 14:25:53 +0100493 fd = qemu_accept(s->listen_fd, (struct sockaddr *)&saddr, &len);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000494 if (fd < 0 && errno != EINTR) {
495 return;
496 } else if (fd >= 0) {
Zhi Yong Wu011de2b2012-07-20 14:25:53 +0100497 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000498 break;
499 }
500 }
Zhi Yong Wu011de2b2012-07-20 14:25:53 +0100501
502 s->fd = fd;
503 s->nc.link_down = false;
504 net_socket_connect(s);
505 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
506 "socket: connection from %s:%d",
507 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000508}
509
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100510static int net_socket_listen_init(NetClientState *peer,
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000511 const char *model,
512 const char *name,
513 const char *host_str)
514{
Zhi Yong Wu011de2b2012-07-20 14:25:53 +0100515 NetClientState *nc;
516 NetSocketState *s;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000517 struct sockaddr_in saddr;
Sebastian Ottlikbcbe92f2013-10-02 12:23:14 +0200518 int fd, ret;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000519
520 if (parse_host_port(&saddr, host_str) < 0)
521 return -1;
522
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100523 fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000524 if (fd < 0) {
525 perror("socket");
526 return -1;
527 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100528 qemu_set_nonblock(fd);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000529
Sebastian Ottlikbcbe92f2013-10-02 12:23:14 +0200530 socket_set_fast_reuse(fd);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000531
532 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
533 if (ret < 0) {
534 perror("bind");
Peter Maydella46667e2011-12-24 23:47:11 +0000535 closesocket(fd);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000536 return -1;
537 }
538 ret = listen(fd, 0);
539 if (ret < 0) {
540 perror("listen");
Peter Maydella46667e2011-12-24 23:47:11 +0000541 closesocket(fd);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000542 return -1;
543 }
Zhi Yong Wu011de2b2012-07-20 14:25:53 +0100544
545 nc = qemu_new_net_client(&net_socket_info, peer, model, name);
546 s = DO_UPCAST(NetSocketState, nc, nc);
547 s->fd = -1;
548 s->listen_fd = fd;
549 s->nc.link_down = true;
550
551 qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000552 return 0;
553}
554
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100555static int net_socket_connect_init(NetClientState *peer,
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000556 const char *model,
557 const char *name,
558 const char *host_str)
559{
560 NetSocketState *s;
561 int fd, connected, ret, err;
562 struct sockaddr_in saddr;
563
564 if (parse_host_port(&saddr, host_str) < 0)
565 return -1;
566
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100567 fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000568 if (fd < 0) {
569 perror("socket");
570 return -1;
571 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100572 qemu_set_nonblock(fd);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000573
574 connected = 0;
575 for(;;) {
576 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
577 if (ret < 0) {
578 err = socket_error();
579 if (err == EINTR || err == EWOULDBLOCK) {
580 } else if (err == EINPROGRESS) {
581 break;
582#ifdef _WIN32
Pavel Dovgalukc7eb1f02011-02-21 14:46:44 +0300583 } else if (err == WSAEALREADY || err == WSAEINVAL) {
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000584 break;
585#endif
586 } else {
587 perror("connect");
588 closesocket(fd);
589 return -1;
590 }
591 } else {
592 connected = 1;
593 break;
594 }
595 }
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100596 s = net_socket_fd_init(peer, model, name, fd, connected);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000597 if (!s)
598 return -1;
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000599 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000600 "socket: connect to %s:%d",
601 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
602 return 0;
603}
604
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100605static int net_socket_mcast_init(NetClientState *peer,
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000606 const char *model,
607 const char *name,
Mike Ryan3a75e742010-12-01 11:16:47 -0800608 const char *host_str,
609 const char *localaddr_str)
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000610{
611 NetSocketState *s;
612 int fd;
613 struct sockaddr_in saddr;
Mike Ryan3a75e742010-12-01 11:16:47 -0800614 struct in_addr localaddr, *param_localaddr;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000615
616 if (parse_host_port(&saddr, host_str) < 0)
617 return -1;
618
Mike Ryan3a75e742010-12-01 11:16:47 -0800619 if (localaddr_str != NULL) {
620 if (inet_aton(localaddr_str, &localaddr) == 0)
621 return -1;
622 param_localaddr = &localaddr;
623 } else {
624 param_localaddr = NULL;
625 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000626
Mike Ryan3a75e742010-12-01 11:16:47 -0800627 fd = net_socket_mcast_create(&saddr, param_localaddr);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000628 if (fd < 0)
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000629 return -1;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000630
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100631 s = net_socket_fd_init(peer, model, name, fd, 0);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000632 if (!s)
633 return -1;
634
635 s->dgram_dst = saddr;
636
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000637 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000638 "socket: mcast=%s:%d",
639 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
640 return 0;
641
642}
643
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100644static int net_socket_udp_init(NetClientState *peer,
Benjamin0e0e7fa2012-01-11 09:20:54 +0900645 const char *model,
646 const char *name,
647 const char *rhost,
648 const char *lhost)
649{
650 NetSocketState *s;
Sebastian Ottlikbcbe92f2013-10-02 12:23:14 +0200651 int fd, ret;
Benjamin0e0e7fa2012-01-11 09:20:54 +0900652 struct sockaddr_in laddr, raddr;
653
654 if (parse_host_port(&laddr, lhost) < 0) {
655 return -1;
656 }
657
658 if (parse_host_port(&raddr, rhost) < 0) {
659 return -1;
660 }
661
662 fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
663 if (fd < 0) {
664 perror("socket(PF_INET, SOCK_DGRAM)");
665 return -1;
666 }
Sebastian Ottlikbcbe92f2013-10-02 12:23:14 +0200667
668 ret = socket_set_fast_reuse(fd);
Benjamin0e0e7fa2012-01-11 09:20:54 +0900669 if (ret < 0) {
Benjamin0e0e7fa2012-01-11 09:20:54 +0900670 closesocket(fd);
671 return -1;
672 }
673 ret = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr));
674 if (ret < 0) {
675 perror("bind");
676 closesocket(fd);
677 return -1;
678 }
Stefan Hajnoczifc13fa02013-03-27 10:10:44 +0100679 qemu_set_nonblock(fd);
Benjamin0e0e7fa2012-01-11 09:20:54 +0900680
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100681 s = net_socket_fd_init(peer, model, name, fd, 0);
Benjamin0e0e7fa2012-01-11 09:20:54 +0900682 if (!s) {
683 return -1;
684 }
685
686 s->dgram_dst = raddr;
687
688 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
689 "socket: udp=%s:%d",
690 inet_ntoa(raddr.sin_addr), ntohs(raddr.sin_port));
691 return 0;
692}
693
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200694int net_init_socket(const NetClientOptions *opts, const char *name,
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100695 NetClientState *peer)
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000696{
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200697 const NetdevSocketOptions *sock;
698
699 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_SOCKET);
700 sock = opts->socket;
701
702 if (sock->has_fd + sock->has_listen + sock->has_connect + sock->has_mcast +
703 sock->has_udp != 1) {
704 error_report("exactly one of fd=, listen=, connect=, mcast= or udp="
705 " is required");
706 return -1;
707 }
708
709 if (sock->has_localaddr && !sock->has_mcast && !sock->has_udp) {
710 error_report("localaddr= is only valid with mcast= or udp=");
711 return -1;
712 }
713
714 if (sock->has_fd) {
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000715 int fd;
716
Nicholas Bellingera96ed022012-08-21 20:52:07 +0000717 fd = monitor_handle_fd_param(cur_mon, sock->fd);
Stefan Hajnoczifc13fa02013-03-27 10:10:44 +0100718 if (fd == -1) {
719 return -1;
720 }
721 qemu_set_nonblock(fd);
722 if (!net_socket_fd_init(peer, "socket", name, fd, 1)) {
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000723 return -1;
724 }
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200725 return 0;
726 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000727
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200728 if (sock->has_listen) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100729 if (net_socket_listen_init(peer, "socket", name, sock->listen) == -1) {
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000730 return -1;
731 }
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200732 return 0;
733 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000734
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200735 if (sock->has_connect) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100736 if (net_socket_connect_init(peer, "socket", name, sock->connect) ==
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200737 -1) {
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000738 return -1;
739 }
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200740 return 0;
741 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000742
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200743 if (sock->has_mcast) {
744 /* if sock->localaddr is missing, it has been initialized to "all bits
745 * zero" */
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100746 if (net_socket_mcast_init(peer, "socket", name, sock->mcast,
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200747 sock->localaddr) == -1) {
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000748 return -1;
749 }
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200750 return 0;
751 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000752
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200753 assert(sock->has_udp);
754 if (!sock->has_localaddr) {
755 error_report("localaddr= is mandatory with udp=");
756 return -1;
757 }
Lei Lif0e3ac72012-11-01 17:39:55 +0800758 if (net_socket_udp_init(peer, "socket", name, sock->udp, sock->localaddr) ==
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200759 -1) {
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000760 return -1;
761 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000762 return 0;
763}