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 7hostprogs-y := test_lru_dist 8hostprogs-y += sock_example 9hostprogs-y += fds_example 10hostprogs-y += sockex1 11hostprogs-y += sockex2 12hostprogs-y += sockex3 13hostprogs-y += tracex1 14hostprogs-y += tracex2 15hostprogs-y += tracex3 16hostprogs-y += tracex4 17hostprogs-y += tracex5 18hostprogs-y += tracex6 19hostprogs-y += tracex7 20hostprogs-y += test_probe_write_user 21hostprogs-y += trace_output 22hostprogs-y += lathist 23hostprogs-y += offwaketime 24hostprogs-y += spintest 25hostprogs-y += map_perf_test 26hostprogs-y += test_overhead 27hostprogs-y += test_cgrp2_array_pin 28hostprogs-y += test_cgrp2_attach 29hostprogs-y += test_cgrp2_attach2 30hostprogs-y += test_cgrp2_sock 31hostprogs-y += test_cgrp2_sock2 32hostprogs-y += xdp1 33hostprogs-y += xdp2 34hostprogs-y += xdp_router_ipv4 35hostprogs-y += test_current_task_under_cgroup 36hostprogs-y += trace_event 37hostprogs-y += sampleip 38hostprogs-y += tc_l2_redirect 39hostprogs-y += lwt_len_hist 40hostprogs-y += xdp_tx_iptunnel 41hostprogs-y += test_map_in_map 42hostprogs-y += per_socket_stats_example 43hostprogs-y += load_sock_ops 44hostprogs-y += xdp_redirect 45hostprogs-y += xdp_redirect_map 46hostprogs-y += xdp_redirect_cpu 47hostprogs-y += xdp_monitor 48hostprogs-y += xdp_rxq_info 49hostprogs-y += syscall_tp 50hostprogs-y += cpustat 51hostprogs-y += xdp_adjust_tail 52hostprogs-y += xdpsock 53hostprogs-y += xdp_fwd 54hostprogs-y += task_fd_query 55 56# Libbpf dependencies 57LIBBPF = $(TOOLS_PATH)/lib/bpf/libbpf.a 58 59CGROUP_HELPERS := ../../tools/testing/selftests/bpf/cgroup_helpers.o 60TRACE_HELPERS := ../../tools/testing/selftests/bpf/trace_helpers.o 61 62fds_example-objs := bpf_load.o fds_example.o 63sockex1-objs := bpf_load.o sockex1_user.o 64sockex2-objs := bpf_load.o sockex2_user.o 65sockex3-objs := bpf_load.o sockex3_user.o 66tracex1-objs := bpf_load.o tracex1_user.o 67tracex2-objs := bpf_load.o tracex2_user.o 68tracex3-objs := bpf_load.o tracex3_user.o 69tracex4-objs := bpf_load.o tracex4_user.o 70tracex5-objs := bpf_load.o tracex5_user.o 71tracex6-objs := bpf_load.o tracex6_user.o 72tracex7-objs := bpf_load.o tracex7_user.o 73load_sock_ops-objs := bpf_load.o load_sock_ops.o 74test_probe_write_user-objs := bpf_load.o test_probe_write_user_user.o 75trace_output-objs := bpf_load.o trace_output_user.o $(TRACE_HELPERS) 76lathist-objs := bpf_load.o lathist_user.o 77offwaketime-objs := bpf_load.o offwaketime_user.o $(TRACE_HELPERS) 78spintest-objs := bpf_load.o spintest_user.o $(TRACE_HELPERS) 79map_perf_test-objs := bpf_load.o map_perf_test_user.o 80test_overhead-objs := bpf_load.o test_overhead_user.o 81test_cgrp2_array_pin-objs := test_cgrp2_array_pin.o 82test_cgrp2_attach-objs := test_cgrp2_attach.o 83test_cgrp2_attach2-objs := test_cgrp2_attach2.o $(CGROUP_HELPERS) 84test_cgrp2_sock-objs := test_cgrp2_sock.o 85test_cgrp2_sock2-objs := bpf_load.o test_cgrp2_sock2.o 86xdp1-objs := xdp1_user.o 87# reuse xdp1 source intentionally 88xdp2-objs := xdp1_user.o 89xdp_router_ipv4-objs := bpf_load.o xdp_router_ipv4_user.o 90test_current_task_under_cgroup-objs := bpf_load.o $(CGROUP_HELPERS) \ 91 test_current_task_under_cgroup_user.o 92trace_event-objs := bpf_load.o trace_event_user.o $(TRACE_HELPERS) 93sampleip-objs := bpf_load.o sampleip_user.o $(TRACE_HELPERS) 94tc_l2_redirect-objs := bpf_load.o tc_l2_redirect_user.o 95lwt_len_hist-objs := bpf_load.o lwt_len_hist_user.o 96xdp_tx_iptunnel-objs := bpf_load.o xdp_tx_iptunnel_user.o 97test_map_in_map-objs := bpf_load.o test_map_in_map_user.o 98per_socket_stats_example-objs := cookie_uid_helper_example.o 99xdp_redirect-objs := bpf_load.o xdp_redirect_user.o 100xdp_redirect_map-objs := bpf_load.o xdp_redirect_map_user.o 101xdp_redirect_cpu-objs := bpf_load.o xdp_redirect_cpu_user.o 102xdp_monitor-objs := bpf_load.o xdp_monitor_user.o 103xdp_rxq_info-objs := xdp_rxq_info_user.o 104syscall_tp-objs := bpf_load.o syscall_tp_user.o 105cpustat-objs := bpf_load.o cpustat_user.o 106xdp_adjust_tail-objs := xdp_adjust_tail_user.o 107xdpsock-objs := bpf_load.o xdpsock_user.o 108xdp_fwd-objs := bpf_load.o xdp_fwd_user.o 109task_fd_query-objs := bpf_load.o task_fd_query_user.o $(TRACE_HELPERS) 110 111# Tell kbuild to always build the programs 112always := $(hostprogs-y) 113always += sockex1_kern.o 114always += sockex2_kern.o 115always += sockex3_kern.o 116always += tracex1_kern.o 117always += tracex2_kern.o 118always += tracex3_kern.o 119always += tracex4_kern.o 120always += tracex5_kern.o 121always += tracex6_kern.o 122always += tracex7_kern.o 123always += sock_flags_kern.o 124always += test_probe_write_user_kern.o 125always += trace_output_kern.o 126always += tcbpf1_kern.o 127always += tc_l2_redirect_kern.o 128always += lathist_kern.o 129always += offwaketime_kern.o 130always += spintest_kern.o 131always += map_perf_test_kern.o 132always += test_overhead_tp_kern.o 133always += test_overhead_raw_tp_kern.o 134always += test_overhead_kprobe_kern.o 135always += parse_varlen.o parse_simple.o parse_ldabs.o 136always += test_cgrp2_tc_kern.o 137always += xdp1_kern.o 138always += xdp2_kern.o 139always += xdp_router_ipv4_kern.o 140always += test_current_task_under_cgroup_kern.o 141always += trace_event_kern.o 142always += sampleip_kern.o 143always += lwt_len_hist_kern.o 144always += xdp_tx_iptunnel_kern.o 145always += test_map_in_map_kern.o 146always += cookie_uid_helper_example.o 147always += tcp_synrto_kern.o 148always += tcp_rwnd_kern.o 149always += tcp_bufs_kern.o 150always += tcp_cong_kern.o 151always += tcp_iw_kern.o 152always += tcp_clamp_kern.o 153always += tcp_basertt_kern.o 154always += xdp_redirect_kern.o 155always += xdp_redirect_map_kern.o 156always += xdp_redirect_cpu_kern.o 157always += xdp_monitor_kern.o 158always += xdp_rxq_info_kern.o 159always += xdp2skb_meta_kern.o 160always += syscall_tp_kern.o 161always += cpustat_kern.o 162always += xdp_adjust_tail_kern.o 163always += xdpsock_kern.o 164always += xdp_fwd_kern.o 165always += task_fd_query_kern.o 166 167HOSTCFLAGS += -I$(objtree)/usr/include 168HOSTCFLAGS += -I$(srctree)/tools/lib/ 169HOSTCFLAGS += -I$(srctree)/tools/testing/selftests/bpf/ 170HOSTCFLAGS += -I$(srctree)/tools/lib/ -I$(srctree)/tools/include 171HOSTCFLAGS += -I$(srctree)/tools/perf 172 173HOSTCFLAGS_bpf_load.o += -I$(objtree)/usr/include -Wno-unused-variable 174HOSTCFLAGS_trace_helpers.o += -I$(srctree)/tools/lib/bpf/ 175 176HOSTCFLAGS_trace_output_user.o += -I$(srctree)/tools/lib/bpf/ 177HOSTCFLAGS_offwaketime_user.o += -I$(srctree)/tools/lib/bpf/ 178HOSTCFLAGS_spintest_user.o += -I$(srctree)/tools/lib/bpf/ 179HOSTCFLAGS_trace_event_user.o += -I$(srctree)/tools/lib/bpf/ 180HOSTCFLAGS_sampleip_user.o += -I$(srctree)/tools/lib/bpf/ 181HOSTCFLAGS_task_fd_query_user.o += -I$(srctree)/tools/lib/bpf/ 182 183HOST_LOADLIBES += $(LIBBPF) -lelf 184HOSTLOADLIBES_tracex4 += -lrt 185HOSTLOADLIBES_trace_output += -lrt 186HOSTLOADLIBES_map_perf_test += -lrt 187HOSTLOADLIBES_test_overhead += -lrt 188HOSTLOADLIBES_xdpsock += -pthread 189 190# Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline: 191# make samples/bpf/ LLC=~/git/llvm/build/bin/llc CLANG=~/git/llvm/build/bin/clang 192LLC ?= llc 193CLANG ?= clang 194 195# Detect that we're cross compiling and use the cross compiler 196ifdef CROSS_COMPILE 197HOSTCC = $(CROSS_COMPILE)gcc 198CLANG_ARCH_ARGS = -target $(ARCH) 199endif 200 201# Trick to allow make to be run from this directory 202all: 203 $(MAKE) -C ../../ $(CURDIR)/ BPF_SAMPLES_PATH=$(CURDIR) 204 205clean: 206 $(MAKE) -C ../../ M=$(CURDIR) clean 207 @rm -f *~ 208 209$(LIBBPF): FORCE 210# Fix up variables inherited from Kbuild that tools/ build system won't like 211 $(MAKE) -C $(dir $@) RM='rm -rf' LDFLAGS= srctree=$(BPF_SAMPLES_PATH)/../../ O= 212 213$(obj)/syscall_nrs.s: $(src)/syscall_nrs.c 214 $(call if_changed_dep,cc_s_c) 215 216$(obj)/syscall_nrs.h: $(obj)/syscall_nrs.s FORCE 217 $(call filechk,offsets,__SYSCALL_NRS_H__) 218 219clean-files += syscall_nrs.h 220 221FORCE: 222 223 224# Verify LLVM compiler tools are available and bpf target is supported by llc 225.PHONY: verify_cmds verify_target_bpf $(CLANG) $(LLC) 226 227verify_cmds: $(CLANG) $(LLC) 228 @for TOOL in $^ ; do \ 229 if ! (which -- "$${TOOL}" > /dev/null 2>&1); then \ 230 echo "*** ERROR: Cannot find LLVM tool $${TOOL}" ;\ 231 exit 1; \ 232 else true; fi; \ 233 done 234 235verify_target_bpf: verify_cmds 236 @if ! (${LLC} -march=bpf -mattr=help > /dev/null 2>&1); then \ 237 echo "*** ERROR: LLVM (${LLC}) does not support 'bpf' target" ;\ 238 echo " NOTICE: LLVM version >= 3.7.1 required" ;\ 239 exit 2; \ 240 else true; fi 241 242$(BPF_SAMPLES_PATH)/*.c: verify_target_bpf $(LIBBPF) 243$(src)/*.c: verify_target_bpf $(LIBBPF) 244 245$(obj)/tracex5_kern.o: $(obj)/syscall_nrs.h 246 247# asm/sysreg.h - inline assembly used by it is incompatible with llvm. 248# But, there is no easy way to fix it, so just exclude it since it is 249# useless for BPF samples. 250$(obj)/%.o: $(src)/%.c 251 @echo " CLANG-bpf " $@ 252 $(Q)$(CLANG) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(EXTRA_CFLAGS) -I$(obj) \ 253 -I$(srctree)/tools/testing/selftests/bpf/ \ 254 -D__KERNEL__ -D__BPF_TRACING__ -Wno-unused-value -Wno-pointer-sign \ 255 -D__TARGET_ARCH_$(ARCH) -Wno-compare-distinct-pointer-types \ 256 -Wno-gnu-variable-sized-type-not-at-end \ 257 -Wno-address-of-packed-member -Wno-tautological-compare \ 258 -Wno-unknown-warning-option $(CLANG_ARCH_ARGS) \ 259 -O2 -emit-llvm -c $< -o -| $(LLC) -march=bpf -filetype=obj -o $@ 260