【未完】eBPF开发学习

开发环境搭建

系统:Ubuntu 20.10
内核:5.8.0-25

由于20.10不是LTS版本,需要先修改/etc/apt/source.list文件,把“http://archive.ubuntu.com/ubuntu”修改为“https://mirrors.ustc.edu.cn/ubuntu-old-releases/ubuntu”,然后update。

sudo sed -i 's/http:\/\/archive.ubuntu.com\/ubuntu/https:\/\/mirrors.ustc.edu.cn\/ubuntu-old-releases\/ubuntu/g' /etc/apt/sources.list
sudo apt-get update

安装相关依赖,下载libbpf的源码并编译:

sudo apt-get install git clang make libelf1 libelf-dev zlib1g-dev
git clone https://github.com/libbpf/libbpf.git
cd libbpf/src/
make
sudo make install

从GitHub下载bpftool的二进制文件并放到/usr/local/sbin/中,然后在eBPF项目代码的目录下生成vmlinux.h。

bpftool btf dump file /sys/kernel/btf/vmlinux format c > vmlinux.h
clang -g -O2 -target bpf -D__TARGET_ARCH_x86_64 -I/usr/src/linux-headers-5.8.0-25/include -I/usr/include -c ./splice.bpf.c -o splice.bpf.o
bpftool gen skeleton ./splice.bpf.o > splice.skel.h
clang -I/usr/include -c splice.c -o splice_epbf.o
clang splice_epbf.o -lbpf -lelf -lz -o splice_epbf

进入root用户,运行程序。

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib64 ./splice_epbf

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注