Skip to content

Commit 86c2f72

Browse files
feat: Update struct_ops to change test_3 return type to int and add BPF helper support
1 parent 540ce67 commit 86c2f72

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

src/features/struct_ops/module/bpf_testmod.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
struct bpf_testmod_ops {
66
int (*test_1)(void);
77
int (*test_2)(int a, int b);
8-
void (*test_3)(const char *buf, int len);
8+
int (*test_3)(const char *buf, int len);
99
};
1010

1111
#endif /* _BPF_TESTMOD_H */

src/features/struct_ops/module/hello.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
#include <linux/btf_ids.h>
77
#include <linux/proc_fs.h>
88
#include <linux/seq_file.h>
9+
#include <linux/bpf_verifier.h>
910

1011
/* Define our custom struct_ops operations */
1112
struct bpf_testmod_ops {
1213
int (*test_1)(void);
1314
int (*test_2)(int a, int b);
14-
void (*test_3)(const char *buf, int len);
15+
int (*test_3)(const char *buf, int len);
1516
};
1617

1718
/* Global instance that BPF programs will implement */
@@ -31,8 +32,9 @@ static int bpf_testmod_ops__test_2(int a, int b)
3132
return 0;
3233
}
3334

34-
static void bpf_testmod_ops__test_3(const char *buf, int len)
35+
static int bpf_testmod_ops__test_3(const char *buf, int len)
3536
{
37+
return 0;
3638
}
3739

3840
/* CFI stubs structure */
@@ -58,8 +60,18 @@ static bool bpf_testmod_ops_is_valid_access(int off, int size,
5860
return true;
5961
}
6062

63+
/* Allow specific BPF helpers to be used in struct_ops programs */
64+
static const struct bpf_func_proto *
65+
bpf_testmod_ops_get_func_proto(enum bpf_func_id func_id,
66+
const struct bpf_prog *prog)
67+
{
68+
/* Use base func proto which includes trace_printk and other basic helpers */
69+
return bpf_base_func_proto(func_id, prog);
70+
}
71+
6172
static const struct bpf_verifier_ops bpf_testmod_verifier_ops = {
6273
.is_valid_access = bpf_testmod_ops_is_valid_access,
74+
.get_func_proto = bpf_testmod_ops_get_func_proto,
6375
};
6476

6577
static int bpf_testmod_ops_init_member(const struct btf_type *t,

src/features/struct_ops/struct_ops.bpf.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@ int BPF_PROG(bpf_testmod_test_2, int a, int b)
2323
}
2424

2525
SEC("struct_ops/test_3")
26-
void BPF_PROG(bpf_testmod_test_3, const char *buf, int len)
26+
int BPF_PROG(bpf_testmod_test_3, const char *buf, int len)
2727
{
2828
bpf_printk("BPF test_3 called with buffer length %d\n", len);
29-
if (len > 0) {
30-
bpf_printk("First char: %c\n", buf[0]);
31-
}
29+
/* Note: Accessing buf pointer requires proper context setup in kernel module */
30+
return 0;
3231
}
3332

3433
/* Define the struct_ops map */

0 commit comments

Comments
 (0)