0%

eBPF:Ubuntu平台使用源码编译BCC

我个人非常不推荐使用APT方式安装
我个人非常不推荐使用APT方式安装
我个人非常不推荐使用APT方式安装

已经踩过坑了,APT方式安装的版本比较旧,导致程序跑不起来。

本文使用ARM虚拟机示例,Ubuntu版本22.04,其他Ubuntu版本或者其他发行版安装时依赖需求可能会有一些差异,建议参考官方文档
https://github.com/iovisor/bcc/blob/master/INSTALL.md

检查内核配置

1
2
magicdian@magicdians-arm-ubuntu-server:~$ uname -a
Linux magicdians-arm-ubuntu-server 5.15.0-76-generic #83~20.04.1-Ubuntu SMP Wed Jun 21 20:24:56 UTC 2023 aarch64 aarch64 aarch64 GNU/Linux

检查Linux内核BPF相关配置, 可以看到基本的BPF内核支持是有的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
magicdian@magicdians-arm-ubuntu-server:~$ cat /boot/config-$(uname -r) | grep "BPF|BTF" -Ei
CONFIG_BPF=y
CONFIG_HAVE_EBPF_JIT=y
CONFIG_ARCH_WANT_DEFAULT_BPF_JIT=y
# BPF subsystem
CONFIG_BPF_SYSCALL=y
CONFIG_BPF_JIT=y
CONFIG_BPF_JIT_ALWAYS_ON=y
CONFIG_BPF_JIT_DEFAULT_ON=y
CONFIG_BPF_UNPRIV_DEFAULT_OFF=y
# CONFIG_BPF_PRELOAD is not set
CONFIG_BPF_LSM=y
# end of BPF subsystem
CONFIG_CGROUP_BPF=y
CONFIG_IPV6_SEG6_BPF=y
CONFIG_NETFILTER_XT_MATCH_BPF=m
CONFIG_BPFILTER=y
CONFIG_BPFILTER_UMH=m
CONFIG_NET_CLS_BPF=m
CONFIG_NET_ACT_BPF=m
CONFIG_BPF_STREAM_PARSER=y
CONFIG_LWTUNNEL_BPF=y
CONFIG_VIDEO_SONY_BTF_MPX=m
CONFIG_DEBUG_INFO_BTF=y
CONFIG_PAHOLE_HAS_SPLIT_BTF=y
CONFIG_DEBUG_INFO_BTF_MODULES=y
CONFIG_BPF_EVENTS=y
CONFIG_BPF_KPROBE_OVERRIDE=y
CONFIG_TEST_BPF=m

安装依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# For Focal (20.04.1 LTS)
sudo apt install -y zip bison build-essential cmake flex git libedit-dev \
libllvm12 llvm-12-dev libclang-12-dev python zlib1g-dev libelf-dev libfl-dev python3-setuptools \
liblzma-dev arping netperf iperf

# For Hirsute (21.04) or Impish (21.10)
sudo apt install -y zip bison build-essential cmake flex git libedit-dev \
libllvm12 llvm-12-dev libclang-12-dev python3 zlib1g-dev libelf-dev libfl-dev python3-setuptools \
liblzma-dev arping netperf iperf

# For Jammy (22.04)
sudo apt install -y zip bison build-essential cmake flex git libedit-dev \
libllvm14 llvm-14-dev libclang-14-dev python3 zlib1g-dev libelf-dev libfl-dev python3-setuptools \
liblzma-dev libdebuginfod-dev arping netperf iperf

# For Lunar Lobster (23.04)
sudo apt install -y zip bison build-essential cmake flex git libedit-dev \
libllvm15 llvm-15-dev libclang-15-dev python3 zlib1g-dev libelf-dev libfl-dev python3-setuptools \
liblzma-dev libdebuginfod-dev arping netperf iperf libpolly-15-dev

# For other versions
sudo apt-get -y install zip bison build-essential cmake flex git libedit-dev \
libllvm3.7 llvm-3.7-dev libclang-3.7-dev python zlib1g-dev libelf-dev python3-setuptools \
liblzma-dev arping netperf iperf

# For Lua support
sudo apt-get -y install luajit luajit-5.1-dev

官方提供了不同版本的依赖需求,根据自己需要进行选择,我使用Ubuntu 22.04,使用这个命令,建议执行之前先用sudo apt-get update命令更新一下

1
2
3
sudo apt install -y zip bison build-essential cmake flex git libedit-dev \
libllvm14 llvm-14-dev libclang-14-dev python3 zlib1g-dev libelf-dev libfl-dev python3-setuptools \
liblzma-dev libdebuginfod-dev arping netperf iperf

我们还需要安装Linux Header

1
2
sudo apt install linux-generic
sudo apt install linux-headers-$(uname -r)

下载源码

1
git clone https://github.com/iovisor/bcc.git

创建build目录并进入

1
mkdir bcc/build; cd bcc/build

执行cmake

1
cmake ..

编译并安装bcc

执行make即可,然后执行sudo make install

配置编译python3绑定

1
2
3
4
5
cmake -DPYTHON_CMD=python3 .. # build python3 binding
pushd src/python/
make
sudo make install
popd

配置成功

以上步骤执行完后,进python3看看是否能够顺利import,如果可以的话环境配置就没问题了

1
2
3
4
5
magicdian@magicdians-arm-ubuntu-server:~$ python3
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from bcc import BPF
>>>