genetlink-legacy.rst 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. .. SPDX-License-Identifier: BSD-3-Clause
  2. =================================================================
  3. Netlink specification support for legacy Generic Netlink families
  4. =================================================================
  5. This document describes the many additional quirks and properties
  6. required to describe older Generic Netlink families which form
  7. the ``genetlink-legacy`` protocol level.
  8. Specification
  9. =============
  10. Globals
  11. -------
  12. Attributes listed directly at the root level of the spec file.
  13. version
  14. ~~~~~~~
  15. Generic Netlink family version, default is 1.
  16. ``version`` has historically been used to introduce family changes
  17. which may break backwards compatibility. Since compatibility breaking changes
  18. are generally not allowed ``version`` is very rarely used.
  19. Attribute type nests
  20. --------------------
  21. New Netlink families should use ``multi-attr`` to define arrays.
  22. Older families (e.g. ``genetlink`` control family) attempted to
  23. define array types reusing attribute type to carry information.
  24. For reference the ``multi-attr`` array may look like this::
  25. [ARRAY-ATTR]
  26. [INDEX (optionally)]
  27. [MEMBER1]
  28. [MEMBER2]
  29. [SOME-OTHER-ATTR]
  30. [ARRAY-ATTR]
  31. [INDEX (optionally)]
  32. [MEMBER1]
  33. [MEMBER2]
  34. where ``ARRAY-ATTR`` is the array entry type.
  35. indexed-array
  36. ~~~~~~~~~~~~~
  37. ``indexed-array`` wraps the entire array in an extra attribute (hence
  38. limiting its size to 64kB). The ``ENTRY`` nests are special and have the
  39. index of the entry as their type instead of normal attribute type.
  40. A ``sub-type`` is needed to describe what type in the ``ENTRY``. A ``nest``
  41. ``sub-type`` means there are nest arrays in the ``ENTRY``, with the structure
  42. looks like::
  43. [SOME-OTHER-ATTR]
  44. [ARRAY-ATTR]
  45. [ENTRY]
  46. [MEMBER1]
  47. [MEMBER2]
  48. [ENTRY]
  49. [MEMBER1]
  50. [MEMBER2]
  51. Other ``sub-type`` like ``u32`` means there is only one member as described
  52. in ``sub-type`` in the ``ENTRY``. The structure looks like::
  53. [SOME-OTHER-ATTR]
  54. [ARRAY-ATTR]
  55. [ENTRY u32]
  56. [ENTRY u32]
  57. type-value
  58. ~~~~~~~~~~
  59. ``type-value`` is a construct which uses attribute types to carry
  60. information about a single object (often used when array is dumped
  61. entry-by-entry).
  62. ``type-value`` can have multiple levels of nesting, for example
  63. genetlink's policy dumps create the following structures::
  64. [POLICY-IDX]
  65. [ATTR-IDX]
  66. [POLICY-INFO-ATTR1]
  67. [POLICY-INFO-ATTR2]
  68. Where the first level of nest has the policy index as it's attribute
  69. type, it contains a single nest which has the attribute index as its
  70. type. Inside the attr-index nest are the policy attributes. Modern
  71. Netlink families should have instead defined this as a flat structure,
  72. the nesting serves no good purpose here.
  73. Operations
  74. ==========
  75. Enum (message ID) model
  76. -----------------------
  77. unified
  78. ~~~~~~~
  79. Modern families use the ``unified`` message ID model, which uses
  80. a single enumeration for all messages within family. Requests and
  81. responses share the same message ID. Notifications have separate
  82. IDs from the same space. For example given the following list
  83. of operations:
  84. .. code-block:: yaml
  85. -
  86. name: a
  87. value: 1
  88. do: ...
  89. -
  90. name: b
  91. do: ...
  92. -
  93. name: c
  94. value: 4
  95. notify: a
  96. -
  97. name: d
  98. do: ...
  99. Requests and responses for operation ``a`` will have the ID of 1,
  100. the requests and responses of ``b`` - 2 (since there is no explicit
  101. ``value`` it's previous operation ``+ 1``). Notification ``c`` will
  102. use the ID of 4, operation ``d`` 5 etc.
  103. directional
  104. ~~~~~~~~~~~
  105. The ``directional`` model splits the ID assignment by the direction of
  106. the message. Messages from and to the kernel can't be confused with
  107. each other so this conserves the ID space (at the cost of making
  108. the programming more cumbersome).
  109. In this case ``value`` attribute should be specified in the ``request``
  110. ``reply`` sections of the operations (if an operation has both ``do``
  111. and ``dump`` the IDs are shared, ``value`` should be set in ``do``).
  112. For notifications the ``value`` is provided at the op level but it
  113. only allocates a ``reply`` (i.e. a "from-kernel" ID). Let's look
  114. at an example:
  115. .. code-block:: yaml
  116. -
  117. name: a
  118. do:
  119. request:
  120. value: 2
  121. attributes: ...
  122. reply:
  123. value: 1
  124. attributes: ...
  125. -
  126. name: b
  127. notify: a
  128. -
  129. name: c
  130. notify: a
  131. value: 7
  132. -
  133. name: d
  134. do: ...
  135. In this case ``a`` will use 2 when sending the message to the kernel
  136. and expects message with ID 1 in response. Notification ``b`` allocates
  137. a "from-kernel" ID which is 2. ``c`` allocates "from-kernel" ID of 7.
  138. If operation ``d`` does not set ``values`` explicitly in the spec
  139. it will be allocated 3 for the request (``a`` is the previous operation
  140. with a request section and the value of 2) and 8 for response (``c`` is
  141. the previous operation in the "from-kernel" direction).
  142. Other quirks
  143. ============
  144. Structures
  145. ----------
  146. Legacy families can define C structures both to be used as the contents of
  147. an attribute and as a fixed message header. Structures are defined in
  148. ``definitions`` and referenced in operations or attributes.
  149. members
  150. ~~~~~~~
  151. - ``name`` - The attribute name of the struct member
  152. - ``type`` - One of the scalar types ``u8``, ``u16``, ``u32``, ``u64``, ``s8``,
  153. ``s16``, ``s32``, ``s64``, ``string``, ``binary`` or ``bitfield32``.
  154. - ``byte-order`` - ``big-endian`` or ``little-endian``
  155. - ``doc``, ``enum``, ``enum-as-flags``, ``display-hint`` - Same as for
  156. :ref:`attribute definitions <attribute_properties>`
  157. Note that structures defined in YAML are implicitly packed according to C
  158. conventions. For example, the following struct is 4 bytes, not 6 bytes:
  159. .. code-block:: c
  160. struct {
  161. u8 a;
  162. u16 b;
  163. u8 c;
  164. }
  165. Any padding must be explicitly added and C-like languages should infer the
  166. need for explicit padding from whether the members are naturally aligned.
  167. Here is the struct definition from above, declared in YAML:
  168. .. code-block:: yaml
  169. definitions:
  170. -
  171. name: message-header
  172. type: struct
  173. members:
  174. -
  175. name: a
  176. type: u8
  177. -
  178. name: b
  179. type: u16
  180. -
  181. name: c
  182. type: u8
  183. Fixed Headers
  184. ~~~~~~~~~~~~~
  185. Fixed message headers can be added to operations using ``fixed-header``.
  186. The default ``fixed-header`` can be set in ``operations`` and it can be set
  187. or overridden for each operation.
  188. .. code-block:: yaml
  189. operations:
  190. fixed-header: message-header
  191. list:
  192. -
  193. name: get
  194. fixed-header: custom-header
  195. attribute-set: message-attrs
  196. Attributes
  197. ~~~~~~~~~~
  198. A ``binary`` attribute can be interpreted as a C structure using a
  199. ``struct`` property with the name of the structure definition. The
  200. ``struct`` property implies ``sub-type: struct`` so it is not necessary to
  201. specify a sub-type.
  202. .. code-block:: yaml
  203. attribute-sets:
  204. -
  205. name: stats-attrs
  206. attributes:
  207. -
  208. name: stats
  209. type: binary
  210. struct: vport-stats
  211. C Arrays
  212. --------
  213. Legacy families also use ``binary`` attributes to encapsulate C arrays. The
  214. ``sub-type`` is used to identify the type of scalar to extract.
  215. .. code-block:: yaml
  216. attributes:
  217. -
  218. name: ports
  219. type: binary
  220. sub-type: u32
  221. Multi-message DO
  222. ----------------
  223. New Netlink families should never respond to a DO operation with multiple
  224. replies, with ``NLM_F_MULTI`` set. Use a filtered dump instead.
  225. At the spec level we can define a ``dumps`` property for the ``do``,
  226. perhaps with values of ``combine`` and ``multi-object`` depending
  227. on how the parsing should be implemented (parse into a single reply
  228. vs list of objects i.e. pretty much a dump).