From patchwork Sun Sep 27 13:15:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vladislav Grishenko X-Patchwork-Id: 1482 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director9.mail.ord1d.rsapps.net ([172.30.191.6]) by backend30.mail.ord1d.rsapps.net with LMTP id 0F4eA3MdcV89NAAAIUCqbw (envelope-from ) for ; Sun, 27 Sep 2020 19:17:07 -0400 Received: from proxy6.mail.ord1d.rsapps.net ([172.30.191.6]) by director9.mail.ord1d.rsapps.net with LMTP id YIkTA3MdcV9DSgAAalYnBA (envelope-from ) for ; Sun, 27 Sep 2020 19:17:07 -0400 Received: from smtp26.gate.ord1d ([172.30.191.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy6.mail.ord1d.rsapps.net with LMTPS id ELuCAnMdcV+HEwAAQyIf0w (envelope-from ) for ; Sun, 27 Sep 2020 19:17:07 -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: smtp26.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: 8efd65cc-0117-11eb-8dda-525400c5b129-1-1 Received: from [216.105.38.7] ([216.105.38.7:47846] helo=lists.sourceforge.net) by smtp26.gate.ord1d.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 2A/4A-28476-27D117F5; Sun, 27 Sep 2020 19:17:06 -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 1kMftN-0007aS-44; Sun, 27 Sep 2020 23:15:41 +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 1kMftL-0007aL-P2 for openvpn-devel@lists.sourceforge.net; Sun, 27 Sep 2020 23:15:39 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=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:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=FvYyANR9eifpQnrO1N8nnWU0GlkvL9vB2lRXPwMI0Hk=; b=St7lPxPfC6Vv8m/K7i/JDcb2CB zw2QLmZmsNXzvs5n19GVi54Oabn8IQUVtdJac6ghwWdZ/gU00Iks5xgksf2S1aUXNj0tV1v1NVzeC 0RE12x5fxg755CJAa9y6SdvHqErqv6vZOXSpjbCDpvlvSp1uM/mnD80N9Rf1vzCCXTGI=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=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: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=FvYyANR9eifpQnrO1N8nnWU0GlkvL9vB2lRXPwMI0Hk=; b=jdgQb2W7PG2pLWSw3ojY0aFaqe dsI1SCeoMPVIRnGLY3C+FVMrozgpP4jrPZ+J0CcsbcWGJ6fojm4Sqynh9asTlc+gxqBJlXXg9Pr1t CAUMT4CrfZjKe7aEhDtjr7evfF3AYoBPTOmj0R6X44c5u7Ageg43wJDqbnVrpE5fhIPg=; Received: from forwardcorp1p.mail.yandex.net ([77.88.29.217]) by sfi-mx-3.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92.2) id 1kMftE-0088Mc-Qd for openvpn-devel@lists.sourceforge.net; Sun, 27 Sep 2020 23:15:39 +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 forwardcorp1p.mail.yandex.net (Yandex) with ESMTP id 3B1B42E14C1 for ; Mon, 28 Sep 2020 02:15:23 +0300 (MSK) Received: from myt5-70c90f7d6d7d.qloud-c.yandex.net (myt5-70c90f7d6d7d.qloud-c.yandex.net [2a02:6b8:c12:3e2c:0:640:70c9:f7d]) by myt5-23f0be3aa648.qloud-c.yandex.net (mxbackcorp/Yandex) with ESMTP id RqtkfOhFO1-FNwmaH9q; Mon, 28 Sep 2020 02:15:23 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1601248523; bh=FvYyANR9eifpQnrO1N8nnWU0GlkvL9vB2lRXPwMI0Hk=; h=Message-Id:Date:Subject:To:From; b=yLY0eRRNJUsOq2DQsSHKlo8mfVq1jr8Wq5Xp2WXXerm1512uhB1ntqSbtY0ewoxlX D8/4XYXwD/oJs03SyL+YO2wYUS3GhU5fuAe56UtjrHG90oOtD5uqgjTRlDNAM0cAUU c5mKriQIMLW9NZ7wS5aZ33WY1cnprSSp2Iij3dx4= Received: from 37.9.83.137-vpn.dhcp.yndx.net (37.9.83.137-vpn.dhcp.yndx.net [37.9.83.137]) by myt5-70c90f7d6d7d.qloud-c.yandex.net (smtpcorp/Yandex) with ESMTPSA id la9yXVotaK-FMnuanQx; Mon, 28 Sep 2020 02:15:22 +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:15:13 +0500 Message-Id: <20200927231513.27608-1-themiron@yandex-team.ru> X-Mailer: git-send-email 2.17.1 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: 1kMftE-0088Mc-Qd 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. Signed-off-by: Vladislav Grishenko --- src/openvpn/socket.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/openvpn/socket.c b/src/openvpn/socket.c index 76bdbfc5..66b1e4e2 100644 --- a/src/openvpn/socket.c +++ b/src/openvpn/socket.c @@ -1464,13 +1464,26 @@ 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; + timeout = 1000; + } + + status = poll(fds, 1, timeout); #else fd_set writes; struct timeval tv; @@ -1479,6 +1492,11 @@ 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; + tv.tv_sec = 1; + } status = select(sd + 1, NULL, &writes, NULL, &tv); #endif