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