Skip to content

Commit

Permalink
selftests/bpf: Add weak/typeless ksym test for light skeleton
Browse files Browse the repository at this point in the history
Also, avoid using CO-RE features, as lskel doesn't support CO-RE, yet.
Include both light and libbpf skeleton in same file to test both of them
together.

In c48e51c ("bpf: selftests: Add selftests for module kfunc support"),
I added support for generating both lskel and libbpf skel for a BPF
object, however the name parameter for bpftool caused collisions when
included in same file together. This meant that every test needed a
separate file for a libbpf/light skeleton separation instead of
subtests.

Change that by appending a "_lskel" suffix to the name for files using
light skeleton, and convert all existing users.

Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20211028063501.2239335-7-memxor@gmail.com
  • Loading branch information
Kumar Kartikeya Dwivedi authored and Alexei Starovoitov committed Oct 28, 2021
1 parent 92274e2 commit 087cba7
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 107 deletions.
4 changes: 2 additions & 2 deletions tools/testing/selftests/bpf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \
LSKELS := kfunc_call_test.c fentry_test.c fexit_test.c fexit_sleep.c \
test_ringbuf.c atomics.c trace_printk.c trace_vprintk.c
# Generate both light skeleton and libbpf skeleton for these
LSKELS_EXTRA := test_ksyms_module.c
LSKELS_EXTRA := test_ksyms_module.c test_ksyms_weak.c
SKEL_BLACKLIST += $$(LSKELS)

test_static_linked.skel.h-deps := test_static_linked1.o test_static_linked2.o
Expand Down Expand Up @@ -404,7 +404,7 @@ $(TRUNNER_BPF_LSKELS): %.lskel.h: %.o $(BPFTOOL) | $(TRUNNER_OUTPUT)
$(Q)$$(BPFTOOL) gen object $$(<:.o=.linked2.o) $$(<:.o=.linked1.o)
$(Q)$$(BPFTOOL) gen object $$(<:.o=.linked3.o) $$(<:.o=.linked2.o)
$(Q)diff $$(<:.o=.linked2.o) $$(<:.o=.linked3.o)
$(Q)$$(BPFTOOL) gen skeleton -L $$(<:.o=.linked3.o) name $$(notdir $$(<:.o=)) > $$@
$(Q)$$(BPFTOOL) gen skeleton -L $$(<:.o=.linked3.o) name $$(notdir $$(<:.o=_lskel)) > $$@

$(TRUNNER_BPF_SKELS_LINKED): $(TRUNNER_BPF_OBJS) $(BPFTOOL) | $(TRUNNER_OUTPUT)
$$(call msg,LINK-BPF,$(TRUNNER_BINARY),$$(@:.skel.h=.o))
Expand Down
34 changes: 17 additions & 17 deletions tools/testing/selftests/bpf/prog_tests/atomics.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

#include "atomics.lskel.h"

static void test_add(struct atomics *skel)
static void test_add(struct atomics_lskel *skel)
{
int err, prog_fd;
__u32 duration = 0, retval;
int link_fd;

link_fd = atomics__add__attach(skel);
link_fd = atomics_lskel__add__attach(skel);
if (!ASSERT_GT(link_fd, 0, "attach(add)"))
return;

Expand All @@ -36,13 +36,13 @@ static void test_add(struct atomics *skel)
close(link_fd);
}

static void test_sub(struct atomics *skel)
static void test_sub(struct atomics_lskel *skel)
{
int err, prog_fd;
__u32 duration = 0, retval;
int link_fd;

link_fd = atomics__sub__attach(skel);
link_fd = atomics_lskel__sub__attach(skel);
if (!ASSERT_GT(link_fd, 0, "attach(sub)"))
return;

Expand All @@ -69,13 +69,13 @@ static void test_sub(struct atomics *skel)
close(link_fd);
}

static void test_and(struct atomics *skel)
static void test_and(struct atomics_lskel *skel)
{
int err, prog_fd;
__u32 duration = 0, retval;
int link_fd;

link_fd = atomics__and__attach(skel);
link_fd = atomics_lskel__and__attach(skel);
if (!ASSERT_GT(link_fd, 0, "attach(and)"))
return;

Expand All @@ -97,13 +97,13 @@ static void test_and(struct atomics *skel)
close(link_fd);
}

static void test_or(struct atomics *skel)
static void test_or(struct atomics_lskel *skel)
{
int err, prog_fd;
__u32 duration = 0, retval;
int link_fd;

link_fd = atomics__or__attach(skel);
link_fd = atomics_lskel__or__attach(skel);
if (!ASSERT_GT(link_fd, 0, "attach(or)"))
return;

Expand All @@ -126,13 +126,13 @@ static void test_or(struct atomics *skel)
close(link_fd);
}

static void test_xor(struct atomics *skel)
static void test_xor(struct atomics_lskel *skel)
{
int err, prog_fd;
__u32 duration = 0, retval;
int link_fd;

link_fd = atomics__xor__attach(skel);
link_fd = atomics_lskel__xor__attach(skel);
if (!ASSERT_GT(link_fd, 0, "attach(xor)"))
return;

Expand All @@ -154,13 +154,13 @@ static void test_xor(struct atomics *skel)
close(link_fd);
}

