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