Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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>

authored by

Jordan Rife and committed by
Alexei Starovoitov
5a047b22 5eff48f3

+34 -1
+34 -1
tools/testing/selftests/bpf/prog_tests/sock_addr.c
··· 367 367 } expected_result; 368 368 }; 369 369 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 + 370 402 #define BPF_SKEL_FUNCS(skel_name, prog_name) \ 371 403 static void *prog_name##_load(int cgroup_fd, \ 372 404 enum bpf_attach_type attach_type, \ ··· 1374 1342 continue; 1375 1343 1376 1344 skel = test->loadfn(cgroup_fd, test->attach_type, 1377 - test->expected_result == LOAD_REJECT); 1345 + test->expected_result == LOAD_REJECT || 1346 + test->expected_result == ATTACH_REJECT); 1378 1347 if (!skel) 1379 1348 continue; 1380 1349