xref: /linux/tools/testing/selftests/bpf/Makefile (revision 0d97dacc46d9af2daba1af224747d452bd988365)
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 test_lirc_mode2_user get_cgroup_id_user \
27	test_socket_cookie 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
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
62TEST_PROGS_EXTENDED := with_addr.sh \
63	with_tunnels.sh \
64	tcp_client.py \
65	tcp_server.py
66
67# Compile but not part of 'make run_tests'
68TEST_GEN_PROGS_EXTENDED = test_libbpf_open test_sock_addr test_skb_cgroup_id_user \
69	flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user
70
71include ../lib.mk
72
73# NOTE: $(OUTPUT) won't get default value if used before lib.mk
74TEST_CUSTOM_PROGS = $(OUTPUT)/urandom_read
75all: $(TEST_CUSTOM_PROGS)
76
77$(OUTPUT)/urandom_read: $(OUTPUT)/%: %.c
78	$(CC) -o $@ $< -Wl,--build-id
79
80$(OUTPUT)/test_maps: map_tests/*.c
81
82BPFOBJ := $(OUTPUT)/libbpf.a
83
84$(TEST_GEN_PROGS): test_stub.o $(BPFOBJ)
85
86$(TEST_GEN_PROGS_EXTENDED): test_stub.o $(OUTPUT)/libbpf.a
87
88$(OUTPUT)/test_dev_cgroup: cgroup_helpers.c
89$(OUTPUT)/test_skb_cgroup_id_user: cgroup_helpers.c
90$(OUTPUT)/test_sock: cgroup_helpers.c
91$(OUTPUT)/test_sock_addr: cgroup_helpers.c
92$(OUTPUT)/test_socket_cookie: cgroup_helpers.c
93$(OUTPUT)/test_sockmap: cgroup_helpers.c
94$(OUTPUT)/test_tcpbpf_user: cgroup_helpers.c
95$(OUTPUT)/test_tcpnotify_user: cgroup_helpers.c trace_helpers.c
96$(OUTPUT)/test_progs: trace_helpers.c
97$(OUTPUT)/get_cgroup_id_user: cgroup_helpers.c
98$(OUTPUT)/test_cgroup_storage: cgroup_helpers.c
99$(OUTPUT)/test_netcnt: cgroup_helpers.c
100$(OUTPUT)/test_sock_fields: cgroup_helpers.c
101$(OUTPUT)/test_sysctl: cgroup_helpers.c
102
103.PHONY: force
104
105# force a rebuild of BPFOBJ when its dependencies are updated
106force:
107
108$(BPFOBJ): force
109	$(MAKE) -C $(BPFDIR) OUTPUT=$(OUTPUT)/
110
111PROBE := $(shell $(LLC) -march=bpf -mcpu=probe -filetype=null /dev/null 2>&1)
112
113# Let newer LLVM versions transparently probe the kernel for availability
114# of full BPF instruction set.
115ifeq ($(PROBE),)
116  CPU ?= probe
117else
118  CPU ?= generic
119endif
120
121# Get Clang's default includes on this system, as opposed to those seen by
122# '-target bpf'. This fixes "missing" files on some architectures/distros,
123# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
124#
125# Use '-idirafter': Don't interfere with include mechanics except where the
126# build would have failed anyways.
127CLANG_SYS_INCLUDES := $(shell $(CLANG) -v -E - </dev/null 2>&1 \
128	| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }')
129
130CLANG_FLAGS = -I. -I./include/uapi -I../../../include/uapi \
131	      $(CLANG_SYS_INCLUDES) \
132	      -Wno-compare-distinct-pointer-types
133
134$(OUTPUT)/test_l4lb_noinline.o: CLANG_FLAGS += -fno-inline
135$(OUTPUT)/test_xdp_noinline.o: CLANG_FLAGS += -fno-inline
136
137$(OUTPUT)/test_queue_map.o: test_queue_stack_map.h
138$(OUTPUT)/test_stack_map.o: test_queue_stack_map.h
139
140$(OUTPUT)/flow_dissector_load.o: flow_dissector_load.h
141$(OUTPUT)/test_progs.o: flow_dissector_load.h
142
143BTF_LLC_PROBE := $(shell $(LLC) -march=bpf -mattr=help 2>&1 | grep dwarfris)
144BTF_PAHOLE_PROBE := $(shell $(BTF_PAHOLE) --help 2>&1 | grep BTF)
145BTF_OBJCOPY_PROBE := $(shell $(LLVM_OBJCOPY) --help 2>&1 | grep -i 'usage.*llvm')
146BTF_LLVM_PROBE := $(shell echo "int main() { return 0; }" | \
147			  $(CLANG) -target bpf -O2 -g -c -x c - -o ./llvm_btf_verify.o; \
148			  $(LLVM_READELF) -S ./llvm_btf_verify.o | grep BTF; \
149			  /bin/rm -f ./llvm_btf_verify.o)
150
151ifneq ($(BTF_LLVM_PROBE),)
152	CLANG_FLAGS += -g
153else
154ifneq ($(BTF_LLC_PROBE),)
155ifneq ($(BTF_PAHOLE_PROBE),)
156ifneq ($(BTF_OBJCOPY_PROBE),)
157	CLANG_FLAGS += -g
158	LLC_FLAGS += -mattr=dwarfris
159	DWARF2BTF = y
160endif
161endif
162endif
163endif
164
165TEST_PROGS_CFLAGS := -I. -I$(OUTPUT)
166TEST_VERIFIER_CFLAGS := -I. -I$(OUTPUT) -Iverifier
167
168ifneq ($(SUBREG_CODEGEN),)
169ALU32_BUILD_DIR = $(OUTPUT)/alu32
170TEST_CUSTOM_PROGS += $(ALU32_BUILD_DIR)/test_progs_32
171$(ALU32_BUILD_DIR):
172	mkdir -p $@
173
174$(ALU32_BUILD_DIR)/urandom_read: $(OUTPUT)/urandom_read
175	cp $< $@
176
177$(ALU32_BUILD_DIR)/test_progs_32: test_progs.c $(OUTPUT)/libbpf.a\
178						$(ALU32_BUILD_DIR) \
179						$(ALU32_BUILD_DIR)/urandom_read
180	$(CC) $(TEST_PROGS_CFLAGS) $(CFLAGS) \
181		-o $(ALU32_BUILD_DIR)/test_progs_32 \
182		test_progs.c test_stub.c trace_helpers.c prog_tests/*.c \
183		$(OUTPUT)/libbpf.a $(LDLIBS)
184
185$(ALU32_BUILD_DIR)/test_progs_32: $(PROG_TESTS_H)
186$(ALU32_BUILD_DIR)/test_progs_32: prog_tests/*.c
187
188$(ALU32_BUILD_DIR)/%.o: progs/%.c $(ALU32_BUILD_DIR) \
189					$(ALU32_BUILD_DIR)/test_progs_32
190	$(CLANG) $(CLANG_FLAGS) \
191		 -O2 -target bpf -emit-llvm -c $< -o - |      \
192	$(LLC) -march=bpf -mattr=+alu32 -mcpu=$(CPU) $(LLC_FLAGS) \
193		-filetype=obj -o $@
194ifeq ($(DWARF2BTF),y)
195	$(BTF_PAHOLE) -J $@
196endif
197endif
198
199# Have one program compiled without "-target bpf" to test whether libbpf loads
200# it successfully
201$(OUTPUT)/test_xdp.o: progs/test_xdp.c
202	$(CLANG) $(CLANG_FLAGS) \
203		-O2 -emit-llvm -c $< -o - | \
204	$(LLC) -march=bpf -mcpu=$(CPU) $(LLC_FLAGS) -filetype=obj -o $@
205ifeq ($(DWARF2BTF),y)
206	$(BTF_PAHOLE) -J $@
207endif
208
209$(OUTPUT)/%.o: progs/%.c
210	$(CLANG) $(CLANG_FLAGS) \
211		 -O2 -target bpf -emit-llvm -c $< -o - |      \
212	$(LLC) -march=bpf -mcpu=$(CPU) $(LLC_FLAGS) -filetype=obj -o $@
213ifeq ($(DWARF2BTF),y)
214	$(BTF_PAHOLE) -J $@
215endif
216
217PROG_TESTS_H := $(OUTPUT)/prog_tests/tests.h
218test_progs.c: $(PROG_TESTS_H)
219$(OUTPUT)/test_progs: CFLAGS += $(TEST_PROGS_CFLAGS)
220$(OUTPUT)/test_progs: prog_tests/*.c
221
222PROG_TESTS_DIR = $(OUTPUT)/prog_tests
223$(PROG_TESTS_DIR):
224	mkdir -p $@
225
226PROG_TESTS_FILES := $(wildcard prog_tests/*.c)
227$(PROG_TESTS_H): $(PROG_TESTS_DIR) $(PROG_TESTS_FILES)
228	$(shell ( cd prog_tests/; \
229		  echo '/* Generated header, do not edit */'; \
230		  echo '#ifdef DECLARE'; \
231		  ls *.c 2> /dev/null | \
232			sed -e 's@\([^\.]*\)\.c@extern void test_\1(void);@'; \
233		  echo '#endif'; \
234		  echo '#ifdef CALL'; \
235		  ls *.c 2> /dev/null | \
236			sed -e 's@\([^\.]*\)\.c@test_\1();@'; \
237		  echo '#endif' \
238		 ) > $(PROG_TESTS_H))
239
240TEST_MAPS_CFLAGS := -I. -I$(OUTPUT)
241MAP_TESTS_DIR = $(OUTPUT)/map_tests
242$(MAP_TESTS_DIR):
243	mkdir -p $@
244MAP_TESTS_H := $(MAP_TESTS_DIR)/tests.h
245test_maps.c: $(MAP_TESTS_H)
246$(OUTPUT)/test_maps: CFLAGS += $(TEST_MAPS_CFLAGS)
247MAP_TESTS_FILES := $(wildcard map_tests/*.c)
248$(MAP_TESTS_H): $(MAP_TESTS_DIR) $(MAP_TESTS_FILES)
249	$(shell ( cd map_tests/; \
250		  echo '/* Generated header, do not edit */'; \
251		  echo '#ifdef DECLARE'; \
252		  ls *.c 2> /dev/null | \
253			sed -e 's@\([^\.]*\)\.c@extern void test_\1(void);@'; \
254		  echo '#endif'; \
255		  echo '#ifdef CALL'; \
256		  ls *.c 2> /dev/null | \
257			sed -e 's@\([^\.]*\)\.c@test_\1();@'; \
258		  echo '#endif' \
259		 ) > $(MAP_TESTS_H))
260
261VERIFIER_TESTS_H := $(OUTPUT)/verifier/tests.h
262test_verifier.c: $(VERIFIER_TESTS_H)
263$(OUTPUT)/test_verifier: CFLAGS += $(TEST_VERIFIER_CFLAGS)
264
265VERIFIER_TESTS_DIR = $(OUTPUT)/verifier
266$(VERIFIER_TESTS_DIR):
267	mkdir -p $@
268
269VERIFIER_TEST_FILES := $(wildcard verifier/*.c)
270$(OUTPUT)/verifier/tests.h: $(VERIFIER_TESTS_DIR) $(VERIFIER_TEST_FILES)
271	$(shell ( cd verifier/; \
272		  echo '/* Generated header, do not edit */'; \
273		  echo '#ifdef FILL_ARRAY'; \
274		  ls *.c 2> /dev/null | \
275			sed -e 's@\(.*\)@#include \"\1\"@'; \
276		  echo '#endif' \
277		 ) > $(VERIFIER_TESTS_H))
278
279EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(ALU32_BUILD_DIR) \
280	$(VERIFIER_TESTS_H) $(PROG_TESTS_H) $(MAP_TESTS_H)
281