Skip to content

Commit 2c9870b

Browse files
feat: Add initial implementation of struct_ops BPF program and kernel module with kfunc support
1 parent 134ee88 commit 2c9870b

File tree

11 files changed

+659
-0
lines changed

11 files changed

+659
-0
lines changed

src/features/struct_ops/.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
level=Depth
2+
type=Features

src/features/struct_ops/.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.vscode
2+
package.json
3+
*.o
4+
*.skel.json
5+
*.skel.yaml
6+
package.yaml
7+
ecli
8+
ecc
9+
.output
10+
struct_ops

src/features/struct_ops/Makefile

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2+
OUTPUT := .output
3+
CLANG ?= clang
4+
LIBBPF_SRC := $(abspath ../../third_party/libbpf/src)
5+
BPFTOOL_SRC := $(abspath ../../third_party/bpftool/src)
6+
LIBBPF_OBJ := $(abspath $(OUTPUT)/libbpf.a)
7+
BPFTOOL_OUTPUT ?= $(abspath $(OUTPUT)/bpftool)
8+
BPFTOOL ?= $(BPFTOOL_OUTPUT)/bootstrap/bpftool
9+
LIBBLAZESYM_SRC := $(abspath ../../third_party/blazesym/)
10+
LIBBLAZESYM_OBJ := $(abspath $(OUTPUT)/libblazesym.a)
11+
LIBBLAZESYM_HEADER := $(abspath $(OUTPUT)/blazesym.h)
12+
ARCH ?= $(shell uname -m | sed 's/x86_64/x86/' \
13+
| sed 's/arm.*/arm/' \
14+
| sed 's/aarch64/arm64/' \
15+
| sed 's/ppc64le/powerpc/' \
16+
| sed 's/mips.*/mips/' \
17+
| sed 's/riscv64/riscv/' \
18+
| sed 's/loongarch64/loongarch/')
19+
VMLINUX := ../../third_party/vmlinux/$(ARCH)/vmlinux.h
20+
# Use our own libbpf API headers and Linux UAPI headers distributed with
21+
# libbpf to avoid dependency on system-wide headers, which could be missing or
22+
# outdated
23+
INCLUDES := -I$(OUTPUT) -I../../third_party/libbpf/include/uapi -I$(dir $(VMLINUX))
24+
CFLAGS := -g -Wall
25+
ALL_LDFLAGS := $(LDFLAGS) $(EXTRA_LDFLAGS)
26+
27+
APPS = struct_ops # minimal minimal_legacy uprobe kprobe fentry usdt sockfilter tc ksyscall
28+
29+
CARGO ?= $(shell which cargo)
30+
ifeq ($(strip $(CARGO)),)
31+
BZS_APPS :=
32+
else
33+
BZS_APPS := # profile
34+
APPS += $(BZS_APPS)
35+
# Required by libblazesym
36+
ALL_LDFLAGS += -lrt -ldl -lpthread -lm
37+
endif
38+
39+
# Get Clang's default includes on this system. We'll explicitly add these dirs
40+
# to the includes list when compiling with `-target bpf` because otherwise some
41+
# architecture-specific dirs will be "missing" on some architectures/distros -
42+
# headers such as asm/types.h, asm/byteorder.h, asm/socket.h, asm/sockios.h,
43+
# sys/cdefs.h etc. might be missing.
44+
#
45+
# Use '-idirafter': Don't interfere with include mechanics except where the
46+
# build would have failed anyways.
47+
CLANG_BPF_SYS_INCLUDES ?= $(shell $(CLANG) -v -E - </dev/null 2>&1 \
48+
| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }')
49+
50+
ifeq ($(V),1)
51+
Q =
52+
msg =
53+
else
54+
Q = @
55+
msg = @printf ' %-8s %s%s\n' \
56+
"$(1)" \
57+
"$(patsubst $(abspath $(OUTPUT))/%,%,$(2))" \
58+
"$(if $(3), $(3))";
59+
MAKEFLAGS += --no-print-directory
60+
endif
61+
62+
define allow-override
63+
$(if $(or $(findstring environment,$(origin $(1))),\
64+
$(findstring command line,$(origin $(1)))),,\
65+
$(eval $(1) = $(2)))
66+
endef
67+
68+
$(call allow-override,CC,$(CROSS_COMPILE)cc)
69+
$(call allow-override,LD,$(CROSS_COMPILE)ld)
70+
71+
.PHONY: all
72+
all: $(APPS)
73+
74+
.PHONY: clean
75+
clean:
76+
$(call msg,CLEAN)
77+
$(Q)rm -rf $(OUTPUT) $(APPS)
78+
79+
$(OUTPUT) $(OUTPUT)/libbpf $(BPFTOOL_OUTPUT):
80+
$(call msg,MKDIR,$@)
81+
$(Q)mkdir -p $@
82+
83+
# Build libbpf
84+
$(LIBBPF_OBJ): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(OUTPUT)/libbpf
85+
$(call msg,LIB,$@)
86+
$(Q)$(MAKE) -C $(LIBBPF_SRC) BUILD_STATIC_ONLY=1 \
87+
OBJDIR=$(dir $@)/libbpf DESTDIR=$(dir $@) \
88+
INCLUDEDIR= LIBDIR= UAPIDIR= \
89+
install
90+
91+
# Build bpftool
92+
$(BPFTOOL): | $(BPFTOOL_OUTPUT)
93+
$(call msg,BPFTOOL,$@)
94+
$(Q)$(MAKE) ARCH= CROSS_COMPILE= OUTPUT=$(BPFTOOL_OUTPUT)/ -C $(BPFTOOL_SRC) bootstrap
95+
96+
97+
$(LIBBLAZESYM_SRC)/target/release/libblazesym.a::
98+
$(Q)cd $(LIBBLAZESYM_SRC) && $(CARGO) build --features=cheader,dont-generate-test-files --release
99+
100+
$(LIBBLAZESYM_OBJ): $(LIBBLAZESYM_SRC)/target/release/libblazesym.a | $(OUTPUT)
101+
$(call msg,LIB, $@)
102+
$(Q)cp $(LIBBLAZESYM_SRC)/target/release/libblazesym.a $@
103+
104+
$(LIBBLAZESYM_HEADER): $(LIBBLAZESYM_SRC)/target/release/libblazesym.a | $(OUTPUT)
105+
$(call msg,LIB,$@)
106+
$(Q)cp $(LIBBLAZESYM_SRC)/target/release/blazesym.h $@
107+
108+
# Build BPF code
109+
$(OUTPUT)/%.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(VMLINUX) | $(OUTPUT) $(BPFTOOL)
110+
$(call msg,BPF,$@)
111+
$(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \
112+
$(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) \
113+
-c $(filter %.c,$^) -o $(patsubst %.bpf.o,%.tmp.bpf.o,$@)
114+
$(Q)$(BPFTOOL) gen object $@ $(patsubst %.bpf.o,%.tmp.bpf.o,$@)
115+
116+
# Generate BPF skeletons
117+
$(OUTPUT)/%.skel.h: $(OUTPUT)/%.bpf.o | $(OUTPUT) $(BPFTOOL)
118+
$(call msg,GEN-SKEL,$@)
119+
$(Q)$(BPFTOOL) gen skeleton $< > $@
120+
121+
# Build user-space code
122+
$(patsubst %,$(OUTPUT)/%.o,$(APPS)): %.o: %.skel.h
123+
124+
$(OUTPUT)/%.o: %.c $(wildcard %.h) | $(OUTPUT)
125+
$(call msg,CC,$@)
126+
$(Q)$(CC) $(CFLAGS) $(INCLUDES) -c $(filter %.c,$^) -o $@
127+
128+
$(patsubst %,$(OUTPUT)/%.o,$(BZS_APPS)): $(LIBBLAZESYM_HEADER)
129+
130+
$(BZS_APPS): $(LIBBLAZESYM_OBJ)
131+
132+
# Build application binary
133+
$(APPS): %: $(OUTPUT)/%.o $(LIBBPF_OBJ) | $(OUTPUT)
134+
$(call msg,BINARY,$@)
135+
$(Q)$(CC) $(CFLAGS) $^ $(ALL_LDFLAGS) -lelf -lz -o $@
136+
137+
# delete failed targets
138+
.DELETE_ON_ERROR:
139+
140+
# keep intermediate (.skel.h, .bpf.o, etc) targets
141+
.SECONDARY:
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Ignore object files and kernel modules
2+
*.o
3+
*.ko
4+
*.mod
5+
*.mod.c
6+
*.symvers
7+
*.order
8+
9+
# Ignore temporary and backup files
10+
*~
11+
*.bak
12+
*.tmp
13+
*.swp
14+
15+
# Ignore build directory if generated
16+
/Module.symvers
17+
/Modules.markers
18+
/Module.markers
19+
/modules.order
20+
21+
# Ignore other automatically generated files
22+
*.cmd
23+
.tmp_versions/
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
obj-m += hello.o # hello.o is the target
2+
3+
# Enable BTF generation
4+
KBUILD_CFLAGS += -g -O2
5+
6+
all:
7+
# Compile the module with BTF information
8+
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
9+
10+
clean:
11+
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# write a basic kernel module
2+
3+
## hello world
4+
5+
Writing a Linux kernel module involves creating code that can be loaded into and unloaded from the kernel dynamically, without rebooting the system. Here’s a simple step-by-step guide to help you write a basic kernel module:
6+
7+
### 1. Set Up Your Environment
8+
9+
Make sure you have the Linux kernel headers installed and a suitable development environment ready. For Ubuntu or Debian, install them with:
10+
11+
```bash
12+
sudo apt-get install linux-headers-$(uname -r) build-essential
13+
```
14+
15+
### 2. Write the Kernel Module Code
16+
17+
Here’s an example of a very basic Linux kernel module:
18+
19+
```c
20+
// hello.c: A simple Linux kernel module
21+
#include <linux/init.h> // Macros for module initialization
22+
#include <linux/module.h> // Core header for loading modules
23+
#include <linux/kernel.h> // Kernel logging macros
24+
25+
// Function executed when the module is loaded
26+
static int __init hello_init(void)
27+
{
28+
printk(KERN_INFO "Hello, world!\n");
29+
return 0; // Return 0 if successful
30+
}
31+
32+
// Function executed when the module is removed
33+
static void __exit hello_exit(void)
34+
{
35+
printk(KERN_INFO "Goodbye, world!\n");
36+
}
37+
38+
// Macros to define the module’s init and exit points
39+
module_init(hello_init);
40+
module_exit(hello_exit);
41+
42+
MODULE_LICENSE("GPL"); // License type (GPL)
43+
MODULE_AUTHOR("Your Name"); // Module author
44+
MODULE_DESCRIPTION("A simple module"); // Module description
45+
MODULE_VERSION("1.0"); // Module version
46+
```
47+
48+
### 3. Create a Makefile
49+
50+
To compile the kernel module, you’ll need a `Makefile`. Here's a simple one:
51+
52+
```makefile
53+
obj-m += hello.o # hello.o is the target
54+
55+
all:
56+
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
57+
58+
clean:
59+
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
60+
```
61+
62+
### 4. Compile the Module
63+
64+
Run the following command in the directory where your `hello.c` and `Makefile` are located:
65+
66+
```bash
67+
make
68+
```
69+
70+
This will generate a file called `hello.ko`, which is the compiled kernel module.
71+
72+
### 5. Load the Module
73+
74+
To insert the module into the kernel, use `insmod`:
75+
76+
```bash
77+
sudo insmod hello.ko
78+
```
79+
80+
### 6. Check the Logs
81+
82+
To see the output from the `printk` statements, use the `dmesg` command:
83+
84+
```bash
85+
dmesg | tail
86+
```
87+
88+
You should see something like:
89+
90+
```txt
91+
[ 1234.5678] Hello, world!
92+
```
93+
94+
### 7. Remove the Module
95+
96+
To unload the module, use `rmmod`:
97+
98+
```bash
99+
sudo rmmod hello
100+
```
101+
102+
Again, check the logs using `dmesg`:
103+
104+
```bash
105+
sudo dmesg | tail
106+
```
107+
108+
You should see:
109+
110+
```txt
111+
[ 1234.9876] Goodbye, world!
112+
```
113+
114+
### 8. Clean Up
115+
116+
To clean up the build files, run:
117+
118+
```bash
119+
make clean
120+
```
121+
122+
### Notes
123+
124+
- **License**: The `MODULE_LICENSE("GPL")` ensures the module is GPL-compliant, which allows it to use symbols (functions) exported by the kernel.
125+
- **Debugging**: Use `printk` for logging within the module. It behaves similarly to `printf` but is designed for kernel space.
126+
- **Module Parameters**: You can add parameters to modules using `module_param()` to pass arguments when the module is loaded.
127+
128+
### Next Steps
129+
130+
Once you are familiar with this basic example, you can explore:
131+
132+
- Writing more advanced modules that interact with hardware or the filesystem.
133+
- Using kernel-specific APIs like work queues, kthreads, or handling interrupts.
134+
- Diving into eBPF or loadable kernel module techniques for debugging and tracing kernel events.

0 commit comments

Comments
 (0)