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
912CC = 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
1122ARCH ?= $(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
1525ifeq ($(ARCH ) ,x86_64)
1626 ARCH := x86
1727else ifeq ($(ARCH),i686)
18- ARCH := x86
28+ ARCH := i386
1929else ifeq ($(ARCH),armv7l)
2030 ARCH := arm
2131else ifeq ($(ARCH),armv8l)
@@ -30,46 +40,44 @@ else ifeq ($(ARCH),s390x)
3040 ARCH := s390
3141endif
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
4055CFLAGS = -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
6473all : $(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
7280clean :
7381 rm -f $(BIN )
7482
75- .SUFFIXES :
83+ .PHONY : all clean
0 commit comments