blob: 38638845db9d7480ea0a218f56116ae19658d116 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001# SPDX-License-Identifier: GPL-2.0
Jakub Kicinski787360f2018-05-14 22:35:04 -07002
3BPF_SAMPLES_PATH ?= $(abspath $(srctree)/$(src))
4TOOLS_PATH := $(BPF_SAMPLES_PATH)/../../tools
5
Quentin Monnet929bef42021-10-06 12:10:49 +01006pound := \#
7
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -07008# List of programs to build
Ivan Khoronzhuk1d97c6c2019-10-11 03:28:01 +03009tprogs-y := test_lru_dist
10tprogs-y += sock_example
11tprogs-y += fds_example
12tprogs-y += sockex1
13tprogs-y += sockex2
14tprogs-y += sockex3
15tprogs-y += tracex1
16tprogs-y += tracex2
17tprogs-y += tracex3
18tprogs-y += tracex4
19tprogs-y += tracex5
20tprogs-y += tracex6
21tprogs-y += tracex7
22tprogs-y += test_probe_write_user
23tprogs-y += trace_output
24tprogs-y += lathist
25tprogs-y += offwaketime
26tprogs-y += spintest
27tprogs-y += map_perf_test
28tprogs-y += test_overhead
29tprogs-y += test_cgrp2_array_pin
30tprogs-y += test_cgrp2_attach
31tprogs-y += test_cgrp2_sock
32tprogs-y += test_cgrp2_sock2
33tprogs-y += xdp1
34tprogs-y += xdp2
35tprogs-y += xdp_router_ipv4
36tprogs-y += test_current_task_under_cgroup
37tprogs-y += trace_event
38tprogs-y += sampleip
39tprogs-y += tc_l2_redirect
40tprogs-y += lwt_len_hist
41tprogs-y += xdp_tx_iptunnel
42tprogs-y += test_map_in_map
Prashant Bhole5984dc62019-12-16 16:16:19 +090043tprogs-y += per_socket_stats_example
Ivan Khoronzhuk1d97c6c2019-10-11 03:28:01 +030044tprogs-y += xdp_rxq_info
45tprogs-y += syscall_tp
46tprogs-y += cpustat
47tprogs-y += xdp_adjust_tail
48tprogs-y += xdpsock
Mariusz Dudek3627d972020-12-03 10:05:46 +010049tprogs-y += xdpsock_ctrl_proc
Cristian Dumitrescu35149b22020-08-28 10:26:28 +020050tprogs-y += xsk_fwd
Ivan Khoronzhuk1d97c6c2019-10-11 03:28:01 +030051tprogs-y += xdp_fwd
52tprogs-y += task_fd_query
53tprogs-y += xdp_sample_pkts
54tprogs-y += ibumad
55tprogs-y += hbm
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -070056
Kumar Kartikeya Dwivedie531a222021-08-21 05:50:06 +053057tprogs-y += xdp_redirect_cpu
Kumar Kartikeya Dwivedi594a1162021-08-21 05:50:10 +053058tprogs-y += xdp_redirect_map_multi
Kumar Kartikeya Dwivedibbe65862021-08-21 05:50:08 +053059tprogs-y += xdp_redirect_map
Kumar Kartikeya Dwivedib926c552021-08-21 05:50:04 +053060tprogs-y += xdp_redirect
Kumar Kartikeya Dwivedi6e1051a2021-08-21 05:50:02 +053061tprogs-y += xdp_monitor
62
Joe Stringer43371c82016-12-14 14:43:39 -080063# Libbpf dependencies
Quentin Monnet3f7a3312021-10-07 20:44:35 +010064LIBBPF_SRC = $(TOOLS_PATH)/lib/bpf
65LIBBPF_OUTPUT = $(abspath $(BPF_SAMPLES_PATH))/libbpf
66LIBBPF_DESTDIR = $(LIBBPF_OUTPUT)
67LIBBPF_INCLUDE = $(LIBBPF_DESTDIR)/include
68LIBBPF = $(LIBBPF_OUTPUT)/libbpf.a
Jakub Kicinski787360f2018-05-14 22:35:04 -070069
Roman Gushchin9d1f1592017-11-05 08:15:33 -050070CGROUP_HELPERS := ../../tools/testing/selftests/bpf/cgroup_helpers.o
Yonghong Song28dbf862018-04-28 22:28:13 -070071TRACE_HELPERS := ../../tools/testing/selftests/bpf/trace_helpers.o
Kumar Kartikeya Dwivedi6e1051a2021-08-21 05:50:02 +053072XDP_SAMPLE := xdp_sample_user.o
Joe Stringer43371c82016-12-14 14:43:39 -080073
Jakub Kicinski1a9b2682019-02-27 19:04:13 -080074fds_example-objs := fds_example.o
75sockex1-objs := sockex1_user.o
76sockex2-objs := sockex2_user.o
Daniel T. Leebc1a8592020-05-16 13:06:06 +090077sockex3-objs := sockex3_user.o
Daniel T. Lee63841bc2020-05-16 13:06:05 +090078tracex1-objs := tracex1_user.o $(TRACE_HELPERS)
79tracex2-objs := tracex2_user.o
80tracex3-objs := tracex3_user.o
81tracex4-objs := tracex4_user.o
Daniel T. Leebc1a8592020-05-16 13:06:06 +090082tracex5-objs := tracex5_user.o $(TRACE_HELPERS)
Daniel T. Lee63841bc2020-05-16 13:06:05 +090083tracex6-objs := tracex6_user.o
84tracex7-objs := tracex7_user.o
Daniel T. Lee3677d0a2020-08-23 17:53:33 +090085test_probe_write_user-objs := test_probe_write_user_user.o
Daniel T. Leeceb5dea2020-11-24 09:03:10 +000086trace_output-objs := trace_output_user.o
Daniel T. Lee3677d0a2020-08-23 17:53:33 +090087lathist-objs := lathist_user.o
Daniel T. Leef0c328f2020-08-23 17:53:34 +090088offwaketime-objs := offwaketime_user.o $(TRACE_HELPERS)
Daniel T. Lee3677d0a2020-08-23 17:53:33 +090089spintest-objs := spintest_user.o $(TRACE_HELPERS)
Daniel T. Lee35a8b6d2020-08-23 17:53:32 +090090map_perf_test-objs := map_perf_test_user.o
Daniel T. Leec6497df2020-11-24 09:03:08 +000091test_overhead-objs := test_overhead_user.o
Jakub Kicinski0cc54db2018-05-14 22:35:05 -070092test_cgrp2_array_pin-objs := test_cgrp2_array_pin.o
93test_cgrp2_attach-objs := test_cgrp2_attach.o
Jakub Kicinski0cc54db2018-05-14 22:35:05 -070094test_cgrp2_sock-objs := test_cgrp2_sock.o
Daniel T. Leed89af132020-11-24 09:03:05 +000095test_cgrp2_sock2-objs := test_cgrp2_sock2.o
Jakub Kicinski0cc54db2018-05-14 22:35:05 -070096xdp1-objs := xdp1_user.o
Brenden Blanco764cbcce2016-07-19 12:16:57 -070097# reuse xdp1 source intentionally
Jakub Kicinski0cc54db2018-05-14 22:35:05 -070098xdp2-objs := xdp1_user.o
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +010099xdp_router_ipv4-objs := xdp_router_ipv4_user.o
Daniel T. Lee3677d0a2020-08-23 17:53:33 +0900100test_current_task_under_cgroup-objs := $(CGROUP_HELPERS) \
Sargun Dhillon9e6e60e2016-08-12 08:57:04 -0700101 test_current_task_under_cgroup_user.o
Daniel T. Leeaa5e2af2020-03-21 19:04:24 +0900102trace_event-objs := trace_event_user.o $(TRACE_HELPERS)
103sampleip-objs := sampleip_user.o $(TRACE_HELPERS)
Daniel T. Leeceb5dea2020-11-24 09:03:10 +0000104tc_l2_redirect-objs := tc_l2_redirect_user.o
105lwt_len_hist-objs := lwt_len_hist_user.o
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100106xdp_tx_iptunnel-objs := xdp_tx_iptunnel_user.o
Daniel T. Lee88795b42020-07-08 03:48:53 +0900107test_map_in_map-objs := test_map_in_map_user.o
Jakub Kicinski0cc54db2018-05-14 22:35:05 -0700108per_socket_stats_example-objs := cookie_uid_helper_example.o
Jakub Kicinski0cc54db2018-05-14 22:35:05 -0700109xdp_rxq_info-objs := xdp_rxq_info_user.o
Daniel T. Leef0c328f2020-08-23 17:53:34 +0900110syscall_tp-objs := syscall_tp_user.o
111cpustat-objs := cpustat_user.o
Jakub Kicinski0cc54db2018-05-14 22:35:05 -0700112xdp_adjust_tail-objs := xdp_adjust_tail_user.o
Jakub Kicinski67481822018-07-26 14:32:21 -0700113xdpsock-objs := xdpsock_user.o
Mariusz Dudek3627d972020-12-03 10:05:46 +0100114xdpsock_ctrl_proc-objs := xdpsock_ctrl_proc.o
Cristian Dumitrescu35149b22020-08-28 10:26:28 +0200115xsk_fwd-objs := xsk_fwd.o
Jakub Kicinskie1a40ef2018-07-26 14:32:20 -0700116xdp_fwd-objs := xdp_fwd_user.o
Daniel T. Lee4fe66412020-11-24 09:03:06 +0000117task_fd_query-objs := task_fd_query_user.o $(TRACE_HELPERS)
Daniel T. Leeceb5dea2020-11-24 09:03:10 +0000118xdp_sample_pkts-objs := xdp_sample_pkts_user.o
Daniel T. Lee763af202020-11-24 09:03:07 +0000119ibumad-objs := ibumad_user.o
Daniel T. Leec5815ac2020-11-24 09:03:04 +0000120hbm-objs := hbm.o $(CGROUP_HELPERS)
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -0700121
Kumar Kartikeya Dwivedi594a1162021-08-21 05:50:10 +0530122xdp_redirect_map_multi-objs := xdp_redirect_map_multi_user.o $(XDP_SAMPLE)
Kumar Kartikeya Dwivedie531a222021-08-21 05:50:06 +0530123xdp_redirect_cpu-objs := xdp_redirect_cpu_user.o $(XDP_SAMPLE)
Kumar Kartikeya Dwivedibbe65862021-08-21 05:50:08 +0530124xdp_redirect_map-objs := xdp_redirect_map_user.o $(XDP_SAMPLE)
Kumar Kartikeya Dwivedib926c552021-08-21 05:50:04 +0530125xdp_redirect-objs := xdp_redirect_user.o $(XDP_SAMPLE)
Kumar Kartikeya Dwivedi6e1051a2021-08-21 05:50:02 +0530126xdp_monitor-objs := xdp_monitor_user.o $(XDP_SAMPLE)
127
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -0700128# Tell kbuild to always build the programs
Masahiro Yamada5f2fb522020-02-02 01:49:24 +0900129always-y := $(tprogs-y)
130always-y += sockex1_kern.o
131always-y += sockex2_kern.o
132always-y += sockex3_kern.o
133always-y += tracex1_kern.o
134always-y += tracex2_kern.o
135always-y += tracex3_kern.o
136always-y += tracex4_kern.o
137always-y += tracex5_kern.o
138always-y += tracex6_kern.o
139always-y += tracex7_kern.o
140always-y += sock_flags_kern.o
141always-y += test_probe_write_user_kern.o
142always-y += trace_output_kern.o
143always-y += tcbpf1_kern.o
144always-y += tc_l2_redirect_kern.o
145always-y += lathist_kern.o
146always-y += offwaketime_kern.o
147always-y += spintest_kern.o
148always-y += map_perf_test_kern.o
149always-y += test_overhead_tp_kern.o
150always-y += test_overhead_raw_tp_kern.o
151always-y += test_overhead_kprobe_kern.o
152always-y += parse_varlen.o parse_simple.o parse_ldabs.o
153always-y += test_cgrp2_tc_kern.o
154always-y += xdp1_kern.o
155always-y += xdp2_kern.o
156always-y += xdp_router_ipv4_kern.o
157always-y += test_current_task_under_cgroup_kern.o
158always-y += trace_event_kern.o
159always-y += sampleip_kern.o
160always-y += lwt_len_hist_kern.o
161always-y += xdp_tx_iptunnel_kern.o
162always-y += test_map_in_map_kern.o
163always-y += tcp_synrto_kern.o
164always-y += tcp_rwnd_kern.o
165always-y += tcp_bufs_kern.o
166always-y += tcp_cong_kern.o
167always-y += tcp_iw_kern.o
168always-y += tcp_clamp_kern.o
169always-y += tcp_basertt_kern.o
170always-y += tcp_tos_reflect_kern.o
171always-y += tcp_dumpstats_kern.o
Masahiro Yamada5f2fb522020-02-02 01:49:24 +0900172always-y += xdp_rxq_info_kern.o
173always-y += xdp2skb_meta_kern.o
174always-y += syscall_tp_kern.o
175always-y += cpustat_kern.o
176always-y += xdp_adjust_tail_kern.o
177always-y += xdp_fwd_kern.o
178always-y += task_fd_query_kern.o
179always-y += xdp_sample_pkts_kern.o
180always-y += ibumad_kern.o
181always-y += hbm_out_kern.o
182always-y += hbm_edt_kern.o
183always-y += xdpsock_kern.o
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -0700184
Ivan Khoronzhuk0e865ae2019-10-11 03:27:58 +0300185ifeq ($(ARCH), arm)
186# Strip all except -D__LINUX_ARM_ARCH__ option needed to handle linux
187# headers when arm instruction set identification is requested.
188ARM_ARCH_SELECTOR := $(filter -D__LINUX_ARM_ARCH__%, $(KBUILD_CFLAGS))
189BPF_EXTRA_CFLAGS := $(ARM_ARCH_SELECTOR)
Ivan Khoronzhuk1d97c6c2019-10-11 03:28:01 +0300190TPROGS_CFLAGS += $(ARM_ARCH_SELECTOR)
Ivan Khoronzhuk0e865ae2019-10-11 03:27:58 +0300191endif
192
Tiezhu Yang190d1c92021-01-25 13:05:46 +0800193ifeq ($(ARCH), mips)
194TPROGS_CFLAGS += -D__SANE_USERSPACE_TYPES__
Tiezhu Yang058107a2021-01-26 22:05:25 +0800195ifdef CONFIG_MACH_LOONGSON64
196BPF_EXTRA_CFLAGS += -I$(srctree)/arch/mips/include/asm/mach-loongson64
197BPF_EXTRA_CFLAGS += -I$(srctree)/arch/mips/include/asm/mach-generic
198endif
Tiezhu Yang190d1c92021-01-25 13:05:46 +0800199endif
200
Ivan Khoronzhuk10cb3d82019-10-11 03:28:02 +0300201TPROGS_CFLAGS += -Wall -O2
202TPROGS_CFLAGS += -Wmissing-prototypes
203TPROGS_CFLAGS += -Wstrict-prototypes
204
Ivan Khoronzhuk1d97c6c2019-10-11 03:28:01 +0300205TPROGS_CFLAGS += -I$(objtree)/usr/include
Ivan Khoronzhuk1d97c6c2019-10-11 03:28:01 +0300206TPROGS_CFLAGS += -I$(srctree)/tools/testing/selftests/bpf/
Quentin Monnet3f7a3312021-10-07 20:44:35 +0100207TPROGS_CFLAGS += -I$(LIBBPF_INCLUDE)
Ivan Khoronzhuk1d97c6c2019-10-11 03:28:01 +0300208TPROGS_CFLAGS += -I$(srctree)/tools/include
209TPROGS_CFLAGS += -I$(srctree)/tools/perf
David S. Miller14684b92019-11-09 11:04:37 -0800210TPROGS_CFLAGS += -DHAVE_ATTR_TEST=0
Ivan Khoronzhuk1d97c6c2019-10-11 03:28:01 +0300211
Ivan Khoronzhukb2327c12019-10-11 03:28:07 +0300212ifdef SYSROOT
213TPROGS_CFLAGS += --sysroot=$(SYSROOT)
214TPROGS_LDFLAGS := -L$(SYSROOT)/usr/lib
215endif
216
Toke Høiland-Jørgensen5615ed42019-12-16 11:24:05 +0100217TPROGS_LDLIBS += $(LIBBPF) -lelf -lz
Alexander Lobakine64fbca2021-12-03 20:50:03 +0100218TPROGLDLIBS_xdp_monitor += -lm
219TPROGLDLIBS_xdp_redirect += -lm
220TPROGLDLIBS_xdp_redirect_cpu += -lm
221TPROGLDLIBS_xdp_redirect_map += -lm
222TPROGLDLIBS_xdp_redirect_map_multi += -lm
Ivan Khoronzhuk1d97c6c2019-10-11 03:28:01 +0300223TPROGLDLIBS_tracex4 += -lrt
224TPROGLDLIBS_trace_output += -lrt
225TPROGLDLIBS_map_perf_test += -lrt
226TPROGLDLIBS_test_overhead += -lrt
Mariusz Dudek3627d972020-12-03 10:05:46 +0100227TPROGLDLIBS_xdpsock += -pthread -lcap
Cristian Dumitrescu35149b22020-08-28 10:26:28 +0200228TPROGLDLIBS_xsk_fwd += -pthread
Ivan Khoronzhuk1d97c6c2019-10-11 03:28:01 +0300229
Jesper Dangaard Brouerbdefbbf2016-04-28 14:21:14 +0200230# Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline:
Tiezhu Yang628add72021-01-22 09:39:44 +0800231# make M=samples/bpf LLC=~/git/llvm-project/llvm/build/bin/llc CLANG=~/git/llvm-project/llvm/build/bin/clang
Jesper Dangaard Brouer6ccfba72016-04-28 14:20:53 +0200232LLC ?= llc
Jesper Dangaard Brouerbdefbbf2016-04-28 14:21:14 +0200233CLANG ?= clang
Yonghong Song9618bde2020-10-05 21:34:26 -0700234OPT ?= opt
235LLVM_DIS ?= llvm-dis
Taeung Song9778cfd2018-07-26 19:13:44 +0900236LLVM_OBJCOPY ?= llvm-objcopy
Pu Lehui44ce0ac2021-10-21 20:39:13 +0800237LLVM_READELF ?= llvm-readelf
Taeung Song9778cfd2018-07-26 19:13:44 +0900238BTF_PAHOLE ?= pahole
Jesper Dangaard Brouer6ccfba72016-04-28 14:20:53 +0200239
Joel Fernandes876e88e2017-09-20 09:11:57 -0700240# Detect that we're cross compiling and use the cross compiler
241ifdef CROSS_COMPILE
Ivan Khoronzhuk518c1342019-10-11 03:27:56 +0300242CLANG_ARCH_ARGS = --target=$(notdir $(CROSS_COMPILE:%-=%))
Joel Fernandes876e88e2017-09-20 09:11:57 -0700243endif
244
Jakub Kicinski0ed3cc42019-06-07 10:11:16 -0700245# Don't evaluate probes and warnings if we need to run make recursively
246ifneq ($(src),)
Quentin Monnet929bef42021-10-06 12:10:49 +0100247HDR_PROBE := $(shell printf "$(pound)include <linux/types.h>\n struct list_head { int a; }; int main() { return 0; }" | \
Ivan Khoronzhuka833eff2019-10-11 03:28:03 +0300248 $(CC) $(TPROGS_CFLAGS) $(TPROGS_LDFLAGS) -x c - \
249 -o /dev/null 2>/dev/null && echo okay)
Jakub Kicinski07c3bbd2019-06-05 16:47:22 -0700250
251ifeq ($(HDR_PROBE),)
252$(warning WARNING: Detected possible issues with include path.)
253$(warning WARNING: Please install kernel headers locally (make headers_install).)
254endif
255
Taeung Song9778cfd2018-07-26 19:13:44 +0900256BTF_LLC_PROBE := $(shell $(LLC) -march=bpf -mattr=help 2>&1 | grep dwarfris)
257BTF_PAHOLE_PROBE := $(shell $(BTF_PAHOLE) --help 2>&1 | grep BTF)
258BTF_OBJCOPY_PROBE := $(shell $(LLVM_OBJCOPY) --help 2>&1 | grep -i 'usage.*llvm')
Yonghong Song9ce6ae22018-11-19 15:29:17 -0800259BTF_LLVM_PROBE := $(shell echo "int main() { return 0; }" | \
260 $(CLANG) -target bpf -O2 -g -c -x c - -o ./llvm_btf_verify.o; \
Pu Lehui44ce0ac2021-10-21 20:39:13 +0800261 $(LLVM_READELF) -S ./llvm_btf_verify.o | grep BTF; \
Yonghong Song9ce6ae22018-11-19 15:29:17 -0800262 /bin/rm -f ./llvm_btf_verify.o)
Taeung Song9778cfd2018-07-26 19:13:44 +0900263
Toke Høiland-Jørgensen45027892019-12-16 11:38:19 +0100264BPF_EXTRA_CFLAGS += -fno-stack-protector
Yonghong Song9ce6ae22018-11-19 15:29:17 -0800265ifneq ($(BTF_LLVM_PROBE),)
Ivan Khoronzhuk2a560df2019-10-11 03:27:57 +0300266 BPF_EXTRA_CFLAGS += -g
Yonghong Song9ce6ae22018-11-19 15:29:17 -0800267else
Taeung Song9778cfd2018-07-26 19:13:44 +0900268ifneq ($(and $(BTF_LLC_PROBE),$(BTF_PAHOLE_PROBE),$(BTF_OBJCOPY_PROBE)),)
Ivan Khoronzhuk2a560df2019-10-11 03:27:57 +0300269 BPF_EXTRA_CFLAGS += -g
Taeung Song9778cfd2018-07-26 19:13:44 +0900270 LLC_FLAGS += -mattr=dwarfris
271 DWARF2BTF = y
272endif
Yonghong Song9ce6ae22018-11-19 15:29:17 -0800273endif
Jakub Kicinski0ed3cc42019-06-07 10:11:16 -0700274endif
Taeung Song9778cfd2018-07-26 19:13:44 +0900275
Jesper Dangaard Brouerb62a7962016-04-28 14:21:09 +0200276# Trick to allow make to be run from this directory
Jakub Kicinski787360f2018-05-14 22:35:04 -0700277all:
Jesper Dangaard Brouer6cc2c872019-11-10 17:31:16 +0100278 $(MAKE) -C ../../ M=$(CURDIR) BPF_SAMPLES_PATH=$(CURDIR)
Jesper Dangaard Brouerb62a7962016-04-28 14:21:09 +0200279
280clean:
Uwe Kleine-Könige19b7ce2016-11-22 09:30:26 +0100281 $(MAKE) -C ../../ M=$(CURDIR) clean
Toke Høiland-Jørgensenb2e5e932020-01-20 14:06:41 +0100282 @find $(CURDIR) -type f -name '*~' -delete
Quentin Monnet3f7a3312021-10-07 20:44:35 +0100283 @$(RM) -r $(CURDIR)/libbpf $(CURDIR)/bpftool
Jesper Dangaard Brouerb62a7962016-04-28 14:21:09 +0200284
Quentin Monneta60d24e2021-10-07 20:44:36 +0100285$(LIBBPF): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(LIBBPF_OUTPUT)
Jakub Kicinski787360f2018-05-14 22:35:04 -0700286# Fix up variables inherited from Kbuild that tools/ build system won't like
Quentin Monnet3f7a3312021-10-07 20:44:35 +0100287 $(MAKE) -C $(LIBBPF_SRC) RM='rm -rf' EXTRA_CFLAGS="$(TPROGS_CFLAGS)" \
288 LDFLAGS=$(TPROGS_LDFLAGS) srctree=$(BPF_SAMPLES_PATH)/../../ \
289 O= OUTPUT=$(LIBBPF_OUTPUT)/ DESTDIR=$(LIBBPF_DESTDIR) prefix= \
290 $@ install_headers
Mickaël Salaünc25ef6a2018-01-26 01:39:30 +0100291
Kumar Kartikeya Dwivedi384b6b32021-08-21 05:50:00 +0530292BPFTOOLDIR := $(TOOLS_PATH)/bpf/bpftool
Quentin Monnet3f7a3312021-10-07 20:44:35 +0100293BPFTOOL_OUTPUT := $(abspath $(BPF_SAMPLES_PATH))/bpftool
294BPFTOOL := $(BPFTOOL_OUTPUT)/bpftool
295$(BPFTOOL): $(LIBBPF) $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT)
296 $(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../ \
297 OUTPUT=$(BPFTOOL_OUTPUT)/ \
298 LIBBPF_OUTPUT=$(LIBBPF_OUTPUT)/ \
299 LIBBPF_DESTDIR=$(LIBBPF_DESTDIR)/
300
301$(LIBBPF_OUTPUT) $(BPFTOOL_OUTPUT):
302 $(call msg,MKDIR,$@)
303 $(Q)mkdir -p $@
Kumar Kartikeya Dwivedi384b6b32021-08-21 05:50:00 +0530304
David Daney4b7190e2017-06-13 16:49:38 -0700305$(obj)/syscall_nrs.h: $(obj)/syscall_nrs.s FORCE
306 $(call filechk,offsets,__SYSCALL_NRS_H__)
307
Masahiro Yamada2c667d72018-12-22 18:50:34 +0900308targets += syscall_nrs.s
David Daney4b7190e2017-06-13 16:49:38 -0700309clean-files += syscall_nrs.h
310
311FORCE:
312
313
Jesper Dangaard Brouerbdefbbf2016-04-28 14:21:14 +0200314# Verify LLVM compiler tools are available and bpf target is supported by llc
315.PHONY: verify_cmds verify_target_bpf $(CLANG) $(LLC)
Jesper Dangaard Brouer7b01dd52016-04-28 14:20:58 +0200316
Jesper Dangaard Brouerbdefbbf2016-04-28 14:21:14 +0200317verify_cmds: $(CLANG) $(LLC)
318 @for TOOL in $^ ; do \
319 if ! (which -- "$${TOOL}" > /dev/null 2>&1); then \
320 echo "*** ERROR: Cannot find LLVM tool $${TOOL}" ;\
321 exit 1; \
322 else true; fi; \
323 done
Jesper Dangaard Brouer7b01dd52016-04-28 14:20:58 +0200324
Jesper Dangaard Brouerbdefbbf2016-04-28 14:21:14 +0200325verify_target_bpf: verify_cmds
Jesper Dangaard Brouer7b01dd52016-04-28 14:20:58 +0200326 @if ! (${LLC} -march=bpf -mattr=help > /dev/null 2>&1); then \
327 echo "*** ERROR: LLVM (${LLC}) does not support 'bpf' target" ;\
328 echo " NOTICE: LLVM version >= 3.7.1 required" ;\
329 exit 2; \
330 else true; fi
331
Jakub Kicinski787360f2018-05-14 22:35:04 -0700332$(BPF_SAMPLES_PATH)/*.c: verify_target_bpf $(LIBBPF)
333$(src)/*.c: verify_target_bpf $(LIBBPF)
Jesper Dangaard Brouer7b01dd52016-04-28 14:20:58 +0200334
Quentin Monnet3f7a3312021-10-07 20:44:35 +0100335libbpf_hdrs: $(LIBBPF)
Andrii Nakryiko527024f2021-12-01 15:28:22 -0800336$(obj)/$(TRACE_HELPERS) $(obj)/$(CGROUP_HELPERS) $(obj)/$(XDP_SAMPLE): | libbpf_hdrs
Quentin Monnet3f7a3312021-10-07 20:44:35 +0100337
338.PHONY: libbpf_hdrs
339
Kumar Kartikeya Dwivedie531a222021-08-21 05:50:06 +0530340$(obj)/xdp_redirect_cpu_user.o: $(obj)/xdp_redirect_cpu.skel.h
Kumar Kartikeya Dwivedi594a1162021-08-21 05:50:10 +0530341$(obj)/xdp_redirect_map_multi_user.o: $(obj)/xdp_redirect_map_multi.skel.h
Kumar Kartikeya Dwivedibbe65862021-08-21 05:50:08 +0530342$(obj)/xdp_redirect_map_user.o: $(obj)/xdp_redirect_map.skel.h
Kumar Kartikeya Dwivedib926c552021-08-21 05:50:04 +0530343$(obj)/xdp_redirect_user.o: $(obj)/xdp_redirect.skel.h
Kumar Kartikeya Dwivedi6e1051a2021-08-21 05:50:02 +0530344$(obj)/xdp_monitor_user.o: $(obj)/xdp_monitor.skel.h
345
David Daney4b7190e2017-06-13 16:49:38 -0700346$(obj)/tracex5_kern.o: $(obj)/syscall_nrs.h
brakmo187d0732019-03-01 12:38:48 -0800347$(obj)/hbm_out_kern.o: $(src)/hbm.h $(src)/hbm_kern.h
brakmoa1270fe2019-03-01 12:38:49 -0800348$(obj)/hbm.o: $(src)/hbm.h
brakmo71634d72019-07-02 15:09:52 -0700349$(obj)/hbm_edt_kern.o: $(src)/hbm.h $(src)/hbm_kern.h
David Daney4b7190e2017-06-13 16:49:38 -0700350
Andrii Nakryiko527024f2021-12-01 15:28:22 -0800351# Override includes for xdp_sample_user.o because $(srctree)/usr/include in
352# TPROGS_CFLAGS causes conflicts
Alexander Lobakine64fbca2021-12-03 20:50:03 +0100353XDP_SAMPLE_CFLAGS += -Wall -O2 \
Andrii Nakryiko527024f2021-12-01 15:28:22 -0800354 -I$(src)/../../tools/include \
355 -I$(src)/../../tools/include/uapi \
356 -I$(LIBBPF_INCLUDE) \
357 -I$(src)/../../tools/testing/selftests/bpf
358
359$(obj)/$(XDP_SAMPLE): TPROGS_CFLAGS = $(XDP_SAMPLE_CFLAGS)
360$(obj)/$(XDP_SAMPLE): $(src)/xdp_sample_user.h $(src)/xdp_sample_shared.h
361
Ivan Khoronzhuk1d97c6c2019-10-11 03:28:01 +0300362-include $(BPF_SAMPLES_PATH)/Makefile.target
363
Kumar Kartikeya Dwivedi571fa242021-09-28 11:16:08 +0530364VMLINUX_BTF_PATHS ?= $(abspath $(if $(O),$(O)/vmlinux)) \
365 $(abspath $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux)) \
366 $(abspath ./vmlinux)
Kumar Kartikeya Dwivedi384b6b32021-08-21 05:50:00 +0530367VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
368
Kumar Kartikeya Dwivedi384b6b32021-08-21 05:50:00 +0530369$(obj)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL)
370ifeq ($(VMLINUX_H),)
371 $(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
372else
373 $(Q)cp "$(VMLINUX_H)" $@
374endif
375
Kumar Kartikeya Dwivedi571fa242021-09-28 11:16:08 +0530376ifeq ($(VMLINUX_BTF),)
377 $(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)",\
378 build the kernel or set VMLINUX_BTF variable)
379endif
380
Kumar Kartikeya Dwivedi384b6b32021-08-21 05:50:00 +0530381clean-files += vmlinux.h
382
Kumar Kartikeya Dwivedi3f199562021-08-21 05:50:01 +0530383# Get Clang's default includes on this system, as opposed to those seen by
384# '-target bpf'. This fixes "missing" files on some architectures/distros,
385# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
386#
387# Use '-idirafter': Don't interfere with include mechanics except where the
388# build would have failed anyways.
389define get_sys_includes
390$(shell $(1) -v -E - </dev/null 2>&1 \
391 | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
392$(shell $(1) -dM -E - </dev/null | grep '#define __riscv_xlen ' | sed 's/#define /-D/' | sed 's/ /=/')
393endef
394
395CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
396
Kumar Kartikeya Dwivedi79ccf452021-08-21 05:50:05 +0530397$(obj)/xdp_redirect_cpu.bpf.o: $(obj)/xdp_sample.bpf.o
Kumar Kartikeya Dwivedia29b3ca2021-08-21 05:50:09 +0530398$(obj)/xdp_redirect_map_multi.bpf.o: $(obj)/xdp_sample.bpf.o
Kumar Kartikeya Dwivedi54af7692021-08-21 05:50:07 +0530399$(obj)/xdp_redirect_map.bpf.o: $(obj)/xdp_sample.bpf.o
Kumar Kartikeya Dwivedi66fc4ca2021-08-21 05:50:03 +0530400$(obj)/xdp_redirect.bpf.o: $(obj)/xdp_sample.bpf.o
Kumar Kartikeya Dwivedi3f199562021-08-21 05:50:01 +0530401$(obj)/xdp_monitor.bpf.o: $(obj)/xdp_sample.bpf.o
402
403$(obj)/%.bpf.o: $(src)/%.bpf.c $(obj)/vmlinux.h $(src)/xdp_sample.bpf.h $(src)/xdp_sample_shared.h
404 @echo " CLANG-BPF " $@
405 $(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(SRCARCH) \
406 -Wno-compare-distinct-pointer-types -I$(srctree)/include \
407 -I$(srctree)/samples/bpf -I$(srctree)/tools/include \
Quentin Monnet3f7a3312021-10-07 20:44:35 +0100408 -I$(LIBBPF_INCLUDE) $(CLANG_SYS_INCLUDES) \
Kumar Kartikeya Dwivedi3f199562021-08-21 05:50:01 +0530409 -c $(filter %.bpf.c,$^) -o $@
410
Kumar Kartikeya Dwivedia29b3ca2021-08-21 05:50:09 +0530411LINKED_SKELS := xdp_redirect_cpu.skel.h xdp_redirect_map_multi.skel.h \
412 xdp_redirect_map.skel.h xdp_redirect.skel.h xdp_monitor.skel.h
Kumar Kartikeya Dwivedi3f199562021-08-21 05:50:01 +0530413clean-files += $(LINKED_SKELS)
414
Kumar Kartikeya Dwivedi79ccf452021-08-21 05:50:05 +0530415xdp_redirect_cpu.skel.h-deps := xdp_redirect_cpu.bpf.o xdp_sample.bpf.o
Kumar Kartikeya Dwivedia29b3ca2021-08-21 05:50:09 +0530416xdp_redirect_map_multi.skel.h-deps := xdp_redirect_map_multi.bpf.o xdp_sample.bpf.o
Kumar Kartikeya Dwivedi54af7692021-08-21 05:50:07 +0530417xdp_redirect_map.skel.h-deps := xdp_redirect_map.bpf.o xdp_sample.bpf.o
Kumar Kartikeya Dwivedi66fc4ca2021-08-21 05:50:03 +0530418xdp_redirect.skel.h-deps := xdp_redirect.bpf.o xdp_sample.bpf.o
Kumar Kartikeya Dwivedi3f199562021-08-21 05:50:01 +0530419xdp_monitor.skel.h-deps := xdp_monitor.bpf.o xdp_sample.bpf.o
420
421LINKED_BPF_SRCS := $(patsubst %.bpf.o,%.bpf.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
422
423BPF_SRCS_LINKED := $(notdir $(wildcard $(src)/*.bpf.c))
424BPF_OBJS_LINKED := $(patsubst %.bpf.c,$(obj)/%.bpf.o, $(BPF_SRCS_LINKED))
425BPF_SKELS_LINKED := $(addprefix $(obj)/,$(LINKED_SKELS))
426
427$(BPF_SKELS_LINKED): $(BPF_OBJS_LINKED) $(BPFTOOL)
428 @echo " BPF GEN-OBJ " $(@:.skel.h=)
429 $(Q)$(BPFTOOL) gen object $(@:.skel.h=.lbpf.o) $(addprefix $(obj)/,$($(@F)-deps))
430 @echo " BPF GEN-SKEL" $(@:.skel.h=)
431 $(Q)$(BPFTOOL) gen skeleton $(@:.skel.h=.lbpf.o) name $(notdir $(@:.skel.h=)) > $@
432
Naveen N. Rao128d1512016-04-04 22:31:33 +0530433# asm/sysreg.h - inline assembly used by it is incompatible with llvm.
434# But, there is no easy way to fix it, so just exclude it since it is
Yang Shi30b50aa2015-11-12 14:07:46 -0800435# useless for BPF samples.
Yonghong Song9618bde2020-10-05 21:34:26 -0700436# below we use long chain of commands, clang | opt | llvm-dis | llc,
437# to generate final object file. 'clang' compiles the source into IR
438# with native target, e.g., x64, arm64, etc. 'opt' does bpf CORE IR builtin
439# processing (llvm12) and IR optimizations. 'llvm-dis' converts
440# 'opt' output to IR, and finally 'llc' generates bpf byte code.
Brenden Blancob88c06e2015-05-11 21:25:51 -0700441$(obj)/%.o: $(src)/%.c
Jakub Kicinski768759e2018-05-14 22:35:06 -0700442 @echo " CLANG-bpf " $@
Ivan Khoronzhuk2a560df2019-10-11 03:27:57 +0300443 $(Q)$(CLANG) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(BPF_EXTRA_CFLAGS) \
444 -I$(obj) -I$(srctree)/tools/testing/selftests/bpf/ \
Quentin Monnet3f7a3312021-10-07 20:44:35 +0100445 -I$(LIBBPF_INCLUDE) \
Alexei Starovoitovb1ae32d2018-05-13 12:32:22 -0700446 -D__KERNEL__ -D__BPF_TRACING__ -Wno-unused-value -Wno-pointer-sign \
Ilya Leoshkevich81f522f2019-07-15 11:11:03 +0200447 -D__TARGET_ARCH_$(SRCARCH) -Wno-compare-distinct-pointer-types \
Alexei Starovoitov69a9d092016-12-01 18:31:12 -0800448 -Wno-gnu-variable-sized-type-not-at-end \
449 -Wno-address-of-packed-member -Wno-tautological-compare \
Abhijit Ayarekar9db95832017-10-13 12:24:06 -0700450 -Wno-unknown-warning-option $(CLANG_ARCH_ARGS) \
Toke Høiland-Jørgensen5a0ae982021-07-05 12:38:41 +0200451 -fno-asynchronous-unwind-tables \
Yonghong Song6bf3bbe2019-01-12 15:04:30 -0800452 -I$(srctree)/samples/bpf/ -include asm_goto_workaround.h \
Yonghong Song9618bde2020-10-05 21:34:26 -0700453 -O2 -emit-llvm -Xclang -disable-llvm-passes -c $< -o - | \
454 $(OPT) -O2 -mtriple=bpf-pc-linux | $(LLVM_DIS) | \
455 $(LLC) -march=bpf $(LLC_FLAGS) -filetype=obj -o $@
Taeung Song9778cfd2018-07-26 19:13:44 +0900456ifeq ($(DWARF2BTF),y)
457 $(BTF_PAHOLE) -J $@
458endif