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 "config-host.h"
28 #include "monitor/monitor.h"
29 #include "qemu-common.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
{
40 int state
; /* 0 = getting length, 1 = getting data */
42 unsigned int packet_len
;
43 unsigned int send_index
; /* number of bytes sent (only SOCK_STREAM) */
44 uint8_t buf
[NET_BUFSIZE
];
45 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
46 IOHandler
*send_fn
; /* differs between SOCK_STREAM/SOCK_DGRAM */
47 bool read_poll
; /* waiting to receive data? */
48 bool write_poll
; /* waiting to transmit data? */
51 static void net_socket_accept(void *opaque
);
52 static void net_socket_writable(void *opaque
);
54 /* Only read packets from socket when peer can receive them */
55 static int net_socket_can_send(void *opaque
)
57 NetSocketState
*s
= opaque
;
59 return qemu_can_send_packet(&s
->nc
);
62 static void net_socket_update_fd_handler(NetSocketState
*s
)
64 qemu_set_fd_handler2(s
->fd
,
65 s
->read_poll
? net_socket_can_send
: NULL
,
66 s
->read_poll
? s
->send_fn
: NULL
,
67 s
->write_poll
? net_socket_writable
: NULL
,
71 static void net_socket_read_poll(NetSocketState
*s
, bool enable
)
73 s
->read_poll
= enable
;
74 net_socket_update_fd_handler(s
);
77 static void net_socket_write_poll(NetSocketState
*s
, bool enable
)
79 s
->write_poll
= enable
;
80 net_socket_update_fd_handler(s
);
83 static void net_socket_writable(void *opaque
)
85 NetSocketState
*s
= opaque
;
87 net_socket_write_poll(s
, false);
89 qemu_flush_queued_packets(&s
->nc
);
92 static ssize_t
net_socket_receive(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
94 NetSocketState
*s
= DO_UPCAST(NetSocketState
, nc
, nc
);
95 uint32_t len
= htonl(size
);
96 struct iovec iov
[] = {
99 .iov_len
= sizeof(len
),
101 .iov_base
= (void *)buf
,
108 remaining
= iov_size(iov
, 2) - s
->send_index
;
109 ret
= iov_send(s
->fd
, iov
, 2, s
->send_index
, remaining
);
111 if (ret
== -1 && errno
== EAGAIN
) {
112 ret
= 0; /* handled further down */
118 if (ret
< (ssize_t
)remaining
) {
119 s
->send_index
+= ret
;
120 net_socket_write_poll(s
, true);
127 static ssize_t
net_socket_receive_dgram(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
129 NetSocketState
*s
= DO_UPCAST(NetSocketState
, nc
, nc
);
133 ret
= qemu_sendto(s
->fd
, buf
, size
, 0,
134 (struct sockaddr
*)&s
->dgram_dst
,
135 sizeof(s
->dgram_dst
));
136 } while (ret
== -1 && errno
== EINTR
);
138 if (ret
== -1 && errno
== EAGAIN
) {
139 net_socket_write_poll(s
, true);
145 static void net_socket_send(void *opaque
)
147 NetSocketState
*s
= opaque
;
150 uint8_t buf1
[NET_BUFSIZE
];
153 size
= qemu_recv(s
->fd
, buf1
, sizeof(buf1
), 0);
155 err
= socket_error();
156 if (err
!= EWOULDBLOCK
)
158 } else if (size
== 0) {
159 /* end of connection */
161 net_socket_read_poll(s
, false);
162 net_socket_write_poll(s
, false);
163 if (s
->listen_fd
!= -1) {
164 qemu_set_fd_handler(s
->listen_fd
, net_socket_accept
, NULL
, s
);
172 s
->nc
.link_down
= true;
173 memset(s
->buf
, 0, sizeof(s
->buf
));
174 memset(s
->nc
.info_str
, 0, sizeof(s
->nc
.info_str
));
180 /* reassemble a packet from the network */
186 memcpy(s
->buf
+ s
->index
, buf
, l
);
192 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
198 l
= s
->packet_len
- s
->index
;
201 if (s
->index
+ l
<= sizeof(s
->buf
)) {
202 memcpy(s
->buf
+ s
->index
, buf
, l
);
204 fprintf(stderr
, "serious error: oversized packet received,"
205 "connection terminated.\n");
213 if (s
->index
>= s
->packet_len
) {
214 qemu_send_packet(&s
->nc
, s
->buf
, s
->packet_len
);
223 static void net_socket_send_dgram(void *opaque
)
225 NetSocketState
*s
= opaque
;
228 size
= qemu_recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
232 /* end of connection */
233 net_socket_read_poll(s
, false);
234 net_socket_write_poll(s
, false);
237 qemu_send_packet(&s
->nc
, s
->buf
, size
);
240 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
, struct in_addr
*localaddr
)
251 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
252 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) "
253 "does not contain a multicast address\n",
254 inet_ntoa(mcastaddr
->sin_addr
),
255 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
259 fd
= qemu_socket(PF_INET
, SOCK_DGRAM
, 0);
261 perror("socket(PF_INET, SOCK_DGRAM)");
265 /* Allow multiple sockets to bind the same multicast ip and port by setting
266 * SO_REUSEADDR. This is the only situation where SO_REUSEADDR should be set
267 * on windows. Use socket_set_fast_reuse otherwise as it sets SO_REUSEADDR
268 * only on posix systems.
271 ret
= qemu_setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
273 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
277 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
283 /* Add host to multicast group */
284 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
286 imr
.imr_interface
= *localaddr
;
288 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
291 ret
= qemu_setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
292 &imr
, sizeof(struct ip_mreq
));
294 perror("setsockopt(IP_ADD_MEMBERSHIP)");
298 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
300 ret
= qemu_setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
301 &loop
, sizeof(loop
));
303 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
307 /* If a bind address is given, only send packets from that address */
308 if (localaddr
!= NULL
) {
309 ret
= qemu_setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_IF
,
310 localaddr
, sizeof(*localaddr
));
312 perror("setsockopt(IP_MULTICAST_IF)");
317 qemu_set_nonblock(fd
);
325 static void net_socket_cleanup(NetClientState
*nc
)
327 NetSocketState
*s
= DO_UPCAST(NetSocketState
, nc
, nc
);
329 net_socket_read_poll(s
, false);
330 net_socket_write_poll(s
, false);
334 if (s
->listen_fd
!= -1) {
335 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
336 closesocket(s
->listen_fd
);
341 static NetClientInfo net_dgram_socket_info
= {
342 .type
= NET_CLIENT_OPTIONS_KIND_SOCKET
,
343 .size
= sizeof(NetSocketState
),
344 .receive
= net_socket_receive_dgram
,
345 .cleanup
= net_socket_cleanup
,
348 static NetSocketState
*net_socket_fd_init_dgram(NetClientState
*peer
,
351 int fd
, int is_connected
)
353 struct sockaddr_in saddr
;
355 socklen_t saddr_len
= sizeof(saddr
);
359 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
360 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
361 * by ONLY ONE process: we must "clone" this dgram socket --jjo
365 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
367 if (saddr
.sin_addr
.s_addr
== 0) {
368 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, "
369 "cannot setup multicast dst addr\n", fd
);
372 /* clone dgram socket */
373 newfd
= net_socket_mcast_create(&saddr
, NULL
);
375 /* error already reported by net_socket_mcast_create() */
378 /* clone newfd to fd, close newfd */
384 "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
385 fd
, strerror(errno
));
390 nc
= qemu_new_net_client(&net_dgram_socket_info
, peer
, model
, name
);
392 snprintf(nc
->info_str
, sizeof(nc
->info_str
),
393 "socket: fd=%d (%s mcast=%s:%d)",
394 fd
, is_connected
? "cloned" : "",
395 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
397 s
= DO_UPCAST(NetSocketState
, nc
, nc
);
401 s
->send_fn
= net_socket_send_dgram
;
402 net_socket_read_poll(s
, true);
404 /* mcast: save bound address as dst */
406 s
->dgram_dst
= saddr
;
416 static void net_socket_connect(void *opaque
)
418 NetSocketState
*s
= opaque
;
419 s
->send_fn
= net_socket_send
;
420 net_socket_read_poll(s
, true);
423 static NetClientInfo net_socket_info
= {
424 .type
= NET_CLIENT_OPTIONS_KIND_SOCKET
,
425 .size
= sizeof(NetSocketState
),
426 .receive
= net_socket_receive
,
427 .cleanup
= net_socket_cleanup
,
430 static NetSocketState
*net_socket_fd_init_stream(NetClientState
*peer
,
433 int fd
, int is_connected
)
438 nc
= qemu_new_net_client(&net_socket_info
, peer
, model
, name
);
440 snprintf(nc
->info_str
, sizeof(nc
->info_str
), "socket: fd=%d", fd
);
442 s
= DO_UPCAST(NetSocketState
, nc
, nc
);
447 /* Disable Nagle algorithm on TCP sockets to reduce latency */
448 socket_set_nodelay(fd
);
451 net_socket_connect(s
);
453 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
458 static NetSocketState
*net_socket_fd_init(NetClientState
*peer
,
459 const char *model
, const char *name
,
460 int fd
, int is_connected
)
462 int so_type
= -1, optlen
=sizeof(so_type
);
464 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
465 (socklen_t
*)&optlen
)< 0) {
466 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n",
473 return net_socket_fd_init_dgram(peer
, model
, name
, fd
, is_connected
);
475 return net_socket_fd_init_stream(peer
, model
, name
, fd
, is_connected
);
477 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
478 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
479 return net_socket_fd_init_stream(peer
, model
, name
, fd
, is_connected
);
484 static void net_socket_accept(void *opaque
)
486 NetSocketState
*s
= opaque
;
487 struct sockaddr_in saddr
;
493 fd
= qemu_accept(s
->listen_fd
, (struct sockaddr
*)&saddr
, &len
);
494 if (fd
< 0 && errno
!= EINTR
) {
496 } else if (fd
>= 0) {
497 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
503 s
->nc
.link_down
= false;
504 net_socket_connect(s
);
505 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
506 "socket: connection from %s:%d",
507 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
510 static int net_socket_listen_init(NetClientState
*peer
,
513 const char *host_str
)
517 struct sockaddr_in saddr
;
520 if (parse_host_port(&saddr
, host_str
) < 0)
523 fd
= qemu_socket(PF_INET
, SOCK_STREAM
, 0);
528 qemu_set_nonblock(fd
);
530 socket_set_fast_reuse(fd
);
532 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
545 nc
= qemu_new_net_client(&net_socket_info
, peer
, model
, name
);
546 s
= DO_UPCAST(NetSocketState
, nc
, nc
);
549 s
->nc
.link_down
= true;
551 qemu_set_fd_handler(s
->listen_fd
, net_socket_accept
, NULL
, s
);
555 static int net_socket_connect_init(NetClientState
*peer
,
558 const char *host_str
)
561 int fd
, connected
, ret
, err
;
562 struct sockaddr_in saddr
;
564 if (parse_host_port(&saddr
, host_str
) < 0)
567 fd
= qemu_socket(PF_INET
, SOCK_STREAM
, 0);
572 qemu_set_nonblock(fd
);
576 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
578 err
= socket_error();
579 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
580 } else if (err
== EINPROGRESS
) {
583 } else if (err
== WSAEALREADY
|| err
== WSAEINVAL
) {
596 s
= net_socket_fd_init(peer
, model
, name
, fd
, connected
);
599 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
600 "socket: connect to %s:%d",
601 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
605 static int net_socket_mcast_init(NetClientState
*peer
,
608 const char *host_str
,
609 const char *localaddr_str
)
613 struct sockaddr_in saddr
;
614 struct in_addr localaddr
, *param_localaddr
;
616 if (parse_host_port(&saddr
, host_str
) < 0)
619 if (localaddr_str
!= NULL
) {
620 if (inet_aton(localaddr_str
, &localaddr
) == 0)
622 param_localaddr
= &localaddr
;
624 param_localaddr
= NULL
;
627 fd
= net_socket_mcast_create(&saddr
, param_localaddr
);
631 s
= net_socket_fd_init(peer
, model
, name
, fd
, 0);
635 s
->dgram_dst
= saddr
;
637 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
638 "socket: mcast=%s:%d",
639 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
644 static int net_socket_udp_init(NetClientState
*peer
,
652 struct sockaddr_in laddr
, raddr
;
654 if (parse_host_port(&laddr
, lhost
) < 0) {
658 if (parse_host_port(&raddr
, rhost
) < 0) {
662 fd
= qemu_socket(PF_INET
, SOCK_DGRAM
, 0);
664 perror("socket(PF_INET, SOCK_DGRAM)");
668 ret
= socket_set_fast_reuse(fd
);
673 ret
= bind(fd
, (struct sockaddr
*)&laddr
, sizeof(laddr
));
679 qemu_set_nonblock(fd
);
681 s
= net_socket_fd_init(peer
, model
, name
, fd
, 0);
686 s
->dgram_dst
= raddr
;
688 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
690 inet_ntoa(raddr
.sin_addr
), ntohs(raddr
.sin_port
));
694 int net_init_socket(const NetClientOptions
*opts
, const char *name
,
695 NetClientState
*peer
)
697 const NetdevSocketOptions
*sock
;
699 assert(opts
->kind
== NET_CLIENT_OPTIONS_KIND_SOCKET
);
702 if (sock
->has_fd
+ sock
->has_listen
+ sock
->has_connect
+ sock
->has_mcast
+
703 sock
->has_udp
!= 1) {
704 error_report("exactly one of fd=, listen=, connect=, mcast= or udp="
709 if (sock
->has_localaddr
&& !sock
->has_mcast
&& !sock
->has_udp
) {
710 error_report("localaddr= is only valid with mcast= or udp=");
717 fd
= monitor_handle_fd_param(cur_mon
, sock
->fd
);
721 qemu_set_nonblock(fd
);
722 if (!net_socket_fd_init(peer
, "socket", name
, fd
, 1)) {
728 if (sock
->has_listen
) {
729 if (net_socket_listen_init(peer
, "socket", name
, sock
->listen
) == -1) {
735 if (sock
->has_connect
) {
736 if (net_socket_connect_init(peer
, "socket", name
, sock
->connect
) ==
743 if (sock
->has_mcast
) {
744 /* if sock->localaddr is missing, it has been initialized to "all bits
746 if (net_socket_mcast_init(peer
, "socket", name
, sock
->mcast
,
747 sock
->localaddr
) == -1) {
753 assert(sock
->has_udp
);
754 if (!sock
->has_localaddr
) {
755 error_report("localaddr= is mandatory with udp=");
758 if (net_socket_udp_init(peer
, "socket", name
, sock
->udp
, sock
->localaddr
) ==