0001 .. SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
0002
0003 API naming convention
0004 =====================
0005
0006 libbpf API provides access to a few logically separated groups of
0007 functions and types. Every group has its own naming convention
0008 described here. It's recommended to follow these conventions whenever a
0009 new function or type is added to keep libbpf API clean and consistent.
0010
0011 All types and functions provided by libbpf API should have one of the
0012 following prefixes: ``bpf_``, ``btf_``, ``libbpf_``, ``btf_dump_``,
0013 ``ring_buffer_``, ``perf_buffer_``.
0014
0015 System call wrappers
0016 --------------------
0017
0018 System call wrappers are simple wrappers for commands supported by
0019 sys_bpf system call. These wrappers should go to ``bpf.h`` header file
0020 and map one to one to corresponding commands.
0021
0022 For example ``bpf_map_lookup_elem`` wraps ``BPF_MAP_LOOKUP_ELEM``
0023 command of sys_bpf, ``bpf_prog_attach`` wraps ``BPF_PROG_ATTACH``, etc.
0024
0025 Objects
0026 -------
0027
0028 Another class of types and functions provided by libbpf API is "objects"
0029 and functions to work with them. Objects are high-level abstractions
0030 such as BPF program or BPF map. They're represented by corresponding
0031 structures such as ``struct bpf_object``, ``struct bpf_program``,
0032 ``struct bpf_map``, etc.
0033
0034 Structures are forward declared and access to their fields should be
0035 provided via corresponding getters and setters rather than directly.
0036
0037 These objects are associated with corresponding parts of ELF object that
0038 contains compiled BPF programs.
0039
0040 For example ``struct bpf_object`` represents ELF object itself created
0041 from an ELF file or from a buffer, ``struct bpf_program`` represents a
0042 program in ELF object and ``struct bpf_map`` is a map.
0043
0044 Functions that work with an object have names built from object name,
0045 double underscore and part that describes function purpose.
0046
0047 For example ``bpf_object__open`` consists of the name of corresponding
0048 object, ``bpf_object``, double underscore and ``open`` that defines the
0049 purpose of the function to open ELF file and create ``bpf_object`` from
0050 it.
0051
0052 All objects and corresponding functions other than BTF related should go
0053 to ``libbpf.h``. BTF types and functions should go to ``btf.h``.
0054
0055 Auxiliary functions
0056 -------------------
0057
0058 Auxiliary functions and types that don't fit well in any of categories
0059 described above should have ``libbpf_`` prefix, e.g.
0060 ``libbpf_get_error`` or ``libbpf_prog_type_by_name``.
0061
0062 ABI
0063 ---
0064
0065 libbpf can be both linked statically or used as DSO. To avoid possible
0066 conflicts with other libraries an application is linked with, all
0067 non-static libbpf symbols should have one of the prefixes mentioned in
0068 API documentation above. See API naming convention to choose the right
0069 name for a new symbol.
0070
0071 Symbol visibility
0072 -----------------
0073
0074 libbpf follow the model when all global symbols have visibility "hidden"
0075 by default and to make a symbol visible it has to be explicitly
0076 attributed with ``LIBBPF_API`` macro. For example:
0077
0078 .. code-block:: c
0079
0080 LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id);
0081
0082 This prevents from accidentally exporting a symbol, that is not supposed
0083 to be a part of ABI what, in turn, improves both libbpf developer- and
0084 user-experiences.
0085
0086 ABI versionning
0087 ---------------
0088
0089 To make future ABI extensions possible libbpf ABI is versioned.
0090 Versioning is implemented by ``libbpf.map`` version script that is
0091 passed to linker.
0092
0093 Version name is ``LIBBPF_`` prefix + three-component numeric version,
0094 starting from ``0.0.1``.
0095
0096 Every time ABI is being changed, e.g. because a new symbol is added or
0097 semantic of existing symbol is changed, ABI version should be bumped.
0098 This bump in ABI version is at most once per kernel development cycle.
0099
0100 For example, if current state of ``libbpf.map`` is:
0101
0102 .. code-block:: none
0103
0104 LIBBPF_0.0.1 {
0105 global:
0106 bpf_func_a;
0107 bpf_func_b;
0108 local:
0109 \*;
0110 };
0111
0112 , and a new symbol ``bpf_func_c`` is being introduced, then
0113 ``libbpf.map`` should be changed like this:
0114
0115 .. code-block:: none
0116
0117 LIBBPF_0.0.1 {
0118 global:
0119 bpf_func_a;
0120 bpf_func_b;
0121 local:
0122 \*;
0123 };
0124 LIBBPF_0.0.2 {
0125 global:
0126 bpf_func_c;
0127 } LIBBPF_0.0.1;
0128
0129 , where new version ``LIBBPF_0.0.2`` depends on the previous
0130 ``LIBBPF_0.0.1``.
0131
0132 Format of version script and ways to handle ABI changes, including
0133 incompatible ones, described in details in [1].
0134
0135 Stand-alone build
0136 -------------------
0137
0138 Under https://github.com/libbpf/libbpf there is a (semi-)automated
0139 mirror of the mainline's version of libbpf for a stand-alone build.
0140
0141 However, all changes to libbpf's code base must be upstreamed through
0142 the mainline kernel tree.
0143
0144
0145 API documentation convention
0146 ============================
0147
0148 The libbpf API is documented via comments above definitions in
0149 header files. These comments can be rendered by doxygen and sphinx
0150 for well organized html output. This section describes the
0151 convention in which these comments should be formated.
0152
0153 Here is an example from btf.h:
0154
0155 .. code-block:: c
0156
0157 /**
0158 * @brief **btf__new()** creates a new instance of a BTF object from the raw
0159 * bytes of an ELF's BTF section
0160 * @param data raw bytes
0161 * @param size number of bytes passed in `data`
0162 * @return new BTF object instance which has to be eventually freed with
0163 * **btf__free()**
0164 *
0165 * On error, error-code-encoded-as-pointer is returned, not a NULL. To extract
0166 * error code from such a pointer `libbpf_get_error()` should be used. If
0167 * `libbpf_set_strict_mode(LIBBPF_STRICT_CLEAN_PTRS)` is enabled, NULL is
0168 * returned on error instead. In both cases thread-local `errno` variable is
0169 * always set to error code as well.
0170 */
0171
0172 The comment must start with a block comment of the form '/\*\*'.
0173
0174 The documentation always starts with a @brief directive. This line is a short
0175 description about this API. It starts with the name of the API, denoted in bold
0176 like so: **api_name**. Please include an open and close parenthesis if this is a
0177 function. Follow with the short description of the API. A longer form description
0178 can be added below the last directive, at the bottom of the comment.
0179
0180 Parameters are denoted with the @param directive, there should be one for each
0181 parameter. If this is a function with a non-void return, use the @return directive
0182 to document it.
0183
0184 License
0185 -------------------
0186
0187 libbpf is dual-licensed under LGPL 2.1 and BSD 2-Clause.
0188
0189 Links
0190 -------------------
0191
0192 [1] https://www.akkadia.org/drepper/dsohowto.pdf
0193 (Chapter 3. Maintaining APIs and ABIs).