当前位置: 首页 > news >正文

ebpf1 - 小镇

ebpf1

如果你要的是:

CentOS 7.9 从零开始,纯源码方式,最终得到可开发 TLS Tracing 的完整 eBPF 环境

那么不要混用 yum 包、第三方仓库、ELRepo。

直接按下面顺序做。

目标版本

OS          CentOS 7.9Kernel      5.10.216GCC         9.5Python      3.8.18CMake       3.26.5LLVM        14.0.6libbpf      1.4+bpftool     最新主线pahole      1.24+

第一阶段:基础编译环境

安装系统依赖

yum groupinstall -y "Development Tools"yum install -y \
wget \
curl \
git \
flex \
bison \
bc \
perl \
rsync \
openssl-devel \
elfutils-libelf-devel \
ncurses-devel \
zlib-devel \
xz-devel \
readline-devel \
sqlite-devel \
libffi-devel \
glibc-devel

验证:

gcc --version
g++ --version
make --version

第二阶段:源码编译 Python 3.8

CentOS7 默认 Python2 不能满足 LLVM 构建。

cd /usr/srcwget https://www.python.org/ftp/python/3.8.18/Python-3.8.18.tgztar xf Python-3.8.18.tgzcd Python-3.8.18./configure \
--prefix=/opt/python38 \
--enable-optimizationsmake -j$(nproc)make install

配置 PATH:

echo 'export PATH=/opt/python38/bin:$PATH' >> /etc/profilesource /etc/profile

验证:

python3 --version

应为:

Python 3.8.18

第三阶段:源码编译 CMake

cd /usr/srcwget https://github.com/Kitware/CMake/releases/download/v3.26.5/cmake-3.26.5.tar.gztar xf cmake-3.26.5.tar.gzcd cmake-3.26.5./bootstrapmake -j$(nproc)make install

验证:

cmake --version

第四阶段:源码编译 GCC 9.5

CentOS7 自带 GCC4.8 太老。

下载

cd /usr/srcwget https://ftp.gnu.org/gnu/gcc/gcc-9.5.0/gcc-9.5.0.tar.gztar xf gcc-9.5.0.tar.gzcd gcc-9.5.0

下载依赖:

./contrib/download_prerequisites

编译

mkdir buildcd build

配置:

../configure \
--prefix=/opt/gcc95 \
--enable-languages=c,c++ \
--disable-multilib

编译:

make -j$(nproc)

安装:

make install

配置环境:

echo 'export PATH=/opt/gcc95/bin:$PATH' >> /etc/profileecho 'export LD_LIBRARY_PATH=/opt/gcc95/lib64:$LD_LIBRARY_PATH' >> /etc/profilesource /etc/profile

验证:

gcc --version

输出:

gcc 9.5.0

第五阶段:源码编译 pahole

BTF 生成器。

cd /usr/srcgit clone https://github.com/acmel/dwarves.gitcd dwarves
mkdir buildcd build
cmake ..
make -j$(nproc)make install

验证:

pahole --version

建议:

1.24+

第六阶段:源码编译 Linux Kernel 5.10.216

下载:

cd /usr/srcwget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.10.216.tar.xztar xf linux-5.10.216.tar.xzcd linux-5.10.216

导入配置

cp /boot/config-$(uname -r) .configmake olddefconfig

打开配置

make menuconfig

必须启用:

CONFIG_BPF=y
CONFIG_BPF_SYSCALL=y
CONFIG_BPF_JIT=yCONFIG_KPROBES=y
CONFIG_UPROBES=yCONFIG_TRACEPOINTS=yCONFIG_PERF_EVENTS=yCONFIG_DEBUG_INFO=y
CONFIG_DEBUG_INFO_BTF=y

编译

make -j$(nproc)

安装

make modules_installmake install

更新 grub:

grub2-mkconfig -o /boot/grub2/grub.cfg

重启:

reboot

验证:

uname -r

应为:

5.10.216

第七阶段:源码编译 LLVM14

下载:

cd /usr/srcgit clone https://github.com/llvm/llvm-project.gitcd llvm-projectgit fetch --tagsgit checkout llvmorg-14.0.6

创建目录:

mkdir buildcd build

配置:

cmake ../llvm \
-DLLVM_ENABLE_PROJECTS="clang;lld" \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_TARGETS_TO_BUILD="X86;BPF"

注意:

BPF 必须出现

否则:

clang -target bpf

会报:

unknown target triple 'bpf'

编译:

make -j$(nproc)

安装:

make install

验证:

llc --version

必须看到:

bpf
bpfel
bpfeb

测试:

echo 'int x=1;' > test.cclang -target bpf -c test.c -o test.o

成功即可。


第八阶段:源码编译 libbpf

cd /usr/srcgit clone https://github.com/libbpf/libbpf.gitcd libbpf/src
make
make install

验证:

pkg-config --modversion libbpf

第九阶段:源码编译 bpftool

cd /usr/srcgit clone --recurse-submodules https://github.com/libbpf/bpftool.gitcd bpftool/src
make -j$(nproc)
make install

验证:

bpftool version

第十阶段:环境验收

Kernel

uname -r

结果:

5.10.216

BPF

grep CONFIG_BPF /boot/config-$(uname -r)

必须:

CONFIG_BPF=y
CONFIG_BPF_SYSCALL=y

Uprobe

grep CONFIG_UPROBES /boot/config-$(uname -r)

必须:

CONFIG_UPROBES=y

Kprobe

grep CONFIG_KPROBES /boot/config-$(uname -r)

必须:

CONFIG_KPROBES=y

BPF Syscall

bpftool feature probe kernel

必须出现:

bpf() syscall is available

LLVM

llc --version

必须出现:

bpf
bpfel
bpfeb

Clang BPF

clang -target bpf -c test.c -o test.o

成功。


BTF(可选但推荐)

ls /sys/kernel/btf/vmlinux

存在最好。

不存在也不影响传统 Uprobe/Kprobe 开发,但会影响 CO-RE 和现代 libbpf 工作流。


当上述全部通过后,你就具备了开发:

  • OpenSSL SSL_do_handshake

  • SSL_read

  • SSL_write

  • Nginx TLS 全链路追踪

  • ClientHello/ServerHello 分析

  • TLS Alert 分析

  • FIN/RST 根因定位

  • TLS Session Reconstruction

等生产级 eBPF 工具的完整开发环境。