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 ret
= qemu_sendto(s
->fd
, buf
, size
, 0,
123 (struct sockaddr
*)&s
->dgram_dst
,
124 sizeof(s
->dgram_dst
));
125 } while (ret
== -1 && errno
== EINTR
);
127 if (ret
== -1 && errno
== EAGAIN
) {
128 net_socket_write_poll(s
, true);
134 static void net_socket_send_completed(NetClientState
*nc
, ssize_t len
)
136 NetSocketState
*s
= DO_UPCAST(NetSocketState
, nc
, nc
);
139 net_socket_read_poll(s
, true);
143 static void net_socket_rs_finalize(SocketReadState
*rs
)
145 NetSocketState
*s
= container_of(rs
, NetSocketState
, rs
);
147 if (qemu_send_packet_async(&s
->nc
, rs
->buf
,
149 net_socket_send_completed
) == 0) {
150 net_socket_read_poll(s
, false);
154 static void net_socket_send(void *opaque
)
156 NetSocketState
*s
= opaque
;
159 uint8_t buf1
[NET_BUFSIZE
];
162 size
= qemu_recv(s
->fd
, buf1
, sizeof(buf1
), 0);
164 if (errno
!= EWOULDBLOCK
)
166 } else if (size
== 0) {
167 /* end of connection */
169 net_socket_read_poll(s
, false);
170 net_socket_write_poll(s
, false);
171 if (s
->listen_fd
!= -1) {
172 qemu_set_fd_handler(s
->listen_fd
, net_socket_accept
, NULL
, s
);
177 net_socket_rs_init(&s
->rs
, net_socket_rs_finalize
, false);
178 s
->nc
.link_down
= true;
179 memset(s
->nc
.info_str
, 0, sizeof(s
->nc
.info_str
));
185 ret
= net_fill_rstate(&s
->rs
, buf
, size
);
192 static void net_socket_send_dgram(void *opaque
)
194 NetSocketState
*s
= opaque
;
197 size
= qemu_recv(s
->fd
, s
->rs
.buf
, sizeof(s
->rs
.buf
), 0);
201 /* end of connection */
202 net_socket_read_poll(s
, false);
203 net_socket_write_poll(s
, false);
206 if (qemu_send_packet_async(&s
->nc
, s
->rs
.buf
, size
,
207 net_socket_send_completed
) == 0) {
208 net_socket_read_poll(s
, false);
212 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
,
213 struct in_addr
*localaddr
,
225 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
226 error_setg(errp
, "specified mcastaddr %s (0x%08x) "
227 "does not contain a multicast address",
228 inet_ntoa(mcastaddr
->sin_addr
),
229 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
233 fd
= qemu_socket(PF_INET
, SOCK_DGRAM
, 0);
235 error_setg_errno(errp
, errno
, "can't create datagram socket");
239 /* Allow multiple sockets to bind the same multicast ip and port by setting
240 * SO_REUSEADDR. This is the only situation where SO_REUSEADDR should be set
241 * on windows. Use socket_set_fast_reuse otherwise as it sets SO_REUSEADDR
242 * only on posix systems.
245 ret
= qemu_setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
247 error_setg_errno(errp
, errno
,
248 "can't set socket option SO_REUSEADDR");
252 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
254 error_setg_errno(errp
, errno
, "can't bind ip=%s to socket",
255 inet_ntoa(mcastaddr
->sin_addr
));
259 /* Add host to multicast group */
260 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
262 imr
.imr_interface
= *localaddr
;
264 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
267 ret
= qemu_setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
268 &imr
, sizeof(struct ip_mreq
));
270 error_setg_errno(errp
, errno
,
271 "can't add socket to multicast group %s",
272 inet_ntoa(imr
.imr_multiaddr
));
276 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
278 ret
= qemu_setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
279 &loop
, sizeof(loop
));
281 error_setg_errno(errp
, errno
,
282 "can't force multicast message to loopback");
286 /* If a bind address is given, only send packets from that address */
287 if (localaddr
!= NULL
) {
288 ret
= qemu_setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_IF
,
289 localaddr
, sizeof(*localaddr
));
291 error_setg_errno(errp
, errno
,
292 "can't set the default network send interface");
297 qemu_set_nonblock(fd
);
305 static void net_socket_cleanup(NetClientState
*nc
)
307 NetSocketState
*s
= DO_UPCAST(NetSocketState
, nc
, nc
);
309 net_socket_read_poll(s
, false);
310 net_socket_write_poll(s
, false);
314 if (s
->listen_fd
!= -1) {
315 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
316 closesocket(s
->listen_fd
);
321 static NetClientInfo net_dgram_socket_info
= {
322 .type
= NET_CLIENT_DRIVER_SOCKET
,
323 .size
= sizeof(NetSocketState
),
324 .receive
= net_socket_receive_dgram
,
325 .cleanup
= net_socket_cleanup
,
328 static NetSocketState
*net_socket_fd_init_dgram(NetClientState
*peer
,
331 int fd
, int is_connected
,
335 struct sockaddr_in saddr
;
340 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
341 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
342 * by ONLY ONE process: we must "clone" this dgram socket --jjo
345 if (is_connected
&& mcast
!= NULL
) {
346 if (parse_host_port(&saddr
, mcast
, errp
) < 0) {
350 if (saddr
.sin_addr
.s_addr
== 0) {
351 error_setg(errp
, "can't setup multicast destination address");
354 /* clone dgram socket */
355 newfd
= net_socket_mcast_create(&saddr
, NULL
, errp
);
359 /* clone newfd to fd, close newfd */
365 nc
= qemu_new_net_client(&net_dgram_socket_info
, peer
, model
, name
);
367 s
= DO_UPCAST(NetSocketState
, nc
, nc
);
371 s
->send_fn
= net_socket_send_dgram
;
372 net_socket_rs_init(&s
->rs
, net_socket_rs_finalize
, false);
373 net_socket_read_poll(s
, true);
375 /* mcast: save bound address as dst */
376 if (is_connected
&& mcast
!= NULL
) {
377 s
->dgram_dst
= saddr
;
378 snprintf(nc
->info_str
, sizeof(nc
->info_str
),
379 "socket: fd=%d (cloned mcast=%s:%d)",
380 fd
, inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
382 snprintf(nc
->info_str
, sizeof(nc
->info_str
),
383 "socket: fd=%d", fd
);
393 static void net_socket_connect(void *opaque
)
395 NetSocketState
*s
= opaque
;
396 s
->send_fn
= net_socket_send
;
397 net_socket_read_poll(s
, true);
400 static NetClientInfo net_socket_info
= {
401 .type
= NET_CLIENT_DRIVER_SOCKET
,
402 .size
= sizeof(NetSocketState
),
403 .receive
= net_socket_receive
,
404 .cleanup
= net_socket_cleanup
,
407 static NetSocketState
*net_socket_fd_init_stream(NetClientState
*peer
,
410 int fd
, int is_connected
)
415 nc
= qemu_new_net_client(&net_socket_info
, peer
, model
, name
);
417 snprintf(nc
->info_str
, sizeof(nc
->info_str
), "socket: fd=%d", fd
);
419 s
= DO_UPCAST(NetSocketState
, nc
, nc
);
423 net_socket_rs_init(&s
->rs
, net_socket_rs_finalize
, false);
425 /* Disable Nagle algorithm on TCP sockets to reduce latency */
426 socket_set_nodelay(fd
);
429 net_socket_connect(s
);
431 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
436 static NetSocketState
*net_socket_fd_init(NetClientState
*peer
,
437 const char *model
, const char *name
,
438 int fd
, int is_connected
,
439 const char *mc
, Error
**errp
)
441 int so_type
= -1, optlen
=sizeof(so_type
);
443 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
444 (socklen_t
*)&optlen
)< 0) {
445 error_setg(errp
, "can't get socket option SO_TYPE");
451 return net_socket_fd_init_dgram(peer
, model
, name
, fd
, is_connected
,
454 return net_socket_fd_init_stream(peer
, model
, name
, fd
, is_connected
);
456 error_report("socket type=%d for fd=%d must be either"
457 " SOCK_DGRAM or SOCK_STREAM", so_type
, fd
);
463 static void net_socket_accept(void *opaque
)
465 NetSocketState
*s
= opaque
;
466 struct sockaddr_in saddr
;
472 fd
= qemu_accept(s
->listen_fd
, (struct sockaddr
*)&saddr
, &len
);
473 if (fd
< 0 && errno
!= EINTR
) {
475 } else if (fd
>= 0) {
476 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
482 s
->nc
.link_down
= false;
483 net_socket_connect(s
);
484 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
485 "socket: connection from %s:%d",
486 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
489 static int net_socket_listen_init(NetClientState
*peer
,
492 const char *host_str
,
497 struct sockaddr_in saddr
;
500 if (parse_host_port(&saddr
, host_str
, errp
) < 0) {
504 fd
= qemu_socket(PF_INET
, SOCK_STREAM
, 0);
506 error_setg_errno(errp
, errno
, "can't create stream socket");
509 qemu_set_nonblock(fd
);
511 socket_set_fast_reuse(fd
);
513 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
515 error_setg_errno(errp
, errno
, "can't bind ip=%s to socket",
516 inet_ntoa(saddr
.sin_addr
));
522 error_setg_errno(errp
, errno
, "can't listen on socket");
527 nc
= qemu_new_net_client(&net_socket_info
, peer
, model
, name
);
528 s
= DO_UPCAST(NetSocketState
, nc
, nc
);
531 s
->nc
.link_down
= true;
532 net_socket_rs_init(&s
->rs
, net_socket_rs_finalize
, false);
534 qemu_set_fd_handler(s
->listen_fd
, net_socket_accept
, NULL
, s
);
538 static int net_socket_connect_init(NetClientState
*peer
,
541 const char *host_str
,
545 int fd
, connected
, ret
;
546 struct sockaddr_in saddr
;
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
);
561 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
563 if (errno
== EINTR
|| errno
== EWOULDBLOCK
) {
565 } else if (errno
== EINPROGRESS
||
570 error_setg_errno(errp
, errno
, "can't connect socket");
579 s
= net_socket_fd_init(peer
, model
, name
, fd
, connected
, NULL
, errp
);
584 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
585 "socket: connect to %s:%d",
586 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
590 static int net_socket_mcast_init(NetClientState
*peer
,
593 const char *host_str
,
594 const char *localaddr_str
,
599 struct sockaddr_in saddr
;
600 struct in_addr localaddr
, *param_localaddr
;
602 if (parse_host_port(&saddr
, host_str
, errp
) < 0) {
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",
612 param_localaddr
= &localaddr
;
614 param_localaddr
= NULL
;
617 fd
= net_socket_mcast_create(&saddr
, param_localaddr
, errp
);
622 s
= net_socket_fd_init(peer
, model
, name
, fd
, 0, NULL
, errp
);
627 s
->dgram_dst
= saddr
;
629 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
630 "socket: mcast=%s:%d",
631 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
636 static int net_socket_udp_init(NetClientState
*peer
,
645 struct sockaddr_in laddr
, raddr
;
647 if (parse_host_port(&laddr
, lhost
, errp
) < 0) {
651 if (parse_host_port(&raddr
, rhost
, errp
) < 0) {
655 fd
= qemu_socket(PF_INET
, SOCK_DGRAM
, 0);
657 error_setg_errno(errp
, errno
, "can't create datagram socket");
661 ret
= socket_set_fast_reuse(fd
);
663 error_setg_errno(errp
, errno
,
664 "can't set socket option SO_REUSEADDR");
668 ret
= bind(fd
, (struct sockaddr
*)&laddr
, sizeof(laddr
));
670 error_setg_errno(errp
, errno
, "can't bind ip=%s to socket",
671 inet_ntoa(laddr
.sin_addr
));
675 qemu_set_nonblock(fd
);
677 s
= net_socket_fd_init(peer
, model
, name
, fd
, 0, NULL
, errp
);
682 s
->dgram_dst
= raddr
;
684 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
686 inet_ntoa(raddr
.sin_addr
), ntohs(raddr
.sin_port
));
690 int net_init_socket(const Netdev
*netdev
, const char *name
,
691 NetClientState
*peer
, Error
**errp
)
693 const NetdevSocketOptions
*sock
;
695 assert(netdev
->type
== NET_CLIENT_DRIVER_SOCKET
);
696 sock
= &netdev
->u
.socket
;
698 if (sock
->has_fd
+ sock
->has_listen
+ sock
->has_connect
+ sock
->has_mcast
+
699 sock
->has_udp
!= 1) {
700 error_setg(errp
, "exactly one of listen=, connect=, mcast= or udp="
705 if (sock
->has_localaddr
&& !sock
->has_mcast
&& !sock
->has_udp
) {
706 error_setg(errp
, "localaddr= is only valid with mcast= or udp=");
713 fd
= monitor_fd_param(cur_mon
, sock
->fd
, errp
);
717 qemu_set_nonblock(fd
);
718 if (!net_socket_fd_init(peer
, "socket", name
, fd
, 1, sock
->mcast
,
725 if (sock
->has_listen
) {
726 if (net_socket_listen_init(peer
, "socket", name
, sock
->listen
, errp
)
733 if (sock
->has_connect
) {
734 if (net_socket_connect_init(peer
, "socket", name
, sock
->connect
, errp
)
741 if (sock
->has_mcast
) {
742 /* if sock->localaddr is missing, it has been initialized to "all bits
744 if (net_socket_mcast_init(peer
, "socket", name
, sock
->mcast
,
745 sock
->localaddr
, errp
) < 0) {
751 assert(sock
->has_udp
);
752 if (!sock
->has_localaddr
) {
753 error_setg(errp
, "localaddr= is mandatory with udp=");
756 if (net_socket_udp_init(peer
, "socket", name
, sock
->udp
, sock
->localaddr
,