392 lines · 13.5 KB · Makefile
RawDownload
# SPDX-License-Identifier: GPL-2.0 BPF_SAMPLES_PATH ?= $(abspath $(src))TOOLS_PATH := $(BPF_SAMPLES_PATH)/../../tools pound := \# # List of programs to buildtprogs-y := test_lru_disttprogs-y += sock_exampletprogs-y += fds_exampletprogs-y += sockex1tprogs-y += sockex2tprogs-y += sockex3tprogs-y += tracex1tprogs-y += tracex3tprogs-y += tracex4tprogs-y += tracex5tprogs-y += tracex6tprogs-y += trace_outputtprogs-y += lathisttprogs-y += offwaketimetprogs-y += spintesttprogs-y += map_perf_testtprogs-y += xdp_router_ipv4tprogs-y += trace_eventtprogs-y += sampleiptprogs-y += tc_l2_redirecttprogs-y += lwt_len_histtprogs-y += xdp_tx_iptunneltprogs-y += test_map_in_maptprogs-y += per_socket_stats_exampletprogs-y += syscall_tptprogs-y += cpustattprogs-y += xdp_adjust_tailtprogs-y += xdp_fwdtprogs-y += task_fd_querytprogs-y += ibumadtprogs-y += hbm # Libbpf dependenciesLIBBPF_SRC = $(TOOLS_PATH)/lib/bpfLIBBPF_OUTPUT = $(abspath $(BPF_SAMPLES_PATH))/libbpfLIBBPF_DESTDIR = $(LIBBPF_OUTPUT)LIBBPF_INCLUDE = $(LIBBPF_DESTDIR)/includeLIBBPF = $(LIBBPF_OUTPUT)/libbpf.a CGROUP_HELPERS := ../../tools/testing/selftests/bpf/cgroup_helpers.oTRACE_HELPERS := ../../tools/testing/selftests/bpf/trace_helpers.oXDP_SAMPLE := xdp_sample_user.o fds_example-objs := fds_example.osockex1-objs := sockex1_user.osockex2-objs := sockex2_user.osockex3-objs := sockex3_user.otracex1-objs := tracex1_user.o $(TRACE_HELPERS)tracex3-objs := tracex3_user.otracex4-objs := tracex4_user.otracex5-objs := tracex5_user.o $(TRACE_HELPERS)tracex6-objs := tracex6_user.otrace_output-objs := trace_output_user.olathist-objs := lathist_user.ooffwaketime-objs := offwaketime_user.o $(TRACE_HELPERS)spintest-objs := spintest_user.o $(TRACE_HELPERS)map_perf_test-objs := map_perf_test_user.otest_overhead-objs := test_overhead_user.otrace_event-objs := trace_event_user.o $(TRACE_HELPERS)sampleip-objs := sampleip_user.o $(TRACE_HELPERS)tc_l2_redirect-objs := tc_l2_redirect_user.olwt_len_hist-objs := lwt_len_hist_user.oxdp_tx_iptunnel-objs := xdp_tx_iptunnel_user.otest_map_in_map-objs := test_map_in_map_user.oper_socket_stats_example-objs := cookie_uid_helper_example.osyscall_tp-objs := syscall_tp_user.ocpustat-objs := cpustat_user.oxdp_adjust_tail-objs := xdp_adjust_tail_user.oxdp_fwd-objs := xdp_fwd_user.otask_fd_query-objs := task_fd_query_user.o $(TRACE_HELPERS)ibumad-objs := ibumad_user.ohbm-objs := hbm.o $(CGROUP_HELPERS) xdp_router_ipv4-objs := xdp_router_ipv4_user.o $(XDP_SAMPLE) # Tell kbuild to always build the programsalways-y := $(tprogs-y)always-y += sockex1_kern.oalways-y += sockex2_kern.oalways-y += sockex3_kern.oalways-y += tracex1.bpf.oalways-y += tracex3.bpf.oalways-y += tracex4.bpf.oalways-y += tracex5.bpf.oalways-y += tracex6.bpf.oalways-y += trace_output.bpf.oalways-y += tcbpf1_kern.oalways-y += tc_l2_redirect_kern.oalways-y += lathist_kern.oalways-y += offwaketime.bpf.oalways-y += spintest.bpf.oalways-y += map_perf_test.bpf.oalways-y += parse_varlen.o parse_simple.o parse_ldabs.oalways-y += trace_event_kern.oalways-y += sampleip_kern.oalways-y += lwt_len_hist.bpf.oalways-y += xdp_tx_iptunnel_kern.oalways-y += test_map_in_map.bpf.oalways-y += tcp_synrto_kern.oalways-y += tcp_rwnd_kern.oalways-y += tcp_bufs_kern.oalways-y += tcp_cong_kern.oalways-y += tcp_iw_kern.oalways-y += tcp_clamp_kern.oalways-y += tcp_basertt_kern.oalways-y += tcp_tos_reflect_kern.oalways-y += tcp_dumpstats_kern.oalways-y += xdp2skb_meta_kern.oalways-y += syscall_tp_kern.oalways-y += cpustat_kern.oalways-y += xdp_adjust_tail_kern.oalways-y += xdp_fwd_kern.oalways-y += task_fd_query_kern.oalways-y += ibumad_kern.oalways-y += hbm_out_kern.oalways-y += hbm_edt_kern.o COMMON_CFLAGS = $(TPROGS_USER_CFLAGS)TPROGS_LDFLAGS = $(TPROGS_USER_LDFLAGS) ifeq ($(ARCH), arm)# Strip all except -D__LINUX_ARM_ARCH__ option needed to handle linux# headers when arm instruction set identification is requested.ARM_ARCH_SELECTOR := $(filter -D__LINUX_ARM_ARCH__%, $(KBUILD_CFLAGS))BPF_EXTRA_CFLAGS := $(ARM_ARCH_SELECTOR)TPROGS_CFLAGS += $(ARM_ARCH_SELECTOR)endif ifeq ($(ARCH), mips)TPROGS_CFLAGS += -D__SANE_USERSPACE_TYPES__ifdef CONFIG_MACH_LOONGSON64BPF_EXTRA_CFLAGS += -I$(srctree)/arch/mips/include/asm/mach-loongson64BPF_EXTRA_CFLAGS += -I$(srctree)/arch/mips/include/asm/mach-genericendifendif ifeq ($(ARCH), x86)BPF_EXTRA_CFLAGS += -fcf-protectionendif COMMON_CFLAGS += -Wall -O2COMMON_CFLAGS += -Wmissing-prototypesCOMMON_CFLAGS += -Wstrict-prototypesCOMMON_CFLAGS += $(call try-run,\	printf "int main() { return 0; }" |\	$(CC) -Werror -fsanitize=bounds -x c - -o "$$TMP",-fsanitize=bounds,) TPROGS_CFLAGS += $(COMMON_CFLAGS)TPROGS_CFLAGS += -I$(objtree)/usr/includeTPROGS_CFLAGS += -I$(srctree)/tools/testing/selftests/bpf/TPROGS_CFLAGS += -I$(LIBBPF_INCLUDE)TPROGS_CFLAGS += -I$(srctree)/tools/includeTPROGS_CFLAGS += -I$(srctree)/tools/perfTPROGS_CFLAGS += -I$(srctree)/tools/libTPROGS_CFLAGS += -DHAVE_ATTR_TEST=0 ifdef SYSROOTCOMMON_CFLAGS += --sysroot=$(SYSROOT)TPROGS_LDFLAGS := -L$(SYSROOT)/usr/libendif TPROGS_LDLIBS			+= $(LIBBPF) -lelf -lzTPROGLDLIBS_xdp_router_ipv4	+= -lm -pthreadTPROGLDLIBS_tracex4		+= -lrtTPROGLDLIBS_trace_output	+= -lrtTPROGLDLIBS_map_perf_test	+= -lrt # Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline:# make M=samples/bpf LLC=~/git/llvm-project/llvm/build/bin/llc CLANG=~/git/llvm-project/llvm/build/bin/clangLLC ?= llcCLANG ?= clangOPT ?= optLLVM_DIS ?= llvm-disLLVM_OBJCOPY ?= llvm-objcopyLLVM_READELF ?= llvm-readelfBTF_PAHOLE ?= pahole # Detect that we're cross compiling and use the cross compilerifdef CROSS_COMPILECLANG_ARCH_ARGS = --target=$(notdir $(CROSS_COMPILE:%-=%))endif # Don't evaluate probes and warnings if we need to run make recursivelyifneq ($(src),)HDR_PROBE := $(shell printf "$(pound)include <linux/types.h>\n struct list_head { int a; }; int main() { return 0; }" | \	$(CC) $(TPROGS_CFLAGS) $(TPROGS_LDFLAGS) -x c - \	-o /dev/null 2>/dev/null && echo okay) ifeq ($(HDR_PROBE),)$(warning WARNING: Detected possible issues with include path.)$(warning WARNING: Please install kernel headers locally (make headers_install).)endif BTF_LLC_PROBE := $(shell $(LLC) -march=bpf -mattr=help 2>&1 | grep dwarfris)BTF_PAHOLE_PROBE := $(shell $(BTF_PAHOLE) --help 2>&1 | grep BTF)BTF_OBJCOPY_PROBE := $(shell $(LLVM_OBJCOPY) --help 2>&1 | grep -i 'usage.*llvm')BTF_LLVM_PROBE := $(shell echo "int main() { return 0; }" | \			  $(CLANG) --target=bpf -O2 -g -c -x c - -o ./llvm_btf_verify.o; \			  $(LLVM_READELF) -S ./llvm_btf_verify.o | grep BTF; \			  /bin/rm -f ./llvm_btf_verify.o) BPF_EXTRA_CFLAGS += -fno-stack-protectorifneq ($(BTF_LLVM_PROBE),)	BPF_EXTRA_CFLAGS += -gelseifneq ($(and $(BTF_LLC_PROBE),$(BTF_PAHOLE_PROBE),$(BTF_OBJCOPY_PROBE)),)	BPF_EXTRA_CFLAGS += -g	LLC_FLAGS += -mattr=dwarfris	DWARF2BTF = yendifendifendif # Trick to allow make to be run from this directoryall:	$(MAKE) -C ../../ M=$(CURDIR) BPF_SAMPLES_PATH=$(CURDIR) clean:	$(MAKE) -C ../../ M=$(CURDIR) clean	@find $(CURDIR) -type f -name '*~' -delete	@$(RM) -r $(CURDIR)/libbpf $(CURDIR)/bpftool $(LIBBPF): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(LIBBPF_OUTPUT)# Fix up variables inherited from Kbuild that tools/ build system won't like	$(MAKE) -C $(LIBBPF_SRC) RM='rm -rf' EXTRA_CFLAGS="$(COMMON_CFLAGS)" \		LDFLAGS="$(TPROGS_LDFLAGS)" srctree=$(BPF_SAMPLES_PATH)/../../ \		O= OUTPUT=$(LIBBPF_OUTPUT)/ DESTDIR=$(LIBBPF_DESTDIR) prefix= \		$@ install_headers BPFTOOLDIR := $(TOOLS_PATH)/bpf/bpftoolBPFTOOL_OUTPUT := $(abspath $(BPF_SAMPLES_PATH))/bpftoolDEFAULT_BPFTOOL := $(BPFTOOL_OUTPUT)/bootstrap/bpftoolBPFTOOL ?= $(DEFAULT_BPFTOOL)$(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT)	$(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../ 		\		OUTPUT=$(BPFTOOL_OUTPUT)/ bootstrap $(LIBBPF_OUTPUT) $(BPFTOOL_OUTPUT):	$(call msg,MKDIR,$@)	$(Q)mkdir -p $@ $(obj)/syscall_nrs.h:	$(obj)/syscall_nrs.s FORCE	$(call filechk,offsets,__SYSCALL_NRS_H__) targets += syscall_nrs.sclean-files += syscall_nrs.h FORCE:  # Verify LLVM compiler tools are available and bpf target is supported by llc.PHONY: verify_cmds verify_target_bpf $(CLANG) $(LLC) verify_cmds: $(CLANG) $(LLC)	@for TOOL in $^ ; do \		if ! (which -- "$${TOOL}" > /dev/null 2>&1); then \			echo "*** ERROR: Cannot find LLVM tool $${TOOL}" ;\			exit 1; \		else true; fi; \	done verify_target_bpf: verify_cmds	@if ! (${LLC} -march=bpf -mattr=help > /dev/null 2>&1); then \		echo "*** ERROR: LLVM (${LLC}) does not support 'bpf' target" ;\		echo "   NOTICE: LLVM version >= 3.7.1 required" ;\		exit 2; \	else true; fi $(BPF_SAMPLES_PATH)/*.c: verify_target_bpf $(LIBBPF)$(src)/*.c: verify_target_bpf $(LIBBPF) libbpf_hdrs: $(LIBBPF)$(obj)/$(TRACE_HELPERS) $(obj)/$(CGROUP_HELPERS) $(obj)/$(XDP_SAMPLE): | libbpf_hdrs .PHONY: libbpf_hdrs $(obj)/xdp_router_ipv4_user.o: $(obj)/xdp_router_ipv4.skel.h $(obj)/tracex5.bpf.o: $(obj)/syscall_nrs.h$(obj)/hbm_out_kern.o: $(src)/hbm.h $(src)/hbm_kern.h$(obj)/hbm.o: $(src)/hbm.h$(obj)/hbm_edt_kern.o: $(src)/hbm.h $(src)/hbm_kern.h # Override includes for xdp_sample_user.o because $(srctree)/usr/include in# TPROGS_CFLAGS causes conflictsXDP_SAMPLE_CFLAGS += -Wall -O2 \		     -I$(src)/../../tools/include \		     -I$(src)/../../tools/include/uapi \		     -I$(LIBBPF_INCLUDE) \		     -I$(src)/../../tools/testing/selftests/bpf $(obj)/$(XDP_SAMPLE): TPROGS_CFLAGS = $(XDP_SAMPLE_CFLAGS) $(TPROGS_USER_CFLAGS)$(obj)/$(XDP_SAMPLE): $(src)/xdp_sample_user.h $(src)/xdp_sample_shared.h# Override includes for trace_helpers.o because __must_check won't be defined# in our include path.$(obj)/$(TRACE_HELPERS): TPROGS_CFLAGS := $(TPROGS_CFLAGS) -D__must_check= -include $(BPF_SAMPLES_PATH)/Makefile.target VMLINUX_BTF_PATHS ?= $(abspath $(if $(O),$(O)/vmlinux))				\		     $(abspath $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux))	\		     $(abspath $(objtree)/vmlinux)VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS)))) $(obj)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL)ifeq ($(VMLINUX_H),)ifeq ($(VMLINUX_BTF),)	$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)",\		build the kernel or set VMLINUX_BTF like "VMLINUX_BTF=/sys/kernel/btf/vmlinux" or VMLINUX_H variable)endif	$(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@else	$(Q)cp "$(VMLINUX_H)" $@endif clean-files += vmlinux.h # Get Clang's default includes on this system, as opposed to those seen by# '--target=bpf'. This fixes "missing" files on some architectures/distros,# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.## Use '-idirafter': Don't interfere with include mechanics except where the# build would have failed anyways.define get_sys_includes$(shell $(1) -v -E - </dev/null 2>&1 \        | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \$(shell $(1) -dM -E - </dev/null | grep '#define __riscv_xlen ' | sed 's/#define /-D/' | sed 's/ /=/')endef CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG)) $(obj)/xdp_router_ipv4.bpf.o: $(obj)/xdp_sample.bpf.o $(obj)/%.bpf.o: $(src)/%.bpf.c $(obj)/vmlinux.h $(src)/xdp_sample.bpf.h $(src)/xdp_sample_shared.h	@echo "  CLANG-BPF " $@	$(Q)$(CLANG) -g -O2 --target=bpf -D__TARGET_ARCH_$(SRCARCH) \		-Wno-compare-distinct-pointer-types -I$(srctree)/include \		-I$(srctree)/samples/bpf -I$(srctree)/tools/include \		-I$(LIBBPF_INCLUDE) $(CLANG_SYS_INCLUDES) \		-c $(filter %.bpf.c,$^) -o $@ LINKED_SKELS := xdp_router_ipv4.skel.hclean-files += $(LINKED_SKELS) xdp_router_ipv4.skel.h-deps := xdp_router_ipv4.bpf.o xdp_sample.bpf.o LINKED_BPF_SRCS := $(patsubst %.bpf.o,%.bpf.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps))) BPF_SRCS_LINKED := $(notdir $(wildcard $(src)/*.bpf.c))BPF_OBJS_LINKED := $(patsubst %.bpf.c,$(obj)/%.bpf.o, $(BPF_SRCS_LINKED))BPF_SKELS_LINKED := $(addprefix $(obj)/,$(LINKED_SKELS)) $(BPF_SKELS_LINKED): $(BPF_OBJS_LINKED) $(BPFTOOL)	@echo "  BPF GEN-OBJ " $(@:.skel.h=)	$(Q)$(BPFTOOL) gen object $(@:.skel.h=.lbpf.o) $(addprefix $(obj)/,$($(@F)-deps))	@echo "  BPF GEN-SKEL" $(@:.skel.h=)	$(Q)$(BPFTOOL) gen skeleton $(@:.skel.h=.lbpf.o) name $(notdir $(@:.skel.h=)) > $@ # asm/sysreg.h - inline assembly used by it is incompatible with llvm.# But, there is no easy way to fix it, so just exclude it since it is# useless for BPF samples.# below we use long chain of commands, clang | opt | llvm-dis | llc,# to generate final object file. 'clang' compiles the source into IR# with native target, e.g., x64, arm64, etc. 'opt' does bpf CORE IR builtin# processing (llvm12) and IR optimizations. 'llvm-dis' converts# 'opt' output to IR, and finally 'llc' generates bpf byte code.$(obj)/%.o: $(src)/%.c	@echo "  CLANG-bpf " $@	$(Q)$(CLANG) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(BPF_EXTRA_CFLAGS) \		-I$(obj) -I$(srctree)/tools/testing/selftests/bpf/ \		-I$(LIBBPF_INCLUDE) $(CLANG_SYS_INCLUDES) \		-D__KERNEL__ -D__BPF_TRACING__ -Wno-unused-value -Wno-pointer-sign \		-D__TARGET_ARCH_$(SRCARCH) -Wno-compare-distinct-pointer-types \		-Wno-gnu-variable-sized-type-not-at-end \		-Wno-address-of-packed-member -Wno-tautological-compare \		-Wno-unknown-warning-option $(CLANG_ARCH_ARGS) \		-fno-asynchronous-unwind-tables \		-I$(srctree)/samples/bpf/ -include asm_goto_workaround.h \		-O2 -emit-llvm -Xclang -disable-llvm-passes -c $< -o - | \		$(OPT) -O2 -mtriple=bpf-pc-linux | $(LLVM_DIS) | \		$(LLC) -march=bpf $(LLC_FLAGS) -filetype=obj -o $@ifeq ($(DWARF2BTF),y)	$(BTF_PAHOLE) -J $@endif