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: 13665630 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:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13665631 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 77C4510E6; Thu, 16 May 2024 01:13: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=1715822028; cv=none; b=HVYa/gv3OvcJ3Ps6dqk1UiT3HBzc3D8agN4v0oRuDdpHhZXLnlFNIYVDL955ejZp+Lpsl787RL77mYUm32lLq4d5Phaqm7SDIzfZwDsq1U9gZoKW+d7yBDmBa5kchJeHxCHXrP+Y8j5gw4mROqgGfilsJjuoR0i1BUYIIceNPkc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715822028; c=relaxed/simple; bh=9RIzon2SQFRluCkM4WuVpqfZM7QKLJta1nv2UWTfiXM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Hn17x042tzZXSOq/i/mTPlAzBkGPMbeUoiJkv7F9eUtacZMPjYI1L/E73SNymOnRMAEiciq/BzeI44kYs0yD7ntSs2Buf8dhmhqqXg59lpWJ8U0Cq4i4I5K9wn0xR9IPDn7TjwSHvaTDMpThQOpXAYcEQk95bp2uuJHccQscuzY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=fm+gUajB; 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="fm+gUajB" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7B4A3C116B1; Thu, 16 May 2024 01:13:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715822028; bh=9RIzon2SQFRluCkM4WuVpqfZM7QKLJta1nv2UWTfiXM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fm+gUajBIrXdq1jEl+IXdn6UM+bOrpU9FP5krOj+r8eFWqku/gyisAG5FaDkTiM+e Cg2hv5CbW1WhZHB1vTLXh1r8oHUYM9gXBONzdXUP+00DjMxr24b+OzeuqApZHZV/KL +CoZ38NgyrnEALo44lZb5cwHEkk8BJDl+T1AeqXIO1INqXij/SDD6PVdI9F4zzGRw8 E0/7PmRR52kFRTWzki8UTn3DpmvDkqc695sB5+fFtejZzoj2P1ppU8d0HjV2uGXtN3 SI6JyhmnILhdpWiOCkgrdjhzQfIlFSB1KzZExQwXFvI2dO5NFtw3VQkZDXNHxPprYd eOJjHwgeWTu2w== 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 2/8] selftests/bpf: Use unshare_netns helper Date: Thu, 16 May 2024 09:13:08 +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 The newly added helper unshare_netns() can be used to replace unshare() and "ip link set dev lo up" in sk_assign.c, btf_skc_cls_ingress.c and tcp_custom_syncookie.c to simplify the code. Signed-off-by: Geliang Tang --- .../testing/selftests/bpf/prog_tests/btf_skc_cls_ingress.c | 6 +----- tools/testing/selftests/bpf/prog_tests/sk_assign.c | 4 +--- .../testing/selftests/bpf/prog_tests/tcp_custom_syncookie.c | 5 +---- 3 files changed, 3 insertions(+), 12 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/btf_skc_cls_ingress.c b/tools/testing/selftests/bpf/prog_tests/btf_skc_cls_ingress.c index ef4d6a3ae423..f697397c641e 100644 --- a/tools/testing/selftests/bpf/prog_tests/btf_skc_cls_ingress.c +++ b/tools/testing/selftests/bpf/prog_tests/btf_skc_cls_ingress.c @@ -27,15 +27,11 @@ static int prepare_netns(void) LIBBPF_OPTS(bpf_tc_opts, tc_attach, .prog_fd = bpf_program__fd(skel->progs.cls_ingress)); - if (CHECK(unshare(CLONE_NEWNET), "create netns", + if (CHECK(unshare_netns(), "create netns", "unshare(CLONE_NEWNET): %s (%d)", strerror(errno), errno)) return -1; - if (CHECK(system("ip link set dev lo up"), - "ip link set dev lo up", "failed\n")) - return -1; - qdisc_lo.ifindex = if_nametoindex("lo"); if (!ASSERT_OK(bpf_tc_hook_create(&qdisc_lo), "qdisc add dev lo clsact")) return -1; diff --git a/tools/testing/selftests/bpf/prog_tests/sk_assign.c b/tools/testing/selftests/bpf/prog_tests/sk_assign.c index 0b9bd1d6f7cc..56df5861059f 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_assign.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_assign.c @@ -47,12 +47,10 @@ configure_stack(void) return false; /* Move to a new networking namespace */ - if (CHECK_FAIL(unshare(CLONE_NEWNET))) + if (CHECK_FAIL(unshare_netns())) return false; /* Configure necessary links, routes */ - if (CHECK_FAIL(system("ip link set dev lo up"))) - return false; if (CHECK_FAIL(system("ip route add local default dev lo"))) return false; if (CHECK_FAIL(system("ip -6 route add local default dev lo"))) diff --git a/tools/testing/selftests/bpf/prog_tests/tcp_custom_syncookie.c b/tools/testing/selftests/bpf/prog_tests/tcp_custom_syncookie.c index eaf441dc7e79..07817d9c039c 100644 --- a/tools/testing/selftests/bpf/prog_tests/tcp_custom_syncookie.c +++ b/tools/testing/selftests/bpf/prog_tests/tcp_custom_syncookie.c @@ -32,12 +32,9 @@ static struct test_tcp_custom_syncookie_case { static int setup_netns(void) { - if (!ASSERT_OK(unshare(CLONE_NEWNET), "create netns")) + if (!ASSERT_OK(unshare_netns(), "create netns")) return -1; - if (!ASSERT_OK(system("ip link set dev lo up"), "ip")) - goto err; - if (!ASSERT_OK(write_sysctl("/proc/sys/net/ipv4/tcp_ecn", "1"), "write_sysctl")) goto err; 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: 13665632 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:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13665633 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 073DC15C3; Thu, 16 May 2024 01:13:59 +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=1715822040; cv=none; b=hNQ/NJRTfhrmI+8MKllm6iAiwyeuzS0zbF7iQ2Csq1cuoVacxWl0bYT2pbz/cighqlNgHxNzsXj+KsmRlGYiwe0CUMT6Aw/VCFmeOxrITzi5WS7Hi9zo4xAPtR7A+m4g0texG2rlJwnycx3+fjASAVBwgmjz0NdyMB8HpW0viHU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715822040; c=relaxed/simple; bh=Q8CrCR/sNC50qbiUi7YHDx9SVsY8RVE8vsth9qrUnu0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RJAtZCI4EjUXz9ifX91xDz/ED/5pm/Or6rbMvfs0uT1uuwZt7/TKA731EEcXws8QDpq86peXvsHLMpZ4Glmt6FWe9NUTvxZMUKQdcX3p3OjI3FUF8Rk9XAIep8KtD5+vAAfUaky6FfVpyk2MrG0sSipU0hEvT984s7ur3HoovMg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=aeIcOo0O; 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="aeIcOo0O" Received: by smtp.kernel.org (Postfix) with ESMTPSA id F2820C116B1; Thu, 16 May 2024 01:13:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715822039; bh=Q8CrCR/sNC50qbiUi7YHDx9SVsY8RVE8vsth9qrUnu0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aeIcOo0OduKM5WpyGBEGFby2wJbah8MEDd5p/I6ExGeepnCFZh93+ZVKBKe2/Si3J w9wgXJOz8UzU0uaabyGkWNR1uL4Oku6MDjeoJ3O6TazZc9BtwQwk40Fq2ppkGWQr44 1PUEumNZ8ArFWwDLMfKM19yB64FO0MPoGL9Bjy9gup3GaI6LfgG3SW4HjQVN9/yOpY nBrmb1k6MDumHCtnuTh358YRuxG5OIhjpiwPYr0ZK8UgqRDkEqlZsoZ12ewGOxgnnM sqdDkdXTWOoQ3WzG0aagnReYk1uxo4NSRBPXenZpgjMpbPdfZ7Wa1W/Nt8QdbT3cW8 AwIrB2R/Woenw== 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 4/8] selftests/bpf: Export create_netns helper Date: Thu, 16 May 2024 09:13:10 +0800 Message-ID: <035102a383c461fe64514e37fd7a9dcb946185ca.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 moves create_netns() from mptcp.c into network_helpers.c, and export it in network_helpers.h as a public helper. The new helper accepts a string parameter, and uses SYS_NOFAIL() instead of SYS() to execute the command, since SYS() deponds on test__fail() which is defined in test_progs.c. SYS() is not suitable to be used in network_helpers.c. Also using "ip netns del" command to delete netns before runing "ip netns add" to add it, since the next tests run may fail due to unable to create netns when last run tests interrupt. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 28 +++++++++++++++++++ tools/testing/selftests/bpf/network_helpers.h | 1 + .../testing/selftests/bpf/prog_tests/mptcp.c | 14 ++-------- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 919bb2a0c6a6..5555b109899f 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -498,6 +498,34 @@ void close_netns(struct nstoken *token) free(token); } +struct nstoken *create_netns(const char *name) +{ + struct nstoken *token = NULL; + + SYS_NOFAIL("ip netns del %s", name); + if (SYS_NOFAIL("ip netns add %s", name)) { + log_err("add netns %s failed", name); + goto fail; + } + + if (SYS_NOFAIL("ip -net %s link set dev lo up", name)) { + log_err("set dev lo up failed"); + goto fail; + } + + token = open_netns(name); + if (!token) { + log_err("open netns %s failed", name); + goto fail; + } + + return token; + +fail: + SYS_NOFAIL("ip netns del %s", name); + return NULL; +} + int get_socket_local_port(int sock_fd) { struct sockaddr_storage addr; diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index b087330f2861..5c0b082153fd 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -81,6 +81,7 @@ struct nstoken; */ struct nstoken *open_netns(const char *name); void close_netns(struct nstoken *token); +struct nstoken *create_netns(const char *name); int send_recv_data(int lfd, int fd, uint32_t total_bytes); int unshare_netns(void); diff --git a/tools/testing/selftests/bpf/prog_tests/mptcp.c b/tools/testing/selftests/bpf/prog_tests/mptcp.c index 4472aa404da0..abb4372114b6 100644 --- a/tools/testing/selftests/bpf/prog_tests/mptcp.c +++ b/tools/testing/selftests/bpf/prog_tests/mptcp.c @@ -79,16 +79,6 @@ struct mptcp_storage { char ca_name[TCP_CA_NAME_MAX]; }; -static struct nstoken *create_netns(void) -{ - SYS(fail, "ip netns add %s", NS_TEST); - SYS(fail, "ip -net %s link set dev lo up", NS_TEST); - - return open_netns(NS_TEST); -fail: - return NULL; -} - static void cleanup_netns(struct nstoken *nstoken) { if (nstoken) @@ -228,7 +218,7 @@ static void test_base(void) if (!ASSERT_GE(cgroup_fd, 0, "test__join_cgroup")) return; - nstoken = create_netns(); + nstoken = create_netns(NS_TEST); if (!ASSERT_OK_PTR(nstoken, "create_netns")) goto fail; @@ -344,7 +334,7 @@ static void test_mptcpify(void) if (!ASSERT_GE(cgroup_fd, 0, "test__join_cgroup")) return; - nstoken = create_netns(); + nstoken = create_netns(NS_TEST); if (!ASSERT_OK_PTR(nstoken, "create_netns")) goto fail; 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: 13665634 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:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13665635 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 A562F1366; Thu, 16 May 2024 01:14:13 +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=1715822053; cv=none; b=HatPA7XzFvQuz43/+sGcXSFWwEjAELU6tRzTlMP3zZHWGHYpa7ExlojOK+7yn6mmEmCcWKDV7SV/yzue3g4aqgQ3Ds3+SW8DBvq7cC0BdFtfjwltF9HJPr38t59FoJD/HJdP0ntFZRvXifU/RAxVazlSzFHFbvFrMucitLWlKhQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715822053; c=relaxed/simple; bh=sTsEXtYpJEks91ezB9zAnTDOubi6UMJkQfgV9scEPS0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AzBHU1NJjVviuE9IBXhNLgyO6dksWcFn8UAiVI+eLxRBWhLhxYooNUtCJqD4MzBYEigUGl4XzsPKWB4TZV5Ii3pu/TmFnJU+q8y9rOgtqcCYgW0uDerpl/MEG6Vb9d3B8FfduneUwXUQ6WcRu/Ddw3eG9E3/h0xZsCIg3naiV0E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=AqnRpEYu; 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="AqnRpEYu" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5EEFBC4AF0F; Thu, 16 May 2024 01:14:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715822053; bh=sTsEXtYpJEks91ezB9zAnTDOubi6UMJkQfgV9scEPS0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AqnRpEYuG3rHjsLM1rOjiJaMM93YMU3XE0hTNGeDIpjwguV0X5tcZQ3LbyvCDAzFb EDmdwUOIXRAyFLTysqcy0OHacjINqL4IVEcczK6fxHNKG13L1a4gfVZIZ6XxPJiniM dpSOZwJKav6+IIhQyIBrnjB7ppmHjZBmXWJsLE0nh+NTWjzG1Q9wDEEaTM4H3GT2nH Jp9BMYzcjTrM8fleDTCO6lx9Us5r1wlbd6LMKLmqRH3z3BUFIo5WTM82KmjFbPIyS1 lpBszEIT8XM9zRaMCsUp4q+wBhz7WCeYM9eZX/WiQlNRCSxiUFCHGP6HyCAyzri24a H5d8rEeaQBqoQ== 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 6/8] selftests/bpf: Export cleanup_netns helper Date: Thu, 16 May 2024 09:13:12 +0800 Message-ID: <480a636e822c9bc86e20add4ca20f58855ab6578.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 adds a new struct member name in struct nstoken, to save the name of the given network namespace. It dups the name string in open_netns() and freed in close_netns(). Then move cleanup_netns() from mptcp.c into network_helpers.c as a public helper. In it the newly added name field is passed to command "ip netns del" to delete this network namespace. This makes it more flexible. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 21 +++++++++++++++++++ tools/testing/selftests/bpf/network_helpers.h | 1 + .../testing/selftests/bpf/prog_tests/mptcp.c | 8 ------- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 5555b109899f..2293eac2dcec 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -444,6 +444,7 @@ char *ping_command(int family) struct nstoken { int orig_netns_fd; + char *name; }; struct nstoken *open_netns(const char *name) @@ -459,6 +460,13 @@ struct nstoken *open_netns(const char *name) return NULL; } + token->name = strdup(name); + if (!token->name) { + log_err("Failed to dup name"); + free(token); + return NULL; + } + token->orig_netns_fd = open("/proc/self/ns/net", O_RDONLY); if (token->orig_netns_fd == -1) { log_err("Failed to open(/proc/self/ns/net)"); @@ -483,6 +491,7 @@ struct nstoken *open_netns(const char *name) fail: if (token->orig_netns_fd != -1) close(token->orig_netns_fd); + free(token->name); free(token); return NULL; } @@ -494,6 +503,8 @@ void close_netns(struct nstoken *token) if (setns(token->orig_netns_fd, CLONE_NEWNET)) log_err("Failed to setns(orig_netns_fd)"); + if (token->name) + free(token->name); close(token->orig_netns_fd); free(token); } @@ -526,6 +537,16 @@ struct nstoken *create_netns(const char *name) return NULL; } +void cleanup_netns(struct nstoken *token) +{ + if (!token) + return; + + if (SYS_NOFAIL("ip netns del %s", token->name)) + log_err("del netns %s failed", token->name); + close_netns(token); +} + int get_socket_local_port(int sock_fd) { struct sockaddr_storage addr; diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 5c0b082153fd..42d503a9f55e 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); struct nstoken *create_netns(const char *name); +void cleanup_netns(struct nstoken *token); int send_recv_data(int lfd, int fd, uint32_t total_bytes); int unshare_netns(void); diff --git a/tools/testing/selftests/bpf/prog_tests/mptcp.c b/tools/testing/selftests/bpf/prog_tests/mptcp.c index abb4372114b6..c7f61fd9e7a5 100644 --- a/tools/testing/selftests/bpf/prog_tests/mptcp.c +++ b/tools/testing/selftests/bpf/prog_tests/mptcp.c @@ -79,14 +79,6 @@ struct mptcp_storage { char ca_name[TCP_CA_NAME_MAX]; }; -static void cleanup_netns(struct nstoken *nstoken) -{ - if (nstoken) - close_netns(nstoken); - - SYS_NOFAIL("ip netns del %s", NS_TEST); -} - static int start_mptcp_server(int family, const char *addr_str, __u16 port, int timeout_ms) { 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: 13665636 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); } From patchwork Thu May 16 01:13:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13665637 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 A3F091366; Thu, 16 May 2024 01:14:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715822065; cv=none; b=H3JZ8Lfsjan/K1V4kBzPQqqIMSHxpEAp5oah2V8cGxL7waayVntND0u+5XrIGzPuNA/G2c+7d7sf5bsV2lZh83nkrJBT7CsiQ0FGkBDp/RBA8dLenf8SEIs4zECdKgezxqVRgd4WU9St5YexU6MLlhfpLCl3TR6KbVgzK8PVFho= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715822065; c=relaxed/simple; bh=G2WA+IiJuJCE45LBBIyhxmHVn1AFRba13pD3Hykej9s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Q7oaD0xTqn09xtP6Ta48ltSwlmU7aDO+dE0dRK7t6GOVTlEcIicGLXxYJ3WnSEmPwjaAYB2QC8lSUk59SEp/vxE79DaE+JeP5bo9c5Y1vOr7veDPhV5kFLIu5NNhfszwJykTvoai2kUQwzEuM51N5qT2oBTiz5rTC6eBjtgH9Ss= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DhbMaN9G; 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="DhbMaN9G" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DCEC6C32786; Thu, 16 May 2024 01:14:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715822065; bh=G2WA+IiJuJCE45LBBIyhxmHVn1AFRba13pD3Hykej9s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DhbMaN9GFU9HZdxClw+ajXv7vVpBARXL6b0zCtdEMK4NDAGIrP1pOhU+tpX/MfG3A 7fXRSqt/ccgZzlyHbaS9MbAKlVv7z8z+Ml9D3VpYqWWgg3j2Ebzvu03qji12JJyslI mPNTgmC9tOamv6fVJ/gzcjCl9Ck5dvGUjiVW1wLfr5vxzhTX++//4htY5h3KATUC3p kw8+x5W+E/BHudNIt5v4YOkzWY7dYOdfWfH+nH1QwN+bHV/K/Ql7rwNo6CqVRsziYM gXiAEC/gSzW/DFq9YgnXkRZeJmE3aa9PaoxfZgWpYA2OOZwfFwA79oiSpYPOKCkoTC Z31GHs+SEGOuA== 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 8/8] selftests/bpf: Use netns helpers in lwt tests Date: Thu, 16 May 2024 09:13:14 +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 netns helpers create_netns() and cleanup_netns() in lwt_helpers.h instead of using the local function netns_create() and netns_delete(). For using these helpers. network_helpers.h needs to be included in lwt_helpers.h. Then '#include "network_helpers.h"' in lwt_redirect.c and lwt_reroute.c can be dropped. Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/lwt_helpers.h | 26 +++++-------------- .../selftests/bpf/prog_tests/lwt_redirect.c | 2 -- .../selftests/bpf/prog_tests/lwt_reroute.c | 2 -- 3 files changed, 7 insertions(+), 23 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/lwt_helpers.h b/tools/testing/selftests/bpf/prog_tests/lwt_helpers.h index fb1eb8c67361..602a268502e2 100644 --- a/tools/testing/selftests/bpf/prog_tests/lwt_helpers.h +++ b/tools/testing/selftests/bpf/prog_tests/lwt_helpers.h @@ -9,6 +9,7 @@ #include #include "test_progs.h" +#include "network_helpers.h" #define log_err(MSG, ...) \ fprintf(stderr, "(%s:%d: errno: %s) " MSG "\n", \ @@ -16,27 +17,14 @@ #define RUN_TEST(name) \ ({ \ - if (test__start_subtest(#name)) \ - if (ASSERT_OK(netns_create(), "netns_create")) { \ - struct nstoken *token = open_netns(NETNS); \ - if (ASSERT_OK_PTR(token, "setns")) { \ - test_ ## name(); \ - close_netns(token); \ - } \ - netns_delete(); \ - } \ + if (test__start_subtest(#name)) { \ + struct nstoken *token = create_netns(NETNS); \ + if (ASSERT_OK_PTR(token, "setns")) \ + test_ ## name(); \ + cleanup_netns(token); \ + } \ }) -static inline int netns_create(void) -{ - return system("ip netns add " NETNS); -} - -static inline int netns_delete(void) -{ - return system("ip netns del " NETNS ">/dev/null 2>&1"); -} - static int open_tuntap(const char *dev_name, bool need_mac) { int err = 0; diff --git a/tools/testing/selftests/bpf/prog_tests/lwt_redirect.c b/tools/testing/selftests/bpf/prog_tests/lwt_redirect.c index 835a1d756c16..70b80171f7f4 100644 --- a/tools/testing/selftests/bpf/prog_tests/lwt_redirect.c +++ b/tools/testing/selftests/bpf/prog_tests/lwt_redirect.c @@ -57,7 +57,6 @@ #define NETNS "ns_lwt_redirect" #include "lwt_helpers.h" #include "test_progs.h" -#include "network_helpers.h" #define BPF_OBJECT "test_lwt_redirect.bpf.o" #define INGRESS_SEC(need_mac) ((need_mac) ? "redir_ingress" : "redir_ingress_nomac") @@ -308,7 +307,6 @@ static void test_lwt_redirect_dev_carrier_down(void) static void *test_lwt_redirect_run(void *arg) { - netns_delete(); RUN_TEST(lwt_redirect_normal); RUN_TEST(lwt_redirect_normal_nomac); RUN_TEST(lwt_redirect_dev_down); diff --git a/tools/testing/selftests/bpf/prog_tests/lwt_reroute.c b/tools/testing/selftests/bpf/prog_tests/lwt_reroute.c index 03825d2b45a8..f51cbde7d8b3 100644 --- a/tools/testing/selftests/bpf/prog_tests/lwt_reroute.c +++ b/tools/testing/selftests/bpf/prog_tests/lwt_reroute.c @@ -50,7 +50,6 @@ */ #define NETNS "ns_lwt_reroute" #include "lwt_helpers.h" -#include "network_helpers.h" #include #define BPF_OBJECT "test_lwt_reroute.bpf.o" @@ -242,7 +241,6 @@ static void test_lwt_reroute_qdisc_dropped(void) static void *test_lwt_reroute_run(void *arg) { - netns_delete(); RUN_TEST(lwt_reroute_normal_xmit); RUN_TEST(lwt_reroute_qdisc_dropped); return NULL;