c-code-gen.rst 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. .. SPDX-License-Identifier: BSD-3-Clause
  2. ==============================
  3. Netlink spec C code generation
  4. ==============================
  5. This document describes how Netlink specifications are used to render
  6. C code (uAPI, policies etc.). It also defines the additional properties
  7. allowed in older families by the ``genetlink-c`` protocol level,
  8. to control the naming.
  9. For brevity this document refers to ``name`` properties of various
  10. objects by the object type. For example ``$attr`` is the value
  11. of ``name`` in an attribute, and ``$family`` is the name of the
  12. family (the global ``name`` property).
  13. The upper case is used to denote literal values, e.g. ``$family-CMD``
  14. means the concatenation of ``$family``, a dash character, and the literal
  15. ``CMD``.
  16. The names of ``#defines`` and enum values are always converted to upper case,
  17. and with dashes (``-``) replaced by underscores (``_``).
  18. If the constructed name is a C keyword, an extra underscore is
  19. appended (``do`` -> ``do_``).
  20. Globals
  21. =======
  22. ``c-family-name`` controls the name of the ``#define`` for the family
  23. name, default is ``$family-FAMILY-NAME``.
  24. ``c-version-name`` controls the name of the ``#define`` for the version
  25. of the family, default is ``$family-FAMILY-VERSION``.
  26. ``max-by-define`` selects if max values for enums are defined as a
  27. ``#define`` rather than inside the enum.
  28. Definitions
  29. ===========
  30. Constants
  31. ---------
  32. Every constant is rendered as a ``#define``.
  33. The name of the constant is ``$family-$constant`` and the value
  34. is rendered as a string or integer according to its type in the spec.
  35. Enums and flags
  36. ---------------
  37. Enums are named ``$family-$enum``. The full name can be set directly
  38. or suppressed by specifying the ``enum-name`` property.
  39. Default entry name is ``$family-$enum-$entry``.
  40. If ``name-prefix`` is specified it replaces the ``$family-$enum``
  41. portion of the entry name.
  42. Boolean ``render-max`` controls creation of the max values
  43. (which are enabled by default for attribute enums). These max
  44. values are named ``__$pfx-MAX`` and ``$pfx-MAX``. The name
  45. of the first value can be overridden via ``enum-cnt-name`` property.
  46. Attributes
  47. ==========
  48. Each attribute set (excluding fractional sets) is rendered as an enum.
  49. Attribute enums are traditionally unnamed in netlink headers.
  50. If naming is desired ``enum-name`` can be used to specify the name.
  51. The default attribute name prefix is ``$family-A`` if the name of the set
  52. is the same as the name of the family and ``$family-A-$set`` if the names
  53. differ. The prefix can be overridden by the ``name-prefix`` property of a set.
  54. The rest of the section will refer to the prefix as ``$pfx``.
  55. Attributes are named ``$pfx-$attribute``.
  56. Attribute enums end with two special values ``__$pfx-MAX`` and ``$pfx-MAX``
  57. which are used for sizing attribute tables.
  58. These two names can be specified directly with the ``attr-cnt-name``
  59. and ``attr-max-name`` properties respectively.
  60. If ``max-by-define`` is set to ``true`` at the global level ``attr-max-name``
  61. will be specified as a ``#define`` rather than an enum value.
  62. Operations
  63. ==========
  64. Operations are named ``$family-CMD-$operation``.
  65. If ``name-prefix`` is specified it replaces the ``$family-CMD``
  66. portion of the name.
  67. Similarly to attribute enums operation enums end with special count and max
  68. attributes. For operations those attributes can be renamed with
  69. ``cmd-cnt-name`` and ``cmd-max-name``. Max will be a define if ``max-by-define``
  70. is ``true``.
  71. Multicast groups
  72. ================
  73. Each multicast group gets a define rendered into the kernel uAPI header.
  74. The name of the define is ``$family-MCGRP-$group``, and can be overwritten
  75. with the ``c-define-name`` property.
  76. Code generation
  77. ===============
  78. uAPI header is assumed to come from ``<linux/$family.h>`` in the default header
  79. search path. It can be changed using the ``uapi-header`` global property.