Skip to content

Commit ee40256

Browse files
puranjaymohantheihor
authored andcommitted
Add an action to build cilium bpf programs
Add an action that clones the upstream cilium repo and builds the bpf programs inside it. The build script modifies some files in the bpf .c and .h files to make it work with veristat. Signed-off-by: Puranjay Mohan <puranjay@kernel.org>
1 parent af3742c commit ee40256

5 files changed

Lines changed: 147 additions & 0 deletions

File tree

build-cilium/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Build cilium bpf programs
2+
3+
This action builds programs in bpf/ directory from https://github.com/cilium/cilium
4+
5+
## Required inputs
6+
7+
* `output-dir` - Path to the output directory, where built artifacts will be placed.
8+
9+
## Environment variables
10+
11+
* `LLVM_VERSION` - LLVM (clang) version. Default: `20`.
12+
* `CILIUM_ROOT` - Path to the cilium repository. If not set (default), the action will clone the main branch of https://github.com/cilium/cilium to a temporary directory.

build-cilium/action.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: 'Build cilium/cilium bpf programs'
2+
inputs:
3+
output-dir:
4+
description: 'Path to the output of cilium build'
5+
required: true
6+
7+
runs:
8+
using: "composite"
9+
steps:
10+
11+
- name: Install cilium dependencies
12+
shell: bash
13+
run: ${GITHUB_ACTION_PATH}/install-dependencies.sh
14+
15+
- name: Build cilium/cilium bpf programs
16+
env:
17+
OUTPUT_DIR: ${{ inputs.output-dir }}
18+
shell: bash
19+
run: ${GITHUB_ACTION_PATH}/build-cilium.sh

