Skip to contentSkip to conversation

selftests/bpf: tc_links/tc_opts: Unserialize tests

PATCH Superseded Bastien Curutchet (eBPF Foundation) 3 participants 7 messages Feb 17, 2025 – Feb 19, 2025

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover selftests/bpf: tc_links/tc_opts: Unserialize tests

Hi all,

Both tc_links.c and tc_opts.c do their tests on the loopback interface. It prevents from parallelizing their executions.

Use namespaces and the new append_tid() helper to allow this parallelization.

---
Bastien Curutchet (eBPF Foundation) (3):
selftests/bpf: tc_helpers: Add create_and_open_tid_ns() selftests/bpf: tc_link/tc_opts: Use unique namespace selftests/bpf: tc_links/tc_opts: Serialize tests

 .../testing/selftests/bpf/prog_tests/tc_helpers.h  |  12 ++
 tools/testing/selftests/bpf/prog_tests/tc_links.c  | 164 +++++++++++++--
 tools/testing/selftests/bpf/prog_tests/tc_opts.c   | 230 ++++++++++++++++++---
 3 files changed, 361 insertions(+), 45 deletions(-)

---
base-commit: cfed0f474a4bb2f12b54de5d6a7301cfb7dc0dbd
change-id: 20250128-tc_links-d894a23b7063

Best regards,

Signature · 1 line
Bastien Curutchet (eBPF Foundation) <[email protected]>
#1 lore raw

PATCH 1/3 selftests/bpf: tc_helpers: Add create_and_open_tid_ns()

Add a create_and_open_tid_ns() helper that creates a new network namespace and open it.

Use the append_tid() helper to ensure the uniqueness of the namespace name.

---
 tools/testing/selftests/bpf/prog_tests/tc_helpers.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
tools/testing/selftests/bpf/prog_tests/tc_helpers.h +12 -0
index 924d0e25320c71691998d6fce4d0aae367457862..8689ecd88fa89f4e622729d4f1b6d33c25fa89d8 100644
@@ -3,6 +3,7 @@
 #ifndef TC_HELPERS
 #define TC_HELPERS
 #include <test_progs.h>
+#include "network_helpers.h"
 
 #ifndef loopback
 # define loopback 1
@@ -75,4 +76,15 @@ static inline void tc_skel_reset_all_seen(struct test_tc_link *skel)
 	memset(skel->bss, 0, sizeof(*skel->bss));
 }
 
+static inline struct netns_obj *create_and_open_tid_ns(char *ns_name, size_t sz)
+{
+	if (!ns_name)
+		return NULL;
+
+	if (append_tid(ns_name, sz))
+		return NULL;
+
+	return netns_new(ns_name, true);
+}
+
 #endif /* TC_HELPERS */
Signature · 1 line
2.48.1
#2 lore raw

PATCH 2/3 selftests/bpf: tc_link/tc_opts: Use unique namespace

In reply to Bastien Curutchet (eBPF Foundation)

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.

---
 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(+)
tools/testing/selftests/bpf/prog_tests/tc_links.c +124 -0
index 1af9ec1149aab6ca5ef6986fc16a650162266966..07a2fc3d44d03e18dd3c715b0b26c7f1ac6d47cb 100644
@@ -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);
 }
tools/testing/selftests/bpf/prog_tests/tc_opts.c +180 -0

361 lines not on this page · raw message

Signature · 1 line
2.48.1
#3 lore raw

PATCH 3/3 selftests/bpf: tc_links/tc_opts: Serialize tests

In reply to Bastien Curutchet (eBPF Foundation)

The tests aren't allowed to be run in parallel while they could be.

Replace serial_test_*() calls by test_*() ones to allow parallelization of these tests.
Rename some 'subtests' functions to avoid name conflicts with the actual tests.

---
 tools/testing/selftests/bpf/prog_tests/tc_links.c | 40 +++++++++---------
 tools/testing/selftests/bpf/prog_tests/tc_opts.c  | 50 +++++++++++------------
 2 files changed, 45 insertions(+), 45 deletions(-)
tools/testing/selftests/bpf/prog_tests/tc_links.c +20 -20
index 07a2fc3d44d03e18dd3c715b0b26c7f1ac6d47cb..c94fe1e1687239ed4090f53bff40ede10e501f55 100644
@@ -269,7 +269,7 @@ static void test_tc_links_before_target(int target)
 	assert_mprog_count(target, 0);
 }
 
