xref: /linux-6.15/samples/bpf/Makefile (revision ec6c4be0)
1# SPDX-License-Identifier: GPL-2.0
2
3BPF_SAMPLES_PATH ?= $(abspath $(src))
4TOOLS_PATH := $(BPF_SAMPLES_PATH)/../../tools
5
6pound := \#
7
8# List of programs to build
9tprogs-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 += tracex3
17tprogs-y += tracex4
18tprogs-y += tracex5
19tprogs-y += tracex6
20tprogs-y += tracex7
21tprogs-y += test_probe_write_user
22tprogs-y += trace_output
23tprogs-y += lathist
24tprogs-y += offwaketime
25tprogs-y += spintest
26tprogs-y += map_perf_test
27tprogs-y += test_overhead
28tprogs-y += test_cgrp2_array_pin
29tprogs-y += test_cgrp2_attach
30tprogs-y += test_cgrp2_sock2
31tprogs-y += xdp_router_ipv4
32tprogs-y += test_current_task_under_cgroup
33tprogs-y += trace_event
34tprogs-y += sampleip
35tprogs-y += tc_l2_redirect
36tprogs-y += lwt_len_hist
37tprogs-y += xdp_tx_iptunnel
38tprogs-y += test_map_in_map
39tprogs-y += per_socket_stats_example
40tprogs-y += syscall_tp
41tprogs-y += cpustat
42tprogs-y += xdp_adjust_tail
43tprogs-y += xdp_fwd
44tprogs-y += task_fd_query
45tprogs-y += ibumad
46tprogs-y += hbm
47
48# Libbpf dependencies
49LIBBPF_SRC = $(TOOLS_PATH)/lib/bpf
50LIBBPF_OUTPUT = $(abspath $(BPF_SAMPLES_PATH))/libbpf
51LIBBPF_DESTDIR = $(LIBBPF_OUTPUT)
52LIBBPF_INCLUDE = $(LIBBPF_DESTDIR)/include
53LIBBPF = $(LIBBPF_OUTPUT)/libbpf.a
54
55CGROUP_HELPERS := ../../tools/testing/selftests/bpf/cgroup_helpers.o
56TRACE_HELPERS := ../../tools/testing/selftests/bpf/trace_helpers.o
57XDP_SAMPLE := xdp_sample_user.o
58
59fds_example-objs := fds_example.o
60sockex1-objs := sockex1_user.o
61sockex2-objs := sockex2_user.o
62sockex3-objs := sockex3_user.o
63tracex1-objs := tracex1_user.o $(TRACE_HELPERS)
64tracex3-objs := tracex3_user.o
65tracex4-objs := tracex4_user.o
66tracex5-objs := tracex5_user.o $(TRACE_HELPERS)
67tracex6-objs := tracex6_user.o
68tracex7-objs := tracex7_user.o
69test_probe_write_user-objs := test_probe_write_user_user.o
70trace_output-objs := trace_output_user.o
71lathist-objs := lathist_user.o
72offwaketime-objs := offwaketime_user.o $(TRACE_HELPERS)
73spintest-objs := spintest_user.o $(TRACE_HELPERS)
74map_perf_test-objs := map_perf_test_user.o
75test_overhead-objs := test_overhead_user.o
76test_cgrp2_array_pin-objs := test_cgrp2_array_pin.o
77test_cgrp2_attach-objs := test_cgrp2_attach.o
78test_cgrp2_sock2-objs := test_cgrp2_sock2.o
79test_current_task_under_cgroup-objs := $(CGROUP_HELPERS) \
80				       test_current_task_under_cgroup_user.o
81trace_event-objs := trace_event_user.o $(TRACE_HELPERS)
82sampleip-objs := sampleip_user.o $(TRACE_HELPERS)
83tc_l2_redirect-objs := tc_l2_redirect_user.o
84lwt_len_hist-objs := lwt_len_hist_user.o
85xdp_tx_iptunnel-objs := xdp_tx_iptunnel_user.o
86test_map_in_map-objs := test_map_in_map_user.o
87per_socket_stats_example-objs := cookie_uid_helper_example.o
88syscall_tp-objs := syscall_tp_user.o
89cpustat-objs := cpustat_user.o
90xdp_adjust_tail-objs := xdp_adjust_tail_user.o
91xdp_fwd-objs := xdp_fwd_user.o
92task_fd_query-objs := task_fd_query_user.o $(TRACE_HELPERS)
93ibumad-objs := ibumad_user.o
94hbm-objs := hbm.o $(CGROUP_HELPERS)
95
96xdp_router_ipv4-objs := xdp_router_ipv4_user.o $(XDP_SAMPLE)
97
98# Tell kbuild to always build the programs
99always-y := $(tprogs-y)
100always-y += sockex1_kern.o
101always-y += sockex2_kern.o
102always-y += sockex3_kern.o
103always-y += tracex1.bpf.o
104always-y += tracex3.bpf.o
105always-y += tracex4.bpf.o
106always-y += tracex5.bpf.o
107always-y += tracex6.bpf.o
108always-y += tracex7.bpf.o
109always-y += sock_flags.bpf.o
110always-y += test_probe_write_user.bpf.o
111always-y += trace_output.bpf.o
112always-y += tcbpf1_kern.o
113always-y += tc_l2_redirect_kern.o
114always-y += lathist_kern.o
115always-y += offwaketime.bpf.o
116always-y += spintest.bpf.o
117always-y += map_perf_test.bpf.o
118always-y += test_overhead_tp.bpf.o
119always-y += test_overhead_raw_tp.bpf.o
120always-y += test_overhead_kprobe.bpf.o
121always-y += parse_varlen.o parse_simple.o parse_ldabs.o
122always-y += test_cgrp2_tc.bpf.o
123always-y += test_current_task_under_cgroup.bpf.o
124always-y += trace_event_kern.o
125always-y += sampleip_kern.o
126always-y += lwt_len_hist.bpf.o
127always-y += xdp_tx_iptunnel_kern.o
128always-y += test_map_in_map.bpf.o
129always-y += tcp_synrto_kern.o
130always-y += tcp_rwnd_kern.o
131always-y += tcp_bufs_kern.o
132always-y += tcp_cong_kern.o
133always-y += tcp_iw_kern.o
134always-y += tcp_clamp_kern.o
135always-y += tcp_basertt_kern.o
136always-y += tcp_tos_reflect_kern.o
137always-y += tcp_dumpstats_kern.o
138always-y += xdp2skb_meta_kern.o
139always-y += syscall_tp_kern.o
140always-y += cpustat_kern.o
141always-y += xdp_adjust_tail_kern.o
142always-y += xdp_fwd_kern.o
143always-y += task_fd_query_kern.o
144always-y += ibumad_kern.o
145always-y += hbm_out_kern.o
146always-y += hbm_edt_kern.o
147
148TPROGS_CFLAGS = $(TPROGS_USER_CFLAGS)
149TPROGS_LDFLAGS = $(TPROGS_USER_LDFLAGS)
150
151ifeq ($(ARCH), arm)
152# Strip all except -D__LINUX_ARM_ARCH__ option needed to handle linux
153# headers when arm instruction set identification is requested.
154ARM_ARCH_SELECTOR := $(filter -D__LINUX_ARM_ARCH__%, $(KBUILD_CFLAGS))
155BPF_EXTRA_CFLAGS := $(ARM_ARCH_SELECTOR)
156TPROGS_CFLAGS += $(ARM_ARCH_SELECTOR)
157endif
158
159ifeq ($(ARCH), mips)
160TPROGS_CFLAGS += -D__SANE_USERSPACE_TYPES__
161ifdef CONFIG_MACH_LOONGSON64
162BPF_EXTRA_CFLAGS += -I$(srctree)/arch/mips/include/asm/mach-loongson64
163BPF_EXTRA_CFLAGS += -I$(srctree)/arch/mips/include/asm/mach-generic
164endif
165endif
166
167ifeq ($(ARCH), x86)
168BPF_EXTRA_CFLAGS += -fcf-protection
169endif
170
171TPROGS_CFLAGS += -Wall -O2
172TPROGS_CFLAGS += -Wmissing-prototypes
173TPROGS_CFLAGS += -Wstrict-prototypes
174TPROGS_CFLAGS += $(call try-run,\
175	printf "int main() { return 0; }" |\
176	$(CC) -Werror -fsanitize=bounds -x c - -o "$$TMP",-fsanitize=bounds,)
177
178TPROGS_CFLAGS += -I$(objtree)/usr/include
179TPROGS_CFLAGS += -I$(srctree)/tools/testing/selftests/bpf/
180TPROGS_CFLAGS += -I$(LIBBPF_INCLUDE)
181TPROGS_CFLAGS += -I$(srctree)/tools/include
182TPROGS_CFLAGS += -I$(srctree)/tools/perf
183TPROGS_CFLAGS += -I$(srctree)/tools/lib
184TPROGS_CFLAGS += -DHAVE_ATTR_TEST=0
185
186ifdef SYSROOT
187TPROGS_CFLAGS += --sysroot=$(SYSROOT)
188TPROGS_LDFLAGS := -L$(SYSROOT)/usr/lib
189endif
190
191TPROGS_LDLIBS			+= $(LIBBPF) -lelf -lz
192TPROGLDLIBS_xdp_router_ipv4	+= -lm -pthread
193TPROGLDLIBS_tracex4		+= -lrt
194TPROGLDLIBS_trace_output	+= -lrt
195TPROGLDLIBS_map_perf_test	+= -lrt
196TPROGLDLIBS_test_overhead	+= -lrt
197
198# Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline:
199# make M=samples/bpf LLC=~/git/llvm-project/llvm/build/bin/llc CLANG=~/git/llvm-project/llvm/build/bin/clang
200LLC ?= llc
201CLANG ?= clang
202OPT ?= opt
203LLVM_DIS ?= llvm-dis
204LLVM_OBJCOPY ?= llvm-objcopy
205LLVM_READELF ?= llvm-readelf
206BTF_PAHOLE ?= pahole
207
208# Detect that we're cross compiling and use the cross compiler
209ifdef CROSS_COMPILE
210CLANG_ARCH_ARGS = --target=$(notdir $(CROSS_COMPILE:%-=%))
211endif
212
213# Don't evaluate probes and warnings if we need to run make recursively
214ifneq ($(src),)
215HDR_PROBE := $(shell printf "$(pound)include <linux/types.h>\n struct list_head { int a; }; int main() { return 0; }" | \
216	$(CC) $(TPROGS_CFLAGS) $(TPROGS_LDFLAGS) -x c - \
217	-o /dev/null 2>/dev/null && echo okay)
218
219ifeq ($(HDR_PROBE),)
220$(warning WARNING: Detected possible issues with include path.)
221$(warning WARNING: Please install kernel headers locally (make headers_install).)
222endif
223
224BTF_LLC_PROBE := $(shell $(LLC) -march=bpf -mattr=help 2>&1 | grep dwarfris)
225BTF_PAHOLE_PROBE := $(shell $(BTF_PAHOLE) --help 2>&1 | grep BTF)
226BTF_OBJCOPY_PROBE := $(shell $(LLVM_OBJCOPY) --help 2>&1 | grep -i 'usage.*llvm')
227BTF_LLVM_PROBE := $(shell echo "int main() { return 0; }" | \
228			  $(CLANG) --target=bpf -O2 -g -c -x c - -o ./llvm_btf_verify.o; \
229			  $(LLVM_READELF) -S ./llvm_btf_verify.o | grep BTF; \
230			  /bin/rm -f ./llvm_btf_verify.o)
231
232BPF_EXTRA_CFLAGS += -fno-stack-protector
233ifneq ($(BTF_LLVM_PROBE),)
234	BPF_EXTRA_CFLAGS += -g
235else
236ifneq ($(and $(BTF_LLC_PROBE),$(BTF_PAHOLE_PROBE),$(BTF_OBJCOPY_PROBE)),)
237	BPF_EXTRA_CFLAGS += -g
238	LLC_FLAGS += -mattr=dwarfris
239	DWARF2BTF = y
240endif
241endif
242endif
243
244# Trick to allow make to be run from this directory
245all:
246	$(MAKE) -C ../../ M=$(CURDIR) BPF_SAMPLES_PATH=$(CURDIR)
247
248clean:
249	$(MAKE) -C ../../ M=$(CURDIR) clean
250	@find $(CURDIR) -type f -name '*~' -delete
251	@$(RM) -r $(CURDIR)/libbpf $(CURDIR)/bpftool
252
253$(LIBBPF): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(LIBBPF_OUTPUT)
254# Fix up variables inherited from Kbuild that tools/ build system won't like
255	$(MAKE) -C $(LIBBPF_SRC) RM='rm -rf' EXTRA_CFLAGS="$(TPROGS_CFLAGS)" \
256		LDFLAGS="$(TPROGS_LDFLAGS)" srctree=$(BPF_SAMPLES_PATH)/../../ \
257		O= OUTPUT=$(LIBBPF_OUTPUT)/ DESTDIR=$(LIBBPF_DESTDIR) prefix= \
258		$@ install_headers
259
260BPFTOOLDIR := $(TOOLS_PATH)/bpf/bpftool
261BPFTOOL_OUTPUT := $(abspath $(BPF_SAMPLES_PATH))/bpftool
262DEFAULT_BPFTOOL := $(BPFTOOL_OUTPUT)/bootstrap/bpftool
263BPFTOOL ?= $(DEFAULT_BPFTOOL)
264$(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT)
265	$(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../ 		\
266		OUTPUT=$(BPFTOOL_OUTPUT)/ bootstrap
267
268$(LIBBPF_OUTPUT) $(BPFTOOL_OUTPUT):
269	$(call msg,MKDIR,$@)
270	$(Q)mkdir -p $@
271
272$(obj)/syscall_nrs.h:	$(obj)/syscall_nrs.s FORCE
273	$(call filechk,offsets,__SYSCALL_NRS_H__)
274
275targets += syscall_nrs.s
276clean-files += syscall_nrs.h
277
278FORCE:
279
280
281# Verify LLVM compiler tools are available and bpf target is supported by llc
282.PHONY: verify_cmds verify_target_bpf $(CLANG) $(LLC)
283
284verify_cmds: $(CLANG) $(LLC)
285	@for TOOL in $^ ; do \
286		if ! (which -- "$${TOOL}" > /dev/null 2>&1); then \
287			echo "*** ERROR: Cannot find LLVM tool $${TOOL}" ;\
288			exit 1; \
289		else true; fi; \
290	done
291
292verify_target_bpf: verify_cmds
293	@if ! (${LLC} -march=bpf -mattr=help > /dev/null 2>&1); then \
294		echo "*** ERROR: LLVM (${LLC}) does not support 'bpf' target" ;\
295		echo "   NOTICE: LLVM version >= 3.7.1 required" ;\
296		exit 2; \
297	else true; fi
298
299$(BPF_SAMPLES_PATH)/*.c: verify_target_bpf $(LIBBPF)
300$(src)/*.c: verify_target_bpf $(LIBBPF)
301
302libbpf_hdrs: $(LIBBPF)
303$(obj)/$(TRACE_HELPERS) $(obj)/$(CGROUP_HELPERS) $(obj)/$(XDP_SAMPLE): | libbpf_hdrs
304
305.PHONY: libbpf_hdrs
306
307$(obj)/xdp_router_ipv4_user.o: $(obj)/xdp_router_ipv4.skel.h
308
309$(obj)/tracex5.bpf.o: $(obj)/syscall_nrs.h
310$(obj)/hbm_out_kern.o: $(src)/hbm.h $(src)/hbm_kern.h
311$(obj)/hbm.o: $(src)/hbm.h
312$(obj)/hbm_edt_kern.o: $(src)/hbm.h $(src)/hbm_kern.h
313
314# Override includes for xdp_sample_user.o because $(srctree)/usr/include in
315# TPROGS_CFLAGS causes conflicts
316XDP_SAMPLE_CFLAGS += -Wall -O2 \
317		     -I$(src)/../../tools/include \
318		     -I$(src)/../../tools/include/uapi \
319		     -I$(LIBBPF_INCLUDE) \
320		     -I$(src)/../../tools/testing/selftests/bpf
321
322$(obj)/$(XDP_SAMPLE): TPROGS_CFLAGS = $(XDP_SAMPLE_CFLAGS) $(TPROGS_USER_CFLAGS)
323$(obj)/$(XDP_SAMPLE): $(src)/xdp_sample_user.h $(src)/xdp_sample_shared.h
324# Override includes for trace_helpers.o because __must_check won't be defined
325# in our include path.
326$(obj)/$(TRACE_HELPERS): TPROGS_CFLAGS := $(TPROGS_CFLAGS) -D__must_check=
327
328-include $(BPF_SAMPLES_PATH)/Makefile.target
329
330VMLINUX_BTF_PATHS ?= $(abspath $(if $(O),$(O)/vmlinux))				\
331		     $(abspath $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux))	\
332		     $(abspath ./vmlinux)
333VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
334
335$(obj)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL)
336ifeq ($(VMLINUX_H),)
337ifeq ($(VMLINUX_BTF),)
338	$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)",\
339		build the kernel or set VMLINUX_BTF like "VMLINUX_BTF=/sys/kernel/btf/vmlinux" or VMLINUX_H variable)
340endif
341	$(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
342else
343	$(Q)cp "$(VMLINUX_H)" $@
344endif
345
346clean-files += vmlinux.h
347
348# Get Clang's default includes on this system, as opposed to those seen by
349# '--target=bpf'. This fixes "missing" files on some architectures/distros,
350# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
351#
352# Use '-idirafter': Don't interfere with include mechanics except where the
353# build would have failed anyways.
354define get_sys_includes
355$(shell $(1) -v -E - </dev/null 2>&1 \
356        | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
357$(shell $(1) -dM -E - </dev/null | grep '#define __riscv_xlen ' | sed 's/#define /-D/' | sed 's/ /=/')
358endef
359
360CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
361
362$(obj)/xdp_router_ipv4.bpf.o: $(obj)/xdp_sample.bpf.o
363
364$(obj)/%.bpf.o: $(src)/%.bpf.c $(obj)/vmlinux.h $(src)/xdp_sample.bpf.h $(src)/xdp_sample_shared.h
365	@echo "  CLANG-BPF " $@
366	$(Q)$(CLANG) -g -O2 --target=bpf -D__TARGET_ARCH_$(SRCARCH) \
367		-Wno-compare-distinct-pointer-types -I$(srctree)/include \
368		-I$(srctree)/samples/bpf -I$(srctree)/tools/include \
369		-I$(LIBBPF_INCLUDE) $(CLANG_SYS_INCLUDES) \
370		-c $(filter %.bpf.c,$^) -o $@
371
372LINKED_SKELS := xdp_router_ipv4.skel.h
373clean-files += $(LINKED_SKELS)
374
375xdp_router_ipv4.skel.h-deps := xdp_router_ipv4.bpf.o xdp_sample.bpf.o
376
377LINKED_BPF_SRCS := $(patsubst %.bpf.o,%.bpf.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
378
379BPF_SRCS_LINKED := $(notdir $(wildcard $(src)/*.bpf.c))
380BPF_OBJS_LINKED := $(patsubst %.bpf.c,$(obj)/%.bpf.o, $(BPF_SRCS_LINKED))
381BPF_SKELS_LINKED := $(addprefix $(obj)/,$(LINKED_SKELS))
382
383$(BPF_SKELS_LINKED): $(BPF_OBJS_LINKED) $(BPFTOOL)
384	@echo "  BPF GEN-OBJ " $(@:.skel.h=)
385	$(Q)$(BPFTOOL) gen object $(@:.skel.h=.lbpf.o) $(addprefix $(obj)/,$($(@F)-deps))
386	@echo "  BPF GEN-SKEL" $(@:.skel.h=)
387	$(Q)$(BPFTOOL) gen skeleton $(@:.skel.h=.lbpf.o) name $(notdir $(@:.skel.h=)) > $@
388
389# asm/sysreg.h - inline assembly used by it is incompatible with llvm.
390# But, there is no easy way to fix it, so just exclude it since it is
391# useless for BPF samples.
392# below we use long chain of commands, clang | opt | llvm-dis | llc,
393# to generate final object file. 'clang' compiles the source into IR
394# with native target, e.g., x64, arm64, etc. 'opt' does bpf CORE IR builtin
395# processing (llvm12) and IR optimizations. 'llvm-dis' converts
396# 'opt' output to IR, and finally 'llc' generates bpf byte code.
397$(obj)/%.o: $(src)/%.c
398	@echo "  CLANG-bpf " $@
399	$(Q)$(CLANG) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(BPF_EXTRA_CFLAGS) \
400		-I$(obj) -I$(srctree)/tools/testing/selftests/bpf/ \
401		-I$(LIBBPF_INCLUDE) \
402		-D__KERNEL__ -D__BPF_TRACING__ -Wno-unused-value -Wno-pointer-sign \
403		-D__TARGET_ARCH_$(SRCARCH) -Wno-compare-distinct-pointer-types \
404		-Wno-gnu-variable-sized-type-not-at-end \
405		-Wno-address-of-packed-member -Wno-tautological-compare \
406		-Wno-unknown-warning-option $(CLANG_ARCH_ARGS) \
407		-fno-asynchronous-unwind-tables \
408		-I$(srctree)/samples/bpf/ -include asm_goto_workaround.h \
409		-O2 -emit-llvm -Xclang -disable-llvm-passes -c $< -o - | \
410		$(OPT) -O2 -mtriple=bpf-pc-linux | $(LLVM_DIS) | \
411		$(LLC) -march=bpf $(LLC_FLAGS) -filetype=obj -o $@
412ifeq ($(DWARF2BTF),y)
413	$(BTF_PAHOLE) -J $@
414endif
415