1# SPDX-License-Identifier: GPL-2.0 2include ../../../build/Build.include 3include ../../../scripts/Makefile.arch 4include ../../../scripts/Makefile.include 5 6CXX ?= $(CROSS_COMPILE)g++ 7OBJCOPY ?= $(CROSS_COMPILE)objcopy 8 9CURDIR := $(abspath .) 10TOOLSDIR := $(abspath ../../..) 11LIBDIR := $(TOOLSDIR)/lib 12BPFDIR := $(LIBDIR)/bpf 13TOOLSINCDIR := $(TOOLSDIR)/include 14TOOLSARCHINCDIR := $(TOOLSDIR)/arch/$(SRCARCH)/include 15BPFTOOLDIR := $(TOOLSDIR)/bpf/bpftool 16APIDIR := $(TOOLSINCDIR)/uapi 17ifneq ($(O),) 18GENDIR := $(O)/include/generated 19else 20GENDIR := $(abspath ../../../../include/generated) 21endif 22GENHDR := $(GENDIR)/autoconf.h 23PKG_CONFIG ?= $(CROSS_COMPILE)pkg-config 24 25ifneq ($(wildcard $(GENHDR)),) 26 GENFLAGS := -DHAVE_GENHDR 27endif 28 29BPF_GCC ?= $(shell command -v bpf-gcc;) 30ifdef ASAN 31SAN_CFLAGS ?= -fsanitize=address -fno-omit-frame-pointer 32else 33SAN_CFLAGS ?= 34endif 35SAN_LDFLAGS ?= $(SAN_CFLAGS) 36RELEASE ?= 37OPT_FLAGS ?= $(if $(RELEASE),-O2,-O0) 38 39LIBELF_CFLAGS := $(shell $(PKG_CONFIG) libelf --cflags 2>/dev/null) 40LIBELF_LIBS := $(shell $(PKG_CONFIG) libelf --libs 2>/dev/null || echo -lelf) 41 42SKIP_DOCS ?= 43SKIP_LLVM ?= 44SKIP_LIBBFD ?= 45SKIP_CRYPTO ?= 46 47# When BPF_STRICT_BUILD is 1, any BPF object, skeleton, test object, or 48# benchmark compilation failure is fatal. Set to 0 to tolerate failures 49# and continue building the remaining tests. 50BPF_STRICT_BUILD ?= 1 51PERMISSIVE := $(filter 0,$(BPF_STRICT_BUILD)) 52 53ifeq ($(srctree),) 54srctree := $(patsubst %/,%,$(dir $(CURDIR))) 55srctree := $(patsubst %/,%,$(dir $(srctree))) 56srctree := $(patsubst %/,%,$(dir $(srctree))) 57srctree := $(patsubst %/,%,$(dir $(srctree))) 58endif 59 60CFLAGS += -g $(OPT_FLAGS) -rdynamic -std=gnu11 \ 61 -Wall -Werror -fno-omit-frame-pointer \ 62 -Wno-unused-but-set-variable \ 63 $(GENFLAGS) $(SAN_CFLAGS) $(LIBELF_CFLAGS) \ 64 -I$(CURDIR) -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR) \ 65 -I$(TOOLSINCDIR) -I$(TOOLSARCHINCDIR) -I$(APIDIR) -I$(OUTPUT) \ 66 -I$(CURDIR)/libarena/include 67LDFLAGS += $(SAN_LDFLAGS) 68LDLIBS += $(LIBELF_LIBS) -lz -lrt -lpthread 69 70PCAP_CFLAGS := $(shell $(PKG_CONFIG) --cflags libpcap 2>/dev/null && echo "-DTRAFFIC_MONITOR=1") 71PCAP_LIBS := $(shell $(PKG_CONFIG) --libs libpcap 2>/dev/null) 72LDLIBS += $(PCAP_LIBS) 73CFLAGS += $(PCAP_CFLAGS) 74 75# Some utility functions use LLVM libraries 76jit_disasm_helpers.c-CFLAGS = $(LLVM_CFLAGS) 77 78ifneq ($(LLVM),) 79# Silence some warnings when compiled with clang 80CFLAGS += -Wno-unused-command-line-argument 81endif 82 83# Check whether bpf cpu=v4 is supported or not by clang 84ifneq ($(shell $(CLANG) --target=bpf -mcpu=help 2>&1 | grep 'v4'),) 85CLANG_CPUV4 := 1 86endif 87 88# Check whether clang supports BPF address sanitizer (requires LLVM 22+) 89CLANG_HAS_ARENA_ASAN := $(shell echo 'int x;' | \ 90 $(CLANG) --target=bpf -fsanitize=kernel-address \ 91 -mllvm -asan-shadow-addr-space=1 \ 92 -x c -c - -o /dev/null 2>/dev/null && echo 1) 93 94# Order correspond to 'make run_tests' order 95TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_progs \ 96 test_sockmap \ 97 test_tcpnotify_user \ 98 test_progs-no_alu32 99TEST_INST_SUBDIRS := no_alu32 100 101# Also test bpf-gcc, if present 102ifneq ($(BPF_GCC),) 103TEST_GEN_PROGS += test_progs-bpf_gcc 104TEST_INST_SUBDIRS += bpf_gcc 105 106# The following tests contain C code that, although technically legal, 107# triggers GCC warnings that cannot be disabled: declaration of 108# anonymous struct types in function parameter lists. 109progs/btf_dump_test_case_bitfields.c-bpf_gcc-CFLAGS := -Wno-error 110progs/btf_dump_test_case_namespacing.c-bpf_gcc-CFLAGS := -Wno-error 111progs/btf_dump_test_case_packing.c-bpf_gcc-CFLAGS := -Wno-error 112progs/btf_dump_test_case_padding.c-bpf_gcc-CFLAGS := -Wno-error 113progs/btf_dump_test_case_syntax.c-bpf_gcc-CFLAGS := -Wno-error 114 115endif 116 117ifneq ($(CLANG_CPUV4),) 118TEST_GEN_PROGS += test_progs-cpuv4 119TEST_INST_SUBDIRS += cpuv4 120endif 121 122TEST_FILES = xsk_prereqs.sh $(wildcard progs/btf_dump_test_case_*.c) 123 124# Order correspond to 'make run_tests' order 125TEST_PROGS := test_kmod.sh \ 126 test_lirc_mode2.sh \ 127 test_bpftool_build.sh \ 128 test_doc_build.sh \ 129 test_xsk.sh \ 130 test_xdp_features.sh 131 132TEST_PROGS_EXTENDED := \ 133 ima_setup.sh verify_sig_setup.sh 134 135TEST_KMODS := bpf_testmod.ko bpf_test_no_cfi.ko bpf_test_modorder_x.ko \ 136 bpf_test_modorder_y.ko bpf_test_rqspinlock.ko 137TEST_KMOD_TARGETS = $(addprefix $(OUTPUT)/,$(TEST_KMODS)) 138 139# Compile but not part of 'make run_tests' 140TEST_GEN_PROGS_EXTENDED = \ 141 bench \ 142 flow_dissector_load \ 143 test_cpp \ 144 test_lirc_mode2_user \ 145 veristat \ 146 xdp_features \ 147 xdp_hw_metadata \ 148 xdp_synproxy \ 149 xskxceiver 150 151TEST_GEN_FILES += $(TEST_KMODS) liburandom_read.so urandom_read sign-file uprobe_multi 152 153ifneq ($(V),1) 154submake_extras := feature_display=0 155endif 156 157# override lib.mk's default rules 158OVERRIDE_TARGETS := 1 159override define CLEAN 160 $(call msg,CLEAN) 161 $(Q)$(RM) -r $(TEST_GEN_PROGS) 162 $(Q)$(RM) -r $(TEST_GEN_PROGS_EXTENDED) 163 $(Q)$(RM) -r $(TEST_GEN_FILES) 164 $(Q)$(RM) -r $(TEST_KMODS) 165 $(Q)$(RM) -r $(EXTRA_CLEAN) 166 $(Q)$(MAKE) -C test_kmods clean 167 $(Q)$(MAKE) -C libarena clean 168 $(Q)$(MAKE) docs-clean 169endef 170 171include ../lib.mk 172 173NON_CHECK_FEAT_TARGETS := clean docs-clean emit_tests 174CHECK_FEAT := $(filter-out $(NON_CHECK_FEAT_TARGETS),$(or $(MAKECMDGOALS), "none")) 175ifneq ($(CHECK_FEAT),) 176FEATURE_USER := .selftests 177FEATURE_TESTS := llvm 178FEATURE_DISPLAY := $(FEATURE_TESTS) 179 180# Makefile.feature expects OUTPUT to end with a slash 181ifeq ($(shell expr $(MAKE_VERSION) \>= 4.4), 1) 182$(let OUTPUT,$(OUTPUT)/,\ 183 $(eval include ../../../build/Makefile.feature)) 184else 185override OUTPUT := $(OUTPUT)/ 186$(eval include ../../../build/Makefile.feature) 187override OUTPUT := $(patsubst %/,%,$(OUTPUT)) 188endif 189endif 190 191ifneq ($(SKIP_LLVM),1) 192ifeq ($(feature-llvm),1) 193 LLVM_CFLAGS += -DHAVE_LLVM_SUPPORT 194 LLVM_CONFIG_LIB_COMPONENTS := mcdisassembler all-targets 195 # both llvm-config and lib.mk add -D_GNU_SOURCE, which ends up as conflict 196 LLVM_CFLAGS += $(filter-out -D_GNU_SOURCE,$(shell $(LLVM_CONFIG) --cflags)) 197 # Prefer linking statically if it's available, otherwise fallback to shared 198 ifeq ($(shell $(LLVM_CONFIG) --link-static --libs >/dev/null 2>&1 && echo static),static) 199 LLVM_LDLIBS += $(shell $(LLVM_CONFIG) --link-static --libs $(LLVM_CONFIG_LIB_COMPONENTS)) 200 LLVM_LDLIBS += $(filter-out -lxml2,$(shell $(LLVM_CONFIG) --link-static --system-libs $(LLVM_CONFIG_LIB_COMPONENTS))) 201 LLVM_LDLIBS += -lstdc++ 202 else 203 LLVM_LDLIBS += $(shell $(LLVM_CONFIG) --link-shared --libs $(LLVM_CONFIG_LIB_COMPONENTS)) 204 endif 205 LLVM_LDFLAGS += $(shell $(LLVM_CONFIG) --ldflags) 206endif 207endif 208 209SCRATCH_DIR := $(OUTPUT)/tools 210BUILD_DIR := $(SCRATCH_DIR)/build 211INCLUDE_DIR := $(SCRATCH_DIR)/include 212BPFOBJ := $(BUILD_DIR)/libbpf/libbpf.a 213ifneq ($(CROSS_COMPILE),) 214HOST_BUILD_DIR := $(BUILD_DIR)/host 215HOST_SCRATCH_DIR := $(OUTPUT)/host-tools 216HOST_INCLUDE_DIR := $(HOST_SCRATCH_DIR)/include 217else 218HOST_BUILD_DIR := $(BUILD_DIR) 219HOST_SCRATCH_DIR := $(SCRATCH_DIR) 220HOST_INCLUDE_DIR := $(INCLUDE_DIR) 221endif 222HOST_BPFOBJ := $(HOST_BUILD_DIR)/libbpf/libbpf.a 223RESOLVE_BTFIDS := $(HOST_BUILD_DIR)/resolve_btfids/resolve_btfids 224VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \ 225 $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \ 226 ../../../../vmlinux \ 227 /sys/kernel/btf/vmlinux \ 228 /boot/vmlinux-$(shell uname -r) 229VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS)))) 230ifeq ($(VMLINUX_BTF),) 231$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)") 232endif 233 234# Define simple and short `make test_progs`, `make test_maps`, etc targets 235# to build individual tests. 236# NOTE: Semicolon at the end is critical to override lib.mk's default static 237# rule for binaries. 238$(notdir $(TEST_GEN_PROGS) $(TEST_KMODS) \ 239 $(TEST_GEN_PROGS_EXTENDED)): %: $(OUTPUT)/% ; 240 241# sort removes libbpf duplicates when not cross-building 242MAKE_DIRS := $(sort $(BUILD_DIR)/libbpf $(HOST_BUILD_DIR)/libbpf \ 243 $(BUILD_DIR)/bpftool $(HOST_BUILD_DIR)/bpftool \ 244 $(HOST_BUILD_DIR)/resolve_btfids \ 245 $(INCLUDE_DIR)) 246$(MAKE_DIRS): 247 $(call msg,MKDIR,,$@) 248 $(Q)mkdir -p $@ 249 250$(OUTPUT)/%.o: %.c 251 $(call msg,CC,,$@) 252 $(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@ 253 254$(OUTPUT)/%:%.c 255 $(call msg,BINARY,,$@) 256 $(Q)$(LINK.c) $^ $(LDLIBS) -o $@ 257 258# LLVM's ld.lld doesn't support all the architectures, so use it only on x86 259ifeq ($(SRCARCH),$(filter $(SRCARCH),x86 riscv)) 260LLD := lld 261else 262LLD := $(shell command -v $(LD)) 263endif 264 265# Filter out -static for liburandom_read.so and its dependent targets so that static builds 266# do not fail. Static builds leave urandom_read relying on system-wide shared libraries. 267$(OUTPUT)/liburandom_read.so: urandom_read_lib1.c urandom_read_lib2.c liburandom_read.map 268 $(call msg,LIB,,$@) 269 $(Q)$(CLANG) $(CLANG_TARGET_ARCH) \ 270 $(filter-out -static,$(CFLAGS) $(LDFLAGS)) \ 271 $(filter %.c,$^) $(filter-out -static,$(LDLIBS)) \ 272 -Wno-unused-command-line-argument \ 273 -fuse-ld=$(LLD) -Wl,-znoseparate-code -Wl,--build-id=sha1 \ 274 -Wl,--version-script=liburandom_read.map \ 275 -fPIC -shared -o $@ 276 277$(OUTPUT)/urandom_read: urandom_read.c urandom_read_aux.c $(OUTPUT)/liburandom_read.so 278 $(call msg,BINARY,,$@) 279 $(Q)$(CLANG) $(CLANG_TARGET_ARCH) \ 280 $(filter-out -static,$(CFLAGS) $(LDFLAGS)) $(filter %.c,$^) \ 281 -Wno-unused-command-line-argument \ 282 -lurandom_read $(filter-out -static,$(LDLIBS)) -L$(OUTPUT) \ 283 -fuse-ld=$(LLD) -Wl,-znoseparate-code -Wl,--build-id=sha1 \ 284 -Wl,-rpath=. -o $@ 285 286$(OUTPUT)/sign-file: ../../../../scripts/sign-file.c 287 $(call msg,SIGN-FILE,,$@) 288 $(Q)$(CC) $(shell $(PKG_CONFIG) --cflags libcrypto 2> /dev/null) \ 289 -I$(srctree)/tools/include/uapi/ \ 290 $< -o $@ \ 291 $(shell $(PKG_CONFIG) --libs libcrypto 2> /dev/null || echo -lcrypto) 292 293# This should really be a grouped target, but make versions before 4.3 don't 294# support that for regular rules. However, pattern matching rules are implicitly 295# treated as grouped even with older versions of make, so as a workaround, the 296# subst() turns the rule into a pattern matching rule 297$(addprefix test_kmods/,$(subst .ko,%ko,$(TEST_KMODS))): $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard test_kmods/Makefile test_kmods/*.[ch]) 298 $(Q)$(RM) test_kmods/*.ko test_kmods/*.mod.o # force re-compilation 299 $(Q)$(MAKE) $(submake_extras) -C test_kmods \ 300 $(if $(O),O=$(abspath $(O))) \ 301 $(if $(KBUILD_OUTPUT),KBUILD_OUTPUT=$(abspath $(KBUILD_OUTPUT)))\ 302 RESOLVE_BTFIDS=$(RESOLVE_BTFIDS) \ 303 EXTRA_CFLAGS='' EXTRA_LDFLAGS='' 304 305$(TEST_KMOD_TARGETS): $(addprefix test_kmods/,$(TEST_KMODS)) 306 $(call msg,MOD,,$@) 307 $(Q)$(if $(PERMISSIVE),if [ -f test_kmods/$(@F) ]; then )cp test_kmods/$(@F) $@$(if $(PERMISSIVE),; fi) 308 309 310DEFAULT_BPFTOOL := $(HOST_SCRATCH_DIR)/sbin/bpftool 311ifneq ($(CROSS_COMPILE),) 312CROSS_BPFTOOL := $(SCRATCH_DIR)/sbin/bpftool 313TRUNNER_BPFTOOL := $(CROSS_BPFTOOL) 314USE_BOOTSTRAP := "" 315else 316TRUNNER_BPFTOOL := $(DEFAULT_BPFTOOL) 317USE_BOOTSTRAP := "bootstrap/" 318endif 319 320TEST_GEN_PROGS_EXTENDED += $(TRUNNER_BPFTOOL) 321 322$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(BPFOBJ) 323 324TESTING_HELPERS := $(OUTPUT)/testing_helpers.o 325CGROUP_HELPERS := $(OUTPUT)/cgroup_helpers.o 326UNPRIV_HELPERS := $(OUTPUT)/unpriv_helpers.o 327TRACE_HELPERS := $(OUTPUT)/trace_helpers.o 328JSON_WRITER := $(OUTPUT)/json_writer.o 329CAP_HELPERS := $(OUTPUT)/cap_helpers.o 330NETWORK_HELPERS := $(OUTPUT)/network_helpers.o 331 332$(OUTPUT)/test_sockmap: $(CGROUP_HELPERS) $(TESTING_HELPERS) 333$(OUTPUT)/test_tcpnotify_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(TRACE_HELPERS) 334$(OUTPUT)/test_sock_fields: $(CGROUP_HELPERS) $(TESTING_HELPERS) 335$(OUTPUT)/test_tag: $(TESTING_HELPERS) 336$(OUTPUT)/test_lirc_mode2_user: $(TESTING_HELPERS) 337$(OUTPUT)/flow_dissector_load: $(TESTING_HELPERS) 338$(OUTPUT)/test_maps: $(TESTING_HELPERS) 339$(OUTPUT)/test_verifier: $(TESTING_HELPERS) $(CAP_HELPERS) $(UNPRIV_HELPERS) 340$(OUTPUT)/xsk.o: $(BPFOBJ) 341 342BPFTOOL ?= $(DEFAULT_BPFTOOL) 343$(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) \ 344 $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/bpftool 345 $(Q)$(MAKE) $(submake_extras) -C $(BPFTOOLDIR) \ 346 ARCH= CROSS_COMPILE= CC="$(HOSTCC)" LD="$(HOSTLD)" \ 347 EXTRA_CFLAGS='-g $(OPT_FLAGS) $(SAN_CFLAGS) $(EXTRA_CFLAGS)' \ 348 EXTRA_LDFLAGS='$(SAN_LDFLAGS) $(EXTRA_LDFLAGS)' \ 349 OUTPUT=$(HOST_BUILD_DIR)/bpftool/ \ 350 LIBBPF_OUTPUT=$(HOST_BUILD_DIR)/libbpf/ \ 351 LIBBPF_DESTDIR=$(HOST_SCRATCH_DIR)/ \ 352 SKIP_LLVM=$(SKIP_LLVM) \ 353 SKIP_LIBBFD=$(SKIP_LIBBFD) \ 354 SKIP_CRYPTO=$(SKIP_CRYPTO) \ 355 prefix= DESTDIR=$(HOST_SCRATCH_DIR)/ install-bin 356 357ifneq ($(CROSS_COMPILE),) 358$(CROSS_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) \ 359 $(BPFOBJ) | $(BUILD_DIR)/bpftool 360 $(Q)$(MAKE) $(submake_extras) -C $(BPFTOOLDIR) \ 361 ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) \ 362 EXTRA_CFLAGS='-g $(OPT_FLAGS) $(SAN_CFLAGS) $(EXTRA_CFLAGS)' \ 363 EXTRA_LDFLAGS='$(SAN_LDFLAGS) $(EXTRA_LDFLAGS)' \ 364 OUTPUT=$(BUILD_DIR)/bpftool/ \ 365 LIBBPF_OUTPUT=$(BUILD_DIR)/libbpf/ \ 366 LIBBPF_DESTDIR=$(SCRATCH_DIR)/ \ 367 SKIP_LLVM=$(SKIP_LLVM) \ 368 SKIP_LIBBFD=$(SKIP_LIBBFD) \ 369 SKIP_CRYPTO=$(SKIP_CRYPTO) \ 370 prefix= DESTDIR=$(SCRATCH_DIR)/ install-bin 371endif 372 373ifneq ($(SKIP_DOCS),1) 374all: docs 375endif 376 377docs: 378 $(Q)RST2MAN_OPTS="--exit-status=1" $(MAKE) $(submake_extras) \ 379 -f Makefile.docs \ 380 prefix= OUTPUT=$(OUTPUT)/ DESTDIR=$(OUTPUT)/ $@ 381 382docs-clean: 383 $(Q)$(MAKE) $(submake_extras) \ 384 -f Makefile.docs \ 385 prefix= OUTPUT=$(OUTPUT)/ DESTDIR=$(OUTPUT)/ $@ 386 387$(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile) \ 388 $(APIDIR)/linux/bpf.h \ 389 | $(BUILD_DIR)/libbpf 390 $(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(BUILD_DIR)/libbpf/ \ 391 EXTRA_CFLAGS='-g $(OPT_FLAGS) $(SAN_CFLAGS) $(EXTRA_CFLAGS)' \ 392 EXTRA_LDFLAGS='$(SAN_LDFLAGS) $(EXTRA_LDFLAGS)' \ 393 DESTDIR=$(SCRATCH_DIR) prefix= all install_headers 394 395ifneq ($(BPFOBJ),$(HOST_BPFOBJ)) 396$(HOST_BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile) \ 397 $(APIDIR)/linux/bpf.h \ 398 | $(HOST_BUILD_DIR)/libbpf 399 $(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) \ 400 ARCH= CROSS_COMPILE= \ 401 EXTRA_CFLAGS='-g $(OPT_FLAGS) $(EXTRA_CFLAGS)' \ 402 EXTRA_LDFLAGS='$(EXTRA_LDFLAGS)' \ 403 OUTPUT=$(HOST_BUILD_DIR)/libbpf/ \ 404 CC="$(HOSTCC)" LD="$(HOSTLD)" \ 405 DESTDIR=$(HOST_SCRATCH_DIR)/ prefix= all install_headers 406endif 407 408# vmlinux.h is first dumped to a temporary file and then compared to 409# the previous version. This helps to avoid unnecessary re-builds of 410# $(TRUNNER_BPF_OBJS) 411$(INCLUDE_DIR)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL) | $(INCLUDE_DIR) 412ifeq ($(VMLINUX_H),) 413 $(call msg,GEN,,$@) 414 $(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $(INCLUDE_DIR)/.vmlinux.h.tmp 415 $(Q)cmp -s $(INCLUDE_DIR)/.vmlinux.h.tmp $@ || mv $(INCLUDE_DIR)/.vmlinux.h.tmp $@ 416else 417 $(call msg,CP,,$@) 418 $(Q)cp "$(VMLINUX_H)" $@ 419endif 420 421$(RESOLVE_BTFIDS): $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/resolve_btfids \ 422 $(TOOLSDIR)/bpf/resolve_btfids/main.c \ 423 $(TOOLSDIR)/lib/rbtree.c \ 424 $(TOOLSDIR)/lib/zalloc.c \ 425 $(TOOLSDIR)/lib/string.c \ 426 $(TOOLSDIR)/lib/ctype.c \ 427 $(TOOLSDIR)/lib/str_error_r.c 428 $(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/resolve_btfids \ 429 CC="$(HOSTCC)" LD="$(HOSTLD)" AR="$(HOSTAR)" \ 430 LIBBPF_INCLUDE=$(HOST_INCLUDE_DIR) \ 431 EXTRA_LDFLAGS='$(SAN_LDFLAGS) $(EXTRA_LDFLAGS)' \ 432 HOSTPKG_CONFIG='$(PKG_CONFIG)' \ 433 OUTPUT=$(HOST_BUILD_DIR)/resolve_btfids/ BPFOBJ=$(HOST_BPFOBJ) 434 435# Get Clang's default includes on this system, as opposed to those seen by 436# '--target=bpf'. This fixes "missing" files on some architectures/distros, 437# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc. 438# 439# Use '-idirafter': Don't interfere with include mechanics except where the 440# build would have failed anyways. 441define get_sys_includes 442$(shell $(1) $(2) -v -E - </dev/null 2>&1 \ 443 | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \ 444$(shell $(1) $(2) -dM -E - </dev/null | grep '__riscv_xlen ' | awk '{printf("-D__riscv_xlen=%d -D__BITS_PER_LONG=%d", $$3, $$3)}') \ 445$(shell $(1) $(2) -dM -E - </dev/null | grep '__loongarch_grlen ' | awk '{printf("-D__BITS_PER_LONG=%d", $$3)}') \ 446$(shell $(1) $(2) -dM -E - </dev/null | grep -E 'MIPS(EL|EB)|_MIPS_SZ(PTR|LONG) |_MIPS_SIM |_ABI(O32|N32|64) ' | awk '{printf("-D%s=%s ", $$2, $$3)}') 447endef 448 449# Determine target endianness. 450IS_LITTLE_ENDIAN := $(shell $(CC) -dM -E - </dev/null | \ 451 grep 'define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__') 452MENDIAN:=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian) 453BPF_TARGET_ENDIAN:=$(if $(IS_LITTLE_ENDIAN),--target=bpfel,--target=bpfeb) 454 455ifneq ($(CROSS_COMPILE),) 456CLANG_TARGET_ARCH = --target=$(notdir $(CROSS_COMPILE:%-=%)) 457endif 458 459CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG),$(CLANG_TARGET_ARCH)) 460BPF_CFLAGS = -g -Wall -Werror -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN) \ 461 -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR) \ 462 -I$(CURDIR)/libarena/include \ 463 -I$(abspath $(OUTPUT)/../usr/include) \ 464 -std=gnu11 \ 465 -fno-strict-aliasing \ 466 -Wno-microsoft-anon-tag \ 467 -fms-extensions \ 468 -Wno-compare-distinct-pointer-types \ 469 -Wno-initializer-overrides \ 470 # 471# TODO: enable me -Wsign-compare 472 473CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) 474 475$(OUTPUT)/test_l4lb_noinline.o: BPF_CFLAGS += -fno-inline 476$(OUTPUT)/test_xdp_noinline.o: BPF_CFLAGS += -fno-inline 477 478$(OUTPUT)/flow_dissector_load.o: flow_dissector_load.h 479$(OUTPUT)/cgroup_getset_retval_hooks.o: cgroup_getset_retval_hooks.h 480 481# Build BPF object using Clang 482# $1 - input .c file 483# $2 - output .o file 484# $3 - CFLAGS 485# $4 - binary name 486define CLANG_BPF_BUILD_RULE 487 $(call msg,CLNG-BPF,$4,$2) 488 $(Q)$(CLANG) $3 -O2 $(BPF_TARGET_ENDIAN) -c $1 -mcpu=v3 -o $2 $(if $(PERMISSIVE),|| \ 489 ($(RM) $2; printf ' %-12s %s\n' 'SKIP-BPF' '$(notdir $2)' 1>&2)) 490endef 491# Similar to CLANG_BPF_BUILD_RULE, but with disabled alu32 492define CLANG_NOALU32_BPF_BUILD_RULE 493 $(call msg,CLNG-BPF,$4,$2) 494 $(Q)$(CLANG) $3 -O2 $(BPF_TARGET_ENDIAN) -c $1 -mcpu=v2 -o $2 $(if $(PERMISSIVE),|| \ 495 ($(RM) $2; printf ' %-12s %s\n' 'SKIP-BPF' '$(notdir $2)' 1>&2)) 496endef 497# Similar to CLANG_BPF_BUILD_RULE, but with cpu-v4 498define CLANG_CPUV4_BPF_BUILD_RULE 499 $(call msg,CLNG-BPF,$4,$2) 500 $(Q)$(CLANG) $3 -O2 $(BPF_TARGET_ENDIAN) -c $1 -mcpu=v4 -o $2 $(if $(PERMISSIVE),|| \ 501 ($(RM) $2; printf ' %-12s %s\n' 'SKIP-BPF' '$(notdir $2)' 1>&2)) 502endef 503# Build BPF object using GCC 504define GCC_BPF_BUILD_RULE 505 $(call msg,GCC-BPF,$4,$2) 506 $(Q)$(BPF_GCC) $3 -DBPF_NO_PRESERVE_ACCESS_INDEX -Wno-attributes -O2 -c $1 -o $2 $(if $(PERMISSIVE),|| \ 507 ($(RM) $2; printf ' %-12s %s\n' 'SKIP-BPF' '$(notdir $2)' 1>&2)) 508endef 509 510SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c 511 512LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \ 513 linked_vars.skel.h linked_maps.skel.h \ 514 test_subskeleton.skel.h test_subskeleton_lib.skel.h \ 515 test_usdt.skel.h tracing_multi.skel.h \ 516 tracing_multi_module.skel.h \ 517 tracing_multi_intersect.skel.h \ 518 tracing_multi_session.skel.h 519 520LSKELS := fexit_sleep.c trace_printk.c trace_vprintk.c map_ptr_kern.c \ 521 core_kern.c core_kern_overflow.c test_ringbuf.c \ 522 test_ringbuf_n.c test_ringbuf_map_key.c test_ringbuf_write.c \ 523 test_ringbuf_overwrite.c 524 525LSKELS_SIGNED := fentry_test.c fexit_test.c atomics.c 526 527# Generate both light skeleton and libbpf skeleton for these 528LSKELS_EXTRA := test_ksyms_module.c test_ksyms_weak.c kfunc_call_test.c \ 529 kfunc_call_test_subprog.c 530SKEL_BLACKLIST += $$(LSKELS) $$(LSKELS_SIGNED) 531 532test_static_linked.skel.h-deps := test_static_linked1.bpf.o test_static_linked2.bpf.o 533linked_funcs.skel.h-deps := linked_funcs1.bpf.o linked_funcs2.bpf.o 534linked_vars.skel.h-deps := linked_vars1.bpf.o linked_vars2.bpf.o 535linked_maps.skel.h-deps := linked_maps1.bpf.o linked_maps2.bpf.o 536# In the subskeleton case, we want the test_subskeleton_lib.subskel.h file 537# but that's created as a side-effect of the skel.h generation. 538test_subskeleton.skel.h-deps := test_subskeleton_lib2.bpf.o test_subskeleton_lib.bpf.o test_subskeleton.bpf.o 539test_subskeleton_lib.skel.h-deps := test_subskeleton_lib2.bpf.o test_subskeleton_lib.bpf.o 540test_usdt.skel.h-deps := test_usdt.bpf.o test_usdt_multispec.bpf.o 541xsk_xdp_progs.skel.h-deps := xsk_xdp_progs.bpf.o 542xdp_hw_metadata.skel.h-deps := xdp_hw_metadata.bpf.o 543xdp_features.skel.h-deps := xdp_features.bpf.o 544tracing_multi.skel.h-deps := tracing_multi_attach.bpf.o tracing_multi_check.bpf.o 545tracing_multi_module.skel.h-deps := tracing_multi_attach_module.bpf.o tracing_multi_check.bpf.o 546tracing_multi_intersect.skel.h-deps := tracing_multi_intersect_attach.bpf.o tracing_multi_check.bpf.o 547tracing_multi_session.skel.h-deps := tracing_multi_session_attach.bpf.o tracing_multi_check.bpf.o 548 549LINKED_BPF_OBJS := $(foreach skel,$(LINKED_SKELS),$($(skel)-deps)) 550LINKED_BPF_SRCS := $(patsubst %.bpf.o,%.c,$(LINKED_BPF_OBJS)) 551 552HEADERS_FOR_BPF_OBJS := $(wildcard $(BPFDIR)/*.bpf.h) \ 553 $(wildcard $(CURDIR)/libarena/include/*.[ch]) \ 554 $(addprefix $(BPFDIR)/, bpf_core_read.h \ 555 bpf_endian.h \ 556 bpf_helpers.h \ 557 bpf_tracing.h) 558 559# Set up extra TRUNNER_XXX "temporary" variables in the environment (relies on 560# $eval()) and pass control to DEFINE_TEST_RUNNER_RULES. 561# Parameters: 562# $1 - test runner base binary name (e.g., test_progs) 563# $2 - test runner extra "flavor" (e.g., no_alu32, cpuv4, bpf_gcc, etc) 564define DEFINE_TEST_RUNNER 565 566LSKEL_SIGN := -S -k $(PRIVATE_KEY) -i $(VERIFICATION_CERT) 567TRUNNER_OUTPUT := $(OUTPUT)$(if $2,/)$2 568TRUNNER_BINARY := $1$(if $2,-)$2 569TRUNNER_TEST_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.test.o, \ 570 $$(notdir $$(wildcard $(TRUNNER_TESTS_DIR)/*.c))) 571TRUNNER_EXTRA_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, \ 572 $$(filter %.c,$(TRUNNER_EXTRA_SOURCES))) 573TRUNNER_LIB_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, \ 574 $$(filter %.c,$(TRUNNER_LIB_SOURCES))) 575TRUNNER_EXTRA_HDRS := $$(filter %.h,$(TRUNNER_EXTRA_SOURCES)) 576TRUNNER_TESTS_HDR := $(TRUNNER_TESTS_DIR)/tests.h 577TRUNNER_BPF_SRCS := $$(notdir $$(wildcard $(TRUNNER_BPF_PROGS_DIR)/*.c)) 578TRUNNER_BPF_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.bpf.o, $$(TRUNNER_BPF_SRCS)) 579TRUNNER_BPF_SKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.skel.h, \ 580 $$(filter-out $(SKEL_BLACKLIST) $(LINKED_BPF_SRCS),\ 581 $$(TRUNNER_BPF_SRCS))) 582TRUNNER_BPF_LSKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.lskel.h, $$(LSKELS) $$(LSKELS_EXTRA)) 583TRUNNER_BPF_SKELS_LINKED := $$(addprefix $$(TRUNNER_OUTPUT)/,$(LINKED_SKELS)) 584TRUNNER_BPF_LSKELS_SIGNED := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.lskel.h, $$(LSKELS_SIGNED)) 585TEST_GEN_FILES += $$(TRUNNER_BPF_OBJS) 586 587# Evaluate rules now with extra TRUNNER_XXX variables above already defined 588$$(eval $$(call DEFINE_TEST_RUNNER_RULES,$1,$2)) 589 590endef 591 592# Using TRUNNER_XXX variables, provided by callers of DEFINE_TEST_RUNNER and 593# set up by DEFINE_TEST_RUNNER itself, create test runner build rules with: 594# $1 - test runner base binary name (e.g., test_progs) 595# $2 - test runner extra "flavor" (e.g., no_alu32, cpuv4, bpf_gcc, etc) 596define DEFINE_TEST_RUNNER_RULES 597 598# Permissive build behaviour (skip-on-failure compile, partial-link) only 599# applies to test_progs and its flavors; runners that use strong cross-object 600# references (e.g. test_maps) keep strict semantics even when permissive. 601# The check is inlined per-runner so $1 is substituted at $(call) time and 602# the result is baked into each rule's recipe. 603 604ifeq ($($(TRUNNER_OUTPUT)-dir),) 605$(TRUNNER_OUTPUT)-dir := y 606$(TRUNNER_OUTPUT): 607 $$(call msg,MKDIR,,$$@) 608 $(Q)mkdir -p $$@ 609endif 610 611# ensure we set up BPF objects generation rule just once for a given 612# input/output directory combination 613ifeq ($($(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs),) 614$(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs := y 615$(TRUNNER_BPF_OBJS): $(TRUNNER_OUTPUT)/%.bpf.o: \ 616 $(TRUNNER_BPF_PROGS_DIR)/%.c \ 617 $(TRUNNER_BPF_PROGS_DIR)/*.h \ 618 $$(INCLUDE_DIR)/vmlinux.h \ 619 $(HEADERS_FOR_BPF_OBJS) \ 620 | $(TRUNNER_OUTPUT) $$(BPFOBJ) 621 $$(call $(TRUNNER_BPF_BUILD_RULE),$$<,$$@, \ 622 $(TRUNNER_BPF_CFLAGS) \ 623 $$($$<-CFLAGS) \ 624 $$($$<-$2-CFLAGS),$(TRUNNER_BINARY)) 625 626$(TRUNNER_BPF_SKELS): %.skel.h: %.bpf.o $(BPFTOOL) | $(TRUNNER_OUTPUT) 627 $(Q)$(if $(PERMISSIVE),if [ ! -f $$< ]; then \ 628 $$(RM) $$@ $$(@:.skel.h=.subskel.h); \ 629 printf ' %-12s %s\n' 'SKIP-SKEL' '$$(notdir $$@)' 1>&2; \ 630 exit 0; \ 631 fi;) \ 632 printf ' %-12s %s\n' 'GEN-SKEL' '[$(TRUNNER_BINARY)] $$(notdir $$@)' 1>&2; \ 633 $$(BPFTOOL) gen object $$(<:.o=.linked1.o) $$< && \ 634 $$(BPFTOOL) gen object $$(<:.o=.linked2.o) $$(<:.o=.linked1.o) && \ 635 $$(BPFTOOL) gen object $$(<:.o=.linked3.o) $$(<:.o=.linked2.o) && \ 636 diff $$(<:.o=.linked2.o) $$(<:.o=.linked3.o) && \ 637 $$(BPFTOOL) gen skeleton $$(<:.o=.linked3.o) name $$(notdir $$(<:.bpf.o=)) > $$@ && \ 638 $$(BPFTOOL) gen subskeleton $$(<:.o=.linked3.o) name $$(notdir $$(<:.bpf.o=)) > $$(@:.skel.h=.subskel.h) $(if $(PERMISSIVE),|| { \ 639 $$(RM) $$@ $$(@:.skel.h=.subskel.h); \ 640 printf ' %-12s %s\n' 'SKIP-SKEL' '$$(notdir $$@)' 1>&2; \ 641 }) && \ 642 rm -f $$(<:.o=.linked1.o) $$(<:.o=.linked2.o) $$(<:.o=.linked3.o) 643 644$(TRUNNER_BPF_LSKELS): %.lskel.h: %.bpf.o $(BPFTOOL) | $(TRUNNER_OUTPUT) 645 $(Q)$(if $(PERMISSIVE),if [ ! -f $$< ]; then \ 646 $$(RM) $$@; \ 647 printf ' %-12s %s\n' 'SKIP-SKEL' '$$(notdir $$@)' 1>&2; \ 648 exit 0; \ 649 fi;) \ 650 printf ' %-12s %s\n' 'GEN-SKEL' '[$(TRUNNER_BINARY)] $$(notdir $$@)' 1>&2; \ 651 $$(BPFTOOL) gen object $$(<:.o=.llinked1.o) $$< && \ 652 $$(BPFTOOL) gen object $$(<:.o=.llinked2.o) $$(<:.o=.llinked1.o) && \ 653 $$(BPFTOOL) gen object $$(<:.o=.llinked3.o) $$(<:.o=.llinked2.o) && \ 654 diff $$(<:.o=.llinked2.o) $$(<:.o=.llinked3.o) && \ 655 $$(BPFTOOL) gen skeleton -L $$(<:.o=.llinked3.o) name $$(notdir $$(<:.bpf.o=_lskel)) > $$@ $(if $(PERMISSIVE),|| { \ 656 $$(RM) $$@; \ 657 printf ' %-12s %s\n' 'SKIP-SKEL' '$$(notdir $$@)' 1>&2; \ 658 }) && \ 659 rm -f $$(<:.o=.llinked1.o) $$(<:.o=.llinked2.o) $$(<:.o=.llinked3.o) 660 661$(TRUNNER_BPF_LSKELS_SIGNED): %.lskel.h: %.bpf.o $(BPFTOOL) | $(TRUNNER_OUTPUT) 662 $(Q)$(if $(PERMISSIVE),if [ ! -f $$< ]; then \ 663 $$(RM) $$@; \ 664 printf ' %-12s %s\n' 'SKIP-SKEL' '$$(notdir $$@)' 1>&2; \ 665 exit 0; \ 666 fi;) \ 667 printf ' %-12s %s\n' 'GEN-SKEL' '[$(TRUNNER_BINARY) (signed)] $$(notdir $$@)' 1>&2; \ 668 $$(BPFTOOL) gen object $$(<:.o=.llinked1.o) $$< && \ 669 $$(BPFTOOL) gen object $$(<:.o=.llinked2.o) $$(<:.o=.llinked1.o) && \ 670 $$(BPFTOOL) gen object $$(<:.o=.llinked3.o) $$(<:.o=.llinked2.o) && \ 671 diff $$(<:.o=.llinked2.o) $$(<:.o=.llinked3.o) && \ 672 $$(BPFTOOL) gen skeleton $(LSKEL_SIGN) $$(<:.o=.llinked3.o) name $$(notdir $$(<:.bpf.o=_lskel)) > $$@ $(if $(PERMISSIVE),|| { \ 673 $$(RM) $$@; \ 674 printf ' %-12s %s\n' 'SKIP-SKEL' '$$(notdir $$@)' 1>&2; \ 675 }) && \ 676 rm -f $$(<:.o=.llinked1.o) $$(<:.o=.llinked2.o) $$(<:.o=.llinked3.o) 677 678$(LINKED_BPF_OBJS): %: $(TRUNNER_OUTPUT)/% 679 680# .SECONDEXPANSION here allows to correctly expand %-deps variables as prerequisites 681.SECONDEXPANSION: 682$(TRUNNER_BPF_SKELS_LINKED): $(TRUNNER_OUTPUT)/%: $$$$(%-deps) $(BPFTOOL) | $(TRUNNER_OUTPUT) 683 $(Q)$(if $(PERMISSIVE),for f in $$(addprefix $(TRUNNER_OUTPUT)/,$$($$(@F)-deps)); do \ 684 if [ ! -f $$$$f ]; then \ 685 $$(RM) $$@ $$(@:.skel.h=.subskel.h); \ 686 printf ' %-12s %s\n' 'SKIP-SKEL' '$$(notdir $$@)' 1>&2; \ 687 exit 0; \ 688 fi; \ 689 done;) \ 690 printf ' %-12s %s\n' 'LINK-BPF' '[$(TRUNNER_BINARY)] $$(notdir $$(@:.skel.h=.bpf.o))' 1>&2; \ 691 $$(BPFTOOL) gen object $$(@:.skel.h=.linked1.o) $$(addprefix $(TRUNNER_OUTPUT)/,$$($$(@F)-deps)) && \ 692 $$(BPFTOOL) gen object $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked1.o) && \ 693 $$(BPFTOOL) gen object $$(@:.skel.h=.linked3.o) $$(@:.skel.h=.linked2.o) && \ 694 diff $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked3.o) && \ 695 printf ' %-12s %s\n' 'GEN-SKEL' '[$(TRUNNER_BINARY)] $$(notdir $$@)' 1>&2 && \ 696 $$(BPFTOOL) gen skeleton $$(@:.skel.h=.linked3.o) name $$(notdir $$(@:.skel.h=)) > $$@ && \ 697 $$(BPFTOOL) gen subskeleton $$(@:.skel.h=.linked3.o) name $$(notdir $$(@:.skel.h=)) > $$(@:.skel.h=.subskel.h) $(if $(PERMISSIVE),|| { \ 698 $$(RM) $$@ $$(@:.skel.h=.subskel.h); \ 699 printf ' %-12s %s\n' 'SKIP-SKEL' '$$(notdir $$@)' 1>&2; \ 700 }) && \ 701 rm -f $$(@:.skel.h=.linked1.o) $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked3.o) 702 703# When the compiler generates a %.d file, only skel basenames (not 704# full paths) are specified as prerequisites for corresponding %.o 705# file. vpath directives below instruct make to search for skel files 706# in TRUNNER_OUTPUT, if they are not present in the working directory. 707vpath %.skel.h $(TRUNNER_OUTPUT) 708vpath %.lskel.h $(TRUNNER_OUTPUT) 709vpath %.subskel.h $(TRUNNER_OUTPUT) 710 711endif 712 713# ensure we set up tests.h header generation rule just once 714ifeq ($($(TRUNNER_TESTS_DIR)-tests-hdr),) 715$(TRUNNER_TESTS_DIR)-tests-hdr := y 716$(TRUNNER_TESTS_HDR): $(TRUNNER_TESTS_DIR)/*.c 717 $$(call msg,TEST-HDR,$(TRUNNER_BINARY),$$@) 718 $$(shell (echo '/* Generated header, do not edit */'; \ 719 sed -n -E 's/^void (serial_)?test_([a-zA-Z0-9_]+)\((void)?\).*/DEFINE_TEST(\2)/p' \ 720 $(TRUNNER_TESTS_DIR)/*.c | sort ; \ 721 ) > $$@) 722endif 723 724$(TRUNNER_OUTPUT)/resolve_btfids.test.o: $(RESOLVE_BTFIDS) $(TRUNNER_OUTPUT)/btf_data.bpf.o 725$(TRUNNER_OUTPUT)/resolve_btfids.test.o: private TEST_NEEDS_BTFIDS = 1 726 727# compile individual test files 728# Note: we cd into output directory to ensure embedded BPF object is found 729$(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o: \ 730 $(TRUNNER_TESTS_DIR)/%.c \ 731 | $(TRUNNER_OUTPUT)/%.test.d 732 $$(call msg,TEST-OBJ,$(TRUNNER_BINARY),$$@) 733 $(Q)(cd $$(@D) && $$(CC) -I. $$(CFLAGS) -MMD -MT $$@ -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F)) $(if $(filter test_progs%,$1),$(if $(PERMISSIVE),|| \ 734 ($(RM) $$@; printf ' %-12s %s\n' 'SKIP-TEST' '$$(notdir $$@)' 1>&2))) 735 $$(if $$(TEST_NEEDS_BTFIDS), \ 736 $(Q)if [ -f $$@ ]; then \ 737 $(if $(filter 1,$(V)),true,printf ' %-8s%s %s\n' "BTFIDS" " [$(TRUNNER_BINARY)]" "$$(notdir $$@)"); \ 738 $(RESOLVE_BTFIDS) --btf $(TRUNNER_OUTPUT)/btf_data.bpf.o $$@; \ 739 $(RESOLVE_BTFIDS) --patch_btfids $$@.BTF_ids $$@; \ 740 fi) 741 742$(TRUNNER_TEST_OBJS:.o=.d): $(TRUNNER_OUTPUT)/%.test.d: \ 743 $(TRUNNER_TESTS_DIR)/%.c \ 744 $(TRUNNER_EXTRA_HDRS) \ 745 $$(BPFOBJ) | $(TRUNNER_OUTPUT) \ 746 $(TRUNNER_BPF_SKELS) \ 747 $(TRUNNER_BPF_LSKELS) \ 748 $(TRUNNER_BPF_LSKELS_SIGNED) \ 749 $(TRUNNER_BPF_SKELS_LINKED) 750 751ifeq ($(filter clean docs-clean emit_tests,$(MAKECMDGOALS)),) 752include $(wildcard $(TRUNNER_TEST_OBJS:.o=.d)) 753endif 754 755# add per extra obj CFGLAGS definitions 756$(foreach N,$(patsubst $(TRUNNER_OUTPUT)/%.o,%,$(TRUNNER_EXTRA_OBJS)), \ 757 $(eval $(TRUNNER_OUTPUT)/$(N).o: CFLAGS += $($(N).c-CFLAGS))) 758 759$(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o: \ 760 %.c \ 761 $(TRUNNER_EXTRA_HDRS) \ 762 $(VERIFY_SIG_HDR) \ 763 $(TRUNNER_TESTS_HDR) \ 764 $$(BPFOBJ) | $(TRUNNER_OUTPUT) 765 $$(call msg,EXT-OBJ,$(TRUNNER_BINARY),$$@) 766 $(Q)$$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@ 767 768$(TRUNNER_LIB_OBJS): $(TRUNNER_OUTPUT)/%.o:$(TOOLSDIR)/lib/%.c 769 $$(call msg,LIB-OBJ,$(TRUNNER_BINARY),$$@) 770 $(Q)$$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@ 771 772# non-flavored in-srctree builds receive special treatment, in particular, we 773# do not need to copy extra resources (see e.g. test_btf_dump_case()) 774$(TRUNNER_BINARY)-extras: $(TRUNNER_EXTRA_FILES) | $(TRUNNER_OUTPUT) 775ifneq ($2:$(OUTPUT),:$(shell pwd)) 776 $$(call msg,EXT-COPY,$(TRUNNER_BINARY),$(TRUNNER_EXTRA_FILES)) 777 $(Q)rsync -aq $(if $(PERMISSIVE),--ignore-missing-args) $$^ $(TRUNNER_OUTPUT)/ 778endif 779 780# some X.test.o files have runtime dependencies on Y.bpf.o files 781$(OUTPUT)/$(TRUNNER_BINARY): | $(TRUNNER_BPF_OBJS) 782 783$(OUTPUT)/$(TRUNNER_BINARY): $(if $(filter test_progs%,$1),$(if $(PERMISSIVE),$$(wildcard $(TRUNNER_TEST_OBJS)),$(TRUNNER_TEST_OBJS)),$(TRUNNER_TEST_OBJS)) \ 784 $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ) \ 785 $(TRUNNER_LIB_OBJS) \ 786 $(TRUNNER_BPFTOOL) \ 787 $(OUTPUT)/veristat \ 788 | $(TRUNNER_BINARY)-extras \ 789 $(if $(filter test_progs%,$1),$(if $(PERMISSIVE),$(TRUNNER_TEST_OBJS))) 790 $$(call msg,BINARY,,$$@) 791 $(Q)$$(CC) $$(CFLAGS) $(if $(filter test_progs%,$1),$(if $(PERMISSIVE),$$(filter %.a %.o,$$(wildcard $(TRUNNER_TEST_OBJS)) $$(filter-out $(TRUNNER_TEST_OBJS),$$^)),$$(filter %.a %.o,$$^)),$$(filter %.a %.o,$$^)) $$(LDLIBS) $$(LLVM_LDLIBS) $$(LDFLAGS) $$(LLVM_LDFLAGS) -o $$@ 792 $(Q)ln -sf $(if $2,..,.)/tools/build/bpftool/$(USE_BOOTSTRAP)bpftool \ 793 $(OUTPUT)/$(if $2,$2/)bpftool 794 795endef 796 797VERIFY_SIG_SETUP := $(CURDIR)/verify_sig_setup.sh 798VERIFY_SIG_HDR := verification_cert.h 799VERIFICATION_CERT := $(BUILD_DIR)/signing_key.der 800PRIVATE_KEY := $(BUILD_DIR)/signing_key.pem 801 802$(VERIFICATION_CERT) $(PRIVATE_KEY): $(VERIFY_SIG_SETUP) 803 $(Q)mkdir -p $(BUILD_DIR) 804 $(Q)$(VERIFY_SIG_SETUP) genkey $(BUILD_DIR) 805 806# Generates a header with C array declaration, containing test_progs_verification_cert bytes 807$(VERIFY_SIG_HDR): $(VERIFICATION_CERT) 808 $(Q)(echo "unsigned char test_progs_verification_cert[] = {"; \ 809 od -v -t 'xC' -w12 $< | sed 's/ \(\S\+\)/ 0x\1,/g;s/^\S\+/ /;$$d'; \ 810 echo "};"; \ 811 echo "unsigned int test_progs_verification_cert_len = $$(wc -c < $<);") > $@ 812 813LIBARENA_MAKE_ARGS = \ 814 BPFTOOL="$(BPFTOOL)" \ 815 INCLUDE_DIR="$(INCLUDE_DIR)" \ 816 LIBBPF_INCLUDE="$(HOST_INCLUDE_DIR)" \ 817 BPFOBJ="$(BPFOBJ)" \ 818 LDLIBS="$(LDLIBS) -lzstd" \ 819 CLANG="$(CLANG)" \ 820 BPF_CFLAGS="$(BPF_CFLAGS) $(CLANG_CFLAGS)" \ 821 BPF_TARGET_ENDIAN="$(BPF_TARGET_ENDIAN)" \ 822 Q="$(Q)" 823 824LIBARENA_BPF_DEPS := $(wildcard libarena/Makefile \ 825 libarena/include/* \ 826 libarena/include/libarena/* \ 827 libarena/src/* \ 828 libarena/selftests/* \ 829 libarena/*.bpf.o) 830 831LIBARENA_SKEL := libarena/libarena.skel.h 832 833$(LIBARENA_SKEL): $(INCLUDE_DIR)/vmlinux.h $(BPFOBJ) $(LIBARENA_BPF_DEPS) 834 +$(MAKE) -C libarena libarena.skel.h $(LIBARENA_MAKE_ARGS) 835 836ifneq ($(CLANG_HAS_ARENA_ASAN),) 837LIBARENA_ASAN_SKEL := libarena/libarena_asan.skel.h 838CFLAGS += -DHAS_BPF_ARENA_ASAN 839 840$(LIBARENA_ASAN_SKEL): $(INCLUDE_DIR)/vmlinux.h $(BPFOBJ) $(LIBARENA_BPF_DEPS) 841 +$(MAKE) -C libarena libarena_asan.skel.h $(LIBARENA_MAKE_ARGS) 842endif 843 844# Define test_progs test runner. 845TRUNNER_TESTS_DIR := prog_tests 846TRUNNER_BPF_PROGS_DIR := progs 847TRUNNER_EXTRA_SOURCES := test_progs.c \ 848 cgroup_helpers.c \ 849 trace_helpers.c \ 850 network_helpers.c \ 851 testing_helpers.c \ 852 btf_helpers.c \ 853 cap_helpers.c \ 854 unpriv_helpers.c \ 855 sysctl_helpers.c \ 856 netlink_helpers.c \ 857 jit_disasm_helpers.c \ 858 io_helpers.c \ 859 test_loader.c \ 860 xsk.c \ 861 disasm.c \ 862 disasm_helpers.c \ 863 json_writer.c \ 864 $(VERIFY_SIG_HDR) \ 865 flow_dissector_load.h \ 866 ip_check_defrag_frags.h \ 867 bpftool_helpers.c \ 868 usdt_1.c usdt_2.c \ 869 $(LIBARENA_SKEL) \ 870 $(LIBARENA_ASAN_SKEL) 871TRUNNER_LIB_SOURCES := find_bit.c 872TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \ 873 $(OUTPUT)/liburandom_read.so \ 874 $(OUTPUT)/xdp_synproxy \ 875 $(OUTPUT)/sign-file \ 876 $(OUTPUT)/uprobe_multi \ 877 $(TEST_KMOD_TARGETS) \ 878 ima_setup.sh \ 879 $(VERIFY_SIG_SETUP) \ 880 $(wildcard progs/btf_dump_test_case_*.c) \ 881 $(wildcard progs/*.bpf.o) 882TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE 883TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS) -DENABLE_ATOMICS_TESTS 884$(eval $(call DEFINE_TEST_RUNNER,test_progs)) 885 886# Define test_progs-no_alu32 test runner. 887TRUNNER_BPF_BUILD_RULE := CLANG_NOALU32_BPF_BUILD_RULE 888TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS) 889$(eval $(call DEFINE_TEST_RUNNER,test_progs,no_alu32)) 890 891# Define test_progs-cpuv4 test runner. 892ifneq ($(CLANG_CPUV4),) 893TRUNNER_BPF_BUILD_RULE := CLANG_CPUV4_BPF_BUILD_RULE 894TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS) -DENABLE_ATOMICS_TESTS 895$(eval $(call DEFINE_TEST_RUNNER,test_progs,cpuv4)) 896endif 897 898# Define test_progs BPF-GCC-flavored test runner. 899ifneq ($(BPF_GCC),) 900TRUNNER_BPF_BUILD_RULE := GCC_BPF_BUILD_RULE 901TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(call get_sys_includes,gcc,) 902$(eval $(call DEFINE_TEST_RUNNER,test_progs,bpf_gcc)) 903endif 904 905# Define test_maps test runner. 906TRUNNER_TESTS_DIR := map_tests 907TRUNNER_BPF_PROGS_DIR := progs 908TRUNNER_EXTRA_SOURCES := test_maps.c 909TRUNNER_LIB_SOURCES := 910TRUNNER_EXTRA_FILES := 911TRUNNER_BPF_BUILD_RULE := $$(error no BPF objects should be built) 912TRUNNER_BPF_CFLAGS := 913$(eval $(call DEFINE_TEST_RUNNER,test_maps)) 914 915# Define test_verifier test runner. 916# It is much simpler than test_maps/test_progs and sufficiently different from 917# them (e.g., test.h is using completely pattern), that it's worth just 918# explicitly defining all the rules explicitly. 919verifier/tests.h: verifier/*.c 920 $(shell ( cd verifier/; \ 921 echo '/* Generated header, do not edit */'; \ 922 echo '#ifdef FILL_ARRAY'; \ 923 ls *.c 2> /dev/null | sed -e 's@\(.*\)@#include \"\1\"@'; \ 924 echo '#endif' \ 925 ) > verifier/tests.h) 926$(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT) 927 $(call msg,BINARY,,$@) 928 $(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@ 929 930# Include find_bit.c to compile xskxceiver. 931EXTRA_SRC := $(TOOLSDIR)/lib/find_bit.c prog_tests/test_xsk.c prog_tests/test_xsk.h 932$(OUTPUT)/xskxceiver: $(EXTRA_SRC) xskxceiver.c xskxceiver.h $(OUTPUT)/network_helpers.o $(OUTPUT)/xsk.o $(OUTPUT)/xsk_xdp_progs.skel.h $(BPFOBJ) | $(OUTPUT) 933 $(call msg,BINARY,,$@) 934 $(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@ 935 936$(OUTPUT)/xdp_hw_metadata: xdp_hw_metadata.c $(OUTPUT)/network_helpers.o $(OUTPUT)/xsk.o $(OUTPUT)/xdp_hw_metadata.skel.h | $(OUTPUT) 937 $(call msg,BINARY,,$@) 938 $(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@ 939 940$(OUTPUT)/xdp_features: xdp_features.c $(OUTPUT)/network_helpers.o $(OUTPUT)/xdp_features.skel.h | $(OUTPUT) 941 $(call msg,BINARY,,$@) 942 $(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@ 943 944# Make sure we are able to include and link libbpf against c++. 945CXXFLAGS += $(CFLAGS) 946CXXFLAGS := $(subst -D_GNU_SOURCE=,,$(CXXFLAGS)) 947CXXFLAGS := $(subst -std=gnu11,-std=gnu++11,$(CXXFLAGS)) 948$(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ) 949 $(call msg,CXX,,$@) 950 $(Q)$(CXX) $(CXXFLAGS) $(filter %.a %.o %.cpp,$^) $(LDLIBS) -o $@ 951 952# Benchmark runner 953$(OUTPUT)/bench_%.o: benchs/bench_%.c bench.h $(BPFOBJ) 954 $(call msg,CC,,$@) 955 $(Q)$(CC) $(CFLAGS) -O2 -c $(filter %.c,$^) $(LDLIBS) -o $@ $(if $(PERMISSIVE),|| \ 956 ($(RM) $@; printf ' %-12s %s\n' 'SKIP-BENCH' '$(notdir $@)' 1>&2)) 957$(OUTPUT)/bench_rename.o: $(OUTPUT)/test_overhead.skel.h 958$(OUTPUT)/bench_trigger.o: $(OUTPUT)/trigger_bench.skel.h 959$(OUTPUT)/bench_ringbufs.o: $(OUTPUT)/ringbuf_bench.skel.h \ 960 $(OUTPUT)/perfbuf_bench.skel.h 961$(OUTPUT)/bench_bloom_filter_map.o: $(OUTPUT)/bloom_filter_bench.skel.h 962$(OUTPUT)/bench_bpf_loop.o: $(OUTPUT)/bpf_loop_bench.skel.h 963$(OUTPUT)/bench_strncmp.o: $(OUTPUT)/strncmp_bench.skel.h 964$(OUTPUT)/bench_bpf_hashmap_full_update.o: $(OUTPUT)/bpf_hashmap_full_update_bench.skel.h 965$(OUTPUT)/bench_local_storage.o: $(OUTPUT)/local_storage_bench.skel.h 966$(OUTPUT)/bench_local_storage_rcu_tasks_trace.o: $(OUTPUT)/local_storage_rcu_tasks_trace_bench.skel.h 967$(OUTPUT)/bench_local_storage_create.o: $(OUTPUT)/bench_local_storage_create.skel.h 968$(OUTPUT)/bench_bpf_hashmap_lookup.o: $(OUTPUT)/bpf_hashmap_lookup.skel.h 969$(OUTPUT)/bench_htab_mem.o: $(OUTPUT)/htab_mem_bench.skel.h 970$(OUTPUT)/bench_bpf_crypto.o: $(OUTPUT)/crypto_bench.skel.h 971$(OUTPUT)/bench_sockmap.o: $(OUTPUT)/bench_sockmap_prog.skel.h 972$(OUTPUT)/bench_lpm_trie_map.o: $(OUTPUT)/lpm_trie_bench.skel.h $(OUTPUT)/lpm_trie_map.skel.h 973$(OUTPUT)/bench_bpf_nop.o: $(OUTPUT)/bpf_nop_bench.skel.h bench_bpf_timing.h 974$(OUTPUT)/bench_xdp_lb.o: $(OUTPUT)/xdp_lb_bench.skel.h bench_bpf_timing.h 975$(OUTPUT)/bench_bpf_timing.o: bench_bpf_timing.h 976$(OUTPUT)/bench.o: bench.h testing_helpers.h $(BPFOBJ) 977$(OUTPUT)/bench: LDLIBS += -lm 978$(OUTPUT)/bench: $(OUTPUT)/bench.o \ 979 $(TESTING_HELPERS) \ 980 $(TRACE_HELPERS) \ 981 $(CGROUP_HELPERS) \ 982 $(OUTPUT)/bench_count.o \ 983 $(OUTPUT)/bench_rename.o \ 984 $(OUTPUT)/bench_trigger.o \ 985 $(OUTPUT)/bench_ringbufs.o \ 986 $(OUTPUT)/bench_bloom_filter_map.o \ 987 $(OUTPUT)/bench_bpf_loop.o \ 988 $(OUTPUT)/bench_strncmp.o \ 989 $(OUTPUT)/bench_bpf_hashmap_full_update.o \ 990 $(OUTPUT)/bench_local_storage.o \ 991 $(OUTPUT)/bench_local_storage_rcu_tasks_trace.o \ 992 $(OUTPUT)/bench_bpf_hashmap_lookup.o \ 993 $(OUTPUT)/bench_local_storage_create.o \ 994 $(OUTPUT)/bench_htab_mem.o \ 995 $(OUTPUT)/bench_bpf_crypto.o \ 996 $(OUTPUT)/bench_sockmap.o \ 997 $(OUTPUT)/bench_lpm_trie_map.o \ 998 $(OUTPUT)/bench_bpf_timing.o \ 999 $(OUTPUT)/bench_bpf_nop.o \ 1000 $(OUTPUT)/bench_xdp_lb.o \ 1001 $(OUTPUT)/usdt_1.o \ 1002 $(OUTPUT)/usdt_2.o \ 1003 # 1004 $(call msg,BINARY,,$@) 1005 $(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(filter %.a %.o,$^) $(LDLIBS) -o $@ $(if $(PERMISSIVE),|| \ 1006 ($(RM) $@; printf ' %-12s %s\n' 'SKIP-LINK' '$(notdir $@) (some benchmarks may have been skipped)' 1>&2)) 1007 1008# This works around GCC warning about snprintf truncating strings like: 1009# 1010# char a[PATH_MAX], b[PATH_MAX]; 1011# snprintf(a, "%s/foo", b); // triggers -Wformat-truncation 1012$(OUTPUT)/veristat.o: CFLAGS += -Wno-format-truncation 1013$(OUTPUT)/veristat.o: $(BPFOBJ) 1014$(OUTPUT)/veristat: $(OUTPUT)/veristat.o 1015 $(call msg,BINARY,,$@) 1016 $(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(filter %.a %.o,$^) $(LDLIBS) -o $@ 1017 1018# Linking uprobe_multi can fail due to relocation overflows on mips. 1019$(OUTPUT)/uprobe_multi: CFLAGS += $(if $(filter mips, $(ARCH)),-mxgot) 1020$(OUTPUT)/uprobe_multi: uprobe_multi.c uprobe_multi.ld 1021 $(call msg,BINARY,,$@) 1022 $(Q)$(CC) $(CFLAGS) -Wl,-T,uprobe_multi.ld -O0 $(LDFLAGS) \ 1023 $(filter-out %.ld,$^) $(LDLIBS) -o $@ 1024 1025EXTRA_CLEAN := $(SCRATCH_DIR) $(HOST_SCRATCH_DIR) \ 1026 prog_tests/tests.h map_tests/tests.h verifier/tests.h \ 1027 feature bpftool $(TEST_KMOD_TARGETS) \ 1028 $(addprefix $(OUTPUT)/,*.o *.d *.skel.h *.lskel.h *.subskel.h \ 1029 *.BTF *.BTF_ids *.BTF.base \ 1030 no_alu32 cpuv4 bpf_gcc \ 1031 liburandom_read.so) \ 1032 $(OUTPUT)/FEATURE-DUMP.selftests 1033 1034.PHONY: docs docs-clean 1035 1036# Delete partially updated (corrupted) files on error 1037.DELETE_ON_ERROR: 1038 1039# When permissive, tell rsync to ignore missing source arguments so that 1040# partial builds do not abort installation. 1041ifneq ($(PERMISSIVE),) 1042override define INSTALL_SINGLE_RULE 1043 $(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH)) 1044 $(if $(INSTALL_LIST),rsync -a --copy-unsafe-links --ignore-missing-args $(INSTALL_LIST) $(INSTALL_PATH)/) 1045endef 1046endif 1047 1048DEFAULT_INSTALL_RULE := $(INSTALL_RULE) 1049override define INSTALL_RULE 1050 $(DEFAULT_INSTALL_RULE) 1051 @for DIR in $(TEST_INST_SUBDIRS); do \ 1052 mkdir -p $(INSTALL_PATH)/$$DIR; \ 1053 rsync -a $(if $(PERMISSIVE),--ignore-missing-args) \ 1054 $(OUTPUT)/$$DIR/*.bpf.o \ 1055 $(INSTALL_PATH)/$$DIR; \ 1056 done 1057endef 1058 1059libarena: $(LIBARENA_SKEL) 1060 1061ifneq ($(CLANG_HAS_ARENA_ASAN),) 1062libarena_asan: $(LIBARENA_ASAN_SKEL) 1063endif 1064