mirror of
https://github.com/eunomia-bpf/bpf-developer-tutorial.git
synced 2026-02-03 10:14:44 +08:00
Add some desc for userspace eBPF
This commit is contained in:
@@ -74,6 +74,7 @@ Android:
|
||||
- [使用 eBPF 替换任意程序读取或写入的文本](src/27-replace/README.md)
|
||||
- [BPF的生命周期:使用 Detached 模式在用户态应用退出后持续运行 eBPF 程序](src/28-detach/README.md)
|
||||
- [eBPF 运行时的安全性与面临的挑战](src/18-further-reading/ebpf-security.zh.md)
|
||||
- [用户空间 eBPF 运行时:深度解析与应用实践](src\36-userspace-ebpf\README.md)
|
||||
|
||||
持续更新中...
|
||||
|
||||
@@ -171,7 +172,7 @@ eunomia-bpf 由一个编译工具链和一个运行时库组成, 对比传统的
|
||||
|
||||
## 让 ChatGPT 来帮助我们
|
||||
|
||||
本教程借助 ChatGPT 来学习编写 eBPF 程序,同时我们尝试教会 ChatGPT 编写 eBPF 程序,大概步骤如下:
|
||||
本教程也尝试借助 ChatGPT 来学习编写 eBPF 程序,同时我们尝试教会 ChatGPT 编写 eBPF 程序,大概步骤如下:
|
||||
|
||||
1. 告诉它基本的 eBPF 编程相关的常识
|
||||
2. 告诉它一些案例:hello world,eBPF 程序的基本结构,如何使用 eBPF 程序进行追踪,并且让它开始编写教程
|
||||
@@ -181,7 +182,7 @@ eunomia-bpf 由一个编译工具链和一个运行时库组成, 对比传统的
|
||||
|
||||

