From 84a6686187c61ddfa5f9f9b4f37a7851f04f8bd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Geyslan=20Greg=C3=B3rio?= Date: Wed, 8 Feb 2023 19:29:51 -0300 Subject: [PATCH 1/2] build: add minimum vmlinux.h and uapi headers - Add caped vmlinux.h and uapi headers that are needed to compile bpf selftests. - uapi headers also provide exclusive definitions that are not available in the libbpf internal headers. - Add a new makefile target to generate uapi headers. - Remove bpftool dependency from Makefile. --- Makefile | 39 ++++------- libbpfgo.h | 1 + samples/fentry/main.bpf.c | 4 +- selftest/build/Makefile | 6 +- selftest/build/libbpfgo_test.bpf.c | 4 +- selftest/cgroup-legacy/main.bpf.c | 8 ++- selftest/cgroup/main.bpf.c | 4 +- selftest/common/Makefile | 8 +-- selftest/common/vmlinux.h | 32 +++++++++ selftest/create-map/main.bpf.c | 2 +- selftest/error-handling/main.bpf.c | 2 +- selftest/global-variable/main.bpf.c | 4 +- selftest/iterators/main.bpf.c | 4 +- selftest/map-batch/main.bpf.c | 4 +- selftest/map-pin-info/main.bpf.c | 4 +- selftest/map-update/main.bpf.c | 6 +- selftest/multiple-objects/Makefile | 16 ++--- selftest/multiple-objects/first.bpf.c | 2 +- selftest/multiple-objects/map.bpf.c | 5 +- selftest/multiple-objects/map.bpf.h | 4 +- selftest/multiple-objects/second.bpf.c | 2 +- selftest/netns/main.bpf.c | 4 +- selftest/object-iterator/main.bpf.c | 4 +- selftest/percpu/main.bpf.c | 4 +- selftest/perfbuffers/main.bpf.c | 4 +- selftest/probe-features/main.bpf.c | 3 +- selftest/ringbuffers/main.bpf.c | 4 +- selftest/set-attach/main.bpf.c | 4 +- selftest/spinlocks/main.bpf.c | 4 +- selftest/tc/main.bpf.c | 4 +- selftest/tracing/main.bpf.c | 4 +- selftest/uprobe/Makefile | 40 ++++++----- selftest/uprobe/main.bpf.c | 4 +- selftest/version/Makefile | 92 +------------------------- selftest/version/main.bpf.c | 1 - selftest/xdp/Makefile | 92 +------------------------- selftest/xdp/main.bpf.c | 4 +- selftest/xdp/run.sh | 23 +------ 38 files changed, 152 insertions(+), 304 deletions(-) create mode 100644 selftest/common/vmlinux.h mode change 100644 => 120000 selftest/version/Makefile mode change 100644 => 120000 selftest/xdp/Makefile mode change 100755 => 120000 selftest/xdp/run.sh diff --git a/Makefile b/Makefile index dd13021d..9be59877 100644 --- a/Makefile +++ b/Makefile @@ -13,14 +13,11 @@ CLANG = clang GO = go VAGRANT = vagrant CLANG_FMT = clang-format +GIT = $(shell which git || /bin/false) HOSTOS = $(shell uname) ARCH ?= $(shell uname -m | sed 's/x86_64/amd64/g; s/aarch64/arm64/g') -BTFFILE = /sys/kernel/btf/vmlinux -BPFTOOL = $(shell which bpftool || /bin/false) -GIT = $(shell which git || /bin/false) -VMLINUXH = $(OUTPUT)/vmlinux.h # libbpf @@ -46,6 +43,16 @@ CGO_LDFLAGS_DYN = "-lelf -lz -lbpf" all: libbpfgo-static test: libbpfgo-static-test +# libbpf uapi + +.PHONY: libbpf-uapi + +libbpf-uapi: $(LIBBPF_SRC) +# UAPI headers can be installed by a different package so they're not installed +# in by (libbpf) install rule. + UAPIDIR=$(LIBBPF_DESTDIR) \ + $(MAKE) -C $(LIBBPF_SRC) install_uapi_headers + # libbpfgo test object libbpfgo-test-bpf-static: libbpfgo-static # needed for serialization @@ -73,7 +80,7 @@ libbpfgo-dynamic-test: libbpfgo-test-bpf-dynamic # libbpf: static -libbpfgo-static: $(VMLINUXH) | $(LIBBPF_OBJ) +libbpfgo-static: libbpf-uapi $(LIBBPF_OBJ) CC=$(CLANG) \ CGO_CFLAGS=$(CGO_CFLAGS_STATIC) \ CGO_LDFLAGS=$(CGO_LDFLAGS_STATIC) \ @@ -92,28 +99,6 @@ libbpfgo-static-test: libbpfgo-test-bpf-static -v -tags netgo -ldflags $(CGO_EXTLDFLAGS_STATIC) \ . -# vmlinux header file - -.PHONY: vmlinuxh -vmlinuxh: $(VMLINUXH) - -$(VMLINUXH): $(OUTPUT) - @if [ ! -f $(BTFFILE) ]; then \ - echo "ERROR: kernel does not seem to support BTF"; \ - exit 1; \ - fi - @if [ ! $(BPFTOOL) ]; then \ - echo "ERROR: could not find bpftool"; \ - exit 1; \ - fi; - - @echo "INFO: generating $(VMLINUXH) from $(BTFFILE)"; \ - if ! $(BPFTOOL) btf dump file $(BTFFILE) format c > $(VMLINUXH); then \ - echo "ERROR: could not create $(VMLINUXH)"; \ - rm -f "$(VMLINUXH)"; \ - exit 1; \ - fi; - # static libbpf generation for the git submodule .PHONY: libbpf-static diff --git a/libbpfgo.h b/libbpfgo.h index eda01a53..79c7533b 100644 --- a/libbpfgo.h +++ b/libbpfgo.h @@ -14,6 +14,7 @@ #include #include +#include // uapi int libbpf_print_fn(enum libbpf_print_level level, // libbpf print level const char *format, // format used for the msg diff --git a/samples/fentry/main.bpf.c b/samples/fentry/main.bpf.c index a8807980..0df5f6ee 100644 --- a/samples/fentry/main.bpf.c +++ b/samples/fentry/main.bpf.c @@ -1,8 +1,10 @@ //+build ignore -#include "vmlinux.h" +#include #include #include +#include "vmlinux.h" + #ifdef asm_inline #undef asm_inline #define asm_inline asm diff --git a/selftest/build/Makefile b/selftest/build/Makefile index a519c140..14ac048a 100644 --- a/selftest/build/Makefile +++ b/selftest/build/Makefile @@ -18,14 +18,10 @@ all: $(TEST).bpf.o outputdir: $(MAKE) -C $(BASEDIR) outputdir -vmlinuxh: outputdir - $(MAKE) -C $(BASEDIR) vmlinuxh - ## test bpf dependency $(TEST).bpf.o: $(TEST).bpf.c - $(MAKE) -C $(BASEDIR) vmlinuxh - $(CLANG) $(CFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT) -c $< -o $@ + $(CLANG) $(CFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT) -I$(abspath ../common) -c $< -o $@ ## clean diff --git a/selftest/build/libbpfgo_test.bpf.c b/selftest/build/libbpfgo_test.bpf.c index 3c6afae9..9e53b020 100644 --- a/selftest/build/libbpfgo_test.bpf.c +++ b/selftest/build/libbpfgo_test.bpf.c @@ -1,8 +1,10 @@ //+build ignore -#include +#include #include #include +#include "vmlinux.h" + char LICENSE[] SEC("license") = "Dual BSD/GPL"; struct { diff --git a/selftest/cgroup-legacy/main.bpf.c b/selftest/cgroup-legacy/main.bpf.c index b9a68989..4bcf8d10 100644 --- a/selftest/cgroup-legacy/main.bpf.c +++ b/selftest/cgroup-legacy/main.bpf.c @@ -1,11 +1,15 @@ //+build ignore -#include - +#include #include #include #include #include +#include +#include + +#include "vmlinux.h" + #ifdef asm_inline #undef asm_inline #define asm_inline asm diff --git a/selftest/cgroup/main.bpf.c b/selftest/cgroup/main.bpf.c index daeb5f29..16dea682 100644 --- a/selftest/cgroup/main.bpf.c +++ b/selftest/cgroup/main.bpf.c @@ -1,8 +1,10 @@ //+build ignore -#include "vmlinux.h" +#include #include #include +#include "vmlinux.h" + #ifdef asm_inline #undef asm_inline #define asm_inline asm diff --git a/selftest/common/Makefile b/selftest/common/Makefile index 1fff89ec..f3143bc7 100644 --- a/selftest/common/Makefile +++ b/selftest/common/Makefile @@ -14,7 +14,7 @@ ARCH := $(shell uname -m | sed 's/x86_64/amd64/g; s/aarch64/arm64/g') CFLAGS = -g -O2 -Wall -fpie LDFLAGS = -CGO_CFLAGS_STATIC = "-I$(abspath $(OUTPUT))" +CGO_CFLAGS_STATIC = "-I$(abspath $(OUTPUT)) -I$(abspath ../common)" CGO_LDFLAGS_STATIC = "-lelf -lz $(LIBBPF_OBJ)" CGO_EXTLDFLAGS_STATIC = '-w -extldflags "-static"' @@ -41,17 +41,13 @@ libbpfgo-static: libbpfgo-dynamic: $(MAKE) -C $(BASEDIR) libbpfgo-dynamic -vmlinuxh: - $(MAKE) -C $(BASEDIR) vmlinuxh - outputdir: $(MAKE) -C $(BASEDIR) outputdir ## test bpf dependency $(MAIN).bpf.o: $(MAIN).bpf.c - $(MAKE) -C $(BASEDIR) vmlinuxh - $(CLANG) $(CFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT) -c $< -o $@ + $(CLANG) $(CFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT) -I$(abspath ../common) -c $< -o $@ ## test diff --git a/selftest/common/vmlinux.h b/selftest/common/vmlinux.h new file mode 100644 index 00000000..1e405bc4 --- /dev/null +++ b/selftest/common/vmlinux.h @@ -0,0 +1,32 @@ +#ifndef __VMLINUX_H__ +#define __VMLINUX_H__ + +typedef __u8 u8; + +typedef __s16 s16; + +typedef __u16 u16; + +typedef __s32 s32; + +typedef __u32 u32; + +typedef __s64 s64; + +typedef __u64 u64; + +struct trace_entry { + short unsigned int type; + unsigned char flags; + unsigned char preempt_count; + int pid; +}; + +struct trace_event_raw_sys_enter { + struct trace_entry ent; + long int id; + long unsigned int args[6]; + char __data[0]; +}; + +#endif /* __VMLINUX_H__ */ diff --git a/selftest/create-map/main.bpf.c b/selftest/create-map/main.bpf.c index 5c53e576..7da4a8bc 100644 --- a/selftest/create-map/main.bpf.c +++ b/selftest/create-map/main.bpf.c @@ -1,5 +1,5 @@ //+build ignore -#include "vmlinux.h" +#include #include SEC("kprobe/sys_execve") diff --git a/selftest/error-handling/main.bpf.c b/selftest/error-handling/main.bpf.c index 75c9c97f..b34b91cc 100644 --- a/selftest/error-handling/main.bpf.c +++ b/selftest/error-handling/main.bpf.c @@ -1,5 +1,5 @@ //+build ignore -#include "vmlinux.h" +#include #include SEC("kprobe/sys_mmap") diff --git a/selftest/global-variable/main.bpf.c b/selftest/global-variable/main.bpf.c index d0059879..13aae52d 100644 --- a/selftest/global-variable/main.bpf.c +++ b/selftest/global-variable/main.bpf.c @@ -1,8 +1,10 @@ //+build ignore -#include "vmlinux.h" +#include #include #include +#include "vmlinux.h" + #ifdef asm_inline #undef asm_inline #define asm_inline asm diff --git a/selftest/iterators/main.bpf.c b/selftest/iterators/main.bpf.c index e188257f..657871b7 100644 --- a/selftest/iterators/main.bpf.c +++ b/selftest/iterators/main.bpf.c @@ -1,7 +1,9 @@ //+build ignore -#include "vmlinux.h" +#include #include +#include "vmlinux.h" + #ifdef asm_inline #undef asm_inline #define asm_inline asm diff --git a/selftest/map-batch/main.bpf.c b/selftest/map-batch/main.bpf.c index 77aa5e2c..c5eda8a4 100644 --- a/selftest/map-batch/main.bpf.c +++ b/selftest/map-batch/main.bpf.c @@ -1,7 +1,9 @@ //+build ignore -#include "vmlinux.h" +#include #include +#include "vmlinux.h" + struct { __uint(type, BPF_MAP_TYPE_HASH); __type(key, u32); diff --git a/selftest/map-pin-info/main.bpf.c b/selftest/map-pin-info/main.bpf.c index 86a36d59..f776a57c 100644 --- a/selftest/map-pin-info/main.bpf.c +++ b/selftest/map-pin-info/main.bpf.c @@ -1,7 +1,9 @@ //+build ignore -#include "vmlinux.h" +#include #include +#include "vmlinux.h" + struct value { int x; char y; diff --git a/selftest/map-update/main.bpf.c b/selftest/map-update/main.bpf.c index d4c64e6a..40dcb49b 100644 --- a/selftest/map-update/main.bpf.c +++ b/selftest/map-update/main.bpf.c @@ -1,7 +1,9 @@ //+build ignore -#include "vmlinux.h" +#include #include +#include "vmlinux.h" + struct value { int x; char y; @@ -30,7 +32,7 @@ int kprobe__sys_mmap(struct pt_regs *ctx) } bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, v1, sizeof(struct value)); - int64_t secondKey = 42069420; + s64 secondKey = 42069420; struct value *v2 = bpf_map_lookup_elem(&tester, &secondKey); if (!v2) { return 1; diff --git a/selftest/multiple-objects/Makefile b/selftest/multiple-objects/Makefile index 9b97cdff..0eea5857 100644 --- a/selftest/multiple-objects/Makefile +++ b/selftest/multiple-objects/Makefile @@ -14,12 +14,12 @@ ARCH := $(shell uname -m | sed 's/x86_64/amd64/g; s/aarch64/arm64/g') CFLAGS = -g -O2 -Wall -fpie LDFLAGS = -CGO_CFLAGS_STATIC = "-I$(abspath $(OUTPUT))" +CGO_CFLAGS_STATIC = "-I$(abspath $(OUTPUT)) -I$(abspath ../common)" CGO_LDFLAGS_STATIC = "-lelf -lz $(LIBBPF_OBJ)" CGO_EXTLDFLAGS_STATIC = '-w -extldflags "-static"' CGO_CFLAGS_DYN = "-I. -I/usr/include/" -CGO_LDFLAGS_DYN = "-lelf -lz -lbpf" +CGO_LDFLAGS_DYN = "-lelf -lz -lbpf" MAIN = main FIRST = first @@ -44,25 +44,19 @@ libbpfgo-static: libbpfgo-dynamic: $(MAKE) -C $(BASEDIR) libbpfgo-dynamic -vmlinuxh: - $(MAKE) -C $(BASEDIR) vmlinuxh - outputdir: $(MAKE) -C $(BASEDIR) outputdir ## test bpf dependency $(FIRST).bpf.o: $(FIRST).bpf.c - $(MAKE) -C $(BASEDIR) vmlinuxh - $(CLANG) $(CFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT) -c $< -o $@ + $(CLANG) $(CFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT) -I$(abspath ../common) -c $< -o $@ $(SECOND).bpf.o: $(SECOND).bpf.c - $(MAKE) -C $(BASEDIR) vmlinuxh - $(CLANG) $(CFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT) -c $< -o $@ + $(CLANG) $(CFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT) -I$(abspath ../common) -c $< -o $@ $(MAP).bpf.o: $(MAP).bpf.c - $(MAKE) -C $(BASEDIR) vmlinuxh - $(CLANG) $(CFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT) -c $< -o $@ + $(CLANG) $(CFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT) -I$(abspath ../common) -c $< -o $@ ## test diff --git a/selftest/multiple-objects/first.bpf.c b/selftest/multiple-objects/first.bpf.c index 2c60c388..2c84b3e4 100644 --- a/selftest/multiple-objects/first.bpf.c +++ b/selftest/multiple-objects/first.bpf.c @@ -1,5 +1,5 @@ //+build ignore -#include "vmlinux.h" +#include #include #include #include diff --git a/selftest/multiple-objects/map.bpf.c b/selftest/multiple-objects/map.bpf.c index 29326476..1eb7bcec 100644 --- a/selftest/multiple-objects/map.bpf.c +++ b/selftest/multiple-objects/map.bpf.c @@ -1,7 +1,6 @@ //+build ignore - -#include "vmlinux.h" +#include #include #include #include -#include "map.bpf.h" \ No newline at end of file +#include "map.bpf.h" diff --git a/selftest/multiple-objects/map.bpf.h b/selftest/multiple-objects/map.bpf.h index 2cdbea16..d32838e9 100644 --- a/selftest/multiple-objects/map.bpf.h +++ b/selftest/multiple-objects/map.bpf.h @@ -1,10 +1,10 @@ //+build ignore - -#include "vmlinux.h" #include #include #include +#include "vmlinux.h" + struct { __uint(pinning, LIBBPF_PIN_BY_NAME); __uint(type, BPF_MAP_TYPE_RINGBUF); diff --git a/selftest/multiple-objects/second.bpf.c b/selftest/multiple-objects/second.bpf.c index 1b9718c4..a8fb711f 100644 --- a/selftest/multiple-objects/second.bpf.c +++ b/selftest/multiple-objects/second.bpf.c @@ -1,5 +1,5 @@ //+build ignore -#include "vmlinux.h" +#include #include #include #include diff --git a/selftest/netns/main.bpf.c b/selftest/netns/main.bpf.c index f2d7148e..f8741c7c 100644 --- a/selftest/netns/main.bpf.c +++ b/selftest/netns/main.bpf.c @@ -1,8 +1,10 @@ //+build ignore -#include "vmlinux.h" +#include #include #include +#include "vmlinux.h" + #ifdef asm_inline #undef asm_inline #define asm_inline asm diff --git a/selftest/object-iterator/main.bpf.c b/selftest/object-iterator/main.bpf.c index 2d14ac0f..d9a6ab61 100644 --- a/selftest/object-iterator/main.bpf.c +++ b/selftest/object-iterator/main.bpf.c @@ -1,8 +1,10 @@ //+build ignore -#include "vmlinux.h" +#include #include #include +#include "vmlinux.h" + #ifdef asm_inline #undef asm_inline #define asm_inline asm diff --git a/selftest/percpu/main.bpf.c b/selftest/percpu/main.bpf.c index 8a35d902..edd893a2 100644 --- a/selftest/percpu/main.bpf.c +++ b/selftest/percpu/main.bpf.c @@ -1,6 +1,8 @@ //+build ignore +#include +#include + #include "vmlinux.h" -#include #ifdef asm_inline #undef asm_inline diff --git a/selftest/perfbuffers/main.bpf.c b/selftest/perfbuffers/main.bpf.c index f5bb12dc..140f6349 100644 --- a/selftest/perfbuffers/main.bpf.c +++ b/selftest/perfbuffers/main.bpf.c @@ -1,7 +1,9 @@ //+build ignore -#include "vmlinux.h" +#include #include +#include "vmlinux.h" + #ifdef asm_inline #undef asm_inline #define asm_inline asm diff --git a/selftest/probe-features/main.bpf.c b/selftest/probe-features/main.bpf.c index d8d10068..ff55bb86 100644 --- a/selftest/probe-features/main.bpf.c +++ b/selftest/probe-features/main.bpf.c @@ -1,7 +1,8 @@ //+build ignore -#include "vmlinux.h" +#include #include +#include "vmlinux.h" SEC("kprobe/sys_mmap") int kprobe__sys_mmap(struct pt_regs *ctx) diff --git a/selftest/ringbuffers/main.bpf.c b/selftest/ringbuffers/main.bpf.c index b3cf80f2..8ef1fbcb 100644 --- a/selftest/ringbuffers/main.bpf.c +++ b/selftest/ringbuffers/main.bpf.c @@ -1,6 +1,8 @@ //+build ignore +#include +#include + #include "vmlinux.h" -#include #ifdef asm_inline #undef asm_inline diff --git a/selftest/set-attach/main.bpf.c b/selftest/set-attach/main.bpf.c index 729f273e..39250e1c 100644 --- a/selftest/set-attach/main.bpf.c +++ b/selftest/set-attach/main.bpf.c @@ -1,8 +1,10 @@ //+build ignore -#include "vmlinux.h" +#include #include #include +#include "vmlinux.h" + #ifdef asm_inline #undef asm_inline #define asm_inline asm diff --git a/selftest/spinlocks/main.bpf.c b/selftest/spinlocks/main.bpf.c index 2c8ac594..4a9ec198 100644 --- a/selftest/spinlocks/main.bpf.c +++ b/selftest/spinlocks/main.bpf.c @@ -1,7 +1,9 @@ //+build ignore -#include "vmlinux.h" +#include #include +#include "vmlinux.h" + #ifdef asm_inline #undef asm_inline #define asm_inline asm diff --git a/selftest/tc/main.bpf.c b/selftest/tc/main.bpf.c index 4a258fea..c954123d 100644 --- a/selftest/tc/main.bpf.c +++ b/selftest/tc/main.bpf.c @@ -1,8 +1,10 @@ //+build ignore -#include "vmlinux.h" +#include #include #include +#include "vmlinux.h" + #ifdef asm_inline #undef asm_inline #define asm_inline asm diff --git a/selftest/tracing/main.bpf.c b/selftest/tracing/main.bpf.c index 063359e5..c366b19f 100644 --- a/selftest/tracing/main.bpf.c +++ b/selftest/tracing/main.bpf.c @@ -1,8 +1,10 @@ //+build ignore -#include "vmlinux.h" +#include #include #include +#include "vmlinux.h" + #ifdef asm_inline #undef asm_inline #define asm_inline asm diff --git a/selftest/uprobe/Makefile b/selftest/uprobe/Makefile index 66e8de67..eb6864d5 100644 --- a/selftest/uprobe/Makefile +++ b/selftest/uprobe/Makefile @@ -9,22 +9,24 @@ CC = gcc CLANG = clang GO = go +ARCH := $(shell uname -m | sed 's/x86_64/amd64/g; s/aarch64/arm64/g') + CFLAGS = -g -O2 -Wall -fpie LDFLAGS = -ARCH := $(shell uname -m | sed 's/x86_64/amd64/g; s/aarch64/arm64/g') -CGO_CFLAGS_STATIC = "-I$(abspath $(OUTPUT))" +CGO_CFLAGS_STATIC = "-I$(abspath $(OUTPUT)) -I$(abspath ../common)" CGO_LDFLAGS_STATIC = "-lelf -lz $(LIBBPF_OBJ)" +CGO_EXTLDFLAGS_STATIC = '-w -extldflags "-static"' CGO_CFLAGS_DYN = "-I. -I/usr/include/" CGO_LDFLAGS_DYN = "-lelf -lz -lbpf" +TEST = main + .PHONY: $(TEST) .PHONY: $(TEST).go .PHONY: $(TEST).bpf.c -TEST = main - all: $(TEST)-static .PHONY: libbpfgo @@ -39,15 +41,10 @@ libbpfgo-static: libbpfgo-dynamic: $(MAKE) -C $(BASEDIR) libbpfgo-dynamic -vmlinuxh: - $(MAKE) -C $(BASEDIR) vmlinuxh - ## test (bpf) -.PHONY: $(TEST)-bpf - -$(TEST)-bpf: vmlinuxh - $(CLANG) $(CFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT) -c $(TEST).bpf.c -o $(TEST).bpf.o +$(TEST).bpf.o: $(TEST).bpf.c + $(CLANG) $(CFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT) -I$(abspath ../common) -c $< -o $@ ## test deps @@ -70,13 +67,20 @@ gotest: .PHONY: $(TEST)-static .PHONY: $(TEST)-dynamic -$(TEST)-static: $(TEST)-bpf libbpfgo-static $(DEPS) - CC=$(CLANG) CGO_CFLAGS=$(CGO_CFLAGS_STATIC) CGO_LDFLAGS=$(CGO_LDFLAGS_STATIC) \ - $(GO) build -o $(TEST)-static ./$(TEST).go - -$(TEST)-dynamic: $(TEST)-bpf libbpfgo-dynamic $(DEPS) - CC=$(CLANG) CGO_CFLAGS=$(CGO_CFLAGS_DYN) CGO_LDFLAGS=$(CGO_LDFLAGS_DYN) \ - $(GO) build -o ./$(TEST)-dynamic ./$(TEST).go +$(TEST)-static: libbpfgo-static | $(TEST).bpf.o $(DEPS) + CC=$(CLANG) \ + CGO_CFLAGS=$(CGO_CFLAGS_STATIC) \ + CGO_LDFLAGS=$(CGO_LDFLAGS_STATIC) \ + GOOS=linux GOARCH=$(ARCH) \ + $(GO) build \ + -tags netgo -ldflags $(CGO_EXTLDFLAGS_STATIC) \ + -o $(TEST)-static ./$(TEST).go + +$(TEST)-dynamic: libbpfgo-dynamic | $(TEST).bpf.o $(DEPS) + CC=$(CLANG) \ + CGO_CFLAGS=$(CGO_CFLAGS_DYN) \ + CGO_LDFLAGS=$(CGO_LDFLAGS_DYN) \ + $(GO) build -o ./$(TEST)-dynamic ./$(TEST).go ## run diff --git a/selftest/uprobe/main.bpf.c b/selftest/uprobe/main.bpf.c index 88d53b94..43fa0ed2 100644 --- a/selftest/uprobe/main.bpf.c +++ b/selftest/uprobe/main.bpf.c @@ -1,6 +1,8 @@ //+build ignore +#include +#include + #include "vmlinux.h" -#include #ifdef asm_inline #undef asm_inline diff --git a/selftest/version/Makefile b/selftest/version/Makefile deleted file mode 100644 index 628ba6f7..00000000 --- a/selftest/version/Makefile +++ /dev/null @@ -1,91 +0,0 @@ -BASEDIR = $(abspath ../../) - -OUTPUT = ../../output - -LIBBPF_SRC = $(abspath ../../libbpf/src) -LIBBPF_OBJ = $(abspath $(OUTPUT)/libbpf.a) - -CC = gcc -CLANG = clang - -ARCH := $(shell uname -m) -ARCH := $(subst x86_64,amd64,$(ARCH)) - -CFLAGS = -g -O2 -Wall -fpie -LDFLAGS = - -CGO_CFLAGS_STATIC = "-I$(abspath $(OUTPUT))" -CGO_LDFLAGS_STATIC = "-lelf -lz $(LIBBPF_OBJ)" -CGO_EXTLDFLAGS_STATIC = '-w -extldflags "-static"' - -CGO_CFLAGS_DYN = "-I. -I/usr/include/" -CGO_LDFLAGS_DYN = "-lelf -lz -lbpf" - -.PHONY: $(TEST) -.PHONY: $(TEST).go -.PHONY: $(TEST).bpf.c - -TEST = main - -all: $(TEST)-static - -.PHONY: libbpfgo -.PHONY: libbpfgo-static -.PHONY: libbpfgo-dynamic - -## libbpfgo - -libbpfgo-static: - $(MAKE) -C $(BASEDIR) libbpfgo-static - -libbpfgo-dynamic: - $(MAKE) -C $(BASEDIR) libbpfgo-dynamic - -vmlinuxh: - $(MAKE) -C $(BASEDIR) vmlinuxh - -outputdir: - $(MAKE) -C $(BASEDIR) outputdir - -## test bpf dependency - -$(TEST).bpf.o: $(TEST).bpf.c - $(MAKE) -C $(BASEDIR) vmlinuxh - $(CLANG) $(CFLAGS) -target bpf -I$(OUTPUT) -c $< -o $@ - -## test - -.PHONY: $(TEST)-static -.PHONY: $(TEST)-dynamic - -$(TEST)-static: libbpfgo-static | $(TEST).bpf.o - CC=$(CLANG) \ - CGO_CFLAGS=$(CGO_CFLAGS_STATIC) \ - CGO_LDFLAGS=$(CGO_LDFLAGS_STATIC) \ - GOOS=linux GOARCH=$(ARCH) \ - go build \ - -tags netgo -ldflags $(CGO_EXTLDFLAGS_STATIC) \ - -o $(TEST)-static ./$(TEST).go - -$(TEST)-dynamic: libbpfgo-dynamic | $(TEST).bpf.o - CC=$(CLANG) \ - CGO_CFLAGS=$(CGO_CFLAGS_DYN) \ - CGO_LDFLAGS=$(CGO_LDFLAGS_DYN) \ - go build -o ./$(TEST)-dynamic ./$(TEST).go - -## run - -.PHONY: run -.PHONY: run-static -.PHONY: run-dynamic - -run: run-static - -run-static: $(TEST)-static - ./run.sh $(TEST)-static - -run-dynamic: $(TEST)-dynamic - ./run.sh $(TEST)-dynamic - -clean: - rm -f *.o *-static *-dynamic diff --git a/selftest/version/Makefile b/selftest/version/Makefile new file mode 120000 index 00000000..d981720c --- /dev/null +++ b/selftest/version/Makefile @@ -0,0 +1 @@ +../common/Makefile \ No newline at end of file diff --git a/selftest/version/main.bpf.c b/selftest/version/main.bpf.c index 2d4d2dbd..cc98c25f 100644 --- a/selftest/version/main.bpf.c +++ b/selftest/version/main.bpf.c @@ -3,4 +3,3 @@ #undef asm_inline #define asm_inline asm #endif - diff --git a/selftest/xdp/Makefile b/selftest/xdp/Makefile deleted file mode 100644 index a8356a68..00000000 --- a/selftest/xdp/Makefile +++ /dev/null @@ -1,91 +0,0 @@ -BASEDIR = $(abspath ../../) - -OUTPUT = ../../output - -LIBBPF_SRC = $(abspath ../../libbpf/src) -LIBBPF_OBJ = $(abspath $(OUTPUT)/libbpf.a) - -CC = gcc -CLANG = clang -GO = go - -ARCH := $(shell uname -m | sed 's/x86_64/amd64/g; s/aarch64/arm64/g') - -CFLAGS = -g -O2 -Wall -fpie -LDFLAGS = - -CGO_CFLAGS_STATIC = "-I$(abspath $(OUTPUT))" -CGO_LDFLAGS_STATIC = "-lelf -lz $(LIBBPF_OBJ)" -CGO_EXTLDFLAGS_STATIC = '-w -extldflags "-static"' - -CGO_CFLAGS_DYN = "-I. -I/usr/include/" -CGO_LDFLAGS_DYN = "-lelf -lz -lbpf" - -.PHONY: $(TEST) -.PHONY: $(TEST).go -.PHONY: $(TEST).bpf.c - -TEST = main - -all: $(TEST)-static - -.PHONY: libbpfgo -.PHONY: libbpfgo-static -.PHONY: libbpfgo-dynamic - -## libbpfgo - -libbpfgo-static: - $(MAKE) -C $(BASEDIR) libbpfgo-static - -libbpfgo-dynamic: - $(MAKE) -C $(BASEDIR) libbpfgo-dynamic - -vmlinuxh: - $(MAKE) -C $(BASEDIR) vmlinuxh - -outputdir: - $(MAKE) -C $(BASEDIR) outputdir - -## test bpf dependency - -$(TEST).bpf.o: $(TEST).bpf.c - $(MAKE) -C $(BASEDIR) vmlinuxh - $(CLANG) $(CFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT) -c $< -o $@ - -## test - -.PHONY: $(TEST)-static -.PHONY: $(TEST)-dynamic - -$(TEST)-static: libbpfgo-static | $(TEST).bpf.o - CC=$(CLANG) \ - CGO_CFLAGS=$(CGO_CFLAGS_STATIC) \ - CGO_LDFLAGS=$(CGO_LDFLAGS_STATIC) \ - GOOS=linux GOARCH=$(ARCH) \ - $(GO) build \ - -tags netgo -ldflags $(CGO_EXTLDFLAGS_STATIC) \ - -o $(TEST)-static ./$(TEST).go - -$(TEST)-dynamic: libbpfgo-dynamic | $(TEST).bpf.o - CC=$(CLANG) \ - CGO_CFLAGS=$(CGO_CFLAGS_DYN) \ - CGO_LDFLAGS=$(CGO_LDFLAGS_DYN) \ - $(GO) build -o ./$(TEST)-dynamic ./$(TEST).go - -## run - -.PHONY: run -.PHONY: run-static -.PHONY: run-dynamic - -run: run-static - -run-static: $(TEST)-static - sudo ./run.sh $(TEST)-static - -run-dynamic: $(TEST)-dynamic - sudo ./run.sh $(TEST)-dynamic - -clean: - rm -f *.o *-static *-dynamic diff --git a/selftest/xdp/Makefile b/selftest/xdp/Makefile new file mode 120000 index 00000000..d981720c --- /dev/null +++ b/selftest/xdp/Makefile @@ -0,0 +1 @@ +../common/Makefile \ No newline at end of file diff --git a/selftest/xdp/main.bpf.c b/selftest/xdp/main.bpf.c index fd955c06..f61e3875 100644 --- a/selftest/xdp/main.bpf.c +++ b/selftest/xdp/main.bpf.c @@ -1,8 +1,10 @@ //+build ignore -#include "vmlinux.h" +#include #include #include +#include "vmlinux.h" + #ifdef asm_inline #undef asm_inline #define asm_inline asm diff --git a/selftest/xdp/run.sh b/selftest/xdp/run.sh deleted file mode 100755 index 106723ad..00000000 --- a/selftest/xdp/run.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/bash - -# SETTINGS - -TEST=$(dirname $0)/$1 # execute -TIMEOUT=5 # seconds - -# COMMON - -COMMON="$(dirname $0)/../common/common.sh" -[[ -f $COMMON ]] && { . $COMMON; } || { error "no common"; exit 1; } - -# MAIN - -kern_version ge 5.8 - -check_build -check_ppid -test_exec -test_finish - -exit 0 diff --git a/selftest/xdp/run.sh b/selftest/xdp/run.sh new file mode 120000 index 00000000..c1317de3 --- /dev/null +++ b/selftest/xdp/run.sh @@ -0,0 +1 @@ +../common/run-5.8.sh \ No newline at end of file From e77c2da93eafe4097dee10efca87ce3a045caa37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Geyslan=20Greg=C3=B3rio?= Date: Wed, 8 Feb 2023 19:53:58 -0300 Subject: [PATCH 2/2] install headers required for selftests In file included from main.bpf.c:2: In file included from ../../output/uapi/linux/bpf.h:11: /usr/include/linux/types.h:5:10: fatal error: 'asm/types.h' file not found --- .github/actions/build-dependencies/action.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/actions/build-dependencies/action.yaml b/.github/actions/build-dependencies/action.yaml index 3753c2c0..fe44878a 100644 --- a/.github/actions/build-dependencies/action.yaml +++ b/.github/actions/build-dependencies/action.yaml @@ -13,6 +13,7 @@ runs: sudo apt-get update sudo apt-get install --yes bsdutils sudo apt-get install --yes build-essential + sudo apt-get install --yes gcc-multilib sudo apt-get install --yes pkgconf sudo apt-get install --yes llvm-12 clang-12 sudo apt-get install --yes clang-format-12