From patchwork Tue Jul 9 09:16:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13727513 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 52E981DA2F; Tue, 9 Jul 2024 09:16:48 +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=1720516608; cv=none; b=RTflzzeFunjNxeAvq4s7x7QX9rgL01chRiJxprqveNlqPxPxu5cbjeF+mhfBKPRzC47SXHgHpCIl5blyXV73O/zKB48/nh7DeMbyr11K+WOGnHgLdgxc8I2Uv7afSyk2dofgELplllXOYxZFLHca2BP4fivFdCmAA2GRXmKw+1E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720516608; c=relaxed/simple; bh=bAX1OLxg4DkksZvjRAzn/L1GhmwINGXrUi0DgLiNpt4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=laiGv2nazwLgmLNcfZuSedtHU2tJRjc9m6iQ/voPTygDhquAZuZfxTJO1/wjIc7VgNDLnCQFk6/Ln8PpoHfnmM/RxfSRkRZuAd8o/RcYUJtM4444Ht761j81CrMBHTWXnq8uZ0ziIH55c5Xh7vRMYGUdEUQLVmPC64BGZFgLVpg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FI/TbSRa; 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="FI/TbSRa" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6FBFAC4AF10; Tue, 9 Jul 2024 09:16:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720516608; bh=bAX1OLxg4DkksZvjRAzn/L1GhmwINGXrUi0DgLiNpt4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FI/TbSRatTZ/4gu+8lUuTEizJSrAHBko6K/ssAosGbA4oCtBeXZ10QcaM0RWp7iCc y8uMnKaNN0VgOoWbVmtyhbeNwhx7quqFFOILBGynzum//SUScVCwMubMRVl89YkQkq aRmm9caHVIMmCzahz+ET33jPtfkOJDfOjOqiWZJKSNnpei5CrwifTAtrSnlTdu9dcY pnOPcQbgNS/u7dbDL4mZjz0xLAc5RHQvwBqLoxBID2IkCoaEjhHTjIYTp2Sx78PQKR b0C0p5tw8d0qYE0LCuCA5/bHVVKZs8k/MeiDU9XovdH6kLk9MUQqYGi1gnt8rGeCyM ybhzWLynSq7qQ== 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 v11 1/9] selftests/bpf: Add backlog for network_helper_opts Date: Tue, 9 Jul 2024 17:16:17 +0800 Message-ID: <1660229659b66eaad07aa2126e9c9fe217eba0dd.1720515893.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 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 Tue Jul 9 09:16:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13727514 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 7735114E2E6; Tue, 9 Jul 2024 09:16:55 +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=1720516615; cv=none; b=p12ziLh6gubXP0TikZ+zkbfaIllS4Z8HsfZqSzI6mZf2EfVWpdbi9z3onZFl9mpE7bqEcF9zGlG+sQcNBMArWXB9GgsQsNIeTB63kiHd+O50C7sKNbACrwkyl6l93e4KkRm5cMThJeZZQsw0an2+CEl/gwTTHrqHTPhRkuu8u/U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720516615; c=relaxed/simple; bh=60zEMjX7htBU0OqT1VipLkt5OIQycGxibF5IvB/pNzs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KbPSuB1ILmNI0vOS6uoYRoMamQ+e0svhn/8S1AnIrNGsme/BmrQFaNsZS2tFMW1wTFMgMfERsZmu/lkHcfQL1AAQMA9RdUg9MFDCLU8Q/qf+XLT6qdqERxiguepFAJoUiY0rKjTY2Dp043HJPha671JERIiv6MJnDp1JRtVlHS0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=h5pvPy0f; 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="h5pvPy0f" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C50E1C32786; Tue, 9 Jul 2024 09:16:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720516615; bh=60zEMjX7htBU0OqT1VipLkt5OIQycGxibF5IvB/pNzs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=h5pvPy0fRlGBxmbtbl4wg7ealufSzJFD0PtH8s9dlbSBHwXgy5HD6lUFtnTxvCbqX k6hnFO7wqCgkxxzjHA4Nj0ObPE/y19MsMleAPnx6P3blGve9cTwMx5pPbQb+IyQA5p +BrRaHeNEaYU+Ah5qE7unmGUQjL+/WBcpnN1oTK+TEeO8jiYJO2ZJLIArGhTkD3wTZ o+hQuK/yJ9Uo/AFGbtaZEs1F95JQnm4wKHgcVaGOlKdL569Qthcy4c0WRNmC15eukQ 81rJVBVoWtaFtFiSf1EcGohXyBRQAIzjVraTlINInB8PcwAaHlPK3QyqzyFWPovpej Av2UsectCnMXg== 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, Martin KaFai Lau Subject: [PATCH bpf-next v11 2/9] selftests/bpf: Add ASSERT_OK_FD macro Date: Tue, 9 Jul 2024 17:16:18 +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 Add a new dedicated ASSERT macro ASSERT_OK_FD to test whether a socket FD is valid or not. It can be used to replace macros ASSERT_GT(fd, 0, ""), ASSERT_NEQ(fd, -1, "") or statements (fd < 0), (fd != -1). Suggested-by: Martin KaFai Lau Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/test_progs.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tools/testing/selftests/bpf/test_progs.h b/tools/testing/selftests/bpf/test_progs.h index 0ba5a20b19ba..4f7b91c25b1e 100644 --- a/tools/testing/selftests/bpf/test_progs.h +++ b/tools/testing/selftests/bpf/test_progs.h @@ -377,6 +377,14 @@ int test__join_cgroup(const char *path); ___ok; \ }) +#define ASSERT_OK_FD(fd, name) ({ \ + static int duration = 0; \ + int ___fd = (fd); \ + bool ___ok = ___fd >= 0; \ + CHECK(!___ok, (name), "unexpected fd: %d\n", ___fd); \ + ___ok; \ +}) + #define SYS(goto_label, fmt, ...) \ ({ \ char cmd[1024]; \ From patchwork Tue Jul 9 09:16:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13727515 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 2324014E2FA; Tue, 9 Jul 2024 09:17:01 +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=1720516622; cv=none; b=pExjvqTjTJZH1KjJKqWoWltRdlX/Od51Lma/DSp8gzL7R1awlXfabDgYgQ/hAkkLch4yRcM0Y0a0uMSwyQFunsIkFT6tnRIIJZSVu5YE7bETPqmQ7ZNlCPoD4I+FwNnMMzuaNobzD/02obPeino8F6LQ0zsXWkyXCxUPwZpwee4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720516622; c=relaxed/simple; bh=fM+Fvp9zNJCCiGYWxrP8nEmg3zhc+E1Yb8AOlDJckXc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Q0JAWP+poguDcfhzZjhYAIHoOwghnXX708aevwKADd5q3NPeMxMJV05tHHHSaUNqZrSLWKxsyQgGgU8LexXyfFyE/Qw2NgMnv/EDMieo5e5FjvlsPt3BULqgXJIwB4Fib+6ZdJ7HXW8nkVXPOmcIkLT4g3u8KgB3i63EgEkYtAA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bFKixkb2; 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="bFKixkb2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A1DCDC3277B; Tue, 9 Jul 2024 09:16:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720516621; bh=fM+Fvp9zNJCCiGYWxrP8nEmg3zhc+E1Yb8AOlDJckXc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bFKixkb26ydQkxEYkcFOI1F0oFPz4quWlu+sjKO6jR1cSjLVqhi92VG7mhva8eisB GlR2UsDKI/pFk6hP9zlipQ22DRzlTxLOo59uYHBa7Y9VVdi03a5Su+6XKDo2veHa5S 3HJLdH/6+yIDf8ueYY9TBUMdVj6aGxWkaGjc6cllFaUBFfb9SSfmBJIQQ80n71kcCU IlE9kBjWyIQrvT8heiI6Ifg+a19BFrfWr04n50EQWKHGco0LLwmbqEQ5o6MwyJrx3C M1cCQKpYODwLmodHTo3bnF4sWcmKQeC2ONaiIUIZRQY97LIlW3as74PZEV81xBHvDJ YLNPgr9duwBiw== 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 v11 3/9] selftests/bpf: Close fd in error path in drop_on_reuseport Date: Tue, 9 Jul 2024 17:16:19 +0800 Message-ID: <86aed33b4b0ea3f04497c757845cff7e8e621a2d.1720515893.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 Tue Jul 9 09:16:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13727516 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 09AE214BFB4; Tue, 9 Jul 2024 09:17:08 +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=1720516629; cv=none; b=g26jmxiKMhliRgRa+M2An3RZ0HNPRSiyUg7KHSt85fRZ2L7fSStIe8slt5Ne/C1sVphvV69JwpUGVGwHceJsgTlSIU7xzdmbjNN0JScaLwcbGfce0LrOBgoYyHmpMw4u+rLzH7BEFpBNtAHzFRoj7eE1zZRBdILxCGDElb9Nrn0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720516629; c=relaxed/simple; bh=ApZvhLxtD4l70b1t3CJeyj/B8UQ6goleRLaq5M5l6PM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eFujUohVXet8zA0GSAXJ1prjSvcsRrcoP4hkiW3tgYGyt7ePHTMmLQuMo/5Cz7aYf8tySUDbGEpC3IUE/bDZLC1Fm3iin10U3TTVLRoIF1uFFRpGzCD3fqJPEd6litBSyXl0iWceP3HkNi6dkKmNDje3S/dPFsZuB1J+0xZAclw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=GzFiO5H0; 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="GzFiO5H0" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4F69CC3277B; Tue, 9 Jul 2024 09:17:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720516628; bh=ApZvhLxtD4l70b1t3CJeyj/B8UQ6goleRLaq5M5l6PM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GzFiO5H0FW5TxZ1x3d/UT/sCzZxzBuiLk8wEtbxzTINkdXVIs2o43Dtys6PlQ6+Ay Los/liv0rxNkZxcjX6iuyrIDr9JUaj5gbgLWpILHJLYDH9XOHo/FWEbzQjkIAVXz4w czJNJQaeUWTF8sBJS/ybTBFm3QdpCKp1f6JftpkEhXUKrjBqmv9LZBNzFQbd+TVDel Vqy4z1FHF8Yl2RU1LfX+JFQq+OIQI2PtpbX34in4xhvDxAORLVv6U3XLADV9b1l8BK KWrRuqrHRlJ08nZ9ds2Xzlg50IexIpZdIS+QT82HRVZWN3/DTxSjRYg3H8JZOfH44L f9iehTsZVfmEQ== 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 v11 4/9] selftests/bpf: Use start_server_str in sk_lookup Date: Tue, 9 Jul 2024 17:16:20 +0800 Message-ID: <5981539f5591d2c4998c962ef2bf45f34c940548.1720515893.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 start_server_str() to simplify make_server() in prog_tests/sk_lookup.c. Add a callback setsockopts() to do all sockopts, set it to post_socket_cb pointer of struct network_helper_opts. And add a new struct cb_opts to save the data needed to pass to the callback. Then pass this network_helper_opts to start_server_str(). Also use ASSERT_OK_FD() to check fd returned by start_server_str(). 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..20ee5da2c721 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_OK_FD(fd, "start_server_str")) + return -1; /* Late attach reuseport prog so we can have one init path */ if (reuseport_prog) { From patchwork Tue Jul 9 09:16:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13727517 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 948C115383C; Tue, 9 Jul 2024 09:17: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=1720516635; cv=none; b=kGA2GRKXcKjh6G9nNeq2hWzN1tVJj9i6jHuSq9egkdi8KrdBWoUVZ3RLnE/Gq8CNTh0+vI3YGUxHYs+03YcEZvtquXvALlH2DtgOMTHwJdLh0F/sI4wIY8AHyQICNYAowy6uMhk648bolJQ+Ps3M1xPRY/wLevFyDb5/CjVun8A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720516635; c=relaxed/simple; bh=RtLAAxP63ev6PDaj8W5yBc8/wpTYJbAhYJ9yfFG6RIY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AaJbjbEDYyY0njo+On6r3UpJoQUHQBJBszioZvLtFpCh1LPlDTs/thQRWTlKcL0P0yjFyfVJ1YgQaJSDHHYIpWcyTpRlXNqUOTydQUPxEKkjujd4XayWM7+YbhTaMDN+RrPSbNniE7nF/MVChcbH5WV8ASluz8SbCqHABRDFVNo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ea9DS2+B; 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="ea9DS2+B" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 41C42C32786; Tue, 9 Jul 2024 09:17:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720516635; bh=RtLAAxP63ev6PDaj8W5yBc8/wpTYJbAhYJ9yfFG6RIY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ea9DS2+BKOusGq892C+lT1qD2mzSpkT97qzt9Dr4gBJjMt78oFAu0jHf9E3EmcZM6 JIXKaP+4D2pLCdgdR8ox6rN23163kVwsjnnddVX12JkKKwG+/EQ4RfivcYphbehvHY Rbn2oufEXZbVIcKCb05lppGNXQeqN+isn7ASy1NlRAkOkXXXXAlHufz+G8YTkyCIh0 8y5KhrPSYi94SIgWu/l8dmp622KwmUtSgaBYhwjI/Z/ZhY/zuAIcx1xo5UaAJQ3cJf i3nEImUsSzb0v1JiWAt1kUgfEvFsdWb1OWDx1L0HcwNS7lWYYr34WGuRgHkZWA0QSn vfGb3/THR6x7g== 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 v11 5/9] selftests/bpf: Use start_server_addr in sk_lookup Date: Tue, 9 Jul 2024 17:16:21 +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() in udp_recv_send() in prog_tests/sk_lookup.c to simplify the code. And use ASSERT_OK_FD() to check fd returned by start_server_addr(). Acked-by: Eduard Zingerman Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/prog_tests/sk_lookup.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index 20ee5da2c721..386e482be617 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -416,18 +416,12 @@ static int udp_recv_send(int server_fd) } /* Reply from original destination address. */ - fd = socket(dst_addr->ss_family, SOCK_DGRAM, 0); - if (CHECK(fd < 0, "socket", "failed\n")) { + fd = start_server_addr(SOCK_DGRAM, dst_addr, sizeof(*dst_addr), NULL); + if (!ASSERT_OK_FD(fd, "start_server_addr")) { log_err("failed to create tx socket"); return -1; } - ret = bind(fd, (struct sockaddr *)dst_addr, sizeof(*dst_addr)); - if (CHECK(ret, "bind", "failed\n")) { - log_err("failed to bind tx socket"); - goto out; - } - msg.msg_control = NULL; msg.msg_controllen = 0; n = sendmsg(fd, &msg, 0); From patchwork Tue Jul 9 09:16:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13727518 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 BD96E15383C; Tue, 9 Jul 2024 09:17:21 +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=1720516641; cv=none; b=UY+VdPV9zs6Oz0QckSIBDkuzy1VuLU4V9i8IpcopAJtZswzmmLDXryfZsKK2bRrPjmdgHippAljSB3rtudcKhgyZPfct5Y+ZXVanla2VzC1NI+w6hu4KxYxVnsfLZ43wM1UsNCTh1SamLx4rIGDe6UFwSlnbnmjcHddcnSKxVqM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720516641; c=relaxed/simple; bh=obV9apooCgZ6Dwx3T8r2jSXX7I/NFkL8m08yFMyG2Fg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Jaz3jxcul+4HgUmPozLfAIwwh6ZWM4hBIxqlwy9vT/0q0Yd1/86sy+PT1NJsY/A3iqdvNtzUjIK+lP5qefEGAVcZxlZNl8WU6p9ws9g580Z54et2p2hYre1vBY5awS6/rdIyaApOx0pckzGRazLY9AvCDjF5URFpJFg9Bca7pa0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=sGN5wxwc; 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="sGN5wxwc" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E756CC4AF0D; Tue, 9 Jul 2024 09:17:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720516641; bh=obV9apooCgZ6Dwx3T8r2jSXX7I/NFkL8m08yFMyG2Fg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sGN5wxwc+IZz5jDWLTLxRFREEqlRScoL1bGIiRBHXkkZ+aK/GmlYe/k8fQjgnPbHq 5IS7aMxy3oSmZ9/UA102oVo6JCeGprJ3LD/zJJwbOAYz5n/qStfWASeRm5ccqdtZBU MBVcK3vP8eUGcNb7ACgZ+60QgX+m1wf/q353wJeXrE1TIVgRByE5ujLnU9cQkcjG7F kXcyoFI7j3ST2/kcu7CD2pIpyYw1js3u1di6IfxcM4S4iogTK7PC9akun/1rVW08QR 9/NA0rb/fzKfpNJiOEog3ZaiaW769IAiKvMsRMKJ7L2w8SUT3fKmIOfGhWOEVzrhjl naIytYAxQ9wvA== 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 v11 6/9] selftests/bpf: Use connect_fd_to_fd in sk_lookup Date: Tue, 9 Jul 2024 17:16:22 +0800 Message-ID: <7077c277cde5a1864cdc244727162fb75c8bb9c5.1720515893.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 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 386e482be617..ad3f943cc2bd 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -633,9 +633,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, @@ -643,12 +640,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, 0); + if (!ASSERT_OK(err, "connect_fd_to_fd")) goto close; } From patchwork Tue Jul 9 09:16:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13727519 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 347BD15383C; Tue, 9 Jul 2024 09:17:28 +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=1720516648; cv=none; b=aHnAKXEXGtNQ9SULC2UJyTol8mRd5xsK1nhOMBSYP5ja4+QwJeXZLlGDXemPwiDyAulpQxKeUhIbY5eo+4W/cOzQ0htjX2i/5ZdI9iKG8SFPrUoHsNWJLVIRnpnM655RaFys8tnWh4mKVRObSKPcw6qFxWVHKFjPUsNgK9K72Bk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720516648; c=relaxed/simple; bh=6OiEDsf7O33EMz4IPGSykQE7yUi/sOdP2yVB48lXWLY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=A6QFGuH+ZyJbJss5BRNGo5tDBfg4WHtGEfqu3lG7w9Qifhk8n3zgU0NuMFXdqNXWYkyEjakA4MdGepXQsE/k3QLj6CC7QoT8WcCDu9kQOq9Ifqx8ki/XdNS8L0ulYfBHFO1Ue51qz1aSsmBVcbFiznFxRgzKtDRafgal+a01Kp4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=skYe6Szd; 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="skYe6Szd" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 47B67C4AF0B; Tue, 9 Jul 2024 09:17:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720516648; bh=6OiEDsf7O33EMz4IPGSykQE7yUi/sOdP2yVB48lXWLY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=skYe6SzdnGQ9zt1iEXR9AbIB8DgBdr+j09iCucGo4nTBQq77plxTr91dATnr+Vem5 rbPNgT49qmAAMCZ8mvGHDJX7BHKWMp2Kbfc2Xu7WKXj9DO76ia5W5AUvJtvVtCm1K1 WxqdCKa4Uat94mTWbko+O8rx9AtTsF/XtPaH0gxNbbp/l9XfcBwkSF+sf1cqoBgvn6 SPrXw7H4ijRfRZA41+7fFt+kCEeiWtlqnLDY+lkYT138K7IWlXc9xzGAiRsvHn+enw hfyNhkcP5ULnl6tjzSTn30IRaWfDgpwqLoCR+WMx8lTk6tcEqz7HyoJoJcPSKTZIUq gUo8GVyk0unHw== 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 v11 7/9] selftests/bpf: Add connect_fd_to_addr_str helper Date: Tue, 9 Jul 2024 17:16:23 +0800 Message-ID: <96f04aa5677d628ad5eae1981565b24bf94c9d77.1720515893.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 Similar to connect_fd_to_fd() helper to connect from a client fd to a server fd, this patch adds a new helper connect_fd_to_addr_str() to connect from a client fd to a server address. It accepts the server address string "addr_str", together with the server family, type and port, as parameters instead of using a "server_fd" like connect_fd_to_fd(). Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 21 +++++++++++++++++++ tools/testing/selftests/bpf/network_helpers.h | 3 +++ 2 files changed, 24 insertions(+) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index e0cba4178e41..9758e707b859 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -388,6 +388,27 @@ int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms) return 0; } +int connect_fd_to_addr_str(int client_fd, int family, int type, + const char *addr_str, __u16 port, + const struct network_helper_opts *opts) +{ + struct sockaddr_storage addr; + socklen_t len; + + if (!opts) + opts = &default_opts; + + if (settimeo(client_fd, opts->timeout_ms)) + return -1; + + if (make_sockaddr(family, addr_str, port, &addr, &len)) { + log_err("Failed to make server addr"); + return -1; + } + + return connect_fd_to_addr(client_fd, &addr, len, false); +} + int make_sockaddr(int family, const char *addr_str, __u16 port, struct sockaddr_storage *addr, socklen_t *len) { diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 4f26bfc2dbf5..43526271366f 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -67,6 +67,9 @@ int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t len 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_addr_str(int client_fd, int family, int type, + const char *addr_str, __u16 port, + 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, From patchwork Tue Jul 9 09:16:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13727520 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 7E0B7154C18; Tue, 9 Jul 2024 09:17:35 +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=1720516655; cv=none; b=pf6qXlSByKryWJkYjheQsdUGDGEUr3EMrd28/kphvgqJur639NJjKeurCyj3u8kGAJMSketP7LFHeGjrsGtX9ZFMPADVm3BNP41wQ6qs/98ZrHAyFhi5IdOdUAgdywUggGyn4wKA3MJCzsyzqklRYvKnhNj4KtlvRM6IMURD0pE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720516655; c=relaxed/simple; bh=FTs7Ldb0HAt0X7TgHeEKPK53qGq/J14+v1OuN6qPB5I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Xuip9+UwpXWxgbQZfLChzfcqrfCX/QT5qZstSD4Nx2yBK0GRabmZgIkb0Kj1kOTCd8/yOVJCWHI3UWWf3kFWsYZRqHzuLTcc4mbEC47wgzXtZIfsmbb4Q+pa5sC1iksb/sUhnV5tQMzB1odnLClkNGh8G54mAhzyQxFwxMEUE1k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=GAgejxZM; 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="GAgejxZM" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B53ABC4AF13; Tue, 9 Jul 2024 09:17:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720516655; bh=FTs7Ldb0HAt0X7TgHeEKPK53qGq/J14+v1OuN6qPB5I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GAgejxZMG3Aox7rmBUoTds3IqlaZDh3TXRBufiLAhbuBldWVxGx3hZQG8HdWuo48N 86OhDfRVXxedT8W7KnpviakTO/1I4zEIEBPn13zeXsBDCWoRw4Y00rhcQSC1U8JtJa ArJ6hjWf/jnFM2wFcE94dxutBYzztgc+oVYhBO5Pa7f3AWWpesyR8z9H76f9IqfYVa MB/6IcjHklMCR5OkcH/ed9uuK5JBkYZ9UOzBINI9K/gGOKKTF4vl39AJ4h7qapeJmX aDBw04s6+wsE2QvLonxmL8XRB3by7gCaaVqukF9sx01XPzwKYSmOYWZIYrc23jagia BJ4apggWtk6Ug== 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 v11 8/9] selftests/bpf: Use connect_fd_to_addr_str in sk_lookup Date: Tue, 9 Jul 2024 17:16:24 +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 the new helper connect_fd_to_addr_str() in make_client() 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(). It is used to check with "errno" after invoking connect_fd_to_addr_str(). Signed-off-by: Geliang Tang --- .../testing/selftests/bpf/prog_tests/sk_lookup.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index ad3f943cc2bd..26a1c339492e 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -229,17 +229,17 @@ 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, int expect_errno) { - struct sockaddr_storage addr = {0}; + int family = is_ipv6(ip) ? AF_INET6 : AF_INET; int err, fd; - fd = make_socket(sotype, ip, port, &addr); + fd = socket(family, sotype, 0); if (fd < 0) return -1; - err = connect(fd, (void *)&addr, inetaddr_len(&addr)); - if (CHECK(err, "make_client", "connect")) { + err = connect_fd_to_addr_str(fd, family, sotype, ip, port, NULL); + if (CHECK(err && (!expect_errno || errno != expect_errno), "make_client", "connect")) { log_err("failed to connect client socket"); goto fail; } @@ -645,7 +645,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; @@ -1151,7 +1151,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; @@ -1165,7 +1165,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 Tue Jul 9 09:16:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13727526 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 0155615443C; Tue, 9 Jul 2024 09:17:41 +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=1720516662; cv=none; b=RNVrkYGhgDdCjFU4UB+vd3Z8AVQ/cHFAWBwrayrzZl50jboWnK+qQCxnJXboQwKGAYuFcriDLPsE2wDsNN/mMfkZNQo42vEJqIdE/MPj6MxEi09JN2WxRk6kf8wteWp+k5EsmS+ogvsIuahFUMpTB/xyoHAKtHekgXLBuNIgDug= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720516662; c=relaxed/simple; bh=cs6yIxGVVGrcoFSCRxBNO4LhFqM+0ikFcaOOvwvx0uI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RaSdVZgnBd+TLaACx6l+Jisxg6CmawXVf9Mmc2oqQZFydLNIyHJrdXTUJkNb19VvGwwAYU9zfjpwL2QJi8PUOMsS/syh04pABeh7R4PUGjIsdrIfWu9rU8qMXP+aBgh+yyIzTxMbeMcyZJQAO0d4TQmiNRz0UcEGL6h4w2f31xc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=UNEjF1ob; 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="UNEjF1ob" Received: by smtp.kernel.org (Postfix) with ESMTPSA id ACB82C4AF13; Tue, 9 Jul 2024 09:17:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720516661; bh=cs6yIxGVVGrcoFSCRxBNO4LhFqM+0ikFcaOOvwvx0uI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UNEjF1ob0RvI9pRsD5NRKl45G/F2/p9l1SoQRTlOrcwe428S3HSYVdEKhKAUNu0Z3 7y4qF/yEJvGojZJ+2z4HtcREV78HDusaZCuYqgtoNmpQKdmUk4tyscM9yYh5vGrZAm G98RDkuyxzkBTNZRyPnl2kHv7cUpYrH32qUEbqPbPqSJ/h83yyqVJNxaj2aJptQuXq aeZY/J5hNFA9DoJm3Qg7ypkESIsdqpiliPgnuSrAZol57RA9lrS59UV0ngvisQ/koP dQGjEW+eAVcrnp/Mg0FsWoKmCeqF7wqtn8wdknNJsqQT2oAxlnTbGLkCmTCLI4b3HX /NGuBeEl0/e6g== 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 v11 9/9] selftests/bpf: Drop make_socket in sk_lookup Date: Tue, 9 Jul 2024 17:16:25 +0800 Message-ID: <2c7bf233c4f657719ad7a62c301a60fe744916f5.1720515893.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 drop_on_lookup(), drop_on_reuseport() and 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 | 59 +++---------------- 1 file changed, 8 insertions(+), 51 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index 26a1c339492e..f4bc6f968789 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; @@ -861,7 +821,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; @@ -875,12 +834,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) @@ -975,7 +934,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; @@ -999,12 +957,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) @@ -1214,7 +1172,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; @@ -1247,11 +1204,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 = t->expect_errno ? -errno : 0; if (CHECK(err && !t->expect_errno, "connect", "unexpected error %d\n", errno)) goto out_close_client;