ubuntu 18.04 中 eBPF samples/bpf 编译

1. history 信息

一次成功编译 bpf 后执行 history 得到的信息:

yingzhi@yingzhi-Host:~/ex/ex_kernel/linux-5.4$ history
    1  ls
    2  mkdir ex
    3  cd ex
    4  mkdir ex_kernel
    5  ls /boot/
    6  sudo apt install linux-source
    7  ls /usr/src/
    8  uname -r
    9  cd ex_kernel/
   10  wget http://ftp.sjtu.edu.cn/sites/ftp.kernel.org/pub/linux/kernel/v5.x/linux-5.4.tar.gz
   11  ls
   12  tar zxf linux-5.4.tar.gz 
   13  ls
   14  cd linux-5.4/
   15  cp /boot/config-5.4.0-152-generic .config
   16  make oldconfig
   17  python --version
   18  sudo apt install python3
   19  python3 --version
   20  python
   21  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
   22  make oldconfig
   23  make prepare
   24  make modules_prepare
   25  make headers_install
   26  make M=samples/bpf
   27  cd samples/bpf
   28  ls
   29  ./sockex1
   30  ./trace5
   31  ./tracex5
   32  ./sampleip 
   33  cd ..
   34  l
   35  wget
   36  wget http://ftp.sjtu.edu.cn/sites/ftp.kernel.org/pub/linux/kernel/v5.x/linux-5.4.1.tar.gz
   37  ls
   38  tar zxf linux-5.4.1.tar.gz 
   39  uname -r
   40  cd linux-5.4.1/
   41  cp /boot/config-5.4.0-152-generic .config
   42  cd ..
   43  cd linux-5.4
   44  ls
   45  cd samples/bpf/
   46  ls
   47  sudo ./tracex6
   48  ls
   49  sudo ./sockex1
   50  cd ../../../linux-5.4.1/
   51  ls
   52  make oldconfig
   53  make prepare
   54  make modules_prepare
   55  make headers_install
   56  make M=samples/bpf
   57  ls
   58  cd samples/bpf/
   59  ls
   60  sudo ./sockex1
   61  cd ../..
   62  ls
   63  cd ..
   64  ls
   65  git --version
   66  sudo apt install nasm
   67  ls
   68  cd ..
   69  mkdir asm_ex
   70  cd ex
   71  cd ex_kernel/
   72  ls
   73  cd linux-5.4
   74  ls
   75  make -j4
   76  llc -version
   77  ls
   78  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 -r




yingzhi@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

ubuntu 18.04 中 eBPF samples/bpf 编译_第1张图片

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

你可能感兴趣的:(eBPF)