Skip to content

Commit b80d182

Browse files
committed
ebpf_prog: Switch eBPF build to BPF with CO-RE.
Add minimal hand-written vmlinux.h containing only the kernel struct definitions OpenSnitch accesses (sock, task_struct, sockaddr_in, etc.). BTF relocation resolves struct field offsets at load time, so compiled objects handle field offset changes without recompilation. Replace vendored libbpf headers with system libbpf via pkg-config. System libbpf is maintained by the distribution. Map ARCH to _TARGET_ARCH* values. libbpf's bpf_tracing.h requires these for correct register access macros per architecture.
1 parent a6a1313 commit b80d182

10 files changed

Lines changed: 458 additions & 6030 deletions

File tree

ebpf_prog/Makefile

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
1-
# OpenSnitch - 2023
1+
# OpenSnitch eBPF - CO-RE (Compile Once, Run Everywhere) Build
22
#
3-
# On Debian based distros we need the following 2 directories.
4-
# Otherwise, just use the kernel headers from the kernel sources.
3+
# This Makefile builds eBPF programs with CO-RE support.
4+
# CO-RE programs use BTF relocations resolved at load time.
55
#
6-
KERNEL_VER ?= $(shell ls -d /lib/modules/*/source | sort | tail -1 | cut -d/ -f4)
7-
KERNEL_DIR ?= /lib/modules/$(KERNEL_VER)/source
8-
KERNEL_HEADERS ?= /usr/src/linux-headers-$(KERNEL_VER)/
6+
# Requirements:
7+
# - clang with BPF target support (or bpf-unknown-none-gcc--not recommended)
8+
# - vmlinux.h (in bpf_headers/)
9+
# - libbpf (for bpf_helpers.h, bpf_tracing.h, bpf_core_read.h)
10+
11+
# Clang with BPF target
912
CC = clang
10-
LLC ?= llc
13+
CFLAGS_TARGET = -target bpf
14+
15+
# GCC BPF cross-compiler (alternative)
16+
#CC = bpf-unknown-none-gcc
17+
#CFLAGS_TARGET = -gbtf
18+
#OBJCOPY = bpf-unknown-none-objcopy
19+
20+
# Target architecture for __TARGET_ARCH_xxx define
21+
# Override with: make ARCH=arm64
1122
ARCH ?= $(shell uname -m)
1223

13-
# as in /usr/src/linux-headers-*/arch/
14-
# TODO: extract correctly the archs, and add more if needed.
24+
# Normalize architecture names
1525
ifeq ($(ARCH),x86_64)
1626
ARCH := x86
1727
else ifeq ($(ARCH),i686)
18-
ARCH := x86
28+
ARCH := i386
1929
else ifeq ($(ARCH),armv7l)
2030
ARCH := arm
2131
else ifeq ($(ARCH),armv8l)
@@ -30,46 +40,44 @@ else ifeq ($(ARCH),s390x)
3040
ARCH := s390
3141
endif
3242

33-
ifeq ($(ARCH),arm)
34-
# on previous archs, it fails with "SMP not supported on pre-ARMv6"
35-
EXTRA_FLAGS = "-D__LINUX_ARM_ARCH__=7"
36-
endif
43+
# Get libbpf include path from pkg-config
44+
# pkg-config never implemented proper cross-compilation support (--host was
45+
# proposed in 2005 but never merged). It filters -I paths that match
46+
# C_INCLUDE_PATH, but BPF cross-compiler uses CROSS_C_INCLUDE_PATH instead.
47+
# PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=1 disables this filtering.
48+
LIBBPF_CFLAGS := $(shell PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=1 pkg-config --cflags libbpf)
49+
50+
# Source files
51+
SRC = opensnitch.c opensnitch-procs.c opensnitch-dns.c
52+
BIN = $(SRC:.c=.o)
3753

38-
SRC := $(wildcard *.c)
39-
BIN := $(SRC:.c=.o)
54+
# Compiler flags for CO-RE BPF programs
4055
CFLAGS = -I. \
41-
-I$(KERNEL_HEADERS)/arch/$(ARCH)/include/generated/ \
42-
-I$(KERNEL_HEADERS)/include \
43-
-include $(KERNEL_DIR)/include/linux/kconfig.h \
44-
-I$(KERNEL_DIR)/include \
45-
-I$(KERNEL_DIR)/include/uapi \
46-
-I$(KERNEL_DIR)/include/generated/uapi \
47-
-I$(KERNEL_DIR)/arch/$(ARCH)/include \
48-
-I$(KERNEL_DIR)/arch/$(ARCH)/include/generated \
49-
-I$(KERNEL_DIR)/arch/$(ARCH)/include/uapi \
50-
-I$(KERNEL_DIR)/arch/$(ARCH)/include/generated/uapi \
51-
-I$(KERNEL_DIR)/tools/testing/selftests/bpf/ \
52-
-D__KERNEL__ -D__BPF_TRACING__ -Wno-unused-value -Wno-pointer-sign \
53-
-D__TARGET_ARCH_$(ARCH) -Wno-compare-distinct-pointer-types \
54-
$(EXTRA_FLAGS) \
55-
-Wunused \
56+
$(CFLAGS_TARGET) \
57+
$(LIBBPF_CFLAGS) \
58+
-Ibpf_headers \
59+
-D__KERNEL__ \
60+
-D__BPF_TRACING__ \
61+
-D__TARGET_ARCH_$(ARCH) \
62+
-Wall \
5663
-Wno-unused-value \
5764
-Wno-gnu-variable-sized-type-not-at-end \
65+
-Wno-pointer-sign \
66+
-Wno-compare-distinct-pointer-types \
5867
-Wno-address-of-packed-member \
5968
-Wno-tautological-compare \
6069
-Wno-unknown-warning-option \
6170
-fno-stack-protector \
62-
-g -O2 -emit-llvm
71+
-g -O2
6372

6473
all: $(BIN)
6574

66-
%.bc: %.c
67-
$(CC) $(CFLAGS) -c $<
68-
69-
%.o: %.bc
70-
$(LLC) -march=bpf -mcpu=generic -filetype=obj -o $@ $<
75+
%.o: %.c bpf_headers/vmlinux.h
76+
$(CC) $(CFLAGS) -c $< -o $@
77+
# GCC BPF: strip .BTF.ext section (incompatible with cilium/ebpf)
78+
# $(OBJCOPY) --remove-section=.BTF.ext --remove-section=.rel.BTF.ext $@
7179

7280
clean:
7381
rm -f $(BIN)
7482

75-
.SUFFIXES:
83+
.PHONY: all clean

0 commit comments

Comments
 (0)