add i32 support special case and find ctx repetition in multiple functions error.

This commit is contained in:
2025-11-05 17:38:38 +05:30
parent 3489f45b63
commit 2685d0a0ee
6 changed files with 63 additions and 19 deletions

View File

@ -1,19 +1,23 @@
BPF_CLANG := clang
CFLAGS := -O0 -emit-llvm -target bpf -c
CFLAGS := -emit-llvm -target bpf -c
SRC := $(wildcard *.bpf.c)
LL := $(SRC:.bpf.c=.bpf.ll)
LL2 := $(SRC:.bpf.c=.bpf.o2.ll)
OBJ := $(SRC:.bpf.c=.bpf.o)
.PHONY: all clean
all: $(LL) $(OBJ)
all: $(LL) $(OBJ) $(LL2)
%.bpf.o: %.bpf.c
$(BPF_CLANG) -O2 -g -target bpf -c $< -o $@
%.bpf.ll: %.bpf.c
$(BPF_CLANG) $(CFLAGS) -g -S $< -o $@
$(BPF_CLANG) -O0 $(CFLAGS) -g -S $< -o $@
%.bpf.o2.ll: %.bpf.c
$(BPF_CLANG) -O2 $(CFLAGS) -g -S $< -o $@
clean:
rm -f $(LL) $(OBJ)
rm -f $(LL) $(OBJ) $(LL2)

View File

@ -5,9 +5,9 @@ SEC("xdp")
int print_xdp_data(struct xdp_md *ctx)
{
// 'data' is a pointer to the start of packet data
void *data = (void *)(long)ctx->data;
long data = (long)ctx->data;
bpf_printk("ctx->data = %p\n", data);
bpf_printk("ctx->data = %lld\n", data);
return XDP_PASS;
}

View File

@ -1,4 +1,4 @@
from ctypes import c_int64, c_int32
from ctypes import c_int64, c_int32, c_void_p
from pythonbpf import bpf, section, bpfglobal, compile_to_ir, compile
from vmlinux import struct_xdp_md
from vmlinux import XDP_PASS
@ -8,10 +8,16 @@ from vmlinux import XDP_PASS
@section("xdp")
def print_xdp_data(ctx: struct_xdp_md) -> c_int64:
data = ctx.data # 32-bit field: packet start pointer
something = c_int32(2 + data)
something = c_void_p(data)
print(f"ctx->data = {something}")
return c_int64(XDP_PASS)
@bpf
@section("xdp")
def print_xdp_dat2a(ct2x: struct_xdp_md) -> c_int64:
data = ct2x.data # 32-bit field: packet start pointer
print(f"ct2x->data = {data}")
return c_int64(XDP_PASS)
@bpf
@bpfglobal