bpftime Documentation

repository·master·Indexed 23 days ago

https://github.com/eunomia-bpf/bpftime

A high-performance userspace eBPF runtime and extension framework that allows developers to run eBPF programs (Uprobes, XDP, Syscalls, etc.) in userspace. It features a modular Attach System supporting Frida-based uprobes, syscall tracing, and CUDA kernel instrumentation for GPU eBPF execution on Linux.

Tokens
68K
Snippets
191
Records
349
Agent score
80%

What's inside bpftime

  1. What is bpftime?

    master

    bpftime is a high-performance userspace eBPF runtime and general extension framework. Unlike a simple VM, it is a complete runtime environment including a loader, verifier, helpers, maps, ufunc (user functions), and support for multiple event sources like Observability, Network, Policy, or Access Control.

    It is designed to bypass the kernel to achieve faster performance for Uprobes, USDT, Syscall hooks, and XDP, while maintaining compatibility with existing eBPF toolchains like clang, libbpf, and bpftrace.

  2. Overview of bpftime benchmark experiments

    master

    The benchmark directory contains various experiments designed to evaluate bpftime across different use cases:

    • Micro-benchmarks: Measures performance overhead and latency for Uprobe/uretprobe, memory operations (read/write), map operations, syscall tracepoints, and MPK enable/disable. Compares bpftime against traditional kernel eBPF.
    • SSL/TLS Traffic Inspection (sslsniff): Demonstrates real-time interception of SSL/TLS traffic by hooking OpenSSL functions in Nginx.
    • System Call Counting (syscount): Evaluates the ability to trace and count system calls (e.g., in Nginx) compared to kernel-based tracing.
    • Nginx Plugin/Module: Showcases integration of bpftime as an Nginx plugin or module.
    • DeepFlow Integration: Measures performance impact when integrated with the DeepFlow observability platform.
    • FUSE (Filesystem in Userspace): Evaluates performance when instrumenting FUSE-based filesystems for syscall result caching.
    • Redis Durability Tuning: Demonstrates dynamic runtime tuning of Redis durability settings.
    • Compatibility: Validates that existing eBPF programs run on bpftime without modification.
  3. Run eBPF on GPU with bpftime

    master

    bpftime enables eBPF programs to execute within GPU kernels on both NVIDIA (via CUDA) and AMD (via ROCm) GPUs. This allows for real-time profiling, debugging, and runtime extension of GPU computing workloads without requiring modifications to the original application source code.

    Note: GPU support is currently considered experimental.

  4. Overview of Redis Durability Tuning Implementations

    master

    This project explores several BPFtime-based extensions to improve Redis durability (reducing the data loss gap between everysec and alwayson) while minimizing the performance penalty.

    Available Implementations

    ImplementationDirectoryDescription
    Delayed fsyncdelayed-fsync/Extends fdatasync to wait for the previous call, ensuring at most 2 updates are lost.
    Fast-path optimizationfsync-fast-notify/Uses a shared variable in the kernel to track completed fdatasync operations; only executes syscalls if the previous one hasn't completed.
    BPF Sync Kernelbpf-sync-kernel/A kernel-level approach for optimizing sync operations.
    Batch Processbatch_process/A batch processing implementation.
    IO_uringpoc-iouring-minimal/A minimal proof-of-concept using io_uring for batched I/O.

    Each directory contains the C source files for the BPF program, a Makefile, and a specific README.

  5. Overview of ubpf-vm wrapper

    master

    The ubpf-vm is a wrapper around ubpf designed to provide a unified interface for bpftime. It includes specific adaptations to bridge feature gaps between ubpf and the requirements of the bpftime runtime, specifically:

    • Helper ID Remapping: It remaps helper IDs to a range of 0-63, as ubpf only supports up to 64 helpers.
    • Instruction Patching: It patches lddw (load double word) instructions during the code loading process to ensure compatibility.
  6. Understand GPU Map Consistency and Implementation

    master

    The example uses BPF_MAP_TYPE_GPU_ARRAY_MAP and BPF_MAP_TYPE_GPU_HASH_MAP. These are non-per-thread, single-copy shared maps using UVA Zero-Copy (Unified Virtual Addressing), allowing the GPU and host to see the same memory.

    Write Semantics (Device Side)

    • Mechanism: Uses a memcpy overwrite via the trampoline fast-path. This is non-atomic; the last writer wins.
    • Visibility: A system-level memory fence is executed after the write to ensure the host sees the update.
    • Optimization: This method avoids the CPU handshake path, reducing latency.

    Read Semantics (Host Side)

    • Mechanism: Reads are lock-free.
    • Consistency: You may read a slightly older value, but you will not read a partially written value. For stronger consistency, implement locks or double-read version checks on the host side.
  7. How bpftime execution modes work

    master

    bpftime operates in two primary modes:

    1. Userspace Only: Runs eBPF programs entirely in userspace without kernel involvement. This allows running on older Linux versions or systems where kernel eBPF is unavailable or restricted (no root required). It uses a userspace verifier (like PREVAIL) to ensure safety.
    2. Run with Kernel eBPF: Allows bpftime to coexist with the kernel. It can load eBPF programs from the kernel and use kernel eBPF maps to cooperate with kernel-level programs such as kprobes and network filters.
  8. Understand BPFtime Embedded VM performance

    master

    BPFtime includes an embedded eBPF VM designed for low-latency execution. Benchmark results show the embed operation has an average latency of approximately 106.30 ns.

    ### Embedded VM Performance
    
    | Operation | Min (ns) | Max (ns) | Avg (ns) | Std Dev |
    |-----------|----------|----------|----------|---------|
    | embed | 75.47 | 221.55 | 106.30 | 39.99 |
  9. How the LPM Trie Demo works

    master

    The demo implements real-time file access interception using the following workflow:

    1. Monitor (Server): Loads the BPF program and initializes the BPF_MAP_TYPE_LPM_TRIE map with a set of allowed prefixes.
    2. Target (Client): Executes file operations that trigger uprobe events.
    3. LPM Trie: The BPF program uses the LPM Trie to automatically find the longest matching prefix for the requested file path to determine access control.
    4. Event Queue: Processes the resulting file access events in real-time.

    Architecture Flow:

    • Monitor (Server) $\rightarrow$ bpftime Server $\rightarrow$ BPF Maps (LPM Trie, Queue, Counter)
    • Target (Client) $\rightarrow$ bpftime Agent $\rightarrow$ Shared Memory
    • BPF Program $\rightarrow$ LPM Trie $\rightarrow$ Access Control
  10. How CUDA eBPF attachment works

    master

    The bpftime-nv-attach module allows eBPF programs to be injected into and executed within CUDA kernels via several mechanisms:

    1. CUDA API Interception: Uses Frida-gum to hook functions like __cudaRegisterFatBinary, __cudaRegisterFunction, __cudaRegisterFatBinaryEnd, and cudaLaunchKernel to intercept binary data before it reaches the driver.
    2. PTX Code Transformation: Extracts PTX from fat binaries, patches it with instrumentation code, converts eBPF to PTX via LLVM, and recompiles it using NVCC.
    3. Register Protection: Implements mechanisms to save/restore registers to preserve original kernel behavior.
    4. Memory and Data Sharing: Sets up shared memory and communication channels between host and device for eBPF maps.

    Supported Attachment Types

    • Memory Capture: Intercepts memory operations (load/store).
    • Function Probes: Executes at the beginning of CUDA kernel functions.
    • Return Probes: Executes before kernel functions return.
  11. Benchmark BPF map operations

    master

    bpftime userspace map operations are designed to be faster than kernel operations. The benchmark tests various map types including:

    • Hash maps (bpf_map_lookup_elem, bpf_map_update_elem, bpf_map_delete_elem)
    • Array maps
    • Per-CPU maps (Hash and Array)

    Performance can be improved by inlining the map operation functions. Note that current versions may still have performance issues with per-CPU maps in the userspace runtime that are subject to future fixes.

  12. Key features of bpftime

    master

    bpftime provides several advanced capabilities for eBPF users:

    • Dynamic Binary Rewriting: Attach eBPF programs to Uprobes, Syscall tracepoints, and GPU kernels without manual instrumentation or process restarts. It can trace, change, hook, or filter execution safely.
    • High Performance: Offers up to 10x speedup in Uprobe overhead compared to kernel uprobes/uretprobes and is up to 10x faster than NVbit.
    • Interprocess eBPF Maps: Implements maps in shared userspace memory for aggregation or control plane communication.
    • Toolchain Compatibility: Works with clang, libbpf, and bpftrace. Supports CO-RE via BTF and provides userspace ufunc access.
    • Multi JIT Support: Supports llvmbpf (a high-speed LLVM-powered JIT/AOT compiler) and ubpf JIT or an interpreter.
    • Kernel Cooperation: Can load userspace eBPF from the kernel and use kernel eBPF maps to cooperate with kernel-level programs like kprobes.