blob: a81af15e4ded6635c1d68471ec2a81477becedd3 [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
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +01004include ../../../scripts/Makefile.include
Hendrik Brueckner618e1652017-12-04 10:56:48 +01005
Ilya Leoshkeviche7ad28e2020-06-02 19:56:49 +02006CXX ?= $(CROSS_COMPILE)g++
7
Andrii Nakryiko74b5a592019-10-15 23:00:49 -07008CURDIR := $(abspath .)
Andrii Nakryikof3c926a2019-12-13 17:43:38 -08009TOOLSDIR := $(abspath ../../..)
10LIBDIR := $(TOOLSDIR)/lib
Zi Shen Lime8f1f342017-03-19 23:03:14 -070011BPFDIR := $(LIBDIR)/bpf
Andrii Nakryikof3c926a2019-12-13 17:43:38 -080012TOOLSINCDIR := $(TOOLSDIR)/include
13BPFTOOLDIR := $(TOOLSDIR)/bpf/bpftool
14APIDIR := $(TOOLSINCDIR)/uapi
Andrii Nakryiko74b5a592019-10-15 23:00:49 -070015GENDIR := $(abspath ../../../../include/generated)
Daniel Borkmann02ea80b2017-03-31 02:24:04 +020016GENHDR := $(GENDIR)/autoconf.h
Mickaël Salaünd498f872017-02-11 23:20:23 +010017
Daniel Borkmann02ea80b2017-03-31 02:24:04 +020018ifneq ($(wildcard $(GENHDR)),)
19 GENFLAGS := -DHAVE_GENHDR
20endif
21
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) \
Jean-Philippe Brucker5837ced2021-01-13 17:33:17 +010026 -I$(TOOLSINCDIR) -I$(APIDIR) -I$(OUTPUT) \
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 \
Alexander Duyckaaf376b2020-11-03 13:34:48 -080033 test_verifier_log test_dev_cgroup \
Florent Revest61f8c9c2021-02-10 12:14:04 +010034 test_sock test_sockmap get_cgroup_id_user \
Jakub Sitnicki415bb4e2019-12-12 11:22:58 +010035 test_cgroup_storage \
Martin KaFai Lau6f521a22020-09-24 17:04:27 -070036 test_netcnt test_tcpnotify_user test_sysctl \
Carlos Neira09c02d52021-01-14 11:10:36 -030037 test_progs-no_alu32
Jiong Wang3ef84342019-01-26 12:26:14 -050038
Andrii Nakryiko74b5a592019-10-15 23:00:49 -070039# Also test bpf-gcc, if present
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +020040ifneq ($(BPF_GCC),)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -070041TEST_GEN_PROGS += test_progs-bpf_gcc
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +020042endif
43
Jean-Philippe Bruckerd6ac8ca2021-01-13 17:33:18 +010044TEST_GEN_FILES = test_lwt_ip_encap.o \
45 test_tc_edt.o
Jean-Philippe Bruckerb8d1cbe2021-01-13 17:33:20 +010046TEST_FILES = xsk_prereqs.sh \
47 $(wildcard progs/btf_dump_test_case_*.c)
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 \
YiFei Zhud42d1cc2020-09-15 16:45:43 -070069 test_bpftool.sh \
70 test_bpftool_metadata.sh \
Weqaar Janjuaa8905252020-12-07 21:53:29 +000071 test_xsk.sh
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +020072
Anders Roxellf98937c2019-01-08 10:23:03 +010073TEST_PROGS_EXTENDED := with_addr.sh \
Stanislav Fomichev1be72f22019-01-14 09:41:41 -080074 with_tunnels.sh \
Anders Roxell3035bb72019-08-20 15:41:21 +020075 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 \
Weqaar Janjuafacb7cb2020-12-07 21:53:30 +000080 test_lirc_mode2_user xdping test_cpp runqslower bench bpf_testmod.ko \
81 xdpxceiver
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +010082
Jean-Philippe Bruckerca1e8462021-01-13 17:33:19 +010083TEST_CUSTOM_PROGS = $(OUTPUT)/urandom_read
Andrii Nakryiko74b5a592019-10-15 23:00:49 -070084
Andrii Nakryiko92f74402019-12-16 22:14:25 -080085# Emit succinct information message describing current building step
86# $1 - generic step name (e.g., CC, LINK, etc);
87# $2 - optional "flavor" specifier; if provided, will be emitted as [flavor];
88# $3 - target (assumed to be file); only file name will be emitted;
89# $4 - optional extra arg, emitted as-is, if provided.
90ifeq ($(V),1)
Andrii Nakryiko965b9fe2020-01-09 21:17:16 -080091Q =
Andrii Nakryiko92f74402019-12-16 22:14:25 -080092msg =
93else
Andrii Nakryiko965b9fe2020-01-09 21:17:16 -080094Q = @
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -080095msg = @printf ' %-8s%s %s%s\n' "$(1)" "$(if $(2), [$(2)])" "$(notdir $(3))" "$(if $(4), $(4))";
Andrii Nakryiko965b9fe2020-01-09 21:17:16 -080096MAKEFLAGS += --no-print-directory
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -080097submake_extras := feature_display=0
Andrii Nakryiko92f74402019-12-16 22:14:25 -080098endif
99
100# override lib.mk's default rules
101OVERRIDE_TARGETS := 1
102override define CLEAN
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800103 $(call msg,CLEAN)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700104 $(Q)$(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
Andrii Nakryiko9f7fa222020-12-03 12:46:26 -0800105 $(Q)$(MAKE) -C bpf_testmod clean
Andrii Nakryiko92f74402019-12-16 22:14:25 -0800106endef
107
Zi Shen Lime8f1f342017-03-19 23:03:14 -0700108include ../lib.mk
Alexei Starovoitov1da8ac72017-03-10 22:05:55 -0800109
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100110SCRATCH_DIR := $(OUTPUT)/tools
111BUILD_DIR := $(SCRATCH_DIR)/build
112INCLUDE_DIR := $(SCRATCH_DIR)/include
113BPFOBJ := $(BUILD_DIR)/libbpf/libbpf.a
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100114ifneq ($(CROSS_COMPILE),)
115HOST_BUILD_DIR := $(BUILD_DIR)/host
116HOST_SCRATCH_DIR := $(OUTPUT)/host-tools
117else
118HOST_BUILD_DIR := $(BUILD_DIR)
119HOST_SCRATCH_DIR := $(SCRATCH_DIR)
120endif
121HOST_BPFOBJ := $(HOST_BUILD_DIR)/libbpf/libbpf.a
122RESOLVE_BTFIDS := $(HOST_BUILD_DIR)/resolve_btfids/resolve_btfids
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100123
Andrii Nakryikoa67079b2020-12-10 17:59:46 -0800124VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \
125 $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \
126 ../../../../vmlinux \
127 /sys/kernel/btf/vmlinux \
128 /boot/vmlinux-$(shell uname -r)
129VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
Kamal Mostafa1a3449c2020-12-15 10:20:10 -0800130ifeq ($(VMLINUX_BTF),)
131$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)")
132endif
Andrii Nakryikoa67079b2020-12-10 17:59:46 -0800133
Andrii Nakryiko03dcb782019-10-15 23:00:48 -0700134# Define simple and short `make test_progs`, `make test_sysctl`, etc targets
135# to build individual tests.
136# NOTE: Semicolon at the end is critical to override lib.mk's default static
137# rule for binaries.
138$(notdir $(TEST_GEN_PROGS) \
139 $(TEST_PROGS) \
140 $(TEST_PROGS_EXTENDED) \
141 $(TEST_GEN_PROGS_EXTENDED) \
142 $(TEST_CUSTOM_PROGS)): %: $(OUTPUT)/% ;
143
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100144# sort removes libbpf duplicates when not cross-building
145MAKE_DIRS := $(sort $(BUILD_DIR)/libbpf $(HOST_BUILD_DIR)/libbpf \
146 $(HOST_BUILD_DIR)/bpftool $(HOST_BUILD_DIR)/resolve_btfids \
147 $(INCLUDE_DIR))
148$(MAKE_DIRS):
149 $(call msg,MKDIR,,$@)
150 $(Q)mkdir -p $@
151
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700152$(OUTPUT)/%.o: %.c
153 $(call msg,CC,,$@)
154 $(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
155
Andrii Nakryiko92f74402019-12-16 22:14:25 -0800156$(OUTPUT)/%:%.c
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800157 $(call msg,BINARY,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700158 $(Q)$(LINK.c) $^ $(LDLIBS) -o $@
Andrii Nakryiko92f74402019-12-16 22:14:25 -0800159
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700160$(OUTPUT)/urandom_read: urandom_read.c
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800161 $(call msg,BINARY,,$@)
Bill Wendlinga9684332020-09-22 16:21:40 -0700162 $(Q)$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS) -Wl,--build-id=sha1
Jiong Wang3ef84342019-01-26 12:26:14 -0500163
Andrii Nakryiko9f7fa222020-12-03 12:46:26 -0800164$(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(wildcard bpf_testmod/Makefile bpf_testmod/*.[ch])
165 $(call msg,MOD,,$@)
Andrii Nakryikoa67079b2020-12-10 17:59:46 -0800166 $(Q)$(RM) bpf_testmod/bpf_testmod.ko # force re-compilation
Andrii Nakryiko9f7fa222020-12-03 12:46:26 -0800167 $(Q)$(MAKE) $(submake_extras) -C bpf_testmod
168 $(Q)cp bpf_testmod/bpf_testmod.ko $@
169
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100170$(OUTPUT)/test_stub.o: test_stub.c $(BPFOBJ)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800171 $(call msg,CC,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700172 $(Q)$(CC) -c $(CFLAGS) -o $@ $<
Ilya Leoshkeviche93d9912019-10-28 11:21:10 +0100173
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100174DEFAULT_BPFTOOL := $(HOST_SCRATCH_DIR)/sbin/bpftool
Andrii Nakryiko6bcaf412020-08-04 17:47:57 -0700175
176$(OUTPUT)/runqslower: $(BPFOBJ) | $(DEFAULT_BPFTOOL)
Toke Høiland-Jørgensenbfe42792020-01-20 14:06:43 +0100177 $(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/runqslower \
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100178 OUTPUT=$(SCRATCH_DIR)/ VMLINUX_BTF=$(VMLINUX_BTF) \
Veronika Kabatovab26d1e22020-04-28 19:37:42 +0200179 BPFOBJ=$(BPFOBJ) BPF_INCLUDE=$(INCLUDE_DIR) && \
180 cp $(SCRATCH_DIR)/runqslower $@
Zi Shen Lime8f1f342017-03-19 23:03:14 -0700181
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700182$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/test_stub.o $(BPFOBJ)
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +0100183
Jesper Dangaard Brouer41757dc2018-02-13 14:19:15 +0100184$(OUTPUT)/test_dev_cgroup: cgroup_helpers.c
Andrey Ignatov5ecd8c22018-08-12 10:49:30 -0700185$(OUTPUT)/test_skb_cgroup_id_user: cgroup_helpers.c
Andrey Ignatov1d436882018-03-30 15:08:08 -0700186$(OUTPUT)/test_sock: cgroup_helpers.c
Andrey Ignatove50b0a62018-03-30 15:08:03 -0700187$(OUTPUT)/test_sock_addr: cgroup_helpers.c
John Fastabend16962b22018-04-23 14:30:38 -0700188$(OUTPUT)/test_sockmap: cgroup_helpers.c
Sowmini Varadhan435f90a2018-11-07 16:12:02 -0800189$(OUTPUT)/test_tcpnotify_user: cgroup_helpers.c trace_helpers.c
Yonghong Songf2690992018-06-03 15:59:43 -0700190$(OUTPUT)/get_cgroup_id_user: cgroup_helpers.c
Roman Gushchin68cfa3a2018-08-02 14:27:29 -0700191$(OUTPUT)/test_cgroup_storage: cgroup_helpers.c
Roman Gushchin371e4fc2018-09-28 14:46:00 +0000192$(OUTPUT)/test_netcnt: cgroup_helpers.c
Martin KaFai Laue0b27b32019-02-09 23:22:28 -0800193$(OUTPUT)/test_sock_fields: cgroup_helpers.c
Andrey Ignatov1f5fa9a2019-03-08 15:08:21 -0800194$(OUTPUT)/test_sysctl: cgroup_helpers.c
Jesper Dangaard Brouer41757dc2018-02-13 14:19:15 +0100195
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800196BPFTOOL ?= $(DEFAULT_BPFTOOL)
Andrii Nakryiko03506292020-01-23 21:41:48 -0800197$(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) \
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100198 $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/bpftool
Andrii Nakryiko03506292020-01-23 21:41:48 -0800199 $(Q)$(MAKE) $(submake_extras) -C $(BPFTOOLDIR) \
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100200 CC=$(HOSTCC) LD=$(HOSTLD) \
201 OUTPUT=$(HOST_BUILD_DIR)/bpftool/ \
202 prefix= DESTDIR=$(HOST_SCRATCH_DIR)/ install
Quentin Monnet41d5c372020-09-09 17:22:51 +0100203 $(Q)mkdir -p $(BUILD_DIR)/bpftool/Documentation
204 $(Q)RST2MAN_OPTS="--exit-status=1" $(MAKE) $(submake_extras) \
205 -C $(BPFTOOLDIR)/Documentation \
206 OUTPUT=$(BUILD_DIR)/bpftool/Documentation/ \
207 prefix= DESTDIR=$(SCRATCH_DIR)/ install
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800208
Andrii Nakryiko03506292020-01-23 21:41:48 -0800209$(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile) \
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100210 ../../../include/uapi/linux/bpf.h \
211 | $(INCLUDE_DIR) $(BUILD_DIR)/libbpf
212 $(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(BUILD_DIR)/libbpf/ \
Andrii Nakryiko03506292020-01-23 21:41:48 -0800213 DESTDIR=$(SCRATCH_DIR) prefix= all install_headers
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800214
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100215ifneq ($(BPFOBJ),$(HOST_BPFOBJ))
216$(HOST_BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile) \
217 ../../../include/uapi/linux/bpf.h \
218 | $(INCLUDE_DIR) $(HOST_BUILD_DIR)/libbpf
219 $(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) \
220 OUTPUT=$(HOST_BUILD_DIR)/libbpf/ CC=$(HOSTCC) LD=$(HOSTLD) \
221 DESTDIR=$(HOST_SCRATCH_DIR)/ prefix= all install_headers
222endif
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700223
Andrii Nakryikoacbd0622020-03-13 10:23:36 -0700224$(INCLUDE_DIR)/vmlinux.h: $(VMLINUX_BTF) | $(BPFTOOL) $(INCLUDE_DIR)
Andrii Nakryikoca4db632020-06-29 17:47:59 -0700225ifeq ($(VMLINUX_H),)
Andrii Nakryikoacbd0622020-03-13 10:23:36 -0700226 $(call msg,GEN,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700227 $(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
Andrii Nakryikoca4db632020-06-29 17:47:59 -0700228else
229 $(call msg,CP,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700230 $(Q)cp "$(VMLINUX_H)" $@
Andrii Nakryikoca4db632020-06-29 17:47:59 -0700231endif
Andrii Nakryikoacbd0622020-03-13 10:23:36 -0700232
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100233$(RESOLVE_BTFIDS): $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/resolve_btfids \
Jiri Olsacc15a202020-07-11 23:53:29 +0200234 $(TOOLSDIR)/bpf/resolve_btfids/main.c \
235 $(TOOLSDIR)/lib/rbtree.c \
236 $(TOOLSDIR)/lib/zalloc.c \
237 $(TOOLSDIR)/lib/string.c \
238 $(TOOLSDIR)/lib/ctype.c \
239 $(TOOLSDIR)/lib/str_error_r.c
240 $(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/resolve_btfids \
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100241 CC=$(HOSTCC) LD=$(HOSTLD) AR=$(HOSTAR) \
242 OUTPUT=$(HOST_BUILD_DIR)/resolve_btfids/ BPFOBJ=$(HOST_BPFOBJ)
Jiri Olsacc15a202020-07-11 23:53:29 +0200243
Sirio Balmelli167381f2018-05-21 09:00:03 +0200244# Get Clang's default includes on this system, as opposed to those seen by
245# '-target bpf'. This fixes "missing" files on some architectures/distros,
246# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
247#
248# Use '-idirafter': Don't interfere with include mechanics except where the
249# build would have failed anyways.
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200250define get_sys_includes
251$(shell $(1) -v -E - </dev/null 2>&1 \
Björn Töpel6016df82020-11-18 08:16:38 +0100252 | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
253$(shell $(1) -dM -E - </dev/null | grep '#define __riscv_xlen ' | sed 's/#define /-D/' | sed 's/ /=/')
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200254endef
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700255
Ilya Leoshkevich313e7f62019-10-28 11:20:49 +0100256# Determine target endianness.
257IS_LITTLE_ENDIAN = $(shell $(CC) -dM -E - </dev/null | \
258 grep 'define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__')
259MENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian)
260
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200261CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
Ilya Leoshkevich313e7f62019-10-28 11:20:49 +0100262BPF_CFLAGS = -g -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN) \
Tobias Klauserbcd66b12020-03-13 12:31:05 +0100263 -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR) \
264 -I$(abspath $(OUTPUT)/../usr/include)
Sirio Balmelli167381f2018-05-21 09:00:03 +0200265
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200266CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \
267 -Wno-compare-distinct-pointer-types
Alexei Starovoitov3bc35c62017-12-14 17:55:11 -0800268
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200269$(OUTPUT)/test_l4lb_noinline.o: BPF_CFLAGS += -fno-inline
270$(OUTPUT)/test_xdp_noinline.o: BPF_CFLAGS += -fno-inline
Alexei Starovoitov3bc35c62017-12-14 17:55:11 -0800271
Stanislav Fomichevbf0f0fd2019-01-28 08:53:55 -0800272$(OUTPUT)/flow_dissector_load.o: flow_dissector_load.h
Stanislav Fomichevbf0f0fd2019-01-28 08:53:55 -0800273
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700274# Build BPF object using Clang
275# $1 - input .c file
276# $2 - output .o file
277# $3 - CFLAGS
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700278define CLANG_BPF_BUILD_RULE
Andrew Delgadillo89ad7422020-12-11 00:43:44 +0000279 $(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2)
280 $(Q)$(CLANG) $3 -O2 -target bpf -c $1 -o $2 -mcpu=v3
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700281endef
Andrii Nakryiko24f65052019-11-19 16:25:10 -0800282# Similar to CLANG_BPF_BUILD_RULE, but with disabled alu32
283define CLANG_NOALU32_BPF_BUILD_RULE
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800284 $(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2)
Andrew Delgadillo89ad7422020-12-11 00:43:44 +0000285 $(Q)$(CLANG) $3 -O2 -target bpf -c $1 -o $2 -mcpu=v2
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700286endef
287# Build BPF object using GCC
288define GCC_BPF_BUILD_RULE
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800289 $(call msg,GCC-BPF,$(TRUNNER_BINARY),$2)
Andrew Delgadillo89ad7422020-12-11 00:43:44 +0000290 $(Q)$(BPF_GCC) $3 -O2 -c $1 -o $2
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700291endef
Stanislav Fomicheve78e00b2019-03-06 11:59:26 -0800292
Jakub Sitnicki0b9ad562020-04-29 20:11:54 +0200293SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800294
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700295# Set up extra TRUNNER_XXX "temporary" variables in the environment (relies on
296# $eval()) and pass control to DEFINE_TEST_RUNNER_RULES.
297# Parameters:
298# $1 - test runner base binary name (e.g., test_progs)
Yonghong Songe13a2fe2019-10-21 21:31:19 -0700299# $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700300define DEFINE_TEST_RUNNER
Jiong Wang3ef84342019-01-26 12:26:14 -0500301
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700302TRUNNER_OUTPUT := $(OUTPUT)$(if $2,/)$2
303TRUNNER_BINARY := $1$(if $2,-)$2
304TRUNNER_TEST_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.test.o, \
305 $$(notdir $$(wildcard $(TRUNNER_TESTS_DIR)/*.c)))
306TRUNNER_EXTRA_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, \
307 $$(filter %.c,$(TRUNNER_EXTRA_SOURCES)))
308TRUNNER_EXTRA_HDRS := $$(filter %.h,$(TRUNNER_EXTRA_SOURCES))
309TRUNNER_TESTS_HDR := $(TRUNNER_TESTS_DIR)/tests.h
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800310TRUNNER_BPF_SRCS := $$(notdir $$(wildcard $(TRUNNER_BPF_PROGS_DIR)/*.c))
311TRUNNER_BPF_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, $$(TRUNNER_BPF_SRCS))
312TRUNNER_BPF_SKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.skel.h, \
313 $$(filter-out $(SKEL_BLACKLIST), \
314 $$(TRUNNER_BPF_SRCS)))
Yauheni Kaliuta309b81f2020-05-13 05:17:22 +0300315TEST_GEN_FILES += $$(TRUNNER_BPF_OBJS)
Jiong Wang3ef84342019-01-26 12:26:14 -0500316
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700317# Evaluate rules now with extra TRUNNER_XXX variables above already defined
318$$(eval $$(call DEFINE_TEST_RUNNER_RULES,$1,$2))
Stanislav Fomichev3f306582019-03-01 19:42:13 -0800319
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700320endef
Jiong Wang3ef84342019-01-26 12:26:14 -0500321
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700322# Using TRUNNER_XXX variables, provided by callers of DEFINE_TEST_RUNNER and
323# set up by DEFINE_TEST_RUNNER itself, create test runner build rules with:
324# $1 - test runner base binary name (e.g., test_progs)
Yonghong Songe13a2fe2019-10-21 21:31:19 -0700325# $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700326define DEFINE_TEST_RUNNER_RULES
327
328ifeq ($($(TRUNNER_OUTPUT)-dir),)
329$(TRUNNER_OUTPUT)-dir := y
330$(TRUNNER_OUTPUT):
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800331 $$(call msg,MKDIR,,$$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700332 $(Q)mkdir -p $$@
Jiong Wang3ef84342019-01-26 12:26:14 -0500333endif
334
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700335# ensure we set up BPF objects generation rule just once for a given
336# input/output directory combination
337ifeq ($($(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs),)
338$(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs := y
339$(TRUNNER_BPF_OBJS): $(TRUNNER_OUTPUT)/%.o: \
340 $(TRUNNER_BPF_PROGS_DIR)/%.c \
341 $(TRUNNER_BPF_PROGS_DIR)/*.h \
Andrii Nakryikoacbd0622020-03-13 10:23:36 -0700342 $$(INCLUDE_DIR)/vmlinux.h \
Andrii Nakryiko3ac2e202020-08-20 16:12:35 -0700343 $(wildcard $(BPFDIR)/bpf_*.h) | $(TRUNNER_OUTPUT)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700344 $$(call $(TRUNNER_BPF_BUILD_RULE),$$<,$$@, \
Andrew Delgadillo89ad7422020-12-11 00:43:44 +0000345 $(TRUNNER_BPF_CFLAGS))
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800346
347$(TRUNNER_BPF_SKELS): $(TRUNNER_OUTPUT)/%.skel.h: \
348 $(TRUNNER_OUTPUT)/%.o \
349 | $(BPFTOOL) $(TRUNNER_OUTPUT)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800350 $$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700351 $(Q)$$(BPFTOOL) gen skeleton $$< > $$@
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700352endif
353
354# ensure we set up tests.h header generation rule just once
355ifeq ($($(TRUNNER_TESTS_DIR)-tests-hdr),)
356$(TRUNNER_TESTS_DIR)-tests-hdr := y
357$(TRUNNER_TESTS_HDR): $(TRUNNER_TESTS_DIR)/*.c
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800358 $$(call msg,TEST-HDR,$(TRUNNER_BINARY),$$@)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700359 $$(shell ( cd $(TRUNNER_TESTS_DIR); \
360 echo '/* Generated header, do not edit */'; \
361 ls *.c 2> /dev/null | \
362 sed -e 's@\([^\.]*\)\.c@DEFINE_TEST(\1)@'; \
363 ) > $$@)
364endif
365
366# compile individual test files
367# Note: we cd into output directory to ensure embedded BPF object is found
368$(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o: \
369 $(TRUNNER_TESTS_DIR)/%.c \
370 $(TRUNNER_EXTRA_HDRS) \
371 $(TRUNNER_BPF_OBJS) \
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800372 $(TRUNNER_BPF_SKELS) \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700373 $$(BPFOBJ) | $(TRUNNER_OUTPUT)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800374 $$(call msg,TEST-OBJ,$(TRUNNER_BINARY),$$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700375 $(Q)cd $$(@D) && $$(CC) -I. $$(CFLAGS) -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700376
377$(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o: \
378 %.c \
379 $(TRUNNER_EXTRA_HDRS) \
380 $(TRUNNER_TESTS_HDR) \
381 $$(BPFOBJ) | $(TRUNNER_OUTPUT)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800382 $$(call msg,EXT-OBJ,$(TRUNNER_BINARY),$$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700383 $(Q)$$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700384
Ilya Leoshkevich86fd1662021-02-24 12:14:45 +0100385# non-flavored in-srctree builds receive special treatment, in particular, we
386# do not need to copy extra resources (see e.g. test_btf_dump_case())
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700387$(TRUNNER_BINARY)-extras: $(TRUNNER_EXTRA_FILES) | $(TRUNNER_OUTPUT)
Ilya Leoshkevich86fd1662021-02-24 12:14:45 +0100388ifneq ($2:$(OUTPUT),:$(shell pwd))
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800389 $$(call msg,EXT-COPY,$(TRUNNER_BINARY),$(TRUNNER_EXTRA_FILES))
Ilya Leoshkevich86fd1662021-02-24 12:14:45 +0100390 $(Q)rsync -aq $$^ $(TRUNNER_OUTPUT)/
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700391endif
392
393$(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS) \
394 $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ) \
Jiri Olsacc15a202020-07-11 23:53:29 +0200395 $(RESOLVE_BTFIDS) \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700396 | $(TRUNNER_BINARY)-extras
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800397 $$(call msg,BINARY,,$$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700398 $(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
Brendan Jackman58c185b2020-12-03 12:08:50 +0000399 $(Q)$(RESOLVE_BTFIDS) --no-fail --btf $(TRUNNER_OUTPUT)/btf_data.o $$@
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700400
401endef
402
403# Define test_progs test runner.
404TRUNNER_TESTS_DIR := prog_tests
405TRUNNER_BPF_PROGS_DIR := progs
406TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c \
Andrii Nakryikocd492912020-05-12 12:24:42 -0700407 network_helpers.c testing_helpers.c \
Andrii Nakryiko1306c982020-11-04 20:33:56 -0800408 btf_helpers.c flow_dissector_load.h
Andrii Nakryiko9f7fa222020-12-03 12:46:26 -0800409TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko \
KP Singh854055c2020-11-26 18:49:46 +0000410 ima_setup.sh \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700411 $(wildcard progs/btf_dump_test_case_*.c)
412TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE
Andrii Nakryiko6910d7d2020-01-09 21:17:15 -0800413TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS)
Brendan Jackman98d666d2021-01-14 18:17:50 +0000414TRUNNER_BPF_CFLAGS += -DENABLE_ATOMICS_TESTS
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700415$(eval $(call DEFINE_TEST_RUNNER,test_progs))
416
Yonghong Songe13a2fe2019-10-21 21:31:19 -0700417# Define test_progs-no_alu32 test runner.
Andrii Nakryiko24f65052019-11-19 16:25:10 -0800418TRUNNER_BPF_BUILD_RULE := CLANG_NOALU32_BPF_BUILD_RULE
Brendan Jackman98d666d2021-01-14 18:17:50 +0000419TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS)
Yonghong Songe13a2fe2019-10-21 21:31:19 -0700420$(eval $(call DEFINE_TEST_RUNNER,test_progs,no_alu32))
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700421
422# Define test_progs BPF-GCC-flavored test runner.
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200423ifneq ($(BPF_GCC),)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700424TRUNNER_BPF_BUILD_RULE := GCC_BPF_BUILD_RULE
Ilya Leoshkevich313e7f62019-10-28 11:20:49 +0100425TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(call get_sys_includes,gcc)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700426$(eval $(call DEFINE_TEST_RUNNER,test_progs,bpf_gcc))
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200427endif
428
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700429# Define test_maps test runner.
430TRUNNER_TESTS_DIR := map_tests
431TRUNNER_BPF_PROGS_DIR := progs
432TRUNNER_EXTRA_SOURCES := test_maps.c
433TRUNNER_EXTRA_FILES :=
434TRUNNER_BPF_BUILD_RULE := $$(error no BPF objects should be built)
435TRUNNER_BPF_CFLAGS :=
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700436$(eval $(call DEFINE_TEST_RUNNER,test_maps))
Martin KaFai Lauc0fa1b62018-04-18 15:56:06 -0700437
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700438# Define test_verifier test runner.
439# It is much simpler than test_maps/test_progs and sufficiently different from
440# them (e.g., test.h is using completely pattern), that it's worth just
441# explicitly defining all the rules explicitly.
442verifier/tests.h: verifier/*.c
Stanislav Fomicheve78e00b2019-03-06 11:59:26 -0800443 $(shell ( cd verifier/; \
Jakub Kicinski2dfb4012019-01-25 15:24:42 -0800444 echo '/* Generated header, do not edit */'; \
445 echo '#ifdef FILL_ARRAY'; \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700446 ls *.c 2> /dev/null | sed -e 's@\(.*\)@#include \"\1\"@'; \
Jakub Kicinski2dfb4012019-01-25 15:24:42 -0800447 echo '#endif' \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700448 ) > verifier/tests.h)
449$(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800450 $(call msg,BINARY,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700451 $(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
Jakub Kicinski2dfb4012019-01-25 15:24:42 -0800452
Stanislav Fomichev6bf6aff2019-12-02 13:59:31 -0800453# Make sure we are able to include and link libbpf against c++.
Andrii Nakryiko7c8dce42019-12-26 13:02:53 -0800454$(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800455 $(call msg,CXX,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700456 $(Q)$(CXX) $(CFLAGS) $^ $(LDLIBS) -o $@
Stanislav Fomichev6bf6aff2019-12-02 13:59:31 -0800457
Andrii Nakryiko8e7c2a02020-05-12 12:24:43 -0700458# Benchmark runner
459$(OUTPUT)/bench_%.o: benchs/bench_%.c bench.h
460 $(call msg,CC,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700461 $(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
Andrii Nakryiko4eaf0b52020-05-12 12:24:44 -0700462$(OUTPUT)/bench_rename.o: $(OUTPUT)/test_overhead.skel.h
Andrii Nakryikoc5d420c2020-05-12 12:24:45 -0700463$(OUTPUT)/bench_trigger.o: $(OUTPUT)/trigger_bench.skel.h
Andrii Nakryikoc97099b2020-05-29 00:54:23 -0700464$(OUTPUT)/bench_ringbufs.o: $(OUTPUT)/ringbuf_bench.skel.h \
465 $(OUTPUT)/perfbuf_bench.skel.h
Andrii Nakryiko8e7c2a02020-05-12 12:24:43 -0700466$(OUTPUT)/bench.o: bench.h testing_helpers.h
467$(OUTPUT)/bench: LDLIBS += -lm
468$(OUTPUT)/bench: $(OUTPUT)/bench.o $(OUTPUT)/testing_helpers.o \
Andrii Nakryiko4eaf0b52020-05-12 12:24:44 -0700469 $(OUTPUT)/bench_count.o \
Andrii Nakryikoc5d420c2020-05-12 12:24:45 -0700470 $(OUTPUT)/bench_rename.o \
Andrii Nakryikoc97099b2020-05-29 00:54:23 -0700471 $(OUTPUT)/bench_trigger.o \
472 $(OUTPUT)/bench_ringbufs.o
Andrii Nakryiko8e7c2a02020-05-12 12:24:43 -0700473 $(call msg,BINARY,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700474 $(Q)$(CC) $(LDFLAGS) -o $@ $(filter %.a %.o,$^) $(LDLIBS)
Andrii Nakryiko8e7c2a02020-05-12 12:24:43 -0700475
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100476EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(SCRATCH_DIR) $(HOST_SCRATCH_DIR) \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700477 prog_tests/tests.h map_tests/tests.h verifier/tests.h \
Andrii Nakryiko2031af22020-01-09 21:17:14 -0800478 feature \
Andrii Nakryiko9f7fa222020-12-03 12:46:26 -0800479 $(addprefix $(OUTPUT)/,*.o *.skel.h no_alu32 bpf_gcc bpf_testmod.ko)