[PATCH net-next v3 04/13] tools: ynl-gen: refactor local vars for .attr_put() callers

Asbjørn Sloth Tønnesen posted 13 patches 2 weeks, 6 days ago
There is a newer version of this series
[PATCH net-next v3 04/13] tools: ynl-gen: refactor local vars for .attr_put() callers
Posted by Asbjørn Sloth Tønnesen 2 weeks, 6 days ago
Refactor the generation of local variables needed when building
requests, by moving the logic into the Type classes, and use the
same helper in all places where .attr_put() is called.

If any attributes requests identical local_vars, then they will
be deduplicated, attributes are assumed to only use their local
variables transiently.

This patch fixes the build errors below:
$ make -C tools/net/ynl/generated/
[...]
-e      GEN wireguard-user.c
-e      GEN wireguard-user.h
-e      CC wireguard-user.o
wireguard-user.c: In function ‘wireguard_get_device_dump’:
wireguard-user.c:480:9: error: ‘array’ undeclared (first use in func)
  480 |         array = ynl_attr_nest_start(nlh, WGDEVICE_A_PEERS);
      |         ^~~~~
wireguard-user.c:480:9: note: each undeclared identifier is reported
                        only once for each function it appears in
wireguard-user.c:481:14: error: ‘i’ undeclared (first use in func)
  481 |         for (i = 0; i < req->_count.peers; i++)
      |              ^
wireguard-user.c: In function ‘wireguard_set_device’:
wireguard-user.c:533:9: error: ‘array’ undeclared (first use in func)
  533 |         array = ynl_attr_nest_start(nlh, WGDEVICE_A_PEERS);
      |         ^~~~~
make: *** [Makefile:52: wireguard-user.o] Error 1
make: Leaving directory '/usr/src/linux/tools/net/ynl/generated'

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

diff --git a/tools/net/ynl/pyynl/ynl_gen_c.py b/tools/net/ynl/pyynl/ynl_gen_c.py
index 3266af19edcd..e4cb8c95632c 100755
--- a/tools/net/ynl/pyynl/ynl_gen_c.py
+++ b/tools/net/ynl/pyynl/ynl_gen_c.py
@@ -235,6 +235,12 @@ class Type(SpecAttr):
         line = f"ynl_attr_put_{put_type}(nlh, {self.enum_name}, {var}->{self.c_name})"
         self._attr_put_line(ri, var, line)
 
+    def attr_put_local_vars(self):
+        local_vars = []
+        if self.presence_type() == 'count':
+            local_vars.append('unsigned int i;')
+        return local_vars
+
     def attr_put(self, ri, var):
         raise Exception(f"Put not implemented for class type {self.type}")
 
@@ -840,6 +846,10 @@ class TypeArrayNest(Type):
                      '}']
         return get_lines, None, local_vars
 
+    def attr_put_local_vars(self):
+        local_vars = ['struct nlattr *array;']
+        return local_vars + super().attr_put_local_vars()
+
     def attr_put(self, ri, var):
         ri.cw.p(f'array = ynl_attr_nest_start(nlh, {self.enum_name});')
         if self.sub_type in scalars:
@@ -2040,6 +2050,13 @@ def put_enum_to_str(family, cw, enum):
     _put_enum_to_str_helper(cw, enum.render_name, map_name, 'value', enum=enum)
 
 
+def put_local_vars(struct):
+    local_vars = set()
+    for _, attr in struct.member_list():
+        local_vars |= set(attr.attr_put_local_vars())
+    return list(local_vars)
+
+
 def put_req_nested_prototype(ri, struct, suffix=';'):
     func_args = ['struct nlmsghdr *nlh',
                  'unsigned int attr_type',
@@ -2062,15 +2079,7 @@ def put_req_nested(ri, struct):
         init_lines.append(f"hdr = ynl_nlmsg_put_extra_header(nlh, {struct_sz});")
         init_lines.append(f"memcpy(hdr, &obj->_hdr, {struct_sz});")
 
-    has_anest = False
-    has_count = False
-    for _, arg in struct.member_list():
-        has_anest |= arg.type == 'indexed-array'
-        has_count |= arg.presence_type() == 'count'
-    if has_anest:
-        local_vars.append('struct nlattr *array;')
-    if has_count:
-        local_vars.append('unsigned int i;')
+    local_vars += put_local_vars(struct)
 
     put_req_nested_prototype(ri, struct, suffix='')
     ri.cw.block_start()
@@ -2354,10 +2363,7 @@ def print_req(ri):
         local_vars += ['size_t hdr_len;',
                        'void *hdr;']
 
-    for _, attr in ri.struct["request"].member_list():
-        if attr.presence_type() == 'count':
-            local_vars += ['unsigned int i;']
-            break
+    local_vars += put_local_vars(ri.struct["request"])
 
     print_prototype(ri, direction, terminate=False)
     ri.cw.block_start()
@@ -2424,6 +2430,9 @@ def print_dump(ri):
         local_vars += ['size_t hdr_len;',
                        'void *hdr;']
 
+    if "request" in ri.op[ri.op_mode]:
+        local_vars += put_local_vars(ri.struct["request"])
+
     ri.cw.write_func_lvar(local_vars)
 
     ri.cw.p('yds.yarg.ys = ys;')
-- 
2.51.0

Re: [PATCH net-next v3 04/13] tools: ynl-gen: refactor local vars for .attr_put() callers
Posted by Jakub Kicinski 2 weeks, 5 days ago
On Thu, 11 Sep 2025 20:04:57 +0000 Asbjørn Sloth Tønnesen wrote:
> +    def attr_put_local_vars(self):
> +        local_vars = []
> +        if self.presence_type() == 'count':
> +            local_vars.append('unsigned int i;')
> +        return local_vars
> +
>      def attr_put(self, ri, var):
>          raise Exception(f"Put not implemented for class type {self.type}")
>  
> @@ -840,6 +846,10 @@ class TypeArrayNest(Type):
>                       '}']
>          return get_lines, None, local_vars
>  
> +    def attr_put_local_vars(self):
> +        local_vars = ['struct nlattr *array;']
> +        return local_vars + super().attr_put_local_vars()

