xref: /linux/tools/testing/selftests/bpf/Makefile (revision 4dba3e7852b7717a20ba206ab23ad289a0a5c504)
1# SPDX-License-Identifier: GPL-2.0
2include ../../../build/Build.include
3include ../../../scripts/Makefile.arch
4include ../../../scripts/Makefile.include
5
6CXX ?= $(CROSS_COMPILE)g++
7
8CURDIR := $(abspath .)
9TOOLSDIR := $(abspath ../../..)
10LIBDIR := $(TOOLSDIR)/lib
11BPFDIR := $(LIBDIR)/bpf
12TOOLSINCDIR := $(TOOLSDIR)/include
13BPFTOOLDIR := $(TOOLSDIR)/bpf/bpftool
14APIDIR := $(TOOLSINCDIR)/uapi
15GENDIR := $(abspath ../../../../include/generated)
16GENHDR := $(GENDIR)/autoconf.h
17HOSTPKG_CONFIG := pkg-config
18
19ifneq ($(wildcard $(GENHDR)),)
20  GENFLAGS := -DHAVE_GENHDR
21endif
22
23BPF_GCC		?= $(shell command -v bpf-gcc;)
24SAN_CFLAGS	?=
25CFLAGS += -g -O0 -rdynamic -Wall -Werror $(GENFLAGS) $(SAN_CFLAGS)	\
26	  -I$(CURDIR) -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR)		\
27	  -I$(TOOLSINCDIR) -I$(APIDIR) -I$(OUTPUT)
28LDFLAGS += $(SAN_CFLAGS)
29LDLIBS += -lelf -lz -lrt -lpthread
30
31# Silence some warnings when compiled with clang
32ifneq ($(LLVM),)
33CFLAGS += -Wno-unused-command-line-argument
34endif
35
36# Order correspond to 'make run_tests' order
37TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \
38	test_verifier_log test_dev_cgroup \
39	test_sock test_sockmap get_cgroup_id_user \
40	test_cgroup_storage \
41	test_tcpnotify_user test_sysctl \
42	test_progs-no_alu32
43
44# Also test bpf-gcc, if present
45ifneq ($(BPF_GCC),)
46TEST_GEN_PROGS += test_progs-bpf_gcc
47endif
48
49TEST_GEN_FILES = test_lwt_ip_encap.bpf.o test_tc_edt.bpf.o
50TEST_FILES = xsk_prereqs.sh $(wildcard progs/btf_dump_test_case_*.c)
51
52# Order correspond to 'make run_tests' order
53TEST_PROGS := test_kmod.sh \
54	test_xdp_redirect.sh \
55	test_xdp_redirect_multi.sh \
56	test_xdp_meta.sh \
57	test_xdp_veth.sh \
58	test_offload.py \
59	test_sock_addr.sh \
60	test_tunnel.sh \
61	test_lwt_seg6local.sh \
62	test_lirc_mode2.sh \
63	test_skb_cgroup_id.sh \
64	test_flow_dissector.sh \
65	test_xdp_vlan_mode_generic.sh \
66	test_xdp_vlan_mode_native.sh \
67	test_lwt_ip_encap.sh \
68	test_tcp_check_syncookie.sh \
69	test_tc_tunnel.sh \
70	test_tc_edt.sh \
71	test_xdping.sh \
72	test_bpftool_build.sh \
73	test_bpftool.sh \
74	test_bpftool_metadata.sh \
75	test_doc_build.sh \
76	test_xsk.sh \
77	test_xdp_features.sh
78
79TEST_PROGS_EXTENDED := with_addr.sh \
80	with_tunnels.sh ima_setup.sh verify_sig_setup.sh \
81	test_xdp_vlan.sh test_bpftool.py
82
83# Compile but not part of 'make run_tests'
84TEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \
85	flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \
86	test_lirc_mode2_user xdping test_cpp runqslower bench bpf_testmod.ko \
87	xskxceiver xdp_redirect_multi xdp_synproxy veristat xdp_hw_metadata \
88	xdp_features
89
90TEST_CUSTOM_PROGS = $(OUTPUT)/urandom_read $(OUTPUT)/sign-file
91TEST_GEN_FILES += liburandom_read.so
92
93# Emit succinct information message describing current building step
94# $1 - generic step name (e.g., CC, LINK, etc);
95# $2 - optional "flavor" specifier; if provided, will be emitted as [flavor];
96# $3 - target (assumed to be file); only file name will be emitted;
97# $4 - optional extra arg, emitted as-is, if provided.
98ifeq ($(V),1)
99Q =
100msg =
101else
102Q = @
103msg = @printf '  %-8s%s %s%s\n' "$(1)" "$(if $(2), [$(2)])" "$(notdir $(3))" "$(if $(4), $(4))";
104MAKEFLAGS += --no-print-directory
105submake_extras := feature_display=0
106endif
107
108# override lib.mk's default rules
109OVERRIDE_TARGETS := 1
110override define CLEAN
111	$(call msg,CLEAN)
112	$(Q)$(RM) -r $(TEST_GEN_PROGS)
113	$(Q)$(RM) -r $(TEST_GEN_PROGS_EXTENDED)
114	$(Q)$(RM) -r $(TEST_GEN_FILES)
115	$(Q)$(RM) -r $(EXTRA_CLEAN)
116	$(Q)$(MAKE) -C bpf_testmod clean
117	$(Q)$(MAKE) docs-clean
118endef
119
120include ../lib.mk
121
122SCRATCH_DIR := $(OUTPUT)/tools
123BUILD_DIR := $(SCRATCH_DIR)/build
124INCLUDE_DIR := $(SCRATCH_DIR)/include
125BPFOBJ := $(BUILD_DIR)/libbpf/libbpf.a
126ifneq ($(CROSS_COMPILE),)
127HOST_BUILD_DIR		:= $(BUILD_DIR)/host
128HOST_SCRATCH_DIR	:= $(OUTPUT)/host-tools
129HOST_INCLUDE_DIR	:= $(HOST_SCRATCH_DIR)/include
130else
131HOST_BUILD_DIR		:= $(BUILD_DIR)
132HOST_SCRATCH_DIR	:= $(SCRATCH_DIR)
133HOST_INCLUDE_DIR	:= $(INCLUDE_DIR)
134endif
135HOST_BPFOBJ := $(HOST_BUILD_DIR)/libbpf/libbpf.a
136RESOLVE_BTFIDS := $(HOST_BUILD_DIR)/resolve_btfids/resolve_btfids
137RUNQSLOWER_OUTPUT := $(BUILD_DIR)/runqslower/
138
139VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux)				\
140		     $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux)	\
141		     ../../../../vmlinux				\
142		     /sys/kernel/btf/vmlinux				\
143		     /boot/vmlinux-$(shell uname -r)
144VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
145ifeq ($(VMLINUX_BTF),)
146$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)")
147endif
148
149# Define simple and short `make test_progs`, `make test_sysctl`, etc targets
150# to build individual tests.
151# NOTE: Semicolon at the end is critical to override lib.mk's default static
152# rule for binaries.
153$(notdir $(TEST_GEN_PROGS)						\
154	 $(TEST_PROGS)							\
155	 $(TEST_PROGS_EXTENDED)						\
156	 $(TEST_GEN_PROGS_EXTENDED)					\
157	 $(TEST_CUSTOM_PROGS)): %: $(OUTPUT)/% ;
158
159# sort removes libbpf duplicates when not cross-building
160MAKE_DIRS := $(sort $(BUILD_DIR)/libbpf $(HOST_BUILD_DIR)/libbpf	       \
161	       $(HOST_BUILD_DIR)/bpftool $(HOST_BUILD_DIR)/resolve_btfids      \
162	       $(RUNQSLOWER_OUTPUT) $(INCLUDE_DIR))
163$(MAKE_DIRS):
164	$(call msg,MKDIR,,$@)
165	$(Q)mkdir -p $@
166
167$(OUTPUT)/%.o: %.c
168	$(call msg,CC,,$@)
169	$(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
170
171$(OUTPUT)/%:%.c
172	$(call msg,BINARY,,$@)
173	$(Q)$(LINK.c) $^ $(LDLIBS) -o $@
174
175# LLVM's ld.lld doesn't support all the architectures, so use it only on x86
176ifeq ($(SRCARCH),x86)
177LLD := lld
178else
179LLD := ld
180endif
181
182# Filter out -static for liburandom_read.so and its dependent targets so that static builds
183# do not fail. Static builds leave urandom_read relying on system-wide shared libraries.
184$(OUTPUT)/liburandom_read.so: urandom_read_lib1.c urandom_read_lib2.c
185	$(call msg,LIB,,$@)
186	$(Q)$(CLANG) $(filter-out -static,$(CFLAGS) $(LDFLAGS))   \
187		     $^ $(filter-out -static,$(LDLIBS))	     \
188		     -fuse-ld=$(LLD) -Wl,-znoseparate-code -Wl,--build-id=sha1 \
189		     -fPIC -shared -o $@
190
191$(OUTPUT)/urandom_read: urandom_read.c urandom_read_aux.c $(OUTPUT)/liburandom_read.so
192	$(call msg,BINARY,,$@)
193	$(Q)$(CLANG) $(filter-out -static,$(CFLAGS) $(LDFLAGS)) $(filter %.c,$^) \
194		     -lurandom_read $(filter-out -static,$(LDLIBS)) -L$(OUTPUT)  \
195		     -fuse-ld=$(LLD) -Wl,-znoseparate-code -Wl,--build-id=sha1 \
196		     -Wl,-rpath=. -o $@
197
198$(OUTPUT)/sign-file: ../../../../scripts/sign-file.c
199	$(call msg,SIGN-FILE,,$@)
200	$(Q)$(CC) $(shell $(HOSTPKG_CONFIG)--cflags libcrypto 2> /dev/null) \
201		  $< -o $@ \
202		  $(shell $(HOSTPKG_CONFIG) --libs libcrypto 2> /dev/null || echo -lcrypto)
203
204$(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(wildcard bpf_testmod/Makefile bpf_testmod/*.[ch])
205	$(call msg,MOD,,$@)
206	$(Q)$(RM) bpf_testmod/bpf_testmod.ko # force re-compilation
207	$(Q)$(MAKE) $(submake_extras) RESOLVE_BTFIDS=$(RESOLVE_BTFIDS) -C bpf_testmod
208	$(Q)cp bpf_testmod/bpf_testmod.ko $@
209
210DEFAULT_BPFTOOL := $(HOST_SCRATCH_DIR)/sbin/bpftool
211
212$(OUTPUT)/runqslower: $(BPFOBJ) | $(DEFAULT_BPFTOOL) $(RUNQSLOWER_OUTPUT)
213	$(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/runqslower	       \
214		    OUTPUT=$(RUNQSLOWER_OUTPUT) VMLINUX_BTF=$(VMLINUX_BTF)     \
215		    BPFTOOL_OUTPUT=$(HOST_BUILD_DIR)/bpftool/		       \
216		    BPFOBJ_OUTPUT=$(BUILD_DIR)/libbpf			       \
217		    BPFOBJ=$(BPFOBJ) BPF_INCLUDE=$(INCLUDE_DIR) &&	       \
218		    cp $(RUNQSLOWER_OUTPUT)runqslower $@
219
220TEST_GEN_PROGS_EXTENDED += $(DEFAULT_BPFTOOL)
221
222$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(BPFOBJ)
223
224CGROUP_HELPERS	:= $(OUTPUT)/cgroup_helpers.o
225TESTING_HELPERS	:= $(OUTPUT)/testing_helpers.o
226TRACE_HELPERS	:= $(OUTPUT)/trace_helpers.o
227CAP_HELPERS	:= $(OUTPUT)/cap_helpers.o
228
229$(OUTPUT)/test_dev_cgroup: $(CGROUP_HELPERS) $(TESTING_HELPERS)
230$(OUTPUT)/test_skb_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS)
231$(OUTPUT)/test_sock: $(CGROUP_HELPERS) $(TESTING_HELPERS)
232$(OUTPUT)/test_sock_addr: $(CGROUP_HELPERS) $(TESTING_HELPERS)
233$(OUTPUT)/test_sockmap: $(CGROUP_HELPERS) $(TESTING_HELPERS)
234$(OUTPUT)/test_tcpnotify_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(TRACE_HELPERS)
235$(OUTPUT)/get_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS)
236$(OUTPUT)/test_cgroup_storage: $(CGROUP_HELPERS) $(TESTING_HELPERS)
237$(OUTPUT)/test_sock_fields: $(CGROUP_HELPERS) $(TESTING_HELPERS)
238$(OUTPUT)/test_sysctl: $(CGROUP_HELPERS) $(TESTING_HELPERS)
239$(OUTPUT)/test_tag: $(TESTING_HELPERS)
240$(OUTPUT)/test_lirc_mode2_user: $(TESTING_HELPERS)
241$(OUTPUT)/xdping: $(TESTING_HELPERS)
242$(OUTPUT)/flow_dissector_load: $(TESTING_HELPERS)
243$(OUTPUT)/test_maps: $(TESTING_HELPERS)
244$(OUTPUT)/test_verifier: $(TESTING_HELPERS) $(CAP_HELPERS)
245$(OUTPUT)/xsk.o: $(BPFOBJ)
246
247BPFTOOL ?= $(DEFAULT_BPFTOOL)
248$(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile)    \
249		    $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/bpftool
250	$(Q)$(MAKE) $(submake_extras)  -C $(BPFTOOLDIR)			       \
251		    ARCH= CROSS_COMPILE= CC=$(HOSTCC) LD=$(HOSTLD) 	       \
252		    EXTRA_CFLAGS='-g -O0'				       \
253		    OUTPUT=$(HOST_BUILD_DIR)/bpftool/			       \
254		    LIBBPF_OUTPUT=$(HOST_BUILD_DIR)/libbpf/		       \
255		    LIBBPF_DESTDIR=$(HOST_SCRATCH_DIR)/			       \
256		    prefix= DESTDIR=$(HOST_SCRATCH_DIR)/ install-bin
257
258all: docs
259
260docs:
261	$(Q)RST2MAN_OPTS="--exit-status=1" $(MAKE) $(submake_extras)	\
262	            -f Makefile.docs					\
263	            prefix= OUTPUT=$(OUTPUT)/ DESTDIR=$(OUTPUT)/ $@
264
265docs-clean:
266	$(Q)$(MAKE) $(submake_extras)					\
267	            -f Makefile.docs					\
268	            prefix= OUTPUT=$(OUTPUT)/ DESTDIR=$(OUTPUT)/ $@
269
270$(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile)		       \
271	   $(APIDIR)/linux/bpf.h					       \
272	   | $(BUILD_DIR)/libbpf
273	$(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(BUILD_DIR)/libbpf/ \
274		    EXTRA_CFLAGS='-g -O0'				       \
275		    DESTDIR=$(SCRATCH_DIR) prefix= all install_headers
276
277ifneq ($(BPFOBJ),$(HOST_BPFOBJ))
278$(HOST_BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile)		       \
279		$(APIDIR)/linux/bpf.h					       \
280		| $(HOST_BUILD_DIR)/libbpf
281	$(Q)$(MAKE) $(submake_extras) -C $(BPFDIR)                             \
282		    EXTRA_CFLAGS='-g -O0' ARCH= CROSS_COMPILE=		       \
283		    OUTPUT=$(HOST_BUILD_DIR)/libbpf/ CC=$(HOSTCC) LD=$(HOSTLD) \
284		    DESTDIR=$(HOST_SCRATCH_DIR)/ prefix= all install_headers
285endif
286
287$(INCLUDE_DIR)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL) | $(INCLUDE_DIR)
288ifeq ($(VMLINUX_H),)
289	$(call msg,GEN,,$@)
290	$(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
291else
292	$(call msg,CP,,$@)
293	$(Q)cp "$(VMLINUX_H)" $@
294endif
295
296$(RESOLVE_BTFIDS): $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/resolve_btfids	\
297		       $(TOOLSDIR)/bpf/resolve_btfids/main.c	\
298		       $(TOOLSDIR)/lib/rbtree.c			\
299		       $(TOOLSDIR)/lib/zalloc.c			\
300		       $(TOOLSDIR)/lib/string.c			\
301		       $(TOOLSDIR)/lib/ctype.c			\
302		       $(TOOLSDIR)/lib/str_error_r.c
303	$(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/resolve_btfids	\
304		CC=$(HOSTCC) LD=$(HOSTLD) AR=$(HOSTAR) \
305		LIBBPF_INCLUDE=$(HOST_INCLUDE_DIR) \
306		OUTPUT=$(HOST_BUILD_DIR)/resolve_btfids/ BPFOBJ=$(HOST_BPFOBJ)
307
308# Get Clang's default includes on this system, as opposed to those seen by
309# '-target bpf'. This fixes "missing" files on some architectures/distros,
310# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
311#
312# Use '-idirafter': Don't interfere with include mechanics except where the
313# build would have failed anyways.
314define get_sys_includes
315$(shell $(1) $(2) -v -E - </dev/null 2>&1 \
316	| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
317$(shell $(1) $(2) -dM -E - </dev/null | grep '__riscv_xlen ' | awk '{printf("-D__riscv_xlen=%d -D__BITS_PER_LONG=%d", $$3, $$3)}')
318endef
319
320# Determine target endianness.
321IS_LITTLE_ENDIAN = $(shell $(CC) -dM -E - </dev/null | \
322			grep 'define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__')
323MENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian)
324
325ifneq ($(CROSS_COMPILE),)
326CLANG_TARGET_ARCH = --target=$(notdir $(CROSS_COMPILE:%-=%))
327endif
328
329CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG),$(CLANG_TARGET_ARCH))
330BPF_CFLAGS = -g -Werror -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN) 		\
331	     -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR)			\
332	     -I$(abspath $(OUTPUT)/../usr/include)
333
334CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \
335	       -Wno-compare-distinct-pointer-types
336
337$(OUTPUT)/test_l4lb_noinline.o: BPF_CFLAGS += -fno-inline
338$(OUTPUT)/test_xdp_noinline.o: BPF_CFLAGS += -fno-inline
339
340$(OUTPUT)/flow_dissector_load.o: flow_dissector_load.h
341$(OUTPUT)/cgroup_getset_retval_hooks.o: cgroup_getset_retval_hooks.h
342
343# Build BPF object using Clang
344# $1 - input .c file
345# $2 - output .o file
346# $3 - CFLAGS
347define CLANG_BPF_BUILD_RULE
348	$(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2)
349	$(Q)$(CLANG) $3 -O2 -target bpf -c $1 -mcpu=v3 -o $2
350endef
351# Similar to CLANG_BPF_BUILD_RULE, but with disabled alu32
352define CLANG_NOALU32_BPF_BUILD_RULE
353	$(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2)
354	$(Q)$(CLANG) $3 -O2 -target bpf -c $1 -mcpu=v2 -o $2
355endef
356# Build BPF object using GCC
357define GCC_BPF_BUILD_RULE
358	$(call msg,GCC-BPF,$(TRUNNER_BINARY),$2)
359	$(Q)$(BPF_GCC) $3 -O2 -c $1 -o $2
360endef
361
362SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c
363
364LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h		\
365		linked_vars.skel.h linked_maps.skel.h 			\
366		test_subskeleton.skel.h test_subskeleton_lib.skel.h	\
367		test_usdt.skel.h
368
369LSKELS := fentry_test.c fexit_test.c fexit_sleep.c atomics.c 		\
370	trace_printk.c trace_vprintk.c map_ptr_kern.c 			\
371	core_kern.c core_kern_overflow.c test_ringbuf.c			\
372	test_ringbuf_map_key.c
373
374# Generate both light skeleton and libbpf skeleton for these
375LSKELS_EXTRA := test_ksyms_module.c test_ksyms_weak.c kfunc_call_test.c \
376	kfunc_call_test_subprog.c
377SKEL_BLACKLIST += $$(LSKELS)
378
379test_static_linked.skel.h-deps := test_static_linked1.bpf.o test_static_linked2.bpf.o
380linked_funcs.skel.h-deps := linked_funcs1.bpf.o linked_funcs2.bpf.o
381linked_vars.skel.h-deps := linked_vars1.bpf.o linked_vars2.bpf.o
382linked_maps.skel.h-deps := linked_maps1.bpf.o linked_maps2.bpf.o
383# In the subskeleton case, we want the test_subskeleton_lib.subskel.h file
384# but that's created as a side-effect of the skel.h generation.
385test_subskeleton.skel.h-deps := test_subskeleton_lib2.bpf.o test_subskeleton_lib.bpf.o test_subskeleton.bpf.o
386test_subskeleton_lib.skel.h-deps := test_subskeleton_lib2.bpf.o test_subskeleton_lib.bpf.o
387test_usdt.skel.h-deps := test_usdt.bpf.o test_usdt_multispec.bpf.o
388xsk_xdp_progs.skel.h-deps := xsk_xdp_progs.bpf.o
389xdp_hw_metadata.skel.h-deps := xdp_hw_metadata.bpf.o
390xdp_features.skel.h-deps := xdp_features.bpf.o
391
392LINKED_BPF_SRCS := $(patsubst %.bpf.o,%.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
393
394# Set up extra TRUNNER_XXX "temporary" variables in the environment (relies on
395# $eval()) and pass control to DEFINE_TEST_RUNNER_RULES.
396# Parameters:
397# $1 - test runner base binary name (e.g., test_progs)
398# $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc)
399define DEFINE_TEST_RUNNER
400
401TRUNNER_OUTPUT := $(OUTPUT)$(if $2,/)$2
402TRUNNER_BINARY := $1$(if $2,-)$2
403TRUNNER_TEST_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.test.o,	\
404				 $$(notdir $$(wildcard $(TRUNNER_TESTS_DIR)/*.c)))
405TRUNNER_EXTRA_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o,		\
406				 $$(filter %.c,$(TRUNNER_EXTRA_SOURCES)))
407TRUNNER_EXTRA_HDRS := $$(filter %.h,$(TRUNNER_EXTRA_SOURCES))
408TRUNNER_TESTS_HDR := $(TRUNNER_TESTS_DIR)/tests.h
409TRUNNER_BPF_SRCS := $$(notdir $$(wildcard $(TRUNNER_BPF_PROGS_DIR)/*.c))
410TRUNNER_BPF_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.bpf.o, $$(TRUNNER_BPF_SRCS))
411TRUNNER_BPF_SKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.skel.h,	\
412				 $$(filter-out $(SKEL_BLACKLIST) $(LINKED_BPF_SRCS),\
413					       $$(TRUNNER_BPF_SRCS)))
414TRUNNER_BPF_LSKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.lskel.h, $$(LSKELS) $$(LSKELS_EXTRA))
415TRUNNER_BPF_SKELS_LINKED := $$(addprefix $$(TRUNNER_OUTPUT)/,$(LINKED_SKELS))
416TEST_GEN_FILES += $$(TRUNNER_BPF_OBJS)
417
418# Evaluate rules now with extra TRUNNER_XXX variables above already defined
419$$(eval $$(call DEFINE_TEST_RUNNER_RULES,$1,$2))
420
421endef
422
423# Using TRUNNER_XXX variables, provided by callers of DEFINE_TEST_RUNNER and
424# set up by DEFINE_TEST_RUNNER itself, create test runner build rules with:
425# $1 - test runner base binary name (e.g., test_progs)
426# $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc)
427define DEFINE_TEST_RUNNER_RULES
428
429ifeq ($($(TRUNNER_OUTPUT)-dir),)
430$(TRUNNER_OUTPUT)-dir := y
431$(TRUNNER_OUTPUT):
432	$$(call msg,MKDIR,,$$@)
433	$(Q)mkdir -p $$@
434endif
435
436# ensure we set up BPF objects generation rule just once for a given
437# input/output directory combination
438ifeq ($($(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs),)
439$(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs := y
440$(TRUNNER_BPF_OBJS): $(TRUNNER_OUTPUT)/%.bpf.o:				\
441		     $(TRUNNER_BPF_PROGS_DIR)/%.c			\
442		     $(TRUNNER_BPF_PROGS_DIR)/*.h			\
443		     $$(INCLUDE_DIR)/vmlinux.h				\
444		     $(wildcard $(BPFDIR)/bpf_*.h)			\
445		     $(wildcard $(BPFDIR)/*.bpf.h)			\
446		     | $(TRUNNER_OUTPUT) $$(BPFOBJ)
447	$$(call $(TRUNNER_BPF_BUILD_RULE),$$<,$$@,			\
448					  $(TRUNNER_BPF_CFLAGS))
449
450$(TRUNNER_BPF_SKELS): %.skel.h: %.bpf.o $(BPFTOOL) | $(TRUNNER_OUTPUT)
451	$$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
452	$(Q)$$(BPFTOOL) gen object $$(<:.o=.linked1.o) $$<
453	$(Q)$$(BPFTOOL) gen object $$(<:.o=.linked2.o) $$(<:.o=.linked1.o)
454	$(Q)$$(BPFTOOL) gen object $$(<:.o=.linked3.o) $$(<:.o=.linked2.o)
455	$(Q)diff $$(<:.o=.linked2.o) $$(<:.o=.linked3.o)
456	$(Q)$$(BPFTOOL) gen skeleton $$(<:.o=.linked3.o) name $$(notdir $$(<:.bpf.o=)) > $$@
457	$(Q)$$(BPFTOOL) gen subskeleton $$(<:.o=.linked3.o) name $$(notdir $$(<:.bpf.o=)) > $$(@:.skel.h=.subskel.h)
458
459$(TRUNNER_BPF_LSKELS): %.lskel.h: %.bpf.o $(BPFTOOL) | $(TRUNNER_OUTPUT)
460	$$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
461	$(Q)$$(BPFTOOL) gen object $$(<:.o=.llinked1.o) $$<
462	$(Q)$$(BPFTOOL) gen object $$(<:.o=.llinked2.o) $$(<:.o=.llinked1.o)
463	$(Q)$$(BPFTOOL) gen object $$(<:.o=.llinked3.o) $$(<:.o=.llinked2.o)
464	$(Q)diff $$(<:.o=.llinked2.o) $$(<:.o=.llinked3.o)
465	$(Q)$$(BPFTOOL) gen skeleton -L $$(<:.o=.llinked3.o) name $$(notdir $$(<:.bpf.o=_lskel)) > $$@
466
467$(TRUNNER_BPF_SKELS_LINKED): $(TRUNNER_BPF_OBJS) $(BPFTOOL) | $(TRUNNER_OUTPUT)
468	$$(call msg,LINK-BPF,$(TRUNNER_BINARY),$$(@:.skel.h=.bpf.o))
469	$(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked1.o) $$(addprefix $(TRUNNER_OUTPUT)/,$$($$(@F)-deps))
470	$(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked1.o)
471	$(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked3.o) $$(@:.skel.h=.linked2.o)
472	$(Q)diff $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked3.o)
473	$$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
474	$(Q)$$(BPFTOOL) gen skeleton $$(@:.skel.h=.linked3.o) name $$(notdir $$(@:.skel.h=)) > $$@
475	$(Q)$$(BPFTOOL) gen subskeleton $$(@:.skel.h=.linked3.o) name $$(notdir $$(@:.skel.h=)) > $$(@:.skel.h=.subskel.h)
476endif
477
478# ensure we set up tests.h header generation rule just once
479ifeq ($($(TRUNNER_TESTS_DIR)-tests-hdr),)
480$(TRUNNER_TESTS_DIR)-tests-hdr := y
481$(TRUNNER_TESTS_HDR): $(TRUNNER_TESTS_DIR)/*.c
482	$$(call msg,TEST-HDR,$(TRUNNER_BINARY),$$@)
483	$$(shell (echo '/* Generated header, do not edit */';					\
484		  sed -n -E 's/^void (serial_)?test_([a-zA-Z0-9_]+)\((void)?\).*/DEFINE_TEST(\2)/p'	\
485			$(TRUNNER_TESTS_DIR)/*.c | sort ;	\
486		 ) > $$@)
487endif
488
489# compile individual test files
490# Note: we cd into output directory to ensure embedded BPF object is found
491$(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o:			\
492		      $(TRUNNER_TESTS_DIR)/%.c				\
493		      $(TRUNNER_EXTRA_HDRS)				\
494		      $(TRUNNER_BPF_OBJS)				\
495		      $(TRUNNER_BPF_SKELS)				\
496		      $(TRUNNER_BPF_LSKELS)				\
497		      $(TRUNNER_BPF_SKELS_LINKED)			\
498		      $$(BPFOBJ) | $(TRUNNER_OUTPUT)
499	$$(call msg,TEST-OBJ,$(TRUNNER_BINARY),$$@)
500	$(Q)cd $$(@D) && $$(CC) -I. $$(CFLAGS) -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F)
501
502$(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o:				\
503		       %.c						\
504		       $(TRUNNER_EXTRA_HDRS)				\
505		       $(TRUNNER_TESTS_HDR)				\
506		       $$(BPFOBJ) | $(TRUNNER_OUTPUT)
507	$$(call msg,EXT-OBJ,$(TRUNNER_BINARY),$$@)
508	$(Q)$$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@
509
510# non-flavored in-srctree builds receive special treatment, in particular, we
511# do not need to copy extra resources (see e.g. test_btf_dump_case())
512$(TRUNNER_BINARY)-extras: $(TRUNNER_EXTRA_FILES) | $(TRUNNER_OUTPUT)
513ifneq ($2:$(OUTPUT),:$(shell pwd))
514	$$(call msg,EXT-COPY,$(TRUNNER_BINARY),$(TRUNNER_EXTRA_FILES))
515	$(Q)rsync -aq $$^ $(TRUNNER_OUTPUT)/
516endif
517
518$(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS)			\
519			     $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ)		\
520			     $(RESOLVE_BTFIDS)				\
521			     | $(TRUNNER_BINARY)-extras
522	$$(call msg,BINARY,,$$@)
523	$(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
524	$(Q)$(RESOLVE_BTFIDS) --btf $(TRUNNER_OUTPUT)/btf_data.bpf.o $$@
525	$(Q)ln -sf $(if $2,..,.)/tools/build/bpftool/bootstrap/bpftool \
526		   $(OUTPUT)/$(if $2,$2/)bpftool
527
528endef
529
530# Define test_progs test runner.
531TRUNNER_TESTS_DIR := prog_tests
532TRUNNER_BPF_PROGS_DIR := progs
533TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c	\
534			 network_helpers.c testing_helpers.c		\
535			 btf_helpers.c flow_dissector_load.h		\
536			 cap_helpers.c test_loader.c xsk.c
537TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko	\
538		       $(OUTPUT)/liburandom_read.so			\
539		       $(OUTPUT)/xdp_synproxy				\
540		       $(OUTPUT)/sign-file				\
541		       ima_setup.sh 					\
542		       verify_sig_setup.sh				\
543		       $(wildcard progs/btf_dump_test_case_*.c)		\
544		       $(wildcard progs/*.bpf.o)
545TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE
546TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS) -DENABLE_ATOMICS_TESTS
547$(eval $(call DEFINE_TEST_RUNNER,test_progs))
548
549# Define test_progs-no_alu32 test runner.
550TRUNNER_BPF_BUILD_RULE := CLANG_NOALU32_BPF_BUILD_RULE
551TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS)
552$(eval $(call DEFINE_TEST_RUNNER,test_progs,no_alu32))
553
554# Define test_progs BPF-GCC-flavored test runner.
555ifneq ($(BPF_GCC),)
556TRUNNER_BPF_BUILD_RULE := GCC_BPF_BUILD_RULE
557TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(call get_sys_includes,gcc,)
558$(eval $(call DEFINE_TEST_RUNNER,test_progs,bpf_gcc))
559endif
560
561# Define test_maps test runner.
562TRUNNER_TESTS_DIR := map_tests
563TRUNNER_BPF_PROGS_DIR := progs
564TRUNNER_EXTRA_SOURCES := test_maps.c
565TRUNNER_EXTRA_FILES :=
566TRUNNER_BPF_BUILD_RULE := $$(error no BPF objects should be built)
567TRUNNER_BPF_CFLAGS :=
568$(eval $(call DEFINE_TEST_RUNNER,test_maps))
569
570# Define test_verifier test runner.
571# It is much simpler than test_maps/test_progs and sufficiently different from
572# them (e.g., test.h is using completely pattern), that it's worth just
573# explicitly defining all the rules explicitly.
574verifier/tests.h: verifier/*.c
575	$(shell ( cd verifier/; \
576		  echo '/* Generated header, do not edit */'; \
577		  echo '#ifdef FILL_ARRAY'; \
578		  ls *.c 2> /dev/null | sed -e 's@\(.*\)@#include \"\1\"@'; \
579		  echo '#endif' \
580		) > verifier/tests.h)
581$(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT)
582	$(call msg,BINARY,,$@)
583	$(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
584
585$(OUTPUT)/xskxceiver: xskxceiver.c $(OUTPUT)/xsk.o $(OUTPUT)/xsk_xdp_progs.skel.h $(BPFOBJ) | $(OUTPUT)
586	$(call msg,BINARY,,$@)
587	$(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
588
589$(OUTPUT)/xdp_hw_metadata: xdp_hw_metadata.c $(OUTPUT)/network_helpers.o $(OUTPUT)/xsk.o $(OUTPUT)/xdp_hw_metadata.skel.h | $(OUTPUT)
590	$(call msg,BINARY,,$@)
591	$(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
592
593$(OUTPUT)/xdp_features: xdp_features.c $(OUTPUT)/network_helpers.o $(OUTPUT)/xdp_features.skel.h | $(OUTPUT)
594	$(call msg,BINARY,,$@)
595	$(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
596
597# Make sure we are able to include and link libbpf against c++.
598$(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ)
599	$(call msg,CXX,,$@)
600	$(Q)$(CXX) $(CFLAGS) $(filter %.a %.o %.cpp,$^) $(LDLIBS) -o $@
601
602# Benchmark runner
603$(OUTPUT)/bench_%.o: benchs/bench_%.c bench.h $(BPFOBJ)
604	$(call msg,CC,,$@)
605	$(Q)$(CC) $(CFLAGS) -O2 -c $(filter %.c,$^) $(LDLIBS) -o $@
606$(OUTPUT)/bench_rename.o: $(OUTPUT)/test_overhead.skel.h
607$(OUTPUT)/bench_trigger.o: $(OUTPUT)/trigger_bench.skel.h
608$(OUTPUT)/bench_ringbufs.o: $(OUTPUT)/ringbuf_bench.skel.h \
609			    $(OUTPUT)/perfbuf_bench.skel.h
610$(OUTPUT)/bench_bloom_filter_map.o: $(OUTPUT)/bloom_filter_bench.skel.h
611$(OUTPUT)/bench_bpf_loop.o: $(OUTPUT)/bpf_loop_bench.skel.h
612$(OUTPUT)/bench_strncmp.o: $(OUTPUT)/strncmp_bench.skel.h
613$(OUTPUT)/bench_bpf_hashmap_full_update.o: $(OUTPUT)/bpf_hashmap_full_update_bench.skel.h
614$(OUTPUT)/bench_local_storage.o: $(OUTPUT)/local_storage_bench.skel.h
615$(OUTPUT)/bench_local_storage_rcu_tasks_trace.o: $(OUTPUT)/local_storage_rcu_tasks_trace_bench.skel.h
616$(OUTPUT)/bench.o: bench.h testing_helpers.h $(BPFOBJ)
617$(OUTPUT)/bench: LDLIBS += -lm
618$(OUTPUT)/bench: $(OUTPUT)/bench.o \
619		 $(TESTING_HELPERS) \
620		 $(TRACE_HELPERS) \
621		 $(OUTPUT)/bench_count.o \
622		 $(OUTPUT)/bench_rename.o \
623		 $(OUTPUT)/bench_trigger.o \
624		 $(OUTPUT)/bench_ringbufs.o \
625		 $(OUTPUT)/bench_bloom_filter_map.o \
626		 $(OUTPUT)/bench_bpf_loop.o \
627		 $(OUTPUT)/bench_strncmp.o \
628		 $(OUTPUT)/bench_bpf_hashmap_full_update.o \
629		 $(OUTPUT)/bench_local_storage.o \
630		 $(OUTPUT)/bench_local_storage_rcu_tasks_trace.o
631	$(call msg,BINARY,,$@)
632	$(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(filter %.a %.o,$^) $(LDLIBS) -o $@
633
634$(OUTPUT)/veristat.o: $(BPFOBJ)
635$(OUTPUT)/veristat: $(OUTPUT)/veristat.o
636	$(call msg,BINARY,,$@)
637	$(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(filter %.a %.o,$^) $(LDLIBS) -o $@
638
639EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(SCRATCH_DIR) $(HOST_SCRATCH_DIR)	\
640	prog_tests/tests.h map_tests/tests.h verifier/tests.h		\
641	feature bpftool							\
642	$(addprefix $(OUTPUT)/,*.o *.skel.h *.lskel.h *.subskel.h	\
643			       no_alu32 bpf_gcc bpf_testmod.ko		\
644			       liburandom_read.so)
645
646.PHONY: docs docs-clean
647
648# Delete partially updated (corrupted) files on error
649.DELETE_ON_ERROR:
650