xref: /aosp_15_r20/external/libbpf/README.md (revision f7c14bbac8cf49633f2740db462ea43457973ec4)
1*f7c14bbaSAndroid Build Coastguard Worker<picture>
2*f7c14bbaSAndroid Build Coastguard Worker  <source media="(prefers-color-scheme: dark)" srcset="assets/libbpf-logo-sideways-darkbg.png" width="40%">
3*f7c14bbaSAndroid Build Coastguard Worker  <img src="assets/libbpf-logo-sideways.png" width="40%">
4*f7c14bbaSAndroid Build Coastguard Worker</picture>
5*f7c14bbaSAndroid Build Coastguard Worker
6*f7c14bbaSAndroid Build Coastguard Workerlibbpf
7*f7c14bbaSAndroid Build Coastguard Worker[![Github Actions Builds & Tests](https://github.com/libbpf/libbpf/actions/workflows/test.yml/badge.svg)](https://github.com/libbpf/libbpf/actions/workflows/test.yml)
8*f7c14bbaSAndroid Build Coastguard Worker[![Coverity](https://img.shields.io/coverity/scan/18195.svg)](https://scan.coverity.com/projects/libbpf)
9*f7c14bbaSAndroid Build Coastguard Worker[![CodeQL](https://github.com/libbpf/libbpf/workflows/CodeQL/badge.svg?branch=master)](https://github.com/libbpf/libbpf/actions?query=workflow%3ACodeQL+branch%3Amaster)
10*f7c14bbaSAndroid Build Coastguard Worker[![OSS-Fuzz Status](https://oss-fuzz-build-logs.storage.googleapis.com/badges/libbpf.svg)](https://oss-fuzz-build-logs.storage.googleapis.com/index.html#libbpf)
11*f7c14bbaSAndroid Build Coastguard Worker[![Read the Docs](https://readthedocs.org/projects/libbpf/badge/?version=latest)](https://libbpf.readthedocs.io/en/latest/)
12*f7c14bbaSAndroid Build Coastguard Worker======
13*f7c14bbaSAndroid Build Coastguard Worker
14*f7c14bbaSAndroid Build Coastguard Worker**This is the official home of the libbpf library.**
15*f7c14bbaSAndroid Build Coastguard Worker
16*f7c14bbaSAndroid Build Coastguard Worker*Please use this Github repository for building and packaging libbpf
17*f7c14bbaSAndroid Build Coastguard Workerand when using it in your projects through Git submodule.*
18*f7c14bbaSAndroid Build Coastguard Worker
19*f7c14bbaSAndroid Build Coastguard WorkerLibbpf *authoritative source code* is developed as part of [bpf-next Linux source
20*f7c14bbaSAndroid Build Coastguard Workertree](https://kernel.googlesource.com/pub/scm/linux/kernel/git/bpf/bpf-next) under
21*f7c14bbaSAndroid Build Coastguard Worker`tools/lib/bpf` subdirectory and is periodically synced to Github. As such, all the
22*f7c14bbaSAndroid Build Coastguard Workerlibbpf changes should be sent to [BPF mailing list](http://vger.kernel.org/vger-lists.html#bpf),
23*f7c14bbaSAndroid Build Coastguard Workerplease don't open PRs here unless you are changing Github-specific parts of libbpf
24*f7c14bbaSAndroid Build Coastguard Worker(e.g., Github-specific Makefile).
25*f7c14bbaSAndroid Build Coastguard Worker
26*f7c14bbaSAndroid Build Coastguard WorkerLibbpf and general BPF usage questions
27*f7c14bbaSAndroid Build Coastguard Worker======================================
28*f7c14bbaSAndroid Build Coastguard Worker
29*f7c14bbaSAndroid Build Coastguard WorkerLibbpf documentation can be found [here](https://libbpf.readthedocs.io/en/latest/api.html).
30*f7c14bbaSAndroid Build Coastguard WorkerIt's an ongoing effort and has ways to go, but please take a look and consider contributing as well.
31*f7c14bbaSAndroid Build Coastguard Worker
32*f7c14bbaSAndroid Build Coastguard WorkerPlease check out [libbpf-bootstrap](https://github.com/libbpf/libbpf-bootstrap)
33*f7c14bbaSAndroid Build Coastguard Workerand [the companion blog post](https://nakryiko.com/posts/libbpf-bootstrap/) for
34*f7c14bbaSAndroid Build Coastguard Workerthe examples of building BPF applications with libbpf.
35*f7c14bbaSAndroid Build Coastguard Worker[libbpf-tools](https://github.com/iovisor/bcc/tree/master/libbpf-tools) are also
36*f7c14bbaSAndroid Build Coastguard Workera good source of the real-world libbpf-based tracing tools.
37*f7c14bbaSAndroid Build Coastguard Worker
38*f7c14bbaSAndroid Build Coastguard WorkerSee also ["BPF CO-RE reference guide"](https://nakryiko.com/posts/bpf-core-reference-guide/)
39*f7c14bbaSAndroid Build Coastguard Workerfor the coverage of practical aspects of building BPF CO-RE applications and
40*f7c14bbaSAndroid Build Coastguard Worker["BPF CO-RE"](https://nakryiko.com/posts/bpf-portability-and-co-re/) for
41*f7c14bbaSAndroid Build Coastguard Workergeneral introduction into BPF portability issues and BPF CO-RE origins.
42*f7c14bbaSAndroid Build Coastguard Worker
43*f7c14bbaSAndroid Build Coastguard WorkerAll general BPF questions, including kernel functionality, libbpf APIs and
44*f7c14bbaSAndroid Build Coastguard Workertheir application, should be sent to [email protected] mailing list. You can
45*f7c14bbaSAndroid Build Coastguard Workersubscribe to it [here](http://vger.kernel.org/vger-lists.html#bpf) and search
46*f7c14bbaSAndroid Build Coastguard Workerits archive [here](https://lore.kernel.org/bpf/). Please search the archive
47*f7c14bbaSAndroid Build Coastguard Workerbefore asking new questions. It very well might be that this was already
48*f7c14bbaSAndroid Build Coastguard Workeraddressed or answered before.
49*f7c14bbaSAndroid Build Coastguard Worker
50*f7c14bbaSAndroid Build Coastguard Worker[email protected] is monitored by many more people and they will happily try
51*f7c14bbaSAndroid Build Coastguard Workerto help you with whatever issue you have. This repository's PRs and issues
52*f7c14bbaSAndroid Build Coastguard Workershould be opened only for dealing with issues pertaining to specific way this
53*f7c14bbaSAndroid Build Coastguard Workerlibbpf mirror repo is set up and organized.
54*f7c14bbaSAndroid Build Coastguard Worker
55*f7c14bbaSAndroid Build Coastguard WorkerBuilding libbpf
56*f7c14bbaSAndroid Build Coastguard Worker===============
57*f7c14bbaSAndroid Build Coastguard Workerlibelf is an internal dependency of libbpf and thus it is required to link
58*f7c14bbaSAndroid Build Coastguard Workeragainst and must be installed on the system for applications to work.
59*f7c14bbaSAndroid Build Coastguard Workerpkg-config is used by default to find libelf, and the program called can be
60*f7c14bbaSAndroid Build Coastguard Workeroverridden with `PKG_CONFIG`.
61*f7c14bbaSAndroid Build Coastguard Worker
62*f7c14bbaSAndroid Build Coastguard WorkerIf using `pkg-config` at build time is not desired, it can be disabled by
63*f7c14bbaSAndroid Build Coastguard Workersetting `NO_PKG_CONFIG=1` when calling make.
64*f7c14bbaSAndroid Build Coastguard Worker
65*f7c14bbaSAndroid Build Coastguard WorkerTo build both static libbpf.a and shared libbpf.so:
66*f7c14bbaSAndroid Build Coastguard Worker```bash
67*f7c14bbaSAndroid Build Coastguard Worker$ cd src
68*f7c14bbaSAndroid Build Coastguard Worker$ make
69*f7c14bbaSAndroid Build Coastguard Worker```
70*f7c14bbaSAndroid Build Coastguard Worker
71*f7c14bbaSAndroid Build Coastguard WorkerTo build only static libbpf.a library in directory
72*f7c14bbaSAndroid Build Coastguard Workerbuild/ and install them together with libbpf headers in a staging directory
73*f7c14bbaSAndroid Build Coastguard Workerroot/:
74*f7c14bbaSAndroid Build Coastguard Worker```bash
75*f7c14bbaSAndroid Build Coastguard Worker$ cd src
76*f7c14bbaSAndroid Build Coastguard Worker$ mkdir build root
77*f7c14bbaSAndroid Build Coastguard Worker$ BUILD_STATIC_ONLY=y OBJDIR=build DESTDIR=root make install
78*f7c14bbaSAndroid Build Coastguard Worker```
79*f7c14bbaSAndroid Build Coastguard Worker
80*f7c14bbaSAndroid Build Coastguard WorkerTo build both static libbpf.a and shared libbpf.so against a custom libelf
81*f7c14bbaSAndroid Build Coastguard Workerdependency installed in /build/root/ and install them together with libbpf
82*f7c14bbaSAndroid Build Coastguard Workerheaders in a build directory /build/root/:
83*f7c14bbaSAndroid Build Coastguard Worker```bash
84*f7c14bbaSAndroid Build Coastguard Worker$ cd src
85*f7c14bbaSAndroid Build Coastguard Worker$ PKG_CONFIG_PATH=/build/root/lib64/pkgconfig DESTDIR=/build/root make install
86*f7c14bbaSAndroid Build Coastguard Worker```
87*f7c14bbaSAndroid Build Coastguard Worker
88*f7c14bbaSAndroid Build Coastguard WorkerBPF CO-RE (Compile Once – Run Everywhere)
89*f7c14bbaSAndroid Build Coastguard Worker=========================================
90*f7c14bbaSAndroid Build Coastguard Worker
91*f7c14bbaSAndroid Build Coastguard WorkerLibbpf supports building BPF CO-RE-enabled applications, which, in contrast to
92*f7c14bbaSAndroid Build Coastguard Worker[BCC](https://github.com/iovisor/bcc/), do not require Clang/LLVM runtime
93*f7c14bbaSAndroid Build Coastguard Workerbeing deployed to target servers and doesn't rely on kernel-devel headers
94*f7c14bbaSAndroid Build Coastguard Workerbeing available.
95*f7c14bbaSAndroid Build Coastguard Worker
96*f7c14bbaSAndroid Build Coastguard WorkerIt does rely on kernel to be built with [BTF type
97*f7c14bbaSAndroid Build Coastguard Workerinformation](https://www.kernel.org/doc/html/latest/bpf/btf.html), though.
98*f7c14bbaSAndroid Build Coastguard WorkerSome major Linux distributions come with kernel BTF already built in:
99*f7c14bbaSAndroid Build Coastguard Worker  - Fedora 31+
100*f7c14bbaSAndroid Build Coastguard Worker  - RHEL 8.2+
101*f7c14bbaSAndroid Build Coastguard Worker  - OpenSUSE Tumbleweed (in the next release, as of 2020-06-04)
102*f7c14bbaSAndroid Build Coastguard Worker  - Arch Linux (from kernel 5.7.1.arch1-1)
103*f7c14bbaSAndroid Build Coastguard Worker  - Manjaro (from kernel 5.4 if compiled after 2021-06-18)
104*f7c14bbaSAndroid Build Coastguard Worker  - Ubuntu 20.10
105*f7c14bbaSAndroid Build Coastguard Worker  - Debian 11 (amd64/arm64)
106*f7c14bbaSAndroid Build Coastguard Worker
107*f7c14bbaSAndroid Build Coastguard WorkerIf your kernel doesn't come with BTF built-in, you'll need to build custom
108*f7c14bbaSAndroid Build Coastguard Workerkernel. You'll need:
109*f7c14bbaSAndroid Build Coastguard Worker  - `pahole` 1.16+ tool (part of `dwarves` package), which performs DWARF to
110*f7c14bbaSAndroid Build Coastguard Worker    BTF conversion;
111*f7c14bbaSAndroid Build Coastguard Worker  - kernel built with `CONFIG_DEBUG_INFO_BTF=y` option;
112*f7c14bbaSAndroid Build Coastguard Worker  - you can check if your kernel has BTF built-in by looking for
113*f7c14bbaSAndroid Build Coastguard Worker    `/sys/kernel/btf/vmlinux` file:
114*f7c14bbaSAndroid Build Coastguard Worker
115*f7c14bbaSAndroid Build Coastguard Worker```shell
116*f7c14bbaSAndroid Build Coastguard Worker$ ls -la /sys/kernel/btf/vmlinux
117*f7c14bbaSAndroid Build Coastguard Worker-r--r--r--. 1 root root 3541561 Jun  2 18:16 /sys/kernel/btf/vmlinux
118*f7c14bbaSAndroid Build Coastguard Worker```
119*f7c14bbaSAndroid Build Coastguard Worker
120*f7c14bbaSAndroid Build Coastguard WorkerTo develop and build BPF programs, you'll need Clang/LLVM 10+. The following
121*f7c14bbaSAndroid Build Coastguard Workerdistributions have Clang/LLVM 10+ packaged by default:
122*f7c14bbaSAndroid Build Coastguard Worker  - Fedora 32+
123*f7c14bbaSAndroid Build Coastguard Worker  - Ubuntu 20.04+
124*f7c14bbaSAndroid Build Coastguard Worker  - Arch Linux
125*f7c14bbaSAndroid Build Coastguard Worker  - Ubuntu 20.10 (LLVM 11)
126*f7c14bbaSAndroid Build Coastguard Worker  - Debian 11 (LLVM 11)
127*f7c14bbaSAndroid Build Coastguard Worker  - Alpine 3.13+
128*f7c14bbaSAndroid Build Coastguard Worker
129*f7c14bbaSAndroid Build Coastguard WorkerOtherwise, please make sure to update it on your system.
130*f7c14bbaSAndroid Build Coastguard Worker
131*f7c14bbaSAndroid Build Coastguard WorkerThe following resources are useful to understand what BPF CO-RE is and how to
132*f7c14bbaSAndroid Build Coastguard Workeruse it:
133*f7c14bbaSAndroid Build Coastguard Worker- [BPF CO-RE reference guide](https://nakryiko.com/posts/bpf-core-reference-guide/)
134*f7c14bbaSAndroid Build Coastguard Worker- [BPF Portability and CO-RE](https://nakryiko.com/posts/bpf-portability-and-co-re/)
135*f7c14bbaSAndroid Build Coastguard Worker- [HOWTO: BCC to libbpf conversion](https://nakryiko.com/posts/bcc-to-libbpf-howto-guide/)
136*f7c14bbaSAndroid Build Coastguard Worker- [libbpf-tools in BCC repo](https://github.com/iovisor/bcc/tree/master/libbpf-tools)
137*f7c14bbaSAndroid Build Coastguard Worker  contain lots of real-world tools converted from BCC to BPF CO-RE. Consider
138*f7c14bbaSAndroid Build Coastguard Worker  converting some more to both contribute to the BPF community and gain some
139*f7c14bbaSAndroid Build Coastguard Worker  more experience with it.
140*f7c14bbaSAndroid Build Coastguard Worker
141*f7c14bbaSAndroid Build Coastguard WorkerDistributions
142*f7c14bbaSAndroid Build Coastguard Worker=============
143*f7c14bbaSAndroid Build Coastguard Worker
144*f7c14bbaSAndroid Build Coastguard WorkerDistributions packaging libbpf from this mirror:
145*f7c14bbaSAndroid Build Coastguard Worker  - [Fedora](https://src.fedoraproject.org/rpms/libbpf)
146*f7c14bbaSAndroid Build Coastguard Worker  - [Gentoo](https://packages.gentoo.org/packages/dev-libs/libbpf)
147*f7c14bbaSAndroid Build Coastguard Worker  - [Debian](https://packages.debian.org/source/sid/libbpf)
148*f7c14bbaSAndroid Build Coastguard Worker  - [Arch](https://archlinux.org/packages/core/x86_64/libbpf/)
149*f7c14bbaSAndroid Build Coastguard Worker  - [Ubuntu](https://packages.ubuntu.com/source/jammy/libbpf)
150*f7c14bbaSAndroid Build Coastguard Worker  - [Alpine](https://pkgs.alpinelinux.org/packages?name=libbpf)
151*f7c14bbaSAndroid Build Coastguard Worker
152*f7c14bbaSAndroid Build Coastguard WorkerBenefits of packaging from the mirror over packaging from kernel sources:
153*f7c14bbaSAndroid Build Coastguard Worker  - Consistent versioning across distributions.
154*f7c14bbaSAndroid Build Coastguard Worker  - No ties to any specific kernel, transparent handling of older kernels.
155*f7c14bbaSAndroid Build Coastguard Worker    Libbpf is designed to be kernel-agnostic and work across multitude of
156*f7c14bbaSAndroid Build Coastguard Worker    kernel versions. It has built-in mechanisms to gracefully handle older
157*f7c14bbaSAndroid Build Coastguard Worker    kernels, that are missing some of the features, by working around or
158*f7c14bbaSAndroid Build Coastguard Worker    gracefully degrading functionality. Thus libbpf is not tied to a specific
159*f7c14bbaSAndroid Build Coastguard Worker    kernel version and can/should be packaged and versioned independently.
160*f7c14bbaSAndroid Build Coastguard Worker  - Continuous integration testing via
161*f7c14bbaSAndroid Build Coastguard Worker    [GitHub Actions](https://github.com/libbpf/libbpf/actions).
162*f7c14bbaSAndroid Build Coastguard Worker  - Static code analysis via [LGTM](https://lgtm.com/projects/g/libbpf/libbpf)
163*f7c14bbaSAndroid Build Coastguard Worker    and [Coverity](https://scan.coverity.com/projects/libbpf).
164*f7c14bbaSAndroid Build Coastguard Worker
165*f7c14bbaSAndroid Build Coastguard WorkerPackage dependencies of libbpf, package names may vary across distros:
166*f7c14bbaSAndroid Build Coastguard Worker  - zlib
167*f7c14bbaSAndroid Build Coastguard Worker  - libelf
168*f7c14bbaSAndroid Build Coastguard Worker
169*f7c14bbaSAndroid Build Coastguard Worker[![libbpf distro packaging status](https://repology.org/badge/vertical-allrepos/libbpf.svg)](https://repology.org/project/libbpf/versions)
170*f7c14bbaSAndroid Build Coastguard Worker
171*f7c14bbaSAndroid Build Coastguard Worker
172*f7c14bbaSAndroid Build Coastguard Workerbpf-next to Github sync
173*f7c14bbaSAndroid Build Coastguard Worker=======================
174*f7c14bbaSAndroid Build Coastguard Worker
175*f7c14bbaSAndroid Build Coastguard WorkerAll the gory details of syncing can be found in `scripts/sync-kernel.sh`
176*f7c14bbaSAndroid Build Coastguard Workerscript. See [SYNC.md](SYNC.md) for instruction.
177*f7c14bbaSAndroid Build Coastguard Worker
178*f7c14bbaSAndroid Build Coastguard WorkerSome header files in this repo (`include/linux/*.h`) are reduced versions of
179*f7c14bbaSAndroid Build Coastguard Workertheir counterpart files at
180*f7c14bbaSAndroid Build Coastguard Worker[bpf-next](https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/)'s
181*f7c14bbaSAndroid Build Coastguard Worker`tools/include/linux/*.h` to make compilation successful.
182*f7c14bbaSAndroid Build Coastguard Worker
183*f7c14bbaSAndroid Build Coastguard WorkerLicense
184*f7c14bbaSAndroid Build Coastguard Worker=======
185*f7c14bbaSAndroid Build Coastguard Worker
186*f7c14bbaSAndroid Build Coastguard WorkerThis work is dual-licensed under BSD 2-clause license and GNU LGPL v2.1 license.
187*f7c14bbaSAndroid Build Coastguard WorkerYou can choose between one of them if you use this work.
188*f7c14bbaSAndroid Build Coastguard Worker
189*f7c14bbaSAndroid Build Coastguard Worker`SPDX-License-Identifier: BSD-2-Clause OR LGPL-2.1`
190