Skip to contentSkip to conversation

netlink: specs: support generating code for genl socket priv

PATCH Applied Jakub Kicinski 2 participants 2 messages Mar 8, 2024 – Mar 11, 2024

Subsystems (3)
lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

PATCH 1/1 netlink: specs: support generating code for genl socket priv

The family struct is auto-generated for new families, support use of the sock_priv_* mechanism added in commit a731132424ad ("genetlink: introduce per-sock family private storage").

For example if the family wants to use struct sk_buff as its private struct (unrealistic but just for illustration), it would add to its spec:

kernel-family:
headers: [ "linux/skbuff.h" ]
sock-priv: struct sk_buff

ynl-gen-c will declare the appropriate priv size and hook in function prototypes to be implemented by the family.

--
CC: [email protected]
CC: [email protected]
CC: [email protected]

---
 Documentation/netlink/genetlink-c.yaml      | 19 +++++++++++++++++++
 Documentation/netlink/genetlink-legacy.yaml | 19 +++++++++++++++++++
 Documentation/netlink/genetlink.yaml        | 19 +++++++++++++++++++
 tools/net/ynl/lib/nlspec.py                 |  2 ++
 tools/net/ynl/ynl-gen-c.py                  | 10 ++++++++++
 5 files changed, 69 insertions(+)
Documentation/netlink/genetlink-c.yaml +19 -0
index c58f7153fcf8..292068cc8a5a 100644
@@ -370,3 +370,22 @@ additionalProperties: False
               type: string
             # End genetlink-c
             flags: *cmd_flags
+
+  kernel-family:
+    description: Additional global attributes used for kernel C code generation.
+    type: object
+    additionalProperties: False
+    properties:
+      headers:
+        description: |
+          List of extra headers which should be included in the source
+          of the generated code.
+        type: array
+        items:
+          type: string
+      sock-priv:
+        description: |
+          Literal name of the type which is used within the kernel
+          to store the socket state. The type / structure is internal
+          to the kernel, and is not defined in the spec.
+        type: string
Documentation/netlink/genetlink-legacy.yaml +19 -0
index 938703088306..b6de66e38bdb 100644
@@ -431,3 +431,22 @@ additionalProperties: False
               type: string
             # End genetlink-c
             flags: *cmd_flags
+
+  kernel-family:
+    description: Additional global attributes used for kernel C code generation.
+    type: object
+    additionalProperties: False
+    properties:
+      headers:
+        description: |
+          List of extra headers which should be included in the source
+          of the generated code.
+        type: array
+        items:
+          type: string
+      sock-priv:
+        description: |
+          Literal name of the type which is used within the kernel
+          to store the socket state. The type / structure is internal
+          to the kernel, and is not defined in the spec.
+        type: string
Documentation/netlink/genetlink.yaml +19 -0
index 3283bf458ff1..0512c771d737 100644
@@ -328,3 +328,22 @@ additionalProperties: False
                 The name for the group, used to form the define and the value of the define.
               type: string
             flags: *cmd_flags
+
+  kernel-family:
+    description: Additional global attributes used for kernel C code generation.
+    type: object
+    additionalProperties: False
+    properties:
+      headers:
+        description: |
+          List of extra headers which should be included in the source
+          of the generated code.
+        type: array
+        items:
+          type: string
+      sock-priv:
+        description: |
+          Literal name of the type which is used within the kernel
+          to store the socket state. The type / structure is internal
+          to the kernel, and is not defined in the spec.
+        type: string
tools/net/ynl/lib/nlspec.py +2 -0
index fbce52395b3b..6d08ab9e213f 100644
@@ -418,6 +418,7 @@ jsonschema = None
         consts     dict of all constants/enums
         fixed_header  string, optional name of family default fixed header struct
         mcast_groups  dict of all multicast groups (index by name)
+        kernel_family   dict of kernel family attributes
     """
     def __init__(self, spec_path, schema_path=None, exclude_ops=None):
         with open(spec_path, "r") as stream:
@@ -461,6 +462,7 @@ jsonschema = None
         self.ntfs = collections.OrderedDict()
         self.consts = collections.OrderedDict()
         self.mcast_groups = collections.OrderedDict()
+        self.kernel_family = collections.OrderedDict(self.yaml.get('kernel-family', {}))
 
         last_exception = None
         while len(self._resolution_list) > 0:
tools/net/ynl/ynl-gen-c.py +10 -0
index 2f5febfe66a1..d78dd005cdb9 100755
@@ -2340,6 +2340,10 @@ _C_KW = {
 
     cw.p(f"extern struct genl_family {family.c_name}_nl_family;")
     cw.nl()
+    if 'sock-priv' in family.kernel_family:
+        cw.p(f'void {family.c_name}_nl_sock_priv_init({family.kernel_family["sock-priv"]} *priv);')
+        cw.p(f'void {family.c_name}_nl_sock_priv_destroy({family.kernel_family["sock-priv"]} *priv);')
+        cw.nl()
 
 
 def print_kernel_family_struct_src(family, cw):
@@ -2361,6 +2365,11 @@ _C_KW = {
     if family.mcgrps['list']:
         cw.p(f'.mcgrps\t\t= {family.c_name}_nl_mcgrps,')
         cw.p(f'.n_mcgrps\t= ARRAY_SIZE({family.c_name}_nl_mcgrps),')
+    if 'sock-priv' in family.kernel_family:
+        cw.p(f'.sock_priv_size\t= sizeof({family.kernel_family["sock-priv"]}),')
+        # Force cast here, actual helpers take pointer to the real type.
+        cw.p(f'.sock_priv_init\t= (void *){family.c_name}_nl_sock_priv_init,')
+        cw.p(f'.sock_priv_destroy = (void *){family.c_name}_nl_sock_priv_destroy,')
     cw.block_end(';')
 
 
@@ -2657,6 +2666,7 @@ _C_KW = {
                 cw.p(f'#include "{os.path.basename(args.out_file[:-2])}.h"')
             cw.nl()
         headers = ['uapi/' + parsed.uapi_header]
+        headers += parsed.kernel_family.get('headers', [])
     else:
         cw.p('#include <stdlib.h>')
         cw.p('#include <string.h>')
Signature · 1 line
2.44.0
#1 lore raw
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 9 lines · On Fri, 8 Mar 2024 11:03:19 -0800 you wrote:

The family struct is auto-generated for new families, support use of the sock_priv_* mechanism added in commit a731132424ad ("genetlink: introduce per-sock family private storage").

For example if the family wants to use struct sk_buff as its private struct (unrealistic but just for illustration), it would add to its spec:

[...]

Here is the summary with links:
- [net-next] netlink: specs: support generating code for genl socket priv

    https://git.kernel.org/netdev/net-next/c/ba980f8dff54

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html