Revert "qapi: BlockExportRemoveMode: move comments to TODO"
[qemu/armbru.git] / net / socket.c
blobba6e5b0b0035a3493f6213339f49b5f9aeea01e0
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "qemu/osdep.h"
26 #include "net/net.h"
27 #include "clients.h"
28 #include "monitor/monitor.h"
29 #include "qapi/error.h"
30 #include "qemu/error-report.h"
31 #include "qemu/option.h"
32 #include "qemu/sockets.h"
33 #include "qemu/iov.h"
34 #include "qemu/main-loop.h"
36 typedef struct NetSocketState {
37 NetClientState nc;
38 int listen_fd;
39 int fd;
40 SocketReadState rs;
41 unsigned int send_index; /* number of bytes sent (only SOCK_STREAM) */
42 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
43 IOHandler *send_fn; /* differs between SOCK_STREAM/SOCK_DGRAM */
44 bool read_poll; /* waiting to receive data? */
45 bool write_poll; /* waiting to transmit data? */
46 } NetSocketState;
48 static void net_socket_accept(void *opaque);
49 static void net_socket_writable(void *opaque);
51 static void net_socket_update_fd_handler(NetSocketState *s)
53 qemu_set_fd_handler(s->fd,
54 s->read_poll ? s->send_fn : NULL,
55 s->write_poll ? net_socket_writable : NULL,
56 s);
59 static void net_socket_read_poll(NetSocketState *s, bool enable)
61 s->read_poll = enable;
62 net_socket_update_fd_handler(s);
65 static void net_socket_write_poll(NetSocketState *s, bool enable)
67 s->write_poll = enable;
68 net_socket_update_fd_handler(s);
71 static void net_socket_writable(void *opaque)
73 NetSocketState *s = opaque;
75 net_socket_write_poll(s, false);
77 qemu_flush_queued_packets(&s->nc);
80 static ssize_t net_socket_receive(NetClientState *nc, const uint8_t *buf, size_t size)
82 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
83 uint32_t len = htonl(size);
84 struct iovec iov[] = {
86 .iov_base = &len,
87 .iov_len = sizeof(len),
88 }, {
89 .iov_base = (void *)buf,
90 .iov_len = size,
93 size_t remaining;
94 ssize_t ret;
96 remaining = iov_size(iov, 2) - s->send_index;
97 ret = iov_send(s->fd, iov, 2, s->send_index, remaining);
99 if (ret == -1 && errno == EAGAIN) {
100 ret = 0; /* handled further down */
102 if (ret == -1) {
103 s->send_index = 0;
104 return -errno;
106 if (ret < (ssize_t)remaining) {
107 s->send_index += ret;
108 net_socket_write_poll(s, true);
109 return 0;
111 s->send_index = 0;
112 return size;
115 static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf, size_t size)
117 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
118 ssize_t ret;
120 ret = RETRY_ON_EINTR(
121 s->dgram_dst.sin_family != AF_UNIX ?
122 sendto(s->fd, buf, size, 0,
123 (struct sockaddr *)&s->dgram_dst,
124 sizeof(s->dgram_dst)) :
125 send(s->fd, buf, size, 0)
128 if (ret == -1 && errno == EAGAIN) {
129 net_socket_write_poll(s, true);
130 return 0;
132 return ret;
135 static void net_socket_send_completed(NetClientState *nc, ssize_t len)
137 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
139 if (!s->read_poll) {
140 net_socket_read_poll(s, true);
144 static void net_socket_rs_finalize(SocketReadState *rs)
146 NetSocketState *s = container_of(rs, NetSocketState, rs);
148 if (qemu_send_packet_async(&s->nc, rs->buf,
149 rs->packet_len,
150 net_socket_send_completed) == 0) {
151 net_socket_read_poll(s, false);
155 static void net_socket_send(void *opaque)
157 NetSocketState *s = opaque;
158 int size;
159 int ret;
160 uint8_t buf1[NET_BUFSIZE];
161 const uint8_t *buf;
163 size = recv(s->fd, buf1, sizeof(buf1), 0);
164 if (size < 0) {
165 if (errno != EWOULDBLOCK)
166 goto eoc;
167 } else if (size == 0) {
168 /* end of connection */
169 eoc:
170 net_socket_read_poll(s, false);
171 net_socket_write_poll(s, false);
172 if (s->listen_fd != -1) {
173 qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
175 close(s->fd);
177 s->fd = -1;
178 net_socket_rs_init(&s->rs, net_socket_rs_finalize, false);
179 s->nc.link_down = true;
180 qemu_set_info_str(&s->nc, "%s", "");
182 return;
184 buf = buf1;
186 ret = net_fill_rstate(&s->rs, buf, size);
188 if (ret == -1) {
189 goto eoc;
193 static void net_socket_send_dgram(void *opaque)
195 NetSocketState *s = opaque;
196 int size;
198 size = recv(s->fd, s->rs.buf, sizeof(s->rs.buf), 0);
199 if (size < 0)
200 return;
201 if (size == 0) {
202 /* end of connection */
203 net_socket_read_poll(s, false);
204 net_socket_write_poll(s, false);
205 return;
207 if (qemu_send_packet_async(&s->nc, s->rs.buf, size,
208 net_socket_send_completed) == 0) {
209 net_socket_read_poll(s, false);
213 static int net_socket_mcast_create(struct sockaddr_in *mcastaddr,
214 struct in_addr *localaddr,
215 Error **errp)
217 struct ip_mreq imr;
218 int fd;
219 int val, ret;
220 #ifdef __OpenBSD__
221 unsigned char loop;
222 #else
223 int loop;
224 #endif
226 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
227 error_setg(errp, "specified mcastaddr %s (0x%08x) "
228 "does not contain a multicast address",
229 inet_ntoa(mcastaddr->sin_addr),
230 (int)ntohl(mcastaddr->sin_addr.s_addr));
231 return -1;
234 fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
235 if (fd < 0) {
236 error_setg_errno(errp, errno, "can't create datagram socket");
237 return -1;
240 /* Allow multiple sockets to bind the same multicast ip and port by setting
241 * SO_REUSEADDR. This is the only situation where SO_REUSEADDR should be set
242 * on windows. Use socket_set_fast_reuse otherwise as it sets SO_REUSEADDR
243 * only on posix systems.
245 val = 1;
246 ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
247 if (ret < 0) {
248 error_setg_errno(errp, errno,
249 "can't set socket option SO_REUSEADDR");
250 goto fail;
253 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
254 if (ret < 0) {
255 error_setg_errno(errp, errno, "can't bind ip=%s to socket",
256 inet_ntoa(mcastaddr->sin_addr));
257 goto fail;
260 /* Add host to multicast group */
261 imr.imr_multiaddr = mcastaddr->sin_addr;
262 if (localaddr) {
263 imr.imr_interface = *localaddr;
264 } else {
265 imr.imr_interface.s_addr = htonl(INADDR_ANY);
268 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
269 &imr, sizeof(struct ip_mreq));
270 if (ret < 0) {
271 error_setg_errno(errp, errno,
272 "can't add socket to multicast group %s",
273 inet_ntoa(imr.imr_multiaddr));
274 goto fail;
277 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
278 loop = 1;
279 ret = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
280 &loop, sizeof(loop));
281 if (ret < 0) {
282 error_setg_errno(errp, errno,
283 "can't force multicast message to loopback");
284 goto fail;
287 /* If a bind address is given, only send packets from that address */
288 if (localaddr != NULL) {
289 ret = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
290 localaddr, sizeof(*localaddr));
291 if (ret < 0) {
292 error_setg_errno(errp, errno,
293 "can't set the default network send interface");
294 goto fail;
298 qemu_socket_set_nonblock(fd);
299 return fd;
300 fail:
301 if (fd >= 0)
302 close(fd);
303 return -1;
306 static void net_socket_cleanup(NetClientState *nc)
308 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
309 if (s->fd != -1) {
310 net_socket_read_poll(s, false);
311 net_socket_write_poll(s, false);
312 close(s->fd);
313 s->fd = -1;
315 if (s->listen_fd != -1) {
316 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
317 close(s->listen_fd);
318 s->listen_fd = -1;
322 static NetClientInfo net_dgram_socket_info = {
323 .type = NET_CLIENT_DRIVER_SOCKET,
324 .size = sizeof(NetSocketState),
325 .receive = net_socket_receive_dgram,
326 .cleanup = net_socket_cleanup,
329 static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
330 const char *model,
331 const char *name,
332 int fd, int is_connected,
333 const char *mcast,
334 Error **errp)
336 struct sockaddr_in saddr;
337 int newfd;
338 NetClientState *nc;
339 NetSocketState *s;
340 SocketAddress *sa;
341 SocketAddressType sa_type;
343 sa = socket_local_address(fd, errp);
344 if (!sa) {
345 return NULL;
347 sa_type = sa->type;
348 qapi_free_SocketAddress(sa);
350 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
351 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
352 * by ONLY ONE process: we must "clone" this dgram socket --jjo
355 if (is_connected && mcast != NULL) {
356 if (parse_host_port(&saddr, mcast, errp) < 0) {
357 goto err;
359 /* must be bound */
360 if (saddr.sin_addr.s_addr == 0) {
361 error_setg(errp, "can't setup multicast destination address");
362 goto err;
364 /* clone dgram socket */
365 newfd = net_socket_mcast_create(&saddr, NULL, errp);
366 if (newfd < 0) {
367 goto err;
369 /* clone newfd to fd, close newfd */
370 dup2(newfd, fd);
371 close(newfd);
375 nc = qemu_new_net_client(&net_dgram_socket_info, peer, model, name);
377 s = DO_UPCAST(NetSocketState, nc, nc);
379 s->fd = fd;
380 s->listen_fd = -1;
381 s->send_fn = net_socket_send_dgram;
382 net_socket_rs_init(&s->rs, net_socket_rs_finalize, false);
383 net_socket_read_poll(s, true);
385 /* mcast: save bound address as dst */
386 if (is_connected && mcast != NULL) {
387 s->dgram_dst = saddr;
388 qemu_set_info_str(nc, "socket: fd=%d (cloned mcast=%s:%d)", fd,
389 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
390 } else {
391 if (sa_type == SOCKET_ADDRESS_TYPE_UNIX) {
392 s->dgram_dst.sin_family = AF_UNIX;
395 qemu_set_info_str(nc, "socket: fd=%d %s", fd,
396 SocketAddressType_str(sa_type));
399 return s;
401 err:
402 close(fd);
403 return NULL;
406 static void net_socket_connect(void *opaque)
408 NetSocketState *s = opaque;
409 s->send_fn = net_socket_send;
410 net_socket_read_poll(s, true);
413 static NetClientInfo net_socket_info = {
414 .type = NET_CLIENT_DRIVER_SOCKET,
415 .size = sizeof(NetSocketState),
416 .receive = net_socket_receive,
417 .cleanup = net_socket_cleanup,
420 static NetSocketState *net_socket_fd_init_stream(NetClientState *peer,
421 const char *model,
422 const char *name,
423 int fd, int is_connected)
425 NetClientState *nc;
426 NetSocketState *s;
428 nc = qemu_new_net_client(&net_socket_info, peer, model, name);
430 qemu_set_info_str(nc, "socket: fd=%d", fd);
432 s = DO_UPCAST(NetSocketState, nc, nc);
434 s->fd = fd;
435 s->listen_fd = -1;
436 net_socket_rs_init(&s->rs, net_socket_rs_finalize, false);
438 /* Disable Nagle algorithm on TCP sockets to reduce latency */
439 socket_set_nodelay(fd);
441 if (is_connected) {
442 net_socket_connect(s);
443 } else {
444 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
446 return s;
449 static NetSocketState *net_socket_fd_init(NetClientState *peer,
450 const char *model, const char *name,
451 int fd, int is_connected,
452 const char *mc, Error **errp)
454 int so_type = -1, optlen=sizeof(so_type);
456 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
457 (socklen_t *)&optlen)< 0) {
458 error_setg(errp, "can't get socket option SO_TYPE");
459 close(fd);
460 return NULL;
462 switch(so_type) {
463 case SOCK_DGRAM:
464 return net_socket_fd_init_dgram(peer, model, name, fd, is_connected,
465 mc, errp);
466 case SOCK_STREAM:
467 return net_socket_fd_init_stream(peer, model, name, fd, is_connected);
468 default:
469 error_setg(errp, "socket type=%d for fd=%d must be either"
470 " SOCK_DGRAM or SOCK_STREAM", so_type, fd);
471 close(fd);
473 return NULL;
476 static void net_socket_accept(void *opaque)
478 NetSocketState *s = opaque;
479 struct sockaddr_in saddr;
480 socklen_t len;
481 int fd;
483 for(;;) {
484 len = sizeof(saddr);
485 fd = qemu_accept(s->listen_fd, (struct sockaddr *)&saddr, &len);
486 if (fd < 0 && errno != EINTR) {
487 return;
488 } else if (fd >= 0) {
489 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
490 break;
494 s->fd = fd;
495 s->nc.link_down = false;
496 net_socket_connect(s);
497 qemu_set_info_str(&s->nc, "socket: connection from %s:%d",
498 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
501 static int net_socket_listen_init(NetClientState *peer,
502 const char *model,
503 const char *name,
504 const char *host_str,
505 Error **errp)
507 NetClientState *nc;
508 NetSocketState *s;
509 struct sockaddr_in saddr;
510 int fd, ret;
512 if (parse_host_port(&saddr, host_str, errp) < 0) {
513 return -1;
516 fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
517 if (fd < 0) {
518 error_setg_errno(errp, errno, "can't create stream socket");
519 return -1;
521 qemu_socket_set_nonblock(fd);
523 socket_set_fast_reuse(fd);
525 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
526 if (ret < 0) {
527 error_setg_errno(errp, errno, "can't bind ip=%s to socket",
528 inet_ntoa(saddr.sin_addr));
529 close(fd);
530 return -1;
532 ret = listen(fd, 0);
533 if (ret < 0) {
534 error_setg_errno(errp, errno, "can't listen on socket");
535 close(fd);
536 return -1;
539 nc = qemu_new_net_client(&net_socket_info, peer, model, name);
540 s = DO_UPCAST(NetSocketState, nc, nc);
541 s->fd = -1;
542 s->listen_fd = fd;
543 s->nc.link_down = true;
544 net_socket_rs_init(&s->rs, net_socket_rs_finalize, false);
546 qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
547 return 0;
550 static int net_socket_connect_init(NetClientState *peer,
551 const char *model,
552 const char *name,
553 const char *host_str,
554 Error **errp)
556 NetSocketState *s;
557 int fd, connected, ret;
558 struct sockaddr_in saddr;
560 if (parse_host_port(&saddr, host_str, errp) < 0) {
561 return -1;
564 fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
565 if (fd < 0) {
566 error_setg_errno(errp, errno, "can't create stream socket");
567 return -1;
569 qemu_socket_set_nonblock(fd);
571 connected = 0;
572 for(;;) {
573 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
574 if (ret < 0) {
575 if (errno == EINTR || errno == EWOULDBLOCK) {
576 /* continue */
577 } else if (errno == EINPROGRESS ||
578 errno == EALREADY) {
579 break;
580 } else {
581 error_setg_errno(errp, errno, "can't connect socket");
582 close(fd);
583 return -1;
585 } else {
586 connected = 1;
587 break;
590 s = net_socket_fd_init(peer, model, name, fd, connected, NULL, errp);
591 if (!s) {
592 return -1;
595 qemu_set_info_str(&s->nc, "socket: connect to %s:%d",
596 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
597 return 0;
600 static int net_socket_mcast_init(NetClientState *peer,
601 const char *model,
602 const char *name,
603 const char *host_str,
604 const char *localaddr_str,
605 Error **errp)
607 NetSocketState *s;
608 int fd;
609 struct sockaddr_in saddr;
610 struct in_addr localaddr, *param_localaddr;
612 if (parse_host_port(&saddr, host_str, errp) < 0) {
613 return -1;
616 if (localaddr_str != NULL) {
617 if (inet_aton(localaddr_str, &localaddr) == 0) {
618 error_setg(errp, "localaddr '%s' is not a valid IPv4 address",
619 localaddr_str);
620 return -1;
622 param_localaddr = &localaddr;
623 } else {
624 param_localaddr = NULL;
627 fd = net_socket_mcast_create(&saddr, param_localaddr, errp);
628 if (fd < 0) {
629 return -1;
632 s = net_socket_fd_init(peer, model, name, fd, 0, NULL, errp);
633 if (!s) {
634 return -1;
637 s->dgram_dst = saddr;
639 qemu_set_info_str(&s->nc, "socket: mcast=%s:%d",
640 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
641 return 0;
645 static int net_socket_udp_init(NetClientState *peer,
646 const char *model,
647 const char *name,
648 const char *rhost,
649 const char *lhost,
650 Error **errp)
652 NetSocketState *s;
653 int fd, ret;
654 struct sockaddr_in laddr, raddr;
656 if (parse_host_port(&laddr, lhost, errp) < 0) {
657 return -1;
660 if (parse_host_port(&raddr, rhost, errp) < 0) {
661 return -1;
664 fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
665 if (fd < 0) {
666 error_setg_errno(errp, errno, "can't create datagram socket");
667 return -1;
670 ret = socket_set_fast_reuse(fd);
671 if (ret < 0) {
672 error_setg_errno(errp, errno,
673 "can't set socket option SO_REUSEADDR");
674 close(fd);
675 return -1;
677 ret = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr));
678 if (ret < 0) {
679 error_setg_errno(errp, errno, "can't bind ip=%s to socket",
680 inet_ntoa(laddr.sin_addr));
681 close(fd);
682 return -1;
684 qemu_socket_set_nonblock(fd);
686 s = net_socket_fd_init(peer, model, name, fd, 0, NULL, errp);
687 if (!s) {
688 return -1;
691 s->dgram_dst = raddr;
693 qemu_set_info_str(&s->nc, "socket: udp=%s:%d", inet_ntoa(raddr.sin_addr),
694 ntohs(raddr.sin_port));
695 return 0;
698 int net_init_socket(const Netdev *netdev, const char *name,
699 NetClientState *peer, Error **errp)
701 const NetdevSocketOptions *sock;
703 assert(netdev->type == NET_CLIENT_DRIVER_SOCKET);
704 sock = &netdev->u.socket;
706 if (!!sock->fd + !!sock->listen + !!sock->connect + !!sock->mcast +
707 !!sock->udp != 1) {
708 error_setg(errp, "exactly one of listen=, connect=, mcast= or udp="
709 " is required");
710 return -1;
713 if (sock->localaddr && !sock->mcast && !sock->udp) {
714 error_setg(errp, "localaddr= is only valid with mcast= or udp=");
715 return -1;
718 if (sock->fd) {
719 int fd, ret;
721 fd = monitor_fd_param(monitor_cur(), sock->fd, errp);
722 if (fd == -1) {
723 return -1;
725 ret = qemu_socket_try_set_nonblock(fd);
726 if (ret < 0) {
727 error_setg_errno(errp, -ret, "%s: Can't use file descriptor %d",
728 name, fd);
729 return -1;
731 if (!net_socket_fd_init(peer, "socket", name, fd, 1, sock->mcast,
732 errp)) {
733 return -1;
735 return 0;
738 if (sock->listen) {
739 if (net_socket_listen_init(peer, "socket", name, sock->listen, errp)
740 < 0) {
741 return -1;
743 return 0;
746 if (sock->connect) {
747 if (net_socket_connect_init(peer, "socket", name, sock->connect, errp)
748 < 0) {
749 return -1;
751 return 0;
754 if (sock->mcast) {
755 /* if sock->localaddr is missing, it has been initialized to "all bits
756 * zero" */
757 if (net_socket_mcast_init(peer, "socket", name, sock->mcast,
758 sock->localaddr, errp) < 0) {
759 return -1;
761 return 0;
764 assert(sock->udp);
765 if (!sock->localaddr) {
766 error_setg(errp, "localaddr= is mandatory with udp=");
767 return -1;
769 if (net_socket_udp_init(peer, "socket", name, sock->udp, sock->localaddr,
770 errp) < 0) {
771 return -1;
773 return 0;