From patchwork Fri Jul 20 13:25:53 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 172275 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id B71122C0353 for ; Fri, 20 Jul 2012 23:26:58 +1000 (EST) Received: from localhost ([::1]:45727 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SsDEO-0000dP-RB for incoming@patchwork.ozlabs.org; Fri, 20 Jul 2012 09:26:56 -0400 Received: from eggs.gnu.org ([208.118.235.92]:54791) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SsDDx-0007u6-0D for qemu-devel@nongnu.org; Fri, 20 Jul 2012 09:26:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SsDDp-0004CB-8C for qemu-devel@nongnu.org; Fri, 20 Jul 2012 09:26:28 -0400 Received: from e06smtp15.uk.ibm.com ([195.75.94.111]:42415) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SsDDo-0004C6-Sj for qemu-devel@nongnu.org; Fri, 20 Jul 2012 09:26:21 -0400 Received: from /spool/local by e06smtp15.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Fri, 20 Jul 2012 14:26:19 +0100 Received: from d06nrmr1507.portsmouth.uk.ibm.com (9.149.38.233) by e06smtp15.uk.ibm.com (192.168.101.145) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Fri, 20 Jul 2012 14:26:17 +0100 Received: from d06av03.portsmouth.uk.ibm.com (d06av03.portsmouth.uk.ibm.com [9.149.37.213]) by d06nrmr1507.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q6KDQHI92093292 for ; Fri, 20 Jul 2012 14:26:17 +0100 Received: from d06av03.portsmouth.uk.ibm.com (localhost.localdomain [127.0.0.1]) by d06av03.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q6KDQBgP005457 for ; Fri, 20 Jul 2012 07:26:12 -0600 Received: from localhost (stefanha-thinkpad.manchester-maybrook.uk.ibm.com [9.174.219.145]) by d06av03.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id q6KDQB12005451; Fri, 20 Jul 2012 07:26:11 -0600 From: Stefan Hajnoczi To: Date: Fri, 20 Jul 2012 14:25:53 +0100 Message-Id: <1342790753-12727-3-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1342790753-12727-1-git-send-email-stefanha@linux.vnet.ibm.com> References: <1342790753-12727-1-git-send-email-stefanha@linux.vnet.ibm.com> x-cbid: 12072013-0342-0000-0000-00000259B1E1 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 195.75.94.111 Cc: Zhi Yong Wu , Stefan Hajnoczi , Zhi Yong Wu Subject: [Qemu-devel] [PATCH 2/2] net: add the support for -netdev socket, listen X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Zhi Yong Wu 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 Signed-off-by: Stefan Hajnoczi --- 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; }