From: zwu.kernel@gmail.com
To: qemu-devel@nongnu.org
Cc: aliguori@us.ibm.com, Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>,
stefanha@linux.vnet.ibm.com
Subject: [Qemu-devel] [PATCH v2 2/3] net: add the support for -netdev socket, listen
Date: Wed, 20 Jun 2012 17:46:21 +0800 [thread overview]
Message-ID: <1340185582-30955-3-git-send-email-zwu.kernel@gmail.com> (raw)
In-Reply-To: <1340185582-30955-1-git-send-email-zwu.kernel@gmail.com>
From: Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>
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.
Suggested-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Signed-off-by: Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>
---
net/socket.c | 60 +++++++++++++++++++++++++++++----------------------------
1 files changed, 31 insertions(+), 29 deletions(-)
diff --git a/net/socket.c b/net/socket.c
index ba8583f..e61e346 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -35,6 +35,7 @@
typedef struct NetSocketState {
NetClientState nc;
+ int listen_fd;
int fd;
int state; /* 0 = getting length, 1 = getting data */
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) */
} NetSocketState;
-typedef struct NetSocketListenState {
- NetClientState *peer;
- char *model;
- char *name;
- int fd;
-} NetSocketListenState;
+static void net_socket_accept(void *opaque);
/* 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)
@@ -86,7 +82,16 @@ static void net_socket_send(void *opaque)
/* end of connection */
eoc:
qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
+ qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
closesocket(s->fd);
+
+ s->fd = -1;
+ s->state = 0;
+ s->index = 0;
+ s->packet_len = 0;
+ memset(s->buf, 0, sizeof(s->buf));
+ memset(s->nc.info_str, 0, sizeof(s->nc.info_str));
+
return;
}
buf = buf1;
@@ -377,29 +382,28 @@ static NetSocketState *net_socket_fd_init(NetClientState *peer,
static void net_socket_accept(void *opaque)
{
- NetSocketListenState *s = opaque;
- NetSocketState *s1;
+ NetSocketState *s = opaque;
struct sockaddr_in saddr;
socklen_t len;
int fd;
for(;;) {
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) {
return;
} else if (fd >= 0) {
+ qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
break;
}
}
- s1 = net_socket_fd_init(s->peer, s->model, s->name, fd, 1);
- if (!s1) {
- closesocket(fd);
- } else {
- snprintf(s1->nc.info_str, sizeof(s1->nc.info_str),
- "socket: connection from %s:%d",
- inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
- }
+
+ 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));
}
static int net_socket_listen_init(NetClientState *peer,
@@ -407,19 +411,17 @@ static int net_socket_listen_init(NetClientState *peer,
const char *name,
const char *host_str)
{
- NetSocketListenState *s;
- int fd, val, ret;
+ NetClientState *nc;
+ NetSocketState *s;
struct sockaddr_in saddr;
+ int fd, val, ret;
if (parse_host_port(&saddr, host_str) < 0)
return -1;
- s = g_malloc0(sizeof(NetSocketListenState));
-
fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
if (fd < 0) {
perror("socket");
- g_free(s);
return -1;
}
socket_set_nonblock(fd);
@@ -431,22 +433,22 @@ static int net_socket_listen_init(NetClientState *peer,
ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
if (ret < 0) {
perror("bind");
- g_free(s);
closesocket(fd);
return -1;
}
ret = listen(fd, 0);
if (ret < 0) {
perror("listen");
- g_free(s);
closesocket(fd);
return -1;
}
- s->peer = peer;
- s->model = g_strdup(model);
- s->name = name ? g_strdup(name) : NULL;
- s->fd = fd;
- qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
+
+ nc = qemu_new_net_client(&net_socket_info, peer, model, name);
+ s = DO_UPCAST(NetSocketState, nc, nc);
+ s->listen_fd = fd;
+ s->nc.link_down = true;
+
+ qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
return 0;
}
--
1.7.6
next prev parent reply other threads:[~2012-06-20 9:46 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-06-20 9:46 [Qemu-devel] [PATCH v2 0/3] Some socket fix patches zwu.kernel
2012-06-20 9:46 ` [Qemu-devel] [PATCH v2 1/3] net: fix the coding style zwu.kernel
2012-06-20 9:46 ` zwu.kernel [this message]
2012-06-20 9:46 ` [Qemu-devel] [PATCH v2 3/3] net: complete NetSocketState lifecycle handling zwu.kernel
2012-06-24 15:13 ` [Qemu-devel] [PATCH v2 0/3] Some socket fix patches Zhi Yong Wu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1340185582-30955-3-git-send-email-zwu.kernel@gmail.com \
--to=zwu.kernel@gmail.com \
--cc=aliguori@us.ibm.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@linux.vnet.ibm.com \
--cc=wuzhy@linux.vnet.ibm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.