blob: 9fa2cd8d516775e92d4150cc9fd0cc9214bbf8cb [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 */
Peter Maydell2744d922016-01-29 17:50:00 +000024#include "qemu/osdep.h"
Mark McLoughlin42281ac2009-11-25 18:48:56 +000025
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"
Markus Armbrusterda34e652016-03-14 09:01:28 +010029#include "qapi/error.h"
Mark McLoughlin42281ac2009-11-25 18:48:56 +000030#include "qemu-common.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010031#include "qemu/error-report.h"
32#include "qemu/option.h"
33#include "qemu/sockets.h"
34#include "qemu/iov.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010035#include "qemu/main-loop.h"
Mark McLoughlin42281ac2009-11-25 18:48:56 +000036
37typedef struct NetSocketState {
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010038 NetClientState nc;
Zhi Yong Wu011de2b2012-07-20 14:25:53 +010039 int listen_fd;
Mark McLoughlin42281ac2009-11-25 18:48:56 +000040 int fd;
41 int state; /* 0 = getting length, 1 = getting data */
42 unsigned int index;
43 unsigned int packet_len;
Stefan Hajnoczi45a7f542012-08-20 10:14:35 +010044 unsigned int send_index; /* number of bytes sent (only SOCK_STREAM) */
Scott Feldmand32fcad2013-03-18 11:43:44 -070045 uint8_t buf[NET_BUFSIZE];
Mark McLoughlin42281ac2009-11-25 18:48:56 +000046 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
Stefan Hajnoczi863f6782012-08-20 10:21:54 +010047 IOHandler *send_fn; /* differs between SOCK_STREAM/SOCK_DGRAM */
48 bool read_poll; /* waiting to receive data? */
49 bool write_poll; /* waiting to transmit data? */
Mark McLoughlin42281ac2009-11-25 18:48:56 +000050} NetSocketState;
51
Zhi Yong Wu011de2b2012-07-20 14:25:53 +010052static void net_socket_accept(void *opaque);
Stefan Hajnoczi863f6782012-08-20 10:21:54 +010053static void net_socket_writable(void *opaque);
54
Stefan Hajnoczi863f6782012-08-20 10:21:54 +010055static void net_socket_update_fd_handler(NetSocketState *s)
56{
Fam Zheng82e1cc42015-06-04 14:45:18 +080057 qemu_set_fd_handler(s->fd,
58 s->read_poll ? s->send_fn : NULL,
59 s->write_poll ? net_socket_writable : NULL,
60 s);
Stefan Hajnoczi863f6782012-08-20 10:21:54 +010061}
62
63static void net_socket_read_poll(NetSocketState *s, bool enable)
64{
65 s->read_poll = enable;
66 net_socket_update_fd_handler(s);
67}
68
69static void net_socket_write_poll(NetSocketState *s, bool enable)
70{
71 s->write_poll = enable;
72 net_socket_update_fd_handler(s);
73}
74
75static void net_socket_writable(void *opaque)
76{
77 NetSocketState *s = opaque;
78
79 net_socket_write_poll(s, false);
80
81 qemu_flush_queued_packets(&s->nc);
82}
Mark McLoughlin42281ac2009-11-25 18:48:56 +000083
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010084static ssize_t net_socket_receive(NetClientState *nc, const uint8_t *buf, size_t size)
Mark McLoughlin42281ac2009-11-25 18:48:56 +000085{
Mark McLoughlin564f63e2009-11-25 18:49:08 +000086 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
Stefan Hajnoczi45a7f542012-08-20 10:14:35 +010087 uint32_t len = htonl(size);
88 struct iovec iov[] = {
89 {
90 .iov_base = &len,
91 .iov_len = sizeof(len),
92 }, {
93 .iov_base = (void *)buf,
94 .iov_len = size,
95 },
96 };
97 size_t remaining;
98 ssize_t ret;
Mark McLoughlin42281ac2009-11-25 18:48:56 +000099
Stefan Hajnoczi45a7f542012-08-20 10:14:35 +0100100 remaining = iov_size(iov, 2) - s->send_index;
101 ret = iov_send(s->fd, iov, 2, s->send_index, remaining);
102
103 if (ret == -1 && errno == EAGAIN) {
104 ret = 0; /* handled further down */
105 }
106 if (ret == -1) {
107 s->send_index = 0;
108 return -errno;
109 }
110 if (ret < (ssize_t)remaining) {
111 s->send_index += ret;
112 net_socket_write_poll(s, true);
113 return 0;
114 }
115 s->send_index = 0;
116 return size;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000117}
118
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100119static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf, size_t size)
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000120{
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000121 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
Stefan Hajnoczi213fd502012-08-20 10:28:53 +0100122 ssize_t ret;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000123
Stefan Hajnoczi213fd502012-08-20 10:28:53 +0100124 do {
Stefan Weil73062df2012-09-22 21:13:28 +0200125 ret = qemu_sendto(s->fd, buf, size, 0,
126 (struct sockaddr *)&s->dgram_dst,
127 sizeof(s->dgram_dst));
Stefan Hajnoczi213fd502012-08-20 10:28:53 +0100128 } while (ret == -1 && errno == EINTR);
129
130 if (ret == -1 && errno == EAGAIN) {
131 net_socket_write_poll(s, true);
132 return 0;
133 }
134 return ret;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000135}
136
Fam Zheng6e99c632015-06-04 14:45:16 +0800137static void net_socket_send_completed(NetClientState *nc, ssize_t len)
138{
139 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
140
141 if (!s->read_poll) {
142 net_socket_read_poll(s, true);
143 }
144}
145
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000146static void net_socket_send(void *opaque)
147{
148 NetSocketState *s = opaque;
Daniel P. Berrangeb16a44e2016-03-07 20:36:03 +0000149 int size;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000150 unsigned l;
Scott Feldmand32fcad2013-03-18 11:43:44 -0700151 uint8_t buf1[NET_BUFSIZE];
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000152 const uint8_t *buf;
153
Blue Swirl00aa0042011-07-23 20:04:29 +0000154 size = qemu_recv(s->fd, buf1, sizeof(buf1), 0);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000155 if (size < 0) {
Daniel P. Berrangeb16a44e2016-03-07 20:36:03 +0000156 if (errno != EWOULDBLOCK)
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000157 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 McLoughlin42281ac2009-11-25 18:48:56 +0000214 s->index = 0;
215 s->state = 0;
Jason Wang091f1f52015-07-07 17:00:56 +0800216 if (qemu_send_packet_async(&s->nc, s->buf, s->packet_len,
Fam Zheng6e99c632015-06-04 14:45:16 +0800217 net_socket_send_completed) == 0) {
218 net_socket_read_poll(s, false);
219 break;
220 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000221 }
222 break;
223 }
224 }
225}
226
227static void net_socket_send_dgram(void *opaque)
228{
229 NetSocketState *s = opaque;
230 int size;
231
Blue Swirl00aa0042011-07-23 20:04:29 +0000232 size = qemu_recv(s->fd, s->buf, sizeof(s->buf), 0);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000233 if (size < 0)
234 return;
235 if (size == 0) {
236 /* end of connection */
Stefan Hajnoczi863f6782012-08-20 10:21:54 +0100237 net_socket_read_poll(s, false);
238 net_socket_write_poll(s, false);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000239 return;
240 }
Fam Zheng6e99c632015-06-04 14:45:16 +0800241 if (qemu_send_packet_async(&s->nc, s->buf, size,
242 net_socket_send_completed) == 0) {
243 net_socket_read_poll(s, false);
244 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000245}
246
Mike Ryan3a75e742010-12-01 11:16:47 -0800247static int net_socket_mcast_create(struct sockaddr_in *mcastaddr, struct in_addr *localaddr)
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000248{
249 struct ip_mreq imr;
250 int fd;
251 int val, ret;
Brad23ddf2b2011-08-07 11:06:43 +0000252#ifdef __OpenBSD__
253 unsigned char loop;
254#else
255 int loop;
256#endif
257
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000258 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000259 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) "
260 "does not contain a multicast address\n",
261 inet_ntoa(mcastaddr->sin_addr),
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000262 (int)ntohl(mcastaddr->sin_addr.s_addr));
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000263 return -1;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000264
265 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100266 fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000267 if (fd < 0) {
268 perror("socket(PF_INET, SOCK_DGRAM)");
269 return -1;
270 }
271
Sebastian Ottlikbcbe92f2013-10-02 12:23:14 +0200272 /* Allow multiple sockets to bind the same multicast ip and port by setting
273 * SO_REUSEADDR. This is the only situation where SO_REUSEADDR should be set
274 * on windows. Use socket_set_fast_reuse otherwise as it sets SO_REUSEADDR
275 * only on posix systems.
276 */
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000277 val = 1;
Stefan Weil9957fc72013-03-08 19:58:32 +0100278 ret = qemu_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000279 if (ret < 0) {
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000280 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
281 goto fail;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000282 }
283
284 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
285 if (ret < 0) {
286 perror("bind");
287 goto fail;
288 }
289
290 /* Add host to multicast group */
291 imr.imr_multiaddr = mcastaddr->sin_addr;
Mike Ryan3a75e742010-12-01 11:16:47 -0800292 if (localaddr) {
293 imr.imr_interface = *localaddr;
294 } else {
295 imr.imr_interface.s_addr = htonl(INADDR_ANY);
296 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000297
Stefan Weil9957fc72013-03-08 19:58:32 +0100298 ret = qemu_setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
299 &imr, sizeof(struct ip_mreq));
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000300 if (ret < 0) {
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000301 perror("setsockopt(IP_ADD_MEMBERSHIP)");
302 goto fail;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000303 }
304
305 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
Brad23ddf2b2011-08-07 11:06:43 +0000306 loop = 1;
Stefan Weil9957fc72013-03-08 19:58:32 +0100307 ret = qemu_setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
308 &loop, sizeof(loop));
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000309 if (ret < 0) {
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000310 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
311 goto fail;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000312 }
313
Mike Ryan3a75e742010-12-01 11:16:47 -0800314 /* If a bind address is given, only send packets from that address */
315 if (localaddr != NULL) {
Stefan Weil9957fc72013-03-08 19:58:32 +0100316 ret = qemu_setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
317 localaddr, sizeof(*localaddr));
Mike Ryan3a75e742010-12-01 11:16:47 -0800318 if (ret < 0) {
319 perror("setsockopt(IP_MULTICAST_IF)");
320 goto fail;
321 }
322 }
323
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100324 qemu_set_nonblock(fd);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000325 return fd;
326fail:
327 if (fd >= 0)
328 closesocket(fd);
329 return -1;
330}
331
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100332static void net_socket_cleanup(NetClientState *nc)
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000333{
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000334 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
Zhi Yong Wu011de2b2012-07-20 14:25:53 +0100335 if (s->fd != -1) {
Stefan Hajnoczi863f6782012-08-20 10:21:54 +0100336 net_socket_read_poll(s, false);
337 net_socket_write_poll(s, false);
Zhi Yong Wu011de2b2012-07-20 14:25:53 +0100338 close(s->fd);
339 s->fd = -1;
340 }
341 if (s->listen_fd != -1) {
342 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
343 closesocket(s->listen_fd);
344 s->listen_fd = -1;
345 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000346}
347
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000348static NetClientInfo net_dgram_socket_info = {
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200349 .type = NET_CLIENT_OPTIONS_KIND_SOCKET,
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000350 .size = sizeof(NetSocketState),
351 .receive = net_socket_receive_dgram,
352 .cleanup = net_socket_cleanup,
353};
354
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100355static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000356 const char *model,
357 const char *name,
358 int fd, int is_connected)
359{
360 struct sockaddr_in saddr;
361 int newfd;
zhanghailianged6273e2014-11-17 13:54:05 +0800362 socklen_t saddr_len = sizeof(saddr);
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100363 NetClientState *nc;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000364 NetSocketState *s;
365
366 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
367 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
368 * by ONLY ONE process: we must "clone" this dgram socket --jjo
369 */
370
371 if (is_connected) {
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000372 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
373 /* must be bound */
374 if (saddr.sin_addr.s_addr == 0) {
375 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, "
376 "cannot setup multicast dst addr\n", fd);
Stefan Hajnoczie5d1fca2011-12-07 15:01:49 +0000377 goto err;
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000378 }
379 /* clone dgram socket */
380 newfd = net_socket_mcast_create(&saddr, NULL);
381 if (newfd < 0) {
382 /* error already reported by net_socket_mcast_create() */
Stefan Hajnoczie5d1fca2011-12-07 15:01:49 +0000383 goto err;
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000384 }
385 /* clone newfd to fd, close newfd */
386 dup2(newfd, fd);
387 close(newfd);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000388
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000389 } else {
390 fprintf(stderr,
391 "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
392 fd, strerror(errno));
Stefan Hajnoczie5d1fca2011-12-07 15:01:49 +0000393 goto err;
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000394 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000395 }
396
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100397 nc = qemu_new_net_client(&net_dgram_socket_info, peer, model, name);
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000398
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000399 s = DO_UPCAST(NetSocketState, nc, nc);
400
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000401 s->fd = fd;
Zhi Yong Wu011de2b2012-07-20 14:25:53 +0100402 s->listen_fd = -1;
Stefan Hajnoczi863f6782012-08-20 10:21:54 +0100403 s->send_fn = net_socket_send_dgram;
404 net_socket_read_poll(s, true);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000405
406 /* mcast: save bound address as dst */
Zhi Yong Wue34cde32012-07-20 14:25:52 +0100407 if (is_connected) {
408 s->dgram_dst = saddr;
Gonglei8db804a2014-11-20 19:35:01 +0800409 snprintf(nc->info_str, sizeof(nc->info_str),
410 "socket: fd=%d (cloned mcast=%s:%d)",
411 fd, inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
412 } else {
413 snprintf(nc->info_str, sizeof(nc->info_str),
414 "socket: fd=%d", fd);
Zhi Yong Wue34cde32012-07-20 14:25:52 +0100415 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000416
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000417 return s;
Stefan Hajnoczie5d1fca2011-12-07 15:01:49 +0000418
419err:
420 closesocket(fd);
421 return NULL;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000422}
423
424static void net_socket_connect(void *opaque)
425{
426 NetSocketState *s = opaque;
Stefan Hajnoczi863f6782012-08-20 10:21:54 +0100427 s->send_fn = net_socket_send;
428 net_socket_read_poll(s, true);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000429}
430
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000431static NetClientInfo net_socket_info = {
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200432 .type = NET_CLIENT_OPTIONS_KIND_SOCKET,
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000433 .size = sizeof(NetSocketState),
434 .receive = net_socket_receive,
435 .cleanup = net_socket_cleanup,
436};
437
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100438static NetSocketState *net_socket_fd_init_stream(NetClientState *peer,
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000439 const char *model,
440 const char *name,
441 int fd, int is_connected)
442{
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100443 NetClientState *nc;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000444 NetSocketState *s;
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000445
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100446 nc = qemu_new_net_client(&net_socket_info, peer, model, name);
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000447
448 snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d", fd);
449
450 s = DO_UPCAST(NetSocketState, nc, nc);
451
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000452 s->fd = fd;
Zhi Yong Wu011de2b2012-07-20 14:25:53 +0100453 s->listen_fd = -1;
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000454
Stefan Hajnoczi20048d02013-02-27 15:05:47 +0100455 /* Disable Nagle algorithm on TCP sockets to reduce latency */
456 socket_set_nodelay(fd);
457
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000458 if (is_connected) {
459 net_socket_connect(s);
460 } else {
461 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
462 }
463 return s;
464}
465
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100466static NetSocketState *net_socket_fd_init(NetClientState *peer,
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000467 const char *model, const char *name,
468 int fd, int is_connected)
469{
470 int so_type = -1, optlen=sizeof(so_type);
471
472 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
473 (socklen_t *)&optlen)< 0) {
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000474 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n",
475 fd);
Stefan Hajnoczie5d1fca2011-12-07 15:01:49 +0000476 closesocket(fd);
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000477 return NULL;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000478 }
479 switch(so_type) {
480 case SOCK_DGRAM:
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100481 return net_socket_fd_init_dgram(peer, model, name, fd, is_connected);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000482 case SOCK_STREAM:
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100483 return net_socket_fd_init_stream(peer, model, name, fd, is_connected);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000484 default:
485 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
486 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 +0100487 return net_socket_fd_init_stream(peer, model, name, fd, is_connected);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000488 }
489 return NULL;
490}
491
492static void net_socket_accept(void *opaque)
493{
Zhi Yong Wu011de2b2012-07-20 14:25:53 +0100494 NetSocketState *s = opaque;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000495 struct sockaddr_in saddr;
496 socklen_t len;
497 int fd;
498
499 for(;;) {
500 len = sizeof(saddr);
Zhi Yong Wu011de2b2012-07-20 14:25:53 +0100501 fd = qemu_accept(s->listen_fd, (struct sockaddr *)&saddr, &len);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000502 if (fd < 0 && errno != EINTR) {
503 return;
504 } else if (fd >= 0) {
Zhi Yong Wu011de2b2012-07-20 14:25:53 +0100505 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000506 break;
507 }
508 }
Zhi Yong Wu011de2b2012-07-20 14:25:53 +0100509
510 s->fd = fd;
511 s->nc.link_down = false;
512 net_socket_connect(s);
513 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
514 "socket: connection from %s:%d",
515 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000516}
517
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100518static int net_socket_listen_init(NetClientState *peer,
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000519 const char *model,
520 const char *name,
521 const char *host_str)
522{
Zhi Yong Wu011de2b2012-07-20 14:25:53 +0100523 NetClientState *nc;
524 NetSocketState *s;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000525 struct sockaddr_in saddr;
Sebastian Ottlikbcbe92f2013-10-02 12:23:14 +0200526 int fd, ret;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000527
528 if (parse_host_port(&saddr, host_str) < 0)
529 return -1;
530
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100531 fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000532 if (fd < 0) {
533 perror("socket");
534 return -1;
535 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100536 qemu_set_nonblock(fd);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000537
Sebastian Ottlikbcbe92f2013-10-02 12:23:14 +0200538 socket_set_fast_reuse(fd);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000539
540 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
541 if (ret < 0) {
542 perror("bind");
Peter Maydella46667e2011-12-24 23:47:11 +0000543 closesocket(fd);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000544 return -1;
545 }
546 ret = listen(fd, 0);
547 if (ret < 0) {
548 perror("listen");
Peter Maydella46667e2011-12-24 23:47:11 +0000549 closesocket(fd);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000550 return -1;
551 }
Zhi Yong Wu011de2b2012-07-20 14:25:53 +0100552
553 nc = qemu_new_net_client(&net_socket_info, peer, model, name);
554 s = DO_UPCAST(NetSocketState, nc, nc);
555 s->fd = -1;
556 s->listen_fd = fd;
557 s->nc.link_down = true;
558
559 qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000560 return 0;
561}
562
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100563static int net_socket_connect_init(NetClientState *peer,
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000564 const char *model,
565 const char *name,
566 const char *host_str)
567{
568 NetSocketState *s;
Daniel P. Berrangeb16a44e2016-03-07 20:36:03 +0000569 int fd, connected, ret;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000570 struct sockaddr_in saddr;
571
572 if (parse_host_port(&saddr, host_str) < 0)
573 return -1;
574
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100575 fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000576 if (fd < 0) {
577 perror("socket");
578 return -1;
579 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100580 qemu_set_nonblock(fd);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000581
582 connected = 0;
583 for(;;) {
584 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
585 if (ret < 0) {
Daniel P. Berrangeb16a44e2016-03-07 20:36:03 +0000586 if (errno == EINTR || errno == EWOULDBLOCK) {
587 /* continue */
588 } else if (errno == EINPROGRESS ||
589 errno == EALREADY ||
590 errno == EINVAL) {
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000591 break;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000592 } else {
593 perror("connect");
594 closesocket(fd);
595 return -1;
596 }
597 } else {
598 connected = 1;
599 break;
600 }
601 }
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100602 s = net_socket_fd_init(peer, model, name, fd, connected);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000603 if (!s)
604 return -1;
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000605 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000606 "socket: connect to %s:%d",
607 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
608 return 0;
609}
610
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100611static int net_socket_mcast_init(NetClientState *peer,
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000612 const char *model,
613 const char *name,
Mike Ryan3a75e742010-12-01 11:16:47 -0800614 const char *host_str,
615 const char *localaddr_str)
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000616{
617 NetSocketState *s;
618 int fd;
619 struct sockaddr_in saddr;
Mike Ryan3a75e742010-12-01 11:16:47 -0800620 struct in_addr localaddr, *param_localaddr;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000621
622 if (parse_host_port(&saddr, host_str) < 0)
623 return -1;
624
Mike Ryan3a75e742010-12-01 11:16:47 -0800625 if (localaddr_str != NULL) {
626 if (inet_aton(localaddr_str, &localaddr) == 0)
627 return -1;
628 param_localaddr = &localaddr;
629 } else {
630 param_localaddr = NULL;
631 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000632
Mike Ryan3a75e742010-12-01 11:16:47 -0800633 fd = net_socket_mcast_create(&saddr, param_localaddr);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000634 if (fd < 0)
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000635 return -1;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000636
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100637 s = net_socket_fd_init(peer, model, name, fd, 0);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000638 if (!s)
639 return -1;
640
641 s->dgram_dst = saddr;
642
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000643 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000644 "socket: mcast=%s:%d",
645 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
646 return 0;
647
648}
649
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100650static int net_socket_udp_init(NetClientState *peer,
Benjamin0e0e7fa2012-01-11 09:20:54 +0900651 const char *model,
652 const char *name,
653 const char *rhost,
654 const char *lhost)
655{
656 NetSocketState *s;
Sebastian Ottlikbcbe92f2013-10-02 12:23:14 +0200657 int fd, ret;
Benjamin0e0e7fa2012-01-11 09:20:54 +0900658 struct sockaddr_in laddr, raddr;
659
660 if (parse_host_port(&laddr, lhost) < 0) {
661 return -1;
662 }
663
664 if (parse_host_port(&raddr, rhost) < 0) {
665 return -1;
666 }
667
668 fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
669 if (fd < 0) {
670 perror("socket(PF_INET, SOCK_DGRAM)");
671 return -1;
672 }
Sebastian Ottlikbcbe92f2013-10-02 12:23:14 +0200673
674 ret = socket_set_fast_reuse(fd);
Benjamin0e0e7fa2012-01-11 09:20:54 +0900675 if (ret < 0) {
Benjamin0e0e7fa2012-01-11 09:20:54 +0900676 closesocket(fd);
677 return -1;
678 }
679 ret = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr));
680 if (ret < 0) {
681 perror("bind");
682 closesocket(fd);
683 return -1;
684 }
Stefan Hajnoczifc13fa02013-03-27 10:10:44 +0100685 qemu_set_nonblock(fd);
Benjamin0e0e7fa2012-01-11 09:20:54 +0900686
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100687 s = net_socket_fd_init(peer, model, name, fd, 0);
Benjamin0e0e7fa2012-01-11 09:20:54 +0900688 if (!s) {
689 return -1;
690 }
691
692 s->dgram_dst = raddr;
693
694 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
695 "socket: udp=%s:%d",
696 inet_ntoa(raddr.sin_addr), ntohs(raddr.sin_port));
697 return 0;
698}
699
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200700int net_init_socket(const NetClientOptions *opts, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200701 NetClientState *peer, Error **errp)
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000702{
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200703 /* FIXME error_setg(errp, ...) on failure */
Markus Armbruster1677f4c2015-02-09 14:03:19 +0100704 Error *err = NULL;
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200705 const NetdevSocketOptions *sock;
706
Eric Blake8d0bcba2015-10-26 16:34:56 -0600707 assert(opts->type == NET_CLIENT_OPTIONS_KIND_SOCKET);
Eric Blake32bafa82016-03-17 16:48:37 -0600708 sock = opts->u.socket.data;
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200709
710 if (sock->has_fd + sock->has_listen + sock->has_connect + sock->has_mcast +
711 sock->has_udp != 1) {
712 error_report("exactly one of fd=, listen=, connect=, mcast= or udp="
713 " is required");
714 return -1;
715 }
716
717 if (sock->has_localaddr && !sock->has_mcast && !sock->has_udp) {
718 error_report("localaddr= is only valid with mcast= or udp=");
719 return -1;
720 }
721
722 if (sock->has_fd) {
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000723 int fd;
724
Markus Armbruster1677f4c2015-02-09 14:03:19 +0100725 fd = monitor_fd_param(cur_mon, sock->fd, &err);
Stefan Hajnoczifc13fa02013-03-27 10:10:44 +0100726 if (fd == -1) {
Markus Armbruster1677f4c2015-02-09 14:03:19 +0100727 error_report_err(err);
Stefan Hajnoczifc13fa02013-03-27 10:10:44 +0100728 return -1;
729 }
730 qemu_set_nonblock(fd);
731 if (!net_socket_fd_init(peer, "socket", name, fd, 1)) {
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000732 return -1;
733 }
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200734 return 0;
735 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000736
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200737 if (sock->has_listen) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100738 if (net_socket_listen_init(peer, "socket", name, sock->listen) == -1) {
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000739 return -1;
740 }
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200741 return 0;
742 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000743
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200744 if (sock->has_connect) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100745 if (net_socket_connect_init(peer, "socket", name, sock->connect) ==
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200746 -1) {
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000747 return -1;
748 }
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200749 return 0;
750 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000751
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200752 if (sock->has_mcast) {
753 /* if sock->localaddr is missing, it has been initialized to "all bits
754 * zero" */
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100755 if (net_socket_mcast_init(peer, "socket", name, sock->mcast,
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200756 sock->localaddr) == -1) {
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000757 return -1;
758 }
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200759 return 0;
760 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000761
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200762 assert(sock->has_udp);
763 if (!sock->has_localaddr) {
764 error_report("localaddr= is mandatory with udp=");
765 return -1;
766 }
Lei Lif0e3ac72012-11-01 17:39:55 +0800767 if (net_socket_udp_init(peer, "socket", name, sock->udp, sock->localaddr) ==
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200768 -1) {
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000769 return -1;
770 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000771 return 0;
772}