All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH 0/2] net: Make -netdev socket,listen= work
@ 2012-07-20 13:25 Stefan Hajnoczi
  2012-07-20 13:25 ` [Qemu-devel] [PATCH 1/2] net: fix the coding style Stefan Hajnoczi
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Stefan Hajnoczi @ 2012-07-20 13:25 UTC (permalink / raw)
  To: qemu-devel; +Cc: Stefan Hajnoczi, Zhi Yong Wu

The socket backend does not support the listen= option with -netdev.  The
problem is how the socket NetClientState lifecycle is implemented: the socket
backend waits for an incoming client connection before creating a
NetClientState.  The guest -device wants a peer= on startup, so QEMU fails with
an error about the non-existent peer.

This series makes -netdev socket,listen= work by creating the NetClientState
right away.  This allows -device peer= to find the socket backend.

This code was written by Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>.  I have only
cleaned up and tested it.  The following work:
 * -net socket,listen=:1234 -net nic,model=virtio
 * -netdev socket,listen=:1234,id=netdev0 -device virtio-net-pci,netdev=netdev0

Zhi Yong Wu (2):
  net: fix the coding style
  net: add the support for -netdev socket, listen

 net/socket.c |   82 +++++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 50 insertions(+), 32 deletions(-)

-- 
1.7.10.4

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Qemu-devel] [PATCH 1/2] net: fix the coding style
  2012-07-20 13:25 [Qemu-devel] [PATCH 0/2] net: Make -netdev socket,listen= work Stefan Hajnoczi
@ 2012-07-20 13:25 ` Stefan Hajnoczi
  2012-07-20 13:25 ` [Qemu-devel] [PATCH 2/2] net: add the support for -netdev socket, listen Stefan Hajnoczi
  2012-07-31 13:31 ` [Qemu-devel] [PATCH 0/2] net: Make -netdev socket,listen= work Stefan Hajnoczi
  2 siblings, 0 replies; 7+ messages in thread
From: Stefan Hajnoczi @ 2012-07-20 13:25 UTC (permalink / raw)
  To: qemu-devel; +Cc: Zhi Yong Wu, Stefan Hajnoczi, Zhi Yong Wu

From: Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>

Signed-off-by: Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
---
 net/socket.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/socket.c b/net/socket.c
index 312730a..4a093cb 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -301,7 +301,9 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
     qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
 
     /* mcast: save bound address as dst */
-    if (is_connected) s->dgram_dst=saddr;
+    if (is_connected) {
+        s->dgram_dst = saddr;
+    }
 
     return s;
 
-- 
1.7.10.4

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [Qemu-devel] [PATCH 2/2] net: add the support for -netdev socket, listen
  2012-07-20 13:25 [Qemu-devel] [PATCH 0/2] net: Make -netdev socket,listen= work Stefan Hajnoczi
  2012-07-20 13:25 ` [Qemu-devel] [PATCH 1/2] net: fix the coding style Stefan Hajnoczi
@ 2012-07-20 13:25 ` Stefan Hajnoczi
  2012-07-31 13:31 ` [Qemu-devel] [PATCH 0/2] net: Make -netdev socket,listen= work Stefan Hajnoczi
  2 siblings, 0 replies; 7+ messages in thread
From: Stefan Hajnoczi @ 2012-07-20 13:25 UTC (permalink / raw)
  To: qemu-devel; +Cc: Zhi Yong Wu, Stefan Hajnoczi, Zhi Yong Wu

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.

Signed-off-by: Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
---
 net/socket.c |   78 +++++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 47 insertions(+), 31 deletions(-)

diff --git a/net/socket.c b/net/socket.c
index 4a093cb..c172c24 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,19 @@ static void net_socket_send(void *opaque)
         /* end of connection */
     eoc:
         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);
+
+        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;
     }
     buf = buf1;
@@ -234,8 +242,16 @@ fail:
 static void net_socket_cleanup(NetClientState *nc)
 {
     NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
-    qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
-    close(s->fd);
+    if (s->fd != -1) {
+        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 = {
@@ -297,6 +313,7 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
     s = DO_UPCAST(NetSocketState, nc, nc);
 
     s->fd = fd;
+    s->listen_fd = -1;
 
     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->fd = fd;
+    s->listen_fd = -1;
 
     if (is_connected) {
         net_socket_connect(s);
@@ -377,29 +395,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 +424,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 +446,23 @@ 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->fd = -1;
+    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.10.4

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [Qemu-devel] [PATCH 0/2] net: Make -netdev socket,listen= work
  2012-07-20 13:25 [Qemu-devel] [PATCH 0/2] net: Make -netdev socket,listen= work Stefan Hajnoczi
  2012-07-20 13:25 ` [Qemu-devel] [PATCH 1/2] net: fix the coding style Stefan Hajnoczi
  2012-07-20 13:25 ` [Qemu-devel] [PATCH 2/2] net: add the support for -netdev socket, listen Stefan Hajnoczi
@ 2012-07-31 13:31 ` Stefan Hajnoczi
  2 siblings, 0 replies; 7+ messages in thread
From: Stefan Hajnoczi @ 2012-07-31 13:31 UTC (permalink / raw)
  To: Stefan Hajnoczi; +Cc: qemu-devel, Zhi Yong Wu

