Skip to content

Commit ba980f8

Browse files
committed
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 a731132 ("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. Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent a0d9429 commit ba980f8

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

Documentation/netlink/genetlink-c.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,22 @@ properties:
378378
type: string
379379
# End genetlink-c
380380
flags: *cmd_flags
381+
382+
kernel-family:
383+
description: Additional global attributes used for kernel C code generation.
384+
type: object
385+
additionalProperties: False
386+
properties:
387+
headers:
388+
description: |
389+
List of extra headers which should be included in the source
390+
of the generated code.
391+
type: array
392+
items:
393+
type: string
394+
sock-priv:
395+
description: |
396+
Literal name of the type which is used within the kernel
397+
to store the socket state. The type / structure is internal
398+
to the kernel, and is not defined in the spec.
399+
type: string

Documentation/netlink/genetlink-legacy.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,3 +439,22 @@ properties:
439439
type: string
440440
# End genetlink-c
441441
flags: *cmd_flags
442+
443+
kernel-family:
444+
description: Additional global attributes used for kernel C code generation.
445+
type: object
446+
additionalProperties: False
447+
properties:
448+
headers:
449+
description: |
450+
List of extra headers which should be included in the source
451+
of the generated code.
452+
type: array
453+
items:
454+
type: string
455+
sock-priv:
456+
description: |
457+
Literal name of the type which is used within the kernel
458+
to store the socket state. The type / structure is internal
459+
to the kernel, and is not defined in the spec.
460+
type: string

Documentation/netlink/genetlink.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,22 @@ properties:
328328
The name for the group, used to form the define and the value of the define.
329329
type: string
330330
flags: *cmd_flags
331+
332+
kernel-family:
333+
description: Additional global attributes used for kernel C code generation.
334+
type: object
335+
additionalProperties: False
336+
properties:
337+
headers:
338+
description: |
339+
List of extra headers which should be included in the source
340+
of the generated code.
341+
type: array
342+
items:
343+
type: string
344+
sock-priv:
345+
description: |
346+
Literal name of the type which is used within the kernel
347+
to store the socket state. The type / structure is internal
348+
to the kernel, and is not defined in the spec.
349+
type: string

tools/net/ynl/lib/nlspec.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ class SpecFamily(SpecElement):
418418
consts dict of all constants/enums
419419
fixed_header string, optional name of family default fixed header struct
420420
mcast_groups dict of all multicast groups (index by name)
421+
kernel_family dict of kernel family attributes
421422
"""
422423
def __init__(self, spec_path, schema_path=None, exclude_ops=None):
423424
with open(spec_path, "r") as stream:
@@ -461,6 +462,7 @@ def __init__(self, spec_path, schema_path=None, exclude_ops=None):
461462
self.ntfs = collections.OrderedDict()
462463
self.consts = collections.OrderedDict()
463464
self.mcast_groups = collections.OrderedDict()
465+
self.kernel_family = collections.OrderedDict(self.yaml.get('kernel-family', {}))
464466

465467
last_exception = None
466468
while len(self._resolution_list) > 0:

tools/net/ynl/ynl-gen-c.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,6 +2342,10 @@ def print_kernel_family_struct_hdr(family, cw):
23422342

23432343
cw.p(f"extern struct genl_family {family.c_name}_nl_family;")
23442344
cw.nl()
2345+
if 'sock-priv' in family.kernel_family:
2346+
cw.p(f'void {family.c_name}_nl_sock_priv_init({family.kernel_family["sock-priv"]} *priv);')
2347+
cw.p(f'void {family.c_name}_nl_sock_priv_destroy({family.kernel_family["sock-priv"]} *priv);')
2348+
cw.nl()
23452349

23462350

23472351
def print_kernel_family_struct_src(family, cw):
@@ -2363,6 +2367,11 @@ def print_kernel_family_struct_src(family, cw):
23632367
if family.mcgrps['list']:
23642368
cw.p(f'.mcgrps\t\t= {family.c_name}_nl_mcgrps,')
23652369
cw.p(f'.n_mcgrps\t= ARRAY_SIZE({family.c_name}_nl_mcgrps),')
2370+
if 'sock-priv' in family.kernel_family:
2371+
cw.p(f'.sock_priv_size\t= sizeof({family.kernel_family["sock-priv"]}),')
2372+
# Force cast here, actual helpers take pointer to the real type.
2373+
cw.p(f'.sock_priv_init\t= (void *){family.c_name}_nl_sock_priv_init,')
2374+
cw.p(f'.sock_priv_destroy = (void *){family.c_name}_nl_sock_priv_destroy,')
23662375
cw.block_end(';')
23672376

23682377

@@ -2659,6 +2668,7 @@ def main():
26592668
cw.p(f'#include "{os.path.basename(args.out_file[:-2])}.h"')
26602669
cw.nl()
26612670
headers = ['uapi/' + parsed.uapi_header]
2671+
headers += parsed.kernel_family.get('headers', [])
26622672
else:
26632673
cw.p('#include <stdlib.h>')
26642674
cw.p('#include <string.h>')

0 commit comments

Comments
 (0)