Commit 0a099d14 authored by Alexander Duyck's avatar Alexander Duyck Committed by Alexei Starovoitov

selftests/bpf: Migrate tcpbpf_user.c to use BPF skeleton

Update tcpbpf_user.c to make use of the BPF skeleton. Doing this we can
simplify test_tcpbpf_user and reduce the overhead involved in setting up
the test.

In addition we can clean up the remaining bits such as the one remaining
CHECK_FAIL at the end of test_tcpbpf_user so that the function only makes
use of CHECK as needed.
Signed-off-by: default avatarAlexander Duyck <alexanderduyck@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
Acked-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/160443931155.1086697.17869006617113525162.stgit@localhost.localdomain
parent d3813ea1
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include <network_helpers.h> #include <network_helpers.h>
#include "test_tcpbpf.h" #include "test_tcpbpf.h"
#include "test_tcpbpf_kern.skel.h"
#define LO_ADDR6 "::1" #define LO_ADDR6 "::1"
#define CG_NAME "/tcpbpf-user-test" #define CG_NAME "/tcpbpf-user-test"
...@@ -130,44 +131,30 @@ static void run_test(int map_fd, int sock_map_fd) ...@@ -130,44 +131,30 @@ static void run_test(int map_fd, int sock_map_fd)
void test_tcpbpf_user(void) void test_tcpbpf_user(void)
{ {
const char *file = "test_tcpbpf_kern.o"; struct test_tcpbpf_kern *skel;
int prog_fd, map_fd, sock_map_fd; int map_fd, sock_map_fd;
int error = EXIT_FAILURE;
struct bpf_object *obj;
int cg_fd = -1; int cg_fd = -1;
int rv;
cg_fd = test__join_cgroup(CG_NAME); skel = test_tcpbpf_kern__open_and_load();
if (cg_fd < 0) if (CHECK(!skel, "open and load skel", "failed"))
goto err; return;
if (bpf_prog_load(file, BPF_PROG_TYPE_SOCK_OPS, &obj, &prog_fd)) {
printf("FAILED: load_bpf_file failed for: %s\n", file);
goto err;
}
rv = bpf_prog_attach(prog_fd, cg_fd, BPF_CGROUP_SOCK_OPS, 0); cg_fd = test__join_cgroup(CG_NAME);
if (rv) { if (CHECK(cg_fd < 0, "test__join_cgroup(" CG_NAME ")",
printf("FAILED: bpf_prog_attach: %d (%s)\n", "cg_fd:%d errno:%d", cg_fd, errno))
error, strerror(errno));
goto err; goto err;
}
map_fd = bpf_find_map(__func__, obj, "global_map"); map_fd = bpf_map__fd(skel->maps.global_map);
if (map_fd < 0) sock_map_fd = bpf_map__fd(skel->maps.sockopt_results);
goto err;
sock_map_fd = bpf_find_map(__func__, obj, "sockopt_results"); skel->links.bpf_testcb = bpf_program__attach_cgroup(skel->progs.bpf_testcb, cg_fd);
if (sock_map_fd < 0) if (!ASSERT_OK_PTR(skel->links.bpf_testcb, "attach_cgroup(bpf_testcb)"))
goto err; goto err;
run_test(map_fd, sock_map_fd); run_test(map_fd, sock_map_fd);
error = 0;
err: err:
bpf_prog_detach(cg_fd, BPF_CGROUP_SOCK_OPS);
if (cg_fd != -1) if (cg_fd != -1)
close(cg_fd); close(cg_fd);
test_tcpbpf_kern__destroy(skel);
CHECK_FAIL(error);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment