1 # SPDX-License-Identifier: GPL-2.0
2 include ../../../../scripts/Kbuild.include
3 include ../../../scripts/Makefile.arch
5 CXX ?= $(CROSS_COMPILE)g++
8 TOOLSDIR := $(abspath ../../..)
9 LIBDIR := $(TOOLSDIR)/lib
10 BPFDIR := $(LIBDIR)/bpf
11 TOOLSINCDIR := $(TOOLSDIR)/include
12 BPFTOOLDIR := $(TOOLSDIR)/bpf/bpftool
13 APIDIR := $(TOOLSINCDIR)/uapi
14 GENDIR := $(abspath ../../../../include/generated)
15 GENHDR := $(GENDIR)/autoconf.h
17 ifneq ($(wildcard $(GENHDR)),)
18 GENFLAGS := -DHAVE_GENHDR
23 LLVM_OBJCOPY ?= llvm-objcopy
24 BPF_GCC ?= $(shell command -v bpf-gcc;)
26 CFLAGS += -g -rdynamic -Wall -O2 $(GENFLAGS) $(SAN_CFLAGS) \
27 -I$(CURDIR) -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR) \
28 -I$(TOOLSINCDIR) -I$(APIDIR) \
29 -Dbpf_prog_load=bpf_prog_test_load \
30 -Dbpf_load_program=bpf_test_load_program
31 LDLIBS += -lcap -lelf -lz -lrt -lpthread
33 # Order correspond to 'make run_tests' order
34 TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \
35 test_align test_verifier_log test_dev_cgroup test_tcpbpf_user \
36 test_sock test_btf test_sockmap get_cgroup_id_user test_socket_cookie \
38 test_netcnt test_tcpnotify_user test_sock_fields test_sysctl \
40 test_current_pid_tgid_new_ns
42 # Also test bpf-gcc, if present
44 TEST_GEN_PROGS += test_progs-bpf_gcc
48 TEST_FILES = test_lwt_ip_encap.o \
51 # Order correspond to 'make run_tests' order
52 TEST_PROGS := test_kmod.sh \
53 test_xdp_redirect.sh \
59 test_lwt_seg6local.sh \
61 test_skb_cgroup_id.sh \
62 test_flow_dissector.sh \
63 test_xdp_vlan_mode_generic.sh \
64 test_xdp_vlan_mode_native.sh \
65 test_lwt_ip_encap.sh \
66 test_tcp_check_syncookie.sh \
70 test_bpftool_build.sh \
73 TEST_PROGS_EXTENDED := with_addr.sh \
79 # Compile but not part of 'make run_tests'
80 TEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \
81 flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \
82 test_lirc_mode2_user xdping test_cpp runqslower bench
84 TEST_CUSTOM_PROGS = urandom_read
86 # Emit succinct information message describing current building step
87 # $1 - generic step name (e.g., CC, LINK, etc);
88 # $2 - optional "flavor" specifier; if provided, will be emitted as [flavor];
89 # $3 - target (assumed to be file); only file name will be emitted;
90 # $4 - optional extra arg, emitted as-is, if provided.
96 msg = @printf ' %-8s%s %s%s\n' "$(1)" "$(if $(2), [$(2)])" "$(notdir $(3))" "$(if $(4), $(4))";
97 MAKEFLAGS += --no-print-directory
98 submake_extras := feature_display=0
101 # override lib.mk's default rules
102 OVERRIDE_TARGETS := 1
103 override define CLEAN
105 $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
110 SCRATCH_DIR := $(OUTPUT)/tools
111 BUILD_DIR := $(SCRATCH_DIR)/build
112 INCLUDE_DIR := $(SCRATCH_DIR)/include
113 BPFOBJ := $(BUILD_DIR)/libbpf/libbpf.a
114 RESOLVE_BTFIDS := $(BUILD_DIR)/resolve_btfids/resolve_btfids
116 # Define simple and short `make test_progs`, `make test_sysctl`, etc targets
117 # to build individual tests.
118 # NOTE: Semicolon at the end is critical to override lib.mk's default static
120 $(notdir $(TEST_GEN_PROGS) \
122 $(TEST_PROGS_EXTENDED) \
123 $(TEST_GEN_PROGS_EXTENDED) \
124 $(TEST_CUSTOM_PROGS)): %: $(OUTPUT)/% ;
127 $(call msg,BINARY,,$@)
128 $(LINK.c) $^ $(LDLIBS) -o $@
130 $(OUTPUT)/urandom_read: urandom_read.c
131 $(call msg,BINARY,,$@)
132 $(CC) $(LDFLAGS) -o $@ $< $(LDLIBS) -Wl,--build-id
134 $(OUTPUT)/test_stub.o: test_stub.c $(BPFOBJ)
136 $(CC) -c $(CFLAGS) -o $@ $<
138 VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \
139 $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \
140 ../../../../vmlinux \
141 /sys/kernel/btf/vmlinux \
142 /boot/vmlinux-$(shell uname -r)
143 VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
145 $(OUTPUT)/runqslower: $(BPFOBJ)
146 $(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/runqslower \
147 OUTPUT=$(SCRATCH_DIR)/ VMLINUX_BTF=$(VMLINUX_BTF) \
148 BPFOBJ=$(BPFOBJ) BPF_INCLUDE=$(INCLUDE_DIR) && \
149 cp $(SCRATCH_DIR)/runqslower $@
151 $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/test_stub.o $(BPFOBJ)
153 $(OUTPUT)/test_dev_cgroup: cgroup_helpers.c
154 $(OUTPUT)/test_skb_cgroup_id_user: cgroup_helpers.c
155 $(OUTPUT)/test_sock: cgroup_helpers.c
156 $(OUTPUT)/test_sock_addr: cgroup_helpers.c
157 $(OUTPUT)/test_socket_cookie: cgroup_helpers.c
158 $(OUTPUT)/test_sockmap: cgroup_helpers.c
159 $(OUTPUT)/test_tcpbpf_user: cgroup_helpers.c
160 $(OUTPUT)/test_tcpnotify_user: cgroup_helpers.c trace_helpers.c
161 $(OUTPUT)/get_cgroup_id_user: cgroup_helpers.c
162 $(OUTPUT)/test_cgroup_storage: cgroup_helpers.c
163 $(OUTPUT)/test_netcnt: cgroup_helpers.c
164 $(OUTPUT)/test_sock_fields: cgroup_helpers.c
165 $(OUTPUT)/test_sysctl: cgroup_helpers.c
167 DEFAULT_BPFTOOL := $(SCRATCH_DIR)/sbin/bpftool
168 BPFTOOL ?= $(DEFAULT_BPFTOOL)
169 $(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) \
170 $(BPFOBJ) | $(BUILD_DIR)/bpftool
171 $(Q)$(MAKE) $(submake_extras) -C $(BPFTOOLDIR) \
172 OUTPUT=$(BUILD_DIR)/bpftool/ \
173 prefix= DESTDIR=$(SCRATCH_DIR)/ install
175 $(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile) \
176 ../../../include/uapi/linux/bpf.h \
177 | $(INCLUDE_DIR) $(BUILD_DIR)/libbpf
178 $(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(BUILD_DIR)/libbpf/ \
179 DESTDIR=$(SCRATCH_DIR) prefix= all install_headers
181 $(BUILD_DIR)/libbpf $(BUILD_DIR)/bpftool $(BUILD_DIR)/resolve_btfids $(INCLUDE_DIR):
182 $(call msg,MKDIR,,$@)
185 $(INCLUDE_DIR)/vmlinux.h: $(VMLINUX_BTF) | $(BPFTOOL) $(INCLUDE_DIR)
188 $(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
194 $(RESOLVE_BTFIDS): $(BPFOBJ) | $(BUILD_DIR)/resolve_btfids \
195 $(TOOLSDIR)/bpf/resolve_btfids/main.c \
196 $(TOOLSDIR)/lib/rbtree.c \
197 $(TOOLSDIR)/lib/zalloc.c \
198 $(TOOLSDIR)/lib/string.c \
199 $(TOOLSDIR)/lib/ctype.c \
200 $(TOOLSDIR)/lib/str_error_r.c
201 $(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/resolve_btfids \
202 OUTPUT=$(BUILD_DIR)/resolve_btfids/ BPFOBJ=$(BPFOBJ)
204 # Get Clang's default includes on this system, as opposed to those seen by
205 # '-target bpf'. This fixes "missing" files on some architectures/distros,
206 # such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
208 # Use '-idirafter': Don't interfere with include mechanics except where the
209 # build would have failed anyways.
210 define get_sys_includes
211 $(shell $(1) -v -E - </dev/null 2>&1 \
212 | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }')
215 # Determine target endianness.
216 IS_LITTLE_ENDIAN = $(shell $(CC) -dM -E - </dev/null | \
217 grep 'define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__')
218 MENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian)
220 CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
221 BPF_CFLAGS = -g -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN) \
222 -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR) \
223 -I$(abspath $(OUTPUT)/../usr/include)
225 CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \
226 -Wno-compare-distinct-pointer-types
228 $(OUTPUT)/test_l4lb_noinline.o: BPF_CFLAGS += -fno-inline
229 $(OUTPUT)/test_xdp_noinline.o: BPF_CFLAGS += -fno-inline
231 $(OUTPUT)/flow_dissector_load.o: flow_dissector_load.h
233 # Build BPF object using Clang
235 # $2 - output .o file
238 define CLANG_BPF_BUILD_RULE
239 $(call msg,CLNG-LLC,$(TRUNNER_BINARY),$2)
240 ($(CLANG) $3 -O2 -target bpf -emit-llvm \
241 -c $1 -o - || echo "BPF obj compilation failed") | \
242 $(LLC) -mattr=dwarfris -march=bpf -mcpu=v3 $4 -filetype=obj -o $2
244 # Similar to CLANG_BPF_BUILD_RULE, but with disabled alu32
245 define CLANG_NOALU32_BPF_BUILD_RULE
246 $(call msg,CLNG-LLC,$(TRUNNER_BINARY),$2)
247 ($(CLANG) $3 -O2 -target bpf -emit-llvm \
248 -c $1 -o - || echo "BPF obj compilation failed") | \
249 $(LLC) -march=bpf -mcpu=v2 $4 -filetype=obj -o $2
251 # Similar to CLANG_BPF_BUILD_RULE, but using native Clang and bpf LLC
252 define CLANG_NATIVE_BPF_BUILD_RULE
253 $(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2)
254 ($(CLANG) $3 -O2 -emit-llvm \
255 -c $1 -o - || echo "BPF obj compilation failed") | \
256 $(LLC) -march=bpf -mcpu=v3 $4 -filetype=obj -o $2
258 # Build BPF object using GCC
259 define GCC_BPF_BUILD_RULE
260 $(call msg,GCC-BPF,$(TRUNNER_BINARY),$2)
261 $(BPF_GCC) $3 $4 -O2 -c $1 -o $2
264 SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c
266 # Set up extra TRUNNER_XXX "temporary" variables in the environment (relies on
267 # $eval()) and pass control to DEFINE_TEST_RUNNER_RULES.
269 # $1 - test runner base binary name (e.g., test_progs)
270 # $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc)
271 define DEFINE_TEST_RUNNER
273 TRUNNER_OUTPUT := $(OUTPUT)$(if $2,/)$2
274 TRUNNER_BINARY := $1$(if $2,-)$2
275 TRUNNER_TEST_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.test.o, \
276 $$(notdir $$(wildcard $(TRUNNER_TESTS_DIR)/*.c)))
277 TRUNNER_EXTRA_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, \
278 $$(filter %.c,$(TRUNNER_EXTRA_SOURCES)))
279 TRUNNER_EXTRA_HDRS := $$(filter %.h,$(TRUNNER_EXTRA_SOURCES))
280 TRUNNER_TESTS_HDR := $(TRUNNER_TESTS_DIR)/tests.h
281 TRUNNER_BPF_SRCS := $$(notdir $$(wildcard $(TRUNNER_BPF_PROGS_DIR)/*.c))
282 TRUNNER_BPF_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, $$(TRUNNER_BPF_SRCS))
283 TRUNNER_BPF_SKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.skel.h, \
284 $$(filter-out $(SKEL_BLACKLIST), \
285 $$(TRUNNER_BPF_SRCS)))
286 TEST_GEN_FILES += $$(TRUNNER_BPF_OBJS)
288 # Evaluate rules now with extra TRUNNER_XXX variables above already defined
289 $$(eval $$(call DEFINE_TEST_RUNNER_RULES,$1,$2))
293 # Using TRUNNER_XXX variables, provided by callers of DEFINE_TEST_RUNNER and
294 # set up by DEFINE_TEST_RUNNER itself, create test runner build rules with:
295 # $1 - test runner base binary name (e.g., test_progs)
296 # $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc)
297 define DEFINE_TEST_RUNNER_RULES
299 ifeq ($($(TRUNNER_OUTPUT)-dir),)
300 $(TRUNNER_OUTPUT)-dir := y
302 $$(call msg,MKDIR,,$$@)
306 # ensure we set up BPF objects generation rule just once for a given
307 # input/output directory combination
308 ifeq ($($(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs),)
309 $(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs := y
310 $(TRUNNER_BPF_OBJS): $(TRUNNER_OUTPUT)/%.o: \
311 $(TRUNNER_BPF_PROGS_DIR)/%.c \
312 $(TRUNNER_BPF_PROGS_DIR)/*.h \
313 $$(INCLUDE_DIR)/vmlinux.h \
314 $$(BPFOBJ) | $(TRUNNER_OUTPUT)
315 $$(call $(TRUNNER_BPF_BUILD_RULE),$$<,$$@, \
316 $(TRUNNER_BPF_CFLAGS), \
317 $(TRUNNER_BPF_LDFLAGS))
319 $(TRUNNER_BPF_SKELS): $(TRUNNER_OUTPUT)/%.skel.h: \
320 $(TRUNNER_OUTPUT)/%.o \
321 | $(BPFTOOL) $(TRUNNER_OUTPUT)
322 $$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
323 $$(BPFTOOL) gen skeleton $$< > $$@
326 # ensure we set up tests.h header generation rule just once
327 ifeq ($($(TRUNNER_TESTS_DIR)-tests-hdr),)
328 $(TRUNNER_TESTS_DIR)-tests-hdr := y
329 $(TRUNNER_TESTS_HDR): $(TRUNNER_TESTS_DIR)/*.c
330 $$(call msg,TEST-HDR,$(TRUNNER_BINARY),$$@)
331 $$(shell ( cd $(TRUNNER_TESTS_DIR); \
332 echo '/* Generated header, do not edit */'; \
333 ls *.c 2> /dev/null | \
334 sed -e 's@\([^\.]*\)\.c@DEFINE_TEST(\1)@'; \
338 # compile individual test files
339 # Note: we cd into output directory to ensure embedded BPF object is found
340 $(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o: \
341 $(TRUNNER_TESTS_DIR)/%.c \
342 $(TRUNNER_EXTRA_HDRS) \
343 $(TRUNNER_BPF_OBJS) \
344 $(TRUNNER_BPF_SKELS) \
345 $$(BPFOBJ) | $(TRUNNER_OUTPUT)
346 $$(call msg,TEST-OBJ,$(TRUNNER_BINARY),$$@)
347 cd $$(@D) && $$(CC) -I. $$(CFLAGS) -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F)
349 $(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o: \
351 $(TRUNNER_EXTRA_HDRS) \
352 $(TRUNNER_TESTS_HDR) \
353 $$(BPFOBJ) | $(TRUNNER_OUTPUT)
354 $$(call msg,EXT-OBJ,$(TRUNNER_BINARY),$$@)
355 $$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@
357 # only copy extra resources if in flavored build
358 $(TRUNNER_BINARY)-extras: $(TRUNNER_EXTRA_FILES) | $(TRUNNER_OUTPUT)
360 $$(call msg,EXT-COPY,$(TRUNNER_BINARY),$(TRUNNER_EXTRA_FILES))
361 cp -a $$^ $(TRUNNER_OUTPUT)/
364 $(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS) \
365 $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ) \
367 | $(TRUNNER_BINARY)-extras
368 $$(call msg,BINARY,,$$@)
369 $$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
370 $(RESOLVE_BTFIDS) --no-fail --btf btf_data.o $$@
374 # Define test_progs test runner.
375 TRUNNER_TESTS_DIR := prog_tests
376 TRUNNER_BPF_PROGS_DIR := progs
377 TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c \
378 network_helpers.c testing_helpers.c \
379 flow_dissector_load.h
380 TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \
381 $(wildcard progs/btf_dump_test_case_*.c)
382 TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE
383 TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS)
384 TRUNNER_BPF_LDFLAGS := -mattr=+alu32
385 $(eval $(call DEFINE_TEST_RUNNER,test_progs))
387 # Define test_progs-no_alu32 test runner.
388 TRUNNER_BPF_BUILD_RULE := CLANG_NOALU32_BPF_BUILD_RULE
389 TRUNNER_BPF_LDFLAGS :=
390 $(eval $(call DEFINE_TEST_RUNNER,test_progs,no_alu32))
392 # Define test_progs BPF-GCC-flavored test runner.
394 TRUNNER_BPF_BUILD_RULE := GCC_BPF_BUILD_RULE
395 TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(call get_sys_includes,gcc)
396 TRUNNER_BPF_LDFLAGS :=
397 $(eval $(call DEFINE_TEST_RUNNER,test_progs,bpf_gcc))
400 # Define test_maps test runner.
401 TRUNNER_TESTS_DIR := map_tests
402 TRUNNER_BPF_PROGS_DIR := progs
403 TRUNNER_EXTRA_SOURCES := test_maps.c
404 TRUNNER_EXTRA_FILES :=
405 TRUNNER_BPF_BUILD_RULE := $$(error no BPF objects should be built)
406 TRUNNER_BPF_CFLAGS :=
407 TRUNNER_BPF_LDFLAGS :=
408 $(eval $(call DEFINE_TEST_RUNNER,test_maps))
410 # Define test_verifier test runner.
411 # It is much simpler than test_maps/test_progs and sufficiently different from
412 # them (e.g., test.h is using completely pattern), that it's worth just
413 # explicitly defining all the rules explicitly.
414 verifier/tests.h: verifier/*.c
415 $(shell ( cd verifier/; \
416 echo '/* Generated header, do not edit */'; \
417 echo '#ifdef FILL_ARRAY'; \
418 ls *.c 2> /dev/null | sed -e 's@\(.*\)@#include \"\1\"@'; \
420 ) > verifier/tests.h)
421 $(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT)
422 $(call msg,BINARY,,$@)
423 $(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
425 # Make sure we are able to include and link libbpf against c++.
426 $(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ)
428 $(CXX) $(CFLAGS) $^ $(LDLIBS) -o $@
431 $(OUTPUT)/bench_%.o: benchs/bench_%.c bench.h
433 $(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
434 $(OUTPUT)/bench_rename.o: $(OUTPUT)/test_overhead.skel.h
435 $(OUTPUT)/bench_trigger.o: $(OUTPUT)/trigger_bench.skel.h
436 $(OUTPUT)/bench_ringbufs.o: $(OUTPUT)/ringbuf_bench.skel.h \
437 $(OUTPUT)/perfbuf_bench.skel.h
438 $(OUTPUT)/bench.o: bench.h testing_helpers.h
439 $(OUTPUT)/bench: LDLIBS += -lm
440 $(OUTPUT)/bench: $(OUTPUT)/bench.o $(OUTPUT)/testing_helpers.o \
441 $(OUTPUT)/bench_count.o \
442 $(OUTPUT)/bench_rename.o \
443 $(OUTPUT)/bench_trigger.o \
444 $(OUTPUT)/bench_ringbufs.o
445 $(call msg,BINARY,,$@)
446 $(CC) $(LDFLAGS) -o $@ $(filter %.a %.o,$^) $(LDLIBS)
448 EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(SCRATCH_DIR) \
449 prog_tests/tests.h map_tests/tests.h verifier/tests.h \
451 $(addprefix $(OUTPUT)/,*.o *.skel.h no_alu32 bpf_gcc)