BPF编译运行实例

        网上一大堆eBPF的入门概念,但是自己编译运行一遍才感到心理踏实。

目标:

编译运行 《Linux内核观测技术 BPF》里面的配套代码 hello_world (chapter2) 示范例,如下效果

原环境:

 操作步骤: 

1,获取bpf示例源码:

git clone https://github.com/bpftools/linux-observability-with-bpf.git

获取好源码后,需要仔细看里面的 README.md 说明,后续的操作基本围绕着它进行。为显示其重要性,这里贴出原始说明

<summary>Ubuntu 18.04</summary>

First, we need to install some build dependencies and all the tools needed for the examples:

```bash
sudo apt update
sudo apt install build-essential git make libelf-dev clang strace tar bpfcc-tools linux-headers-$(uname -r) gcc-multilib
```

**Note on Kernel version**: make sure to have a recent kernel to run the examples, a version `>=5.0.0` will do the job. Most Ubuntu `18.04` providers are shipping with the kernel `4.15` that doesn't work for most of the examples. Upgrading options are left to the reader, we've been successful on aws by installing the `linux-image-5.0.0-1019-aws` package.

After dependencies, we need grab a copy of the kernel source code for the current release.
Since this assumes that you are running an updated Ubuntu 18.04 we can get it directly from the repo they provide.

```
cd /tmp
git clone --depth 1 git://kernel.ubuntu.com/ubuntu/ubuntu-bionic.git
```

Now that we have the kernel source, we can move it to the `/kernel-src` folder.

**NOTE THAT: All the examples** using kernel sources in this repo assume that the kernel sources are available at `/kernel-src`. In case you don't like it,
make sure you do a search and replace!


At this point we move the kernel sources and compile `libbpf`.

```
sudo mv ubuntu-bionic /kernel-src
cd /kernel-src/tools/lib/bpf
sudo make && sudo make install prefix=/usr/local
```

Ubuntu doesn't have the library path that the makefile expects so we need to move our libraries
to its library path now.

```
sudo mv /usr/local/lib64/libbpf.* /lib/x86_64-linux-gnu/
```

从说明中,我们知道ubuntu系统内核版本至少是 5.0.0,如果内核版本不够需要升级内核。另外需要安装各种依赖,安装依赖会出现各种问题,是最花费时间的。

阅读完说明后,大的思路就有了:先升级内核,apt install README.md 里面的依赖(可以需要编译源码),编译bpf示例,运行验证。 

2,升级内核

从官网获取内核源码包The Linux Kernel Archiveshttps://www.kernel.org/内核编译一般会比较顺利,按下面的操作执行

(假设内核源码压缩包放在新建的 /data/jsf/ebpf/kernel 目录下)

tar -xvf linux-5.18.11.tar.xz

cd linux-5.18.11/

cp /boot/config-4.9.0-141-custom .config    #使用编译本系统的config文件编译新内核

make oldconfig   #如果新内核有新编译选项,一路Enter默认即可

make -j 48   #后面的48是服务器的cpu核心数,表示并发编译数

make modules_install  #安装内核模块

make install   #包含更新系统grub引导,可以发现 /boot/grub/grub.cfg 新增的新内核镜像的引导

reboot   #以新内核镜像启动

验证

关于ubuntu grub 引导可以参考 ubuntu修改默认启动内核 - 走看看

关于更多linux kernel make 选项,参考 make help

关于生成内核头文件 headers_install 在下一小节说明

3,安装依赖

编译 libbpf.so 

libbpf.so 源码位于内核源码树的 tools/lib/bpf 目录中,直接进入到这个目录中编译即可,需要注意编译完后,使用 ldconfig -v 2>/dev/null | grep libbpf 命令测试一遍, 如果没有看到 libbpf.so ,需要手动添加(假设 libbpf.so注册在 /usr/local/lib64 目录): 

首先按照 README.md 的说明安装,对于不能安装成功的则手动编译源码安装

安装 clang llvm 

直接从官网下载编译好的二进制配置下环境变量即可

下载地址 Release LLVM 12.0.0 · llvm/llvm-project · GitHub

参考:LLVM_Ubuntu安装_落子无悔!的博客-CSDN博客_ubuntu安装llvm

最后验证

安装 BCC 

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

还是一样的,仔细里面的阅读  README.md 以及 INSTALL.md 文件

这里附带说明下 BCC 的官方说明:

BCC is a toolkit for creating efficient kernel tracing and manipulation
programs, and includes several useful tools and examples. It makes use of
extended BPF (Berkeley Packet Filters), formally known as eBPF, a new feature
that was first added to Linux 3.15. Much of what BCC uses requires Linux 4.1
and above.
找到对应的系统,按说明执行:

其中这里遇到一个cmake编译问题,不管是升级g++还是cmake指定std c++11都不行,最后直接改了 bcc/src/cc/common.cc:178 去掉后面的一个参数0才顺利编译通过。

4,编译运行

这里需要注意内核头文件的引用。因为默认使用的是ubuntu16系统的原生内核头文件,直接编译会报头文件宏定义错误

需要在内核源码目录下生成内核头文件 

make ARCH=x86_64 INSTALL_HDR_PATH=/usr headers_install

运行测试,写一个fork.c 程序,每执行一下,就会出一次打印:

​​​​​​​

 

入门操作是完成了,后续主要的还是要多改代码,多看书,多理解概念,最后多运用。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值