blob: 8b515a17f44b995a10e961b185a257ceff78cba9 [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 \
Weqaar Janjuaa8905252020-12-07 21:53:29 +000049 test_tc_edt.o \
50 xsk_prereqs.sh
Andrii Nakryiko74b5a592019-10-15 23:00:49 -070051
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +010052# Order correspond to 'make run_tests' order
53TEST_PROGS := test_kmod.sh \
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +010054 test_xdp_redirect.sh \
55 test_xdp_meta.sh \
Toshiaki Makita88091ff2019-06-20 11:23:23 +090056 test_xdp_veth.sh \
Andrey Ignatov622adaf2018-03-30 15:08:06 -070057 test_offload.py \
William Tu933a7412018-04-26 14:01:39 -070058 test_sock_addr.sh \
Mathieu Xhonneuxc99a84e2018-05-20 14:58:17 +010059 test_tunnel.sh \
Sean Young6bdd5332018-05-27 12:24:10 +010060 test_lwt_seg6local.sh \
Andrey Ignatov5ecd8c22018-08-12 10:49:30 -070061 test_lirc_mode2.sh \
Petar Penkov50b3ed52018-09-14 07:46:22 -070062 test_skb_cgroup_id.sh \
Jesper Dangaard Brouer97396ff2018-10-09 12:04:53 +020063 test_flow_dissector.sh \
Jesper Dangaard Brouerd35661f2019-08-01 20:00:21 +020064 test_xdp_vlan_mode_generic.sh \
65 test_xdp_vlan_mode_native.sh \
Lorenz Bauerbafc0ba2019-03-22 09:54:06 +080066 test_lwt_ip_encap.sh \
Willem de Bruijn98cdabc2019-03-22 14:32:49 -040067 test_tcp_check_syncookie.sh \
Peter Oskolkov7df5e3d2019-03-22 16:40:19 -070068 test_tc_tunnel.sh \
Alan Maguirecd538502019-05-31 18:47:14 +010069 test_tc_edt.sh \
Quentin Monnet45c5589d2019-08-30 12:00:38 +010070 test_xdping.sh \
Michal Rostecki736332742020-02-26 17:59:39 +010071 test_bpftool_build.sh \
YiFei Zhud42d1cc2020-09-15 16:45:43 -070072 test_bpftool.sh \
73 test_bpftool_metadata.sh \
Weqaar Janjuaa8905252020-12-07 21:53:29 +000074 test_xsk.sh
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +020075
Anders Roxellf98937c2019-01-08 10:23:03 +010076TEST_PROGS_EXTENDED := with_addr.sh \
Stanislav Fomichev1be72f22019-01-14 09:41:41 -080077 with_tunnels.sh \
Anders Roxell3035bb72019-08-20 15:41:21 +020078 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 \
Weqaar Janjuafacb7cb2020-12-07 21:53:30 +000083 test_lirc_mode2_user xdping test_cpp runqslower bench bpf_testmod.ko \
84 xdpxceiver
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +010085
Andrii Nakryiko74b5a592019-10-15 23:00:49 -070086TEST_CUSTOM_PROGS = urandom_read
87
Andrii Nakryiko92f74402019-12-16 22:14:25 -080088# Emit succinct information message describing current building step
89# $1 - generic step name (e.g., CC, LINK, etc);
90# $2 - optional "flavor" specifier; if provided, will be emitted as [flavor];
91# $3 - target (assumed to be file); only file name will be emitted;
92# $4 - optional extra arg, emitted as-is, if provided.
93ifeq ($(V),1)
Andrii Nakryiko965b9fe2020-01-09 21:17:16 -080094Q =
Andrii Nakryiko92f74402019-12-16 22:14:25 -080095msg =
96else
Andrii Nakryiko965b9fe2020-01-09 21:17:16 -080097Q = @
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -080098msg = @printf ' %-8s%s %s%s\n' "$(1)" "$(if $(2), [$(2)])" "$(notdir $(3))" "$(if $(4), $(4))";
Andrii Nakryiko965b9fe2020-01-09 21:17:16 -080099MAKEFLAGS += --no-print-directory
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800100submake_extras := feature_display=0
Andrii Nakryiko92f74402019-12-16 22:14:25 -0800101endif
102
103# override lib.mk's default rules
104OVERRIDE_TARGETS := 1
105override define CLEAN
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800106 $(call msg,CLEAN)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700107 $(Q)$(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
Andrii Nakryiko9f7fa222020-12-03 12:46:26 -0800108 $(Q)$(MAKE) -C bpf_testmod clean
Andrii Nakryiko92f74402019-12-16 22:14:25 -0800109endef
110
Zi Shen Lime8f1f342017-03-19 23:03:14 -0700111include ../lib.mk
Alexei Starovoitov1da8ac72017-03-10 22:05:55 -0800112
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100113SCRATCH_DIR := $(OUTPUT)/tools
114BUILD_DIR := $(SCRATCH_DIR)/build
115INCLUDE_DIR := $(SCRATCH_DIR)/include
116BPFOBJ := $(BUILD_DIR)/libbpf/libbpf.a
Jiri Olsacc15a202020-07-11 23:53:29 +0200117RESOLVE_BTFIDS := $(BUILD_DIR)/resolve_btfids/resolve_btfids
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100118
Andrii Nakryikoa67079b2020-12-10 17:59:46 -0800119VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \
120 $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \
121 ../../../../vmlinux \
122 /sys/kernel/btf/vmlinux \
123 /boot/vmlinux-$(shell uname -r)
124VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
125
Andrii Nakryiko03dcb782019-10-15 23:00:48 -0700126# Define simple and short `make test_progs`, `make test_sysctl`, etc targets
127# to build individual tests.
128# NOTE: Semicolon at the end is critical to override lib.mk's default static
129# rule for binaries.
130$(notdir $(TEST_GEN_PROGS) \
131 $(TEST_PROGS) \
132 $(TEST_PROGS_EXTENDED) \
133 $(TEST_GEN_PROGS_EXTENDED) \
134 $(TEST_CUSTOM_PROGS)): %: $(OUTPUT)/% ;
135
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700136$(OUTPUT)/%.o: %.c
137 $(call msg,CC,,$@)
138 $(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
139
Andrii Nakryiko92f74402019-12-16 22:14:25 -0800140$(OUTPUT)/%:%.c
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800141 $(call msg,BINARY,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700142 $(Q)$(LINK.c) $^ $(LDLIBS) -o $@
Andrii Nakryiko92f74402019-12-16 22:14:25 -0800143
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700144$(OUTPUT)/urandom_read: urandom_read.c
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800145 $(call msg,BINARY,,$@)
Bill Wendlinga9684332020-09-22 16:21:40 -0700146 $(Q)$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS) -Wl,--build-id=sha1
Jiong Wang3ef84342019-01-26 12:26:14 -0500147
Andrii Nakryiko9f7fa222020-12-03 12:46:26 -0800148$(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(wildcard bpf_testmod/Makefile bpf_testmod/*.[ch])
149 $(call msg,MOD,,$@)
Andrii Nakryikoa67079b2020-12-10 17:59:46 -0800150 $(Q)$(RM) bpf_testmod/bpf_testmod.ko # force re-compilation
Andrii Nakryiko9f7fa222020-12-03 12:46:26 -0800151 $(Q)$(MAKE) $(submake_extras) -C bpf_testmod
152 $(Q)cp bpf_testmod/bpf_testmod.ko $@
153
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100154$(OUTPUT)/test_stub.o: test_stub.c $(BPFOBJ)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800155 $(call msg,CC,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700156 $(Q)$(CC) -c $(CFLAGS) -o $@ $<
Ilya Leoshkeviche93d9912019-10-28 11:21:10 +0100157
Andrii Nakryiko6bcaf412020-08-04 17:47:57 -0700158DEFAULT_BPFTOOL := $(SCRATCH_DIR)/sbin/bpftool
159
160$(OUTPUT)/runqslower: $(BPFOBJ) | $(DEFAULT_BPFTOOL)
Toke Høiland-Jørgensenbfe42792020-01-20 14:06:43 +0100161 $(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/runqslower \
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100162 OUTPUT=$(SCRATCH_DIR)/ VMLINUX_BTF=$(VMLINUX_BTF) \
Veronika Kabatovab26d1e22020-04-28 19:37:42 +0200163 BPFOBJ=$(BPFOBJ) BPF_INCLUDE=$(INCLUDE_DIR) && \
164 cp $(SCRATCH_DIR)/runqslower $@
Zi Shen Lime8f1f342017-03-19 23:03:14 -0700165
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700166$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/test_stub.o $(BPFOBJ)
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +0100167
Jesper Dangaard Brouer41757dc2018-02-13 14:19:15 +0100168$(OUTPUT)/test_dev_cgroup: cgroup_helpers.c
Andrey Ignatov5ecd8c22018-08-12 10:49:30 -0700169$(OUTPUT)/test_skb_cgroup_id_user: cgroup_helpers.c
Andrey Ignatov1d436882018-03-30 15:08:08 -0700170$(OUTPUT)/test_sock: cgroup_helpers.c
Andrey Ignatove50b0a62018-03-30 15:08:03 -0700171$(OUTPUT)/test_sock_addr: cgroup_helpers.c
Andrey Ignatov194db0d2018-07-30 17:42:31 -0700172$(OUTPUT)/test_socket_cookie: cgroup_helpers.c
John Fastabend16962b22018-04-23 14:30:38 -0700173$(OUTPUT)/test_sockmap: cgroup_helpers.c
Sowmini Varadhan435f90a2018-11-07 16:12:02 -0800174$(OUTPUT)/test_tcpnotify_user: cgroup_helpers.c trace_helpers.c
Yonghong Songf2690992018-06-03 15:59:43 -0700175$(OUTPUT)/get_cgroup_id_user: cgroup_helpers.c
Roman Gushchin68cfa3a2018-08-02 14:27:29 -0700176$(OUTPUT)/test_cgroup_storage: cgroup_helpers.c
Roman Gushchin371e4fc2018-09-28 14:46:00 +0000177$(OUTPUT)/test_netcnt: cgroup_helpers.c
Martin KaFai Laue0b27b32019-02-09 23:22:28 -0800178$(OUTPUT)/test_sock_fields: cgroup_helpers.c
Andrey Ignatov1f5fa9a2019-03-08 15:08:21 -0800179$(OUTPUT)/test_sysctl: cgroup_helpers.c
Jesper Dangaard Brouer41757dc2018-02-13 14:19:15 +0100180
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800181BPFTOOL ?= $(DEFAULT_BPFTOOL)
Andrii Nakryiko03506292020-01-23 21:41:48 -0800182$(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) \
183 $(BPFOBJ) | $(BUILD_DIR)/bpftool
184 $(Q)$(MAKE) $(submake_extras) -C $(BPFTOOLDIR) \
185 OUTPUT=$(BUILD_DIR)/bpftool/ \
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100186 prefix= DESTDIR=$(SCRATCH_DIR)/ install
Quentin Monnet41d5c372020-09-09 17:22:51 +0100187 $(Q)mkdir -p $(BUILD_DIR)/bpftool/Documentation
188 $(Q)RST2MAN_OPTS="--exit-status=1" $(MAKE) $(submake_extras) \
189 -C $(BPFTOOLDIR)/Documentation \
190 OUTPUT=$(BUILD_DIR)/bpftool/Documentation/ \
191 prefix= DESTDIR=$(SCRATCH_DIR)/ install
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800192
Andrii Nakryiko03506292020-01-23 21:41:48 -0800193$(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile) \
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100194 ../../../include/uapi/linux/bpf.h \
195 | $(INCLUDE_DIR) $(BUILD_DIR)/libbpf
196 $(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(BUILD_DIR)/libbpf/ \
Andrii Nakryiko03506292020-01-23 21:41:48 -0800197 DESTDIR=$(SCRATCH_DIR) prefix= all install_headers
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800198
Jiri Olsacc15a202020-07-11 23:53:29 +0200199$(BUILD_DIR)/libbpf $(BUILD_DIR)/bpftool $(BUILD_DIR)/resolve_btfids $(INCLUDE_DIR):
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100200 $(call msg,MKDIR,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700201 $(Q)mkdir -p $@
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700202
Andrii Nakryikoacbd0622020-03-13 10:23:36 -0700203$(INCLUDE_DIR)/vmlinux.h: $(VMLINUX_BTF) | $(BPFTOOL) $(INCLUDE_DIR)
Andrii Nakryikoca4db632020-06-29 17:47:59 -0700204ifeq ($(VMLINUX_H),)
Andrii Nakryikoacbd0622020-03-13 10:23:36 -0700205 $(call msg,GEN,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700206 $(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
Andrii Nakryikoca4db632020-06-29 17:47:59 -0700207else
208 $(call msg,CP,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700209 $(Q)cp "$(VMLINUX_H)" $@
Andrii Nakryikoca4db632020-06-29 17:47:59 -0700210endif
Andrii Nakryikoacbd0622020-03-13 10:23:36 -0700211
Jiri Olsacc15a202020-07-11 23:53:29 +0200212$(RESOLVE_BTFIDS): $(BPFOBJ) | $(BUILD_DIR)/resolve_btfids \
213 $(TOOLSDIR)/bpf/resolve_btfids/main.c \
214 $(TOOLSDIR)/lib/rbtree.c \
215 $(TOOLSDIR)/lib/zalloc.c \
216 $(TOOLSDIR)/lib/string.c \
217 $(TOOLSDIR)/lib/ctype.c \
218 $(TOOLSDIR)/lib/str_error_r.c
219 $(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/resolve_btfids \
220 OUTPUT=$(BUILD_DIR)/resolve_btfids/ BPFOBJ=$(BPFOBJ)
221
Sirio Balmelli167381f2018-05-21 09:00:03 +0200222# Get Clang's default includes on this system, as opposed to those seen by
223# '-target bpf'. This fixes "missing" files on some architectures/distros,
224# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
225#
226# Use '-idirafter': Don't interfere with include mechanics except where the
227# build would have failed anyways.
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200228define get_sys_includes
229$(shell $(1) -v -E - </dev/null 2>&1 \
Björn Töpel6016df82020-11-18 08:16:38 +0100230 | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
231$(shell $(1) -dM -E - </dev/null | grep '#define __riscv_xlen ' | sed 's/#define /-D/' | sed 's/ /=/')
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200232endef
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700233
Ilya Leoshkevich313e7f62019-10-28 11:20:49 +0100234# Determine target endianness.
235IS_LITTLE_ENDIAN = $(shell $(CC) -dM -E - </dev/null | \
236 grep 'define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__')
237MENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian)
238
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200239CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
Ilya Leoshkevich313e7f62019-10-28 11:20:49 +0100240BPF_CFLAGS = -g -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN) \
Tobias Klauserbcd66b12020-03-13 12:31:05 +0100241 -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR) \
242 -I$(abspath $(OUTPUT)/../usr/include)
Sirio Balmelli167381f2018-05-21 09:00:03 +0200243
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200244CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \
245 -Wno-compare-distinct-pointer-types
Alexei Starovoitov3bc35c62017-12-14 17:55:11 -0800246
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200247$(OUTPUT)/test_l4lb_noinline.o: BPF_CFLAGS += -fno-inline
248$(OUTPUT)/test_xdp_noinline.o: BPF_CFLAGS += -fno-inline
Alexei Starovoitov3bc35c62017-12-14 17:55:11 -0800249
Stanislav Fomichevbf0f0fd2019-01-28 08:53:55 -0800250$(OUTPUT)/flow_dissector_load.o: flow_dissector_load.h
Stanislav Fomichevbf0f0fd2019-01-28 08:53:55 -0800251
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700252# Build BPF object using Clang
253# $1 - input .c file
254# $2 - output .o file
255# $3 - CFLAGS
256# $4 - LDFLAGS
257define CLANG_BPF_BUILD_RULE
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800258 $(call msg,CLNG-LLC,$(TRUNNER_BINARY),$2)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700259 $(Q)($(CLANG) $3 -O2 -target bpf -emit-llvm \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700260 -c $1 -o - || echo "BPF obj compilation failed") | \
Yonghong Song83250f22020-02-18 16:42:36 -0800261 $(LLC) -mattr=dwarfris -march=bpf -mcpu=v3 $4 -filetype=obj -o $2
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700262endef
Andrii Nakryiko24f65052019-11-19 16:25:10 -0800263# Similar to CLANG_BPF_BUILD_RULE, but with disabled alu32
264define CLANG_NOALU32_BPF_BUILD_RULE
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800265 $(call msg,CLNG-LLC,$(TRUNNER_BINARY),$2)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700266 $(Q)($(CLANG) $3 -O2 -target bpf -emit-llvm \
Andrii Nakryiko24f65052019-11-19 16:25:10 -0800267 -c $1 -o - || echo "BPF obj compilation failed") | \
268 $(LLC) -march=bpf -mcpu=v2 $4 -filetype=obj -o $2
269endef
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700270# Similar to CLANG_BPF_BUILD_RULE, but using native Clang and bpf LLC
271define CLANG_NATIVE_BPF_BUILD_RULE
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800272 $(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700273 $(Q)($(CLANG) $3 -O2 -emit-llvm \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700274 -c $1 -o - || echo "BPF obj compilation failed") | \
Yonghong Song83250f22020-02-18 16:42:36 -0800275 $(LLC) -march=bpf -mcpu=v3 $4 -filetype=obj -o $2
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700276endef
277# Build BPF object using GCC
278define GCC_BPF_BUILD_RULE
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800279 $(call msg,GCC-BPF,$(TRUNNER_BINARY),$2)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700280 $(Q)$(BPF_GCC) $3 $4 -O2 -c $1 -o $2
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700281endef
Stanislav Fomicheve78e00b2019-03-06 11:59:26 -0800282
Jakub Sitnicki0b9ad562020-04-29 20:11:54 +0200283SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800284
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700285# Set up extra TRUNNER_XXX "temporary" variables in the environment (relies on
286# $eval()) and pass control to DEFINE_TEST_RUNNER_RULES.
287# Parameters:
288# $1 - test runner base binary name (e.g., test_progs)
Yonghong Songe13a2fe2019-10-21 21:31:19 -0700289# $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700290define DEFINE_TEST_RUNNER
Jiong Wang3ef84342019-01-26 12:26:14 -0500291
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700292TRUNNER_OUTPUT := $(OUTPUT)$(if $2,/)$2
293TRUNNER_BINARY := $1$(if $2,-)$2
294TRUNNER_TEST_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.test.o, \
295 $$(notdir $$(wildcard $(TRUNNER_TESTS_DIR)/*.c)))
296TRUNNER_EXTRA_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, \
297 $$(filter %.c,$(TRUNNER_EXTRA_SOURCES)))
298TRUNNER_EXTRA_HDRS := $$(filter %.h,$(TRUNNER_EXTRA_SOURCES))
299TRUNNER_TESTS_HDR := $(TRUNNER_TESTS_DIR)/tests.h
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800300TRUNNER_BPF_SRCS := $$(notdir $$(wildcard $(TRUNNER_BPF_PROGS_DIR)/*.c))
301TRUNNER_BPF_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, $$(TRUNNER_BPF_SRCS))
302TRUNNER_BPF_SKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.skel.h, \
303 $$(filter-out $(SKEL_BLACKLIST), \
304 $$(TRUNNER_BPF_SRCS)))
Yauheni Kaliuta309b81f2020-05-13 05:17:22 +0300305TEST_GEN_FILES += $$(TRUNNER_BPF_OBJS)
Jiong Wang3ef84342019-01-26 12:26:14 -0500306
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700307# Evaluate rules now with extra TRUNNER_XXX variables above already defined
308$$(eval $$(call DEFINE_TEST_RUNNER_RULES,$1,$2))
Stanislav Fomichev3f306582019-03-01 19:42:13 -0800309
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700310endef
Jiong Wang3ef84342019-01-26 12:26:14 -0500311
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700312# Using TRUNNER_XXX variables, provided by callers of DEFINE_TEST_RUNNER and
313# set up by DEFINE_TEST_RUNNER itself, create test runner build rules with:
314# $1 - test runner base binary name (e.g., test_progs)
Yonghong Songe13a2fe2019-10-21 21:31:19 -0700315# $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700316define DEFINE_TEST_RUNNER_RULES
317
318ifeq ($($(TRUNNER_OUTPUT)-dir),)
319$(TRUNNER_OUTPUT)-dir := y
320$(TRUNNER_OUTPUT):
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800321 $$(call msg,MKDIR,,$$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700322 $(Q)mkdir -p $$@
Jiong Wang3ef84342019-01-26 12:26:14 -0500323endif
324
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700325# ensure we set up BPF objects generation rule just once for a given
326# input/output directory combination
327ifeq ($($(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs),)
328$(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs := y
329$(TRUNNER_BPF_OBJS): $(TRUNNER_OUTPUT)/%.o: \
330 $(TRUNNER_BPF_PROGS_DIR)/%.c \
331 $(TRUNNER_BPF_PROGS_DIR)/*.h \
Andrii Nakryikoacbd0622020-03-13 10:23:36 -0700332 $$(INCLUDE_DIR)/vmlinux.h \
Andrii Nakryiko3ac2e202020-08-20 16:12:35 -0700333 $(wildcard $(BPFDIR)/bpf_*.h) | $(TRUNNER_OUTPUT)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700334 $$(call $(TRUNNER_BPF_BUILD_RULE),$$<,$$@, \
335 $(TRUNNER_BPF_CFLAGS), \
336 $(TRUNNER_BPF_LDFLAGS))
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800337
338$(TRUNNER_BPF_SKELS): $(TRUNNER_OUTPUT)/%.skel.h: \
339 $(TRUNNER_OUTPUT)/%.o \
340 | $(BPFTOOL) $(TRUNNER_OUTPUT)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800341 $$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700342 $(Q)$$(BPFTOOL) gen skeleton $$< > $$@
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700343endif
344
345# ensure we set up tests.h header generation rule just once
346ifeq ($($(TRUNNER_TESTS_DIR)-tests-hdr),)
347$(TRUNNER_TESTS_DIR)-tests-hdr := y
348$(TRUNNER_TESTS_HDR): $(TRUNNER_TESTS_DIR)/*.c
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800349 $$(call msg,TEST-HDR,$(TRUNNER_BINARY),$$@)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700350 $$(shell ( cd $(TRUNNER_TESTS_DIR); \
351 echo '/* Generated header, do not edit */'; \
352 ls *.c 2> /dev/null | \
353 sed -e 's@\([^\.]*\)\.c@DEFINE_TEST(\1)@'; \
354 ) > $$@)
355endif
356
357# compile individual test files
358# Note: we cd into output directory to ensure embedded BPF object is found
359$(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o: \
360 $(TRUNNER_TESTS_DIR)/%.c \
361 $(TRUNNER_EXTRA_HDRS) \
362 $(TRUNNER_BPF_OBJS) \
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800363 $(TRUNNER_BPF_SKELS) \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700364 $$(BPFOBJ) | $(TRUNNER_OUTPUT)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800365 $$(call msg,TEST-OBJ,$(TRUNNER_BINARY),$$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700366 $(Q)cd $$(@D) && $$(CC) -I. $$(CFLAGS) -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700367
368$(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o: \
369 %.c \
370 $(TRUNNER_EXTRA_HDRS) \
371 $(TRUNNER_TESTS_HDR) \
372 $$(BPFOBJ) | $(TRUNNER_OUTPUT)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800373 $$(call msg,EXT-OBJ,$(TRUNNER_BINARY),$$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700374 $(Q)$$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700375
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800376# only copy extra resources if in flavored build
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700377$(TRUNNER_BINARY)-extras: $(TRUNNER_EXTRA_FILES) | $(TRUNNER_OUTPUT)
378ifneq ($2,)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800379 $$(call msg,EXT-COPY,$(TRUNNER_BINARY),$(TRUNNER_EXTRA_FILES))
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700380 $(Q)cp -a $$^ $(TRUNNER_OUTPUT)/
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700381endif
382
383$(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS) \
384 $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ) \
Jiri Olsacc15a202020-07-11 23:53:29 +0200385 $(RESOLVE_BTFIDS) \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700386 | $(TRUNNER_BINARY)-extras
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800387 $$(call msg,BINARY,,$$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700388 $(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
Brendan Jackman58c185b2020-12-03 12:08:50 +0000389 $(Q)$(RESOLVE_BTFIDS) --no-fail --btf $(TRUNNER_OUTPUT)/btf_data.o $$@
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700390
391endef
392
393# Define test_progs test runner.
394TRUNNER_TESTS_DIR := prog_tests
395TRUNNER_BPF_PROGS_DIR := progs
396TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c \
Andrii Nakryikocd492912020-05-12 12:24:42 -0700397 network_helpers.c testing_helpers.c \
Andrii Nakryiko1306c982020-11-04 20:33:56 -0800398 btf_helpers.c flow_dissector_load.h
Andrii Nakryiko9f7fa222020-12-03 12:46:26 -0800399TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko \
KP Singh854055c2020-11-26 18:49:46 +0000400 ima_setup.sh \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700401 $(wildcard progs/btf_dump_test_case_*.c)
402TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE
Andrii Nakryiko6910d7d2020-01-09 21:17:15 -0800403TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS)
Yonghong Songe13a2fe2019-10-21 21:31:19 -0700404TRUNNER_BPF_LDFLAGS := -mattr=+alu32
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700405$(eval $(call DEFINE_TEST_RUNNER,test_progs))
406
Yonghong Songe13a2fe2019-10-21 21:31:19 -0700407# Define test_progs-no_alu32 test runner.
Andrii Nakryiko24f65052019-11-19 16:25:10 -0800408TRUNNER_BPF_BUILD_RULE := CLANG_NOALU32_BPF_BUILD_RULE
Yonghong Songe13a2fe2019-10-21 21:31:19 -0700409TRUNNER_BPF_LDFLAGS :=
410$(eval $(call DEFINE_TEST_RUNNER,test_progs,no_alu32))
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700411
412# Define test_progs BPF-GCC-flavored test runner.
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200413ifneq ($(BPF_GCC),)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700414TRUNNER_BPF_BUILD_RULE := GCC_BPF_BUILD_RULE
Ilya Leoshkevich313e7f62019-10-28 11:20:49 +0100415TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(call get_sys_includes,gcc)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700416TRUNNER_BPF_LDFLAGS :=
417$(eval $(call DEFINE_TEST_RUNNER,test_progs,bpf_gcc))
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200418endif
419
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700420# Define test_maps test runner.
421TRUNNER_TESTS_DIR := map_tests
422TRUNNER_BPF_PROGS_DIR := progs
423TRUNNER_EXTRA_SOURCES := test_maps.c
424TRUNNER_EXTRA_FILES :=
425TRUNNER_BPF_BUILD_RULE := $$(error no BPF objects should be built)
426TRUNNER_BPF_CFLAGS :=
427TRUNNER_BPF_LDFLAGS :=
428$(eval $(call DEFINE_TEST_RUNNER,test_maps))
Martin KaFai Lauc0fa1b62018-04-18 15:56:06 -0700429
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700430# Define test_verifier test runner.
431# It is much simpler than test_maps/test_progs and sufficiently different from
432# them (e.g., test.h is using completely pattern), that it's worth just
433# explicitly defining all the rules explicitly.
434verifier/tests.h: verifier/*.c
Stanislav Fomicheve78e00b2019-03-06 11:59:26 -0800435 $(shell ( cd verifier/; \
Jakub Kicinski2dfb4012019-01-25 15:24:42 -0800436 echo '/* Generated header, do not edit */'; \
437 echo '#ifdef FILL_ARRAY'; \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700438 ls *.c 2> /dev/null | sed -e 's@\(.*\)@#include \"\1\"@'; \
Jakub Kicinski2dfb4012019-01-25 15:24:42 -0800439 echo '#endif' \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700440 ) > verifier/tests.h)
441$(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800442 $(call msg,BINARY,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700443 $(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
Jakub Kicinski2dfb4012019-01-25 15:24:42 -0800444
Stanislav Fomichev6bf6aff2019-12-02 13:59:31 -0800445# Make sure we are able to include and link libbpf against c++.
Andrii Nakryiko7c8dce42019-12-26 13:02:53 -0800446$(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800447 $(call msg,CXX,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700448 $(Q)$(CXX) $(CFLAGS) $^ $(LDLIBS) -o $@
Stanislav Fomichev6bf6aff2019-12-02 13:59:31 -0800449
Andrii Nakryiko8e7c2a02020-05-12 12:24:43 -0700450# Benchmark runner
451$(OUTPUT)/bench_%.o: benchs/bench_%.c bench.h
452 $(call msg,CC,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700453 $(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
Andrii Nakryiko4eaf0b52020-05-12 12:24:44 -0700454$(OUTPUT)/bench_rename.o: $(OUTPUT)/test_overhead.skel.h
Andrii Nakryikoc5d420c2020-05-12 12:24:45 -0700455$(OUTPUT)/bench_trigger.o: $(OUTPUT)/trigger_bench.skel.h
Andrii Nakryikoc97099b2020-05-29 00:54:23 -0700456$(OUTPUT)/bench_ringbufs.o: $(OUTPUT)/ringbuf_bench.skel.h \
457 $(OUTPUT)/perfbuf_bench.skel.h
Andrii Nakryiko8e7c2a02020-05-12 12:24:43 -0700458$(OUTPUT)/bench.o: bench.h testing_helpers.h
459$(OUTPUT)/bench: LDLIBS += -lm
460$(OUTPUT)/bench: $(OUTPUT)/bench.o $(OUTPUT)/testing_helpers.o \
Andrii Nakryiko4eaf0b52020-05-12 12:24:44 -0700461 $(OUTPUT)/bench_count.o \
Andrii Nakryikoc5d420c2020-05-12 12:24:45 -0700462 $(OUTPUT)/bench_rename.o \
Andrii Nakryikoc97099b2020-05-29 00:54:23 -0700463 $(OUTPUT)/bench_trigger.o \
464 $(OUTPUT)/bench_ringbufs.o
Andrii Nakryiko8e7c2a02020-05-12 12:24:43 -0700465 $(call msg,BINARY,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700466 $(Q)$(CC) $(LDFLAGS) -o $@ $(filter %.a %.o,$^) $(LDLIBS)
Andrii Nakryiko8e7c2a02020-05-12 12:24:43 -0700467
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100468EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(SCRATCH_DIR) \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700469 prog_tests/tests.h map_tests/tests.h verifier/tests.h \
Andrii Nakryiko2031af22020-01-09 21:17:14 -0800470 feature \
Andrii Nakryiko9f7fa222020-12-03 12:46:26 -0800471 $(addprefix $(OUTPUT)/,*.o *.skel.h no_alu32 bpf_gcc bpf_testmod.ko)