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
24 #include "qemu/osdep.h"
28 #include "monitor/monitor.h"
29 #include "qapi/error.h"
30 #include "qemu-common.h"
31 #include "qemu/error-report.h"
32 #include "qemu/option.h"
33 #include "qemu/sockets.h"
35 #include "qemu/main-loop.h"
37 typedef struct NetSocketState
{
42 unsigned int send_index
; /* number of bytes sent (only SOCK_STREAM) */
43 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
44 IOHandler
*send_fn
; /* differs between SOCK_STREAM/SOCK_DGRAM */
45 bool read_poll
; /* waiting to receive data? */
46 bool write_poll
; /* waiting to transmit data? */
49 static void net_socket_accept(void *opaque
);
50 static void net_socket_writable(void *opaque
);
52 static void net_socket_update_fd_handler(NetSocketState
*s
)
54 qemu_set_fd_handler(s
->fd
,
55 s
->read_poll
? s
->send_fn
: NULL
,
56 s
->write_poll
? net_socket_writable
: NULL
,
60 static void net_socket_read_poll(NetSocketState
*s
, bool enable
)
62 s
->read_poll
= enable
;
63 net_socket_update_fd_handler(s
);
66 static void net_socket_write_poll(NetSocketState
*s
, bool enable
)
68 s
->write_poll
= enable
;
69 net_socket_update_fd_handler(s
);
72 static void net_socket_writable(void *opaque
)
74 NetSocketState
*s
= opaque
;
76 net_socket_write_poll(s
, false);
78 qemu_flush_queued_packets(&s
->nc
);
81 static ssize_t
net_socket_receive(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
83 NetSocketState
*s
= DO_UPCAST(NetSocketState
, nc
, nc
);
84 uint32_t len
= htonl(size
);
85 struct iovec iov
[] = {
88 .iov_len
= sizeof(len
),
90 .iov_base
= (void *)buf
,
97 remaining
= iov_size(iov
, 2) - s
->send_index
;
98 ret
= iov_send(s
->fd
, iov
, 2, s
->send_index
, remaining
);
100 if (ret
== -1 && errno
== EAGAIN
) {
101 ret
= 0; /* handled further down */
107 if (ret
< (ssize_t
)remaining
) {
108 s
->send_index
+= ret
;
109 net_socket_write_poll(s
, true);
116 static ssize_t
net_socket_receive_dgram(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
118 NetSocketState
*s
= DO_UPCAST(NetSocketState
, nc
, nc
);
122 if (s
->dgram_dst
.sin_family
!= AF_UNIX
) {
123 ret
= qemu_sendto(s
->fd
, buf
, size
, 0,
124 (struct sockaddr
*)&s
->dgram_dst
,
125 sizeof(s
->dgram_dst
));
127 ret
= send(s
->fd
, buf
, size
, 0);
129 } while (ret
== -1 && errno
== EINTR
);
131 if (ret
== -1 && errno
== EAGAIN
) {
132 net_socket_write_poll(s
, true);
138 static void net_socket_send_completed(NetClientState
*nc
, ssize_t len
)
140 NetSocketState
*s
= DO_UPCAST(NetSocketState
, nc
, nc
);
143 net_socket_read_poll(s
, true);
147 static void net_socket_rs_finalize(SocketReadState
*rs
)
149 NetSocketState
*s
= container_of(rs
, NetSocketState
, rs
);
151 if (qemu_send_packet_async(&s
->nc
, rs
->buf
,
153 net_socket_send_completed
) == 0) {
154 net_socket_read_poll(s
, false);
158 static void net_socket_send(void *opaque
)
160 NetSocketState
*s
= opaque
;
163 uint8_t buf1
[NET_BUFSIZE
];
166 size
= qemu_recv(s
->fd
, buf1
, sizeof(buf1
), 0);
168 if (errno
!= EWOULDBLOCK
)
170 } else if (size
== 0) {
171 /* end of connection */
173 net_socket_read_poll(s
, false);
174 net_socket_write_poll(s
, false);
175 if (s
->listen_fd
!= -1) {
176 qemu_set_fd_handler(s
->listen_fd
, net_socket_accept
, NULL
, s
);
181 net_socket_rs_init(&s
->rs
, net_socket_rs_finalize
, false);
182 s
->nc
.link_down
= true;
183 memset(s
->nc
.info_str
, 0, sizeof(s
->nc
.info_str
));
189 ret
= net_fill_rstate(&s
->rs
, buf
, size
);
196 static void net_socket_send_dgram(void *opaque
)
198 NetSocketState
*s
= opaque
;
201 size
= qemu_recv(s
->fd
, s
->rs
.buf
, sizeof(s
->rs
.buf
), 0);
205 /* end of connection */
206 net_socket_read_poll(s
, false);
207 net_socket_write_poll(s
, false);
210 if (qemu_send_packet_async(&s
->nc
, s
->rs
.buf
, size
,
211 net_socket_send_completed
) == 0) {
212 net_socket_read_poll(s
, false);
216 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
,
217 struct in_addr
*localaddr
,
229 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
230 error_setg(errp
, "specified mcastaddr %s (0x%08x) "
231 "does not contain a multicast address",
232 inet_ntoa(mcastaddr
->sin_addr
),
233 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
237 fd
= qemu_socket(PF_INET
, SOCK_DGRAM
, 0);
239 error_setg_errno(errp
, errno
, "can't create datagram socket");
243 /* Allow multiple sockets to bind the same multicast ip and port by setting
244 * SO_REUSEADDR. This is the only situation where SO_REUSEADDR should be set
245 * on windows. Use socket_set_fast_reuse otherwise as it sets SO_REUSEADDR
246 * only on posix systems.
249 ret
= qemu_setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
251 error_setg_errno(errp
, errno
,
252 "can't set socket option SO_REUSEADDR");
256 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
258 error_setg_errno(errp
, errno
, "can't bind ip=%s to socket",
259 inet_ntoa(mcastaddr
->sin_addr
));
263 /* Add host to multicast group */
264 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
266 imr
.imr_interface
= *localaddr
;
268 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
271 ret
= qemu_setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
272 &imr
, sizeof(struct ip_mreq
));
274 error_setg_errno(errp
, errno
,
275 "can't add socket to multicast group %s",
276 inet_ntoa(imr
.imr_multiaddr
));
280 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
282 ret
= qemu_setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
283 &loop
, sizeof(loop
));
285 error_setg_errno(errp
, errno
,
286 "can't force multicast message to loopback");
290 /* If a bind address is given, only send packets from that address */
291 if (localaddr
!= NULL
) {
292 ret
= qemu_setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_IF
,
293 localaddr
, sizeof(*localaddr
));
295 error_setg_errno(errp
, errno
,
296 "can't set the default network send interface");
301 qemu_set_nonblock(fd
);
309 static void net_socket_cleanup(NetClientState
*nc
)
311 NetSocketState
*s
= DO_UPCAST(NetSocketState
, nc
, nc
);
313 net_socket_read_poll(s
, false);
314 net_socket_write_poll(s
, false);
318 if (s
->listen_fd
!= -1) {
319 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
320 closesocket(s
->listen_fd
);
325 static NetClientInfo net_dgram_socket_info
= {
326 .type
= NET_CLIENT_DRIVER_SOCKET
,
327 .size
= sizeof(NetSocketState
),
328 .receive
= net_socket_receive_dgram
,
329 .cleanup
= net_socket_cleanup
,
332 static NetSocketState
*net_socket_fd_init_dgram(NetClientState
*peer
,
335 int fd
, int is_connected
,
339 struct sockaddr_in saddr
;
344 SocketAddressType sa_type
;
345 NetdevSocketOptions
*stored
;
347 sa
= socket_local_address(fd
, errp
);
352 qapi_free_SocketAddress(sa
);
354 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
355 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
356 * by ONLY ONE process: we must "clone" this dgram socket --jjo
359 if (is_connected
&& mcast
!= NULL
) {
360 if (parse_host_port(&saddr
, mcast
, errp
) < 0) {
364 if (saddr
.sin_addr
.s_addr
== 0) {
365 error_setg(errp
, "can't setup multicast destination address");
368 /* clone dgram socket */
369 newfd
= net_socket_mcast_create(&saddr
, NULL
, errp
);
373 /* clone newfd to fd, close newfd */
379 nc
= qemu_new_net_client(&net_dgram_socket_info
, peer
, model
, name
);
381 s
= DO_UPCAST(NetSocketState
, nc
, nc
);
385 s
->send_fn
= net_socket_send_dgram
;
386 net_socket_rs_init(&s
->rs
, net_socket_rs_finalize
, false);
387 net_socket_read_poll(s
, true);
389 /* Store startup parameters */
390 nc
->stored_config
= g_new0(NetdevInfo
, 1);
391 nc
->stored_config
->type
= NET_BACKEND_SOCKET
;
392 stored
= &nc
->stored_config
->u
.socket
;
394 stored
->has_fd
= true;
395 stored
->fd
= g_strdup_printf("%d", fd
);
397 /* mcast: save bound address as dst */
398 if (is_connected
&& mcast
!= NULL
) {
399 stored
->has_mcast
= true;
400 stored
->mcast
= g_strdup(mcast
);
402 s
->dgram_dst
= saddr
;
403 snprintf(nc
->info_str
, sizeof(nc
->info_str
),
404 "socket: fd=%d (cloned mcast=%s:%d)",
405 fd
, inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
407 if (sa_type
== SOCKET_ADDRESS_TYPE_UNIX
) {
408 s
->dgram_dst
.sin_family
= AF_UNIX
;
411 snprintf(nc
->info_str
, sizeof(nc
->info_str
),
412 "socket: fd=%d %s", fd
, SocketAddressType_str(sa_type
));
422 static void net_socket_connect(void *opaque
)
424 NetSocketState
*s
= opaque
;
425 s
->send_fn
= net_socket_send
;
426 net_socket_read_poll(s
, true);
429 static NetClientInfo net_socket_info
= {
430 .type
= NET_CLIENT_DRIVER_SOCKET
,
431 .size
= sizeof(NetSocketState
),
432 .receive
= net_socket_receive
,
433 .cleanup
= net_socket_cleanup
,
436 static NetSocketState
*net_socket_fd_init_stream(NetClientState
*peer
,
439 int fd
, int is_connected
)
443 NetdevSocketOptions
*stored
;
445 nc
= qemu_new_net_client(&net_socket_info
, peer
, model
, name
);
447 snprintf(nc
->info_str
, sizeof(nc
->info_str
), "socket: fd=%d", fd
);
449 s
= DO_UPCAST(NetSocketState
, nc
, nc
);
453 net_socket_rs_init(&s
->rs
, net_socket_rs_finalize
, false);
455 /* Disable Nagle algorithm on TCP sockets to reduce latency */
456 socket_set_nodelay(fd
);
459 net_socket_connect(s
);
461 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
464 /* Store startup parameters */
465 nc
->stored_config
= g_new0(NetdevInfo
, 1);
466 nc
->stored_config
->type
= NET_BACKEND_SOCKET
;
467 stored
= &nc
->stored_config
->u
.socket
;
469 stored
->has_fd
= true;
470 stored
->fd
= g_strdup_printf("%d", fd
);
475 static NetSocketState
*net_socket_fd_init(NetClientState
*peer
,
476 const char *model
, const char *name
,
477 int fd
, int is_connected
,
478 const char *mc
, Error
**errp
)
480 int so_type
= -1, optlen
=sizeof(so_type
);
482 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
483 (socklen_t
*)&optlen
)< 0) {
484 error_setg(errp
, "can't get socket option SO_TYPE");
490 return net_socket_fd_init_dgram(peer
, model
, name
, fd
, is_connected
,
493 return net_socket_fd_init_stream(peer
, model
, name
, fd
, is_connected
);
495 error_setg(errp
, "socket type=%d for fd=%d must be either"
496 " SOCK_DGRAM or SOCK_STREAM", so_type
, fd
);
502 static void net_socket_accept(void *opaque
)
504 NetSocketState
*s
= opaque
;
505 struct sockaddr_in saddr
;
508 NetdevSocketOptions
*stored
;
512 fd
= qemu_accept(s
->listen_fd
, (struct sockaddr
*)&saddr
, &len
);
513 if (fd
< 0 && errno
!= EINTR
) {
515 } else if (fd
>= 0) {
516 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
522 s
->nc
.link_down
= false;
523 net_socket_connect(s
);
525 /* Store additional startup parameters (extend net_socket_listen_init) */
526 stored
= &s
->nc
.stored_config
->u
.socket
;
528 stored
->has_fd
= true;
529 stored
->fd
= g_strdup_printf("%d", fd
);
531 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
532 "socket: connection from %s:%d",
533 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
536 static int net_socket_listen_init(NetClientState
*peer
,
539 const char *host_str
,
544 struct sockaddr_in saddr
;
546 NetdevSocketOptions
*stored
;
548 if (parse_host_port(&saddr
, host_str
, errp
) < 0) {
552 fd
= qemu_socket(PF_INET
, SOCK_STREAM
, 0);
554 error_setg_errno(errp
, errno
, "can't create stream socket");
557 qemu_set_nonblock(fd
);
559 socket_set_fast_reuse(fd
);
561 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
563 error_setg_errno(errp
, errno
, "can't bind ip=%s to socket",
564 inet_ntoa(saddr
.sin_addr
));
570 error_setg_errno(errp
, errno
, "can't listen on socket");
575 nc
= qemu_new_net_client(&net_socket_info
, peer
, model
, name
);
576 s
= DO_UPCAST(NetSocketState
, nc
, nc
);
579 s
->nc
.link_down
= true;
580 net_socket_rs_init(&s
->rs
, net_socket_rs_finalize
, false);
582 qemu_set_fd_handler(s
->listen_fd
, net_socket_accept
, NULL
, s
);
584 /* Store startup parameters */
585 nc
->stored_config
= g_new0(NetdevInfo
, 1);
586 nc
->stored_config
->type
= NET_BACKEND_SOCKET
;
587 stored
= &nc
->stored_config
->u
.socket
;
589 stored
->has_listen
= true;
590 stored
->listen
= g_strdup(host_str
);
595 static int net_socket_connect_init(NetClientState
*peer
,
598 const char *host_str
,
602 int fd
, connected
, ret
;
603 struct sockaddr_in saddr
;
604 NetdevSocketOptions
*stored
;
606 if (parse_host_port(&saddr
, host_str
, errp
) < 0) {
610 fd
= qemu_socket(PF_INET
, SOCK_STREAM
, 0);
612 error_setg_errno(errp
, errno
, "can't create stream socket");
615 qemu_set_nonblock(fd
);
619 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
621 if (errno
== EINTR
|| errno
== EWOULDBLOCK
) {
623 } else if (errno
== EINPROGRESS
||
628 error_setg_errno(errp
, errno
, "can't connect socket");
637 s
= net_socket_fd_init(peer
, model
, name
, fd
, connected
, NULL
, errp
);
642 /* Store additional startup parameters (extend net_socket_fd_init) */
643 stored
= &s
->nc
.stored_config
->u
.socket
;
645 stored
->has_connect
= true;
646 stored
->connect
= g_strdup(host_str
);
648 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
649 "socket: connect to %s:%d",
650 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
654 static int net_socket_mcast_init(NetClientState
*peer
,
657 const char *host_str
,
658 const char *localaddr_str
,
663 struct sockaddr_in saddr
;
664 struct in_addr localaddr
, *param_localaddr
;
665 NetdevSocketOptions
*stored
;
667 if (parse_host_port(&saddr
, host_str
, errp
) < 0) {
671 if (localaddr_str
!= NULL
) {
672 if (inet_aton(localaddr_str
, &localaddr
) == 0) {
673 error_setg(errp
, "localaddr '%s' is not a valid IPv4 address",
677 param_localaddr
= &localaddr
;
679 param_localaddr
= NULL
;
682 fd
= net_socket_mcast_create(&saddr
, param_localaddr
, errp
);
687 s
= net_socket_fd_init(peer
, model
, name
, fd
, 0, NULL
, errp
);
692 s
->dgram_dst
= saddr
;
694 /* Store additional startup parameters (extend net_socket_fd_init) */
695 stored
= &s
->nc
.stored_config
->u
.socket
;
697 if (!stored
->has_mcast
) {
698 stored
->has_mcast
= true;
699 stored
->mcast
= g_strdup(host_str
);
703 stored
->has_localaddr
= true;
704 stored
->localaddr
= g_strdup(localaddr_str
);
707 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
708 "socket: mcast=%s:%d",
709 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
714 static int net_socket_udp_init(NetClientState
*peer
,
723 struct sockaddr_in laddr
, raddr
;
724 NetdevSocketOptions
*stored
;
726 if (parse_host_port(&laddr
, lhost
, errp
) < 0) {
730 if (parse_host_port(&raddr
, rhost
, errp
) < 0) {
734 fd
= qemu_socket(PF_INET
, SOCK_DGRAM
, 0);
736 error_setg_errno(errp
, errno
, "can't create datagram socket");
740 ret
= socket_set_fast_reuse(fd
);
742 error_setg_errno(errp
, errno
,
743 "can't set socket option SO_REUSEADDR");
747 ret
= bind(fd
, (struct sockaddr
*)&laddr
, sizeof(laddr
));
749 error_setg_errno(errp
, errno
, "can't bind ip=%s to socket",
750 inet_ntoa(laddr
.sin_addr
));
754 qemu_set_nonblock(fd
);
756 s
= net_socket_fd_init(peer
, model
, name
, fd
, 0, NULL
, errp
);
761 s
->dgram_dst
= raddr
;
763 /* Store additional startup parameters (extend net_socket_fd_init) */
764 stored
= &s
->nc
.stored_config
->u
.socket
;
766 stored
->has_localaddr
= true;
767 stored
->localaddr
= g_strdup(lhost
);
769 stored
->has_udp
= true;
770 stored
->udp
= g_strdup(rhost
);
772 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
774 inet_ntoa(raddr
.sin_addr
), ntohs(raddr
.sin_port
));
778 int net_init_socket(const Netdev
*netdev
, const char *name
,
779 NetClientState
*peer
, Error
**errp
)
781 const NetdevSocketOptions
*sock
;
783 assert(netdev
->type
== NET_CLIENT_DRIVER_SOCKET
);
784 sock
= &netdev
->u
.socket
;
786 if (sock
->has_fd
+ sock
->has_listen
+ sock
->has_connect
+ sock
->has_mcast
+
787 sock
->has_udp
!= 1) {
788 error_setg(errp
, "exactly one of listen=, connect=, mcast= or udp="
793 if (sock
->has_localaddr
&& !sock
->has_mcast
&& !sock
->has_udp
) {
794 error_setg(errp
, "localaddr= is only valid with mcast= or udp=");
801 fd
= monitor_fd_param(monitor_cur(), sock
->fd
, errp
);
805 ret
= qemu_try_set_nonblock(fd
);
807 error_setg_errno(errp
, -ret
, "%s: Can't use file descriptor %d",
811 if (!net_socket_fd_init(peer
, "socket", name
, fd
, 1, sock
->mcast
,
818 if (sock
->has_listen
) {
819 if (net_socket_listen_init(peer
, "socket", name
, sock
->listen
, errp
)
826 if (sock
->has_connect
) {
827 if (net_socket_connect_init(peer
, "socket", name
, sock
->connect
, errp
)
834 if (sock
->has_mcast
) {
835 /* if sock->localaddr is missing, it has been initialized to "all bits
837 if (net_socket_mcast_init(peer
, "socket", name
, sock
->mcast
,
838 sock
->localaddr
, errp
) < 0) {
844 assert(sock
->has_udp
);
845 if (!sock
->has_localaddr
) {
846 error_setg(errp
, "localaddr= is mandatory with udp=");
849 if (net_socket_udp_init(peer
, "socket", name
, sock
->udp
, sock
->localaddr
,