2009-11-25 21:48:56 +03:00
|
|
|
/*
|
|
|
|
* QEMU System Emulator
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003-2008 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "config-host.h"
|
|
|
|
|
2012-10-24 10:43:34 +04:00
|
|
|
#include "net/net.h"
|
2012-09-17 20:43:51 +04:00
|
|
|
#include "clients.h"
|
2012-12-17 21:19:49 +04:00
|
|
|
#include "monitor/monitor.h"
|
2009-11-25 21:48:56 +03:00
|
|
|
#include "qemu-common.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/error-report.h"
|
|
|
|
#include "qemu/option.h"
|
|
|
|
#include "qemu/sockets.h"
|
|
|
|
#include "qemu/iov.h"
|
2013-08-21 19:02:47 +04:00
|
|
|
#include "qemu/main-loop.h"
|
2009-11-25 21:48:56 +03:00
|
|
|
|
|
|
|
typedef struct NetSocketState {
|
2012-07-24 19:35:13 +04:00
|
|
|
NetClientState nc;
|
net: add the support for -netdev socket, listen
The -net socket,listen option does not work with the newer -netdev
syntax:
http://lists.gnu.org/archive/html/qemu-devel/2011-11/msg01508.html
This patch makes it work now.
For the case where one vlan has multiple listenning sockets,
the patch will also provide the support.
Supported syntax:
1.) -net socket,listen=127.0.0.1:1234,vlan=0
2.) -net socket,listen=127.0.0.1:1234,vlan=0 -net socket,listen=127.0.0.1:1235,vlan=0
3.) -netdev socket,listen=127.0.0.1:1234,id=socket0
Drop the NetSocketListenState struct and add a listen_fd field
to NetSocketState. When a -netdev socket,listen= instance is created
there will be a NetSocketState with fd=-1 and a valid listen_fd. The
net_socket_accept() handler waits for listen_fd to become readable and
then accepts the connection. When this state transition happens, we no
longer monitor listen_fd for incoming connections...until the client
disconnects again.
Signed-off-by: Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
2012-07-20 17:25:53 +04:00
|
|
|
int listen_fd;
|
2009-11-25 21:48:56 +03:00
|
|
|
int fd;
|
|
|
|
int state; /* 0 = getting length, 1 = getting data */
|
|
|
|
unsigned int index;
|
|
|
|
unsigned int packet_len;
|
2012-08-20 13:14:35 +04:00
|
|
|
unsigned int send_index; /* number of bytes sent (only SOCK_STREAM) */
|
2013-03-18 22:43:44 +04:00
|
|
|
uint8_t buf[NET_BUFSIZE];
|
2009-11-25 21:48:56 +03:00
|
|
|
struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
|
2012-08-20 13:21:54 +04:00
|
|
|
IOHandler *send_fn; /* differs between SOCK_STREAM/SOCK_DGRAM */
|
|
|
|
bool read_poll; /* waiting to receive data? */
|
|
|
|
bool write_poll; /* waiting to transmit data? */
|
2009-11-25 21:48:56 +03:00
|
|
|
} NetSocketState;
|
|
|
|
|
net: add the support for -netdev socket, listen
The -net socket,listen option does not work with the newer -netdev
syntax:
http://lists.gnu.org/archive/html/qemu-devel/2011-11/msg01508.html
This patch makes it work now.
For the case where one vlan has multiple listenning sockets,
the patch will also provide the support.
Supported syntax:
1.) -net socket,listen=127.0.0.1:1234,vlan=0
2.) -net socket,listen=127.0.0.1:1234,vlan=0 -net socket,listen=127.0.0.1:1235,vlan=0
3.) -netdev socket,listen=127.0.0.1:1234,id=socket0
Drop the NetSocketListenState struct and add a listen_fd field
to NetSocketState. When a -netdev socket,listen= instance is created
there will be a NetSocketState with fd=-1 and a valid listen_fd. The
net_socket_accept() handler waits for listen_fd to become readable and
then accepts the connection. When this state transition happens, we no
longer monitor listen_fd for incoming connections...until the client
disconnects again.
Signed-off-by: Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
2012-07-20 17:25:53 +04:00
|
|
|
static void net_socket_accept(void *opaque);
|
2012-08-20 13:21:54 +04:00
|
|
|
static void net_socket_writable(void *opaque);
|
|
|
|
|
|
|
|
/* Only read packets from socket when peer can receive them */
|
|
|
|
static int net_socket_can_send(void *opaque)
|
|
|
|
{
|
|
|
|
NetSocketState *s = opaque;
|
|
|
|
|
|
|
|
return qemu_can_send_packet(&s->nc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void net_socket_update_fd_handler(NetSocketState *s)
|
|
|
|
{
|
|
|
|
qemu_set_fd_handler2(s->fd,
|
|
|
|
s->read_poll ? net_socket_can_send : NULL,
|
|
|
|
s->read_poll ? s->send_fn : NULL,
|
|
|
|
s->write_poll ? net_socket_writable : NULL,
|
|
|
|
s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void net_socket_read_poll(NetSocketState *s, bool enable)
|
|
|
|
{
|
|
|
|
s->read_poll = enable;
|
|
|
|
net_socket_update_fd_handler(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void net_socket_write_poll(NetSocketState *s, bool enable)
|
|
|
|
{
|
|
|
|
s->write_poll = enable;
|
|
|
|
net_socket_update_fd_handler(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void net_socket_writable(void *opaque)
|
|
|
|
{
|
|
|
|
NetSocketState *s = opaque;
|
|
|
|
|
|
|
|
net_socket_write_poll(s, false);
|
|
|
|
|
|
|
|
qemu_flush_queued_packets(&s->nc);
|
|
|
|
}
|
2009-11-25 21:48:56 +03:00
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
static ssize_t net_socket_receive(NetClientState *nc, const uint8_t *buf, size_t size)
|
2009-11-25 21:48:56 +03:00
|
|
|
{
|
2009-11-25 21:49:08 +03:00
|
|
|
NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
|
2012-08-20 13:14:35 +04:00
|
|
|
uint32_t len = htonl(size);
|
|
|
|
struct iovec iov[] = {
|
|
|
|
{
|
|
|
|
.iov_base = &len,
|
|
|
|
.iov_len = sizeof(len),
|
|
|
|
}, {
|
|
|
|
.iov_base = (void *)buf,
|
|
|
|
.iov_len = size,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
size_t remaining;
|
|
|
|
ssize_t ret;
|
2009-11-25 21:48:56 +03:00
|
|
|
|
2012-08-20 13:14:35 +04:00
|
|
|
remaining = iov_size(iov, 2) - s->send_index;
|
|
|
|
ret = iov_send(s->fd, iov, 2, s->send_index, remaining);
|
|
|
|
|
|
|
|
if (ret == -1 && errno == EAGAIN) {
|
|
|
|
ret = 0; /* handled further down */
|
|
|
|
}
|
|
|
|
if (ret == -1) {
|
|
|
|
s->send_index = 0;
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
if (ret < (ssize_t)remaining) {
|
|
|
|
s->send_index += ret;
|
|
|
|
net_socket_write_poll(s, true);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
s->send_index = 0;
|
|
|
|
return size;
|
2009-11-25 21:48:56 +03:00
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf, size_t size)
|
2009-11-25 21:48:56 +03:00
|
|
|
{
|
2009-11-25 21:49:08 +03:00
|
|
|
NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
|
2012-08-20 13:28:53 +04:00
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
do {
|
2012-09-22 23:13:28 +04:00
|
|
|
ret = qemu_sendto(s->fd, buf, size, 0,
|
|
|
|
(struct sockaddr *)&s->dgram_dst,
|
|
|
|
sizeof(s->dgram_dst));
|
2012-08-20 13:28:53 +04:00
|
|
|
} while (ret == -1 && errno == EINTR);
|
|
|
|
|
|
|
|
if (ret == -1 && errno == EAGAIN) {
|
|
|
|
net_socket_write_poll(s, true);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ret;
|
2009-11-25 21:48:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void net_socket_send(void *opaque)
|
|
|
|
{
|
|
|
|
NetSocketState *s = opaque;
|
|
|
|
int size, err;
|
|
|
|
unsigned l;
|
2013-03-18 22:43:44 +04:00
|
|
|
uint8_t buf1[NET_BUFSIZE];
|
2009-11-25 21:48:56 +03:00
|
|
|
const uint8_t *buf;
|
|
|
|
|
2011-07-24 00:04:29 +04:00
|
|
|
size = qemu_recv(s->fd, buf1, sizeof(buf1), 0);
|
2009-11-25 21:48:56 +03:00
|
|
|
if (size < 0) {
|
|
|
|
err = socket_error();
|
|
|
|
if (err != EWOULDBLOCK)
|
|
|
|
goto eoc;
|
|
|
|
} else if (size == 0) {
|
|
|
|
/* end of connection */
|
|
|
|
eoc:
|
2012-08-20 13:21:54 +04:00
|
|
|
net_socket_read_poll(s, false);
|
|
|
|
net_socket_write_poll(s, false);
|
net: add the support for -netdev socket, listen
The -net socket,listen option does not work with the newer -netdev
syntax:
http://lists.gnu.org/archive/html/qemu-devel/2011-11/msg01508.html
This patch makes it work now.
For the case where one vlan has multiple listenning sockets,
the patch will also provide the support.
Supported syntax:
1.) -net socket,listen=127.0.0.1:1234,vlan=0
2.) -net socket,listen=127.0.0.1:1234,vlan=0 -net socket,listen=127.0.0.1:1235,vlan=0
3.) -netdev socket,listen=127.0.0.1:1234,id=socket0
Drop the NetSocketListenState struct and add a listen_fd field
to NetSocketState. When a -netdev socket,listen= instance is created
there will be a NetSocketState with fd=-1 and a valid listen_fd. The
net_socket_accept() handler waits for listen_fd to become readable and
then accepts the connection. When this state transition happens, we no
longer monitor listen_fd for incoming connections...until the client
disconnects again.
Signed-off-by: Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
2012-07-20 17:25:53 +04:00
|
|
|
if (s->listen_fd != -1) {
|
|
|
|
qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
|
|
|
|
}
|
2009-11-25 21:48:56 +03:00
|
|
|
closesocket(s->fd);
|
net: add the support for -netdev socket, listen
The -net socket,listen option does not work with the newer -netdev
syntax:
http://lists.gnu.org/archive/html/qemu-devel/2011-11/msg01508.html
This patch makes it work now.
For the case where one vlan has multiple listenning sockets,
the patch will also provide the support.
Supported syntax:
1.) -net socket,listen=127.0.0.1:1234,vlan=0
2.) -net socket,listen=127.0.0.1:1234,vlan=0 -net socket,listen=127.0.0.1:1235,vlan=0
3.) -netdev socket,listen=127.0.0.1:1234,id=socket0
Drop the NetSocketListenState struct and add a listen_fd field
to NetSocketState. When a -netdev socket,listen= instance is created
there will be a NetSocketState with fd=-1 and a valid listen_fd. The
net_socket_accept() handler waits for listen_fd to become readable and
then accepts the connection. When this state transition happens, we no
longer monitor listen_fd for incoming connections...until the client
disconnects again.
Signed-off-by: Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
2012-07-20 17:25:53 +04:00
|
|
|
|
|
|
|
s->fd = -1;
|
|
|
|
s->state = 0;
|
|
|
|
s->index = 0;
|
|
|
|
s->packet_len = 0;
|
|
|
|
s->nc.link_down = true;
|
|
|
|
memset(s->buf, 0, sizeof(s->buf));
|
|
|
|
memset(s->nc.info_str, 0, sizeof(s->nc.info_str));
|
|
|
|
|
2009-11-25 21:48:56 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
buf = buf1;
|
|
|
|
while (size > 0) {
|
|
|
|
/* reassemble a packet from the network */
|
|
|
|
switch(s->state) {
|
|
|
|
case 0:
|
|
|
|
l = 4 - s->index;
|
|
|
|
if (l > size)
|
|
|
|
l = size;
|
|
|
|
memcpy(s->buf + s->index, buf, l);
|
|
|
|
buf += l;
|
|
|
|
size -= l;
|
|
|
|
s->index += l;
|
|
|
|
if (s->index == 4) {
|
|
|
|
/* got length */
|
|
|
|
s->packet_len = ntohl(*(uint32_t *)s->buf);
|
|
|
|
s->index = 0;
|
|
|
|
s->state = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
l = s->packet_len - s->index;
|
|
|
|
if (l > size)
|
|
|
|
l = size;
|
|
|
|
if (s->index + l <= sizeof(s->buf)) {
|
|
|
|
memcpy(s->buf + s->index, buf, l);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "serious error: oversized packet received,"
|
|
|
|
"connection terminated.\n");
|
|
|
|
s->state = 0;
|
|
|
|
goto eoc;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->index += l;
|
|
|
|
buf += l;
|
|
|
|
size -= l;
|
|
|
|
if (s->index >= s->packet_len) {
|
2009-11-25 21:49:08 +03:00
|
|
|
qemu_send_packet(&s->nc, s->buf, s->packet_len);
|
2009-11-25 21:48:56 +03:00
|
|
|
s->index = 0;
|
|
|
|
s->state = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void net_socket_send_dgram(void *opaque)
|
|
|
|
{
|
|
|
|
NetSocketState *s = opaque;
|
|
|
|
int size;
|
|
|
|
|
2011-07-24 00:04:29 +04:00
|
|
|
size = qemu_recv(s->fd, s->buf, sizeof(s->buf), 0);
|
2009-11-25 21:48:56 +03:00
|
|
|
if (size < 0)
|
|
|
|
return;
|
|
|
|
if (size == 0) {
|
|
|
|
/* end of connection */
|
2012-08-20 13:21:54 +04:00
|
|
|
net_socket_read_poll(s, false);
|
|
|
|
net_socket_write_poll(s, false);
|
2009-11-25 21:48:56 +03:00
|
|
|
return;
|
|
|
|
}
|
2009-11-25 21:49:08 +03:00
|
|
|
qemu_send_packet(&s->nc, s->buf, size);
|
2009-11-25 21:48:56 +03:00
|
|
|
}
|
|
|
|
|
2010-12-01 22:16:47 +03:00
|
|
|
static int net_socket_mcast_create(struct sockaddr_in *mcastaddr, struct in_addr *localaddr)
|
2009-11-25 21:48:56 +03:00
|
|
|
{
|
|
|
|
struct ip_mreq imr;
|
|
|
|
int fd;
|
|
|
|
int val, ret;
|
Fix forcing multicast msgs to loopback on OpenBSD.
Fix forcing multicast msgs to loopback on OpenBSD.
e.g.
$ sudo qemu -m 128 -no-fd-bootchk \
-hda virtual.img -boot n -nographic \
-net nic,vlan=0,model=rtl8139,macaddr=52:54:00:12:34:03 \
-net user -tftp /usr/src/sys/arch/i386/compile/TEST -bootp pxeboot \
-net nic,vlan=1,model=rtl8139,macaddr=52:54:00:23:03:01 \
-net tap,vlan=1,script=no \
-net nic,vlan=3,model=rtl8139,macaddr=52:54:00:23:03:03 \
-net socket,vlan=3,mcast=230.0.0.1:10003
setsockopt(SOL_IP, IP_MULTICAST_LOOP): Invalid argument
qemu: -net socket,vlan=3,mcast=230.0.0.1:10003: Device 'socket' could not be initialized
Signed-off-by: Brad Smith <brad@comstyle.com>
Signed-off-by: Blue Swirl <blauwirbel@gmail.com>
2011-08-07 15:06:43 +04:00
|
|
|
#ifdef __OpenBSD__
|
|
|
|
unsigned char loop;
|
|
|
|
#else
|
|
|
|
int loop;
|
|
|
|
#endif
|
|
|
|
|
2009-11-25 21:48:56 +03:00
|
|
|
if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
|
2011-12-07 19:01:48 +04:00
|
|
|
fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) "
|
|
|
|
"does not contain a multicast address\n",
|
|
|
|
inet_ntoa(mcastaddr->sin_addr),
|
2009-11-25 21:48:56 +03:00
|
|
|
(int)ntohl(mcastaddr->sin_addr.s_addr));
|
2011-12-07 19:01:48 +04:00
|
|
|
return -1;
|
2009-11-25 21:48:56 +03:00
|
|
|
|
|
|
|
}
|
2009-12-02 14:24:42 +03:00
|
|
|
fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
|
2009-11-25 21:48:56 +03:00
|
|
|
if (fd < 0) {
|
|
|
|
perror("socket(PF_INET, SOCK_DGRAM)");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-10-02 14:23:14 +04:00
|
|
|
/* Allow multiple sockets to bind the same multicast ip and port by setting
|
|
|
|
* SO_REUSEADDR. This is the only situation where SO_REUSEADDR should be set
|
|
|
|
* on windows. Use socket_set_fast_reuse otherwise as it sets SO_REUSEADDR
|
|
|
|
* only on posix systems.
|
|
|
|
*/
|
2009-11-25 21:48:56 +03:00
|
|
|
val = 1;
|
2013-03-08 22:58:32 +04:00
|
|
|
ret = qemu_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
|
2009-11-25 21:48:56 +03:00
|
|
|
if (ret < 0) {
|
2011-12-07 19:01:48 +04:00
|
|
|
perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
|
|
|
|
goto fail;
|
2009-11-25 21:48:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
|
|
|
|
if (ret < 0) {
|
|
|
|
perror("bind");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add host to multicast group */
|
|
|
|
imr.imr_multiaddr = mcastaddr->sin_addr;
|
2010-12-01 22:16:47 +03:00
|
|
|
if (localaddr) {
|
|
|
|
imr.imr_interface = *localaddr;
|
|
|
|
} else {
|
|
|
|
imr.imr_interface.s_addr = htonl(INADDR_ANY);
|
|
|
|
}
|
2009-11-25 21:48:56 +03:00
|
|
|
|
2013-03-08 22:58:32 +04:00
|
|
|
ret = qemu_setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
|
|
|
|
&imr, sizeof(struct ip_mreq));
|
2009-11-25 21:48:56 +03:00
|
|
|
if (ret < 0) {
|
2011-12-07 19:01:48 +04:00
|
|
|
perror("setsockopt(IP_ADD_MEMBERSHIP)");
|
|
|
|
goto fail;
|
2009-11-25 21:48:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Force mcast msgs to loopback (eg. several QEMUs in same host */
|
Fix forcing multicast msgs to loopback on OpenBSD.
Fix forcing multicast msgs to loopback on OpenBSD.
e.g.
$ sudo qemu -m 128 -no-fd-bootchk \
-hda virtual.img -boot n -nographic \
-net nic,vlan=0,model=rtl8139,macaddr=52:54:00:12:34:03 \
-net user -tftp /usr/src/sys/arch/i386/compile/TEST -bootp pxeboot \
-net nic,vlan=1,model=rtl8139,macaddr=52:54:00:23:03:01 \
-net tap,vlan=1,script=no \
-net nic,vlan=3,model=rtl8139,macaddr=52:54:00:23:03:03 \
-net socket,vlan=3,mcast=230.0.0.1:10003
setsockopt(SOL_IP, IP_MULTICAST_LOOP): Invalid argument
qemu: -net socket,vlan=3,mcast=230.0.0.1:10003: Device 'socket' could not be initialized
Signed-off-by: Brad Smith <brad@comstyle.com>
Signed-off-by: Blue Swirl <blauwirbel@gmail.com>
2011-08-07 15:06:43 +04:00
|
|
|
loop = 1;
|
2013-03-08 22:58:32 +04:00
|
|
|
ret = qemu_setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
|
|
|
|
&loop, sizeof(loop));
|
2009-11-25 21:48:56 +03:00
|
|
|
if (ret < 0) {
|
2011-12-07 19:01:48 +04:00
|
|
|
perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
|
|
|
|
goto fail;
|
2009-11-25 21:48:56 +03:00
|
|
|
}
|
|
|
|
|
2010-12-01 22:16:47 +03:00
|
|
|
/* If a bind address is given, only send packets from that address */
|
|
|
|
if (localaddr != NULL) {
|
2013-03-08 22:58:32 +04:00
|
|
|
ret = qemu_setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
|
|
|
|
localaddr, sizeof(*localaddr));
|
2010-12-01 22:16:47 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
perror("setsockopt(IP_MULTICAST_IF)");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-27 13:10:43 +04:00
|
|
|
qemu_set_nonblock(fd);
|
2009-11-25 21:48:56 +03:00
|
|
|
return fd;
|
|
|
|
fail:
|
|
|
|
if (fd >= 0)
|
|
|
|
closesocket(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
static void net_socket_cleanup(NetClientState *nc)
|
2009-11-25 21:48:56 +03:00
|
|
|
{
|
2009-11-25 21:49:08 +03:00
|
|
|
NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
|
net: add the support for -netdev socket, listen
The -net socket,listen option does not work with the newer -netdev
syntax:
http://lists.gnu.org/archive/html/qemu-devel/2011-11/msg01508.html
This patch makes it work now.
For the case where one vlan has multiple listenning sockets,
the patch will also provide the support.
Supported syntax:
1.) -net socket,listen=127.0.0.1:1234,vlan=0
2.) -net socket,listen=127.0.0.1:1234,vlan=0 -net socket,listen=127.0.0.1:1235,vlan=0
3.) -netdev socket,listen=127.0.0.1:1234,id=socket0
Drop the NetSocketListenState struct and add a listen_fd field
to NetSocketState. When a -netdev socket,listen= instance is created
there will be a NetSocketState with fd=-1 and a valid listen_fd. The
net_socket_accept() handler waits for listen_fd to become readable and
then accepts the connection. When this state transition happens, we no
longer monitor listen_fd for incoming connections...until the client
disconnects again.
Signed-off-by: Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
2012-07-20 17:25:53 +04:00
|
|
|
if (s->fd != -1) {
|
2012-08-20 13:21:54 +04:00
|
|
|
net_socket_read_poll(s, false);
|
|
|
|
net_socket_write_poll(s, false);
|
net: add the support for -netdev socket, listen
The -net socket,listen option does not work with the newer -netdev
syntax:
http://lists.gnu.org/archive/html/qemu-devel/2011-11/msg01508.html
This patch makes it work now.
For the case where one vlan has multiple listenning sockets,
the patch will also provide the support.
Supported syntax:
1.) -net socket,listen=127.0.0.1:1234,vlan=0
2.) -net socket,listen=127.0.0.1:1234,vlan=0 -net socket,listen=127.0.0.1:1235,vlan=0
3.) -netdev socket,listen=127.0.0.1:1234,id=socket0
Drop the NetSocketListenState struct and add a listen_fd field
to NetSocketState. When a -netdev socket,listen= instance is created
there will be a NetSocketState with fd=-1 and a valid listen_fd. The
net_socket_accept() handler waits for listen_fd to become readable and
then accepts the connection. When this state transition happens, we no
longer monitor listen_fd for incoming connections...until the client
disconnects again.
Signed-off-by: Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
2012-07-20 17:25:53 +04:00
|
|
|
close(s->fd);
|
|
|
|
s->fd = -1;
|
|
|
|
}
|
|
|
|
if (s->listen_fd != -1) {
|
|
|
|
qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
|
|
|
|
closesocket(s->listen_fd);
|
|
|
|
s->listen_fd = -1;
|
|
|
|
}
|
2009-11-25 21:48:56 +03:00
|
|
|
}
|
|
|
|
|
2009-11-25 21:49:08 +03:00
|
|
|
static NetClientInfo net_dgram_socket_info = {
|
2012-07-17 18:17:12 +04:00
|
|
|
.type = NET_CLIENT_OPTIONS_KIND_SOCKET,
|
2009-11-25 21:49:08 +03:00
|
|
|
.size = sizeof(NetSocketState),
|
|
|
|
.receive = net_socket_receive_dgram,
|
|
|
|
.cleanup = net_socket_cleanup,
|
|
|
|
};
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
|
2009-11-25 21:48:56 +03:00
|
|
|
const char *model,
|
|
|
|
const char *name,
|
|
|
|
int fd, int is_connected)
|
|
|
|
{
|
|
|
|
struct sockaddr_in saddr;
|
|
|
|
int newfd;
|
2014-11-17 08:54:05 +03:00
|
|
|
socklen_t saddr_len = sizeof(saddr);
|
2012-07-24 19:35:13 +04:00
|
|
|
NetClientState *nc;
|
2009-11-25 21:48:56 +03:00
|
|
|
NetSocketState *s;
|
|
|
|
|
|
|
|
/* fd passed: multicast: "learn" dgram_dst address from bound address and save it
|
|
|
|
* Because this may be "shared" socket from a "master" process, datagrams would be recv()
|
|
|
|
* by ONLY ONE process: we must "clone" this dgram socket --jjo
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (is_connected) {
|
2011-12-07 19:01:48 +04:00
|
|
|
if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
|
|
|
|
/* must be bound */
|
|
|
|
if (saddr.sin_addr.s_addr == 0) {
|
|
|
|
fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, "
|
|
|
|
"cannot setup multicast dst addr\n", fd);
|
2011-12-07 19:01:49 +04:00
|
|
|
goto err;
|
2011-12-07 19:01:48 +04:00
|
|
|
}
|
|
|
|
/* clone dgram socket */
|
|
|
|
newfd = net_socket_mcast_create(&saddr, NULL);
|
|
|
|
if (newfd < 0) {
|
|
|
|
/* error already reported by net_socket_mcast_create() */
|
2011-12-07 19:01:49 +04:00
|
|
|
goto err;
|
2011-12-07 19:01:48 +04:00
|
|
|
}
|
|
|
|
/* clone newfd to fd, close newfd */
|
|
|
|
dup2(newfd, fd);
|
|
|
|
close(newfd);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
fprintf(stderr,
|
|
|
|
"qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
|
|
|
|
fd, strerror(errno));
|
2011-12-07 19:01:49 +04:00
|
|
|
goto err;
|
2011-12-07 19:01:48 +04:00
|
|
|
}
|
2009-11-25 21:48:56 +03:00
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:08 +04:00
|
|
|
nc = qemu_new_net_client(&net_dgram_socket_info, peer, model, name);
|
2009-11-25 21:49:08 +03:00
|
|
|
|
|
|
|
s = DO_UPCAST(NetSocketState, nc, nc);
|
|
|
|
|
2009-11-25 21:48:56 +03:00
|
|
|
s->fd = fd;
|
net: add the support for -netdev socket, listen
The -net socket,listen option does not work with the newer -netdev
syntax:
http://lists.gnu.org/archive/html/qemu-devel/2011-11/msg01508.html
This patch makes it work now.
For the case where one vlan has multiple listenning sockets,
the patch will also provide the support.
Supported syntax:
1.) -net socket,listen=127.0.0.1:1234,vlan=0
2.) -net socket,listen=127.0.0.1:1234,vlan=0 -net socket,listen=127.0.0.1:1235,vlan=0
3.) -netdev socket,listen=127.0.0.1:1234,id=socket0
Drop the NetSocketListenState struct and add a listen_fd field
to NetSocketState. When a -netdev socket,listen= instance is created
there will be a NetSocketState with fd=-1 and a valid listen_fd. The
net_socket_accept() handler waits for listen_fd to become readable and
then accepts the connection. When this state transition happens, we no
longer monitor listen_fd for incoming connections...until the client
disconnects again.
Signed-off-by: Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
2012-07-20 17:25:53 +04:00
|
|
|
s->listen_fd = -1;
|
2012-08-20 13:21:54 +04:00
|
|
|
s->send_fn = net_socket_send_dgram;
|
|
|
|
net_socket_read_poll(s, true);
|
2009-11-25 21:48:56 +03:00
|
|
|
|
|
|
|
/* mcast: save bound address as dst */
|
2012-07-20 17:25:52 +04:00
|
|
|
if (is_connected) {
|
|
|
|
s->dgram_dst = saddr;
|
2014-11-20 14:35:01 +03:00
|
|
|
snprintf(nc->info_str, sizeof(nc->info_str),
|
|
|
|
"socket: fd=%d (cloned mcast=%s:%d)",
|
|
|
|
fd, inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
|
|
|
|
} else {
|
|
|
|
snprintf(nc->info_str, sizeof(nc->info_str),
|
|
|
|
"socket: fd=%d", fd);
|
2012-07-20 17:25:52 +04:00
|
|
|
}
|
2009-11-25 21:48:56 +03:00
|
|
|
|
|
|
|
return s;
|
2011-12-07 19:01:49 +04:00
|
|
|
|
|
|
|
err:
|
|
|
|
closesocket(fd);
|
|
|
|
return NULL;
|
2009-11-25 21:48:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void net_socket_connect(void *opaque)
|
|
|
|
{
|
|
|
|
NetSocketState *s = opaque;
|
2012-08-20 13:21:54 +04:00
|
|
|
s->send_fn = net_socket_send;
|
|
|
|
net_socket_read_poll(s, true);
|
2009-11-25 21:48:56 +03:00
|
|
|
}
|
|
|
|
|
2009-11-25 21:49:08 +03:00
|
|
|
static NetClientInfo net_socket_info = {
|
2012-07-17 18:17:12 +04:00
|
|
|
.type = NET_CLIENT_OPTIONS_KIND_SOCKET,
|
2009-11-25 21:49:08 +03:00
|
|
|
.size = sizeof(NetSocketState),
|
|
|
|
.receive = net_socket_receive,
|
|
|
|
.cleanup = net_socket_cleanup,
|
|
|
|
};
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
static NetSocketState *net_socket_fd_init_stream(NetClientState *peer,
|
2009-11-25 21:48:56 +03:00
|
|
|
const char *model,
|
|
|
|
const char *name,
|
|
|
|
int fd, int is_connected)
|
|
|
|
{
|
2012-07-24 19:35:13 +04:00
|
|
|
NetClientState *nc;
|
2009-11-25 21:48:56 +03:00
|
|
|
NetSocketState *s;
|
2009-11-25 21:49:08 +03:00
|
|
|
|
2012-07-24 19:35:08 +04:00
|
|
|
nc = qemu_new_net_client(&net_socket_info, peer, model, name);
|
2009-11-25 21:49:08 +03:00
|
|
|
|
|
|
|
snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d", fd);
|
|
|
|
|
|
|
|
s = DO_UPCAST(NetSocketState, nc, nc);
|
|
|
|
|
2009-11-25 21:48:56 +03:00
|
|
|
s->fd = fd;
|
net: add the support for -netdev socket, listen
The -net socket,listen option does not work with the newer -netdev
syntax:
http://lists.gnu.org/archive/html/qemu-devel/2011-11/msg01508.html
This patch makes it work now.
For the case where one vlan has multiple listenning sockets,
the patch will also provide the support.
Supported syntax:
1.) -net socket,listen=127.0.0.1:1234,vlan=0
2.) -net socket,listen=127.0.0.1:1234,vlan=0 -net socket,listen=127.0.0.1:1235,vlan=0
3.) -netdev socket,listen=127.0.0.1:1234,id=socket0
Drop the NetSocketListenState struct and add a listen_fd field
to NetSocketState. When a -netdev socket,listen= instance is created
there will be a NetSocketState with fd=-1 and a valid listen_fd. The
net_socket_accept() handler waits for listen_fd to become readable and
then accepts the connection. When this state transition happens, we no
longer monitor listen_fd for incoming connections...until the client
disconnects again.
Signed-off-by: Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
2012-07-20 17:25:53 +04:00
|
|
|
s->listen_fd = -1;
|
2009-11-25 21:49:08 +03:00
|
|
|
|
2013-02-27 18:05:47 +04:00
|
|
|
/* Disable Nagle algorithm on TCP sockets to reduce latency */
|
|
|
|
socket_set_nodelay(fd);
|
|
|
|
|
2009-11-25 21:48:56 +03:00
|
|
|
if (is_connected) {
|
|
|
|
net_socket_connect(s);
|
|
|
|
} else {
|
|
|
|
qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
static NetSocketState *net_socket_fd_init(NetClientState *peer,
|
2009-11-25 21:48:56 +03:00
|
|
|
const char *model, const char *name,
|
|
|
|
int fd, int is_connected)
|
|
|
|
{
|
|
|
|
int so_type = -1, optlen=sizeof(so_type);
|
|
|
|
|
|
|
|
if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
|
|
|
|
(socklen_t *)&optlen)< 0) {
|
2011-12-07 19:01:48 +04:00
|
|
|
fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n",
|
|
|
|
fd);
|
2011-12-07 19:01:49 +04:00
|
|
|
closesocket(fd);
|
2011-12-07 19:01:48 +04:00
|
|
|
return NULL;
|
2009-11-25 21:48:56 +03:00
|
|
|
}
|
|
|
|
switch(so_type) {
|
|
|
|
case SOCK_DGRAM:
|
2012-07-24 19:35:05 +04:00
|
|
|
return net_socket_fd_init_dgram(peer, model, name, fd, is_connected);
|
2009-11-25 21:48:56 +03:00
|
|
|
case SOCK_STREAM:
|
2012-07-24 19:35:05 +04:00
|
|
|
return net_socket_fd_init_stream(peer, model, name, fd, is_connected);
|
2009-11-25 21:48:56 +03:00
|
|
|
default:
|
|
|
|
/* who knows ... this could be a eg. a pty, do warn and continue as stream */
|
|
|
|
fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
|
2012-07-24 19:35:05 +04:00
|
|
|
return net_socket_fd_init_stream(peer, model, name, fd, is_connected);
|
2009-11-25 21:48:56 +03:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void net_socket_accept(void *opaque)
|
|
|
|
{
|
net: add the support for -netdev socket, listen
The -net socket,listen option does not work with the newer -netdev
syntax:
http://lists.gnu.org/archive/html/qemu-devel/2011-11/msg01508.html
This patch makes it work now.
For the case where one vlan has multiple listenning sockets,
the patch will also provide the support.
Supported syntax:
1.) -net socket,listen=127.0.0.1:1234,vlan=0
2.) -net socket,listen=127.0.0.1:1234,vlan=0 -net socket,listen=127.0.0.1:1235,vlan=0
3.) -netdev socket,listen=127.0.0.1:1234,id=socket0
Drop the NetSocketListenState struct and add a listen_fd field
to NetSocketState. When a -netdev socket,listen= instance is created
there will be a NetSocketState with fd=-1 and a valid listen_fd. The
net_socket_accept() handler waits for listen_fd to become readable and
then accepts the connection. When this state transition happens, we no
longer monitor listen_fd for incoming connections...until the client
disconnects again.
Signed-off-by: Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
2012-07-20 17:25:53 +04:00
|
|
|
NetSocketState *s = opaque;
|
2009-11-25 21:48:56 +03:00
|
|
|
struct sockaddr_in saddr;
|
|
|
|
socklen_t len;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
len = sizeof(saddr);
|
net: add the support for -netdev socket, listen
The -net socket,listen option does not work with the newer -netdev
syntax:
http://lists.gnu.org/archive/html/qemu-devel/2011-11/msg01508.html
This patch makes it work now.
For the case where one vlan has multiple listenning sockets,
the patch will also provide the support.
Supported syntax:
1.) -net socket,listen=127.0.0.1:1234,vlan=0
2.) -net socket,listen=127.0.0.1:1234,vlan=0 -net socket,listen=127.0.0.1:1235,vlan=0
3.) -netdev socket,listen=127.0.0.1:1234,id=socket0
Drop the NetSocketListenState struct and add a listen_fd field
to NetSocketState. When a -netdev socket,listen= instance is created
there will be a NetSocketState with fd=-1 and a valid listen_fd. The
net_socket_accept() handler waits for listen_fd to become readable and
then accepts the connection. When this state transition happens, we no
longer monitor listen_fd for incoming connections...until the client
disconnects again.
Signed-off-by: Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
2012-07-20 17:25:53 +04:00
|
|
|
fd = qemu_accept(s->listen_fd, (struct sockaddr *)&saddr, &len);
|
2009-11-25 21:48:56 +03:00
|
|
|
if (fd < 0 && errno != EINTR) {
|
|
|
|
return;
|
|
|
|
} else if (fd >= 0) {
|
net: add the support for -netdev socket, listen
The -net socket,listen option does not work with the newer -netdev
syntax:
http://lists.gnu.org/archive/html/qemu-devel/2011-11/msg01508.html
This patch makes it work now.
For the case where one vlan has multiple listenning sockets,
the patch will also provide the support.
Supported syntax:
1.) -net socket,listen=127.0.0.1:1234,vlan=0
2.) -net socket,listen=127.0.0.1:1234,vlan=0 -net socket,listen=127.0.0.1:1235,vlan=0
3.) -netdev socket,listen=127.0.0.1:1234,id=socket0
Drop the NetSocketListenState struct and add a listen_fd field
to NetSocketState. When a -netdev socket,listen= instance is created
there will be a NetSocketState with fd=-1 and a valid listen_fd. The
net_socket_accept() handler waits for listen_fd to become readable and
then accepts the connection. When this state transition happens, we no
longer monitor listen_fd for incoming connections...until the client
disconnects again.
Signed-off-by: Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
2012-07-20 17:25:53 +04:00
|
|
|
qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
|
2009-11-25 21:48:56 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
net: add the support for -netdev socket, listen
The -net socket,listen option does not work with the newer -netdev
syntax:
http://lists.gnu.org/archive/html/qemu-devel/2011-11/msg01508.html
This patch makes it work now.
For the case where one vlan has multiple listenning sockets,
the patch will also provide the support.
Supported syntax:
1.) -net socket,listen=127.0.0.1:1234,vlan=0
2.) -net socket,listen=127.0.0.1:1234,vlan=0 -net socket,listen=127.0.0.1:1235,vlan=0
3.) -netdev socket,listen=127.0.0.1:1234,id=socket0
Drop the NetSocketListenState struct and add a listen_fd field
to NetSocketState. When a -netdev socket,listen= instance is created
there will be a NetSocketState with fd=-1 and a valid listen_fd. The
net_socket_accept() handler waits for listen_fd to become readable and
then accepts the connection. When this state transition happens, we no
longer monitor listen_fd for incoming connections...until the client
disconnects again.
Signed-off-by: Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
2012-07-20 17:25:53 +04:00
|
|
|
|
|
|
|
s->fd = fd;
|
|
|
|
s->nc.link_down = false;
|
|
|
|
net_socket_connect(s);
|
|
|
|
snprintf(s->nc.info_str, sizeof(s->nc.info_str),
|
|
|
|
"socket: connection from %s:%d",
|
|
|
|
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
|
2009-11-25 21:48:56 +03:00
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
static int net_socket_listen_init(NetClientState *peer,
|
2009-11-25 21:48:56 +03:00
|
|
|
const char *model,
|
|
|
|
const char *name,
|
|
|
|
const char *host_str)
|
|
|
|
{
|
net: add the support for -netdev socket, listen
The -net socket,listen option does not work with the newer -netdev
syntax:
http://lists.gnu.org/archive/html/qemu-devel/2011-11/msg01508.html
This patch makes it work now.
For the case where one vlan has multiple listenning sockets,
the patch will also provide the support.
Supported syntax:
1.) -net socket,listen=127.0.0.1:1234,vlan=0
2.) -net socket,listen=127.0.0.1:1234,vlan=0 -net socket,listen=127.0.0.1:1235,vlan=0
3.) -netdev socket,listen=127.0.0.1:1234,id=socket0
Drop the NetSocketListenState struct and add a listen_fd field
to NetSocketState. When a -netdev socket,listen= instance is created
there will be a NetSocketState with fd=-1 and a valid listen_fd. The
net_socket_accept() handler waits for listen_fd to become readable and
then accepts the connection. When this state transition happens, we no
longer monitor listen_fd for incoming connections...until the client
disconnects again.
Signed-off-by: Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
2012-07-20 17:25:53 +04:00
|
|
|
NetClientState *nc;
|
|
|
|
NetSocketState *s;
|
2009-11-25 21:48:56 +03:00
|
|
|
struct sockaddr_in saddr;
|
2013-10-02 14:23:14 +04:00
|
|
|
int fd, ret;
|
2009-11-25 21:48:56 +03:00
|
|
|
|
|
|
|
if (parse_host_port(&saddr, host_str) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2009-12-02 14:24:42 +03:00
|
|
|
fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
|
2009-11-25 21:48:56 +03:00
|
|
|
if (fd < 0) {
|
|
|
|
perror("socket");
|
|
|
|
return -1;
|
|
|
|
}
|
2013-03-27 13:10:43 +04:00
|
|
|
qemu_set_nonblock(fd);
|
2009-11-25 21:48:56 +03:00
|
|
|
|
2013-10-02 14:23:14 +04:00
|
|
|
socket_set_fast_reuse(fd);
|
2009-11-25 21:48:56 +03:00
|
|
|
|
|
|
|
ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
|
|
|
|
if (ret < 0) {
|
|
|
|
perror("bind");
|
2011-12-25 03:47:11 +04:00
|
|
|
closesocket(fd);
|
2009-11-25 21:48:56 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ret = listen(fd, 0);
|
|
|
|
if (ret < 0) {
|
|
|
|
perror("listen");
|
2011-12-25 03:47:11 +04:00
|
|
|
closesocket(fd);
|
2009-11-25 21:48:56 +03:00
|
|
|
return -1;
|
|
|
|
}
|
net: add the support for -netdev socket, listen
The -net socket,listen option does not work with the newer -netdev
syntax:
http://lists.gnu.org/archive/html/qemu-devel/2011-11/msg01508.html
This patch makes it work now.
For the case where one vlan has multiple listenning sockets,
the patch will also provide the support.
Supported syntax:
1.) -net socket,listen=127.0.0.1:1234,vlan=0
2.) -net socket,listen=127.0.0.1:1234,vlan=0 -net socket,listen=127.0.0.1:1235,vlan=0
3.) -netdev socket,listen=127.0.0.1:1234,id=socket0
Drop the NetSocketListenState struct and add a listen_fd field
to NetSocketState. When a -netdev socket,listen= instance is created
there will be a NetSocketState with fd=-1 and a valid listen_fd. The
net_socket_accept() handler waits for listen_fd to become readable and
then accepts the connection. When this state transition happens, we no
longer monitor listen_fd for incoming connections...until the client
disconnects again.
Signed-off-by: Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
2012-07-20 17:25:53 +04:00
|
|
|
|
|
|
|
nc = qemu_new_net_client(&net_socket_info, peer, model, name);
|
|
|
|
s = DO_UPCAST(NetSocketState, nc, nc);
|
|
|
|
s->fd = -1;
|
|
|
|
s->listen_fd = fd;
|
|
|
|
s->nc.link_down = true;
|
|
|
|
|
|
|
|
qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
|
2009-11-25 21:48:56 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
static int net_socket_connect_init(NetClientState *peer,
|
2009-11-25 21:48:56 +03:00
|
|
|
const char *model,
|
|
|
|
const char *name,
|
|
|
|
const char *host_str)
|
|
|
|
{
|
|
|
|
NetSocketState *s;
|
|
|
|
int fd, connected, ret, err;
|
|
|
|
struct sockaddr_in saddr;
|
|
|
|
|
|
|
|
if (parse_host_port(&saddr, host_str) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2009-12-02 14:24:42 +03:00
|
|
|
fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
|
2009-11-25 21:48:56 +03:00
|
|
|
if (fd < 0) {
|
|
|
|
perror("socket");
|
|
|
|
return -1;
|
|
|
|
}
|
2013-03-27 13:10:43 +04:00
|
|
|
qemu_set_nonblock(fd);
|
2009-11-25 21:48:56 +03:00
|
|
|
|
|
|
|
connected = 0;
|
|
|
|
for(;;) {
|
|
|
|
ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
|
|
|
|
if (ret < 0) {
|
|
|
|
err = socket_error();
|
|
|
|
if (err == EINTR || err == EWOULDBLOCK) {
|
|
|
|
} else if (err == EINPROGRESS) {
|
|
|
|
break;
|
|
|
|
#ifdef _WIN32
|
2011-02-21 14:46:44 +03:00
|
|
|
} else if (err == WSAEALREADY || err == WSAEINVAL) {
|
2009-11-25 21:48:56 +03:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
perror("connect");
|
|
|
|
closesocket(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
connected = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-07-24 19:35:05 +04:00
|
|
|
s = net_socket_fd_init(peer, model, name, fd, connected);
|
2009-11-25 21:48:56 +03:00
|
|
|
if (!s)
|
|
|
|
return -1;
|
2009-11-25 21:49:08 +03:00
|
|
|
snprintf(s->nc.info_str, sizeof(s->nc.info_str),
|
2009-11-25 21:48:56 +03:00
|
|
|
"socket: connect to %s:%d",
|
|
|
|
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
static int net_socket_mcast_init(NetClientState *peer,
|
2009-11-25 21:48:56 +03:00
|
|
|
const char *model,
|
|
|
|
const char *name,
|
2010-12-01 22:16:47 +03:00
|
|
|
const char *host_str,
|
|
|
|
const char *localaddr_str)
|
2009-11-25 21:48:56 +03:00
|
|
|
{
|
|
|
|
NetSocketState *s;
|
|
|
|
int fd;
|
|
|
|
struct sockaddr_in saddr;
|
2010-12-01 22:16:47 +03:00
|
|
|
struct in_addr localaddr, *param_localaddr;
|
2009-11-25 21:48:56 +03:00
|
|
|
|
|
|
|
if (parse_host_port(&saddr, host_str) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2010-12-01 22:16:47 +03:00
|
|
|
if (localaddr_str != NULL) {
|
|
|
|
if (inet_aton(localaddr_str, &localaddr) == 0)
|
|
|
|
return -1;
|
|
|
|
param_localaddr = &localaddr;
|
|
|
|
} else {
|
|
|
|
param_localaddr = NULL;
|
|
|
|
}
|
2009-11-25 21:48:56 +03:00
|
|
|
|
2010-12-01 22:16:47 +03:00
|
|
|
fd = net_socket_mcast_create(&saddr, param_localaddr);
|
2009-11-25 21:48:56 +03:00
|
|
|
if (fd < 0)
|
2011-12-07 19:01:48 +04:00
|
|
|
return -1;
|
2009-11-25 21:48:56 +03:00
|
|
|
|
2012-07-24 19:35:05 +04:00
|
|
|
s = net_socket_fd_init(peer, model, name, fd, 0);
|
2009-11-25 21:48:56 +03:00
|
|
|
if (!s)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
s->dgram_dst = saddr;
|
|
|
|
|
2009-11-25 21:49:08 +03:00
|
|
|
snprintf(s->nc.info_str, sizeof(s->nc.info_str),
|
2009-11-25 21:48:56 +03:00
|
|
|
"socket: mcast=%s:%d",
|
|
|
|
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
static int net_socket_udp_init(NetClientState *peer,
|
2012-01-11 04:20:54 +04:00
|
|
|
const char *model,
|
|
|
|
const char *name,
|
|
|
|
const char *rhost,
|
|
|
|
const char *lhost)
|
|
|
|
{
|
|
|
|
NetSocketState *s;
|
2013-10-02 14:23:14 +04:00
|
|
|
int fd, ret;
|
2012-01-11 04:20:54 +04:00
|
|
|
struct sockaddr_in laddr, raddr;
|
|
|
|
|
|
|
|
if (parse_host_port(&laddr, lhost) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parse_host_port(&raddr, rhost) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("socket(PF_INET, SOCK_DGRAM)");
|
|
|
|
return -1;
|
|
|
|
}
|
2013-10-02 14:23:14 +04:00
|
|
|
|
|
|
|
ret = socket_set_fast_reuse(fd);
|
2012-01-11 04:20:54 +04:00
|
|
|
if (ret < 0) {
|
|
|
|
closesocket(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ret = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr));
|
|
|
|
if (ret < 0) {
|
|
|
|
perror("bind");
|
|
|
|
closesocket(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-03-27 13:10:44 +04:00
|
|
|
qemu_set_nonblock(fd);
|
2012-01-11 04:20:54 +04:00
|
|
|
|
2012-07-24 19:35:05 +04:00
|
|
|
s = net_socket_fd_init(peer, model, name, fd, 0);
|
2012-01-11 04:20:54 +04:00
|
|
|
if (!s) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->dgram_dst = raddr;
|
|
|
|
|
|
|
|
snprintf(s->nc.info_str, sizeof(s->nc.info_str),
|
|
|
|
"socket: udp=%s:%d",
|
|
|
|
inet_ntoa(raddr.sin_addr), ntohs(raddr.sin_port));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-17 18:17:21 +04:00
|
|
|
int net_init_socket(const NetClientOptions *opts, const char *name,
|
2015-05-15 14:58:50 +03:00
|
|
|
NetClientState *peer, Error **errp)
|
2009-11-25 21:48:56 +03:00
|
|
|
{
|
2015-05-15 14:58:50 +03:00
|
|
|
/* FIXME error_setg(errp, ...) on failure */
|
2015-02-09 16:03:19 +03:00
|
|
|
Error *err = NULL;
|
2012-07-17 18:17:17 +04:00
|
|
|
const NetdevSocketOptions *sock;
|
2009-11-25 21:48:56 +03:00
|
|
|
|
2012-07-17 18:17:17 +04:00
|
|
|
assert(opts->kind == NET_CLIENT_OPTIONS_KIND_SOCKET);
|
|
|
|
sock = opts->socket;
|
2009-11-25 21:48:56 +03:00
|
|
|
|
2012-07-17 18:17:17 +04:00
|
|
|
if (sock->has_fd + sock->has_listen + sock->has_connect + sock->has_mcast +
|
|
|
|
sock->has_udp != 1) {
|
|
|
|
error_report("exactly one of fd=, listen=, connect=, mcast= or udp="
|
|
|
|
" is required");
|
|
|
|
return -1;
|
|
|
|
}
|
2009-11-25 21:48:56 +03:00
|
|
|
|
2012-07-17 18:17:17 +04:00
|
|
|
if (sock->has_localaddr && !sock->has_mcast && !sock->has_udp) {
|
|
|
|
error_report("localaddr= is only valid with mcast= or udp=");
|
|
|
|
return -1;
|
|
|
|
}
|
2009-11-25 21:48:56 +03:00
|
|
|
|
2012-07-17 18:17:17 +04:00
|
|
|
if (sock->has_fd) {
|
|
|
|
int fd;
|
2009-11-25 21:48:56 +03:00
|
|
|
|
2015-02-09 16:03:19 +03:00
|
|
|
fd = monitor_fd_param(cur_mon, sock->fd, &err);
|
2013-03-27 13:10:44 +04:00
|
|
|
if (fd == -1) {
|
2015-02-09 16:03:19 +03:00
|
|
|
error_report_err(err);
|
2013-03-27 13:10:44 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
qemu_set_nonblock(fd);
|
|
|
|
if (!net_socket_fd_init(peer, "socket", name, fd, 1)) {
|
2009-11-25 21:48:56 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2012-07-17 18:17:17 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2009-11-25 21:48:56 +03:00
|
|
|
|
2012-07-17 18:17:17 +04:00
|
|
|
if (sock->has_listen) {
|
2012-07-24 19:35:05 +04:00
|
|
|
if (net_socket_listen_init(peer, "socket", name, sock->listen) == -1) {
|
2009-11-25 21:48:56 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2012-07-17 18:17:17 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2009-11-25 21:48:56 +03:00
|
|
|
|
2012-07-17 18:17:17 +04:00
|
|
|
if (sock->has_connect) {
|
2012-07-24 19:35:05 +04:00
|
|
|
if (net_socket_connect_init(peer, "socket", name, sock->connect) ==
|
2012-07-17 18:17:17 +04:00
|
|
|
-1) {
|
2009-11-25 21:48:56 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2012-07-17 18:17:17 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2009-11-25 21:48:56 +03:00
|
|
|
|
2012-07-17 18:17:17 +04:00
|
|
|
if (sock->has_mcast) {
|
|
|
|
/* if sock->localaddr is missing, it has been initialized to "all bits
|
|
|
|
* zero" */
|
2012-07-24 19:35:05 +04:00
|
|
|
if (net_socket_mcast_init(peer, "socket", name, sock->mcast,
|
2012-07-17 18:17:17 +04:00
|
|
|
sock->localaddr) == -1) {
|
2012-01-11 04:20:54 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2012-07-17 18:17:17 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2012-01-11 04:20:54 +04:00
|
|
|
|
2012-07-17 18:17:17 +04:00
|
|
|
assert(sock->has_udp);
|
|
|
|
if (!sock->has_localaddr) {
|
|
|
|
error_report("localaddr= is mandatory with udp=");
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-01 13:39:55 +04:00
|
|
|
if (net_socket_udp_init(peer, "socket", name, sock->udp, sock->localaddr) ==
|
2012-07-17 18:17:17 +04:00
|
|
|
-1) {
|
2009-11-25 21:48:56 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|