xref: /linux/Documentation/userspace-api/netlink/c-code-gen.rst (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1*9d6a6507SJakub Kicinski.. SPDX-License-Identifier: BSD-3-Clause
2*9d6a6507SJakub Kicinski
3*9d6a6507SJakub Kicinski==============================
4*9d6a6507SJakub KicinskiNetlink spec C code generation
5*9d6a6507SJakub Kicinski==============================
6*9d6a6507SJakub Kicinski
7*9d6a6507SJakub KicinskiThis document describes how Netlink specifications are used to render
8*9d6a6507SJakub KicinskiC code (uAPI, policies etc.). It also defines the additional properties
9*9d6a6507SJakub Kicinskiallowed in older families by the ``genetlink-c`` protocol level,
10*9d6a6507SJakub Kicinskito control the naming.
11*9d6a6507SJakub Kicinski
12*9d6a6507SJakub KicinskiFor brevity this document refers to ``name`` properties of various
13*9d6a6507SJakub Kicinskiobjects by the object type. For example ``$attr`` is the value
14*9d6a6507SJakub Kicinskiof ``name`` in an attribute, and ``$family`` is the name of the
15*9d6a6507SJakub Kicinskifamily (the global ``name`` property).
16*9d6a6507SJakub Kicinski
17*9d6a6507SJakub KicinskiThe upper case is used to denote literal values, e.g. ``$family-CMD``
18*9d6a6507SJakub Kicinskimeans the concatenation of ``$family``, a dash character, and the literal
19*9d6a6507SJakub Kicinski``CMD``.
20*9d6a6507SJakub Kicinski
21*9d6a6507SJakub KicinskiThe names of ``#defines`` and enum values are always converted to upper case,
22*9d6a6507SJakub Kicinskiand with dashes (``-``) replaced by underscores (``_``).
23*9d6a6507SJakub Kicinski
24*9d6a6507SJakub KicinskiIf the constructed name is a C keyword, an extra underscore is
25*9d6a6507SJakub Kicinskiappended (``do`` -> ``do_``).
26*9d6a6507SJakub Kicinski
27*9d6a6507SJakub KicinskiGlobals
28*9d6a6507SJakub Kicinski=======
29*9d6a6507SJakub Kicinski
30*9d6a6507SJakub Kicinski``c-family-name`` controls the name of the ``#define`` for the family
31*9d6a6507SJakub Kicinskiname, default is ``$family-FAMILY-NAME``.
32*9d6a6507SJakub Kicinski
33*9d6a6507SJakub Kicinski``c-version-name`` controls the name of the ``#define`` for the version
34*9d6a6507SJakub Kicinskiof the family, default is ``$family-FAMILY-VERSION``.
35*9d6a6507SJakub Kicinski
36*9d6a6507SJakub Kicinski``max-by-define`` selects if max values for enums are defined as a
37*9d6a6507SJakub Kicinski``#define`` rather than inside the enum.
38*9d6a6507SJakub Kicinski
39*9d6a6507SJakub KicinskiDefinitions
40*9d6a6507SJakub Kicinski===========
41*9d6a6507SJakub Kicinski
42*9d6a6507SJakub KicinskiConstants
43*9d6a6507SJakub Kicinski---------
44*9d6a6507SJakub Kicinski
45*9d6a6507SJakub KicinskiEvery constant is rendered as a ``#define``.
46*9d6a6507SJakub KicinskiThe name of the constant is ``$family-$constant`` and the value
47*9d6a6507SJakub Kicinskiis rendered as a string or integer according to its type in the spec.
48*9d6a6507SJakub Kicinski
49*9d6a6507SJakub KicinskiEnums and flags
50*9d6a6507SJakub Kicinski---------------
51*9d6a6507SJakub Kicinski
52*9d6a6507SJakub KicinskiEnums are named ``$family-$enum``. The full name can be set directly
53*9d6a6507SJakub Kicinskior suppressed by specifying the ``enum-name`` property.
54*9d6a6507SJakub KicinskiDefault entry name is ``$family-$enum-$entry``.
55*9d6a6507SJakub KicinskiIf ``name-prefix`` is specified it replaces the ``$family-$enum``
56*9d6a6507SJakub Kicinskiportion of the entry name.
57*9d6a6507SJakub Kicinski
58*9d6a6507SJakub KicinskiBoolean ``render-max`` controls creation of the max values
59*9d6a6507SJakub Kicinski(which are enabled by default for attribute enums).
60*9d6a6507SJakub Kicinski
61*9d6a6507SJakub KicinskiAttributes
62*9d6a6507SJakub Kicinski==========
63*9d6a6507SJakub Kicinski
64*9d6a6507SJakub KicinskiEach attribute set (excluding fractional sets) is rendered as an enum.
65*9d6a6507SJakub Kicinski
66*9d6a6507SJakub KicinskiAttribute enums are traditionally unnamed in netlink headers.
67*9d6a6507SJakub KicinskiIf naming is desired ``enum-name`` can be used to specify the name.
68*9d6a6507SJakub Kicinski
69*9d6a6507SJakub KicinskiThe default attribute name prefix is ``$family-A`` if the name of the set
70*9d6a6507SJakub Kicinskiis the same as the name of the family and ``$family-A-$set`` if the names
71*9d6a6507SJakub Kicinskidiffer. The prefix can be overridden by the ``name-prefix`` property of a set.
72*9d6a6507SJakub KicinskiThe rest of the section will refer to the prefix as ``$pfx``.
73*9d6a6507SJakub Kicinski
74*9d6a6507SJakub KicinskiAttributes are named ``$pfx-$attribute``.
75*9d6a6507SJakub Kicinski
76*9d6a6507SJakub KicinskiAttribute enums end with two special values ``__$pfx-MAX`` and ``$pfx-MAX``
77*9d6a6507SJakub Kicinskiwhich are used for sizing attribute tables.
78*9d6a6507SJakub KicinskiThese two names can be specified directly with the ``attr-cnt-name``
79*9d6a6507SJakub Kicinskiand ``attr-max-name`` properties respectively.
80*9d6a6507SJakub Kicinski
81*9d6a6507SJakub KicinskiIf ``max-by-define`` is set to ``true`` at the global level ``attr-max-name``
82*9d6a6507SJakub Kicinskiwill be specified as a ``#define`` rather than an enum value.
83*9d6a6507SJakub Kicinski
84*9d6a6507SJakub KicinskiOperations
85*9d6a6507SJakub Kicinski==========
86*9d6a6507SJakub Kicinski
87*9d6a6507SJakub KicinskiOperations are named ``$family-CMD-$operation``.
88*9d6a6507SJakub KicinskiIf ``name-prefix`` is specified it replaces the ``$family-CMD``
89*9d6a6507SJakub Kicinskiportion of the name.
90*9d6a6507SJakub Kicinski
91*9d6a6507SJakub KicinskiSimilarly to attribute enums operation enums end with special count and max
92*9d6a6507SJakub Kicinskiattributes. For operations those attributes can be renamed with
93*9d6a6507SJakub Kicinski``cmd-cnt-name`` and ``cmd-max-name``. Max will be a define if ``max-by-define``
94*9d6a6507SJakub Kicinskiis ``true``.
95*9d6a6507SJakub Kicinski
96*9d6a6507SJakub KicinskiMulticast groups
97*9d6a6507SJakub Kicinski================
98*9d6a6507SJakub Kicinski
99*9d6a6507SJakub KicinskiEach multicast group gets a define rendered into the kernel uAPI header.
100*9d6a6507SJakub KicinskiThe name of the define is ``$family-MCGRP-$group``, and can be overwritten
101*9d6a6507SJakub Kicinskiwith the ``c-define-name`` property.
102*9d6a6507SJakub Kicinski
103*9d6a6507SJakub KicinskiCode generation
104*9d6a6507SJakub Kicinski===============
105*9d6a6507SJakub Kicinski
106*9d6a6507SJakub KicinskiuAPI header is assumed to come from ``<linux/$family.h>`` in the default header
107*9d6a6507SJakub Kicinskisearch path. It can be changed using the ``uapi-header`` global property.
108