BPF允许任何人在Linux内核之中执行任意的代码,这听起来的十分危险,但是由于有着BPF验证器使得这一过程变的相当的安全。BPF时内核的一个模块,所有的BPF程序都必须经过它的审查才能够被加载到内核之中去运行。
验证器执行的第一项检查就是对BPF虚拟机加载的代码进行静态分析。这一步的目的是保证程序可以按照预期去结束,而不会产生死循环拜拜浪费系统资源。验证器会创建一个DAG(有向无环图),将BPF程序的每个执行首位相连之后去执行DFS(深度优先遍历),当且仅当每个路径都能达到DAG的底部才会通过验证。
之后其会执行第二项检查,也就是对BPF程序执行预执行处理。这个时候验证器会去分析程序执行的每条指令,确保不会执行无效的指令。同时也会检查所有内存指针是否可以正确访问和解引用。
BPF程序可以使用尾部调用来调用其他BPF程序,这是个强大的功能。其允许通过组合比较小的BPF功能来实现更为复杂的程序。当从一个BPF程序调用另外一个BPF程序的时候,内核会完全重置程序上下文。这意味着如果想要在多个BPF程序之中共享信息这是做不到的。为了解决程序间共享信息的问题,BPF引入了BPF映射的机制来解决这个问题,我们会在后面详细的介绍BPF映射机制。
注:内核5.2 版本之前BPF只允许执行4096条指令,所以才有了尾部调用这个特性。从5.2开始,指令限制扩展到了100w条,尾部调用的递归层次也有了32次的限制。
BPF程序在4系内核之后就已经成为了内核的顶级子系统,但是为了让我们的系统能够稳定运行BPF程序,还是推荐安装5系内核。首先,我们可以使用如下的命令获取当前系统的版本:
uname -aLinux localhost 5.0.9 #2 SMP PREEMPT Mon Feb 27 00:00:23 CST 2023 x86_64 x86_64 x86_64 GNU/Linux
笔者这里的系统已经经过升级了,如果没有经历过升级,可以按照如下的命令获取系统的源码:
# 获取相应版本的内核源码
cd /tmp
wget -c https://mirrors.aliyun.com/linux-kernel//v5.x/linux-5.0.9.tar.gz -O - | tar -xz
之后的过程,同学们可以百度相应的教程获取安装,本文章将专注于BPF技术的使用。
安装好相应内核之后,为了让我们在开发的时候更为容易,推荐这里将内核源码单独编译一下,方便我们链接:
tar -xvf linux-5.0.9.tar.gz
sudo mv linux-5.0.9 /kernel-src
cd /kernel-src/tools/lib/bpf
sudo make && sudo make install prefix=/
升级好内核环境之后,我们还需要安装BPF程序的依赖环境,主要可以分为三个部分:
sudo dnf install make glibc-devel.i686 elfutils-libelf-devel wget tar clang bcc strace kernel-devel -y
在安装好上述程序之后,我们使用如下的代码可以来测试我们的环境是否配置完成。BPF程序可以由C语言来编写,之后由LLVM编译,其可以将C语言写的程序编译成能够加载到内核执行的汇编代码。
# 指定编译器为clang
CLANG = clang
# 编译完后的程序名称
EXECABLE = monitor-exec
# 源码名称
BPFCODE = bpf_program
# BPF依赖地址
BPFTOOLS = /kernel-src/samples/bpf
BPFLOADER = $(BPFTOOLS)/bpf_load.c
# 指定头文件
CCINCLUDE += -I/kernel-src/tools/testing/selftests/bpf
LOADINCLUDE += -I/kernel-src/samples/bpf
LOADINCLUDE += -I/kernel-src/tools/lib
LOADINCLUDE += -I/kernel-src/tools/perf
LOADINCLUDE += -I/kernel-src/tools/include
LIBRARY_PATH = -L/usr/local/lib64
BPFSO = -lbpfCFLAGS += $(shell grep -q "define HAVE_ATTR_TEST 1" /kernel-src/tools/perf/perf-sys.h \&& echo "-DHAVE_ATTR_TEST=0").PHONY: clean $(CLANG) bpfload buildclean:rm -f *.o *.so $(EXECABLE)build: ${BPFCODE.c} ${BPFLOADER}$(CLANG) -O2 -target bpf -c $(BPFCODE:=.c) $(CCINCLUDE) -o ${BPFCODE:=.o}bpfload: build# 编译程序clang $(CFLAGS) -o $(EXECABLE) -lelf $(LOADINCLUDE) $(LIBRARY_PATH) $(BPFSO) \$(BPFLOADER) loader.c$(EXECABLE): bpfload.DEFAULT_GOAL := $(EXECABLE)
程序源码有两个,一个是bpf_program.c
这里面存放的是要执行的BPF源码,其会被编译成为一个.o
文件。
在这里我们使用BPF提供的SEC
属性告知BPF虚拟机在何时运行此程序。下面的代码会在execve
系统调用跟踪点被执行的时候运行BPF程序。当内核检测到execve
的时候,BPF程序被执行时,我们会看到输出消息"Hello, World, BPF!"
#include
#define SEC(NAME) __attribute__((section(NAME), used))static int (*bpf_trace_printk)(const char *fmt, int fmt_size,...) = (void *)BPF_FUNC_trace_printk;SEC("tracepoint/syscalls/sys_enter_execve")
int bpf_prog(void *ctx) {char msg[] = "Hello, World, BPF!";bpf_trace_printk(msg, sizeof(msg));return 0;
}// 程序许可证,linux内核只允许加载GPL许可的程序
char _license[] SEC("license") = "GPL";
上面的.o
文件会被下面的这个由loader.c
编译成为的moniter-exec
程序去执行。其会把BPF程序加载到内核之中去运行,这里依赖的就是我们使用的load_bpf_file
,其将会获取一个二进制文件并把它加载到内核之中。
#include "bpf_load.h"
#include int main(int argc, char **argv) {if (load_bpf_file("bpf_program.o") != 0) {printf("The kernel didn't load the BPF program\n");return -1;}read_trace_pipe();return 0;
}
之后我们执行如下的命令去编译上述的代码:
make# 运行以下程序
sudo ./loader
BPF映射以
此内容会存在较多的代码,这里会将相关所需要的MakeFile文件内容展示出来:
CLANG = clangINCLUDE_PATH += -I/kernel-src/tools/lib/bpf
INCLUDE_PATH += -I/kernel-src/tools/**
LIBRARY_PATH = -L/usr/local/lib64
BPFSO = -lbpf
.PHONY: clean clean:rm -f # 要删除的BPF模块build: # 填写要编译的 BPF程序模块.DEFAULT_GOAL := build
创建BPF映射的最值方式就是使用bpf_create_map
系统调用。这个函数需要传入五个参数:
BPF_MAP_CREATE
,则表示创建一个新的映射。int bpf_create_map(bpf_map_type map_type, int key_size, int value_size, int max_entries, int map_flags);
如果创建成功,这个接口会返回一个指向这个map的文件描述符。如果创建失败,将返回-1。失败会有三种原因,我们可以通过errno
来进行区分。
EINVAL
;EPERM
;ENOMEM
;#include
#include
#include
#include
#include int main(int argc, char **argv) {//# createint fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(int), sizeof(int), 100, 0);if (fd < 0) {printf("Failed to create map: %d (%s)\n", fd, strerror(errno));return -1;}printf("Create BPF map success!\n");
}
我们在一开始提到的MakeFile文件之中添加如下信息即可编译上述代码:
create: map_create.c clang -o create -lelf $(INCLUDE_PATH) $(LIBRARY_PATH) $(BPFSO) $?
...
build: create
最后运行编译后的程序:
sudo ./create
Create BPF map success!
在Demo之中我们使用到了BPF_MAP_TYPE_HASH
这个map类型,其表示在内核空间之中创建一个哈希表映射。除此之外,BPF还支持如下的Map类型:
EINVAL
错误。bpf_tail_call
来执行刚刚提到的尾部调用。bpf_get_stackid
将栈跟踪信息写入到该映射。BPF映射的基本特征使基于文件描述符的,这意味着关闭文件描述符后,映射及其所保存的所有信息都会消失。这意味着我们无法获取已经结束的BPF程序保存在映射之中的信息,在Linux 内核4.4 版本之后,引入了两个新的系统调用,bpf_obj_pin
用来固定(固定后不可更改)和bpf_obj_get
获取来自BPF虚拟文件系统的映射和BPF程序。
BPF虚拟文件系统的默认目录使/sys/fs/bpf
,如果Linux系统内核不支持BPF,可以使用mount命令挂载此文件系统:
mount -t bpf /sys/fs/bpf /sys/fs/bpf
BPF固定的系统调用为bpf_obj_pin
,其函数原型如下:
int bpf_obj_pin(int file_fd, const char* file_path)
#include
#include
#include
#include
#include
#include
#include static const char *file_path = "/sys/fs/bpf/my_hash";int main(int argc, char **argv) {//# createint fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(int), sizeof(int), 100, 0);if (fd < 0) {printf("Failed to create map: %d (%s)\n", fd, strerror(errno));return -1;}int pinned = bpf_obj_pin(fd, file_path);if (pinned < 0) {printf("Failed to pin map to the file system: %d (%s)\n", pinned,strerror(errno));return -1;}return 0;
}
我们在一开始提到的MakeFile文件之中添加如下信息即可编译上述代码:
save: map_save.c clang -o save -lelf $(INCLUDE_PATH) $(LIBRARY_PATH) $(BPFSO) $?
...
build: save
之后,我们可以查看这个目录查看是否固定成功了:
sudo ls /sys/fs/bpf/
my_hash
我们可以使用bpf_map_update_elem
系统调用去插入元素到刚创建的map之中。内核程序需要从bpf/bpf_helpers.h
文件加载此函数,而用户空间程序则需要从tools/lib/bpf/bpf.h
文件加载,所以内核程序访问的函数签名和用户空间之不同的。当然,访问的行为也是不同的:内核程序可以原子的执行更新操作,用户空间则需要发送消息到内核,之后先复制值,然后再进行更新映射。这意味着更新操作不是原子性的。
下面使这个函数的函数原型,如果执行成功,该函数返回0;如果失败,则将返回复数并且把失败的原因写入全局变量errno之中。
int bpf_map_update_elem(int file_fd, void* key, void* value, int type);
#include
#include
#include
#include
#include
#include #include "bpf.h"extern char *optarg;extern int optind;extern int opterr;extern int optopt;static const char *file_path = "/sys/fs/bpf/my_hash";int main(int argc, char **argv) {char ch;int key;int value;while ((ch = getopt(argc, argv, "k:v:")) != -1) {switch (ch) {case 'k':printf("set key: %s\n", optarg);key = atoi(optarg);break;case 'v':printf("set value: %s\n", optarg);value = atoi(optarg);break;}}int fd, added, pinned;//# openfd = bpf_obj_get(file_path);if (fd < 0) {printf("Failed to fetch the map: %d (%s)\n", fd, strerror(errno));return -1;}added = bpf_map_update_elem(fd, &key, &value, BPF_ANY);if (added < 0) {printf("Failed to update map: %d (%s)\n", added, strerror(errno));return -1;}return 0;
}
我们在一开始提到的MakeFile文件之中添加如下信息即可编译上述代码:
update: map_update.c clang -o update -lelf $(INCLUDE_PATH) $(LIBRARY_PATH) $(BPFSO) $?
...
build: update
最后运行编译后的程序:
sudo ./update -k 1 -v 9
set key: 1
set value: 9
当新元素写入到map之后,我们可以使用bpf_map_lookup_elem
系统调用来读取map之中的元素,其函数原型如下:
下面使这个函数的函数原型,如果执行成功,该函数返回0;如果失败,则将返回复数并且把失败的原因写入全局变量errno之中。
int bpf_map_lookp_elem(int file_fd, void* key, void* value);
#include
#include
#include
#include
#include "bpf.h"
#include
#include
#include "bpf.h"extern char* optarg;extern int optind;extern int opterr;extern int optopt;static const char *file_path = "/sys/fs/bpf/my_hash";int main(int argc, char **argv) {char ch;int key;int value;while ((ch = getopt(argc, argv, "k:v:")) != -1){switch (ch){case 'k':key = atoi(optarg);break;}}int fd, result;fd = bpf_obj_get(file_path);if (fd < 0) {printf("Failed to fetch the map: %d (%s)\n", fd, strerror(errno));return -1;}result = bpf_map_lookup_elem(fd, &key, &value);if (result < 0) {printf("Failed to read value from the map: %d (%s)\n", result,strerror(errno));return -1;}printf("Value read from the key %d: '%d'\n", key,value);return 0;
}
我们在一开始提到的MakeFile文件之中添加如下信息即可编译上述代码:
fetch: map_fetch.c clang -o fetch -lelf $(INCLUDE_PATH) $(LIBRARY_PATH) $(BPFSO) $?
...
build: fetch
最后运行编译后的程序:
sudo ./update -k 1 -v 9
set key: 1
set value: 9
当新元素写入到map之后,我们可以使用bpf_map_delete_elem
系统调用来删除map之中的元素,其函数原型如下:
下面使这个函数的函数原型,如果执行成功,该函数返回0;如果失败,则将返回复数并且把失败的原因写入全局变量errno之中。
int bpf_map_delete_elem(int file_fd, void* key);
#include
#include
#include
#include
#include "bpf.h"
#include
#include
#include "bpf.h"extern char* optarg;extern int optind;extern int opterr;extern int optopt;static const char *file_path = "/sys/fs/bpf/my_hash";int main(int argc, char **argv) {char ch;int key;int value;while ((ch = getopt(argc, argv, "k:v:")) != -1){switch (ch){case 'k':key = atoi(optarg);break;}}int fd,result;fd = bpf_obj_get(file_path);if (fd < 0) {printf("Failed to fetch the map: %d (%s)\n", fd, strerror(errno));return -1;}key = 1;result = bpf_map_delete_elem(fd, &key);if (result < 0) {printf("Failed to delete value from the map: %d (%s)\n", fd,strerror(errno));return -1;}printf("delte key:%d success!\n", key);return 0;
}
我们在一开始提到的MakeFile文件之中添加如下信息即可编译上述代码:
delete: map_delete.c clang -o delete -lelf $(INCLUDE_PATH) $(LIBRARY_PATH) $(BPFSO) $?
...
build: delete
最后运行编译后的程序:
sudo ./delete -k 1
delte key:1 success!
假设我们写入了很多元素到map之后,我们可以使用bpf_map_get_next_key
系统调用来遍历map之中的元素,其函数原型如下:
下面使这个函数的函数原型,如果执行成功,该函数返回0;如果失败,则将返回复数并且把失败的原因写入全局变量errno之中。
int bpf_map_get_next_key(int file_fd, void* key, void* next_key);
#include
#include
#include
#include
#include
#include #include "bpf.h"extern char *optarg;extern int optind;extern int opterr;extern int optopt;static const char *file_path = "/sys/fs/bpf/my_hash";int main(int argc, char **argv) {int fd, value, result;fd = bpf_obj_get(file_path);if (fd < 0) {printf("Failed to fetch the map: %d (%s)\n", fd, strerror(errno));return -1;}int start_key = -1;int next_key;while (bpf_map_get_next_key(fd, &start_key, &next_key) == 0) {start_key = next_key;printf("Key read from the map: '%d'\n", next_key);}return 0;
}
iter: map_iter.c clang -o iter -lelf $(INCLUDE_PATH) $(LIBRARY_PATH) $(BPFSO) $?
...
build: iter
最后运行编译后的程序:
[ik@localhost chapter-3]$ sudo ./iter
Key read from the map: '2'
Key read from the map: '8'
Key read from the map: '10'
Key read from the map: '5'
Key read from the map: '6'
Key read from the map: '3'
Key read from the map: '4'
Key read from the map: '9'
Key read from the map: '7'
Key read from the map: '11'
上一篇:深入Arrays.sort源码