1# SPDX-License-Identifier: GPL-2.0 2include ../../../build/Build.include 3include ../../../scripts/Makefile.arch 4include ../../../scripts/Makefile.include 5 6CXX ?= $(CROSS_COMPILE)g++ 7 8CURDIR := $(abspath .) 9TOOLSDIR := $(abspath ../../..) 10LIBDIR := $(TOOLSDIR)/lib 11BPFDIR := $(LIBDIR)/bpf 12TOOLSINCDIR := $(TOOLSDIR)/include 13BPFTOOLDIR := $(TOOLSDIR)/bpf/bpftool 14APIDIR := $(TOOLSINCDIR)/uapi 15GENDIR := $(abspath ../../../../include/generated) 16GENHDR := $(GENDIR)/autoconf.h 17HOSTPKG_CONFIG := pkg-config 18 19ifneq ($(wildcard $(GENHDR)),) 20 GENFLAGS := -DHAVE_GENHDR 21endif 22 23BPF_GCC ?= $(shell command -v bpf-gcc;) 24SAN_CFLAGS ?= 25CFLAGS += -g -O0 -rdynamic -Wall -Werror $(GENFLAGS) $(SAN_CFLAGS) \ 26 -I$(CURDIR) -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR) \ 27 -I$(TOOLSINCDIR) -I$(APIDIR) -I$(OUTPUT) 28LDFLAGS += $(SAN_CFLAGS) 29LDLIBS += -lelf -lz -lrt -lpthread 30 31# Silence some warnings when compiled with clang 32ifneq ($(LLVM),) 33CFLAGS += -Wno-unused-command-line-argument 34endif 35 36# Order correspond to 'make run_tests' order 37TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \ 38 test_verifier_log test_dev_cgroup \ 39 test_sock test_sockmap get_cgroup_id_user \ 40 test_cgroup_storage \ 41 test_tcpnotify_user test_sysctl \ 42 test_progs-no_alu32 43 44# Also test bpf-gcc, if present 45ifneq ($(BPF_GCC),) 46TEST_GEN_PROGS += test_progs-bpf_gcc 47endif 48 49TEST_GEN_FILES = test_lwt_ip_encap.bpf.o test_tc_edt.bpf.o 50TEST_FILES = xsk_prereqs.sh $(wildcard progs/btf_dump_test_case_*.c) 51 52# Order correspond to 'make run_tests' order 53TEST_PROGS := test_kmod.sh \ 54 test_xdp_redirect.sh \ 55 test_xdp_redirect_multi.sh \ 56 test_xdp_meta.sh \ 57 test_xdp_veth.sh \ 58 test_offload.py \ 59 test_sock_addr.sh \ 60 test_tunnel.sh \ 61 test_lwt_seg6local.sh \ 62 test_lirc_mode2.sh \ 63 test_skb_cgroup_id.sh \ 64 test_flow_dissector.sh \ 65 test_xdp_vlan_mode_generic.sh \ 66 test_xdp_vlan_mode_native.sh \ 67 test_lwt_ip_encap.sh \ 68 test_tcp_check_syncookie.sh \ 69 test_tc_tunnel.sh \ 70 test_tc_edt.sh \ 71 test_xdping.sh \ 72 test_bpftool_build.sh \ 73 test_bpftool.sh \ 74 test_bpftool_metadata.sh \ 75 test_doc_build.sh \ 76 test_xsk.sh 77 78TEST_PROGS_EXTENDED := with_addr.sh \ 79 with_tunnels.sh ima_setup.sh verify_sig_setup.sh \ 80 test_xdp_vlan.sh test_bpftool.py 81 82# Compile but not part of 'make run_tests' 83TEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \ 84 flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \ 85 test_lirc_mode2_user xdping test_cpp runqslower bench bpf_testmod.ko \ 86 xskxceiver xdp_redirect_multi xdp_synproxy veristat 87 88TEST_CUSTOM_PROGS = $(OUTPUT)/urandom_read $(OUTPUT)/sign-file 89TEST_GEN_FILES += liburandom_read.so 90 91# Emit succinct information message describing current building step 92# $1 - generic step name (e.g., CC, LINK, etc); 93# $2 - optional "flavor" specifier; if provided, will be emitted as [flavor]; 94# $3 - target (assumed to be file); only file name will be emitted; 95# $4 - optional extra arg, emitted as-is, if provided. 96ifeq ($(V),1) 97Q = 98msg = 99else 100Q = @ 101msg = @printf ' %-8s%s %s%s\n' "$(1)" "$(if $(2), [$(2)])" "$(notdir $(3))" "$(if $(4), $(4))"; 102MAKEFLAGS += --no-print-directory 103submake_extras := feature_display=0 104endif 105 106# override lib.mk's default rules 107OVERRIDE_TARGETS := 1 108override define CLEAN 109 $(call msg,CLEAN) 110 $(Q)$(RM) -r $(TEST_GEN_PROGS) 111 $(Q)$(RM) -r $(TEST_GEN_PROGS_EXTENDED) 112 $(Q)$(RM) -r $(TEST_GEN_FILES) 113 $(Q)$(RM) -r $(EXTRA_CLEAN) 114 $(Q)$(MAKE) -C bpf_testmod clean 115 $(Q)$(MAKE) docs-clean 116endef 117 118include ../lib.mk 119 120SCRATCH_DIR := $(OUTPUT)/tools 121BUILD_DIR := $(SCRATCH_DIR)/build 122INCLUDE_DIR := $(SCRATCH_DIR)/include 123BPFOBJ := $(BUILD_DIR)/libbpf/libbpf.a 124ifneq ($(CROSS_COMPILE),) 125HOST_BUILD_DIR := $(BUILD_DIR)/host 126HOST_SCRATCH_DIR := $(OUTPUT)/host-tools 127HOST_INCLUDE_DIR := $(HOST_SCRATCH_DIR)/include 128else 129HOST_BUILD_DIR := $(BUILD_DIR) 130HOST_SCRATCH_DIR := $(SCRATCH_DIR) 131HOST_INCLUDE_DIR := $(INCLUDE_DIR) 132endif 133HOST_BPFOBJ := $(HOST_BUILD_DIR)/libbpf/libbpf.a 134RESOLVE_BTFIDS := $(HOST_BUILD_DIR)/resolve_btfids/resolve_btfids 135RUNQSLOWER_OUTPUT := $(BUILD_DIR)/runqslower/ 136 137VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \ 138 $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \ 139 ../../../../vmlinux \ 140 /sys/kernel/btf/vmlinux \ 141 /boot/vmlinux-$(shell uname -r) 142VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS)))) 143ifeq ($(VMLINUX_BTF),) 144$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)") 145endif 146 147# Define simple and short `make test_progs`, `make test_sysctl`, etc targets 148# to build individual tests. 149# NOTE: Semicolon at the end is critical to override lib.mk's default static 150# rule for binaries. 151$(notdir $(TEST_GEN_PROGS) \ 152 $(TEST_PROGS) \ 153 $(TEST_PROGS_EXTENDED) \ 154 $(TEST_GEN_PROGS_EXTENDED) \ 155 $(TEST_CUSTOM_PROGS)): %: $(OUTPUT)/% ; 156 157# sort removes libbpf duplicates when not cross-building 158MAKE_DIRS := $(sort $(BUILD_DIR)/libbpf $(HOST_BUILD_DIR)/libbpf \ 159 $(HOST_BUILD_DIR)/bpftool $(HOST_BUILD_DIR)/resolve_btfids \ 160 $(RUNQSLOWER_OUTPUT) $(INCLUDE_DIR)) 161$(MAKE_DIRS): 162 $(call msg,MKDIR,,$@) 163 $(Q)mkdir -p $@ 164 165$(OUTPUT)/%.o: %.c 166 $(call msg,CC,,$@) 167 $(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@ 168 169$(OUTPUT)/%:%.c 170 $(call msg,BINARY,,$@) 171 $(Q)$(LINK.c) $^ $(LDLIBS) -o $@ 172 173# LLVM's ld.lld doesn't support all the architectures, so use it only on x86 174ifeq ($(SRCARCH),x86) 175LLD := lld 176else 177LLD := ld 178endif 179 180# Filter out -static for liburandom_read.so and its dependent targets so that static builds 181# do not fail. Static builds leave urandom_read relying on system-wide shared libraries. 182$(OUTPUT)/liburandom_read.so: urandom_read_lib1.c urandom_read_lib2.c 183 $(call msg,LIB,,$@) 184 $(Q)$(CLANG) $(filter-out -static,$(CFLAGS) $(LDFLAGS)) $^ $(LDLIBS) \ 185 -fuse-ld=$(LLD) -Wl,-znoseparate-code -fPIC -shared -o $@ 186 187$(OUTPUT)/urandom_read: urandom_read.c urandom_read_aux.c $(OUTPUT)/liburandom_read.so 188 $(call msg,BINARY,,$@) 189 $(Q)$(CLANG) $(filter-out -static,$(CFLAGS) $(LDFLAGS)) $(filter %.c,$^) \ 190 liburandom_read.so $(LDLIBS) \ 191 -fuse-ld=$(LLD) -Wl,-znoseparate-code \ 192 -Wl,-rpath=. -Wl,--build-id=sha1 -o $@ 193 194$(OUTPUT)/sign-file: ../../../../scripts/sign-file.c 195 $(call msg,SIGN-FILE,,$@) 196 $(Q)$(CC) $(shell $(HOSTPKG_CONFIG)--cflags libcrypto 2> /dev/null) \ 197 $< -o $@ \ 198 $(shell $(HOSTPKG_CONFIG) --libs libcrypto 2> /dev/null || echo -lcrypto) 199 200$(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(wildcard bpf_testmod/Makefile bpf_testmod/*.[ch]) 201 $(call msg,MOD,,$@) 202 $(Q)$(RM) bpf_testmod/bpf_testmod.ko # force re-compilation 203 $(Q)$(MAKE) $(submake_extras) -C bpf_testmod 204 $(Q)cp bpf_testmod/bpf_testmod.ko $@ 205 206DEFAULT_BPFTOOL := $(HOST_SCRATCH_DIR)/sbin/bpftool 207 208$(OUTPUT)/runqslower: $(BPFOBJ) | $(DEFAULT_BPFTOOL) $(RUNQSLOWER_OUTPUT) 209 $(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/runqslower \ 210 OUTPUT=$(RUNQSLOWER_OUTPUT) VMLINUX_BTF=$(VMLINUX_BTF) \ 211 BPFTOOL_OUTPUT=$(HOST_BUILD_DIR)/bpftool/ \ 212 BPFOBJ_OUTPUT=$(BUILD_DIR)/libbpf \ 213 BPFOBJ=$(BPFOBJ) BPF_INCLUDE=$(INCLUDE_DIR) && \ 214 cp $(RUNQSLOWER_OUTPUT)runqslower $@ 215 216TEST_GEN_PROGS_EXTENDED += $(DEFAULT_BPFTOOL) 217 218$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(BPFOBJ) 219 220CGROUP_HELPERS := $(OUTPUT)/cgroup_helpers.o 221TESTING_HELPERS := $(OUTPUT)/testing_helpers.o 222TRACE_HELPERS := $(OUTPUT)/trace_helpers.o 223CAP_HELPERS := $(OUTPUT)/cap_helpers.o 224 225$(OUTPUT)/test_dev_cgroup: $(CGROUP_HELPERS) $(TESTING_HELPERS) 226$(OUTPUT)/test_skb_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) 227$(OUTPUT)/test_sock: $(CGROUP_HELPERS) $(TESTING_HELPERS) 228$(OUTPUT)/test_sock_addr: $(CGROUP_HELPERS) $(TESTING_HELPERS) 229$(OUTPUT)/test_sockmap: $(CGROUP_HELPERS) $(TESTING_HELPERS) 230$(OUTPUT)/test_tcpnotify_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(TRACE_HELPERS) 231$(OUTPUT)/get_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) 232$(OUTPUT)/test_cgroup_storage: $(CGROUP_HELPERS) $(TESTING_HELPERS) 233$(OUTPUT)/test_sock_fields: $(CGROUP_HELPERS) $(TESTING_HELPERS) 234$(OUTPUT)/test_sysctl: $(CGROUP_HELPERS) $(TESTING_HELPERS) 235$(OUTPUT)/test_tag: $(TESTING_HELPERS) 236$(OUTPUT)/test_lirc_mode2_user: $(TESTING_HELPERS) 237$(OUTPUT)/xdping: $(TESTING_HELPERS) 238$(OUTPUT)/flow_dissector_load: $(TESTING_HELPERS) 239$(OUTPUT)/test_maps: $(TESTING_HELPERS) 240$(OUTPUT)/test_verifier: $(TESTING_HELPERS) $(CAP_HELPERS) 241$(OUTPUT)/xsk.o: $(BPFOBJ) 242$(OUTPUT)/xskxceiver: $(OUTPUT)/xsk.o 243 244BPFTOOL ?= $(DEFAULT_BPFTOOL) 245$(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) \ 246 $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/bpftool 247 $(Q)$(MAKE) $(submake_extras) -C $(BPFTOOLDIR) \ 248 ARCH= CROSS_COMPILE= CC=$(HOSTCC) LD=$(HOSTLD) \ 249 EXTRA_CFLAGS='-g -O0' \ 250 OUTPUT=$(HOST_BUILD_DIR)/bpftool/ \ 251 LIBBPF_OUTPUT=$(HOST_BUILD_DIR)/libbpf/ \ 252 LIBBPF_DESTDIR=$(HOST_SCRATCH_DIR)/ \ 253 prefix= DESTDIR=$(HOST_SCRATCH_DIR)/ install-bin 254 255all: docs 256 257docs: 258 $(Q)RST2MAN_OPTS="--exit-status=1" $(MAKE) $(submake_extras) \ 259 -f Makefile.docs \ 260 prefix= OUTPUT=$(OUTPUT)/ DESTDIR=$(OUTPUT)/ $@ 261 262docs-clean: 263 $(Q)$(MAKE) $(submake_extras) \ 264 -f Makefile.docs \ 265 prefix= OUTPUT=$(OUTPUT)/ DESTDIR=$(OUTPUT)/ $@ 266 267$(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile) \ 268 $(APIDIR)/linux/bpf.h \ 269 | $(BUILD_DIR)/libbpf 270 $(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(BUILD_DIR)/libbpf/ \ 271 EXTRA_CFLAGS='-g -O0' \ 272 DESTDIR=$(SCRATCH_DIR) prefix= all install_headers 273 274ifneq ($(BPFOBJ),$(HOST_BPFOBJ)) 275$(HOST_BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile) \ 276 $(APIDIR)/linux/bpf.h \ 277 | $(HOST_BUILD_DIR)/libbpf 278 $(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) \ 279 EXTRA_CFLAGS='-g -O0' ARCH= CROSS_COMPILE= \ 280 OUTPUT=$(HOST_BUILD_DIR)/libbpf/ CC=$(HOSTCC) LD=$(HOSTLD) \ 281 DESTDIR=$(HOST_SCRATCH_DIR)/ prefix= all install_headers 282endif 283 284$(INCLUDE_DIR)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL) | $(INCLUDE_DIR) 285ifeq ($(VMLINUX_H),) 286 $(call msg,GEN,,$@) 287 $(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@ 288else 289 $(call msg,CP,,$@) 290 $(Q)cp "$(VMLINUX_H)" $@ 291endif 292 293$(RESOLVE_BTFIDS): $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/resolve_btfids \ 294 $(TOOLSDIR)/bpf/resolve_btfids/main.c \ 295 $(TOOLSDIR)/lib/rbtree.c \ 296 $(TOOLSDIR)/lib/zalloc.c \ 297 $(TOOLSDIR)/lib/string.c \ 298 $(TOOLSDIR)/lib/ctype.c \ 299 $(TOOLSDIR)/lib/str_error_r.c 300 $(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/resolve_btfids \ 301 CC=$(HOSTCC) LD=$(HOSTLD) AR=$(HOSTAR) \ 302 LIBBPF_INCLUDE=$(HOST_INCLUDE_DIR) \ 303 OUTPUT=$(HOST_BUILD_DIR)/resolve_btfids/ BPFOBJ=$(HOST_BPFOBJ) 304 305# Get Clang's default includes on this system, as opposed to those seen by 306# '-target bpf'. This fixes "missing" files on some architectures/distros, 307# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc. 308# 309# Use '-idirafter': Don't interfere with include mechanics except where the 310# build would have failed anyways. 311define get_sys_includes 312$(shell $(1) -v -E - </dev/null 2>&1 \ 313 | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \ 314$(shell $(1) -dM -E - </dev/null | grep '__riscv_xlen ' | awk '{printf("-D__riscv_xlen=%d -D__BITS_PER_LONG=%d", $$3, $$3)}') 315endef 316 317# Determine target endianness. 318IS_LITTLE_ENDIAN = $(shell $(CC) -dM -E - </dev/null | \ 319 grep 'define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__') 320MENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian) 321 322CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG)) 323BPF_CFLAGS = -g -Werror -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN) \ 324 -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR) \ 325 -I$(abspath $(OUTPUT)/../usr/include) 326 327CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \ 328 -Wno-compare-distinct-pointer-types 329 330$(OUTPUT)/test_l4lb_noinline.o: BPF_CFLAGS += -fno-inline 331$(OUTPUT)/test_xdp_noinline.o: BPF_CFLAGS += -fno-inline 332 333$(OUTPUT)/flow_dissector_load.o: flow_dissector_load.h 334$(OUTPUT)/cgroup_getset_retval_hooks.o: cgroup_getset_retval_hooks.h 335 336# Build BPF object using Clang 337# $1 - input .c file 338# $2 - output .o file 339# $3 - CFLAGS 340define CLANG_BPF_BUILD_RULE 341 $(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2) 342 $(Q)$(CLANG) $3 -O2 -target bpf -c $1 -mcpu=v3 -o $2 343endef 344# Similar to CLANG_BPF_BUILD_RULE, but with disabled alu32 345define CLANG_NOALU32_BPF_BUILD_RULE 346 $(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2) 347 $(Q)$(CLANG) $3 -O2 -target bpf -c $1 -mcpu=v2 -o $2 348endef 349# Build BPF object using GCC 350define GCC_BPF_BUILD_RULE 351 $(call msg,GCC-BPF,$(TRUNNER_BINARY),$2) 352 $(Q)$(BPF_GCC) $3 -O2 -c $1 -o $2 353endef 354 355SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c 356 357LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \ 358 linked_vars.skel.h linked_maps.skel.h \ 359 test_subskeleton.skel.h test_subskeleton_lib.skel.h \ 360 test_usdt.skel.h 361 362LSKELS := fentry_test.c fexit_test.c fexit_sleep.c atomics.c \ 363 trace_printk.c trace_vprintk.c map_ptr_kern.c \ 364 core_kern.c core_kern_overflow.c test_ringbuf.c \ 365 test_ringbuf_map_key.c 366 367# Generate both light skeleton and libbpf skeleton for these 368LSKELS_EXTRA := test_ksyms_module.c test_ksyms_weak.c kfunc_call_test.c \ 369 kfunc_call_test_subprog.c 370SKEL_BLACKLIST += $$(LSKELS) 371 372test_static_linked.skel.h-deps := test_static_linked1.bpf.o test_static_linked2.bpf.o 373linked_funcs.skel.h-deps := linked_funcs1.bpf.o linked_funcs2.bpf.o 374linked_vars.skel.h-deps := linked_vars1.bpf.o linked_vars2.bpf.o 375linked_maps.skel.h-deps := linked_maps1.bpf.o linked_maps2.bpf.o 376# In the subskeleton case, we want the test_subskeleton_lib.subskel.h file 377# but that's created as a side-effect of the skel.h generation. 378test_subskeleton.skel.h-deps := test_subskeleton_lib2.bpf.o test_subskeleton_lib.bpf.o test_subskeleton.bpf.o 379test_subskeleton_lib.skel.h-deps := test_subskeleton_lib2.bpf.o test_subskeleton_lib.bpf.o 380test_usdt.skel.h-deps := test_usdt.bpf.o test_usdt_multispec.bpf.o 381 382LINKED_BPF_SRCS := $(patsubst %.bpf.o,%.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps))) 383 384# Set up extra TRUNNER_XXX "temporary" variables in the environment (relies on 385# $eval()) and pass control to DEFINE_TEST_RUNNER_RULES. 386# Parameters: 387# $1 - test runner base binary name (e.g., test_progs) 388# $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc) 389define DEFINE_TEST_RUNNER 390 391TRUNNER_OUTPUT := $(OUTPUT)$(if $2,/)$2 392TRUNNER_BINARY := $1$(if $2,-)$2 393TRUNNER_TEST_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.test.o, \ 394 $$(notdir $$(wildcard $(TRUNNER_TESTS_DIR)/*.c))) 395TRUNNER_EXTRA_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, \ 396 $$(filter %.c,$(TRUNNER_EXTRA_SOURCES))) 397TRUNNER_EXTRA_HDRS := $$(filter %.h,$(TRUNNER_EXTRA_SOURCES)) 398TRUNNER_TESTS_HDR := $(TRUNNER_TESTS_DIR)/tests.h 399TRUNNER_BPF_SRCS := $$(notdir $$(wildcard $(TRUNNER_BPF_PROGS_DIR)/*.c)) 400TRUNNER_BPF_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.bpf.o, $$(TRUNNER_BPF_SRCS)) 401TRUNNER_BPF_SKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.skel.h, \ 402 $$(filter-out $(SKEL_BLACKLIST) $(LINKED_BPF_SRCS),\ 403 $$(TRUNNER_BPF_SRCS))) 404TRUNNER_BPF_LSKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.lskel.h, $$(LSKELS) $$(LSKELS_EXTRA)) 405TRUNNER_BPF_SKELS_LINKED := $$(addprefix $$(TRUNNER_OUTPUT)/,$(LINKED_SKELS)) 406TEST_GEN_FILES += $$(TRUNNER_BPF_OBJS) 407 408# Evaluate rules now with extra TRUNNER_XXX variables above already defined 409$$(eval $$(call DEFINE_TEST_RUNNER_RULES,$1,$2)) 410 411endef 412 413# Using TRUNNER_XXX variables, provided by callers of DEFINE_TEST_RUNNER and 414# set up by DEFINE_TEST_RUNNER itself, create test runner build rules with: 415# $1 - test runner base binary name (e.g., test_progs) 416# $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc) 417define DEFINE_TEST_RUNNER_RULES 418 419ifeq ($($(TRUNNER_OUTPUT)-dir),) 420$(TRUNNER_OUTPUT)-dir := y 421$(TRUNNER_OUTPUT): 422 $$(call msg,MKDIR,,$$@) 423 $(Q)mkdir -p $$@ 424endif 425 426# ensure we set up BPF objects generation rule just once for a given 427# input/output directory combination 428ifeq ($($(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs),) 429$(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs := y 430$(TRUNNER_BPF_OBJS): $(TRUNNER_OUTPUT)/%.bpf.o: \ 431 $(TRUNNER_BPF_PROGS_DIR)/%.c \ 432 $(TRUNNER_BPF_PROGS_DIR)/*.h \ 433 $$(INCLUDE_DIR)/vmlinux.h \ 434 $(wildcard $(BPFDIR)/bpf_*.h) \ 435 $(wildcard $(BPFDIR)/*.bpf.h) \ 436 | $(TRUNNER_OUTPUT) $$(BPFOBJ) 437 $$(call $(TRUNNER_BPF_BUILD_RULE),$$<,$$@, \ 438 $(TRUNNER_BPF_CFLAGS)) 439 440$(TRUNNER_BPF_SKELS): %.skel.h: %.bpf.o $(BPFTOOL) | $(TRUNNER_OUTPUT) 441 $$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@) 442 $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked1.o) $$< 443 $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked2.o) $$(<:.o=.linked1.o) 444 $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked3.o) $$(<:.o=.linked2.o) 445 $(Q)diff $$(<:.o=.linked2.o) $$(<:.o=.linked3.o) 446 $(Q)$$(BPFTOOL) gen skeleton $$(<:.o=.linked3.o) name $$(notdir $$(<:.bpf.o=)) > $$@ 447 $(Q)$$(BPFTOOL) gen subskeleton $$(<:.o=.linked3.o) name $$(notdir $$(<:.bpf.o=)) > $$(@:.skel.h=.subskel.h) 448 449$(TRUNNER_BPF_LSKELS): %.lskel.h: %.bpf.o $(BPFTOOL) | $(TRUNNER_OUTPUT) 450 $$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@) 451 $(Q)$$(BPFTOOL) gen object $$(<:.o=.llinked1.o) $$< 452 $(Q)$$(BPFTOOL) gen object $$(<:.o=.llinked2.o) $$(<:.o=.llinked1.o) 453 $(Q)$$(BPFTOOL) gen object $$(<:.o=.llinked3.o) $$(<:.o=.llinked2.o) 454 $(Q)diff $$(<:.o=.llinked2.o) $$(<:.o=.llinked3.o) 455 $(Q)$$(BPFTOOL) gen skeleton -L $$(<:.o=.llinked3.o) name $$(notdir $$(<:.bpf.o=_lskel)) > $$@ 456 457$(TRUNNER_BPF_SKELS_LINKED): $(TRUNNER_BPF_OBJS) $(BPFTOOL) | $(TRUNNER_OUTPUT) 458 $$(call msg,LINK-BPF,$(TRUNNER_BINARY),$$(@:.skel.h=.bpf.o)) 459 $(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked1.o) $$(addprefix $(TRUNNER_OUTPUT)/,$$($$(@F)-deps)) 460 $(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked1.o) 461 $(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked3.o) $$(@:.skel.h=.linked2.o) 462 $(Q)diff $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked3.o) 463 $$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@) 464 $(Q)$$(BPFTOOL) gen skeleton $$(@:.skel.h=.linked3.o) name $$(notdir $$(@:.skel.h=)) > $$@ 465 $(Q)$$(BPFTOOL) gen subskeleton $$(@:.skel.h=.linked3.o) name $$(notdir $$(@:.skel.h=)) > $$(@:.skel.h=.subskel.h) 466endif 467 468# ensure we set up tests.h header generation rule just once 469ifeq ($($(TRUNNER_TESTS_DIR)-tests-hdr),) 470$(TRUNNER_TESTS_DIR)-tests-hdr := y 471$(TRUNNER_TESTS_HDR): $(TRUNNER_TESTS_DIR)/*.c 472 $$(call msg,TEST-HDR,$(TRUNNER_BINARY),$$@) 473 $$(shell (echo '/* Generated header, do not edit */'; \ 474 sed -n -E 's/^void (serial_)?test_([a-zA-Z0-9_]+)\((void)?\).*/DEFINE_TEST(\2)/p' \ 475 $(TRUNNER_TESTS_DIR)/*.c | sort ; \ 476 ) > $$@) 477endif 478 479# compile individual test files 480# Note: we cd into output directory to ensure embedded BPF object is found 481$(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o: \ 482 $(TRUNNER_TESTS_DIR)/%.c \ 483 $(TRUNNER_EXTRA_HDRS) \ 484 $(TRUNNER_BPF_OBJS) \ 485 $(TRUNNER_BPF_SKELS) \ 486 $(TRUNNER_BPF_LSKELS) \ 487 $(TRUNNER_BPF_SKELS_LINKED) \ 488 $$(BPFOBJ) | $(TRUNNER_OUTPUT) 489 $$(call msg,TEST-OBJ,$(TRUNNER_BINARY),$$@) 490 $(Q)cd $$(@D) && $$(CC) -I. $$(CFLAGS) -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F) 491 492$(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o: \ 493 %.c \ 494 $(TRUNNER_EXTRA_HDRS) \ 495 $(TRUNNER_TESTS_HDR) \ 496 $$(BPFOBJ) | $(TRUNNER_OUTPUT) 497 $$(call msg,EXT-OBJ,$(TRUNNER_BINARY),$$@) 498 $(Q)$$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@ 499 500# non-flavored in-srctree builds receive special treatment, in particular, we 501# do not need to copy extra resources (see e.g. test_btf_dump_case()) 502$(TRUNNER_BINARY)-extras: $(TRUNNER_EXTRA_FILES) | $(TRUNNER_OUTPUT) 503ifneq ($2:$(OUTPUT),:$(shell pwd)) 504 $$(call msg,EXT-COPY,$(TRUNNER_BINARY),$(TRUNNER_EXTRA_FILES)) 505 $(Q)rsync -aq $$^ $(TRUNNER_OUTPUT)/ 506endif 507 508$(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS) \ 509 $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ) \ 510 $(RESOLVE_BTFIDS) \ 511 | $(TRUNNER_BINARY)-extras 512 $$(call msg,BINARY,,$$@) 513 $(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@ 514 $(Q)$(RESOLVE_BTFIDS) --btf $(TRUNNER_OUTPUT)/btf_data.bpf.o $$@ 515 $(Q)ln -sf $(if $2,..,.)/tools/build/bpftool/bootstrap/bpftool $(if $2,$2/)bpftool 516 517endef 518 519# Define test_progs test runner. 520TRUNNER_TESTS_DIR := prog_tests 521TRUNNER_BPF_PROGS_DIR := progs 522TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c \ 523 network_helpers.c testing_helpers.c \ 524 btf_helpers.c flow_dissector_load.h \ 525 cap_helpers.c 526TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko \ 527 $(OUTPUT)/liburandom_read.so \ 528 $(OUTPUT)/xdp_synproxy \ 529 $(OUTPUT)/sign-file \ 530 ima_setup.sh verify_sig_setup.sh \ 531 $(wildcard progs/btf_dump_test_case_*.c) 532TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE 533TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS) -DENABLE_ATOMICS_TESTS 534$(eval $(call DEFINE_TEST_RUNNER,test_progs)) 535 536# Define test_progs-no_alu32 test runner. 537TRUNNER_BPF_BUILD_RULE := CLANG_NOALU32_BPF_BUILD_RULE 538TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS) 539$(eval $(call DEFINE_TEST_RUNNER,test_progs,no_alu32)) 540 541# Define test_progs BPF-GCC-flavored test runner. 542ifneq ($(BPF_GCC),) 543TRUNNER_BPF_BUILD_RULE := GCC_BPF_BUILD_RULE 544TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(call get_sys_includes,gcc) 545$(eval $(call DEFINE_TEST_RUNNER,test_progs,bpf_gcc)) 546endif 547 548# Define test_maps test runner. 549TRUNNER_TESTS_DIR := map_tests 550TRUNNER_BPF_PROGS_DIR := progs 551TRUNNER_EXTRA_SOURCES := test_maps.c 552TRUNNER_EXTRA_FILES := 553TRUNNER_BPF_BUILD_RULE := $$(error no BPF objects should be built) 554TRUNNER_BPF_CFLAGS := 555$(eval $(call DEFINE_TEST_RUNNER,test_maps)) 556 557# Define test_verifier test runner. 558# It is much simpler than test_maps/test_progs and sufficiently different from 559# them (e.g., test.h is using completely pattern), that it's worth just 560# explicitly defining all the rules explicitly. 561verifier/tests.h: verifier/*.c 562 $(shell ( cd verifier/; \ 563 echo '/* Generated header, do not edit */'; \ 564 echo '#ifdef FILL_ARRAY'; \ 565 ls *.c 2> /dev/null | sed -e 's@\(.*\)@#include \"\1\"@'; \ 566 echo '#endif' \ 567 ) > verifier/tests.h) 568$(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT) 569 $(call msg,BINARY,,$@) 570 $(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@ 571 572# Make sure we are able to include and link libbpf against c++. 573$(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ) 574 $(call msg,CXX,,$@) 575 $(Q)$(CXX) $(CFLAGS) $(filter %.a %.o %.cpp,$^) $(LDLIBS) -o $@ 576 577# Benchmark runner 578$(OUTPUT)/bench_%.o: benchs/bench_%.c bench.h $(BPFOBJ) 579 $(call msg,CC,,$@) 580 $(Q)$(CC) $(CFLAGS) -O2 -c $(filter %.c,$^) $(LDLIBS) -o $@ 581$(OUTPUT)/bench_rename.o: $(OUTPUT)/test_overhead.skel.h 582$(OUTPUT)/bench_trigger.o: $(OUTPUT)/trigger_bench.skel.h 583$(OUTPUT)/bench_ringbufs.o: $(OUTPUT)/ringbuf_bench.skel.h \ 584 $(OUTPUT)/perfbuf_bench.skel.h 585$(OUTPUT)/bench_bloom_filter_map.o: $(OUTPUT)/bloom_filter_bench.skel.h 586$(OUTPUT)/bench_bpf_loop.o: $(OUTPUT)/bpf_loop_bench.skel.h 587$(OUTPUT)/bench_strncmp.o: $(OUTPUT)/strncmp_bench.skel.h 588$(OUTPUT)/bench_bpf_hashmap_full_update.o: $(OUTPUT)/bpf_hashmap_full_update_bench.skel.h 589$(OUTPUT)/bench_local_storage.o: $(OUTPUT)/local_storage_bench.skel.h 590$(OUTPUT)/bench_local_storage_rcu_tasks_trace.o: $(OUTPUT)/local_storage_rcu_tasks_trace_bench.skel.h 591$(OUTPUT)/bench.o: bench.h testing_helpers.h $(BPFOBJ) 592$(OUTPUT)/bench: LDLIBS += -lm 593$(OUTPUT)/bench: $(OUTPUT)/bench.o \ 594 $(TESTING_HELPERS) \ 595 $(TRACE_HELPERS) \ 596 $(OUTPUT)/bench_count.o \ 597 $(OUTPUT)/bench_rename.o \ 598 $(OUTPUT)/bench_trigger.o \ 599 $(OUTPUT)/bench_ringbufs.o \ 600 $(OUTPUT)/bench_bloom_filter_map.o \ 601 $(OUTPUT)/bench_bpf_loop.o \ 602 $(OUTPUT)/bench_strncmp.o \ 603 $(OUTPUT)/bench_bpf_hashmap_full_update.o \ 604 $(OUTPUT)/bench_local_storage.o \ 605 $(OUTPUT)/bench_local_storage_rcu_tasks_trace.o 606 $(call msg,BINARY,,$@) 607 $(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(filter %.a %.o,$^) $(LDLIBS) -o $@ 608 609$(OUTPUT)/veristat.o: $(BPFOBJ) 610$(OUTPUT)/veristat: $(OUTPUT)/veristat.o 611 $(call msg,BINARY,,$@) 612 $(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(filter %.a %.o,$^) $(LDLIBS) -o $@ 613 614EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(SCRATCH_DIR) $(HOST_SCRATCH_DIR) \ 615 prog_tests/tests.h map_tests/tests.h verifier/tests.h \ 616 feature bpftool \ 617 $(addprefix $(OUTPUT)/,*.o *.skel.h *.lskel.h *.subskel.h \ 618 no_alu32 bpf_gcc bpf_testmod.ko \ 619 liburandom_read.so) 620 621.PHONY: docs docs-clean 622