static void test_cmpxchg(struct atomics *skel)
static void test_cmpxchg(struct atomics_lskel *skel)
{
int err, prog_fd;
__u32 duration = 0, retval;
int link_fd;

link_fd = atomics__cmpxchg__attach(skel);
link_fd = atomics_lskel__cmpxchg__attach(skel);
if (!ASSERT_GT(link_fd, 0, "attach(cmpxchg)"))
return;

Expand All @@ -183,13 +183,13 @@ static void test_cmpxchg(struct atomics *skel)
close(link_fd);
}

static void test_xchg(struct atomics *skel)
static void test_xchg(struct atomics_lskel *skel)
{
int err, prog_fd;
__u32 duration = 0, retval;
int link_fd;

link_fd = atomics__xchg__attach(skel);
link_fd = atomics_lskel__xchg__attach(skel);
if (!ASSERT_GT(link_fd, 0, "attach(xchg)"))
return;

Expand All @@ -212,10 +212,10 @@ static void test_xchg(struct atomics *skel)

void test_atomics(void)
{
struct atomics *skel;
struct atomics_lskel *skel;
__u32 duration = 0;

skel = atomics__open_and_load();
skel = atomics_lskel__open_and_load();
if (CHECK(!skel, "skel_load", "atomics skeleton failed\n"))
return;

Expand Down Expand Up @@ -243,5 +243,5 @@ void test_atomics(void)
test_xchg(skel);

cleanup:
atomics__destroy(skel);
atomics_lskel__destroy(skel);
}
16 changes: 8 additions & 8 deletions tools/testing/selftests/bpf/prog_tests/fentry_fexit.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@

void test_fentry_fexit(void)
{
struct fentry_test *fentry_skel = NULL;
struct fexit_test *fexit_skel = NULL;
struct fentry_test_lskel *fentry_skel = NULL;
struct fexit_test_lskel *fexit_skel = NULL;
__u64 *fentry_res, *fexit_res;
__u32 duration = 0, retval;
int err, prog_fd, i;

fentry_skel = fentry_test__open_and_load();
fentry_skel = fentry_test_lskel__open_and_load();
if (CHECK(!fentry_skel, "fentry_skel_load", "fentry skeleton failed\n"))
goto close_prog;
fexit_skel = fexit_test__open_and_load();
fexit_skel = fexit_test_lskel__open_and_load();
if (CHECK(!fexit_skel, "fexit_skel_load", "fexit skeleton failed\n"))
goto close_prog;

err = fentry_test__attach(fentry_skel);
err = fentry_test_lskel__attach(fentry_skel);
if (CHECK(err, "fentry_attach", "fentry attach failed: %d\n", err))
goto close_prog;
err = fexit_test__attach(fexit_skel);
err = fexit_test_lskel__attach(fexit_skel);
if (CHECK(err, "fexit_attach", "fexit attach failed: %d\n", err))
goto close_prog;

Expand All @@ -44,6 +44,6 @@ void test_fentry_fexit(void)
}

close_prog:
fentry_test__destroy(fentry_skel);
fexit_test__destroy(fexit_skel);
fentry_test_lskel__destroy(fentry_skel);
fexit_test_lskel__destroy(fexit_skel);
}
14 changes: 7 additions & 7 deletions tools/testing/selftests/bpf/prog_tests/fentry_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
#include <test_progs.h>
#include "fentry_test.lskel.h"

static int fentry_test(struct fentry_test *fentry_skel)
static int fentry_test(struct fentry_test_lskel *fentry_skel)
{
int err, prog_fd, i;
__u32 duration = 0, retval;
int link_fd;
__u64 *result;

err = fentry_test__attach(fentry_skel);
err = fentry_test_lskel__attach(fentry_skel);
if (!ASSERT_OK(err, "fentry_attach"))
return err;

/* Check that already linked program can't be attached again. */
link_fd = fentry_test__test1__attach(fentry_skel);
link_fd = fentry_test_lskel__test1__attach(fentry_skel);
if (!ASSERT_LT(link_fd, 0, "fentry_attach_link"))
return -1;

Expand All @@ -31,7 +31,7 @@ static int fentry_test(struct fentry_test *fentry_skel)
return -1;
}

fentry_test__detach(fentry_skel);
fentry_test_lskel__detach(fentry_skel);

/* zero results for re-attach test */
memset(fentry_skel->bss, 0, sizeof(*fentry_skel->bss));
Expand All @@ -40,10 +40,10 @@ static int fentry_test(struct fentry_test *fentry_skel)

