Makefile 13 KB

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