1. history 信息
一次成功编译 bpf 后执行 history 得到的信息:
yingzhi@yingzhi-Host:~/ex/ex_kernel/linux-5.4$ history1 ls2 mkdir ex3 cd ex4 mkdir ex_kernel5 ls /boot/6 sudo apt install linux-source7 ls /usr/src/8 uname -r9 cd ex_kernel/10 wget http://ftp.sjtu.edu.cn/sites/ftp.kernel.org/pub/linux/kernel/v5.x/linux-5.4.tar.gz11 ls12 tar zxf linux-5.4.tar.gz 13 ls14 cd linux-5.4/15 cp /boot/config-5.4.0-152-generic .config16 make oldconfig17 python --version18 sudo apt install python319 python3 --version20 python21 sudo apt install -y bison build-essential cmake flex git libedit-dev pkg-config libmnl-dev python zlib1g-dev libssl-dev libelf-dev libcap-dev libfl-dev llvm clang pkg-config gcc-multilib luajit libluajit-5.1-dev libncurses5-dev libclang-dev clang-tools22 make oldconfig23 make prepare24 make modules_prepare25 make headers_install26 make M=samples/bpf27 cd samples/bpf28 ls29 ./sockex130 ./trace531 ./tracex532 ./sampleip 33 cd ..34 l35 wget36 wget http://ftp.sjtu.edu.cn/sites/ftp.kernel.org/pub/linux/kernel/v5.x/linux-5.4.1.tar.gz37 ls38 tar zxf linux-5.4.1.tar.gz 39 uname -r40 cd linux-5.4.1/41 cp /boot/config-5.4.0-152-generic .config42 cd ..43 cd linux-5.444 ls45 cd samples/bpf/46 ls47 sudo ./tracex648 ls49 sudo ./sockex150 cd ../../../linux-5.4.1/51 ls52 make oldconfig53 make prepare54 make modules_prepare55 make headers_install56 make M=samples/bpf57 ls58 cd samples/bpf/59 ls60 sudo ./sockex161 cd ../..62 ls63 cd ..64 ls65 git --version66 sudo apt install nasm67 ls68 cd ..69 mkdir asm_ex70 cd ex71 cd ex_kernel/72 ls73 cd linux-5.474 ls75 make -j476 llc -version77 ls78 history
yingzhi@yingzhi-Host:~/ex/ex_kernel/linux-5.4$
2. 使用 ubuntu 18.04
ubuntu 18.04 编译 BPF比较简单, ubuntu 22.04 适合系统熟手;
找一台 旧机器,安装一个 ubuntu 18.0;
使用国内源:
yingzhi@yingzhi-Host:~/ex/ex_kernel/linux-5.4$ cat /etc/apt/sources.list
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
yingzhi@yingzhi-Host:~/ex/ex_kernel/linux-5.4$
sudo apt update
sudo apt upgrade
3. 选择linux kernel 源代码版本
$ name -ryingzhi@yingzhi-Host:~/ex/ex_kernel/linux-5.4$ uname -r
5.4.0-84-generic
yingzhi@yingzhi-Host:~/ex/ex_kernel/linux-5.4$
http://ftp.sjtu.edu.cn/sites/ftp.kernel.org/pub/linux/kernel/v5.x/linux-5.4.1.tar.gz
$ wget http://ftp.sjtu.edu.cn/sites/ftp.kernel.org/pub/linux/kernel/v5.x/linux-5.4.1.tar.gz$ tar dxf linux-5.4.1.tar.gz
$ cd linux-5.4.1/# or 版本不一定合适
$ sudo apt install linux-source
$ ls /usr/src/
4. 编译 bpf
4.1 准备环境
sudo apt install -y bison build-essential cmake flex git libedit-dev pkg-config libmnl-dev python zlib1g-dev libssl-dev libelf-dev libcap-dev libfl-dev llvm clang pkg-config gcc-multilib luajit libluajit-5.1-dev libncurses5-dev libclang-dev clang-tools
4.2 拷贝当前系统的kernel 配置文件 .config
$ cp /boot/config-5.4.0-152-generic /home/yingzhi/ex/ex_kernel/linux-5.4.1/.config
4.3 编译ebpf samples
$ cd /home/yingzhi/ex/ex_kernel/linux-5.4.1/
$ make oldconfig
$ make prepare
$ make modules_prepare
$ make headers_install
$ make M=samples/bpf
4.4 运行 示例
ls ./samples/bpf/
$ sudo ./sockex1
5. bpf 原理
5.1 架构
5.2 编写并分析一个示例
5.3 bpf map
5.4 bpf trace
5.5 bpf tool
5.6 bpf && network
5.7 xdp
6. eBPF JIT
7. 参考:
1.
eBPF - Introduction, Tutorials & Community Resources