[PATCH net-next 3/6] tools: ynl-gen: use uapi mask definition in NLA_POLICY_MASK

Asbjørn Sloth Tønnesen posted 6 patches 2 months, 1 week ago
[PATCH net-next 3/6] tools: ynl-gen: use uapi mask definition in NLA_POLICY_MASK
Posted by Asbjørn Sloth Tønnesen 2 months, 1 week ago
Currently when generating policies using NLA_POLICY_MASK(), then
we emit a pre-computed decimal mask.

When render-max is set, then we can re-use the mask definition,
that has been generated in the uapi header.

This IMHO makes the generated code read more like handwritten code.

This patch assumes that "kernel source" is only generated, when
"uapi header" is also generated through ynl-gen, when render-max is
set in the spec. AFAICT this is fine, as render-max is pointless
when uapi is not generated by ynl-gen.

Currently no generated policies are changed by this, as there are
no specs which are used for generation, which also has render-max.
In the future this might be used for code generation by wireguard.

Signed-off-by: Asbjørn Sloth Tønnesen <ast@fiberby.net>
---
 tools/net/ynl/pyynl/ynl_gen_c.py | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/tools/net/ynl/pyynl/ynl_gen_c.py b/tools/net/ynl/pyynl/ynl_gen_c.py
index 2666cc54d09c0..b00762721280c 100755
--- a/tools/net/ynl/pyynl/ynl_gen_c.py
+++ b/tools/net/ynl/pyynl/ynl_gen_c.py
@@ -418,12 +418,18 @@ class TypeScalar(Type):
         if 'flags-mask' in self.checks or self.is_bitfield:
             if self.is_bitfield:
                 enum = self.family.consts[self.attr['enum']]
-                mask = enum.get_mask(as_flags=True)
+                if enum.get('render-max', False):
+                    mask = c_upper(enum.enum_max_name)
+                else:
+                    mask = enum.get_mask(as_flags=True)
             else:
                 flags = self.family.consts[self.checks['flags-mask']]
                 flag_cnt = len(flags['entries'])
                 mask = (1 << flag_cnt) - 1
-            return f"NLA_POLICY_MASK({policy}, 0x{mask:x})"
+
+            if isinstance(mask, int):
+                mask = f'0x{mask:x}'
+            return f"NLA_POLICY_MASK({policy}, {mask})"
         elif 'full-range' in self.checks:
             return f"NLA_POLICY_FULL_RANGE({policy}, &{c_lower(self.enum_name)}_range)"
         elif 'range' in self.checks:
-- 
2.51.0

Re: [PATCH net-next 3/6] tools: ynl-gen: use uapi mask definition in NLA_POLICY_MASK
Posted by Jakub Kicinski 2 months ago
On Mon, 13 Oct 2025 16:50:00 +0000 Asbjørn Sloth Tønnesen wrote:
> Currently when generating policies using NLA_POLICY_MASK(), then
> we emit a pre-computed decimal mask.
> 
> When render-max is set, then we can re-use the mask definition,
> that has been generated in the uapi header.

This will encourage people to render masks in uAPI which just pollutes
the uAPI files.