mirror of
https://github.com/eunomia-bpf/bpf-developer-tutorial.git
synced 2026-05-05 21:01:27 +08:00
feat: Update struct_ops to change test_3 return type to int and add BPF helper support
This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
struct bpf_testmod_ops {
|
struct bpf_testmod_ops {
|
||||||
int (*test_1)(void);
|
int (*test_1)(void);
|
||||||
int (*test_2)(int a, int b);
|
int (*test_2)(int a, int b);
|
||||||
void (*test_3)(const char *buf, int len);
|
int (*test_3)(const char *buf, int len);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* _BPF_TESTMOD_H */
|
#endif /* _BPF_TESTMOD_H */
|
||||||
@@ -6,12 +6,13 @@
|
|||||||
#include <linux/btf_ids.h>
|
#include <linux/btf_ids.h>
|
||||||
#include <linux/proc_fs.h>
|
#include <linux/proc_fs.h>
|
||||||
#include <linux/seq_file.h>
|
#include <linux/seq_file.h>
|
||||||
|
#include <linux/bpf_verifier.h>
|
||||||
|
|
||||||
/* Define our custom struct_ops operations */
|
/* Define our custom struct_ops operations */
|
||||||
struct bpf_testmod_ops {
|
struct bpf_testmod_ops {
|
||||||
int (*test_1)(void);
|
int (*test_1)(void);
|
||||||
int (*test_2)(int a, int b);
|
int (*test_2)(int a, int b);
|
||||||
void (*test_3)(const char *buf, int len);
|
int (*test_3)(const char *buf, int len);
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Global instance that BPF programs will implement */
|
/* Global instance that BPF programs will implement */
|
||||||
@@ -31,8 +32,9 @@ static int bpf_testmod_ops__test_2(int a, int b)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void bpf_testmod_ops__test_3(const char *buf, int len)
|
static int bpf_testmod_ops__test_3(const char *buf, int len)
|
||||||
{
|
{
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* CFI stubs structure */
|
/* CFI stubs structure */
|
||||||
@@ -58,8 +60,18 @@ static bool bpf_testmod_ops_is_valid_access(int off, int size,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Allow specific BPF helpers to be used in struct_ops programs */
|
||||||
|
static const struct bpf_func_proto *
|
||||||
|
bpf_testmod_ops_get_func_proto(enum bpf_func_id func_id,
|
||||||
|
const struct bpf_prog *prog)
|
||||||
|
{
|
||||||
|
/* Use base func proto which includes trace_printk and other basic helpers */
|
||||||
|
return bpf_base_func_proto(func_id, prog);
|
||||||
|
}
|
||||||
|
|
||||||
static const struct bpf_verifier_ops bpf_testmod_verifier_ops = {
|
static const struct bpf_verifier_ops bpf_testmod_verifier_ops = {
|
||||||
.is_valid_access = bpf_testmod_ops_is_valid_access,
|
.is_valid_access = bpf_testmod_ops_is_valid_access,
|
||||||
|
.get_func_proto = bpf_testmod_ops_get_func_proto,
|
||||||
};
|
};
|
||||||
|
|
||||||
static int bpf_testmod_ops_init_member(const struct btf_type *t,
|
static int bpf_testmod_ops_init_member(const struct btf_type *t,
|
||||||
|
|||||||
@@ -23,12 +23,11 @@ int BPF_PROG(bpf_testmod_test_2, int a, int b)
|
|||||||
}
|
}
|
||||||
|
|
||||||
SEC("struct_ops/test_3")
|
SEC("struct_ops/test_3")
|
||||||
void BPF_PROG(bpf_testmod_test_3, const char *buf, int len)
|
int BPF_PROG(bpf_testmod_test_3, const char *buf, int len)
|
||||||
{
|
{
|
||||||
bpf_printk("BPF test_3 called with buffer length %d\n", len);
|
bpf_printk("BPF test_3 called with buffer length %d\n", len);
|
||||||
if (len > 0) {
|
/* Note: Accessing buf pointer requires proper context setup in kernel module */
|
||||||
bpf_printk("First char: %c\n", buf[0]);
|
return 0;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Define the struct_ops map */
|
/* Define the struct_ops map */
|
||||||
|
|||||||
Reference in New Issue
Block a user