From patchwork Mon Jul 8 02:29:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13726134 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3A90B184F; Mon, 8 Jul 2024 02:30:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720405812; cv=none; b=YzUkKMlo8lZ6U24IYBMxaK6eBn2Xual6BbKmrxFb9I+28eQXEvr8ISFl2zwAEzJ4X+E/ZWCjNMoEy9BdeU0r5f93Mnk8yE2oDRumfsaMRiBb+ECVjESmoziiiQa8wzKQ0HIt4yMeJG6VJxydd5jdruP50g89HJiFFr7SABWVbHk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720405812; c=relaxed/simple; bh=bAX1OLxg4DkksZvjRAzn/L1GhmwINGXrUi0DgLiNpt4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DgWYetkHn64wBRSusieN7k3ZK5askKh70uxxY7l6DKhSo+4DkiFk67/f7+KV/d60Bg74RGTRNTabyBU4v2B3HDBScNlW08qOt+fn46aKMfMn2NKIyg/Lb5aK6yp9yazqCNoKtcsHTQiNd6eDkV+aswwazf/VCesYufJGhpuxnLI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=LIvfY4sy; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="LIvfY4sy" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BBCB0C3277B; Mon, 8 Jul 2024 02:30:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720405811; bh=bAX1OLxg4DkksZvjRAzn/L1GhmwINGXrUi0DgLiNpt4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LIvfY4sykQmxUTM9nl3RcnC6xZ4gWee2y1YjeKwxap/1gy1uJKZ3ftNmHCVwgbojt qmJEaVAjojmpfmm26PyGD0EArejZOhQNWEeCAV68vHo60mCh9G2IbRKAAb40AdavJX aGaTZlDMUmLX/orWwJVekHrDEb8BbfLohGm6JajReW4jdKMhwpZcy/NnfWJ5o+zy8C t77dDQSo36GETC1Pd7fwHAcxqtzNaHGfjll7Rpls2tjOulcRaZszJylXHLl0lyFHox QxSNFmaTDNssx6DXTCiwW/78dO5I/+eT3oGhYg8a/nrQNgDy54oGaoZaiEFNao2TDG TMmSsjqq8K1dQ== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v10 01/12] selftests/bpf: Add backlog for network_helper_opts Date: Mon, 8 Jul 2024 10:29:39 +0800 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang Some callers expect __start_server() helper to pass their own "backlog" value to listen() instead of the default of 1. So this patch adds struct member "backlog" for network_helper_opts to allow callers to set "backlog" value via start_server_str() helper. listen(fd, 0 /* backlog */) can be used to enforce syncookie. Meaning backlog 0 is a legit value. Using 0 as a default and changing it to 1 here is fine. It makes the test program easier to write for the common case. Enforcing syncookie mode by using backlog 0 is a niche use case but it should at least have a way for the caller to do that. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 2 +- tools/testing/selftests/bpf/network_helpers.h | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 44c2c8fa542a..e0cba4178e41 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -106,7 +106,7 @@ static int __start_server(int type, const struct sockaddr *addr, socklen_t addrl } if (type == SOCK_STREAM) { - if (listen(fd, 1) < 0) { + if (listen(fd, opts->backlog ? MAX(opts->backlog, 0) : 1) < 0) { log_err("Failed to listed on socket"); goto error_close; } diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 9ea36524b9db..4f26bfc2dbf5 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -25,6 +25,10 @@ struct network_helper_opts { int timeout_ms; bool must_fail; int proto; + /* The backlog argument for listen(), defines the maximum length to which + * the queue of pending connections for sockfd may grow. + */ + int backlog; int (*post_socket_cb)(int fd, void *opts); void *cb_opts; }; From patchwork Mon Jul 8 02:29:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13726135 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 352051FA3; Mon, 8 Jul 2024 02:30:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720405819; cv=none; b=lVUqwm4OXGGjY8nBKQPCMpv5WHYMyNMoQXwBuGP/01vmbxV0gUzFJ7z0EXFB1Zo4JzqKvO5o/MXbz4I4HPTWX8HAW/YDIY0Qr6mbc3ltGzep5YDHAJi+/XC++pdudg5/SDptXUPh4zWnfxSX1VrHYFq1LQ/ItpUWo5MbBetO4I0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720405819; c=relaxed/simple; bh=00pdNC+8+bJCChvMSP7B2ks/Ry/OoNnOXIemGC5RruM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IfNnO/zBDkPmZty/I7NG45VKDXJebcKWeWNoGGixCZ5vwZKMVvKBwlKuySpaynMZgQWCNizYo6ZQW8MwMfB/DFxd7Pn7bMEkQsAx+BCe0c/Ew7CXLDksJL2uaBxgszo0CaGaSIxfuVh/M7/ebjfSWMTeGwHSCG85fKDbKxuNSsc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=U7O9D65f; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="U7O9D65f" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9643DC4AF14; Mon, 8 Jul 2024 02:30:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720405818; bh=00pdNC+8+bJCChvMSP7B2ks/Ry/OoNnOXIemGC5RruM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=U7O9D65f6HmnYQhWcurwSJSsWMHQW56LEVQakloS0vjHZDJmnaBYmm0oBLxGB0mYt kBGcq8PM/TAtNWZKGRtDkEPsg/J6AXSm85OAEOQ/G6SWdh0qkFaQcwB1+Ka0nKX6o5 mwOaqh7FUor6RnOa18sFSWzgQv1hf5PL/MvUc6CuqJXXOtNHlNYF1Rtj3bAO2hLFeg HuK2TcH06817YBFoO8E+X2BpmO2Q98u4W+l4uG6CnI8GKbWuPRh8ynQP+rn9sEH57x 4Tp8Wb6FD9WPOiJErqjf35Rs+ToCRA6kWRRQdhtDigDLGCLe8Jmssh/ad9wdI8j+9O wYClT+kMQGRoQ== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v10 02/12] selftests/bpf: Use start_server_str in sockmap_ktls Date: Mon, 8 Jul 2024 10:29:40 +0800 Message-ID: <52e84e1325b977c971d1745f8056a1cf9b5add17.1720405046.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang Include network_helpers.h in prog_tests/sockmap_ktls.c, use public network helper start_server_str() instead of local defined function tcp_server(). This can avoid duplicate code. Technically, this is not a one-for-one replacement, as start_server_str() also does bind(). But the difference does not seem to matter. Acked-by: Eduard Zingerman Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sockmap_ktls.c | 23 +++++-------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c index 2d0796314862..32be112967a5 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c @@ -6,25 +6,11 @@ #include #include "test_progs.h" +#include "network_helpers.h" #define MAX_TEST_NAME 80 #define TCP_ULP 31 -static int tcp_server(int family) -{ - int err, s; - - s = socket(family, SOCK_STREAM, 0); - if (!ASSERT_GE(s, 0, "socket")) - return -1; - - err = listen(s, SOMAXCONN); - if (!ASSERT_OK(err, "listen")) - return -1; - - return s; -} - static int disconnect(int fd) { struct sockaddr unspec = { AF_UNSPEC }; @@ -35,12 +21,15 @@ static int disconnect(int fd) /* Disconnect (unhash) a kTLS socket after removing it from sockmap. */ static void test_sockmap_ktls_disconnect_after_delete(int family, int map) { + struct network_helper_opts opts = { + .backlog = SOMAXCONN, + }; struct sockaddr_storage addr = {0}; socklen_t len = sizeof(addr); int err, cli, srv, zero = 0; - srv = tcp_server(family); - if (srv == -1) + srv = start_server_str(family, SOCK_STREAM, NULL, 0, &opts); + if (!ASSERT_GE(srv, 0, "start_server_str")) return; err = getsockname(srv, (struct sockaddr *)&addr, &len); From patchwork Mon Jul 8 02:29:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13726136 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 03931184F; Mon, 8 Jul 2024 02:30:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720405826; cv=none; b=db4sdJ2xojLqWgpOD8K5LclRuh2RXLJtCR07bUMkbNaO1rKSjuUpmMYa+XLOtRpqfiwfSiErHqAvXRe2JlRXBDpb2gP8PC+8p+uP0zPNwd8h+Cl4nY/qydzKymISoHCqdSyvOVhcs0kC8zNsgCoe95wlo+H749VRsDlpVr1Z76M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720405826; c=relaxed/simple; bh=Qq9hn27IvadosGSaFEY1IhNuXDt86aHiR/dUXu2AMTk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SViJaJO2rWLNxFHDMgCxGxbANiWpssWDReU9EEA/He2+Gp0gJDjn04raardrS8AezXjJfrxUW/rro5sp5a3bTgEY3W34dWuBvjkNycC4KMuOh4Mjzl/RBRLwUROaRRtbbUUxp4e+5RDT8HRg60qiBmwGySayFTHaheiDXWUyf+8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DvBMcH6s; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="DvBMcH6s" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8C39EC4AF0E; Mon, 8 Jul 2024 02:30:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720405825; bh=Qq9hn27IvadosGSaFEY1IhNuXDt86aHiR/dUXu2AMTk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DvBMcH6snhujarZnl0iZUu1eGJ8IsjBTrEOAtl5knifC55cEMg1520ZqBzH8nJxJI k9JyPHPsYW34c0MFQDsFJfIVgm6OiktDVXg55UGsQCEgZh3ANDQuUOYCzhKe4YYjpw Qksm+5jyahozfAUV9+vSPS71kjK+FZ9eBku4pDBJ7/u8ltsWrsnOuaVwyg5MeHhLtI lzPoQTVtYr9ISZEoAGEbjnrAkIoEeYx+jpugCXlEW/qoGceyl5HPupuCvZOnGN53t+ FzRO0FMTOAHFKvY2PEeE+mOydqz+DXA3ZYmNqU0XO0z4cViaPM6iyh0hVbPRVXONch yR+1R1ZPNlEAQ== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v10 03/12] selftests/bpf: Use connect_to_fd_opts in sockmap_ktls Date: Mon, 8 Jul 2024 10:29:41 +0800 Message-ID: <20077624db9fbde04addd639f36ccfffbda5e033.1720405046.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang Use public network helper connect_to_fd_opts() instead of open-coding it in prog_tests/sockmap_ktls.c. This can avoid duplicate code. Acked-by: Eduard Zingerman Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sockmap_ktls.c | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c index 32be112967a5..a794aa688ab5 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c @@ -24,26 +24,16 @@ static void test_sockmap_ktls_disconnect_after_delete(int family, int map) struct network_helper_opts opts = { .backlog = SOMAXCONN, }; - struct sockaddr_storage addr = {0}; - socklen_t len = sizeof(addr); int err, cli, srv, zero = 0; srv = start_server_str(family, SOCK_STREAM, NULL, 0, &opts); if (!ASSERT_GE(srv, 0, "start_server_str")) return; - err = getsockname(srv, (struct sockaddr *)&addr, &len); - if (!ASSERT_OK(err, "getsockopt")) - goto close_srv; - - cli = socket(family, SOCK_STREAM, 0); - if (!ASSERT_GE(cli, 0, "socket")) + cli = connect_to_fd_opts(srv, SOCK_STREAM, NULL); + if (!ASSERT_GE(cli, 0, "connect_to_fd_opts")) goto close_srv; - err = connect(cli, (struct sockaddr *)&addr, len); - if (!ASSERT_OK(err, "connect")) - goto close_cli; - err = bpf_map_update_elem(map, &zero, &cli, 0); if (!ASSERT_OK(err, "bpf_map_update_elem")) goto close_cli; From patchwork Mon Jul 8 02:29:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13726137 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 40183538A; Mon, 8 Jul 2024 02:30:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720405832; cv=none; b=j3uP8wZJXbmh6vGx7Yi/x45RgWh9kqcJSib8hRO6mTA6PQn+LA+S5/KQDRW810stiBQh9wje5rfQmEdmr/SYrsRbKHbPohTA/BuZkXVCWtx/30XDIyFnabuhKT2j5fUq0CwbUPILAsPDsIINXK3t5jcNQzIK75/kYg5c9hWl4Sk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720405832; c=relaxed/simple; bh=rRP0ZiPRY0W17MVOWxZwWV5aU8LBd+K/G2ydWK7Wq+o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HEpDpxrBi88j2Wbg88oE0cYeLvXdPDfj3+o2g7VAIQxhxpTjxgh8p5LJRvL0qIBBzA+YXQl+CoX7sBIkUEHEeuOI5kFl+kVph7S05SupwVrTVOdWekvnQk5HTCfXaZP90pdSDn5O4VbOCmc9AFEexQnPHrigdb8i6OYZFWClVUI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=f6JuFKcK; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="f6JuFKcK" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3DB85C3277B; Mon, 8 Jul 2024 02:30:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720405831; bh=rRP0ZiPRY0W17MVOWxZwWV5aU8LBd+K/G2ydWK7Wq+o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=f6JuFKcKg1FWqh2nKb4KSB6piD37DM6ouwUy8vi29FPd2TrxbRwW3qYw0KeYqM2YQ N7dbiS2lFbCDaVQcY+NIHmkRXzqRHX57VswimH7UT7YarTraF/Q3t4qBDjQCF1M6IK axrFsCeKBZ11WRsuh0XjHbGmKBAtqfzHVhkWzdxUIo91CnXUhRUMX3DT0d80lfZFxD 8y08DBBgHhvrhXXYccUXzyDFSiwm47q7HSg7arwllA15gd8yxWEtgWJH/cN00UOffU Cb3zlxbAKYZUnZRa5M+Ng1BHMGG4t0ilW80wrRCcdU2XOjaYIJDhAYQauKe9h70wN4 y+yhRK044UEOA== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v10 04/12] selftests/bpf: Use make_sockaddr in sockmap_ktls Date: Mon, 8 Jul 2024 10:29:42 +0800 Message-ID: <8de45e7b849e086e4b91ee30c2896087f4e73a27.1720405046.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang This patch uses public helper make_sockaddr() exported in network_helpers.h instead of open-coding in sockmap_ktls.c. This can avoid duplicate code. Acked-by: Eduard Zingerman Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sockmap_ktls.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c index a794aa688ab5..3c223601e8b6 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c @@ -59,23 +59,11 @@ static void test_sockmap_ktls_update_fails_when_sock_has_ulp(int family, int map { struct sockaddr_storage addr = {}; socklen_t len = sizeof(addr); - struct sockaddr_in6 *v6; - struct sockaddr_in *v4; int err, s, zero = 0; - switch (family) { - case AF_INET: - v4 = (struct sockaddr_in *)&addr; - v4->sin_family = AF_INET; - break; - case AF_INET6: - v6 = (struct sockaddr_in6 *)&addr; - v6->sin6_family = AF_INET6; - break; - default: - PRINT_FAIL("unsupported socket family %d", family); + err = make_sockaddr(family, NULL, 0, &addr, &len); + if (!ASSERT_OK(err, "make_sockaddr")) return; - } s = socket(family, SOCK_STREAM, 0); if (!ASSERT_GE(s, 0, "socket")) From patchwork Mon Jul 8 02:29:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13726138 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E998FA94B; Mon, 8 Jul 2024 02:30:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720405839; cv=none; b=t4yvWxgYPnZcp2hQrpuFBVVSDGuaaKYVo4SecFLqjbbZbNGW4jttwvgFwh2ojYK4TLVJr2+jgKYH2EasmzOdmLwBDyT+WxcCuc0f5fDFweNBByztylSRUeczlYA5JwOv/esFdQQeKLtCrxKJDPpSRPVWvQE2lNEwLXIKzbFR610= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720405839; c=relaxed/simple; bh=HmXOdU8AvXotdcvvc/YDpHs6DrXYIXQa6FQSAA6yhPQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Sra1CMgvNviuX8RFkR9snyXBLJ2fGbcbnsIIQ088rnN9mvgjxL8KEjm71F0q9lq9MgvqWSLtFdS68f1cs5900c0p0bnAx+VQP3adBW7dqSqVpagjbFdaqj7MVARIpiAhpCSQQd2EBZEJdxiRIx3byENo7PunXjyxrXRulNWMlzg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=PKSfc0HJ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="PKSfc0HJ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 75DDEC4AF0E; Mon, 8 Jul 2024 02:30:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720405838; bh=HmXOdU8AvXotdcvvc/YDpHs6DrXYIXQa6FQSAA6yhPQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PKSfc0HJbse2jIutHgAXtSdK4C7nRYgL7SfD9/vgbfFxCfqtkAlfwkGYmGklL2I2V /EdLTb/ETyHvpV25UKiyBhGTns+obkaBsMM+6x7R4XVQB96sZcjZ6lrf+TtRpuWG1j OWTU/rDniDlAbwxIQ8UCHr/I5CLHmgh1osNJi91NHsMfCaryeXTCHByK3u9urtHPvT KdacFkIYEsQF+DnbRF+0PhwUhLeEtRO+x1VSHsXDsJNhf+wVdYQV+ohSs9gy/q0Bua TK9sPB0j1SD5SBV93Mx5pLIE7A6V8IyEUsK6yEDvkCT06/fjPdDQH1ZWxNwOEBagqj 4PLdJq/6eDFMQ== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v10 05/12] selftests/bpf: Add network_helper_opts for connect_fd_to_fd Date: Mon, 8 Jul 2024 10:29:43 +0800 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang To make connect_fd_to_fd() more flexible, use a struct network_helper_opts parameter "opts" instead of timeout_ms. With this change, other opts like "must_fail" can be used in connect_fd_to_fd() too. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 10 +++++++--- tools/testing/selftests/bpf/network_helpers.h | 3 ++- tools/testing/selftests/bpf/prog_tests/bpf_nf.c | 5 ++++- .../selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c | 5 ++++- .../testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c | 4 ++-- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index e0cba4178e41..475a5a04e61e 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -369,12 +369,16 @@ int connect_to_fd(int server_fd, int timeout_ms) return connect_to_fd_opts(server_fd, type, &opts); } -int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms) +int connect_fd_to_fd(int client_fd, int server_fd, + const struct network_helper_opts *opts) { struct sockaddr_storage addr; socklen_t len = sizeof(addr); - if (settimeo(client_fd, timeout_ms)) + if (!opts) + opts = &default_opts; + + if (settimeo(client_fd, opts->timeout_ms)) return -1; if (getsockname(server_fd, (struct sockaddr *)&addr, &len)) { @@ -382,7 +386,7 @@ int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms) return -1; } - if (connect_fd_to_addr(client_fd, &addr, len, false)) + if (connect_fd_to_addr(client_fd, &addr, len, opts->must_fail)) return -1; return 0; diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 4f26bfc2dbf5..fcda6b2333ad 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -66,7 +66,8 @@ int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t len const struct network_helper_opts *opts); int connect_to_fd(int server_fd, int timeout_ms); int connect_to_fd_opts(int server_fd, int type, const struct network_helper_opts *opts); -int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms); +int connect_fd_to_fd(int client_fd, int server_fd, + const struct network_helper_opts *opts); int fastopen_connect(int server_fd, const char *data, unsigned int data_len, int timeout_ms); int make_sockaddr(int family, const char *addr_str, __u16 port, diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_nf.c b/tools/testing/selftests/bpf/prog_tests/bpf_nf.c index a4a1f93878d4..cd4dce5bd7f1 100644 --- a/tools/testing/selftests/bpf/prog_tests/bpf_nf.c +++ b/tools/testing/selftests/bpf/prog_tests/bpf_nf.c @@ -33,13 +33,16 @@ enum { static int connect_to_server(int srv_fd) { + struct network_helper_opts opts = { + .timeout_ms = TIMEOUT_MS, + }; int fd = -1; fd = socket(AF_INET, SOCK_STREAM, 0); if (!ASSERT_GE(fd, 0, "socket")) goto out; - if (!ASSERT_EQ(connect_fd_to_fd(fd, srv_fd, TIMEOUT_MS), 0, "connect_fd_to_fd")) { + if (!ASSERT_EQ(connect_fd_to_fd(fd, srv_fd, &opts), 0, "connect_fd_to_fd")) { close(fd); fd = -1; } diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c index b9dc4ec655b5..bcf142779cc6 100644 --- a/tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c @@ -8,6 +8,9 @@ static void run_lookup_test(__u16 *g_serv_port, int out_sk) { + struct network_helper_opts opts = { + .timeout_ms = 1000, + }; int serv_sk = -1, in_sk = -1, serv_in_sk = -1, err; struct sockaddr_in6 addr = {}; socklen_t addr_len = sizeof(addr); @@ -24,7 +27,7 @@ static void run_lookup_test(__u16 *g_serv_port, int out_sk) *g_serv_port = addr.sin6_port; /* Client outside of test cgroup should fail to connect by timeout. */ - err = connect_fd_to_fd(out_sk, serv_sk, 1000); + err = connect_fd_to_fd(out_sk, serv_sk, &opts); if (CHECK(!err || errno != EINPROGRESS, "connect_fd_to_fd", "unexpected result err %d errno %d\n", err, errno)) goto cleanup; diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c b/tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c index a1542faf7873..f35a9897db24 100644 --- a/tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c +++ b/tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c @@ -86,7 +86,7 @@ static int talk_to_cgroup(int *client_fd, int *listen_fd, int *service_fd, skel->bss->g_sock_port = ntohs(port); /* Connect client to server */ - err = connect_fd_to_fd(*client_fd, *listen_fd, 0); + err = connect_fd_to_fd(*client_fd, *listen_fd, NULL); if (!ASSERT_OK(err, "connect_fd_to_fd")) return -1; *service_fd = accept(*listen_fd, NULL, NULL); @@ -136,7 +136,7 @@ static int talk_to_outside(int *client_fd, int *listen_fd, int *service_fd, skel->bss->g_sock_port = ntohs(port); /* Connect client to server */ - err = connect_fd_to_fd(*client_fd, *listen_fd, 0); + err = connect_fd_to_fd(*client_fd, *listen_fd, NULL); if (!ASSERT_OK(err, "connect_fd_to_fd")) return -1; *service_fd = accept(*listen_fd, NULL, NULL); From patchwork Mon Jul 8 02:29:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13726139 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2FC77442F; Mon, 8 Jul 2024 02:30:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720405845; cv=none; b=LUKGZ3Xt+cCNkbLMAyiO6XSW/lRmnDRCs0z8FADsprKzpcw5e9gdfnRwV6+zPwqP35o2Hx1FXXkH2xpzZX1bT2iy/Pu9RZJTLUGw/MxU8sP7eO4JapAehCfcDNxL8eKJtcSRY9MvrO4O2yWaQrQkUjr3HTQVXzuwHMEMJJYO33Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720405845; c=relaxed/simple; bh=xkEJWzojc89EojQm4GQ2qYFqMqQKzaIHgglr7UG2tq0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kkJQ0VlF7OOESqxAp9oR5tkHUFE5+jI/usIFPlCOc+gHPpavdNV/v1WgI0lORDhS28+L7CbHIOwaWpwrr5vKIyTb6yLceMMetJ/seYFyQjw3hgyrhFsO4MORM4AbHQD28Y1ZEu7vSK2x0LtHEHPxQgfHTjE+4E+jqh0DHnqPwuE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=a6+5UoTY; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="a6+5UoTY" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2303FC4AF07; Mon, 8 Jul 2024 02:30:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720405845; bh=xkEJWzojc89EojQm4GQ2qYFqMqQKzaIHgglr7UG2tq0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=a6+5UoTY4TFQVSrlOH0zleLuVCmkRI11Mw+oyoxSzT6wkZEWfvG4OX6bYV0okPOMF /Vk/VaanbLfzdgBTGi2bZPaRXTKLwjteyF89UN/RcebRd5suDbYjeEC7DwzoijRcYA V17ar9DTb2tvHOCkPG7BMb8XRKN1i2B9cEl5FAoPQal2+79PBTeiiDUX0GrKWA3hZH guPhwK0yuzljiADvexDzuHWuKw97co3iG020HqTU78dL5z52ZNV8zVP2OQMlMwi5yO hDF00YXvqaigZYlWHiManqgrILE5PGXAKWgIFscN4fJ2NbfzJYcjbcppLaX4Mdr6OD s9du//vaQsAZw== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v10 06/12] selftests/bpf: Add expect_errno for network_helper_opts Date: Mon, 8 Jul 2024 10:29:44 +0800 Message-ID: <35ab4f6f094e3c700aa9ec20ee6d6d1a91284b5a.1720405046.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang The errno EPERM is skipped in connect_fd_to_addr() by cgroup_v1v2 tests. More generally, it makes sense to add a struct member "expect_errno" for network_helper_opts to identify the expect errno to be skipped. Correspondingly, connect_fd_to_addr() helper needs to add a new parameter "expect_errno" too to accept "opts->expect_errno" passed from the caller connect_to_addr() or connect_fd_to_fd(). With this change, only need to set "expect_errno" as EPERM in run_test() in prog_tests/cgroup_v1v2.c. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 11 +++++++---- tools/testing/selftests/bpf/network_helpers.h | 1 + tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c | 1 + 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 475a5a04e61e..062170d6be1c 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -279,7 +279,8 @@ int client_socket(int family, int type, static int connect_fd_to_addr(int fd, const struct sockaddr_storage *addr, - socklen_t addrlen, const bool must_fail) + socklen_t addrlen, const bool must_fail, + const int expect_errno) { int ret; @@ -290,7 +291,7 @@ static int connect_fd_to_addr(int fd, log_err("Unexpected success to connect to server"); return -1; } - if (errno != EPERM) { + if (errno != expect_errno) { log_err("Unexpected error from connect to server"); return -1; } @@ -318,7 +319,8 @@ int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t add return -1; } - if (connect_fd_to_addr(fd, addr, addrlen, opts->must_fail)) + if (connect_fd_to_addr(fd, addr, addrlen, opts->must_fail, + opts->expect_errno)) goto error_close; return fd; @@ -386,7 +388,8 @@ int connect_fd_to_fd(int client_fd, int server_fd, return -1; } - if (connect_fd_to_addr(client_fd, &addr, len, opts->must_fail)) + if (connect_fd_to_addr(client_fd, &addr, len, opts->must_fail, + opts->expect_errno)) return -1; return 0; diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index fcda6b2333ad..14d161d35248 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -24,6 +24,7 @@ typedef __u16 __sum16; struct network_helper_opts { int timeout_ms; bool must_fail; + int expect_errno; int proto; /* The backlog argument for listen(), defines the maximum length to which * the queue of pending connections for sockfd may grow. diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c b/tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c index 9709c8db7275..ff477163f0ea 100644 --- a/tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c +++ b/tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c @@ -11,6 +11,7 @@ static int run_test(int cgroup_fd, int server_fd, bool classid) { struct network_helper_opts opts = { .must_fail = true, + .expect_errno = EPERM, }; struct connect4_dropper *skel; int fd, err = 0; From patchwork Mon Jul 8 02:29:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13726140 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CFF676AAD; Mon, 8 Jul 2024 02:30:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720405851; cv=none; b=SEmtcfN7+FrJ1ewGsAgSgXzlIlgxfzTqfbgGmcU1BkA568WcOzypjEPlAiOKvUbzci/l63qrTJPni8eRiwZ2hMiwwtw2ksjAEKKBhrcn1OEr/iSywQnS4TByT1WkV3LIEfzSQimqxdVp9IwFSyjCQfyK8AgenC0UwyD990sUnWM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720405851; c=relaxed/simple; bh=lWhh21S3N9IZn9P4yBZQ2+ZX9zmh3TrRV2vGlW4M73g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=U44jNU3oFx3fyusyc00MnhOs3jwPuXVGQPu60IEmOYNOQuyWHFOgPmDXVd+xwYBzpi8LZvTzxlJEMMDvYermdJiyakWfQBCxbRyKAFss3qRJLa4SRjyi3q2LbU8fn+CDW9192vSBqF3x8eehkiMKeTNcN278Bn6XN2D70ztRsx8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lbobls9C; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="lbobls9C" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A8DC4C4AF0C; Mon, 8 Jul 2024 02:30:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720405851; bh=lWhh21S3N9IZn9P4yBZQ2+ZX9zmh3TrRV2vGlW4M73g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lbobls9CLf0uz2iw5QK/JT/gnGKBFgse7q2dLofHQlVxLa4NbHFqJynhFvu53NtB/ +7SFNwfrxD/QaAic96e9oA6TB8Yylx/y924mnyyhdLn9MR6p+v8btGcFV9NPEz7PRi q+TqlJDSal9yrxZfnSWgaHw8chcrPLwF//Ol3CuExh+caLzJC69eOmpY2bAxo7BiyI BQRPcmOYoZB8pGO1JR63W0aKnvnpAxE5rgjAcgzK6JrQIpoG61wD+jWsaWMP0zWYbU gDE0luEnU2HydBkStU0HVlWXQmuGvcpufGrULbtD+8fDFuETfZ3BKsW5zlLYgevIeX LoKC3QG02FvQg== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v10 07/12] selftests/bpf: Set expect_errno for cgroup_skb_sk_lookup Date: Mon, 8 Jul 2024 10:29:45 +0800 Message-ID: <49c13acb05e74e13b1a20c8bbb89d26376ffbf11.1720405046.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang EINPROGRESS is skipped in run_lookup_test() in cgroup_skb_sk_lookup tests, but it is still showed in the log: ./test_progs -t cgroup_skb_sk_lookup -v run_cgroup_bpf_test:PASS:skel_open_load 0 nsec run_cgroup_bpf_test:PASS:cgroup_join 0 nsec run_cgroup_bpf_test:PASS:cgroup_attach 0 nsec run_lookup_test:PASS:start_server 0 nsec run_lookup_test:PASS:getsockname 0 nsec (network_helpers.c:300: errno: Operation now in progress) Failed to \ connect to server run_lookup_test:PASS:connect_fd_to_fd 0 nsec run_lookup_test:PASS:connect_to_fd 0 nsec run_lookup_test:PASS:accept 0 nsec #51 cgroup_skb_sk_lookup:OK To fix this, set EINPROGRESS as "expect_errno" of network_helper_opts and pass it to connect_fd_to_fd(). Skip this expect_errno when must_fail is false too in connect_fd_to_addr(). connect_fd_to_fd() returns "0" when connect() fails but "expect_errno" matched. So "err" is "0" after invoking connect_fd_to_fd() in run_lookup_test(). "err = -errno" is needed to get the real error number before checking value of "err". Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 2 +- tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 062170d6be1c..1403487c5921 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -296,7 +296,7 @@ static int connect_fd_to_addr(int fd, return -1; } } else { - if (ret) { + if (ret && errno != expect_errno) { log_err("Failed to connect to server"); return -1; } diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c index bcf142779cc6..cce6ca45b2b0 100644 --- a/tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c @@ -10,6 +10,7 @@ static void run_lookup_test(__u16 *g_serv_port, int out_sk) { struct network_helper_opts opts = { .timeout_ms = 1000, + .expect_errno = EINPROGRESS, }; int serv_sk = -1, in_sk = -1, serv_in_sk = -1, err; struct sockaddr_in6 addr = {}; @@ -28,6 +29,8 @@ static void run_lookup_test(__u16 *g_serv_port, int out_sk) /* Client outside of test cgroup should fail to connect by timeout. */ err = connect_fd_to_fd(out_sk, serv_sk, &opts); + if (!err) + err = -errno; if (CHECK(!err || errno != EINPROGRESS, "connect_fd_to_fd", "unexpected result err %d errno %d\n", err, errno)) goto cleanup; From patchwork Mon Jul 8 02:29:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13726141 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B0B18816; Mon, 8 Jul 2024 02:30:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720405857; cv=none; b=Y1McxEB+iV/CnM7bCphX1QlE07eFsy2aKM7SXlHGE2tTRblbA86JI1OXxE3XcrWL8LsrL3e8jYUu5atxKfePf3xIvd707zZVHpv6PdclwlU65iCDm3kTdSQ6n+naj/BqSRbNvc0lkxy5JgsTtPVSjL9L7Bgdtmtk8IzZcMwJxW8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720405857; c=relaxed/simple; bh=fM+Fvp9zNJCCiGYWxrP8nEmg3zhc+E1Yb8AOlDJckXc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=prkV2wL/46J2vmBvbPKy6fY8YI/BqYjo8JfvdJE/dfzPt+ekQbJA4Z2tXf/sYR35WJkXT9hdCTeC3nQpdPyUxhAOWPgdJDse/ml5WZMnSB/2+9NKrLGzC8IpE2BeJNyMPb60DDf1EJ10o/7I4PIdcyQ0Umx9Rxlpz9mnj2iJQ3I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=VJR/pntR; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="VJR/pntR" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0B769C4AF0C; Mon, 8 Jul 2024 02:30:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720405857; bh=fM+Fvp9zNJCCiGYWxrP8nEmg3zhc+E1Yb8AOlDJckXc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VJR/pntRAE/jgxtlGUK7LaRkTtaiJrgbdc6l3cpoZmSMcSMaIClbePVBxajYpYMAV GRt+H6ebCIkV4bwQYIEzIFJKRBxOc5VIv5L6lCvJAsevDR0SaKgCrG+thZZPiLCnkD rOdPrHL87Ob1EZ53Ft9GLnJEeoIf7xFla+29VL7jZpdZFbDpWnrcBDX4ECjLVn89j6 KiGx26SmtlKpi6E6ni5GqtOV1XPdua2XFX8G7TA6uCdmvvzUSikJ3Q/7ojqtul2is7 inxUxh5Bkov2IRpX8rMvdLSDIv+FN9XXOq+FvwzEF17lTTSuY3lIUnSYM/P3gkP4WQ N0MGtRCxrxOvw== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v10 08/12] selftests/bpf: Close fd in error path in drop_on_reuseport Date: Mon, 8 Jul 2024 10:29:46 +0800 Message-ID: <285c913620e67c2e8160ec056b5332c8c19d438b.1720405046.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang In the error path when update_lookup_map() fails in drop_on_reuseport in prog_tests/sk_lookup.c, "server1", the fd of server 1, should be closed. This patch fixes this by using "goto close_srv1" lable instead of "detach" to close "server1" in this case. Acked-by: Eduard Zingerman Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/prog_tests/sk_lookup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index 597d0467a926..de2466547efe 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -994,7 +994,7 @@ static void drop_on_reuseport(const struct test *t) err = update_lookup_map(t->sock_map, SERVER_A, server1); if (err) - goto detach; + goto close_srv1; /* second server on destination address we should never reach */ server2 = make_server(t->sotype, t->connect_to.ip, t->connect_to.port, From patchwork Mon Jul 8 02:29:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13726142 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 92B99184F; Mon, 8 Jul 2024 02:31:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720405863; cv=none; b=t5aIgU0GUMLj6X+VDkQW5YdVY+dclaE0On0jL2PWGnW4I91OZeYUbs5OwuljMG7dPI6mTwMTaZ8hVebAEjMyWtGsugiDa6DwYEhWb8AR7rwDVfimYbVGYPl1IkCw8iLC444AIyDsD2fPuDXLc1A1zAn7LSn7XnmJmndSPOnd/9c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720405863; c=relaxed/simple; bh=3p/deRrkgPGTdGxSapLPunN1pNLrCJ3NVhjsTWR8QBc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aE76J2QquT2zWIKC/hWm6QlnwCv5DycB8eEtuhnXzafEoovBLyv4soRoBKexvhgP9l8+UeNZeNYsxkDrEcUGNMq2gUL2zmYWdjBlRh+dBf6m516umnUKhJaPJgluzwPxzsdJJ84wTFdRP/UQoNIiv+kf9lUi7T1NAAxMFHuEyik= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=mRrRk2DF; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="mRrRk2DF" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 142BBC4AF07; Mon, 8 Jul 2024 02:30:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720405863; bh=3p/deRrkgPGTdGxSapLPunN1pNLrCJ3NVhjsTWR8QBc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mRrRk2DFqrS9LpdZmRzrY4js6IY9jsXvn4Ne1fQS6ZhtXW4Kl+C4d3Rk2J+2t4d6v 3eJHuogKj6CKITRmy9gwYqv2BP6CQ9sMhutenBDdR/sMqWZzaINzy7q0Lk+qby+uuC h6JwFERIL5Wt/2Y3jwe+K/4Ue7aqSlLzzlKi7rYb7nVKCaq9++GUg3cKT7Cw4nZAMI UUWpTyR7uJGF+j7o4j3oyegQ/PF8GzIKdIQ9IQ05Sw3tIBBrDjl7C6YbLG5yXAHtG7 p7+qnAfXgYXpk3B35tHsFxI+xp4GEKtGYCw7sTWzmTU0hPEo41TQYCEsWEtDnUHPcj zPLV0MnTJe2ig== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v10 09/12] selftests/bpf: Use start_server_str in sk_lookup Date: Mon, 8 Jul 2024 10:29:47 +0800 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang This patch uses public helper start_server_addr() instead of local defined function make_server() in prog_tests/sk_lookup.c to avoid duplicate code. Add a helper setsockopts() to set SOL_CUSTOM sockopt looply, set it to setsockopt pointer of struct network_helper_opts, and pass it to start_server_addr(). Acked-by: Eduard Zingerman Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sk_lookup.c | 58 +++++++++++-------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index de2466547efe..d03ea3e64a2e 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -77,6 +77,12 @@ struct test { bool reuseport_has_conns; /* Add a connected socket to reuseport group */ }; +struct cb_opts { + int family; + int sotype; + bool reuseport; +}; + static __u32 duration; /* for CHECK macro */ static bool is_ipv6(const char *ip) @@ -142,19 +148,14 @@ static int make_socket(int sotype, const char *ip, int port, return fd; } -static int make_server(int sotype, const char *ip, int port, - struct bpf_program *reuseport_prog) +static int setsockopts(int fd, void *opts) { - struct sockaddr_storage addr = {0}; + struct cb_opts *co = (struct cb_opts *)opts; const int one = 1; - int err, fd = -1; - - fd = make_socket(sotype, ip, port, &addr); - if (fd < 0) - return -1; + int err = 0; /* Enabled for UDPv6 sockets for IPv4-mapped IPv6 to work. */ - if (sotype == SOCK_DGRAM) { + if (co->sotype == SOCK_DGRAM) { err = setsockopt(fd, SOL_IP, IP_RECVORIGDSTADDR, &one, sizeof(one)); if (CHECK(err, "setsockopt(IP_RECVORIGDSTADDR)", "failed\n")) { @@ -163,7 +164,7 @@ static int make_server(int sotype, const char *ip, int port, } } - if (sotype == SOCK_DGRAM && addr.ss_family == AF_INET6) { + if (co->sotype == SOCK_DGRAM && co->family == AF_INET6) { err = setsockopt(fd, SOL_IPV6, IPV6_RECVORIGDSTADDR, &one, sizeof(one)); if (CHECK(err, "setsockopt(IPV6_RECVORIGDSTADDR)", "failed\n")) { @@ -172,7 +173,7 @@ static int make_server(int sotype, const char *ip, int port, } } - if (sotype == SOCK_STREAM) { + if (co->sotype == SOCK_STREAM) { err = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); if (CHECK(err, "setsockopt(SO_REUSEADDR)", "failed\n")) { @@ -181,7 +182,7 @@ static int make_server(int sotype, const char *ip, int port, } } - if (reuseport_prog) { + if (co->reuseport) { err = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)); if (CHECK(err, "setsockopt(SO_REUSEPORT)", "failed\n")) { @@ -190,19 +191,28 @@ static int make_server(int sotype, const char *ip, int port, } } - err = bind(fd, (void *)&addr, inetaddr_len(&addr)); - if (CHECK(err, "bind", "failed\n")) { - log_err("failed to bind listen socket"); - goto fail; - } +fail: + return err; +} - if (sotype == SOCK_STREAM) { - err = listen(fd, SOMAXCONN); - if (CHECK(err, "make_server", "listen")) { - log_err("failed to listen on port %d", port); - goto fail; - } - } +static int make_server(int sotype, const char *ip, int port, + struct bpf_program *reuseport_prog) +{ + struct cb_opts cb_opts = { + .family = is_ipv6(ip) ? AF_INET6 : AF_INET, + .sotype = sotype, + .reuseport = reuseport_prog, + }; + struct network_helper_opts opts = { + .backlog = SOMAXCONN, + .post_socket_cb = setsockopts, + .cb_opts = &cb_opts, + }; + int err, fd; + + fd = start_server_str(cb_opts.family, sotype, ip, port, &opts); + if (!ASSERT_GE(fd, 0, "start_server_str")) + return -1; /* Late attach reuseport prog so we can have one init path */ if (reuseport_prog) { From patchwork Mon Jul 8 02:29:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13726143 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EAC9A803; Mon, 8 Jul 2024 02:31:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720405871; cv=none; b=TsRgUQKaLcaZofGlpizylTb8h1g1hJlcuvno2ODfLtiIvLCXxfqmUNLLkebes12C/EjxIQbW0oXNB2fpnmV2oFTAHbaM23JLKwHLF6fRJLT/M44B0V1A2WbDF4OXhFvT3DYfJIhCgKaXQzPo9FbdDeXalayn7/pizArF+dd6+DY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720405871; c=relaxed/simple; bh=5NfKVOskZyhSWPjqaC3bovwMwDTjbj5c/qMAAgRj5xs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YrixLij7P/3MRKGYkXosMqNcZTPUea3uTHLM2K05pV89hoALjDSPV0OPHH32F1n1A8KTNtOeRjLN6C8wDNcw+AdOUgcwCcTSCfpMJdTonuXkR0CnVRhdKFV9zqEMwJ5T8oQBCPfAAYbyc6N+ut/HJsIpo7APvCAtj6yZHaB7kms= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=KPHYMoiV; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="KPHYMoiV" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E06BCC4AF11; Mon, 8 Jul 2024 02:31:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720405869; bh=5NfKVOskZyhSWPjqaC3bovwMwDTjbj5c/qMAAgRj5xs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KPHYMoiVa/A+SIAB/T/YQ+aYNS4XC34VT13wbwjnYilDfrpLmaL6AAinGrYjVTNRe Sn+J9NtzqFw/es8/gIIFiTfiuGkLnmOr0DwOhvgCOakUZAlwB1y7UAz8QcuYPtIo/N yR7avA8+m1T069m3NIGJpAU/rmI1aUnwocML9xn5iwhufeUs0+TFZBlGWaO82dNdG6 6UgrEjWkID1Tv9Gu/cECj9BnMkX2fVnjlnzKn5vk6LK5dEdCtG+9Ev7Yw9VItHfNra 2YBIqGW3mEsd0fY7oy2cwIziiBUo3yoT1N0VPXwuQg2VyfTQcW2dijx7YP5oOJAZJW MGmj7yL6a2l6g== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v10 10/12] selftests/bpf: Use connect_fd_to_fd in sk_lookup Date: Mon, 8 Jul 2024 10:29:48 +0800 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang This patch uses public helper connect_fd_to_fd() exported in network_helpers.h instead of using getsockname() + connect() in run_lookup_prog() in prog_tests/sk_lookup.c. This can simplify the code. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/prog_tests/sk_lookup.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index d03ea3e64a2e..184b838a9872 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -639,9 +639,6 @@ static void run_lookup_prog(const struct test *t) * BPF socket lookup. */ if (t->reuseport_has_conns) { - struct sockaddr_storage addr = {}; - socklen_t len = sizeof(addr); - /* Add an extra socket to reuseport group */ reuse_conn_fd = make_server(t->sotype, t->listen_at.ip, t->listen_at.port, @@ -649,12 +646,8 @@ static void run_lookup_prog(const struct test *t) if (reuse_conn_fd < 0) goto close; - /* Connect the extra socket to itself */ - err = getsockname(reuse_conn_fd, (void *)&addr, &len); - if (CHECK(err, "getsockname", "errno %d\n", errno)) - goto close; - err = connect(reuse_conn_fd, (void *)&addr, len); - if (CHECK(err, "connect", "errno %d\n", errno)) + err = connect_fd_to_fd(reuse_conn_fd, reuse_conn_fd, NULL); + if (!ASSERT_OK(err, "connect_fd_to_fd")) goto close; } From patchwork Mon Jul 8 02:29:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13726144 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7889F3FC2; Mon, 8 Jul 2024 02:31:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720405875; cv=none; b=KbyVf9koTIQbd6Aly7xUu5PjqhycEGkVLPk4i9NT+Bwi9fi0cICta+UVvqAfzMXli5AqGUs4JiApDKZ8sa5v0rp/4/14l4Cz/QEf4OjyPHQuNWvks2xMiv5VjQZINdfrBqjSmrYeU/3GqlXGqCLD0bJyz/BavSWf3+EpWGmLqPE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720405875; c=relaxed/simple; bh=/V6c1yuMC1EczJD4hdOVaOvhn62H4BXQjA2cI5wRGPc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=i8TABOIry0nt5xWQb2gHTeShB7L+fP/LOwDVT/2+2pMmdMnNppSevm4aMij8UmCc9zkSnS5cINZjhmsnAzQKj8E7rULO/cju2VhOtCb8OSztDFTH7bmr0FSI+5iMWWHbc89yRymWuFwCjZFWX8YlCXmwbmtGYXDgPTwvYjqExgI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=oQzpq7kX; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="oQzpq7kX" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2B47CC3277B; Mon, 8 Jul 2024 02:31:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720405875; bh=/V6c1yuMC1EczJD4hdOVaOvhn62H4BXQjA2cI5wRGPc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oQzpq7kX9miy8h3Q2wgVfv2Af4AMTg9gtfivjU56SJy1auvc68fLrlJc0u+OnLLih EY8Fg58+9tggzMiAi1vm/y8aLbIaUjrzSkT8OfB4Lrgtr9WhhSchZ7mmOjIQEkojPn bIuCLUOvFu+wrGYRcd9bhhAarolL0PfxRg1vzqBvN5J54pQFb6Q4GO0fClItRqk9Va A9qgUNf5770tErlBjtRP8D/Bt92N3Pt4hzNJpJAe/k3IvXNVh1lSg2neAyRUM3TpEi bvQj7nVmbIIA/C1j8F5AhIwQdp7c2rLMy+tewpb+z+rJL0uez70Do/QBa0BAK/ZI0T F7uSW7//wgM1Q== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v10 11/12] selftests/bpf: Use connect_to_addr in sk_lookup Date: Mon, 8 Jul 2024 10:29:49 +0800 Message-ID: <396cb73d1db1ab0eb7adba73fdedb08600e51fda.1720405046.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang Add a new parameter expect_errno for make_client(), and use public network helpers make_sockaddr() + connect_to_addr() in it instead of using local defined function make_socket() + connect(). This local function can be dropped latter. A new parameter "expect_errno" is added for make_client() too to allow different "expect_errno" is passed to make_client() and set as "opts->expect_errno", then passed this opts to connect_to_addr(). connect_to_addr() returns normal "fd" instead of "-1" when connect() fails but "expect_errno" matched. So "err = -errno" are needed to get the real error numbers before checking values of "err" in both drop_on_lookup() and drop_on_reuseport(). Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sk_lookup.c | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index 184b838a9872..5e013e0f8136 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -229,25 +229,27 @@ static int make_server(int sotype, const char *ip, int port, return -1; } -static int make_client(int sotype, const char *ip, int port) +static int make_client(int sotype, const char *ip, int port, + const int expect_errno) { - struct sockaddr_storage addr = {0}; + int family = is_ipv6(ip) ? AF_INET6 : AF_INET; + struct network_helper_opts opts = { + .timeout_ms = IO_TIMEOUT_SEC, + .expect_errno = expect_errno, + }; + struct sockaddr_storage addr; + socklen_t len; int err, fd; - fd = make_socket(sotype, ip, port, &addr); - if (fd < 0) + err = make_sockaddr(family, ip, port, &addr, &len); + if (!ASSERT_OK(err, "make_sockaddr")) return -1; - err = connect(fd, (void *)&addr, inetaddr_len(&addr)); - if (CHECK(err, "make_client", "connect")) { - log_err("failed to connect client socket"); - goto fail; - } + fd = connect_to_addr(sotype, &addr, len, &opts); + if (!ASSERT_GE(fd, 0, "connect_to_addr")) + return -1; return fd; -fail: - close(fd); - return -1; } static __u64 socket_cookie(int fd) @@ -651,7 +653,7 @@ static void run_lookup_prog(const struct test *t) goto close; } - client_fd = make_client(t->sotype, t->connect_to.ip, t->connect_to.port); + client_fd = make_client(t->sotype, t->connect_to.ip, t->connect_to.port, 0); if (client_fd < 0) goto close; @@ -867,7 +869,6 @@ static void test_redirect_lookup(struct test_sk_lookup *skel) static void drop_on_lookup(const struct test *t) { - struct sockaddr_storage dst = {}; int client_fd, server_fd, err; struct bpf_link *lookup_link; ssize_t n; @@ -881,12 +882,12 @@ static void drop_on_lookup(const struct test *t) if (server_fd < 0) goto detach; - client_fd = make_socket(t->sotype, t->connect_to.ip, - t->connect_to.port, &dst); + client_fd = make_client(t->sotype, t->connect_to.ip, + t->connect_to.port, ECONNREFUSED); if (client_fd < 0) goto close_srv; - err = connect(client_fd, (void *)&dst, inetaddr_len(&dst)); + err = -errno; if (t->sotype == SOCK_DGRAM) { err = send_byte(client_fd); if (err) @@ -981,7 +982,6 @@ static void test_drop_on_lookup(struct test_sk_lookup *skel) static void drop_on_reuseport(const struct test *t) { - struct sockaddr_storage dst = { 0 }; int client, server1, server2, err; struct bpf_link *lookup_link; ssize_t n; @@ -1005,12 +1005,12 @@ static void drop_on_reuseport(const struct test *t) if (server2 < 0) goto close_srv1; - client = make_socket(t->sotype, t->connect_to.ip, - t->connect_to.port, &dst); + client = make_client(t->sotype, t->connect_to.ip, + t->connect_to.port, ECONNREFUSED); if (client < 0) goto close_srv2; - err = connect(client, (void *)&dst, inetaddr_len(&dst)); + err = -errno; if (t->sotype == SOCK_DGRAM) { err = send_byte(client); if (err) @@ -1157,7 +1157,7 @@ static void run_sk_assign_connected(struct test_sk_lookup *skel, if (server_fd < 0) return; - connected_fd = make_client(sotype, EXT_IP4, EXT_PORT); + connected_fd = make_client(sotype, EXT_IP4, EXT_PORT, 0); if (connected_fd < 0) goto out_close_server; @@ -1171,7 +1171,7 @@ static void run_sk_assign_connected(struct test_sk_lookup *skel, goto out_close_connected; /* Try to redirect TCP SYN / UDP packet to a connected socket */ - client_fd = make_client(sotype, EXT_IP4, EXT_PORT); + client_fd = make_client(sotype, EXT_IP4, EXT_PORT, 0); if (client_fd < 0) goto out_unlink_prog; if (sotype == SOCK_DGRAM) { From patchwork Mon Jul 8 02:29:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13726145 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B192F803; Mon, 8 Jul 2024 02:31:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720405880; cv=none; b=GoRzt/D3plxcNa75lWP3/sn1r6CNk1Q1ssCfP0iAPzS4XLBYhjlHTpRRMEhe6KbdpdLvYvgOefjahHwFdNNmofpjd6XJdfA5c8xBQexLInQqA6HMLl3gVXyNzR7oT/eUEvL7yytfL4j7kHgVW9LBWd9kKtwRbC1uvwF6JvtHYrA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720405880; c=relaxed/simple; bh=8fDEfzWnbO0l/KBAAoAS40/0DvLfb8+vaBAwKdnfSww=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=seT4L/cInjocvbYCwSq1HsND1xdGp4G6c/56zpzl3lW8Mja9+fD75b3qbidH8Y+x1Q89aIRp4v9tV1y1RszJBN2d5h9X4TWq0txgp3sbQ7XT6h2CpRFgm7R6V4Jb+zhmzaKhriFMah5zv8elRslyv+NW5RZhiTF7pu/yvunyjHQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=W5r1sgS8; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="W5r1sgS8" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A85CAC3277B; Mon, 8 Jul 2024 02:31:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720405880; bh=8fDEfzWnbO0l/KBAAoAS40/0DvLfb8+vaBAwKdnfSww=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=W5r1sgS8SjbYYz6UB4jEjvga/ygylmhDmQECDO/+Fk8n3D7A8gOaqCEyMIpP6fDHH fS26F8yKQzrpkuCogjOYOt4MsfyEf17Oq7ZqKxe+78f4K82+MsdE15J1TX7GNJYu6d 0FqQ3RP7E/DFae16UVgfX2jFcThjVHLUYL5eR9Q4E4lNbRUw0KT9nrEZpPJ3zZLejZ QtQc+6Za8OzEGEMkra2M0LaZfFSToLxMvVtwz7GqHdknXxSZzsmD/1k+9KsYmUoewu TRL1KVj4KxBHFA5u8GheGUQEU9c8V4lJkwMX1LgC4g4zdqm/ZMusgF5jARfahRG4JC zMNMUN/SNJAIg== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v10 12/12] selftests/bpf: Drop make_socket in sk_lookup Date: Mon, 8 Jul 2024 10:29:50 +0800 Message-ID: <411ddbcb2bf875b096466f0f28472f3d5bd5dd94.1720405046.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang Use local helper make_client() in run_multi_prog_lookup() instead of using make_socket() + connect(). Then make_socket() and inetaddr_len() can be dropped now. make_client() returns normal "fd" instead of "-1" when connect() fails but "expect_errno" matched in it. So "err = -errno" is needed to get the real error number before checking value of "err". Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sk_lookup.c | 45 +------------------ 1 file changed, 2 insertions(+), 43 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index 5e013e0f8136..95307ccbfc42 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -108,46 +108,6 @@ static int attach_reuseport(int sock_fd, struct bpf_program *reuseport_prog) return 0; } -static socklen_t inetaddr_len(const struct sockaddr_storage *addr) -{ - return (addr->ss_family == AF_INET ? sizeof(struct sockaddr_in) : - addr->ss_family == AF_INET6 ? sizeof(struct sockaddr_in6) : 0); -} - -static int make_socket(int sotype, const char *ip, int port, - struct sockaddr_storage *addr) -{ - struct timeval timeo = { .tv_sec = IO_TIMEOUT_SEC }; - int err, family, fd; - - family = is_ipv6(ip) ? AF_INET6 : AF_INET; - err = make_sockaddr(family, ip, port, addr, NULL); - if (CHECK(err, "make_address", "failed\n")) - return -1; - - fd = socket(addr->ss_family, sotype, 0); - if (CHECK(fd < 0, "socket", "failed\n")) { - log_err("failed to make socket"); - return -1; - } - - err = setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo)); - if (CHECK(err, "setsockopt(SO_SNDTIMEO)", "failed\n")) { - log_err("failed to set SNDTIMEO"); - close(fd); - return -1; - } - - err = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo)); - if (CHECK(err, "setsockopt(SO_RCVTIMEO)", "failed\n")) { - log_err("failed to set RCVTIMEO"); - close(fd); - return -1; - } - - return fd; -} - static int setsockopts(int fd, void *opts) { struct cb_opts *co = (struct cb_opts *)opts; @@ -1220,7 +1180,6 @@ struct test_multi_prog { static void run_multi_prog_lookup(const struct test_multi_prog *t) { - struct sockaddr_storage dst = {}; int map_fd, server_fd, client_fd; struct bpf_link *link1, *link2; int prog_idx, done, err; @@ -1253,11 +1212,11 @@ static void run_multi_prog_lookup(const struct test_multi_prog *t) if (err) goto out_close_server; - client_fd = make_socket(SOCK_STREAM, EXT_IP4, EXT_PORT, &dst); + client_fd = make_client(SOCK_STREAM, EXT_IP4, EXT_PORT, t->expect_errno); if (client_fd < 0) goto out_close_server; - err = connect(client_fd, (void *)&dst, inetaddr_len(&dst)); + err = -errno; if (CHECK(err && !t->expect_errno, "connect", "unexpected error %d\n", errno)) goto out_close_client;