From patchwork Thu May 16 01:13:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 797382 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 39AA84A0A; Thu, 16 May 2024 01:13:40 +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=1715822021; cv=none; b=M6JLpgg3fHc94rDPyAKutUY2apBlqJ1LJB3adhsN7i7m8i9yQStwZQ+4IP+VQ5IDEq2vPp0aO4Cks7MKY1vQopFcTQyOtfqPut63tzqc63n4YsqW/uMmbyUR1H7EhG6DNm/eAgpI7l1I97vS/NpWtS08DXrQ7QtoTyUMvCnSD5A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715822021; c=relaxed/simple; bh=PYLreC9y69ftkHES1PiYml+GPmE1yXRehjsAcxtM0yE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=epF4nD35ECiPSYlprqwaLS90ghe0x0Mh/IfyKGLVQ5cX5vrMOHajGAUMnJ3yeSUGJxgABcrPiHIfv/xYpM2WNy72Mg2q2l43ddwS2MuY/wqC8CbujCX0F0N0KfCiKUxhqpvt7drllwvOHdgYO+aQTQQcw3vvCklEw3UfqN8Q/Hg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ibU4bvWb; 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="ibU4bvWb" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 86B4AC32789; Thu, 16 May 2024 01:13:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715822020; bh=PYLreC9y69ftkHES1PiYml+GPmE1yXRehjsAcxtM0yE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ibU4bvWbLh+yvbt5FdWsnHYegiOXOcO2vzCjNnEN5Mo/1DJBYlf00SrnZHFfNLKaU V1pPwwT0d/5871mwU5BK7occF0ytCdc0dWxebcv/288+MwaKSBvPlKTn+O1i8VOx7p 118gmRuuZ+VmhlenVU7o0VpTMcmdug5xWVkwdJ6JynzCufatKELhrsa/Ky0t0U82KE BwGn9GgkBVTLgM+BSBQ4KPv8SZ04RjkNWpvn/vtsHI7f+PUQu8xmd5gYF5IfhIL6jQ bQ8IAy4ORl/oz5nvIBsXKZLc+lX9H7jtdaOD9o5gEKgN5wSCzzeadHnDxXoV2OC74J ErCLedkyD4rHQ== 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, mptcp@lists.linux.dev, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v2 1/8] selftests/bpf: Add unshare_netns helper Date: Thu, 16 May 2024 09:13:07 +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 Many BPF selftests create new test network namespaces by using CLONE_NEWNET flag to unshare the network namespace, so that the calling process is moved into a new network namespace which is not shared with any previously existing process. So this patch adds a new helper in network_helpers.c named unshare_netns(), which create a new network namespace and set the net device lo up. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 19 +++++++++++++++++++ tools/testing/selftests/bpf/network_helpers.h | 1 + 2 files changed, 20 insertions(+) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 35250e6cde7f..919bb2a0c6a6 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -670,3 +670,22 @@ int send_recv_data(int lfd, int fd, uint32_t total_bytes) return err; } + +int unshare_netns(void) +{ + int err; + + err = unshare(CLONE_NEWNET); + if (err) { + log_err("unshare netns failed"); + return err; + } + + err = SYS_NOFAIL("ip link set dev lo up"); + if (err) { + log_err("set dev lo up failed"); + return err; + } + + return 0; +} diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 883c7ea9d8d5..b087330f2861 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -82,6 +82,7 @@ struct nstoken; struct nstoken *open_netns(const char *name); void close_netns(struct nstoken *token); int send_recv_data(int lfd, int fd, uint32_t total_bytes); +int unshare_netns(void); static __u16 csum_fold(__u32 csum) { From patchwork Thu May 16 01:13:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 797381 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 ABE2A1854; Thu, 16 May 2024 01:13:54 +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=1715822034; cv=none; b=Hstlzb54ibQSh4E5QqWO6XA2YF0i7/1CbstnV/QYgCk8531+maFoU7bemHmJSQOWrjIPjjN24G3DhBenXkfeJvBOE6j86wptgodRccq56x/O1q8Q1uaoKJ7r0gq2GE6cYg/N3R+7GyBlpKajh+zGw2HvqrSr3/i7Y0CgX25atok= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715822034; c=relaxed/simple; bh=tbrFM8SkYDvnxIQcgUhbjrMR9nLEahNNCk3IKrv15cs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UFW1hxfGrSPbnDhXj49EI0ZdufQME99sReUp61dUhjfa2EqmQbYOpgdC5RoIzc/SzXs3azK0vNNh8DdoMpY1CF9cSPjAsL+Gn60npGEuvwxRSII+teMLv0d6fQY+6X5yoUCuclfKc8j0LPMGceOF47kHHaHwhAZGJhz5thTJUKY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=f6As9B6K; 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="f6As9B6K" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D8F55C32782; Thu, 16 May 2024 01:13:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715822034; bh=tbrFM8SkYDvnxIQcgUhbjrMR9nLEahNNCk3IKrv15cs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=f6As9B6Kpa736Jph+Dy4j8r2QQdW0QvoQrXKTDUH0NLHfD7KwML6JnLKHSeZhGMW0 MuCo6tuWLfW2ptmKg9V+Rth8tQm8CsajvIrSWJ+XHcXRD5iCCAEr4+8emLbCxM9f+m E+SHP3LdrT/iGJrRUD2eSC4ur2F/G9Xm6WRl6rICFvJQy09G0gKJUG/aa9Su0S8Q8R wdoczfWgjnRFGWUM6zkpCUcyfaXF71ZqKaeXuov5UHp5S4LFqJsJYv26fLX9EdiQy2 rtLLUwjTA7p/2D95aOxy5LC6kaWDmZg+yhxgbJudBx2uX8gnBQG7wIaD1pzmbzbx0n 7gbUpRvGtRjLQ== 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, mptcp@lists.linux.dev, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v2 3/8] selftests/bpf: Drop duplicate create_netns Date: Thu, 16 May 2024 09:13:09 +0800 Message-ID: <1b427d0f482b6e683a55d8be6280c4389a2662da.1715821541.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 drops the duplicate local functions create_netns() in bind_perm.c, bpf_iter_setsockopt.c, setget_sockopt.c, sock_fields.c and tcp_hdr_options.c and uses unshare_netns() instead. A new helper create_netns() will be added in network_helpers.c as a public one. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/prog_tests/bind_perm.c | 11 ++--------- .../selftests/bpf/prog_tests/bpf_iter_setsockopt.c | 13 +------------ .../selftests/bpf/prog_tests/setget_sockopt.c | 10 ++-------- .../testing/selftests/bpf/prog_tests/sock_fields.c | 13 +------------ .../selftests/bpf/prog_tests/tcp_hdr_options.c | 13 +------------ 5 files changed, 7 insertions(+), 53 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/bind_perm.c b/tools/testing/selftests/bpf/prog_tests/bind_perm.c index f7cd129cb82b..bc80c2773b67 100644 --- a/tools/testing/selftests/bpf/prog_tests/bind_perm.c +++ b/tools/testing/selftests/bpf/prog_tests/bind_perm.c @@ -7,16 +7,9 @@ #include "test_progs.h" #include "cap_helpers.h" +#include "network_helpers.h" #include "bind_perm.skel.h" -static int create_netns(void) -{ - if (!ASSERT_OK(unshare(CLONE_NEWNET), "create netns")) - return -1; - - return 0; -} - void try_bind(int family, int port, int expected_errno) { struct sockaddr_storage addr = {}; @@ -54,7 +47,7 @@ void test_bind_perm(void) __u64 old_caps = 0; int cgroup_fd; - if (create_netns()) + if (unshare_netns()) return; cgroup_fd = test__join_cgroup("/bind_perm"); diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_iter_setsockopt.c b/tools/testing/selftests/bpf/prog_tests/bpf_iter_setsockopt.c index b52ff8ce34db..eff5d445896b 100644 --- a/tools/testing/selftests/bpf/prog_tests/bpf_iter_setsockopt.c +++ b/tools/testing/selftests/bpf/prog_tests/bpf_iter_setsockopt.c @@ -8,17 +8,6 @@ #include "bpf_cubic.skel.h" #include "bpf_iter_setsockopt.skel.h" -static int create_netns(void) -{ - if (!ASSERT_OK(unshare(CLONE_NEWNET), "create netns")) - return -1; - - if (!ASSERT_OK(system("ip link set dev lo up"), "bring up lo")) - return -1; - - return 0; -} - static unsigned int set_bpf_cubic(int *fds, unsigned int nr_fds) { unsigned int i; @@ -187,7 +176,7 @@ void serial_test_bpf_iter_setsockopt(void) struct bpf_link *cubic_link = NULL; struct bpf_link *dctcp_link = NULL; - if (create_netns()) + if (unshare_netns()) return; /* Load iter_skel */ diff --git a/tools/testing/selftests/bpf/prog_tests/setget_sockopt.c b/tools/testing/selftests/bpf/prog_tests/setget_sockopt.c index 7d4a9b3d3722..a44b6e3cec15 100644 --- a/tools/testing/selftests/bpf/prog_tests/setget_sockopt.c +++ b/tools/testing/selftests/bpf/prog_tests/setget_sockopt.c @@ -20,14 +20,8 @@ static const char addr6_str[] = "::1"; static struct setget_sockopt *skel; static int cg_fd; -static int create_netns(void) +static int create_veth(void) { - if (!ASSERT_OK(unshare(CLONE_NEWNET), "create netns")) - return -1; - - if (!ASSERT_OK(system("ip link set dev lo up"), "set lo up")) - return -1; - if (!ASSERT_OK(system("ip link add dev binddevtest1 type veth peer name binddevtest2"), "add veth")) return -1; @@ -160,7 +154,7 @@ void test_setget_sockopt(void) if (cg_fd < 0) return; - if (create_netns()) + if (unshare_netns() || create_veth()) goto done; skel = setget_sockopt__open(); diff --git a/tools/testing/selftests/bpf/prog_tests/sock_fields.c b/tools/testing/selftests/bpf/prog_tests/sock_fields.c index 7d23166c77af..3499f54b14d1 100644 --- a/tools/testing/selftests/bpf/prog_tests/sock_fields.c +++ b/tools/testing/selftests/bpf/prog_tests/sock_fields.c @@ -45,17 +45,6 @@ static __u64 child_cg_id; static int linum_map_fd; static __u32 duration; -static bool create_netns(void) -{ - if (!ASSERT_OK(unshare(CLONE_NEWNET), "create netns")) - return false; - - if (!ASSERT_OK(system("ip link set dev lo up"), "bring up lo")) - return false; - - return true; -} - static void print_sk(const struct bpf_sock *sk, const char *prefix) { char src_ip4[24], dst_ip4[24]; @@ -350,7 +339,7 @@ void serial_test_sock_fields(void) struct bpf_link *link; /* Use a dedicated netns to have a fixed listen port */ - if (!create_netns()) + if (unshare_netns()) return; /* Create a cgroup, get fd, and join it */ diff --git a/tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c b/tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c index 56685fc03c7e..6b4d8fd0f087 100644 --- a/tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c +++ b/tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c @@ -40,17 +40,6 @@ struct sk_fds { int active_lport; }; -static int create_netns(void) -{ - if (!ASSERT_OK(unshare(CLONE_NEWNET), "create netns")) - return -1; - - if (!ASSERT_OK(system("ip link set dev lo up"), "run ip cmd")) - return -1; - - return 0; -} - static void print_hdr_stg(const struct hdr_stg *hdr_stg, const char *prefix) { fprintf(stderr, "%s{active:%u, resend_syn:%u, syncookie:%u, fastopen:%u}\n", @@ -548,7 +537,7 @@ void test_tcp_hdr_options(void) if (!test__start_subtest(tests[i].desc)) continue; - if (create_netns()) + if (unshare_netns()) break; tests[i].run(); From patchwork Thu May 16 01:13:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 797380 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 CB8971362; Thu, 16 May 2024 01:14:05 +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=1715822045; cv=none; b=Mzv5tfbWXyPh1soZe+9yRdfLbor+jpjmDCe61WqpXSXHt8A8t43RyDKX7ShfxCNqCrkftflsmJlr52KuGELZ1FP4NHGGYNsG5uYi2aRRkelD12yojg2AVtQ7JI2eEPqVDzy+odsmqxza9r7eGnodAY/tkIrJnRmGZKZ3kH03Jo4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715822045; c=relaxed/simple; bh=4QX+ONTirKaC8feJ1dluSUmW6WX/Sk/AkAufShM/UBs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dX7gZsuK5cwK0MJIO+sdwpoQ/TaEU8cRdWiWMIIzwk12NtyV/8KDrgXWbBIw2JHbjE1E00Wbg68bpqVYPJiIR1HMge+ao7GfHdYGiDw5IUDw2Vmc22SAnXAShE+Y+77MCaj9fmOPzTK1CUOlDfKO0gtx/utPJ3TsqTkGfTGbOyk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=TBG3TajT; 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="TBG3TajT" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3C7F8C116B1; Thu, 16 May 2024 01:14:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715822045; bh=4QX+ONTirKaC8feJ1dluSUmW6WX/Sk/AkAufShM/UBs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TBG3TajT2OhTdU4vipuPBPZBS9LmOnX6fj3FVWuyY+/iDBhGoBhWWRXapHeqqoRad tadWqmrfzQSjUglXjGQr/dn18ZRJfXodxczP1mpbjKm66oa5pA2IE1EvP0Tct9/04b J72tQ/QD+dCx6f9Fhk/Op6Iy/lxcx7Aglr0uhD40JWtPWKmHyrWIl11p57QEX2veDA WIxVWXP4AdqdaUpixRcqTU8Bnoi0fBQj3Ex9JaL1sXSkkmjLwNi/6UgZys0rypj1cv cYVWydgzGflrSTalNhbuHXPEn9MXk9TjbXKq/7KhzFUHbrMRmZurS5ANVJ/1fDpPLY /sNayGi9hoaMg== 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, mptcp@lists.linux.dev, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v2 5/8] selftests/bpf: Use create_netns helper Date: Thu, 16 May 2024 09:13:11 +0800 Message-ID: <8859791415bfbbf0015667f1b1c7b1c1c5956574.1715821541.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 The newly added helper create_netns() can be used in assign_reuse.c, fib_lookup.c, ns_current_pid_tgid.c, sock_destroy.c and sock_iter_batch.c to simplify the code. It changes the behavior of test fib_lookup.c a little bit, but doesn't affect the results. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/prog_tests/assign_reuse.c | 9 ++------- tools/testing/selftests/bpf/prog_tests/fib_lookup.c | 4 +--- .../selftests/bpf/prog_tests/ns_current_pid_tgid.c | 5 +---- tools/testing/selftests/bpf/prog_tests/sock_destroy.c | 5 +---- tools/testing/selftests/bpf/prog_tests/sock_iter_batch.c | 6 +----- 5 files changed, 6 insertions(+), 23 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/assign_reuse.c b/tools/testing/selftests/bpf/prog_tests/assign_reuse.c index 989ee4d9785b..21e1549d9749 100644 --- a/tools/testing/selftests/bpf/prog_tests/assign_reuse.c +++ b/tools/testing/selftests/bpf/prog_tests/assign_reuse.c @@ -175,12 +175,9 @@ void test_assign_reuse(void) { struct nstoken *tok = NULL; - SYS(out, "ip netns add %s", NS_TEST); - SYS(cleanup, "ip -net %s link set dev lo up", NS_TEST); - - tok = open_netns(NS_TEST); + tok = create_netns(NS_TEST); if (!ASSERT_OK_PTR(tok, "netns token")) - return; + goto cleanup; if (test__start_subtest("tcpv4")) run_assign_reuse(AF_INET, SOCK_STREAM, "127.0.0.1", PORT); @@ -194,6 +191,4 @@ void test_assign_reuse(void) cleanup: close_netns(tok); SYS_NOFAIL("ip netns delete %s", NS_TEST); -out: - return; } diff --git a/tools/testing/selftests/bpf/prog_tests/fib_lookup.c b/tools/testing/selftests/bpf/prog_tests/fib_lookup.c index bd7658958004..b7eac1fce746 100644 --- a/tools/testing/selftests/bpf/prog_tests/fib_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/fib_lookup.c @@ -315,9 +315,7 @@ void test_fib_lookup(void) return; prog_fd = bpf_program__fd(skel->progs.fib_lookup); - SYS(fail, "ip netns add %s", NS_TEST); - - nstoken = open_netns(NS_TEST); + nstoken = create_netns(NS_TEST); if (!ASSERT_OK_PTR(nstoken, "open_netns")) goto fail; diff --git a/tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c b/tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c index e72d75d6baa7..6dae14d106ac 100644 --- a/tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c +++ b/tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c @@ -204,10 +204,7 @@ static void test_in_netns(int (*fn)(void *), void *arg) { struct nstoken *nstoken = NULL; - SYS(cleanup, "ip netns add ns_current_pid_tgid"); - SYS(cleanup, "ip -net ns_current_pid_tgid link set dev lo up"); - - nstoken = open_netns("ns_current_pid_tgid"); + nstoken = create_netns("ns_current_pid_tgid"); if (!ASSERT_OK_PTR(nstoken, "open_netns")) goto cleanup; diff --git a/tools/testing/selftests/bpf/prog_tests/sock_destroy.c b/tools/testing/selftests/bpf/prog_tests/sock_destroy.c index 9c11938fe597..c156218c7280 100644 --- a/tools/testing/selftests/bpf/prog_tests/sock_destroy.c +++ b/tools/testing/selftests/bpf/prog_tests/sock_destroy.c @@ -193,10 +193,7 @@ void test_sock_destroy(void) if (!ASSERT_OK_PTR(skel->links.sock_connect, "prog_attach")) goto cleanup; - SYS(cleanup, "ip netns add %s", TEST_NS); - SYS(cleanup, "ip -net %s link set dev lo up", TEST_NS); - - nstoken = open_netns(TEST_NS); + nstoken = create_netns(TEST_NS); if (!ASSERT_OK_PTR(nstoken, "open_netns")) goto cleanup; diff --git a/tools/testing/selftests/bpf/prog_tests/sock_iter_batch.c b/tools/testing/selftests/bpf/prog_tests/sock_iter_batch.c index d56e18b25528..b82ab5ddd9e6 100644 --- a/tools/testing/selftests/bpf/prog_tests/sock_iter_batch.c +++ b/tools/testing/selftests/bpf/prog_tests/sock_iter_batch.c @@ -112,11 +112,7 @@ void test_sock_iter_batch(void) { struct nstoken *nstoken = NULL; - SYS_NOFAIL("ip netns del " TEST_NS); - SYS(done, "ip netns add %s", TEST_NS); - SYS(done, "ip -net %s link set dev lo up", TEST_NS); - - nstoken = open_netns(TEST_NS); + nstoken = create_netns(TEST_NS); if (!ASSERT_OK_PTR(nstoken, "open_netns")) goto done; From patchwork Thu May 16 01:13:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 797379 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 A484710F1; Thu, 16 May 2024 01:14:19 +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=1715822059; cv=none; b=VZb9YYxUIcSx5prpNILUFzfHVXw/Yp5qj4QaElSMwjMBcjT7gnPqLUy+b6NmKXoTjlmGH87H98QTRE9E9XrKEdyI/NMquGSVOEvoA/9KcsRxxOAhrf2lXJ21K44iqeBJyUwCHaito87VJb0tdGrjUTuV5j8iZvF8yXvSEDHrDcw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715822059; c=relaxed/simple; bh=Uo4wLsAlHWOererHPGvsbRVZjycyG+wZVHCA7IFyqz4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kNi28ftQQvFjlbCr6G/KEigE8mWSd5wBwNevQBbw8YevJElcx7nAzuaOvpdjdVsJhrXQ686LnUAVdIm4bZTrK3/sM5Znbu3YIXohxCj2KgTdEm/PzcjCJBGXpQZyVklaA6wmFsxMap1jQ2Wl+GvHxs4/9lDfBRfkMvWabvruMDI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Jde1oyOx; 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="Jde1oyOx" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C7785C32786; Thu, 16 May 2024 01:14:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715822059; bh=Uo4wLsAlHWOererHPGvsbRVZjycyG+wZVHCA7IFyqz4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Jde1oyOxYp8DaQCMaya6jI/lNx+AG1YZ9HYLA1K1TW+pB3ETgU9IMeDO0TPyERsmN 27KWpX6Qmhb+CLdgefCydChempXl1Vs0q9bWVfiezGEyM11b9Fp77SqV39OFILLgb/ KA28Vjcg+f6kXHP76qx6i5uqhxRnI06ZGJN/RsuBSLmVJltDTJc/oXkW3p40X+Cq7J rR7yc7H4ZM6Wn9bveLYzMEo/bjdm+iQWPeBuwyxSGUIFL30bBr0hnrZ0XcUkX6qsUy ar+e8Vr6cpDbgkBvu9/EMrCajzY58Ebp4Gos6jPLHnIqOk65r6ABtVR7DiyGCdsp3l UGuM0CgoZTnyw== 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, mptcp@lists.linux.dev, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v2 7/8] selftests/bpf: Use cleanup_netns helper Date: Thu, 16 May 2024 09:13:13 +0800 Message-ID: <4b69f489f2cd66dcd69851e4cfc419a3dde4b575.1715821541.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 cleanup_netns() helper in BPF tests wide to replace close_netns() and "ip netns del", included assign_reuse.c, crypto_sanity.c, decap_sanity.c, fib_lookup.c, ns_current_pid_tgid.c, sock_destroy.c, sock_iter_batch.c, xdp_dev_bound_only.c and xdp_do_redirect.c. This can simplify the code. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/prog_tests/assign_reuse.c | 3 +-- tools/testing/selftests/bpf/prog_tests/crypto_sanity.c | 3 +-- tools/testing/selftests/bpf/prog_tests/decap_sanity.c | 6 ++---- tools/testing/selftests/bpf/prog_tests/fib_lookup.c | 4 +--- .../testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c | 4 +--- tools/testing/selftests/bpf/prog_tests/sock_destroy.c | 4 +--- tools/testing/selftests/bpf/prog_tests/sock_iter_batch.c | 3 +-- tools/testing/selftests/bpf/prog_tests/xdp_dev_bound_only.c | 3 +-- tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c | 4 +--- 9 files changed, 10 insertions(+), 24 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/assign_reuse.c b/tools/testing/selftests/bpf/prog_tests/assign_reuse.c index 21e1549d9749..6fa08c47357c 100644 --- a/tools/testing/selftests/bpf/prog_tests/assign_reuse.c +++ b/tools/testing/selftests/bpf/prog_tests/assign_reuse.c @@ -189,6 +189,5 @@ void test_assign_reuse(void) run_assign_reuse(AF_INET6, SOCK_DGRAM, "::1", PORT); cleanup: - close_netns(tok); - SYS_NOFAIL("ip netns delete %s", NS_TEST); + cleanup_netns(tok); } diff --git a/tools/testing/selftests/bpf/prog_tests/crypto_sanity.c b/tools/testing/selftests/bpf/prog_tests/crypto_sanity.c index b1a3a49a822a..ce6ceac8a812 100644 --- a/tools/testing/selftests/bpf/prog_tests/crypto_sanity.c +++ b/tools/testing/selftests/bpf/prog_tests/crypto_sanity.c @@ -190,8 +190,7 @@ void test_crypto_sanity(void) ASSERT_OK(err, "bpf_tc_detach decrypt"); fail: - close_netns(nstoken); + cleanup_netns(nstoken); deinit_afalg(); - SYS_NOFAIL("ip netns del " NS_TEST " &> /dev/null"); crypto_sanity__destroy(skel); } diff --git a/tools/testing/selftests/bpf/prog_tests/decap_sanity.c b/tools/testing/selftests/bpf/prog_tests/decap_sanity.c index dcb9e5070cc3..9528b039ad33 100644 --- a/tools/testing/selftests/bpf/prog_tests/decap_sanity.c +++ b/tools/testing/selftests/bpf/prog_tests/decap_sanity.c @@ -68,10 +68,8 @@ void test_decap_sanity(void) ASSERT_FALSE(skel->bss->broken_csum_start, "broken_csum_start"); fail: - if (nstoken) { + if (nstoken) bpf_tc_hook_destroy(&qdisc_hook); - close_netns(nstoken); - } - SYS_NOFAIL("ip netns del " NS_TEST); + cleanup_netns(nstoken); decap_sanity__destroy(skel); } diff --git a/tools/testing/selftests/bpf/prog_tests/fib_lookup.c b/tools/testing/selftests/bpf/prog_tests/fib_lookup.c index b7eac1fce746..9de05603098d 100644 --- a/tools/testing/selftests/bpf/prog_tests/fib_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/fib_lookup.c @@ -368,8 +368,6 @@ void test_fib_lookup(void) } fail: - if (nstoken) - close_netns(nstoken); - SYS_NOFAIL("ip netns del " NS_TEST); + cleanup_netns(nstoken); fib_lookup__destroy(skel); } diff --git a/tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c b/tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c index 6dae14d106ac..b4af39dbc449 100644 --- a/tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c +++ b/tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c @@ -211,9 +211,7 @@ static void test_in_netns(int (*fn)(void *), void *arg) test_ns_current_pid_tgid_new_ns(fn, arg); cleanup: - if (nstoken) - close_netns(nstoken); - SYS_NOFAIL("ip netns del ns_current_pid_tgid"); + cleanup_netns(nstoken); } /* TODO: use a different tracepoint */ diff --git a/tools/testing/selftests/bpf/prog_tests/sock_destroy.c b/tools/testing/selftests/bpf/prog_tests/sock_destroy.c index c156218c7280..1d3abc7a92c4 100644 --- a/tools/testing/selftests/bpf/prog_tests/sock_destroy.c +++ b/tools/testing/selftests/bpf/prog_tests/sock_destroy.c @@ -209,9 +209,7 @@ void test_sock_destroy(void) RUN_TESTS(sock_destroy_prog_fail); cleanup: - if (nstoken) - close_netns(nstoken); - SYS_NOFAIL("ip netns del " TEST_NS); + cleanup_netns(nstoken); if (cgroup_fd >= 0) close(cgroup_fd); sock_destroy_prog__destroy(skel); diff --git a/tools/testing/selftests/bpf/prog_tests/sock_iter_batch.c b/tools/testing/selftests/bpf/prog_tests/sock_iter_batch.c index b82ab5ddd9e6..f37e6450d08b 100644 --- a/tools/testing/selftests/bpf/prog_tests/sock_iter_batch.c +++ b/tools/testing/selftests/bpf/prog_tests/sock_iter_batch.c @@ -124,8 +124,7 @@ void test_sock_iter_batch(void) do_test(SOCK_DGRAM, true); do_test(SOCK_DGRAM, false); } - close_netns(nstoken); done: - SYS_NOFAIL("ip netns del " TEST_NS); + cleanup_netns(nstoken); } diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_dev_bound_only.c b/tools/testing/selftests/bpf/prog_tests/xdp_dev_bound_only.c index 7dd18c6d06c6..315777a4c7ce 100644 --- a/tools/testing/selftests/bpf/prog_tests/xdp_dev_bound_only.c +++ b/tools/testing/selftests/bpf/prog_tests/xdp_dev_bound_only.c @@ -53,9 +53,8 @@ void test_xdp_dev_bound_only_offdev(void) out: close(fd1); close(fd2); - close_netns(tok); /* eth42 was added inside netns, removing the netns will * also remove eth42 veth pair. */ - SYS_NOFAIL("ip netns del " LOCAL_NETNS); + cleanup_netns(tok); } diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c b/tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c index bad0ea167be7..27cb409a237f 100644 --- a/tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c +++ b/tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c @@ -241,8 +241,6 @@ void test_xdp_do_redirect(void) out_tc: bpf_tc_hook_destroy(&tc_hook); out: - if (nstoken) - close_netns(nstoken); - SYS_NOFAIL("ip netns del testns"); + cleanup_netns(nstoken); test_xdp_do_redirect__destroy(skel); }