rust ebpf编程


难度 中等

rust中有一个ebpf编程框架aya,其地址为https://github.com/aya-rs/aya。

下载依赖

export RUSTUP_DIST_SERVER=https://rsproxy.cn
export RUSTUP_UPDATE_ROOT=https://rsproxy.cn/rustup

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y

export PATH="/root/.cargo/bin:${PATH}
rustup install stable
rustup toolchain install nightly --component rust-src
cargo install bpf-linker
cargo install cargo-generate
cargo generate --git https://github.com/aya-rs/aya-template
root@linux-kernel-test:~/ebpf_dev# cargo generate --git https://github.com/aya-rs/aya-template
🤷   Project Name: aya_test
🔧   Destination: /root/ebpf_dev/aya_test ...
🔧   project-name: aya_test ...
🔧   Generating template ...
✔ 🤷   Which type of eBPF program? · uprobe
🤷   Target to attach the (u|uret)probe? (e.g libc): libc
🤷   Function name to attach the (u|uret)probe? (e.g getaddrinfo): getaddrinfo
[ 1/25]   Done: .cargo/config.toml                                                                                                                                                              [ 2/25]   Done: .cargo                                                                                                                                                                          [ 3/25]   Done: .gitignore                                                                                                                                                                      [ 4/25]   Done: Cargo.toml                                                                                                                                                                      [ 5/25]   Done: LICENSE-APACHE                                                                                                                                                                  [ 6/25]   Done: LICENSE-GPL2                                                                                                                                                                    [ 7/25]   Done: LICENSE-MIT                                                                                                                                                                     [ 8/25]   Done: README.md                                                                                                                                                                       [ 9/25]   Ignored: pre-script.rhai                                                                                                                                                              [10/25]   Done: rustfmt.toml                                                                                                                                                                    [11/25]   Done: aya_test/Cargo.toml                                                                                                                                                             [12/25]   Done: aya_test/build.rs                                                                                                                                                               [13/25]   Done: aya_test/src/main.rs                                                                                                                                                            [14/25]   Done: aya_test/src                                                                                                                                                                    [15/25]   Done: aya_test                                                                                                                                                                        [16/25]   Done: aya_test-common/Cargo.toml                                                                                                                                                      [17/25]   Done: aya_test-common/src/lib.rs                                                                                                                                                      [18/25]   Done: aya_test-common/src                                                                                                                                                             [19/25]   Done: aya_test-common                                                                                                                                                                 [20/25]   Done: aya_test-ebpf/Cargo.toml                                                                                                                                                        [21/25]   Done: aya_test-ebpf/build.rs                                                                                                                                                          [22/25]   Done: aya_test-ebpf/src/lib.rs                                                                                                                                                        [23/25]   Done: aya_test-ebpf/src/main.rs                                                                                                                                                       [24/25]   Done: aya_test-ebpf/src                                                                                                                                                               [25/25]   Done: aya_test-ebpf                                                                                                                                                                   🔧   Moving generated files into: `/root/ebpf_dev/aya_test`...
🔧   Initializing a fresh Git repository
✨   Done! New project created /root/ebpf_dev/aya_test

ebpf程序分类

ebpf按照attach point分为多种类型:

  • kprobe/kretprobe
  • uprobe/uretprobe:用户态进程的函数入口/返回
  • tracepoint
  • socket filter
  • tc
  • xdp

uprobe (function entry)

当用户态程序执行到目标函数的入口时,内核会出发uprobe,并执行对应的ebpf程序。

uprobe可以获取:

  • 函数入口参数(寄存器或者栈上)
  • 当前进程PID、TGID(线程组)
  • 寄存器上下文
  • 用户空间内存
  • 时间戳

uprobe典型使用场景:

  • 记录函数输入参数
  • 保存入口时间戳,用于计算耗时
  • 当前进程/线程id
  • 读取用户态memory

uretprobe(function probe)

当目标函数执行到return指令时,内核将会触发uretprobe。

