From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:51299) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S0yLi-0004P1-Oj for qemu-devel@nongnu.org; Fri, 24 Feb 2012 11:50:32 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1S0yLd-0001Ll-8O for qemu-devel@nongnu.org; Fri, 24 Feb 2012 11:50:26 -0500 Received: from mail-pw0-f45.google.com ([209.85.160.45]:46030) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S0yLc-0001Le-T0 for qemu-devel@nongnu.org; Fri, 24 Feb 2012 11:50:21 -0500 Received: by pbbro12 with SMTP id ro12so3362032pbb.4 for ; Fri, 24 Feb 2012 08:50:19 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <4F47A750.4050305@us.ibm.com> References: <1329556789-24944-1-git-send-email-zwu.kernel@gmail.com> <4F47A750.4050305@us.ibm.com> Date: Sat, 25 Feb 2012 00:50:19 +0800 Message-ID: From: Zhi Yong Wu Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH v2] net: add the support for -netdev socket, listen List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Anthony Liguori Cc: Zhi Yong Wu , qemu-devel@nongnu.org, stefanha@linux.vnet.ibm.com On Fri, Feb 24, 2012 at 11:05 PM, Anthony Liguori wro= te: > On 02/18/2012 03:19 AM, zwu.kernel@gmail.com wrote: >> >> 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 >> --- >> =A0net.c =A0 =A0 =A0 =A0| =A0 26 +++++++++++++++++++++ >> =A0net.h =A0 =A0 =A0 =A0| =A0 =A02 + >> =A0net/socket.c | =A0 72 >> +++++++++++++++++++++++++++++++++++++++++++++------------- >> =A03 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, >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 int iovcnt, >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 void *opaque); >> >> +VLANClientState *qemu_lookup_net_client(VLANState *vlan, >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0const char *name) >> +{ >> + =A0 =A0VLANClientState *vc =3D NULL; >> + >> + =A0 =A0if (vlan) { >> + =A0 =A0 =A0 =A0QTAILQ_FOREACH(vc,&vlan->clients, next) { >> >> + =A0 =A0 =A0 =A0 =A0 =A0if (!strcmp(vc->name, name)) { >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0break; >> + =A0 =A0 =A0 =A0 =A0 =A0} >> + =A0 =A0 =A0 =A0} >> + =A0 =A0} else { >> + =A0 =A0 =A0 =A0QTAILQ_FOREACH(vc,&non_vlan_clients, next) { >> >> + =A0 =A0 =A0 =A0 =A0 =A0if (!strcmp(vc->name, name)) { >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0break; >> + =A0 =A0 =A0 =A0 =A0 =A0} >> + =A0 =A0 =A0 =A0} >> + =A0 =A0} >> + >> + =A0 =A0if (!vc) { >> + =A0 =A0 =A0 =A0return NULL; >> + =A0 =A0} >> + >> + =A0 =A0return vc; >> +} >> + >> =A0VLANClientState *qemu_new_net_client(NetClientInfo *info, >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 VLANState *vlan, >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 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 { >> >> =A0VLANState *qemu_find_vlan(int id, int allocate); >> =A0VLANClientState *qemu_find_netdev(const char *id); >> +VLANClientState *qemu_lookup_net_client(VLANState *vlan, >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0const char *name); >> =A0VLANClientState *qemu_new_net_client(NetClientInfo *info, >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 VLANState *vlan, >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 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 { >> =A0} NetSocketState; >> >> =A0typedef struct NetSocketListenState { >> + =A0 =A0VLANClientState *nc; >> =A0 =A0 =A0VLANState *vlan; >> =A0 =A0 =A0char *model; >> =A0 =A0 =A0char *name; >> @@ -247,7 +248,8 @@ static NetClientInfo net_dgram_socket_info =3D { >> =A0static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0const char *model, >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0const char *name, >> - =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0int fd, int is_connected) >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0int fd, int is_connected, >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0int is_listen) >> =A0{ >> =A0 =A0 =A0struct sockaddr_in saddr; >> =A0 =A0 =A0int newfd; >> @@ -286,15 +288,28 @@ static NetSocketState >> *net_socket_fd_init_dgram(VLANState *vlan, >> =A0 =A0 =A0 =A0 =A0} >> =A0 =A0 =A0} >> >> - =A0 =A0nc =3D qemu_new_net_client(&net_dgram_socket_info, vlan, NULL, = model, >> name); >> + >> + =A0 =A0if (!is_listen || (is_listen&& =A0!is_connected)) { >> >> + =A0 =A0 =A0 =A0nc =3D qemu_new_net_client(&net_dgram_socket_info, >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 vlan, = NULL, model, name); >> + =A0 =A0} else { >> + =A0 =A0 =A0 =A0nc =3D qemu_lookup_net_client(vlan, name); >> + =A0 =A0 =A0 =A0if (!nc) { >> + =A0 =A0 =A0 =A0 =A0 =A0goto err; >> + =A0 =A0 =A0 =A0} >> + =A0 =A0} >> + >> + =A0 =A0s =3D DO_UPCAST(NetSocketState, nc, nc); >> + >> + =A0 =A0if (is_listen&& =A0!is_connected) { >> >> + =A0 =A0 =A0 =A0return s; >> + =A0 =A0} >> >> =A0 =A0 =A0snprintf(nc->info_str, sizeof(nc->info_str), >> =A0 =A0 =A0 =A0 =A0 =A0 =A0"socket: fd=3D%d (%s mcast=3D%s:%d)", >> =A0 =A0 =A0 =A0 =A0 =A0 =A0fd, is_connected ? "cloned" : "", >> =A0 =A0 =A0 =A0 =A0 =A0 =A0inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_po= rt)); >> >> - =A0 =A0s =3D DO_UPCAST(NetSocketState, nc, nc); >> - >> =A0 =A0 =A0s->fd =3D fd; >> >> =A0 =A0 =A0qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s); >> @@ -325,16 +340,29 @@ static NetClientInfo net_socket_info =3D { >> =A0static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 const char *model, >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 const char *name, >> - =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 int fd, int >> is_connected) >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 int fd, int >> is_connected, >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 int is_listen) >> =A0{ >> =A0 =A0 =A0VLANClientState *nc; >> =A0 =A0 =A0NetSocketState *s; >> >> - =A0 =A0nc =3D qemu_new_net_client(&net_socket_info, vlan, NULL, model,= name); >> + =A0 =A0if (!is_listen || (is_listen&& =A0!is_connected)) { >> >> + =A0 =A0 =A0 =A0nc =3D qemu_new_net_client(&net_socket_info, vlan, NULL= , model, >> name); >> + =A0 =A0} else { >> + =A0 =A0 =A0 =A0nc =3D qemu_lookup_net_client(vlan, name); >> + =A0 =A0 =A0 =A0if (!nc) { >> + =A0 =A0 =A0 =A0 =A0 =A0return NULL; >> + =A0 =A0 =A0 =A0} >> + =A0 =A0} >> + >> + =A0 =A0s =3D DO_UPCAST(NetSocketState, nc, nc); >> + >> + =A0 =A0if (is_listen&& =A0!is_connected) { >> >> + =A0 =A0 =A0 =A0return s; >> + =A0 =A0} >> >> =A0 =A0 =A0snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=3D%d= ", fd); >> >> - =A0 =A0s =3D DO_UPCAST(NetSocketState, nc, nc); >> >> =A0 =A0 =A0s->fd =3D fd; >> >> @@ -348,7 +376,8 @@ static NetSocketState >> *net_socket_fd_init_stream(VLANState *vlan, >> >> =A0static NetSocketState *net_socket_fd_init(VLANState *vlan, >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0const char *model, const char >> *name, >> - =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0int fd, int is_connected) >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0int fd, int is_connected, >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0int is_listen) >> =A0{ >> =A0 =A0 =A0int so_type =3D -1, optlen=3Dsizeof(so_type); >> >> @@ -361,13 +390,16 @@ static NetSocketState *net_socket_fd_init(VLANStat= e >> *vlan, >> =A0 =A0 =A0} >> =A0 =A0 =A0switch(so_type) { >> =A0 =A0 =A0case SOCK_DGRAM: >> - =A0 =A0 =A0 =A0return net_socket_fd_init_dgram(vlan, model, name, fd, >> is_connected); >> + =A0 =A0 =A0 =A0return net_socket_fd_init_dgram(vlan, model, >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0name, fd, is_connected, >> is_listen); >> =A0 =A0 =A0case SOCK_STREAM: >> - =A0 =A0 =A0 =A0return net_socket_fd_init_stream(vlan, model, name, fd, >> is_connected); >> + =A0 =A0 =A0 =A0return net_socket_fd_init_stream(vlan, model, >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 name, fd, is_connected, >> is_listen); >> =A0 =A0 =A0default: >> =A0 =A0 =A0 =A0 =A0/* who knows ... this could be a eg. a pty, do warn a= nd continue >> as stream */ >> =A0 =A0 =A0 =A0 =A0fprintf(stderr, "qemu: warning: socket type=3D%d for = fd=3D%d is not >> SOCK_DGRAM or SOCK_STREAM\n", so_type, fd); >> - =A0 =A0 =A0 =A0return net_socket_fd_init_stream(vlan, model, name, fd, >> is_connected); >> + =A0 =A0 =A0 =A0return net_socket_fd_init_stream(vlan, model, >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 name, fd, is_connected, >> is_listen); >> =A0 =A0 =A0} >> =A0 =A0 =A0return NULL; >> =A0} >> @@ -389,14 +421,17 @@ static void net_socket_accept(void *opaque) >> =A0 =A0 =A0 =A0 =A0 =A0 =A0break; >> =A0 =A0 =A0 =A0 =A0} >> =A0 =A0 =A0} >> - =A0 =A0s1 =3D net_socket_fd_init(s->vlan, s->model, s->name, fd, 1); >> + >> + =A0 =A0s1 =3D net_socket_fd_init(s->vlan, s->model, s->name, fd, 1, 1)= ; >> =A0 =A0 =A0if (s1) { >> =A0 =A0 =A0 =A0 =A0snprintf(s1->nc.info_str, sizeof(s1->nc.info_str), >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 "socket: connection from %s:%d", >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 inet_ntoa(saddr.sin_addr), ntohs(sad= dr.sin_port)); >> + =A0 =A0 =A0 =A0s1->nc.link_down =3D false; > > > I find this to be a little odd. =A0Nothing else uses link_down link this.= =A0Why > are you setting this flag? s1->nc.link_down =3D false this mean that the link are re-up. In current upstream, the option -netdev socket,listen can not work. The reason is that when network card is created, it will look for its peer net client. Because socket is still in listen state, its net client object will not be created until this socket accept the client's request. This will cause that network card fails to look for its peer net client. This is the root reason for the option -netdev socket,listen can not work. In this patch, the net client object for socket is created in advance when socket is in listen state. This void that network card fail to look for its peer net client object. As you've known, at the moment, the client request has not arrived, and the socket is still in listen state, so the link state is set to down state to reject packet trasmission. When the client request arrives, socket accept its request, and the link state is re-set to up state, data packets can be trasmitted now. > > Regards, > > Anthony Liguori > --=20 Regards, Zhi Yong Wu