Doesn't feel right. The Type method is a helper which is compatible
with the specific types by checking presence, then you override it,
and on top of that combine the output with super(). I don't like.
Re: [PATCH net-next v3 04/13] tools: ynl-gen: refactor local vars for .attr_put() callers
Posted by Asbjørn Sloth Tønnesen 2 weeks, 4 days ago
On 9/13/25 12:19 AM, Jakub Kicinski wrote:
> On Thu, 11 Sep 2025 20:04:57 +0000 Asbjørn Sloth Tønnesen wrote:
>> +    def attr_put_local_vars(self):
>> +        local_vars = []
>> +        if self.presence_type() == 'count':
>> +            local_vars.append('unsigned int i;')
>> +        return local_vars
>> +
>>       def attr_put(self, ri, var):
>>           raise Exception(f"Put not implemented for class type {self.type}")
>>   
>> @@ -840,6 +846,10 @@ class TypeArrayNest(Type):
>>                        '}']
>>           return get_lines, None, local_vars
>>   
>> +    def attr_put_local_vars(self):
>> +        local_vars = ['struct nlattr *array;']
>> +        return local_vars + super().attr_put_local_vars()
> 
> Doesn't feel right. The Type method is a helper which is compatible
> with the specific types by checking presence, then you override it,
> and on top of that combine the output with super(). I don't like.

I prefer to keep the array variable as a detail isolated to TypeArrayNest,
so I have given it another spin in v4.
Re: [PATCH net-next v3 04/13] tools: ynl-gen: refactor local vars for .attr_put() callers
Posted by Donald Hunter 2 weeks, 6 days ago
Asbjørn Sloth Tønnesen <ast@fiberby.net> writes:

> Refactor the generation of local variables needed when building
> requests, by moving the logic into the Type classes, and use the
> same helper in all places where .attr_put() is called.
>
> If any attributes requests identical local_vars, then they will
> be deduplicated, attributes are assumed to only use their local
> variables transiently.
>
> This patch fixes the build errors below:
> $ make -C tools/net/ynl/generated/
> [...]
> -e      GEN wireguard-user.c
> -e      GEN wireguard-user.h
> -e      CC wireguard-user.o
> wireguard-user.c: In function ‘wireguard_get_device_dump’:
> wireguard-user.c:480:9: error: ‘array’ undeclared (first use in func)
>   480 |         array = ynl_attr_nest_start(nlh, WGDEVICE_A_PEERS);
>       |         ^~~~~
> wireguard-user.c:480:9: note: each undeclared identifier is reported
>                         only once for each function it appears in
> wireguard-user.c:481:14: error: ‘i’ undeclared (first use in func)
>   481 |         for (i = 0; i < req->_count.peers; i++)
>       |              ^
> wireguard-user.c: In function ‘wireguard_set_device’:
> wireguard-user.c:533:9: error: ‘array’ undeclared (first use in func)
>   533 |         array = ynl_attr_nest_start(nlh, WGDEVICE_A_PEERS);
>       |         ^~~~~
> make: *** [Makefile:52: wireguard-user.o] Error 1
> make: Leaving directory '/usr/src/linux/tools/net/ynl/generated'
>
> Signed-off-by: Asbjørn Sloth Tønnesen <ast@fiberby.net>

Reviewed-by: Donald Hunter <donald.hunter@gmail.com>