From patchwork Wed May 15 05:59:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 797138 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 216303BBF4; Wed, 15 May 2024 06:00:14 +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=1715752815; cv=none; b=tc18ox/Rf+kXJN3O3aknypcSgIP7BrI6wuduyhORMErK/36jSwnO55X6vQRDiODWgbdyn9LV6pQCFlo6v0f2t0OTLILwzhO7bTgOJO8HmXwvi3wSyrYG/rhOk+2HFYpONOSL3HB4FuFSc42leSnF19CiRtMymp6Q8BDYdnCfO2M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715752815; c=relaxed/simple; bh=PYLreC9y69ftkHES1PiYml+GPmE1yXRehjsAcxtM0yE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=moGFNY4wjnyg4w3+jJdPBfxnUfWJOfjXFmS6Yi0npNZFHq13L4p2BS9OyBHH5p5+PA9bosCtlNN7llwVYr5y6TkoMb7lq/PWfnVZslGnxyOdJLYOv5QFSCW5EzJ/tG696T43XEe94nH7yQQDMbm4nsuMPGlFcsSGkno1A6XEy+I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Ko+jMzfG; 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="Ko+jMzfG" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9A633C2BD11; Wed, 15 May 2024 06:00:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715752814; bh=PYLreC9y69ftkHES1PiYml+GPmE1yXRehjsAcxtM0yE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ko+jMzfGVy07o73PO/KxNjgPETIRkKhaUUXGDbKSgD1LP03Duu7OEfpEZIM8YVS5X fPTuxjCmuO9MYqBwDvmGNwueWsGtg3ehzANIQOojZfCKkVx7RbVABjChI+vfLi3nKB DUOvnS+fOjO6cnGP2+RaL0tVTxeHBri+2cYQ2Z01QtojDN4YHMYRVor5murUW5Tx2r NxDxg8eusl/EvZTZtyC8eX6jr5MAJIQsIZzbGqCBFTaHNwgI009XlMwv9rPPaX9mEu tpzeSr6tKHoRpc2XS2CxwMrWlaBHBiN2/GzPRLF/ywOBommQ4quo87BQw0bv0DUubj oVkC54l/cbFiw== 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, Geliang Tang Subject: [PATCH bpf-next 1/9] selftests/bpf: Add unshare_netns helper Date: Wed, 15 May 2024 13:59:28 +0800 Message-ID: <50d0c331d3418c4dcde72bfbce45196c6806c9cc.1715751995.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 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 Wed May 15 05:59:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 797389 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 2734E3C485; Wed, 15 May 2024 06:00: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=1715752821; cv=none; b=KvnAzkGHxONV7eu8y6KrcOHCKGHVFjQInp4/I+gdkJbQaG0oR9lPGZDWsCwMDXRLgrDMOm+U52QNWb6FBYId0YPEpUWYd/eGZMjDS4nNCTrKWs9Yhw1F4wmFT+Sqg+lOq5dswaUO0VMzi2WrPgtatNmgVMxEozVgvBbs2NEcwqM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715752821; c=relaxed/simple; bh=9RIzon2SQFRluCkM4WuVpqfZM7QKLJta1nv2UWTfiXM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FdIbJUDmdLFfFo+g/1yADnu8VGsmDodx2kU38OQZAgoDIoocVXMLeXWSFqvNm+4dUUhZPuqsRfkH+2sT0kF21kAaW0TAhYbmt7HQ3mIgvLhLPoQgT9RQvEnRe3wLqFcMGmNKyGCJYQuuQBAWy8inVvJ9Adwa4Z9WYzhU91y5GKs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Yp8dyDSx; 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="Yp8dyDSx" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 01AC9C116B1; Wed, 15 May 2024 06:00:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715752821; bh=9RIzon2SQFRluCkM4WuVpqfZM7QKLJta1nv2UWTfiXM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Yp8dyDSxZAmuZMQXPWYut6khNpgyV5wwdXWIgiP75jpxt/TKy5TpyCsogimqq15EG pJ080tfzyksAJTVRJueixlX6DZjKEcj0vjf4LgtxYABhiFecrxQDQzAFNfLqTnO2Eg OgQkmX06E7Y5u0k1JNYZzznvltVe1nrQFe7ZVo5WWvN0zUTs2sG0VyEDSXE0knUa4X hEirg7DVFyB1mskjoZ023H+nalXMr0jWWErEh7YEzceM7OB+Qxj+DCsbBxdpe1tzjI d7V/yQrdb2As485Xg1fFw65VZe9LXy6a72RJMu1mZi/SeUsrEB1tUQJ27Tcd4yb/9a KdT8bFbbMXAvQ== 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, Geliang Tang Subject: [PATCH bpf-next 2/9] selftests/bpf: Use unshare_netns helper Date: Wed, 15 May 2024 13:59:29 +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 Wed May 15 05:59:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 797137 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 33E383D388; Wed, 15 May 2024 06:00: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=1715752829; cv=none; b=WSI4LITgaKLMnuzNkFIrPMHqIv3b9+/Upte79DI/Q7QvDn7u9JjqwLETPjfPHoEb1X3HQr16IT/0HRB8ZB7fPVMMhthEPAUHn+3ADdED97M/kBmbICYJL017DBa/dDlNfml/QOVZo+yDGUWz0iNaL6hvvmHc92C/jPFOoco75Pc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715752829; c=relaxed/simple; bh=tbrFM8SkYDvnxIQcgUhbjrMR9nLEahNNCk3IKrv15cs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=D+/qrNoZ1ZOjPQiazgNEPOMSyY3V6HawuHc/3cbbW94vCDcE91+u50toOzvTZ8bvaTMGUc/O8e4jZ7fDONrbH14UmKh4Rl5WSVOvyCcCdNEn0AAXzLoLcU8RhOA6B1zJ2TkLKSMRC7zDR4V0B5L+5PlfXOBuHawu2ATrhwlSyIE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=m5MW5drk; 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="m5MW5drk" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0FB02C32782; Wed, 15 May 2024 06:00:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715752828; bh=tbrFM8SkYDvnxIQcgUhbjrMR9nLEahNNCk3IKrv15cs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=m5MW5drkonn2FzeumrfkBGlA7DV+wnahgX3rBmVIpQo87ufQ2/bVfPbrA+nYOgIgH nBdWGStA/9ObBJ3Om9E8p8Q15aO48VwmOONgfHkV8+XPjMYM599Yk7OdSGTXm3sWyv NcjU8rg1EpEMi6SwbPTxLuzOnWulSbpzBboy+TwLDUTpyj6bR1a/b4ieEJK1k0vceC NzmsK7KKrbOQzblDHuNU+IIf/pvQIhgKtD9Q/L0W0NmkR8XpS2MBwtIj238hZR1p5g dGrhuS5h5sOIfRB3CUNkUXEijFdCQqpzEH9DzNFLJEHhv55phkSR7VUubD7BJGafkH 1c6CieMY4eptw== 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, Geliang Tang Subject: [PATCH bpf-next 3/9] selftests/bpf: Drop duplicate create_netns Date: Wed, 15 May 2024 13:59:30 +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 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 Wed May 15 05:59:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 797388 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 301993D388; Wed, 15 May 2024 06:00: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=1715752836; cv=none; b=I3p5HkIvvjTfx1fBKgQTzR0CEFZEwf4ZkhNATA9NMk8cYIQ6jH0k9vWFI96u9j398k8xJmVNfDRJVQ9soRjdLdktxfLWD8xdyFZXXtPx7nOzNDjfi/Tn8jf5+16I2CgsE5pqq7oitHdFI8SmhITKA1b9dH8tpQbropKN/byvwqs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715752836; c=relaxed/simple; bh=buIc9dAsozt/z4H3V4yyy1DAHtlN4MAh/XNT1Hm9sYs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NfGTvw+N5KIUdhNSEfahNRbD8GCK/e+0QGKRA0pP8rMZHcxHoWjSAx9x+gQMTYPuupq43oIUjU2MJsSm0I7fh/ZKnaegGQHIctuH5YGivjZHreTSavxnZTY11caRAaBpIXPQTKcyN1g0ham1N9J74UXrBEh9h0U+7rnU32orbrk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=OmARngpp; 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="OmARngpp" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 68997C116B1; Wed, 15 May 2024 06:00:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715752835; bh=buIc9dAsozt/z4H3V4yyy1DAHtlN4MAh/XNT1Hm9sYs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OmARngppkh5iEJFbyMUIG/u7d9jncYY3n1caTGOSrDEhcixobx9UT0hv7dabeOocP c+fBmtwByk2m3vEmAstfJZk85soGCfCLO3hiNzEOhrGmWlaUGtya3Xw8Sv+o/Ns5AD QsvNvxILzQThEaLGyCRmRXjQj00+QrGX0WPLRq/BYDsWZYHdfKPmZPKCYK8G/8Xptl woTMiQI1LW9WcPEsPq9qAuhy4i87wzzyAfjGXxpcRUp6pp+//PiVB8V3WXrbh80jBV FBgARhozRtDC1r2zeSAADFxV446CFMDwFM/iQRiDR4vmBZvjreXjtsObl1T1y/Ka0h 0t9g4SrclvBcA== 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, Geliang Tang Subject: [PATCH bpf-next 4/9] selftests/bpf: Export create_netns helper Date: Wed, 15 May 2024 13:59:31 +0800 Message-ID: <659d1fe7e47b6c63f9648cf7c2433e29dadf458f.1715751995.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. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 27 +++++++++++++++++++ tools/testing/selftests/bpf/network_helpers.h | 1 + .../testing/selftests/bpf/prog_tests/mptcp.c | 14 ++-------- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 919bb2a0c6a6..c115aeca0a66 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -498,6 +498,33 @@ void close_netns(struct nstoken *token) free(token); } +struct nstoken *create_netns(const char *name) +{ + struct nstoken *token = NULL; + + 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 793b4b9c2bd2..ae42ae41f4b4 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 Wed May 15 05:59:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 797136 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 C0E3D3C485; Wed, 15 May 2024 06:00:43 +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=1715752843; cv=none; b=iqp90xFlXPNky5eBRfn9Kyw8aJOmXEUD3H+E9wF90dt9lAU2gueb5oCXoV+tsiNGhcQy36WCf7uXEo4nIwLCoaxXfv5ZXgmkhV1uVAXjmvb/MR09/ACSHjGttPUzYqp4yGY1XHJC2wN+HEpHuTta0t2bRHh140ZI9y27aBPSihs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715752843; c=relaxed/simple; bh=nldUSDpla7riCahA4LY3CS7ej4UNHCzwLWbb6fr1yh4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=C24fzB2d2gOP2TJmdZr9RLnpAwaw+SLM2zXafA729XwIkaYSPQDFS2+bdyGREA6FE46wBZmtzq8D30WpSkZSVpjsY2OOTEokXUA8/npVp2P6Leid1hx6pZizGtABSM7yxEcpgltVi9102iscLdQhsuE/C/eOLqjOakZ9VPHaBzQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=mph49uLB; 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="mph49uLB" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 55732C4AF09; Wed, 15 May 2024 06:00:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715752843; bh=nldUSDpla7riCahA4LY3CS7ej4UNHCzwLWbb6fr1yh4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mph49uLB2V/3pmuv8rGdEr2XZWaBSniab8bljQeWjWvKvoaplSgXfblYYC4E+Uwpy Htv+6iYUp0A+Nz8JfXnqrABZscfQbmoy8OO7XkcLvHWFPsJNdi08gSDonM3j2aDq2y V2dEUnP3idcM7uNRe34AwXS2dU07WqH7WsDDWddZVv9d84190BKD/iO/0oGGRt65Dm AsBGfnBciuU/CC/mBinfIBD3gj+acj52NjsnRchdz3Ap0NpjIZ4xjwv0ku4F8qEEoM LNV0G9cyXwS+R4/RIV1dSRuywbp7nXfZC5ivGjLvJEGCdoYf8eVvf9MgjRW1rutMfI luPxNVPqzWEWA== 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, Geliang Tang Subject: [PATCH bpf-next 5/9] selftests/bpf: Use create_netns helper Date: Wed, 15 May 2024 13:59:32 +0800 Message-ID: <8927853212a8bfedc303452f450ee7a0cf520d99.1715751995.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 | 4 +--- 5 files changed, 6 insertions(+), 21 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..6a19bfdc0677 100644 --- a/tools/testing/selftests/bpf/prog_tests/sock_iter_batch.c +++ b/tools/testing/selftests/bpf/prog_tests/sock_iter_batch.c @@ -113,10 +113,8 @@ 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 Wed May 15 05:59:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 797387 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 635C83C485; Wed, 15 May 2024 06:00:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715752850; cv=none; b=RDRtZYMDjeA/HMrkCT7lq48nk5M+mDKS1F9+fzX9NLP9zrNquUnSTOOCILwKThiBGkylQsYEw91QAig4nc69QxuzUp6xGgKgBKOsh2T9ShMP4tQHyBvhdKuM/VOXYHf7chxzMpA2Epdg0cYjahbi7e2LTsp6GY3TokgrLx2c/Lc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715752850; c=relaxed/simple; bh=gFtH4vnhvrfgIng/oPWR939UW+4mcOZR1dqwqPB1/OI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=k/tZYWMxG22O1fWzYOH6ih7OeMTvWyqqYiJzBkneWDGZP9b2KlcqfLf/Sen5D00Ps8xnySpBuoUbZUopU5N0lrnTErYXRXHV3krVks+R8zupQJLf5KH5OGBZzkxd+UngzSfqWxVgj8vUWlA8Za3WdoP3/4aHJuII2W0FgWgfHK4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=SUvxgOyL; 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="SUvxgOyL" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E165EC2BD11; Wed, 15 May 2024 06:00:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715752850; bh=gFtH4vnhvrfgIng/oPWR939UW+4mcOZR1dqwqPB1/OI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SUvxgOyLLHPR+caBSyHmck15JG/v5z52QqNDZ5fH0b3kjMCegUH2U6Z7geIdCWQ0Y ZLUw0mD7HW61egojkquUrsP2TLg6VExE8MeAEk8QNoXRNB+AOX9bUyMZULcoRCV5nQ RUlQ0JhDMFQVOPRuvjW7AZvQ+So0/FPBKJluoxoOXkdHAxm8SgzzJM8MKieTyxbiI4 DHtvlAg2thTuDR852Xg3zgBIwccsVjEhOy9N5Ag+Qy3bJMXEOMQSB3EnrT/BSB7MeM eZ4c+znMFM181OWG0OjYUIywNs01DZYkBcU/IaRd/P/FMs0JMRWXWdxy+Xnznn0QS/ DFTqo1mNFMKPw== 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, Geliang Tang Subject: [PATCH bpf-next 6/9] selftests/bpf: Export cleanup_netns helper Date: Wed, 15 May 2024 13:59:33 +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 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 c115aeca0a66..0b25b008f4f6 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,10 +503,22 @@ 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); } +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); +} + struct nstoken *create_netns(const char *name) { struct nstoken *token = NULL; diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 5c0b082153fd..7ed6d78b3744 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); +void cleanup_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 ae42ae41f4b4..11267f987e7e 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 Wed May 15 05:59:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 797135 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 152093D388; Wed, 15 May 2024 06:00:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715752858; cv=none; b=Y3gkyzhHIU82/AC8MfFaxS9GVY4eDdme6sNNHfPdlog0hRsTgygI+gduoAmBD+WFaaSC+K+3GW6lhjndYqqPUhpXbwmIIxyAeCZod3UGOJ4WbveDWSWQwl371kb2b3QGStslcdQbVbtdZq/cd3uSimzQTAyFKrvkN6QDFSMlaj0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715752858; c=relaxed/simple; bh=ahPd+Unc79ctEtQ8nwVSV0Z1os6ivAaHf/XZcrXhMxM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OSuSqAv4uEeL44XxJFrrL4cMrYZ0jblpVLmI79Gqpi0XEhNmOGXHTkc8E+8XOB1/EgQJLKiTGYNDIENw876G1JfKHn317ILfYuqgIF6KES6A6+UK55y03skHYJ/DNAu9LamTxcTWQUQefdO8V0o9hVeKJnKf5Anf78oatGazM50= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bCCPXEnQ; 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="bCCPXEnQ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E408AC4AF0F; Wed, 15 May 2024 06:00:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715752857; bh=ahPd+Unc79ctEtQ8nwVSV0Z1os6ivAaHf/XZcrXhMxM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bCCPXEnQXF+3hlJRjcBHccyPlHGtnz6w/jmU8OMuNtcY/zC1LX7jalKzFQJBJLg94 Ks0s+/nJoGkDU8UPcLRvyxxgRPsfhQRWMhGsPTAFBZEE6DiwmdKkEMf93W8LO/9KeH EyZZwS8Q5vG8ihe71zRbiWGO37XutIEyNnSYM+ii9B5W2KGCYYraUCLy4J2VkGeAkv x/wI8ldkLChVBS4B5WLf6iRMxAvQ1gfwG16h1Ykn4vz5e97cOlJO/nAxlvQGIgNdk1 31jrrbh3r6cDxQcRaADU4OWnzaUIuNaQ1Y13K1xzsJNikUaOAgni9iiWslE5pAqN4N K3LeDiKPb5nTw== 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, Geliang Tang Subject: [PATCH bpf-next 7/9] selftests/bpf: Use cleanup_netns helper Date: Wed, 15 May 2024 13:59:34 +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 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 6a19bfdc0677..786cf316b3fd 100644 --- a/tools/testing/selftests/bpf/prog_tests/sock_iter_batch.c +++ b/tools/testing/selftests/bpf/prog_tests/sock_iter_batch.c @@ -126,8 +126,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 498d3bdaa4b0..7ae93bbdbf82 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 Wed May 15 05:59:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 797386 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 B405540C03; Wed, 15 May 2024 06:01:06 +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=1715752866; cv=none; b=ALFeLOOyf/ddHzOLtJOdCy6Sqr8omGzDGLR8+Cj4rR/ekUj+DRL7hcW+Lhk6mCZBMmNeYBjseGsa6aTKjs51QTvqNK0cO8pVoLhNApi3F72lngyhjbw+e42oPtjU8IJPMCMwBB2HKaZtVO+PXpHP1SmBNFaJJr5S6PwLKtelX78= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715752866; c=relaxed/simple; bh=G2WA+IiJuJCE45LBBIyhxmHVn1AFRba13pD3Hykej9s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GqAIL8LjN2zGmJdFh2Q90MebxWZq+VlguIgDBxwsWpBoktE8BLkJuw6rnQBAdqgQIUXvOhnHn6DSPATop/vwCeFYs89YQsZqvc1jIXLivKpScp6Y3hCIdYheXaMGM0jKlgETXSr8ghsDDP0erliBB0EuCzOSJuAKq8ZzoFQTgaI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=fSiHs2Jw; 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="fSiHs2Jw" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 42F81C116B1; Wed, 15 May 2024 06:00:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715752866; bh=G2WA+IiJuJCE45LBBIyhxmHVn1AFRba13pD3Hykej9s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fSiHs2JwVX4TODhgplxbC+vHukL9aEg7wYIQDH5tgAvJ554+lAesZK3sTZmiGCZZ2 DUnQR0uB3HPX5IA4RBZWlILMoLHVYiGKCoxUdbr9IidHh2BNv5s8Vjf6VNJ1DFBcCt 3CvKA1ptqWqvIflHLYzBHPKZDng5I74D6FRUGGNQ9bKwGM1dvqpL0D3JyQzozF24F/ WAoDrBaveO74yFAB0wDoPapSkoqZuobYZilW5P3GUufYe2vm1y6PhEIIJqVfV1yon0 eMVPWkceOncJbIds+ULg6vBZ2tkJzFxsJhM4xncMPE2yoTzkvEwkUnBHxjJuzq6wDM fEtPtgvI3/L9Q== 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, Geliang Tang Subject: [PATCH bpf-next 8/9] selftests/bpf: Use netns helpers in lwt tests Date: Wed, 15 May 2024 13:59:35 +0800 Message-ID: <92f1fe99018b50a92fedd7ce68abcb744f49d1cf.1715751995.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 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; From patchwork Wed May 15 05:59:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 797134 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 E55AB40862; Wed, 15 May 2024 06:01: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=1715752874; cv=none; b=kKVhlqPA5XGO2v/EXE58SKaONM3dMk62cE+A/gTl/xizD1BBBYAI+PfBNP01xCOrY8tWOmvfzspCPJnHsd9Q76v2VXwOrGREGBMPP2FFMQEHqCJPHB0cvBb6epSleBz5K2sWH9ktANZ8doM70accvjuYwgkrxCdSSyIA2wdlWkk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715752874; c=relaxed/simple; bh=v2VRj9lBoAF6iF1QM9Z49yG7Oe8T0ukmCgaWUngLrP4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nzWb/4Tj2V7STHu9lgenLatVTFBcANVy/SCG2UwKldJXktT5rJFS3Zx1sUxrtufZmAjtqUXYFhrTUM2zRmvGmv46dErtKeqzNz+5L1YFcPt2eknZU+esq0sV2kFaIWRmfPBg3x8DBoyLOCZaghXfO+PllMxIjYB650/KzcD2KFk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=j6QklQBE; 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="j6QklQBE" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 04B2DC2BD11; Wed, 15 May 2024 06:01:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715752873; bh=v2VRj9lBoAF6iF1QM9Z49yG7Oe8T0ukmCgaWUngLrP4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=j6QklQBEq8iY/45up63lz7ShI5e+HxEt11Xxnc0V203YrEPQGqghBLRJsSHEpcG0R ipALNF2oWIpkEiGtugglhAHSOQaDyL/yI4cMjgI0+cstsMe7HEzZhBhUpruT26hyBt lFFjO7ynWQjohFoZOhDOXl1mnKgTctXpI7aKlgCGE8dT/31niGE8GkNRygOo9Dkzji ecTRy2Le9JhNAv05GyquUMRo4lQ61CwJCGUtftMCIbbZMj22LIZU3Rge4N8PcQc2vJ mQcTIfoqyUO7E+U4fbddLmqXrUckrjW3+LVfUE4e4uu1S5NN40kEm2xovg1eW+O9ok 5eppEYPq/SJQg== 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, Geliang Tang Subject: [PATCH bpf-next 9/9] selftests/bpf: Use netns helpers in test_tunnel Date: Wed, 15 May 2024 13:59:36 +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 test_tunnel.c instead of using open_netns() and close_netns() directly. Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/test_tunnel.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/test_tunnel.c b/tools/testing/selftests/bpf/prog_tests/test_tunnel.c index cec746e77cd3..6706ee1cb36d 100644 --- a/tools/testing/selftests/bpf/prog_tests/test_tunnel.c +++ b/tools/testing/selftests/bpf/prog_tests/test_tunnel.c @@ -102,7 +102,6 @@ static int config_device(void) { - SYS(fail, "ip netns add at_ns0"); SYS(fail, "ip link add veth0 address " MAC_VETH1 " type veth peer name veth1"); SYS(fail, "ip link set veth0 netns at_ns0"); SYS(fail, "ip addr add " IP4_ADDR1_VETH1 "/24 dev veth1"); @@ -117,7 +116,7 @@ static int config_device(void) static void cleanup(void) { - SYS_NOFAIL("test -f /var/run/netns/at_ns0 && ip netns delete at_ns0"); + SYS_NOFAIL("test -f /var/run/netns/at_ns0"); SYS_NOFAIL("ip link del veth1"); SYS_NOFAIL("ip link del %s", VXLAN_TUNL_DEV1); SYS_NOFAIL("ip link del %s", IP6VXLAN_TUNL_DEV1); @@ -444,7 +443,7 @@ static void test_vxlan_tunnel(void) goto done; /* load and attach prog set_md to tunnel dev tc hook point at_ns0 */ - nstoken = open_netns("at_ns0"); + nstoken = create_netns("at_ns0"); if (!ASSERT_OK_PTR(nstoken, "setns src")) goto done; ifindex = if_nametoindex(VXLAN_TUNL_DEV0); @@ -456,7 +455,7 @@ static void test_vxlan_tunnel(void) goto done; if (attach_tc_prog(&tc_hook, -1, set_dst_prog_fd)) goto done; - close_netns(nstoken); + cleanup_netns(nstoken); /* use veth1 ip 2 as tunnel source ip */ local_ip_map_fd = bpf_map__fd(skel->maps.local_ip_map); @@ -517,7 +516,7 @@ static void test_ip6vxlan_tunnel(void) goto done; /* load and attach prog set_md to tunnel dev tc hook point at_ns0 */ - nstoken = open_netns("at_ns0"); + nstoken = create_netns("at_ns0"); if (!ASSERT_OK_PTR(nstoken, "setns src")) goto done; ifindex = if_nametoindex(IP6VXLAN_TUNL_DEV0); @@ -529,7 +528,7 @@ static void test_ip6vxlan_tunnel(void) goto done; if (attach_tc_prog(&tc_hook, -1, set_dst_prog_fd)) goto done; - close_netns(nstoken); + cleanup_netns(nstoken); /* use veth1 ip 2 as tunnel source ip */ local_ip_map_fd = bpf_map__fd(skel->maps.local_ip_map); @@ -611,13 +610,13 @@ static void test_ipip_tunnel(enum ipip_encap encap) goto done; /* ping from at_ns0 namespace test */ - nstoken = open_netns("at_ns0"); + nstoken = create_netns("at_ns0"); if (!ASSERT_OK_PTR(nstoken, "setns")) goto done; err = test_ping(AF_INET, IP4_ADDR_TUNL_DEV1); if (!ASSERT_OK(err, "test_ping")) goto done; - close_netns(nstoken); + cleanup_netns(nstoken); done: /* delete ipip tunnel */ @@ -667,11 +666,11 @@ static void test_xfrm_tunnel(void) goto done; /* ping from at_ns0 namespace test */ - nstoken = open_netns("at_ns0"); + nstoken = create_netns("at_ns0"); if (!ASSERT_OK_PTR(nstoken, "setns")) goto done; err = test_ping(AF_INET, IP4_ADDR_TUNL_DEV1); - close_netns(nstoken); + cleanup_netns(nstoken); if (!ASSERT_OK(err, "test_ping")) goto done;