|
| 1 | +# Copyright Contributors to the L3AF Project. |
1 | 2 | # SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) |
2 | | - |
3 | | -LINUX_SRC_PATH ?= /usr/src/linux |
4 | | -BPF_SAMPLES_PATH := $(LINUX_SRC_PATH)/samples/bpf |
5 | | -TOOLS_PATH := $(BPF_SAMPLES_PATH)/../../tools |
| 3 | +BPF_CLANG ?= clang |
| 4 | +USER_PROG := connection_limit_user.c |
| 5 | +USER_OBJ := connection_limit |
6 | 6 | L3AF_SRC_PATH := $(CURDIR) |
7 | | -HEADERS := $(BPF_SAMPLES_PATH)/eBPF-Package-Repository/headers |
8 | | -# List of programs to build |
9 | | -tprogs-y := connection_limit |
10 | | - |
11 | | -# Libbpf dependencies |
12 | | -LIBBPF = $(TOOLS_PATH)/lib/bpf/libbpf.a |
13 | | - |
14 | | -connection_limit-objs := connection_limit_user.o |
15 | | - |
16 | | -# Tell kbuild to always build the programs |
17 | | -always-y := $(tprogs-y) |
18 | | -always-y += connection_limit.bpf.o connection_limit_kern.o |
19 | | - |
20 | | - |
21 | | -ifeq ($(ARCH), arm) |
22 | | -# Strip all except -D__LINUX_ARM_ARCH__ option needed to handle linux |
23 | | -# headers when arm instruction set identification is requested. |
24 | | -ARM_ARCH_SELECTOR := $(filter -D__LINUX_ARM_ARCH__%, $(KBUILD_CFLAGS)) |
25 | | -BPF_EXTRA_CFLAGS := $(ARM_ARCH_SELECTOR) |
26 | | -TPROGS_CFLAGS += $(ARM_ARCH_SELECTOR) |
27 | | -endif |
28 | | - |
29 | | -ifeq ($(ARCH), mips) |
30 | | -TPROGS_CFLAGS += -D__SANE_USERSPACE_TYPES__ |
31 | | -ifdef CONFIG_MACH_LOONGSON64 |
32 | | -BPF_EXTRA_CFLAGS += -I$(srctree)/arch/mips/include/asm/mach-loongson64 |
33 | | -BPF_EXTRA_CFLAGS += -I$(srctree)/arch/mips/include/asm/mach-generic |
| 7 | +KERNEL_RELEASE := $(shell uname -r) |
| 8 | +LIB_PATH := /usr/src/linux-headers-$(KERNEL_RELEASE) |
| 9 | +BPF_OBJ := connection_limit.bpf.o |
| 10 | +# BPF FlAGS |
| 11 | +BPF_CFLAGS := -g -O2 -target bpf -I. |
| 12 | +BPF_CFLAGS += -I$(LIB_PATH)/tools/testing/selftests/bpf/ |
| 13 | +BPF_CFLAGS += -I$(LIB_PATH)/tools/lib/ |
| 14 | +BPF_CFLAGS += -I$(LIB_PATH)/tools/include |
| 15 | +BPF_CFLAGS += -I$(LIB_PATH)/tools/perf |
| 16 | +BPF_CFLAGS += -I$(LIB_PATH)/tools/ |
| 17 | +BPF_CFLAGS += -I$(LIB_PATH)/tools/bpf/resolve_btfids/libbpf |
| 18 | +BPF_CFLAGS += -I$(LIB_PATH)/tools/bpf/resolve_btfids/libbpf/include |
| 19 | +BPF_CFLAGS += -emit-llvm |
| 20 | +ARCH := $(shell uname -m) |
| 21 | + |
| 22 | +ifeq ($(ARCH),x86_64) |
| 23 | + BPF_CFLAGS += -D__TARGET_ARCH_x86 |
| 24 | +else ifeq ($(ARCH),aarch64) |
| 25 | + BPF_CFLAGS += -D__TARGET_ARCH_arm64 |
34 | 26 | endif |
35 | | -endif |
36 | | - |
37 | | -TPROGS_CFLAGS += -Wall -O2 |
38 | | -TPROGS_CFLAGS += -Wmissing-prototypes |
39 | | -TPROGS_CFLAGS += -Wstrict-prototypes |
40 | 27 |
|
41 | | -TPROGS_CFLAGS += -I$(objtree)/usr/include |
42 | | -TPROGS_CFLAGS += -I$(srctree)/tools/testing/selftests/bpf/ |
43 | | -TPROGS_CFLAGS += -I$(srctree)/tools/lib/ |
44 | | -TPROGS_CFLAGS += -I$(srctree)/tools/include |
45 | | -TPROGS_CFLAGS += -I$(srctree)/tools/perf |
46 | | -TPROGS_CFLAGS += -DHAVE_ATTR_TEST=0 |
47 | | - |
48 | | -ifdef SYSROOT |
49 | | -TPROGS_CFLAGS += --sysroot=$(SYSROOT) |
50 | | -TPROGS_LDFLAGS := -L$(SYSROOT)/usr/lib |
| 28 | +ifeq ($(ARCH),aarch64) |
| 29 | + BPF_CFLAGS += -D__LINUX_ARM_ARCH__=8 |
| 30 | +else ifeq ($(ARCH),armv7l) |
| 31 | + BPF_CFLAGS += -D__LINUX_ARM_ARCH__=7 |
| 32 | +else |
| 33 | + BPF_CFLAGS += |
51 | 34 | endif |
52 | 35 |
|
53 | | -TPROGS_LDLIBS += $(LIBBPF) -lelf -lz |
54 | 36 |
|
55 | | -# Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline: |
56 | | -# make M=samples/bpf LLC=~/git/llvm-project/llvm/build/bin/llc CLANG=~/git/llvm-project/llvm/build/bin/clang |
57 | | -LLC ?= llc |
58 | | -CLANG ?= clang |
59 | | -OPT ?= opt |
60 | | -LLVM_DIS ?= llvm-dis |
61 | | -LLVM_OBJCOPY ?= llvm-objcopy |
62 | | -BTF_PAHOLE ?= pahole |
| 37 | +# USER PROGRAM FLAGS |
| 38 | +USER_CFLAGS := -g -O2 -Wall -I. -I/usr/include/bpf -Wunused-but-set-variable -Wno-pointer-sign -Wno-compare-distinct-pointer-types -Wno-gnu-variable-sized-type-not-at-end -Wno-address-of-packed-member -Wno-tautological-compare -fno-asynchronous-unwind-tables |
| 39 | +USER_CFLAGS += -I$(LIB_PATH)/tools/testing/selftests/bpf |
| 40 | +USER_CFLAGS += -I$(LIB_PATH)/tools/lib |
| 41 | +USER_CFLAGS += -I$(LIB_PATH)/tools/include |
| 42 | +USER_CFLAGS += -I$(LIB_PATH)/tools/perf |
| 43 | +USER_LDFLAGS := -lbpf -lelf -lz |
63 | 44 |
|
64 | | -# Detect that we're cross compiling and use the cross compiler |
65 | | -ifdef CROSS_COMPILE |
66 | | -CLANG_ARCH_ARGS = --target=$(notdir $(CROSS_COMPILE:%-=%)) |
67 | | -endif |
68 | 45 |
|
69 | | -# Don't evaluate probes and warnings if we need to run make recursively |
70 | | -ifneq ($(src),) |
71 | | -HDR_PROBE := $(shell printf "\#include <linux/types.h>\n struct list_head { int a; }; int main() { return 0; }" | \ |
72 | | - $(CC) $(TPROGS_CFLAGS) $(TPROGS_LDFLAGS) -x c - \ |
73 | | - -o /dev/null 2>/dev/null && echo okay) |
74 | | - |
75 | | -ifeq ($(HDR_PROBE),) |
76 | | -$(warning WARNING: Detected possible issues with include path.) |
77 | | -$(warning WARNING: Please install kernel headers locally (make headers_install).) |
78 | | -endif |
| 46 | +all: $(BPF_OBJ) $(USER_OBJ) tar.zip |
79 | 47 |
|
80 | | -BTF_LLC_PROBE := $(shell $(LLC) -march=bpf -mattr=help 2>&1 | grep dwarfris) |
81 | | -BTF_PAHOLE_PROBE := $(shell $(BTF_PAHOLE) --help 2>&1 | grep BTF) |
82 | | -BTF_OBJCOPY_PROBE := $(shell $(LLVM_OBJCOPY) --help 2>&1 | grep -i 'usage.*llvm') |
83 | | -BTF_LLVM_PROBE := $(shell echo "int main() { return 0; }" | \ |
84 | | - $(CLANG) -target bpf -O2 -g -c -x c - -o ./llvm_btf_verify.o; \ |
85 | | - readelf -S ./llvm_btf_verify.o | grep BTF; \ |
86 | | - /bin/rm -f ./llvm_btf_verify.o) |
| 48 | +$(LIB_PATH)/tools/vmlinux.h: |
| 49 | + @echo " Generating vmlinux.h" $@ |
| 50 | + bpftool btf dump file /sys/kernel/btf/vmlinux format c > $@; |
87 | 51 |
|
88 | | -BPF_EXTRA_CFLAGS += -fno-stack-protector |
89 | | -ifneq ($(BTF_LLVM_PROBE),) |
90 | | - BPF_EXTRA_CFLAGS += -g |
91 | | -else |
92 | | -ifneq ($(and $(BTF_LLC_PROBE),$(BTF_PAHOLE_PROBE),$(BTF_OBJCOPY_PROBE)),) |
93 | | - BPF_EXTRA_CFLAGS += -g |
94 | | - LLC_FLAGS += -mattr=dwarfris |
95 | | - DWARF2BTF = y |
96 | | -endif |
97 | | -endif |
98 | | -endif |
| 52 | +%.bpf.o: %.bpf.c $(LIB_PATH)/tools/vmlinux.h |
| 53 | + @echo "Compiling BPF program: $< -> $@" |
| 54 | + $(BPF_CLANG) $(BPF_CFLAGS) -c $< -o - | llc -march=bpf -filetype=obj -o $@ |
99 | 55 |
|
100 | | -# Trick to allow make to be run from this directory |
101 | | -all:build tar.zip |
102 | | -build: |
103 | | - if [ ! -f $(L3AF_SRC_PATH)/vmlinux.h ]; then \ |
104 | | - bpftool btf dump file /sys/kernel/btf/vmlinux format c > $(L3AF_SRC_PATH)/vmlinux.h; \ |
105 | | - fi |
106 | | - $(MAKE) -C $(LINUX_SRC_PATH) M=$(L3AF_SRC_PATH) |
| 56 | +$(USER_OBJ): $(USER_PROG) |
| 57 | + $(BPF_CLANG) $(USER_CFLAGS) $< -o $@ $(USER_LDFLAGS) |
107 | 58 |
|
108 | 59 | tar.zip: |
109 | 60 | @rm -rf l3af_connection_limit |
110 | 61 | @rm -f l3af_connection_limit.tar.gz |
111 | 62 | @mkdir l3af_connection_limit |
112 | 63 | @cp $(L3AF_SRC_PATH)/connection_limit.bpf.o l3af_connection_limit/ |
113 | | - @cp $(L3AF_SRC_PATH)/connection_limit_kern.o l3af_connection_limit/ |
114 | 64 | @cp $(L3AF_SRC_PATH)/connection_limit l3af_connection_limit/ |
115 | 65 | @tar -cvf l3af_connection_limit.tar ./l3af_connection_limit |
116 | 66 | @gzip l3af_connection_limit.tar |
117 | 67 |
|
118 | 68 | clean: |
119 | | - $(MAKE) -C $(LINUX_SRC_PATH) M=$(L3AF_SRC_PATH) clean |
120 | 69 | @find $(CURDIR) -type f -name '*~' -delete |
121 | 70 | @rm -f ./*.o |
| 71 | + @rm -f connection_limit |
122 | 72 | @rm -f l3af_connection_limit.tar.gz |
123 | | - @rm -rf $(L3AF_SRC_PATH)/vmlinux.h |
124 | | - |
125 | | -$(LIBBPF): FORCE |
126 | | -# Fix up variables inherited from Kbuild that tools/ build system won't like |
127 | | - $(MAKE) -C $(dir $@) RM='rm -rf' EXTRA_CFLAGS="$(TPROGS_CFLAGS)" \ |
128 | | - LDFLAGS=$(TPROGS_LDFLAGS) srctree=$(BPF_SAMPLES_PATH)/../../ O= |
129 | | - |
130 | | -BPFTOOLDIR := $(TOOLS_PATH)/bpf/bpftool |
131 | | -BPFTOOL := $(BPFTOOLDIR)/bpftool |
132 | | -$(BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) |
133 | | - $(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../ |
134 | | - |
135 | | -$(obj)/syscall_nrs.h: $(obj)/syscall_nrs.s FORCE |
136 | | - $(call filechk,offsets,__SYSCALL_NRS_H__) |
137 | | - |
138 | | -targets += syscall_nrs.s |
139 | | -clean-files += syscall_nrs.h |
140 | | - |
141 | | -FORCE: |
142 | | - |
143 | | - |
144 | | -# Verify LLVM compiler tools are available and bpf target is supported by llc |
145 | | -.PHONY: verify_cmds verify_target_bpf $(CLANG) $(LLC) |
146 | | - |
147 | | -verify_cmds: $(CLANG) $(LLC) |
148 | | - @for TOOL in $^ ; do \ |
149 | | - if ! (which -- "$${TOOL}" > /dev/null 2>&1); then \ |
150 | | - echo "*** ERROR: Cannot find LLVM tool $${TOOL}" ;\ |
151 | | - exit 1; \ |
152 | | - else true; fi; \ |
153 | | - done |
154 | | - |
155 | | -verify_target_bpf: verify_cmds |
156 | | - @if ! (${LLC} -march=bpf -mattr=help > /dev/null 2>&1); then \ |
157 | | - echo "*** ERROR: LLVM (${LLC}) does not support 'bpf' target" ;\ |
158 | | - echo " NOTICE: LLVM version >= 3.7.1 required" ;\ |
159 | | - exit 2; \ |
160 | | - else true; fi |
161 | | - |
162 | | -$(BPF_SAMPLES_PATH)/*.c: verify_target_bpf $(LIBBPF) |
163 | | -$(src)/*.c: verify_target_bpf $(LIBBPF) |
164 | | - |
165 | | - |
166 | | --include $(BPF_SAMPLES_PATH)/Makefile.target |
167 | | - |
168 | | -VMLINUX_BTF_PATHS ?= $(abspath $(if $(O),$(O)/vmlinux)) \ |
169 | | - $(abspath $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux)) \ |
170 | | - $(abspath ./vmlinux) |
171 | | -VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS)))) |
172 | | - |
173 | | -clean-files += vmlinux.h |
174 | | - |
175 | | -# Get Clang's default includes on this system, as opposed to those seen by |
176 | | -# '-target bpf'. This fixes "missing" files on some architectures/distros, |
177 | | -# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc. |
178 | | -# |
179 | | -# Use '-idirafter': Don't interfere with include mechanics except where the |
180 | | -# build would have failed anyways. |
181 | | -define get_sys_includes |
182 | | -$(shell $(1) -v -E - </dev/null 2>&1 \ |
183 | | - | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \ |
184 | | -$(shell $(1) -dM -E - </dev/null | grep '#define __riscv_xlen ' | sed 's/#define /-D/' | sed 's/ /=/') |
185 | | -endef |
186 | | - |
187 | | -CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG)) |
188 | | - |
189 | | -# |
190 | | - |
191 | | -$(obj)/%.bpf.o: $(src)/%.bpf.c $(BPF_SAMPLES_PATH)/xdp_sample.bpf.h $(BPF_SAMPLES_PATH)/xdp_sample_shared.h |
192 | | - @echo " CLANG-BPF " $@ |
193 | | - $(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(SRCARCH) \ |
194 | | - -Wno-compare-distinct-pointer-types -I$(srctree)/include \ |
195 | | - -I$(srctree)/samples/bpf -I$(srctree)/tools/include \ |
196 | | - -I$(srctree)/tools/lib -I$(srctree)/tools/lib/bpf $(CLANG_SYS_INCLUDES) \ |
197 | | - -c $(filter %.bpf.c,$^) -o $@ |
198 | | - |
199 | | -# asm/sysreg.h - inline assembly used by it is incompatible with llvm. |
200 | | -# But, there is no easy way to fix it, so just exclude it since it is |
201 | | -# useless for BPF samples. |
202 | | -# below we use long chain of commands, clang | opt | llvm-dis | llc, |
203 | | -# to generate final object file. 'clang' compiles the source into IR |
204 | | -# with native target, e.g., x64, arm64, etc. 'opt' does bpf CORE IR builtin |
205 | | -# processing (llvm12) and IR optimizations. 'llvm-dis' converts |
206 | | -# 'opt' output to IR, and finally 'llc' generates bpf byte code. |
207 | | - |
208 | | -$(obj)/%.o: $(src)/%.c |
209 | | - @echo " CLANG-bpf " $@ |
210 | | - $(Q)$(CLANG) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(BPF_EXTRA_CFLAGS) \ |
211 | | - -I$(obj) -I$(srctree)/tools/testing/selftests/bpf/ \ |
212 | | - -I$(srctree)/tools/lib/ -I$(srctree)/tools/lib/bpf \ |
213 | | - -D__KERNEL__ -D__BPF_TRACING__ -Wno-unused-value -Wno-pointer-sign \ |
214 | | - -D__TARGET_ARCH_$(SRCARCH) -Wno-compare-distinct-pointer-types \ |
215 | | - -Wno-gnu-variable-sized-type-not-at-end \ |
216 | | - -Wno-address-of-packed-member -Wno-tautological-compare \ |
217 | | - -Wno-unknown-warning-option $(CLANG_ARCH_ARGS) \ |
218 | | - -fno-asynchronous-unwind-tables \ |
219 | | - -I$(srctree)/samples/bpf/ -include asm_goto_workaround.h \ |
220 | | - -O2 -emit-llvm -Xclang -disable-llvm-passes -c $< -o - | \ |
221 | | - $(OPT) -O2 -mtriple=bpf-pc-linux | $(LLVM_DIS) | \ |
222 | | - $(LLC) -march=bpf $(LLC_FLAGS) -filetype=obj -o $@ |
| 73 | + @rm -rf l3af_connection_limit |
0 commit comments