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