• Home
Name Date Size #Lines LOC

..--

.gitignoreD08-May-2024137 109

BuildD08-May-2024163 43

MakefileD08-May-202410.4 KiB309222

README.rstD08-May-20245.5 KiB169124

bpf.cD08-May-202424 KiB934728

bpf.hD08-May-20249.9 KiB293224

bpf_core_read.hD08-May-202414.1 KiB354169

bpf_endian.hD08-May-20243.7 KiB10068

bpf_helpers.hD08-May-20244 KiB13766

bpf_prog_linfo.cD08-May-20246 KiB247179

bpf_tracing.hD08-May-202418.5 KiB451361

btf.cD08-May-2024114.6 KiB4,4852,975

btf.hD08-May-202410.9 KiB362271

btf_dump.cD08-May-202443.1 KiB1,532982

hashmap.cD08-May-20245 KiB242186

hashmap.hD08-May-20245.8 KiB196119

libbpf.cD08-May-2024282.3 KiB10,9538,553

libbpf.hD08-May-202427.7 KiB767481

libbpf.mapD08-May-20247.3 KiB340329

libbpf.pc.templateD08-May-2024252 1310

libbpf_common.hD08-May-20241.4 KiB4316

libbpf_errno.cD08-May-20241.9 KiB6745

libbpf_internal.hD08-May-202410.3 KiB322188

libbpf_probes.cD08-May-20248.6 KiB360291

libbpf_util.hD08-May-20241.6 KiB4832

netlink.cD08-May-20248.5 KiB373299

nlattr.cD08-May-20244.9 KiB196119

nlattr.hD08-May-20242.7 KiB10754

ringbuf.cD08-May-20246.6 KiB301230

str_error.cD08-May-2024636 2212

str_error.hD08-May-2024205 74

xsk.cD08-May-202421.7 KiB960751

xsk.hD08-May-20246.4 KiB256175

README.rst

1.. SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2
3libbpf API naming convention
4============================
5
6libbpf API provides access to a few logically separated groups of
7functions and types. Every group has its own naming convention
8described here. It's recommended to follow these conventions whenever a
9new function or type is added to keep libbpf API clean and consistent.
10
11All types and functions provided by libbpf API should have one of the
12following prefixes: ``bpf_``, ``btf_``, ``libbpf_``, ``xsk_``,
13``perf_buffer_``.
14
15System call wrappers
16--------------------
17
18System call wrappers are simple wrappers for commands supported by
19sys_bpf system call. These wrappers should go to ``bpf.h`` header file
20and map one-on-one to corresponding commands.
21
22For example ``bpf_map_lookup_elem`` wraps ``BPF_MAP_LOOKUP_ELEM``
23command of sys_bpf, ``bpf_prog_attach`` wraps ``BPF_PROG_ATTACH``, etc.
24
25Objects
26-------
27
28Another class of types and functions provided by libbpf API is "objects"
29and functions to work with them. Objects are high-level abstractions
30such as BPF program or BPF map. They're represented by corresponding
31structures such as ``struct bpf_object``, ``struct bpf_program``,
32``struct bpf_map``, etc.
33
34Structures are forward declared and access to their fields should be
35provided via corresponding getters and setters rather than directly.
36
37These objects are associated with corresponding parts of ELF object that
38contains compiled BPF programs.
39
40For example ``struct bpf_object`` represents ELF object itself created
41from an ELF file or from a buffer, ``struct bpf_program`` represents a
42program in ELF object and ``struct bpf_map`` is a map.
43
44Functions that work with an object have names built from object name,
45double underscore and part that describes function purpose.
46
47For example ``bpf_object__open`` consists of the name of corresponding
48object, ``bpf_object``, double underscore and ``open`` that defines the
49purpose of the function to open ELF file and create ``bpf_object`` from
50it.
51
52Another example: ``bpf_program__load`` is named for corresponding
53object, ``bpf_program``, that is separated from other part of the name
54by double underscore.
55
56All objects and corresponding functions other than BTF related should go
57to ``libbpf.h``. BTF types and functions should go to ``btf.h``.
58
59Auxiliary functions
60-------------------
61
62Auxiliary functions and types that don't fit well in any of categories
63described above should have ``libbpf_`` prefix, e.g.
64``libbpf_get_error`` or ``libbpf_prog_type_by_name``.
65
66AF_XDP functions
67-------------------
68
69AF_XDP functions should have an ``xsk_`` prefix, e.g.
70``xsk_umem__get_data`` or ``xsk_umem__create``. The interface consists
71of both low-level ring access functions and high-level configuration
72functions. These can be mixed and matched. Note that these functions
73are not reentrant for performance reasons.
74
75Please take a look at Documentation/networking/af_xdp.rst in the Linux
76kernel source tree on how to use XDP sockets and for some common
77mistakes in case you do not get any traffic up to user space.
78
79libbpf ABI
80==========
81
82libbpf can be both linked statically or used as DSO. To avoid possible
83conflicts with other libraries an application is linked with, all
84non-static libbpf symbols should have one of the prefixes mentioned in
85API documentation above. See API naming convention to choose the right
86name for a new symbol.
87
88Symbol visibility
89-----------------
90
91libbpf follow the model when all global symbols have visibility "hidden"
92by default and to make a symbol visible it has to be explicitly
93attributed with ``LIBBPF_API`` macro. For example:
94
95.. code-block:: c
96
97        LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id);
98
99This prevents from accidentally exporting a symbol, that is not supposed
100to be a part of ABI what, in turn, improves both libbpf developer- and
101user-experiences.
102
103ABI versionning
104---------------
105
106To make future ABI extensions possible libbpf ABI is versioned.
107Versioning is implemented by ``libbpf.map`` version script that is
108passed to linker.
109
110Version name is ``LIBBPF_`` prefix + three-component numeric version,
111starting from ``0.0.1``.
112
113Every time ABI is being changed, e.g. because a new symbol is added or
114semantic of existing symbol is changed, ABI version should be bumped.
115This bump in ABI version is at most once per kernel development cycle.
116
117For example, if current state of ``libbpf.map`` is:
118
119.. code-block::
120        LIBBPF_0.0.1 {
121        	global:
122                        bpf_func_a;
123                        bpf_func_b;
124        	local:
125        		\*;
126        };
127
128, and a new symbol ``bpf_func_c`` is being introduced, then
129``libbpf.map`` should be changed like this:
130
131.. code-block::
132        LIBBPF_0.0.1 {
133        	global:
134                        bpf_func_a;
135                        bpf_func_b;
136        	local:
137        		\*;
138        };
139        LIBBPF_0.0.2 {
140                global:
141                        bpf_func_c;
142        } LIBBPF_0.0.1;
143
144, where new version ``LIBBPF_0.0.2`` depends on the previous
145``LIBBPF_0.0.1``.
146
147Format of version script and ways to handle ABI changes, including
148incompatible ones, described in details in [1].
149
150Stand-alone build
151=================
152
153Under https://github.com/libbpf/libbpf there is a (semi-)automated
154mirror of the mainline's version of libbpf for a stand-alone build.
155
156However, all changes to libbpf's code base must be upstreamed through
157the mainline kernel tree.
158
159License
160=======
161
162libbpf is dual-licensed under LGPL 2.1 and BSD 2-Clause.
163
164Links
165=====
166
167[1] https://www.akkadia.org/drepper/dsohowto.pdf
168    (Chapter 3. Maintaining APIs and ABIs).
169