Skip to content

Commit 37a54c5

Browse files
authored
Merge pull request #109 from Atul-source/build-without-kernel-clone
Build without kernel clone
2 parents 8d034bd + a6b53a5 commit 37a54c5

34 files changed

+590
-2604
lines changed

.github/workflows/ci-build-ubuntu.yaml

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ jobs:
1717
- name: Set env variables
1818
run: |
1919
echo "REPO=eBPF-Package-Repository" >> $GITHUB_ENV
20-
echo "BPF_PATH=samples/bpf" >> $GITHUB_ENV
21-
echo "LINUX_SRC_PATH=$GITHUB_WORKSPACE/linux" >> $GITHUB_ENV
2220
echo "DEBIAN_FRONTEND=noninteractive" >> $GITHUB_ENV
2321
echo "TZ=Etc/UTC" >> $GITHUB_ENV
2422
@@ -51,16 +49,9 @@ jobs:
5149
dwarves \
5250
zlib1g \
5351
libelf1 \
54-
pkg-config
55-
56-
- name: Clone dependencies
57-
run: |
58-
git clone --branch v6.8 --depth 1 https://github.com/torvalds/linux.git $LINUX_SRC_PATH
59-
cd $LINUX_SRC_PATH
60-
make olddefconfig
61-
make prepare
62-
yes | make -j$(nproc)
63-
make headers_install
52+
pkg-config \
53+
libbpf-dev \
54+
libzstd-dev
6455
6556
- name: Install bpftool
6657
run: |
@@ -74,45 +65,45 @@ jobs:
7465
- name: Clone kernel function repository
7566
uses: actions/checkout@v4.2.2
7667
with:
77-
path: linux/${{ env.BPF_PATH }}/${{ env.REPO }}/
68+
path: ${{ env.REPO }}
7869

7970
- name: Build eBPF Program and Upload artifacts
8071
run: |
81-
cd $LINUX_SRC_PATH/$BPF_PATH/$REPO
72+
cd $REPO
8273
bash buildscript.sh
8374
8475
- name: Upload xdp-root
8576
uses: actions/upload-artifact@v4
8677
with:
8778
name: xdp-root-linux-artifact-${{ matrix.codename }}
88-
path: ${{ env.LINUX_SRC_PATH }}/${{ env.BPF_PATH }}/${{ env.REPO }}/xdp-root/l3af_xdp_root/*
79+
path: ${{ env.REPO }}/xdp-root/l3af_xdp_root/*
8980

9081
- name: Upload ratelimiting
9182
uses: actions/upload-artifact@v4
9283
with:
9384
name: ratelimiting-linux-artifact-${{ matrix.codename }}
94-
path: ${{ env.LINUX_SRC_PATH }}/${{ env.BPF_PATH }}/${{ env.REPO }}/ratelimiting/l3af_ratelimiting/*
85+
path: ${{ env.REPO }}/ratelimiting/l3af_ratelimiting/*
9586

9687
- name: Upload connection-limit
9788
uses: actions/upload-artifact@v4
9889
with:
9990
name: connection-limit-linux-artifact-${{ matrix.codename }}
100-
path: ${{ env.LINUX_SRC_PATH }}/${{ env.BPF_PATH }}/${{ env.REPO }}/connection-limit/l3af_connection_limit/*
91+
path: ${{ env.REPO }}/connection-limit/l3af_connection_limit/*
10192

10293
- name: Upload tc-root
10394
uses: actions/upload-artifact@v4
10495
with:
10596
name: tc-root-linux-artifact-${{ matrix.codename }}
106-
path: ${{ env.LINUX_SRC_PATH }}/${{ env.BPF_PATH }}/${{ env.REPO }}/tc-root/l3af_tc_root/*
97+
path: ${{ env.REPO }}/tc-root/l3af_tc_root/*
10798

10899
- name: Upload ipfix-flow-exporter
109100
uses: actions/upload-artifact@v4
110101
with:
111102
name: ipfix-flow-exporter-linux-artifact-${{ matrix.codename }}
112-
path: ${{ env.LINUX_SRC_PATH }}/${{ env.BPF_PATH }}/${{ env.REPO }}/ipfix-flow-exporter/l3af_bpf_ipfix/*
103+
path: ${{ env.REPO }}/ipfix-flow-exporter/l3af_bpf_ipfix/*
113104

114105
- name: Upload traffic-mirroring
115106
uses: actions/upload-artifact@v4
116107
with:
117108
name: traffic-mirroring-linux-artifact-${{ matrix.codename }}
118-
path: ${{ env.LINUX_SRC_PATH }}/${{ env.BPF_PATH }}/${{ env.REPO }}/traffic-mirroring/l3af_traffic_mirroring/*
109+
path: ${{ env.REPO }}/traffic-mirroring/l3af_traffic_mirroring/*

buildscript.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ declare -a progs=("xdp-root" "ratelimiting" "connection-limit" "tc-root" "ipfix-
99
for prog in "${progs[@]}"
1010
do
1111
cd $prog
12-
make
12+
make $1
1313
cd ../
1414
done

connection-limit/Makefile

Lines changed: 47 additions & 196 deletions
Original file line numberDiff line numberDiff line change
@@ -1,222 +1,73 @@
1+
# Copyright Contributors to the L3AF Project.
12
# 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
66
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
3426
endif
35-
endif
36-
37-
TPROGS_CFLAGS += -Wall -O2
38-
TPROGS_CFLAGS += -Wmissing-prototypes
39-
TPROGS_CFLAGS += -Wstrict-prototypes
4027

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 +=
5134
endif
5235

53-
TPROGS_LDLIBS += $(LIBBPF) -lelf -lz
5436

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
6344

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
6845

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
7947

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 > $@;
8751

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 $@
9955

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)
10758

10859
tar.zip:
10960
@rm -rf l3af_connection_limit
11061
@rm -f l3af_connection_limit.tar.gz
11162
@mkdir l3af_connection_limit
11263
@cp $(L3AF_SRC_PATH)/connection_limit.bpf.o l3af_connection_limit/
113-
@cp $(L3AF_SRC_PATH)/connection_limit_kern.o l3af_connection_limit/
11464
@cp $(L3AF_SRC_PATH)/connection_limit l3af_connection_limit/
11565
@tar -cvf l3af_connection_limit.tar ./l3af_connection_limit
11666
@gzip l3af_connection_limit.tar
11767

11868
clean:
119-
$(MAKE) -C $(LINUX_SRC_PATH) M=$(L3AF_SRC_PATH) clean
12069
@find $(CURDIR) -type f -name '*~' -delete
12170
@rm -f ./*.o
71+
@rm -f connection_limit
12272
@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

Comments
 (0)