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