Skip to content

Commit

Permalink
elf: fix in-kernel kfunc test
Browse files Browse the repository at this point in the history
We currently test that resolving kfunc works both against in-kernel
as well as module functions. Commit 65eb006d85a2 ("bpf: Move kernel
test kfuncs to bpf_testmod") in the Linux kernel moved the
bpf_kfunc_call_test_mem_len_pass1 kfunc into btf_testmod.

Rewrite the test to use the conntrack kfuncs which were also
introduced in 5.18. They can also be in a module if nf_conntrack is
a module, but our CI kernels and my Fedora host kernel currently
compile it in.

Signed-off-by: Lorenz Bauer <lmb@isovalent.com>
  • Loading branch information
lmb committed Nov 24, 2023
1 parent 0628a2d commit 4f9c666
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
6 changes: 3 additions & 3 deletions elf_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ func TestKconfigConfig(t *testing.T) {
}

func TestKfunc(t *testing.T) {
testutils.SkipOnOldKernel(t, "5.18", "bpf_kfunc_call_test_mem_len_pass1")
testutils.SkipOnOldKernel(t, "5.18", "kfunc support")
testutils.Files(t, testutils.Glob(t, "testdata/kfunc-e*.elf"), func(t *testing.T, file string) {
spec, err := LoadCollectionSpec(file)
if err != nil {
Expand All @@ -734,7 +734,7 @@ func TestKfunc(t *testing.T) {
err = spec.LoadAndAssign(&obj, nil)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatalf("%v+", err)
t.Fatalf("%+v", err)
}
defer obj.Main.Close()

Expand All @@ -751,7 +751,7 @@ func TestKfunc(t *testing.T) {
}

func TestInvalidKfunc(t *testing.T) {
testutils.SkipOnOldKernel(t, "5.18", "bpf_kfunc_call_test_mem_len_pass1")
testutils.SkipOnOldKernel(t, "5.18", "kfunc support")

if !haveTestmod(t) {
t.Skip("bpf_testmod not loaded")
Expand Down
6 changes: 4 additions & 2 deletions testdata/common.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#pragma once

typedef _Bool bool;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef signed int int32_t;
typedef unsigned long uint64_t;

enum libbpf_tristate {
TRI_NO = 0,
TRI_YES = 1,
TRI_NO = 0,
TRI_YES = 1,
TRI_MODULE = 2,
};

Expand Down
Binary file modified testdata/kfunc-eb.elf
Binary file not shown.
Binary file modified testdata/kfunc-el.elf
Binary file not shown.
18 changes: 15 additions & 3 deletions testdata/kfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@

char __license[] __section("license") = "Dual MIT/GPL";

extern void bpf_kfunc_call_test_mem_len_pass1(void *mem, int len) __ksym;
// CO-RE type compat checking doesn't allow matches between forward declarations
// and structs so we can't use forward declarations. Empty structs work just fine.
struct __sk_buff {};
struct nf_conn {};
struct bpf_sock_tuple {};
struct bpf_ct_opts {};

__section("tc") int call_kfunc() {
bpf_kfunc_call_test_mem_len_pass1((void *)0, 0);
extern struct nf_conn *bpf_skb_ct_lookup(struct __sk_buff *, struct bpf_sock_tuple *, uint32_t, struct bpf_ct_opts *, uint32_t) __ksym;
extern void bpf_ct_release(struct nf_conn *) __ksym;

__section("tc") int call_kfunc(void *ctx) {
char buf[1];
struct nf_conn *conn = bpf_skb_ct_lookup(ctx, (void *)buf, 0, (void *)buf, 0);
if (conn) {
bpf_ct_release(conn);
}
return 1;
}

0 comments on commit 4f9c666

Please sign in to comment.