[PATCH bpf-next 4/5] selftests/bpf: sockmap_redir: Let test specify skel's redirect_type

Michal Luczaj
2025-09-05T11:12:06Z

Preparatory patch before adding SK_PASS/SK_DROP support: allow to
dynamically switch BPF program's redirect_type. This way, after setting up
for a redirection, test can make the BPF program skip the actual
bpf_{sk,msg}_redirect_{map,hash} part and return a specified verdict.

Signed-off-by: Michal Luczaj <mhal@rbox.co>
---
tools/testing/selftests/bpf/prog_tests/sockmap_redir.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_redir.c b/tools/testing/selftests/bpf/prog_tests/sockmap_redir.c
index 9fc5b4dd323d8554e6dc89d06be9054612e41020..f89df1ca1174800d801f221a819b099abf911450 100644
--- a/tools/testing/selftests/bpf/prog_tests/sockmap_redir.c
+++ b/tools/testing/selftests/bpf/prog_tests/sockmap_redir.c
@@ -371,7 +371,7 @@ static void test_redir(enum bpf_map_type type, struct redir_spec *redir,
}

static void test_sockets(enum bpf_map_type type, struct redir_spec *redir,
- struct maps *maps)
+ struct maps *maps, int *skel_redir_type)
{
struct socket_spec *s, sockets[] = {
{ AF_INET, SOCK_STREAM },
@@ -392,6 +392,8 @@ static void test_sockets(enum bpf_map_type type, struct redir_spec *redir,
if (socket_spec_pairs(s))
goto out;

+ *skel_redir_type = type;
+
/* Intra-proto */
for (s = sockets; s < sockets + ARRAY_SIZE(sockets); s++)
test_redir(type, redir, maps, s, s);
@@ -451,7 +453,6 @@ static void test_map(enum bpf_map_type type)
return;
}

- skel->bss->redirect_type = type;
maps.verd = bpf_map__fd(skel->maps.verdict_map);
get_redir_params(r, skel, &prog_fd, &attach_type,
&skel->bss->redirect_flags);
@@ -459,7 +460,7 @@ static void test_map(enum bpf_map_type type)
if (xbpf_prog_attach(prog_fd, maps.in, attach_type, 0))
return;

- test_sockets(type, r, &maps);
+ test_sockets(type, r, &maps, &skel->bss->redirect_type);

if (xbpf_prog_detach2(prog_fd, maps.in, attach_type))
return;

--
2.50.1