1*f7c14bbaSAndroid Build Coastguard Worker.. SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) 2*f7c14bbaSAndroid Build Coastguard Worker 3*f7c14bbaSAndroid Build Coastguard WorkerAPI naming convention 4*f7c14bbaSAndroid Build Coastguard Worker===================== 5*f7c14bbaSAndroid Build Coastguard Worker 6*f7c14bbaSAndroid Build Coastguard Workerlibbpf API provides access to a few logically separated groups of 7*f7c14bbaSAndroid Build Coastguard Workerfunctions and types. Every group has its own naming convention 8*f7c14bbaSAndroid Build Coastguard Workerdescribed here. It's recommended to follow these conventions whenever a 9*f7c14bbaSAndroid Build Coastguard Workernew function or type is added to keep libbpf API clean and consistent. 10*f7c14bbaSAndroid Build Coastguard Worker 11*f7c14bbaSAndroid Build Coastguard WorkerAll types and functions provided by libbpf API should have one of the 12*f7c14bbaSAndroid Build Coastguard Workerfollowing prefixes: ``bpf_``, ``btf_``, ``libbpf_``, ``btf_dump_``, 13*f7c14bbaSAndroid Build Coastguard Worker``ring_buffer_``, ``perf_buffer_``. 14*f7c14bbaSAndroid Build Coastguard Worker 15*f7c14bbaSAndroid Build Coastguard WorkerSystem call wrappers 16*f7c14bbaSAndroid Build Coastguard Worker-------------------- 17*f7c14bbaSAndroid Build Coastguard Worker 18*f7c14bbaSAndroid Build Coastguard WorkerSystem call wrappers are simple wrappers for commands supported by 19*f7c14bbaSAndroid Build Coastguard Workersys_bpf system call. These wrappers should go to ``bpf.h`` header file 20*f7c14bbaSAndroid Build Coastguard Workerand map one to one to corresponding commands. 21*f7c14bbaSAndroid Build Coastguard Worker 22*f7c14bbaSAndroid Build Coastguard WorkerFor example ``bpf_map_lookup_elem`` wraps ``BPF_MAP_LOOKUP_ELEM`` 23*f7c14bbaSAndroid Build Coastguard Workercommand of sys_bpf, ``bpf_prog_attach`` wraps ``BPF_PROG_ATTACH``, etc. 24*f7c14bbaSAndroid Build Coastguard Worker 25*f7c14bbaSAndroid Build Coastguard WorkerObjects 26*f7c14bbaSAndroid Build Coastguard Worker------- 27*f7c14bbaSAndroid Build Coastguard Worker 28*f7c14bbaSAndroid Build Coastguard WorkerAnother class of types and functions provided by libbpf API is "objects" 29*f7c14bbaSAndroid Build Coastguard Workerand functions to work with them. Objects are high-level abstractions 30*f7c14bbaSAndroid Build Coastguard Workersuch as BPF program or BPF map. They're represented by corresponding 31*f7c14bbaSAndroid Build Coastguard Workerstructures such as ``struct bpf_object``, ``struct bpf_program``, 32*f7c14bbaSAndroid Build Coastguard Worker``struct bpf_map``, etc. 33*f7c14bbaSAndroid Build Coastguard Worker 34*f7c14bbaSAndroid Build Coastguard WorkerStructures are forward declared and access to their fields should be 35*f7c14bbaSAndroid Build Coastguard Workerprovided via corresponding getters and setters rather than directly. 36*f7c14bbaSAndroid Build Coastguard Worker 37*f7c14bbaSAndroid Build Coastguard WorkerThese objects are associated with corresponding parts of ELF object that 38*f7c14bbaSAndroid Build Coastguard Workercontains compiled BPF programs. 39*f7c14bbaSAndroid Build Coastguard Worker 40*f7c14bbaSAndroid Build Coastguard WorkerFor example ``struct bpf_object`` represents ELF object itself created 41*f7c14bbaSAndroid Build Coastguard Workerfrom an ELF file or from a buffer, ``struct bpf_program`` represents a 42*f7c14bbaSAndroid Build Coastguard Workerprogram in ELF object and ``struct bpf_map`` is a map. 43*f7c14bbaSAndroid Build Coastguard Worker 44*f7c14bbaSAndroid Build Coastguard WorkerFunctions that work with an object have names built from object name, 45*f7c14bbaSAndroid Build Coastguard Workerdouble underscore and part that describes function purpose. 46*f7c14bbaSAndroid Build Coastguard Worker 47*f7c14bbaSAndroid Build Coastguard WorkerFor example ``bpf_object__open`` consists of the name of corresponding 48*f7c14bbaSAndroid Build Coastguard Workerobject, ``bpf_object``, double underscore and ``open`` that defines the 49*f7c14bbaSAndroid Build Coastguard Workerpurpose of the function to open ELF file and create ``bpf_object`` from 50*f7c14bbaSAndroid Build Coastguard Workerit. 51*f7c14bbaSAndroid Build Coastguard Worker 52*f7c14bbaSAndroid Build Coastguard WorkerAll objects and corresponding functions other than BTF related should go 53*f7c14bbaSAndroid Build Coastguard Workerto ``libbpf.h``. BTF types and functions should go to ``btf.h``. 54*f7c14bbaSAndroid Build Coastguard Worker 55*f7c14bbaSAndroid Build Coastguard WorkerAuxiliary functions 56*f7c14bbaSAndroid Build Coastguard Worker------------------- 57*f7c14bbaSAndroid Build Coastguard Worker 58*f7c14bbaSAndroid Build Coastguard WorkerAuxiliary functions and types that don't fit well in any of categories 59*f7c14bbaSAndroid Build Coastguard Workerdescribed above should have ``libbpf_`` prefix, e.g. 60*f7c14bbaSAndroid Build Coastguard Worker``libbpf_get_error`` or ``libbpf_prog_type_by_name``. 61*f7c14bbaSAndroid Build Coastguard Worker 62*f7c14bbaSAndroid Build Coastguard WorkerABI 63*f7c14bbaSAndroid Build Coastguard Worker--- 64*f7c14bbaSAndroid Build Coastguard Worker 65*f7c14bbaSAndroid Build Coastguard Workerlibbpf can be both linked statically or used as DSO. To avoid possible 66*f7c14bbaSAndroid Build Coastguard Workerconflicts with other libraries an application is linked with, all 67*f7c14bbaSAndroid Build Coastguard Workernon-static libbpf symbols should have one of the prefixes mentioned in 68*f7c14bbaSAndroid Build Coastguard WorkerAPI documentation above. See API naming convention to choose the right 69*f7c14bbaSAndroid Build Coastguard Workername for a new symbol. 70*f7c14bbaSAndroid Build Coastguard Worker 71*f7c14bbaSAndroid Build Coastguard WorkerSymbol visibility 72*f7c14bbaSAndroid Build Coastguard Worker----------------- 73*f7c14bbaSAndroid Build Coastguard Worker 74*f7c14bbaSAndroid Build Coastguard Workerlibbpf follow the model when all global symbols have visibility "hidden" 75*f7c14bbaSAndroid Build Coastguard Workerby default and to make a symbol visible it has to be explicitly 76*f7c14bbaSAndroid Build Coastguard Workerattributed with ``LIBBPF_API`` macro. For example: 77*f7c14bbaSAndroid Build Coastguard Worker 78*f7c14bbaSAndroid Build Coastguard Worker.. code-block:: c 79*f7c14bbaSAndroid Build Coastguard Worker 80*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id); 81*f7c14bbaSAndroid Build Coastguard Worker 82*f7c14bbaSAndroid Build Coastguard WorkerThis prevents from accidentally exporting a symbol, that is not supposed 83*f7c14bbaSAndroid Build Coastguard Workerto be a part of ABI what, in turn, improves both libbpf developer- and 84*f7c14bbaSAndroid Build Coastguard Workeruser-experiences. 85*f7c14bbaSAndroid Build Coastguard Worker 86*f7c14bbaSAndroid Build Coastguard WorkerABI versioning 87*f7c14bbaSAndroid Build Coastguard Worker-------------- 88*f7c14bbaSAndroid Build Coastguard Worker 89*f7c14bbaSAndroid Build Coastguard WorkerTo make future ABI extensions possible libbpf ABI is versioned. 90*f7c14bbaSAndroid Build Coastguard WorkerVersioning is implemented by ``libbpf.map`` version script that is 91*f7c14bbaSAndroid Build Coastguard Workerpassed to linker. 92*f7c14bbaSAndroid Build Coastguard Worker 93*f7c14bbaSAndroid Build Coastguard WorkerVersion name is ``LIBBPF_`` prefix + three-component numeric version, 94*f7c14bbaSAndroid Build Coastguard Workerstarting from ``0.0.1``. 95*f7c14bbaSAndroid Build Coastguard Worker 96*f7c14bbaSAndroid Build Coastguard WorkerEvery time ABI is being changed, e.g. because a new symbol is added or 97*f7c14bbaSAndroid Build Coastguard Workersemantic of existing symbol is changed, ABI version should be bumped. 98*f7c14bbaSAndroid Build Coastguard WorkerThis bump in ABI version is at most once per kernel development cycle. 99*f7c14bbaSAndroid Build Coastguard Worker 100*f7c14bbaSAndroid Build Coastguard WorkerFor example, if current state of ``libbpf.map`` is: 101*f7c14bbaSAndroid Build Coastguard Worker 102*f7c14bbaSAndroid Build Coastguard Worker.. code-block:: none 103*f7c14bbaSAndroid Build Coastguard Worker 104*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_0.0.1 { 105*f7c14bbaSAndroid Build Coastguard Worker global: 106*f7c14bbaSAndroid Build Coastguard Worker bpf_func_a; 107*f7c14bbaSAndroid Build Coastguard Worker bpf_func_b; 108*f7c14bbaSAndroid Build Coastguard Worker local: 109*f7c14bbaSAndroid Build Coastguard Worker \*; 110*f7c14bbaSAndroid Build Coastguard Worker }; 111*f7c14bbaSAndroid Build Coastguard Worker 112*f7c14bbaSAndroid Build Coastguard Worker, and a new symbol ``bpf_func_c`` is being introduced, then 113*f7c14bbaSAndroid Build Coastguard Worker``libbpf.map`` should be changed like this: 114*f7c14bbaSAndroid Build Coastguard Worker 115*f7c14bbaSAndroid Build Coastguard Worker.. code-block:: none 116*f7c14bbaSAndroid Build Coastguard Worker 117*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_0.0.1 { 118*f7c14bbaSAndroid Build Coastguard Worker global: 119*f7c14bbaSAndroid Build Coastguard Worker bpf_func_a; 120*f7c14bbaSAndroid Build Coastguard Worker bpf_func_b; 121*f7c14bbaSAndroid Build Coastguard Worker local: 122*f7c14bbaSAndroid Build Coastguard Worker \*; 123*f7c14bbaSAndroid Build Coastguard Worker }; 124*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_0.0.2 { 125*f7c14bbaSAndroid Build Coastguard Worker global: 126*f7c14bbaSAndroid Build Coastguard Worker bpf_func_c; 127*f7c14bbaSAndroid Build Coastguard Worker } LIBBPF_0.0.1; 128*f7c14bbaSAndroid Build Coastguard Worker 129*f7c14bbaSAndroid Build Coastguard Worker, where new version ``LIBBPF_0.0.2`` depends on the previous 130*f7c14bbaSAndroid Build Coastguard Worker``LIBBPF_0.0.1``. 131*f7c14bbaSAndroid Build Coastguard Worker 132*f7c14bbaSAndroid Build Coastguard WorkerFormat of version script and ways to handle ABI changes, including 133*f7c14bbaSAndroid Build Coastguard Workerincompatible ones, described in details in [1]. 134*f7c14bbaSAndroid Build Coastguard Worker 135*f7c14bbaSAndroid Build Coastguard WorkerStand-alone build 136*f7c14bbaSAndroid Build Coastguard Worker------------------- 137*f7c14bbaSAndroid Build Coastguard Worker 138*f7c14bbaSAndroid Build Coastguard WorkerUnder https://github.com/libbpf/libbpf there is a (semi-)automated 139*f7c14bbaSAndroid Build Coastguard Workermirror of the mainline's version of libbpf for a stand-alone build. 140*f7c14bbaSAndroid Build Coastguard Worker 141*f7c14bbaSAndroid Build Coastguard WorkerHowever, all changes to libbpf's code base must be upstreamed through 142*f7c14bbaSAndroid Build Coastguard Workerthe mainline kernel tree. 143*f7c14bbaSAndroid Build Coastguard Worker 144*f7c14bbaSAndroid Build Coastguard Worker 145*f7c14bbaSAndroid Build Coastguard WorkerAPI documentation convention 146*f7c14bbaSAndroid Build Coastguard Worker============================ 147*f7c14bbaSAndroid Build Coastguard Worker 148*f7c14bbaSAndroid Build Coastguard WorkerThe libbpf API is documented via comments above definitions in 149*f7c14bbaSAndroid Build Coastguard Workerheader files. These comments can be rendered by doxygen and sphinx 150*f7c14bbaSAndroid Build Coastguard Workerfor well organized html output. This section describes the 151*f7c14bbaSAndroid Build Coastguard Workerconvention in which these comments should be formatted. 152*f7c14bbaSAndroid Build Coastguard Worker 153*f7c14bbaSAndroid Build Coastguard WorkerHere is an example from btf.h: 154*f7c14bbaSAndroid Build Coastguard Worker 155*f7c14bbaSAndroid Build Coastguard Worker.. code-block:: c 156*f7c14bbaSAndroid Build Coastguard Worker 157*f7c14bbaSAndroid Build Coastguard Worker /** 158*f7c14bbaSAndroid Build Coastguard Worker * @brief **btf__new()** creates a new instance of a BTF object from the raw 159*f7c14bbaSAndroid Build Coastguard Worker * bytes of an ELF's BTF section 160*f7c14bbaSAndroid Build Coastguard Worker * @param data raw bytes 161*f7c14bbaSAndroid Build Coastguard Worker * @param size number of bytes passed in `data` 162*f7c14bbaSAndroid Build Coastguard Worker * @return new BTF object instance which has to be eventually freed with 163*f7c14bbaSAndroid Build Coastguard Worker * **btf__free()** 164*f7c14bbaSAndroid Build Coastguard Worker * 165*f7c14bbaSAndroid Build Coastguard Worker * On error, error-code-encoded-as-pointer is returned, not a NULL. To extract 166*f7c14bbaSAndroid Build Coastguard Worker * error code from such a pointer `libbpf_get_error()` should be used. If 167*f7c14bbaSAndroid Build Coastguard Worker * `libbpf_set_strict_mode(LIBBPF_STRICT_CLEAN_PTRS)` is enabled, NULL is 168*f7c14bbaSAndroid Build Coastguard Worker * returned on error instead. In both cases thread-local `errno` variable is 169*f7c14bbaSAndroid Build Coastguard Worker * always set to error code as well. 170*f7c14bbaSAndroid Build Coastguard Worker */ 171*f7c14bbaSAndroid Build Coastguard Worker 172*f7c14bbaSAndroid Build Coastguard WorkerThe comment must start with a block comment of the form '/\*\*'. 173*f7c14bbaSAndroid Build Coastguard Worker 174*f7c14bbaSAndroid Build Coastguard WorkerThe documentation always starts with a @brief directive. This line is a short 175*f7c14bbaSAndroid Build Coastguard Workerdescription about this API. It starts with the name of the API, denoted in bold 176*f7c14bbaSAndroid Build Coastguard Workerlike so: **api_name**. Please include an open and close parenthesis if this is a 177*f7c14bbaSAndroid Build Coastguard Workerfunction. Follow with the short description of the API. A longer form description 178*f7c14bbaSAndroid Build Coastguard Workercan be added below the last directive, at the bottom of the comment. 179*f7c14bbaSAndroid Build Coastguard Worker 180*f7c14bbaSAndroid Build Coastguard WorkerParameters are denoted with the @param directive, there should be one for each 181*f7c14bbaSAndroid Build Coastguard Workerparameter. If this is a function with a non-void return, use the @return directive 182*f7c14bbaSAndroid Build Coastguard Workerto document it. 183*f7c14bbaSAndroid Build Coastguard Worker 184*f7c14bbaSAndroid Build Coastguard WorkerLicense 185*f7c14bbaSAndroid Build Coastguard Worker------------------- 186*f7c14bbaSAndroid Build Coastguard Worker 187*f7c14bbaSAndroid Build Coastguard Workerlibbpf is dual-licensed under LGPL 2.1 and BSD 2-Clause. 188*f7c14bbaSAndroid Build Coastguard Worker 189*f7c14bbaSAndroid Build Coastguard WorkerLinks 190*f7c14bbaSAndroid Build Coastguard Worker------------------- 191*f7c14bbaSAndroid Build Coastguard Worker 192*f7c14bbaSAndroid Build Coastguard Worker[1] https://www.akkadia.org/drepper/dsohowto.pdf 193*f7c14bbaSAndroid Build Coastguard Worker (Chapter 3. Maintaining APIs and ABIs). 194