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