Skip to content

Commit

Permalink
selftests/bpf: Add a cgroup prog bpf_get_ns_current_pid_tgid() test
Browse files Browse the repository at this point in the history
Add a cgroup bpf program test where the bpf program is running
in a pid namespace. The test is successfully:
  #165/3   ns_current_pid_tgid/new_ns_cgrp:OK

Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20240315184910.2976522-1-yonghong.song@linux.dev
  • Loading branch information
Yonghong Song authored and anakryiko committed Mar 19, 2024
1 parent 4d4bd29 commit 87ade6c
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
73 changes: 73 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <sys/wait.h>
#include <sys/mount.h>
#include <sys/fcntl.h>
#include "network_helpers.h"

#define STACK_SIZE (1024 * 1024)
static char child_stack[STACK_SIZE];
Expand Down Expand Up @@ -74,6 +75,50 @@ static int test_current_pid_tgid_tp(void *args)
return ret;
}

static int test_current_pid_tgid_cgrp(void *args)
{
struct test_ns_current_pid_tgid__bss *bss;
struct test_ns_current_pid_tgid *skel;
int server_fd = -1, ret = -1, err;
int cgroup_fd = *(int *)args;
pid_t tgid, pid;

skel = test_ns_current_pid_tgid__open();
if (!ASSERT_OK_PTR(skel, "test_ns_current_pid_tgid__open"))
return ret;

bpf_program__set_autoload(skel->progs.cgroup_bind4, true);

err = test_ns_current_pid_tgid__load(skel);
if (!ASSERT_OK(err, "test_ns_current_pid_tgid__load"))
goto cleanup;

bss = skel->bss;
if (get_pid_tgid(&pid, &tgid, bss))
goto cleanup;

skel->links.cgroup_bind4 = bpf_program__attach_cgroup(
skel->progs.cgroup_bind4, cgroup_fd);
if (!ASSERT_OK_PTR(skel->links.cgroup_bind4, "bpf_program__attach_cgroup"))
goto cleanup;

server_fd = start_server(AF_INET, SOCK_STREAM, NULL, 0, 0);
if (!ASSERT_GE(server_fd, 0, "start_server"))
goto cleanup;

if (!ASSERT_EQ(bss->user_pid, pid, "pid"))
goto cleanup;
if (!ASSERT_EQ(bss->user_tgid, tgid, "tgid"))
goto cleanup;
ret = 0;

cleanup:
if (server_fd >= 0)
close(server_fd);
test_ns_current_pid_tgid__destroy(skel);
return ret;
}

static void test_ns_current_pid_tgid_new_ns(int (*fn)(void *), void *arg)
{
int wstatus;
Expand All @@ -95,11 +140,39 @@ static void test_ns_current_pid_tgid_new_ns(int (*fn)(void *), void *arg)
return;
}

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");
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");
}

/* TODO: use a different tracepoint */
void serial_test_ns_current_pid_tgid(void)
{
if (test__start_subtest("root_ns_tp"))
test_current_pid_tgid_tp(NULL);
if (test__start_subtest("new_ns_tp"))
test_ns_current_pid_tgid_new_ns(test_current_pid_tgid_tp, NULL);
if (test__start_subtest("new_ns_cgrp")) {
int cgroup_fd = -1;

cgroup_fd = test__join_cgroup("/sock_addr");
if (ASSERT_GE(cgroup_fd, 0, "join_cgroup")) {
test_in_netns(test_current_pid_tgid_cgrp, &cgroup_fd);
close(cgroup_fd);
}
}
}
7 changes: 7 additions & 0 deletions tools/testing/selftests/bpf/progs/test_ns_current_pid_tgid.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,11 @@ int tp_handler(const void *ctx)
return 0;
}

SEC("?cgroup/bind4")
int cgroup_bind4(struct bpf_sock_addr *ctx)
{
get_pid_tgid();
return 1;
}

char _license[] SEC("license") = "GPL";

0 comments on commit 87ade6c

Please sign in to comment.