From: Jakub Kicinski <kuba@kernel.org>
To: davem@davemloft.net
Cc: netdev@vger.kernel.org, edumazet@google.com, pabeni@redhat.com,
andrew+netdev@lunn.ch, horms@kernel.org, donald.hunter@gmail.com,
jacob.e.keller@intel.com, sdf@fomichev.me,
Jakub Kicinski <kuba@kernel.org>
Subject: [PATCH net-next 08/12] tools: ynl-gen: mutli-attr: support binary types with struct
Date: Wed, 23 Apr 2025 19:12:03 -0700 [thread overview]
Message-ID: <20250424021207.1167791-9-kuba@kernel.org> (raw)
In-Reply-To: <20250424021207.1167791-1-kuba@kernel.org>
Binary types with struct are fixed size, relatively easy to
handle for multi attr. Declare the member as a pointer.
Count the members, allocate an array, copy in the data.
Allow the netlink attr to be smaller or larger than our view
of the struct in case the build headers are newer or older
than the running kernel.
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
tools/net/ynl/pyynl/ynl_gen_c.py | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
diff --git a/tools/net/ynl/pyynl/ynl_gen_c.py b/tools/net/ynl/pyynl/ynl_gen_c.py
index de3e807c4a2c..14179b84d8ee 100755
--- a/tools/net/ynl/pyynl/ynl_gen_c.py
+++ b/tools/net/ynl/pyynl/ynl_gen_c.py
@@ -640,6 +640,8 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S
def _complex_member_type(self, ri):
if 'type' not in self.attr or self.attr['type'] == 'nest':
return self.nested_struct_type
+ elif self.attr['type'] == 'binary' and 'struct' in self.attr:
+ return None # use arg_member()
elif self.attr['type'] == 'string':
return 'struct ynl_string *'
elif self.attr['type'] in scalars:
@@ -648,6 +650,12 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S
else:
raise Exception(f"Sub-type {self.attr['type']} not supported yet")
+ def arg_member(self, ri):
+ if self.type == 'binary' and 'struct' in self.attr:
+ return [f'struct {c_lower(self.attr["struct"])} *{self.c_name}',
+ f'unsigned int n_{self.c_name}']
+ return super().arg_member(ri)
+
def free_needs_iter(self):
return self.attr['type'] in {'nest', 'string'}
@@ -655,6 +663,8 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S
lines = []
if self.attr['type'] in scalars:
lines += [f"free({var}->{ref}{self.c_name});"]
+ elif self.attr['type'] == 'binary' and 'struct' in self.attr:
+ lines += [f"free({var}->{ref}{self.c_name});"]
elif self.attr['type'] == 'string':
lines += [
f"for (i = 0; i < {var}->{ref}n_{self.c_name}; i++)",
@@ -685,6 +695,9 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S
put_type = self.type
ri.cw.p(f"for (i = 0; i < {var}->n_{self.c_name}; i++)")
ri.cw.p(f"ynl_attr_put_{put_type}(nlh, {self.enum_name}, {var}->{self.c_name}[i]);")
+ elif self.attr['type'] == 'binary' and 'struct' in self.attr:
+ ri.cw.p(f"for (i = 0; i < {var}->n_{self.c_name}; i++)")
+ ri.cw.p(f"ynl_attr_put(nlh, {self.enum_name}, &{var}->{self.c_name}[i], sizeof(struct {c_lower(self.attr['struct'])}));")
elif self.attr['type'] == 'string':
ri.cw.p(f"for (i = 0; i < {var}->n_{self.c_name}; i++)")
ri.cw.p(f"ynl_attr_put_str(nlh, {self.enum_name}, {var}->{self.c_name}[i]->str);")
@@ -1849,6 +1862,12 @@ _C_KW = {
ri.cw.p('return YNL_PARSE_CB_ERROR;')
elif aspec.type in scalars:
ri.cw.p(f"dst->{aspec.c_name}[i] = ynl_attr_get_{aspec.type}(attr);")
+ elif aspec.type == 'binary' and 'struct' in aspec:
+ ri.cw.p('size_t len = ynl_attr_data_len(attr);')
+ ri.cw.nl()
+ ri.cw.p(f'if (len > sizeof(dst->{aspec.c_name}[0]))')
+ ri.cw.p(f'len = sizeof(dst->{aspec.c_name}[0]);')
+ ri.cw.p(f"memcpy(&dst->{aspec.c_name}[i], ynl_attr_data(attr), len);")
elif aspec.type == 'string':
ri.cw.p('unsigned int len;')
ri.cw.nl()
--
2.49.0
next prev parent reply other threads:[~2025-04-24 2:12 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-24 2:11 [PATCH net-next 00/12] tools: ynl-gen: additional C types and classic netlink handling Jakub Kicinski
2025-04-24 2:11 ` [PATCH net-next 01/12] tools: ynl-gen: fix comment about nested struct dict Jakub Kicinski
2025-04-24 15:40 ` Jacob Keller
2025-04-24 2:11 ` [PATCH net-next 02/12] tools: ynl-gen: factor out free_needs_iter for a struct Jakub Kicinski
2025-04-24 15:42 ` Jacob Keller
2025-04-24 2:11 ` [PATCH net-next 03/12] tools: ynl-gen: fill in missing empty attr lists Jakub Kicinski
2025-04-24 15:49 ` Jacob Keller
2025-04-25 2:36 ` Jakub Kicinski
2025-04-24 2:11 ` [PATCH net-next 04/12] tools: ynl: let classic netlink requests specify extra nlflags Jakub Kicinski
2025-04-24 15:51 ` Jacob Keller
2025-04-24 2:12 ` [PATCH net-next 05/12] tools: ynl-gen: support using dump types for ntf Jakub Kicinski
2025-04-24 15:51 ` Jacob Keller
2025-04-24 2:12 ` [PATCH net-next 06/12] tools: ynl-gen: support CRUD-like notifications for classic Netlink Jakub Kicinski
2025-04-24 15:52 ` Jacob Keller
2025-04-24 2:12 ` [PATCH net-next 07/12] tools: ynl-gen: multi-attr: type gen for string Jakub Kicinski
2025-04-24 15:54 ` Jacob Keller
2025-04-24 2:12 ` Jakub Kicinski [this message]
2025-04-24 15:55 ` [PATCH net-next 08/12] tools: ynl-gen: mutli-attr: support binary types with struct Jacob Keller
2025-04-24 2:12 ` [PATCH net-next 09/12] tools: ynl-gen: array-nest: support put for scalar Jakub Kicinski
2025-04-24 15:56 ` Jacob Keller
2025-04-24 2:12 ` [PATCH net-next 10/12] tools: ynl-gen: array-nest: support binary array with exact-len Jakub Kicinski
2025-04-24 15:57 ` Jacob Keller
2025-04-24 2:12 ` [PATCH net-next 11/12] tools: ynl-gen: don't init enum checks for classic netlink Jakub Kicinski
2025-04-24 15:59 ` Jacob Keller
2025-04-25 2:36 ` Jakub Kicinski
2025-04-24 2:12 ` [PATCH net-next 12/12] tools: ynl: allow fixed-header to be specified per op Jakub Kicinski
2025-04-24 16:00 ` Jacob Keller
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250424021207.1167791-9-kuba@kernel.org \
--to=kuba@kernel.org \
--cc=andrew+netdev@lunn.ch \
--cc=davem@davemloft.net \
--cc=donald.hunter@gmail.com \
--cc=edumazet@google.com \
--cc=horms@kernel.org \
--cc=jacob.e.keller@intel.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=sdf@fomichev.me \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).