selftests: bpf: tc-bpf flow shaping with EDT
[linux-2.6-block.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 \
194db0d9 24 test_sock test_btf test_sockmap test_lirc_mode2_user get_cgroup_id_user \
371e4fcc 25 test_socket_cookie test_cgroup_storage test_select_reuseport test_section_names \
e0b27b3f 26 test_netcnt test_tcpnotify_user test_sock_fields
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 \
bafc0ba8 66 flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user
f09b2e38 67
e8f1f34a
ZSL
68include ../lib.mk
69
3ef84346
JW
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
f6827526 75 $(CC) -o $@ $< -Wl,--build-id
3ef84346 76
41757dcb 77BPFOBJ := $(OUTPUT)/libbpf.a
e8f1f34a
ZSL
78
79$(TEST_GEN_PROGS): $(BPFOBJ)
1da8ac7c 80
f09b2e38
JDB
81$(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/libbpf.a
82
41757dcb 83$(OUTPUT)/test_dev_cgroup: cgroup_helpers.c
5ecd8c22 84$(OUTPUT)/test_skb_cgroup_id_user: cgroup_helpers.c
1d436885 85$(OUTPUT)/test_sock: cgroup_helpers.c
e50b0a6f 86$(OUTPUT)/test_sock_addr: cgroup_helpers.c
194db0d9 87$(OUTPUT)/test_socket_cookie: cgroup_helpers.c
16962b24 88$(OUTPUT)/test_sockmap: cgroup_helpers.c
c65267e5 89$(OUTPUT)/test_tcpbpf_user: cgroup_helpers.c
435f90a3 90$(OUTPUT)/test_tcpnotify_user: cgroup_helpers.c trace_helpers.c
173965fb 91$(OUTPUT)/test_progs: trace_helpers.c
f269099a 92$(OUTPUT)/get_cgroup_id_user: cgroup_helpers.c
68cfa3ac 93$(OUTPUT)/test_cgroup_storage: cgroup_helpers.c
371e4fcc 94$(OUTPUT)/test_netcnt: cgroup_helpers.c
e0b27b3f 95$(OUTPUT)/test_sock_fields: cgroup_helpers.c
41757dcb 96
e8f1f34a 97.PHONY: force
d498f871 98
d498f871
MS
99# force a rebuild of BPFOBJ when its dependencies are updated
100force:
101
102$(BPFOBJ): force
e8f1f34a 103 $(MAKE) -C $(BPFDIR) OUTPUT=$(OUTPUT)/
6882804c 104
cd95a892 105PROBE := $(shell $(LLC) -march=bpf -mcpu=probe -filetype=null /dev/null 2>&1)
22c88526
DB
106
107# Let newer LLVM versions transparently probe the kernel for availability
108# of full BPF instruction set.
109ifeq ($(PROBE),)
110 CPU ?= probe
111else
112 CPU ?= generic
113endif
6882804c 114
167381f3
SB
115# Get Clang's default includes on this system, as opposed to those seen by
116# '-target bpf'. This fixes "missing" files on some architectures/distros,
117# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
118#
119# Use '-idirafter': Don't interfere with include mechanics except where the
120# build would have failed anyways.
121CLANG_SYS_INCLUDES := $(shell $(CLANG) -v -E - </dev/null 2>&1 \
122 | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }')
123
3bc35c63 124CLANG_FLAGS = -I. -I./include/uapi -I../../../include/uapi \
167381f3 125 $(CLANG_SYS_INCLUDES) \
3bc35c63
AS
126 -Wno-compare-distinct-pointer-types
127
128$(OUTPUT)/test_l4lb_noinline.o: CLANG_FLAGS += -fno-inline
b0b04fc4 129$(OUTPUT)/test_xdp_noinline.o: CLANG_FLAGS += -fno-inline
3bc35c63 130
43b987d2
MV
131$(OUTPUT)/test_queue_map.o: test_queue_stack_map.h
132$(OUTPUT)/test_stack_map.o: test_queue_stack_map.h
133
bf0f0fd9
SF
134$(OUTPUT)/flow_dissector_load.o: flow_dissector_load.h
135$(OUTPUT)/test_progs.o: flow_dissector_load.h
136
91bc07c9
JS
137BTF_LLC_PROBE := $(shell $(LLC) -march=bpf -mattr=help 2>&1 | grep dwarfris)
138BTF_PAHOLE_PROBE := $(shell $(BTF_PAHOLE) --help 2>&1 | grep BTF)
7c3e8b64 139BTF_OBJCOPY_PROBE := $(shell $(LLVM_OBJCOPY) --help 2>&1 | grep -i 'usage.*llvm')
9ce6ae22
YS
140BTF_LLVM_PROBE := $(shell echo "int main() { return 0; }" | \
141 $(CLANG) -target bpf -O2 -g -c -x c - -o ./llvm_btf_verify.o; \
2f092126 142 $(LLVM_READELF) -S ./llvm_btf_verify.o | grep BTF; \
9ce6ae22 143 /bin/rm -f ./llvm_btf_verify.o)
c0fa1b6c 144
9ce6ae22
YS
145ifneq ($(BTF_LLVM_PROBE),)
146 CLANG_FLAGS += -g
147else
c0fa1b6c
MKL
148ifneq ($(BTF_LLC_PROBE),)
149ifneq ($(BTF_PAHOLE_PROBE),)
150ifneq ($(BTF_OBJCOPY_PROBE),)
151 CLANG_FLAGS += -g
152 LLC_FLAGS += -mattr=dwarfris
153 DWARF2BTF = y
154endif
155endif
156endif
9ce6ae22 157endif
c0fa1b6c 158
e78e00bd
SF
159TEST_PROGS_CFLAGS := -I. -I$(OUTPUT)
160TEST_VERIFIER_CFLAGS := -I. -I$(OUTPUT) -Iverifier
161
3ef84346
JW
162ifneq ($(SUBREG_CODEGEN),)
163ALU32_BUILD_DIR = $(OUTPUT)/alu32
164TEST_CUSTOM_PROGS += $(ALU32_BUILD_DIR)/test_progs_32
165$(ALU32_BUILD_DIR):
166 mkdir -p $@
167
168$(ALU32_BUILD_DIR)/urandom_read: $(OUTPUT)/urandom_read
169 cp $< $@
170
e78e00bd
SF
171$(ALU32_BUILD_DIR)/test_progs_32: test_progs.c $(OUTPUT)/libbpf.a\
172 $(ALU32_BUILD_DIR) \
3ef84346 173 $(ALU32_BUILD_DIR)/urandom_read
e78e00bd
SF
174 $(CC) $(TEST_PROGS_CFLAGS) $(CFLAGS) \
175 -o $(ALU32_BUILD_DIR)/test_progs_32 \
176 test_progs.c trace_helpers.c prog_tests/*.c \
177 $(OUTPUT)/libbpf.a $(LDLIBS)
3f306588
SF
178
179$(ALU32_BUILD_DIR)/test_progs_32: $(PROG_TESTS_H)
3f306588 180$(ALU32_BUILD_DIR)/test_progs_32: prog_tests/*.c
3ef84346 181
bd4aed0e
JW
182$(ALU32_BUILD_DIR)/%.o: progs/%.c $(ALU32_BUILD_DIR) \
183 $(ALU32_BUILD_DIR)/test_progs_32
3ef84346
JW
184 $(CLANG) $(CLANG_FLAGS) \
185 -O2 -target bpf -emit-llvm -c $< -o - | \
186 $(LLC) -march=bpf -mattr=+alu32 -mcpu=$(CPU) $(LLC_FLAGS) \
187 -filetype=obj -o $@
188ifeq ($(DWARF2BTF),y)
189 $(BTF_PAHOLE) -J $@
190endif
191endif
192
f96afa76
QM
193# Have one program compiled without "-target bpf" to test whether libbpf loads
194# it successfully
bd4aed0e 195$(OUTPUT)/test_xdp.o: progs/test_xdp.c
f96afa76
QM
196 $(CLANG) $(CLANG_FLAGS) \
197 -O2 -emit-llvm -c $< -o - | \
198 $(LLC) -march=bpf -mcpu=$(CPU) $(LLC_FLAGS) -filetype=obj -o $@
199ifeq ($(DWARF2BTF),y)
200 $(BTF_PAHOLE) -J $@
201endif
c0fa1b6c 202
bd4aed0e 203$(OUTPUT)/%.o: progs/%.c
3bc35c63 204 $(CLANG) $(CLANG_FLAGS) \
22c88526 205 -O2 -target bpf -emit-llvm -c $< -o - | \
c0fa1b6c
MKL
206 $(LLC) -march=bpf -mcpu=$(CPU) $(LLC_FLAGS) -filetype=obj -o $@
207ifeq ($(DWARF2BTF),y)
208 $(BTF_PAHOLE) -J $@
209endif
81f77fd0 210
3f306588
SF
211PROG_TESTS_H := $(OUTPUT)/prog_tests/tests.h
212$(OUTPUT)/test_progs: $(PROG_TESTS_H)
e78e00bd 213$(OUTPUT)/test_progs: CFLAGS += $(TEST_PROGS_CFLAGS)
3f306588
SF
214$(OUTPUT)/test_progs: prog_tests/*.c
215
e78e00bd
SF
216PROG_TESTS_DIR = $(OUTPUT)/prog_tests
217$(PROG_TESTS_DIR):
218 mkdir -p $@
219
3f306588 220PROG_TESTS_FILES := $(wildcard prog_tests/*.c)
e78e00bd
SF
221$(PROG_TESTS_H): $(PROG_TESTS_DIR) $(PROG_TESTS_FILES)
222 $(shell ( cd prog_tests/; \
3f306588
SF
223 echo '/* Generated header, do not edit */'; \
224 echo '#ifdef DECLARE'; \
225 ls *.c 2> /dev/null | \
226 sed -e 's@\([^\.]*\)\.c@extern void test_\1(void);@'; \
227 echo '#endif'; \
228 echo '#ifdef CALL'; \
229 ls *.c 2> /dev/null | \
230 sed -e 's@\([^\.]*\)\.c@test_\1();@'; \
231 echo '#endif' \
232 ) > $(PROG_TESTS_H))
233
2a118154
SF
234VERIFIER_TESTS_H := $(OUTPUT)/verifier/tests.h
235$(OUTPUT)/test_verifier: $(VERIFIER_TESTS_H)
e78e00bd
SF
236$(OUTPUT)/test_verifier: CFLAGS += $(TEST_VERIFIER_CFLAGS)
237
238VERIFIER_TESTS_DIR = $(OUTPUT)/verifier
239$(VERIFIER_TESTS_DIR):
240 mkdir -p $@
2dfb4012
JK
241
242VERIFIER_TEST_FILES := $(wildcard verifier/*.c)
e78e00bd
SF
243$(OUTPUT)/verifier/tests.h: $(VERIFIER_TESTS_DIR) $(VERIFIER_TEST_FILES)
244 $(shell ( cd verifier/; \
2dfb4012
JK
245 echo '/* Generated header, do not edit */'; \
246 echo '#ifdef FILL_ARRAY'; \
247 ls *.c 2> /dev/null | \
248 sed -e 's@\(.*\)@#include \"\1\"@'; \
249 echo '#endif' \
2a118154 250 ) > $(VERIFIER_TESTS_H))
2dfb4012 251
2a118154 252EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(ALU32_BUILD_DIR) \
3f306588 253 $(VERIFIER_TESTS_H) $(PROG_TESTS_H)