blob: 894192c319fbec2f9bcee8683661dda8299e1a16 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001# SPDX-License-Identifier: GPL-2.0
Ilya Leoshkevich9cae4ac2019-07-11 11:12:49 +02002include ../../../../scripts/Kbuild.include
Ilya Leoshkevich748e50c2019-07-11 16:29:27 +02003include ../../../scripts/Makefile.arch
Hendrik Brueckner618e1652017-12-04 10:56:48 +01004
Ilya Leoshkeviche7ad28e2020-06-02 19:56:49 +02005CXX ?= $(CROSS_COMPILE)g++
6
Andrii Nakryiko74b5a592019-10-15 23:00:49 -07007CURDIR := $(abspath .)
Andrii Nakryikof3c926a2019-12-13 17:43:38 -08008TOOLSDIR := $(abspath ../../..)
9LIBDIR := $(TOOLSDIR)/lib
Zi Shen Lime8f1f342017-03-19 23:03:14 -070010BPFDIR := $(LIBDIR)/bpf
Andrii Nakryikof3c926a2019-12-13 17:43:38 -080011TOOLSINCDIR := $(TOOLSDIR)/include
12BPFTOOLDIR := $(TOOLSDIR)/bpf/bpftool
13APIDIR := $(TOOLSINCDIR)/uapi
Andrii Nakryiko74b5a592019-10-15 23:00:49 -070014GENDIR := $(abspath ../../../../include/generated)
Daniel Borkmann02ea80b2017-03-31 02:24:04 +020015GENHDR := $(GENDIR)/autoconf.h
Mickaël Salaünd498f872017-02-11 23:20:23 +010016
Daniel Borkmann02ea80b2017-03-31 02:24:04 +020017ifneq ($(wildcard $(GENHDR)),)
18 GENFLAGS := -DHAVE_GENHDR
19endif
20
Jiong Wang3ef84342019-01-26 12:26:14 -050021CLANG ?= clang
22LLC ?= llc
23LLVM_OBJCOPY ?= llvm-objcopy
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +020024BPF_GCC ?= $(shell command -v bpf-gcc;)
Andrii Nakryiko02995dd2020-04-28 18:21:02 -070025SAN_CFLAGS ?=
26CFLAGS += -g -rdynamic -Wall -O2 $(GENFLAGS) $(SAN_CFLAGS) \
27 -I$(CURDIR) -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR) \
28 -I$(TOOLSINCDIR) -I$(APIDIR) \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -070029 -Dbpf_prog_load=bpf_prog_test_load \
Jiong Wang9d120b42019-05-24 23:25:21 +010030 -Dbpf_load_program=bpf_test_load_program
Andrii Nakryiko166750b2019-12-13 17:47:08 -080031LDLIBS += -lcap -lelf -lz -lrt -lpthread
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +020032
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +010033# Order correspond to 'make run_tests' order
David S. Miller18b3ad92017-05-10 11:43:51 -070034TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \
Alexander Duyckaaf376b2020-11-03 13:34:48 -080035 test_verifier_log test_dev_cgroup \
Andrii Nakryikoc64779e2020-09-15 17:48:19 -070036 test_sock test_sockmap get_cgroup_id_user test_socket_cookie \
Jakub Sitnicki415bb4e2019-12-12 11:22:58 +010037 test_cgroup_storage \
Martin KaFai Lau6f521a22020-09-24 17:04:27 -070038 test_netcnt test_tcpnotify_user test_sysctl \
Carlos Neira1c1052e2020-03-04 17:41:57 -030039 test_progs-no_alu32 \
40 test_current_pid_tgid_new_ns
Jiong Wang3ef84342019-01-26 12:26:14 -050041
Andrii Nakryiko74b5a592019-10-15 23:00:49 -070042# Also test bpf-gcc, if present
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +020043ifneq ($(BPF_GCC),)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -070044TEST_GEN_PROGS += test_progs-bpf_gcc
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +020045endif
46
Andrii Nakryiko74b5a592019-10-15 23:00:49 -070047TEST_GEN_FILES =
Anders Roxelle47a1792019-11-11 17:17:28 +010048TEST_FILES = test_lwt_ip_encap.o \
49 test_tc_edt.o
Andrii Nakryiko74b5a592019-10-15 23:00:49 -070050
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +010051# Order correspond to 'make run_tests' order
52TEST_PROGS := test_kmod.sh \
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +010053 test_xdp_redirect.sh \
54 test_xdp_meta.sh \
Toshiaki Makita88091ff2019-06-20 11:23:23 +090055 test_xdp_veth.sh \
Andrey Ignatov622adaf2018-03-30 15:08:06 -070056 test_offload.py \
William Tu933a7412018-04-26 14:01:39 -070057 test_sock_addr.sh \
Mathieu Xhonneuxc99a84e2018-05-20 14:58:17 +010058 test_tunnel.sh \
Sean Young6bdd5332018-05-27 12:24:10 +010059 test_lwt_seg6local.sh \
Andrey Ignatov5ecd8c22018-08-12 10:49:30 -070060 test_lirc_mode2.sh \
Petar Penkov50b3ed52018-09-14 07:46:22 -070061 test_skb_cgroup_id.sh \
Jesper Dangaard Brouer97396ff2018-10-09 12:04:53 +020062 test_flow_dissector.sh \
Jesper Dangaard Brouerd35661f2019-08-01 20:00:21 +020063 test_xdp_vlan_mode_generic.sh \
64 test_xdp_vlan_mode_native.sh \
Lorenz Bauerbafc0ba2019-03-22 09:54:06 +080065 test_lwt_ip_encap.sh \
Willem de Bruijn98cdabc2019-03-22 14:32:49 -040066 test_tcp_check_syncookie.sh \
Peter Oskolkov7df5e3d2019-03-22 16:40:19 -070067 test_tc_tunnel.sh \
Alan Maguirecd538502019-05-31 18:47:14 +010068 test_tc_edt.sh \
Quentin Monnet45c5589d2019-08-30 12:00:38 +010069 test_xdping.sh \
Michal Rostecki736332742020-02-26 17:59:39 +010070 test_bpftool_build.sh \
YiFei Zhud42d1cc2020-09-15 16:45:43 -070071 test_bpftool.sh \
72 test_bpftool_metadata.sh \
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +020073
Anders Roxellf98937c2019-01-08 10:23:03 +010074TEST_PROGS_EXTENDED := with_addr.sh \
Stanislav Fomichev1be72f22019-01-14 09:41:41 -080075 with_tunnels.sh \
Anders Roxellf98937c2019-01-08 10:23:03 +010076 tcp_client.py \
Anders Roxell3035bb72019-08-20 15:41:21 +020077 tcp_server.py \
78 test_xdp_vlan.sh
Anders Roxellef4ab842018-10-10 16:27:04 +020079
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +010080# Compile but not part of 'make run_tests'
Andrii Nakryikocb79a4e2019-10-15 23:00:51 -070081TEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \
Hangbin Liu25a79912019-06-04 10:35:05 +080082 flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \
Andrii Nakryiko8e7c2a02020-05-12 12:24:43 -070083 test_lirc_mode2_user xdping test_cpp runqslower bench
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +010084
Andrii Nakryiko74b5a592019-10-15 23:00:49 -070085TEST_CUSTOM_PROGS = urandom_read
86
Andrii Nakryiko92f74402019-12-16 22:14:25 -080087# 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.
92ifeq ($(V),1)
Andrii Nakryiko965b9fe2020-01-09 21:17:16 -080093Q =
Andrii Nakryiko92f74402019-12-16 22:14:25 -080094msg =
95else
Andrii Nakryiko965b9fe2020-01-09 21:17:16 -080096Q = @
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -080097msg = @printf ' %-8s%s %s%s\n' "$(1)" "$(if $(2), [$(2)])" "$(notdir $(3))" "$(if $(4), $(4))";
Andrii Nakryiko965b9fe2020-01-09 21:17:16 -080098MAKEFLAGS += --no-print-directory
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -080099submake_extras := feature_display=0
Andrii Nakryiko92f74402019-12-16 22:14:25 -0800100endif
101
102# override lib.mk's default rules
103OVERRIDE_TARGETS := 1
104override define CLEAN
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800105 $(call msg,CLEAN)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700106 $(Q)$(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
Andrii Nakryiko92f74402019-12-16 22:14:25 -0800107endef
108
Zi Shen Lime8f1f342017-03-19 23:03:14 -0700109include ../lib.mk
Alexei Starovoitov1da8ac72017-03-10 22:05:55 -0800110
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100111SCRATCH_DIR := $(OUTPUT)/tools
112BUILD_DIR := $(SCRATCH_DIR)/build
113INCLUDE_DIR := $(SCRATCH_DIR)/include
114BPFOBJ := $(BUILD_DIR)/libbpf/libbpf.a
Jiri Olsacc15a202020-07-11 23:53:29 +0200115RESOLVE_BTFIDS := $(BUILD_DIR)/resolve_btfids/resolve_btfids
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100116
Andrii Nakryiko03dcb782019-10-15 23:00:48 -0700117# 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_PROGS) \
123 $(TEST_PROGS_EXTENDED) \
124 $(TEST_GEN_PROGS_EXTENDED) \
125 $(TEST_CUSTOM_PROGS)): %: $(OUTPUT)/% ;
126
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700127$(OUTPUT)/%.o: %.c
128 $(call msg,CC,,$@)
129 $(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
130
Andrii Nakryiko92f74402019-12-16 22:14:25 -0800131$(OUTPUT)/%:%.c
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800132 $(call msg,BINARY,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700133 $(Q)$(LINK.c) $^ $(LDLIBS) -o $@
Andrii Nakryiko92f74402019-12-16 22:14:25 -0800134
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700135$(OUTPUT)/urandom_read: urandom_read.c
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800136 $(call msg,BINARY,,$@)
Bill Wendlinga9684332020-09-22 16:21:40 -0700137 $(Q)$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS) -Wl,--build-id=sha1
Jiong Wang3ef84342019-01-26 12:26:14 -0500138
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100139$(OUTPUT)/test_stub.o: test_stub.c $(BPFOBJ)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800140 $(call msg,CC,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700141 $(Q)$(CC) -c $(CFLAGS) -o $@ $<
Ilya Leoshkeviche93d9912019-10-28 11:21:10 +0100142
Andrii Nakryikoca4db632020-06-29 17:47:59 -0700143VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \
Andrii Nakryikocc6fa772020-03-04 10:43:36 -0800144 $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \
145 ../../../../vmlinux \
146 /sys/kernel/btf/vmlinux \
147 /boot/vmlinux-$(shell uname -r)
Andrii Nakryikoca4db632020-06-29 17:47:59 -0700148VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
Andrii Nakryikocc6fa772020-03-04 10:43:36 -0800149
Andrii Nakryiko6bcaf412020-08-04 17:47:57 -0700150DEFAULT_BPFTOOL := $(SCRATCH_DIR)/sbin/bpftool
151
152$(OUTPUT)/runqslower: $(BPFOBJ) | $(DEFAULT_BPFTOOL)
Toke Høiland-Jørgensenbfe42792020-01-20 14:06:43 +0100153 $(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/runqslower \
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100154 OUTPUT=$(SCRATCH_DIR)/ VMLINUX_BTF=$(VMLINUX_BTF) \
Veronika Kabatovab26d1e22020-04-28 19:37:42 +0200155 BPFOBJ=$(BPFOBJ) BPF_INCLUDE=$(INCLUDE_DIR) && \
156 cp $(SCRATCH_DIR)/runqslower $@
Zi Shen Lime8f1f342017-03-19 23:03:14 -0700157
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700158$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/test_stub.o $(BPFOBJ)
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +0100159
Jesper Dangaard Brouer41757dc2018-02-13 14:19:15 +0100160$(OUTPUT)/test_dev_cgroup: cgroup_helpers.c
Andrey Ignatov5ecd8c22018-08-12 10:49:30 -0700161$(OUTPUT)/test_skb_cgroup_id_user: cgroup_helpers.c
Andrey Ignatov1d436882018-03-30 15:08:08 -0700162$(OUTPUT)/test_sock: cgroup_helpers.c
Andrey Ignatove50b0a62018-03-30 15:08:03 -0700163$(OUTPUT)/test_sock_addr: cgroup_helpers.c
Andrey Ignatov194db0d2018-07-30 17:42:31 -0700164$(OUTPUT)/test_socket_cookie: cgroup_helpers.c
John Fastabend16962b22018-04-23 14:30:38 -0700165$(OUTPUT)/test_sockmap: cgroup_helpers.c
Sowmini Varadhan435f90a2018-11-07 16:12:02 -0800166$(OUTPUT)/test_tcpnotify_user: cgroup_helpers.c trace_helpers.c
Yonghong Songf2690992018-06-03 15:59:43 -0700167$(OUTPUT)/get_cgroup_id_user: cgroup_helpers.c
Roman Gushchin68cfa3a2018-08-02 14:27:29 -0700168$(OUTPUT)/test_cgroup_storage: cgroup_helpers.c
Roman Gushchin371e4fc2018-09-28 14:46:00 +0000169$(OUTPUT)/test_netcnt: cgroup_helpers.c
Martin KaFai Laue0b27b32019-02-09 23:22:28 -0800170$(OUTPUT)/test_sock_fields: cgroup_helpers.c
Andrey Ignatov1f5fa9a2019-03-08 15:08:21 -0800171$(OUTPUT)/test_sysctl: cgroup_helpers.c
Jesper Dangaard Brouer41757dc2018-02-13 14:19:15 +0100172
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800173BPFTOOL ?= $(DEFAULT_BPFTOOL)
Andrii Nakryiko03506292020-01-23 21:41:48 -0800174$(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) \
175 $(BPFOBJ) | $(BUILD_DIR)/bpftool
176 $(Q)$(MAKE) $(submake_extras) -C $(BPFTOOLDIR) \
177 OUTPUT=$(BUILD_DIR)/bpftool/ \
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100178 prefix= DESTDIR=$(SCRATCH_DIR)/ install
Quentin Monnet41d5c372020-09-09 17:22:51 +0100179 $(Q)mkdir -p $(BUILD_DIR)/bpftool/Documentation
180 $(Q)RST2MAN_OPTS="--exit-status=1" $(MAKE) $(submake_extras) \
181 -C $(BPFTOOLDIR)/Documentation \
182 OUTPUT=$(BUILD_DIR)/bpftool/Documentation/ \
183 prefix= DESTDIR=$(SCRATCH_DIR)/ install
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800184
Andrii Nakryiko03506292020-01-23 21:41:48 -0800185$(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile) \
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100186 ../../../include/uapi/linux/bpf.h \
187 | $(INCLUDE_DIR) $(BUILD_DIR)/libbpf
188 $(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(BUILD_DIR)/libbpf/ \
Andrii Nakryiko03506292020-01-23 21:41:48 -0800189 DESTDIR=$(SCRATCH_DIR) prefix= all install_headers
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800190
Jiri Olsacc15a202020-07-11 23:53:29 +0200191$(BUILD_DIR)/libbpf $(BUILD_DIR)/bpftool $(BUILD_DIR)/resolve_btfids $(INCLUDE_DIR):
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100192 $(call msg,MKDIR,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700193 $(Q)mkdir -p $@
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700194
Andrii Nakryikoacbd0622020-03-13 10:23:36 -0700195$(INCLUDE_DIR)/vmlinux.h: $(VMLINUX_BTF) | $(BPFTOOL) $(INCLUDE_DIR)
Andrii Nakryikoca4db632020-06-29 17:47:59 -0700196ifeq ($(VMLINUX_H),)
Andrii Nakryikoacbd0622020-03-13 10:23:36 -0700197 $(call msg,GEN,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700198 $(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
Andrii Nakryikoca4db632020-06-29 17:47:59 -0700199else
200 $(call msg,CP,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700201 $(Q)cp "$(VMLINUX_H)" $@
Andrii Nakryikoca4db632020-06-29 17:47:59 -0700202endif
Andrii Nakryikoacbd0622020-03-13 10:23:36 -0700203
Jiri Olsacc15a202020-07-11 23:53:29 +0200204$(RESOLVE_BTFIDS): $(BPFOBJ) | $(BUILD_DIR)/resolve_btfids \
205 $(TOOLSDIR)/bpf/resolve_btfids/main.c \
206 $(TOOLSDIR)/lib/rbtree.c \
207 $(TOOLSDIR)/lib/zalloc.c \
208 $(TOOLSDIR)/lib/string.c \
209 $(TOOLSDIR)/lib/ctype.c \
210 $(TOOLSDIR)/lib/str_error_r.c
211 $(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/resolve_btfids \
212 OUTPUT=$(BUILD_DIR)/resolve_btfids/ BPFOBJ=$(BPFOBJ)
213
Sirio Balmelli167381f2018-05-21 09:00:03 +0200214# Get Clang's default includes on this system, as opposed to those seen by
215# '-target bpf'. This fixes "missing" files on some architectures/distros,
216# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
217#
218# Use '-idirafter': Don't interfere with include mechanics except where the
219# build would have failed anyways.
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200220define get_sys_includes
221$(shell $(1) -v -E - </dev/null 2>&1 \
Björn Töpel6016df82020-11-18 08:16:38 +0100222 | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
223$(shell $(1) -dM -E - </dev/null | grep '#define __riscv_xlen ' | sed 's/#define /-D/' | sed 's/ /=/')
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200224endef
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700225
Ilya Leoshkevich313e7f62019-10-28 11:20:49 +0100226# Determine target endianness.
227IS_LITTLE_ENDIAN = $(shell $(CC) -dM -E - </dev/null | \
228 grep 'define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__')
229MENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian)
230
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200231CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
Ilya Leoshkevich313e7f62019-10-28 11:20:49 +0100232BPF_CFLAGS = -g -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN) \
Tobias Klauserbcd66b12020-03-13 12:31:05 +0100233 -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR) \
234 -I$(abspath $(OUTPUT)/../usr/include)
Sirio Balmelli167381f2018-05-21 09:00:03 +0200235
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200236CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \
237 -Wno-compare-distinct-pointer-types
Alexei Starovoitov3bc35c62017-12-14 17:55:11 -0800238
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200239$(OUTPUT)/test_l4lb_noinline.o: BPF_CFLAGS += -fno-inline
240$(OUTPUT)/test_xdp_noinline.o: BPF_CFLAGS += -fno-inline
Alexei Starovoitov3bc35c62017-12-14 17:55:11 -0800241
Stanislav Fomichevbf0f0fd2019-01-28 08:53:55 -0800242$(OUTPUT)/flow_dissector_load.o: flow_dissector_load.h
Stanislav Fomichevbf0f0fd2019-01-28 08:53:55 -0800243
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700244# Build BPF object using Clang
245# $1 - input .c file
246# $2 - output .o file
247# $3 - CFLAGS
248# $4 - LDFLAGS
249define CLANG_BPF_BUILD_RULE
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800250 $(call msg,CLNG-LLC,$(TRUNNER_BINARY),$2)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700251 $(Q)($(CLANG) $3 -O2 -target bpf -emit-llvm \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700252 -c $1 -o - || echo "BPF obj compilation failed") | \
Yonghong Song83250f22020-02-18 16:42:36 -0800253 $(LLC) -mattr=dwarfris -march=bpf -mcpu=v3 $4 -filetype=obj -o $2
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700254endef
Andrii Nakryiko24f65052019-11-19 16:25:10 -0800255# Similar to CLANG_BPF_BUILD_RULE, but with disabled alu32
256define CLANG_NOALU32_BPF_BUILD_RULE
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800257 $(call msg,CLNG-LLC,$(TRUNNER_BINARY),$2)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700258 $(Q)($(CLANG) $3 -O2 -target bpf -emit-llvm \
Andrii Nakryiko24f65052019-11-19 16:25:10 -0800259 -c $1 -o - || echo "BPF obj compilation failed") | \
260 $(LLC) -march=bpf -mcpu=v2 $4 -filetype=obj -o $2
261endef
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700262# Similar to CLANG_BPF_BUILD_RULE, but using native Clang and bpf LLC
263define CLANG_NATIVE_BPF_BUILD_RULE
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800264 $(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700265 $(Q)($(CLANG) $3 -O2 -emit-llvm \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700266 -c $1 -o - || echo "BPF obj compilation failed") | \
Yonghong Song83250f22020-02-18 16:42:36 -0800267 $(LLC) -march=bpf -mcpu=v3 $4 -filetype=obj -o $2
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700268endef
269# Build BPF object using GCC
270define GCC_BPF_BUILD_RULE
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800271 $(call msg,GCC-BPF,$(TRUNNER_BINARY),$2)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700272 $(Q)$(BPF_GCC) $3 $4 -O2 -c $1 -o $2
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700273endef
Stanislav Fomicheve78e00b2019-03-06 11:59:26 -0800274
Jakub Sitnicki0b9ad562020-04-29 20:11:54 +0200275SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800276
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700277# Set up extra TRUNNER_XXX "temporary" variables in the environment (relies on
278# $eval()) and pass control to DEFINE_TEST_RUNNER_RULES.
279# Parameters:
280# $1 - test runner base binary name (e.g., test_progs)
Yonghong Songe13a2fe2019-10-21 21:31:19 -0700281# $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700282define DEFINE_TEST_RUNNER
Jiong Wang3ef84342019-01-26 12:26:14 -0500283
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700284TRUNNER_OUTPUT := $(OUTPUT)$(if $2,/)$2
285TRUNNER_BINARY := $1$(if $2,-)$2
286TRUNNER_TEST_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.test.o, \
287 $$(notdir $$(wildcard $(TRUNNER_TESTS_DIR)/*.c)))
288TRUNNER_EXTRA_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, \
289 $$(filter %.c,$(TRUNNER_EXTRA_SOURCES)))
290TRUNNER_EXTRA_HDRS := $$(filter %.h,$(TRUNNER_EXTRA_SOURCES))
291TRUNNER_TESTS_HDR := $(TRUNNER_TESTS_DIR)/tests.h
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800292TRUNNER_BPF_SRCS := $$(notdir $$(wildcard $(TRUNNER_BPF_PROGS_DIR)/*.c))
293TRUNNER_BPF_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, $$(TRUNNER_BPF_SRCS))
294TRUNNER_BPF_SKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.skel.h, \
295 $$(filter-out $(SKEL_BLACKLIST), \
296 $$(TRUNNER_BPF_SRCS)))
Yauheni Kaliuta309b81f2020-05-13 05:17:22 +0300297TEST_GEN_FILES += $$(TRUNNER_BPF_OBJS)
Jiong Wang3ef84342019-01-26 12:26:14 -0500298
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700299# Evaluate rules now with extra TRUNNER_XXX variables above already defined
300$$(eval $$(call DEFINE_TEST_RUNNER_RULES,$1,$2))
Stanislav Fomichev3f306582019-03-01 19:42:13 -0800301
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700302endef
Jiong Wang3ef84342019-01-26 12:26:14 -0500303
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700304# Using TRUNNER_XXX variables, provided by callers of DEFINE_TEST_RUNNER and
305# set up by DEFINE_TEST_RUNNER itself, create test runner build rules with:
306# $1 - test runner base binary name (e.g., test_progs)
Yonghong Songe13a2fe2019-10-21 21:31:19 -0700307# $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700308define DEFINE_TEST_RUNNER_RULES
309
310ifeq ($($(TRUNNER_OUTPUT)-dir),)
311$(TRUNNER_OUTPUT)-dir := y
312$(TRUNNER_OUTPUT):
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800313 $$(call msg,MKDIR,,$$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700314 $(Q)mkdir -p $$@
Jiong Wang3ef84342019-01-26 12:26:14 -0500315endif
316
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700317# ensure we set up BPF objects generation rule just once for a given
318# input/output directory combination
319ifeq ($($(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs),)
320$(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs := y
321$(TRUNNER_BPF_OBJS): $(TRUNNER_OUTPUT)/%.o: \
322 $(TRUNNER_BPF_PROGS_DIR)/%.c \
323 $(TRUNNER_BPF_PROGS_DIR)/*.h \
Andrii Nakryikoacbd0622020-03-13 10:23:36 -0700324 $$(INCLUDE_DIR)/vmlinux.h \
Andrii Nakryiko3ac2e202020-08-20 16:12:35 -0700325 $(wildcard $(BPFDIR)/bpf_*.h) | $(TRUNNER_OUTPUT)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700326 $$(call $(TRUNNER_BPF_BUILD_RULE),$$<,$$@, \
327 $(TRUNNER_BPF_CFLAGS), \
328 $(TRUNNER_BPF_LDFLAGS))
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800329
330$(TRUNNER_BPF_SKELS): $(TRUNNER_OUTPUT)/%.skel.h: \
331 $(TRUNNER_OUTPUT)/%.o \
332 | $(BPFTOOL) $(TRUNNER_OUTPUT)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800333 $$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700334 $(Q)$$(BPFTOOL) gen skeleton $$< > $$@
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700335endif
336
337# ensure we set up tests.h header generation rule just once
338ifeq ($($(TRUNNER_TESTS_DIR)-tests-hdr),)
339$(TRUNNER_TESTS_DIR)-tests-hdr := y
340$(TRUNNER_TESTS_HDR): $(TRUNNER_TESTS_DIR)/*.c
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800341 $$(call msg,TEST-HDR,$(TRUNNER_BINARY),$$@)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700342 $$(shell ( cd $(TRUNNER_TESTS_DIR); \
343 echo '/* Generated header, do not edit */'; \
344 ls *.c 2> /dev/null | \
345 sed -e 's@\([^\.]*\)\.c@DEFINE_TEST(\1)@'; \
346 ) > $$@)
347endif
348
349# compile individual test files
350# Note: we cd into output directory to ensure embedded BPF object is found
351$(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o: \
352 $(TRUNNER_TESTS_DIR)/%.c \
353 $(TRUNNER_EXTRA_HDRS) \
354 $(TRUNNER_BPF_OBJS) \
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800355 $(TRUNNER_BPF_SKELS) \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700356 $$(BPFOBJ) | $(TRUNNER_OUTPUT)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800357 $$(call msg,TEST-OBJ,$(TRUNNER_BINARY),$$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700358 $(Q)cd $$(@D) && $$(CC) -I. $$(CFLAGS) -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700359
360$(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o: \
361 %.c \
362 $(TRUNNER_EXTRA_HDRS) \
363 $(TRUNNER_TESTS_HDR) \
364 $$(BPFOBJ) | $(TRUNNER_OUTPUT)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800365 $$(call msg,EXT-OBJ,$(TRUNNER_BINARY),$$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700366 $(Q)$$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700367
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800368# only copy extra resources if in flavored build
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700369$(TRUNNER_BINARY)-extras: $(TRUNNER_EXTRA_FILES) | $(TRUNNER_OUTPUT)
370ifneq ($2,)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800371 $$(call msg,EXT-COPY,$(TRUNNER_BINARY),$(TRUNNER_EXTRA_FILES))
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700372 $(Q)cp -a $$^ $(TRUNNER_OUTPUT)/
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700373endif
374
375$(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS) \
376 $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ) \
Jiri Olsacc15a202020-07-11 23:53:29 +0200377 $(RESOLVE_BTFIDS) \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700378 | $(TRUNNER_BINARY)-extras
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800379 $$(call msg,BINARY,,$$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700380 $(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
381 $(Q)$(RESOLVE_BTFIDS) --no-fail --btf btf_data.o $$@
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700382
383endef
384
385# Define test_progs test runner.
386TRUNNER_TESTS_DIR := prog_tests
387TRUNNER_BPF_PROGS_DIR := progs
388TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c \
Andrii Nakryikocd492912020-05-12 12:24:42 -0700389 network_helpers.c testing_helpers.c \
Andrii Nakryiko1306c982020-11-04 20:33:56 -0800390 btf_helpers.c flow_dissector_load.h
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700391TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \
KP Singh854055c2020-11-26 18:49:46 +0000392 ima_setup.sh \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700393 $(wildcard progs/btf_dump_test_case_*.c)
394TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE
Andrii Nakryiko6910d7d2020-01-09 21:17:15 -0800395TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS)
Yonghong Songe13a2fe2019-10-21 21:31:19 -0700396TRUNNER_BPF_LDFLAGS := -mattr=+alu32
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700397$(eval $(call DEFINE_TEST_RUNNER,test_progs))
398
Yonghong Songe13a2fe2019-10-21 21:31:19 -0700399# Define test_progs-no_alu32 test runner.
Andrii Nakryiko24f65052019-11-19 16:25:10 -0800400TRUNNER_BPF_BUILD_RULE := CLANG_NOALU32_BPF_BUILD_RULE
Yonghong Songe13a2fe2019-10-21 21:31:19 -0700401TRUNNER_BPF_LDFLAGS :=
402$(eval $(call DEFINE_TEST_RUNNER,test_progs,no_alu32))
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700403
404# Define test_progs BPF-GCC-flavored test runner.
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200405ifneq ($(BPF_GCC),)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700406TRUNNER_BPF_BUILD_RULE := GCC_BPF_BUILD_RULE
Ilya Leoshkevich313e7f62019-10-28 11:20:49 +0100407TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(call get_sys_includes,gcc)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700408TRUNNER_BPF_LDFLAGS :=
409$(eval $(call DEFINE_TEST_RUNNER,test_progs,bpf_gcc))
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200410endif
411
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700412# Define test_maps test runner.
413TRUNNER_TESTS_DIR := map_tests
414TRUNNER_BPF_PROGS_DIR := progs
415TRUNNER_EXTRA_SOURCES := test_maps.c
416TRUNNER_EXTRA_FILES :=
417TRUNNER_BPF_BUILD_RULE := $$(error no BPF objects should be built)
418TRUNNER_BPF_CFLAGS :=
419TRUNNER_BPF_LDFLAGS :=
420$(eval $(call DEFINE_TEST_RUNNER,test_maps))
Martin KaFai Lauc0fa1b62018-04-18 15:56:06 -0700421
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700422# Define test_verifier test runner.
423# It is much simpler than test_maps/test_progs and sufficiently different from
424# them (e.g., test.h is using completely pattern), that it's worth just
425# explicitly defining all the rules explicitly.
426verifier/tests.h: verifier/*.c
Stanislav Fomicheve78e00b2019-03-06 11:59:26 -0800427 $(shell ( cd verifier/; \
Jakub Kicinski2dfb4012019-01-25 15:24:42 -0800428 echo '/* Generated header, do not edit */'; \
429 echo '#ifdef FILL_ARRAY'; \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700430 ls *.c 2> /dev/null | sed -e 's@\(.*\)@#include \"\1\"@'; \
Jakub Kicinski2dfb4012019-01-25 15:24:42 -0800431 echo '#endif' \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700432 ) > verifier/tests.h)
433$(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800434 $(call msg,BINARY,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700435 $(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
Jakub Kicinski2dfb4012019-01-25 15:24:42 -0800436
Stanislav Fomichev6bf6aff2019-12-02 13:59:31 -0800437# Make sure we are able to include and link libbpf against c++.
Andrii Nakryiko7c8dce42019-12-26 13:02:53 -0800438$(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800439 $(call msg,CXX,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700440 $(Q)$(CXX) $(CFLAGS) $^ $(LDLIBS) -o $@
Stanislav Fomichev6bf6aff2019-12-02 13:59:31 -0800441
Andrii Nakryiko8e7c2a02020-05-12 12:24:43 -0700442# Benchmark runner
443$(OUTPUT)/bench_%.o: benchs/bench_%.c bench.h
444 $(call msg,CC,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700445 $(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
Andrii Nakryiko4eaf0b52020-05-12 12:24:44 -0700446$(OUTPUT)/bench_rename.o: $(OUTPUT)/test_overhead.skel.h
Andrii Nakryikoc5d420c2020-05-12 12:24:45 -0700447$(OUTPUT)/bench_trigger.o: $(OUTPUT)/trigger_bench.skel.h
Andrii Nakryikoc97099b2020-05-29 00:54:23 -0700448$(OUTPUT)/bench_ringbufs.o: $(OUTPUT)/ringbuf_bench.skel.h \
449 $(OUTPUT)/perfbuf_bench.skel.h
Andrii Nakryiko8e7c2a02020-05-12 12:24:43 -0700450$(OUTPUT)/bench.o: bench.h testing_helpers.h
451$(OUTPUT)/bench: LDLIBS += -lm
452$(OUTPUT)/bench: $(OUTPUT)/bench.o $(OUTPUT)/testing_helpers.o \
Andrii Nakryiko4eaf0b52020-05-12 12:24:44 -0700453 $(OUTPUT)/bench_count.o \
Andrii Nakryikoc5d420c2020-05-12 12:24:45 -0700454 $(OUTPUT)/bench_rename.o \
Andrii Nakryikoc97099b2020-05-29 00:54:23 -0700455 $(OUTPUT)/bench_trigger.o \
456 $(OUTPUT)/bench_ringbufs.o
Andrii Nakryiko8e7c2a02020-05-12 12:24:43 -0700457 $(call msg,BINARY,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700458 $(Q)$(CC) $(LDFLAGS) -o $@ $(filter %.a %.o,$^) $(LDLIBS)
Andrii Nakryiko8e7c2a02020-05-12 12:24:43 -0700459
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100460EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(SCRATCH_DIR) \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700461 prog_tests/tests.h map_tests/tests.h verifier/tests.h \
Andrii Nakryiko2031af22020-01-09 21:17:14 -0800462 feature \
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100463 $(addprefix $(OUTPUT)/,*.o *.skel.h no_alu32 bpf_gcc)