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