From patchwork Thu Jul 4 08:59:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13723457 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 C8D05147C74; Thu, 4 Jul 2024 09:00: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=1720083611; cv=none; b=YqVdXYEYAgjul+gMoZ47vMM3gbqHZCCO56UU/VqjVoprqUqmmL+EnZ6xyJiy0zh7JDhFSUCIvGKssE5HWnLx3JQAXz0qt2okL5cezH1D1Bu2p4yzt3bG+ozviKatJ0F/E0zaF8atU4jtXo7sZY7+l0IDOVMuSrgtT3uRx0vmb+I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720083611; c=relaxed/simple; bh=bAX1OLxg4DkksZvjRAzn/L1GhmwINGXrUi0DgLiNpt4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=F2HbDnzkMJn3PePk76I03EkKiXLFTjR0O8Yuk3B/CxB+JVm0IdUvjahmenoATbhgzXNw+oNsuzAzxznX5uFfcHqef1W2cQoLTu49dKyqPh2GIvdu1m5DrR9nr65F5CqkeLlavkKQYIl0vdcEUNF0kxydFM9lJjPmN3z4Q/pjnPM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=RToQcFT1; 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="RToQcFT1" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 22760C3277B; Thu, 4 Jul 2024 09:00:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720083611; bh=bAX1OLxg4DkksZvjRAzn/L1GhmwINGXrUi0DgLiNpt4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RToQcFT1BfLF3rVfX+ixOZwKtwZtysgJQmP0KmvPIHFlWX/joosRmRm69MLu4lbh6 PlE+LJjec7q7CpVVYmmi/1hwWZT/8zqaJfZesMt3Zu9noBK+pYgVmxcuKT7dlXCJ0x 87B9tEkMJ8d3E/UmX1r564dEmYpbYOaI2VVGruZoFmtO3tmB8f9ysd/sOcAPKbQVHt cJFGxeUr2NN+WsZEbfnNtevtSU7dPJyt0IMO9rdzC0bq8WX7eTwXR2PkO9Sw8WkqYj fuO6Hzad/9lOT2/+rXwMn7ZXdWpg+sAlbNNR227C2L+FjnBbEOnDafjOcyVahMvCyS WLs2v7J5AA2jg== 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 v7 1/9] selftests/bpf: Add backlog for network_helper_opts Date: Thu, 4 Jul 2024 16:59:38 +0800 Message-ID: <0ec29609fa37917abf0746d45458eef45d35c3d6.1720083019.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 Thu Jul 4 08:59: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: 13723458 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 8904E335BA; Thu, 4 Jul 2024 09:00:17 +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=1720083617; cv=none; b=Ff2caMJ1pt7TZGThHhmPiA7oCfJBdxyq9ei7xuGO1DsKYI858NdnhRznP5VKcY1w5XvWPkceG7ZqfsnsUr2g0eeFl5gb5iR2C21d4iBy+FIBTqR/d6cI7h7PglMsXNmpWzdOLmsKuBmw+8DX590nA7DkxDuVfcb1c68hiByVYo0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720083617; c=relaxed/simple; bh=00pdNC+8+bJCChvMSP7B2ks/Ry/OoNnOXIemGC5RruM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Iiaeim5pvvbA5Q+i66Lb6J2n9zMVk9JPtsZPxKYXL+MZ7cFdIKNdPwPWShhXWM2WvE7Hz4zXSbjyECgKMBIEauHxKCQ9ngzXTx9ruvNnTKyOLdDCF047PJNiQIJ2GKCbQJQhxFIMOQDDdaNXfTVLKEUw5JoCOhI/mhRgrxhy14M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=u4Nm5DLt; 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="u4Nm5DLt" Received: by smtp.kernel.org (Postfix) with ESMTPSA id F13BEC3277B; Thu, 4 Jul 2024 09:00:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720083617; bh=00pdNC+8+bJCChvMSP7B2ks/Ry/OoNnOXIemGC5RruM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=u4Nm5DLttdH1YeASKuxwHAI6ZxGORrPvdWgtkkS+FTYTUgTPLsh9Ds5EauNcgk/Si 3NpUhLOzbqztqigy26J6Apen66VLG5gCejAwPO9UP2WM7lSjCXbh9584jH8Yy32Gco ZmZrix0xtnTLm8ipXPZjCzkV4vjPHr/U8mZUftfLvMA61frltn0lGSFbNK43cx0At2 yFHY1b0D8LG4cfHbE63m7QvKGMrRQa0eo/vtCbU83UTJzCg350g11Qage1i1uRhqom 0a69CK66Oakfk+hiFu3yQS1UHeKE2m5EBk3WVImX1ls1RhBwjlhfVfALMZsgJJE6H4 QwC+jiOLwf1zQ== 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 v7 2/9] selftests/bpf: Use start_server_str in sockmap_ktls Date: Thu, 4 Jul 2024 16:59:39 +0800 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 Thu Jul 4 08:59: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: 13723459 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 1F4D81A4F25; Thu, 4 Jul 2024 09:00:23 +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=1720083625; cv=none; b=FLOYgyLj16QwYA1NaXkeuxxPUeOvCokNnxC202EOTX3glMF1N7u57CLoF8WWZwiOGgIflJhq3lgS4QevbGBUCqqJ2H99Rp0kRmJ7qPgDlkWoUSJTcAAoYpyJ9oJzZTyABEJ/tqpvhGuIxIrbmwhZyNm+/cF4aD5KTboOZzyK0B4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720083625; c=relaxed/simple; bh=Qq9hn27IvadosGSaFEY1IhNuXDt86aHiR/dUXu2AMTk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=R5E9fTMqBNEhfxkUm4wmQq2w5+kn439FePALAsrwQPZJd3KeVixW0mOEYhPJzg1UibRl7iAyGOFYg/mVMbu6kONxu/7Jet+6GvcTWejPHyrWf/3PPAsYJDYDxvD6vAINWq9sAlaRKdf9/wMtV+FJqkCMZxWvyrhigRMY6hNqiX4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NzyAhkrG; 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="NzyAhkrG" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D9888C3277B; Thu, 4 Jul 2024 09:00:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720083623; bh=Qq9hn27IvadosGSaFEY1IhNuXDt86aHiR/dUXu2AMTk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NzyAhkrGQQtCpKK5yM3WQfivzUjx80P6znJ0QRTS4E2T1d31unM0dnxbQE5eR4Jn8 +S+cuOAyPH+AinUG44coQQxXKpWK9f7niDPj4vEM2i2jVqQYoKeMG1PGkH96jhPwV1 Omth95rupSqxZ+3h/jHaedf/MQh5OxUZJggR2zmfw/qQsjq8NxQ7LTeRxk7iIMtu9c ojzxSxBQafaKEt6nZE77V4Kq/5gMBUb5cFNyDHBNxDbHKKRYzc5QGmZZGQlksM2GXj jSbLoHOmBtWn/WUehgVyl2mFLCi7mTNY3zurLGjTfqUIfLPxGz834b2oHNxxJqZRl2 J85K/O+gYJDNg== 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 v7 3/9] selftests/bpf: Use connect_to_fd_opts in sockmap_ktls Date: Thu, 4 Jul 2024 16:59:40 +0800 Message-ID: <79a880e0114603d720251357d918100172959120.1720083019.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 Thu Jul 4 08:59: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: 13723460 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 18F25156679; Thu, 4 Jul 2024 09:00:30 +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=1720083631; cv=none; b=Jw9MTNeAQ6m0ImXkd0olQL0x0+OHMOzV6IAf7aX6AiDrZJfQQpLLH3cWLr+FeEWGUH6jRtSjB+IkSKLXUVYt/wEku1V6l2KCVPbORAOzocsSXmWvt/6bQOkiMsyhOg23Ga6183YNsEFQm9XA4SXI0oXOCAvQ+M4CcmGq4MPYLho= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720083631; c=relaxed/simple; bh=rRP0ZiPRY0W17MVOWxZwWV5aU8LBd+K/G2ydWK7Wq+o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hw56BOdKaxeFgv2Dp1OJnKMQzBWDu+czuiOUXJUpbwyUv16XTmCKiKOLI1z9xZVGTHwGLF+Y5MAFZZ5MiE+7q8KpMfTiuPWE9BopS7kbgcQKfZeJKrF7rKbyJdTx+OI2gHquD0Y0+fFpQVN/+touv7e/cMdUbfmaMUx1z4HWoY0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NNZdpHzd; 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="NNZdpHzd" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8C955C4AF0C; Thu, 4 Jul 2024 09:00:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720083630; bh=rRP0ZiPRY0W17MVOWxZwWV5aU8LBd+K/G2ydWK7Wq+o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NNZdpHzdXQQ9n9laDUm85qAEqNNN5FqrS1g3GaeHLOxS/ppKUI5xuoRRYI7B0ty0X RRrfwxYBWIlDjWJugSXfhn5ys1t1IKr9SivcQdEhPE2aM3R/3YgHwyqqSZS8+TXv9H IxRneINFtvrb5igKjMCRfMNi2QrrSoUCrF8yIrAQMYaqB01zhFOfMYzCTywYRwvmcK S5McakVtCLscij7SO6jHKQlD6Tj/4W7y0X5/6A7RG+VW0ud9C3tJVFmfgtK8vKHMcV 3CKn/2a6UVBXLE7ifIe5DDjH4dxOX4Rb81ok+otrFmPxZc8u4mAwuO10NferOHQfDE +V4Xssxa5P6PA== 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 v7 4/9] selftests/bpf: Use make_sockaddr in sockmap_ktls Date: Thu, 4 Jul 2024 16:59:41 +0800 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 Thu Jul 4 08:59: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: 13723461 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 03FA2156679; Thu, 4 Jul 2024 09:00:37 +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=1720083638; cv=none; b=gwb6Gap314VFrIlnNc66cZGZUyL/ZUNfxkSWCuk1QyeQd6u/Hdld5e3M/8cPQfgKWlSWu2hejjC51dEmmuhcYJQceNHkivpJCosKyT2v3jIU7XUpQSEpIeTVPa69BKOSO6EHNw0pST6LOY740jHde2dkkoNiLsTXdLghkGLIzpw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720083638; c=relaxed/simple; bh=JAWAcKR7zBXRC50szWd1XFpZsq7hHNsF3ZdFSJ3411U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CwIilaNX5qFGxyMsLn3DR1db0Ymx0ksEnqddfG9f7SVMaKnl5Ga3KxnmCs7QS9RcSFOW3A1D7Yd88XVAn43kJ6IsAV1+7ZblW0UKBrg86vsKAXXpQuQkXFpQ8D1e3c3uz8jCB6/vEsi64Pf5LUl1m1cLw9z4N5A8iSOSu7UnnXU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Wy182bbP; 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="Wy182bbP" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7D4C8C3277B; Thu, 4 Jul 2024 09:00:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720083637; bh=JAWAcKR7zBXRC50szWd1XFpZsq7hHNsF3ZdFSJ3411U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Wy182bbPQ7a51xlnDxYIrYjZLWBRqz9x8Xefvqt4IHchquH+PFypnfoBy1tElBEKC DSvWkDKp1ACt7rWacpDKHeGbrAjuk4cXl4RSaXPh/ZQW88YXM+QgNAjGEZlMfsmt+7 hJWmVFzTlRIHh8zjM+9xPyDtN1/pvID3GYw1blBIH/6eCFZR6iXiGLFyq1U8yKW8MJ zb/F2QGacMliiAWLirwwBsrZgOWczmICCZiUIfeTKX5vGHgvA+6D5smjcCeAoZ1fKc CP4oQRVfZ0rGuUejhqFzKH1AErscUMrMNCNAEl7OfV3V58gBXEsFzWMM4a7LREyWy1 ancnm9xSMO0hw== 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 v7 5/9] selftests/bpf: Close fd in error path in drop_on_reuseport Date: Thu, 4 Jul 2024 16:59:42 +0800 Message-ID: <8f05f5acdf4871454ab052ab39dac22fe6e5d1fe.1720083019.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Geliang Tang Server 1 fd should be closed in the error path when update_lookup_map() fails. This patch fixes it by goto "close_srv1" instead of "detach" lable in that 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 Thu Jul 4 08:59: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: 13723462 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 ED4C818FC81; Thu, 4 Jul 2024 09:00:44 +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=1720083645; cv=none; b=chXj4NNc/BaqRDSrmtxYI51yK4raGm9fK46dXA1IYqSCUoHkIJW6b5SkEUX0DTj+OzVHQ9i5UVEBWb22v6WCx7yQbJJblVgXDuIer5wFYUa8rPMqeIsXZYuCrqMna0xyNBeWAccVFa1jFap1fbKpVXeoJNaUPCvVXTOxkMkSVWE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720083645; c=relaxed/simple; bh=3p/deRrkgPGTdGxSapLPunN1pNLrCJ3NVhjsTWR8QBc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QUCXeP5eqIed1AKeSPaMxhKb/KU4STY8pHSaPEF5L1kx+7AAF1Q1U0EYMD0fYbKDMbW4We0s/eG9dL7Q4LWj+bpVFfniC6MyGEsjm7MerdbmnQdAgbZIWYwLemmfQEmKYys0OxbMCRVxt6nzL+Gp10mwLJt+20wBeh1MgWH5KY4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=vRLs7Gg2; 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="vRLs7Gg2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C98CFC3277B; Thu, 4 Jul 2024 09:00:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720083644; bh=3p/deRrkgPGTdGxSapLPunN1pNLrCJ3NVhjsTWR8QBc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vRLs7Gg2iuk0yKmOsYWdqQJCiF8y+HKyYm2zrjScP9lt4cxJVIXV4dQcBXnLoShJ9 m+iQgG0NYuHtzuP/Segilad522LeVo7OBVpDQith0fvHfHBH00O0kx4lRz2i+J30YA AofRYyejaIG3E/eMTdD1DnCrCWB/F6Gvx5V5eS5vzeTxHK/IHmxCt0Gj6G2VDDWezm OL3mdMuZs+QUvu8GmdqF57Lx2RMPSUE35NsghfLVcdaruPZJoI3nuf97a6CkkEzH5d Z0HGkHHP4spA0XDUD1C2C0HMUdTi3d9R0pV+2xu8+P2S9NKIOdv1X4p2jHMtOCRbDm lxhetpJ0i2vIg== 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 v7 6/9] selftests/bpf: Use start_server_str in sk_lookup Date: Thu, 4 Jul 2024 16:59:43 +0800 Message-ID: <776dc3eb2672f0b84ad64dc527a78b6b61b06187.1720083019.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 Thu Jul 4 08:59: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: 13723463 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 1DCD32BB06; Thu, 4 Jul 2024 09:00:50 +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=1720083651; cv=none; b=E1jWsdwS8uvwjHz8TsBaaHEhJgW6r6MEDlhUt2jeynWCLyb38HdJnBKfDrKyq8l2OWrWN5hlqbVxyFQs7lGyydiWj3j+fasTh1+pF2/Mz4J5GB/HBfkjRlXBE53+UjytxQkVUBRKij0/9awGlTyBivCSn/pYiKQfCAgLiSHv0hM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720083651; c=relaxed/simple; bh=yIL0fQNsMx5ijF7GWUBR64Zkm01hjeVcW0TnosV2/ds=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eqV2nAVB9jdLmsi3iNS+sfsVNAicbAMu2H8HsNkD4qvNEudJlJLT7Ogp4x8aEz5GWKNwUWdzBMY0BBTAUknn7S3Fdzab2lUjClwk2OmQRATs8sI1VmPOvnShOIlPCzHAzDdNT9xIzXKSdB/SIv4gpNfeJ3weg698E0nj9Dlbv1E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZSglCuMo; 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="ZSglCuMo" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4B6FFC3277B; Thu, 4 Jul 2024 09:00:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720083650; bh=yIL0fQNsMx5ijF7GWUBR64Zkm01hjeVcW0TnosV2/ds=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZSglCuMo5Y6gfausc3OJ21X5LLVRp7qyWPAvwAV06b/rnZzSzwkmPdgBN0Y8DbBGs f1pSO1nkuLU1y1VQyGy8yie7MslJbkmUETLYtYFzRqvCSvh2Q/Wlps/6vU/Cqzhwit sFOeYgPkYKpEx2H3yPr7R579iBWQXK8WpJMYpxKdcSWX5TJNewIsgsBVfyGtnNmU9O dK1F+V8rP5UmWzylF7BFJ8mEFTOqIrKc1v7WECGQ/SW94ghZ02WPyvSIeP2B1wKYR9 ndjdq4PpdGblsVl8nOloczKrY/AnLhJ48J+Xc/wmJL0SskTwpjJDKWi/RIvIZ2zbZN owi+LUjHu5gmw== 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 v7 7/9] selftests/bpf: Use connect_to_fd_opts in sk_lookup Date: Thu, 4 Jul 2024 16:59:44 +0800 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Geliang Tang This patch uses public helper connect_to_fd_opts() exported in network_helpers.h instead of using make_socket() and connect() in prog_tests/sk_lookup.c. This can simplify the code. Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sk_lookup.c | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 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..ef4a3db34c5f 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -874,7 +874,9 @@ static void test_redirect_lookup(struct test_sk_lookup *skel) static void drop_on_lookup(const struct test *t) { - struct sockaddr_storage dst = {}; + struct network_helper_opts opts = { + .timeout_ms = IO_TIMEOUT_SEC, + }; int client_fd, server_fd, err; struct bpf_link *lookup_link; ssize_t n; @@ -888,12 +890,11 @@ 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 = connect_to_fd_opts(server_fd, t->sotype, &opts); if (client_fd < 0) goto close_srv; - err = connect(client_fd, (void *)&dst, inetaddr_len(&dst)); + err = 0; if (t->sotype == SOCK_DGRAM) { err = send_byte(client_fd); if (err) @@ -988,7 +989,9 @@ 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 }; + struct network_helper_opts opts = { + .timeout_ms = IO_TIMEOUT_SEC, + }; int client, server1, server2, err; struct bpf_link *lookup_link; ssize_t n; @@ -1012,12 +1015,11 @@ 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 = connect_to_fd_opts(server2, t->sotype, &opts); if (client < 0) goto close_srv2; - err = connect(client, (void *)&dst, inetaddr_len(&dst)); + err = 0; if (t->sotype == SOCK_DGRAM) { err = send_byte(client); if (err) @@ -1158,13 +1160,16 @@ static void run_sk_assign_connected(struct test_sk_lookup *skel, int sotype) { int err, client_fd, connected_fd, server_fd; + struct network_helper_opts opts = { + .timeout_ms = IO_TIMEOUT_SEC, + }; struct bpf_link *lookup_link; server_fd = make_server(sotype, EXT_IP4, EXT_PORT, NULL); if (server_fd < 0) return; - connected_fd = make_client(sotype, EXT_IP4, EXT_PORT); + connected_fd = connect_to_fd_opts(server_fd, sotype, &opts); if (connected_fd < 0) goto out_close_server; @@ -1178,7 +1183,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 = connect_to_fd_opts(server_fd, sotype, &opts); if (client_fd < 0) goto out_unlink_prog; if (sotype == SOCK_DGRAM) { From patchwork Thu Jul 4 08:59: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: 13723464 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 3ABBE1AAE08; Thu, 4 Jul 2024 09:00: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=1720083657; cv=none; b=a4Zo+EjgToRKu+ML/4BXNEAnnom2tJq1qc8gCvGEOamsML7AHWZnhklatLko1TwGCQQRruZl3lPGHZCszwkamyhKC+3PXpxjLc4DJrpZVnyYpKT4ugSs7rSxOsbxPkHKRPAv85gJCdiERvXvREUpJa0BXo+ABop7XvDs8FmtXM0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720083657; c=relaxed/simple; bh=yBpwufIyHtUyQP/wn4qffLKhfsxUhBi0F8+lFPe+2YQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Z58ZYuT79Ciav5ymQf9VqXoUIP69q9zm2vJ/6fX2UcjzyQivqRtmc+sxUbmA4a4J0p2Kpa7ZN5xSl8IEed3chFgdlCOSrYd6REZoXbzv6vOC5dZSwlOuX7DxVFryh7Wz5au210tIUyhxMWcuiBRsDXJN7KSBWLOHKkyYwTDW5Zk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Ia/4JBTt; 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="Ia/4JBTt" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3854EC4AF11; Thu, 4 Jul 2024 09:00:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720083656; bh=yBpwufIyHtUyQP/wn4qffLKhfsxUhBi0F8+lFPe+2YQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ia/4JBTtcWfpkthIISV0F31R5EK2REtP2DmQOKWXvcol/yRa+1lJS9by691NDVCnO /g2NkEwQnJXI6SIbl/w18l/ld0wsBYAizG7zOwf5OvmUbu3zeKnMKi3wI/CYnfuWjW 3gcy7vjdOozCDG6D5mSnu9y8zd6RCZv2XBdM/bKuYSe58k1VnjbQr6KCHNLrBn1YVM 4OESl9zzvgkLKOXCHzsSorn0RjRX8o7332x85ie4yyjOzTwPr3FGbkh6AngQS2WcXv o9BC87DAadl1Uz8wX7xYg+Qbs8ZsBs6dXz0ZkAG8Y5srzfySupXEnB0PGnOEAzQ7wX ZbcagxnpAiWVA== 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 v7 8/9] selftests/bpf: Use connect_to_addr in sk_lookup Date: Thu, 4 Jul 2024 16:59:45 +0800 Message-ID: <04e41d6e3158b294a30ae6bf93a856b63832d65a.1720083019.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Geliang Tang Use public network helpers make_sockaddr() and connect_to_addr() instead of using the local defined function make_socket() and connect(). This make_socket() can be dropped latter. Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sk_lookup.c | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index ef4a3db34c5f..7c42ebff80a4 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -231,23 +231,23 @@ static int make_server(int sotype, const char *ip, int port, static int make_client(int sotype, const char *ip, int port) { + int family = is_ipv6(ip) ? AF_INET6 : AF_INET; + struct network_helper_opts opts = { + .timeout_ms = IO_TIMEOUT_SEC, + }; struct sockaddr_storage addr = {0}; + 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) From patchwork Thu Jul 4 08:59: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: 13723465 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 41B5B1A38DD; Thu, 4 Jul 2024 09:01:04 +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=1720083665; cv=none; b=YodQ61Wme7j3wKYBigWZvsUDFlQn3hNVDUlLd0a421QQWuUMlfudkWvWhTzU9YBvgPFV8QT0NJB1XsY4JogqQZhGIj9Lui67mfhkKk6vSNgLx/KbrUDd4XUr9zYn25RLxXHAReewJIPlznMiYFC5sQJSarEhHoC1+tMATS9xpzU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720083665; c=relaxed/simple; bh=s76YQ0SKgyu159tTZF5FTGtD2Bn4gBeXuU8Ve31f3Fs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IkTfOuICKR957/nWriSle6yQVQVrrTB1zwiH6QsIsHpsv28UKK+3kL0Ss2KwMJmzUn66tUSykz+kWOIPtmvZ7Np0/3WKUioql+dpPYsGycivrTxP1cjrHEpH5b3Hogf+tBd/AZt+mlt5Gclv2SUFW8Iyg1/70ugoSNQ6ZPKimYQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=CPvq6cc2; 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="CPvq6cc2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B3ABDC3277B; Thu, 4 Jul 2024 09:00:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720083664; bh=s76YQ0SKgyu159tTZF5FTGtD2Bn4gBeXuU8Ve31f3Fs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CPvq6cc2jxEtbwcVGdYUpJWpaHFbk9x6ZE91olKxYtMSpd4NBDBlPfTVkwbKYe/P7 KNoecNb2WhicOb+JC/YaLjDym1zDlewc9WCUw5XkYu2ZXcb2DmqePQ23P8DGeHfhIi RxenMKASIkUX87l/Nx2zNuyvKtVjCnC11/qqUZ1YqoBmk5BdmdDp1H66RrLkTabbic gdNmjybMHp+cHHRLrHxLl/3SuekdGkIEzccSX1dkAPnYi2YntHsjV0AhT6nAWNkkyB 0EjfFGnjrvX5lFyiODaFDmaBbiIyysfkzislQMFZKXF2S17qSxmXGBRUMkaDsBaGh6 99CpDyx+UhLYQ== 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 v7 9/9] selftests/bpf: Drop make_socket in sk_lookup Date: Thu, 4 Jul 2024 16:59:46 +0800 Message-ID: <3948df0b9ab7c77c1a59c6156e0e7be1bfe29e65.1720083019.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Geliang Tang Use local helper make_client() instead of using make_socket() and connect(). Then make_socket() and inetaddr_len() can be dropped now. 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 7c42ebff80a4..c359dc988de3 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; @@ -1232,7 +1192,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; @@ -1265,11 +1224,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); if (client_fd < 0) goto out_close_server; - err = connect(client_fd, (void *)&dst, inetaddr_len(&dst)); + err = 0; if (CHECK(err && !t->expect_errno, "connect", "unexpected error %d\n", errno)) goto out_close_client;