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>
This commit is contained in:
parent
e34cde35e7
commit
011de2b512
76
net/socket.c
76
net/socket.c
|
@ -35,6 +35,7 @@
|
||||||
|
|
||||||
typedef struct NetSocketState {
|
typedef struct NetSocketState {
|
||||||
NetClientState nc;
|
NetClientState nc;
|
||||||
|
int listen_fd;
|
||||||
int fd;
|
int fd;
|
||||||
int state; /* 0 = getting length, 1 = getting data */
|
int state; /* 0 = getting length, 1 = getting data */
|
||||||
unsigned int index;
|
unsigned int index;
|
||||||
|
@ -43,12 +44,7 @@ typedef struct NetSocketState {
|
||||||
struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
|
struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
|
||||||
} NetSocketState;
|
} NetSocketState;
|
||||||
|
|
||||||
typedef struct NetSocketListenState {
|
static void net_socket_accept(void *opaque);
|
||||||
NetClientState *peer;
|
|
||||||
char *model;
|
|
||||||
char *name;
|
|
||||||
int fd;
|
|
||||||
} NetSocketListenState;
|
|
||||||
|
|
||||||
/* XXX: we consider we can send the whole packet without blocking */
|
/* XXX: we consider we can send the whole packet without blocking */
|
||||||
static ssize_t net_socket_receive(NetClientState *nc, const uint8_t *buf, size_t size)
|
static ssize_t net_socket_receive(NetClientState *nc, const uint8_t *buf, size_t size)
|
||||||
|
@ -86,7 +82,19 @@ static void net_socket_send(void *opaque)
|
||||||
/* end of connection */
|
/* end of connection */
|
||||||
eoc:
|
eoc:
|
||||||
qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
|
qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
|
||||||
|
if (s->listen_fd != -1) {
|
||||||
|
qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
|
||||||
|
}
|
||||||
closesocket(s->fd);
|
closesocket(s->fd);
|
||||||
|
|
||||||
|
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));
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
buf = buf1;
|
buf = buf1;
|
||||||
|
@ -234,8 +242,16 @@ fail:
|
||||||
static void net_socket_cleanup(NetClientState *nc)
|
static void net_socket_cleanup(NetClientState *nc)
|
||||||
{
|
{
|
||||||
NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
|
NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
|
||||||
qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
|
if (s->fd != -1) {
|
||||||
close(s->fd);
|
qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static NetClientInfo net_dgram_socket_info = {
|
static NetClientInfo net_dgram_socket_info = {
|
||||||
|
@ -297,6 +313,7 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
|
||||||
s = DO_UPCAST(NetSocketState, nc, nc);
|
s = DO_UPCAST(NetSocketState, nc, nc);
|
||||||
|
|
||||||
s->fd = fd;
|
s->fd = fd;
|
||||||
|
s->listen_fd = -1;
|
||||||
|
|
||||||
qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
|
qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
|
||||||
|
|
||||||
|
@ -340,6 +357,7 @@ static NetSocketState *net_socket_fd_init_stream(NetClientState *peer,
|
||||||
s = DO_UPCAST(NetSocketState, nc, nc);
|
s = DO_UPCAST(NetSocketState, nc, nc);
|
||||||
|
|
||||||
s->fd = fd;
|
s->fd = fd;
|
||||||
|
s->listen_fd = -1;
|
||||||
|
|
||||||
if (is_connected) {
|
if (is_connected) {
|
||||||
net_socket_connect(s);
|
net_socket_connect(s);
|
||||||
|
@ -377,27 +395,28 @@ static NetSocketState *net_socket_fd_init(NetClientState *peer,
|
||||||
|
|
||||||
static void net_socket_accept(void *opaque)
|
static void net_socket_accept(void *opaque)
|
||||||
{
|
{
|
||||||
NetSocketListenState *s = opaque;
|
NetSocketState *s = opaque;
|
||||||
NetSocketState *s1;
|
|
||||||
struct sockaddr_in saddr;
|
struct sockaddr_in saddr;
|
||||||
socklen_t len;
|
socklen_t len;
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
for(;;) {
|
for(;;) {
|
||||||
len = sizeof(saddr);
|
len = sizeof(saddr);
|
||||||
fd = qemu_accept(s->fd, (struct sockaddr *)&saddr, &len);
|
fd = qemu_accept(s->listen_fd, (struct sockaddr *)&saddr, &len);
|
||||||
if (fd < 0 && errno != EINTR) {
|
if (fd < 0 && errno != EINTR) {
|
||||||
return;
|
return;
|
||||||
} else if (fd >= 0) {
|
} else if (fd >= 0) {
|
||||||
|
qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
s1 = net_socket_fd_init(s->peer, s->model, s->name, fd, 1);
|
|
||||||
if (s1) {
|
s->fd = fd;
|
||||||
snprintf(s1->nc.info_str, sizeof(s1->nc.info_str),
|
s->nc.link_down = false;
|
||||||
"socket: connection from %s:%d",
|
net_socket_connect(s);
|
||||||
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
|
snprintf(s->nc.info_str, sizeof(s->nc.info_str),
|
||||||
}
|
"socket: connection from %s:%d",
|
||||||
|
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int net_socket_listen_init(NetClientState *peer,
|
static int net_socket_listen_init(NetClientState *peer,
|
||||||
|
@ -405,19 +424,17 @@ static int net_socket_listen_init(NetClientState *peer,
|
||||||
const char *name,
|
const char *name,
|
||||||
const char *host_str)
|
const char *host_str)
|
||||||
{
|
{
|
||||||
NetSocketListenState *s;
|
NetClientState *nc;
|
||||||
int fd, val, ret;
|
NetSocketState *s;
|
||||||
struct sockaddr_in saddr;
|
struct sockaddr_in saddr;
|
||||||
|
int fd, val, ret;
|
||||||
|
|
||||||
if (parse_host_port(&saddr, host_str) < 0)
|
if (parse_host_port(&saddr, host_str) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
s = g_malloc0(sizeof(NetSocketListenState));
|
|
||||||
|
|
||||||
fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
|
fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
perror("socket");
|
perror("socket");
|
||||||
g_free(s);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
socket_set_nonblock(fd);
|
socket_set_nonblock(fd);
|
||||||
|
@ -429,22 +446,23 @@ static int net_socket_listen_init(NetClientState *peer,
|
||||||
ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
|
ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
perror("bind");
|
perror("bind");
|
||||||
g_free(s);
|
|
||||||
closesocket(fd);
|
closesocket(fd);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
ret = listen(fd, 0);
|
ret = listen(fd, 0);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
perror("listen");
|
perror("listen");
|
||||||
g_free(s);
|
|
||||||
closesocket(fd);
|
closesocket(fd);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
s->peer = peer;
|
|
||||||
s->model = g_strdup(model);
|
nc = qemu_new_net_client(&net_socket_info, peer, model, name);
|
||||||
s->name = name ? g_strdup(name) : NULL;
|
s = DO_UPCAST(NetSocketState, nc, nc);
|
||||||
s->fd = fd;
|
s->fd = -1;
|
||||||
qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
|
s->listen_fd = fd;
|
||||||
|
s->nc.link_down = true;
|
||||||
|
|
||||||
|
qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue