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