From patchwork Sat Feb 18 09:19:49 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhiyong Wu X-Patchwork-Id: 142032 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id CCE05B6FA2 for ; Sat, 18 Feb 2012 20:21:02 +1100 (EST) Received: from localhost ([::1]:59701 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RygTS-0002uc-35 for incoming@patchwork.ozlabs.org; Sat, 18 Feb 2012 04:20:58 -0500 Received: from eggs.gnu.org ([140.186.70.92]:34730) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RygTJ-0002uR-QP for qemu-devel@nongnu.org; Sat, 18 Feb 2012 04:20:51 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RygTI-000861-6h for qemu-devel@nongnu.org; Sat, 18 Feb 2012 04:20:49 -0500 Received: from e36.co.us.ibm.com ([32.97.110.154]:59153) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RygTH-00085d-Rf for qemu-devel@nongnu.org; Sat, 18 Feb 2012 04:20:48 -0500 Received: from /spool/local by e36.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Sat, 18 Feb 2012 02:20:44 -0700 Received: from d03dlp02.boulder.ibm.com (9.17.202.178) by e36.co.us.ibm.com (192.168.1.136) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Sat, 18 Feb 2012 02:19:59 -0700 Received: from d03relay02.boulder.ibm.com (d03relay02.boulder.ibm.com [9.17.195.227]) by d03dlp02.boulder.ibm.com (Postfix) with ESMTP id 0AE623E40048 for ; Sat, 18 Feb 2012 02:19:59 -0700 (MST) Received: from d03av03.boulder.ibm.com (d03av03.boulder.ibm.com [9.17.195.169]) by d03relay02.boulder.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q1I9Jwgo165492 for ; Sat, 18 Feb 2012 02:19:58 -0700 Received: from d03av03.boulder.ibm.com (loopback [127.0.0.1]) by d03av03.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q1I9Jwg1023760 for ; Sat, 18 Feb 2012 02:19:58 -0700 Received: from us.ibm.com (f15.cn.ibm.com [9.115.118.120] (may be forged)) by d03av03.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with SMTP id q1I9JtBH023728; Sat, 18 Feb 2012 02:19:56 -0700 Received: by us.ibm.com (sSMTP sendmail emulation); Sat, 18 Feb 2012 17:19:51 +0800 From: zwu.kernel@gmail.com To: qemu-devel@nongnu.org Date: Sat, 18 Feb 2012 17:19:49 +0800 Message-Id: <1329556789-24944-1-git-send-email-zwu.kernel@gmail.com> X-Mailer: git-send-email 1.7.6 X-Content-Scanned: Fidelis XPS MAILER x-cbid: 12021809-3352-0000-0000-000002B8DD4D X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 32.97.110.154 Cc: aliguori@us.ibm.com, Zhi Yong Wu , stefanha@linux.vnet.ibm.com Subject: [Qemu-devel] [PATCH v2] 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. Signed-off-by: Zhi Yong Wu --- net.c | 26 +++++++++++++++++++++ net.h | 2 + net/socket.c | 72 +++++++++++++++++++++++++++++++++++++++++++++------------- 3 files changed, 84 insertions(+), 16 deletions(-) diff --git a/net.c b/net.c index c34474f..60e7b35 100644 --- a/net.c +++ b/net.c @@ -190,6 +190,32 @@ static ssize_t qemu_deliver_packet_iov(VLANClientState *sender, int iovcnt, void *opaque); +VLANClientState *qemu_lookup_net_client(VLANState *vlan, + const char *name) +{ + VLANClientState *vc = NULL; + + if (vlan) { + QTAILQ_FOREACH(vc, &vlan->clients, next) { + if (!strcmp(vc->name, name)) { + break; + } + } + } else { + QTAILQ_FOREACH(vc, &non_vlan_clients, next) { + if (!strcmp(vc->name, name)) { + break; + } + } + } + + if (!vc) { + return NULL; + } + + return vc; +} + VLANClientState *qemu_new_net_client(NetClientInfo *info, VLANState *vlan, VLANClientState *peer, diff --git a/net.h b/net.h index 75a8c15..7f73160 100644 --- a/net.h +++ b/net.h @@ -90,6 +90,8 @@ struct VLANState { VLANState *qemu_find_vlan(int id, int allocate); VLANClientState *qemu_find_netdev(const char *id); +VLANClientState *qemu_lookup_net_client(VLANState *vlan, + const char *name); VLANClientState *qemu_new_net_client(NetClientInfo *info, VLANState *vlan, VLANClientState *peer, diff --git a/net/socket.c b/net/socket.c index d4c2002..3ecee59 100644 --- a/net/socket.c +++ b/net/socket.c @@ -43,6 +43,7 @@ typedef struct NetSocketState { } NetSocketState; typedef struct NetSocketListenState { + VLANClientState *nc; VLANState *vlan; char *model; char *name; @@ -247,7 +248,8 @@ static NetClientInfo net_dgram_socket_info = { static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, const char *model, const char *name, - int fd, int is_connected) + int fd, int is_connected, + int is_listen) { struct sockaddr_in saddr; int newfd; @@ -286,15 +288,28 @@ static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, } } - nc = qemu_new_net_client(&net_dgram_socket_info, vlan, NULL, model, name); + + if (!is_listen || (is_listen && !is_connected)) { + nc = qemu_new_net_client(&net_dgram_socket_info, + vlan, NULL, model, name); + } else { + nc = qemu_lookup_net_client(vlan, name); + if (!nc) { + goto err; + } + } + + s = DO_UPCAST(NetSocketState, nc, nc); + + if (is_listen && !is_connected) { + return s; + } snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d (%s mcast=%s:%d)", fd, is_connected ? "cloned" : "", inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); - s = DO_UPCAST(NetSocketState, nc, nc); - s->fd = fd; qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s); @@ -325,16 +340,29 @@ static NetClientInfo net_socket_info = { static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, const char *model, const char *name, - int fd, int is_connected) + int fd, int is_connected, + int is_listen) { VLANClientState *nc; NetSocketState *s; - nc = qemu_new_net_client(&net_socket_info, vlan, NULL, model, name); + if (!is_listen || (is_listen && !is_connected)) { + nc = qemu_new_net_client(&net_socket_info, vlan, NULL, model, name); + } else { + nc = qemu_lookup_net_client(vlan, name); + if (!nc) { + return NULL; + } + } + + s = DO_UPCAST(NetSocketState, nc, nc); + + if (is_listen && !is_connected) { + return s; + } snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d", fd); - s = DO_UPCAST(NetSocketState, nc, nc); s->fd = fd; @@ -348,7 +376,8 @@ static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, static NetSocketState *net_socket_fd_init(VLANState *vlan, const char *model, const char *name, - int fd, int is_connected) + int fd, int is_connected, + int is_listen) { int so_type = -1, optlen=sizeof(so_type); @@ -361,13 +390,16 @@ static NetSocketState *net_socket_fd_init(VLANState *vlan, } switch(so_type) { case SOCK_DGRAM: - return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected); + return net_socket_fd_init_dgram(vlan, model, + name, fd, is_connected, is_listen); case SOCK_STREAM: - return net_socket_fd_init_stream(vlan, model, name, fd, is_connected); + return net_socket_fd_init_stream(vlan, model, + name, fd, is_connected, is_listen); 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); - return net_socket_fd_init_stream(vlan, model, name, fd, is_connected); + return net_socket_fd_init_stream(vlan, model, + name, fd, is_connected, is_listen); } return NULL; } @@ -389,14 +421,17 @@ static void net_socket_accept(void *opaque) break; } } - s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1); + + s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1, 1); if (s1) { snprintf(s1->nc.info_str, sizeof(s1->nc.info_str), "socket: connection from %s:%d", inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); + s1->nc.link_down = false; } } + static int net_socket_listen_init(VLANState *vlan, const char *model, const char *name, @@ -405,6 +440,7 @@ static int net_socket_listen_init(VLANState *vlan, NetSocketListenState *s; int fd, val, ret; struct sockaddr_in saddr; + NetSocketState *ns; if (parse_host_port(&saddr, host_str) < 0) return -1; @@ -441,6 +477,10 @@ static int net_socket_listen_init(VLANState *vlan, s->model = g_strdup(model); s->name = name ? g_strdup(name) : NULL; s->fd = fd; + + ns = net_socket_fd_init(s->vlan, s->model, s->name, fd, 0, 1); + ns->nc.link_down = true; + qemu_set_fd_handler(fd, net_socket_accept, NULL, s); return 0; } @@ -486,7 +526,7 @@ static int net_socket_connect_init(VLANState *vlan, break; } } - s = net_socket_fd_init(vlan, model, name, fd, connected); + s = net_socket_fd_init(vlan, model, name, fd, connected, 0); if (!s) return -1; snprintf(s->nc.info_str, sizeof(s->nc.info_str), @@ -521,7 +561,7 @@ static int net_socket_mcast_init(VLANState *vlan, if (fd < 0) return -1; - s = net_socket_fd_init(vlan, model, name, fd, 0); + s = net_socket_fd_init(vlan, model, name, fd, 0, 0); if (!s) return -1; @@ -572,7 +612,7 @@ static int net_socket_udp_init(VLANState *vlan, return -1; } - s = net_socket_fd_init(vlan, model, name, fd, 0); + s = net_socket_fd_init(vlan, model, name, fd, 0, 0); if (!s) { return -1; } @@ -606,7 +646,7 @@ int net_init_socket(QemuOpts *opts, return -1; } - if (!net_socket_fd_init(vlan, "socket", name, fd, 1)) { + if (!net_socket_fd_init(vlan, "socket", name, fd, 1, 0)) { return -1; } } else if (qemu_opt_get(opts, "listen")) {