void test_fentry_test(void)
{
struct fentry_test *fentry_skel = NULL;
struct fentry_test_lskel *fentry_skel = NULL;
int err;

fentry_skel = fentry_test__open_and_load();
fentry_skel = fentry_test_lskel__open_and_load();
if (!ASSERT_OK_PTR(fentry_skel, "fentry_skel_load"))
goto cleanup;

Expand All @@ -55,5 +55,5 @@ void test_fentry_test(void)
ASSERT_OK(err, "fentry_second_attach");

cleanup:
fentry_test__destroy(fentry_skel);
fentry_test_lskel__destroy(fentry_skel);
}
12 changes: 6 additions & 6 deletions tools/testing/selftests/bpf/prog_tests/fexit_sleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

static int do_sleep(void *skel)
{
struct fexit_sleep *fexit_skel = skel;
struct fexit_sleep_lskel *fexit_skel = skel;
struct timespec ts1 = { .tv_nsec = 1 };
struct timespec ts2 = { .tv_sec = 10 };

Expand All @@ -25,16 +25,16 @@ static char child_stack[STACK_SIZE];

void test_fexit_sleep(void)
{
struct fexit_sleep *fexit_skel = NULL;
struct fexit_sleep_lskel *fexit_skel = NULL;
int wstatus, duration = 0;
pid_t cpid;
int err, fexit_cnt;

fexit_skel = fexit_sleep__open_and_load();
fexit_skel = fexit_sleep_lskel__open_and_load();
if (CHECK(!fexit_skel, "fexit_skel_load", "fexit skeleton failed\n"))
goto cleanup;

err = fexit_sleep__attach(fexit_skel);
err = fexit_sleep_lskel__attach(fexit_skel);
if (CHECK(err, "fexit_attach", "fexit attach failed: %d\n", err))
goto cleanup;

Expand All @@ -60,7 +60,7 @@ void test_fexit_sleep(void)
*/
close(fexit_skel->progs.nanosleep_fentry.prog_fd);
close(fexit_skel->progs.nanosleep_fexit.prog_fd);
fexit_sleep__detach(fexit_skel);
fexit_sleep_lskel__detach(fexit_skel);

/* kill the thread to unwind sys_nanosleep stack through the trampoline */
kill(cpid, 9);
Expand All @@ -78,5 +78,5 @@ void test_fexit_sleep(void)
goto cleanup;

cleanup:
fexit_sleep__destroy(fexit_skel);
fexit_sleep_lskel__destroy(fexit_skel);
}
14 changes: 7 additions & 7 deletions tools/testing/selftests/bpf/prog_tests/fexit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
#include <test_progs.h>
#include "fexit_test.lskel.h"

static int fexit_test(struct fexit_test *fexit_skel)
static int fexit_test(struct fexit_test_lskel *fexit_skel)
{
int err, prog_fd, i;
__u32 duration = 0, retval;
int link_fd;
__u64 *result;

err = fexit_test__attach(fexit_skel);
err = fexit_test_lskel__attach(fexit_skel);
if (!ASSERT_OK(err, "fexit_attach"))
return err;

/* Check that already linked program can't be attached again. */
link_fd = fexit_test__test1__attach(fexit_skel);
link_fd = fexit_test_lskel__test1__attach(fexit_skel);
if (!ASSERT_LT(link_fd, 0, "fexit_attach_link"))
return -1;

Expand All @@ -31,7 +31,7 @@ static int fexit_test(struct fexit_test *fexit_skel)
return -1;
}

fexit_test__detach(fexit_skel);
fexit_test_lskel__detach(fexit_skel);

/* zero results for re-attach test */
memset(fexit_skel->bss, 0, sizeof(*fexit_skel->bss));
Expand All @@ -40,10 +40,10 @@ static int fexit_test(struct fexit_test *fexit_skel)

void test_fexit_test(void)
{
struct fexit_test *fexit_skel = NULL;
struct fexit_test_lskel *fexit_skel = NULL;
int err;

fexit_skel = fexit_test__open_and_load();
fexit_skel = fexit_test_lskel__open_and_load();
if (!ASSERT_OK_PTR(fexit_skel, "fexit_skel_load"))
goto cleanup;

Expand All @@ -55,5 +55,5 @@ void test_fexit_test(void)
ASSERT_OK(err, "fexit_second_attach");

cleanup:
fexit_test__destroy(fexit_skel);
fexit_test_lskel__destroy(fexit_skel);
}
6 changes: 3 additions & 3 deletions tools/testing/selftests/bpf/prog_tests/kfunc_call.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

static void test_main(void)
{
struct kfunc_call_test *skel;
struct kfunc_call_test_lskel *skel;
int prog_fd, retval, err;

skel = kfunc_call_test__open_and_load();
skel = kfunc_call_test_lskel__open_and_load();
if (!ASSERT_OK_PTR(skel, "skel"))
return;

Expand All @@ -26,7 +26,7 @@ static void test_main(void)
ASSERT_OK(err, "bpf_prog_test_run(test2)");
ASSERT_EQ(retval, 3, "test2-retval");

kfunc_call_test__destroy(skel);
kfunc_call_test_lskel__destroy(skel);
}

static void test_subprog(void)
Expand Down
Loading

0 comments on commit 087cba7

Please sign in to comment.