-void serial_test_tc_links_before(void)
+void test_tc_links_before(void)
 {
 	char ns_name[NS_NAME_MAX_LEN] = NS_NAME;
 	struct netns_obj *ns;
@@ -432,7 +432,7 @@ static void test_tc_links_after_target(int target)
 	assert_mprog_count(target, 0);
 }
 
-void serial_test_tc_links_after(void)
+void test_tc_links_after(void)
 {
 	char ns_name[NS_NAME_MAX_LEN] = NS_NAME;
 	struct netns_obj *ns;
@@ -541,7 +541,7 @@ static void test_tc_links_revision_target(int target)
 	assert_mprog_count(target, 0);
 }
 
-void serial_test_tc_links_revision(void)
+void test_tc_links_revision(void)
 {
 	char ns_name[NS_NAME_MAX_LEN] = NS_NAME;
 	struct netns_obj *ns;
@@ -654,7 +654,7 @@ static void test_tc_chain_classic(int target, bool chain_tc_old)
 	assert_mprog_count(target, 0);
 }
 
-void serial_test_tc_links_chain_classic(void)
+void test_tc_links_chain_classic(void)
 {
 	char ns_name[NS_NAME_MAX_LEN] = NS_NAME;
 	struct netns_obj *ns;
@@ -891,7 +891,7 @@ static void test_tc_links_replace_target(int target)
 	assert_mprog_count(target, 0);
 }
 
-void serial_test_tc_links_replace(void)
+void test_tc_links_replace(void)
 {
 	char ns_name[NS_NAME_MAX_LEN] = NS_NAME;
 	struct netns_obj *ns;
@@ -1212,7 +1212,7 @@ static void test_tc_links_invalid_target(int target)
 	assert_mprog_count(target, 0);
 }
 
-void serial_test_tc_links_invalid(void)
+void test_tc_links_invalid(void)
 {
 	char ns_name[NS_NAME_MAX_LEN] = NS_NAME;
 	struct netns_obj *ns;
@@ -1377,7 +1377,7 @@ static void test_tc_links_prepend_target(int target)
 	assert_mprog_count(target, 0);
 }
 
-void serial_test_tc_links_prepend(void)
+void test_tc_links_prepend(void)
 {
 	char ns_name[NS_NAME_MAX_LEN] = NS_NAME;
 	struct netns_obj *ns;
@@ -1542,7 +1542,7 @@ static void test_tc_links_append_target(int target)
 	assert_mprog_count(target, 0);
 }
 
-void serial_test_tc_links_append(void)
+void test_tc_links_append(void)
 {
 	char ns_name[NS_NAME_MAX_LEN] = NS_NAME;
 	struct netns_obj *ns;
@@ -1649,7 +1649,7 @@ static void test_tc_links_dev_cleanup_target(int target)
 	ASSERT_EQ(if_nametoindex("tcx_opts2"), 0, "dev2_removed");
 }
 
-void serial_test_tc_links_dev_cleanup(void)
+void test_tc_links_dev_cleanup(void)
 {
 	char ns_name[NS_NAME_MAX_LEN] = NS_NAME;
 	struct netns_obj *ns;
@@ -1762,7 +1762,7 @@ static void test_tc_chain_mixed(int target)
 	test_tc_link__destroy(skel);
 }
 
-void serial_test_tc_links_chain_mixed(void)
+void test_tc_links_chain_mixed(void)
 {
 	char ns_name[NS_NAME_MAX_LEN] = NS_NAME;
 	struct netns_obj *ns;
@@ -1777,7 +1777,7 @@ void serial_test_tc_links_chain_mixed(void)
 	netns_free(ns);
 }
 
