libbpf_naming_convention.rst 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. .. SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
  2. API naming convention
  3. =====================
  4. libbpf API provides access to a few logically separated groups of
  5. functions and types. Every group has its own naming convention
  6. described here. It's recommended to follow these conventions whenever a
  7. new function or type is added to keep libbpf API clean and consistent.
  8. All types and functions provided by libbpf API should have one of the
  9. following prefixes: ``bpf_``, ``btf_``, ``libbpf_``, ``btf_dump_``,
  10. ``ring_buffer_``, ``perf_buffer_``.
  11. System call wrappers
  12. --------------------
  13. System call wrappers are simple wrappers for commands supported by
  14. sys_bpf system call. These wrappers should go to ``bpf.h`` header file
  15. and map one to one to corresponding commands.
  16. For example ``bpf_map_lookup_elem`` wraps ``BPF_MAP_LOOKUP_ELEM``
  17. command of sys_bpf, ``bpf_prog_attach`` wraps ``BPF_PROG_ATTACH``, etc.
  18. Objects
  19. -------
  20. Another class of types and functions provided by libbpf API is "objects"
  21. and functions to work with them. Objects are high-level abstractions
  22. such as BPF program or BPF map. They're represented by corresponding
  23. structures such as ``struct bpf_object``, ``struct bpf_program``,
  24. ``struct bpf_map``, etc.
  25. Structures are forward declared and access to their fields should be
  26. provided via corresponding getters and setters rather than directly.
  27. These objects are associated with corresponding parts of ELF object that
  28. contains compiled BPF programs.
  29. For example ``struct bpf_object`` represents ELF object itself created
  30. from an ELF file or from a buffer, ``struct bpf_program`` represents a
  31. program in ELF object and ``struct bpf_map`` is a map.
  32. Functions that work with an object have names built from object name,
  33. double underscore and part that describes function purpose.
  34. For example ``bpf_object__open`` consists of the name of corresponding
  35. object, ``bpf_object``, double underscore and ``open`` that defines the
  36. purpose of the function to open ELF file and create ``bpf_object`` from
  37. it.
  38. All objects and corresponding functions other than BTF related should go
  39. to ``libbpf.h``. BTF types and functions should go to ``btf.h``.
  40. Auxiliary functions
  41. -------------------
  42. Auxiliary functions and types that don't fit well in any of categories
  43. described above should have ``libbpf_`` prefix, e.g.
  44. ``libbpf_get_error`` or ``libbpf_prog_type_by_name``.
  45. ABI
  46. ---
  47. libbpf can be both linked statically or used as DSO. To avoid possible
  48. conflicts with other libraries an application is linked with, all
  49. non-static libbpf symbols should have one of the prefixes mentioned in
  50. API documentation above. See API naming convention to choose the right
  51. name for a new symbol.
  52. Symbol visibility
  53. -----------------
  54. libbpf follow the model when all global symbols have visibility "hidden"
  55. by default and to make a symbol visible it has to be explicitly
  56. attributed with ``LIBBPF_API`` macro. For example:
  57. .. code-block:: c
  58. LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id);
  59. This prevents from accidentally exporting a symbol, that is not supposed
  60. to be a part of ABI what, in turn, improves both libbpf developer- and
  61. user-experiences.
  62. ABI versionning
  63. ---------------
  64. To make future ABI extensions possible libbpf ABI is versioned.
  65. Versioning is implemented by ``libbpf.map`` version script that is
  66. passed to linker.
  67. Version name is ``LIBBPF_`` prefix + three-component numeric version,
  68. starting from ``0.0.1``.
  69. Every time ABI is being changed, e.g. because a new symbol is added or
  70. semantic of existing symbol is changed, ABI version should be bumped.
  71. This bump in ABI version is at most once per kernel development cycle.
  72. For example, if current state of ``libbpf.map`` is:
  73. .. code-block:: none
  74. LIBBPF_0.0.1 {
  75. global:
  76. bpf_func_a;
  77. bpf_func_b;
  78. local:
  79. \*;
  80. };
  81. , and a new symbol ``bpf_func_c`` is being introduced, then
  82. ``libbpf.map`` should be changed like this:
  83. .. code-block:: none
  84. LIBBPF_0.0.1 {
  85. global:
  86. bpf_func_a;
  87. bpf_func_b;
  88. local:
  89. \*;
  90. };
  91. LIBBPF_0.0.2 {
  92. global:
  93. bpf_func_c;
  94. } LIBBPF_0.0.1;
  95. , where new version ``LIBBPF_0.0.2`` depends on the previous
  96. ``LIBBPF_0.0.1``.
  97. Format of version script and ways to handle ABI changes, including
  98. incompatible ones, described in details in [1].
  99. Stand-alone build
  100. -------------------
  101. Under https://github.com/libbpf/libbpf there is a (semi-)automated
  102. mirror of the mainline's version of libbpf for a stand-alone build.
  103. However, all changes to libbpf's code base must be upstreamed through
  104. the mainline kernel tree.
  105. API documentation convention
  106. ============================
  107. The libbpf API is documented via comments above definitions in
  108. header files. These comments can be rendered by doxygen and sphinx
  109. for well organized html output. This section describes the
  110. convention in which these comments should be formated.
  111. Here is an example from btf.h:
  112. .. code-block:: c
  113. /**
  114. * @brief **btf__new()** creates a new instance of a BTF object from the raw
  115. * bytes of an ELF's BTF section
  116. * @param data raw bytes
  117. * @param size number of bytes passed in `data`
  118. * @return new BTF object instance which has to be eventually freed with
  119. * **btf__free()**
  120. *
  121. * On error, error-code-encoded-as-pointer is returned, not a NULL. To extract
  122. * error code from such a pointer `libbpf_get_error()` should be used. If
  123. * `libbpf_set_strict_mode(LIBBPF_STRICT_CLEAN_PTRS)` is enabled, NULL is
  124. * returned on error instead. In both cases thread-local `errno` variable is
  125. * always set to error code as well.
  126. */
  127. The comment must start with a block comment of the form '/\*\*'.
  128. The documentation always starts with a @brief directive. This line is a short
  129. description about this API. It starts with the name of the API, denoted in bold
  130. like so: **api_name**. Please include an open and close parenthesis if this is a
  131. function. Follow with the short description of the API. A longer form description
  132. can be added below the last directive, at the bottom of the comment.
  133. Parameters are denoted with the @param directive, there should be one for each
  134. parameter. If this is a function with a non-void return, use the @return directive
  135. to document it.
  136. License
  137. -------------------
  138. libbpf is dual-licensed under LGPL 2.1 and BSD 2-Clause.
  139. Links
  140. -------------------
  141. [1] https://www.akkadia.org/drepper/dsohowto.pdf
  142. (Chapter 3. Maintaining APIs and ABIs).