xref: /linux/tools/testing/selftests/bpf/Makefile (revision 7d9e5f422150ed00de744e02a80734d74cc9704d)
1# SPDX-License-Identifier: GPL-2.0
2
3LIBDIR := ../../../lib
4BPFDIR := $(LIBDIR)/bpf
5APIDIR := ../../../include/uapi
6GENDIR := ../../../../include/generated
7GENHDR := $(GENDIR)/autoconf.h
8
9ifneq ($(wildcard $(GENHDR)),)
10  GENFLAGS := -DHAVE_GENHDR
11endif
12
13CLANG		?= clang
14LLC		?= llc
15LLVM_OBJCOPY	?= llvm-objcopy
16LLVM_READELF	?= llvm-readelf
17BTF_PAHOLE	?= pahole
18CFLAGS += -Wall -O2 -I$(APIDIR) -I$(LIBDIR) -I$(BPFDIR) -I$(GENDIR) $(GENFLAGS) -I../../../include \
19	  -Dbpf_prog_load=bpf_prog_test_load \
20	  -Dbpf_load_program=bpf_test_load_program
21LDLIBS += -lcap -lelf -lrt -lpthread
22
23# Order correspond to 'make run_tests' order
24TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \
25	test_align test_verifier_log test_dev_cgroup test_tcpbpf_user \
26	test_sock test_btf test_sockmap get_cgroup_id_user test_socket_cookie \
27	test_cgroup_storage test_select_reuseport test_section_names \
28	test_netcnt test_tcpnotify_user test_sock_fields test_sysctl test_hashmap \
29	test_btf_dump test_cgroup_attach xdping
30
31BPF_OBJ_FILES = $(patsubst %.c,%.o, $(notdir $(wildcard progs/*.c)))
32TEST_GEN_FILES = $(BPF_OBJ_FILES)
33
34# Also test sub-register code-gen if LLVM has eBPF v3 processor support which
35# contains both ALU32 and JMP32 instructions.
36SUBREG_CODEGEN := $(shell echo "int cal(int a) { return a > 0; }" | \
37			$(CLANG) -target bpf -O2 -emit-llvm -S -x c - -o - | \
38			$(LLC) -mattr=+alu32 -mcpu=v3 2>&1 | \
39			grep 'if w')
40ifneq ($(SUBREG_CODEGEN),)
41TEST_GEN_FILES += $(patsubst %.o,alu32/%.o, $(BPF_OBJ_FILES))
42endif
43
44# Order correspond to 'make run_tests' order
45TEST_PROGS := test_kmod.sh \
46	test_libbpf.sh \
47	test_xdp_redirect.sh \
48	test_xdp_meta.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.sh \
57	test_lwt_ip_encap.sh \
58	test_tcp_check_syncookie.sh \
59	test_tc_tunnel.sh \
60	test_tc_edt.sh \
61	test_xdping.sh
62
63TEST_PROGS_EXTENDED := with_addr.sh \
64	with_tunnels.sh \
65	tcp_client.py \
66	tcp_server.py
67
68# Compile but not part of 'make run_tests'
69TEST_GEN_PROGS_EXTENDED = test_libbpf_open test_sock_addr test_skb_cgroup_id_user \
70	flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \
71	test_lirc_mode2_user
72
73include ../lib.mk
74
75# NOTE: $(OUTPUT) won't get default value if used before lib.mk
76TEST_CUSTOM_PROGS = $(OUTPUT)/urandom_read
77all: $(TEST_CUSTOM_PROGS)
78
79$(OUTPUT)/urandom_read: $(OUTPUT)/%: %.c
80	$(CC) -o $@ $< -Wl,--build-id
81
82$(OUTPUT)/test_maps: map_tests/*.c
83
84BPFOBJ := $(OUTPUT)/libbpf.a
85
86$(TEST_GEN_PROGS): test_stub.o $(BPFOBJ)
87
88$(TEST_GEN_PROGS_EXTENDED): test_stub.o $(OUTPUT)/libbpf.a
89
90$(OUTPUT)/test_dev_cgroup: cgroup_helpers.c
91$(OUTPUT)/test_skb_cgroup_id_user: cgroup_helpers.c
92$(OUTPUT)/test_sock: cgroup_helpers.c
93$(OUTPUT)/test_sock_addr: cgroup_helpers.c
94$(OUTPUT)/test_socket_cookie: cgroup_helpers.c
95$(OUTPUT)/test_sockmap: cgroup_helpers.c
96$(OUTPUT)/test_tcpbpf_user: cgroup_helpers.c
97$(OUTPUT)/test_tcpnotify_user: cgroup_helpers.c trace_helpers.c
98$(OUTPUT)/test_progs: trace_helpers.c
99$(OUTPUT)/get_cgroup_id_user: cgroup_helpers.c
100$(OUTPUT)/test_cgroup_storage: cgroup_helpers.c
101$(OUTPUT)/test_netcnt: cgroup_helpers.c
102$(OUTPUT)/test_sock_fields: cgroup_helpers.c
103$(OUTPUT)/test_sysctl: cgroup_helpers.c
104$(OUTPUT)/test_cgroup_attach: cgroup_helpers.c
105
106.PHONY: force
107
108# force a rebuild of BPFOBJ when its dependencies are updated
109force:
110
111$(BPFOBJ): force
112	$(MAKE) -C $(BPFDIR) OUTPUT=$(OUTPUT)/
113
114PROBE := $(shell $(LLC) -march=bpf -mcpu=probe -filetype=null /dev/null 2>&1)
115
116# Let newer LLVM versions transparently probe the kernel for availability
117# of full BPF instruction set.
118ifeq ($(PROBE),)
119  CPU ?= probe
120else
121  CPU ?= generic
122endif
123
124# Get Clang's default includes on this system, as opposed to those seen by
125# '-target bpf'. This fixes "missing" files on some architectures/distros,
126# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
127#
128# Use '-idirafter': Don't interfere with include mechanics except where the
129# build would have failed anyways.
130CLANG_SYS_INCLUDES := $(shell $(CLANG) -v -E - </dev/null 2>&1 \
131	| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }')
132
133CLANG_FLAGS = -I. -I./include/uapi -I../../../include/uapi \
134	      $(CLANG_SYS_INCLUDES) \
135	      -Wno-compare-distinct-pointer-types
136
137$(OUTPUT)/test_l4lb_noinline.o: CLANG_FLAGS += -fno-inline
138$(OUTPUT)/test_xdp_noinline.o: CLANG_FLAGS += -fno-inline
139
140$(OUTPUT)/test_queue_map.o: test_queue_stack_map.h
141$(OUTPUT)/test_stack_map.o: test_queue_stack_map.h
142
143$(OUTPUT)/flow_dissector_load.o: flow_dissector_load.h
144$(OUTPUT)/test_progs.o: flow_dissector_load.h
145
146BTF_LLC_PROBE := $(shell $(LLC) -march=bpf -mattr=help 2>&1 | grep dwarfris)
147BTF_PAHOLE_PROBE := $(shell $(BTF_PAHOLE) --help 2>&1 | grep BTF)
148BTF_OBJCOPY_PROBE := $(shell $(LLVM_OBJCOPY) --help 2>&1 | grep -i 'usage.*llvm')
149BTF_LLVM_PROBE := $(shell echo "int main() { return 0; }" | \
150			  $(CLANG) -target bpf -O2 -g -c -x c - -o ./llvm_btf_verify.o; \
151			  $(LLVM_READELF) -S ./llvm_btf_verify.o | grep BTF; \
152			  /bin/rm -f ./llvm_btf_verify.o)
153
154ifneq ($(BTF_LLVM_PROBE),)
155	CLANG_FLAGS += -g
156else
157ifneq ($(BTF_LLC_PROBE),)
158ifneq ($(BTF_PAHOLE_PROBE),)
159ifneq ($(BTF_OBJCOPY_PROBE),)
160	CLANG_FLAGS += -g
161	LLC_FLAGS += -mattr=dwarfris
162	DWARF2BTF = y
163endif
164endif
165endif
166endif
167
168TEST_PROGS_CFLAGS := -I. -I$(OUTPUT)
169TEST_VERIFIER_CFLAGS := -I. -I$(OUTPUT) -Iverifier
170
171ifneq ($(SUBREG_CODEGEN),)
172ALU32_BUILD_DIR = $(OUTPUT)/alu32
173TEST_CUSTOM_PROGS += $(ALU32_BUILD_DIR)/test_progs_32
174$(ALU32_BUILD_DIR):
175	mkdir -p $@
176
177$(ALU32_BUILD_DIR)/urandom_read: $(OUTPUT)/urandom_read
178	cp $< $@
179
180$(ALU32_BUILD_DIR)/test_progs_32: test_progs.c $(OUTPUT)/libbpf.a\
181						$(ALU32_BUILD_DIR) \
182						$(ALU32_BUILD_DIR)/urandom_read
183	$(CC) $(TEST_PROGS_CFLAGS) $(CFLAGS) \
184		-o $(ALU32_BUILD_DIR)/test_progs_32 \
185		test_progs.c test_stub.c trace_helpers.c prog_tests/*.c \
186		$(OUTPUT)/libbpf.a $(LDLIBS)
187
188$(ALU32_BUILD_DIR)/test_progs_32: $(PROG_TESTS_H)
189$(ALU32_BUILD_DIR)/test_progs_32: prog_tests/*.c
190
191$(ALU32_BUILD_DIR)/%.o: progs/%.c $(ALU32_BUILD_DIR) \
192					$(ALU32_BUILD_DIR)/test_progs_32
193	$(CLANG) $(CLANG_FLAGS) \
194		 -O2 -target bpf -emit-llvm -c $< -o - |      \
195	$(LLC) -march=bpf -mattr=+alu32 -mcpu=$(CPU) $(LLC_FLAGS) \
196		-filetype=obj -o $@
197ifeq ($(DWARF2BTF),y)
198	$(BTF_PAHOLE) -J $@
199endif
200endif
201
202# Have one program compiled without "-target bpf" to test whether libbpf loads
203# it successfully
204$(OUTPUT)/test_xdp.o: progs/test_xdp.c
205	$(CLANG) $(CLANG_FLAGS) \
206		-O2 -emit-llvm -c $< -o - | \
207	$(LLC) -march=bpf -mcpu=$(CPU) $(LLC_FLAGS) -filetype=obj -o $@
208ifeq ($(DWARF2BTF),y)
209	$(BTF_PAHOLE) -J $@
210endif
211
212$(OUTPUT)/%.o: progs/%.c
213	$(CLANG) $(CLANG_FLAGS) \
214		 -O2 -target bpf -emit-llvm -c $< -o - |      \
215	$(LLC) -march=bpf -mcpu=$(CPU) $(LLC_FLAGS) -filetype=obj -o $@
216ifeq ($(DWARF2BTF),y)
217	$(BTF_PAHOLE) -J $@
218endif
219
220PROG_TESTS_H := $(OUTPUT)/prog_tests/tests.h
221test_progs.c: $(PROG_TESTS_H)
222$(OUTPUT)/test_progs: CFLAGS += $(TEST_PROGS_CFLAGS)
223$(OUTPUT)/test_progs: prog_tests/*.c
224
225PROG_TESTS_DIR = $(OUTPUT)/prog_tests
226$(PROG_TESTS_DIR):
227	mkdir -p $@
228
229PROG_TESTS_FILES := $(wildcard prog_tests/*.c)
230$(PROG_TESTS_H): $(PROG_TESTS_DIR) $(PROG_TESTS_FILES)
231	$(shell ( cd prog_tests/; \
232		  echo '/* Generated header, do not edit */'; \
233		  echo '#ifdef DECLARE'; \
234		  ls *.c 2> /dev/null | \
235			sed -e 's@\([^\.]*\)\.c@extern void test_\1(void);@'; \
236		  echo '#endif'; \
237		  echo '#ifdef CALL'; \
238		  ls *.c 2> /dev/null | \
239			sed -e 's@\([^\.]*\)\.c@test_\1();@'; \
240		  echo '#endif' \
241		 ) > $(PROG_TESTS_H))
242
243TEST_MAPS_CFLAGS := -I. -I$(OUTPUT)
244MAP_TESTS_DIR = $(OUTPUT)/map_tests
245$(MAP_TESTS_DIR):
246	mkdir -p $@
247MAP_TESTS_H := $(MAP_TESTS_DIR)/tests.h
248test_maps.c: $(MAP_TESTS_H)
249$(OUTPUT)/test_maps: CFLAGS += $(TEST_MAPS_CFLAGS)
250MAP_TESTS_FILES := $(wildcard map_tests/*.c)
251$(MAP_TESTS_H): $(MAP_TESTS_DIR) $(MAP_TESTS_FILES)
252	$(shell ( cd map_tests/; \
253		  echo '/* Generated header, do not edit */'; \
254		  echo '#ifdef DECLARE'; \
255		  ls *.c 2> /dev/null | \
256			sed -e 's@\([^\.]*\)\.c@extern void test_\1(void);@'; \
257		  echo '#endif'; \
258		  echo '#ifdef CALL'; \
259		  ls *.c 2> /dev/null | \
260			sed -e 's@\([^\.]*\)\.c@test_\1();@'; \
261		  echo '#endif' \
262		 ) > $(MAP_TESTS_H))
263
264VERIFIER_TESTS_H := $(OUTPUT)/verifier/tests.h
265test_verifier.c: $(VERIFIER_TESTS_H)
266$(OUTPUT)/test_verifier: CFLAGS += $(TEST_VERIFIER_CFLAGS)
267
268VERIFIER_TESTS_DIR = $(OUTPUT)/verifier
269$(VERIFIER_TESTS_DIR):
270	mkdir -p $@
271
272VERIFIER_TEST_FILES := $(wildcard verifier/*.c)
273$(OUTPUT)/verifier/tests.h: $(VERIFIER_TESTS_DIR) $(VERIFIER_TEST_FILES)
274	$(shell ( cd verifier/; \
275		  echo '/* Generated header, do not edit */'; \
276		  echo '#ifdef FILL_ARRAY'; \
277		  ls *.c 2> /dev/null | \
278			sed -e 's@\(.*\)@#include \"\1\"@'; \
279		  echo '#endif' \
280		 ) > $(VERIFIER_TESTS_H))
281
282EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(ALU32_BUILD_DIR) \
283	$(VERIFIER_TESTS_H) $(PROG_TESTS_H) $(MAP_TESTS_H) \
284	feature
285