How cilium/ebpf works
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.