From patchwork Wed Aug 9 01:50:52 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Michael S. Tsirkin" X-Patchwork-Id: 9889417 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 4189E601EB for ; Wed, 9 Aug 2017 01:52:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 322802896A for ; Wed, 9 Aug 2017 01:52:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 27489289C0; Wed, 9 Aug 2017 01:52:28 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 9AB9D28908 for ; Wed, 9 Aug 2017 01:52:27 +0000 (UTC) Received: from localhost ([::1]:45285 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dfGAc-0003Cy-Qy for patchwork-qemu-devel@patchwork.kernel.org; Tue, 08 Aug 2017 21:52:26 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60089) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dfG9E-0003B0-KY for qemu-devel@nongnu.org; Tue, 08 Aug 2017 21:51:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dfG9B-00067O-BS for qemu-devel@nongnu.org; Tue, 08 Aug 2017 21:51:00 -0400 Received: from mx1.redhat.com ([209.132.183.28]:35698) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dfG9B-000677-1d for qemu-devel@nongnu.org; Tue, 08 Aug 2017 21:50:57 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 002D87F41C; Wed, 9 Aug 2017 01:50:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 002D87F41C Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=mst@redhat.com Received: from redhat.com (ovpn-120-92.rdu2.redhat.com [10.10.120.92]) by smtp.corp.redhat.com (Postfix) with SMTP id 611DF5D6A0; Wed, 9 Aug 2017 01:50:53 +0000 (UTC) Date: Wed, 9 Aug 2017 04:50:52 +0300 From: "Michael S. Tsirkin" To: qemu-devel@nongnu.org Message-ID: <1502241852-6302-4-git-send-email-mst@redhat.com> References: <1502241852-6302-1-git-send-email-mst@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <1502241852-6302-1-git-send-email-mst@redhat.com> X-Mutt-Fcc: =sent X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Wed, 09 Aug 2017 01:50:56 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 3/5] net: fix -netdev socket, fd= for UDP sockets X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , Jason Wang , Jens Freimann Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Jens Freimann This patch fixes -netdev socket,fd= for UDP sockets Currently -netdev socket,fd=<...> results in qemu: error: specified mcastaddr "127.0.0.1" (0x7f000001) does not contain a multicast address qemu-system-x86_64: -netdev socket,id=n1,fd=3: Device 'socket' could not be initialized To fix these we need to allow specifying multicast and fd arguments for the same netdev. With this the user can specify "-netdev fd=3,mcast=" Cc: Jason Wang Fixes: 3d830459b1eccdb61b75e2712fd364012ce5a115 Signed-off-by: Jens Freimann Reviewed-by: Michael S. Tsirkin Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- net/socket.c | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/net/socket.c b/net/socket.c index f85ef7d..18af2ab 100644 --- a/net/socket.c +++ b/net/socket.c @@ -320,11 +320,11 @@ static NetClientInfo net_dgram_socket_info = { static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer, const char *model, const char *name, - int fd, int is_connected) + int fd, int is_connected, + const char *mcast) { struct sockaddr_in saddr; int newfd; - socklen_t saddr_len = sizeof(saddr); NetClientState *nc; NetSocketState *s; @@ -333,8 +333,13 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer, * by ONLY ONE process: we must "clone" this dgram socket --jjo */ - if (is_connected) { - if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) { + if (is_connected && mcast != NULL) { + if (parse_host_port(&saddr, mcast) < 0) { + fprintf(stderr, + "qemu: error: init_dgram: fd=%d failed parse_host_port()\n", + fd); + goto err; + } /* must be bound */ if (saddr.sin_addr.s_addr == 0) { fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, " @@ -351,12 +356,6 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer, dup2(newfd, fd); close(newfd); - } else { - fprintf(stderr, - "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n", - fd, strerror(errno)); - goto err; - } } nc = qemu_new_net_client(&net_dgram_socket_info, peer, model, name); @@ -432,7 +431,7 @@ static NetSocketState *net_socket_fd_init_stream(NetClientState *peer, static NetSocketState *net_socket_fd_init(NetClientState *peer, const char *model, const char *name, - int fd, int is_connected) + int fd, int is_connected, const char *mc) { int so_type = -1, optlen=sizeof(so_type); @@ -445,7 +444,7 @@ static NetSocketState *net_socket_fd_init(NetClientState *peer, } switch(so_type) { case SOCK_DGRAM: - return net_socket_fd_init_dgram(peer, model, name, fd, is_connected); + return net_socket_fd_init_dgram(peer, model, name, fd, is_connected, mc); case SOCK_STREAM: return net_socket_fd_init_stream(peer, model, name, fd, is_connected); default: @@ -567,7 +566,7 @@ static int net_socket_connect_init(NetClientState *peer, break; } } - s = net_socket_fd_init(peer, model, name, fd, connected); + s = net_socket_fd_init(peer, model, name, fd, connected, NULL); if (!s) return -1; snprintf(s->nc.info_str, sizeof(s->nc.info_str), @@ -602,7 +601,7 @@ static int net_socket_mcast_init(NetClientState *peer, if (fd < 0) return -1; - s = net_socket_fd_init(peer, model, name, fd, 0); + s = net_socket_fd_init(peer, model, name, fd, 0, NULL); if (!s) return -1; @@ -652,7 +651,7 @@ static int net_socket_udp_init(NetClientState *peer, } qemu_set_nonblock(fd); - s = net_socket_fd_init(peer, model, name, fd, 0); + s = net_socket_fd_init(peer, model, name, fd, 0, NULL); if (!s) { return -1; } @@ -675,9 +674,9 @@ int net_init_socket(const Netdev *netdev, const char *name, assert(netdev->type == NET_CLIENT_DRIVER_SOCKET); sock = &netdev->u.socket; - if (sock->has_fd + sock->has_listen + sock->has_connect + sock->has_mcast + - sock->has_udp != 1) { - error_report("exactly one of fd=, listen=, connect=, mcast= or udp=" + if (sock->has_listen + sock->has_connect + sock->has_mcast + + sock->has_udp > 1) { + error_report("exactly one of listen=, connect=, mcast= or udp=" " is required"); return -1; } @@ -696,7 +695,7 @@ int net_init_socket(const Netdev *netdev, const char *name, return -1; } qemu_set_nonblock(fd); - if (!net_socket_fd_init(peer, "socket", name, fd, 1)) { + if (!net_socket_fd_init(peer, "socket", name, fd, 1, sock->mcast)) { return -1; } return 0;