17c2aad20Sopenharmony_ci<picture>
27c2aad20Sopenharmony_ci  <source media="(prefers-color-scheme: dark)" srcset="assets/libbpf-logo-sideways-darkbg.png" width="40%">
37c2aad20Sopenharmony_ci  <img src="assets/libbpf-logo-sideways.png" width="40%">
47c2aad20Sopenharmony_ci</picture>
57c2aad20Sopenharmony_ci
67c2aad20Sopenharmony_cilibbpf
77c2aad20Sopenharmony_ci[![Github Actions Builds & Tests](https://github.com/libbpf/libbpf/actions/workflows/test.yml/badge.svg)](https://github.com/libbpf/libbpf/actions/workflows/test.yml)
87c2aad20Sopenharmony_ci[![Coverity](https://img.shields.io/coverity/scan/18195.svg)](https://scan.coverity.com/projects/libbpf)
97c2aad20Sopenharmony_ci[![CodeQL](https://github.com/libbpf/libbpf/workflows/CodeQL/badge.svg?branch=master)](https://github.com/libbpf/libbpf/actions?query=workflow%3ACodeQL+branch%3Amaster)
107c2aad20Sopenharmony_ci[![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)
117c2aad20Sopenharmony_ci[![Read the Docs](https://readthedocs.org/projects/libbpf/badge/?version=latest)](https://libbpf.readthedocs.io/en/latest/)
127c2aad20Sopenharmony_ci======
137c2aad20Sopenharmony_ci
147c2aad20Sopenharmony_ci**This is the official home of the libbpf library.**
157c2aad20Sopenharmony_ci
167c2aad20Sopenharmony_ci*Please use this Github repository for building and packaging libbpf
177c2aad20Sopenharmony_ciand when using it in your projects through Git submodule.*
187c2aad20Sopenharmony_ci
197c2aad20Sopenharmony_ciLibbpf *authoritative source code* is developed as part of [bpf-next Linux source
207c2aad20Sopenharmony_citree](https://kernel.googlesource.com/pub/scm/linux/kernel/git/bpf/bpf-next) under
217c2aad20Sopenharmony_ci`tools/lib/bpf` subdirectory and is periodically synced to Github. As such, all the
227c2aad20Sopenharmony_cilibbpf changes should be sent to [BPF mailing list](http://vger.kernel.org/vger-lists.html#bpf),
237c2aad20Sopenharmony_ciplease don't open PRs here unless you are changing Github-specific parts of libbpf
247c2aad20Sopenharmony_ci(e.g., Github-specific Makefile).
257c2aad20Sopenharmony_ci
267c2aad20Sopenharmony_ciLibbpf and general BPF usage questions
277c2aad20Sopenharmony_ci======================================
287c2aad20Sopenharmony_ci
297c2aad20Sopenharmony_ciLibbpf documentation can be found [here](https://libbpf.readthedocs.io/en/latest/api.html).
307c2aad20Sopenharmony_ciIt's an ongoing effort and has ways to go, but please take a look and consider contributing as well.
317c2aad20Sopenharmony_ci
327c2aad20Sopenharmony_ciPlease check out [libbpf-bootstrap](https://github.com/libbpf/libbpf-bootstrap)
337c2aad20Sopenharmony_ciand [the companion blog post](https://nakryiko.com/posts/libbpf-bootstrap/) for
347c2aad20Sopenharmony_cithe examples of building BPF applications with libbpf.
357c2aad20Sopenharmony_ci[libbpf-tools](https://github.com/iovisor/bcc/tree/master/libbpf-tools) are also
367c2aad20Sopenharmony_cia good source of the real-world libbpf-based tracing tools.
377c2aad20Sopenharmony_ci
387c2aad20Sopenharmony_ciSee also ["BPF CO-RE reference guide"](https://nakryiko.com/posts/bpf-core-reference-guide/)
397c2aad20Sopenharmony_cifor the coverage of practical aspects of building BPF CO-RE applications and
407c2aad20Sopenharmony_ci["BPF CO-RE"](https://nakryiko.com/posts/bpf-portability-and-co-re/) for
417c2aad20Sopenharmony_cigeneral introduction into BPF portability issues and BPF CO-RE origins.
427c2aad20Sopenharmony_ci
437c2aad20Sopenharmony_ciAll general BPF questions, including kernel functionality, libbpf APIs and
447c2aad20Sopenharmony_citheir application, should be sent to bpf@vger.kernel.org mailing list. You can
457c2aad20Sopenharmony_cisubscribe to it [here](http://vger.kernel.org/vger-lists.html#bpf) and search
467c2aad20Sopenharmony_ciits archive [here](https://lore.kernel.org/bpf/). Please search the archive
477c2aad20Sopenharmony_cibefore asking new questions. It very well might be that this was already
487c2aad20Sopenharmony_ciaddressed or answered before.
497c2aad20Sopenharmony_ci
507c2aad20Sopenharmony_cibpf@vger.kernel.org is monitored by many more people and they will happily try
517c2aad20Sopenharmony_cito help you with whatever issue you have. This repository's PRs and issues
527c2aad20Sopenharmony_cishould be opened only for dealing with issues pertaining to specific way this
537c2aad20Sopenharmony_cilibbpf mirror repo is set up and organized.
547c2aad20Sopenharmony_ci
557c2aad20Sopenharmony_ciBuilding libbpf
567c2aad20Sopenharmony_ci===============
577c2aad20Sopenharmony_cilibelf is an internal dependency of libbpf and thus it is required to link
587c2aad20Sopenharmony_ciagainst and must be installed on the system for applications to work.
597c2aad20Sopenharmony_cipkg-config is used by default to find libelf, and the program called can be
607c2aad20Sopenharmony_cioverridden with `PKG_CONFIG`.
617c2aad20Sopenharmony_ci
627c2aad20Sopenharmony_ciIf using `pkg-config` at build time is not desired, it can be disabled by
637c2aad20Sopenharmony_cisetting `NO_PKG_CONFIG=1` when calling make.
647c2aad20Sopenharmony_ci
657c2aad20Sopenharmony_ciTo build both static libbpf.a and shared libbpf.so:
667c2aad20Sopenharmony_ci```bash
677c2aad20Sopenharmony_ci$ cd src
687c2aad20Sopenharmony_ci$ make
697c2aad20Sopenharmony_ci```
707c2aad20Sopenharmony_ci
717c2aad20Sopenharmony_ciTo build only static libbpf.a library in directory
727c2aad20Sopenharmony_cibuild/ and install them together with libbpf headers in a staging directory
737c2aad20Sopenharmony_ciroot/:
747c2aad20Sopenharmony_ci```bash
757c2aad20Sopenharmony_ci$ cd src
767c2aad20Sopenharmony_ci$ mkdir build root
777c2aad20Sopenharmony_ci$ BUILD_STATIC_ONLY=y OBJDIR=build DESTDIR=root make install
787c2aad20Sopenharmony_ci```
797c2aad20Sopenharmony_ci
807c2aad20Sopenharmony_ciTo build both static libbpf.a and shared libbpf.so against a custom libelf
817c2aad20Sopenharmony_cidependency installed in /build/root/ and install them together with libbpf
827c2aad20Sopenharmony_ciheaders in a build directory /build/root/:
837c2aad20Sopenharmony_ci```bash
847c2aad20Sopenharmony_ci$ cd src
857c2aad20Sopenharmony_ci$ PKG_CONFIG_PATH=/build/root/lib64/pkgconfig DESTDIR=/build/root make install
867c2aad20Sopenharmony_ci```
877c2aad20Sopenharmony_ci
887c2aad20Sopenharmony_ciBPF CO-RE (Compile Once – Run Everywhere)
897c2aad20Sopenharmony_ci=========================================
907c2aad20Sopenharmony_ci
917c2aad20Sopenharmony_ciLibbpf supports building BPF CO-RE-enabled applications, which, in contrast to
927c2aad20Sopenharmony_ci[BCC](https://github.com/iovisor/bcc/), do not require Clang/LLVM runtime
937c2aad20Sopenharmony_cibeing deployed to target servers and doesn't rely on kernel-devel headers
947c2aad20Sopenharmony_cibeing available.
957c2aad20Sopenharmony_ci
967c2aad20Sopenharmony_ciIt does rely on kernel to be built with [BTF type
977c2aad20Sopenharmony_ciinformation](https://www.kernel.org/doc/html/latest/bpf/btf.html), though.
987c2aad20Sopenharmony_ciSome major Linux distributions come with kernel BTF already built in:
997c2aad20Sopenharmony_ci  - Fedora 31+
1007c2aad20Sopenharmony_ci  - RHEL 8.2+
1017c2aad20Sopenharmony_ci  - OpenSUSE Tumbleweed (in the next release, as of 2020-06-04)
1027c2aad20Sopenharmony_ci  - Arch Linux (from kernel 5.7.1.arch1-1)
1037c2aad20Sopenharmony_ci  - Manjaro (from kernel 5.4 if compiled after 2021-06-18)
1047c2aad20Sopenharmony_ci  - Ubuntu 20.10
1057c2aad20Sopenharmony_ci  - Debian 11 (amd64/arm64)
1067c2aad20Sopenharmony_ci
1077c2aad20Sopenharmony_ciIf your kernel doesn't come with BTF built-in, you'll need to build custom
1087c2aad20Sopenharmony_cikernel. You'll need:
1097c2aad20Sopenharmony_ci  - `pahole` 1.16+ tool (part of `dwarves` package), which performs DWARF to
1107c2aad20Sopenharmony_ci    BTF conversion;
1117c2aad20Sopenharmony_ci  - kernel built with `CONFIG_DEBUG_INFO_BTF=y` option;
1127c2aad20Sopenharmony_ci  - you can check if your kernel has BTF built-in by looking for
1137c2aad20Sopenharmony_ci    `/sys/kernel/btf/vmlinux` file:
1147c2aad20Sopenharmony_ci  
1157c2aad20Sopenharmony_ci```shell
1167c2aad20Sopenharmony_ci$ ls -la /sys/kernel/btf/vmlinux
1177c2aad20Sopenharmony_ci-r--r--r--. 1 root root 3541561 Jun  2 18:16 /sys/kernel/btf/vmlinux
1187c2aad20Sopenharmony_ci```
1197c2aad20Sopenharmony_ci  
1207c2aad20Sopenharmony_ciTo develop and build BPF programs, you'll need Clang/LLVM 10+. The following
1217c2aad20Sopenharmony_cidistributions have Clang/LLVM 10+ packaged by default:
1227c2aad20Sopenharmony_ci  - Fedora 32+
1237c2aad20Sopenharmony_ci  - Ubuntu 20.04+
1247c2aad20Sopenharmony_ci  - Arch Linux
1257c2aad20Sopenharmony_ci  - Ubuntu 20.10 (LLVM 11)
1267c2aad20Sopenharmony_ci  - Debian 11 (LLVM 11)
1277c2aad20Sopenharmony_ci  - Alpine 3.13+
1287c2aad20Sopenharmony_ci
1297c2aad20Sopenharmony_ciOtherwise, please make sure to update it on your system.
1307c2aad20Sopenharmony_ci
1317c2aad20Sopenharmony_ciThe following resources are useful to understand what BPF CO-RE is and how to
1327c2aad20Sopenharmony_ciuse it:
1337c2aad20Sopenharmony_ci- [BPF CO-RE reference guide](https://nakryiko.com/posts/bpf-core-reference-guide/)
1347c2aad20Sopenharmony_ci- [BPF Portability and CO-RE](https://nakryiko.com/posts/bpf-portability-and-co-re/)
1357c2aad20Sopenharmony_ci- [HOWTO: BCC to libbpf conversion](https://nakryiko.com/posts/bcc-to-libbpf-howto-guide/)
1367c2aad20Sopenharmony_ci- [libbpf-tools in BCC repo](https://github.com/iovisor/bcc/tree/master/libbpf-tools)
1377c2aad20Sopenharmony_ci  contain lots of real-world tools converted from BCC to BPF CO-RE. Consider
1387c2aad20Sopenharmony_ci  converting some more to both contribute to the BPF community and gain some
1397c2aad20Sopenharmony_ci  more experience with it.
1407c2aad20Sopenharmony_ci
1417c2aad20Sopenharmony_ciDistributions
1427c2aad20Sopenharmony_ci=============
1437c2aad20Sopenharmony_ci
1447c2aad20Sopenharmony_ciDistributions packaging libbpf from this mirror:
1457c2aad20Sopenharmony_ci  - [Fedora](https://src.fedoraproject.org/rpms/libbpf)
1467c2aad20Sopenharmony_ci  - [Gentoo](https://packages.gentoo.org/packages/dev-libs/libbpf)
1477c2aad20Sopenharmony_ci  - [Debian](https://packages.debian.org/source/sid/libbpf)
1487c2aad20Sopenharmony_ci  - [Arch](https://archlinux.org/packages/core/x86_64/libbpf/)
1497c2aad20Sopenharmony_ci  - [Ubuntu](https://packages.ubuntu.com/source/impish/libbpf)
1507c2aad20Sopenharmony_ci  - [Alpine](https://pkgs.alpinelinux.org/packages?name=libbpf)
1517c2aad20Sopenharmony_ci
1527c2aad20Sopenharmony_ciBenefits of packaging from the mirror over packaging from kernel sources:
1537c2aad20Sopenharmony_ci  - Consistent versioning across distributions.
1547c2aad20Sopenharmony_ci  - No ties to any specific kernel, transparent handling of older kernels.
1557c2aad20Sopenharmony_ci    Libbpf is designed to be kernel-agnostic and work across multitude of
1567c2aad20Sopenharmony_ci    kernel versions. It has built-in mechanisms to gracefully handle older
1577c2aad20Sopenharmony_ci    kernels, that are missing some of the features, by working around or
1587c2aad20Sopenharmony_ci    gracefully degrading functionality. Thus libbpf is not tied to a specific
1597c2aad20Sopenharmony_ci    kernel version and can/should be packaged and versioned independently.
1607c2aad20Sopenharmony_ci  - Continuous integration testing via
1617c2aad20Sopenharmony_ci    [GitHub Actions](https://github.com/libbpf/libbpf/actions).
1627c2aad20Sopenharmony_ci  - Static code analysis via [LGTM](https://lgtm.com/projects/g/libbpf/libbpf)
1637c2aad20Sopenharmony_ci    and [Coverity](https://scan.coverity.com/projects/libbpf).
1647c2aad20Sopenharmony_ci
1657c2aad20Sopenharmony_ciPackage dependencies of libbpf, package names may vary across distros:
1667c2aad20Sopenharmony_ci  - zlib
1677c2aad20Sopenharmony_ci  - libelf
1687c2aad20Sopenharmony_ci
1697c2aad20Sopenharmony_ci[![libbpf distro packaging status](https://repology.org/badge/vertical-allrepos/libbpf.svg)](https://repology.org/project/libbpf/versions)
1707c2aad20Sopenharmony_ci
1717c2aad20Sopenharmony_ci
1727c2aad20Sopenharmony_cibpf-next to Github sync
1737c2aad20Sopenharmony_ci=======================
1747c2aad20Sopenharmony_ci
1757c2aad20Sopenharmony_ciAll the gory details of syncing can be found in `scripts/sync-kernel.sh`
1767c2aad20Sopenharmony_ciscript. See [SYNC.md](SYNC.md) for instruction.
1777c2aad20Sopenharmony_ci
1787c2aad20Sopenharmony_ciSome header files in this repo (`include/linux/*.h`) are reduced versions of
1797c2aad20Sopenharmony_citheir counterpart files at
1807c2aad20Sopenharmony_ci[bpf-next](https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/)'s
1817c2aad20Sopenharmony_ci`tools/include/linux/*.h` to make compilation successful.
1827c2aad20Sopenharmony_ci
1837c2aad20Sopenharmony_ciLicense
1847c2aad20Sopenharmony_ci=======
1857c2aad20Sopenharmony_ci
1867c2aad20Sopenharmony_ciThis work is dual-licensed under BSD 2-clause license and GNU LGPL v2.1 license.
1877c2aad20Sopenharmony_ciYou can choose between one of them if you use this work.
1887c2aad20Sopenharmony_ci
1897c2aad20Sopenharmony_ci`SPDX-License-Identifier: BSD-2-Clause OR LGPL-2.1`
190