Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf: Add documentation for libbpf including API autogen

This patch is meant to start the initiative to document libbpf.
It includes .rst files which are text documentation describing building,
API naming convention, as well as an index to generated API documentation.

In this approach the generated API documentation is enabled by the kernels
existing kernel documentation system which uses sphinx. The resulting docs
would then be synced to kernel.org/doc

You can test this by running `make htmldocs` and serving the html in
Documentation/output. Since libbpf does not yet have comments in kernel
doc format, see kernel.org/doc/html/latest/doc-guide/kernel-doc.html for
an example so you can test this.

The advantage of this approach is to use the existing sphinx
infrastructure that the kernel has, and have libbpf docs in
the same place as everything else.

The current plan is to have the libbpf mirror sync the generated docs
and version them based on the libbpf releases which are cut on github.

This patch includes the addition of libbpf_api.rst which pulls comment
documentation from header files in libbpf under tools/lib/bpf/. The comment
docs would be of the standard kernel doc format.

Signed-off-by: Grant Seltzer <grantseltzer@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20210618140459.9887-2-grantseltzer@gmail.com

authored by

Grant Seltzer and committed by
Daniel Borkmann
f42cfb46 7c6090ee

+103 -18
+13
Documentation/bpf/index.rst
··· 12 12 The Cilium project also maintains a `BPF and XDP Reference Guide`_ 13 13 that goes into great technical depth about the BPF Architecture. 14 14 15 + libbpf 16 + ====== 17 + 18 + Libbpf is a userspace library for loading and interacting with bpf programs. 19 + 20 + .. toctree:: 21 + :maxdepth: 1 22 + 23 + libbpf/libbpf 24 + libbpf/libbpf_api 25 + libbpf/libbpf_build 26 + libbpf/libbpf_naming_convention 27 + 15 28 BPF Type Format (BTF) 16 29 ===================== 17 30
+14
Documentation/bpf/libbpf/libbpf.rst
··· 1 + .. SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) 2 + 3 + libbpf 4 + ====== 5 + 6 + This is documentation for libbpf, a userspace library for loading and 7 + interacting with bpf programs. 8 + 9 + All general BPF questions, including kernel functionality, libbpf APIs and 10 + their application, should be sent to bpf@vger.kernel.org mailing list. 11 + You can `subscribe <http://vger.kernel.org/vger-lists.html#bpf>`_ to the 12 + mailing list search its `archive <https://lore.kernel.org/bpf/>`_. 13 + Please search the archive before asking new questions. It very well might 14 + be that this was already addressed or answered before.
+27
Documentation/bpf/libbpf/libbpf_api.rst
··· 1 + .. SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) 2 + 3 + API 4 + === 5 + 6 + This documentation is autogenerated from header files in libbpf, tools/lib/bpf 7 + 8 + .. kernel-doc:: tools/lib/bpf/libbpf.h 9 + :internal: 10 + 11 + .. kernel-doc:: tools/lib/bpf/bpf.h 12 + :internal: 13 + 14 + .. kernel-doc:: tools/lib/bpf/btf.h 15 + :internal: 16 + 17 + .. kernel-doc:: tools/lib/bpf/xsk.h 18 + :internal: 19 + 20 + .. kernel-doc:: tools/lib/bpf/bpf_tracing.h 21 + :internal: 22 + 23 + .. kernel-doc:: tools/lib/bpf/bpf_core_read.h 24 + :internal: 25 + 26 + .. kernel-doc:: tools/lib/bpf/bpf_endian.h 27 + :internal:
+37
Documentation/bpf/libbpf/libbpf_build.rst
··· 1 + .. SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) 2 + 3 + Building libbpf 4 + =============== 5 + 6 + libelf and zlib are internal dependencies of libbpf and thus are required to link 7 + against and must be installed on the system for applications to work. 8 + pkg-config is used by default to find libelf, and the program called 9 + can be overridden with PKG_CONFIG. 10 + 11 + If using pkg-config at build time is not desired, it can be disabled by 12 + setting NO_PKG_CONFIG=1 when calling make. 13 + 14 + To build both static libbpf.a and shared libbpf.so: 15 + 16 + .. code-block:: bash 17 + 18 + $ cd src 19 + $ make 20 + 21 + To build only static libbpf.a library in directory build/ and install them 22 + together with libbpf headers in a staging directory root/: 23 + 24 + .. code-block:: bash 25 + 26 + $ cd src 27 + $ mkdir build root 28 + $ BUILD_STATIC_ONLY=y OBJDIR=build DESTDIR=root make install 29 + 30 + To build both static libbpf.a and shared libbpf.so against a custom libelf 31 + dependency installed in /build/root/ and install them together with libbpf 32 + headers in a build directory /build/root/: 33 + 34 + .. code-block:: bash 35 + 36 + $ cd src 37 + $ PKG_CONFIG_PATH=/build/root/lib64/pkgconfig DESTDIR=/build/root make
+12 -18
tools/lib/bpf/README.rst Documentation/bpf/libbpf/libbpf_naming_convention.rst
··· 1 1 .. SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) 2 2 3 - libbpf API naming convention 4 - ============================ 3 + API naming convention 4 + ===================== 5 5 6 6 libbpf API provides access to a few logically separated groups of 7 7 functions and types. Every group has its own naming convention ··· 10 10 11 11 All types and functions provided by libbpf API should have one of the 12 12 following prefixes: ``bpf_``, ``btf_``, ``libbpf_``, ``xsk_``, 13 - ``perf_buffer_``. 13 + ``btf_dump_``, ``ring_buffer_``, ``perf_buffer_``. 14 14 15 15 System call wrappers 16 16 -------------------- 17 17 18 18 System call wrappers are simple wrappers for commands supported by 19 19 sys_bpf system call. These wrappers should go to ``bpf.h`` header file 20 - and map one-on-one to corresponding commands. 20 + and map one to one to corresponding commands. 21 21 22 22 For example ``bpf_map_lookup_elem`` wraps ``BPF_MAP_LOOKUP_ELEM`` 23 23 command of sys_bpf, ``bpf_prog_attach`` wraps ``BPF_PROG_ATTACH``, etc. ··· 49 49 purpose of the function to open ELF file and create ``bpf_object`` from 50 50 it. 51 51 52 - Another example: ``bpf_program__load`` is named for corresponding 53 - object, ``bpf_program``, that is separated from other part of the name 54 - by double underscore. 55 - 56 52 All objects and corresponding functions other than BTF related should go 57 53 to ``libbpf.h``. BTF types and functions should go to ``btf.h``. 58 54 ··· 68 72 functions. These can be mixed and matched. Note that these functions 69 73 are not reentrant for performance reasons. 70 74 71 - Please take a look at Documentation/networking/af_xdp.rst in the Linux 72 - kernel source tree on how to use XDP sockets and for some common 73 - mistakes in case you do not get any traffic up to user space. 74 - 75 - libbpf ABI 75 + ABI 76 76 ========== 77 77 78 78 libbpf can be both linked statically or used as DSO. To avoid possible ··· 108 116 109 117 For example, if current state of ``libbpf.map`` is: 110 118 111 - .. code-block:: 119 + .. code-block:: c 120 + 112 121 LIBBPF_0.0.1 { 113 122 global: 114 123 bpf_func_a; ··· 121 128 , and a new symbol ``bpf_func_c`` is being introduced, then 122 129 ``libbpf.map`` should be changed like this: 123 130 124 - .. code-block:: 131 + .. code-block:: c 132 + 125 133 LIBBPF_0.0.1 { 126 134 global: 127 135 bpf_func_a; ··· 142 148 incompatible ones, described in details in [1]. 143 149 144 150 Stand-alone build 145 - ================= 151 + ------------------- 146 152 147 153 Under https://github.com/libbpf/libbpf there is a (semi-)automated 148 154 mirror of the mainline's version of libbpf for a stand-alone build. ··· 151 157 the mainline kernel tree. 152 158 153 159 License 154 - ======= 160 + ------------------- 155 161 156 162 libbpf is dual-licensed under LGPL 2.1 and BSD 2-Clause. 157 163 158 164 Links 159 - ===== 165 + ------------------- 160 166 161 167 [1] https://www.akkadia.org/drepper/dsohowto.pdf 162 168 (Chapter 3. Maintaining APIs and ABIs).