On Fri, Jul 20, 2012 at 2:25 PM, Stefan Hajnoczi
<stefanha@linux.vnet.ibm.com> wrote:
> The socket backend does not support the listen= option with -netdev.  The
> problem is how the socket NetClientState lifecycle is implemented: the socket
> backend waits for an incoming client connection before creating a
> NetClientState.  The guest -device wants a peer= on startup, so QEMU fails with
> an error about the non-existent peer.
>
> This series makes -netdev socket,listen= work by creating the NetClientState
> right away.  This allows -device peer= to find the socket backend.
>
> This code was written by Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>.  I have only
> cleaned up and tested it.  The following work:
>  * -net socket,listen=:1234 -net nic,model=virtio
>  * -netdev socket,listen=:1234,id=netdev0 -device virtio-net-pci,netdev=netdev0
>
> Zhi Yong Wu (2):
>   net: fix the coding style
>   net: add the support for -netdev socket, listen
>
>  net/socket.c |   82 +++++++++++++++++++++++++++++++++++-----------------------
>  1 file changed, 50 insertions(+), 32 deletions(-)

Merged into the net tree:
https://github.com/stefanha/qemu/tree/net

Stefan

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Qemu-devel] [PATCH 0/2] net: Make -netdev socket,listen= work
  2012-07-31 13:04 ` Stefan Hajnoczi
@ 2012-07-31 14:20   ` Paolo Bonzini
  0 siblings, 0 replies; 7+ messages in thread
From: Paolo Bonzini @ 2012-07-31 14:20 UTC (permalink / raw)
  To: Stefan Hajnoczi; +Cc: Laszlo Ersek, qemu-devel, Stefan Hajnoczi, Zhi Yong Wu

Il 31/07/2012 15:04, Stefan Hajnoczi ha scritto:
>> > Looks good to me. A minor nit: 2/2 keeps the close(s->fd) call (instead
>> > of calling closesocket(s->fd), like in eoc handling) in
>> > net_socket_cleanup().
> The reason I didn't change close(2) to closesocket() is because
> -netdev socket,fd= can pass arbitrary file descriptors.  I suspect
> only real socket objects will work but we basically don't know what
> the passed file descriptor is.  This is the reason why I didn't feel
> 100% happy converting it to closesocket().
> 

-netdev socket,fd= is probably not usable from Windows, so no problem
using close().

Paolo

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Qemu-devel] [PATCH 0/2] net: Make -netdev socket,listen= work
  2012-07-31 11:41 Laszlo Ersek
@ 2012-07-31 13:04 ` Stefan Hajnoczi
  2012-07-31 14:20   ` Paolo Bonzini
  0 siblings, 1 reply; 7+ messages in thread
From: Stefan Hajnoczi @ 2012-07-31 13:04 UTC (permalink / raw)
  To: Laszlo Ersek; +Cc: Zhi Yong Wu, qemu-devel, Stefan Hajnoczi

On Tue, Jul 31, 2012 at 12:41 PM, Laszlo Ersek <lersek@redhat.com> wrote:
> (in reply to
> <http://lists.gnu.org/archive/html/qemu-devel/2012-07/msg02637.html> --
> In-Reply-To set, but that may not be enough for the web archive)
>
> Looks good to me. A minor nit: 2/2 keeps the close(s->fd) call (instead
> of calling closesocket(s->fd), like in eoc handling) in
> net_socket_cleanup().

The reason I didn't change close(2) to closesocket() is because
-netdev socket,fd= can pass arbitrary file descriptors.  I suspect
only real socket objects will work but we basically don't know what
the passed file descriptor is.  This is the reason why I didn't feel
100% happy converting it to closesocket().

> Reviewed-by: Laszlo Ersek <lersek@redhat.com>

Thanks!

Stefan

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Qemu-devel] [PATCH 0/2] net: Make -netdev socket,listen= work
@ 2012-07-31 11:41 Laszlo Ersek
  2012-07-31 13:04 ` Stefan Hajnoczi
  0 siblings, 1 reply; 7+ messages in thread
From: Laszlo Ersek @ 2012-07-31 11:41 UTC (permalink / raw)
  To: qemu-devel; +Cc: Stefan Hajnoczi, Zhi Yong Wu

(in reply to
<http://lists.gnu.org/archive/html/qemu-devel/2012-07/msg02637.html> --
In-Reply-To set, but that may not be enough for the web archive)

Looks good to me. A minor nit: 2/2 keeps the close(s->fd) call (instead
of calling closesocket(s->fd), like in eoc handling) in
net_socket_cleanup().

Reviewed-by: Laszlo Ersek <lersek@redhat.com>

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2012-07-31 14:20 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-07-20 13:25 [Qemu-devel] [PATCH 0/2] net: Make -netdev socket,listen= work Stefan Hajnoczi
2012-07-20 13:25 ` [Qemu-devel] [PATCH 1/2] net: fix the coding style Stefan Hajnoczi
2012-07-20 13:25 ` [Qemu-devel] [PATCH 2/2] net: add the support for -netdev socket, listen Stefan Hajnoczi
2012-07-31 13:31 ` [Qemu-devel] [PATCH 0/2] net: Make -netdev socket,listen= work Stefan Hajnoczi
2012-07-31 11:41 Laszlo Ersek
2012-07-31 13:04 ` Stefan Hajnoczi
2012-07-31 14:20   ` Paolo Bonzini

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.