1# SPDX-License-Identifier: GPL-2.0
2include ../../../../scripts/Kbuild.include
3include ../../../scripts/Makefile.arch
4
5CURDIR := $(abspath .)
6TOOLSDIR := $(abspath ../../..)
7LIBDIR := $(TOOLSDIR)/lib
8BPFDIR := $(LIBDIR)/bpf
9TOOLSINCDIR := $(TOOLSDIR)/include
10BPFTOOLDIR := $(TOOLSDIR)/bpf/bpftool
11APIDIR := $(TOOLSINCDIR)/uapi
12GENDIR := $(abspath ../../../../include/generated)
13GENHDR := $(GENDIR)/autoconf.h
14
15ifneq ($(wildcard $(GENHDR)),)
16  GENFLAGS := -DHAVE_GENHDR
17endif
18
19CLANG		?= clang
20LLC		?= llc
21LLVM_OBJCOPY	?= llvm-objcopy
22BPF_GCC		?= $(shell command -v bpf-gcc;)
23CFLAGS += -g -rdynamic -Wall -O2 $(GENFLAGS) -I$(CURDIR)		\
24	  -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR) -I$(TOOLSINCDIR)	\
25	  -I$(APIDIR)							\
26	  -Dbpf_prog_load=bpf_prog_test_load				\
27	  -Dbpf_load_program=bpf_test_load_program
28LDLIBS += -lcap -lelf -lz -lrt -lpthread
29
30# Order correspond to 'make run_tests' order
31TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \
32	test_align test_verifier_log test_dev_cgroup test_tcpbpf_user \
33	test_sock test_btf test_sockmap get_cgroup_id_user test_socket_cookie \
34	test_cgroup_storage \
35	test_netcnt test_tcpnotify_user test_sock_fields test_sysctl test_hashmap \
36	test_progs-no_alu32 \
37	test_current_pid_tgid_new_ns
38
39# Also test bpf-gcc, if present
40ifneq ($(BPF_GCC),)
41TEST_GEN_PROGS += test_progs-bpf_gcc
42endif
43
44TEST_GEN_FILES =
45TEST_FILES = test_lwt_ip_encap.o \
46	test_tc_edt.o
47
48# Order correspond to 'make run_tests' order
49TEST_PROGS := test_kmod.sh \
50	test_xdp_redirect.sh \
51	test_xdp_meta.sh \
52	test_xdp_veth.sh \
53	test_offload.py \
54	test_sock_addr.sh \
55	test_tunnel.sh \
56	test_lwt_seg6local.sh \
57	test_lirc_mode2.sh \
58	test_skb_cgroup_id.sh \
59	test_flow_dissector.sh \
60	test_xdp_vlan_mode_generic.sh \
61	test_xdp_vlan_mode_native.sh \
62	test_lwt_ip_encap.sh \
63	test_tcp_check_syncookie.sh \
64	test_tc_tunnel.sh \
65	test_tc_edt.sh \
66	test_xdping.sh \
67	test_bpftool_build.sh \
68	test_bpftool.sh
69
70TEST_PROGS_EXTENDED := with_addr.sh \
71	with_tunnels.sh \
72	tcp_client.py \
73	tcp_server.py \
74	test_xdp_vlan.sh
75
76# Compile but not part of 'make run_tests'
77TEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \
78	flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \
79	test_lirc_mode2_user xdping test_cpp runqslower
80
81TEST_CUSTOM_PROGS = urandom_read
82
83# Emit succinct information message describing current building step
84# $1 - generic step name (e.g., CC, LINK, etc);
85# $2 - optional "flavor" specifier; if provided, will be emitted as [flavor];
86# $3 - target (assumed to be file); only file name will be emitted;
87# $4 - optional extra arg, emitted as-is, if provided.
88ifeq ($(V),1)
89Q =
90msg =
91else
92Q = @
93msg = @printf '  %-8s%s %s%s\n' "$(1)" "$(if $(2), [$(2)])" "$(notdir $(3))" "$(if $(4), $(4))";
94MAKEFLAGS += --no-print-directory
95submake_extras := feature_display=0
96endif
97
98# override lib.mk's default rules
99OVERRIDE_TARGETS := 1
100override define CLEAN
101	$(call msg,CLEAN)
102	$(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
103endef
104
105include ../lib.mk
106
107SCRATCH_DIR := $(OUTPUT)/tools
108BUILD_DIR := $(SCRATCH_DIR)/build
109INCLUDE_DIR := $(SCRATCH_DIR)/include
110BPFOBJ := $(BUILD_DIR)/libbpf/libbpf.a
111
112# Define simple and short `make test_progs`, `make test_sysctl`, etc targets
113# to build individual tests.
114# NOTE: Semicolon at the end is critical to override lib.mk's default static
115# rule for binaries.
116$(notdir $(TEST_GEN_PROGS)						\
117	 $(TEST_PROGS)							\
118	 $(TEST_PROGS_EXTENDED)						\
119	 $(TEST_GEN_PROGS_EXTENDED)					\
120	 $(TEST_CUSTOM_PROGS)): %: $(OUTPUT)/% ;
121
122$(OUTPUT)/%:%.c
123	$(call msg,BINARY,,$@)
124	$(LINK.c) $^ $(LDLIBS) -o $@
125
126$(OUTPUT)/urandom_read: urandom_read.c
127	$(call msg,BINARY,,$@)
128	$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS) -Wl,--build-id
129
130$(OUTPUT)/test_stub.o: test_stub.c $(BPFOBJ)
131	$(call msg,CC,,$@)
132	$(CC) -c $(CFLAGS) -o $@ $<
133
134VMLINUX_BTF_PATHS := $(if $(O),$(O)/vmlinux)				\
135		     $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux)	\
136		     ../../../../vmlinux				\
137		     /sys/kernel/btf/vmlinux				\
138		     /boot/vmlinux-$(shell uname -r)
139VMLINUX_BTF := $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
140
141$(OUTPUT)/runqslower: $(BPFOBJ)
142	$(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/runqslower	\
143		    OUTPUT=$(SCRATCH_DIR)/ VMLINUX_BTF=$(VMLINUX_BTF)   \
144		    BPFOBJ=$(BPFOBJ) BPF_INCLUDE=$(INCLUDE_DIR)
145
146$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/test_stub.o $(BPFOBJ)
147
148$(OUTPUT)/test_dev_cgroup: cgroup_helpers.c
149$(OUTPUT)/test_skb_cgroup_id_user: cgroup_helpers.c
150$(OUTPUT)/test_sock: cgroup_helpers.c
151$(OUTPUT)/test_sock_addr: cgroup_helpers.c
152$(OUTPUT)/test_socket_cookie: cgroup_helpers.c
153$(OUTPUT)/test_sockmap: cgroup_helpers.c
154$(OUTPUT)/test_tcpbpf_user: cgroup_helpers.c
155$(OUTPUT)/test_tcpnotify_user: cgroup_helpers.c trace_helpers.c
156$(OUTPUT)/get_cgroup_id_user: cgroup_helpers.c
157$(OUTPUT)/test_cgroup_storage: cgroup_helpers.c
158$(OUTPUT)/test_netcnt: cgroup_helpers.c
159$(OUTPUT)/test_sock_fields: cgroup_helpers.c
160$(OUTPUT)/test_sysctl: cgroup_helpers.c
161
162DEFAULT_BPFTOOL := $(SCRATCH_DIR)/sbin/bpftool
163BPFTOOL ?= $(DEFAULT_BPFTOOL)
164$(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile)    \
165		    $(BPFOBJ) | $(BUILD_DIR)/bpftool
166	$(Q)$(MAKE) $(submake_extras)  -C $(BPFTOOLDIR)			       \
167		    OUTPUT=$(BUILD_DIR)/bpftool/			       \
168		    prefix= DESTDIR=$(SCRATCH_DIR)/ install
169
170$(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile)		       \
171	   ../../../include/uapi/linux/bpf.h                                   \
172	   | $(INCLUDE_DIR) $(BUILD_DIR)/libbpf
173	$(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(BUILD_DIR)/libbpf/ \
174		    DESTDIR=$(SCRATCH_DIR) prefix= all install_headers
175
176$(BUILD_DIR)/libbpf $(BUILD_DIR)/bpftool $(INCLUDE_DIR):
177	$(call msg,MKDIR,,$@)
178	mkdir -p $@
179
180$(INCLUDE_DIR)/vmlinux.h: $(VMLINUX_BTF) | $(BPFTOOL) $(INCLUDE_DIR)
181	$(call msg,GEN,,$@)
182	$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
183
184# Get Clang's default includes on this system, as opposed to those seen by
185# '-target bpf'. This fixes "missing" files on some architectures/distros,
186# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
187#
188# Use '-idirafter': Don't interfere with include mechanics except where the
189# build would have failed anyways.
190define get_sys_includes
191$(shell $(1) -v -E - </dev/null 2>&1 \
192	| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }')
193endef
194
195# Determine target endianness.
196IS_LITTLE_ENDIAN = $(shell $(CC) -dM -E - </dev/null | \
197			grep 'define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__')
198MENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian)
199
200CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
201BPF_CFLAGS = -g -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN) 			\
202	     -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR)			\
203	     -I$(abspath $(OUTPUT)/../usr/include)
204
205CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \
206	       -Wno-compare-distinct-pointer-types
207
208$(OUTPUT)/test_l4lb_noinline.o: BPF_CFLAGS += -fno-inline
209$(OUTPUT)/test_xdp_noinline.o: BPF_CFLAGS += -fno-inline
210
211$(OUTPUT)/flow_dissector_load.o: flow_dissector_load.h
212
213# Build BPF object using Clang
214# $1 - input .c file
215# $2 - output .o file
216# $3 - CFLAGS
217# $4 - LDFLAGS
218define CLANG_BPF_BUILD_RULE
219	$(call msg,CLNG-LLC,$(TRUNNER_BINARY),$2)
220	($(CLANG) $3 -O2 -target bpf -emit-llvm				\
221		-c $1 -o - || echo "BPF obj compilation failed") | 	\
222	$(LLC) -mattr=dwarfris -march=bpf -mcpu=v3 $4 -filetype=obj -o $2
223endef
224# Similar to CLANG_BPF_BUILD_RULE, but with disabled alu32
225define CLANG_NOALU32_BPF_BUILD_RULE
226	$(call msg,CLNG-LLC,$(TRUNNER_BINARY),$2)
227	($(CLANG) $3 -O2 -target bpf -emit-llvm				\
228		-c $1 -o - || echo "BPF obj compilation failed") | 	\
229	$(LLC) -march=bpf -mcpu=v2 $4 -filetype=obj -o $2
230endef
231# Similar to CLANG_BPF_BUILD_RULE, but using native Clang and bpf LLC
232define CLANG_NATIVE_BPF_BUILD_RULE
233	$(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2)
234	($(CLANG) $3 -O2 -emit-llvm					\
235		-c $1 -o - || echo "BPF obj compilation failed") | 	\
236	$(LLC) -march=bpf -mcpu=v3 $4 -filetype=obj -o $2
237endef
238# Build BPF object using GCC
239define GCC_BPF_BUILD_RULE
240	$(call msg,GCC-BPF,$(TRUNNER_BINARY),$2)
241	$(BPF_GCC) $3 $4 -O2 -c $1 -o $2
242endef
243
244SKEL_BLACKLIST := btf__% test_pinning_invalid.c
245
246# Set up extra TRUNNER_XXX "temporary" variables in the environment (relies on
247# $eval()) and pass control to DEFINE_TEST_RUNNER_RULES.
248# Parameters:
249# $1 - test runner base binary name (e.g., test_progs)
250# $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc)
251define DEFINE_TEST_RUNNER
252
253TRUNNER_OUTPUT := $(OUTPUT)$(if $2,/)$2
254TRUNNER_BINARY := $1$(if $2,-)$2
255TRUNNER_TEST_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.test.o,	\
256				 $$(notdir $$(wildcard $(TRUNNER_TESTS_DIR)/*.c)))
257TRUNNER_EXTRA_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o,		\
258				 $$(filter %.c,$(TRUNNER_EXTRA_SOURCES)))
259TRUNNER_EXTRA_HDRS := $$(filter %.h,$(TRUNNER_EXTRA_SOURCES))
260TRUNNER_TESTS_HDR := $(TRUNNER_TESTS_DIR)/tests.h
261TRUNNER_BPF_SRCS := $$(notdir $$(wildcard $(TRUNNER_BPF_PROGS_DIR)/*.c))
262TRUNNER_BPF_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, $$(TRUNNER_BPF_SRCS))
263TRUNNER_BPF_SKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.skel.h,	\
264				 $$(filter-out $(SKEL_BLACKLIST),	\
265					       $$(TRUNNER_BPF_SRCS)))
266
267# Evaluate rules now with extra TRUNNER_XXX variables above already defined
268$$(eval $$(call DEFINE_TEST_RUNNER_RULES,$1,$2))
269
270endef
271
272# Using TRUNNER_XXX variables, provided by callers of DEFINE_TEST_RUNNER and
273# set up by DEFINE_TEST_RUNNER itself, create test runner build rules with:
274# $1 - test runner base binary name (e.g., test_progs)
275# $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc)
276define DEFINE_TEST_RUNNER_RULES
277
278ifeq ($($(TRUNNER_OUTPUT)-dir),)
279$(TRUNNER_OUTPUT)-dir := y
280$(TRUNNER_OUTPUT):
281	$$(call msg,MKDIR,,$$@)
282	mkdir -p $$@
283endif
284
285# ensure we set up BPF objects generation rule just once for a given
286# input/output directory combination
287ifeq ($($(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs),)
288$(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs := y
289$(TRUNNER_BPF_OBJS): $(TRUNNER_OUTPUT)/%.o:				\
290		     $(TRUNNER_BPF_PROGS_DIR)/%.c			\
291		     $(TRUNNER_BPF_PROGS_DIR)/*.h			\
292		     $$(INCLUDE_DIR)/vmlinux.h				\
293		     $$(BPFOBJ) | $(TRUNNER_OUTPUT)
294	$$(call $(TRUNNER_BPF_BUILD_RULE),$$<,$$@,			\
295					  $(TRUNNER_BPF_CFLAGS),	\
296					  $(TRUNNER_BPF_LDFLAGS))
297
298$(TRUNNER_BPF_SKELS): $(TRUNNER_OUTPUT)/%.skel.h:			\
299		      $(TRUNNER_OUTPUT)/%.o				\
300		      | $(BPFTOOL) $(TRUNNER_OUTPUT)
301	$$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
302	$$(BPFTOOL) gen skeleton $$< > $$@
303endif
304
305# ensure we set up tests.h header generation rule just once
306ifeq ($($(TRUNNER_TESTS_DIR)-tests-hdr),)
307$(TRUNNER_TESTS_DIR)-tests-hdr := y
308$(TRUNNER_TESTS_HDR): $(TRUNNER_TESTS_DIR)/*.c
309	$$(call msg,TEST-HDR,$(TRUNNER_BINARY),$$@)
310	$$(shell ( cd $(TRUNNER_TESTS_DIR);				\
311		  echo '/* Generated header, do not edit */';		\
312		  ls *.c 2> /dev/null |					\
313			sed -e 's@\([^\.]*\)\.c@DEFINE_TEST(\1)@';	\
314		 ) > $$@)
315endif
316
317# compile individual test files
318# Note: we cd into output directory to ensure embedded BPF object is found
319$(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o:			\
320		      $(TRUNNER_TESTS_DIR)/%.c				\
321		      $(TRUNNER_EXTRA_HDRS)				\
322		      $(TRUNNER_BPF_OBJS)				\
323		      $(TRUNNER_BPF_SKELS)				\
324		      $$(BPFOBJ) | $(TRUNNER_OUTPUT)
325	$$(call msg,TEST-OBJ,$(TRUNNER_BINARY),$$@)
326	cd $$(@D) && $$(CC) $$(CFLAGS) -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F)
327
328$(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o:				\
329		       %.c						\
330		       $(TRUNNER_EXTRA_HDRS)				\
331		       $(TRUNNER_TESTS_HDR)				\
332		       $$(BPFOBJ) | $(TRUNNER_OUTPUT)
333	$$(call msg,EXT-OBJ,$(TRUNNER_BINARY),$$@)
334	$$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@
335
336# only copy extra resources if in flavored build
337$(TRUNNER_BINARY)-extras: $(TRUNNER_EXTRA_FILES) | $(TRUNNER_OUTPUT)
338ifneq ($2,)
339	$$(call msg,EXT-COPY,$(TRUNNER_BINARY),$(TRUNNER_EXTRA_FILES))
340	cp -a $$^ $(TRUNNER_OUTPUT)/
341endif
342
343$(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS)			\
344			     $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ)		\
345			     | $(TRUNNER_BINARY)-extras
346	$$(call msg,BINARY,,$$@)
347	$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
348
349endef
350
351# Define test_progs test runner.
352TRUNNER_TESTS_DIR := prog_tests
353TRUNNER_BPF_PROGS_DIR := progs
354TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c	\
355			 flow_dissector_load.h
356TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read				\
357		       $(wildcard progs/btf_dump_test_case_*.c)
358TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE
359TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS)
360TRUNNER_BPF_LDFLAGS := -mattr=+alu32
361$(eval $(call DEFINE_TEST_RUNNER,test_progs))
362
363# Define test_progs-no_alu32 test runner.
364TRUNNER_BPF_BUILD_RULE := CLANG_NOALU32_BPF_BUILD_RULE
365TRUNNER_BPF_LDFLAGS :=
366$(eval $(call DEFINE_TEST_RUNNER,test_progs,no_alu32))
367
368# Define test_progs BPF-GCC-flavored test runner.
369ifneq ($(BPF_GCC),)
370TRUNNER_BPF_BUILD_RULE := GCC_BPF_BUILD_RULE
371TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(call get_sys_includes,gcc)
372TRUNNER_BPF_LDFLAGS :=
373$(eval $(call DEFINE_TEST_RUNNER,test_progs,bpf_gcc))
374endif
375
376# Define test_maps test runner.
377TRUNNER_TESTS_DIR := map_tests
378TRUNNER_BPF_PROGS_DIR := progs
379TRUNNER_EXTRA_SOURCES := test_maps.c
380TRUNNER_EXTRA_FILES :=
381TRUNNER_BPF_BUILD_RULE := $$(error no BPF objects should be built)
382TRUNNER_BPF_CFLAGS :=
383TRUNNER_BPF_LDFLAGS :=
384$(eval $(call DEFINE_TEST_RUNNER,test_maps))
385
386# Define test_verifier test runner.
387# It is much simpler than test_maps/test_progs and sufficiently different from
388# them (e.g., test.h is using completely pattern), that it's worth just
389# explicitly defining all the rules explicitly.
390verifier/tests.h: verifier/*.c
391	$(shell ( cd verifier/; \
392		  echo '/* Generated header, do not edit */'; \
393		  echo '#ifdef FILL_ARRAY'; \
394		  ls *.c 2> /dev/null | sed -e 's@\(.*\)@#include \"\1\"@'; \
395		  echo '#endif' \
396		) > verifier/tests.h)
397$(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT)
398	$(call msg,BINARY,,$@)
399	$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
400
401# Make sure we are able to include and link libbpf against c++.
402$(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ)
403	$(call msg,CXX,,$@)
404	$(CXX) $(CFLAGS) $^ $(LDLIBS) -o $@
405
406EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(SCRATCH_DIR)			\
407	prog_tests/tests.h map_tests/tests.h verifier/tests.h		\
408	feature								\
409	$(addprefix $(OUTPUT)/,*.o *.skel.h no_alu32 bpf_gcc)
410