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
);
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
, struct in_addr
*localaddr
)
223 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
224 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) "
225 "does not contain a multicast address\n",
226 inet_ntoa(mcastaddr
->sin_addr
),
227 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
231 fd
= qemu_socket(PF_INET
, SOCK_DGRAM
, 0);
233 perror("socket(PF_INET, SOCK_DGRAM)");
237 /* Allow multiple sockets to bind the same multicast ip and port by setting
238 * SO_REUSEADDR. This is the only situation where SO_REUSEADDR should be set
239 * on windows. Use socket_set_fast_reuse otherwise as it sets SO_REUSEADDR
240 * only on posix systems.
243 ret
= qemu_setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
245 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
249 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
255 /* Add host to multicast group */
256 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
258 imr
.imr_interface
= *localaddr
;
260 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
263 ret
= qemu_setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
264 &imr
, sizeof(struct ip_mreq
));
266 perror("setsockopt(IP_ADD_MEMBERSHIP)");
270 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
272 ret
= qemu_setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
273 &loop
, sizeof(loop
));
275 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
279 /* If a bind address is given, only send packets from that address */
280 if (localaddr
!= NULL
) {
281 ret
= qemu_setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_IF
,
282 localaddr
, sizeof(*localaddr
));
284 perror("setsockopt(IP_MULTICAST_IF)");
289 qemu_set_nonblock(fd
);
297 static void net_socket_cleanup(NetClientState
*nc
)
299 NetSocketState
*s
= DO_UPCAST(NetSocketState
, nc
, nc
);
301 net_socket_read_poll(s
, false);
302 net_socket_write_poll(s
, false);
306 if (s
->listen_fd
!= -1) {
307 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
308 closesocket(s
->listen_fd
);
313 static NetClientInfo net_dgram_socket_info
= {
314 .type
= NET_CLIENT_DRIVER_SOCKET
,
315 .size
= sizeof(NetSocketState
),
316 .receive
= net_socket_receive_dgram
,
317 .cleanup
= net_socket_cleanup
,
320 static NetSocketState
*net_socket_fd_init_dgram(NetClientState
*peer
,
323 int fd
, int is_connected
)
325 struct sockaddr_in saddr
;
327 socklen_t saddr_len
= sizeof(saddr
);
331 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
332 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
333 * by ONLY ONE process: we must "clone" this dgram socket --jjo
337 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
339 if (saddr
.sin_addr
.s_addr
== 0) {
340 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, "
341 "cannot setup multicast dst addr\n", fd
);
344 /* clone dgram socket */
345 newfd
= net_socket_mcast_create(&saddr
, NULL
);
347 /* error already reported by net_socket_mcast_create() */
350 /* clone newfd to fd, close newfd */
356 "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
357 fd
, strerror(errno
));
362 nc
= qemu_new_net_client(&net_dgram_socket_info
, peer
, model
, name
);
364 s
= DO_UPCAST(NetSocketState
, nc
, nc
);
368 s
->send_fn
= net_socket_send_dgram
;
369 net_socket_rs_init(&s
->rs
, net_socket_rs_finalize
);
370 net_socket_read_poll(s
, true);
372 /* mcast: save bound address as dst */
374 s
->dgram_dst
= saddr
;
375 snprintf(nc
->info_str
, sizeof(nc
->info_str
),
376 "socket: fd=%d (cloned mcast=%s:%d)",
377 fd
, inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
379 snprintf(nc
->info_str
, sizeof(nc
->info_str
),
380 "socket: fd=%d", fd
);
390 static void net_socket_connect(void *opaque
)
392 NetSocketState
*s
= opaque
;
393 s
->send_fn
= net_socket_send
;
394 net_socket_read_poll(s
, true);
397 static NetClientInfo net_socket_info
= {
398 .type
= NET_CLIENT_DRIVER_SOCKET
,
399 .size
= sizeof(NetSocketState
),
400 .receive
= net_socket_receive
,
401 .cleanup
= net_socket_cleanup
,
404 static NetSocketState
*net_socket_fd_init_stream(NetClientState
*peer
,
407 int fd
, int is_connected
)
412 nc
= qemu_new_net_client(&net_socket_info
, peer
, model
, name
);
414 snprintf(nc
->info_str
, sizeof(nc
->info_str
), "socket: fd=%d", fd
);
416 s
= DO_UPCAST(NetSocketState
, nc
, nc
);
420 net_socket_rs_init(&s
->rs
, net_socket_rs_finalize
);
422 /* Disable Nagle algorithm on TCP sockets to reduce latency */
423 socket_set_nodelay(fd
);
426 net_socket_connect(s
);
428 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
433 static NetSocketState
*net_socket_fd_init(NetClientState
*peer
,
434 const char *model
, const char *name
,
435 int fd
, int is_connected
)
437 int so_type
= -1, optlen
=sizeof(so_type
);
439 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
440 (socklen_t
*)&optlen
)< 0) {
441 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n",
448 return net_socket_fd_init_dgram(peer
, model
, name
, fd
, is_connected
);
450 return net_socket_fd_init_stream(peer
, model
, name
, fd
, is_connected
);
452 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
453 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
454 return net_socket_fd_init_stream(peer
, model
, name
, fd
, is_connected
);
459 static void net_socket_accept(void *opaque
)
461 NetSocketState
*s
= opaque
;
462 struct sockaddr_in saddr
;
468 fd
= qemu_accept(s
->listen_fd
, (struct sockaddr
*)&saddr
, &len
);
469 if (fd
< 0 && errno
!= EINTR
) {
471 } else if (fd
>= 0) {
472 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
478 s
->nc
.link_down
= false;
479 net_socket_connect(s
);
480 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
481 "socket: connection from %s:%d",
482 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
485 static int net_socket_listen_init(NetClientState
*peer
,
488 const char *host_str
)
492 SocketAddress
*saddr
;
494 Error
*local_error
= NULL
;
496 saddr
= socket_parse(host_str
, &local_error
);
498 error_report_err(local_error
);
502 ret
= socket_listen(saddr
, &local_error
);
504 qapi_free_SocketAddress(saddr
);
505 error_report_err(local_error
);
509 nc
= qemu_new_net_client(&net_socket_info
, peer
, model
, name
);
510 s
= DO_UPCAST(NetSocketState
, nc
, nc
);
513 s
->nc
.link_down
= true;
514 net_socket_rs_init(&s
->rs
, net_socket_rs_finalize
);
516 qemu_set_fd_handler(s
->listen_fd
, net_socket_accept
, NULL
, s
);
517 qapi_free_SocketAddress(saddr
);
522 NetClientState
*peer
;
523 SocketAddress
*saddr
;
526 } socket_connect_data
;
528 static void socket_connect_data_free(socket_connect_data
*c
)
530 qapi_free_SocketAddress(c
->saddr
);
536 static void net_socket_connected(int fd
, Error
*err
, void *opaque
)
538 socket_connect_data
*c
= opaque
;
540 char *addr_str
= NULL
;
541 Error
*local_error
= NULL
;
543 addr_str
= socket_address_to_string(c
->saddr
, &local_error
);
544 if (addr_str
== NULL
) {
545 error_report_err(local_error
);
550 s
= net_socket_fd_init(c
->peer
, c
->model
, c
->name
, fd
, true);
556 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
557 "socket: connect to %s", addr_str
);
561 socket_connect_data_free(c
);
564 static int net_socket_connect_init(NetClientState
*peer
,
567 const char *host_str
)
569 socket_connect_data
*c
= g_new0(socket_connect_data
, 1);
571 Error
*local_error
= NULL
;
574 c
->model
= g_strdup(model
);
575 c
->name
= g_strdup(name
);
576 c
->saddr
= socket_parse(host_str
, &local_error
);
577 if (c
->saddr
== NULL
) {
581 fd
= socket_connect(c
->saddr
, &local_error
, net_socket_connected
, c
);
589 error_report_err(local_error
);
590 socket_connect_data_free(c
);
594 static int net_socket_mcast_init(NetClientState
*peer
,
597 const char *host_str
,
598 const char *localaddr_str
)
602 struct sockaddr_in saddr
;
603 struct in_addr localaddr
, *param_localaddr
;
605 if (parse_host_port(&saddr
, host_str
) < 0)
608 if (localaddr_str
!= NULL
) {
609 if (inet_aton(localaddr_str
, &localaddr
) == 0)
611 param_localaddr
= &localaddr
;
613 param_localaddr
= NULL
;
616 fd
= net_socket_mcast_create(&saddr
, param_localaddr
);
620 s
= net_socket_fd_init(peer
, model
, name
, fd
, 0);
624 s
->dgram_dst
= saddr
;
626 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
627 "socket: mcast=%s:%d",
628 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
633 static int net_socket_udp_init(NetClientState
*peer
,
641 struct sockaddr_in laddr
, raddr
;
643 if (parse_host_port(&laddr
, lhost
) < 0) {
647 if (parse_host_port(&raddr
, rhost
) < 0) {
651 fd
= qemu_socket(PF_INET
, SOCK_DGRAM
, 0);
653 perror("socket(PF_INET, SOCK_DGRAM)");
657 ret
= socket_set_fast_reuse(fd
);
662 ret
= bind(fd
, (struct sockaddr
*)&laddr
, sizeof(laddr
));
668 qemu_set_nonblock(fd
);
670 s
= net_socket_fd_init(peer
, model
, name
, fd
, 0);
675 s
->dgram_dst
= raddr
;
677 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
679 inet_ntoa(raddr
.sin_addr
), ntohs(raddr
.sin_port
));
683 int net_init_socket(const Netdev
*netdev
, const char *name
,
684 NetClientState
*peer
, Error
**errp
)
686 /* FIXME error_setg(errp, ...) on failure */
688 const NetdevSocketOptions
*sock
;
690 assert(netdev
->type
== NET_CLIENT_DRIVER_SOCKET
);
691 sock
= &netdev
->u
.socket
;
693 if (sock
->has_fd
+ sock
->has_listen
+ sock
->has_connect
+ sock
->has_mcast
+
694 sock
->has_udp
!= 1) {
695 error_report("exactly one of fd=, listen=, connect=, mcast= or udp="
700 if (sock
->has_localaddr
&& !sock
->has_mcast
&& !sock
->has_udp
) {
701 error_report("localaddr= is only valid with mcast= or udp=");
708 fd
= monitor_fd_param(cur_mon
, sock
->fd
, &err
);
710 error_report_err(err
);
713 qemu_set_nonblock(fd
);
714 if (!net_socket_fd_init(peer
, "socket", name
, fd
, 1)) {
720 if (sock
->has_listen
) {
721 if (net_socket_listen_init(peer
, "socket", name
, sock
->listen
) == -1) {
727 if (sock
->has_connect
) {
728 if (net_socket_connect_init(peer
, "socket", name
, sock
->connect
) ==
735 if (sock
->has_mcast
) {
736 /* if sock->localaddr is missing, it has been initialized to "all bits
738 if (net_socket_mcast_init(peer
, "socket", name
, sock
->mcast
,
739 sock
->localaddr
) == -1) {
745 assert(sock
->has_udp
);
746 if (!sock
->has_localaddr
) {
747 error_report("localaddr= is mandatory with udp=");
750 if (net_socket_udp_init(peer
, "socket", name
, sock
->udp
, sock
->localaddr
) ==