# cilium/ebpf architecture

> Architecture diagram of the GitHub repository cilium/ebpf, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/cilium/ebpf
- Repository: https://github.com/cilium/ebpf

## Overview

##### Main Components
- Core API packages:
- asm: Provides an assembler for writing eBPF instructions in Go.
- btf: Implements support for the BPF Type Format for introspecting kernel structures.
- link: Offers functionality to attach eBPF programs to various kernel hooks.
- perf and ringbuf: Provide mechanisms to consume events (via PERF_EVENT_ARRAY or BPF_MAP_TYPE_RINGBUF).
- features: Offers a means to probe and detect BPF-related kernel features.
- rlimit: Contains utilities to modify resource limits (e.g., lifting the RLIMIT_MEMLOCK constraint).
- pin: Provides APIs to work with objects pinned on bpffs.
- Supporting and Utility Components:
- cmd/bpf2go: A command-line tool to compile eBPF programs written in C and embed them into Go.
- internal packages: These include packages such as internal/sys, internal/platform, and others that deal with low-level OS interactions, syscall wrappers, error handling, and platform abstraction.
- linker and map packages: Modules like “link” and “map” that deal with program/linking logic, memory management, and data marshalling.

##### Relationships and Interactions
- At the “user level”, consumers of the library call into the exposed API packages (asm, btf, link, perf, ringbuf, features, rlimit, pin) to create, load, and manage eBPF programs.
- The cmd/bpf2go tool steps in as a higher-level utility that not only compiles C-based eBPF code but also auto-generates Go bindings so that the rest of the system (or an end application) can easily load and interact with the eBPF program.
- Each API package internally communicates with the “internal” layer (which wraps syscall interactions, error handling, and platform-specific logic). For example:
- The asm package produces low-level instructions which are then consumed by the loader functions that interact with the kernel.
- The rlimit package interacts with OS resource configurations prior to eBPF program load.
- Finally, the kernel itself is an external dependency that, through syscalls and direct interactions, is “talked to” by various lower-level modules such as internal/sys, internal/linux, or internal/platform.

##### Important Architectural Patterns/Design Principles
- Layered Architecture: There is a clear separation between high-level API packages (that the end user consumes) and the low-level internal packages that abstract platform and OS-specific calls.
- Modularity: Each major feature is encapsulated in its own package (e.g., asm, link, perf, btf), allowing for coherent functionality and easier extension.
- Command Line Utility Integration: The “cmd/bpf2go” module shows how CLI tools can be integrated into the system to generate and manage eBPF programs via auto-generated Go code.
- Cross-Platform Design: The presence of files like "cpu_windows.go", "internal/platform/platform_windows.go", and various _test.go files indicate that multi-platform support is taken into account.

##### Relevant Technologies/Frameworks and Their Roles
- Go as the primary programming platform.
- eBPF itself (as an underlying kernel technology).
- System calls and kernel interfaces (abstracted through internal/sys for example).
- Testing frameworks accompany the code in nearly every package (as seen by numerous *_test.go files).

##### Instructing the Principal Software Engineer
- Clear nodes/components with labels such as “asm Package”, “btf Package”, “link Package”, “perf Package”, “ringbuf Package”, “features Package”, “rlimit Package”, “pin Package”, and “cmd/bpf2go (CLI Tool)”.
- A distinct node for our “internal” or “platform abstraction layer” where low-level syscall and OS-specific functions are handled.
- An external “Kernel/eBPF subsystem” node that represents how the final eBPF programs interact with the operating system.
- API calls from the externally exposed packages flowing into the internal layer.
- Data flows from the cmd/bpf2go tool to the embedding/use in Go applications.
- Dependencies such as “asm” generating instructions that are used by loader functions.
- From the “rlimit” package directly interacting with OS resource management.
- From the “link” package initiating calls to attach to hook points.
- From the “perf” and “ringbuf” packages collecting kernel event data.

##### Summary
- At the top, the public/library API packages.
- In the middle, the cmd/bpf2go tool and internal abstraction layers.
- At the bottom, the kernel and OS interfaces.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Public API Packages"
        asm["asm Package"]:::api
        btf["btf Package"]:::api
        link["link Package"]:::api
        perf["perf Package"]:::api
        ringbuf["ringbuf Package"]:::api
        features["features Package"]:::api
        rlimit["rlimit Package"]:::api
        pin["pin Package"]:::api
    end

    subgraph "Utility Tools"
        cmd["cmd/bpf2go (CLI Tool)"]:::utility
        linkerMap["Linker/Map Logic"]:::utility
    end

    subgraph "Internal/Platform Abstraction Layer"
        internal["Internal OS/Platform Abstraction"]:::internal
    end

    kernel["Kernel/eBPF Subsystem"]:::external

    %% Connections from Utility Tools to Public API Packages
    cmd -->|"auto-gen-bindings"| asm
    linkerMap -->|"provide-linking"| link

    %% Connections from Public API Packages to Internal Layer
    asm -->|"produces-instructions"| internal
    btf -->|"introspects"| internal
    link -->|"attaches-programs"| internal
    perf -->|"consumes-events"| internal
    ringbuf -->|"collects-events"| internal
    features -->|"detects-features"| internal
    rlimit -->|"modifies-resources"| internal
    pin -->|"manages-pinned-objects"| internal

    %% Connection from Internal Layer to Kernel/eBPF Subsystem
    internal -->|"syscall-interface"| kernel

    %% Styles
    classDef api fill:#ADD8E6,stroke:#00008B,stroke-width:2px;
    classDef utility fill:#90EE90,stroke:#006400,stroke-width:2px;
    classDef internal fill:#D3D3D3,stroke:#696969,stroke-width:2px;
    classDef external fill:#FFD700,stroke:#B8860B,stroke-width:2px;

    %% Click Events
    click asm "https://github.com/cilium/ebpf/tree/main/asm/"
    click btf "https://github.com/cilium/ebpf/tree/main/btf/"
    click link "https://github.com/cilium/ebpf/tree/main/link/"
    click perf "https://github.com/cilium/ebpf/tree/main/perf/"
    click ringbuf "https://github.com/cilium/ebpf/tree/main/ringbuf/"
    click features "https://github.com/cilium/ebpf/tree/main/features/"
    click rlimit "https://github.com/cilium/ebpf/tree/main/rlimit/"
    click pin "https://github.com/cilium/ebpf/tree/main/pin/"
    click cmd "https://github.com/cilium/ebpf/tree/main/cmd/bpf2go/"
    click linkerMap "https://github.com/cilium/ebpf/blob/main/linker.go"
    click internal "https://github.com/cilium/ebpf/tree/main/internal/"
```
