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