blob: e19cc647393641ac75231747a5cf0837400d701a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001# SPDX-License-Identifier: GPL-2.0
Masahiro Yamadab61442d2021-04-16 22:00:51 +09002include ../../../build/Build.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 ?=
Andrii Nakryiko41c472e2021-04-23 11:13:43 -070024CFLAGS += -g -O0 -rdynamic -Wall $(GENFLAGS) $(SAN_CFLAGS) \
Andrii Nakryiko02995dd2020-04-28 18:21:02 -070025 -I$(CURDIR) -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR) \
Andrii Nakryikof19ddfe2021-11-03 15:08:45 -070026 -I$(TOOLSINCDIR) -I$(APIDIR) -I$(OUTPUT)
Andrii Nakryiko166750b2019-12-13 17:47:08 -080027LDLIBS += -lcap -lelf -lz -lrt -lpthread
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +020028
Yonghong Songef998582021-04-13 08:34:29 -070029# Silence some warnings when compiled with clang
30ifneq ($(LLVM),)
31CFLAGS += -Wno-unused-command-line-argument
32endif
33
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +010034# Order correspond to 'make run_tests' order
David S. Miller18b3ad92017-05-10 11:43:51 -070035TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \
Alexander Duyckaaf376b2020-11-03 13:34:48 -080036 test_verifier_log test_dev_cgroup \
Florent Revest61f8c9c2021-02-10 12:14:04 +010037 test_sock test_sockmap get_cgroup_id_user \
Jakub Sitnicki415bb4e2019-12-12 11:22:58 +010038 test_cgroup_storage \
Stanislav Fomichev372642e2021-08-04 13:55:24 -070039 test_tcpnotify_user test_sysctl \
Carlos Neira09c02d52021-01-14 11:10:36 -030040 test_progs-no_alu32
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
Jean-Philippe Bruckerd6ac8ca2021-01-13 17:33:18 +010047TEST_GEN_FILES = test_lwt_ip_encap.o \
48 test_tc_edt.o
Jean-Philippe Bruckerb8d1cbe2021-01-13 17:33:20 +010049TEST_FILES = xsk_prereqs.sh \
50 $(wildcard progs/btf_dump_test_case_*.c)
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 \
Hangbin Liud2329242021-05-19 17:07:47 +080055 test_xdp_redirect_multi.sh \
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +010056 test_xdp_meta.sh \
Toshiaki Makita88091ff2019-06-20 11:23:23 +090057 test_xdp_veth.sh \
Andrey Ignatov622adaf2018-03-30 15:08:06 -070058 test_offload.py \
William Tu933a7412018-04-26 14:01:39 -070059 test_sock_addr.sh \
Mathieu Xhonneuxc99a84e2018-05-20 14:58:17 +010060 test_tunnel.sh \
Sean Young6bdd5332018-05-27 12:24:10 +010061 test_lwt_seg6local.sh \
Andrey Ignatov5ecd8c22018-08-12 10:49:30 -070062 test_lirc_mode2.sh \
Petar Penkov50b3ed52018-09-14 07:46:22 -070063 test_skb_cgroup_id.sh \
Jesper Dangaard Brouer97396ff2018-10-09 12:04:53 +020064 test_flow_dissector.sh \
Jesper Dangaard Brouerd35661f2019-08-01 20:00:21 +020065 test_xdp_vlan_mode_generic.sh \
66 test_xdp_vlan_mode_native.sh \
Lorenz Bauerbafc0ba2019-03-22 09:54:06 +080067 test_lwt_ip_encap.sh \
Willem de Bruijn98cdabc2019-03-22 14:32:49 -040068 test_tcp_check_syncookie.sh \
Peter Oskolkov7df5e3d2019-03-22 16:40:19 -070069 test_tc_tunnel.sh \
Alan Maguirecd538502019-05-31 18:47:14 +010070 test_tc_edt.sh \
Quentin Monnet45c5589d2019-08-30 12:00:38 +010071 test_xdping.sh \
Michal Rostecki736332742020-02-26 17:59:39 +010072 test_bpftool_build.sh \
YiFei Zhud42d1cc2020-09-15 16:45:43 -070073 test_bpftool.sh \
74 test_bpftool_metadata.sh \
Jean-Philippe Bruckera0d73ac2021-03-08 19:28:31 +010075 test_doc_build.sh \
Weqaar Janjuaa8905252020-12-07 21:53:29 +000076 test_xsk.sh
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +020077
Anders Roxellf98937c2019-01-08 10:23:03 +010078TEST_PROGS_EXTENDED := with_addr.sh \
Stanislav Fomichev1be72f22019-01-14 09:41:41 -080079 with_tunnels.sh \
Li Zhijian404bd9f2021-08-20 09:55:56 +080080 test_xdp_vlan.sh test_bpftool.py
Anders Roxellef4ab842018-10-10 16:27:04 +020081
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +010082# Compile but not part of 'make run_tests'
Andrii Nakryikocb79a4e2019-10-15 23:00:51 -070083TEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \
Hangbin Liu25a79912019-06-04 10:35:05 +080084 flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \
Weqaar Janjuafacb7cb2020-12-07 21:53:30 +000085 test_lirc_mode2_user xdping test_cpp runqslower bench bpf_testmod.ko \
Hangbin Liud2329242021-05-19 17:07:47 +080086 xdpxceiver xdp_redirect_multi
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +010087
Jean-Philippe Bruckerca1e8462021-01-13 17:33:19 +010088TEST_CUSTOM_PROGS = $(OUTPUT)/urandom_read
Andrii Nakryiko74b5a592019-10-15 23:00:49 -070089
Andrii Nakryiko92f74402019-12-16 22:14:25 -080090# Emit succinct information message describing current building step
91# $1 - generic step name (e.g., CC, LINK, etc);
92# $2 - optional "flavor" specifier; if provided, will be emitted as [flavor];
93# $3 - target (assumed to be file); only file name will be emitted;
94# $4 - optional extra arg, emitted as-is, if provided.
95ifeq ($(V),1)
Andrii Nakryiko965b9fe2020-01-09 21:17:16 -080096Q =
Andrii Nakryiko92f74402019-12-16 22:14:25 -080097msg =
98else
Andrii Nakryiko965b9fe2020-01-09 21:17:16 -080099Q = @
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800100msg = @printf ' %-8s%s %s%s\n' "$(1)" "$(if $(2), [$(2)])" "$(notdir $(3))" "$(if $(4), $(4))";
Andrii Nakryiko965b9fe2020-01-09 21:17:16 -0800101MAKEFLAGS += --no-print-directory
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800102submake_extras := feature_display=0
Andrii Nakryiko92f74402019-12-16 22:14:25 -0800103endif
104
105# override lib.mk's default rules
106OVERRIDE_TARGETS := 1
107override define CLEAN
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800108 $(call msg,CLEAN)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700109 $(Q)$(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
Andrii Nakryiko9f7fa222020-12-03 12:46:26 -0800110 $(Q)$(MAKE) -C bpf_testmod clean
Joe Stringera01d9352021-03-02 09:19:43 -0800111 $(Q)$(MAKE) docs-clean
Andrii Nakryiko92f74402019-12-16 22:14:25 -0800112endef
113
Zi Shen Lime8f1f342017-03-19 23:03:14 -0700114include ../lib.mk
Alexei Starovoitov1da8ac72017-03-10 22:05:55 -0800115
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100116SCRATCH_DIR := $(OUTPUT)/tools
117BUILD_DIR := $(SCRATCH_DIR)/build
118INCLUDE_DIR := $(SCRATCH_DIR)/include
119BPFOBJ := $(BUILD_DIR)/libbpf/libbpf.a
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100120ifneq ($(CROSS_COMPILE),)
121HOST_BUILD_DIR := $(BUILD_DIR)/host
122HOST_SCRATCH_DIR := $(OUTPUT)/host-tools
Quentin Monnet14789942021-10-07 20:44:30 +0100123HOST_INCLUDE_DIR := $(HOST_SCRATCH_DIR)/include
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100124else
125HOST_BUILD_DIR := $(BUILD_DIR)
126HOST_SCRATCH_DIR := $(SCRATCH_DIR)
Quentin Monnet14789942021-10-07 20:44:30 +0100127HOST_INCLUDE_DIR := $(INCLUDE_DIR)
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100128endif
129HOST_BPFOBJ := $(HOST_BUILD_DIR)/libbpf/libbpf.a
130RESOLVE_BTFIDS := $(HOST_BUILD_DIR)/resolve_btfids/resolve_btfids
Quentin Monnetbe795052021-10-07 20:44:31 +0100131RUNQSLOWER_OUTPUT := $(BUILD_DIR)/runqslower/
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100132
Andrii Nakryikoa67079b2020-12-10 17:59:46 -0800133VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \
134 $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \
135 ../../../../vmlinux \
136 /sys/kernel/btf/vmlinux \
137 /boot/vmlinux-$(shell uname -r)
138VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
Kamal Mostafa1a3449c2020-12-15 10:20:10 -0800139ifeq ($(VMLINUX_BTF),)
140$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)")
141endif
Andrii Nakryikoa67079b2020-12-10 17:59:46 -0800142
Andrii Nakryiko03dcb782019-10-15 23:00:48 -0700143# Define simple and short `make test_progs`, `make test_sysctl`, etc targets
144# to build individual tests.
145# NOTE: Semicolon at the end is critical to override lib.mk's default static
146# rule for binaries.
147$(notdir $(TEST_GEN_PROGS) \
148 $(TEST_PROGS) \
149 $(TEST_PROGS_EXTENDED) \
150 $(TEST_GEN_PROGS_EXTENDED) \
151 $(TEST_CUSTOM_PROGS)): %: $(OUTPUT)/% ;
152
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100153# sort removes libbpf duplicates when not cross-building
154MAKE_DIRS := $(sort $(BUILD_DIR)/libbpf $(HOST_BUILD_DIR)/libbpf \
155 $(HOST_BUILD_DIR)/bpftool $(HOST_BUILD_DIR)/resolve_btfids \
Quentin Monnetbe795052021-10-07 20:44:31 +0100156 $(RUNQSLOWER_OUTPUT) $(INCLUDE_DIR))
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100157$(MAKE_DIRS):
158 $(call msg,MKDIR,,$@)
159 $(Q)mkdir -p $@
160
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700161$(OUTPUT)/%.o: %.c
162 $(call msg,CC,,$@)
163 $(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
164
Andrii Nakryiko92f74402019-12-16 22:14:25 -0800165$(OUTPUT)/%:%.c
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800166 $(call msg,BINARY,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700167 $(Q)$(LINK.c) $^ $(LDLIBS) -o $@
Andrii Nakryiko92f74402019-12-16 22:14:25 -0800168
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700169$(OUTPUT)/urandom_read: urandom_read.c
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800170 $(call msg,BINARY,,$@)
Bill Wendlinga9684332020-09-22 16:21:40 -0700171 $(Q)$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS) -Wl,--build-id=sha1
Jiong Wang3ef84342019-01-26 12:26:14 -0500172
Andrii Nakryiko9f7fa222020-12-03 12:46:26 -0800173$(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(wildcard bpf_testmod/Makefile bpf_testmod/*.[ch])
174 $(call msg,MOD,,$@)
Andrii Nakryikoa67079b2020-12-10 17:59:46 -0800175 $(Q)$(RM) bpf_testmod/bpf_testmod.ko # force re-compilation
Andrii Nakryiko9f7fa222020-12-03 12:46:26 -0800176 $(Q)$(MAKE) $(submake_extras) -C bpf_testmod
177 $(Q)cp bpf_testmod/bpf_testmod.ko $@
178
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100179DEFAULT_BPFTOOL := $(HOST_SCRATCH_DIR)/sbin/bpftool
Andrii Nakryiko6bcaf412020-08-04 17:47:57 -0700180
Quentin Monnetbe795052021-10-07 20:44:31 +0100181$(OUTPUT)/runqslower: $(BPFOBJ) | $(DEFAULT_BPFTOOL) $(RUNQSLOWER_OUTPUT)
182 $(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/runqslower \
183 OUTPUT=$(RUNQSLOWER_OUTPUT) VMLINUX_BTF=$(VMLINUX_BTF) \
184 BPFTOOL_OUTPUT=$(BUILD_DIR)/bpftool/ \
185 BPFOBJ_OUTPUT=$(BUILD_DIR)/libbpf \
186 BPFOBJ=$(BPFOBJ) BPF_INCLUDE=$(INCLUDE_DIR) && \
187 cp $(RUNQSLOWER_OUTPUT)runqslower $@
Zi Shen Lime8f1f342017-03-19 23:03:14 -0700188
Li Zhijian404bd9f2021-08-20 09:55:56 +0800189TEST_GEN_PROGS_EXTENDED += $(DEFAULT_BPFTOOL)
190
Andrii Nakryikof87c1932021-11-03 15:08:43 -0700191$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(BPFOBJ)
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +0100192
Andrii Nakryikof87c1932021-11-03 15:08:43 -0700193$(OUTPUT)/test_dev_cgroup: cgroup_helpers.c testing_helpers.o
194$(OUTPUT)/test_skb_cgroup_id_user: cgroup_helpers.c testing_helpers.o
195$(OUTPUT)/test_sock: cgroup_helpers.c testing_helpers.o
196$(OUTPUT)/test_sock_addr: cgroup_helpers.c testing_helpers.o
197$(OUTPUT)/test_sockmap: cgroup_helpers.c testing_helpers.o
198$(OUTPUT)/test_tcpnotify_user: cgroup_helpers.c trace_helpers.c testing_helpers.o
199$(OUTPUT)/get_cgroup_id_user: cgroup_helpers.c testing_helpers.o
200$(OUTPUT)/test_cgroup_storage: cgroup_helpers.c testing_helpers.o
201$(OUTPUT)/test_sock_fields: cgroup_helpers.c testing_helpers.o
202$(OUTPUT)/test_sysctl: cgroup_helpers.c testing_helpers.o
203$(OUTPUT)/test_tag: testing_helpers.o
204$(OUTPUT)/test_lirc_mode2_user: testing_helpers.o
205$(OUTPUT)/xdping: testing_helpers.o
206$(OUTPUT)/flow_dissector_load: testing_helpers.o
207$(OUTPUT)/test_maps: testing_helpers.o
Andrii Nakryikocbdb1462021-11-03 15:08:44 -0700208$(OUTPUT)/test_verifier: testing_helpers.o
Jesper Dangaard Brouer41757dc2018-02-13 14:19:15 +0100209
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800210BPFTOOL ?= $(DEFAULT_BPFTOOL)
Andrii Nakryiko03506292020-01-23 21:41:48 -0800211$(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) \
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100212 $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/bpftool
Andrii Nakryiko03506292020-01-23 21:41:48 -0800213 $(Q)$(MAKE) $(submake_extras) -C $(BPFTOOLDIR) \
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100214 CC=$(HOSTCC) LD=$(HOSTLD) \
Andrii Nakryiko41c472e2021-04-23 11:13:43 -0700215 EXTRA_CFLAGS='-g -O0' \
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100216 OUTPUT=$(HOST_BUILD_DIR)/bpftool/ \
Quentin Monnetf012ade12021-10-07 20:44:29 +0100217 LIBBPF_OUTPUT=$(HOST_BUILD_DIR)/libbpf/ \
218 LIBBPF_DESTDIR=$(HOST_SCRATCH_DIR)/ \
Quentin Monnetd7db0a42021-10-07 20:44:38 +0100219 prefix= DESTDIR=$(HOST_SCRATCH_DIR)/ install-bin
Joe Stringera01d9352021-03-02 09:19:43 -0800220
Jiri Olsad044d9f2021-04-20 15:24:28 +0200221all: docs
222
Joe Stringera01d9352021-03-02 09:19:43 -0800223docs:
224 $(Q)RST2MAN_OPTS="--exit-status=1" $(MAKE) $(submake_extras) \
225 -f Makefile.docs \
226 prefix= OUTPUT=$(OUTPUT)/ DESTDIR=$(OUTPUT)/ $@
227
228docs-clean:
229 $(Q)$(MAKE) $(submake_extras) \
230 -f Makefile.docs \
231 prefix= OUTPUT=$(OUTPUT)/ DESTDIR=$(OUTPUT)/ $@
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800232
Andrii Nakryiko03506292020-01-23 21:41:48 -0800233$(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile) \
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100234 ../../../include/uapi/linux/bpf.h \
Quentin Monnet14789942021-10-07 20:44:30 +0100235 | $(BUILD_DIR)/libbpf
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100236 $(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(BUILD_DIR)/libbpf/ \
Andrii Nakryiko41c472e2021-04-23 11:13:43 -0700237 EXTRA_CFLAGS='-g -O0' \
Andrii Nakryiko03506292020-01-23 21:41:48 -0800238 DESTDIR=$(SCRATCH_DIR) prefix= all install_headers
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800239
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100240ifneq ($(BPFOBJ),$(HOST_BPFOBJ))
241$(HOST_BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile) \
242 ../../../include/uapi/linux/bpf.h \
Quentin Monnet14789942021-10-07 20:44:30 +0100243 | $(HOST_BUILD_DIR)/libbpf
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100244 $(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) \
Andrii Nakryiko41c472e2021-04-23 11:13:43 -0700245 EXTRA_CFLAGS='-g -O0' \
Andrii Nakryiko252e3cb2021-03-13 13:09:20 -0800246 OUTPUT=$(HOST_BUILD_DIR)/libbpf/ CC=$(HOSTCC) LD=$(HOSTLD) \
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100247 DESTDIR=$(HOST_SCRATCH_DIR)/ prefix= all install_headers
248endif
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700249
Andrii Nakryikocab62c32021-03-18 12:40:34 -0700250$(INCLUDE_DIR)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL) | $(INCLUDE_DIR)
Andrii Nakryikoca4db632020-06-29 17:47:59 -0700251ifeq ($(VMLINUX_H),)
Andrii Nakryikoacbd0622020-03-13 10:23:36 -0700252 $(call msg,GEN,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700253 $(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
Andrii Nakryikoca4db632020-06-29 17:47:59 -0700254else
255 $(call msg,CP,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700256 $(Q)cp "$(VMLINUX_H)" $@
Andrii Nakryikoca4db632020-06-29 17:47:59 -0700257endif
Andrii Nakryikoacbd0622020-03-13 10:23:36 -0700258
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100259$(RESOLVE_BTFIDS): $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/resolve_btfids \
Jiri Olsacc15a202020-07-11 23:53:29 +0200260 $(TOOLSDIR)/bpf/resolve_btfids/main.c \
261 $(TOOLSDIR)/lib/rbtree.c \
262 $(TOOLSDIR)/lib/zalloc.c \
263 $(TOOLSDIR)/lib/string.c \
264 $(TOOLSDIR)/lib/ctype.c \
265 $(TOOLSDIR)/lib/str_error_r.c
266 $(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/resolve_btfids \
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100267 CC=$(HOSTCC) LD=$(HOSTLD) AR=$(HOSTAR) \
Quentin Monnet14789942021-10-07 20:44:30 +0100268 LIBBPF_INCLUDE=$(HOST_INCLUDE_DIR) \
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100269 OUTPUT=$(HOST_BUILD_DIR)/resolve_btfids/ BPFOBJ=$(HOST_BPFOBJ)
Jiri Olsacc15a202020-07-11 23:53:29 +0200270
Sirio Balmelli167381f2018-05-21 09:00:03 +0200271# Get Clang's default includes on this system, as opposed to those seen by
272# '-target bpf'. This fixes "missing" files on some architectures/distros,
273# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
274#
275# Use '-idirafter': Don't interfere with include mechanics except where the
276# build would have failed anyways.
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200277define get_sys_includes
278$(shell $(1) -v -E - </dev/null 2>&1 \
Björn Töpel6016df82020-11-18 08:16:38 +0100279 | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
Björn Töpel36e70b92021-10-28 18:10:57 +0200280$(shell $(1) -dM -E - </dev/null | grep '__riscv_xlen ' | awk '{printf("-D__riscv_xlen=%d -D__BITS_PER_LONG=%d", $$3, $$3)}')
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200281endef
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700282
Ilya Leoshkevich313e7f62019-10-28 11:20:49 +0100283# Determine target endianness.
284IS_LITTLE_ENDIAN = $(shell $(CC) -dM -E - </dev/null | \
285 grep 'define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__')
286MENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian)
287
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200288CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
Ilya Leoshkevich313e7f62019-10-28 11:20:49 +0100289BPF_CFLAGS = -g -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN) \
Tobias Klauserbcd66b12020-03-13 12:31:05 +0100290 -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR) \
291 -I$(abspath $(OUTPUT)/../usr/include)
Sirio Balmelli167381f2018-05-21 09:00:03 +0200292
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200293CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \
294 -Wno-compare-distinct-pointer-types
Alexei Starovoitov3bc35c62017-12-14 17:55:11 -0800295
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200296$(OUTPUT)/test_l4lb_noinline.o: BPF_CFLAGS += -fno-inline
297$(OUTPUT)/test_xdp_noinline.o: BPF_CFLAGS += -fno-inline
Alexei Starovoitov3bc35c62017-12-14 17:55:11 -0800298
Stanislav Fomichevbf0f0fd2019-01-28 08:53:55 -0800299$(OUTPUT)/flow_dissector_load.o: flow_dissector_load.h
Stanislav Fomichevbf0f0fd2019-01-28 08:53:55 -0800300
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700301# Build BPF object using Clang
302# $1 - input .c file
303# $2 - output .o file
304# $3 - CFLAGS
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700305define CLANG_BPF_BUILD_RULE
Andrew Delgadillo89ad7422020-12-11 00:43:44 +0000306 $(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2)
307 $(Q)$(CLANG) $3 -O2 -target bpf -c $1 -o $2 -mcpu=v3
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700308endef
Andrii Nakryiko24f65052019-11-19 16:25:10 -0800309# Similar to CLANG_BPF_BUILD_RULE, but with disabled alu32
310define CLANG_NOALU32_BPF_BUILD_RULE
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800311 $(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2)
Andrew Delgadillo89ad7422020-12-11 00:43:44 +0000312 $(Q)$(CLANG) $3 -O2 -target bpf -c $1 -o $2 -mcpu=v2
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700313endef
314# Build BPF object using GCC
315define GCC_BPF_BUILD_RULE
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800316 $(call msg,GCC-BPF,$(TRUNNER_BINARY),$2)
Andrew Delgadillo89ad7422020-12-11 00:43:44 +0000317 $(Q)$(BPF_GCC) $3 -O2 -c $1 -o $2
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700318endef
Stanislav Fomicheve78e00b2019-03-06 11:59:26 -0800319
Jakub Sitnicki0b9ad562020-04-29 20:11:54 +0200320SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800321
Andrii Nakryiko3b2ad502021-04-23 11:13:47 -0700322LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \
323 linked_vars.skel.h linked_maps.skel.h
Andrii Nakryikoa0964f52021-03-18 12:40:36 -0700324
Alexei Starovoitov4d1b6292021-05-13 17:36:20 -0700325LSKELS := kfunc_call_test.c fentry_test.c fexit_test.c fexit_sleep.c \
Kumar Kartikeya Dwivedic48e51c2021-10-02 06:47:57 +0530326 test_ringbuf.c atomics.c trace_printk.c trace_vprintk.c
327# Generate both light skeleton and libbpf skeleton for these
Kumar Kartikeya Dwivedi087cba72021-10-28 12:04:59 +0530328LSKELS_EXTRA := test_ksyms_module.c test_ksyms_weak.c
Alexei Starovoitov4d1b6292021-05-13 17:36:20 -0700329SKEL_BLACKLIST += $$(LSKELS)
330
Andrii Nakryikoa0964f52021-03-18 12:40:36 -0700331test_static_linked.skel.h-deps := test_static_linked1.o test_static_linked2.o
Andrii Nakryikof2644fb2021-04-23 11:13:45 -0700332linked_funcs.skel.h-deps := linked_funcs1.o linked_funcs2.o
Andrii Nakryiko14f1aae2021-04-23 11:13:46 -0700333linked_vars.skel.h-deps := linked_vars1.o linked_vars2.o
Andrii Nakryiko3b2ad502021-04-23 11:13:47 -0700334linked_maps.skel.h-deps := linked_maps1.o linked_maps2.o
Andrii Nakryikoa0964f52021-03-18 12:40:36 -0700335
Andrii Nakryikob131aed2021-04-23 11:13:44 -0700336LINKED_BPF_SRCS := $(patsubst %.o,%.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
337
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700338# Set up extra TRUNNER_XXX "temporary" variables in the environment (relies on
339# $eval()) and pass control to DEFINE_TEST_RUNNER_RULES.
340# Parameters:
341# $1 - test runner base binary name (e.g., test_progs)
Yonghong Songe13a2fe2019-10-21 21:31:19 -0700342# $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700343define DEFINE_TEST_RUNNER
Jiong Wang3ef84342019-01-26 12:26:14 -0500344
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700345TRUNNER_OUTPUT := $(OUTPUT)$(if $2,/)$2
346TRUNNER_BINARY := $1$(if $2,-)$2
347TRUNNER_TEST_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.test.o, \
348 $$(notdir $$(wildcard $(TRUNNER_TESTS_DIR)/*.c)))
349TRUNNER_EXTRA_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, \
350 $$(filter %.c,$(TRUNNER_EXTRA_SOURCES)))
351TRUNNER_EXTRA_HDRS := $$(filter %.h,$(TRUNNER_EXTRA_SOURCES))
352TRUNNER_TESTS_HDR := $(TRUNNER_TESTS_DIR)/tests.h
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800353TRUNNER_BPF_SRCS := $$(notdir $$(wildcard $(TRUNNER_BPF_PROGS_DIR)/*.c))
354TRUNNER_BPF_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, $$(TRUNNER_BPF_SRCS))
355TRUNNER_BPF_SKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.skel.h, \
Andrii Nakryikob131aed2021-04-23 11:13:44 -0700356 $$(filter-out $(SKEL_BLACKLIST) $(LINKED_BPF_SRCS),\
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800357 $$(TRUNNER_BPF_SRCS)))
Kumar Kartikeya Dwivedic48e51c2021-10-02 06:47:57 +0530358TRUNNER_BPF_LSKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.lskel.h, $$(LSKELS) $$(LSKELS_EXTRA))
Andrii Nakryikoa0964f52021-03-18 12:40:36 -0700359TRUNNER_BPF_SKELS_LINKED := $$(addprefix $$(TRUNNER_OUTPUT)/,$(LINKED_SKELS))
Yauheni Kaliuta309b81f2020-05-13 05:17:22 +0300360TEST_GEN_FILES += $$(TRUNNER_BPF_OBJS)
Jiong Wang3ef84342019-01-26 12:26:14 -0500361
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700362# Evaluate rules now with extra TRUNNER_XXX variables above already defined
363$$(eval $$(call DEFINE_TEST_RUNNER_RULES,$1,$2))
Stanislav Fomichev3f306582019-03-01 19:42:13 -0800364
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700365endef
Jiong Wang3ef84342019-01-26 12:26:14 -0500366
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700367# Using TRUNNER_XXX variables, provided by callers of DEFINE_TEST_RUNNER and
368# set up by DEFINE_TEST_RUNNER itself, create test runner build rules with:
369# $1 - test runner base binary name (e.g., test_progs)
Yonghong Songe13a2fe2019-10-21 21:31:19 -0700370# $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700371define DEFINE_TEST_RUNNER_RULES
372
373ifeq ($($(TRUNNER_OUTPUT)-dir),)
374$(TRUNNER_OUTPUT)-dir := y
375$(TRUNNER_OUTPUT):
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800376 $$(call msg,MKDIR,,$$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700377 $(Q)mkdir -p $$@
Jiong Wang3ef84342019-01-26 12:26:14 -0500378endif
379
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700380# ensure we set up BPF objects generation rule just once for a given
381# input/output directory combination
382ifeq ($($(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs),)
383$(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs := y
384$(TRUNNER_BPF_OBJS): $(TRUNNER_OUTPUT)/%.o: \
385 $(TRUNNER_BPF_PROGS_DIR)/%.c \
386 $(TRUNNER_BPF_PROGS_DIR)/*.h \
Andrii Nakryikoacbd0622020-03-13 10:23:36 -0700387 $$(INCLUDE_DIR)/vmlinux.h \
Jiri Bencd888eaa2021-09-27 18:01:36 +0200388 $(wildcard $(BPFDIR)/bpf_*.h) \
389 | $(TRUNNER_OUTPUT) $$(BPFOBJ)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700390 $$(call $(TRUNNER_BPF_BUILD_RULE),$$<,$$@, \
Andrew Delgadillo89ad7422020-12-11 00:43:44 +0000391 $(TRUNNER_BPF_CFLAGS))
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800392
Andrii Nakryiko14137f32021-03-18 12:40:35 -0700393$(TRUNNER_BPF_SKELS): %.skel.h: %.o $(BPFTOOL) | $(TRUNNER_OUTPUT)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800394 $$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
Andrii Nakryikoa0964f52021-03-18 12:40:36 -0700395 $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked1.o) $$<
396 $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked2.o) $$(<:.o=.linked1.o)
Andrii Nakryiko14137f32021-03-18 12:40:35 -0700397 $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked3.o) $$(<:.o=.linked2.o)
398 $(Q)diff $$(<:.o=.linked2.o) $$(<:.o=.linked3.o)
Andrii Nakryikoa0964f52021-03-18 12:40:36 -0700399 $(Q)$$(BPFTOOL) gen skeleton $$(<:.o=.linked3.o) name $$(notdir $$(<:.o=)) > $$@
400
Alexei Starovoitov4d1b6292021-05-13 17:36:20 -0700401$(TRUNNER_BPF_LSKELS): %.lskel.h: %.o $(BPFTOOL) | $(TRUNNER_OUTPUT)
402 $$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
403 $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked1.o) $$<
404 $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked2.o) $$(<:.o=.linked1.o)
405 $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked3.o) $$(<:.o=.linked2.o)
406 $(Q)diff $$(<:.o=.linked2.o) $$(<:.o=.linked3.o)
Kumar Kartikeya Dwivedi087cba72021-10-28 12:04:59 +0530407 $(Q)$$(BPFTOOL) gen skeleton -L $$(<:.o=.linked3.o) name $$(notdir $$(<:.o=_lskel)) > $$@
Alexei Starovoitov4d1b6292021-05-13 17:36:20 -0700408
Andrii Nakryikoa0964f52021-03-18 12:40:36 -0700409$(TRUNNER_BPF_SKELS_LINKED): $(TRUNNER_BPF_OBJS) $(BPFTOOL) | $(TRUNNER_OUTPUT)
410 $$(call msg,LINK-BPF,$(TRUNNER_BINARY),$$(@:.skel.h=.o))
411 $(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked1.o) $$(addprefix $(TRUNNER_OUTPUT)/,$$($$(@F)-deps))
412 $(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked1.o)
413 $(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked3.o) $$(@:.skel.h=.linked2.o)
414 $(Q)diff $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked3.o)
415 $$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
416 $(Q)$$(BPFTOOL) gen skeleton $$(@:.skel.h=.linked3.o) name $$(notdir $$(@:.skel.h=)) > $$@
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700417endif
418
419# ensure we set up tests.h header generation rule just once
420ifeq ($($(TRUNNER_TESTS_DIR)-tests-hdr),)
421$(TRUNNER_TESTS_DIR)-tests-hdr := y
422$(TRUNNER_TESTS_HDR): $(TRUNNER_TESTS_DIR)/*.c
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800423 $$(call msg,TEST-HDR,$(TRUNNER_BINARY),$$@)
Andrii Nakryiko8ea688e2021-10-22 15:32:26 -0700424 $$(shell (echo '/* Generated header, do not edit */'; \
425 sed -n -E 's/^void (serial_)?test_([a-zA-Z0-9_]+)\((void)?\).*/DEFINE_TEST(\2)/p' \
426 $(TRUNNER_TESTS_DIR)/*.c | sort ; \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700427 ) > $$@)
428endif
429
430# compile individual test files
431# Note: we cd into output directory to ensure embedded BPF object is found
432$(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o: \
433 $(TRUNNER_TESTS_DIR)/%.c \
434 $(TRUNNER_EXTRA_HDRS) \
435 $(TRUNNER_BPF_OBJS) \
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800436 $(TRUNNER_BPF_SKELS) \
Alexei Starovoitov4d1b6292021-05-13 17:36:20 -0700437 $(TRUNNER_BPF_LSKELS) \
Andrii Nakryikoa0964f52021-03-18 12:40:36 -0700438 $(TRUNNER_BPF_SKELS_LINKED) \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700439 $$(BPFOBJ) | $(TRUNNER_OUTPUT)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800440 $$(call msg,TEST-OBJ,$(TRUNNER_BINARY),$$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700441 $(Q)cd $$(@D) && $$(CC) -I. $$(CFLAGS) -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700442
443$(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o: \
444 %.c \
445 $(TRUNNER_EXTRA_HDRS) \
446 $(TRUNNER_TESTS_HDR) \
447 $$(BPFOBJ) | $(TRUNNER_OUTPUT)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800448 $$(call msg,EXT-OBJ,$(TRUNNER_BINARY),$$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700449 $(Q)$$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700450
Ilya Leoshkevich86fd1662021-02-24 12:14:45 +0100451# non-flavored in-srctree builds receive special treatment, in particular, we
452# do not need to copy extra resources (see e.g. test_btf_dump_case())
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700453$(TRUNNER_BINARY)-extras: $(TRUNNER_EXTRA_FILES) | $(TRUNNER_OUTPUT)
Ilya Leoshkevich86fd1662021-02-24 12:14:45 +0100454ifneq ($2:$(OUTPUT),:$(shell pwd))
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800455 $$(call msg,EXT-COPY,$(TRUNNER_BINARY),$(TRUNNER_EXTRA_FILES))
Ilya Leoshkevich86fd1662021-02-24 12:14:45 +0100456 $(Q)rsync -aq $$^ $(TRUNNER_OUTPUT)/
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700457endif
458
459$(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS) \
460 $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ) \
Jiri Olsacc15a202020-07-11 23:53:29 +0200461 $(RESOLVE_BTFIDS) \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700462 | $(TRUNNER_BINARY)-extras
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800463 $$(call msg,BINARY,,$$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700464 $(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
Kumar Kartikeya Dwivedif614f2c2021-10-02 06:47:52 +0530465 $(Q)$(RESOLVE_BTFIDS) --btf $(TRUNNER_OUTPUT)/btf_data.o $$@
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700466
467endef
468
469# Define test_progs test runner.
470TRUNNER_TESTS_DIR := prog_tests
471TRUNNER_BPF_PROGS_DIR := progs
472TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c \
Andrii Nakryikocd492912020-05-12 12:24:42 -0700473 network_helpers.c testing_helpers.c \
Andrii Nakryiko1306c982020-11-04 20:33:56 -0800474 btf_helpers.c flow_dissector_load.h
Andrii Nakryiko9f7fa222020-12-03 12:46:26 -0800475TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko \
KP Singh854055c2020-11-26 18:49:46 +0000476 ima_setup.sh \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700477 $(wildcard progs/btf_dump_test_case_*.c)
478TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE
Andrii Nakryiko6910d7d2020-01-09 21:17:15 -0800479TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS)
Brendan Jackman98d666d2021-01-14 18:17:50 +0000480TRUNNER_BPF_CFLAGS += -DENABLE_ATOMICS_TESTS
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700481$(eval $(call DEFINE_TEST_RUNNER,test_progs))
482
Yonghong Songe13a2fe2019-10-21 21:31:19 -0700483# Define test_progs-no_alu32 test runner.
Andrii Nakryiko24f65052019-11-19 16:25:10 -0800484TRUNNER_BPF_BUILD_RULE := CLANG_NOALU32_BPF_BUILD_RULE
Brendan Jackman98d666d2021-01-14 18:17:50 +0000485TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS)
Yonghong Songe13a2fe2019-10-21 21:31:19 -0700486$(eval $(call DEFINE_TEST_RUNNER,test_progs,no_alu32))
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700487
488# Define test_progs BPF-GCC-flavored test runner.
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200489ifneq ($(BPF_GCC),)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700490TRUNNER_BPF_BUILD_RULE := GCC_BPF_BUILD_RULE
Ilya Leoshkevich313e7f62019-10-28 11:20:49 +0100491TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(call get_sys_includes,gcc)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700492$(eval $(call DEFINE_TEST_RUNNER,test_progs,bpf_gcc))
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200493endif
494
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700495# Define test_maps test runner.
496TRUNNER_TESTS_DIR := map_tests
497TRUNNER_BPF_PROGS_DIR := progs
498TRUNNER_EXTRA_SOURCES := test_maps.c
499TRUNNER_EXTRA_FILES :=
500TRUNNER_BPF_BUILD_RULE := $$(error no BPF objects should be built)
501TRUNNER_BPF_CFLAGS :=
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700502$(eval $(call DEFINE_TEST_RUNNER,test_maps))
Martin KaFai Lauc0fa1b62018-04-18 15:56:06 -0700503
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700504# Define test_verifier test runner.
505# It is much simpler than test_maps/test_progs and sufficiently different from
506# them (e.g., test.h is using completely pattern), that it's worth just
507# explicitly defining all the rules explicitly.
508verifier/tests.h: verifier/*.c
Stanislav Fomicheve78e00b2019-03-06 11:59:26 -0800509 $(shell ( cd verifier/; \
Jakub Kicinski2dfb4012019-01-25 15:24:42 -0800510 echo '/* Generated header, do not edit */'; \
511 echo '#ifdef FILL_ARRAY'; \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700512 ls *.c 2> /dev/null | sed -e 's@\(.*\)@#include \"\1\"@'; \
Jakub Kicinski2dfb4012019-01-25 15:24:42 -0800513 echo '#endif' \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700514 ) > verifier/tests.h)
515$(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800516 $(call msg,BINARY,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700517 $(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
Jakub Kicinski2dfb4012019-01-25 15:24:42 -0800518
Stanislav Fomichev6bf6aff2019-12-02 13:59:31 -0800519# Make sure we are able to include and link libbpf against c++.
Andrii Nakryiko7c8dce42019-12-26 13:02:53 -0800520$(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800521 $(call msg,CXX,,$@)
Yonghong Songa22c0c82021-04-13 08:34:24 -0700522 $(Q)$(CXX) $(CFLAGS) $(filter %.a %.o %.cpp,$^) $(LDLIBS) -o $@
Stanislav Fomichev6bf6aff2019-12-02 13:59:31 -0800523
Andrii Nakryiko8e7c2a02020-05-12 12:24:43 -0700524# Benchmark runner
Quentin Monnet0b46b752021-09-08 14:32:26 -0700525$(OUTPUT)/bench_%.o: benchs/bench_%.c bench.h $(BPFOBJ)
Andrii Nakryiko8e7c2a02020-05-12 12:24:43 -0700526 $(call msg,CC,,$@)
Joanne Koong57fd1c62021-10-27 16:45:03 -0700527 $(Q)$(CC) $(CFLAGS) -O2 -c $(filter %.c,$^) $(LDLIBS) -o $@
Andrii Nakryiko4eaf0b52020-05-12 12:24:44 -0700528$(OUTPUT)/bench_rename.o: $(OUTPUT)/test_overhead.skel.h
Andrii Nakryikoc5d420c2020-05-12 12:24:45 -0700529$(OUTPUT)/bench_trigger.o: $(OUTPUT)/trigger_bench.skel.h
Andrii Nakryikoc97099b2020-05-29 00:54:23 -0700530$(OUTPUT)/bench_ringbufs.o: $(OUTPUT)/ringbuf_bench.skel.h \
531 $(OUTPUT)/perfbuf_bench.skel.h
Joanne Koong57fd1c62021-10-27 16:45:03 -0700532$(OUTPUT)/bench_bloom_filter_map.o: $(OUTPUT)/bloom_filter_bench.skel.h
Quentin Monnet0b46b752021-09-08 14:32:26 -0700533$(OUTPUT)/bench.o: bench.h testing_helpers.h $(BPFOBJ)
Andrii Nakryiko8e7c2a02020-05-12 12:24:43 -0700534$(OUTPUT)/bench: LDLIBS += -lm
535$(OUTPUT)/bench: $(OUTPUT)/bench.o $(OUTPUT)/testing_helpers.o \
Andrii Nakryiko4eaf0b52020-05-12 12:24:44 -0700536 $(OUTPUT)/bench_count.o \
Andrii Nakryikoc5d420c2020-05-12 12:24:45 -0700537 $(OUTPUT)/bench_rename.o \
Andrii Nakryikoc97099b2020-05-29 00:54:23 -0700538 $(OUTPUT)/bench_trigger.o \
Joanne Koong57fd1c62021-10-27 16:45:03 -0700539 $(OUTPUT)/bench_ringbufs.o \
540 $(OUTPUT)/bench_bloom_filter_map.o
Andrii Nakryiko8e7c2a02020-05-12 12:24:43 -0700541 $(call msg,BINARY,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700542 $(Q)$(CC) $(LDFLAGS) -o $@ $(filter %.a %.o,$^) $(LDLIBS)
Andrii Nakryiko8e7c2a02020-05-12 12:24:43 -0700543
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100544EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(SCRATCH_DIR) $(HOST_SCRATCH_DIR) \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700545 prog_tests/tests.h map_tests/tests.h verifier/tests.h \
Andrii Nakryiko2031af22020-01-09 21:17:14 -0800546 feature \
Alexei Starovoitov4d1b6292021-05-13 17:36:20 -0700547 $(addprefix $(OUTPUT)/,*.o *.skel.h *.lskel.h no_alu32 bpf_gcc bpf_testmod.ko)
Joe Stringera01d9352021-03-02 09:19:43 -0800548
549.PHONY: docs docs-clean