uretprobe可以获取如下信息:

  • 函数返回值
  • 函数耗时
  • 当前进程/线程id
  • 读取用户态memory

uretprobe用途如下:

  • 获取返回值
  • 计算函数耗时
  • 分析错误代码
  • 分析一次调用的完整生命周期
#[uprobe]
pub fn exec_simple_query_entry(ctx: ProbeContext) -> u32 {
    let query = query_text(&ctx); // 读取用户态 SQL
    let thread_id = ctx.tgid();   // 进程ID
    let pid = ctx.pid();          // 线程ID
    let event = PostgresEntry::ExecSimpleQuery(query, thread_id, pid);
    submit_entry(ctx, event)
}

文章作者: growdu
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 growdu !
  目录
分类导航
随笔3 AI29 计算机基础13 算法1 博客搭建7 ChatGPT2 集群63 计算机通信1 数据库59 数据库深入93 数据库93 Docker11 DPDK26 编辑工具4 Elasticsearch4 FAQ1 Go Web1 hometown2 编程语言16 人生11 网络9 Linux38 OPC1 openGauss4 页面12 PostgreSQL54 程序员自我修养1 协议11 stock1 存储5 成长之路1 工具20 视频作品1 VPP18 Vue13 Web1 代码示例11 BenchmarkSQL1 PostgreSQL 源码修炼之路14
最热文章
1
PostgreSQL 逻辑复制全链路:以一个真实事务为例,从 `INSERT` 到 `apply` 的源码级旅程
数据库🔥 2162
2
PostgreSQL 逻辑复制 Stream 执行逻辑源码级拆解:从 reorderbuffer 触发到订阅端落盘回放
数据库🔥 1765
3
PostgreSQL 行锁机制源码级深度拆解:4 种锁模式 + xmax + MultiXact 的完整链路
数据库🔥 1656
4
PostgreSQL reorderbuffer.c 函数级源码全集:5615 行 / 95 个函数的逐函数解析
数据库深入🔥 1650
5
PostgreSQL 目录快照源码级深度拆解:系统表读取的 MVCC 真相与失效机制
数据库🔥 1604
6
13 逻辑复制深入
数据库深入🔥 1570
7
0 Postgresql存储、索引及系统优化、主备切换
PostgreSQL🔥 1495
8
PostgreSQL 读一行数据源码级深度拆解:MVCC 在每个环节的落点
数据库🔥 1464
9
PostgreSQL WAL 记录类型完全指南:21 个 RMGR × 80+ 种记录的源码级拆解
数据库🔥 1431
10
一文读懂openguass dcf网络模块
集群🔥 1420
11
PostgreSQL 元数据存储机制:从磁盘文件到内存缓存,`pg_class` 撑起的整个系统表体系
数据库🔥 1411
12
openGauss 并行解码设计原理深度拆解:reader + 多 worker + CSN 三层架构的源码级剖析
数据库深入🔥 1344
13
逻辑复制源码分析
数据库深入🔥 1327
14
PostgreSQL 逻辑复制:catalog 改动、可见性判定与 xmin/xmax 推进全景
数据库深入🔥 1317
15
自托管服务全家桶:从原文方案到可复制实操手册
人生🔥 1278
16
PostgreSQL CREATE_REPLICATION_SLOT 端到端源码分析:从 SQL 到 snapbuild 状态机的完整链路(每个函数每个参数)
数据库深入🔥 1277
17
PostgreSQL syscache 模块源码深度拆解:CatCache 数据结构、LRU 哈希表、cache invalidation 全链路
数据库深入🔥 1216
18
PostgreSQL 18 内核数据结构全景:RangeVar 与 20+ 个核心数据结构源码级拆解
数据库🔥 1158
19
PostgreSQL 逻辑复制里 `RUNNING_XACTS` 写入机制与快照 `xmin / xmax / xip` 的演化:源码级深度拆解
数据库🔥 1119
20
PostgreSQL 分区表:从一行 `PARTITION BY` 到路由热路径的全链路拆解
数据库🔥 1094