ati-vga: Fix aperture sizes
[qemu/armbru.git] / net / socket.c
blob8e3702e1f3a87f4a24e34df400b71e02a4461b4b
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 int net_socket_fd_check(int fd, Error **errp)
451 int so_type, optlen = sizeof(so_type);
453 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
454 (socklen_t *)&optlen) < 0) {
455 error_setg(errp, "can't get socket option SO_TYPE");
456 return -1;
458 if (so_type != SOCK_DGRAM && so_type != SOCK_STREAM) {
459 error_setg(errp, "socket type=%d for fd=%d must be either"
460 " SOCK_DGRAM or SOCK_STREAM", so_type, fd);
461 return -1;
463 return so_type;
466 static void net_socket_accept(void *opaque)
468 NetSocketState *s = opaque;
469 struct sockaddr_in saddr;
470 socklen_t len;
471 int fd;
473 for(;;) {
474 len = sizeof(saddr);
475 fd = qemu_accept(s->listen_fd, (struct sockaddr *)&saddr, &len);
476 if (fd < 0 && errno != EINTR) {
477 return;
478 } else if (fd >= 0) {
479 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
480 break;
484 s->fd = fd;
485 s->nc.link_down = false;
486 net_socket_connect(s);
487 qemu_set_info_str(&s->nc, "socket: connection from %s:%d",
488 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
491 static int net_socket_listen_init(NetClientState *peer,
492 const char *model,
493 const char *name,
494 const char *host_str,
495 Error **errp)
497 NetClientState *nc;
498 NetSocketState *s;
499 struct sockaddr_in saddr;
500 int fd, ret;
502 if (parse_host_port(&saddr, host_str, errp) < 0) {
503 return -1;
506 fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
507 if (fd < 0) {
508 error_setg_errno(errp, errno, "can't create stream socket");
509 return -1;
511 qemu_socket_set_nonblock(fd);
513 socket_set_fast_reuse(fd);
515 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
516 if (ret < 0) {
517 error_setg_errno(errp, errno, "can't bind ip=%s to socket",
518 inet_ntoa(saddr.sin_addr));
519 close(fd);
520 return -1;
522 ret = listen(fd, 0);
523 if (ret < 0) {
524 error_setg_errno(errp, errno, "can't listen on socket");
525 close(fd);
526 return -1;
529 nc = qemu_new_net_client(&net_socket_info, peer, model, name);
530 s = DO_UPCAST(NetSocketState, nc, nc);
531 s->fd = -1;
532 s->listen_fd = fd;
533 s->nc.link_down = true;
534 net_socket_rs_init(&s->rs, net_socket_rs_finalize, false);
536 qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
537 return 0;
540 static int net_socket_connect_init(NetClientState *peer,
541 const char *model,
542 const char *name,
543 const char *host_str,
544 Error **errp)
546 NetSocketState *s;
547 int fd, connected, ret;
548 struct sockaddr_in saddr;
550 if (parse_host_port(&saddr, host_str, errp) < 0) {
551 return -1;
554 fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
555 if (fd < 0) {
556 error_setg_errno(errp, errno, "can't create stream socket");
557 return -1;
559 qemu_socket_set_nonblock(fd);
561 connected = 0;
562 for(;;) {
563 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
564 if (ret < 0) {
565 if (errno == EINTR || errno == EWOULDBLOCK) {
566 /* continue */
567 } else if (errno == EINPROGRESS ||
568 errno == EALREADY) {
569 break;
570 } else {
571 error_setg_errno(errp, errno, "can't connect socket");
572 close(fd);
573 return -1;
575 } else {
576 connected = 1;
577 break;
580 s = net_socket_fd_init_stream(peer, model, name, fd, connected);
581 if (!s) {
582 return -1;
585 qemu_set_info_str(&s->nc, "socket: connect to %s:%d",
586 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
587 return 0;
590 static int net_socket_mcast_init(NetClientState *peer,
591 const char *model,
592 const char *name,
593 const char *host_str,
594 const char *localaddr_str,
595 Error **errp)
597 NetSocketState *s;
598 int fd;
599 struct sockaddr_in saddr;
600 struct in_addr localaddr, *param_localaddr;
602 if (parse_host_port(&saddr, host_str, errp) < 0) {
603 return -1;
606 if (localaddr_str != NULL) {
607 if (inet_aton(localaddr_str, &localaddr) == 0) {
608 error_setg(errp, "localaddr '%s' is not a valid IPv4 address",
609 localaddr_str);
610 return -1;
612 param_localaddr = &localaddr;
613 } else {
614 param_localaddr = NULL;
617 fd = net_socket_mcast_create(&saddr, param_localaddr, errp);
618 if (fd < 0) {
619 return -1;
622 s = net_socket_fd_init_dgram(peer, model, name, fd, 0, NULL, errp);
623 if (!s) {
624 return -1;
627 s->dgram_dst = saddr;
629 qemu_set_info_str(&s->nc, "socket: mcast=%s:%d",
630 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
631 return 0;
635 static int net_socket_udp_init(NetClientState *peer,
636 const char *model,
637 const char *name,
638 const char *rhost,
639 const char *lhost,
640 Error **errp)
642 NetSocketState *s;
643 int fd, ret;
644 struct sockaddr_in laddr, raddr;
646 if (parse_host_port(&laddr, lhost, errp) < 0) {
647 return -1;
650 if (parse_host_port(&raddr, rhost, errp) < 0) {
651 return -1;
654 fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
655 if (fd < 0) {
656 error_setg_errno(errp, errno, "can't create datagram socket");
657 return -1;
660 ret = socket_set_fast_reuse(fd);
661 if (ret < 0) {
662 error_setg_errno(errp, errno,
663 "can't set socket option SO_REUSEADDR");
664 close(fd);
665 return -1;
667 ret = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr));
668 if (ret < 0) {
669 error_setg_errno(errp, errno, "can't bind ip=%s to socket",
670 inet_ntoa(laddr.sin_addr));
671 close(fd);
672 return -1;
674 qemu_socket_set_nonblock(fd);
676 s = net_socket_fd_init_dgram(peer, model, name, fd, 0, NULL, errp);
677 if (!s) {
678 return -1;
681 s->dgram_dst = raddr;
683 qemu_set_info_str(&s->nc, "socket: udp=%s:%d", inet_ntoa(raddr.sin_addr),
684 ntohs(raddr.sin_port));
685 return 0;
688 int net_init_socket(const Netdev *netdev, const char *name,
689 NetClientState *peer, Error **errp)
691 const NetdevSocketOptions *sock;
693 assert(netdev->type == NET_CLIENT_DRIVER_SOCKET);
694 sock = &netdev->u.socket;
696 if (!!sock->fd + !!sock->listen + !!sock->connect + !!sock->mcast +
697 !!sock->udp != 1) {
698 error_setg(errp, "exactly one of listen=, connect=, mcast= or udp="
699 " is required");
700 return -1;
703 if (sock->localaddr && !sock->mcast && !sock->udp) {
704 error_setg(errp, "localaddr= is only valid with mcast= or udp=");
705 return -1;
708 if (sock->fd) {
709 int fd, ret, so_type;
711 fd = monitor_fd_param(monitor_cur(), sock->fd, errp);
712 if (fd == -1) {
713 return -1;
715 so_type = net_socket_fd_check(fd, errp);
716 if (so_type < 0) {
717 return -1;
719 ret = qemu_socket_try_set_nonblock(fd);
720 if (ret < 0) {
721 error_setg_errno(errp, -ret, "%s: Can't use file descriptor %d",
722 name, fd);
723 return -1;
725 switch (so_type) {
726 case SOCK_DGRAM:
727 if (!net_socket_fd_init_dgram(peer, "socket", name, fd, 1,
728 sock->mcast, errp)) {
729 return -1;
731 break;
732 case SOCK_STREAM:
733 if (!net_socket_fd_init_stream(peer, "socket", name, fd, 1)) {
734 return -1;
736 break;
738 return 0;
741 if (sock->listen) {
742 if (net_socket_listen_init(peer, "socket", name, sock->listen, errp)
743 < 0) {
744 return -1;
746 return 0;
749 if (sock->connect) {
750 if (net_socket_connect_init(peer, "socket", name, sock->connect, errp)
751 < 0) {
752 return -1;
754 return 0;
757 if (sock->mcast) {
758 /* if sock->localaddr is missing, it has been initialized to "all bits
759 * zero" */
760 if (net_socket_mcast_init(peer, "socket", name, sock->mcast,
761 sock->localaddr, errp) < 0) {
762 return -1;
764 return 0;
767 assert(sock->udp);
768 if (!sock->localaddr) {
769 error_setg(errp, "localaddr= is mandatory with udp=");
770 return -1;
772 if (net_socket_udp_init(peer, "socket", name, sock->udp, sock->localaddr,
773 errp) < 0) {
774 return -1;
776 return 0;