[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250911200508.79341-7-ast@fiberby.net>
Date: Thu, 11 Sep 2025 20:04:59 +0000
From: Asbjørn Sloth Tønnesen <ast@...erby.net>
To: "Jason A. Donenfeld" <Jason@...c4.com>,
"David S. Miller" <davem@...emloft.net>,
Eric Dumazet <edumazet@...gle.com>,
Jakub Kicinski <kuba@...nel.org>,
Paolo Abeni <pabeni@...hat.com>
Cc: Asbjørn Sloth Tønnesen <ast@...erby.net>,
Donald Hunter <donald.hunter@...il.com>,
Simon Horman <horms@...nel.org>,
Jacob Keller <jacob.e.keller@...el.com>,
Sabrina Dubroca <sd@...asysnail.net>,
wireguard@...ts.zx2c4.com,
netdev@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: [PATCH net-next v3 06/13] tools: ynl-gen: deduplicate fixed_header handling
Fixed headers are handled nearly identical in print_dump(),
print_req() and put_req_nested(), generalize them and use a
common function to generate them.
This only causes cosmetic changes to tc_netem_attrs_put() in
tc-user.c.
Signed-off-by: Asbjørn Sloth Tønnesen <ast@...erby.net>
---
tools/net/ynl/pyynl/ynl_gen_c.py | 39 ++++++++++++++++----------------
1 file changed, 20 insertions(+), 19 deletions(-)
diff --git a/tools/net/ynl/pyynl/ynl_gen_c.py b/tools/net/ynl/pyynl/ynl_gen_c.py
index 7a2e49a84735..b3ce0901a19b 100755
--- a/tools/net/ynl/pyynl/ynl_gen_c.py
+++ b/tools/net/ynl/pyynl/ynl_gen_c.py
@@ -1828,7 +1828,10 @@ class CodeWriter:
if lines and nl_before:
self.nl()
for line in lines or []:
- self.p(line)
+ if line == '':
+ self.nl()
+ else:
+ self.p(line)
scalars = {'u8', 'u16', 'u32', 'u64', 's8', 's16', 's32', 's64', 'uint', 'sint'}
@@ -1921,6 +1924,15 @@ def type_name(ri, direction, deref=False):
return f"struct {op_prefix(ri, direction, deref=deref)}"
+def prepare_fixed_header(var, local_vars, init_lines):
+ local_vars += ['size_t hdr_len;',
+ 'void *hdr;']
+ init_lines += [f'hdr_len = sizeof({var}->_hdr);',
+ 'hdr = ynl_nlmsg_put_extra_header(nlh, hdr_len);',
+ f'memcpy(hdr, &{var}->_hdr, hdr_len);',
+ '']
+
+
def print_prototype(ri, direction, terminate=True, doc=None):
suffix = ';' if terminate else ''
@@ -2074,10 +2086,7 @@ def put_req_nested(ri, struct):
local_vars.append('struct nlattr *nest;')
init_lines.append("nest = ynl_attr_nest_start(nlh, attr_type);")
if struct.fixed_header:
- local_vars.append('void *hdr;')
- struct_sz = f'sizeof({struct.fixed_header})'
- init_lines.append(f"hdr = ynl_nlmsg_put_extra_header(nlh, {struct_sz});")
- init_lines.append(f"memcpy(hdr, &obj->_hdr, {struct_sz});")
+ prepare_fixed_header('obj', local_vars, init_lines)
local_vars += put_local_vars(struct)
@@ -2346,6 +2355,7 @@ def print_req(ri):
ret_ok = '0'
ret_err = '-1'
direction = "request"
+ init_lines = []
local_vars = ['struct ynl_req_state yrs = { .yarg = { .ys = ys, }, };',
'struct nlmsghdr *nlh;',
'int err;']
@@ -2356,8 +2366,7 @@ def print_req(ri):
local_vars += [f'{type_name(ri, rdir(direction))} *rsp;']
if ri.struct["request"].fixed_header:
- local_vars += ['size_t hdr_len;',
- 'void *hdr;']
+ prepare_fixed_header('req', local_vars, init_lines)
local_vars += put_local_vars(ri.struct["request"])
@@ -2376,11 +2385,7 @@ def print_req(ri):
ri.cw.p(f"yrs.yarg.rsp_policy = &{ri.struct['reply'].render_name}_nest;")
ri.cw.nl()
- if ri.struct['request'].fixed_header:
- ri.cw.p("hdr_len = sizeof(req->_hdr);")
- ri.cw.p("hdr = ynl_nlmsg_put_extra_header(nlh, hdr_len);")
- ri.cw.p("memcpy(hdr, &req->_hdr, hdr_len);")
- ri.cw.nl()
+ ri.cw.p_lines(init_lines)
for _, attr in ri.struct["request"].member_list():
attr.attr_put(ri, "req")
@@ -2418,13 +2423,13 @@ def print_dump(ri):
direction = "request"
print_prototype(ri, direction, terminate=False)
ri.cw.block_start()
+ init_lines = []
local_vars = ['struct ynl_dump_state yds = {};',
'struct nlmsghdr *nlh;',
'int err;']
if ri.struct['request'].fixed_header:
- local_vars += ['size_t hdr_len;',
- 'void *hdr;']
+ prepare_fixed_header('req', local_vars, init_lines)
if "request" in ri.op[ri.op_mode]:
local_vars += put_local_vars(ri.struct["request"])
@@ -2446,11 +2451,7 @@ def print_dump(ri):
else:
ri.cw.p(f"nlh = ynl_gemsg_start_dump(ys, {ri.nl.get_family_id()}, {ri.op.enum_name}, 1);")
- if ri.struct['request'].fixed_header:
- ri.cw.p("hdr_len = sizeof(req->_hdr);")
- ri.cw.p("hdr = ynl_nlmsg_put_extra_header(nlh, hdr_len);")
- ri.cw.p("memcpy(hdr, &req->_hdr, hdr_len);")
- ri.cw.nl()
+ ri.cw.p_lines(init_lines)
if "request" in ri.op[ri.op_mode]:
ri.cw.p(f"ys->req_policy = &{ri.struct['request'].render_name}_nest;")
--
2.51.0
Powered by blists - more mailing lists