| .. | .. |
|---|
| 1 | 1 | # SPDX-License-Identifier: GPL-2.0 |
|---|
| 2 | +include ../../../../scripts/Kbuild.include |
|---|
| 3 | +include ../../../scripts/Makefile.arch |
|---|
| 2 | 4 | |
|---|
| 3 | | -LIBDIR := ../../../lib |
|---|
| 5 | +CXX ?= $(CROSS_COMPILE)g++ |
|---|
| 6 | + |
|---|
| 7 | +CURDIR := $(abspath .) |
|---|
| 8 | +TOOLSDIR := $(abspath ../../..) |
|---|
| 9 | +LIBDIR := $(TOOLSDIR)/lib |
|---|
| 4 | 10 | BPFDIR := $(LIBDIR)/bpf |
|---|
| 5 | | -APIDIR := ../../../include/uapi |
|---|
| 6 | | -GENDIR := ../../../../include/generated |
|---|
| 11 | +TOOLSINCDIR := $(TOOLSDIR)/include |
|---|
| 12 | +BPFTOOLDIR := $(TOOLSDIR)/bpf/bpftool |
|---|
| 13 | +APIDIR := $(TOOLSINCDIR)/uapi |
|---|
| 14 | +GENDIR := $(abspath ../../../../include/generated) |
|---|
| 7 | 15 | GENHDR := $(GENDIR)/autoconf.h |
|---|
| 8 | 16 | |
|---|
| 9 | 17 | ifneq ($(wildcard $(GENHDR)),) |
|---|
| 10 | 18 | GENFLAGS := -DHAVE_GENHDR |
|---|
| 11 | 19 | endif |
|---|
| 12 | 20 | |
|---|
| 13 | | -CFLAGS += -Wall -O2 -I$(APIDIR) -I$(LIBDIR) -I$(BPFDIR) -I$(GENDIR) $(GENFLAGS) -I../../../include |
|---|
| 14 | | -LDLIBS += -lcap -lelf -lrt -lpthread |
|---|
| 15 | | - |
|---|
| 16 | | -TEST_CUSTOM_PROGS = $(OUTPUT)/urandom_read |
|---|
| 17 | | -all: $(TEST_CUSTOM_PROGS) |
|---|
| 18 | | - |
|---|
| 19 | | -$(TEST_CUSTOM_PROGS): $(OUTPUT)/%: %.c |
|---|
| 20 | | - $(CC) -o $(TEST_CUSTOM_PROGS) -static $< -Wl,--build-id |
|---|
| 21 | +CLANG ?= clang |
|---|
| 22 | +LLC ?= llc |
|---|
| 23 | +LLVM_OBJCOPY ?= llvm-objcopy |
|---|
| 24 | +BPF_GCC ?= $(shell command -v bpf-gcc;) |
|---|
| 25 | +SAN_CFLAGS ?= |
|---|
| 26 | +CFLAGS += -g -rdynamic -Wall -O2 $(GENFLAGS) $(SAN_CFLAGS) \ |
|---|
| 27 | + -I$(CURDIR) -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR) \ |
|---|
| 28 | + -I$(TOOLSINCDIR) -I$(APIDIR) \ |
|---|
| 29 | + -Dbpf_prog_load=bpf_prog_test_load \ |
|---|
| 30 | + -Dbpf_load_program=bpf_test_load_program |
|---|
| 31 | +LDLIBS += -lcap -lelf -lz -lrt -lpthread |
|---|
| 21 | 32 | |
|---|
| 22 | 33 | # Order correspond to 'make run_tests' order |
|---|
| 23 | 34 | TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \ |
|---|
| 24 | | - test_align test_verifier_log test_dev_cgroup test_tcpbpf_user \ |
|---|
| 25 | | - test_sock test_btf test_sockmap test_lirc_mode2_user get_cgroup_id_user \ |
|---|
| 26 | | - test_socket_cookie test_cgroup_storage test_select_reuseport |
|---|
| 35 | + test_verifier_log test_dev_cgroup test_tcpbpf_user \ |
|---|
| 36 | + test_sock test_sockmap get_cgroup_id_user test_socket_cookie \ |
|---|
| 37 | + test_cgroup_storage \ |
|---|
| 38 | + test_netcnt test_tcpnotify_user test_sysctl \ |
|---|
| 39 | + test_progs-no_alu32 \ |
|---|
| 40 | + test_current_pid_tgid_new_ns |
|---|
| 27 | 41 | |
|---|
| 28 | | -TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test_obj_id.o \ |
|---|
| 29 | | - test_pkt_md_access.o test_xdp_redirect.o test_xdp_meta.o sockmap_parse_prog.o \ |
|---|
| 30 | | - sockmap_verdict_prog.o dev_cgroup.o sample_ret0.o test_tracepoint.o \ |
|---|
| 31 | | - test_l4lb_noinline.o test_xdp_noinline.o test_stacktrace_map.o \ |
|---|
| 32 | | - sample_map_ret0.o test_tcpbpf_kern.o test_stacktrace_build_id.o \ |
|---|
| 33 | | - sockmap_tcp_msg_prog.o connect4_prog.o connect6_prog.o test_adjust_tail.o \ |
|---|
| 34 | | - test_btf_haskv.o test_btf_nokv.o test_sockmap_kern.o test_tunnel_kern.o \ |
|---|
| 35 | | - test_get_stack_rawtp.o test_sockmap_kern.o test_sockhash_kern.o \ |
|---|
| 36 | | - test_lwt_seg6local.o sendmsg4_prog.o sendmsg6_prog.o test_lirc_mode2_kern.o \ |
|---|
| 37 | | - get_cgroup_id_kern.o socket_cookie_prog.o test_select_reuseport_kern.o \ |
|---|
| 38 | | - test_skb_cgroup_id_kern.o |
|---|
| 42 | +# Also test bpf-gcc, if present |
|---|
| 43 | +ifneq ($(BPF_GCC),) |
|---|
| 44 | +TEST_GEN_PROGS += test_progs-bpf_gcc |
|---|
| 45 | +endif |
|---|
| 46 | + |
|---|
| 47 | +TEST_GEN_FILES = |
|---|
| 48 | +TEST_FILES = test_lwt_ip_encap.o \ |
|---|
| 49 | + test_tc_edt.o |
|---|
| 39 | 50 | |
|---|
| 40 | 51 | # Order correspond to 'make run_tests' order |
|---|
| 41 | 52 | TEST_PROGS := test_kmod.sh \ |
|---|
| 42 | | - test_libbpf.sh \ |
|---|
| 43 | 53 | test_xdp_redirect.sh \ |
|---|
| 44 | 54 | test_xdp_meta.sh \ |
|---|
| 55 | + test_xdp_veth.sh \ |
|---|
| 45 | 56 | test_offload.py \ |
|---|
| 46 | 57 | test_sock_addr.sh \ |
|---|
| 47 | 58 | test_tunnel.sh \ |
|---|
| 48 | 59 | test_lwt_seg6local.sh \ |
|---|
| 49 | 60 | test_lirc_mode2.sh \ |
|---|
| 50 | | - test_skb_cgroup_id.sh |
|---|
| 61 | + test_skb_cgroup_id.sh \ |
|---|
| 62 | + test_flow_dissector.sh \ |
|---|
| 63 | + test_xdp_vlan_mode_generic.sh \ |
|---|
| 64 | + test_xdp_vlan_mode_native.sh \ |
|---|
| 65 | + test_lwt_ip_encap.sh \ |
|---|
| 66 | + test_tcp_check_syncookie.sh \ |
|---|
| 67 | + test_tc_tunnel.sh \ |
|---|
| 68 | + test_tc_edt.sh \ |
|---|
| 69 | + test_xdping.sh \ |
|---|
| 70 | + test_bpftool_build.sh \ |
|---|
| 71 | + test_bpftool.sh \ |
|---|
| 72 | + test_bpftool_metadata.sh \ |
|---|
| 73 | + |
|---|
| 74 | +TEST_PROGS_EXTENDED := with_addr.sh \ |
|---|
| 75 | + with_tunnels.sh \ |
|---|
| 76 | + tcp_client.py \ |
|---|
| 77 | + tcp_server.py \ |
|---|
| 78 | + test_xdp_vlan.sh |
|---|
| 51 | 79 | |
|---|
| 52 | 80 | # Compile but not part of 'make run_tests' |
|---|
| 53 | | -TEST_GEN_PROGS_EXTENDED = test_libbpf_open test_sock_addr test_skb_cgroup_id_user |
|---|
| 81 | +TEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \ |
|---|
| 82 | + flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \ |
|---|
| 83 | + test_lirc_mode2_user xdping test_cpp runqslower bench |
|---|
| 84 | + |
|---|
| 85 | +TEST_CUSTOM_PROGS = urandom_read |
|---|
| 86 | + |
|---|
| 87 | +# Emit succinct information message describing current building step |
|---|
| 88 | +# $1 - generic step name (e.g., CC, LINK, etc); |
|---|
| 89 | +# $2 - optional "flavor" specifier; if provided, will be emitted as [flavor]; |
|---|
| 90 | +# $3 - target (assumed to be file); only file name will be emitted; |
|---|
| 91 | +# $4 - optional extra arg, emitted as-is, if provided. |
|---|
| 92 | +ifeq ($(V),1) |
|---|
| 93 | +Q = |
|---|
| 94 | +msg = |
|---|
| 95 | +else |
|---|
| 96 | +Q = @ |
|---|
| 97 | +msg = @printf ' %-8s%s %s%s\n' "$(1)" "$(if $(2), [$(2)])" "$(notdir $(3))" "$(if $(4), $(4))"; |
|---|
| 98 | +MAKEFLAGS += --no-print-directory |
|---|
| 99 | +submake_extras := feature_display=0 |
|---|
| 100 | +endif |
|---|
| 101 | + |
|---|
| 102 | +# override lib.mk's default rules |
|---|
| 103 | +OVERRIDE_TARGETS := 1 |
|---|
| 104 | +override define CLEAN |
|---|
| 105 | + $(call msg,CLEAN) |
|---|
| 106 | + $(Q)$(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN) |
|---|
| 107 | +endef |
|---|
| 54 | 108 | |
|---|
| 55 | 109 | include ../lib.mk |
|---|
| 56 | 110 | |
|---|
| 57 | | -BPFOBJ := $(OUTPUT)/libbpf.a |
|---|
| 111 | +SCRATCH_DIR := $(OUTPUT)/tools |
|---|
| 112 | +BUILD_DIR := $(SCRATCH_DIR)/build |
|---|
| 113 | +INCLUDE_DIR := $(SCRATCH_DIR)/include |
|---|
| 114 | +BPFOBJ := $(BUILD_DIR)/libbpf/libbpf.a |
|---|
| 115 | +RESOLVE_BTFIDS := $(BUILD_DIR)/resolve_btfids/resolve_btfids |
|---|
| 58 | 116 | |
|---|
| 59 | | -$(TEST_GEN_PROGS): $(BPFOBJ) |
|---|
| 117 | +# Define simple and short `make test_progs`, `make test_sysctl`, etc targets |
|---|
| 118 | +# to build individual tests. |
|---|
| 119 | +# NOTE: Semicolon at the end is critical to override lib.mk's default static |
|---|
| 120 | +# rule for binaries. |
|---|
| 121 | +$(notdir $(TEST_GEN_PROGS) \ |
|---|
| 122 | + $(TEST_GEN_PROGS_EXTENDED) \ |
|---|
| 123 | + $(TEST_CUSTOM_PROGS)): %: $(OUTPUT)/% ; |
|---|
| 60 | 124 | |
|---|
| 61 | | -$(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/libbpf.a |
|---|
| 125 | +$(OUTPUT)/%.o: %.c |
|---|
| 126 | + $(call msg,CC,,$@) |
|---|
| 127 | + $(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@ |
|---|
| 128 | + |
|---|
| 129 | +$(OUTPUT)/%:%.c |
|---|
| 130 | + $(call msg,BINARY,,$@) |
|---|
| 131 | + $(Q)$(LINK.c) $^ $(LDLIBS) -o $@ |
|---|
| 132 | + |
|---|
| 133 | +$(OUTPUT)/urandom_read: urandom_read.c |
|---|
| 134 | + $(call msg,BINARY,,$@) |
|---|
| 135 | + $(Q)$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS) -Wl,--build-id=sha1 |
|---|
| 136 | + |
|---|
| 137 | +$(OUTPUT)/test_stub.o: test_stub.c $(BPFOBJ) |
|---|
| 138 | + $(call msg,CC,,$@) |
|---|
| 139 | + $(Q)$(CC) -c $(CFLAGS) -o $@ $< |
|---|
| 140 | + |
|---|
| 141 | +VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \ |
|---|
| 142 | + $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \ |
|---|
| 143 | + ../../../../vmlinux \ |
|---|
| 144 | + /sys/kernel/btf/vmlinux \ |
|---|
| 145 | + /boot/vmlinux-$(shell uname -r) |
|---|
| 146 | +VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS)))) |
|---|
| 147 | +ifeq ($(VMLINUX_BTF),) |
|---|
| 148 | +$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)") |
|---|
| 149 | +endif |
|---|
| 150 | + |
|---|
| 151 | +DEFAULT_BPFTOOL := $(SCRATCH_DIR)/sbin/bpftool |
|---|
| 152 | + |
|---|
| 153 | +$(OUTPUT)/runqslower: $(BPFOBJ) | $(DEFAULT_BPFTOOL) |
|---|
| 154 | + $(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/runqslower \ |
|---|
| 155 | + OUTPUT=$(SCRATCH_DIR)/ VMLINUX_BTF=$(VMLINUX_BTF) \ |
|---|
| 156 | + BPFOBJ=$(BPFOBJ) BPF_INCLUDE=$(INCLUDE_DIR) && \ |
|---|
| 157 | + cp $(SCRATCH_DIR)/runqslower $@ |
|---|
| 158 | + |
|---|
| 159 | +$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/test_stub.o $(BPFOBJ) |
|---|
| 62 | 160 | |
|---|
| 63 | 161 | $(OUTPUT)/test_dev_cgroup: cgroup_helpers.c |
|---|
| 64 | 162 | $(OUTPUT)/test_skb_cgroup_id_user: cgroup_helpers.c |
|---|
| .. | .. |
|---|
| 67 | 165 | $(OUTPUT)/test_socket_cookie: cgroup_helpers.c |
|---|
| 68 | 166 | $(OUTPUT)/test_sockmap: cgroup_helpers.c |
|---|
| 69 | 167 | $(OUTPUT)/test_tcpbpf_user: cgroup_helpers.c |
|---|
| 70 | | -$(OUTPUT)/test_progs: trace_helpers.c |
|---|
| 168 | +$(OUTPUT)/test_tcpnotify_user: cgroup_helpers.c trace_helpers.c |
|---|
| 71 | 169 | $(OUTPUT)/get_cgroup_id_user: cgroup_helpers.c |
|---|
| 72 | 170 | $(OUTPUT)/test_cgroup_storage: cgroup_helpers.c |
|---|
| 171 | +$(OUTPUT)/test_netcnt: cgroup_helpers.c |
|---|
| 172 | +$(OUTPUT)/test_sock_fields: cgroup_helpers.c |
|---|
| 173 | +$(OUTPUT)/test_sysctl: cgroup_helpers.c |
|---|
| 73 | 174 | |
|---|
| 74 | | -.PHONY: force |
|---|
| 175 | +BPFTOOL ?= $(DEFAULT_BPFTOOL) |
|---|
| 176 | +$(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) \ |
|---|
| 177 | + $(BPFOBJ) | $(BUILD_DIR)/bpftool |
|---|
| 178 | + $(Q)$(MAKE) $(submake_extras) -C $(BPFTOOLDIR) \ |
|---|
| 179 | + OUTPUT=$(BUILD_DIR)/bpftool/ \ |
|---|
| 180 | + prefix= DESTDIR=$(SCRATCH_DIR)/ install |
|---|
| 181 | + $(Q)mkdir -p $(BUILD_DIR)/bpftool/Documentation |
|---|
| 182 | + $(Q)RST2MAN_OPTS="--exit-status=1" $(MAKE) $(submake_extras) \ |
|---|
| 183 | + -C $(BPFTOOLDIR)/Documentation \ |
|---|
| 184 | + OUTPUT=$(BUILD_DIR)/bpftool/Documentation/ \ |
|---|
| 185 | + prefix= DESTDIR=$(SCRATCH_DIR)/ install |
|---|
| 75 | 186 | |
|---|
| 76 | | -# force a rebuild of BPFOBJ when its dependencies are updated |
|---|
| 77 | | -force: |
|---|
| 187 | +$(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile) \ |
|---|
| 188 | + ../../../include/uapi/linux/bpf.h \ |
|---|
| 189 | + | $(INCLUDE_DIR) $(BUILD_DIR)/libbpf |
|---|
| 190 | + $(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(BUILD_DIR)/libbpf/ \ |
|---|
| 191 | + DESTDIR=$(SCRATCH_DIR) prefix= all install_headers |
|---|
| 78 | 192 | |
|---|
| 79 | | -$(BPFOBJ): force |
|---|
| 80 | | - $(MAKE) -C $(BPFDIR) OUTPUT=$(OUTPUT)/ |
|---|
| 193 | +$(BUILD_DIR)/libbpf $(BUILD_DIR)/bpftool $(BUILD_DIR)/resolve_btfids $(INCLUDE_DIR): |
|---|
| 194 | + $(call msg,MKDIR,,$@) |
|---|
| 195 | + $(Q)mkdir -p $@ |
|---|
| 81 | 196 | |
|---|
| 82 | | -CLANG ?= clang |
|---|
| 83 | | -LLC ?= llc |
|---|
| 84 | | -LLVM_OBJCOPY ?= llvm-objcopy |
|---|
| 85 | | -BTF_PAHOLE ?= pahole |
|---|
| 86 | | - |
|---|
| 87 | | -PROBE := $(shell $(LLC) -march=bpf -mcpu=probe -filetype=null /dev/null 2>&1) |
|---|
| 88 | | - |
|---|
| 89 | | -# Let newer LLVM versions transparently probe the kernel for availability |
|---|
| 90 | | -# of full BPF instruction set. |
|---|
| 91 | | -ifeq ($(PROBE),) |
|---|
| 92 | | - CPU ?= probe |
|---|
| 197 | +$(INCLUDE_DIR)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL) | $(INCLUDE_DIR) |
|---|
| 198 | +ifeq ($(VMLINUX_H),) |
|---|
| 199 | + $(call msg,GEN,,$@) |
|---|
| 200 | + $(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@ |
|---|
| 93 | 201 | else |
|---|
| 94 | | - CPU ?= generic |
|---|
| 202 | + $(call msg,CP,,$@) |
|---|
| 203 | + $(Q)cp "$(VMLINUX_H)" $@ |
|---|
| 95 | 204 | endif |
|---|
| 205 | + |
|---|
| 206 | +$(RESOLVE_BTFIDS): $(BPFOBJ) | $(BUILD_DIR)/resolve_btfids \ |
|---|
| 207 | + $(TOOLSDIR)/bpf/resolve_btfids/main.c \ |
|---|
| 208 | + $(TOOLSDIR)/lib/rbtree.c \ |
|---|
| 209 | + $(TOOLSDIR)/lib/zalloc.c \ |
|---|
| 210 | + $(TOOLSDIR)/lib/string.c \ |
|---|
| 211 | + $(TOOLSDIR)/lib/ctype.c \ |
|---|
| 212 | + $(TOOLSDIR)/lib/str_error_r.c |
|---|
| 213 | + $(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/resolve_btfids \ |
|---|
| 214 | + OUTPUT=$(BUILD_DIR)/resolve_btfids/ BPFOBJ=$(BPFOBJ) |
|---|
| 96 | 215 | |
|---|
| 97 | 216 | # Get Clang's default includes on this system, as opposed to those seen by |
|---|
| 98 | 217 | # '-target bpf'. This fixes "missing" files on some architectures/distros, |
|---|
| .. | .. |
|---|
| 100 | 219 | # |
|---|
| 101 | 220 | # Use '-idirafter': Don't interfere with include mechanics except where the |
|---|
| 102 | 221 | # build would have failed anyways. |
|---|
| 103 | | -CLANG_SYS_INCLUDES := $(shell $(CLANG) -v -E - </dev/null 2>&1 \ |
|---|
| 104 | | - | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') |
|---|
| 222 | +define get_sys_includes |
|---|
| 223 | +$(shell $(1) -v -E - </dev/null 2>&1 \ |
|---|
| 224 | + | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \ |
|---|
| 225 | +$(shell $(1) -dM -E - </dev/null | grep '#define __riscv_xlen ' | sed 's/#define /-D/' | sed 's/ /=/') |
|---|
| 226 | +endef |
|---|
| 105 | 227 | |
|---|
| 106 | | -CLANG_FLAGS = -I. -I./include/uapi -I../../../include/uapi \ |
|---|
| 107 | | - $(CLANG_SYS_INCLUDES) \ |
|---|
| 108 | | - -Wno-compare-distinct-pointer-types |
|---|
| 228 | +# Determine target endianness. |
|---|
| 229 | +IS_LITTLE_ENDIAN = $(shell $(CC) -dM -E - </dev/null | \ |
|---|
| 230 | + grep 'define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__') |
|---|
| 231 | +MENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian) |
|---|
| 109 | 232 | |
|---|
| 110 | | -$(OUTPUT)/test_l4lb_noinline.o: CLANG_FLAGS += -fno-inline |
|---|
| 111 | | -$(OUTPUT)/test_xdp_noinline.o: CLANG_FLAGS += -fno-inline |
|---|
| 233 | +CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG)) |
|---|
| 234 | +BPF_CFLAGS = -g -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN) \ |
|---|
| 235 | + -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR) \ |
|---|
| 236 | + -I$(abspath $(OUTPUT)/../usr/include) |
|---|
| 112 | 237 | |
|---|
| 113 | | -BTF_LLC_PROBE := $(shell $(LLC) -march=bpf -mattr=help 2>&1 | grep dwarfris) |
|---|
| 114 | | -BTF_PAHOLE_PROBE := $(shell $(BTF_PAHOLE) --help 2>&1 | grep BTF) |
|---|
| 115 | | -BTF_OBJCOPY_PROBE := $(shell $(LLVM_OBJCOPY) --help 2>&1 | grep -i 'usage.*llvm') |
|---|
| 238 | +CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \ |
|---|
| 239 | + -Wno-compare-distinct-pointer-types |
|---|
| 116 | 240 | |
|---|
| 117 | | -ifneq ($(BTF_LLC_PROBE),) |
|---|
| 118 | | -ifneq ($(BTF_PAHOLE_PROBE),) |
|---|
| 119 | | -ifneq ($(BTF_OBJCOPY_PROBE),) |
|---|
| 120 | | - CLANG_FLAGS += -g |
|---|
| 121 | | - LLC_FLAGS += -mattr=dwarfris |
|---|
| 122 | | - DWARF2BTF = y |
|---|
| 123 | | -endif |
|---|
| 124 | | -endif |
|---|
| 241 | +$(OUTPUT)/test_l4lb_noinline.o: BPF_CFLAGS += -fno-inline |
|---|
| 242 | +$(OUTPUT)/test_xdp_noinline.o: BPF_CFLAGS += -fno-inline |
|---|
| 243 | + |
|---|
| 244 | +$(OUTPUT)/flow_dissector_load.o: flow_dissector_load.h |
|---|
| 245 | + |
|---|
| 246 | +# Build BPF object using Clang |
|---|
| 247 | +# $1 - input .c file |
|---|
| 248 | +# $2 - output .o file |
|---|
| 249 | +# $3 - CFLAGS |
|---|
| 250 | +# $4 - LDFLAGS |
|---|
| 251 | +define CLANG_BPF_BUILD_RULE |
|---|
| 252 | + $(call msg,CLNG-LLC,$(TRUNNER_BINARY),$2) |
|---|
| 253 | + $(Q)($(CLANG) $3 -O2 -target bpf -emit-llvm \ |
|---|
| 254 | + -c $1 -o - || echo "BPF obj compilation failed") | \ |
|---|
| 255 | + $(LLC) -mattr=dwarfris -march=bpf -mcpu=v3 $4 -filetype=obj -o $2 |
|---|
| 256 | +endef |
|---|
| 257 | +# Similar to CLANG_BPF_BUILD_RULE, but with disabled alu32 |
|---|
| 258 | +define CLANG_NOALU32_BPF_BUILD_RULE |
|---|
| 259 | + $(call msg,CLNG-LLC,$(TRUNNER_BINARY),$2) |
|---|
| 260 | + $(Q)($(CLANG) $3 -O2 -target bpf -emit-llvm \ |
|---|
| 261 | + -c $1 -o - || echo "BPF obj compilation failed") | \ |
|---|
| 262 | + $(LLC) -march=bpf -mcpu=v2 $4 -filetype=obj -o $2 |
|---|
| 263 | +endef |
|---|
| 264 | +# Similar to CLANG_BPF_BUILD_RULE, but using native Clang and bpf LLC |
|---|
| 265 | +define CLANG_NATIVE_BPF_BUILD_RULE |
|---|
| 266 | + $(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2) |
|---|
| 267 | + $(Q)($(CLANG) $3 -O2 -emit-llvm \ |
|---|
| 268 | + -c $1 -o - || echo "BPF obj compilation failed") | \ |
|---|
| 269 | + $(LLC) -march=bpf -mcpu=v3 $4 -filetype=obj -o $2 |
|---|
| 270 | +endef |
|---|
| 271 | +# Build BPF object using GCC |
|---|
| 272 | +define GCC_BPF_BUILD_RULE |
|---|
| 273 | + $(call msg,GCC-BPF,$(TRUNNER_BINARY),$2) |
|---|
| 274 | + $(Q)$(BPF_GCC) $3 $4 -O2 -c $1 -o $2 |
|---|
| 275 | +endef |
|---|
| 276 | + |
|---|
| 277 | +SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c |
|---|
| 278 | + |
|---|
| 279 | +# Set up extra TRUNNER_XXX "temporary" variables in the environment (relies on |
|---|
| 280 | +# $eval()) and pass control to DEFINE_TEST_RUNNER_RULES. |
|---|
| 281 | +# Parameters: |
|---|
| 282 | +# $1 - test runner base binary name (e.g., test_progs) |
|---|
| 283 | +# $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc) |
|---|
| 284 | +define DEFINE_TEST_RUNNER |
|---|
| 285 | + |
|---|
| 286 | +TRUNNER_OUTPUT := $(OUTPUT)$(if $2,/)$2 |
|---|
| 287 | +TRUNNER_BINARY := $1$(if $2,-)$2 |
|---|
| 288 | +TRUNNER_TEST_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.test.o, \ |
|---|
| 289 | + $$(notdir $$(wildcard $(TRUNNER_TESTS_DIR)/*.c))) |
|---|
| 290 | +TRUNNER_EXTRA_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, \ |
|---|
| 291 | + $$(filter %.c,$(TRUNNER_EXTRA_SOURCES))) |
|---|
| 292 | +TRUNNER_EXTRA_HDRS := $$(filter %.h,$(TRUNNER_EXTRA_SOURCES)) |
|---|
| 293 | +TRUNNER_TESTS_HDR := $(TRUNNER_TESTS_DIR)/tests.h |
|---|
| 294 | +TRUNNER_BPF_SRCS := $$(notdir $$(wildcard $(TRUNNER_BPF_PROGS_DIR)/*.c)) |
|---|
| 295 | +TRUNNER_BPF_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, $$(TRUNNER_BPF_SRCS)) |
|---|
| 296 | +TRUNNER_BPF_SKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.skel.h, \ |
|---|
| 297 | + $$(filter-out $(SKEL_BLACKLIST), \ |
|---|
| 298 | + $$(TRUNNER_BPF_SRCS))) |
|---|
| 299 | +TEST_GEN_FILES += $$(TRUNNER_BPF_OBJS) |
|---|
| 300 | + |
|---|
| 301 | +# Evaluate rules now with extra TRUNNER_XXX variables above already defined |
|---|
| 302 | +$$(eval $$(call DEFINE_TEST_RUNNER_RULES,$1,$2)) |
|---|
| 303 | + |
|---|
| 304 | +endef |
|---|
| 305 | + |
|---|
| 306 | +# Using TRUNNER_XXX variables, provided by callers of DEFINE_TEST_RUNNER and |
|---|
| 307 | +# set up by DEFINE_TEST_RUNNER itself, create test runner build rules with: |
|---|
| 308 | +# $1 - test runner base binary name (e.g., test_progs) |
|---|
| 309 | +# $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc) |
|---|
| 310 | +define DEFINE_TEST_RUNNER_RULES |
|---|
| 311 | + |
|---|
| 312 | +ifeq ($($(TRUNNER_OUTPUT)-dir),) |
|---|
| 313 | +$(TRUNNER_OUTPUT)-dir := y |
|---|
| 314 | +$(TRUNNER_OUTPUT): |
|---|
| 315 | + $$(call msg,MKDIR,,$$@) |
|---|
| 316 | + $(Q)mkdir -p $$@ |
|---|
| 125 | 317 | endif |
|---|
| 126 | 318 | |
|---|
| 127 | | -# Have one program compiled without "-target bpf" to test whether libbpf loads |
|---|
| 128 | | -# it successfully |
|---|
| 129 | | -$(OUTPUT)/test_xdp.o: test_xdp.c |
|---|
| 130 | | - $(CLANG) $(CLANG_FLAGS) \ |
|---|
| 131 | | - -O2 -emit-llvm -c $< -o - | \ |
|---|
| 132 | | - $(LLC) -march=bpf -mcpu=$(CPU) $(LLC_FLAGS) -filetype=obj -o $@ |
|---|
| 133 | | -ifeq ($(DWARF2BTF),y) |
|---|
| 134 | | - $(BTF_PAHOLE) -J $@ |
|---|
| 319 | +# ensure we set up BPF objects generation rule just once for a given |
|---|
| 320 | +# input/output directory combination |
|---|
| 321 | +ifeq ($($(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs),) |
|---|
| 322 | +$(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs := y |
|---|
| 323 | +$(TRUNNER_BPF_OBJS): $(TRUNNER_OUTPUT)/%.o: \ |
|---|
| 324 | + $(TRUNNER_BPF_PROGS_DIR)/%.c \ |
|---|
| 325 | + $(TRUNNER_BPF_PROGS_DIR)/*.h \ |
|---|
| 326 | + $$(INCLUDE_DIR)/vmlinux.h \ |
|---|
| 327 | + $(wildcard $(BPFDIR)/bpf_*.h) \ |
|---|
| 328 | + | $(TRUNNER_OUTPUT) $$(BPFOBJ) |
|---|
| 329 | + $$(call $(TRUNNER_BPF_BUILD_RULE),$$<,$$@, \ |
|---|
| 330 | + $(TRUNNER_BPF_CFLAGS), \ |
|---|
| 331 | + $(TRUNNER_BPF_LDFLAGS)) |
|---|
| 332 | + |
|---|
| 333 | +$(TRUNNER_BPF_SKELS): $(TRUNNER_OUTPUT)/%.skel.h: \ |
|---|
| 334 | + $(TRUNNER_OUTPUT)/%.o \ |
|---|
| 335 | + $(BPFTOOL) \ |
|---|
| 336 | + | $(TRUNNER_OUTPUT) |
|---|
| 337 | + $$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@) |
|---|
| 338 | + $(Q)$$(BPFTOOL) gen skeleton $$< > $$@ |
|---|
| 135 | 339 | endif |
|---|
| 136 | 340 | |
|---|
| 137 | | -$(OUTPUT)/%.o: %.c |
|---|
| 138 | | - $(CLANG) $(CLANG_FLAGS) \ |
|---|
| 139 | | - -O2 -target bpf -emit-llvm -c $< -o - | \ |
|---|
| 140 | | - $(LLC) -march=bpf -mcpu=$(CPU) $(LLC_FLAGS) -filetype=obj -o $@ |
|---|
| 141 | | -ifeq ($(DWARF2BTF),y) |
|---|
| 142 | | - $(BTF_PAHOLE) -J $@ |
|---|
| 341 | +# ensure we set up tests.h header generation rule just once |
|---|
| 342 | +ifeq ($($(TRUNNER_TESTS_DIR)-tests-hdr),) |
|---|
| 343 | +$(TRUNNER_TESTS_DIR)-tests-hdr := y |
|---|
| 344 | +$(TRUNNER_TESTS_HDR): $(TRUNNER_TESTS_DIR)/*.c |
|---|
| 345 | + $$(call msg,TEST-HDR,$(TRUNNER_BINARY),$$@) |
|---|
| 346 | + $$(shell ( cd $(TRUNNER_TESTS_DIR); \ |
|---|
| 347 | + echo '/* Generated header, do not edit */'; \ |
|---|
| 348 | + ls *.c 2> /dev/null | \ |
|---|
| 349 | + sed -e 's@\([^\.]*\)\.c@DEFINE_TEST(\1)@'; \ |
|---|
| 350 | + ) > $$@) |
|---|
| 143 | 351 | endif |
|---|
| 144 | 352 | |
|---|
| 145 | | -EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) |
|---|
| 353 | +# compile individual test files |
|---|
| 354 | +# Note: we cd into output directory to ensure embedded BPF object is found |
|---|
| 355 | +$(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o: \ |
|---|
| 356 | + $(TRUNNER_TESTS_DIR)/%.c \ |
|---|
| 357 | + $(TRUNNER_EXTRA_HDRS) \ |
|---|
| 358 | + $(TRUNNER_BPF_OBJS) \ |
|---|
| 359 | + $(TRUNNER_BPF_SKELS) \ |
|---|
| 360 | + $$(BPFOBJ) | $(TRUNNER_OUTPUT) |
|---|
| 361 | + $$(call msg,TEST-OBJ,$(TRUNNER_BINARY),$$@) |
|---|
| 362 | + $(Q)cd $$(@D) && $$(CC) -I. $$(CFLAGS) -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F) |
|---|
| 363 | + |
|---|
| 364 | +$(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o: \ |
|---|
| 365 | + %.c \ |
|---|
| 366 | + $(TRUNNER_EXTRA_HDRS) \ |
|---|
| 367 | + $(TRUNNER_TESTS_HDR) \ |
|---|
| 368 | + $$(BPFOBJ) | $(TRUNNER_OUTPUT) |
|---|
| 369 | + $$(call msg,EXT-OBJ,$(TRUNNER_BINARY),$$@) |
|---|
| 370 | + $(Q)$$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@ |
|---|
| 371 | + |
|---|
| 372 | +# only copy extra resources if in flavored build |
|---|
| 373 | +$(TRUNNER_BINARY)-extras: $(TRUNNER_EXTRA_FILES) | $(TRUNNER_OUTPUT) |
|---|
| 374 | +ifneq ($2,) |
|---|
| 375 | + $$(call msg,EXT-COPY,$(TRUNNER_BINARY),$(TRUNNER_EXTRA_FILES)) |
|---|
| 376 | + $(Q)cp -a $$^ $(TRUNNER_OUTPUT)/ |
|---|
| 377 | +endif |
|---|
| 378 | + |
|---|
| 379 | +$(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS) \ |
|---|
| 380 | + $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ) \ |
|---|
| 381 | + $(RESOLVE_BTFIDS) \ |
|---|
| 382 | + | $(TRUNNER_BINARY)-extras |
|---|
| 383 | + $$(call msg,BINARY,,$$@) |
|---|
| 384 | + $(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@ |
|---|
| 385 | + $(Q)$(RESOLVE_BTFIDS) --no-fail --btf btf_data.o $$@ |
|---|
| 386 | + |
|---|
| 387 | +endef |
|---|
| 388 | + |
|---|
| 389 | +# Define test_progs test runner. |
|---|
| 390 | +TRUNNER_TESTS_DIR := prog_tests |
|---|
| 391 | +TRUNNER_BPF_PROGS_DIR := progs |
|---|
| 392 | +TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c \ |
|---|
| 393 | + network_helpers.c testing_helpers.c \ |
|---|
| 394 | + flow_dissector_load.h |
|---|
| 395 | +TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \ |
|---|
| 396 | + $(wildcard progs/btf_dump_test_case_*.c) |
|---|
| 397 | +TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE |
|---|
| 398 | +TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS) |
|---|
| 399 | +TRUNNER_BPF_LDFLAGS := -mattr=+alu32 |
|---|
| 400 | +$(eval $(call DEFINE_TEST_RUNNER,test_progs)) |
|---|
| 401 | + |
|---|
| 402 | +# Define test_progs-no_alu32 test runner. |
|---|
| 403 | +TRUNNER_BPF_BUILD_RULE := CLANG_NOALU32_BPF_BUILD_RULE |
|---|
| 404 | +TRUNNER_BPF_LDFLAGS := |
|---|
| 405 | +$(eval $(call DEFINE_TEST_RUNNER,test_progs,no_alu32)) |
|---|
| 406 | + |
|---|
| 407 | +# Define test_progs BPF-GCC-flavored test runner. |
|---|
| 408 | +ifneq ($(BPF_GCC),) |
|---|
| 409 | +TRUNNER_BPF_BUILD_RULE := GCC_BPF_BUILD_RULE |
|---|
| 410 | +TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(call get_sys_includes,gcc) |
|---|
| 411 | +TRUNNER_BPF_LDFLAGS := |
|---|
| 412 | +$(eval $(call DEFINE_TEST_RUNNER,test_progs,bpf_gcc)) |
|---|
| 413 | +endif |
|---|
| 414 | + |
|---|
| 415 | +# Define test_maps test runner. |
|---|
| 416 | +TRUNNER_TESTS_DIR := map_tests |
|---|
| 417 | +TRUNNER_BPF_PROGS_DIR := progs |
|---|
| 418 | +TRUNNER_EXTRA_SOURCES := test_maps.c |
|---|
| 419 | +TRUNNER_EXTRA_FILES := |
|---|
| 420 | +TRUNNER_BPF_BUILD_RULE := $$(error no BPF objects should be built) |
|---|
| 421 | +TRUNNER_BPF_CFLAGS := |
|---|
| 422 | +TRUNNER_BPF_LDFLAGS := |
|---|
| 423 | +$(eval $(call DEFINE_TEST_RUNNER,test_maps)) |
|---|
| 424 | + |
|---|
| 425 | +# Define test_verifier test runner. |
|---|
| 426 | +# It is much simpler than test_maps/test_progs and sufficiently different from |
|---|
| 427 | +# them (e.g., test.h is using completely pattern), that it's worth just |
|---|
| 428 | +# explicitly defining all the rules explicitly. |
|---|
| 429 | +verifier/tests.h: verifier/*.c |
|---|
| 430 | + $(shell ( cd verifier/; \ |
|---|
| 431 | + echo '/* Generated header, do not edit */'; \ |
|---|
| 432 | + echo '#ifdef FILL_ARRAY'; \ |
|---|
| 433 | + ls *.c 2> /dev/null | sed -e 's@\(.*\)@#include \"\1\"@'; \ |
|---|
| 434 | + echo '#endif' \ |
|---|
| 435 | + ) > verifier/tests.h) |
|---|
| 436 | +$(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT) |
|---|
| 437 | + $(call msg,BINARY,,$@) |
|---|
| 438 | + $(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@ |
|---|
| 439 | + |
|---|
| 440 | +# Make sure we are able to include and link libbpf against c++. |
|---|
| 441 | +$(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ) |
|---|
| 442 | + $(call msg,CXX,,$@) |
|---|
| 443 | + $(Q)$(CXX) $(CFLAGS) $^ $(LDLIBS) -o $@ |
|---|
| 444 | + |
|---|
| 445 | +# Benchmark runner |
|---|
| 446 | +$(OUTPUT)/bench_%.o: benchs/bench_%.c bench.h |
|---|
| 447 | + $(call msg,CC,,$@) |
|---|
| 448 | + $(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@ |
|---|
| 449 | +$(OUTPUT)/bench_rename.o: $(OUTPUT)/test_overhead.skel.h |
|---|
| 450 | +$(OUTPUT)/bench_trigger.o: $(OUTPUT)/trigger_bench.skel.h |
|---|
| 451 | +$(OUTPUT)/bench_ringbufs.o: $(OUTPUT)/ringbuf_bench.skel.h \ |
|---|
| 452 | + $(OUTPUT)/perfbuf_bench.skel.h |
|---|
| 453 | +$(OUTPUT)/bench.o: bench.h testing_helpers.h |
|---|
| 454 | +$(OUTPUT)/bench: LDLIBS += -lm |
|---|
| 455 | +$(OUTPUT)/bench: $(OUTPUT)/bench.o $(OUTPUT)/testing_helpers.o \ |
|---|
| 456 | + $(OUTPUT)/bench_count.o \ |
|---|
| 457 | + $(OUTPUT)/bench_rename.o \ |
|---|
| 458 | + $(OUTPUT)/bench_trigger.o \ |
|---|
| 459 | + $(OUTPUT)/bench_ringbufs.o |
|---|
| 460 | + $(call msg,BINARY,,$@) |
|---|
| 461 | + $(Q)$(CC) $(LDFLAGS) -o $@ $(filter %.a %.o,$^) $(LDLIBS) |
|---|
| 462 | + |
|---|
| 463 | +EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(SCRATCH_DIR) \ |
|---|
| 464 | + prog_tests/tests.h map_tests/tests.h verifier/tests.h \ |
|---|
| 465 | + feature \ |
|---|
| 466 | + $(addprefix $(OUTPUT)/,*.o *.skel.h no_alu32 bpf_gcc) |
|---|