From nobody Fri May 17 02:41:21 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 154756470430461.447870553122925; Tue, 15 Jan 2019 07:05:04 -0800 (PST) Received: from localhost ([127.0.0.1]:44902 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjQH1-0005Aq-5s for importer@patchew.org; Tue, 15 Jan 2019 10:05:03 -0500 Received: from eggs.gnu.org ([209.51.188.92]:48624) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjQDJ-0002kB-1J for qemu-devel@nongnu.org; Tue, 15 Jan 2019 10:01:17 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gjQDI-0008LY-1e for qemu-devel@nongnu.org; Tue, 15 Jan 2019 10:01:12 -0500 Received: from mx1.redhat.com ([209.132.183.28]:47820) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gjQDH-0008Ku-OX for qemu-devel@nongnu.org; Tue, 15 Jan 2019 10:01:11 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A3A4B369BC; Tue, 15 Jan 2019 14:53:18 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-112-55.ams2.redhat.com [10.36.112.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id F1BFC5D98E; Tue, 15 Jan 2019 14:53:15 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Date: Tue, 15 Jan 2019 14:52:45 +0000 Message-Id: <20190115145256.9593-2-berrange@redhat.com> In-Reply-To: <20190115145256.9593-1-berrange@redhat.com> References: <20190115145256.9593-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Tue, 15 Jan 2019 14:53:18 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 01/12] chardev: fix validation of options for QMP created chardevs 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: Laurent Vivier , Thomas Huth , Yongji Xie , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" The TLS creds option is not valid with certain address types. The user config was only checked for errors when parsing legacy QemuOpts, thus the user could pass unsupported values via QMP. Pull all code for validating options out into a new method qmp_chardev_validate_socket, that is called from the main qmp_chardev_open_socket method. This adds a missing check for rejecting TLS creds with the vsock address type. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Marc-Andr=C3=A9 Lureau Reviewed-by: Thomas Huth --- chardev/char-socket.c | 92 +++++++++++++++++++++++++++++++------------ 1 file changed, 66 insertions(+), 26 deletions(-) diff --git a/chardev/char-socket.c b/chardev/char-socket.c index eaa8e8b68f..6669acb35f 100644 --- a/chardev/char-socket.c +++ b/chardev/char-socket.c @@ -987,6 +987,65 @@ static gboolean socket_reconnect_timeout(gpointer opaq= ue) return false; } =20 + +static bool qmp_chardev_validate_socket(ChardevSocket *sock, + SocketAddress *addr, + Error **errp) +{ + /* Validate any options which have a dependancy on address type */ + switch (addr->type) { + case SOCKET_ADDRESS_TYPE_FD: + if (sock->has_reconnect) { + error_setg(errp, + "'reconnect' option is incompatible with " + "'fd' address type"); + return false; + } + if (sock->has_tls_creds && + !(sock->has_server && sock->server)) { + error_setg(errp, + "'tls_creds' option is incompatible with " + "'fd' address type as client"); + return false; + } + break; + + case SOCKET_ADDRESS_TYPE_UNIX: + if (sock->has_tls_creds) { + error_setg(errp, + "'tls_creds' option is incompatible with " + "'unix' address type"); + return false; + } + break; + + case SOCKET_ADDRESS_TYPE_INET: + break; + + case SOCKET_ADDRESS_TYPE_VSOCK: + if (sock->has_tls_creds) { + error_setg(errp, + "'tls_creds' option is incompatible with " + "'vsock' address type"); + return false; + } + + default: + break; + } + + /* Validate any options which have a dependancy on client vs server */ + if (!(sock->has_server && sock->server)) { + if (sock->has_websocket && sock->websocket) { + error_setg(errp, "%s", "Websocket client is not implemented"); + return false; + } + } + + return true; +} + + static void qmp_chardev_open_socket(Chardev *chr, ChardevBackend *backend, bool *be_opened, @@ -1004,11 +1063,6 @@ static void qmp_chardev_open_socket(Chardev *chr, QIOChannelSocket *sioc =3D NULL; SocketAddress *addr; =20 - if (!is_listen && is_websock) { - error_setg(errp, "%s", "Websocket client is not implemented"); - goto error; - } - s->is_listen =3D is_listen; s->is_telnet =3D is_telnet; s->is_tn3270 =3D is_tn3270; @@ -1049,10 +1103,10 @@ static void qmp_chardev_open_socket(Chardev *chr, =20 s->addr =3D addr =3D socket_address_flatten(sock->addr); =20 - if (sock->has_reconnect && addr->type =3D=3D SOCKET_ADDRESS_TYPE_FD) { - error_setg(errp, "'reconnect' option is incompatible with 'fd'"); + if (!qmp_chardev_validate_socket(sock, addr, errp)) { goto error; } + qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE); /* TODO SOCKET_ADDRESS_FD where fd has AF_UNIX */ if (addr->type =3D=3D SOCKET_ADDRESS_TYPE_UNIX) { @@ -1140,27 +1194,12 @@ static void qemu_chr_parse_socket(QemuOpts *opts, C= hardevBackend *backend, return; } =20 - backend->type =3D CHARDEV_BACKEND_KIND_SOCKET; - if (path) { - if (tls_creds) { - error_setg(errp, "TLS can only be used over TCP socket"); - return; - } - } else if (host) { - if (!port) { - error_setg(errp, "chardev: socket: no port given"); - return; - } - } else if (fd) { - /* We don't know what host to validate against when in client mode= */ - if (tls_creds && !is_listen) { - error_setg(errp, "TLS can not be used with pre-opened client F= D"); - return; - } - } else { - g_assert_not_reached(); + if (host && !port) { + error_setg(errp, "chardev: socket: no port given"); + return; } =20 + backend->type =3D CHARDEV_BACKEND_KIND_SOCKET; sock =3D backend->u.socket.data =3D g_new0(ChardevSocket, 1); qemu_chr_parse_common(opts, qapi_ChardevSocket_base(sock)); =20 @@ -1178,6 +1217,7 @@ static void qemu_chr_parse_socket(QemuOpts *opts, Cha= rdevBackend *backend, sock->wait =3D is_waitconnect; sock->has_reconnect =3D qemu_opt_find(opts, "reconnect"); sock->reconnect =3D reconnect; + sock->has_tls_creds =3D tls_creds; sock->tls_creds =3D g_strdup(tls_creds); =20 addr =3D g_new0(SocketAddressLegacy, 1); --=20 2.20.1 From nobody Fri May 17 02:41:21 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1547565494609840.1756913216684; Tue, 15 Jan 2019 07:18:14 -0800 (PST) Received: from localhost ([127.0.0.1]:48246 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjQTk-0006cN-Pd for importer@patchew.org; Tue, 15 Jan 2019 10:18:12 -0500 Received: from eggs.gnu.org ([209.51.188.92]:48236) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjQC3-0001jx-Sg for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:59:58 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gjQC3-0007Ex-55 for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:59:55 -0500 Received: from mx1.redhat.com ([209.132.183.28]:55724) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gjQC2-0007ES-Ry for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:59:55 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D1EF713A9E; Tue, 15 Jan 2019 14:53:21 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-112-55.ams2.redhat.com [10.36.112.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 02ACF4F9A1; Tue, 15 Jan 2019 14:53:18 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Date: Tue, 15 Jan 2019 14:52:46 +0000 Message-Id: <20190115145256.9593-3-berrange@redhat.com> In-Reply-To: <20190115145256.9593-1-berrange@redhat.com> References: <20190115145256.9593-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Tue, 15 Jan 2019 14:53:21 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 02/12] chardev: forbid 'reconnect' option with server 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: Laurent Vivier , Thomas Huth , Yongji Xie , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" The 'reconnect' option is used to give the sleep time, in seconds, before a client socket attempts to re-establish a connection to the server. It does not make sense to set this for server sockets, as they will always accept a new client connection immediately after the previous one went away. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Marc-Andr=C3=A9 Lureau Reviewed-by: Thomas Huth --- chardev/char-socket.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/chardev/char-socket.c b/chardev/char-socket.c index 6669acb35f..4570755adf 100644 --- a/chardev/char-socket.c +++ b/chardev/char-socket.c @@ -1035,7 +1035,14 @@ static bool qmp_chardev_validate_socket(ChardevSocke= t *sock, } =20 /* Validate any options which have a dependancy on client vs server */ - if (!(sock->has_server && sock->server)) { + if (!sock->has_server || sock->server) { + if (sock->has_reconnect) { + error_setg(errp, + "'reconnect' option is incompatible with " + "socket in server listen mode"); + return false; + } + } else { if (sock->has_websocket && sock->websocket) { error_setg(errp, "%s", "Websocket client is not implemented"); return false; --=20 2.20.1 From nobody Fri May 17 02:41:21 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1547564676013506.41227947420634; Tue, 15 Jan 2019 07:04:36 -0800 (PST) Received: from localhost ([127.0.0.1]:44780 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjQGY-0004ft-Sk for importer@patchew.org; Tue, 15 Jan 2019 10:04:34 -0500 Received: from eggs.gnu.org ([209.51.188.92]:46475) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjQ63-0004u4-AZ for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:53:44 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gjQ61-0004VT-TM for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:53:42 -0500 Received: from mx1.redhat.com ([209.132.183.28]:1648) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gjQ61-0004Qc-Kd for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:53:41 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id DF18087622; Tue, 15 Jan 2019 14:53:24 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-112-55.ams2.redhat.com [10.36.112.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 33A4A5DE8B; Tue, 15 Jan 2019 14:53:22 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Date: Tue, 15 Jan 2019 14:52:47 +0000 Message-Id: <20190115145256.9593-4-berrange@redhat.com> In-Reply-To: <20190115145256.9593-1-berrange@redhat.com> References: <20190115145256.9593-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Tue, 15 Jan 2019 14:53:24 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 03/12] chardev: forbid 'wait' option with client 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: Laurent Vivier , Thomas Huth , Yongji Xie , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" The 'wait'/'nowait' parameter is used to tell server sockets whether to block until a client is accepted during initialization. Client chardevs have always silently ignored this option. Various tests were mistakenly passing this option for their client chardevs. Signed-off-by: Daniel P. Berrang=C3=A9 Acked-by: Thomas Huth Reviewed-by: Marc-Andr=C3=A9 Lureau --- chardev/char-socket.c | 12 +++++++++++- tests/ivshmem-test.c | 2 +- tests/libqtest.c | 4 ++-- tests/test-filter-redirector.c | 4 ++-- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/chardev/char-socket.c b/chardev/char-socket.c index 4570755adf..233f16f72d 100644 --- a/chardev/char-socket.c +++ b/chardev/char-socket.c @@ -1047,6 +1047,12 @@ static bool qmp_chardev_validate_socket(ChardevSocke= t *sock, error_setg(errp, "%s", "Websocket client is not implemented"); return false; } + if (sock->has_wait) { + error_setg(errp, "%s", + "'wait' option is incompatible with " + "socket in client connect mode"); + return false; + } } =20 return true; @@ -1220,7 +1226,11 @@ static void qemu_chr_parse_socket(QemuOpts *opts, Ch= ardevBackend *backend, sock->tn3270 =3D is_tn3270; sock->has_websocket =3D true; sock->websocket =3D is_websock; - sock->has_wait =3D true; + /* + * We have different default to QMP for 'wait' when 'server' + * is set, hence we can't just check for existance of 'wait' + */ + sock->has_wait =3D qemu_opt_find(opts, "wait") || is_listen; sock->wait =3D is_waitconnect; sock->has_reconnect =3D qemu_opt_find(opts, "reconnect"); sock->reconnect =3D reconnect; diff --git a/tests/ivshmem-test.c b/tests/ivshmem-test.c index fe5eb304b1..faffc1c624 100644 --- a/tests/ivshmem-test.c +++ b/tests/ivshmem-test.c @@ -293,7 +293,7 @@ static void *server_thread(void *data) =20 static void setup_vm_with_server(IVState *s, int nvectors, bool msi) { - char *cmd =3D g_strdup_printf("-chardev socket,id=3Dchr0,path=3D%s,now= ait " + char *cmd =3D g_strdup_printf("-chardev socket,id=3Dchr0,path=3D%s " "-device ivshmem%s,chardev=3Dchr0,vectors= =3D%d", tmpserver, msi ? "-doorbell" : ",size=3D1M,msi=3Doff", diff --git a/tests/libqtest.c b/tests/libqtest.c index 55750dd68d..79bcb24b1c 100644 --- a/tests/libqtest.c +++ b/tests/libqtest.c @@ -232,9 +232,9 @@ QTestState *qtest_init_without_qmp_handshake(const char= *extra_args) qtest_add_abrt_handler(kill_qemu_hook_func, s); =20 command =3D g_strdup_printf("exec %s " - "-qtest unix:%s,nowait " + "-qtest unix:%s " "-qtest-log %s " - "-chardev socket,path=3D%s,nowait,id=3Dchar0= " + "-chardev socket,path=3D%s,id=3Dchar0 " "-mon chardev=3Dchar0,mode=3Dcontrol " "-machine accel=3Dqtest " "-display none " diff --git a/tests/test-filter-redirector.c b/tests/test-filter-redirector.c index 9ca9feabf8..6dc21dd4fb 100644 --- a/tests/test-filter-redirector.c +++ b/tests/test-filter-redirector.c @@ -96,7 +96,7 @@ static void test_redirector_tx(void) "-device %s,netdev=3Dqtest-bn0,id=3Dqtest-e0 " "-chardev socket,id=3Dredirector0,path=3D%s,server,nowait " "-chardev socket,id=3Dredirector1,path=3D%s,server,nowait " - "-chardev socket,id=3Dredirector2,path=3D%s,nowait " + "-chardev socket,id=3Dredirector2,path=3D%s " "-object filter-redirector,id=3Dqtest-f0,netdev=3Dqtest-bn0," "queue=3Dtx,outdev=3Dredirector0 " "-object filter-redirector,id=3Dqtest-f1,netdev=3Dqtest-bn0," @@ -166,7 +166,7 @@ static void test_redirector_rx(void) "-device %s,netdev=3Dqtest-bn0,id=3Dqtest-e0 " "-chardev socket,id=3Dredirector0,path=3D%s,server,nowait " "-chardev socket,id=3Dredirector1,path=3D%s,server,nowait " - "-chardev socket,id=3Dredirector2,path=3D%s,nowait " + "-chardev socket,id=3Dredirector2,path=3D%s " "-object filter-redirector,id=3Dqtest-f0,netdev=3Dqtest-bn0," "queue=3Drx,indev=3Dredirector0 " "-object filter-redirector,id=3Dqtest-f1,netdev=3Dqtest-bn0," --=20 2.20.1 From nobody Fri May 17 02:41:21 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1547565062110136.90188247918877; Tue, 15 Jan 2019 07:11:02 -0800 (PST) Received: from localhost ([127.0.0.1]:46311 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjQMi-0001XF-2A for importer@patchew.org; Tue, 15 Jan 2019 10:10:56 -0500 Received: from eggs.gnu.org ([209.51.188.92]:46488) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjQ64-0004ud-08 for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:53:45 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gjQ62-0004Vk-6H for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:53:43 -0500 Received: from mx1.redhat.com ([209.132.183.28]:37894) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gjQ61-0004RL-SV for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:53:42 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id DDC7F3D941; Tue, 15 Jan 2019 14:53:27 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-112-55.ams2.redhat.com [10.36.112.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3D9D05DE8B; Tue, 15 Jan 2019 14:53:25 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Date: Tue, 15 Jan 2019 14:52:48 +0000 Message-Id: <20190115145256.9593-5-berrange@redhat.com> In-Reply-To: <20190115145256.9593-1-berrange@redhat.com> References: <20190115145256.9593-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Tue, 15 Jan 2019 14:53:27 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 04/12] chardev: remove many local variables in qemu_chr_parse_socket 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: Laurent Vivier , Thomas Huth , Yongji Xie , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Now that all validation is separated off into a separate method, we can directly populate the ChardevSocket struct from the QemuOpts values, avoiding many local variables. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Marc-Andr=C3=A9 Lureau --- chardev/char-socket.c | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/chardev/char-socket.c b/chardev/char-socket.c index 233f16f72d..ba86284ea9 100644 --- a/chardev/char-socket.c +++ b/chardev/char-socket.c @@ -1186,18 +1186,10 @@ error: static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, Error **errp) { - bool is_listen =3D qemu_opt_get_bool(opts, "server", false); - bool is_waitconnect =3D is_listen && qemu_opt_get_bool(opts, "wait", t= rue); - bool is_telnet =3D qemu_opt_get_bool(opts, "telnet", false); - bool is_tn3270 =3D qemu_opt_get_bool(opts, "tn3270", false); - bool is_websock =3D qemu_opt_get_bool(opts, "websocket", false); - bool do_nodelay =3D !qemu_opt_get_bool(opts, "delay", true); - int64_t reconnect =3D qemu_opt_get_number(opts, "reconnect", 0); const char *path =3D qemu_opt_get(opts, "path"); const char *host =3D qemu_opt_get(opts, "host"); const char *port =3D qemu_opt_get(opts, "port"); const char *fd =3D qemu_opt_get(opts, "fd"); - const char *tls_creds =3D qemu_opt_get(opts, "tls-creds"); SocketAddressLegacy *addr; ChardevSocket *sock; =20 @@ -1216,26 +1208,30 @@ static void qemu_chr_parse_socket(QemuOpts *opts, C= hardevBackend *backend, sock =3D backend->u.socket.data =3D g_new0(ChardevSocket, 1); qemu_chr_parse_common(opts, qapi_ChardevSocket_base(sock)); =20 - sock->has_nodelay =3D true; - sock->nodelay =3D do_nodelay; + sock->has_nodelay =3D qemu_opt_get(opts, "delay"); + sock->nodelay =3D !qemu_opt_get_bool(opts, "delay", true); + /* + * We have different default to QMP for 'server', hence + * we can't just check for existance of 'server' + */ sock->has_server =3D true; - sock->server =3D is_listen; - sock->has_telnet =3D true; - sock->telnet =3D is_telnet; - sock->has_tn3270 =3D true; - sock->tn3270 =3D is_tn3270; - sock->has_websocket =3D true; - sock->websocket =3D is_websock; + sock->server =3D qemu_opt_get_bool(opts, "server", false); + sock->has_telnet =3D qemu_opt_get(opts, "telnet"); + sock->telnet =3D qemu_opt_get_bool(opts, "telnet", false); + sock->has_tn3270 =3D qemu_opt_get(opts, "tn3270"); + sock->tn3270 =3D qemu_opt_get_bool(opts, "tn3270", false); + sock->has_websocket =3D qemu_opt_get(opts, "websocket"); + sock->websocket =3D qemu_opt_get_bool(opts, "websocket", false); /* * We have different default to QMP for 'wait' when 'server' * is set, hence we can't just check for existance of 'wait' */ - sock->has_wait =3D qemu_opt_find(opts, "wait") || is_listen; - sock->wait =3D is_waitconnect; + sock->has_wait =3D qemu_opt_find(opts, "wait") || sock->server; + sock->wait =3D qemu_opt_get_bool(opts, "wait", true); sock->has_reconnect =3D qemu_opt_find(opts, "reconnect"); - sock->reconnect =3D reconnect; - sock->has_tls_creds =3D tls_creds; - sock->tls_creds =3D g_strdup(tls_creds); + sock->reconnect =3D qemu_opt_get_number(opts, "reconnect", 0); + sock->has_tls_creds =3D qemu_opt_get(opts, "tls-creds"); + sock->tls_creds =3D g_strdup(qemu_opt_get(opts, "tls-creds")); =20 addr =3D g_new0(SocketAddressLegacy, 1); if (path) { --=20 2.20.1 From nobody Fri May 17 02:41:21 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1547565621191543.5043272988127; Tue, 15 Jan 2019 07:20:21 -0800 (PST) Received: from localhost ([127.0.0.1]:48738 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjQVg-0007qD-E9 for importer@patchew.org; Tue, 15 Jan 2019 10:20:12 -0500 Received: from eggs.gnu.org ([209.51.188.92]:48318) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjQCK-00020b-Kw for qemu-devel@nongnu.org; Tue, 15 Jan 2019 10:00:16 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gjQCE-0007Qm-SO for qemu-devel@nongnu.org; Tue, 15 Jan 2019 10:00:12 -0500 Received: from mx1.redhat.com ([209.132.183.28]:43332) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gjQCE-0007QJ-Mi for qemu-devel@nongnu.org; Tue, 15 Jan 2019 10:00:06 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B09E8C05FF9D; Tue, 15 Jan 2019 14:53:35 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-112-55.ams2.redhat.com [10.36.112.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3BC454DC3A; Tue, 15 Jan 2019 14:53:28 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Date: Tue, 15 Jan 2019 14:52:49 +0000 Message-Id: <20190115145256.9593-6-berrange@redhat.com> In-Reply-To: <20190115145256.9593-1-berrange@redhat.com> References: <20190115145256.9593-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 15 Jan 2019 14:53:35 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 05/12] chardev: ensure qemu_chr_parse_compat reports missing driver error 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: Laurent Vivier , Thomas Huth , Yongji Xie , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" If no valid char driver was identified the qemu_chr_parse_compat method was silent, leaving callers no clue what failed. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Marc-Andr=C3=A9 Lureau --- chardev/char.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/chardev/char.c b/chardev/char.c index ccba36bafb..b99f3692f7 100644 --- a/chardev/char.c +++ b/chardev/char.c @@ -490,6 +490,8 @@ QemuOpts *qemu_chr_parse_compat(const char *label, cons= t char *filename, return opts; } =20 + error_report("'%s' is not a valid char driver", filename); + fail: qemu_opts_del(opts); return NULL; --=20 2.20.1 From nobody Fri May 17 02:41:21 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1547564873474120.71492447459536; Tue, 15 Jan 2019 07:07:53 -0800 (PST) Received: from localhost ([127.0.0.1]:45536 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjQJg-0007Ll-3u for importer@patchew.org; Tue, 15 Jan 2019 10:07:48 -0500 Received: from eggs.gnu.org ([209.51.188.92]:46479) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjQ63-0004uN-Lu for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:53:44 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gjQ62-0004Vb-5u for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:53:43 -0500 Received: from mx1.redhat.com ([209.132.183.28]:48194) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gjQ61-0004Uo-RT for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:53:42 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9D1B420262; Tue, 15 Jan 2019 14:53:39 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-112-55.ams2.redhat.com [10.36.112.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 46A004DC3A; Tue, 15 Jan 2019 14:53:35 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Date: Tue, 15 Jan 2019 14:52:50 +0000 Message-Id: <20190115145256.9593-7-berrange@redhat.com> In-Reply-To: <20190115145256.9593-1-berrange@redhat.com> References: <20190115145256.9593-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Tue, 15 Jan 2019 14:53:39 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 06/12] chardev: remove unused 'sioc' variable & cleanup paths 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: Laurent Vivier , Thomas Huth , Yongji Xie , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" The 'sioc' variable in qmp_chardev_open_socket was unused since commit 3e7d4d20d3a528b1ed10b1dc3d83119bfb0c5f24 Author: Peter Xu Date: Tue Mar 6 13:33:17 2018 +0800 chardev: use chardev's gcontext for async connect Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Marc-Andr=C3=A9 Lureau Reviewed-by: Thomas Huth --- chardev/char-socket.c | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/chardev/char-socket.c b/chardev/char-socket.c index ba86284ea9..3b6ff6619b 100644 --- a/chardev/char-socket.c +++ b/chardev/char-socket.c @@ -1073,7 +1073,6 @@ static void qmp_chardev_open_socket(Chardev *chr, bool is_waitconnect =3D sock->has_wait ? sock->wait : false; bool is_websock =3D sock->has_websocket ? sock->websocket : false; int64_t reconnect =3D sock->has_reconnect ? sock->reconnect : 0; - QIOChannelSocket *sioc =3D NULL; SocketAddress *addr; =20 s->is_listen =3D is_listen; @@ -1088,7 +1087,7 @@ static void qmp_chardev_open_socket(Chardev *chr, if (!creds) { error_setg(errp, "No TLS credentials with id '%s'", sock->tls_creds); - goto error; + return; } s->tls_creds =3D (QCryptoTLSCreds *) object_dynamic_cast(creds, @@ -1096,20 +1095,20 @@ static void qmp_chardev_open_socket(Chardev *chr, if (!s->tls_creds) { error_setg(errp, "Object with id '%s' is not TLS credentials", sock->tls_creds); - goto error; + return; } object_ref(OBJECT(s->tls_creds)); if (is_listen) { if (s->tls_creds->endpoint !=3D QCRYPTO_TLS_CREDS_ENDPOINT_SER= VER) { error_setg(errp, "%s", "Expected TLS credentials for server endpoint"); - goto error; + return; } } else { if (s->tls_creds->endpoint !=3D QCRYPTO_TLS_CREDS_ENDPOINT_CLI= ENT) { error_setg(errp, "%s", "Expected TLS credentials for client endpoint"); - goto error; + return; } } } @@ -1117,7 +1116,7 @@ static void qmp_chardev_open_socket(Chardev *chr, s->addr =3D addr =3D socket_address_flatten(sock->addr); =20 if (!qmp_chardev_validate_socket(sock, addr, errp)) { - goto error; + return; } =20 qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE); @@ -1153,7 +1152,7 @@ static void qmp_chardev_open_socket(Chardev *chr, if (qio_net_listener_open_sync(s->listener, s->addr, errp) < 0= ) { object_unref(OBJECT(s->listener)); s->listener =3D NULL; - goto error; + return; } =20 qapi_free_SocketAddress(s->addr); @@ -1171,16 +1170,9 @@ static void qmp_chardev_open_socket(Chardev *chr, chr->gcontext); } } else if (qemu_chr_wait_connected(chr, errp) < 0) { - goto error; + return; } } - - return; - -error: - if (sioc) { - object_unref(OBJECT(sioc)); - } } =20 static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, --=20 2.20.1 From nobody Fri May 17 02:41:21 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1547564263188209.6209524373926; Tue, 15 Jan 2019 06:57:43 -0800 (PST) Received: from localhost ([127.0.0.1]:43100 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjQ9u-0007qH-2W for importer@patchew.org; Tue, 15 Jan 2019 09:57:42 -0500 Received: from eggs.gnu.org ([209.51.188.92]:46556) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjQ6B-00056c-Kb for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:53:53 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gjQ6A-0004Yv-5r for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:53:51 -0500 Received: from mx1.redhat.com ([209.132.183.28]:44880) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gjQ68-0004YP-Ck for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:53:50 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B03148E595; Tue, 15 Jan 2019 14:53:46 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-112-55.ams2.redhat.com [10.36.112.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2C7135D98E; Tue, 15 Jan 2019 14:53:39 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Date: Tue, 15 Jan 2019 14:52:51 +0000 Message-Id: <20190115145256.9593-8-berrange@redhat.com> In-Reply-To: <20190115145256.9593-1-berrange@redhat.com> References: <20190115145256.9593-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Tue, 15 Jan 2019 14:53:46 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 07/12] chardev: split tcp_chr_wait_connected into two methods 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: Laurent Vivier , Thomas Huth , Yongji Xie , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" The tcp_chr_wait_connected method can deal with either server or client chardevs, but some callers only care about one of these possibilities. The tcp_chr_wait_connected method will also need some refactoring to reliably deal with its primary goal of allowing a device frontend to wait for an established connection, which will interfere with other callers. Split it into two methods, one responsible for server initiated connections, the other responsible for client initiated connections. In doing this split the tcp_char_connect_async() method is renamed to become consistent with naming of the new methods. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Marc-Andr=C3=A9 Lureau --- chardev/char-socket.c | 59 +++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/chardev/char-socket.c b/chardev/char-socket.c index 3b6ff6619b..3bd1be7631 100644 --- a/chardev/char-socket.c +++ b/chardev/char-socket.c @@ -886,30 +886,47 @@ static void tcp_chr_accept(QIONetListener *listener, tcp_chr_new_client(chr, cioc); } =20 -static int tcp_chr_wait_connected(Chardev *chr, Error **errp) + +static int tcp_chr_connect_client_sync(Chardev *chr, Error **errp) +{ + SocketChardev *s =3D SOCKET_CHARDEV(chr); + QIOChannelSocket *sioc =3D qio_channel_socket_new(); + tcp_chr_set_client_ioc_name(chr, sioc); + if (qio_channel_socket_connect_sync(sioc, s->addr, errp) < 0) { + object_unref(OBJECT(sioc)); + return -1; + } + tcp_chr_new_client(chr, sioc); + object_unref(OBJECT(sioc)); + return 0; +} + + +static void tcp_chr_accept_server_sync(Chardev *chr) { SocketChardev *s =3D SOCKET_CHARDEV(chr); QIOChannelSocket *sioc; + info_report("QEMU waiting for connection on: %s", + chr->filename); + sioc =3D qio_net_listener_wait_client(s->listener); + tcp_chr_set_client_ioc_name(chr, sioc); + tcp_chr_new_client(chr, sioc); + object_unref(OBJECT(sioc)); +} + =20 +static int tcp_chr_wait_connected(Chardev *chr, Error **errp) +{ + SocketChardev *s =3D SOCKET_CHARDEV(chr); /* It can't wait on s->connected, since it is set asynchronously * in TLS and telnet cases, only wait for an accepted socket */ while (!s->ioc) { if (s->is_listen) { - info_report("QEMU waiting for connection on: %s", - chr->filename); - sioc =3D qio_net_listener_wait_client(s->listener); - tcp_chr_set_client_ioc_name(chr, sioc); - tcp_chr_new_client(chr, sioc); - object_unref(OBJECT(sioc)); + tcp_chr_accept_server_sync(chr); } else { - sioc =3D qio_channel_socket_new(); - tcp_chr_set_client_ioc_name(chr, sioc); - if (qio_channel_socket_connect_sync(sioc, s->addr, errp) < 0) { - object_unref(OBJECT(sioc)); + if (tcp_chr_connect_client_sync(chr, errp) < 0) { return -1; } - tcp_chr_new_client(chr, sioc); - object_unref(OBJECT(sioc)); } } =20 @@ -958,7 +975,7 @@ cleanup: object_unref(OBJECT(sioc)); } =20 -static void tcp_chr_connect_async(Chardev *chr) +static void tcp_chr_connect_client_async(Chardev *chr) { SocketChardev *s =3D SOCKET_CHARDEV(chr); QIOChannelSocket *sioc; @@ -982,7 +999,7 @@ static gboolean socket_reconnect_timeout(gpointer opaqu= e) return false; } =20 - tcp_chr_connect_async(chr); + tcp_chr_connect_client_async(chr); =20 return false; } @@ -1139,7 +1156,7 @@ static void qmp_chardev_open_socket(Chardev *chr, } =20 if (s->reconnect_time) { - tcp_chr_connect_async(chr); + tcp_chr_connect_client_async(chr); } else { if (s->is_listen) { char *name; @@ -1159,17 +1176,15 @@ static void qmp_chardev_open_socket(Chardev *chr, s->addr =3D socket_local_address(s->listener->sioc[0]->fd, err= p); update_disconnected_filename(s); =20 - if (is_waitconnect && - qemu_chr_wait_connected(chr, errp) < 0) { - return; - } - if (!s->ioc) { + if (is_waitconnect) { + tcp_chr_accept_server_sync(chr); + } else { qio_net_listener_set_client_func_full(s->listener, tcp_chr_accept, chr, NULL, chr->gcontext); } - } else if (qemu_chr_wait_connected(chr, errp) < 0) { + } else if (tcp_chr_connect_client_sync(chr, errp) < 0) { return; } } --=20 2.20.1 From nobody Fri May 17 02:41:21 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1547565258362839.0837362128888; Tue, 15 Jan 2019 07:14:18 -0800 (PST) Received: from localhost ([127.0.0.1]:47198 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjQPt-0003qS-Gf for importer@patchew.org; Tue, 15 Jan 2019 10:14:13 -0500 Received: from eggs.gnu.org ([209.51.188.92]:46624) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjQ6L-0005Kr-1J for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:54:01 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gjQ6K-0004c3-3b for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:54:00 -0500 Received: from mx1.redhat.com ([209.132.183.28]:38644) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gjQ6J-0004bi-S0 for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:54:00 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6185788E56; Tue, 15 Jan 2019 14:53:58 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-112-55.ams2.redhat.com [10.36.112.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0F84C5D98E; Tue, 15 Jan 2019 14:53:46 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Date: Tue, 15 Jan 2019 14:52:52 +0000 Message-Id: <20190115145256.9593-9-berrange@redhat.com> In-Reply-To: <20190115145256.9593-1-berrange@redhat.com> References: <20190115145256.9593-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 15 Jan 2019 14:53:58 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 08/12] chardev: split up qmp_chardev_open_socket connection code 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: Laurent Vivier , Thomas Huth , Yongji Xie , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" In qmp_chardev_open_socket the code for connecting client chardevs is split across two conditionals far apart with some server chardev code in the middle. Split up the method so that code for client connection setup is separate from code for server connection setup. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Marc-Andr=C3=A9 Lureau --- chardev/char-socket.c | 96 +++++++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 36 deletions(-) diff --git a/chardev/char-socket.c b/chardev/char-socket.c index 3bd1be7631..385504b021 100644 --- a/chardev/char-socket.c +++ b/chardev/char-socket.c @@ -1005,6 +1005,61 @@ static gboolean socket_reconnect_timeout(gpointer op= aque) } =20 =20 +static int qmp_chardev_open_socket_server(Chardev *chr, + bool is_telnet, + bool is_waitconnect, + Error **errp) +{ + SocketChardev *s =3D SOCKET_CHARDEV(chr); + char *name; + if (is_telnet) { + s->do_telnetopt =3D 1; + } + s->listener =3D qio_net_listener_new(); + + name =3D g_strdup_printf("chardev-tcp-listener-%s", chr->label); + qio_net_listener_set_name(s->listener, name); + g_free(name); + + if (qio_net_listener_open_sync(s->listener, s->addr, errp) < 0) { + object_unref(OBJECT(s->listener)); + s->listener =3D NULL; + return -1; + } + + qapi_free_SocketAddress(s->addr); + s->addr =3D socket_local_address(s->listener->sioc[0]->fd, errp); + update_disconnected_filename(s); + + if (is_waitconnect) { + tcp_chr_accept_server_sync(chr); + } else { + qio_net_listener_set_client_func_full(s->listener, + tcp_chr_accept, + chr, NULL, + chr->gcontext); + } + + return 0; +} + + +static int qmp_chardev_open_socket_client(Chardev *chr, + int64_t reconnect, + Error **errp) +{ + SocketChardev *s =3D SOCKET_CHARDEV(chr); + + if (reconnect > 0) { + s->reconnect_time =3D reconnect; + tcp_chr_connect_client_async(chr); + return 0; + } else { + return tcp_chr_connect_client_sync(chr, errp); + } +} + + static bool qmp_chardev_validate_socket(ChardevSocket *sock, SocketAddress *addr, Error **errp) @@ -1147,44 +1202,13 @@ static void qmp_chardev_open_socket(Chardev *chr, =20 update_disconnected_filename(s); =20 - if (is_listen) { - if (is_telnet || is_tn3270) { - s->do_telnetopt =3D 1; + if (s->is_listen) { + if (qmp_chardev_open_socket_server(chr, is_telnet || is_tn3270, + is_waitconnect, errp) < 0) { + return; } - } else if (reconnect > 0) { - s->reconnect_time =3D reconnect; - } - - if (s->reconnect_time) { - tcp_chr_connect_client_async(chr); } else { - if (s->is_listen) { - char *name; - s->listener =3D qio_net_listener_new(); - - name =3D g_strdup_printf("chardev-tcp-listener-%s", chr->label= ); - qio_net_listener_set_name(s->listener, name); - g_free(name); - - if (qio_net_listener_open_sync(s->listener, s->addr, errp) < 0= ) { - object_unref(OBJECT(s->listener)); - s->listener =3D NULL; - return; - } - - qapi_free_SocketAddress(s->addr); - s->addr =3D socket_local_address(s->listener->sioc[0]->fd, err= p); - update_disconnected_filename(s); - - if (is_waitconnect) { - tcp_chr_accept_server_sync(chr); - } else { - qio_net_listener_set_client_func_full(s->listener, - tcp_chr_accept, - chr, NULL, - chr->gcontext); - } - } else if (tcp_chr_connect_client_sync(chr, errp) < 0) { + if (qmp_chardev_open_socket_client(chr, reconnect, errp) < 0) { return; } } --=20 2.20.1 From nobody Fri May 17 02:41:21 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1547565378085736.9083436541764; Tue, 15 Jan 2019 07:16:18 -0800 (PST) Received: from localhost ([127.0.0.1]:47770 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjQRp-0005RX-0J for importer@patchew.org; Tue, 15 Jan 2019 10:16:13 -0500 Received: from eggs.gnu.org ([209.51.188.92]:46648) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjQ6Q-0005PH-1K for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:54:07 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gjQ6M-0004cl-7w for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:54:05 -0500 Received: from mx1.redhat.com ([209.132.183.28]:54656) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gjQ6L-0004cU-Js for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:54:02 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id DBDCB87648; Tue, 15 Jan 2019 14:54:00 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-112-55.ams2.redhat.com [10.36.112.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id B37795DF27; Tue, 15 Jan 2019 14:53:58 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Date: Tue, 15 Jan 2019 14:52:53 +0000 Message-Id: <20190115145256.9593-10-berrange@redhat.com> In-Reply-To: <20190115145256.9593-1-berrange@redhat.com> References: <20190115145256.9593-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Tue, 15 Jan 2019 14:54:00 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 09/12] chardev: use a state machine for socket connection state 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: Laurent Vivier , Thomas Huth , Yongji Xie , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" The socket connection state is indicated via the 'bool connected' field in the SocketChardev struct. This variable is somewhat misleading though, as it is only set to true once the connection has completed all required handshakes (eg for TLS, telnet or websockets). IOW there is a period of time in which the socket is connected, but the "connected" flag is still false. The socket chardev really has three states that it can be in, disconnected, connecting and connected and those should be tracked explicitly. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Marc-Andr=C3=A9 Lureau --- chardev/char-socket.c | 63 +++++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 14 deletions(-) diff --git a/chardev/char-socket.c b/chardev/char-socket.c index 385504b021..96a60eb105 100644 --- a/chardev/char-socket.c +++ b/chardev/char-socket.c @@ -46,6 +46,12 @@ typedef struct { size_t buflen; } TCPChardevTelnetInit; =20 +typedef enum { + TCP_CHARDEV_STATE_DISCONNECTED, + TCP_CHARDEV_STATE_CONNECTING, + TCP_CHARDEV_STATE_CONNECTED, +} TCPChardevState; + typedef struct { Chardev parent; QIOChannel *ioc; /* Client I/O channel */ @@ -53,7 +59,7 @@ typedef struct { QIONetListener *listener; GSource *hup_source; QCryptoTLSCreds *tls_creds; - int connected; + TCPChardevState state; int max_size; int do_telnetopt; int do_nodelay; @@ -82,6 +88,21 @@ typedef struct { static gboolean socket_reconnect_timeout(gpointer opaque); static void tcp_chr_telnet_init(Chardev *chr); =20 +static void tcp_chr_change_state(SocketChardev *s, TCPChardevState state) +{ + switch (state) { + case TCP_CHARDEV_STATE_DISCONNECTED: + break; + case TCP_CHARDEV_STATE_CONNECTING: + assert(s->state =3D=3D TCP_CHARDEV_STATE_DISCONNECTED); + break; + case TCP_CHARDEV_STATE_CONNECTED: + assert(s->state =3D=3D TCP_CHARDEV_STATE_CONNECTING); + break; + } + s->state =3D state; +} + static void tcp_chr_reconn_timer_cancel(SocketChardev *s) { if (s->reconnect_timer) { @@ -96,7 +117,7 @@ static void qemu_chr_socket_restart_timer(Chardev *chr) SocketChardev *s =3D SOCKET_CHARDEV(chr); char *name; =20 - assert(s->connected =3D=3D 0); + assert(s->state =3D=3D TCP_CHARDEV_STATE_DISCONNECTED); name =3D g_strdup_printf("chardev-socket-reconnect-%s", chr->label); s->reconnect_timer =3D qemu_chr_timeout_add_ms(chr, s->reconnect_time * 1000, @@ -131,7 +152,7 @@ static int tcp_chr_write(Chardev *chr, const uint8_t *b= uf, int len) { SocketChardev *s =3D SOCKET_CHARDEV(chr); =20 - if (s->connected) { + if (s->state =3D=3D TCP_CHARDEV_STATE_CONNECTED) { int ret =3D io_channel_send_full(s->ioc, buf, len, s->write_msgfds, s->write_msgfds_num); @@ -164,7 +185,7 @@ static int tcp_chr_read_poll(void *opaque) { Chardev *chr =3D CHARDEV(opaque); SocketChardev *s =3D SOCKET_CHARDEV(opaque); - if (!s->connected) { + if (s->state !=3D TCP_CHARDEV_STATE_CONNECTED) { return 0; } s->max_size =3D qemu_chr_be_can_write(chr); @@ -277,7 +298,7 @@ static int tcp_set_msgfds(Chardev *chr, int *fds, int n= um) s->write_msgfds =3D NULL; s->write_msgfds_num =3D 0; =20 - if (!s->connected || + if ((s->state !=3D TCP_CHARDEV_STATE_CONNECTED) || !qio_channel_has_feature(s->ioc, QIO_CHANNEL_FEATURE_FD_PASS)) { return -1; @@ -389,7 +410,7 @@ static void tcp_chr_free_connection(Chardev *chr) s->ioc =3D NULL; g_free(chr->filename); chr->filename =3D NULL; - s->connected =3D 0; + tcp_chr_change_state(s, TCP_CHARDEV_STATE_DISCONNECTED); } =20 static const char *qemu_chr_socket_protocol(SocketChardev *s) @@ -442,12 +463,12 @@ static void update_disconnected_filename(SocketCharde= v *s) =20 /* NB may be called even if tcp_chr_connect has not been * reached, due to TLS or telnet initialization failure, - * so can *not* assume s->connected =3D=3D true + * so can *not* assume s->state =3D=3D TCP_CHARDEV_STATE_CONNECTED */ static void tcp_chr_disconnect(Chardev *chr) { SocketChardev *s =3D SOCKET_CHARDEV(chr); - bool emit_close =3D s->connected; + bool emit_close =3D s->state =3D=3D TCP_CHARDEV_STATE_CONNECTED; =20 tcp_chr_free_connection(chr); =20 @@ -471,7 +492,8 @@ static gboolean tcp_chr_read(QIOChannel *chan, GIOCondi= tion cond, void *opaque) uint8_t buf[CHR_READ_BUF_LEN]; int len, size; =20 - if (!s->connected || s->max_size <=3D 0) { + if ((s->state !=3D TCP_CHARDEV_STATE_CONNECTED) || + s->max_size <=3D 0) { return TRUE; } len =3D sizeof(buf); @@ -508,7 +530,7 @@ static int tcp_chr_sync_read(Chardev *chr, const uint8_= t *buf, int len) SocketChardev *s =3D SOCKET_CHARDEV(chr); int size; =20 - if (!s->connected) { + if (s->state !=3D TCP_CHARDEV_STATE_CONNECTED) { return 0; } =20 @@ -564,7 +586,7 @@ static void update_ioc_handlers(SocketChardev *s) { Chardev *chr =3D CHARDEV(s); =20 - if (!s->connected) { + if (s->state !=3D TCP_CHARDEV_STATE_CONNECTED) { return; } =20 @@ -589,7 +611,7 @@ static void tcp_chr_connect(void *opaque) g_free(chr->filename); chr->filename =3D qemu_chr_compute_filename(s); =20 - s->connected =3D 1; + tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTED); update_ioc_handlers(s); qemu_chr_be_event(chr, CHR_EVENT_OPENED); } @@ -828,7 +850,7 @@ static int tcp_chr_new_client(Chardev *chr, QIOChannelS= ocket *sioc) { SocketChardev *s =3D SOCKET_CHARDEV(chr); =20 - if (s->ioc !=3D NULL) { + if (s->state !=3D TCP_CHARDEV_STATE_CONNECTING) { return -1; } =20 @@ -865,11 +887,17 @@ static int tcp_chr_add_client(Chardev *chr, int fd) { int ret; QIOChannelSocket *sioc; + SocketChardev *s =3D SOCKET_CHARDEV(chr); + + if (s->state !=3D TCP_CHARDEV_STATE_DISCONNECTED) { + return -1; + } =20 sioc =3D qio_channel_socket_new_fd(fd, NULL); if (!sioc) { return -1; } + tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING); tcp_chr_set_client_ioc_name(chr, sioc); ret =3D tcp_chr_new_client(chr, sioc); object_unref(OBJECT(sioc)); @@ -881,7 +909,9 @@ static void tcp_chr_accept(QIONetListener *listener, void *opaque) { Chardev *chr =3D CHARDEV(opaque); + SocketChardev *s =3D SOCKET_CHARDEV(chr); =20 + tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING); tcp_chr_set_client_ioc_name(chr, cioc); tcp_chr_new_client(chr, cioc); } @@ -891,8 +921,10 @@ static int tcp_chr_connect_client_sync(Chardev *chr, E= rror **errp) { SocketChardev *s =3D SOCKET_CHARDEV(chr); QIOChannelSocket *sioc =3D qio_channel_socket_new(); + tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING); tcp_chr_set_client_ioc_name(chr, sioc); if (qio_channel_socket_connect_sync(sioc, s->addr, errp) < 0) { + tcp_chr_change_state(s, TCP_CHARDEV_STATE_DISCONNECTED); object_unref(OBJECT(sioc)); return -1; } @@ -908,6 +940,7 @@ static void tcp_chr_accept_server_sync(Chardev *chr) QIOChannelSocket *sioc; info_report("QEMU waiting for connection on: %s", chr->filename); + tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING); sioc =3D qio_net_listener_wait_client(s->listener); tcp_chr_set_client_ioc_name(chr, sioc); tcp_chr_new_client(chr, sioc); @@ -963,6 +996,7 @@ static void qemu_chr_socket_connected(QIOTask *task, vo= id *opaque) Error *err =3D NULL; =20 if (qio_task_propagate_error(task, &err)) { + tcp_chr_change_state(s, TCP_CHARDEV_STATE_DISCONNECTED); check_report_connect_error(chr, err); error_free(err); goto cleanup; @@ -980,6 +1014,7 @@ static void tcp_chr_connect_client_async(Chardev *chr) SocketChardev *s =3D SOCKET_CHARDEV(chr); QIOChannelSocket *sioc; =20 + tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING); sioc =3D qio_channel_socket_new(); tcp_chr_set_client_ioc_name(chr, sioc); qio_channel_socket_connect_async(sioc, s->addr, @@ -1307,7 +1342,7 @@ char_socket_get_connected(Object *obj, Error **errp) { SocketChardev *s =3D SOCKET_CHARDEV(obj); =20 - return s->connected; + return s->state =3D=3D TCP_CHARDEV_STATE_CONNECTED; } =20 static void char_socket_class_init(ObjectClass *oc, void *data) --=20 2.20.1 From nobody Fri May 17 02:41:21 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1547565242099243.09218960136354; Tue, 15 Jan 2019 07:14:02 -0800 (PST) Received: from localhost ([127.0.0.1]:47137 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjQPd-0003fD-4z for importer@patchew.org; Tue, 15 Jan 2019 10:13:57 -0500 Received: from eggs.gnu.org ([209.51.188.92]:46761) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjQ6s-0005lx-Ck for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:54:35 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gjQ6r-0004sk-Ow for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:54:34 -0500 Received: from mx1.redhat.com ([209.132.183.28]:36490) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gjQ6p-0004r8-7c for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:54:32 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8CF30C05FEF1; Tue, 15 Jan 2019 14:54:27 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-112-55.ams2.redhat.com [10.36.112.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2F3B14DC3A; Tue, 15 Jan 2019 14:54:00 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Date: Tue, 15 Jan 2019 14:52:54 +0000 Message-Id: <20190115145256.9593-11-berrange@redhat.com> In-Reply-To: <20190115145256.9593-1-berrange@redhat.com> References: <20190115145256.9593-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 15 Jan 2019 14:54:27 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 10/12] chardev: honour the reconnect setting in tcp_chr_wait_connected 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: Laurent Vivier , Thomas Huth , Yongji Xie , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" If establishing a client connection fails, the tcp_chr_wait_connected method should sleep for the reconnect timeout and then retry the attempt. This ensures the callers don't immediately abort with an error when the initial connection fails. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Marc-Andr=C3=A9 Lureau --- chardev/char-socket.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/chardev/char-socket.c b/chardev/char-socket.c index 96a60eb105..91d775e9c5 100644 --- a/chardev/char-socket.c +++ b/chardev/char-socket.c @@ -957,8 +957,15 @@ static int tcp_chr_wait_connected(Chardev *chr, Error = **errp) if (s->is_listen) { tcp_chr_accept_server_sync(chr); } else { - if (tcp_chr_connect_client_sync(chr, errp) < 0) { - return -1; + Error *err =3D NULL; + if (tcp_chr_connect_client_sync(chr, &err) < 0) { + if (s->reconnect_time) { + error_free(err); + g_usleep(s->reconnect_time); + } else { + error_propagate(errp, err); + return -1; + } } } } --=20 2.20.1 From nobody Fri May 17 02:41:21 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1547565509419920.729873939439; Tue, 15 Jan 2019 07:18:29 -0800 (PST) Received: from localhost ([127.0.0.1]:48295 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjQU0-0006m2-Dh for importer@patchew.org; Tue, 15 Jan 2019 10:18:28 -0500 Received: from eggs.gnu.org ([209.51.188.92]:46770) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjQ6t-0005mc-4A for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:54:36 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gjQ6r-0004su-TB for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:54:35 -0500 Received: from mx1.redhat.com ([209.132.183.28]:45962) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gjQ6r-0004rq-LM for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:54:33 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E8F7BA0E45; Tue, 15 Jan 2019 14:54:30 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-112-55.ams2.redhat.com [10.36.112.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0836B5D98E; Tue, 15 Jan 2019 14:54:27 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Date: Tue, 15 Jan 2019 14:52:55 +0000 Message-Id: <20190115145256.9593-12-berrange@redhat.com> In-Reply-To: <20190115145256.9593-1-berrange@redhat.com> References: <20190115145256.9593-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Tue, 15 Jan 2019 14:54:31 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 11/12] chardev: disallow TLS/telnet/websocket with tcp_chr_wait_connected 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: Laurent Vivier , Thomas Huth , Yongji Xie , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" In the previous commit commit 1dc8a6695c731abb7461c637b2512c3670d82be4 Author: Marc-Andr=C3=A9 Lureau Date: Tue Aug 16 12:33:32 2016 +0400 char: fix waiting for TLS and telnet connection the tcp_chr_wait_connected() method was changed to check for a non-NULL 's->ioc' as a sign that there is already a connection present, as opposed to checking the "connected" flag to supposedly fix handling of TLS/telnet connections. The original code would repeatedly call tcp_chr_wait_connected creating many connections as 'connected' would never become true. The changed code would still repeatedly call tcp_chr_wait_connected busy waiting because s->ioc is set but the chardev will never see CHR_EVENT_OPENED. IOW, the code is still broken with TLS/telnet, but in a different way. Checking for a non-NULL 's->ioc' does not mean that a CHR_EVENT_OPENED will be ready for a TLS/telnet connection. These protocols (and the websocket protocol) all require the main loop to be running in order to complete the protocol handshake before emitting CHR_EVENT_OPENED. The tcp_chr_wait_connected() method is only used during early startup before a main loop is running, so TLS/telnet/websock connections can never complete initialization. Making this work would require changing tcp_chr_wait_connected to run a main loop. This is quite complex since we must not allow GSource's that other parts of QEMU have registered to run yet. The current callers of tcp_chr_wait_connected do not require use of the TLS/telnet/websocket protocols, so the simplest option is to just forbid this combination completely for now. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Marc-Andr=C3=A9 Lureau --- chardev/char-socket.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/chardev/char-socket.c b/chardev/char-socket.c index 91d775e9c5..7e98a95bbd 100644 --- a/chardev/char-socket.c +++ b/chardev/char-socket.c @@ -951,8 +951,20 @@ static void tcp_chr_accept_server_sync(Chardev *chr) static int tcp_chr_wait_connected(Chardev *chr, Error **errp) { SocketChardev *s =3D SOCKET_CHARDEV(chr); - /* It can't wait on s->connected, since it is set asynchronously - * in TLS and telnet cases, only wait for an accepted socket */ + const char *opts[] =3D { "telnet", "tn3270", "websock", "tls-creds" }; + bool optset[] =3D { s->is_telnet, s->is_tn3270, s->is_websock, s->tls_= creds }; + size_t i; + + QEMU_BUILD_BUG_ON(G_N_ELEMENTS(opts) !=3D G_N_ELEMENTS(optset)); + for (i =3D 0; i < G_N_ELEMENTS(opts); i++) { + if (optset[i]) { + error_setg(errp, + "'%s' option is incompatible with waiting for " + "connection during early startup", opts[i]); + return -1; + } + } + while (!s->ioc) { if (s->is_listen) { tcp_chr_accept_server_sync(chr); --=20 2.20.1 From nobody Fri May 17 02:41:21 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1547564499028414.2048840269845; Tue, 15 Jan 2019 07:01:39 -0800 (PST) Received: from localhost ([127.0.0.1]:44081 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjQDi-0002Tc-0S for importer@patchew.org; Tue, 15 Jan 2019 10:01:38 -0500 Received: from eggs.gnu.org ([209.51.188.92]:46790) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjQ6v-0005p6-MU for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:54:38 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gjQ6t-0004tk-PA for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:54:37 -0500 Received: from mx1.redhat.com ([209.132.183.28]:45592) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gjQ6t-0004sz-2h for qemu-devel@nongnu.org; Tue, 15 Jan 2019 09:54:35 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BF20CC028351; Tue, 15 Jan 2019 14:54:33 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-112-55.ams2.redhat.com [10.36.112.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4B7FD18E52; Tue, 15 Jan 2019 14:54:31 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Date: Tue, 15 Jan 2019 14:52:56 +0000 Message-Id: <20190115145256.9593-13-berrange@redhat.com> In-Reply-To: <20190115145256.9593-1-berrange@redhat.com> References: <20190115145256.9593-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 15 Jan 2019 14:54:33 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 12/12] chardev: fix race with client connections in tcp_chr_wait_connected 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: Laurent Vivier , Thomas Huth , Yongji Xie , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" When the 'reconnect' option is given for a client connection, the qmp_chardev_open_socket_client method will run an asynchronous connection attempt. The QIOChannel socket executes this is a single use background thread, so the connection will succeed immediately (assuming the server is listening). The chardev, however, won't get the result from this background thread until the main loop starts running and processes idle callbacks. Thus when tcp_chr_wait_connected is run s->ioc will be NULL, and the state will still be TCP_CHARDEV_STATE_DISCONNECTED, but there will already be an established connection that will be associated with the chardev by the pending idle callback. tcp_chr_wait_connected doesn't see this and so attempts to establish another connection synchronously. If the server allows multiple connections this is unhelpful but not a fatal problem as the duplicate connection will get ignored by the tcp_chr_new_client method when it sees the state is already connected. If the server only supports a single connection, however, the tcp_chr_wait_connected method will hang forever because the server will not accept its synchronous connection attempt until the first connection is closed. To deal with this we must ensure that qmp_chardev_open_socket_client does not actually start the asynchronous connection attempt. Instead it should schedule a timer with 0ms expiry time, which will only be processed once the main loop starts running. The tcp_chr_wait_connected method can now safely do a synchronous connection attempt without creating a race condition. When the timer expires it will see that a connection has already been established and take no further action. Signed-off-by: Daniel P. Berrang=C3=A9 --- chardev/char-socket.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/chardev/char-socket.c b/chardev/char-socket.c index 7e98a95bbd..07942d7a1b 100644 --- a/chardev/char-socket.c +++ b/chardev/char-socket.c @@ -965,7 +965,25 @@ static int tcp_chr_wait_connected(Chardev *chr, Error = **errp) } } =20 - while (!s->ioc) { + /* + * We expect state to be as follows: + * + * - server + * - wait -> CONNECTED + * - nowait -> DISCONNECTED + * - client + * - reconnect =3D=3D 0 -> CONNECTED + * - reconnect !=3D 0 -> DISCONNECTED + * + */ + if (s->state =3D=3D TCP_CHARDEV_STATE_CONNECTING) { + error_setg(errp, + "Unexpected 'connecting' state when waiting for " + "connection during early startup"); + return -1; + } + + while (s->state !=3D TCP_CHARDEV_STATE_CONNECTED) { if (s->is_listen) { tcp_chr_accept_server_sync(chr); } else { @@ -1106,7 +1124,15 @@ static int qmp_chardev_open_socket_client(Chardev *c= hr, =20 if (reconnect > 0) { s->reconnect_time =3D reconnect; - tcp_chr_connect_client_async(chr); + /* + * We must not start the socket connect attempt until the main + * loop is running, otherwise qemu_chr_wait_connect will not be + * able to take over connection establishment during startup + */ + s->reconnect_timer =3D qemu_chr_timeout_add_ms(chr, + 0, + socket_reconnect_time= out, + chr); return 0; } else { return tcp_chr_connect_client_sync(chr, errp); --=20 2.20.1