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