Skip to contentSkip to conversation

selftests/bpf: Rewrite test_tc_redirect.sh as prog_tests/tc_redirect.c

PATCH Superseded Jussi Maki 3 participants 10 messages Apr 29, 2021 – Jul 27, 2021

Subsystems (3)
lore
Share & tools
lore

Jussi Maki

author
#0 lore raw

PATCH 1/1 selftests/bpf: Rewrite test_tc_redirect.sh as prog_tests/tc_redirect.c

Ports test_tc_redirect.sh to the test_progs framework and removes the old test. This makes it more in line with rest of the tests and makes it possible to run this test with vmtest.sh and under the bpf CI.

---
 tools/testing/selftests/bpf/network_helpers.c |   2 +-
 tools/testing/selftests/bpf/network_helpers.h |   1 +
 .../selftests/bpf/prog_tests/tc_redirect.c    | 481 ++++++++++++++++++
 .../selftests/bpf/progs/test_tc_neigh.c       |  33 +-
 .../selftests/bpf/progs/test_tc_neigh_fib.c   |   9 +-
 .../selftests/bpf/progs/test_tc_peer.c        |  33 +-
 .../testing/selftests/bpf/test_tc_redirect.sh | 216 --------
 7 files changed, 509 insertions(+), 266 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tc_redirect.c
 delete mode 100755 tools/testing/selftests/bpf/test_tc_redirect.sh
tools/testing/selftests/bpf/network_helpers.c +1 -1
index 12ee40284da0..2060bc122c53 100644
@@ -40,7 +40,7 @@ struct ipv6_packet pkt_v6 = {
 	.tcp.doff = 5,
 };
 
-static int settimeo(int fd, int timeout_ms)
+int settimeo(int fd, int timeout_ms)
 {
 	struct timeval timeout = { .tv_sec = 3 };
 
tools/testing/selftests/bpf/network_helpers.h +1 -0
index 7205f8afdba1..5e0d51c07b63 100644
@@ -33,6 +33,7 @@ struct ipv6_packet {
 } __packed;
 extern struct ipv6_packet pkt_v6;
 
+int settimeo(int fd, int timeout_ms);
 int start_server(int family, int type, const char *addr, __u16 port,
 		 int timeout_ms);
 int connect_to_fd(int server_fd, int timeout_ms);
tools/testing/selftests/bpf/prog_tests/tc_redirect.c new file +481 -0

481 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_tc_neigh.c +10 -23
index b985ac4e7a81..90f64a85998f 100644
@@ -33,17 +33,8 @@
 				 a.s6_addr32[3] == b.s6_addr32[3])
 #endif
 
-enum {
-	dev_src,
-	dev_dst,
-};
-
-struct bpf_map_def SEC("maps") ifindex_map = {
-	.type		= BPF_MAP_TYPE_ARRAY,
-	.key_size	= sizeof(int),
-	.value_size	= sizeof(int),
-	.max_entries	= 2,
-};
+static volatile const __u32 IFINDEX_SRC;
+static volatile const __u32 IFINDEX_DST;
 
 static __always_inline bool is_remote_ep_v4(struct __sk_buff *skb,
 					    __be32 addr)
@@ -79,14 +70,8 @@ static __always_inline bool is_remote_ep_v6(struct __sk_buff *skb,
 	return v6_equal(ip6h->daddr, addr);
 }
 
-static __always_inline int get_dev_ifindex(int which)
-{
-	int *ifindex = bpf_map_lookup_elem(&ifindex_map, &which);
-
-	return ifindex ? *ifindex : 0;
-}
-
-SEC("chk_egress") int tc_chk(struct __sk_buff *skb)
+SEC("classifier/chk_egress")
+int tc_chk(struct __sk_buff *skb)
 {
 	void *data_end = ctx_ptr(skb->data_end);
 	void *data = ctx_ptr(skb->data);
@@ -98,7 +83,8 @@ SEC("chk_egress") int tc_chk(struct __sk_buff *skb)
 	return !raw[0] && !raw[1] && !raw[2] ? TC_ACT_SHOT : TC_ACT_OK;
 }
 
-SEC("dst_ingress") int tc_dst(struct __sk_buff *skb)
+SEC("classifier/dst_ingress")
+int tc_dst(struct __sk_buff *skb)
 {
 	__u8 zero[ETH_ALEN * 2];
 	bool redirect = false;
@@ -119,10 +105,11 @@ SEC("dst_ingress") int tc_dst(struct __sk_buff *skb)
 	if (bpf_skb_store_bytes(skb, 0, &zero, sizeof(zero), 0) < 0)
 		return TC_ACT_SHOT;
 
-	return bpf_redirect_neigh(get_dev_ifindex(dev_src), NULL, 0, 0);
+	return bpf_redirect_neigh(IFINDEX_SRC, NULL, 0, 0);
 }
 
-SEC("src_ingress") int tc_src(struct __sk_buff *skb)
+SEC("classifier/src_ingress")
+int tc_src(struct __sk_buff *skb)
 {
 	__u8 zero[ETH_ALEN * 2];
 	bool redirect = false;
@@ -143,7 +130,7 @@ SEC("src_ingress") int tc_src(struct __sk_buff *skb)
 	if (bpf_skb_store_bytes(skb, 0, &zero, sizeof(zero), 0) < 0)
 		return TC_ACT_SHOT;
 
-	return bpf_redirect_neigh(get_dev_ifindex(dev_dst), NULL, 0, 0);
+	return bpf_redirect_neigh(IFINDEX_DST, NULL, 0, 0);
 }
 
 char __license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/test_tc_neigh_fib.c +6 -3
index d82ed3457030..f7ab69cf018e 100644
@@ -75,7 +75,8 @@ static __always_inline int fill_fib_params_v6(struct __sk_buff *skb,
 	return 0;
 }
 
