github.com/cilium/ebpf@v0.10.0/ARCHITECTURE.md (about)

     1  Architecture of the library
     2  ===
     3  
     4      ELF -> Specifications -> Objects -> Links
     5  
     6  ELF
     7  ---
     8  
     9  BPF is usually produced by using Clang to compile a subset of C. Clang outputs
    10  an ELF file which contains program byte code (aka BPF), but also metadata for
    11  maps used by the program. The metadata follows the conventions set by libbpf
    12  shipped with the kernel. Certain ELF sections have special meaning
    13  and contain structures defined by libbpf. Newer versions of clang emit
    14  additional metadata in BPF Type Format (aka BTF).
    15  
    16  The library aims to be compatible with libbpf so that moving from a C toolchain
    17  to a Go one creates little friction. To that end, the [ELF reader](elf_reader.go)
    18  is tested against the Linux selftests and avoids introducing custom behaviour
    19  if possible.
    20  
    21  The output of the ELF reader is a `CollectionSpec` which encodes
    22  all of the information contained in the ELF in a form that is easy to work with
    23  in Go.
    24  
    25  ### BTF
    26  
    27  The BPF Type Format describes more than just the types used by a BPF program. It
    28  includes debug aids like which source line corresponds to which instructions and
    29  what global variables are used.
    30  
    31  [BTF parsing](internal/btf/) lives in a separate internal package since exposing
    32  it would mean an additional maintenance burden, and because the API still
    33  has sharp corners. The most important concept is the `btf.Type` interface, which
    34  also describes things that aren't really types like `.rodata` or `.bss` sections.
    35  `btf.Type`s can form cyclical graphs, which can easily lead to infinite loops if
    36  one is not careful. Hopefully a safe pattern to work with `btf.Type` emerges as
    37  we write more code that deals with it.
    38  
    39  Specifications
    40  ---
    41  
    42  `CollectionSpec`, `ProgramSpec` and `MapSpec` are blueprints for in-kernel
    43  objects and contain everything necessary to execute the relevant `bpf(2)`
    44  syscalls. Since the ELF reader outputs a `CollectionSpec` it's possible to
    45  modify clang-compiled BPF code, for example to rewrite constants. At the same
    46  time the [asm](asm/) package provides an assembler that can be used to generate
    47  `ProgramSpec` on the fly.
    48  
    49  Creating a spec should never require any privileges or be restricted in any way,
    50  for example by only allowing programs in native endianness. This ensures that
    51  the library stays flexible.
    52  
    53  Objects
    54  ---
    55  
    56  `Program` and `Map` are the result of loading specs into the kernel. Sometimes
    57  loading a spec will fail because the kernel is too old, or a feature is not
    58  enabled. There are multiple ways the library deals with that:
    59  
    60  * Fallback: older kernels don't allow naming programs and maps. The library
    61    automatically detects support for names, and omits them during load if
    62    necessary. This works since name is primarily a debug aid.
    63  
    64  * Sentinel error: sometimes it's possible to detect that a feature isn't available.
    65    In that case the library will return an error wrapping `ErrNotSupported`.
    66    This is also useful to skip tests that can't run on the current kernel.
    67  
    68  Once program and map objects are loaded they expose the kernel's low-level API,
    69  e.g. `NextKey`. Often this API is awkward to use in Go, so there are safer
    70  wrappers on top of the low-level API, like `MapIterator`. The low-level API is
    71  useful when our higher-level API doesn't support a particular use case.
    72  
    73  Links
    74  ---
    75  
    76  BPF can be attached to many different points in the kernel and newer BPF hooks
    77  tend to use bpf_link to do so. Older hooks unfortunately use a combination of
    78  syscalls, netlink messages, etc. Adding support for a new link type should not
    79  pull in large dependencies like netlink, so XDP programs or tracepoints are
    80  out of scope.
    81  
    82  Each bpf_link_type has one corresponding Go type, e.g. `link.tracing` corresponds
    83  to BPF_LINK_TRACING. In general, these types should be unexported as long as they
    84  don't export methods outside of the Link interface. Each Go type may have multiple
    85  exported constructors. For example `AttachTracing` and `AttachLSM` create a
    86  tracing link, but are distinct functions since they may require different arguments.