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