build-cilium/build-cilium.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
export CILIUM_ROOT=${CILIUM_ROOT:-}
6+
export CILIUM_REVISION=${CILIUM_REVISION:-main}
7+
8+
if [[ -z "$CILIUM_ROOT" ]]; then
9+
export CILIUM_ROOT=$(mktemp -d scx.XXXX)
10+
git clone --depth=1 --branch="${CILIUM_REVISION}" https://github.com/cilium/cilium.git $CILIUM_ROOT
11+
pushd $CILIUM_ROOT
12+
git reset --hard $CILIUM_REVISION
13+
popd
14+
fi
15+
16+
pushd $CILIUM_ROOT
17+
18+
rm -rf $OUTPUT_DIR && mkdir -p $OUTPUT_DIR/bpf
19+
20+
extract_bpf_progs() {
21+
build_dir=$1
22+
pattern=$2
23+
bpf_dir=$3
24+
find "${build_dir}" -type f -name "$pattern" -printf '%P\0' | \
25+
while IFS= read -r -d '' prog; do
26+
obj_name=$(echo "${prog}" | tr / _)
27+
cp -v "${build_dir}/${prog}" "${bpf_dir}/${obj_name}"
28+
done
29+
}
30+
31+
# Cilium needs some hacks to work properly with upstream kernel and libbpf
32+
pushd bpf
33+
sed -i 's/CILIUM_PIN_REPLACE 1 << 4/CILIUM_PIN_REPLACE 1/' include/bpf/loader.h
34+
sed -i 's/__section(PROG_TYPE "\/entry")/__section(PROG_TYPE)/' include/bpf/section.h
35+
sed -i 's/__section(PROG_TYPE "\/tail")/__section(PROG_TYPE)/' lib/tailcall.h
36+
sed -i '/__declare_tail/{n;/^static __always_inline$/d;}' bpf_host.c bpf_lxc.c lib/nodeport.h lib/nodeport_egress.h
37+
sed -i '/^static __always_inline.*\\$/d' bpf_lxc.c
38+
make -j$(nproc)
39+
popd
40+
41+
extract_bpf_progs bpf "*.o" $OUTPUT_DIR/bpf
42+
43+
popd
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/bin/bash
2+
3+
set -xeuo pipefail
4+
5+
export LLVM_VERSION=${LLVM_VERSION:-21}
6+
export LIBBPF_REVISION=${LIBBPF_REVISION:-master}
7+
export BPFTOOL_REVISION=${BPFTOOL_REVISION:-main}
8+
9+
# Assume Ubuntu/Debian
10+
export DEBIAN_FRONTEND=noninteractive
11+
sudo -E apt-get -y update
12+
13+
# Install LLVM
14+
sudo -E apt-get --no-install-recommends -y install \
15+
curl git gnupg lsb-release software-properties-common wget
16+
wget https://apt.llvm.org/llvm.sh
17+
chmod +x llvm.sh
18+
sudo -E ./llvm.sh ${LLVM_VERSION}
19+
rm llvm.sh
20+
21+
sudo update-alternatives --install \
22+
/usr/bin/clang clang /usr/bin/clang-${LLVM_VERSION} 10
23+
sudo update-alternatives --set clang /usr/bin/clang-${LLVM_VERSION}
24+
sudo update-alternatives --install \
25+
/usr/bin/llvm-strip llvm-strip /usr/bin/llvm-strip-${LLVM_VERSION} 10
26+
sudo update-alternatives --set llvm-strip /usr/bin/llvm-strip-${LLVM_VERSION}
27+
sudo update-alternatives --install \
28+
/usr/bin/llvm-ar llvm-ar /usr/bin/llvm-ar-${LLVM_VERSION} 10
29+
sudo update-alternatives --set llvm-ar /usr/bin/llvm-ar-${LLVM_VERSION}
30+
31+
# Install Go
32+
export GO_VERSION=${GO_VERSION:-1.25.3}
33+
ARCH=$(uname -m)
34+
case $ARCH in
35+
x86_64) GO_ARCH=amd64 ;;
36+
aarch64) GO_ARCH=arm64 ;;
37+
*) echo "Unsupported architecture: $ARCH"; exit 1 ;;
38+
esac
39+
curl -fsSL https://go.dev/dl/go${GO_VERSION}.linux-${GO_ARCH}.tar.gz | sudo tar -C /usr/local -xzf -
40+
echo 'export PATH=$PATH:/usr/local/go/bin' | sudo tee /etc/profile.d/go.sh
41+
export PATH=$PATH:/usr/local/go/bin
42+
# Persist Go PATH for subsequent GitHub Actions steps
43+
echo "/usr/local/go/bin" >> $GITHUB_PATH
44+
45+
# Install libs and other deps
46+
sudo -E apt-get --no-install-recommends -y install \
47+
build-essential libssl-dev libelf-dev libzstd-dev libseccomp-dev \
48+
libbfd-dev libcap-dev jq pkg-config protobuf-compiler
49+
50+
# Build and install libbpf
51+
export LIBBPF_ROOT=$(mktemp -d libbpf.XXXX)
52+
git clone https://github.com/libbpf/libbpf.git $LIBBPF_ROOT
53+
pushd $LIBBPF_ROOT
54+
git reset --hard $LIBBPF_REVISION
55+
make -C src -j$(nproc)
56+
make -C src install
57+
sudo ln -s /usr/lib64/pkgconfig/libbpf.pc /usr/lib/pkgconfig/libbpf.pc
58+
popd
59+
rm -rf $LIBBPF_ROOT
60+
61+
# Build and install bpftool
62+
export BPFTOOL_ROOT=$(mktemp -d bpftool.XXXX)
63+
git clone --recurse-submodules https://github.com/libbpf/bpftool.git $BPFTOOL_ROOT
64+
pushd $BPFTOOL_ROOT
65+
git reset --hard $BPFTOOL_REVISION
66+
git submodule update --init
67+
make LLVM=1 LLVM_VERSION=-${LLVM_VERSION} -C src -j$(nproc)
68+
make LLVM=1 LLVM_VERSION=-${LLVM_VERSION} -C src install
69+
popd
70+
rm -rf $BPFTOOL_ROOT
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
VERISTAT_OBJECTS_DIR="${CILIUM_PROGS}"
2+
VERISTAT_OBJECTS_GLOB="*.o"
3+
VERISTAT_OUTPUT="veristat-cilium"

0 commit comments

Comments
 (0)