xref: /openbmc/linux/Documentation/bpf/libbpf/libbpf_naming_convention.rst (revision f42cfb469f9b4a1c002a03cce3d9329376800a6f)
1*f42cfb46SGrant Seltzer.. SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2*f42cfb46SGrant Seltzer
3*f42cfb46SGrant SeltzerAPI naming convention
4*f42cfb46SGrant Seltzer=====================
5*f42cfb46SGrant Seltzer
6*f42cfb46SGrant Seltzerlibbpf API provides access to a few logically separated groups of
7*f42cfb46SGrant Seltzerfunctions and types. Every group has its own naming convention
8*f42cfb46SGrant Seltzerdescribed here. It's recommended to follow these conventions whenever a
9*f42cfb46SGrant Seltzernew function or type is added to keep libbpf API clean and consistent.
10*f42cfb46SGrant Seltzer
11*f42cfb46SGrant SeltzerAll types and functions provided by libbpf API should have one of the
12*f42cfb46SGrant Seltzerfollowing prefixes: ``bpf_``, ``btf_``, ``libbpf_``, ``xsk_``,
13*f42cfb46SGrant Seltzer``btf_dump_``, ``ring_buffer_``, ``perf_buffer_``.
14*f42cfb46SGrant Seltzer
15*f42cfb46SGrant SeltzerSystem call wrappers
16*f42cfb46SGrant Seltzer--------------------
17*f42cfb46SGrant Seltzer
18*f42cfb46SGrant SeltzerSystem call wrappers are simple wrappers for commands supported by
19*f42cfb46SGrant Seltzersys_bpf system call. These wrappers should go to ``bpf.h`` header file
20*f42cfb46SGrant Seltzerand map one to one to corresponding commands.
21*f42cfb46SGrant Seltzer
22*f42cfb46SGrant SeltzerFor example ``bpf_map_lookup_elem`` wraps ``BPF_MAP_LOOKUP_ELEM``
23*f42cfb46SGrant Seltzercommand of sys_bpf, ``bpf_prog_attach`` wraps ``BPF_PROG_ATTACH``, etc.
24*f42cfb46SGrant Seltzer
25*f42cfb46SGrant SeltzerObjects
26*f42cfb46SGrant Seltzer-------
27*f42cfb46SGrant Seltzer
28*f42cfb46SGrant SeltzerAnother class of types and functions provided by libbpf API is "objects"
29*f42cfb46SGrant Seltzerand functions to work with them. Objects are high-level abstractions
30*f42cfb46SGrant Seltzersuch as BPF program or BPF map. They're represented by corresponding
31*f42cfb46SGrant Seltzerstructures such as ``struct bpf_object``, ``struct bpf_program``,
32*f42cfb46SGrant Seltzer``struct bpf_map``, etc.
33*f42cfb46SGrant Seltzer
34*f42cfb46SGrant SeltzerStructures are forward declared and access to their fields should be
35*f42cfb46SGrant Seltzerprovided via corresponding getters and setters rather than directly.
36*f42cfb46SGrant Seltzer
37*f42cfb46SGrant SeltzerThese objects are associated with corresponding parts of ELF object that
38*f42cfb46SGrant Seltzercontains compiled BPF programs.
39*f42cfb46SGrant Seltzer
40*f42cfb46SGrant SeltzerFor example ``struct bpf_object`` represents ELF object itself created
41*f42cfb46SGrant Seltzerfrom an ELF file or from a buffer, ``struct bpf_program`` represents a
42*f42cfb46SGrant Seltzerprogram in ELF object and ``struct bpf_map`` is a map.
43*f42cfb46SGrant Seltzer
44*f42cfb46SGrant SeltzerFunctions that work with an object have names built from object name,
45*f42cfb46SGrant Seltzerdouble underscore and part that describes function purpose.
46*f42cfb46SGrant Seltzer
47*f42cfb46SGrant SeltzerFor example ``bpf_object__open`` consists of the name of corresponding
48*f42cfb46SGrant Seltzerobject, ``bpf_object``, double underscore and ``open`` that defines the
49*f42cfb46SGrant Seltzerpurpose of the function to open ELF file and create ``bpf_object`` from
50*f42cfb46SGrant Seltzerit.
51*f42cfb46SGrant Seltzer
52*f42cfb46SGrant SeltzerAll objects and corresponding functions other than BTF related should go
53*f42cfb46SGrant Seltzerto ``libbpf.h``. BTF types and functions should go to ``btf.h``.
54*f42cfb46SGrant Seltzer
55*f42cfb46SGrant SeltzerAuxiliary functions
56*f42cfb46SGrant Seltzer-------------------
57*f42cfb46SGrant Seltzer
58*f42cfb46SGrant SeltzerAuxiliary functions and types that don't fit well in any of categories
59*f42cfb46SGrant Seltzerdescribed above should have ``libbpf_`` prefix, e.g.
60*f42cfb46SGrant Seltzer``libbpf_get_error`` or ``libbpf_prog_type_by_name``.
61*f42cfb46SGrant Seltzer
62*f42cfb46SGrant SeltzerAF_XDP functions
63*f42cfb46SGrant Seltzer-------------------
64*f42cfb46SGrant Seltzer
65*f42cfb46SGrant SeltzerAF_XDP functions should have an ``xsk_`` prefix, e.g.
66*f42cfb46SGrant Seltzer``xsk_umem__get_data`` or ``xsk_umem__create``. The interface consists
67*f42cfb46SGrant Seltzerof both low-level ring access functions and high-level configuration
68*f42cfb46SGrant Seltzerfunctions. These can be mixed and matched. Note that these functions
69*f42cfb46SGrant Seltzerare not reentrant for performance reasons.
70*f42cfb46SGrant Seltzer
71*f42cfb46SGrant SeltzerABI
72*f42cfb46SGrant Seltzer==========
73*f42cfb46SGrant Seltzer
74*f42cfb46SGrant Seltzerlibbpf can be both linked statically or used as DSO. To avoid possible
75*f42cfb46SGrant Seltzerconflicts with other libraries an application is linked with, all
76*f42cfb46SGrant Seltzernon-static libbpf symbols should have one of the prefixes mentioned in
77*f42cfb46SGrant SeltzerAPI documentation above. See API naming convention to choose the right
78*f42cfb46SGrant Seltzername for a new symbol.
79*f42cfb46SGrant Seltzer
80*f42cfb46SGrant SeltzerSymbol visibility
81*f42cfb46SGrant Seltzer-----------------
82*f42cfb46SGrant Seltzer
83*f42cfb46SGrant Seltzerlibbpf follow the model when all global symbols have visibility "hidden"
84*f42cfb46SGrant Seltzerby default and to make a symbol visible it has to be explicitly
85*f42cfb46SGrant Seltzerattributed with ``LIBBPF_API`` macro. For example:
86*f42cfb46SGrant Seltzer
87*f42cfb46SGrant Seltzer.. code-block:: c
88*f42cfb46SGrant Seltzer
89*f42cfb46SGrant Seltzer        LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id);
90*f42cfb46SGrant Seltzer
91*f42cfb46SGrant SeltzerThis prevents from accidentally exporting a symbol, that is not supposed
92*f42cfb46SGrant Seltzerto be a part of ABI what, in turn, improves both libbpf developer- and
93*f42cfb46SGrant Seltzeruser-experiences.
94*f42cfb46SGrant Seltzer
95*f42cfb46SGrant SeltzerABI versionning
96*f42cfb46SGrant Seltzer---------------
97*f42cfb46SGrant Seltzer
98*f42cfb46SGrant SeltzerTo make future ABI extensions possible libbpf ABI is versioned.
99*f42cfb46SGrant SeltzerVersioning is implemented by ``libbpf.map`` version script that is
100*f42cfb46SGrant Seltzerpassed to linker.
101*f42cfb46SGrant Seltzer
102*f42cfb46SGrant SeltzerVersion name is ``LIBBPF_`` prefix + three-component numeric version,
103*f42cfb46SGrant Seltzerstarting from ``0.0.1``.
104*f42cfb46SGrant Seltzer
105*f42cfb46SGrant SeltzerEvery time ABI is being changed, e.g. because a new symbol is added or
106*f42cfb46SGrant Seltzersemantic of existing symbol is changed, ABI version should be bumped.
107*f42cfb46SGrant SeltzerThis bump in ABI version is at most once per kernel development cycle.
108*f42cfb46SGrant Seltzer
109*f42cfb46SGrant SeltzerFor example, if current state of ``libbpf.map`` is:
110*f42cfb46SGrant Seltzer
111*f42cfb46SGrant Seltzer.. code-block:: c
112*f42cfb46SGrant Seltzer
113*f42cfb46SGrant Seltzer        LIBBPF_0.0.1 {
114*f42cfb46SGrant Seltzer        	global:
115*f42cfb46SGrant Seltzer                        bpf_func_a;
116*f42cfb46SGrant Seltzer                        bpf_func_b;
117*f42cfb46SGrant Seltzer        	local:
118*f42cfb46SGrant Seltzer        		\*;
119*f42cfb46SGrant Seltzer        };
120*f42cfb46SGrant Seltzer
121*f42cfb46SGrant Seltzer, and a new symbol ``bpf_func_c`` is being introduced, then
122*f42cfb46SGrant Seltzer``libbpf.map`` should be changed like this:
123*f42cfb46SGrant Seltzer
124*f42cfb46SGrant Seltzer.. code-block:: c
125*f42cfb46SGrant Seltzer
126*f42cfb46SGrant Seltzer        LIBBPF_0.0.1 {
127*f42cfb46SGrant Seltzer        	global:
128*f42cfb46SGrant Seltzer                        bpf_func_a;
129*f42cfb46SGrant Seltzer                        bpf_func_b;
130*f42cfb46SGrant Seltzer        	local:
131*f42cfb46SGrant Seltzer        		\*;
132*f42cfb46SGrant Seltzer        };
133*f42cfb46SGrant Seltzer        LIBBPF_0.0.2 {
134*f42cfb46SGrant Seltzer                global:
135*f42cfb46SGrant Seltzer                        bpf_func_c;
136*f42cfb46SGrant Seltzer        } LIBBPF_0.0.1;
137*f42cfb46SGrant Seltzer
138*f42cfb46SGrant Seltzer, where new version ``LIBBPF_0.0.2`` depends on the previous
139*f42cfb46SGrant Seltzer``LIBBPF_0.0.1``.
140*f42cfb46SGrant Seltzer
141*f42cfb46SGrant SeltzerFormat of version script and ways to handle ABI changes, including
142*f42cfb46SGrant Seltzerincompatible ones, described in details in [1].
143*f42cfb46SGrant Seltzer
144*f42cfb46SGrant SeltzerStand-alone build
145*f42cfb46SGrant Seltzer-------------------
146*f42cfb46SGrant Seltzer
147*f42cfb46SGrant SeltzerUnder https://github.com/libbpf/libbpf there is a (semi-)automated
148*f42cfb46SGrant Seltzermirror of the mainline's version of libbpf for a stand-alone build.
149*f42cfb46SGrant Seltzer
150*f42cfb46SGrant SeltzerHowever, all changes to libbpf's code base must be upstreamed through
151*f42cfb46SGrant Seltzerthe mainline kernel tree.
152*f42cfb46SGrant Seltzer
153*f42cfb46SGrant SeltzerLicense
154*f42cfb46SGrant Seltzer-------------------
155*f42cfb46SGrant Seltzer
156*f42cfb46SGrant Seltzerlibbpf is dual-licensed under LGPL 2.1 and BSD 2-Clause.
157*f42cfb46SGrant Seltzer
158*f42cfb46SGrant SeltzerLinks
159*f42cfb46SGrant Seltzer-------------------
160*f42cfb46SGrant Seltzer
161*f42cfb46SGrant Seltzer[1] https://www.akkadia.org/drepper/dsohowto.pdf
162*f42cfb46SGrant Seltzer    (Chapter 3. Maintaining APIs and ABIs).
163