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