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_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 $(Q)$(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)/% ;
128 $(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
131 $(call msg,BINARY,,$@)
132 $(Q)$(LINK.c) $^ $(LDLIBS) -o $@
134 $(OUTPUT)/urandom_read: urandom_read.c
135 $(call msg,BINARY,,$@)
136 $(Q)$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS) -Wl,--build-id
138 $(OUTPUT)/test_stub.o: test_stub.c $(BPFOBJ)
140 $(Q)$(CC) -c $(CFLAGS) -o $@ $<
142 VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \
143 $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \
144 ../../../../vmlinux \
145 /sys/kernel/btf/vmlinux \
146 /boot/vmlinux-$(shell uname -r)
147 VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
149 DEFAULT_BPFTOOL := $(SCRATCH_DIR)/sbin/bpftool
151 $(OUTPUT)/runqslower: $(BPFOBJ) | $(DEFAULT_BPFTOOL)
152 $(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/runqslower \
153 OUTPUT=$(SCRATCH_DIR)/ VMLINUX_BTF=$(VMLINUX_BTF) \
154 BPFOBJ=$(BPFOBJ) BPF_INCLUDE=$(INCLUDE_DIR) && \
155 cp $(SCRATCH_DIR)/runqslower $@
157 $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/test_stub.o $(BPFOBJ)
159 $(OUTPUT)/test_dev_cgroup: cgroup_helpers.c
160 $(OUTPUT)/test_skb_cgroup_id_user: cgroup_helpers.c
161 $(OUTPUT)/test_sock: cgroup_helpers.c
162 $(OUTPUT)/test_sock_addr: cgroup_helpers.c
163 $(OUTPUT)/test_socket_cookie: cgroup_helpers.c
164 $(OUTPUT)/test_sockmap: cgroup_helpers.c
165 $(OUTPUT)/test_tcpbpf_user: cgroup_helpers.c
166 $(OUTPUT)/test_tcpnotify_user: cgroup_helpers.c trace_helpers.c
167 $(OUTPUT)/get_cgroup_id_user: cgroup_helpers.c
168 $(OUTPUT)/test_cgroup_storage: cgroup_helpers.c
169 $(OUTPUT)/test_netcnt: cgroup_helpers.c
170 $(OUTPUT)/test_sock_fields: cgroup_helpers.c
171 $(OUTPUT)/test_sysctl: cgroup_helpers.c
173 BPFTOOL ?= $(DEFAULT_BPFTOOL)
174 $(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) \
175 $(BPFOBJ) | $(BUILD_DIR)/bpftool
176 $(Q)$(MAKE) $(submake_extras) -C $(BPFTOOLDIR) \
177 OUTPUT=$(BUILD_DIR)/bpftool/ \
178 prefix= DESTDIR=$(SCRATCH_DIR)/ install
180 $(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile) \
181 ../../../include/uapi/linux/bpf.h \
182 | $(INCLUDE_DIR) $(BUILD_DIR)/libbpf
183 $(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(BUILD_DIR)/libbpf/ \
184 DESTDIR=$(SCRATCH_DIR) prefix= all install_headers
186 $(BUILD_DIR)/libbpf $(BUILD_DIR)/bpftool $(BUILD_DIR)/resolve_btfids $(INCLUDE_DIR):
187 $(call msg,MKDIR,,$@)
190 $(INCLUDE_DIR)/vmlinux.h: $(VMLINUX_BTF) | $(BPFTOOL) $(INCLUDE_DIR)
193 $(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
196 $(Q)cp "$(VMLINUX_H)" $@
199 $(RESOLVE_BTFIDS): $(BPFOBJ) | $(BUILD_DIR)/resolve_btfids \
200 $(TOOLSDIR)/bpf/resolve_btfids/main.c \
201 $(TOOLSDIR)/lib/rbtree.c \
202 $(TOOLSDIR)/lib/zalloc.c \
203 $(TOOLSDIR)/lib/string.c \
204 $(TOOLSDIR)/lib/ctype.c \
205 $(TOOLSDIR)/lib/str_error_r.c
206 $(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/resolve_btfids \
207 OUTPUT=$(BUILD_DIR)/resolve_btfids/ BPFOBJ=$(BPFOBJ)
209 # Get Clang's default includes on this system, as opposed to those seen by
210 # '-target bpf'. This fixes "missing" files on some architectures/distros,
211 # such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
213 # Use '-idirafter': Don't interfere with include mechanics except where the
214 # build would have failed anyways.
215 define get_sys_includes
216 $(shell $(1) -v -E - </dev/null 2>&1 \
217 | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }')
220 # Determine target endianness.
221 IS_LITTLE_ENDIAN = $(shell $(CC) -dM -E - </dev/null | \
222 grep 'define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__')
223 MENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian)
225 CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
226 BPF_CFLAGS = -g -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN) \
227 -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR) \
228 -I$(abspath $(OUTPUT)/../usr/include)
230 CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \
231 -Wno-compare-distinct-pointer-types
233 $(OUTPUT)/test_l4lb_noinline.o: BPF_CFLAGS += -fno-inline
234 $(OUTPUT)/test_xdp_noinline.o: BPF_CFLAGS += -fno-inline
236 $(OUTPUT)/flow_dissector_load.o: flow_dissector_load.h
238 # Build BPF object using Clang
240 # $2 - output .o file
243 define CLANG_BPF_BUILD_RULE
244 $(call msg,CLNG-LLC,$(TRUNNER_BINARY),$2)
245 $(Q)($(CLANG) $3 -O2 -target bpf -emit-llvm \
246 -c $1 -o - || echo "BPF obj compilation failed") | \
247 $(LLC) -mattr=dwarfris -march=bpf -mcpu=v3 $4 -filetype=obj -o $2
249 # Similar to CLANG_BPF_BUILD_RULE, but with disabled alu32
250 define CLANG_NOALU32_BPF_BUILD_RULE
251 $(call msg,CLNG-LLC,$(TRUNNER_BINARY),$2)
252 $(Q)($(CLANG) $3 -O2 -target bpf -emit-llvm \
253 -c $1 -o - || echo "BPF obj compilation failed") | \
254 $(LLC) -march=bpf -mcpu=v2 $4 -filetype=obj -o $2
256 # Similar to CLANG_BPF_BUILD_RULE, but using native Clang and bpf LLC
257 define CLANG_NATIVE_BPF_BUILD_RULE
258 $(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2)
259 $(Q)($(CLANG) $3 -O2 -emit-llvm \
260 -c $1 -o - || echo "BPF obj compilation failed") | \
261 $(LLC) -march=bpf -mcpu=v3 $4 -filetype=obj -o $2
263 # Build BPF object using GCC
264 define GCC_BPF_BUILD_RULE
265 $(call msg,GCC-BPF,$(TRUNNER_BINARY),$2)
266 $(Q)$(BPF_GCC) $3 $4 -O2 -c $1 -o $2
269 SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c
271 # Set up extra TRUNNER_XXX "temporary" variables in the environment (relies on
272 # $eval()) and pass control to DEFINE_TEST_RUNNER_RULES.
274 # $1 - test runner base binary name (e.g., test_progs)
275 # $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc)
276 define DEFINE_TEST_RUNNER
278 TRUNNER_OUTPUT := $(OUTPUT)$(if $2,/)$2
279 TRUNNER_BINARY := $1$(if $2,-)$2
280 TRUNNER_TEST_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.test.o, \
281 $$(notdir $$(wildcard $(TRUNNER_TESTS_DIR)/*.c)))
282 TRUNNER_EXTRA_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, \
283 $$(filter %.c,$(TRUNNER_EXTRA_SOURCES)))
284 TRUNNER_EXTRA_HDRS := $$(filter %.h,$(TRUNNER_EXTRA_SOURCES))
285 TRUNNER_TESTS_HDR := $(TRUNNER_TESTS_DIR)/tests.h
286 TRUNNER_BPF_SRCS := $$(notdir $$(wildcard $(TRUNNER_BPF_PROGS_DIR)/*.c))
287 TRUNNER_BPF_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, $$(TRUNNER_BPF_SRCS))
288 TRUNNER_BPF_SKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.skel.h, \
289 $$(filter-out $(SKEL_BLACKLIST), \
290 $$(TRUNNER_BPF_SRCS)))
291 TEST_GEN_FILES += $$(TRUNNER_BPF_OBJS)
293 # Evaluate rules now with extra TRUNNER_XXX variables above already defined
294 $$(eval $$(call DEFINE_TEST_RUNNER_RULES,$1,$2))
298 # Using TRUNNER_XXX variables, provided by callers of DEFINE_TEST_RUNNER and
299 # set up by DEFINE_TEST_RUNNER itself, create test runner build rules with:
300 # $1 - test runner base binary name (e.g., test_progs)
301 # $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc)
302 define DEFINE_TEST_RUNNER_RULES
304 ifeq ($($(TRUNNER_OUTPUT)-dir),)
305 $(TRUNNER_OUTPUT)-dir := y
307 $$(call msg,MKDIR,,$$@)
311 # ensure we set up BPF objects generation rule just once for a given
312 # input/output directory combination
313 ifeq ($($(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs),)
314 $(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs := y
315 $(TRUNNER_BPF_OBJS): $(TRUNNER_OUTPUT)/%.o: \
316 $(TRUNNER_BPF_PROGS_DIR)/%.c \
317 $(TRUNNER_BPF_PROGS_DIR)/*.h \
318 $$(INCLUDE_DIR)/vmlinux.h \
319 $$(BPFOBJ) | $(TRUNNER_OUTPUT)
320 $$(call $(TRUNNER_BPF_BUILD_RULE),$$<,$$@, \
321 $(TRUNNER_BPF_CFLAGS), \
322 $(TRUNNER_BPF_LDFLAGS))
324 $(TRUNNER_BPF_SKELS): $(TRUNNER_OUTPUT)/%.skel.h: \
325 $(TRUNNER_OUTPUT)/%.o \
326 | $(BPFTOOL) $(TRUNNER_OUTPUT)
327 $$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
328 $(Q)$$(BPFTOOL) gen skeleton $$< > $$@
331 # ensure we set up tests.h header generation rule just once
332 ifeq ($($(TRUNNER_TESTS_DIR)-tests-hdr),)
333 $(TRUNNER_TESTS_DIR)-tests-hdr := y
334 $(TRUNNER_TESTS_HDR): $(TRUNNER_TESTS_DIR)/*.c
335 $$(call msg,TEST-HDR,$(TRUNNER_BINARY),$$@)
336 $$(shell ( cd $(TRUNNER_TESTS_DIR); \
337 echo '/* Generated header, do not edit */'; \
338 ls *.c 2> /dev/null | \
339 sed -e 's@\([^\.]*\)\.c@DEFINE_TEST(\1)@'; \
343 # compile individual test files
344 # Note: we cd into output directory to ensure embedded BPF object is found
345 $(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o: \
346 $(TRUNNER_TESTS_DIR)/%.c \
347 $(TRUNNER_EXTRA_HDRS) \
348 $(TRUNNER_BPF_OBJS) \
349 $(TRUNNER_BPF_SKELS) \
350 $$(BPFOBJ) | $(TRUNNER_OUTPUT)
351 $$(call msg,TEST-OBJ,$(TRUNNER_BINARY),$$@)
352 $(Q)cd $$(@D) && $$(CC) -I. $$(CFLAGS) -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F)
354 $(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o: \
356 $(TRUNNER_EXTRA_HDRS) \
357 $(TRUNNER_TESTS_HDR) \
358 $$(BPFOBJ) | $(TRUNNER_OUTPUT)
359 $$(call msg,EXT-OBJ,$(TRUNNER_BINARY),$$@)
360 $(Q)$$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@
362 # only copy extra resources if in flavored build
363 $(TRUNNER_BINARY)-extras: $(TRUNNER_EXTRA_FILES) | $(TRUNNER_OUTPUT)
365 $$(call msg,EXT-COPY,$(TRUNNER_BINARY),$(TRUNNER_EXTRA_FILES))
366 $(Q)cp -a $$^ $(TRUNNER_OUTPUT)/
369 $(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS) \
370 $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ) \
372 | $(TRUNNER_BINARY)-extras
373 $$(call msg,BINARY,,$$@)
374 $(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
375 $(Q)$(RESOLVE_BTFIDS) --no-fail --btf btf_data.o $$@
379 # Define test_progs test runner.
380 TRUNNER_TESTS_DIR := prog_tests
381 TRUNNER_BPF_PROGS_DIR := progs
382 TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c \
383 network_helpers.c testing_helpers.c \
384 flow_dissector_load.h
385 TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \
386 $(wildcard progs/btf_dump_test_case_*.c)
387 TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE
388 TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS)
389 TRUNNER_BPF_LDFLAGS := -mattr=+alu32
390 $(eval $(call DEFINE_TEST_RUNNER,test_progs))
392 # Define test_progs-no_alu32 test runner.
393 TRUNNER_BPF_BUILD_RULE := CLANG_NOALU32_BPF_BUILD_RULE
394 TRUNNER_BPF_LDFLAGS :=
395 $(eval $(call DEFINE_TEST_RUNNER,test_progs,no_alu32))
397 # Define test_progs BPF-GCC-flavored test runner.
399 TRUNNER_BPF_BUILD_RULE := GCC_BPF_BUILD_RULE
400 TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(call get_sys_includes,gcc)
401 TRUNNER_BPF_LDFLAGS :=
402 $(eval $(call DEFINE_TEST_RUNNER,test_progs,bpf_gcc))
405 # Define test_maps test runner.
406 TRUNNER_TESTS_DIR := map_tests
407 TRUNNER_BPF_PROGS_DIR := progs
408 TRUNNER_EXTRA_SOURCES := test_maps.c
409 TRUNNER_EXTRA_FILES :=
410 TRUNNER_BPF_BUILD_RULE := $$(error no BPF objects should be built)
411 TRUNNER_BPF_CFLAGS :=
412 TRUNNER_BPF_LDFLAGS :=
413 $(eval $(call DEFINE_TEST_RUNNER,test_maps))
415 # Define test_verifier test runner.
416 # It is much simpler than test_maps/test_progs and sufficiently different from
417 # them (e.g., test.h is using completely pattern), that it's worth just
418 # explicitly defining all the rules explicitly.
419 verifier/tests.h: verifier/*.c
420 $(shell ( cd verifier/; \
421 echo '/* Generated header, do not edit */'; \
422 echo '#ifdef FILL_ARRAY'; \
423 ls *.c 2> /dev/null | sed -e 's@\(.*\)@#include \"\1\"@'; \
425 ) > verifier/tests.h)
426 $(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT)
427 $(call msg,BINARY,,$@)
428 $(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
430 # Make sure we are able to include and link libbpf against c++.
431 $(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ)
433 $(Q)$(CXX) $(CFLAGS) $^ $(LDLIBS) -o $@
436 $(OUTPUT)/bench_%.o: benchs/bench_%.c bench.h
438 $(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
439 $(OUTPUT)/bench_rename.o: $(OUTPUT)/test_overhead.skel.h
440 $(OUTPUT)/bench_trigger.o: $(OUTPUT)/trigger_bench.skel.h
441 $(OUTPUT)/bench_ringbufs.o: $(OUTPUT)/ringbuf_bench.skel.h \
442 $(OUTPUT)/perfbuf_bench.skel.h
443 $(OUTPUT)/bench.o: bench.h testing_helpers.h
444 $(OUTPUT)/bench: LDLIBS += -lm
445 $(OUTPUT)/bench: $(OUTPUT)/bench.o $(OUTPUT)/testing_helpers.o \
446 $(OUTPUT)/bench_count.o \
447 $(OUTPUT)/bench_rename.o \
448 $(OUTPUT)/bench_trigger.o \
449 $(OUTPUT)/bench_ringbufs.o
450 $(call msg,BINARY,,$@)
451 $(Q)$(CC) $(LDFLAGS) -o $@ $(filter %.a %.o,$^) $(LDLIBS)
453 EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(SCRATCH_DIR) \
454 prog_tests/tests.h map_tests/tests.h verifier/tests.h \
456 $(addprefix $(OUTPUT)/,*.o *.skel.h no_alu32 bpf_gcc)