17c2aad20Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0 27c2aad20Sopenharmony_ci 37c2aad20Sopenharmony_ci=============== 47c2aad20Sopenharmony_cilibbpf Overview 57c2aad20Sopenharmony_ci=============== 67c2aad20Sopenharmony_ci 77c2aad20Sopenharmony_cilibbpf is a C-based library containing a BPF loader that takes compiled BPF 87c2aad20Sopenharmony_ciobject files and prepares and loads them into the Linux kernel. libbpf takes the 97c2aad20Sopenharmony_ciheavy lifting of loading, verifying, and attaching BPF programs to various 107c2aad20Sopenharmony_cikernel hooks, allowing BPF application developers to focus only on BPF program 117c2aad20Sopenharmony_cicorrectness and performance. 127c2aad20Sopenharmony_ci 137c2aad20Sopenharmony_ciThe following are the high-level features supported by libbpf: 147c2aad20Sopenharmony_ci 157c2aad20Sopenharmony_ci* Provides high-level and low-level APIs for user space programs to interact 167c2aad20Sopenharmony_ci with BPF programs. The low-level APIs wrap all the bpf system call 177c2aad20Sopenharmony_ci functionality, which is useful when users need more fine-grained control 187c2aad20Sopenharmony_ci over the interactions between user space and BPF programs. 197c2aad20Sopenharmony_ci* Provides overall support for the BPF object skeleton generated by bpftool. 207c2aad20Sopenharmony_ci The skeleton file simplifies the process for the user space programs to access 217c2aad20Sopenharmony_ci global variables and work with BPF programs. 227c2aad20Sopenharmony_ci* Provides BPF-side APIS, including BPF helper definitions, BPF maps support, 237c2aad20Sopenharmony_ci and tracing helpers, allowing developers to simplify BPF code writing. 247c2aad20Sopenharmony_ci* Supports BPF CO-RE mechanism, enabling BPF developers to write portable 257c2aad20Sopenharmony_ci BPF programs that can be compiled once and run across different kernel 267c2aad20Sopenharmony_ci versions. 277c2aad20Sopenharmony_ci 287c2aad20Sopenharmony_ciThis document will delve into the above concepts in detail, providing a deeper 297c2aad20Sopenharmony_ciunderstanding of the capabilities and advantages of libbpf and how it can help 307c2aad20Sopenharmony_ciyou develop BPF applications efficiently. 317c2aad20Sopenharmony_ci 327c2aad20Sopenharmony_ciBPF App Lifecycle and libbpf APIs 337c2aad20Sopenharmony_ci================================== 347c2aad20Sopenharmony_ci 357c2aad20Sopenharmony_ciA BPF application consists of one or more BPF programs (either cooperating or 367c2aad20Sopenharmony_cicompletely independent), BPF maps, and global variables. The global 377c2aad20Sopenharmony_civariables are shared between all BPF programs, which allows them to cooperate on 387c2aad20Sopenharmony_cia common set of data. libbpf provides APIs that user space programs can use to 397c2aad20Sopenharmony_cimanipulate the BPF programs by triggering different phases of a BPF application 407c2aad20Sopenharmony_cilifecycle. 417c2aad20Sopenharmony_ci 427c2aad20Sopenharmony_ciThe following section provides a brief overview of each phase in the BPF life 437c2aad20Sopenharmony_cicycle: 447c2aad20Sopenharmony_ci 457c2aad20Sopenharmony_ci* **Open phase**: In this phase, libbpf parses the BPF 467c2aad20Sopenharmony_ci object file and discovers BPF maps, BPF programs, and global variables. After 477c2aad20Sopenharmony_ci a BPF app is opened, user space apps can make additional adjustments 487c2aad20Sopenharmony_ci (setting BPF program types, if necessary; pre-setting initial values for 497c2aad20Sopenharmony_ci global variables, etc.) before all the entities are created and loaded. 507c2aad20Sopenharmony_ci 517c2aad20Sopenharmony_ci* **Load phase**: In the load phase, libbpf creates BPF 527c2aad20Sopenharmony_ci maps, resolves various relocations, and verifies and loads BPF programs into 537c2aad20Sopenharmony_ci the kernel. At this point, libbpf validates all the parts of a BPF application 547c2aad20Sopenharmony_ci and loads the BPF program into the kernel, but no BPF program has yet been 557c2aad20Sopenharmony_ci executed. After the load phase, it’s possible to set up the initial BPF map 567c2aad20Sopenharmony_ci state without racing with the BPF program code execution. 577c2aad20Sopenharmony_ci 587c2aad20Sopenharmony_ci* **Attachment phase**: In this phase, libbpf 597c2aad20Sopenharmony_ci attaches BPF programs to various BPF hook points (e.g., tracepoints, kprobes, 607c2aad20Sopenharmony_ci cgroup hooks, network packet processing pipeline, etc.). During this 617c2aad20Sopenharmony_ci phase, BPF programs perform useful work such as processing 627c2aad20Sopenharmony_ci packets, or updating BPF maps and global variables that can be read from user 637c2aad20Sopenharmony_ci space. 647c2aad20Sopenharmony_ci 657c2aad20Sopenharmony_ci* **Tear down phase**: In the tear down phase, 667c2aad20Sopenharmony_ci libbpf detaches BPF programs and unloads them from the kernel. BPF maps are 677c2aad20Sopenharmony_ci destroyed, and all the resources used by the BPF app are freed. 687c2aad20Sopenharmony_ci 697c2aad20Sopenharmony_ciBPF Object Skeleton File 707c2aad20Sopenharmony_ci======================== 717c2aad20Sopenharmony_ci 727c2aad20Sopenharmony_ciBPF skeleton is an alternative interface to libbpf APIs for working with BPF 737c2aad20Sopenharmony_ciobjects. Skeleton code abstract away generic libbpf APIs to significantly 747c2aad20Sopenharmony_cisimplify code for manipulating BPF programs from user space. Skeleton code 757c2aad20Sopenharmony_ciincludes a bytecode representation of the BPF object file, simplifying the 767c2aad20Sopenharmony_ciprocess of distributing your BPF code. With BPF bytecode embedded, there are no 777c2aad20Sopenharmony_ciextra files to deploy along with your application binary. 787c2aad20Sopenharmony_ci 797c2aad20Sopenharmony_ciYou can generate the skeleton header file ``(.skel.h)`` for a specific object 807c2aad20Sopenharmony_cifile by passing the BPF object to the bpftool. The generated BPF skeleton 817c2aad20Sopenharmony_ciprovides the following custom functions that correspond to the BPF lifecycle, 827c2aad20Sopenharmony_cieach of them prefixed with the specific object name: 837c2aad20Sopenharmony_ci 847c2aad20Sopenharmony_ci* ``<name>__open()`` – creates and opens BPF application (``<name>`` stands for 857c2aad20Sopenharmony_ci the specific bpf object name) 867c2aad20Sopenharmony_ci* ``<name>__load()`` – instantiates, loads,and verifies BPF application parts 877c2aad20Sopenharmony_ci* ``<name>__attach()`` – attaches all auto-attachable BPF programs (it’s 887c2aad20Sopenharmony_ci optional, you can have more control by using libbpf APIs directly) 897c2aad20Sopenharmony_ci* ``<name>__destroy()`` – detaches all BPF programs and 907c2aad20Sopenharmony_ci frees up all used resources 917c2aad20Sopenharmony_ci 927c2aad20Sopenharmony_ciUsing the skeleton code is the recommended way to work with bpf programs. Keep 937c2aad20Sopenharmony_ciin mind, BPF skeleton provides access to the underlying BPF object, so whatever 947c2aad20Sopenharmony_ciwas possible to do with generic libbpf APIs is still possible even when the BPF 957c2aad20Sopenharmony_ciskeleton is used. It's an additive convenience feature, with no syscalls, and no 967c2aad20Sopenharmony_cicumbersome code. 977c2aad20Sopenharmony_ci 987c2aad20Sopenharmony_ciOther Advantages of Using Skeleton File 997c2aad20Sopenharmony_ci--------------------------------------- 1007c2aad20Sopenharmony_ci 1017c2aad20Sopenharmony_ci* BPF skeleton provides an interface for user space programs to work with BPF 1027c2aad20Sopenharmony_ci global variables. The skeleton code memory maps global variables as a struct 1037c2aad20Sopenharmony_ci into user space. The struct interface allows user space programs to initialize 1047c2aad20Sopenharmony_ci BPF programs before the BPF load phase and fetch and update data from user 1057c2aad20Sopenharmony_ci space afterward. 1067c2aad20Sopenharmony_ci 1077c2aad20Sopenharmony_ci* The ``skel.h`` file reflects the object file structure by listing out the 1087c2aad20Sopenharmony_ci available maps, programs, etc. BPF skeleton provides direct access to all the 1097c2aad20Sopenharmony_ci BPF maps and BPF programs as struct fields. This eliminates the need for 1107c2aad20Sopenharmony_ci string-based lookups with ``bpf_object_find_map_by_name()`` and 1117c2aad20Sopenharmony_ci ``bpf_object_find_program_by_name()`` APIs, reducing errors due to BPF source 1127c2aad20Sopenharmony_ci code and user-space code getting out of sync. 1137c2aad20Sopenharmony_ci 1147c2aad20Sopenharmony_ci* The embedded bytecode representation of the object file ensures that the 1157c2aad20Sopenharmony_ci skeleton and the BPF object file are always in sync. 1167c2aad20Sopenharmony_ci 1177c2aad20Sopenharmony_ciBPF Helpers 1187c2aad20Sopenharmony_ci=========== 1197c2aad20Sopenharmony_ci 1207c2aad20Sopenharmony_cilibbpf provides BPF-side APIs that BPF programs can use to interact with the 1217c2aad20Sopenharmony_cisystem. The BPF helpers definition allows developers to use them in BPF code as 1227c2aad20Sopenharmony_ciany other plain C function. For example, there are helper functions to print 1237c2aad20Sopenharmony_cidebugging messages, get the time since the system was booted, interact with BPF 1247c2aad20Sopenharmony_cimaps, manipulate network packets, etc. 1257c2aad20Sopenharmony_ci 1267c2aad20Sopenharmony_ciFor a complete description of what the helpers do, the arguments they take, and 1277c2aad20Sopenharmony_cithe return value, see the `bpf-helpers 1287c2aad20Sopenharmony_ci<https://man7.org/linux/man-pages/man7/bpf-helpers.7.html>`_ man page. 1297c2aad20Sopenharmony_ci 1307c2aad20Sopenharmony_ciBPF CO-RE (Compile Once – Run Everywhere) 1317c2aad20Sopenharmony_ci========================================= 1327c2aad20Sopenharmony_ci 1337c2aad20Sopenharmony_ciBPF programs work in the kernel space and have access to kernel memory and data 1347c2aad20Sopenharmony_cistructures. One limitation that BPF applications come across is the lack of 1357c2aad20Sopenharmony_ciportability across different kernel versions and configurations. `BCC 1367c2aad20Sopenharmony_ci<https://github.com/iovisor/bcc/>`_ is one of the solutions for BPF 1377c2aad20Sopenharmony_ciportability. However, it comes with runtime overhead and a large binary size 1387c2aad20Sopenharmony_cifrom embedding the compiler with the application. 1397c2aad20Sopenharmony_ci 1407c2aad20Sopenharmony_cilibbpf steps up the BPF program portability by supporting the BPF CO-RE concept. 1417c2aad20Sopenharmony_ciBPF CO-RE brings together BTF type information, libbpf, and the compiler to 1427c2aad20Sopenharmony_ciproduce a single executable binary that you can run on multiple kernel versions 1437c2aad20Sopenharmony_ciand configurations. 1447c2aad20Sopenharmony_ci 1457c2aad20Sopenharmony_ciTo make BPF programs portable libbpf relies on the BTF type information of the 1467c2aad20Sopenharmony_cirunning kernel. Kernel also exposes this self-describing authoritative BTF 1477c2aad20Sopenharmony_ciinformation through ``sysfs`` at ``/sys/kernel/btf/vmlinux``. 1487c2aad20Sopenharmony_ci 1497c2aad20Sopenharmony_ciYou can generate the BTF information for the running kernel with the following 1507c2aad20Sopenharmony_cicommand: 1517c2aad20Sopenharmony_ci 1527c2aad20Sopenharmony_ci:: 1537c2aad20Sopenharmony_ci 1547c2aad20Sopenharmony_ci $ bpftool btf dump file /sys/kernel/btf/vmlinux format c > vmlinux.h 1557c2aad20Sopenharmony_ci 1567c2aad20Sopenharmony_ciThe command generates a ``vmlinux.h`` header file with all kernel types 1577c2aad20Sopenharmony_ci(:doc:`BTF types <../btf>`) that the running kernel uses. Including 1587c2aad20Sopenharmony_ci``vmlinux.h`` in your BPF program eliminates dependency on system-wide kernel 1597c2aad20Sopenharmony_ciheaders. 1607c2aad20Sopenharmony_ci 1617c2aad20Sopenharmony_cilibbpf enables portability of BPF programs by looking at the BPF program’s 1627c2aad20Sopenharmony_cirecorded BTF type and relocation information and matching them to BTF 1637c2aad20Sopenharmony_ciinformation (vmlinux) provided by the running kernel. libbpf then resolves and 1647c2aad20Sopenharmony_cimatches all the types and fields, and updates necessary offsets and other 1657c2aad20Sopenharmony_cirelocatable data to ensure that BPF program’s logic functions correctly for a 1667c2aad20Sopenharmony_cispecific kernel on the host. BPF CO-RE concept thus eliminates overhead 1677c2aad20Sopenharmony_ciassociated with BPF development and allows developers to write portable BPF 1687c2aad20Sopenharmony_ciapplications without modifications and runtime source code compilation on the 1697c2aad20Sopenharmony_citarget machine. 1707c2aad20Sopenharmony_ci 1717c2aad20Sopenharmony_ciThe following code snippet shows how to read the parent field of a kernel 1727c2aad20Sopenharmony_ci``task_struct`` using BPF CO-RE and libbf. The basic helper to read a field in a 1737c2aad20Sopenharmony_ciCO-RE relocatable manner is ``bpf_core_read(dst, sz, src)``, which will read 1747c2aad20Sopenharmony_ci``sz`` bytes from the field referenced by ``src`` into the memory pointed to by 1757c2aad20Sopenharmony_ci``dst``. 1767c2aad20Sopenharmony_ci 1777c2aad20Sopenharmony_ci.. code-block:: C 1787c2aad20Sopenharmony_ci :emphasize-lines: 6 1797c2aad20Sopenharmony_ci 1807c2aad20Sopenharmony_ci //... 1817c2aad20Sopenharmony_ci struct task_struct *task = (void *)bpf_get_current_task(); 1827c2aad20Sopenharmony_ci struct task_struct *parent_task; 1837c2aad20Sopenharmony_ci int err; 1847c2aad20Sopenharmony_ci 1857c2aad20Sopenharmony_ci err = bpf_core_read(&parent_task, sizeof(void *), &task->parent); 1867c2aad20Sopenharmony_ci if (err) { 1877c2aad20Sopenharmony_ci /* handle error */ 1887c2aad20Sopenharmony_ci } 1897c2aad20Sopenharmony_ci 1907c2aad20Sopenharmony_ci /* parent_task contains the value of task->parent pointer */ 1917c2aad20Sopenharmony_ci 1927c2aad20Sopenharmony_ciIn the code snippet, we first get a pointer to the current ``task_struct`` using 1937c2aad20Sopenharmony_ci``bpf_get_current_task()``. We then use ``bpf_core_read()`` to read the parent 1947c2aad20Sopenharmony_cifield of task struct into the ``parent_task`` variable. ``bpf_core_read()`` is 1957c2aad20Sopenharmony_cijust like ``bpf_probe_read_kernel()`` BPF helper, except it records information 1967c2aad20Sopenharmony_ciabout the field that should be relocated on the target kernel. i.e, if the 1977c2aad20Sopenharmony_ci``parent`` field gets shifted to a different offset within 1987c2aad20Sopenharmony_ci``struct task_struct`` due to some new field added in front of it, libbpf will 1997c2aad20Sopenharmony_ciautomatically adjust the actual offset to the proper value. 2007c2aad20Sopenharmony_ci 2017c2aad20Sopenharmony_ciGetting Started with libbpf 2027c2aad20Sopenharmony_ci=========================== 2037c2aad20Sopenharmony_ci 2047c2aad20Sopenharmony_ciCheck out the `libbpf-bootstrap <https://github.com/libbpf/libbpf-bootstrap>`_ 2057c2aad20Sopenharmony_cirepository with simple examples of using libbpf to build various BPF 2067c2aad20Sopenharmony_ciapplications. 2077c2aad20Sopenharmony_ci 2087c2aad20Sopenharmony_ciSee also `libbpf API documentation 2097c2aad20Sopenharmony_ci<https://libbpf.readthedocs.io/en/latest/api.html>`_. 2107c2aad20Sopenharmony_ci 2117c2aad20Sopenharmony_cilibbpf and Rust 2127c2aad20Sopenharmony_ci=============== 2137c2aad20Sopenharmony_ci 2147c2aad20Sopenharmony_ciIf you are building BPF applications in Rust, it is recommended to use the 2157c2aad20Sopenharmony_ci`Libbpf-rs <https://github.com/libbpf/libbpf-rs>`_ library instead of bindgen 2167c2aad20Sopenharmony_cibindings directly to libbpf. Libbpf-rs wraps libbpf functionality in 2177c2aad20Sopenharmony_ciRust-idiomatic interfaces and provides libbpf-cargo plugin to handle BPF code 2187c2aad20Sopenharmony_cicompilation and skeleton generation. Using Libbpf-rs will make building user 2197c2aad20Sopenharmony_cispace part of the BPF application easier. Note that the BPF program themselves 2207c2aad20Sopenharmony_cimust still be written in plain C. 2217c2aad20Sopenharmony_ci 2227c2aad20Sopenharmony_ciAdditional Documentation 2237c2aad20Sopenharmony_ci======================== 2247c2aad20Sopenharmony_ci 2257c2aad20Sopenharmony_ci* `Program types and ELF Sections <https://libbpf.readthedocs.io/en/latest/program_types.html>`_ 2267c2aad20Sopenharmony_ci* `API naming convention <https://libbpf.readthedocs.io/en/latest/libbpf_naming_convention.html>`_ 2277c2aad20Sopenharmony_ci* `Building libbpf <https://libbpf.readthedocs.io/en/latest/libbpf_build.html>`_ 2287c2aad20Sopenharmony_ci* `API documentation Convention <https://libbpf.readthedocs.io/en/latest/libbpf_naming_convention.html#api-documentation-convention>`_ 229