xref: /aosp_15_r20/external/libbpf/docs/libbpf_overview.rst (revision f7c14bbac8cf49633f2740db462ea43457973ec4)
1*f7c14bbaSAndroid Build Coastguard Worker.. SPDX-License-Identifier: GPL-2.0
2*f7c14bbaSAndroid Build Coastguard Worker
3*f7c14bbaSAndroid Build Coastguard Worker===============
4*f7c14bbaSAndroid Build Coastguard Workerlibbpf Overview
5*f7c14bbaSAndroid Build Coastguard Worker===============
6*f7c14bbaSAndroid Build Coastguard Worker
7*f7c14bbaSAndroid Build Coastguard Workerlibbpf is a C-based library containing a BPF loader that takes compiled BPF
8*f7c14bbaSAndroid Build Coastguard Workerobject files and prepares and loads them into the Linux kernel. libbpf takes the
9*f7c14bbaSAndroid Build Coastguard Workerheavy lifting of loading, verifying, and attaching BPF programs to various
10*f7c14bbaSAndroid Build Coastguard Workerkernel hooks, allowing BPF application developers to focus only on BPF program
11*f7c14bbaSAndroid Build Coastguard Workercorrectness and performance.
12*f7c14bbaSAndroid Build Coastguard Worker
13*f7c14bbaSAndroid Build Coastguard WorkerThe following are the high-level features supported by libbpf:
14*f7c14bbaSAndroid Build Coastguard Worker
15*f7c14bbaSAndroid Build Coastguard Worker* Provides high-level and low-level APIs for user space programs to interact
16*f7c14bbaSAndroid Build Coastguard Worker  with BPF programs. The low-level APIs wrap all the bpf system call
17*f7c14bbaSAndroid Build Coastguard Worker  functionality, which is useful when users need more fine-grained control
18*f7c14bbaSAndroid Build Coastguard Worker  over the interactions between user space and BPF programs.
19*f7c14bbaSAndroid Build Coastguard Worker* Provides overall support for the BPF object skeleton generated by bpftool.
20*f7c14bbaSAndroid Build Coastguard Worker  The skeleton file simplifies the process for the user space programs to access
21*f7c14bbaSAndroid Build Coastguard Worker  global variables and work with BPF programs.
22*f7c14bbaSAndroid Build Coastguard Worker* Provides BPF-side APIS, including BPF helper definitions, BPF maps support,
23*f7c14bbaSAndroid Build Coastguard Worker  and tracing helpers, allowing developers to simplify BPF code writing.
24*f7c14bbaSAndroid Build Coastguard Worker* Supports BPF CO-RE mechanism, enabling BPF developers to write portable
25*f7c14bbaSAndroid Build Coastguard Worker  BPF programs that can be compiled once and run across different kernel
26*f7c14bbaSAndroid Build Coastguard Worker  versions.
27*f7c14bbaSAndroid Build Coastguard Worker
28*f7c14bbaSAndroid Build Coastguard WorkerThis document will delve into the above concepts in detail, providing a deeper
29*f7c14bbaSAndroid Build Coastguard Workerunderstanding of the capabilities and advantages of libbpf and how it can help
30*f7c14bbaSAndroid Build Coastguard Workeryou develop BPF applications efficiently.
31*f7c14bbaSAndroid Build Coastguard Worker
32*f7c14bbaSAndroid Build Coastguard WorkerBPF App Lifecycle and libbpf APIs
33*f7c14bbaSAndroid Build Coastguard Worker==================================
34*f7c14bbaSAndroid Build Coastguard Worker
35*f7c14bbaSAndroid Build Coastguard WorkerA BPF application consists of one or more BPF programs (either cooperating or
36*f7c14bbaSAndroid Build Coastguard Workercompletely independent), BPF maps, and global variables. The global
37*f7c14bbaSAndroid Build Coastguard Workervariables are shared between all BPF programs, which allows them to cooperate on
38*f7c14bbaSAndroid Build Coastguard Workera common set of data. libbpf provides APIs that user space programs can use to
39*f7c14bbaSAndroid Build Coastguard Workermanipulate the BPF programs by triggering different phases of a BPF application
40*f7c14bbaSAndroid Build Coastguard Workerlifecycle.
41*f7c14bbaSAndroid Build Coastguard Worker
42*f7c14bbaSAndroid Build Coastguard WorkerThe following section provides a brief overview of each phase in the BPF life
43*f7c14bbaSAndroid Build Coastguard Workercycle:
44*f7c14bbaSAndroid Build Coastguard Worker
45*f7c14bbaSAndroid Build Coastguard Worker* **Open phase**: In this phase, libbpf parses the BPF
46*f7c14bbaSAndroid Build Coastguard Worker  object file and discovers BPF maps, BPF programs, and global variables. After
47*f7c14bbaSAndroid Build Coastguard Worker  a BPF app is opened, user space apps can make additional adjustments
48*f7c14bbaSAndroid Build Coastguard Worker  (setting BPF program types, if necessary; pre-setting initial values for
49*f7c14bbaSAndroid Build Coastguard Worker  global variables, etc.) before all the entities are created and loaded.
50*f7c14bbaSAndroid Build Coastguard Worker
51*f7c14bbaSAndroid Build Coastguard Worker* **Load phase**: In the load phase, libbpf creates BPF
52*f7c14bbaSAndroid Build Coastguard Worker  maps, resolves various relocations, and verifies and loads BPF programs into
53*f7c14bbaSAndroid Build Coastguard Worker  the kernel. At this point, libbpf validates all the parts of a BPF application
54*f7c14bbaSAndroid Build Coastguard Worker  and loads the BPF program into the kernel, but no BPF program has yet been
55*f7c14bbaSAndroid Build Coastguard Worker  executed. After the load phase, it’s possible to set up the initial BPF map
56*f7c14bbaSAndroid Build Coastguard Worker  state without racing with the BPF program code execution.
57*f7c14bbaSAndroid Build Coastguard Worker
58*f7c14bbaSAndroid Build Coastguard Worker* **Attachment phase**: In this phase, libbpf
59*f7c14bbaSAndroid Build Coastguard Worker  attaches BPF programs to various BPF hook points (e.g., tracepoints, kprobes,
60*f7c14bbaSAndroid Build Coastguard Worker  cgroup hooks, network packet processing pipeline, etc.). During this
61*f7c14bbaSAndroid Build Coastguard Worker  phase, BPF programs perform useful work such as processing
62*f7c14bbaSAndroid Build Coastguard Worker  packets, or updating BPF maps and global variables that can be read from user
63*f7c14bbaSAndroid Build Coastguard Worker  space.
64*f7c14bbaSAndroid Build Coastguard Worker
65*f7c14bbaSAndroid Build Coastguard Worker* **Tear down phase**: In the tear down phase,
66*f7c14bbaSAndroid Build Coastguard Worker  libbpf detaches BPF programs and unloads them from the kernel. BPF maps are
67*f7c14bbaSAndroid Build Coastguard Worker  destroyed, and all the resources used by the BPF app are freed.
68*f7c14bbaSAndroid Build Coastguard Worker
69*f7c14bbaSAndroid Build Coastguard WorkerBPF Object Skeleton File
70*f7c14bbaSAndroid Build Coastguard Worker========================
71*f7c14bbaSAndroid Build Coastguard Worker
72*f7c14bbaSAndroid Build Coastguard WorkerBPF skeleton is an alternative interface to libbpf APIs for working with BPF
73*f7c14bbaSAndroid Build Coastguard Workerobjects. Skeleton code abstract away generic libbpf APIs to significantly
74*f7c14bbaSAndroid Build Coastguard Workersimplify code for manipulating BPF programs from user space. Skeleton code
75*f7c14bbaSAndroid Build Coastguard Workerincludes a bytecode representation of the BPF object file, simplifying the
76*f7c14bbaSAndroid Build Coastguard Workerprocess of distributing your BPF code. With BPF bytecode embedded, there are no
77*f7c14bbaSAndroid Build Coastguard Workerextra files to deploy along with your application binary.
78*f7c14bbaSAndroid Build Coastguard Worker
79*f7c14bbaSAndroid Build Coastguard WorkerYou can generate the skeleton header file ``(.skel.h)`` for a specific object
80*f7c14bbaSAndroid Build Coastguard Workerfile by passing the BPF object to the bpftool. The generated BPF skeleton
81*f7c14bbaSAndroid Build Coastguard Workerprovides the following custom functions that correspond to the BPF lifecycle,
82*f7c14bbaSAndroid Build Coastguard Workereach of them prefixed with the specific object name:
83*f7c14bbaSAndroid Build Coastguard Worker
84*f7c14bbaSAndroid Build Coastguard Worker* ``<name>__open()`` – creates and opens BPF application (``<name>`` stands for
85*f7c14bbaSAndroid Build Coastguard Worker  the specific bpf object name)
86*f7c14bbaSAndroid Build Coastguard Worker* ``<name>__load()`` – instantiates, loads,and verifies BPF application parts
87*f7c14bbaSAndroid Build Coastguard Worker* ``<name>__attach()`` – attaches all auto-attachable BPF programs (it’s
88*f7c14bbaSAndroid Build Coastguard Worker  optional, you can have more control by using libbpf APIs directly)
89*f7c14bbaSAndroid Build Coastguard Worker* ``<name>__destroy()`` – detaches all BPF programs and
90*f7c14bbaSAndroid Build Coastguard Worker  frees up all used resources
91*f7c14bbaSAndroid Build Coastguard Worker
92*f7c14bbaSAndroid Build Coastguard WorkerUsing the skeleton code is the recommended way to work with bpf programs. Keep
93*f7c14bbaSAndroid Build Coastguard Workerin mind, BPF skeleton provides access to the underlying BPF object, so whatever
94*f7c14bbaSAndroid Build Coastguard Workerwas possible to do with generic libbpf APIs is still possible even when the BPF
95*f7c14bbaSAndroid Build Coastguard Workerskeleton is used. It's an additive convenience feature, with no syscalls, and no
96*f7c14bbaSAndroid Build Coastguard Workercumbersome code.
97*f7c14bbaSAndroid Build Coastguard Worker
98*f7c14bbaSAndroid Build Coastguard WorkerOther Advantages of Using Skeleton File
99*f7c14bbaSAndroid Build Coastguard Worker---------------------------------------
100*f7c14bbaSAndroid Build Coastguard Worker
101*f7c14bbaSAndroid Build Coastguard Worker* BPF skeleton provides an interface for user space programs to work with BPF
102*f7c14bbaSAndroid Build Coastguard Worker  global variables. The skeleton code memory maps global variables as a struct
103*f7c14bbaSAndroid Build Coastguard Worker  into user space. The struct interface allows user space programs to initialize
104*f7c14bbaSAndroid Build Coastguard Worker  BPF programs before the BPF load phase and fetch and update data from user
105*f7c14bbaSAndroid Build Coastguard Worker  space afterward.
106*f7c14bbaSAndroid Build Coastguard Worker
107*f7c14bbaSAndroid Build Coastguard Worker* The ``skel.h`` file reflects the object file structure by listing out the
108*f7c14bbaSAndroid Build Coastguard Worker  available maps, programs, etc. BPF skeleton provides direct access to all the
109*f7c14bbaSAndroid Build Coastguard Worker  BPF maps and BPF programs as struct fields. This eliminates the need for
110*f7c14bbaSAndroid Build Coastguard Worker  string-based lookups with ``bpf_object_find_map_by_name()`` and
111*f7c14bbaSAndroid Build Coastguard Worker  ``bpf_object_find_program_by_name()`` APIs, reducing errors due to BPF source
112*f7c14bbaSAndroid Build Coastguard Worker  code and user-space code getting out of sync.
113*f7c14bbaSAndroid Build Coastguard Worker
114*f7c14bbaSAndroid Build Coastguard Worker* The embedded bytecode representation of the object file ensures that the
115*f7c14bbaSAndroid Build Coastguard Worker  skeleton and the BPF object file are always in sync.
116*f7c14bbaSAndroid Build Coastguard Worker
117*f7c14bbaSAndroid Build Coastguard WorkerBPF Helpers
118*f7c14bbaSAndroid Build Coastguard Worker===========
119*f7c14bbaSAndroid Build Coastguard Worker
120*f7c14bbaSAndroid Build Coastguard Workerlibbpf provides BPF-side APIs that BPF programs can use to interact with the
121*f7c14bbaSAndroid Build Coastguard Workersystem. The BPF helpers definition allows developers to use them in BPF code as
122*f7c14bbaSAndroid Build Coastguard Workerany other plain C function. For example, there are helper functions to print
123*f7c14bbaSAndroid Build Coastguard Workerdebugging messages, get the time since the system was booted, interact with BPF
124*f7c14bbaSAndroid Build Coastguard Workermaps, manipulate network packets, etc.
125*f7c14bbaSAndroid Build Coastguard Worker
126*f7c14bbaSAndroid Build Coastguard WorkerFor a complete description of what the helpers do, the arguments they take, and
127*f7c14bbaSAndroid Build Coastguard Workerthe return value, see the `bpf-helpers
128*f7c14bbaSAndroid Build Coastguard Worker<https://man7.org/linux/man-pages/man7/bpf-helpers.7.html>`_ man page.
129*f7c14bbaSAndroid Build Coastguard Worker
130*f7c14bbaSAndroid Build Coastguard WorkerBPF CO-RE (Compile Once – Run Everywhere)
131*f7c14bbaSAndroid Build Coastguard Worker=========================================
132*f7c14bbaSAndroid Build Coastguard Worker
133*f7c14bbaSAndroid Build Coastguard WorkerBPF programs work in the kernel space and have access to kernel memory and data
134*f7c14bbaSAndroid Build Coastguard Workerstructures. One limitation that BPF applications come across is the lack of
135*f7c14bbaSAndroid Build Coastguard Workerportability across different kernel versions and configurations. `BCC
136*f7c14bbaSAndroid Build Coastguard Worker<https://github.com/iovisor/bcc/>`_ is one of the solutions for BPF
137*f7c14bbaSAndroid Build Coastguard Workerportability. However, it comes with runtime overhead and a large binary size
138*f7c14bbaSAndroid Build Coastguard Workerfrom embedding the compiler with the application.
139*f7c14bbaSAndroid Build Coastguard Worker
140*f7c14bbaSAndroid Build Coastguard Workerlibbpf steps up the BPF program portability by supporting the BPF CO-RE concept.
141*f7c14bbaSAndroid Build Coastguard WorkerBPF CO-RE brings together BTF type information, libbpf, and the compiler to
142*f7c14bbaSAndroid Build Coastguard Workerproduce a single executable binary that you can run on multiple kernel versions
143*f7c14bbaSAndroid Build Coastguard Workerand configurations.
144*f7c14bbaSAndroid Build Coastguard Worker
145*f7c14bbaSAndroid Build Coastguard WorkerTo make BPF programs portable libbpf relies on the BTF type information of the
146*f7c14bbaSAndroid Build Coastguard Workerrunning kernel. Kernel also exposes this self-describing authoritative BTF
147*f7c14bbaSAndroid Build Coastguard Workerinformation through ``sysfs`` at ``/sys/kernel/btf/vmlinux``.
148*f7c14bbaSAndroid Build Coastguard Worker
149*f7c14bbaSAndroid Build Coastguard WorkerYou can generate the BTF information for the running kernel with the following
150*f7c14bbaSAndroid Build Coastguard Workercommand:
151*f7c14bbaSAndroid Build Coastguard Worker
152*f7c14bbaSAndroid Build Coastguard Worker::
153*f7c14bbaSAndroid Build Coastguard Worker
154*f7c14bbaSAndroid Build Coastguard Worker  $ bpftool btf dump file /sys/kernel/btf/vmlinux format c > vmlinux.h
155*f7c14bbaSAndroid Build Coastguard Worker
156*f7c14bbaSAndroid Build Coastguard WorkerThe command generates a ``vmlinux.h`` header file with all kernel types
157*f7c14bbaSAndroid Build Coastguard Worker(:doc:`BTF types <../btf>`) that the running kernel uses. Including
158*f7c14bbaSAndroid Build Coastguard Worker``vmlinux.h`` in your BPF program eliminates dependency on system-wide kernel
159*f7c14bbaSAndroid Build Coastguard Workerheaders.
160*f7c14bbaSAndroid Build Coastguard Worker
161*f7c14bbaSAndroid Build Coastguard Workerlibbpf enables portability of BPF programs by looking at the BPF program’s
162*f7c14bbaSAndroid Build Coastguard Workerrecorded BTF type and relocation information and matching them to BTF
163*f7c14bbaSAndroid Build Coastguard Workerinformation (vmlinux) provided by the running kernel. libbpf then resolves and
164*f7c14bbaSAndroid Build Coastguard Workermatches all the types and fields, and updates necessary offsets and other
165*f7c14bbaSAndroid Build Coastguard Workerrelocatable data to ensure that BPF program’s logic functions correctly for a
166*f7c14bbaSAndroid Build Coastguard Workerspecific kernel on the host. BPF CO-RE concept thus eliminates overhead
167*f7c14bbaSAndroid Build Coastguard Workerassociated with BPF development and allows developers to write portable BPF
168*f7c14bbaSAndroid Build Coastguard Workerapplications without modifications and runtime source code compilation on the
169*f7c14bbaSAndroid Build Coastguard Workertarget machine.
170*f7c14bbaSAndroid Build Coastguard Worker
171*f7c14bbaSAndroid Build Coastguard WorkerThe following code snippet shows how to read the parent field of a kernel
172*f7c14bbaSAndroid Build Coastguard Worker``task_struct`` using BPF CO-RE and libbf. The basic helper to read a field in a
173*f7c14bbaSAndroid Build Coastguard WorkerCO-RE relocatable manner is ``bpf_core_read(dst, sz, src)``, which will read
174*f7c14bbaSAndroid Build Coastguard Worker``sz`` bytes from the field referenced by ``src`` into the memory pointed to by
175*f7c14bbaSAndroid Build Coastguard Worker``dst``.
176*f7c14bbaSAndroid Build Coastguard Worker
177*f7c14bbaSAndroid Build Coastguard Worker.. code-block:: C
178*f7c14bbaSAndroid Build Coastguard Worker   :emphasize-lines: 6
179*f7c14bbaSAndroid Build Coastguard Worker
180*f7c14bbaSAndroid Build Coastguard Worker    //...
181*f7c14bbaSAndroid Build Coastguard Worker    struct task_struct *task = (void *)bpf_get_current_task();
182*f7c14bbaSAndroid Build Coastguard Worker    struct task_struct *parent_task;
183*f7c14bbaSAndroid Build Coastguard Worker    int err;
184*f7c14bbaSAndroid Build Coastguard Worker
185*f7c14bbaSAndroid Build Coastguard Worker    err = bpf_core_read(&parent_task, sizeof(void *), &task->parent);
186*f7c14bbaSAndroid Build Coastguard Worker    if (err) {
187*f7c14bbaSAndroid Build Coastguard Worker      /* handle error */
188*f7c14bbaSAndroid Build Coastguard Worker    }
189*f7c14bbaSAndroid Build Coastguard Worker
190*f7c14bbaSAndroid Build Coastguard Worker    /* parent_task contains the value of task->parent pointer */
191*f7c14bbaSAndroid Build Coastguard Worker
192*f7c14bbaSAndroid Build Coastguard WorkerIn the code snippet, we first get a pointer to the current ``task_struct`` using
193*f7c14bbaSAndroid Build Coastguard Worker``bpf_get_current_task()``.  We then use ``bpf_core_read()`` to read the parent
194*f7c14bbaSAndroid Build Coastguard Workerfield of task struct into the ``parent_task`` variable. ``bpf_core_read()`` is
195*f7c14bbaSAndroid Build Coastguard Workerjust like ``bpf_probe_read_kernel()`` BPF helper, except it records information
196*f7c14bbaSAndroid Build Coastguard Workerabout the field that should be relocated on the target kernel. i.e, if the
197*f7c14bbaSAndroid Build Coastguard Worker``parent`` field gets shifted to a different offset within
198*f7c14bbaSAndroid Build Coastguard Worker``struct task_struct`` due to some new field added in front of it, libbpf will
199*f7c14bbaSAndroid Build Coastguard Workerautomatically adjust the actual offset to the proper value.
200*f7c14bbaSAndroid Build Coastguard Worker
201*f7c14bbaSAndroid Build Coastguard WorkerGetting Started with libbpf
202*f7c14bbaSAndroid Build Coastguard Worker===========================
203*f7c14bbaSAndroid Build Coastguard Worker
204*f7c14bbaSAndroid Build Coastguard WorkerCheck out the `libbpf-bootstrap <https://github.com/libbpf/libbpf-bootstrap>`_
205*f7c14bbaSAndroid Build Coastguard Workerrepository with simple examples of using libbpf to build various BPF
206*f7c14bbaSAndroid Build Coastguard Workerapplications.
207*f7c14bbaSAndroid Build Coastguard Worker
208*f7c14bbaSAndroid Build Coastguard WorkerSee also `libbpf API documentation
209*f7c14bbaSAndroid Build Coastguard Worker<https://libbpf.readthedocs.io/en/latest/api.html>`_.
210*f7c14bbaSAndroid Build Coastguard Worker
211*f7c14bbaSAndroid Build Coastguard Workerlibbpf and Rust
212*f7c14bbaSAndroid Build Coastguard Worker===============
213*f7c14bbaSAndroid Build Coastguard Worker
214*f7c14bbaSAndroid Build Coastguard WorkerIf you are building BPF applications in Rust, it is recommended to use the
215*f7c14bbaSAndroid Build Coastguard Worker`Libbpf-rs <https://github.com/libbpf/libbpf-rs>`_ library instead of bindgen
216*f7c14bbaSAndroid Build Coastguard Workerbindings directly to libbpf. Libbpf-rs wraps libbpf functionality in
217*f7c14bbaSAndroid Build Coastguard WorkerRust-idiomatic interfaces and provides libbpf-cargo plugin to handle BPF code
218*f7c14bbaSAndroid Build Coastguard Workercompilation and skeleton generation. Using Libbpf-rs will make building user
219*f7c14bbaSAndroid Build Coastguard Workerspace part of the BPF application easier. Note that the BPF program themselves
220*f7c14bbaSAndroid Build Coastguard Workermust still be written in plain C.
221*f7c14bbaSAndroid Build Coastguard Worker
222*f7c14bbaSAndroid Build Coastguard WorkerAdditional Documentation
223*f7c14bbaSAndroid Build Coastguard Worker========================
224*f7c14bbaSAndroid Build Coastguard Worker
225*f7c14bbaSAndroid Build Coastguard Worker* `Program types and ELF Sections <https://libbpf.readthedocs.io/en/latest/program_types.html>`_
226*f7c14bbaSAndroid Build Coastguard Worker* `API naming convention <https://libbpf.readthedocs.io/en/latest/libbpf_naming_convention.html>`_
227*f7c14bbaSAndroid Build Coastguard Worker* `Building libbpf <https://libbpf.readthedocs.io/en/latest/libbpf_build.html>`_
228*f7c14bbaSAndroid Build Coastguard Worker* `API documentation Convention <https://libbpf.readthedocs.io/en/latest/libbpf_naming_convention.html#api-documentation-convention>`_
229