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