-static void test_tc_links_ingress(int target, bool chain_tc_old,
+static void tc_links_ingress(int target, bool chain_tc_old,
 				  bool tcx_teardown_first)
 {
 	LIBBPF_OPTS(bpf_tc_opts, tc_opts,
@@ -1881,7 +1881,7 @@ static void test_tc_links_ingress(int target, bool chain_tc_old,
 	assert_mprog_count(target, 0);
 }
 
-void serial_test_tc_links_ingress(void)
+void test_tc_links_ingress(void)
 {
 	char ns_name[NS_NAME_MAX_LEN] = NS_NAME;
 	struct netns_obj *ns;
@@ -1890,9 +1890,9 @@ void serial_test_tc_links_ingress(void)
 	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);
+	tc_links_ingress(BPF_TCX_INGRESS, true, true);
+	tc_links_ingress(BPF_TCX_INGRESS, true, false);
+	tc_links_ingress(BPF_TCX_INGRESS, false, false);
 
 	netns_free(ns);
 }
@@ -1931,7 +1931,7 @@ static int qdisc_replace(int ifindex, const char *kind, bool block)
 	return err;
 }
 
-void serial_test_tc_links_dev_chain0(void)
+void test_tc_links_dev_chain0(void)
 {
 	char ns_name[NS_NAME_MAX_LEN] = NS_NAME;
 	struct netns_obj *ns;
@@ -1963,7 +1963,7 @@ void serial_test_tc_links_dev_chain0(void)
 	netns_free(ns);
 }
 
-static void test_tc_links_dev_mixed(int target)
+static void tc_links_dev_mixed(int target)
 {
 	LIBBPF_OPTS(bpf_tc_opts, tc_opts, .handle = 1, .priority = 1);
 	LIBBPF_OPTS(bpf_tc_hook, tc_hook);
@@ -2070,7 +2070,7 @@ static void test_tc_links_dev_mixed(int target)
 	ASSERT_EQ(if_nametoindex("tcx_opts2"), 0, "dev2_removed");
 }
 
-void serial_test_tc_links_dev_mixed(void)
+void test_tc_links_dev_mixed(void)
 {
 	char ns_name[NS_NAME_MAX_LEN] = NS_NAME;
 	struct netns_obj *ns;
@@ -2079,8 +2079,8 @@ void serial_test_tc_links_dev_mixed(void)
 	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);
+	tc_links_dev_mixed(BPF_TCX_INGRESS);
+	tc_links_dev_mixed(BPF_TCX_EGRESS);
 
 	netns_free(ns);
 }
tools/testing/selftests/bpf/prog_tests/tc_opts.c +25 -25

182 lines not on this page · raw message

Signature · 1 line
2.48.1
Acked-by on the v1 series
#4 lore raw
In reply to Bastien Curutchet (eBPF Foundation)
Quoted 9 lines · Bastien Curutchet (eBPF Foundation)

Hi all,

Both tc_links.c and tc_opts.c do their tests on the loopback interface. It prevents from parallelizing their executions.

Use namespaces and the new append_tid() helper to allow this parallelization.

LGTM, thank you! Optionally, if there is more to convert, we can think about moving create_and_open_tid_ns to the test_progs itself. For example, if the test name starts with ns_, test_progs can probably do the create_and_open_tid_ns/netns_free part?

#5 lore raw
Quoted 10 lines · Stanislav Fomichev
Quoted 8 lines · Bastien Curutchet (eBPF Foundation)

Hi all,

Both tc_links.c and tc_opts.c do their tests on the loopback interface. It prevents from parallelizing their executions.

Use namespaces and the new append_tid() helper to allow this parallelization.

Quoted 8 lines
Quoted 1 line

LGTM, thank you! Optionally, if there is more to convert, we can think about moving create_and_open_tid_ns to the test_progs itself. For example, if the test name starts with ns_, test_progs can probably do the create_and_open_tid_ns/netns_free part?

That's a good idea.
Let's do it now.
Otherwise most of the patch 2 will be reverted when it's introduced.

pw-bot: cr

#6 lore raw

Hi,

Quoted 15 lines · Alexei Starovoitov
Quoted 14 lines · Stanislav Fomichev
Quoted 9 lines · Bastien Curutchet (eBPF Foundation)

Hi all,

Both tc_links.c and tc_opts.c do their tests on the loopback interface. It prevents from parallelizing their executions.

Use namespaces and the new append_tid() helper to allow this parallelization.

Quoted 9 lines
Quoted 4 lines

LGTM, thank you! Optionally, if there is more to convert, we can think about moving create_and_open_tid_ns to the test_progs itself. For example, if the test name starts with ns_, test_progs can probably do the create_and_open_tid_ns/netns_free part?

That's a good idea.
Let's do it now.
Otherwise most of the patch 2 will be reverted when it's introduced.

Ok, I'll send a V2 with this in test_progs.

Best regards,
Bastien