blob: 1a079e91482f49fd1411deb0e39b98034b4e9187 [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
Andrii Nakryiko74b5a592019-10-15 23:00:49 -07005CURDIR := $(abspath .)
Andrii Nakryikof3c926a2019-12-13 17:43:38 -08006TOOLSDIR := $(abspath ../../..)
7LIBDIR := $(TOOLSDIR)/lib
Zi Shen Lime8f1f342017-03-19 23:03:14 -07008BPFDIR := $(LIBDIR)/bpf
Andrii Nakryikof3c926a2019-12-13 17:43:38 -08009TOOLSINCDIR := $(TOOLSDIR)/include
10BPFTOOLDIR := $(TOOLSDIR)/bpf/bpftool
11APIDIR := $(TOOLSINCDIR)/uapi
Andrii Nakryiko74b5a592019-10-15 23:00:49 -070012GENDIR := $(abspath ../../../../include/generated)
Daniel Borkmann02ea80b2017-03-31 02:24:04 +020013GENHDR := $(GENDIR)/autoconf.h
Mickaël Salaünd498f872017-02-11 23:20:23 +010014
Daniel Borkmann02ea80b2017-03-31 02:24:04 +020015ifneq ($(wildcard $(GENHDR)),)
16 GENFLAGS := -DHAVE_GENHDR
17endif
18
Jiong Wang3ef84342019-01-26 12:26:14 -050019CLANG ?= clang
20LLC ?= llc
21LLVM_OBJCOPY ?= llvm-objcopy
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +020022BPF_GCC ?= $(shell command -v bpf-gcc;)
Andrii Nakryiko02995dd2020-04-28 18:21:02 -070023SAN_CFLAGS ?=
24CFLAGS += -g -rdynamic -Wall -O2 $(GENFLAGS) $(SAN_CFLAGS) \
25 -I$(CURDIR) -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR) \
26 -I$(TOOLSINCDIR) -I$(APIDIR) \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -070027 -Dbpf_prog_load=bpf_prog_test_load \
Jiong Wang9d120b42019-05-24 23:25:21 +010028 -Dbpf_load_program=bpf_test_load_program
Andrii Nakryiko166750b2019-12-13 17:47:08 -080029LDLIBS += -lcap -lelf -lz -lrt -lpthread
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +020030
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +010031# Order correspond to 'make run_tests' order
David S. Miller18b3ad92017-05-10 11:43:51 -070032TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \
Andrey Ignatov1d436882018-03-30 15:08:08 -070033 test_align test_verifier_log test_dev_cgroup test_tcpbpf_user \
Hangbin Liu25a79912019-06-04 10:35:05 +080034 test_sock test_btf test_sockmap get_cgroup_id_user test_socket_cookie \
Jakub Sitnicki415bb4e2019-12-12 11:22:58 +010035 test_cgroup_storage \
Andrii Nakryiko42fce2c2020-04-28 18:21:03 -070036 test_netcnt test_tcpnotify_user test_sock_fields test_sysctl \
Carlos Neira1c1052e2020-03-04 17:41:57 -030037 test_progs-no_alu32 \
38 test_current_pid_tgid_new_ns
Jiong Wang3ef84342019-01-26 12:26:14 -050039
Andrii Nakryiko74b5a592019-10-15 23:00:49 -070040# Also test bpf-gcc, if present
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +020041ifneq ($(BPF_GCC),)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -070042TEST_GEN_PROGS += test_progs-bpf_gcc
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +020043endif
44
Andrii Nakryiko74b5a592019-10-15 23:00:49 -070045TEST_GEN_FILES =
Anders Roxelle47a1792019-11-11 17:17:28 +010046TEST_FILES = test_lwt_ip_encap.o \
47 test_tc_edt.o
Andrii Nakryiko74b5a592019-10-15 23:00:49 -070048
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +010049# Order correspond to 'make run_tests' order
50TEST_PROGS := test_kmod.sh \
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +010051 test_xdp_redirect.sh \
52 test_xdp_meta.sh \
Toshiaki Makita88091ff2019-06-20 11:23:23 +090053 test_xdp_veth.sh \
Andrey Ignatov622adaf2018-03-30 15:08:06 -070054 test_offload.py \
William Tu933a7412018-04-26 14:01:39 -070055 test_sock_addr.sh \
Mathieu Xhonneuxc99a84e2018-05-20 14:58:17 +010056 test_tunnel.sh \
Sean Young6bdd5332018-05-27 12:24:10 +010057 test_lwt_seg6local.sh \
Andrey Ignatov5ecd8c22018-08-12 10:49:30 -070058 test_lirc_mode2.sh \
Petar Penkov50b3ed52018-09-14 07:46:22 -070059 test_skb_cgroup_id.sh \
Jesper Dangaard Brouer97396ff2018-10-09 12:04:53 +020060 test_flow_dissector.sh \
Jesper Dangaard Brouerd35661f2019-08-01 20:00:21 +020061 test_xdp_vlan_mode_generic.sh \
62 test_xdp_vlan_mode_native.sh \
Lorenz Bauerbafc0ba2019-03-22 09:54:06 +080063 test_lwt_ip_encap.sh \
Willem de Bruijn98cdabc2019-03-22 14:32:49 -040064 test_tcp_check_syncookie.sh \
Peter Oskolkov7df5e3d2019-03-22 16:40:19 -070065 test_tc_tunnel.sh \
Alan Maguirecd538502019-05-31 18:47:14 +010066 test_tc_edt.sh \
Quentin Monnet45c5589d2019-08-30 12:00:38 +010067 test_xdping.sh \
Michal Rostecki736332742020-02-26 17:59:39 +010068 test_bpftool_build.sh \
69 test_bpftool.sh
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +020070
Anders Roxellf98937c2019-01-08 10:23:03 +010071TEST_PROGS_EXTENDED := with_addr.sh \
Stanislav Fomichev1be72f22019-01-14 09:41:41 -080072 with_tunnels.sh \
Anders Roxellf98937c2019-01-08 10:23:03 +010073 tcp_client.py \
Anders Roxell3035bb72019-08-20 15:41:21 +020074 tcp_server.py \
75 test_xdp_vlan.sh
Anders Roxellef4ab842018-10-10 16:27:04 +020076
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +010077# Compile but not part of 'make run_tests'
Andrii Nakryikocb79a4e2019-10-15 23:00:51 -070078TEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \
Hangbin Liu25a79912019-06-04 10:35:05 +080079 flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \
Andrii Nakryiko8e7c2a02020-05-12 12:24:43 -070080 test_lirc_mode2_user xdping test_cpp runqslower bench
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +010081
Andrii Nakryiko74b5a592019-10-15 23:00:49 -070082TEST_CUSTOM_PROGS = urandom_read
83
Andrii Nakryiko92f74402019-12-16 22:14:25 -080084# Emit succinct information message describing current building step
85# $1 - generic step name (e.g., CC, LINK, etc);
86# $2 - optional "flavor" specifier; if provided, will be emitted as [flavor];
87# $3 - target (assumed to be file); only file name will be emitted;
88# $4 - optional extra arg, emitted as-is, if provided.
89ifeq ($(V),1)
Andrii Nakryiko965b9fe2020-01-09 21:17:16 -080090Q =
Andrii Nakryiko92f74402019-12-16 22:14:25 -080091msg =
92else
Andrii Nakryiko965b9fe2020-01-09 21:17:16 -080093Q = @
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -080094msg = @printf ' %-8s%s %s%s\n' "$(1)" "$(if $(2), [$(2)])" "$(notdir $(3))" "$(if $(4), $(4))";
Andrii Nakryiko965b9fe2020-01-09 21:17:16 -080095MAKEFLAGS += --no-print-directory
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -080096submake_extras := feature_display=0
Andrii Nakryiko92f74402019-12-16 22:14:25 -080097endif
98
99# override lib.mk's default rules
100OVERRIDE_TARGETS := 1
101override define CLEAN
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800102 $(call msg,CLEAN)
Andrii Nakryiko92f74402019-12-16 22:14:25 -0800103 $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
104endef
105
Zi Shen Lime8f1f342017-03-19 23:03:14 -0700106include ../lib.mk
Alexei Starovoitov1da8ac72017-03-10 22:05:55 -0800107
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100108SCRATCH_DIR := $(OUTPUT)/tools
109BUILD_DIR := $(SCRATCH_DIR)/build
110INCLUDE_DIR := $(SCRATCH_DIR)/include
111BPFOBJ := $(BUILD_DIR)/libbpf/libbpf.a
112
Andrii Nakryiko03dcb782019-10-15 23:00:48 -0700113# Define simple and short `make test_progs`, `make test_sysctl`, etc targets
114# to build individual tests.
115# NOTE: Semicolon at the end is critical to override lib.mk's default static
116# rule for binaries.
117$(notdir $(TEST_GEN_PROGS) \
118 $(TEST_PROGS) \
119 $(TEST_PROGS_EXTENDED) \
120 $(TEST_GEN_PROGS_EXTENDED) \
121 $(TEST_CUSTOM_PROGS)): %: $(OUTPUT)/% ;
122
Andrii Nakryiko92f74402019-12-16 22:14:25 -0800123$(OUTPUT)/%:%.c
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800124 $(call msg,BINARY,,$@)
Andrii Nakryiko92f74402019-12-16 22:14:25 -0800125 $(LINK.c) $^ $(LDLIBS) -o $@
126
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700127$(OUTPUT)/urandom_read: urandom_read.c
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800128 $(call msg,BINARY,,$@)
Daniel Díaz12226532020-01-22 17:44:24 +0100129 $(CC) $(LDFLAGS) -o $@ $< $(LDLIBS) -Wl,--build-id
Jiong Wang3ef84342019-01-26 12:26:14 -0500130
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100131$(OUTPUT)/test_stub.o: test_stub.c $(BPFOBJ)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800132 $(call msg,CC,,$@)
Ilya Leoshkeviche93d9912019-10-28 11:21:10 +0100133 $(CC) -c $(CFLAGS) -o $@ $<
134
Andrii Nakryikocc6fa772020-03-04 10:43:36 -0800135VMLINUX_BTF_PATHS := $(if $(O),$(O)/vmlinux) \
136 $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \
137 ../../../../vmlinux \
138 /sys/kernel/btf/vmlinux \
139 /boot/vmlinux-$(shell uname -r)
Andrii Nakryikoacbd0622020-03-13 10:23:36 -0700140VMLINUX_BTF := $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
Andrii Nakryikocc6fa772020-03-04 10:43:36 -0800141
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100142$(OUTPUT)/runqslower: $(BPFOBJ)
Toke Høiland-Jørgensenbfe42792020-01-20 14:06:43 +0100143 $(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/runqslower \
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100144 OUTPUT=$(SCRATCH_DIR)/ VMLINUX_BTF=$(VMLINUX_BTF) \
Veronika Kabatovab26d1e22020-04-28 19:37:42 +0200145 BPFOBJ=$(BPFOBJ) BPF_INCLUDE=$(INCLUDE_DIR) && \
146 cp $(SCRATCH_DIR)/runqslower $@
Zi Shen Lime8f1f342017-03-19 23:03:14 -0700147
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700148$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/test_stub.o $(BPFOBJ)
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +0100149
Jesper Dangaard Brouer41757dc2018-02-13 14:19:15 +0100150$(OUTPUT)/test_dev_cgroup: cgroup_helpers.c
Andrey Ignatov5ecd8c22018-08-12 10:49:30 -0700151$(OUTPUT)/test_skb_cgroup_id_user: cgroup_helpers.c
Andrey Ignatov1d436882018-03-30 15:08:08 -0700152$(OUTPUT)/test_sock: cgroup_helpers.c
Andrey Ignatove50b0a62018-03-30 15:08:03 -0700153$(OUTPUT)/test_sock_addr: cgroup_helpers.c
Andrey Ignatov194db0d2018-07-30 17:42:31 -0700154$(OUTPUT)/test_socket_cookie: cgroup_helpers.c
John Fastabend16962b22018-04-23 14:30:38 -0700155$(OUTPUT)/test_sockmap: cgroup_helpers.c
Andrey Ignatovc65267e2018-07-11 17:33:35 -0700156$(OUTPUT)/test_tcpbpf_user: cgroup_helpers.c
Sowmini Varadhan435f90a2018-11-07 16:12:02 -0800157$(OUTPUT)/test_tcpnotify_user: cgroup_helpers.c trace_helpers.c
Yonghong Songf2690992018-06-03 15:59:43 -0700158$(OUTPUT)/get_cgroup_id_user: cgroup_helpers.c
Roman Gushchin68cfa3a2018-08-02 14:27:29 -0700159$(OUTPUT)/test_cgroup_storage: cgroup_helpers.c
Roman Gushchin371e4fc2018-09-28 14:46:00 +0000160$(OUTPUT)/test_netcnt: cgroup_helpers.c
Martin KaFai Laue0b27b32019-02-09 23:22:28 -0800161$(OUTPUT)/test_sock_fields: cgroup_helpers.c
Andrey Ignatov1f5fa9a2019-03-08 15:08:21 -0800162$(OUTPUT)/test_sysctl: cgroup_helpers.c
Jesper Dangaard Brouer41757dc2018-02-13 14:19:15 +0100163
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100164DEFAULT_BPFTOOL := $(SCRATCH_DIR)/sbin/bpftool
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800165BPFTOOL ?= $(DEFAULT_BPFTOOL)
Andrii Nakryiko03506292020-01-23 21:41:48 -0800166$(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) \
167 $(BPFOBJ) | $(BUILD_DIR)/bpftool
168 $(Q)$(MAKE) $(submake_extras) -C $(BPFTOOLDIR) \
169 OUTPUT=$(BUILD_DIR)/bpftool/ \
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100170 prefix= DESTDIR=$(SCRATCH_DIR)/ install
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800171
Andrii Nakryiko03506292020-01-23 21:41:48 -0800172$(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile) \
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100173 ../../../include/uapi/linux/bpf.h \
174 | $(INCLUDE_DIR) $(BUILD_DIR)/libbpf
175 $(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(BUILD_DIR)/libbpf/ \
Andrii Nakryiko03506292020-01-23 21:41:48 -0800176 DESTDIR=$(SCRATCH_DIR) prefix= all install_headers
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800177
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100178$(BUILD_DIR)/libbpf $(BUILD_DIR)/bpftool $(INCLUDE_DIR):
179 $(call msg,MKDIR,,$@)
180 mkdir -p $@
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700181
Andrii Nakryikoacbd0622020-03-13 10:23:36 -0700182$(INCLUDE_DIR)/vmlinux.h: $(VMLINUX_BTF) | $(BPFTOOL) $(INCLUDE_DIR)
183 $(call msg,GEN,,$@)
184 $(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
185
Sirio Balmelli167381f2018-05-21 09:00:03 +0200186# Get Clang's default includes on this system, as opposed to those seen by
187# '-target bpf'. This fixes "missing" files on some architectures/distros,
188# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
189#
190# Use '-idirafter': Don't interfere with include mechanics except where the
191# build would have failed anyways.
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200192define get_sys_includes
193$(shell $(1) -v -E - </dev/null 2>&1 \
Sirio Balmelli167381f2018-05-21 09:00:03 +0200194 | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }')
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200195endef
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700196
Ilya Leoshkevich313e7f62019-10-28 11:20:49 +0100197# Determine target endianness.
198IS_LITTLE_ENDIAN = $(shell $(CC) -dM -E - </dev/null | \
199 grep 'define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__')
200MENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian)
201
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200202CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
Ilya Leoshkevich313e7f62019-10-28 11:20:49 +0100203BPF_CFLAGS = -g -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN) \
Tobias Klauserbcd66b12020-03-13 12:31:05 +0100204 -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR) \
205 -I$(abspath $(OUTPUT)/../usr/include)
Sirio Balmelli167381f2018-05-21 09:00:03 +0200206
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200207CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \
208 -Wno-compare-distinct-pointer-types
Alexei Starovoitov3bc35c62017-12-14 17:55:11 -0800209
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200210$(OUTPUT)/test_l4lb_noinline.o: BPF_CFLAGS += -fno-inline
211$(OUTPUT)/test_xdp_noinline.o: BPF_CFLAGS += -fno-inline
Alexei Starovoitov3bc35c62017-12-14 17:55:11 -0800212
Stanislav Fomichevbf0f0fd2019-01-28 08:53:55 -0800213$(OUTPUT)/flow_dissector_load.o: flow_dissector_load.h
Stanislav Fomichevbf0f0fd2019-01-28 08:53:55 -0800214
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700215# Build BPF object using Clang
216# $1 - input .c file
217# $2 - output .o file
218# $3 - CFLAGS
219# $4 - LDFLAGS
220define CLANG_BPF_BUILD_RULE
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800221 $(call msg,CLNG-LLC,$(TRUNNER_BINARY),$2)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700222 ($(CLANG) $3 -O2 -target bpf -emit-llvm \
223 -c $1 -o - || echo "BPF obj compilation failed") | \
Yonghong Song83250f22020-02-18 16:42:36 -0800224 $(LLC) -mattr=dwarfris -march=bpf -mcpu=v3 $4 -filetype=obj -o $2
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700225endef
Andrii Nakryiko24f65052019-11-19 16:25:10 -0800226# Similar to CLANG_BPF_BUILD_RULE, but with disabled alu32
227define CLANG_NOALU32_BPF_BUILD_RULE
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800228 $(call msg,CLNG-LLC,$(TRUNNER_BINARY),$2)
Andrii Nakryiko24f65052019-11-19 16:25:10 -0800229 ($(CLANG) $3 -O2 -target bpf -emit-llvm \
230 -c $1 -o - || echo "BPF obj compilation failed") | \
231 $(LLC) -march=bpf -mcpu=v2 $4 -filetype=obj -o $2
232endef
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700233# Similar to CLANG_BPF_BUILD_RULE, but using native Clang and bpf LLC
234define CLANG_NATIVE_BPF_BUILD_RULE
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800235 $(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700236 ($(CLANG) $3 -O2 -emit-llvm \
237 -c $1 -o - || echo "BPF obj compilation failed") | \
Yonghong Song83250f22020-02-18 16:42:36 -0800238 $(LLC) -march=bpf -mcpu=v3 $4 -filetype=obj -o $2
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700239endef
240# Build BPF object using GCC
241define GCC_BPF_BUILD_RULE
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800242 $(call msg,GCC-BPF,$(TRUNNER_BINARY),$2)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700243 $(BPF_GCC) $3 $4 -O2 -c $1 -o $2
244endef
Stanislav Fomicheve78e00b2019-03-06 11:59:26 -0800245
Jakub Sitnicki0b9ad562020-04-29 20:11:54 +0200246SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800247
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700248# Set up extra TRUNNER_XXX "temporary" variables in the environment (relies on
249# $eval()) and pass control to DEFINE_TEST_RUNNER_RULES.
250# Parameters:
251# $1 - test runner base binary name (e.g., test_progs)
Yonghong Songe13a2fe2019-10-21 21:31:19 -0700252# $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700253define DEFINE_TEST_RUNNER
Jiong Wang3ef84342019-01-26 12:26:14 -0500254
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700255TRUNNER_OUTPUT := $(OUTPUT)$(if $2,/)$2
256TRUNNER_BINARY := $1$(if $2,-)$2
257TRUNNER_TEST_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.test.o, \
258 $$(notdir $$(wildcard $(TRUNNER_TESTS_DIR)/*.c)))
259TRUNNER_EXTRA_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, \
260 $$(filter %.c,$(TRUNNER_EXTRA_SOURCES)))
261TRUNNER_EXTRA_HDRS := $$(filter %.h,$(TRUNNER_EXTRA_SOURCES))
262TRUNNER_TESTS_HDR := $(TRUNNER_TESTS_DIR)/tests.h
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800263TRUNNER_BPF_SRCS := $$(notdir $$(wildcard $(TRUNNER_BPF_PROGS_DIR)/*.c))
264TRUNNER_BPF_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, $$(TRUNNER_BPF_SRCS))
265TRUNNER_BPF_SKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.skel.h, \
266 $$(filter-out $(SKEL_BLACKLIST), \
267 $$(TRUNNER_BPF_SRCS)))
Yauheni Kaliuta309b81f2020-05-13 05:17:22 +0300268TEST_GEN_FILES += $$(TRUNNER_BPF_OBJS)
Jiong Wang3ef84342019-01-26 12:26:14 -0500269
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700270# Evaluate rules now with extra TRUNNER_XXX variables above already defined
271$$(eval $$(call DEFINE_TEST_RUNNER_RULES,$1,$2))
Stanislav Fomichev3f306582019-03-01 19:42:13 -0800272
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700273endef
Jiong Wang3ef84342019-01-26 12:26:14 -0500274
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700275# Using TRUNNER_XXX variables, provided by callers of DEFINE_TEST_RUNNER and
276# set up by DEFINE_TEST_RUNNER itself, create test runner build rules with:
277# $1 - test runner base binary name (e.g., test_progs)
Yonghong Songe13a2fe2019-10-21 21:31:19 -0700278# $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700279define DEFINE_TEST_RUNNER_RULES
280
281ifeq ($($(TRUNNER_OUTPUT)-dir),)
282$(TRUNNER_OUTPUT)-dir := y
283$(TRUNNER_OUTPUT):
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800284 $$(call msg,MKDIR,,$$@)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700285 mkdir -p $$@
Jiong Wang3ef84342019-01-26 12:26:14 -0500286endif
287
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700288# ensure we set up BPF objects generation rule just once for a given
289# input/output directory combination
290ifeq ($($(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs),)
291$(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs := y
292$(TRUNNER_BPF_OBJS): $(TRUNNER_OUTPUT)/%.o: \
293 $(TRUNNER_BPF_PROGS_DIR)/%.c \
294 $(TRUNNER_BPF_PROGS_DIR)/*.h \
Andrii Nakryikoacbd0622020-03-13 10:23:36 -0700295 $$(INCLUDE_DIR)/vmlinux.h \
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100296 $$(BPFOBJ) | $(TRUNNER_OUTPUT)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700297 $$(call $(TRUNNER_BPF_BUILD_RULE),$$<,$$@, \
298 $(TRUNNER_BPF_CFLAGS), \
299 $(TRUNNER_BPF_LDFLAGS))
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800300
301$(TRUNNER_BPF_SKELS): $(TRUNNER_OUTPUT)/%.skel.h: \
302 $(TRUNNER_OUTPUT)/%.o \
303 | $(BPFTOOL) $(TRUNNER_OUTPUT)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800304 $$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800305 $$(BPFTOOL) gen skeleton $$< > $$@
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700306endif
307
308# ensure we set up tests.h header generation rule just once
309ifeq ($($(TRUNNER_TESTS_DIR)-tests-hdr),)
310$(TRUNNER_TESTS_DIR)-tests-hdr := y
311$(TRUNNER_TESTS_HDR): $(TRUNNER_TESTS_DIR)/*.c
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800312 $$(call msg,TEST-HDR,$(TRUNNER_BINARY),$$@)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700313 $$(shell ( cd $(TRUNNER_TESTS_DIR); \
314 echo '/* Generated header, do not edit */'; \
315 ls *.c 2> /dev/null | \
316 sed -e 's@\([^\.]*\)\.c@DEFINE_TEST(\1)@'; \
317 ) > $$@)
318endif
319
320# compile individual test files
321# Note: we cd into output directory to ensure embedded BPF object is found
322$(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o: \
323 $(TRUNNER_TESTS_DIR)/%.c \
324 $(TRUNNER_EXTRA_HDRS) \
325 $(TRUNNER_BPF_OBJS) \
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800326 $(TRUNNER_BPF_SKELS) \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700327 $$(BPFOBJ) | $(TRUNNER_OUTPUT)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800328 $$(call msg,TEST-OBJ,$(TRUNNER_BINARY),$$@)
Andrii Nakryiko76148fa2020-04-28 18:21:01 -0700329 cd $$(@D) && $$(CC) -I. $$(CFLAGS) -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700330
331$(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o: \
332 %.c \
333 $(TRUNNER_EXTRA_HDRS) \
334 $(TRUNNER_TESTS_HDR) \
335 $$(BPFOBJ) | $(TRUNNER_OUTPUT)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800336 $$(call msg,EXT-OBJ,$(TRUNNER_BINARY),$$@)
Andrii Nakryiko45e587b2019-10-23 08:31:28 -0700337 $$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700338
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800339# only copy extra resources if in flavored build
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700340$(TRUNNER_BINARY)-extras: $(TRUNNER_EXTRA_FILES) | $(TRUNNER_OUTPUT)
341ifneq ($2,)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800342 $$(call msg,EXT-COPY,$(TRUNNER_BINARY),$(TRUNNER_EXTRA_FILES))
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700343 cp -a $$^ $(TRUNNER_OUTPUT)/
344endif
345
346$(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS) \
347 $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ) \
348 | $(TRUNNER_BINARY)-extras
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800349 $$(call msg,BINARY,,$$@)
Andrii Nakryiko45e587b2019-10-23 08:31:28 -0700350 $$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700351
352endef
353
354# Define test_progs test runner.
355TRUNNER_TESTS_DIR := prog_tests
356TRUNNER_BPF_PROGS_DIR := progs
357TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c \
Andrii Nakryikocd492912020-05-12 12:24:42 -0700358 network_helpers.c testing_helpers.c \
359 flow_dissector_load.h
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700360TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \
361 $(wildcard progs/btf_dump_test_case_*.c)
362TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE
Andrii Nakryiko6910d7d2020-01-09 21:17:15 -0800363TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS)
Yonghong Songe13a2fe2019-10-21 21:31:19 -0700364TRUNNER_BPF_LDFLAGS := -mattr=+alu32
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700365$(eval $(call DEFINE_TEST_RUNNER,test_progs))
366
Yonghong Songe13a2fe2019-10-21 21:31:19 -0700367# Define test_progs-no_alu32 test runner.
Andrii Nakryiko24f65052019-11-19 16:25:10 -0800368TRUNNER_BPF_BUILD_RULE := CLANG_NOALU32_BPF_BUILD_RULE
Yonghong Songe13a2fe2019-10-21 21:31:19 -0700369TRUNNER_BPF_LDFLAGS :=
370$(eval $(call DEFINE_TEST_RUNNER,test_progs,no_alu32))
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700371
372# Define test_progs BPF-GCC-flavored test runner.
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200373ifneq ($(BPF_GCC),)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700374TRUNNER_BPF_BUILD_RULE := GCC_BPF_BUILD_RULE
Ilya Leoshkevich313e7f62019-10-28 11:20:49 +0100375TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(call get_sys_includes,gcc)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700376TRUNNER_BPF_LDFLAGS :=
377$(eval $(call DEFINE_TEST_RUNNER,test_progs,bpf_gcc))
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200378endif
379
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700380# Define test_maps test runner.
381TRUNNER_TESTS_DIR := map_tests
382TRUNNER_BPF_PROGS_DIR := progs
383TRUNNER_EXTRA_SOURCES := test_maps.c
384TRUNNER_EXTRA_FILES :=
385TRUNNER_BPF_BUILD_RULE := $$(error no BPF objects should be built)
386TRUNNER_BPF_CFLAGS :=
387TRUNNER_BPF_LDFLAGS :=
388$(eval $(call DEFINE_TEST_RUNNER,test_maps))
Martin KaFai Lauc0fa1b62018-04-18 15:56:06 -0700389
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700390# Define test_verifier test runner.
391# It is much simpler than test_maps/test_progs and sufficiently different from
392# them (e.g., test.h is using completely pattern), that it's worth just
393# explicitly defining all the rules explicitly.
394verifier/tests.h: verifier/*.c
Stanislav Fomicheve78e00b2019-03-06 11:59:26 -0800395 $(shell ( cd verifier/; \
Jakub Kicinski2dfb4012019-01-25 15:24:42 -0800396 echo '/* Generated header, do not edit */'; \
397 echo '#ifdef FILL_ARRAY'; \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700398 ls *.c 2> /dev/null | sed -e 's@\(.*\)@#include \"\1\"@'; \
Jakub Kicinski2dfb4012019-01-25 15:24:42 -0800399 echo '#endif' \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700400 ) > verifier/tests.h)
401$(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800402 $(call msg,BINARY,,$@)
Andrii Nakryiko45e587b2019-10-23 08:31:28 -0700403 $(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
Jakub Kicinski2dfb4012019-01-25 15:24:42 -0800404
Stanislav Fomichev6bf6aff2019-12-02 13:59:31 -0800405# Make sure we are able to include and link libbpf against c++.
Andrii Nakryiko7c8dce42019-12-26 13:02:53 -0800406$(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800407 $(call msg,CXX,,$@)
Stanislav Fomichev6bf6aff2019-12-02 13:59:31 -0800408 $(CXX) $(CFLAGS) $^ $(LDLIBS) -o $@
409
Andrii Nakryiko8e7c2a02020-05-12 12:24:43 -0700410# Benchmark runner
411$(OUTPUT)/bench_%.o: benchs/bench_%.c bench.h
412 $(call msg,CC,,$@)
413 $(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
Andrii Nakryiko4eaf0b52020-05-12 12:24:44 -0700414$(OUTPUT)/bench_rename.o: $(OUTPUT)/test_overhead.skel.h
Andrii Nakryiko8e7c2a02020-05-12 12:24:43 -0700415$(OUTPUT)/bench.o: bench.h testing_helpers.h
416$(OUTPUT)/bench: LDLIBS += -lm
417$(OUTPUT)/bench: $(OUTPUT)/bench.o $(OUTPUT)/testing_helpers.o \
Andrii Nakryiko4eaf0b52020-05-12 12:24:44 -0700418 $(OUTPUT)/bench_count.o \
419 $(OUTPUT)/bench_rename.o
Andrii Nakryiko8e7c2a02020-05-12 12:24:43 -0700420 $(call msg,BINARY,,$@)
421 $(CC) $(LDFLAGS) -o $@ $(filter %.a %.o,$^) $(LDLIBS)
422
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100423EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(SCRATCH_DIR) \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700424 prog_tests/tests.h map_tests/tests.h verifier/tests.h \
Andrii Nakryiko2031af22020-01-09 21:17:14 -0800425 feature \
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100426 $(addprefix $(OUTPUT)/,*.o *.skel.h no_alu32 bpf_gcc)