Skip to content

Commit adc6fc2

Browse files
kuba-moogregkh
authored andcommitted
tools: ynl: add scope qualifier for definitions
commit fbf5df3 upstream. Using definitions in kernel policies is awkward right now. On one hand we want defines for max values and such. On the other we don't have a way of adding kernel-only defines. Adding unnecessary defines to uAPI is a bad idea, we won't be able to delete them. And when it comes to policy user space should just query it via the policy dump, not use hard coded defines. Add a "scope" property to definitions, which will let us tell the codegen that a definition is for kernel use only. Support following values: - uapi: render into the uAPI header (default, today's behavior) - kernel: render to kernel header only - user: same as kernel but for the user-side generated header Definitions may have a header property (definition is "external", provided by existing header). Extend the scope to headers, too. If definition has both scope and header properties we will only generate the includes in the right scope. Signed-off-by: Jakub Kicinski <kuba@kernel.org> Link: https://patch.msgid.link/20260510192904.3987113-8-kuba@kernel.org Signed-off-by: Paolo Abeni <pabeni@redhat.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent f54b30f commit adc6fc2

5 files changed

Lines changed: 65 additions & 2 deletions

File tree

Documentation/netlink/genetlink-c.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ properties:
6969
header:
7070
description: For C-compatible languages, header which already defines this value.
7171
type: string
72+
scope:
73+
description: |
74+
Visibility of this definition. "uapi" (default) renders into
75+
the uAPI header, "kernel" renders into the kernel-side
76+
generated header, "user" renders into the user-side
77+
generated header. When combined with `header:`, the
78+
definition is not rendered, and the named header is
79+
included only by code matching the scope.
80+
enum: [ uapi, kernel, user ]
7281
type:
7382
enum: [ const, enum, flags ]
7483
doc:

Documentation/netlink/genetlink-legacy.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ properties:
8383
header:
8484
description: For C-compatible languages, header which already defines this value.
8585
type: string
86+
scope:
87+
description: |
88+
Visibility of this definition. "uapi" (default) renders into
89+
the uAPI header, "kernel" renders into the kernel-side
90+
generated header, "user" renders into the user-side
91+
generated header. When combined with `header:`, the
92+
definition is not rendered, and the named header is
93+
included only by code matching the scope.
94+
enum: [ uapi, kernel, user ]
8695
type:
8796
enum: [ const, enum, flags, struct ] # Trim
8897
doc:

Documentation/netlink/genetlink.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ properties:
5555
header:
5656
description: For C-compatible languages, header which already defines this value.
5757
type: string
58+
scope:
59+
description: |
60+
Visibility of this definition. "uapi" (default) renders into
61+
the uAPI header, "kernel" renders into the kernel-side
62+
generated header, "user" renders into the user-side
63+
generated header. When combined with `header:`, the
64+
definition is not rendered, and the named header is
65+
included only by code matching the scope.
66+
enum: [ uapi, kernel, user ]
5867
type:
5968
enum: [ const, enum, flags ]
6069
doc:

Documentation/netlink/netlink-raw.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ properties:
8181
header:
8282
description: For C-compatible languages, header which already defines this value.
8383
type: string
84+
scope:
85+
description: |
86+
Visibility of this definition. "uapi" (default) renders into
87+
the uAPI header, "kernel" renders into the kernel-side
88+
generated header, "user" renders into the user-side
89+
generated header. When combined with `header:`, the
90+
definition is not rendered, and the named header is
91+
included only by code matching the scope.
92+
enum: [ uapi, kernel, user ]
8493
type:
8594
enum: [ const, enum, flags, struct ] # Trim
8695
doc:

tools/net/ynl/pyynl/ynl_gen_c.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3187,6 +3187,8 @@ def render_uapi(family, cw):
31873187
for const in family['definitions']:
31883188
if const.get('header'):
31893189
continue
3190+
if const.get('scope', 'uapi') != 'uapi':
3191+
continue
31903192

31913193
if const['type'] != 'const':
31923194
cw.writes_defines(defines)
@@ -3314,6 +3316,25 @@ def render_uapi(family, cw):
33143316
cw.p(f'#endif /* {hdr_prot} */')
33153317

33163318

3319+
def render_scoped_consts(family, cw, scope):
3320+
defines = []
3321+
for const in family['definitions']:
3322+
if const['type'] != 'const':
3323+
continue
3324+
if const.get('header'):
3325+
continue
3326+
if const.get('scope') != scope:
3327+
continue
3328+
name_pfx = const.get('name-prefix', f"{family.ident_name}-")
3329+
defines.append([
3330+
c_upper(family.get('c-define-name',
3331+
f"{name_pfx}{const['name']}")),
3332+
const['value']])
3333+
if defines:
3334+
cw.writes_defines(defines)
3335+
cw.nl()
3336+
3337+
33173338
def _render_user_ntf_entry(ri, op):
33183339
if not ri.family.is_classic():
33193340
ri.cw.block_start(line=f"[{op.enum_name}] = ")
@@ -3474,8 +3495,12 @@ def main():
34743495
cw.p('#include "ynl.h"')
34753496
headers = []
34763497
for definition in parsed['definitions'] + parsed['attribute-sets']:
3477-
if 'header' in definition:
3478-
headers.append(definition['header'])
3498+
if 'header' not in definition:
3499+
continue
3500+
scope = definition.get('scope', 'uapi')
3501+
if scope != 'uapi' and scope != args.mode:
3502+
continue
3503+
headers.append(definition['header'])
34793504
if args.mode == 'user':
34803505
headers.append(parsed.uapi_header)
34813506
seen_header = []
@@ -3492,13 +3517,15 @@ def main():
34923517
for one in args.user_header:
34933518
cw.p(f'#include "{one}"')
34943519
else:
3520+
render_scoped_consts(parsed, cw, 'user')
34953521
cw.p('struct ynl_sock;')
34963522
cw.nl()
34973523
render_user_family(parsed, cw, True)
34983524
cw.nl()
34993525

35003526
if args.mode == "kernel":
35013527
if args.header:
3528+
render_scoped_consts(parsed, cw, 'kernel')
35023529
for _, struct in sorted(parsed.pure_nested_structs.items()):
35033530
if struct.request:
35043531
cw.p('/* Common nested types */')

0 commit comments

Comments
 (0)