blob: 42ffc24e9e71050bd64112f9a1ed9673eb8aa70d [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 Nakryiko2a2cb452021-11-07 08:55:13 -080027LDFLAGS += $(SAN_CFLAGS)
Andrii Nakryiko166750b2019-12-13 17:47:08 -080028LDLIBS += -lcap -lelf -lz -lrt -lpthread
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +020029
Yonghong Songef998582021-04-13 08:34:29 -070030# Silence some warnings when compiled with clang
31ifneq ($(LLVM),)
32CFLAGS += -Wno-unused-command-line-argument
33endif
34
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +010035# Order correspond to 'make run_tests' order
David S. Miller18b3ad92017-05-10 11:43:51 -070036TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \
Alexander Duyckaaf376b2020-11-03 13:34:48 -080037 test_verifier_log test_dev_cgroup \
Florent Revest61f8c9c2021-02-10 12:14:04 +010038 test_sock test_sockmap get_cgroup_id_user \
Jakub Sitnicki415bb4e2019-12-12 11:22:58 +010039 test_cgroup_storage \
Stanislav Fomichev372642e2021-08-04 13:55:24 -070040 test_tcpnotify_user test_sysctl \
Carlos Neira09c02d52021-01-14 11:10:36 -030041 test_progs-no_alu32
Jiong Wang3ef84342019-01-26 12:26:14 -050042
Andrii Nakryiko74b5a592019-10-15 23:00:49 -070043# Also test bpf-gcc, if present
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +020044ifneq ($(BPF_GCC),)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -070045TEST_GEN_PROGS += test_progs-bpf_gcc
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +020046endif
47
Andrii Nakryikode29e6b2021-11-10 21:36:17 -080048TEST_GEN_FILES = test_lwt_ip_encap.o test_tc_edt.o
49TEST_FILES = xsk_prereqs.sh $(wildcard progs/btf_dump_test_case_*.c)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -070050
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +010051# Order correspond to 'make run_tests' order
52TEST_PROGS := test_kmod.sh \
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +010053 test_xdp_redirect.sh \
Hangbin Liud2329242021-05-19 17:07:47 +080054 test_xdp_redirect_multi.sh \
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +010055 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 \
Jean-Philippe Bruckera0d73ac2021-03-08 19:28:31 +010074 test_doc_build.sh \
Weqaar Janjuaa8905252020-12-07 21:53:29 +000075 test_xsk.sh
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +020076
Anders Roxellf98937c2019-01-08 10:23:03 +010077TEST_PROGS_EXTENDED := with_addr.sh \
Stanislav Fomichev1be72f22019-01-14 09:41:41 -080078 with_tunnels.sh \
Li Zhijian404bd9f2021-08-20 09:55:56 +080079 test_xdp_vlan.sh test_bpftool.py
Anders Roxellef4ab842018-10-10 16:27:04 +020080
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +010081# Compile but not part of 'make run_tests'
Andrii Nakryikocb79a4e2019-10-15 23:00:51 -070082TEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \
Hangbin Liu25a79912019-06-04 10:35:05 +080083 flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \
Weqaar Janjuafacb7cb2020-12-07 21:53:30 +000084 test_lirc_mode2_user xdping test_cpp runqslower bench bpf_testmod.ko \
Hangbin Liud2329242021-05-19 17:07:47 +080085 xdpxceiver xdp_redirect_multi
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +010086
Jean-Philippe Bruckerca1e8462021-01-13 17:33:19 +010087TEST_CUSTOM_PROGS = $(OUTPUT)/urandom_read
Andrii Nakryiko74b5a592019-10-15 23:00:49 -070088
Andrii Nakryiko92f74402019-12-16 22:14:25 -080089# Emit succinct information message describing current building step
90# $1 - generic step name (e.g., CC, LINK, etc);
91# $2 - optional "flavor" specifier; if provided, will be emitted as [flavor];
92# $3 - target (assumed to be file); only file name will be emitted;
93# $4 - optional extra arg, emitted as-is, if provided.
94ifeq ($(V),1)
Andrii Nakryiko965b9fe2020-01-09 21:17:16 -080095Q =
Andrii Nakryiko92f74402019-12-16 22:14:25 -080096msg =
97else
Andrii Nakryiko965b9fe2020-01-09 21:17:16 -080098Q = @
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -080099msg = @printf ' %-8s%s %s%s\n' "$(1)" "$(if $(2), [$(2)])" "$(notdir $(3))" "$(if $(4), $(4))";
Andrii Nakryiko965b9fe2020-01-09 21:17:16 -0800100MAKEFLAGS += --no-print-directory
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800101submake_extras := feature_display=0
Andrii Nakryiko92f74402019-12-16 22:14:25 -0800102endif
103
104# override lib.mk's default rules
105OVERRIDE_TARGETS := 1
106override define CLEAN
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800107 $(call msg,CLEAN)
Andrii Nakryikode29e6b2021-11-10 21:36:17 -0800108 $(Q)$(RM) -r $(TEST_GEN_PROGS)
109 $(Q)$(RM) -r $(TEST_GEN_PROGS_EXTENDED)
110 $(Q)$(RM) -r $(TEST_GEN_FILES)
111 $(Q)$(RM) -r $(EXTRA_CLEAN)
Andrii Nakryiko9f7fa222020-12-03 12:46:26 -0800112 $(Q)$(MAKE) -C bpf_testmod clean
Joe Stringera01d9352021-03-02 09:19:43 -0800113 $(Q)$(MAKE) docs-clean
Andrii Nakryiko92f74402019-12-16 22:14:25 -0800114endef
115
Zi Shen Lime8f1f342017-03-19 23:03:14 -0700116include ../lib.mk
Alexei Starovoitov1da8ac72017-03-10 22:05:55 -0800117
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100118SCRATCH_DIR := $(OUTPUT)/tools
119BUILD_DIR := $(SCRATCH_DIR)/build
120INCLUDE_DIR := $(SCRATCH_DIR)/include
121BPFOBJ := $(BUILD_DIR)/libbpf/libbpf.a
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100122ifneq ($(CROSS_COMPILE),)
123HOST_BUILD_DIR := $(BUILD_DIR)/host
124HOST_SCRATCH_DIR := $(OUTPUT)/host-tools
Quentin Monnet14789942021-10-07 20:44:30 +0100125HOST_INCLUDE_DIR := $(HOST_SCRATCH_DIR)/include
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100126else
127HOST_BUILD_DIR := $(BUILD_DIR)
128HOST_SCRATCH_DIR := $(SCRATCH_DIR)
Quentin Monnet14789942021-10-07 20:44:30 +0100129HOST_INCLUDE_DIR := $(INCLUDE_DIR)
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100130endif
131HOST_BPFOBJ := $(HOST_BUILD_DIR)/libbpf/libbpf.a
132RESOLVE_BTFIDS := $(HOST_BUILD_DIR)/resolve_btfids/resolve_btfids
Quentin Monnetbe795052021-10-07 20:44:31 +0100133RUNQSLOWER_OUTPUT := $(BUILD_DIR)/runqslower/
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100134
Andrii Nakryikoa67079b2020-12-10 17:59:46 -0800135VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \
136 $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \
137 ../../../../vmlinux \
138 /sys/kernel/btf/vmlinux \
139 /boot/vmlinux-$(shell uname -r)
140VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
Kamal Mostafa1a3449c2020-12-15 10:20:10 -0800141ifeq ($(VMLINUX_BTF),)
142$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)")
143endif
Andrii Nakryikoa67079b2020-12-10 17:59:46 -0800144
Andrii Nakryiko03dcb782019-10-15 23:00:48 -0700145# Define simple and short `make test_progs`, `make test_sysctl`, etc targets
146# to build individual tests.
147# NOTE: Semicolon at the end is critical to override lib.mk's default static
148# rule for binaries.
149$(notdir $(TEST_GEN_PROGS) \
150 $(TEST_PROGS) \
151 $(TEST_PROGS_EXTENDED) \
152 $(TEST_GEN_PROGS_EXTENDED) \
153 $(TEST_CUSTOM_PROGS)): %: $(OUTPUT)/% ;
154
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100155# sort removes libbpf duplicates when not cross-building
156MAKE_DIRS := $(sort $(BUILD_DIR)/libbpf $(HOST_BUILD_DIR)/libbpf \
157 $(HOST_BUILD_DIR)/bpftool $(HOST_BUILD_DIR)/resolve_btfids \
Quentin Monnetbe795052021-10-07 20:44:31 +0100158 $(RUNQSLOWER_OUTPUT) $(INCLUDE_DIR))
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100159$(MAKE_DIRS):
160 $(call msg,MKDIR,,$@)
161 $(Q)mkdir -p $@
162
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700163$(OUTPUT)/%.o: %.c
164 $(call msg,CC,,$@)
165 $(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
166
Andrii Nakryiko92f74402019-12-16 22:14:25 -0800167$(OUTPUT)/%:%.c
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800168 $(call msg,BINARY,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700169 $(Q)$(LINK.c) $^ $(LDLIBS) -o $@
Andrii Nakryiko92f74402019-12-16 22:14:25 -0800170
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700171$(OUTPUT)/urandom_read: urandom_read.c
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800172 $(call msg,BINARY,,$@)
Jean-Philippe Bruckerea790202021-12-16 16:38:43 +0000173 $(Q)$(CC) $(CFLAGS) $(LDFLAGS) $< $(LDLIBS) -Wl,--build-id=sha1 -o $@
Jiong Wang3ef84342019-01-26 12:26:14 -0500174
Andrii Nakryiko9f7fa222020-12-03 12:46:26 -0800175$(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(wildcard bpf_testmod/Makefile bpf_testmod/*.[ch])
176 $(call msg,MOD,,$@)
Andrii Nakryikoa67079b2020-12-10 17:59:46 -0800177 $(Q)$(RM) bpf_testmod/bpf_testmod.ko # force re-compilation
Andrii Nakryiko9f7fa222020-12-03 12:46:26 -0800178 $(Q)$(MAKE) $(submake_extras) -C bpf_testmod
179 $(Q)cp bpf_testmod/bpf_testmod.ko $@
180
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100181DEFAULT_BPFTOOL := $(HOST_SCRATCH_DIR)/sbin/bpftool
Andrii Nakryiko6bcaf412020-08-04 17:47:57 -0700182
Quentin Monnetbe795052021-10-07 20:44:31 +0100183$(OUTPUT)/runqslower: $(BPFOBJ) | $(DEFAULT_BPFTOOL) $(RUNQSLOWER_OUTPUT)
184 $(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/runqslower \
185 OUTPUT=$(RUNQSLOWER_OUTPUT) VMLINUX_BTF=$(VMLINUX_BTF) \
Jean-Philippe Bruckere4ac80e2021-11-12 15:51:30 +0000186 BPFTOOL_OUTPUT=$(HOST_BUILD_DIR)/bpftool/ \
Quentin Monnetbe795052021-10-07 20:44:31 +0100187 BPFOBJ_OUTPUT=$(BUILD_DIR)/libbpf \
188 BPFOBJ=$(BPFOBJ) BPF_INCLUDE=$(INCLUDE_DIR) && \
189 cp $(RUNQSLOWER_OUTPUT)runqslower $@
Zi Shen Lime8f1f342017-03-19 23:03:14 -0700190
Li Zhijian404bd9f2021-08-20 09:55:56 +0800191TEST_GEN_PROGS_EXTENDED += $(DEFAULT_BPFTOOL)
192
Andrii Nakryikof87c1932021-11-03 15:08:43 -0700193$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(BPFOBJ)
Jesper Dangaard Brouerf09b2e32018-02-08 12:48:27 +0100194
Jean-Philippe Bruckereee9a6d2021-12-01 14:51:02 +0000195CGROUP_HELPERS := $(OUTPUT)/cgroup_helpers.o
196TESTING_HELPERS := $(OUTPUT)/testing_helpers.o
197TRACE_HELPERS := $(OUTPUT)/trace_helpers.o
198
199$(OUTPUT)/test_dev_cgroup: $(CGROUP_HELPERS) $(TESTING_HELPERS)
200$(OUTPUT)/test_skb_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS)
201$(OUTPUT)/test_sock: $(CGROUP_HELPERS) $(TESTING_HELPERS)
202$(OUTPUT)/test_sock_addr: $(CGROUP_HELPERS) $(TESTING_HELPERS)
203$(OUTPUT)/test_sockmap: $(CGROUP_HELPERS) $(TESTING_HELPERS)
204$(OUTPUT)/test_tcpnotify_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(TRACE_HELPERS)
205$(OUTPUT)/get_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS)
206$(OUTPUT)/test_cgroup_storage: $(CGROUP_HELPERS) $(TESTING_HELPERS)
207$(OUTPUT)/test_sock_fields: $(CGROUP_HELPERS) $(TESTING_HELPERS)
208$(OUTPUT)/test_sysctl: $(CGROUP_HELPERS) $(TESTING_HELPERS)
209$(OUTPUT)/test_tag: $(TESTING_HELPERS)
210$(OUTPUT)/test_lirc_mode2_user: $(TESTING_HELPERS)
211$(OUTPUT)/xdping: $(TESTING_HELPERS)
212$(OUTPUT)/flow_dissector_load: $(TESTING_HELPERS)
213$(OUTPUT)/test_maps: $(TESTING_HELPERS)
214$(OUTPUT)/test_verifier: $(TESTING_HELPERS)
Jesper Dangaard Brouer41757dc2018-02-13 14:19:15 +0100215
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800216BPFTOOL ?= $(DEFAULT_BPFTOOL)
Andrii Nakryiko03506292020-01-23 21:41:48 -0800217$(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) \
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100218 $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/bpftool
Andrii Nakryiko03506292020-01-23 21:41:48 -0800219 $(Q)$(MAKE) $(submake_extras) -C $(BPFTOOLDIR) \
Jean-Philippe Bruckerea790202021-12-16 16:38:43 +0000220 ARCH= CROSS_COMPILE= CC=$(HOSTCC) LD=$(HOSTLD) \
Andrii Nakryiko41c472e2021-04-23 11:13:43 -0700221 EXTRA_CFLAGS='-g -O0' \
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100222 OUTPUT=$(HOST_BUILD_DIR)/bpftool/ \
Quentin Monnetf012ade12021-10-07 20:44:29 +0100223 LIBBPF_OUTPUT=$(HOST_BUILD_DIR)/libbpf/ \
224 LIBBPF_DESTDIR=$(HOST_SCRATCH_DIR)/ \
Quentin Monnetd7db0a42021-10-07 20:44:38 +0100225 prefix= DESTDIR=$(HOST_SCRATCH_DIR)/ install-bin
Joe Stringera01d9352021-03-02 09:19:43 -0800226
Jiri Olsad044d9f2021-04-20 15:24:28 +0200227all: docs
228
Joe Stringera01d9352021-03-02 09:19:43 -0800229docs:
230 $(Q)RST2MAN_OPTS="--exit-status=1" $(MAKE) $(submake_extras) \
231 -f Makefile.docs \
232 prefix= OUTPUT=$(OUTPUT)/ DESTDIR=$(OUTPUT)/ $@
233
234docs-clean:
235 $(Q)$(MAKE) $(submake_extras) \
236 -f Makefile.docs \
237 prefix= OUTPUT=$(OUTPUT)/ DESTDIR=$(OUTPUT)/ $@
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800238
Andrii Nakryiko03506292020-01-23 21:41:48 -0800239$(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile) \
Andrii Nakryikode29e6b2021-11-10 21:36:17 -0800240 $(APIDIR)/linux/bpf.h \
Quentin Monnet14789942021-10-07 20:44:30 +0100241 | $(BUILD_DIR)/libbpf
Toke Høiland-Jørgensena2c96522020-01-20 14:06:52 +0100242 $(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(BUILD_DIR)/libbpf/ \
Andrii Nakryiko41c472e2021-04-23 11:13:43 -0700243 EXTRA_CFLAGS='-g -O0' \
Andrii Nakryiko03506292020-01-23 21:41:48 -0800244 DESTDIR=$(SCRATCH_DIR) prefix= all install_headers
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800245
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100246ifneq ($(BPFOBJ),$(HOST_BPFOBJ))
Andrii Nakryikode29e6b2021-11-10 21:36:17 -0800247$(HOST_BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile) \
248 $(APIDIR)/linux/bpf.h \
249 | $(HOST_BUILD_DIR)/libbpf
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100250 $(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) \
Jean-Philippe Bruckerea790202021-12-16 16:38:43 +0000251 EXTRA_CFLAGS='-g -O0' ARCH= CROSS_COMPILE= \
Andrii Nakryiko252e3cb2021-03-13 13:09:20 -0800252 OUTPUT=$(HOST_BUILD_DIR)/libbpf/ CC=$(HOSTCC) LD=$(HOSTLD) \
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100253 DESTDIR=$(HOST_SCRATCH_DIR)/ prefix= all install_headers
254endif
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700255
Andrii Nakryikocab62c32021-03-18 12:40:34 -0700256$(INCLUDE_DIR)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL) | $(INCLUDE_DIR)
Andrii Nakryikoca4db632020-06-29 17:47:59 -0700257ifeq ($(VMLINUX_H),)
Andrii Nakryikoacbd0622020-03-13 10:23:36 -0700258 $(call msg,GEN,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700259 $(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
Andrii Nakryikoca4db632020-06-29 17:47:59 -0700260else
261 $(call msg,CP,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700262 $(Q)cp "$(VMLINUX_H)" $@
Andrii Nakryikoca4db632020-06-29 17:47:59 -0700263endif
Andrii Nakryikoacbd0622020-03-13 10:23:36 -0700264
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100265$(RESOLVE_BTFIDS): $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/resolve_btfids \
Jiri Olsacc15a202020-07-11 23:53:29 +0200266 $(TOOLSDIR)/bpf/resolve_btfids/main.c \
267 $(TOOLSDIR)/lib/rbtree.c \
268 $(TOOLSDIR)/lib/zalloc.c \
269 $(TOOLSDIR)/lib/string.c \
270 $(TOOLSDIR)/lib/ctype.c \
271 $(TOOLSDIR)/lib/str_error_r.c
272 $(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/resolve_btfids \
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100273 CC=$(HOSTCC) LD=$(HOSTLD) AR=$(HOSTAR) \
Quentin Monnet14789942021-10-07 20:44:30 +0100274 LIBBPF_INCLUDE=$(HOST_INCLUDE_DIR) \
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100275 OUTPUT=$(HOST_BUILD_DIR)/resolve_btfids/ BPFOBJ=$(HOST_BPFOBJ)
Jiri Olsacc15a202020-07-11 23:53:29 +0200276
Sirio Balmelli167381f2018-05-21 09:00:03 +0200277# Get Clang's default includes on this system, as opposed to those seen by
278# '-target bpf'. This fixes "missing" files on some architectures/distros,
279# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
280#
281# Use '-idirafter': Don't interfere with include mechanics except where the
282# build would have failed anyways.
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200283define get_sys_includes
284$(shell $(1) -v -E - </dev/null 2>&1 \
Björn Töpel6016df82020-11-18 08:16:38 +0100285 | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
Björn Töpel36e70b92021-10-28 18:10:57 +0200286$(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 +0200287endef
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700288
Ilya Leoshkevich313e7f62019-10-28 11:20:49 +0100289# Determine target endianness.
290IS_LITTLE_ENDIAN = $(shell $(CC) -dM -E - </dev/null | \
291 grep 'define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__')
292MENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian)
293
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200294CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
Ilya Leoshkevich313e7f62019-10-28 11:20:49 +0100295BPF_CFLAGS = -g -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN) \
Tobias Klauserbcd66b12020-03-13 12:31:05 +0100296 -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR) \
297 -I$(abspath $(OUTPUT)/../usr/include)
Sirio Balmelli167381f2018-05-21 09:00:03 +0200298
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200299CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \
300 -Wno-compare-distinct-pointer-types
Alexei Starovoitov3bc35c62017-12-14 17:55:11 -0800301
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200302$(OUTPUT)/test_l4lb_noinline.o: BPF_CFLAGS += -fno-inline
303$(OUTPUT)/test_xdp_noinline.o: BPF_CFLAGS += -fno-inline
Alexei Starovoitov3bc35c62017-12-14 17:55:11 -0800304
Stanislav Fomichevbf0f0fd2019-01-28 08:53:55 -0800305$(OUTPUT)/flow_dissector_load.o: flow_dissector_load.h
Stanislav Fomichevbf0f0fd2019-01-28 08:53:55 -0800306
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700307# Build BPF object using Clang
308# $1 - input .c file
309# $2 - output .o file
310# $3 - CFLAGS
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700311define CLANG_BPF_BUILD_RULE
Andrew Delgadillo89ad7422020-12-11 00:43:44 +0000312 $(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2)
Andrii Nakryikode29e6b2021-11-10 21:36:17 -0800313 $(Q)$(CLANG) $3 -O2 -target bpf -c $1 -mcpu=v3 -o $2
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700314endef
Andrii Nakryiko24f65052019-11-19 16:25:10 -0800315# Similar to CLANG_BPF_BUILD_RULE, but with disabled alu32
316define CLANG_NOALU32_BPF_BUILD_RULE
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800317 $(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2)
Andrii Nakryikode29e6b2021-11-10 21:36:17 -0800318 $(Q)$(CLANG) $3 -O2 -target bpf -c $1 -mcpu=v2 -o $2
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700319endef
320# Build BPF object using GCC
321define GCC_BPF_BUILD_RULE
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800322 $(call msg,GCC-BPF,$(TRUNNER_BINARY),$2)
Andrew Delgadillo89ad7422020-12-11 00:43:44 +0000323 $(Q)$(BPF_GCC) $3 -O2 -c $1 -o $2
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700324endef
Stanislav Fomicheve78e00b2019-03-06 11:59:26 -0800325
Jakub Sitnicki0b9ad562020-04-29 20:11:54 +0200326SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800327
Andrii Nakryiko3b2ad502021-04-23 11:13:47 -0700328LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \
329 linked_vars.skel.h linked_maps.skel.h
Andrii Nakryikoa0964f52021-03-18 12:40:36 -0700330
Alexei Starovoitov4d1b6292021-05-13 17:36:20 -0700331LSKELS := kfunc_call_test.c fentry_test.c fexit_test.c fexit_sleep.c \
Alexei Starovoitov650c9db2021-12-01 10:10:37 -0800332 test_ringbuf.c atomics.c trace_printk.c trace_vprintk.c \
Alexei Starovoitov26b367e2021-12-01 10:10:38 -0800333 map_ptr_kern.c core_kern.c
Kumar Kartikeya Dwivedic48e51c2021-10-02 06:47:57 +0530334# Generate both light skeleton and libbpf skeleton for these
Alexei Starovoitovbc5f75d2021-12-01 10:10:35 -0800335LSKELS_EXTRA := test_ksyms_module.c test_ksyms_weak.c kfunc_call_test_subprog.c
Alexei Starovoitov4d1b6292021-05-13 17:36:20 -0700336SKEL_BLACKLIST += $$(LSKELS)
337
Andrii Nakryikoa0964f52021-03-18 12:40:36 -0700338test_static_linked.skel.h-deps := test_static_linked1.o test_static_linked2.o
Andrii Nakryikof2644fb2021-04-23 11:13:45 -0700339linked_funcs.skel.h-deps := linked_funcs1.o linked_funcs2.o
Andrii Nakryiko14f1aae2021-04-23 11:13:46 -0700340linked_vars.skel.h-deps := linked_vars1.o linked_vars2.o
Andrii Nakryiko3b2ad502021-04-23 11:13:47 -0700341linked_maps.skel.h-deps := linked_maps1.o linked_maps2.o
Andrii Nakryikoa0964f52021-03-18 12:40:36 -0700342
Andrii Nakryikob131aed2021-04-23 11:13:44 -0700343LINKED_BPF_SRCS := $(patsubst %.o,%.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
344
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700345# Set up extra TRUNNER_XXX "temporary" variables in the environment (relies on
346# $eval()) and pass control to DEFINE_TEST_RUNNER_RULES.
347# Parameters:
348# $1 - test runner base binary name (e.g., test_progs)
Yonghong Songe13a2fe2019-10-21 21:31:19 -0700349# $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700350define DEFINE_TEST_RUNNER
Jiong Wang3ef84342019-01-26 12:26:14 -0500351
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700352TRUNNER_OUTPUT := $(OUTPUT)$(if $2,/)$2
353TRUNNER_BINARY := $1$(if $2,-)$2
354TRUNNER_TEST_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.test.o, \
355 $$(notdir $$(wildcard $(TRUNNER_TESTS_DIR)/*.c)))
356TRUNNER_EXTRA_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, \
357 $$(filter %.c,$(TRUNNER_EXTRA_SOURCES)))
358TRUNNER_EXTRA_HDRS := $$(filter %.h,$(TRUNNER_EXTRA_SOURCES))
359TRUNNER_TESTS_HDR := $(TRUNNER_TESTS_DIR)/tests.h
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800360TRUNNER_BPF_SRCS := $$(notdir $$(wildcard $(TRUNNER_BPF_PROGS_DIR)/*.c))
361TRUNNER_BPF_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, $$(TRUNNER_BPF_SRCS))
362TRUNNER_BPF_SKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.skel.h, \
Andrii Nakryikob131aed2021-04-23 11:13:44 -0700363 $$(filter-out $(SKEL_BLACKLIST) $(LINKED_BPF_SRCS),\
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800364 $$(TRUNNER_BPF_SRCS)))
Kumar Kartikeya Dwivedic48e51c2021-10-02 06:47:57 +0530365TRUNNER_BPF_LSKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.lskel.h, $$(LSKELS) $$(LSKELS_EXTRA))
Andrii Nakryikoa0964f52021-03-18 12:40:36 -0700366TRUNNER_BPF_SKELS_LINKED := $$(addprefix $$(TRUNNER_OUTPUT)/,$(LINKED_SKELS))
Yauheni Kaliuta309b81f2020-05-13 05:17:22 +0300367TEST_GEN_FILES += $$(TRUNNER_BPF_OBJS)
Jiong Wang3ef84342019-01-26 12:26:14 -0500368
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700369# Evaluate rules now with extra TRUNNER_XXX variables above already defined
370$$(eval $$(call DEFINE_TEST_RUNNER_RULES,$1,$2))
Stanislav Fomichev3f306582019-03-01 19:42:13 -0800371
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700372endef
Jiong Wang3ef84342019-01-26 12:26:14 -0500373
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700374# Using TRUNNER_XXX variables, provided by callers of DEFINE_TEST_RUNNER and
375# set up by DEFINE_TEST_RUNNER itself, create test runner build rules with:
376# $1 - test runner base binary name (e.g., test_progs)
Yonghong Songe13a2fe2019-10-21 21:31:19 -0700377# $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700378define DEFINE_TEST_RUNNER_RULES
379
380ifeq ($($(TRUNNER_OUTPUT)-dir),)
381$(TRUNNER_OUTPUT)-dir := y
382$(TRUNNER_OUTPUT):
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800383 $$(call msg,MKDIR,,$$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700384 $(Q)mkdir -p $$@
Jiong Wang3ef84342019-01-26 12:26:14 -0500385endif
386
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700387# ensure we set up BPF objects generation rule just once for a given
388# input/output directory combination
389ifeq ($($(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs),)
390$(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs := y
391$(TRUNNER_BPF_OBJS): $(TRUNNER_OUTPUT)/%.o: \
392 $(TRUNNER_BPF_PROGS_DIR)/%.c \
393 $(TRUNNER_BPF_PROGS_DIR)/*.h \
Andrii Nakryikoacbd0622020-03-13 10:23:36 -0700394 $$(INCLUDE_DIR)/vmlinux.h \
Jiri Bencd888eaa2021-09-27 18:01:36 +0200395 $(wildcard $(BPFDIR)/bpf_*.h) \
396 | $(TRUNNER_OUTPUT) $$(BPFOBJ)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700397 $$(call $(TRUNNER_BPF_BUILD_RULE),$$<,$$@, \
Andrew Delgadillo89ad7422020-12-11 00:43:44 +0000398 $(TRUNNER_BPF_CFLAGS))
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800399
Andrii Nakryiko14137f32021-03-18 12:40:35 -0700400$(TRUNNER_BPF_SKELS): %.skel.h: %.o $(BPFTOOL) | $(TRUNNER_OUTPUT)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800401 $$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
Andrii Nakryikoa0964f52021-03-18 12:40:36 -0700402 $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked1.o) $$<
403 $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked2.o) $$(<:.o=.linked1.o)
Andrii Nakryiko14137f32021-03-18 12:40:35 -0700404 $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked3.o) $$(<:.o=.linked2.o)
405 $(Q)diff $$(<:.o=.linked2.o) $$(<:.o=.linked3.o)
Andrii Nakryikoa0964f52021-03-18 12:40:36 -0700406 $(Q)$$(BPFTOOL) gen skeleton $$(<:.o=.linked3.o) name $$(notdir $$(<:.o=)) > $$@
407
Alexei Starovoitov4d1b6292021-05-13 17:36:20 -0700408$(TRUNNER_BPF_LSKELS): %.lskel.h: %.o $(BPFTOOL) | $(TRUNNER_OUTPUT)
409 $$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
410 $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked1.o) $$<
411 $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked2.o) $$(<:.o=.linked1.o)
412 $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked3.o) $$(<:.o=.linked2.o)
413 $(Q)diff $$(<:.o=.linked2.o) $$(<:.o=.linked3.o)
Kumar Kartikeya Dwivedi087cba72021-10-28 12:04:59 +0530414 $(Q)$$(BPFTOOL) gen skeleton -L $$(<:.o=.linked3.o) name $$(notdir $$(<:.o=_lskel)) > $$@
Alexei Starovoitov4d1b6292021-05-13 17:36:20 -0700415
Andrii Nakryikoa0964f52021-03-18 12:40:36 -0700416$(TRUNNER_BPF_SKELS_LINKED): $(TRUNNER_BPF_OBJS) $(BPFTOOL) | $(TRUNNER_OUTPUT)
417 $$(call msg,LINK-BPF,$(TRUNNER_BINARY),$$(@:.skel.h=.o))
418 $(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked1.o) $$(addprefix $(TRUNNER_OUTPUT)/,$$($$(@F)-deps))
419 $(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked1.o)
420 $(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked3.o) $$(@:.skel.h=.linked2.o)
421 $(Q)diff $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked3.o)
422 $$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
423 $(Q)$$(BPFTOOL) gen skeleton $$(@:.skel.h=.linked3.o) name $$(notdir $$(@:.skel.h=)) > $$@
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700424endif
425
426# ensure we set up tests.h header generation rule just once
427ifeq ($($(TRUNNER_TESTS_DIR)-tests-hdr),)
428$(TRUNNER_TESTS_DIR)-tests-hdr := y
429$(TRUNNER_TESTS_HDR): $(TRUNNER_TESTS_DIR)/*.c
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800430 $$(call msg,TEST-HDR,$(TRUNNER_BINARY),$$@)
Andrii Nakryiko8ea688e2021-10-22 15:32:26 -0700431 $$(shell (echo '/* Generated header, do not edit */'; \
432 sed -n -E 's/^void (serial_)?test_([a-zA-Z0-9_]+)\((void)?\).*/DEFINE_TEST(\2)/p' \
433 $(TRUNNER_TESTS_DIR)/*.c | sort ; \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700434 ) > $$@)
435endif
436
437# compile individual test files
438# Note: we cd into output directory to ensure embedded BPF object is found
439$(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o: \
440 $(TRUNNER_TESTS_DIR)/%.c \
441 $(TRUNNER_EXTRA_HDRS) \
442 $(TRUNNER_BPF_OBJS) \
Andrii Nakryikof3c926a2019-12-13 17:43:38 -0800443 $(TRUNNER_BPF_SKELS) \
Alexei Starovoitov4d1b6292021-05-13 17:36:20 -0700444 $(TRUNNER_BPF_LSKELS) \
Andrii Nakryikoa0964f52021-03-18 12:40:36 -0700445 $(TRUNNER_BPF_SKELS_LINKED) \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700446 $$(BPFOBJ) | $(TRUNNER_OUTPUT)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800447 $$(call msg,TEST-OBJ,$(TRUNNER_BINARY),$$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700448 $(Q)cd $$(@D) && $$(CC) -I. $$(CFLAGS) -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700449
450$(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o: \
451 %.c \
452 $(TRUNNER_EXTRA_HDRS) \
453 $(TRUNNER_TESTS_HDR) \
454 $$(BPFOBJ) | $(TRUNNER_OUTPUT)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800455 $$(call msg,EXT-OBJ,$(TRUNNER_BINARY),$$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700456 $(Q)$$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700457
Ilya Leoshkevich86fd1662021-02-24 12:14:45 +0100458# non-flavored in-srctree builds receive special treatment, in particular, we
459# do not need to copy extra resources (see e.g. test_btf_dump_case())
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700460$(TRUNNER_BINARY)-extras: $(TRUNNER_EXTRA_FILES) | $(TRUNNER_OUTPUT)
Ilya Leoshkevich86fd1662021-02-24 12:14:45 +0100461ifneq ($2:$(OUTPUT),:$(shell pwd))
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800462 $$(call msg,EXT-COPY,$(TRUNNER_BINARY),$(TRUNNER_EXTRA_FILES))
Ilya Leoshkevich86fd1662021-02-24 12:14:45 +0100463 $(Q)rsync -aq $$^ $(TRUNNER_OUTPUT)/
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700464endif
465
466$(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS) \
467 $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ) \
Jiri Olsacc15a202020-07-11 23:53:29 +0200468 $(RESOLVE_BTFIDS) \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700469 | $(TRUNNER_BINARY)-extras
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800470 $$(call msg,BINARY,,$$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700471 $(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
Kumar Kartikeya Dwivedif614f2c2021-10-02 06:47:52 +0530472 $(Q)$(RESOLVE_BTFIDS) --btf $(TRUNNER_OUTPUT)/btf_data.o $$@
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700473
474endef
475
476# Define test_progs test runner.
477TRUNNER_TESTS_DIR := prog_tests
478TRUNNER_BPF_PROGS_DIR := progs
479TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c \
Andrii Nakryikocd492912020-05-12 12:24:42 -0700480 network_helpers.c testing_helpers.c \
Andrii Nakryikode29e6b2021-11-10 21:36:17 -0800481 btf_helpers.c flow_dissector_load.h
Andrii Nakryiko9f7fa222020-12-03 12:46:26 -0800482TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko \
KP Singh854055c2020-11-26 18:49:46 +0000483 ima_setup.sh \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700484 $(wildcard progs/btf_dump_test_case_*.c)
485TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE
Andrii Nakryikode29e6b2021-11-10 21:36:17 -0800486TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS) -DENABLE_ATOMICS_TESTS
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700487$(eval $(call DEFINE_TEST_RUNNER,test_progs))
488
Yonghong Songe13a2fe2019-10-21 21:31:19 -0700489# Define test_progs-no_alu32 test runner.
Andrii Nakryiko24f65052019-11-19 16:25:10 -0800490TRUNNER_BPF_BUILD_RULE := CLANG_NOALU32_BPF_BUILD_RULE
Brendan Jackman98d666d2021-01-14 18:17:50 +0000491TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS)
Yonghong Songe13a2fe2019-10-21 21:31:19 -0700492$(eval $(call DEFINE_TEST_RUNNER,test_progs,no_alu32))
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700493
494# Define test_progs BPF-GCC-flavored test runner.
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200495ifneq ($(BPF_GCC),)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700496TRUNNER_BPF_BUILD_RULE := GCC_BPF_BUILD_RULE
Ilya Leoshkevich313e7f62019-10-28 11:20:49 +0100497TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(call get_sys_includes,gcc)
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700498$(eval $(call DEFINE_TEST_RUNNER,test_progs,bpf_gcc))
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +0200499endif
500
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700501# Define test_maps test runner.
502TRUNNER_TESTS_DIR := map_tests
503TRUNNER_BPF_PROGS_DIR := progs
504TRUNNER_EXTRA_SOURCES := test_maps.c
505TRUNNER_EXTRA_FILES :=
506TRUNNER_BPF_BUILD_RULE := $$(error no BPF objects should be built)
507TRUNNER_BPF_CFLAGS :=
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700508$(eval $(call DEFINE_TEST_RUNNER,test_maps))
Martin KaFai Lauc0fa1b62018-04-18 15:56:06 -0700509
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700510# Define test_verifier test runner.
511# It is much simpler than test_maps/test_progs and sufficiently different from
512# them (e.g., test.h is using completely pattern), that it's worth just
513# explicitly defining all the rules explicitly.
514verifier/tests.h: verifier/*.c
Stanislav Fomicheve78e00b2019-03-06 11:59:26 -0800515 $(shell ( cd verifier/; \
Jakub Kicinski2dfb4012019-01-25 15:24:42 -0800516 echo '/* Generated header, do not edit */'; \
517 echo '#ifdef FILL_ARRAY'; \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700518 ls *.c 2> /dev/null | sed -e 's@\(.*\)@#include \"\1\"@'; \
Jakub Kicinski2dfb4012019-01-25 15:24:42 -0800519 echo '#endif' \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700520 ) > verifier/tests.h)
521$(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800522 $(call msg,BINARY,,$@)
Andrii Nakryikod5ca5902020-08-06 20:30:57 -0700523 $(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
Jakub Kicinski2dfb4012019-01-25 15:24:42 -0800524
Stanislav Fomichev6bf6aff2019-12-02 13:59:31 -0800525# Make sure we are able to include and link libbpf against c++.
Andrii Nakryiko7c8dce42019-12-26 13:02:53 -0800526$(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ)
Andrii Nakryiko2cc51d32020-01-12 23:31:40 -0800527 $(call msg,CXX,,$@)
Yonghong Songa22c0c82021-04-13 08:34:24 -0700528 $(Q)$(CXX) $(CFLAGS) $(filter %.a %.o %.cpp,$^) $(LDLIBS) -o $@
Stanislav Fomichev6bf6aff2019-12-02 13:59:31 -0800529
Andrii Nakryiko8e7c2a02020-05-12 12:24:43 -0700530# Benchmark runner
Quentin Monnet0b46b752021-09-08 14:32:26 -0700531$(OUTPUT)/bench_%.o: benchs/bench_%.c bench.h $(BPFOBJ)
Andrii Nakryiko8e7c2a02020-05-12 12:24:43 -0700532 $(call msg,CC,,$@)
Joanne Koong57fd1c62021-10-27 16:45:03 -0700533 $(Q)$(CC) $(CFLAGS) -O2 -c $(filter %.c,$^) $(LDLIBS) -o $@
Andrii Nakryiko4eaf0b52020-05-12 12:24:44 -0700534$(OUTPUT)/bench_rename.o: $(OUTPUT)/test_overhead.skel.h
Andrii Nakryikoc5d420c2020-05-12 12:24:45 -0700535$(OUTPUT)/bench_trigger.o: $(OUTPUT)/trigger_bench.skel.h
Andrii Nakryikoc97099b2020-05-29 00:54:23 -0700536$(OUTPUT)/bench_ringbufs.o: $(OUTPUT)/ringbuf_bench.skel.h \
537 $(OUTPUT)/perfbuf_bench.skel.h
Joanne Koong57fd1c62021-10-27 16:45:03 -0700538$(OUTPUT)/bench_bloom_filter_map.o: $(OUTPUT)/bloom_filter_bench.skel.h
Joanne Koongec151032021-11-29 19:06:22 -0800539$(OUTPUT)/bench_bpf_loop.o: $(OUTPUT)/bpf_loop_bench.skel.h
Hou Tao9c426522021-12-10 22:16:51 +0800540$(OUTPUT)/bench_strncmp.o: $(OUTPUT)/strncmp_bench.skel.h
Quentin Monnet0b46b752021-09-08 14:32:26 -0700541$(OUTPUT)/bench.o: bench.h testing_helpers.h $(BPFOBJ)
Andrii Nakryiko8e7c2a02020-05-12 12:24:43 -0700542$(OUTPUT)/bench: LDLIBS += -lm
Andrii Nakryikod41bc482021-11-15 17:30:41 -0800543$(OUTPUT)/bench: $(OUTPUT)/bench.o \
Jean-Philippe Bruckereee9a6d2021-12-01 14:51:02 +0000544 $(TESTING_HELPERS) \
545 $(TRACE_HELPERS) \
Andrii Nakryiko4eaf0b52020-05-12 12:24:44 -0700546 $(OUTPUT)/bench_count.o \
Andrii Nakryikoc5d420c2020-05-12 12:24:45 -0700547 $(OUTPUT)/bench_rename.o \
Andrii Nakryikoc97099b2020-05-29 00:54:23 -0700548 $(OUTPUT)/bench_trigger.o \
Joanne Koong57fd1c62021-10-27 16:45:03 -0700549 $(OUTPUT)/bench_ringbufs.o \
Joanne Koongec151032021-11-29 19:06:22 -0800550 $(OUTPUT)/bench_bloom_filter_map.o \
Hou Tao9c426522021-12-10 22:16:51 +0800551 $(OUTPUT)/bench_bpf_loop.o \
552 $(OUTPUT)/bench_strncmp.o
Andrii Nakryiko8e7c2a02020-05-12 12:24:43 -0700553 $(call msg,BINARY,,$@)
Jean-Philippe Bruckerea790202021-12-16 16:38:43 +0000554 $(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(filter %.a %.o,$^) $(LDLIBS) -o $@
Andrii Nakryiko8e7c2a02020-05-12 12:24:43 -0700555
Jean-Philippe Bruckerde11ae42021-01-13 17:33:16 +0100556EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(SCRATCH_DIR) $(HOST_SCRATCH_DIR) \
Andrii Nakryiko74b5a592019-10-15 23:00:49 -0700557 prog_tests/tests.h map_tests/tests.h verifier/tests.h \
Andrii Nakryiko2031af22020-01-09 21:17:14 -0800558 feature \
Alexei Starovoitov4d1b6292021-05-13 17:36:20 -0700559 $(addprefix $(OUTPUT)/,*.o *.skel.h *.lskel.h no_alu32 bpf_gcc bpf_testmod.ko)
Joe Stringera01d9352021-03-02 09:19:43 -0800560
561.PHONY: docs docs-clean