blob: 4dc20be5fb96a63ccf01b19550518a51ba878b3b [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
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -07006# List of programs to build
Ivan Khoronzhuk1d97c6c2019-10-11 03:28:01 +03007tprogs-y := test_lru_dist
8tprogs-y += sock_example
9tprogs-y += fds_example
10tprogs-y += sockex1
11tprogs-y += sockex2
12tprogs-y += sockex3
13tprogs-y += tracex1
14tprogs-y += tracex2
15tprogs-y += tracex3
16tprogs-y += tracex4
17tprogs-y += tracex5
18tprogs-y += tracex6
19tprogs-y += tracex7
20tprogs-y += test_probe_write_user
21tprogs-y += trace_output
22tprogs-y += lathist
23tprogs-y += offwaketime
24tprogs-y += spintest
25tprogs-y += map_perf_test
26tprogs-y += test_overhead
27tprogs-y += test_cgrp2_array_pin
28tprogs-y += test_cgrp2_attach
29tprogs-y += test_cgrp2_sock
30tprogs-y += test_cgrp2_sock2
31tprogs-y += xdp1
32tprogs-y += xdp2
33tprogs-y += xdp_router_ipv4
34tprogs-y += test_current_task_under_cgroup
35tprogs-y += trace_event
36tprogs-y += sampleip
37tprogs-y += tc_l2_redirect
38tprogs-y += lwt_len_hist
39tprogs-y += xdp_tx_iptunnel
40tprogs-y += test_map_in_map
Prashant Bhole5984dc62019-12-16 16:16:19 +090041tprogs-y += per_socket_stats_example
Ivan Khoronzhuk1d97c6c2019-10-11 03:28:01 +030042tprogs-y += xdp_rxq_info
43tprogs-y += syscall_tp
44tprogs-y += cpustat
45tprogs-y += xdp_adjust_tail
46tprogs-y += xdpsock
Mariusz Dudek3627d972020-12-03 10:05:46 +010047tprogs-y += xdpsock_ctrl_proc
Cristian Dumitrescu35149b22020-08-28 10:26:28 +020048tprogs-y += xsk_fwd
Ivan Khoronzhuk1d97c6c2019-10-11 03:28:01 +030049tprogs-y += xdp_fwd
50tprogs-y += task_fd_query
51tprogs-y += xdp_sample_pkts
52tprogs-y += ibumad
53tprogs-y += hbm
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -070054
Kumar Kartikeya Dwivedie531a222021-08-21 05:50:06 +053055tprogs-y += xdp_redirect_cpu
Kumar Kartikeya Dwivedi594a1162021-08-21 05:50:10 +053056tprogs-y += xdp_redirect_map_multi
Kumar Kartikeya Dwivedibbe65862021-08-21 05:50:08 +053057tprogs-y += xdp_redirect_map
Kumar Kartikeya Dwivedib926c552021-08-21 05:50:04 +053058tprogs-y += xdp_redirect
Kumar Kartikeya Dwivedi6e1051a2021-08-21 05:50:02 +053059tprogs-y += xdp_monitor
60
Joe Stringer43371c82016-12-14 14:43:39 -080061# Libbpf dependencies
Jakub Kicinski787360f2018-05-14 22:35:04 -070062LIBBPF = $(TOOLS_PATH)/lib/bpf/libbpf.a
63
Roman Gushchin9d1f1592017-11-05 08:15:33 -050064CGROUP_HELPERS := ../../tools/testing/selftests/bpf/cgroup_helpers.o
Yonghong Song28dbf862018-04-28 22:28:13 -070065TRACE_HELPERS := ../../tools/testing/selftests/bpf/trace_helpers.o
Kumar Kartikeya Dwivedi6e1051a2021-08-21 05:50:02 +053066XDP_SAMPLE := xdp_sample_user.o
Joe Stringer43371c82016-12-14 14:43:39 -080067
Jakub Kicinski1a9b2682019-02-27 19:04:13 -080068fds_example-objs := fds_example.o
69sockex1-objs := sockex1_user.o
70sockex2-objs := sockex2_user.o
Daniel T. Leebc1a8592020-05-16 13:06:06 +090071sockex3-objs := sockex3_user.o
Daniel T. Lee63841bc2020-05-16 13:06:05 +090072tracex1-objs := tracex1_user.o $(TRACE_HELPERS)
73tracex2-objs := tracex2_user.o
74tracex3-objs := tracex3_user.o
75tracex4-objs := tracex4_user.o
Daniel T. Leebc1a8592020-05-16 13:06:06 +090076tracex5-objs := tracex5_user.o $(TRACE_HELPERS)
Daniel T. Lee63841bc2020-05-16 13:06:05 +090077tracex6-objs := tracex6_user.o
78tracex7-objs := tracex7_user.o
Daniel T. Lee3677d0a2020-08-23 17:53:33 +090079test_probe_write_user-objs := test_probe_write_user_user.o
Daniel T. Leeceb5dea2020-11-24 09:03:10 +000080trace_output-objs := trace_output_user.o
Daniel T. Lee3677d0a2020-08-23 17:53:33 +090081lathist-objs := lathist_user.o
Daniel T. Leef0c328f2020-08-23 17:53:34 +090082offwaketime-objs := offwaketime_user.o $(TRACE_HELPERS)
Daniel T. Lee3677d0a2020-08-23 17:53:33 +090083spintest-objs := spintest_user.o $(TRACE_HELPERS)
Daniel T. Lee35a8b6d2020-08-23 17:53:32 +090084map_perf_test-objs := map_perf_test_user.o
Daniel T. Leec6497df2020-11-24 09:03:08 +000085test_overhead-objs := test_overhead_user.o
Jakub Kicinski0cc54db2018-05-14 22:35:05 -070086test_cgrp2_array_pin-objs := test_cgrp2_array_pin.o
87test_cgrp2_attach-objs := test_cgrp2_attach.o
Jakub Kicinski0cc54db2018-05-14 22:35:05 -070088test_cgrp2_sock-objs := test_cgrp2_sock.o
Daniel T. Leed89af132020-11-24 09:03:05 +000089test_cgrp2_sock2-objs := test_cgrp2_sock2.o
Jakub Kicinski0cc54db2018-05-14 22:35:05 -070090xdp1-objs := xdp1_user.o
Brenden Blanco764cbcce2016-07-19 12:16:57 -070091# reuse xdp1 source intentionally
Jakub Kicinski0cc54db2018-05-14 22:35:05 -070092xdp2-objs := xdp1_user.o
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +010093xdp_router_ipv4-objs := xdp_router_ipv4_user.o
Daniel T. Lee3677d0a2020-08-23 17:53:33 +090094test_current_task_under_cgroup-objs := $(CGROUP_HELPERS) \
Sargun Dhillon9e6e60e2016-08-12 08:57:04 -070095 test_current_task_under_cgroup_user.o
Daniel T. Leeaa5e2af2020-03-21 19:04:24 +090096trace_event-objs := trace_event_user.o $(TRACE_HELPERS)
97sampleip-objs := sampleip_user.o $(TRACE_HELPERS)
Daniel T. Leeceb5dea2020-11-24 09:03:10 +000098tc_l2_redirect-objs := tc_l2_redirect_user.o
99lwt_len_hist-objs := lwt_len_hist_user.o
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100100xdp_tx_iptunnel-objs := xdp_tx_iptunnel_user.o
Daniel T. Lee88795b42020-07-08 03:48:53 +0900101test_map_in_map-objs := test_map_in_map_user.o
Jakub Kicinski0cc54db2018-05-14 22:35:05 -0700102per_socket_stats_example-objs := cookie_uid_helper_example.o
Jakub Kicinski0cc54db2018-05-14 22:35:05 -0700103xdp_rxq_info-objs := xdp_rxq_info_user.o
Daniel T. Leef0c328f2020-08-23 17:53:34 +0900104syscall_tp-objs := syscall_tp_user.o
105cpustat-objs := cpustat_user.o
Jakub Kicinski0cc54db2018-05-14 22:35:05 -0700106xdp_adjust_tail-objs := xdp_adjust_tail_user.o
Jakub Kicinski67481822018-07-26 14:32:21 -0700107xdpsock-objs := xdpsock_user.o
Mariusz Dudek3627d972020-12-03 10:05:46 +0100108xdpsock_ctrl_proc-objs := xdpsock_ctrl_proc.o
Cristian Dumitrescu35149b22020-08-28 10:26:28 +0200109xsk_fwd-objs := xsk_fwd.o
Jakub Kicinskie1a40ef2018-07-26 14:32:20 -0700110xdp_fwd-objs := xdp_fwd_user.o
Daniel T. Lee4fe66412020-11-24 09:03:06 +0000111task_fd_query-objs := task_fd_query_user.o $(TRACE_HELPERS)
Daniel T. Leeceb5dea2020-11-24 09:03:10 +0000112xdp_sample_pkts-objs := xdp_sample_pkts_user.o
Daniel T. Lee763af202020-11-24 09:03:07 +0000113ibumad-objs := ibumad_user.o
Daniel T. Leec5815ac2020-11-24 09:03:04 +0000114hbm-objs := hbm.o $(CGROUP_HELPERS)
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -0700115
Kumar Kartikeya Dwivedi594a1162021-08-21 05:50:10 +0530116xdp_redirect_map_multi-objs := xdp_redirect_map_multi_user.o $(XDP_SAMPLE)
Kumar Kartikeya Dwivedie531a222021-08-21 05:50:06 +0530117xdp_redirect_cpu-objs := xdp_redirect_cpu_user.o $(XDP_SAMPLE)
Kumar Kartikeya Dwivedibbe65862021-08-21 05:50:08 +0530118xdp_redirect_map-objs := xdp_redirect_map_user.o $(XDP_SAMPLE)
Kumar Kartikeya Dwivedib926c552021-08-21 05:50:04 +0530119xdp_redirect-objs := xdp_redirect_user.o $(XDP_SAMPLE)
Kumar Kartikeya Dwivedi6e1051a2021-08-21 05:50:02 +0530120xdp_monitor-objs := xdp_monitor_user.o $(XDP_SAMPLE)
121
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -0700122# Tell kbuild to always build the programs
Masahiro Yamada5f2fb522020-02-02 01:49:24 +0900123always-y := $(tprogs-y)
124always-y += sockex1_kern.o
125always-y += sockex2_kern.o
126always-y += sockex3_kern.o
127always-y += tracex1_kern.o
128always-y += tracex2_kern.o
129always-y += tracex3_kern.o
130always-y += tracex4_kern.o
131always-y += tracex5_kern.o
132always-y += tracex6_kern.o
133always-y += tracex7_kern.o
134always-y += sock_flags_kern.o
135always-y += test_probe_write_user_kern.o
136always-y += trace_output_kern.o
137always-y += tcbpf1_kern.o
138always-y += tc_l2_redirect_kern.o
139always-y += lathist_kern.o
140always-y += offwaketime_kern.o
141always-y += spintest_kern.o
142always-y += map_perf_test_kern.o
143always-y += test_overhead_tp_kern.o
144always-y += test_overhead_raw_tp_kern.o
145always-y += test_overhead_kprobe_kern.o
146always-y += parse_varlen.o parse_simple.o parse_ldabs.o
147always-y += test_cgrp2_tc_kern.o
148always-y += xdp1_kern.o
149always-y += xdp2_kern.o
150always-y += xdp_router_ipv4_kern.o
151always-y += test_current_task_under_cgroup_kern.o
152always-y += trace_event_kern.o
153always-y += sampleip_kern.o
154always-y += lwt_len_hist_kern.o
155always-y += xdp_tx_iptunnel_kern.o
156always-y += test_map_in_map_kern.o
157always-y += tcp_synrto_kern.o
158always-y += tcp_rwnd_kern.o
159always-y += tcp_bufs_kern.o
160always-y += tcp_cong_kern.o
161always-y += tcp_iw_kern.o
162always-y += tcp_clamp_kern.o
163always-y += tcp_basertt_kern.o
164always-y += tcp_tos_reflect_kern.o
165always-y += tcp_dumpstats_kern.o
Masahiro Yamada5f2fb522020-02-02 01:49:24 +0900166always-y += xdp_rxq_info_kern.o
167always-y += xdp2skb_meta_kern.o
168always-y += syscall_tp_kern.o
169always-y += cpustat_kern.o
170always-y += xdp_adjust_tail_kern.o
171always-y += xdp_fwd_kern.o
172always-y += task_fd_query_kern.o
173always-y += xdp_sample_pkts_kern.o
174always-y += ibumad_kern.o
175always-y += hbm_out_kern.o
176always-y += hbm_edt_kern.o
177always-y += xdpsock_kern.o
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -0700178
Ivan Khoronzhuk0e865ae2019-10-11 03:27:58 +0300179ifeq ($(ARCH), arm)
180# Strip all except -D__LINUX_ARM_ARCH__ option needed to handle linux
181# headers when arm instruction set identification is requested.
182ARM_ARCH_SELECTOR := $(filter -D__LINUX_ARM_ARCH__%, $(KBUILD_CFLAGS))
183BPF_EXTRA_CFLAGS := $(ARM_ARCH_SELECTOR)
Ivan Khoronzhuk1d97c6c2019-10-11 03:28:01 +0300184TPROGS_CFLAGS += $(ARM_ARCH_SELECTOR)
Ivan Khoronzhuk0e865ae2019-10-11 03:27:58 +0300185endif
186
Tiezhu Yang190d1c92021-01-25 13:05:46 +0800187ifeq ($(ARCH), mips)
188TPROGS_CFLAGS += -D__SANE_USERSPACE_TYPES__
Tiezhu Yang058107a2021-01-26 22:05:25 +0800189ifdef CONFIG_MACH_LOONGSON64
190BPF_EXTRA_CFLAGS += -I$(srctree)/arch/mips/include/asm/mach-loongson64
191BPF_EXTRA_CFLAGS += -I$(srctree)/arch/mips/include/asm/mach-generic
192endif
Tiezhu Yang190d1c92021-01-25 13:05:46 +0800193endif
194
Ivan Khoronzhuk10cb3d82019-10-11 03:28:02 +0300195TPROGS_CFLAGS += -Wall -O2
196TPROGS_CFLAGS += -Wmissing-prototypes
197TPROGS_CFLAGS += -Wstrict-prototypes
198
Ivan Khoronzhuk1d97c6c2019-10-11 03:28:01 +0300199TPROGS_CFLAGS += -I$(objtree)/usr/include
Ivan Khoronzhuk1d97c6c2019-10-11 03:28:01 +0300200TPROGS_CFLAGS += -I$(srctree)/tools/testing/selftests/bpf/
201TPROGS_CFLAGS += -I$(srctree)/tools/lib/
202TPROGS_CFLAGS += -I$(srctree)/tools/include
203TPROGS_CFLAGS += -I$(srctree)/tools/perf
David S. Miller14684b92019-11-09 11:04:37 -0800204TPROGS_CFLAGS += -DHAVE_ATTR_TEST=0
Ivan Khoronzhuk1d97c6c2019-10-11 03:28:01 +0300205
Ivan Khoronzhukb2327c12019-10-11 03:28:07 +0300206ifdef SYSROOT
207TPROGS_CFLAGS += --sysroot=$(SYSROOT)
208TPROGS_LDFLAGS := -L$(SYSROOT)/usr/lib
209endif
210
Toke Høiland-Jørgensen5615ed42019-12-16 11:24:05 +0100211TPROGS_LDLIBS += $(LIBBPF) -lelf -lz
Ivan Khoronzhuk1d97c6c2019-10-11 03:28:01 +0300212TPROGLDLIBS_tracex4 += -lrt
213TPROGLDLIBS_trace_output += -lrt
214TPROGLDLIBS_map_perf_test += -lrt
215TPROGLDLIBS_test_overhead += -lrt
Mariusz Dudek3627d972020-12-03 10:05:46 +0100216TPROGLDLIBS_xdpsock += -pthread -lcap
Cristian Dumitrescu35149b22020-08-28 10:26:28 +0200217TPROGLDLIBS_xsk_fwd += -pthread
Ivan Khoronzhuk1d97c6c2019-10-11 03:28:01 +0300218
Jesper Dangaard Brouerbdefbbf2016-04-28 14:21:14 +0200219# Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline:
Tiezhu Yang628add72021-01-22 09:39:44 +0800220# 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 +0200221LLC ?= llc
Jesper Dangaard Brouerbdefbbf2016-04-28 14:21:14 +0200222CLANG ?= clang
Yonghong Song9618bde2020-10-05 21:34:26 -0700223OPT ?= opt
224LLVM_DIS ?= llvm-dis
Taeung Song9778cfd2018-07-26 19:13:44 +0900225LLVM_OBJCOPY ?= llvm-objcopy
226BTF_PAHOLE ?= pahole
Jesper Dangaard Brouer6ccfba72016-04-28 14:20:53 +0200227
Joel Fernandes876e88e2017-09-20 09:11:57 -0700228# Detect that we're cross compiling and use the cross compiler
229ifdef CROSS_COMPILE
Ivan Khoronzhuk518c1342019-10-11 03:27:56 +0300230CLANG_ARCH_ARGS = --target=$(notdir $(CROSS_COMPILE:%-=%))
Joel Fernandes876e88e2017-09-20 09:11:57 -0700231endif
232
Jakub Kicinski0ed3cc42019-06-07 10:11:16 -0700233# Don't evaluate probes and warnings if we need to run make recursively
234ifneq ($(src),)
Ivan Khoronzhukcdd5b2d2019-10-11 03:27:54 +0300235HDR_PROBE := $(shell printf "\#include <linux/types.h>\n struct list_head { int a; }; int main() { return 0; }" | \
Ivan Khoronzhuka833eff2019-10-11 03:28:03 +0300236 $(CC) $(TPROGS_CFLAGS) $(TPROGS_LDFLAGS) -x c - \
237 -o /dev/null 2>/dev/null && echo okay)
Jakub Kicinski07c3bbd2019-06-05 16:47:22 -0700238
239ifeq ($(HDR_PROBE),)
240$(warning WARNING: Detected possible issues with include path.)
241$(warning WARNING: Please install kernel headers locally (make headers_install).)
242endif
243
Taeung Song9778cfd2018-07-26 19:13:44 +0900244BTF_LLC_PROBE := $(shell $(LLC) -march=bpf -mattr=help 2>&1 | grep dwarfris)
245BTF_PAHOLE_PROBE := $(shell $(BTF_PAHOLE) --help 2>&1 | grep BTF)
246BTF_OBJCOPY_PROBE := $(shell $(LLVM_OBJCOPY) --help 2>&1 | grep -i 'usage.*llvm')
Yonghong Song9ce6ae22018-11-19 15:29:17 -0800247BTF_LLVM_PROBE := $(shell echo "int main() { return 0; }" | \
248 $(CLANG) -target bpf -O2 -g -c -x c - -o ./llvm_btf_verify.o; \
249 readelf -S ./llvm_btf_verify.o | grep BTF; \
250 /bin/rm -f ./llvm_btf_verify.o)
Taeung Song9778cfd2018-07-26 19:13:44 +0900251
Toke Høiland-Jørgensen45027892019-12-16 11:38:19 +0100252BPF_EXTRA_CFLAGS += -fno-stack-protector
Yonghong Song9ce6ae22018-11-19 15:29:17 -0800253ifneq ($(BTF_LLVM_PROBE),)
Ivan Khoronzhuk2a560df2019-10-11 03:27:57 +0300254 BPF_EXTRA_CFLAGS += -g
Yonghong Song9ce6ae22018-11-19 15:29:17 -0800255else
Taeung Song9778cfd2018-07-26 19:13:44 +0900256ifneq ($(and $(BTF_LLC_PROBE),$(BTF_PAHOLE_PROBE),$(BTF_OBJCOPY_PROBE)),)
Ivan Khoronzhuk2a560df2019-10-11 03:27:57 +0300257 BPF_EXTRA_CFLAGS += -g
Taeung Song9778cfd2018-07-26 19:13:44 +0900258 LLC_FLAGS += -mattr=dwarfris
259 DWARF2BTF = y
260endif
Yonghong Song9ce6ae22018-11-19 15:29:17 -0800261endif
Jakub Kicinski0ed3cc42019-06-07 10:11:16 -0700262endif
Taeung Song9778cfd2018-07-26 19:13:44 +0900263
Jesper Dangaard Brouerb62a7962016-04-28 14:21:09 +0200264# Trick to allow make to be run from this directory
Jakub Kicinski787360f2018-05-14 22:35:04 -0700265all:
Jesper Dangaard Brouer6cc2c872019-11-10 17:31:16 +0100266 $(MAKE) -C ../../ M=$(CURDIR) BPF_SAMPLES_PATH=$(CURDIR)
Jesper Dangaard Brouerb62a7962016-04-28 14:21:09 +0200267
268clean:
Uwe Kleine-Könige19b7ce2016-11-22 09:30:26 +0100269 $(MAKE) -C ../../ M=$(CURDIR) clean
Toke Høiland-Jørgensenb2e5e932020-01-20 14:06:41 +0100270 @find $(CURDIR) -type f -name '*~' -delete
Jesper Dangaard Brouerb62a7962016-04-28 14:21:09 +0200271
Mickaël Salaünc25ef6a2018-01-26 01:39:30 +0100272$(LIBBPF): FORCE
Jakub Kicinski787360f2018-05-14 22:35:04 -0700273# Fix up variables inherited from Kbuild that tools/ build system won't like
Ivan Khoronzhukd8ceae92019-10-11 03:28:06 +0300274 $(MAKE) -C $(dir $@) RM='rm -rf' EXTRA_CFLAGS="$(TPROGS_CFLAGS)" \
275 LDFLAGS=$(TPROGS_LDFLAGS) srctree=$(BPF_SAMPLES_PATH)/../../ O=
Mickaël Salaünc25ef6a2018-01-26 01:39:30 +0100276
Kumar Kartikeya Dwivedi384b6b32021-08-21 05:50:00 +0530277BPFTOOLDIR := $(TOOLS_PATH)/bpf/bpftool
278BPFTOOL := $(BPFTOOLDIR)/bpftool
279$(BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile)
280 $(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../
281
David Daney4b7190e2017-06-13 16:49:38 -0700282$(obj)/syscall_nrs.h: $(obj)/syscall_nrs.s FORCE
283 $(call filechk,offsets,__SYSCALL_NRS_H__)
284
Masahiro Yamada2c667d72018-12-22 18:50:34 +0900285targets += syscall_nrs.s
David Daney4b7190e2017-06-13 16:49:38 -0700286clean-files += syscall_nrs.h
287
288FORCE:
289
290
Jesper Dangaard Brouerbdefbbf2016-04-28 14:21:14 +0200291# Verify LLVM compiler tools are available and bpf target is supported by llc
292.PHONY: verify_cmds verify_target_bpf $(CLANG) $(LLC)
Jesper Dangaard Brouer7b01dd52016-04-28 14:20:58 +0200293
Jesper Dangaard Brouerbdefbbf2016-04-28 14:21:14 +0200294verify_cmds: $(CLANG) $(LLC)
295 @for TOOL in $^ ; do \
296 if ! (which -- "$${TOOL}" > /dev/null 2>&1); then \
297 echo "*** ERROR: Cannot find LLVM tool $${TOOL}" ;\
298 exit 1; \
299 else true; fi; \
300 done
Jesper Dangaard Brouer7b01dd52016-04-28 14:20:58 +0200301
Jesper Dangaard Brouerbdefbbf2016-04-28 14:21:14 +0200302verify_target_bpf: verify_cmds
Jesper Dangaard Brouer7b01dd52016-04-28 14:20:58 +0200303 @if ! (${LLC} -march=bpf -mattr=help > /dev/null 2>&1); then \
304 echo "*** ERROR: LLVM (${LLC}) does not support 'bpf' target" ;\
305 echo " NOTICE: LLVM version >= 3.7.1 required" ;\
306 exit 2; \
307 else true; fi
308
Jakub Kicinski787360f2018-05-14 22:35:04 -0700309$(BPF_SAMPLES_PATH)/*.c: verify_target_bpf $(LIBBPF)
310$(src)/*.c: verify_target_bpf $(LIBBPF)
Jesper Dangaard Brouer7b01dd52016-04-28 14:20:58 +0200311
Kumar Kartikeya Dwivedie531a222021-08-21 05:50:06 +0530312$(obj)/xdp_redirect_cpu_user.o: $(obj)/xdp_redirect_cpu.skel.h
Kumar Kartikeya Dwivedi594a1162021-08-21 05:50:10 +0530313$(obj)/xdp_redirect_map_multi_user.o: $(obj)/xdp_redirect_map_multi.skel.h
Kumar Kartikeya Dwivedibbe65862021-08-21 05:50:08 +0530314$(obj)/xdp_redirect_map_user.o: $(obj)/xdp_redirect_map.skel.h
Kumar Kartikeya Dwivedib926c552021-08-21 05:50:04 +0530315$(obj)/xdp_redirect_user.o: $(obj)/xdp_redirect.skel.h
Kumar Kartikeya Dwivedi6e1051a2021-08-21 05:50:02 +0530316$(obj)/xdp_monitor_user.o: $(obj)/xdp_monitor.skel.h
317
David Daney4b7190e2017-06-13 16:49:38 -0700318$(obj)/tracex5_kern.o: $(obj)/syscall_nrs.h
brakmo187d0732019-03-01 12:38:48 -0800319$(obj)/hbm_out_kern.o: $(src)/hbm.h $(src)/hbm_kern.h
brakmoa1270fe2019-03-01 12:38:49 -0800320$(obj)/hbm.o: $(src)/hbm.h
brakmo71634d72019-07-02 15:09:52 -0700321$(obj)/hbm_edt_kern.o: $(src)/hbm.h $(src)/hbm_kern.h
David Daney4b7190e2017-06-13 16:49:38 -0700322
Ivan Khoronzhuk1d97c6c2019-10-11 03:28:01 +0300323-include $(BPF_SAMPLES_PATH)/Makefile.target
324
Kumar Kartikeya Dwivedi384b6b32021-08-21 05:50:00 +0530325VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \
326 $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \
327 ../../../../vmlinux \
328 /sys/kernel/btf/vmlinux \
329 /boot/vmlinux-$(shell uname -r)
330VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
331
332ifeq ($(VMLINUX_BTF),)
333$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)")
334endif
335
336$(obj)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL)
337ifeq ($(VMLINUX_H),)
338 $(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
339else
340 $(Q)cp "$(VMLINUX_H)" $@
341endif
342
343clean-files += vmlinux.h
344
Kumar Kartikeya Dwivedi3f199562021-08-21 05:50:01 +0530345# Get Clang's default includes on this system, as opposed to those seen by
346# '-target bpf'. This fixes "missing" files on some architectures/distros,
347# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
348#
349# Use '-idirafter': Don't interfere with include mechanics except where the
350# build would have failed anyways.
351define get_sys_includes
352$(shell $(1) -v -E - </dev/null 2>&1 \
353 | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
354$(shell $(1) -dM -E - </dev/null | grep '#define __riscv_xlen ' | sed 's/#define /-D/' | sed 's/ /=/')
355endef
356
357CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
358
Kumar Kartikeya Dwivedi79ccf452021-08-21 05:50:05 +0530359$(obj)/xdp_redirect_cpu.bpf.o: $(obj)/xdp_sample.bpf.o
Kumar Kartikeya Dwivedia29b3ca2021-08-21 05:50:09 +0530360$(obj)/xdp_redirect_map_multi.bpf.o: $(obj)/xdp_sample.bpf.o
Kumar Kartikeya Dwivedi54af7692021-08-21 05:50:07 +0530361$(obj)/xdp_redirect_map.bpf.o: $(obj)/xdp_sample.bpf.o
Kumar Kartikeya Dwivedi66fc4ca2021-08-21 05:50:03 +0530362$(obj)/xdp_redirect.bpf.o: $(obj)/xdp_sample.bpf.o
Kumar Kartikeya Dwivedi3f199562021-08-21 05:50:01 +0530363$(obj)/xdp_monitor.bpf.o: $(obj)/xdp_sample.bpf.o
364
365$(obj)/%.bpf.o: $(src)/%.bpf.c $(obj)/vmlinux.h $(src)/xdp_sample.bpf.h $(src)/xdp_sample_shared.h
366 @echo " CLANG-BPF " $@
367 $(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(SRCARCH) \
368 -Wno-compare-distinct-pointer-types -I$(srctree)/include \
369 -I$(srctree)/samples/bpf -I$(srctree)/tools/include \
370 -I$(srctree)/tools/lib $(CLANG_SYS_INCLUDES) \
371 -c $(filter %.bpf.c,$^) -o $@
372
Kumar Kartikeya Dwivedia29b3ca2021-08-21 05:50:09 +0530373LINKED_SKELS := xdp_redirect_cpu.skel.h xdp_redirect_map_multi.skel.h \
374 xdp_redirect_map.skel.h xdp_redirect.skel.h xdp_monitor.skel.h
Kumar Kartikeya Dwivedi3f199562021-08-21 05:50:01 +0530375clean-files += $(LINKED_SKELS)
376
Kumar Kartikeya Dwivedi79ccf452021-08-21 05:50:05 +0530377xdp_redirect_cpu.skel.h-deps := xdp_redirect_cpu.bpf.o xdp_sample.bpf.o
Kumar Kartikeya Dwivedia29b3ca2021-08-21 05:50:09 +0530378xdp_redirect_map_multi.skel.h-deps := xdp_redirect_map_multi.bpf.o xdp_sample.bpf.o
Kumar Kartikeya Dwivedi54af7692021-08-21 05:50:07 +0530379xdp_redirect_map.skel.h-deps := xdp_redirect_map.bpf.o xdp_sample.bpf.o
Kumar Kartikeya Dwivedi66fc4ca2021-08-21 05:50:03 +0530380xdp_redirect.skel.h-deps := xdp_redirect.bpf.o xdp_sample.bpf.o
Kumar Kartikeya Dwivedi3f199562021-08-21 05:50:01 +0530381xdp_monitor.skel.h-deps := xdp_monitor.bpf.o xdp_sample.bpf.o
382
383LINKED_BPF_SRCS := $(patsubst %.bpf.o,%.bpf.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
384
385BPF_SRCS_LINKED := $(notdir $(wildcard $(src)/*.bpf.c))
386BPF_OBJS_LINKED := $(patsubst %.bpf.c,$(obj)/%.bpf.o, $(BPF_SRCS_LINKED))
387BPF_SKELS_LINKED := $(addprefix $(obj)/,$(LINKED_SKELS))
388
389$(BPF_SKELS_LINKED): $(BPF_OBJS_LINKED) $(BPFTOOL)
390 @echo " BPF GEN-OBJ " $(@:.skel.h=)
391 $(Q)$(BPFTOOL) gen object $(@:.skel.h=.lbpf.o) $(addprefix $(obj)/,$($(@F)-deps))
392 @echo " BPF GEN-SKEL" $(@:.skel.h=)
393 $(Q)$(BPFTOOL) gen skeleton $(@:.skel.h=.lbpf.o) name $(notdir $(@:.skel.h=)) > $@
394
Naveen N. Rao128d1512016-04-04 22:31:33 +0530395# asm/sysreg.h - inline assembly used by it is incompatible with llvm.
396# But, there is no easy way to fix it, so just exclude it since it is
Yang Shi30b50aa2015-11-12 14:07:46 -0800397# useless for BPF samples.
Yonghong Song9618bde2020-10-05 21:34:26 -0700398# below we use long chain of commands, clang | opt | llvm-dis | llc,
399# to generate final object file. 'clang' compiles the source into IR
400# with native target, e.g., x64, arm64, etc. 'opt' does bpf CORE IR builtin
401# processing (llvm12) and IR optimizations. 'llvm-dis' converts
402# 'opt' output to IR, and finally 'llc' generates bpf byte code.
Brenden Blancob88c06e2015-05-11 21:25:51 -0700403$(obj)/%.o: $(src)/%.c
Jakub Kicinski768759e2018-05-14 22:35:06 -0700404 @echo " CLANG-bpf " $@
Ivan Khoronzhuk2a560df2019-10-11 03:27:57 +0300405 $(Q)$(CLANG) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(BPF_EXTRA_CFLAGS) \
406 -I$(obj) -I$(srctree)/tools/testing/selftests/bpf/ \
Toke Høiland-Jørgensen7cf245a2020-01-20 14:06:49 +0100407 -I$(srctree)/tools/lib/ \
Alexei Starovoitovb1ae32d2018-05-13 12:32:22 -0700408 -D__KERNEL__ -D__BPF_TRACING__ -Wno-unused-value -Wno-pointer-sign \
Ilya Leoshkevich81f522f2019-07-15 11:11:03 +0200409 -D__TARGET_ARCH_$(SRCARCH) -Wno-compare-distinct-pointer-types \
Alexei Starovoitov69a9d092016-12-01 18:31:12 -0800410 -Wno-gnu-variable-sized-type-not-at-end \
411 -Wno-address-of-packed-member -Wno-tautological-compare \
Abhijit Ayarekar9db95832017-10-13 12:24:06 -0700412 -Wno-unknown-warning-option $(CLANG_ARCH_ARGS) \
Toke Høiland-Jørgensen5a0ae982021-07-05 12:38:41 +0200413 -fno-asynchronous-unwind-tables \
Yonghong Song6bf3bbe2019-01-12 15:04:30 -0800414 -I$(srctree)/samples/bpf/ -include asm_goto_workaround.h \
Yonghong Song9618bde2020-10-05 21:34:26 -0700415 -O2 -emit-llvm -Xclang -disable-llvm-passes -c $< -o - | \
416 $(OPT) -O2 -mtriple=bpf-pc-linux | $(LLVM_DIS) | \
417 $(LLC) -march=bpf $(LLC_FLAGS) -filetype=obj -o $@
Taeung Song9778cfd2018-07-26 19:13:44 +0900418ifeq ($(DWARF2BTF),y)
419 $(BTF_PAHOLE) -J $@
420endif