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