From patchwork Sun Sep 27 13:32:24 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vladislav Grishenko X-Patchwork-Id: 1483 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director10.mail.ord1d.rsapps.net ([172.30.191.6]) by backend30.mail.ord1d.rsapps.net with LMTP id wBKuLl0hcV/FZAAAIUCqbw (envelope-from ) for ; Sun, 27 Sep 2020 19:33:49 -0400 Received: from proxy18.mail.ord1d.rsapps.net ([172.30.191.6]) by director10.mail.ord1d.rsapps.net with LMTP id mE+eLl0hcV/XWQAApN4f7A (envelope-from ) for ; Sun, 27 Sep 2020 19:33:49 -0400 Received: from smtp13.gate.ord1d ([172.30.191.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy18.mail.ord1d.rsapps.net with LMTPS id IIAxLl0hcV9JIgAATCaURg (envelope-from ) for ; Sun, 27 Sep 2020 19:33:49 -0400 X-Spam-Threshold: 95 X-Spam-Score: 0 X-Spam-Flag: NO X-Virus-Scanned: OK X-Orig-To: openvpnslackdevel@openvpn.net X-Originating-Ip: [216.105.38.7] Authentication-Results: smtp13.gate.ord1d.rsapps.net; iprev=pass policy.iprev="216.105.38.7"; spf=pass smtp.mailfrom="openvpn-devel-bounces@lists.sourceforge.net" smtp.helo="lists.sourceforge.net"; dkim=fail (signature verification failed) header.d=sourceforge.net; dkim=fail (signature verification failed) header.d=sf.net; dkim=fail (signature verification failed) header.d=yandex-team.ru; dmarc=fail (p=none; dis=none) header.from=yandex-team.ru X-Suspicious-Flag: YES X-Classification-ID: e48ab51a-0119-11eb-ae2c-525400b197d9-1-1 Received: from [216.105.38.7] ([216.105.38.7:48184] helo=lists.sourceforge.net) by smtp13.gate.ord1d.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 15/02-30229-C51217F5; Sun, 27 Sep 2020 19:33:49 -0400 Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.90_1) (envelope-from ) id 1kMgA2-0008F5-Oi; Sun, 27 Sep 2020 23:32:54 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kMgA0-0008Ex-RI for openvpn-devel@lists.sourceforge.net; Sun, 27 Sep 2020 23:32:53 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=References:In-Reply-To:Message-Id:Date:Subject:To: From:Sender:Reply-To:Cc:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=y3GxlTQJ2yS0s/8ZJgV4h9LC7PzETlID4/wjzBqZmBg=; b=Vdt3X0O6uAlp9zTzGzY3BOd1VP rnvBO6ox7d+XO5BFtSGiOZnqFIftfE+I9oRNEDYPHjYd7HP8/ge761Cba8vOs3L8IOVwKmLpPpVh3 s/WGxJNZmBGJ4qMLaMu/mPTfKKHKDbqMpQ2ytI8VVUWWM2VKvKuSwMTVL04vVfKHEXhE=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=References:In-Reply-To:Message-Id:Date:Subject:To:From:Sender:Reply-To:Cc :MIME-Version:Content-Type:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=y3GxlTQJ2yS0s/8ZJgV4h9LC7PzETlID4/wjzBqZmBg=; b=m9GyIXhWu8DmaROYqAMEsXEEwF Yv3KpI61NT0/ChBf7TLfRiN7LiwfQ6HyeE5Dx8+PNsr3Lvas14m+bL+uea2bBtGoG+r7WkZxVgyoS gaI57DaNAAPWxTf8OIj6mLHR2E9G1V6BNhXIwUgQtyfI4jJG8QQGFhpsqwu63ByMGTg8=; Received: from forwardcorp1o.mail.yandex.net ([95.108.205.193]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92.2) id 1kMg9u-00Amim-1N for openvpn-devel@lists.sourceforge.net; Sun, 27 Sep 2020 23:32:52 +0000 Received: from myt5-23f0be3aa648.qloud-c.yandex.net (myt5-23f0be3aa648.qloud-c.yandex.net [IPv6:2a02:6b8:c12:3e29:0:640:23f0:be3a]) by forwardcorp1o.mail.yandex.net (Yandex) with ESMTP id 0EC0E2E15DD for ; Mon, 28 Sep 2020 02:32:37 +0300 (MSK) Received: from myt4-18a966dbd9be.qloud-c.yandex.net (myt4-18a966dbd9be.qloud-c.yandex.net [2a02:6b8:c00:12ad:0:640:18a9:66db]) by myt5-23f0be3aa648.qloud-c.yandex.net (mxbackcorp/Yandex) with ESMTP id ooaxGpRHz4-WawmgJaC; Mon, 28 Sep 2020 02:32:36 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1601249556; bh=y3GxlTQJ2yS0s/8ZJgV4h9LC7PzETlID4/wjzBqZmBg=; h=In-Reply-To:Message-Id:References:Date:Subject:To:From; b=OEMZlEpcv2TsM8cSUf63m8kPtwGZEB1CTqoGxDAsAARX5qdeOH7buTJXhl+iPOkdo qtYCNUxx1Up9FBXYACwoxjoUrfPhEHSL+KHKTbTpuBj4UNL9hSqbtJkNiNZ94LGpNp 71vFhzUxIs+5X/dGe7VndVotaF9C5YVKe4ZG1ZBg= Received: from 37.9.83.137-vpn.dhcp.yndx.net (37.9.83.137-vpn.dhcp.yndx.net [37.9.83.137]) by myt4-18a966dbd9be.qloud-c.yandex.net (smtpcorp/Yandex) with ESMTPSA id RGmbQtObaQ-WamuiFgw; Mon, 28 Sep 2020 02:32:36 +0300 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (Client certificate not present) From: Vladislav Grishenko To: openvpn-devel@lists.sourceforge.net Date: Mon, 28 Sep 2020 04:32:24 +0500 Message-Id: <20200927233224.2859-1-themiron@yandex-team.ru> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200927231513.27608-1-themiron@yandex-team.ru> References: <20200927231513.27608-1-themiron@yandex-team.ru> X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. 0.0 URIBL_BLOCKED ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [URIs: yandex-team.ru] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid X-Headers-End: 1kMg9u-00Amim-1N Subject: [Openvpn-devel] [PATCH] Speedup TCP remote hosts connections X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox For non-blocking TCP/Unix connection, OpenVPN checks was it established in loop and if not - sleeps or handles management for next one second. Since the first check is made right after the connection attempt, it will likely be always unsuccessful, resulting in redundant wait for one or more seconds: 00:00:00.667607 fcntl(5, F_SETFL, O_RDONLY|O_NONBLOCK) = 0 00:00:00.667713 connect(5, {...}, 16) = -1 EINPROGRESS (Operation now in progress) 00:00:00.667832 poll([{fd=5, events=POLLOUT}], 1, 0) = 0 (Timeout) 00:00:00.667954 nanosleep({tv_sec=1, tv_nsec=0}, 0x7fff52450270) = 0 00:00:01.668608 poll([{fd=5, events=POLLOUT}], 1, 0) = 1 ([{fd=5, revents=POLLOUT}]) With this patch openvpn_connect() will wait for connection establishment for the first second (if possible) and will return success immediately: this speedups connection and provides real connection time that can be used for detection of the fastest remote server in subsequent patches: 00:00:00.019133 fcntl(5, F_SETFL, O_RDONLY|O_NONBLOCK) = 0 00:00:00.019265 connect(5, {...}, 16) = -1 EINPROGRESS (Operation now in progress) 00:00:00.019388 poll([{fd=5, events=POLLOUT}], 1, 1000) = 1 ([{fd=5, revents=POLLOUT}]) If connection still can't established - this should be treated as either too slow/far or non-responding server, so imprecise connection checks every next one second in loop will be performed as usual. v2: cosmetics, decrease connection_timeout to avoid wait more than it in total Signed-off-by: Vladislav Grishenko --- src/openvpn/socket.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/openvpn/socket.c b/src/openvpn/socket.c index 76bdbfc5..049216ff 100644 --- a/src/openvpn/socket.c +++ b/src/openvpn/socket.c @@ -1464,13 +1464,27 @@ openvpn_connect(socket_descriptor_t sd, #endif ) { + /* Non-blocking connection is in the progress and can be likely + * established within the first second with no need for imprecise + * sleeping in loop. + */ + bool connect_wait = (connect_timeout > 0); while (true) { #if POLL + int timeout = 0; struct pollfd fds[1]; + fds[0].fd = sd; fds[0].events = POLLOUT; - status = poll(fds, 1, 0); + if (connect_wait) + { + connect_wait = false; + connect_timeout--; + timeout = 1000; + } + + status = poll(fds, 1, timeout); #else fd_set writes; struct timeval tv; @@ -1479,6 +1493,12 @@ openvpn_connect(socket_descriptor_t sd, openvpn_fd_set(sd, &writes); tv.tv_sec = 0; tv.tv_usec = 0; + if (connect_wait) + { + connect_wait = false; + connect_timeout--; + tv.tv_sec = 1; + } status = select(sd + 1, NULL, &writes, NULL, &tv); #endif