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