All the tests use the loopback interface. It prevents from running them in parallel. Use the create_and_open_tid_ns() helper to run each test in its own network namespace. Signed-off-by: Bastien Curutchet (eBPF Foundation) <bastien.curutchet@xxxxxxxxxxx> --- tools/testing/selftests/bpf/prog_tests/tc_links.c | 124 +++++++++++++++ tools/testing/selftests/bpf/prog_tests/tc_opts.c | 180 ++++++++++++++++++++++ 2 files changed, 304 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/tc_links.c b/tools/testing/selftests/bpf/prog_tests/tc_links.c index 1af9ec1149aab6ca5ef6986fc16a650162266966..07a2fc3d44d03e18dd3c715b0b26c7f1ac6d47cb 100644 --- a/tools/testing/selftests/bpf/prog_tests/tc_links.c +++ b/tools/testing/selftests/bpf/prog_tests/tc_links.c @@ -7,6 +7,8 @@ #define loopback 1 #define ping_cmd "ping -q -c1 -w1 127.0.0.1 > /dev/null" +#define NS_NAME_MAX_LEN 32 +#define NS_NAME "ns-tc-link-" #include "test_tc_link.skel.h" @@ -17,12 +19,18 @@ void serial_test_tc_links_basic(void) { LIBBPF_OPTS(bpf_prog_query_opts, optq); LIBBPF_OPTS(bpf_tcx_opts, optl); + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; __u32 prog_ids[2], link_ids[2]; __u32 pid1, pid2, lid1, lid2; struct test_tc_link *skel; struct bpf_link *link; + struct netns_obj *ns; int err; + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + skel = test_tc_link__open_and_load(); if (!ASSERT_OK_PTR(skel, "skel_load")) goto cleanup; @@ -110,6 +118,7 @@ void serial_test_tc_links_basic(void) assert_mprog_count(BPF_TCX_INGRESS, 0); assert_mprog_count(BPF_TCX_EGRESS, 0); + netns_free(ns); } static void test_tc_links_before_target(int target) @@ -262,8 +271,17 @@ static void test_tc_links_before_target(int target) void serial_test_tc_links_before(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_links_before_target(BPF_TCX_INGRESS); test_tc_links_before_target(BPF_TCX_EGRESS); + + netns_free(ns); } static void test_tc_links_after_target(int target) @@ -416,8 +434,17 @@ static void test_tc_links_after_target(int target) void serial_test_tc_links_after(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_links_after_target(BPF_TCX_INGRESS); test_tc_links_after_target(BPF_TCX_EGRESS); + + netns_free(ns); } static void test_tc_links_revision_target(int target) @@ -516,8 +543,17 @@ static void test_tc_links_revision_target(int target) void serial_test_tc_links_revision(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_links_revision_target(BPF_TCX_INGRESS); test_tc_links_revision_target(BPF_TCX_EGRESS); + + netns_free(ns); } static void test_tc_chain_classic(int target, bool chain_tc_old) @@ -620,10 +656,19 @@ static void test_tc_chain_classic(int target, bool chain_tc_old) void serial_test_tc_links_chain_classic(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_chain_classic(BPF_TCX_INGRESS, false); test_tc_chain_classic(BPF_TCX_EGRESS, false); test_tc_chain_classic(BPF_TCX_INGRESS, true); test_tc_chain_classic(BPF_TCX_EGRESS, true); + + netns_free(ns); } static void test_tc_links_replace_target(int target) @@ -848,8 +893,17 @@ static void test_tc_links_replace_target(int target) void serial_test_tc_links_replace(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_links_replace_target(BPF_TCX_INGRESS); test_tc_links_replace_target(BPF_TCX_EGRESS); + + netns_free(ns); } static void test_tc_links_invalid_target(int target) @@ -1160,8 +1214,17 @@ static void test_tc_links_invalid_target(int target) void serial_test_tc_links_invalid(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_links_invalid_target(BPF_TCX_INGRESS); test_tc_links_invalid_target(BPF_TCX_EGRESS); + + netns_free(ns); } static void test_tc_links_prepend_target(int target) @@ -1316,8 +1379,17 @@ static void test_tc_links_prepend_target(int target) void serial_test_tc_links_prepend(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_links_prepend_target(BPF_TCX_INGRESS); test_tc_links_prepend_target(BPF_TCX_EGRESS); + + netns_free(ns); } static void test_tc_links_append_target(int target) @@ -1472,8 +1544,17 @@ static void test_tc_links_append_target(int target) void serial_test_tc_links_append(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_links_append_target(BPF_TCX_INGRESS); test_tc_links_append_target(BPF_TCX_EGRESS); + + netns_free(ns); } static void test_tc_links_dev_cleanup_target(int target) @@ -1570,8 +1651,17 @@ static void test_tc_links_dev_cleanup_target(int target) void serial_test_tc_links_dev_cleanup(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_links_dev_cleanup_target(BPF_TCX_INGRESS); test_tc_links_dev_cleanup_target(BPF_TCX_EGRESS); + + netns_free(ns); } static void test_tc_chain_mixed(int target) @@ -1674,8 +1764,17 @@ static void test_tc_chain_mixed(int target) void serial_test_tc_links_chain_mixed(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_chain_mixed(BPF_TCX_INGRESS); test_tc_chain_mixed(BPF_TCX_EGRESS); + + netns_free(ns); } static void test_tc_links_ingress(int target, bool chain_tc_old, @@ -1784,9 +1883,18 @@ static void test_tc_links_ingress(int target, bool chain_tc_old, void serial_test_tc_links_ingress(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_links_ingress(BPF_TCX_INGRESS, true, true); test_tc_links_ingress(BPF_TCX_INGRESS, true, false); test_tc_links_ingress(BPF_TCX_INGRESS, false, false); + + netns_free(ns); } struct qdisc_req { @@ -1825,8 +1933,14 @@ static int qdisc_replace(int ifindex, const char *kind, bool block) void serial_test_tc_links_dev_chain0(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; int err, ifindex; + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + ASSERT_OK(system("ip link add dev foo type veth peer name bar"), "add veth"); ifindex = if_nametoindex("foo"); ASSERT_NEQ(ifindex, 0, "non_zero_ifindex"); @@ -1846,6 +1960,7 @@ void serial_test_tc_links_dev_chain0(void) ASSERT_OK(system("ip link del dev foo"), "del veth"); ASSERT_EQ(if_nametoindex("foo"), 0, "foo removed"); ASSERT_EQ(if_nametoindex("bar"), 0, "bar removed"); + netns_free(ns); } static void test_tc_links_dev_mixed(int target) @@ -1957,6 +2072,15 @@ static void test_tc_links_dev_mixed(int target) void serial_test_tc_links_dev_mixed(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_links_dev_mixed(BPF_TCX_INGRESS); test_tc_links_dev_mixed(BPF_TCX_EGRESS); + + netns_free(ns); } diff --git a/tools/testing/selftests/bpf/prog_tests/tc_opts.c b/tools/testing/selftests/bpf/prog_tests/tc_opts.c index f77f604389aabbc7afb71ac003dc952cba82c460..d38376244532026b2b3d505bcf9711c8e7948e17 100644 --- a/tools/testing/selftests/bpf/prog_tests/tc_opts.c +++ b/tools/testing/selftests/bpf/prog_tests/tc_opts.c @@ -6,6 +6,8 @@ #define loopback 1 #define ping_cmd "ping -q -c1 -w1 127.0.0.1 > /dev/null" +#define NS_NAME_MAX_LEN 32 +#define NS_NAME "ns-tc-opts-" #include "test_tc_link.skel.h" #include "tc_helpers.h" @@ -15,11 +17,17 @@ void serial_test_tc_opts_basic(void) LIBBPF_OPTS(bpf_prog_attach_opts, opta); LIBBPF_OPTS(bpf_prog_detach_opts, optd); LIBBPF_OPTS(bpf_prog_query_opts, optq); + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; __u32 fd1, fd2, id1, id2; struct test_tc_link *skel; + struct netns_obj *ns; __u32 prog_ids[2]; int err; + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + skel = test_tc_link__open_and_load(); if (!ASSERT_OK_PTR(skel, "skel_load")) goto cleanup; @@ -106,6 +114,7 @@ void serial_test_tc_opts_basic(void) cleanup: test_tc_link__destroy(skel); + netns_free(ns); } static void test_tc_opts_before_target(int target) @@ -256,8 +265,17 @@ static void test_tc_opts_before_target(int target) void serial_test_tc_opts_before(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_opts_before_target(BPF_TCX_INGRESS); test_tc_opts_before_target(BPF_TCX_EGRESS); + + netns_free(ns); } static void test_tc_opts_after_target(int target) @@ -447,8 +465,17 @@ static void test_tc_opts_after_target(int target) void serial_test_tc_opts_after(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_opts_after_target(BPF_TCX_INGRESS); test_tc_opts_after_target(BPF_TCX_EGRESS); + + netns_free(ns); } static void test_tc_opts_revision_target(int target) @@ -556,8 +583,17 @@ static void test_tc_opts_revision_target(int target) void serial_test_tc_opts_revision(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_opts_revision_target(BPF_TCX_INGRESS); test_tc_opts_revision_target(BPF_TCX_EGRESS); + + netns_free(ns); } static void test_tc_chain_classic(int target, bool chain_tc_old) @@ -657,10 +693,19 @@ static void test_tc_chain_classic(int target, bool chain_tc_old) void serial_test_tc_opts_chain_classic(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_chain_classic(BPF_TCX_INGRESS, false); test_tc_chain_classic(BPF_TCX_EGRESS, false); test_tc_chain_classic(BPF_TCX_INGRESS, true); test_tc_chain_classic(BPF_TCX_EGRESS, true); + + netns_free(ns); } static void test_tc_opts_replace_target(int target) @@ -866,8 +911,17 @@ static void test_tc_opts_replace_target(int target) void serial_test_tc_opts_replace(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_opts_replace_target(BPF_TCX_INGRESS); test_tc_opts_replace_target(BPF_TCX_EGRESS); + + netns_free(ns); } static void test_tc_opts_invalid_target(int target) @@ -1019,8 +1073,17 @@ static void test_tc_opts_invalid_target(int target) void serial_test_tc_opts_invalid(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_opts_invalid_target(BPF_TCX_INGRESS); test_tc_opts_invalid_target(BPF_TCX_EGRESS); + + netns_free(ns); } static void test_tc_opts_prepend_target(int target) @@ -1159,8 +1222,17 @@ static void test_tc_opts_prepend_target(int target) void serial_test_tc_opts_prepend(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_opts_prepend_target(BPF_TCX_INGRESS); test_tc_opts_prepend_target(BPF_TCX_EGRESS); + + netns_free(ns); } static void test_tc_opts_append_target(int target) @@ -1299,8 +1371,17 @@ static void test_tc_opts_append_target(int target) void serial_test_tc_opts_append(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_opts_append_target(BPF_TCX_INGRESS); test_tc_opts_append_target(BPF_TCX_EGRESS); + + netns_free(ns); } static void test_tc_opts_dev_cleanup_target(int target) @@ -1389,8 +1470,17 @@ static void test_tc_opts_dev_cleanup_target(int target) void serial_test_tc_opts_dev_cleanup(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_opts_dev_cleanup_target(BPF_TCX_INGRESS); test_tc_opts_dev_cleanup_target(BPF_TCX_EGRESS); + + netns_free(ns); } static void test_tc_opts_mixed_target(int target) @@ -1565,8 +1655,17 @@ static void test_tc_opts_mixed_target(int target) void serial_test_tc_opts_mixed(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_opts_mixed_target(BPF_TCX_INGRESS); test_tc_opts_mixed_target(BPF_TCX_EGRESS); + + netns_free(ns); } static void test_tc_opts_demixed_target(int target) @@ -1644,8 +1743,17 @@ static void test_tc_opts_demixed_target(int target) void serial_test_tc_opts_demixed(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_opts_demixed_target(BPF_TCX_INGRESS); test_tc_opts_demixed_target(BPF_TCX_EGRESS); + + netns_free(ns); } static void test_tc_opts_detach_target(int target) @@ -1815,8 +1923,17 @@ static void test_tc_opts_detach_target(int target) void serial_test_tc_opts_detach(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_opts_detach_target(BPF_TCX_INGRESS); test_tc_opts_detach_target(BPF_TCX_EGRESS); + + netns_free(ns); } static void test_tc_opts_detach_before_target(int target) @@ -2022,8 +2139,17 @@ static void test_tc_opts_detach_before_target(int target) void serial_test_tc_opts_detach_before(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_opts_detach_before_target(BPF_TCX_INGRESS); test_tc_opts_detach_before_target(BPF_TCX_EGRESS); + + netns_free(ns); } static void test_tc_opts_detach_after_target(int target) @@ -2238,8 +2364,17 @@ static void test_tc_opts_detach_after_target(int target) void serial_test_tc_opts_detach_after(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_opts_detach_after_target(BPF_TCX_INGRESS); test_tc_opts_detach_after_target(BPF_TCX_EGRESS); + + netns_free(ns); } static void test_tc_opts_delete_empty(int target, bool chain_tc_old) @@ -2267,10 +2402,19 @@ static void test_tc_opts_delete_empty(int target, bool chain_tc_old) void serial_test_tc_opts_delete_empty(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_opts_delete_empty(BPF_TCX_INGRESS, false); test_tc_opts_delete_empty(BPF_TCX_EGRESS, false); test_tc_opts_delete_empty(BPF_TCX_INGRESS, true); test_tc_opts_delete_empty(BPF_TCX_EGRESS, true); + + netns_free(ns); } static void test_tc_chain_mixed(int target) @@ -2374,8 +2518,17 @@ static void test_tc_chain_mixed(int target) void serial_test_tc_opts_chain_mixed(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_chain_mixed(BPF_TCX_INGRESS); test_tc_chain_mixed(BPF_TCX_EGRESS); + + netns_free(ns); } static int generate_dummy_prog(void) @@ -2448,6 +2601,13 @@ static void test_tc_opts_max_target(int target, int flags, bool relative) void serial_test_tc_opts_max(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_opts_max_target(BPF_TCX_INGRESS, 0, false); test_tc_opts_max_target(BPF_TCX_EGRESS, 0, false); @@ -2456,6 +2616,8 @@ void serial_test_tc_opts_max(void) test_tc_opts_max_target(BPF_TCX_INGRESS, BPF_F_AFTER, true); test_tc_opts_max_target(BPF_TCX_EGRESS, BPF_F_AFTER, false); + + netns_free(ns); } static void test_tc_opts_query_target(int target) @@ -2750,8 +2912,17 @@ static void test_tc_opts_query_target(int target) void serial_test_tc_opts_query(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_opts_query_target(BPF_TCX_INGRESS); test_tc_opts_query_target(BPF_TCX_EGRESS); + + netns_free(ns); } static void test_tc_opts_query_attach_target(int target) @@ -2809,6 +2980,15 @@ static void test_tc_opts_query_attach_target(int target) void serial_test_tc_opts_query_attach(void) { + char ns_name[NS_NAME_MAX_LEN] = NS_NAME; + struct netns_obj *ns; + + ns = create_and_open_tid_ns(ns_name, NS_NAME_MAX_LEN); + if (!ASSERT_OK_PTR(ns, "create and open ns")) + return; + test_tc_opts_query_attach_target(BPF_TCX_INGRESS); test_tc_opts_query_attach_target(BPF_TCX_EGRESS); + + netns_free(ns); } -- 2.48.1