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