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/error-report.h"
31 #include "qemu/option.h"
32 #include "qemu/sockets.h"
34 #include "qemu/main-loop.h"
36 typedef struct NetSocketState
{
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? */
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
,
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
[] = {
87 .iov_len
= sizeof(len
),
89 .iov_base
= (void *)buf
,
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 */
106 if (ret
< (ssize_t
)remaining
) {
107 s
->send_index
+= ret
;
108 net_socket_write_poll(s
, true);
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
);
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);
135 static void net_socket_send_completed(NetClientState
*nc
, ssize_t len
)
137 NetSocketState
*s
= DO_UPCAST(NetSocketState
, nc
, nc
);
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
,
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
;
160 uint8_t buf1
[NET_BUFSIZE
];
163 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
165 if (errno
!= EWOULDBLOCK
)
167 } else if (size
== 0) {
168 /* end of connection */
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
);
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", "");
186 ret
= net_fill_rstate(&s
->rs
, buf
, size
);
193 static void net_socket_send_dgram(void *opaque
)
195 NetSocketState
*s
= opaque
;
198 size
= recv(s
->fd
, s
->rs
.buf
, sizeof(s
->rs
.buf
), 0);
202 /* end of connection */
203 net_socket_read_poll(s
, false);
204 net_socket_write_poll(s
, false);
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
,
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
));
234 fd
= qemu_socket(PF_INET
, SOCK_DGRAM
, 0);
236 error_setg_errno(errp
, errno
, "can't create datagram socket");
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.
246 ret
= setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
248 error_setg_errno(errp
, errno
,
249 "can't set socket option SO_REUSEADDR");
253 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
255 error_setg_errno(errp
, errno
, "can't bind ip=%s to socket",
256 inet_ntoa(mcastaddr
->sin_addr
));
260 /* Add host to multicast group */
261 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
263 imr
.imr_interface
= *localaddr
;
265 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
268 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
269 &imr
, sizeof(struct ip_mreq
));
271 error_setg_errno(errp
, errno
,
272 "can't add socket to multicast group %s",
273 inet_ntoa(imr
.imr_multiaddr
));
277 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
279 ret
= setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
280 &loop
, sizeof(loop
));
282 error_setg_errno(errp
, errno
,
283 "can't force multicast message to loopback");
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
));
292 error_setg_errno(errp
, errno
,
293 "can't set the default network send interface");
298 qemu_socket_set_nonblock(fd
);
306 static void net_socket_cleanup(NetClientState
*nc
)
308 NetSocketState
*s
= DO_UPCAST(NetSocketState
, nc
, nc
);
310 net_socket_read_poll(s
, false);
311 net_socket_write_poll(s
, false);
315 if (s
->listen_fd
!= -1) {
316 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
317 closesocket(s
->listen_fd
);
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
,
332 int fd
, int is_connected
,
336 struct sockaddr_in saddr
;
341 SocketAddressType sa_type
;
343 sa
= socket_local_address(fd
, errp
);
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) {
360 if (saddr
.sin_addr
.s_addr
== 0) {
361 error_setg(errp
, "can't setup multicast destination address");
364 /* clone dgram socket */
365 newfd
= net_socket_mcast_create(&saddr
, NULL
, errp
);
369 /* clone newfd to fd, close newfd */
375 nc
= qemu_new_net_client(&net_dgram_socket_info
, peer
, model
, name
);
377 s
= DO_UPCAST(NetSocketState
, nc
, nc
);
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
));
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
));
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
,
423 int fd
, int is_connected
)
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
);
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
);
442 net_socket_connect(s
);
444 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, 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");
464 return net_socket_fd_init_dgram(peer
, model
, name
, fd
, is_connected
,
467 return net_socket_fd_init_stream(peer
, model
, name
, fd
, is_connected
);
469 error_setg(errp
, "socket type=%d for fd=%d must be either"
470 " SOCK_DGRAM or SOCK_STREAM", so_type
, fd
);
476 static void net_socket_accept(void *opaque
)
478 NetSocketState
*s
= opaque
;
479 struct sockaddr_in saddr
;
485 fd
= qemu_accept(s
->listen_fd
, (struct sockaddr
*)&saddr
, &len
);
486 if (fd
< 0 && errno
!= EINTR
) {
488 } else if (fd
>= 0) {
489 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
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
,
504 const char *host_str
,
509 struct sockaddr_in saddr
;
512 if (parse_host_port(&saddr
, host_str
, errp
) < 0) {
516 fd
= qemu_socket(PF_INET
, SOCK_STREAM
, 0);
518 error_setg_errno(errp
, errno
, "can't create stream socket");
521 qemu_socket_set_nonblock(fd
);
523 socket_set_fast_reuse(fd
);
525 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
527 error_setg_errno(errp
, errno
, "can't bind ip=%s to socket",
528 inet_ntoa(saddr
.sin_addr
));
534 error_setg_errno(errp
, errno
, "can't listen on socket");
539 nc
= qemu_new_net_client(&net_socket_info
, peer
, model
, name
);
540 s
= DO_UPCAST(NetSocketState
, nc
, nc
);
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
);
550 static int net_socket_connect_init(NetClientState
*peer
,
553 const char *host_str
,
557 int fd
, connected
, ret
;
558 struct sockaddr_in saddr
;
560 if (parse_host_port(&saddr
, host_str
, errp
) < 0) {
564 fd
= qemu_socket(PF_INET
, SOCK_STREAM
, 0);
566 error_setg_errno(errp
, errno
, "can't create stream socket");
569 qemu_socket_set_nonblock(fd
);
573 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
575 if (errno
== EINTR
|| errno
== EWOULDBLOCK
) {
577 } else if (errno
== EINPROGRESS
||
581 error_setg_errno(errp
, errno
, "can't connect socket");
590 s
= net_socket_fd_init(peer
, model
, name
, fd
, connected
, NULL
, errp
);
595 qemu_set_info_str(&s
->nc
, "socket: connect to %s:%d",
596 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
600 static int net_socket_mcast_init(NetClientState
*peer
,
603 const char *host_str
,
604 const char *localaddr_str
,
609 struct sockaddr_in saddr
;
610 struct in_addr localaddr
, *param_localaddr
;
612 if (parse_host_port(&saddr
, host_str
, errp
) < 0) {
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",
622 param_localaddr
= &localaddr
;
624 param_localaddr
= NULL
;
627 fd
= net_socket_mcast_create(&saddr
, param_localaddr
, errp
);
632 s
= net_socket_fd_init(peer
, model
, name
, fd
, 0, NULL
, errp
);
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
));
645 static int net_socket_udp_init(NetClientState
*peer
,
654 struct sockaddr_in laddr
, raddr
;
656 if (parse_host_port(&laddr
, lhost
, errp
) < 0) {
660 if (parse_host_port(&raddr
, rhost
, errp
) < 0) {
664 fd
= qemu_socket(PF_INET
, SOCK_DGRAM
, 0);
666 error_setg_errno(errp
, errno
, "can't create datagram socket");
670 ret
= socket_set_fast_reuse(fd
);
672 error_setg_errno(errp
, errno
,
673 "can't set socket option SO_REUSEADDR");
677 ret
= bind(fd
, (struct sockaddr
*)&laddr
, sizeof(laddr
));
679 error_setg_errno(errp
, errno
, "can't bind ip=%s to socket",
680 inet_ntoa(laddr
.sin_addr
));
684 qemu_socket_set_nonblock(fd
);
686 s
= net_socket_fd_init(peer
, model
, name
, fd
, 0, NULL
, errp
);
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
));
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
+
708 error_setg(errp
, "exactly one of listen=, connect=, mcast= or udp="
713 if (sock
->localaddr
&& !sock
->mcast
&& !sock
->udp
) {
714 error_setg(errp
, "localaddr= is only valid with mcast= or udp=");
721 fd
= monitor_fd_param(monitor_cur(), sock
->fd
, errp
);
725 ret
= qemu_socket_try_set_nonblock(fd
);
727 error_setg_errno(errp
, -ret
, "%s: Can't use file descriptor %d",
731 if (!net_socket_fd_init(peer
, "socket", name
, fd
, 1, sock
->mcast
,
739 if (net_socket_listen_init(peer
, "socket", name
, sock
->listen
, errp
)
747 if (net_socket_connect_init(peer
, "socket", name
, sock
->connect
, errp
)
755 /* if sock->localaddr is missing, it has been initialized to "all bits
757 if (net_socket_mcast_init(peer
, "socket", name
, sock
->mcast
,
758 sock
->localaddr
, errp
) < 0) {
765 if (!sock
->localaddr
) {
766 error_setg(errp
, "localaddr= is mandatory with udp=");
769 if (net_socket_udp_init(peer
, "socket", name
, sock
->udp
, sock
->localaddr
,