From patchwork Mon Jul 23 11:50:40 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 172660 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 2C3FD2C0361 for ; Mon, 23 Jul 2012 22:56:02 +1000 (EST) Received: from localhost ([::1]:60365 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1StHBg-0000OS-Au for incoming@patchwork.ozlabs.org; Mon, 23 Jul 2012 07:52:32 -0400 Received: from eggs.gnu.org ([208.118.235.92]:54105) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1StHAQ-0005hv-DK for qemu-devel@nongnu.org; Mon, 23 Jul 2012 07:51:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1StHAO-0000aJ-Hc for qemu-devel@nongnu.org; Mon, 23 Jul 2012 07:51:14 -0400 Received: from e06smtp11.uk.ibm.com ([195.75.94.107]:42958) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1StHAO-0000Zc-5K for qemu-devel@nongnu.org; Mon, 23 Jul 2012 07:51:12 -0400 Received: from /spool/local by e06smtp11.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 23 Jul 2012 12:51:11 +0100 Received: from d06nrmr1507.portsmouth.uk.ibm.com (9.149.38.233) by e06smtp11.uk.ibm.com (192.168.101.141) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 23 Jul 2012 12:51:00 +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 q6NBox8g2613462 for ; Mon, 23 Jul 2012 12:50:59 +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 q6NBoxMH011646 for ; Mon, 23 Jul 2012 05:50:59 -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 q6NBoxaA011635; Mon, 23 Jul 2012 05:50:59 -0600 From: Stefan Hajnoczi To: Anthony Liguori Date: Mon, 23 Jul 2012 12:50:40 +0100 Message-Id: <1343044244-28728-16-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1343044244-28728-1-git-send-email-stefanha@linux.vnet.ibm.com> References: <1343044244-28728-1-git-send-email-stefanha@linux.vnet.ibm.com> x-cbid: 12072311-5024-0000-0000-0000034D82A7 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 195.75.94.107 Cc: Laszlo Ersek , qemu-devel@nongnu.org, Stefan Hajnoczi Subject: [Qemu-devel] [PATCH 15/19] convert net_init_socket() to NetClientOptions 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: Laszlo Ersek I "reverse engineered" the following permissions between the -socket sub-options: fd listen connect mcast udp | localaddr fd x . . . . | . listen . x . . . | . connect . . x . . | . mcast . . . x . | x udp . . . . x | x -------------------------------------------+ localaddr . . . x x x I transformed the code accordingly. The real fix would be to embed "fd", "listen", "connect", "mcast" and "udp" in a separate union. However OptsVisitor's enum parser only supports the type=XXX QemuOpt instance as union discriminator. Signed-off-by: Laszlo Ersek Signed-off-by: Stefan Hajnoczi --- net/socket.c | 119 +++++++++++++++++++++------------------------------------- 1 file changed, 43 insertions(+), 76 deletions(-) diff --git a/net/socket.c b/net/socket.c index 563447d0..e3cba20 100644 --- a/net/socket.c +++ b/net/socket.c @@ -586,101 +586,68 @@ static int net_socket_udp_init(VLANState *vlan, return 0; } -int net_init_socket(QemuOpts *opts, const NetClientOptions *new_opts, +int net_init_socket(QemuOpts *old_opts, const NetClientOptions *opts, const char *name, VLANState *vlan) { - if (qemu_opt_get(opts, "fd")) { - int fd; + const NetdevSocketOptions *sock; - if (qemu_opt_get(opts, "listen") || - qemu_opt_get(opts, "connect") || - qemu_opt_get(opts, "mcast") || - qemu_opt_get(opts, "localaddr")) { - error_report("listen=, connect=, mcast= and localaddr= is invalid with fd="); - return -1; - } + assert(opts->kind == NET_CLIENT_OPTIONS_KIND_SOCKET); + sock = opts->socket; - fd = net_handle_fd_param(cur_mon, qemu_opt_get(opts, "fd")); - if (fd == -1) { - return -1; - } + 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=" + " is required"); + return -1; + } - if (!net_socket_fd_init(vlan, "socket", name, fd, 1)) { - return -1; - } - } else if (qemu_opt_get(opts, "listen")) { - const char *listen; - - if (qemu_opt_get(opts, "fd") || - qemu_opt_get(opts, "connect") || - qemu_opt_get(opts, "mcast") || - qemu_opt_get(opts, "localaddr")) { - error_report("fd=, connect=, mcast= and localaddr= is invalid with listen="); - return -1; - } + if (sock->has_localaddr && !sock->has_mcast && !sock->has_udp) { + error_report("localaddr= is only valid with mcast= or udp="); + return -1; + } - listen = qemu_opt_get(opts, "listen"); + if (sock->has_fd) { + int fd; - if (net_socket_listen_init(vlan, "socket", name, listen) == -1) { - return -1; - } - } else if (qemu_opt_get(opts, "connect")) { - const char *connect; - - if (qemu_opt_get(opts, "fd") || - qemu_opt_get(opts, "listen") || - qemu_opt_get(opts, "mcast") || - qemu_opt_get(opts, "localaddr")) { - error_report("fd=, listen=, mcast= and localaddr= is invalid with connect="); + fd = net_handle_fd_param(cur_mon, sock->fd); + if (fd == -1 || !net_socket_fd_init(vlan, "socket", name, fd, 1)) { return -1; } + return 0; + } - connect = qemu_opt_get(opts, "connect"); - - if (net_socket_connect_init(vlan, "socket", name, connect) == -1) { + if (sock->has_listen) { + if (net_socket_listen_init(vlan, "socket", name, sock->listen) == -1) { return -1; } - } else if (qemu_opt_get(opts, "mcast")) { - const char *mcast, *localaddr; + return 0; + } - if (qemu_opt_get(opts, "fd") || - qemu_opt_get(opts, "connect") || - qemu_opt_get(opts, "listen")) { - error_report("fd=, connect= and listen= is invalid with mcast="); + if (sock->has_connect) { + if (net_socket_connect_init(vlan, "socket", name, sock->connect) == + -1) { return -1; } + return 0; + } - mcast = qemu_opt_get(opts, "mcast"); - localaddr = qemu_opt_get(opts, "localaddr"); - - if (net_socket_mcast_init(vlan, "socket", name, mcast, localaddr) == -1) { - return -1; - } - } else if (qemu_opt_get(opts, "udp")) { - const char *udp, *localaddr; - - if (qemu_opt_get(opts, "fd") || - qemu_opt_get(opts, "connect") || - qemu_opt_get(opts, "listen") || - qemu_opt_get(opts, "mcast")) { - error_report("fd=, connect=, listen=" - " and mcast= is invalid with udp="); + if (sock->has_mcast) { + /* if sock->localaddr is missing, it has been initialized to "all bits + * zero" */ + if (net_socket_mcast_init(vlan, "socket", name, sock->mcast, + sock->localaddr) == -1) { return -1; } + return 0; + } - udp = qemu_opt_get(opts, "udp"); - localaddr = qemu_opt_get(opts, "localaddr"); - if (localaddr == NULL) { - error_report("localaddr= is mandatory with udp="); - return -1; - } - - if (net_socket_udp_init(vlan, "udp", name, udp, localaddr) == -1) { - return -1; - } - } else { - error_report("-socket requires fd=, listen=," - " connect=, mcast= or udp="); + assert(sock->has_udp); + if (!sock->has_localaddr) { + error_report("localaddr= is mandatory with udp="); + return -1; + } + if (net_socket_udp_init(vlan, "udp", name, sock->udp, sock->localaddr) == + -1) { return -1; } return 0;