|
||||
|
||||
完整的对话记录可以在这里找到: [ChatGPT.md](ChatGPT.md)
|
||||
完整的对话记录可以在这里找到: [ChatGPT.md](src/ChatGPT.md)
|
||||
|
||||
我们也构建了一个命令行工具的 demo ,通过本教程的训练, 让它通过自然语言描述即可自动编写 eBPF 程序,追踪 Linux 系统:<https://github.com/eunomia-bpf/GPTtrace>
|
||||
|
||||
|
||||
@@ -62,6 +62,7 @@ Security:
|
||||
- [Adding sudo users using eBPF](src/26-sudo/README_en.md)
|
||||
- [Replacing text read or written by any program using eBPF](src/27-replace/README_en.md)
|
||||
- [BPF lifecycle: Running eBPF programs continuously in Detached mode after user-mode applications exit](src/28-detach/README_en.md)
|
||||
- [Userspace eBPF Runtimes: Overview and Applications](src\36-userspace-ebpf\README_en.md)
|
||||
|
||||
Continuously updated...
|
||||
|
||||
|
||||
@@ -32,6 +32,8 @@ Linux 内核的主要目的是抽象出硬件或虚拟硬件,并提供一个
|
||||
|
||||
不仅如此,eBPF 还有能力往用户空间任意进程的任意地址写入数据(bpf_probe_write_user[7]),有限度地修改内核函数的返回值(bpf_override_return[8]),甚至在内核态直接执行某些系统调用[9];所幸的是,eBPF 在加载进内核之前对字节码会进行严格的安全检查,确保没有内存越界等操作,同时,许多可能会扩大攻击面、带来安全风险的功能都是需要在编译内核时明确选择启用才能使用的;在 Wasm 虚拟机将字节码加载进内核之前,也可以明确选择启用或者禁用某些 eBPF 功能,以确保沙箱的安全性。
|
||||
|
||||
除了内核态的 eBPF 运行时,eBPF 也可以拓展到用户空间,例如 [bpftime](https://github.com/eunomia-bpf/bpftime),实现更高性能的用户态追踪、性能分析、插件等等。
|
||||
|
||||
## 2. 关于如何学习 eBPF 相关的开发的一些建议
|
||||
|
||||
本文不会对 eBPF 的原理做更详细的介绍,不过这里有一个学习规划和参考资料,也许会有一些价值:
|
||||
|
||||
@@ -25,6 +25,8 @@ Currently, eBPF is still in the early stages. However, with the help of the kern
|
||||
|
||||
Moreover, eBPF has the ability to write data to any address of a user space process (bpf_probe_write_user[7]), partially modify the return value of a kernel function (bpf_override_return[8]), and even directly execute certain system calls in kernel mode[9]. Fortunately, eBPF performs strict security checks on the bytecode before loading it into the kernel to ensure that there are no operations such as memory out-of-bounds. Moreover, many features that may expand the attack surface and pose security risks need to be explicitly enabled during kernel compilation. Before loading the bytecode into the kernel, the Wasm virtual machine can also choose to enable or disable certain eBPF features to ensure the security of the sandbox.
|
||||
|
||||
In addition to the kernel mode eBPF runtime, eBPF can also be extended to user space, for example, [bpftime](https://github.com/eunomia-bpf/bpftime) to achieve higher performance user space tracing, performance analysis, plugins, etc.
|
||||
|
||||
## 2. Some Tips on Learning eBPF Development
|
||||
|
||||
This article will not provide a more detailed introduction to the principles of eBPF, but here is a learning plan and reference materials that may be of value:
|
||||
|
||||
@@ -42,7 +42,7 @@ TLS 和 SSL 不完全适合 OSI 模型或 TCP/IP 模型的任何单一层次。T
|
||||
|
||||
eBPF (Extended Berkeley Packet Filter): 是一种内核技术,允许用户在内核空间中运行预定义的程序,不需要修改内核源代码或重新加载模块。它创建了一个桥梁,使得用户空间和内核空间可以交互,从而为系统监控、性能分析和网络流量分析等任务提供了无前例的能力。
|
||||
|
||||
uprobes 是eBPF的一个重要特性,允许我们在用户空间应用程序中动态地插入探测点,特别适用于跟踪SSL/TLS库中的函数调用。
|
||||
uprobes 是eBPF的一个重要特性,允许我们在用户空间应用程序中动态地插入探测点,特别适用于跟踪SSL/TLS库中的函数调用。Uprobe 在内核态 eBPF 运行时,也可能产生比较大的性能开销,这时候也可以考虑使用用户态 eBPF 运行时,例如 [bpftime](https://github.com/eunomia-bpf/bpftime)。bpftime 是一个基于 LLVM JIT/AOT 的用户态 eBPF 运行时,它可以在用户态运行 eBPF 程序,和内核态的 eBPF 兼容,避免了内核态和用户态之间的上下文切换,从而提高了 eBPF 程序的执行效率。对于 uprobe 而言,bpftime 的性能开销比 kernel 小一个数量级。
|
||||
|
||||
### 用户态库
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ TLS and SSL don't perfectly align with any single layer of the OSI model or the
|
||||
|
||||
eBPF (Extended Berkeley Packet Filter): It's a kernel technology that allows users to run predefined programs in the kernel space without modifying kernel source code or reloading modules. It creates a bridge that enables interaction between user space and kernel space, providing unprecedented capabilities for tasks like system monitoring, performance analysis, and network traffic analysis.
|
||||
|
||||
uprobes are a significant feature of eBPF, allowing dynamic insertion of probe points in user space applications, particularly useful for tracking function calls in SSL/TLS libraries.
|
||||
uprobes are a significant feature of eBPF, allowing dynamic insertion of probe points in user space applications, particularly useful for tracking function calls in SSL/TLS libraries. Uprobe in kernel mode eBPF runtime may also cause relatively large performance overhead. In this case, you can also consider using user mode eBPF runtime, such as [bpftime](https://github.com/eunomia-bpf/bpftime)。bpftime is a user mode eBPF runtime based on LLVM JIT/AOT. It can run eBPF programs in user mode and is compatible with kernel mode eBPF, avoiding context switching between kernel mode and user mode, thereby improving the execution efficiency of eBPF programs. bpftime can have a performance overhead that is one order of magnitude smaller than that of kernel mode eBPF.
|
||||
|
||||
### User-Space Libraries
|
||||
|
||||
|
||||
155
src/36-userspace-ebpf/README.md
Normal file
155
src/36-userspace-ebpf/README.md
Normal file
@@ -0,0 +1,155 @@
|
||||
# 用户空间 eBPF 运行时:深度解析与应用实践
|
||||
|
||||
郑昱笙
|
||||
|
||||
本文旨在对用户空间的 eBPF 运行时和对应的一些应用场景进行剖析和总结。尽管大多数人对基于内核的 eBPF 已有所了解,用户空间 eBPF 的进展和应用实践同样引人注目。本文还将探讨用户空间 eBPF 运行时与 Wasm 运行时的技术比较,后者在云原生和边缘计算领域已获得广泛的关注。我们也新开源了一个用户态 eBPF 运行时 [bpftime](https://github.com/eunomia-bpf/bpftime)。通过 LLVM `JIT/AOT` 后端支持,我们的基准测试表明 bpftime 是最快的用户空间 eBPF 运行时之一,同时还可以让内核中间的 eBPF Uprobe 无缝在用户空间运行,获得近十倍的性能提升。
|
||||
|
||||
## eBPF:内核的动态扩展运行时与字节码
|
||||
|
||||
### eBPF 究竟是何方神圣?
|
||||
|
||||
eBPF,全称 "extended Berkeley Packet Filter",是一项允许在不更改内核源代码或重启系统的情况下动态干预和修改内核行为的革命性技术。虽然 eBPF 起初是作为网络数据包过滤工具而设计,但如今已广泛应用于从性能分析到安全策略等多个方面,逐渐成为系统管理员的得力助手。
|
||||
|
||||
eBPF 的前身,Berkeley Packet Filter (BPF) —— 20 世纪 90 年代初的产物,主要用于网络数据包的高效过滤。尽管 BPF 已被广大用户所认可,eBPF 的出现则为其带来了更为广泛的指令集,并能直接与内核数据结构互动。自 2014 年 Linux 内核引入 eBPF 以后,它的影响力迅速扩张。Linux 的核心开发团队不断地完善 eBPF,使其从一个基础的网络数据包过滤器逐渐演变为一个功能强大的字节码引擎。
|
||||
|
||||
### eBPF 对现代计算和网络的深远影响
|
||||
|
||||
随着现代计算环境日益复杂,实时数据的采集和深入分析显得尤为重要。在这一背景下,eBPF 凭借其卓越的动态性,为开发者和管理员提供了实时干预系统行为的强大工具。eBPF 以其卓越的灵活性在现代网络解决方案中占据核心地位。它为流量控制、负载均衡及安全策略在内核级别提供了细致的控制手段,确保了系统的性能优化和安全稳定。同时,eBPF 在系统可观察性上也做出了显著贡献,为各种系统调用和硬件事件提供了详细的可编程追踪方案,促进了问题的迅速定位和解决。
|
||||
|
||||
## 用户空间 eBPF 运行时:eBPF 的新生代
|
||||
|
||||
### 什么是用户空间 eBPF 运行时?
|
||||
|
||||
虽然 eBPF 最初是为内核设计的,但它在用户空间的巨大潜力,以及内核对于 `GPL LICENSE` 的限制,也催生了用户空间 eBPF 运行时的产生。这些运行时允许开发者在内核之外利用 eBPF 的能力,提供了一个在内核之外的运行平台,扩展其实用性和适用性,同时不受限于 GPL LICENSE。虽然 eBPF 的一个突出特点是其在内核空间内执行代码的能力,提供快速的可观察性和数据聚合,但在某些情境下,拥有一个用户空间的替代方案变得非常有价值。这些用户空间运行时扩展了 eBPF 多功能性的范围,超越了内核集成,并常常作为特定用例的实验场地、调试工具或框架。
|
||||
|
||||
### 特定运行时简介
|
||||
|
||||
#### **ubpf**
|
||||
|
||||
[uBPF](https://github.com/iovisor/ubpf) 是将 eBPF 引入用户空间的早期尝试之一。主要作为一个概念证明,它作为 eBPF 解释器的用户空间解释与 x86_64 和 arm64 JIT 的结合。尽管其起源是一个早期原型,uBPF 吸引了注意并被用作高性能网络项目(如 DPDK 和 Oko)的基础。它的非 GPL 许可证(Apache)使其适用于各种项目,包括非开源项目。然而,最近,uBPF 正在迎头赶上内核发展,特别是微软为其 eBPF Windows 实现做出的贡献。但是,开发 ubpf 和 rbpf 程序可能需要一个特定的工具链,这对于一些用户可能是一个障碍。ubpf 只有一个有限的哈希 maps 实现,对大多数场景而言可能不够。另外,ubpf 本身只是一个虚拟机/解释器,在实际的使用中,依然需要编写胶水代码,和其他用户空间程序进行编译、链接后才能使用。
|
||||
|
||||
#### **rbpf**
|
||||
|
||||
[rbpf](https://github.com/qmonnet/rbpf) 和 uBPF 非常相似,但重点是使用了 Rust 进行开发,这是一种因其内存安全保证而著称的语言。创建 rbpf 是由于想要探索 eBPF 和 Rust 的交集。虽然没有广泛采纳,但 rbpf 的知名用户包括 Solana 团队,他们使用它为带有 eBPF 驱动的智能合约的区块链工具。rbpf 的一个优势在于其许可证 (MIT),允许在各种项目中广泛重用。rbpf 也缺乏 eBPF Maps 支持,并且仅为 x86_64 提供 JIT 支持。同样,rbpf 也需要编译和手动嵌入对应的应用程序中才可以使用。
|
||||
|
||||
#### **bpftime**
|
||||
|
||||
基于 LLVM JIT/AOT 构建的 [bpftime](https://github.com/eunomia-bpf/bpftime) 是专为用户空间操作设计的一个高性能 eBPF 运行时。它以其快速的 Uprobe 能力和 Syscall 钩子脱颖而出,尤其是 Uprobe 性能比内核提高了十倍。此外,bpftime 提供编程 syscall 钩子、共享内存映射和与熟悉的工具链(如 libbpf 和 clang)的兼容性。其设计解决了一些内核 eBPF 的限制,并在某些方面超越了像 Wasm 运行时这样的插件系统。这是使用 Userspace bpftime 的 eBPF 进行 Hook 的一些性能数据,将用户空间和内核空间进行对比:
|
||||
|
||||
| Probe/Tracepoint Types | Kernel (ns) | Userspace (ns) | Insn Count |
|
||||
|------------------------|-------------:|---------------:|---------------:|
|
||||
| Uprobe | 3224.172760 | 314.569110 | 4 |
|
||||
| Uretprobe | 3996.799580 | 381.270270 | 2 |
|
||||
| Syscall Tracepoint | 151.82801 | 232.57691 | 4 |
|
||||
| Embedding runtime | Not avaliable | 110.008430 | 4 |
|
||||
|
||||
bpftime 可以类似 Kernel 中的 Uprobe 那样,自动将 eBPF 运行时注入到用户空间进程中,无需修改用户空间进程的代码,也无需进行重启进程即可使用。对于 ubpf 和 rbpf 而言,它们依然需要手动编写胶水代码和其他用户空间程序进行集成,相对来说限制了它们的使用场景。在某些场景下,bpftime 可能能作为 kernel eBPF 的一种替代方案,它也不依赖于具体内核版本或 Linux 平台,可以在其他平台上运行。
|
||||
|
||||
## 为什么用户空间版本的 eBPF 会吸引如此多的关注?
|
||||
|
||||
eBPF,原本因其在内核空间的强大性能而被广泛认知,但近年来,其在用户空间的实现也引起了业界的浓厚兴趣。以下是技术社区对于 eBPF 迁移到用户空间的热切关注的核心原因:
|
||||
|
||||
### 性能提升
|
||||
|
||||
在内核空间,eBPF 的 Uprobe 组件时常面临因上下文切换带来的性能瓶颈。这在延迟敏感的应用中可能导致不良影响,从而对实时监控和数据处理带来挑战。但用户空间版本的 eBPF 能够绕过与上下文切换有关的性能损失,实现更高的性能优化。例如,`bpftime` 运行时在用户空间的表现,相较于其内核版本,展现出了显著的性能增益。
|
||||
|
||||
### 灵活性与集成度
|
||||
|
||||
用户空间的 eBPF 运行时带来了更大的灵活性。与其他解决方案如 Wasm 运行时相比,它们无需手动集成即可提供自动插桩的特性。这意味着开发者可以轻松地将其集成进正在运行的进程中,避免了因重新启动或重新编译带来的操作中断。
|
||||
|
||||
### 安全性加固
|
||||
|
||||
在内核空间,eBPF 的执行通常需要 root 访问权限,这可能无意中增加了系统的攻击面,使其容易受到例如容器逃逸或潜在的内核利用等安全威胁。相反,用户空间的实现在这种高风险环境之外运作。它们在用户空间中运行,大大降低了对高权限的依赖,从而减少了潜在的安全风险。
|
||||
|
||||
### 调试与许可的便利性
|
||||
|
||||
用户空间 eBPF 的一个显著优点是,它为开发者提供了更加直观的调试环境。相对于内核空间中有限的调试手段,用户空间解释器提供的断点调试功能更为方便。此外,用户空间 eBPF 的许可证更加灵活,通常采用 Apache 或 MIT 这样的开源许可,这意味着它们可以轻松地与各种项目(包括商业项目)相结合,避免了与内核代码相关的 GPL 限制。
|
||||
|
||||
## 使用案例:现有的 eBPF 用户空间应用
|
||||
|
||||
用户空间 eBPF 正在项目中使用,每个项目都利用 eBPF 的独特功能来增强它们的功能:
|
||||
|
||||
1. [**Oko:**](https://github.com/Orange-OpenSource/Oko)
|
||||
|
||||
Oko 是 Open vSwitch-DPDK 的扩展,提供了与 BPF 程序的运行时扩展。它允许使用 BPF 程序在用户空间处理数据包,提供灵活的数据包处理,并促进 Open vSwitch 与其他系统的集成。
|
||||
|
||||
1. [**DPDK eBPF 支持:**](https://www.dpdk.org/wp-content/uploads/sites/35/2018/10/pm-07-DPDK-BPFu6.pdf)
|
||||
|
||||
DPDK (数据平面开发套件) eBPF 支持通过允许在用户空间使用 eBPF 程序来促进快速的数据包处理,这些程序可以加载并运行以分析网络数据包。这增强了网络应用的灵活性和可编程性,无需修改内核。
|
||||
|
||||
1. [**Solana:**](https://solana.com/)
|
||||
|
||||
Solana 利用 eBPF 实现一个 JIT (即时)编译器,这对于在其区块链网络上执行智能合约是至关重要的。使用 eBPF 确保了安全性、性能和架构中立性,从而允许在 Solana 区块链上的验证器节点上高效地执行智能合约。
|
||||
|
||||
1. [**eBPF for Windows (进行中的工作):**](https://github.com/microsoft/ebpf-for-windows)
|
||||
|
||||
该项目旨在将 Linux 生态系统中熟悉的 eBPF 工具链和 API 带到 Windows,允许在 Windows 之上使用现有的 eBPF 工具链。这展示了将 eBPF 的功能扩展到 Linux 之外的有前景的尝试,尽管它仍然是一个进行中的工作。
|
||||
|
||||
使用 eBPF 的这些应用的好处包括:
|
||||
|
||||
- **灵活性:** eBPF 提供了一个灵活的框架,用于在内核或用户空间中运行程序,使开发人员能够扩展现有系统的功能,而无需修改其核心代码。
|
||||
- **性能:** 通过允许 JIT 编译和高效的数据包处理,eBPF 可以显著提高网络应用和区块链智能合约执行的性能。
|
||||
- **安全性和安全性:** eBPF 框架为验证程序执行前的安全属性提供了机制,从而确保了其集成的系统的完整性和安全性。
|
||||
- **跨平台能力:** eBPF 指令集的架构中立性使得跨平台兼容性成为可能,如 Solana 项目和进行中的 eBPF for Windows 所示。
|
||||
|
||||
这些属性使 eBPF 成为增强各种应用的强大工具,从网络处理到区块链智能合约执行,再到更多。还有一些论文讨论了在用户空间中使用 eBPF 的用途:
|
||||
|
||||
1. [**RapidPatch: 用于实时嵌入式设备的固件热修复**](https://www.usenix.org/conference/usenixsecurity22/presentation/he-yi):
|
||||
|
||||
本文介绍了一个名为 RapidPatch 的新的热修复框架,该框架旨在通过在异构嵌入式设备上安装通用修复程序来促进修复的传播,而不会中断它们上运行的其他任务。此外,RapidPatch 提出了两种类型的 eBPF 补丁,用于不同类型的漏洞,并开发了一个 eBPF 补丁验证器以确保补丁安全。
|
||||
|
||||
2. [**Femto-Containers: 低功耗 IoT 微控制器上的小型软件功能的轻量级虚拟化和故障隔离**](https://arxiv.org/abs/2210.03432):
|
||||
|
||||
本文介绍了 Femto-Containers,这是一个新颖的框架,允许在低功耗 IoT 设备上安全地部署、执行和隔离小型虚拟软件功能。该框架在 RIOT 中实现并提供,RIOT 是一个受欢迎的开源 IoT 操作系统,强调在低功耗 IoT 设备上安全地部署、执行和隔离小型虚拟软件功能。该论文讨论了在一个常见的低功耗 IoT 操作系统 (RIOT) 中集成的 Femto-Container 主机引擎的实现,增强了其在标准的 IPv6/6LoWPAN 网络上按需启动、更新或终止 Femto-Containers 的能力。
|
||||
|
||||
这些论文深入探讨了固件补丁和轻量级虚拟化方面的相关进展,展示了针对实时嵌入式系统和低功耗 IoT 微控制器领域的关键挑战的创新。
|
||||
|
||||
## 用户空间 eBPF 运行时 vs Wasm 运行时
|
||||
|
||||
在不断发展的云原生和边缘计算领域中,eBPF (扩展的伯克利数据包过滤器) 和 Wasm (WebAssembly) 都已成为强大的工具。但它们都有自己的设计原则和权衡取舍。
|
||||
|
||||
## eBPF 在用户空间运行时 vs Wasm 运行时:云原生计算的新纪元
|
||||
|
||||
在飞速进展的云原生与边缘计算生态中,eBPF (扩展的伯克利数据包过滤器) 和 Wasm (WebAssembly) 被广泛认为是两大技术巨头。这两者虽然都非常强大,但各有其独特的设计哲学与优缺点。
|
||||
|
||||
### eBPF 与 Wasm 之间的技术差异
|
||||
|
||||
**eBPF**:
|
||||
|
||||
- **核心理念**:eBPF 是为了满足高性能要求而设计的,特别是针对实时内核交互和高吞吐量的网络任务。
|
||||
- **安全性**:尽管eBPF的主要焦点是性能,但其验证器机制确保了执行的程序在不引发内核恐慌或无限循环的前提下的安全性。
|
||||
|
||||
**Wasm**:
|
||||
|
||||
- **核心理念**:Wasm 诞生于网络环境,其设计重点在于可移植性和执行安全性,旨在实现接近本地机器代码的执行速度。
|
||||
- **安全性**:Wasm 的安全策略主要基于软件故障隔离 (SFI)。沙盒执行确保了代码的安全性,但这可能会带来某些运行时的额外开销。
|
||||
|
||||
这两种技术都依赖于底层的库来执行复杂任务,如 Wasm 所依赖的 `Wasi-nn` 来进行神经网络处理。与这些外部API 交互时,特别是在 Wasm 的环境下,需要进行更多的验证和运行时检查,这可能导致额外的性能损耗。而eBPF则提供了一个更为性能中心化的策略,其验证器确保了代码在主机上的安全执行,而不需要运行时的额外开销。
|
||||
|
||||
在语言支持上,由于 eBPF 的专业特性,其语言选择较为有限,通常是 C 和 Rust。而Wasm则支持更多的编程语言,包括但不限于 C、C++、Rust、Go、Python、Java和C#。这使得Wasm在跨平台部署上有更大的灵活性,但也可能因为不恰当的语言选择引入更多的性能开销。
|
||||
|
||||
为了给大家提供一个直观的对比,我们在 [https://github.com/eunomia-bpf/bpf-benchmark](https://github.com/eunomia-bpf/bpf-benchmark)中展示了eBPF和Wasm运行时的性能比较。
|
||||
|
||||
从更宏观的角度看,eBPF运行时和Wasm实际上可以被视为是相互补充的。尽管 eBPF 拥有出色的验证器机制来确保运行时安全性,但由于其编程语言的局限性和相对较高的开发难度,它并不总是适合作为业务逻辑的首选运行时。反之,eBPF 更适用于像网络流量转发、可观测性和 livepatch 这样的高专业性任务。相对而言,Wasm 运行时可以作为 Serverless 的运行时平台、插件系统和轻量级虚拟化等场景的首选。这两者都有自己的优势,但它们的选择取决于特定的用例和优先级。
|
||||
|
||||
## 总结与前景
|
||||
|
||||
用户空间的eBPF运行时正在打破边界,将eBPF的能力从内核扩展到了更广阔的领域。这种扩展带来了显著的性能、灵活性和安全性提升。例如,`bpftime`运行时显示了其在某些低级性能场景下,甚至超越了像 Wasm 这样的其他技术。也有越来越多的应用将用户空间的 eBPF 用于快速补丁、轻量级虚拟化、网络过滤等场景。
|
||||
|
||||
Wasm 的主要焦点在于可移植性、轻量级虚拟化、安全性、多语言等等,而 eBPF 则针对那些对性能有严格要求的基础设施任务提供了更多的性能优势和动态插桩特性。选择哪种技术取决于特定的需求和优先级。随着它们的进一步发展,用户空间的eBPF运行时正在成为云原生技术堆栈中的重要部分,为业界带来前所未有的安全、效率和创新的组合。
|
||||
|
||||
> 我们诚邀您深入探索用户空间eBPF的世界,您可以从我们的项目 [https://github.com/eunomia-bpf/bpftime](https://github.com/eunomia-bpf/bpftime) 开始。您的贡献、反馈或仅仅是对此工具的使用和 star,都可以为我们的社区带来巨大价值。
|
||||
>
|
||||
> 若您在研究中采用了我们的`bpftime`项目,请[引用我们的仓库](https://github.com/eunomia-bpf/bpftime/blob/master/CITATION.cff)。我们期待您的宝贵意见和反馈,您可以通过 GitHub 仓库的 issue、邮箱 [yunwei356@gmail.com](mailto:yunwei356@gmail.com) 或微信 yunwei2567 与我们联系。
|
||||
|
||||
## 参考资料
|
||||
|
||||
1. bpftime: <https://github.com/eunomia-bpf/bpftime>
|
||||
2. ubpf: <https://github.com/iovisor/ubpf>
|
||||
3. rbpf: <https://github.com/qmonnet/rbpf>
|
||||
4. Oko: <https://github.com/Orange-OpenSource/Oko>
|
||||
5. RapidPatch: Firmware Hotpatching for Real-Time Embedded Devices: <https://www.usenix.org/conference/usenixsecurity22/presentation/he-yi>
|
||||
6. DPDK eBPF Support: <https://www.dpdk.org/wp-content/uploads/sites/35/2018/10/pm-07-DPDK-BPFu6.pdf>
|
||||
7. Solana: <https://solana.com/>
|
||||
8. eBPF for Windows (Work-In-Progress): <https://github.com/microsoft/ebpf-for-windows>
|
||||
9. Femto-Containers: Lightweight Virtualization and Fault Isolation For Small Software Functions on Low-Power IoT Microcontrollers: <https://arxiv.org/abs/2210.03432>
|
||||
154
src/36-userspace-ebpf/README_en.md
Normal file
154
src/36-userspace-ebpf/README_en.md
Normal file
@@ -0,0 +1,154 @@
|
||||
# Userspace eBPF Runtimes: Overview and Applications
|
||||
|
||||
Yusheng Zheng
|
||||
|
||||
In this blog post, we'll dive into the world of eBPF in userspace. While many are familiar with kernel-based eBPF, userspace eBPF runtimes have been making significant strides and offer compelling use cases. We will also compare userspace eBPF runtimes with Wasm runtimes, another popular technology in the cloud-native and edge computing landscape. Among these, we're excited to introduce [bpftime](https://github.com/eunomia-bpf/bpftime). Powered by an LLVM `JIT/AOT` backend, our benchmarks suggest that bpftime stands out as one of the fastest userspace eBPF runtimes available.
|
||||
|
||||
## Introduction to eBPF
|
||||
|
||||
### What is eBPF?
|
||||
|
||||
eBPF, which stands for "extended Berkeley Packet Filter," is a revolutionary technology that facilitates the dynamic tracing and monitoring of kernel operations without modifying kernel source code or rebooting the system. Originally designed for network packet filtering, eBPF has evolved to support a wide range of applications, from performance analysis to security, making it a versatile tool in a system administrator's arsenal.
|
||||
|
||||
The story of eBPF begins with the Berkeley Packet Filter (BPF), introduced in the early 1990s as a way to filter and capture network packets efficiently. Over the years, BPF proved to be an invaluable asset, but there was room for improvement. eBPF emerged as an advanced iteration of BPF, equipped with a richer instruction set and the capability to interact with kernel data structures directly.
|
||||
|
||||
The Linux kernel adopted eBPF around 2014, and since then, its popularity and adoption have skyrocketed. Key contributors to the Linux kernel worked diligently to evolve eBPF from a simple packet filter to a generic and powerful bytecode engine.
|
||||
|
||||
### Its significance in modern computing and network solutions
|
||||
|
||||
In today's complex computing environments, the need for real-time data and insights is paramount. eBPF shines in this regard, allowing developers and administrators to introspect and modify system behaviors on the fly.
|
||||
|
||||
Given its dynamic nature, eBPF has become a cornerstone of modern networking solutions. It enables fine-grained traffic control, load balancing, and security enforcement at the kernel level, ensuring optimal performance and security. Furthermore, in the realm of observability, eBPF provides granular insights into system calls, hardware events, and more, facilitating proactive problem detection and resolution.
|
||||
|
||||
### eBPF: from kernel runtime to userspace runtime
|
||||
|
||||
While the initial design of eBPF was deeply embedded within the kernel, the demand for similar functionality in userspace applications led to the evolution of userspace eBPF runtimes. These runtimes allow developers to leverage eBPF's capabilities outside the kernel, expanding its utility and applicability. Userspace eBPF runtimes make it feasible to apply eBPF's prowess to a broader set of applications, from custom network protocols to novel security solutions, further cementing eBPF's role as a transformative technology in the computing landscape.
|
||||
|
||||
## Userspace eBPF Runtimes and Their Role
|
||||
|
||||
### What is a userspace eBPF runtime?
|
||||
|
||||
A userspace eBPF runtime provides a platform outside of the kernel to run eBPF programs. While one of eBPF's standout attributes is its capability to execute code within the kernel space, offering rapid observability and data aggregation, there are scenarios where having a userspace alternative becomes valuable. These userspace runtimes extend the reach of eBPF's versatility to areas beyond kernel integrations and often serve as experimental grounds, debugging tools, or frameworks for specific use cases.
|
||||
|
||||
### Introduction to specific runtimes
|
||||
|
||||
#### **ubpf**
|
||||
|
||||
[uBPF](https://github.com/iovisor/ubpf) was among the early attempts to bring eBPF to the userspace. Conceived primarily as a proof-of-concept, it served as a user-space interpretation of an eBPF interpreter combined with an x86_64 and arm64 JIT. Despite its origins as an early prototype, uBPF garnered attention and was utilized as a foundation for high-performance networking projects such as DPDK and Oko. Its non-GPL licensing (Apache) makes it favorable for a wide range of projects, inclusive of proprietary ones. However, as of recent, uBPF is catching up with kernel developments, particularly with contributions from Microsoft for its eBPF Windows implementation. However, develop ubpf and rbpf programs may require a specific toolchain, which may be a barrier for some users. ubpf only have a limited hashmap implementation, which may not be enough for some users.
|
||||
|
||||
#### **rbpf**
|
||||
|
||||
[rbpf](https://github.com/qmonnet/rbpf) is heavily influenced by uBPF but with an emphasis on Rust, a language renowned for its memory safety guarantees. The creation of rbpf was driven by a desire to explore the intersections of eBPF and Rust. While not as widespread in adoption, notable users of rbpf include the Solana team, employing it for blockchain tools with eBPF-driven smart contracts. One of rbpf's advantages lies in its licensing (MIT), allowing for broad reuse across various projects. rbpf also lacks eBPF map support, and only has JIT support for x86_64.
|
||||
|
||||
#### **bpftime**
|
||||
|
||||
Built atop LLVM JIT/AOT, [bpftime](https://github.com/eunomia-bpf/bpftime) is a cutting-edge, high-performance eBPF runtime designed exclusively for userspace operations. It stands out with its rapid Uprobe capabilities and Syscall hooks, notably outperforming the kernel Uprobe by a tenfold margin. Additionally, bpftime offers programmatic syscall hooking, shared memory maps, and compatibility with familiar toolchains like libbpf and clang. Its design addresses some kernel eBPF limitations and outpaces plugin systems like the Wasm runtime in certain aspects.
|
||||
|
||||
## Why is Having a Userspace Version of eBPF Interesting?
|
||||
|
||||
eBPF, while renowned for its kernel-space operations, has observed a growing interest in its userspace adaptations. Here's why migrating eBPF to userspace is capturing the attention of technologists:
|
||||
|
||||
### Enhanced Performance
|
||||
|
||||
In kernel operations, the Uprobe component of eBPF is often beleaguered by performance inefficiencies, primarily due to the overheads introduced by context switches. In latency-sensitive applications, these inefficiencies can be detrimental, affecting real-time monitoring and data processing. By transitioning to userspace, eBPF can bypass these context switch related delays, leading to a more optimized performance. Runtimes like `bpftime` exemplify this, offering substantial performance improvements compared to their kernel counterparts.
|
||||
|
||||
### Flexibility and Integration
|
||||
|
||||
Userspace eBPF runtimes champion flexibility. Unlike some alternatives, such as the Wasm runtime, which might necessitate manual integrations, userspace eBPF provides the boon of automatic instrumentation. This means they can be seamlessly introduced into running processes without the need for cumbersome restarts or recompilations, ensuring smoother operational flows.
|
||||
|
||||
### Augmented Security
|
||||
|
||||
Operating in kernel mode, eBPF programs require root access, which can inadvertently expand the attack surface, making systems susceptible to vulnerabilities like container escapes or even potential kernel exploits. Userspace runtimes, however, operate outside this high-risk zone. By functioning in userspace, they demand fewer privileges, inherently reducing the potential avenues for security breaches.
|
||||
|
||||
### Debugging and Licensing Flexibility
|
||||
|
||||
One of the innate advantages of userspace eBPF runtimes is the ease with which developers can debug their code. The accessibility to integrate breakpoints in a userspace interpreter is a marked advantage over the relatively constrained debugging capabilities in kernel eBPF. Additionally, the licensing flexibility of userspace eBPF runtimes, typically offered under licenses like Apache or MIT, ensures they can be paired with a diverse range of projects, including proprietary ones, sidestepping the GPL constraints associated with kernel code.
|
||||
|
||||
## Use Cases: Existing eBPF Userspace Applications
|
||||
|
||||
Userspace eBPF is being utilized in a number of notable projects, each harnessing the unique capabilities of eBPF to enhance their functionalities. Here's how Userspace eBPF is currently utilized in various applications:
|
||||
|
||||
1. [**Oko:**](https://github.com/Orange-OpenSource/Oko)
|
||||
|
||||
Oko is an extension of Open vSwitch-DPDK that provides runtime extension with BPF programs. It enables the use of BPF programs to process packets in userspace, providing flexible packet processing and facilitating the integration of Open vSwitch with other systems.
|
||||
|
||||
1. [**DPDK eBPF Support:**](https://www.dpdk.org/wp-content/uploads/sites/35/2018/10/pm-07-DPDK-BPFu6.pdf)
|
||||
|
||||
The DPDK (Data Plane Development Kit) eBPF support facilitates fast packet processing by enabling the use of eBPF programs in userspace, which can be loaded and run to analyze network packets. This enhances the flexibility and programmability of network applications without requiring kernel modifications.
|
||||
|
||||
1. [**Solana:**](https://solana.com/)
|
||||
|
||||
Solana utilizes eBPF to implement a JIT (Just-In-Time) compiler, which is essential for executing smart contracts on its blockchain network. The use of eBPF ensures safety, performance, and architecture agnosticism, thus allowing efficient execution of smart contracts across validator nodes on the Solana blockchain.
|
||||
|
||||
1. [**eBPF for Windows (Work-In-Progress):**](https://github.com/microsoft/ebpf-for-windows)
|
||||
|
||||
This project is aimed at bringing the eBPF toolchains and APIs familiar in the Linux ecosystem to Windows, allowing existing eBPF toolchains to be utilized on top of Windows. This demonstrates a promising endeavor to extend the capabilities of eBPF beyond Linux, although it's still a work in progress.
|
||||
|
||||
The benefits of using eBPF in these applications include:
|
||||
|
||||
- **Flexibility:** eBPF provides a flexible framework for running programs in the kernel or userspace, enabling developers to extend the functionality of existing systems without modifying their core code.
|
||||
- **Performance:** By allowing JIT compilation and efficient packet processing, eBPF can significantly enhance the performance of network applications and blockchain smart contract execution.
|
||||
- **Safety and Security:** The eBPF framework provides mechanisms for verifying the safety properties of programs before execution, thus ensuring the integrity and security of the systems it is integrated with.
|
||||
- **Cross-platform Capability:** The architecture-agnostic nature of eBPF instruction set enables cross-platform compatibility, as seen in projects like Solana and the work-in-progress eBPF for Windows.
|
||||
|
||||
These attributes make eBPF a powerful tool for augmenting a variety of applications, ranging from network processing to blockchain smart contract execution, and beyond. There are also some papers that discuss the use of eBPF in userspace:
|
||||
|
||||
1. [**RapidPatch: Firmware Hotpatching for Real-Time Embedded Devices**](https://www.usenix.org/conference/usenixsecurity22/presentation/he-yi):
|
||||
|
||||
This paper introduces a new hotpatching framework named RapidPatch, which is designed to facilitate the propagation of patches by installing generic patches on heterogeneous embedded devices without disrupting other tasks running on them.
|
||||
|
||||
Furthermore, RapidPatch proposes two types of eBPF patches for different types of vulnerabilities and develops an eBPF patch verifier to ensure patch safety.
|
||||
|
||||
1. [**Femto-Containers: Lightweight Virtualization and Fault Isolation For Small Software Functions on Low-Power IoT Microcontrollers**](https://arxiv.org/abs/2210.03432):
|
||||
|
||||
This paper presents Femto-Containers, a novel framework that enables the secure deployment, execution, and isolation of small virtual software functions on low-power IoT devices over a network.
|
||||
|
||||
The framework is implemented and provided in RIOT, a popular open source IoT operating system, with an emphasis on secure deployment, execution, and isolation of small virtual software functions on low-power IoT devices, over the network.
|
||||
|
||||
The paper discusses the implementation of a Femto-Container hosting engine integrated within a common low-power IoT operating system (RIOT), enhancing it with the ability to start, update, or terminate Femto-Containers on demand, securely over a standard IPv6/6LoWPAN network.
|
||||
|
||||
These papers delve into pertinent advancements concerning firmware patching and lightweight virtualization, demonstrating innovations that address critical challenges in the domains of real-time embedded systems and low-power IoT microcontrollers respectively.
|
||||
|
||||
## Userspace eBPF Runtime vs Wasm Runtime
|
||||
|
||||
In the evolving landscape of cloud-native and edge computing, both eBPF (extended Berkeley Packet Filter) and Wasm (WebAssembly) have emerged as powerful tools. However, they come with their own set of design principles and trade-offs.
|
||||
|
||||
### A Comparison of eBPF and Wasm
|
||||
|
||||
**eBPF**:
|
||||
|
||||
- **Philosophy**: eBPF prioritizes performance, often making it the choice for real-time kernel operations and high-throughput networking tasks.
|
||||
- **Security**: While performance takes the forefront, security in eBPF is ensured through the use of a verifier, ensuring that all programs are safe to run without causing kernel panics or infinite loops.
|
||||
|
||||
**Wasm**:
|
||||
|
||||
- **Philosophy**: Originally designed for the web, Wasm places a higher emphasis on portability and security. It was conceived to execute code nearly as fast as running native machine code and ensures safety in hostile environments like web browsers.
|
||||
- **Security**: The primary security model for Wasm revolves around Software Fault Isolation (SFI). This model guarantees safe execution by enforcing sandboxing, even though this can introduce some runtime overheads.
|
||||
|
||||
For both technologies, reliance on underlying libraries for complex operations is paramount. For instance, Wasm leans on libraries like `Wasi-nn` for neural network operations. However, when interfacing with such external APIs, especially in Wasm's context, there's a need for additional validation and runtime checks, sometimes leading to substantial performance costs. eBPF, when embedded within the host, capitalizes on its verifier to ensure code safety, offering a more performance-centric approach.
|
||||
|
||||
On the language support front, while eBPF's niche and specialized nature mean limited language support, Wasm boasts a broader language portfolio due to its origin and design for the web.
|
||||
|
||||
## Conclusion
|
||||
|
||||
Userspace eBPF runtimes are an exciting development that expands the capabilities of eBPF beyond the kernel. As highlighted in this post, they offer compelling benefits like enhanced performance, flexibility, and security compared to kernel-based eBPF. Runtimes like bpftime demonstrate the potential for substantial speedups, even outperforming alternatives like Wasm runtimes in certain dimensions like low-level performance.
|
||||
|
||||
With innovative frameworks like RapidPatch and Femto-Containers utilizing userspace eBPF for patching and lightweight virtualization respectively, we are witnessing pioneering use cases that address critical challenges in embedded systems and IoT domains. As eBPF continues its evolution in userspace, we can expect even more creative applications that augment everything from smart contracts to network protocols.
|
||||
|
||||
While alternatives like Wasm certainly have their place with a strong emphasis on web portability and security, eBPF's specialized nature gives it an edge for performance-critical tasks. Ultimately, the choice between the two depends on the specific use case and priorities. As they continue to evolve, userspace eBPF runtimes are cementing their position as an indispensable part of the cloud-native technology stack, offering an unparalleled combination of safety, efficiency and innovation.
|
||||
|
||||
> We encourage our readers to dive deep into the world of userspace eBPF, starting with our bpftime GitHub repository: <https://github.com/eunomia-bpf/bpftime> Contributions, feedback, or simply using the tool can further the cause and provide invaluable insights to the community.
|
||||
>
|
||||
> If you use our project in research, please [cite our repo](https://github.com/eunomia-bpf/bpftime/blob/master/CITATION.cff).
|
||||
|
||||
## reference
|
||||
|
||||
1. bpftime: <https://github.com/eunomia-bpf/bpftime>
|
||||
2. ubpf: <https://github.com/iovisor/ubpf>
|
||||
3. rbpf: <https://github.com/qmonnet/rbpf>
|
||||
4. Oko: <https://github.com/Orange-OpenSource/Oko>
|
||||
5. RapidPatch: Firmware Hotpatching for Real-Time Embedded Devices: <https://www.usenix.org/conference/usenixsecurity22/presentation/he-yi>
|
||||
6. DPDK eBPF Support: <https://www.dpdk.org/wp-content/uploads/sites/35/2018/10/pm-07-DPDK-BPFu6.pdf>
|
||||
7. Solana: <https://solana.com/>
|
||||
8. eBPF for Windows (Work-In-Progress): <https://github.com/microsoft/ebpf-for-windows>
|
||||
9. Femto-Containers: Lightweight Virtualization and Fault Isolation For Small Software Functions on Low-Power IoT Microcontrollers: <https://arxiv.org/abs/2210.03432>
|
||||
@@ -10,7 +10,9 @@ uprobe是一种用户空间探针,uprobe探针允许在用户空间程序中
|
||||
|
||||
uprobe基于文件,当一个二进制文件中的一个函数被跟踪时,所有使用到这个文件的进程都会被插桩,包括那些尚未启动的进程,这样就可以在全系统范围内跟踪系统调用。
|
||||
|
||||
uprobe适用于在用户态去解析一些内核态探针无法解析的流量,例如http2流量(报文header被编码,内核无法解码),https流量(加密流量,内核无法解密)。
|
||||
uprobe适用于在用户态去解析一些内核态探针无法解析的流量,例如http2流量(报文header被编码,内核无法解码),https流量(加密流量,内核无法解密)。具体可以参考 [eBPF 实践教程:使用 uprobe 捕获多种库的 SSL/TLS 明文数据](../30-sslsniff) 中的例子。
|
||||
|
||||
Uprobe 在内核态 eBPF 运行时,也可能产生比较大的性能开销,这时候也可以考虑使用用户态 eBPF 运行时,例如 [bpftime](https://github.com/eunomia-bpf/bpftime)。bpftime 是一个基于 LLVM JIT/AOT 的用户态 eBPF 运行时,它可以在用户态运行 eBPF 程序,和内核态的 eBPF 兼容,避免了内核态和用户态之间的上下文切换,从而提高了 eBPF 程序的执行效率。对于 uprobe 而言,bpftime 的性能开销比 kernel 小一个数量级。
|
||||
|
||||
## 使用 uprobe 捕获 bash 的 readline 函数调用
|
||||
|
||||
|
||||
@@ -10,7 +10,9 @@ uprobe is a user-space probe that allows dynamic instrumentation in user-space p
|
||||
|
||||
uprobe is file-based. When a function in a binary file is traced, all processes that use the file are instrumented, including those that have not yet been started, allowing system calls to be tracked system-wide.
|
||||
|
||||
uprobe is suitable for parsing some traffic in user mode that cannot be resolved by kernel mode probes, such as HTTP/2 traffic (where the header is encoded and cannot be decoded by the kernel) and HTTPS traffic (which is encrypted and cannot be decrypted by the kernel).
|
||||
uprobe is suitable for parsing some traffic in user mode that cannot be resolved by kernel mode probes, such as HTTP/2 traffic (where the header is encoded and cannot be decoded by the kernel) and HTTPS traffic (which is encrypted and cannot be decrypted by the kernel). For more information, see the example in [eBPF Tutorial by Example: Capturing SSL/TLS Plaintext Data from Multiple Libraries with Uprobe](../30-sslsniff).
|
||||
|
||||
Uprobe in kernel mode eBPF runtime may also cause relatively large performance overhead. In this case, you can also consider using user mode eBPF runtime, such as [bpftime](https://github.com/eunomia-bpf/bpftime). bpftime is a user mode eBPF runtime based on LLVM JIT/AOT. It can run eBPF programs in user mode and is compatible with kernel mode eBPF, avoiding context switching between kernel mode and user mode, thereby improving the execution efficiency of eBPF programs.
|
||||
|
||||
## Capturing readline Function Calls in bash using uprobe
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
|
||||
- [在 Android 上使用 eBPF 程序](22-android/README.md)
|
||||
- [使用 eBPF socket filter 或 syscall tracepoint 追踪 HTTP 请求等七层协议](23-http/README.md)
|
||||
- [使用 uprobe 捕获多种库的 SSL/TLS 明文数据](30-sslsniff/README.md)
|
||||
- [使用 Uprobe+eBPF 捕获多种库的 SSL/TLS 明文数据](30-sslsniff/README.md)
|
||||
- [使用 sockops 加速网络请求转发](29-sockops/README.md)
|
||||
- [eBPF 运行时的安全性与面临的挑战](18-further-reading/ebpf-security.zh.md)
|
||||
- [使用 eBPF 隐藏进程或文件信息](24-hide/README.md)
|
||||
@@ -41,7 +41,7 @@
|
||||
- [使用 eBPF 添加 sudo 用户](26-sudo/README.md)
|
||||
- [使用 eBPF 替换任意程序读取或写入的文本](27-replace/README.md)
|
||||
- [BPF的生命周期:使用 Detached 模式在用户态应用退出后持续运行 eBPF 程序](28-detach/README.md)
|
||||
- [使用 eBPF 用户态捕获多种库的 SSL/TLS 明文数据](30-sslsniff/README.md)
|
||||
- [用户空间 eBPF 运行时:深度解析与应用实践](src\36-userspace-ebpf\README.md)
|
||||
|
||||
# bcc 教程与文档
|
||||
|
||||
|
||||
@@ -43,7 +43,8 @@ For the complete source code of the tutorial, please refer to the repo [https://
|
||||
- [Adding sudo users using eBPF](26-sudo/README.md)
|
||||
- [Replacing text read or written by any program using eBPF](27-replace/README.md)
|
||||
- [BPF lifecycle: Running eBPF programs continuously after the user space application exits using Detached mode](28-detach/README.md)
|
||||
|
||||
- [Userspace eBPF Runtimes: Overview and Applications](src\36-userspace-ebpf\README_en.md)
|
||||
|
||||
# bcc tutorial
|
||||
|
||||
- [BPF Features by Linux Kernel Version](bcc-documents/kernel-versions.md)
|
||||
|
||||
Reference in New Issue
Block a user