From d9658a006bacb4a289979d7693de341116e156e5 Mon Sep 17 00:00:00 2001 From: Geliang Tang Date: Sat, 11 May 2024 17:43:23 +0800 Subject: [PATCH] selftests/bpf: ns create selfetsts/bpf: Use ns helpers in assign_reuse selfetsts/bpf: Use ns helpers in test_tunnel selfetsts/bpf: Use ns helpers in ns_current_pid_tgid Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 25 ++++++++++++ tools/testing/selftests/bpf/network_helpers.h | 2 + .../selftests/bpf/prog_tests/assign_reuse.c | 12 ++---- .../selftests/bpf/prog_tests/crypto_sanity.c | 3 +- .../selftests/bpf/prog_tests/decap_sanity.c | 6 +-- .../selftests/bpf/prog_tests/fib_lookup.c | 8 +--- .../selftests/bpf/prog_tests/lwt_helpers.h | 26 ++++--------- .../selftests/bpf/prog_tests/lwt_redirect.c | 3 -- .../selftests/bpf/prog_tests/lwt_reroute.c | 2 - .../testing/selftests/bpf/prog_tests/mptcp.c | 38 +++++-------------- .../bpf/prog_tests/ns_current_pid_tgid.c | 9 +---- .../selftests/bpf/prog_tests/sock_destroy.c | 9 +---- .../bpf/prog_tests/sock_iter_batch.c | 9 +---- .../selftests/bpf/prog_tests/test_tunnel.c | 19 +++++----- .../bpf/prog_tests/xdp_dev_bound_only.c | 3 +- .../bpf/prog_tests/xdp_do_redirect.c | 4 +- 16 files changed, 69 insertions(+), 109 deletions(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 2eb8075678ebd..e7b136f22d1ee 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -683,3 +683,28 @@ int unshare_netns(void) return 0; } + +void del_netns(struct nstoken *token, const char *name) +{ + if (token) + close_netns(token); + + if (SYS_NOFAIL("ip netns del %s", name)) + log_err("del netns %s failed", name); +} + +struct nstoken *add_netns(const char *name) +{ + if (SYS_NOFAIL("ip netns add %s", name)) { + log_err("add netns %s failed", name); + return NULL; + } + + if (SYS_NOFAIL("ip -net %s link set dev lo up", name)) { + log_err("set dev lo up failed"); + SYS_NOFAIL("ip netns del %s", name); + return NULL; + } + + return open_netns(name); +} diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 3b7c427a2618a..8ca81d1f61dce 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -87,6 +87,8 @@ 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); +void del_netns(struct nstoken *token, const char *name); +struct nstoken *add_netns(const char *name); static __u16 csum_fold(__u32 csum) { diff --git a/tools/testing/selftests/bpf/prog_tests/assign_reuse.c b/tools/testing/selftests/bpf/prog_tests/assign_reuse.c index 989ee4d9785bb..413ecf3e8eec7 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 = add_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); @@ -192,8 +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); -out: - return; + del_netns(tok, NS_TEST); } diff --git a/tools/testing/selftests/bpf/prog_tests/crypto_sanity.c b/tools/testing/selftests/bpf/prog_tests/crypto_sanity.c index b1a3a49a822a7..c2741f2ac6d7a 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); + del_netns(nstoken, NS_TEST); 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 dcb9e5070cc3d..7d72cd7e186ed 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); + del_netns(nstoken, NS_TEST); 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 bd76589580041..c4d0e2a780609 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 = add_netns(NS_TEST); if (!ASSERT_OK_PTR(nstoken, "open_netns")) goto fail; @@ -370,8 +368,6 @@ void test_fib_lookup(void) } fail: - if (nstoken) - close_netns(nstoken); - SYS_NOFAIL("ip netns del " NS_TEST); + del_netns(nstoken, NS_TEST); fib_lookup__destroy(skel); } diff --git a/tools/testing/selftests/bpf/prog_tests/lwt_helpers.h b/tools/testing/selftests/bpf/prog_tests/lwt_helpers.h index ace212768f7bc..4d22de56774eb 100644 --- a/tools/testing/selftests/bpf/prog_tests/lwt_helpers.h +++ b/tools/testing/selftests/bpf/prog_tests/lwt_helpers.h @@ -9,30 +9,18 @@ #include #include "test_progs.h" +#include "network_helpers.h" #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 = add_netns(NETNS); \ + if (ASSERT_OK_PTR(token, "setns")) \ + test_ ## name(); \ + del_netns(token, NETNS); \ + } \ }) -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 835a1d756c166..e5629659e12af 100644 --- a/tools/testing/selftests/bpf/prog_tests/lwt_redirect.c +++ b/tools/testing/selftests/bpf/prog_tests/lwt_redirect.c @@ -56,8 +56,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 +306,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 03825d2b45a8b..f51cbde7d8b30 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; diff --git a/tools/testing/selftests/bpf/prog_tests/mptcp.c b/tools/testing/selftests/bpf/prog_tests/mptcp.c index f7e4ce84c0aef..257019e2b81d8 100644 --- a/tools/testing/selftests/bpf/prog_tests/mptcp.c +++ b/tools/testing/selftests/bpf/prog_tests/mptcp.c @@ -80,24 +80,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) - 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) { @@ -229,7 +211,7 @@ static void test_base(void) if (!ASSERT_GE(cgroup_fd, 0, "test__join_cgroup")) return; - nstoken = create_netns(); + nstoken = add_netns(NS_TEST); if (!ASSERT_OK_PTR(nstoken, "create_netns")) goto fail; @@ -253,7 +235,7 @@ static void test_base(void) close(server_fd); fail: - cleanup_netns(nstoken); + del_netns(nstoken, NS_TEST); close(cgroup_fd); } @@ -345,14 +327,14 @@ static void test_mptcpify(void) if (!ASSERT_GE(cgroup_fd, 0, "test__join_cgroup")) return; - nstoken = create_netns(); + nstoken = add_netns(NS_TEST); if (!ASSERT_OK_PTR(nstoken, "create_netns")) goto fail; ASSERT_OK(run_mptcpify(cgroup_fd), "run_mptcpify"); fail: - cleanup_netns(nstoken); + del_netns(nstoken, NS_TEST); close(cgroup_fd); } @@ -437,7 +419,7 @@ static void test_subflow(void) if (!ASSERT_OK(err, "prog_attach")) goto skel_destroy; - nstoken = create_netns(); + nstoken = add_netns(NS_TEST); if (!ASSERT_OK_PTR(nstoken, "create_netns: mptcp_subflow")) goto skel_destroy; @@ -447,7 +429,7 @@ static void test_subflow(void) run_subflow(skel->data->cc); close_netns: - cleanup_netns(nstoken); + del_netns(nstoken, NS_TEST); skel_destroy: mptcp_subflow__destroy(skel); close_cgroup: @@ -458,7 +440,7 @@ static struct nstoken *sched_init(char *flags, char *sched) { struct nstoken *nstoken; - nstoken = create_netns(); + nstoken = add_netns(NS_TEST); if (!ASSERT_OK_PTR(nstoken, "create_netns")) return NULL; @@ -469,7 +451,7 @@ static struct nstoken *sched_init(char *flags, char *sched) return nstoken; fail: - cleanup_netns(nstoken); + del_netns(nstoken, NS_TEST); return NULL; } @@ -530,7 +512,7 @@ static void test_default(void) send_data_and_verify("default", WITH_DATA, WITH_DATA); fail: - cleanup_netns(nstoken); + del_netns(nstoken, NS_TEST); } static void test_bpf_sched(struct bpf_object *obj, char *sched, @@ -557,7 +539,7 @@ static void test_bpf_sched(struct bpf_object *obj, char *sched, send_data_and_verify(sched, addr1, addr2); fail: - cleanup_netns(nstoken); + del_netns(nstoken, NS_TEST); bpf_link__destroy(link); } 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 e72d75d6baa71..e85383b2996c4 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,19 +204,14 @@ 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 = add_netns("ns_current_pid_tgid"); if (!ASSERT_OK_PTR(nstoken, "open_netns")) goto cleanup; test_ns_current_pid_tgid_new_ns(fn, arg); cleanup: - if (nstoken) - close_netns(nstoken); - SYS_NOFAIL("ip netns del ns_current_pid_tgid"); + del_netns(nstoken, "ns_current_pid_tgid"); } /* 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 9c11938fe597d..0918241cbab6b 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 = add_netns(TEST_NS); if (!ASSERT_OK_PTR(nstoken, "open_netns")) goto cleanup; @@ -212,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); + del_netns(nstoken, TEST_NS); 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 d56e18b255280..7e8b88cffd56a 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 = add_netns(TEST_NS); if (!ASSERT_OK_PTR(nstoken, "open_netns")) goto done; @@ -128,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); + del_netns(nstoken, TEST_NS); } diff --git a/tools/testing/selftests/bpf/prog_tests/test_tunnel.c b/tools/testing/selftests/bpf/prog_tests/test_tunnel.c index cec746e77cd3a..d5076835514ad 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 = add_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); + del_netns(nstoken, "at_ns0"); /* 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 = add_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); + del_netns(nstoken, "at_ns0"); /* 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 = add_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); + del_netns(nstoken, "at_ns0"); 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 = add_netns("at_ns0"); if (!ASSERT_OK_PTR(nstoken, "setns")) goto done; err = test_ping(AF_INET, IP4_ADDR_TUNL_DEV1); - close_netns(nstoken); + del_netns(nstoken, "at_ns0"); if (!ASSERT_OK(err, "test_ping")) goto done; 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 7dd18c6d06c60..3f90d4ea940c7 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); + del_netns(tok, LOCAL_NETNS); } 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 498d3bdaa4b0b..7cb00e50b2874 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"); + del_netns(nstoken, "testns"); test_xdp_do_redirect__destroy(skel); }