-SEC("chk_egress") int tc_chk(struct __sk_buff *skb)
+SEC("classifier/chk_egress")
+int tc_chk(struct __sk_buff *skb)
 {
 	void *data_end = ctx_ptr(skb->data_end);
 	void *data = ctx_ptr(skb->data);
@@ -142,12 +143,14 @@ static __always_inline int tc_redir(struct __sk_buff *skb)
 /* these are identical, but keep them separate for compatibility with the
  * section names expected by test_tc_redirect.sh
  */
-SEC("dst_ingress") int tc_dst(struct __sk_buff *skb)
+SEC("classifier/dst_ingress")
+int tc_dst(struct __sk_buff *skb)
 {
 	return tc_redir(skb);
 }
 
-SEC("src_ingress") int tc_src(struct __sk_buff *skb)
+SEC("classifier/src_ingress")
+int tc_src(struct __sk_buff *skb)
 {
 	return tc_redir(skb);
 }
tools/testing/selftests/bpf/progs/test_tc_peer.c +10 -23
index fc84a7685aa2..72c72950c3bb 100644
@@ -8,38 +8,25 @@
 
 #include <bpf/bpf_helpers.h>
 
-enum {
-	dev_src,
-	dev_dst,
-};
+static volatile const __u32 IFINDEX_SRC;
+static volatile const __u32 IFINDEX_DST;
 
-struct bpf_map_def SEC("maps") ifindex_map = {
-	.type		= BPF_MAP_TYPE_ARRAY,
-	.key_size	= sizeof(int),
-	.value_size	= sizeof(int),
-	.max_entries	= 2,
-};
-
-static __always_inline int get_dev_ifindex(int which)
-{
-	int *ifindex = bpf_map_lookup_elem(&ifindex_map, &which);
-
-	return ifindex ? *ifindex : 0;
-}
-
-SEC("chk_egress") int tc_chk(struct __sk_buff *skb)
+SEC("classifier/chk_egress")
+int tc_chk(struct __sk_buff *skb)
 {
 	return TC_ACT_SHOT;
 }
 
-SEC("dst_ingress") int tc_dst(struct __sk_buff *skb)
+SEC("classifier/dst_ingress")
+int tc_dst(struct __sk_buff *skb)
 {
-	return bpf_redirect_peer(get_dev_ifindex(dev_src), 0);
+	return bpf_redirect_peer(IFINDEX_SRC, 0);
 }
 
-SEC("src_ingress") int tc_src(struct __sk_buff *skb)
+SEC("classifier/src_ingress")
+int tc_src(struct __sk_buff *skb)
 {
-	return bpf_redirect_peer(get_dev_ifindex(dev_dst), 0);
+	return bpf_redirect_peer(IFINDEX_DST, 0);
 }
 
 char __license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/test_tc_redirect.sh deleted +0 -216

216 lines not on this page · raw message

Signature · 1 line
2.30.2
#1 lore raw
Quoted 7 lines · Jussi Maki

Ports test_tc_redirect.sh to the test_progs framework and removes the old test. This makes it more in line with rest of the tests and makes it possible to run this test with vmtest.sh and under the bpf CI.

---

Aren't there any Makefile changes that need to be done as well, given you are removing "old style" test script?

Quoted 11 lines
 tools/testing/selftests/bpf/network_helpers.c |   2 +-
 tools/testing/selftests/bpf/network_helpers.h |   1 +
 .../selftests/bpf/prog_tests/tc_redirect.c    | 481 ++++++++++++++++++
 .../selftests/bpf/progs/test_tc_neigh.c       |  33 +-
 .../selftests/bpf/progs/test_tc_neigh_fib.c   |   9 +-
 .../selftests/bpf/progs/test_tc_peer.c        |  33 +-
 .../testing/selftests/bpf/test_tc_redirect.sh | 216 --------
 7 files changed, 509 insertions(+), 266 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tc_redirect.c
 delete mode 100755 tools/testing/selftests/bpf/test_tc_redirect.sh

[...]

tools/testing/selftests/bpf/prog_tests/tc_redirect.c line 59

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tc_redirect.c new file +10 -0
+
+#define TIMEOUT_MILLIS 10000
+
+static const char * const namespaces[] = {NS_SRC, NS_FWD, NS_DST, NULL};
+static int root_netns_fd = -1;
+static __u32 duration;
+
+static void restore_root_netns(void)
+{
+       CHECK_FAIL(setns(root_netns_fd, CLONE_NEWNET));

can you please also switch to ASSERT_xxx() macros while converting this selftest? Thanks!

tools/testing/selftests/bpf/prog_tests/tc_redirect.c line 62

Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/tc_redirect.c new file +3 -0
+}
+
+int setns_by_name(const char *name)

static?

Quoted 19 lines
tools/testing/selftests/bpf/prog_tests/tc_redirect.c new file +19 -0
+{
+       int nsfd;
+       char nspath[PATH_MAX];
+       int err;
+
+       snprintf(nspath, sizeof(nspath), "%s/%s", "/var/run/netns", name);
+       nsfd = open(nspath, O_RDONLY | O_CLOEXEC);
+       if (CHECK(nsfd < 0, nspath, "failed to open\n"))
+               return -EINVAL;
+
+       err = setns(nsfd, CLONE_NEWNET);
+       close(nsfd);
+
+       if (CHECK(err, name, "failed to setns\n"))
+               return -1;
+
+       return 0;
+}
+

[...]

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/tc_redirect.c new file +7 -0
+
+#define SYS(fmt, ...)                                          \
+       ({                                                      \
+               char cmd[1024];                                 \
+               snprintf(cmd, sizeof(cmd), fmt, ##__VA_ARGS__); \
+               if (CHECK(system(cmd), cmd, "failed\n"))        \
+                       goto fail;                              \

tools/testing/selftests/bpf/prog_tests/tc_redirect.c line 153

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tc_redirect.c new file +10 -0
+       })
+
+static int netns_setup_links_and_routes(struct netns_setup_result *result)
+{
+       char veth_src_fwd_addr[IFADDR_STR_LEN+1] = {0,};
+       char veth_dst_fwd_addr[IFADDR_STR_LEN+1] = {0,};
+
+       SYS("ip link add veth_src type veth peer name veth_src_fwd");
+       SYS("ip link add veth_dst type veth peer name veth_dst_fwd");
+       if (CHECK_FAIL(get_ifaddr("veth_src_fwd", veth_src_fwd_addr)))

please no CHECK_FAIL, they are invisible in test_progs logs, so it's harder to debug any failures (and use ASSERT_xxx() instead of CHECK).

Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/tc_redirect.c new file +11 -0
+               goto fail;
+       if (CHECK_FAIL(get_ifaddr("veth_dst_fwd", veth_dst_fwd_addr)))
+               goto fail;
+
+       result->ifindex_veth_src_fwd = get_ifindex("veth_src_fwd");
+       if (CHECK_FAIL(result->ifindex_veth_src_fwd < 0))
+               goto fail;
+       result->ifindex_veth_dst_fwd = get_ifindex("veth_dst_fwd");
+       if (CHECK_FAIL(result->ifindex_veth_dst_fwd < 0))
+               goto fail;
+

[...]

tools/testing/selftests/bpf/prog_tests/tc_redirect.c line 362

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/tc_redirect.c new file +6 -0
+
+       /* bpf_fib_lookup() checks if forwarding is enabled */
+       system("ip netns exec " NS_FWD " sysctl -q -w "
+              "net.ipv4.ip_forward=1 "
+              "net.ipv6.conf.veth_src_fwd.forwarding=1 "
+              "net.ipv6.conf.veth_dst_fwd.forwarding=1");

no SYS() and/or error checking?

tools/testing/selftests/bpf/prog_tests/tc_redirect.c line 370

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/tc_redirect.c new file +8 -0
+
+       test_connectivity();
+done:
+       system("ip netns exec " NS_FWD " sysctl -q -w "
+              "net.ipv4.ip_forward=0 "
+              "net.ipv6.conf.veth_src_fwd.forwarding=0 "
+              "net.ipv6.conf.veth_dst_fwd.forwarding=0");
+

same?

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/tc_redirect.c new file +8 -0
+       bpf_program__unpin(skel->progs.tc_src, SRC_PROG_PIN_FILE);
+       bpf_program__unpin(skel->progs.tc_chk, CHK_PROG_PIN_FILE);
+       bpf_program__unpin(skel->progs.tc_dst, DST_PROG_PIN_FILE);
+       test_tc_neigh_fib__destroy(skel);
+       netns_unload_bpf();
+       restore_root_netns();
+}
+

[...]

author
#2 lore raw
Quoted 7 lines · Andrii Nakryiko
Quoted 5 lines · Jussi Maki

Ports test_tc_redirect.sh to the test_progs framework and removes the old test. This makes it more in line with rest of the tests and makes it possible to run this test with vmtest.sh and under the bpf CI.

Quoted 5 lines
Quoted 2 lines

---

Aren't there any Makefile changes that need to be done as well, given you are removing "old style" test script?

Nope, it isn't mentioned anywhere.

I'll send a v2 that addresses your comments, e.g. switching to ASSERT_xxx() and rewroting the sysctl stuff to handle errors and restore original values so it won't affect other tests.

author
#3 lore raw

PATCH v2 1/1 selftests/bpf: Rewrite test_tc_redirect.sh as prog_tests/tc_redirect.c

In reply to Jussi Maki

Ports test_tc_redirect.sh to the test_progs framework and removes the old test. This makes it more in line with rest of the tests and makes it possible to run this test with vmtest.sh and under the bpf CI.

---
 tools/testing/selftests/bpf/network_helpers.c |   2 +-
 tools/testing/selftests/bpf/network_helpers.h |   1 +
 .../selftests/bpf/prog_tests/tc_redirect.c    | 594 ++++++++++++++++++
 .../selftests/bpf/progs/test_tc_neigh.c       |  33 +-
 .../selftests/bpf/progs/test_tc_neigh_fib.c   |   9 +-
 .../selftests/bpf/progs/test_tc_peer.c        |  33 +-
 .../testing/selftests/bpf/test_tc_redirect.sh | 216 -------
 7 files changed, 622 insertions(+), 266 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tc_redirect.c
 delete mode 100755 tools/testing/selftests/bpf/test_tc_redirect.sh
tools/testing/selftests/bpf/network_helpers.c +1 -1
index 12ee40284da0..2060bc122c53 100644
@@ -40,7 +40,7 @@ struct ipv6_packet pkt_v6 = {
 	.tcp.doff = 5,
 };
 
-static int settimeo(int fd, int timeout_ms)
+int settimeo(int fd, int timeout_ms)
 {
 	struct timeval timeout = { .tv_sec = 3 };
 
tools/testing/selftests/bpf/network_helpers.h +1 -0
index 7205f8afdba1..5e0d51c07b63 100644
@@ -33,6 +33,7 @@ struct ipv6_packet {
 } __packed;
 extern struct ipv6_packet pkt_v6;
 
+int settimeo(int fd, int timeout_ms);
 int start_server(int family, int type, const char *addr, __u16 port,
 		 int timeout_ms);
 int connect_to_fd(int server_fd, int timeout_ms);
tools/testing/selftests/bpf/prog_tests/tc_redirect.c new file +594 -0

594 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_tc_neigh.c +10 -23
index b985ac4e7a81..90f64a85998f 100644
@@ -33,17 +33,8 @@
 				 a.s6_addr32[3] == b.s6_addr32[3])
 #endif
 
-enum {
-	dev_src,
-	dev_dst,
-};
-
-struct bpf_map_def SEC("maps") ifindex_map = {
-	.type		= BPF_MAP_TYPE_ARRAY,
-	.key_size	= sizeof(int),
-	.value_size	= sizeof(int),
-	.max_entries	= 2,
-};
+static volatile const __u32 IFINDEX_SRC;
+static volatile const __u32 IFINDEX_DST;
 
 static __always_inline bool is_remote_ep_v4(struct __sk_buff *skb,
 					    __be32 addr)
@@ -79,14 +70,8 @@ static __always_inline bool is_remote_ep_v6(struct __sk_buff *skb,
 	return v6_equal(ip6h->daddr, addr);
 }
 
-static __always_inline int get_dev_ifindex(int which)
-{
-	int *ifindex = bpf_map_lookup_elem(&ifindex_map, &which);
-
-	return ifindex ? *ifindex : 0;
-}
-
-SEC("chk_egress") int tc_chk(struct __sk_buff *skb)
+SEC("classifier/chk_egress")
+int tc_chk(struct __sk_buff *skb)
 {
 	void *data_end = ctx_ptr(skb->data_end);
 	void *data = ctx_ptr(skb->data);
@@ -98,7 +83,8 @@ SEC("chk_egress") int tc_chk(struct __sk_buff *skb)
 	return !raw[0] && !raw[1] && !raw[2] ? TC_ACT_SHOT : TC_ACT_OK;
 }
 
-SEC("dst_ingress") int tc_dst(struct __sk_buff *skb)
+SEC("classifier/dst_ingress")
+int tc_dst(struct __sk_buff *skb)
 {
 	__u8 zero[ETH_ALEN * 2];
 	bool redirect = false;
@@ -119,10 +105,11 @@ SEC("dst_ingress") int tc_dst(struct __sk_buff *skb)
 	if (bpf_skb_store_bytes(skb, 0, &zero, sizeof(zero), 0) < 0)
 		return TC_ACT_SHOT;
 
-	return bpf_redirect_neigh(get_dev_ifindex(dev_src), NULL, 0, 0);
+	return bpf_redirect_neigh(IFINDEX_SRC, NULL, 0, 0);
 }
 
-SEC("src_ingress") int tc_src(struct __sk_buff *skb)
+SEC("classifier/src_ingress")
+int tc_src(struct __sk_buff *skb)
 {
 	__u8 zero[ETH_ALEN * 2];
 	bool redirect = false;
@@ -143,7 +130,7 @@ SEC("src_ingress") int tc_src(struct __sk_buff *skb)
 	if (bpf_skb_store_bytes(skb, 0, &zero, sizeof(zero), 0) < 0)
 		return TC_ACT_SHOT;
 
-	return bpf_redirect_neigh(get_dev_ifindex(dev_dst), NULL, 0, 0);
+	return bpf_redirect_neigh(IFINDEX_DST, NULL, 0, 0);
 }
 
 char __license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/test_tc_neigh_fib.c +6 -3
index d82ed3457030..f7ab69cf018e 100644
@@ -75,7 +75,8 @@ static __always_inline int fill_fib_params_v6(struct __sk_buff *skb,
 	return 0;
 }
 
-SEC("chk_egress") int tc_chk(struct __sk_buff *skb)
+SEC("classifier/chk_egress")
+int tc_chk(struct __sk_buff *skb)
 {
 	void *data_end = ctx_ptr(skb->data_end);
 	void *data = ctx_ptr(skb->data);
@@ -142,12 +143,14 @@ static __always_inline int tc_redir(struct __sk_buff *skb)
 /* these are identical, but keep them separate for compatibility with the
  * section names expected by test_tc_redirect.sh
  */
-SEC("dst_ingress") int tc_dst(struct __sk_buff *skb)
+SEC("classifier/dst_ingress")
+int tc_dst(struct __sk_buff *skb)
 {
 	return tc_redir(skb);
 }
 
-SEC("src_ingress") int tc_src(struct __sk_buff *skb)
+SEC("classifier/src_ingress")
+int tc_src(struct __sk_buff *skb)
 {
 	return tc_redir(skb);
 }
tools/testing/selftests/bpf/progs/test_tc_peer.c +10 -23
index fc84a7685aa2..72c72950c3bb 100644
@@ -8,38 +8,25 @@
 
 #include <bpf/bpf_helpers.h>
 
-enum {
-	dev_src,
-	dev_dst,
-};
+static volatile const __u32 IFINDEX_SRC;
+static volatile const __u32 IFINDEX_DST;
 
-struct bpf_map_def SEC("maps") ifindex_map = {
-	.type		= BPF_MAP_TYPE_ARRAY,
-	.key_size	= sizeof(int),
-	.value_size	= sizeof(int),
-	.max_entries	= 2,
-};
-
-static __always_inline int get_dev_ifindex(int which)
-{
-	int *ifindex = bpf_map_lookup_elem(&ifindex_map, &which);
-
-	return ifindex ? *ifindex : 0;
-}
-
-SEC("chk_egress") int tc_chk(struct __sk_buff *skb)
+SEC("classifier/chk_egress")
+int tc_chk(struct __sk_buff *skb)
 {
 	return TC_ACT_SHOT;
 }
 
-SEC("dst_ingress") int tc_dst(struct __sk_buff *skb)
+SEC("classifier/dst_ingress")
+int tc_dst(struct __sk_buff *skb)
 {
-	return bpf_redirect_peer(get_dev_ifindex(dev_src), 0);
+	return bpf_redirect_peer(IFINDEX_SRC, 0);
 }
 
-SEC("src_ingress") int tc_src(struct __sk_buff *skb)
+SEC("classifier/src_ingress")
+int tc_src(struct __sk_buff *skb)
 {
-	return bpf_redirect_peer(get_dev_ifindex(dev_dst), 0);
+	return bpf_redirect_peer(IFINDEX_DST, 0);
 }
 
 char __license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/test_tc_redirect.sh deleted +0 -216

216 lines not on this page · raw message

Signature · 1 line
2.30.2
#4 lore raw
Quoted 7 lines · Jussi Maki

Ports test_tc_redirect.sh to the test_progs framework and removes the old test. This makes it more in line with rest of the tests and makes it possible to run this test with vmtest.sh and under the bpf CI.

---

One bug and few mostly mechanical nits. It would be great to get a code review from someone that knows something about iproute2 and the whole networking stuff :)

Quoted 11 lines
 tools/testing/selftests/bpf/network_helpers.c |   2 +-
 tools/testing/selftests/bpf/network_helpers.h |   1 +
 .../selftests/bpf/prog_tests/tc_redirect.c    | 594 ++++++++++++++++++
 .../selftests/bpf/progs/test_tc_neigh.c       |  33 +-
 .../selftests/bpf/progs/test_tc_neigh_fib.c   |   9 +-
 .../selftests/bpf/progs/test_tc_peer.c        |  33 +-
 .../testing/selftests/bpf/test_tc_redirect.sh | 216 -------
 7 files changed, 622 insertions(+), 266 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tc_redirect.c
 delete mode 100755 tools/testing/selftests/bpf/test_tc_redirect.sh

[...]

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/tc_redirect.c new file +8 -0
+
+/**
+ * modify_proc() - Modify entry in /proc
+ *
+ * Modifies an entry in /proc and saves the original value for later
+ * restoration with restore_proc().
+ */
+static int modify_proc(const char *path, const char *newval)

tools/testing/selftests/bpf/prog_tests/tc_redirect.c line 88

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tc_redirect.c new file +10 -0
+{
+       struct proc_mod *mod;
+       FILE *f;
+
+       f = fopen(path, "r+");
+       if (!f)
+               return -1;
+
+       if (num_proc_mods + 1 > MAX_PROC_MODS)
+               goto fail;

you'll decrement num_proc_mods without incrementing it

Quoted 25 lines
tools/testing/selftests/bpf/prog_tests/tc_redirect.c new file +25 -0
+
+       num_proc_mods++;
+       mod = &proc_mods[num_proc_mods-1];
+
+       strncpy(mod->path, path, PATH_MAX);
+
+       if (!fread(mod->oldval, 1, MAX_PROC_VALUE_LEN, f)) {
+               log_err("reading from %s failed", path);
+               goto fail;
+       }
+       rewind(f);
+       if (fwrite(newval, strlen(newval), 1, f) != 1) {
+               log_err("writing to %s failed", path);
+               goto fail;
+       }
+
+       fclose(f);
+       return 0;
+fail:
+
+       fclose(f);
+       num_proc_mods--;
+       return -1;
+}
+

tools/testing/selftests/bpf/prog_tests/tc_redirect.c line 123

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tc_redirect.c new file +10 -0
+/**
+ * restore_proc() - Restore all /proc modifications
+ */
+static void restore_proc(void)
+{
+       int i;
+
+       for (i = 0; i < num_proc_mods; i++) {
+               struct proc_mod *mod = &proc_mods[i];
+               FILE *f = fopen(mod->path, "w");

we typically split variable declaration and function calls when the function can fail, so pretty much anything non-trivial will be done as a separate statement (followed immediately by error check)

Quoted 14 lines
tools/testing/selftests/bpf/prog_tests/tc_redirect.c new file +14 -0
+
+               if (!f) {
+                       log_err("fopen of %s failed", mod->path);
+                       continue;
+               }
+
+               if (fwrite(mod->oldval, mod->oldlen, 1, f) != 1)
+                       log_err("fwrite to %s failed", mod->path);
+
+               fclose(f);
+       }
+       num_proc_mods = 0;
+}
+

[...]

tools/testing/selftests/bpf/prog_tests/tc_redirect.c line 240

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/tc_redirect.c new file +5 -0
+
+static int netns_setup_links_and_routes(struct netns_setup_result *result)
+{
+       char veth_src_fwd_addr[IFADDR_STR_LEN+1] = {0,};
+       char veth_dst_fwd_addr[IFADDR_STR_LEN+1] = {0,};

just "= {};". same effect, but not misleading notation

Quoted 15 lines
tools/testing/selftests/bpf/prog_tests/tc_redirect.c new file +15 -0
+
+       SYS("ip link add veth_src type veth peer name veth_src_fwd");
+       SYS("ip link add veth_dst type veth peer name veth_dst_fwd");
+       if (get_ifaddr("veth_src_fwd", veth_src_fwd_addr))
+               goto fail;
+       if (get_ifaddr("veth_dst_fwd", veth_dst_fwd_addr))
+               goto fail;
+
+       result->ifindex_veth_src_fwd = get_ifindex("veth_src_fwd");
+       if (result->ifindex_veth_src_fwd < 0)
+               goto fail;
+       result->ifindex_veth_dst_fwd = get_ifindex("veth_dst_fwd");
+       if (result->ifindex_veth_dst_fwd < 0)
+               goto fail;
+

[...]

Quoted 21 lines
tools/testing/selftests/bpf/prog_tests/tc_redirect.c new file +21 -0
+static void test_tcp(int family, const char *addr, __u16 port)
+{
+       int listen_fd = -1, accept_fd = -1, client_fd = -1;
+       char buf[] = "testing testing";
+
+       if (!ASSERT_OK(setns_by_name(NS_DST), "setns dst"))
+               return;
+
+       listen_fd = start_server(family, SOCK_STREAM, addr, port, 0);
+       if (!ASSERT_GE(listen_fd, 0, "listen"))
+               goto done;
+
+       if (!ASSERT_OK(setns_by_name(NS_SRC), "setns src"))
+               goto done;
+
+       client_fd = connect_to_fd(listen_fd, TIMEOUT_MILLIS);
+       if (!ASSERT_GE(client_fd, 0, "connect_to_fd"))
+               goto done;
+
+       accept_fd = accept(listen_fd, NULL, NULL);
+       if (!ASSERT_GE(accept_fd, 0, "accept"))

tools/testing/selftests/bpf/prog_tests/tc_redirect.c line 389

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tc_redirect.c new file +10 -0
+               goto done;
+
+       if (!ASSERT_OK(settimeo(accept_fd, TIMEOUT_MILLIS), "settimeo"))
+               goto done;
+
+       if (!ASSERT_EQ(
+               write(client_fd, buf, sizeof(buf)),
+               sizeof(buf),
+               "send to server"))
+               goto done;

I think cases like this are much cleaner if written as:

n = write(client_fd, buf, sizeof(buf));
if (!ASSERT_EQ(n, sizeof(buf), "send to server"))

    goto done;

Don't try to do too much in a single if. Same below.

Quoted 43 lines
tools/testing/selftests/bpf/prog_tests/tc_redirect.c new file +43 -0
+
+       if (!ASSERT_EQ(
+               read(accept_fd, buf, sizeof(buf)),
+               sizeof(buf),
+               "recv from server"))
+               goto done;
+
+done:
+       setns_root();
+       if (listen_fd >= 0)
+               close(listen_fd);
+       if (accept_fd >= 0)
+               close(accept_fd);
+       if (client_fd >= 0)
+               close(client_fd);
+}
+
+static int test_ping(int family, const char *addr)
+{
+       const char *ping = family == AF_INET6 ? "ping6" : "ping";
+
+       SYS("ip netns exec " NS_SRC " %s " PING_ARGS " %s", ping, addr);
+       return 0;
+fail:
+       return -1;
+}
+
+static void test_connectivity(void)
+{
+       test_tcp(AF_INET, IP4_DST, IP4_PORT);
+       test_ping(AF_INET, IP4_DST);
+       test_tcp(AF_INET6, IP6_DST, IP6_PORT);
+       test_ping(AF_INET6, IP6_DST);
+}
+
+static void test_tc_redirect_neigh_fib(struct netns_setup_result *setup_result)
+{
+       struct test_tc_neigh_fib *skel;
+
+       skel = test_tc_neigh_fib__open();
+       if (!ASSERT_OK_PTR(skel, "test_tc_neigh_fib__open"))
+               return;
+

tools/testing/selftests/bpf/prog_tests/tc_redirect.c line 442

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tc_redirect.c new file +10 -0
+       if (!ASSERT_OK(test_tc_neigh_fib__load(skel), "test_tc_neigh_fib__load")) {
+               test_tc_neigh_fib__destroy(skel);
+               return;
+       }
+
+       if (!ASSERT_OK(
+               bpf_program__pin(skel->progs.tc_src, SRC_PROG_PIN_FILE),
+               "pin " SRC_PROG_PIN_FILE))
+               goto done;
+

see above, use

err = bpf_program__pin(...);
if (!ASSERT_OK(err, ...))

That actually makes it easier to debug as well, btw.

Quoted 35 lines
tools/testing/selftests/bpf/prog_tests/tc_redirect.c new file +35 -0
+       if (!ASSERT_OK(
+               bpf_program__pin(skel->progs.tc_chk, CHK_PROG_PIN_FILE),
+               "pin " CHK_PROG_PIN_FILE))
+               goto done;
+
+       if (!ASSERT_OK(
+               bpf_program__pin(skel->progs.tc_dst, DST_PROG_PIN_FILE),
+               "pin " DST_PROG_PIN_FILE))
+               goto done;
+
+       if (netns_load_bpf())
+               goto done;
+
+       /* bpf_fib_lookup() checks if forwarding is enabled */
+       if (!ASSERT_OK(setns_by_name(NS_FWD), "setns fwd"))
+               goto done;
+       if (!ASSERT_OK(modify_proc("/proc/sys/net/ipv4/ip_forward", "1"),
+                                  "set ipv4.ip_forward"))
+               goto done;
+       if (!ASSERT_OK(modify_proc("/proc/sys/net/ipv6/conf/all/forwarding", "1"),
+                                  "set ipv6.forwarding"))
+               goto done;
+       setns_root();
+
+       test_connectivity();
+done:
+       bpf_program__unpin(skel->progs.tc_src, SRC_PROG_PIN_FILE);
+       bpf_program__unpin(skel->progs.tc_chk, CHK_PROG_PIN_FILE);
+       bpf_program__unpin(skel->progs.tc_dst, DST_PROG_PIN_FILE);
+       test_tc_neigh_fib__destroy(skel);
+       netns_unload_bpf();
+       setns_root();
+       restore_proc();
+}
+

[...]

author
#5 lore raw

PATCH v3 1/1 selftests/bpf: Rewrite test_tc_redirect.sh as prog_tests/tc_redirect.c

In reply to Jussi Maki

Ports test_tc_redirect.sh to the test_progs framework and removes the old test. This makes it more in line with rest of the tests and makes it possible to run this test with vmtest.sh and under the bpf CI.

---
 tools/testing/selftests/bpf/network_helpers.c |   2 +-
 tools/testing/selftests/bpf/network_helpers.h |   1 +
 .../selftests/bpf/prog_tests/tc_redirect.c    | 589 ++++++++++++++++++
 .../selftests/bpf/progs/test_tc_neigh.c       |  33 +-
 .../selftests/bpf/progs/test_tc_neigh_fib.c   |   9 +-
 .../selftests/bpf/progs/test_tc_peer.c        |  33 +-
 .../testing/selftests/bpf/test_tc_redirect.sh | 216 -------
 7 files changed, 617 insertions(+), 266 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tc_redirect.c
 delete mode 100755 tools/testing/selftests/bpf/test_tc_redirect.sh
tools/testing/selftests/bpf/network_helpers.c +1 -1
index 12ee40284da0..2060bc122c53 100644
@@ -40,7 +40,7 @@ struct ipv6_packet pkt_v6 = {
 	.tcp.doff = 5,
 };
 
-static int settimeo(int fd, int timeout_ms)
+int settimeo(int fd, int timeout_ms)
 {
 	struct timeval timeout = { .tv_sec = 3 };
 
tools/testing/selftests/bpf/network_helpers.h +1 -0
index 7205f8afdba1..5e0d51c07b63 100644
@@ -33,6 +33,7 @@ struct ipv6_packet {
 } __packed;
 extern struct ipv6_packet pkt_v6;
 
+int settimeo(int fd, int timeout_ms);
 int start_server(int family, int type, const char *addr, __u16 port,
 		 int timeout_ms);
 int connect_to_fd(int server_fd, int timeout_ms);
tools/testing/selftests/bpf/prog_tests/tc_redirect.c new file +589 -0

589 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_tc_neigh.c +10 -23
index b985ac4e7a81..90f64a85998f 100644
@@ -33,17 +33,8 @@
 				 a.s6_addr32[3] == b.s6_addr32[3])
 #endif
 
-enum {
-	dev_src,
-	dev_dst,
-};
-
-struct bpf_map_def SEC("maps") ifindex_map = {
-	.type		= BPF_MAP_TYPE_ARRAY,
-	.key_size	= sizeof(int),
-	.value_size	= sizeof(int),
-	.max_entries	= 2,
-};
+static volatile const __u32 IFINDEX_SRC;
+static volatile const __u32 IFINDEX_DST;
 
 static __always_inline bool is_remote_ep_v4(struct __sk_buff *skb,
 					    __be32 addr)
@@ -79,14 +70,8 @@ static __always_inline bool is_remote_ep_v6(struct __sk_buff *skb,
 	return v6_equal(ip6h->daddr, addr);
 }
 
-static __always_inline int get_dev_ifindex(int which)
-{
-	int *ifindex = bpf_map_lookup_elem(&ifindex_map, &which);
-
-	return ifindex ? *ifindex : 0;
-}
-
-SEC("chk_egress") int tc_chk(struct __sk_buff *skb)
+SEC("classifier/chk_egress")
+int tc_chk(struct __sk_buff *skb)
 {
 	void *data_end = ctx_ptr(skb->data_end);
 	void *data = ctx_ptr(skb->data);
@@ -98,7 +83,8 @@ SEC("chk_egress") int tc_chk(struct __sk_buff *skb)
 	return !raw[0] && !raw[1] && !raw[2] ? TC_ACT_SHOT : TC_ACT_OK;
 }
 
-SEC("dst_ingress") int tc_dst(struct __sk_buff *skb)
+SEC("classifier/dst_ingress")
+int tc_dst(struct __sk_buff *skb)
 {
 	__u8 zero[ETH_ALEN * 2];
 	bool redirect = false;
@@ -119,10 +105,11 @@ SEC("dst_ingress") int tc_dst(struct __sk_buff *skb)
 	if (bpf_skb_store_bytes(skb, 0, &zero, sizeof(zero), 0) < 0)
 		return TC_ACT_SHOT;
 
-	return bpf_redirect_neigh(get_dev_ifindex(dev_src), NULL, 0, 0);
+	return bpf_redirect_neigh(IFINDEX_SRC, NULL, 0, 0);
 }
 
-SEC("src_ingress") int tc_src(struct __sk_buff *skb)
+SEC("classifier/src_ingress")
+int tc_src(struct __sk_buff *skb)
 {
 	__u8 zero[ETH_ALEN * 2];
 	bool redirect = false;
@@ -143,7 +130,7 @@ SEC("src_ingress") int tc_src(struct __sk_buff *skb)
 	if (bpf_skb_store_bytes(skb, 0, &zero, sizeof(zero), 0) < 0)
 		return TC_ACT_SHOT;
 
-	return bpf_redirect_neigh(get_dev_ifindex(dev_dst), NULL, 0, 0);
+	return bpf_redirect_neigh(IFINDEX_DST, NULL, 0, 0);
 }
 
 char __license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/test_tc_neigh_fib.c +6 -3
index d82ed3457030..f7ab69cf018e 100644
@@ -75,7 +75,8 @@ static __always_inline int fill_fib_params_v6(struct __sk_buff *skb,
 	return 0;
 }
 
-SEC("chk_egress") int tc_chk(struct __sk_buff *skb)
+SEC("classifier/chk_egress")
+int tc_chk(struct __sk_buff *skb)
 {
 	void *data_end = ctx_ptr(skb->data_end);
 	void *data = ctx_ptr(skb->data);
@@ -142,12 +143,14 @@ static __always_inline int tc_redir(struct __sk_buff *skb)
 /* these are identical, but keep them separate for compatibility with the
  * section names expected by test_tc_redirect.sh
  */
-SEC("dst_ingress") int tc_dst(struct __sk_buff *skb)
+SEC("classifier/dst_ingress")
+int tc_dst(struct __sk_buff *skb)
 {
 	return tc_redir(skb);
 }
 
-SEC("src_ingress") int tc_src(struct __sk_buff *skb)
+SEC("classifier/src_ingress")
+int tc_src(struct __sk_buff *skb)
 {
 	return tc_redir(skb);
 }
tools/testing/selftests/bpf/progs/test_tc_peer.c +10 -23
index fc84a7685aa2..72c72950c3bb 100644
@@ -8,38 +8,25 @@
 
 #include <bpf/bpf_helpers.h>
 
-enum {
-	dev_src,
-	dev_dst,
-};
+static volatile const __u32 IFINDEX_SRC;
+static volatile const __u32 IFINDEX_DST;
 
-struct bpf_map_def SEC("maps") ifindex_map = {
-	.type		= BPF_MAP_TYPE_ARRAY,
-	.key_size	= sizeof(int),
-	.value_size	= sizeof(int),
-	.max_entries	= 2,
-};
-
-static __always_inline int get_dev_ifindex(int which)
-{
-	int *ifindex = bpf_map_lookup_elem(&ifindex_map, &which);
-
-	return ifindex ? *ifindex : 0;
-}
-
-SEC("chk_egress") int tc_chk(struct __sk_buff *skb)
+SEC("classifier/chk_egress")
+int tc_chk(struct __sk_buff *skb)
 {
 	return TC_ACT_SHOT;
 }
 
-SEC("dst_ingress") int tc_dst(struct __sk_buff *skb)
+SEC("classifier/dst_ingress")
+int tc_dst(struct __sk_buff *skb)
 {
-	return bpf_redirect_peer(get_dev_ifindex(dev_src), 0);
+	return bpf_redirect_peer(IFINDEX_SRC, 0);
 }
 
-SEC("src_ingress") int tc_src(struct __sk_buff *skb)
+SEC("classifier/src_ingress")
+int tc_src(struct __sk_buff *skb)
 {
-	return bpf_redirect_peer(get_dev_ifindex(dev_dst), 0);
+	return bpf_redirect_peer(IFINDEX_DST, 0);
 }
 
 char __license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/test_tc_redirect.sh deleted +0 -216

216 lines not on this page · raw message

Signature · 1 line
2.30.2
#6 lore raw
This patch was applied to bpf/bpf.git (refs/heads/master):

Hello:

This patch was applied to bpf/bpf.git (refs/heads/master):

Quoted 5 lines · On Wed, 5 May 2021 08:59:25 +0000 you wrote:

Ports test_tc_redirect.sh to the test_progs framework and removes the old test. This makes it more in line with rest of the tests and makes it possible to run this test with vmtest.sh and under the bpf CI.

Quoted 11 lines
---
 tools/testing/selftests/bpf/network_helpers.c |   2 +-
 tools/testing/selftests/bpf/network_helpers.h |   1 +
 .../selftests/bpf/prog_tests/tc_redirect.c    | 589 ++++++++++++++++++
 .../selftests/bpf/progs/test_tc_neigh.c       |  33 +-
 .../selftests/bpf/progs/test_tc_neigh_fib.c   |   9 +-
 .../selftests/bpf/progs/test_tc_peer.c        |  33 +-
 .../testing/selftests/bpf/test_tc_redirect.sh | 216 -------
 7 files changed, 617 insertions(+), 266 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tc_redirect.c
 delete mode 100755 tools/testing/selftests/bpf/test_tc_redirect.sh

Here is the summary with links:
- [bpf,v3] selftests/bpf: Rewrite test_tc_redirect.sh as prog_tests/tc_redirect.c

    https://git.kernel.org/bpf/bpf/c/096eccdef0b3

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html

#7 lore raw
In reply to Jussi Maki
Quoted 7 lines · Jussi Maki

Ports test_tc_redirect.sh to the test_progs framework and removes the old test. This makes it more in line with rest of the tests and makes it possible to run this test with vmtest.sh and under the bpf CI.

---

Hey Jussi,

I noticed that running these new tc_redirect tests locally in my qemu image were failing. I narrowed it down to ping6 vs ping -6 differences. My image lacks ping6. I see that there were previous attempts to gracefully handle them both:

da85d8bfd151 ("kselftests/bpf: use ping6 as the default ipv6 ping binary when it exists")
deee2cae27d1 ("kselftests/bpf: use ping6 as the default ipv6 ping binary if it exists")

(those are two different commits, yeah :) )

It seems the shell script version used to handle this more gracefully. Can you please update the test to handle the lack of ping6 binary?

Quoted 11 lines
 tools/testing/selftests/bpf/network_helpers.c |   2 +-
 tools/testing/selftests/bpf/network_helpers.h |   1 +
 .../selftests/bpf/prog_tests/tc_redirect.c    | 481 ++++++++++++++++++
 .../selftests/bpf/progs/test_tc_neigh.c       |  33 +-
 .../selftests/bpf/progs/test_tc_neigh_fib.c   |   9 +-
 .../selftests/bpf/progs/test_tc_peer.c        |  33 +-
 .../testing/selftests/bpf/test_tc_redirect.sh | 216 --------
 7 files changed, 509 insertions(+), 266 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tc_redirect.c
 delete mode 100755 tools/testing/selftests/bpf/test_tc_redirect.sh

[...]

author
#8 lore raw

PATCH 1/1 selftests/bpf: Use ping6 only if available in tc_redirect

In the tc_redirect test only use ping6 if it's available and otherwise fall back to using "ping -6".

---
 .../selftests/bpf/prog_tests/tc_redirect.c    | 23 ++++++++++++++-----
 1 file changed, 17 insertions(+), 6 deletions(-)
tools/testing/selftests/bpf/prog_tests/tc_redirect.c +17 -6
index 5703c918812b..932e4ee3f97c 100644
@@ -13,15 +13,16 @@
 #define _GNU_SOURCE
 
 #include <arpa/inet.h>
+#include <linux/if.h>
+#include <linux/if_tun.h>
 #include <linux/limits.h>
 #include <linux/sysctl.h>
-#include <linux/if_tun.h>
-#include <linux/if.h>
 #include <sched.h>
 #include <stdbool.h>
 #include <stdio.h>
-#include <sys/stat.h>
 #include <sys/mount.h>
+#include <sys/stat.h>
+#include <unistd.h>
 
 #include "test_progs.h"
 #include "network_helpers.h"
@@ -389,11 +390,21 @@ static void test_tcp(int family, const char *addr, __u16 port)
 		close(client_fd);
 }
 
-static int test_ping(int family, const char *addr)
+static char *ping_command(int family)
 {
-	const char *ping = family == AF_INET6 ? "ping6" : "ping";
+	if (family == AF_INET6) {
+		/* On some systems 'ping' doesn't support IPv6, so use ping6 if it is present. */
+		if (!system("which ping6 >/dev/null 2>&1"))
+			return "ping6";
+		else
+			return "ping -6";
+	}
+	return "ping";
+}
 
-	SYS("ip netns exec " NS_SRC " %s " PING_ARGS " %s > /dev/null", ping, addr);
+static int test_ping(int family, const char *addr)
+{
+	SYS("ip netns exec " NS_SRC " %s " PING_ARGS " %s > /dev/null", ping_command(family), addr);
 	return 0;
 fail:
 	return -1;
Signature · 1 line
2.17.1
#9 lore raw

Re: [PATCH bpf] selftests/bpf: Use ping6 only if available in tc_redirect

Quoted 6 lines · Jussi Maki

In the tc_redirect test only use ping6 if it's available and otherwise fall back to using "ping -6".

---

The tests weren't broken per se, they just required a more particular environment set up. So I consider this an improvement rather than bug fix and applied this to bpf-next, where it's much more important to have all the tests passing in an ongoing fashion. Thanks!

Quoted 3 lines
 .../selftests/bpf/prog_tests/tc_redirect.c    | 23 ++++++++++++++-----
 1 file changed, 17 insertions(+), 6 deletions(-)

[...]