1# SPDX-License-Identifier: GPL-2.0 2 3BPF_SAMPLES_PATH ?= $(abspath $(srctree)/$(src)) 4TOOLS_PATH := $(BPF_SAMPLES_PATH)/../../tools 5 6# List of programs to build 7tprogs-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 41tprogs-y += per_socket_stats_example 42tprogs-y += xdp_rxq_info 43tprogs-y += syscall_tp 44tprogs-y += cpustat 45tprogs-y += xdp_adjust_tail 46tprogs-y += xdpsock 47tprogs-y += xdpsock_ctrl_proc 48tprogs-y += xsk_fwd 49tprogs-y += xdp_fwd 50tprogs-y += task_fd_query 51tprogs-y += xdp_sample_pkts 52tprogs-y += ibumad 53tprogs-y += hbm 54 55tprogs-y += xdp_redirect_cpu 56tprogs-y += xdp_redirect_map_multi 57tprogs-y += xdp_redirect_map 58tprogs-y += xdp_redirect 59tprogs-y += xdp_monitor 60 61# Libbpf dependencies 62LIBBPF = $(TOOLS_PATH)/lib/bpf/libbpf.a 63 64CGROUP_HELPERS := ../../tools/testing/selftests/bpf/cgroup_helpers.o 65TRACE_HELPERS := ../../tools/testing/selftests/bpf/trace_helpers.o 66XDP_SAMPLE := xdp_sample_user.o 67 68fds_example-objs := fds_example.o 69sockex1-objs := sockex1_user.o 70sockex2-objs := sockex2_user.o 71sockex3-objs := sockex3_user.o 72tracex1-objs := tracex1_user.o $(TRACE_HELPERS) 73tracex2-objs := tracex2_user.o 74tracex3-objs := tracex3_user.o 75tracex4-objs := tracex4_user.o 76tracex5-objs := tracex5_user.o $(TRACE_HELPERS) 77tracex6-objs := tracex6_user.o 78tracex7-objs := tracex7_user.o 79test_probe_write_user-objs := test_probe_write_user_user.o 80trace_output-objs := trace_output_user.o 81lathist-objs := lathist_user.o 82offwaketime-objs := offwaketime_user.o $(TRACE_HELPERS) 83spintest-objs := spintest_user.o $(TRACE_HELPERS) 84map_perf_test-objs := map_perf_test_user.o 85test_overhead-objs := test_overhead_user.o 86test_cgrp2_array_pin-objs := test_cgrp2_array_pin.o 87test_cgrp2_attach-objs := test_cgrp2_attach.o 88test_cgrp2_sock-objs := test_cgrp2_sock.o 89test_cgrp2_sock2-objs := test_cgrp2_sock2.o 90xdp1-objs := xdp1_user.o 91# reuse xdp1 source intentionally 92xdp2-objs := xdp1_user.o 93xdp_router_ipv4-objs := xdp_router_ipv4_user.o 94test_current_task_under_cgroup-objs := $(CGROUP_HELPERS) \ 95 test_current_task_under_cgroup_user.o 96trace_event-objs := trace_event_user.o $(TRACE_HELPERS) 97sampleip-objs := sampleip_user.o $(TRACE_HELPERS) 98tc_l2_redirect-objs := tc_l2_redirect_user.o 99lwt_len_hist-objs := lwt_len_hist_user.o 100xdp_tx_iptunnel-objs := xdp_tx_iptunnel_user.o 101test_map_in_map-objs := test_map_in_map_user.o 102per_socket_stats_example-objs := cookie_uid_helper_example.o 103xdp_rxq_info-objs := xdp_rxq_info_user.o 104syscall_tp-objs := syscall_tp_user.o 105cpustat-objs := cpustat_user.o 106xdp_adjust_tail-objs := xdp_adjust_tail_user.o 107xdpsock-objs := xdpsock_user.o 108xdpsock_ctrl_proc-objs := xdpsock_ctrl_proc.o 109xsk_fwd-objs := xsk_fwd.o 110xdp_fwd-objs := xdp_fwd_user.o 111task_fd_query-objs := task_fd_query_user.o $(TRACE_HELPERS) 112xdp_sample_pkts-objs := xdp_sample_pkts_user.o 113ibumad-objs := ibumad_user.o 114hbm-objs := hbm.o $(CGROUP_HELPERS) 115 116xdp_redirect_map_multi-objs := xdp_redirect_map_multi_user.o $(XDP_SAMPLE) 117xdp_redirect_cpu-objs := xdp_redirect_cpu_user.o $(XDP_SAMPLE) 118xdp_redirect_map-objs := xdp_redirect_map_user.o $(XDP_SAMPLE) 119xdp_redirect-objs := xdp_redirect_user.o $(XDP_SAMPLE) 120xdp_monitor-objs := xdp_monitor_user.o $(XDP_SAMPLE) 121 122# Tell kbuild to always build the programs 123always-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 166always-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 178 179ifeq ($(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) 184TPROGS_CFLAGS += $(ARM_ARCH_SELECTOR) 185endif 186 187ifeq ($(ARCH), mips) 188TPROGS_CFLAGS += -D__SANE_USERSPACE_TYPES__ 189ifdef 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 193endif 194 195TPROGS_CFLAGS += -Wall -O2 196TPROGS_CFLAGS += -Wmissing-prototypes 197TPROGS_CFLAGS += -Wstrict-prototypes 198 199TPROGS_CFLAGS += -I$(objtree)/usr/include 200TPROGS_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 204TPROGS_CFLAGS += -DHAVE_ATTR_TEST=0 205 206ifdef SYSROOT 207TPROGS_CFLAGS += --sysroot=$(SYSROOT) 208TPROGS_LDFLAGS := -L$(SYSROOT)/usr/lib 209endif 210 211TPROGS_LDLIBS += $(LIBBPF) -lelf -lz 212TPROGLDLIBS_tracex4 += -lrt 213TPROGLDLIBS_trace_output += -lrt 214TPROGLDLIBS_map_perf_test += -lrt 215TPROGLDLIBS_test_overhead += -lrt 216TPROGLDLIBS_xdpsock += -pthread -lcap 217TPROGLDLIBS_xsk_fwd += -pthread 218 219# Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline: 220# make M=samples/bpf LLC=~/git/llvm-project/llvm/build/bin/llc CLANG=~/git/llvm-project/llvm/build/bin/clang 221LLC ?= llc 222CLANG ?= clang 223OPT ?= opt 224LLVM_DIS ?= llvm-dis 225LLVM_OBJCOPY ?= llvm-objcopy 226BTF_PAHOLE ?= pahole 227 228# Detect that we're cross compiling and use the cross compiler 229ifdef CROSS_COMPILE 230CLANG_ARCH_ARGS = --target=$(notdir $(CROSS_COMPILE:%-=%)) 231endif 232 233# Don't evaluate probes and warnings if we need to run make recursively 234ifneq ($(src),) 235HDR_PROBE := $(shell printf "\#include <linux/types.h>\n struct list_head { int a; }; int main() { return 0; }" | \ 236 $(CC) $(TPROGS_CFLAGS) $(TPROGS_LDFLAGS) -x c - \ 237 -o /dev/null 2>/dev/null && echo okay) 238 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 244BTF_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') 247BTF_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) 251 252BPF_EXTRA_CFLAGS += -fno-stack-protector 253ifneq ($(BTF_LLVM_PROBE),) 254 BPF_EXTRA_CFLAGS += -g 255else 256ifneq ($(and $(BTF_LLC_PROBE),$(BTF_PAHOLE_PROBE),$(BTF_OBJCOPY_PROBE)),) 257 BPF_EXTRA_CFLAGS += -g 258 LLC_FLAGS += -mattr=dwarfris 259 DWARF2BTF = y 260endif 261endif 262endif 263 264# Trick to allow make to be run from this directory 265all: 266 $(MAKE) -C ../../ M=$(CURDIR) BPF_SAMPLES_PATH=$(CURDIR) 267 268clean: 269 $(MAKE) -C ../../ M=$(CURDIR) clean 270 @find $(CURDIR) -type f -name '*~' -delete 271 272$(LIBBPF): FORCE 273# Fix up variables inherited from Kbuild that tools/ build system won't like 274 $(MAKE) -C $(dir $@) RM='rm -rf' EXTRA_CFLAGS="$(TPROGS_CFLAGS)" \ 275 LDFLAGS=$(TPROGS_LDFLAGS) srctree=$(BPF_SAMPLES_PATH)/../../ O= 276 277BPFTOOLDIR := $(TOOLS_PATH)/bpf/bpftool 278BPFTOOL := $(BPFTOOLDIR)/bpftool 279$(BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) 280 $(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../ 281 282$(obj)/syscall_nrs.h: $(obj)/syscall_nrs.s FORCE 283 $(call filechk,offsets,__SYSCALL_NRS_H__) 284 285targets += syscall_nrs.s 286clean-files += syscall_nrs.h 287 288FORCE: 289 290 291# Verify LLVM compiler tools are available and bpf target is supported by llc 292.PHONY: verify_cmds verify_target_bpf $(CLANG) $(LLC) 293 294verify_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 301 302verify_target_bpf: verify_cmds 303 @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 309$(BPF_SAMPLES_PATH)/*.c: verify_target_bpf $(LIBBPF) 310$(src)/*.c: verify_target_bpf $(LIBBPF) 311 312$(obj)/xdp_redirect_cpu_user.o: $(obj)/xdp_redirect_cpu.skel.h 313$(obj)/xdp_redirect_map_multi_user.o: $(obj)/xdp_redirect_map_multi.skel.h 314$(obj)/xdp_redirect_map_user.o: $(obj)/xdp_redirect_map.skel.h 315$(obj)/xdp_redirect_user.o: $(obj)/xdp_redirect.skel.h 316$(obj)/xdp_monitor_user.o: $(obj)/xdp_monitor.skel.h 317 318$(obj)/tracex5_kern.o: $(obj)/syscall_nrs.h 319$(obj)/hbm_out_kern.o: $(src)/hbm.h $(src)/hbm_kern.h 320$(obj)/hbm.o: $(src)/hbm.h 321$(obj)/hbm_edt_kern.o: $(src)/hbm.h $(src)/hbm_kern.h 322 323-include $(BPF_SAMPLES_PATH)/Makefile.target 324 325VMLINUX_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 345# 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 359$(obj)/xdp_redirect_cpu.bpf.o: $(obj)/xdp_sample.bpf.o 360$(obj)/xdp_redirect_map_multi.bpf.o: $(obj)/xdp_sample.bpf.o 361$(obj)/xdp_redirect_map.bpf.o: $(obj)/xdp_sample.bpf.o 362$(obj)/xdp_redirect.bpf.o: $(obj)/xdp_sample.bpf.o 363$(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 373LINKED_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 375clean-files += $(LINKED_SKELS) 376 377xdp_redirect_cpu.skel.h-deps := xdp_redirect_cpu.bpf.o xdp_sample.bpf.o 378xdp_redirect_map_multi.skel.h-deps := xdp_redirect_map_multi.bpf.o xdp_sample.bpf.o 379xdp_redirect_map.skel.h-deps := xdp_redirect_map.bpf.o xdp_sample.bpf.o 380xdp_redirect.skel.h-deps := xdp_redirect.bpf.o xdp_sample.bpf.o 381xdp_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 395# 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 397# useless for BPF samples. 398# 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. 403$(obj)/%.o: $(src)/%.c 404 @echo " CLANG-bpf " $@ 405 $(Q)$(CLANG) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(BPF_EXTRA_CFLAGS) \ 406 -I$(obj) -I$(srctree)/tools/testing/selftests/bpf/ \ 407 -I$(srctree)/tools/lib/ \ 408 -D__KERNEL__ -D__BPF_TRACING__ -Wno-unused-value -Wno-pointer-sign \ 409 -D__TARGET_ARCH_$(SRCARCH) -Wno-compare-distinct-pointer-types \ 410 -Wno-gnu-variable-sized-type-not-at-end \ 411 -Wno-address-of-packed-member -Wno-tautological-compare \ 412 -Wno-unknown-warning-option $(CLANG_ARCH_ARGS) \ 413 -fno-asynchronous-unwind-tables \ 414 -I$(srctree)/samples/bpf/ -include asm_goto_workaround.h \ 415 -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 $@ 418ifeq ($(DWARF2BTF),y) 419 $(BTF_PAHOLE) -J $@ 420endif 421