Skip to content

Commit 5a047b2

Browse files
jrifeAlexei Starovoitov
authored andcommitted
selftests/bpf: Handle ATTACH_REJECT test cases
In preparation to move test cases from bpf/test_sock_addr.c that expect ATTACH_REJECT, this patch adds BPF_SKEL_FUNCS_RAW to generate load and destroy functions that use bpf_prog_attach() to control the attach_type. The normal load functions use bpf_program__attach_cgroup which does not have the same degree of control over the attach type, as bpf_program_attach_fd() calls bpf_link_create() with the attach type extracted from prog using bpf_program__expected_attach_type(). It is currently not possible to modify the attach type before bpf_program__attach_cgroup() is called, since bpf_program__set_expected_attach_type() has no effect after the program is loaded. Signed-off-by: Jordan Rife <jrife@google.com> Link: https://lore.kernel.org/r/20240510190246.3247730-5-jrife@google.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 5eff48f commit 5a047b2

1 file changed

Lines changed: 34 additions & 1 deletion

File tree

tools/testing/selftests/bpf/prog_tests/sock_addr.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,38 @@ struct sock_addr_test {
367367
} expected_result;
368368
};
369369

370+
#define BPF_SKEL_FUNCS_RAW(skel_name, prog_name) \
371+
static void *prog_name##_load_raw(int cgroup_fd, \
372+
enum bpf_attach_type attach_type, \
373+
bool expect_reject) \
374+
{ \
375+
struct skel_name *skel = skel_name##__open(); \
376+
int prog_fd = -1; \
377+
if (!ASSERT_OK_PTR(skel, "skel_open")) \
378+
goto cleanup; \
379+
if (!ASSERT_OK(skel_name##__load(skel), "load")) \
380+
goto cleanup; \
381+
prog_fd = bpf_program__fd(skel->progs.prog_name); \
382+
if (!ASSERT_GT(prog_fd, 0, "prog_fd")) \
383+
goto cleanup; \
384+
if (bpf_prog_attach(prog_fd, cgroup_fd, attach_type, \
385+
BPF_F_ALLOW_OVERRIDE), "bpf_prog_attach") { \
386+
ASSERT_TRUE(expect_reject, "unexpected rejection"); \
387+
goto cleanup; \
388+
} \
389+
if (!ASSERT_FALSE(expect_reject, "expected rejection")) \
390+
goto cleanup; \
391+
cleanup: \
392+
if (prog_fd > 0) \
393+
bpf_prog_detach(cgroup_fd, attach_type); \
394+
skel_name##__destroy(skel); \
395+
return NULL; \
396+
} \
397+
static void prog_name##_destroy_raw(void *progfd) \
398+
{ \
399+
/* No-op. *_load_raw does all cleanup. */ \
400+
} \
401+
370402
#define BPF_SKEL_FUNCS(skel_name, prog_name) \
371403
static void *prog_name##_load(int cgroup_fd, \
372404
enum bpf_attach_type attach_type, \
@@ -1342,7 +1374,8 @@ void test_sock_addr(void)
13421374
continue;
13431375

13441376
skel = test->loadfn(cgroup_fd, test->attach_type,
1345-
test->expected_result == LOAD_REJECT);
1377+
test->expected_result == LOAD_REJECT ||
1378+
test->expected_result == ATTACH_REJECT);
13461379
if (!skel)
13471380
continue;
13481381

0 commit comments

Comments
 (0)