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