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