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 12/12] tools: ynl: allow fixed-header to be specified per op
Date: Wed, 23 Apr 2025 19:12:07 -0700 [thread overview]
Message-ID: <20250424021207.1167791-13-kuba@kernel.org> (raw)
In-Reply-To: <20250424021207.1167791-1-kuba@kernel.org>
rtnetlink has variety of ops with different fixed headers.
Detect that op fixed header is not the same as family one,
and use sizeof() directly. For reverse parsing we need to
pass the fixed header len along the policy (in the socket
state).
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
tools/net/ynl/lib/ynl.h | 1 +
tools/net/ynl/lib/ynl.c | 8 ++++----
tools/net/ynl/pyynl/ynl_gen_c.py | 17 ++++++++++++++++-
3 files changed, 21 insertions(+), 5 deletions(-)
diff --git a/tools/net/ynl/lib/ynl.h b/tools/net/ynl/lib/ynl.h
index 0b4acc0d288a..5a27b8648120 100644
--- a/tools/net/ynl/lib/ynl.h
+++ b/tools/net/ynl/lib/ynl.h
@@ -80,6 +80,7 @@ struct ynl_sock {
struct nlmsghdr *nlh;
const struct ynl_policy_nest *req_policy;
+ size_t req_hdr_len;
unsigned char *tx_buf;
unsigned char *rx_buf;
unsigned char raw_buf[];
diff --git a/tools/net/ynl/lib/ynl.c b/tools/net/ynl/lib/ynl.c
index c16f01372ca3..d263f6f40ad5 100644
--- a/tools/net/ynl/lib/ynl.c
+++ b/tools/net/ynl/lib/ynl.c
@@ -191,12 +191,12 @@ ynl_ext_ack_check(struct ynl_sock *ys, const struct nlmsghdr *nlh,
n = snprintf(bad_attr, sizeof(bad_attr), "%sbad attribute: ",
str ? " (" : "");
- start = ynl_nlmsg_data_offset(ys->nlh, ys->family->hdr_len);
+ start = ynl_nlmsg_data_offset(ys->nlh, ys->req_hdr_len);
end = ynl_nlmsg_end_addr(ys->nlh);
off = ys->err.attr_offs;
off -= sizeof(struct nlmsghdr);
- off -= ys->family->hdr_len;
+ off -= ys->req_hdr_len;
n += ynl_err_walk(ys, start, end, off, ys->req_policy,
&bad_attr[n], sizeof(bad_attr) - n, NULL);
@@ -216,14 +216,14 @@ ynl_ext_ack_check(struct ynl_sock *ys, const struct nlmsghdr *nlh,
n = snprintf(miss_attr, sizeof(miss_attr), "%smissing attribute: ",
bad_attr[0] ? ", " : (str ? " (" : ""));
- start = ynl_nlmsg_data_offset(ys->nlh, ys->family->hdr_len);
+ start = ynl_nlmsg_data_offset(ys->nlh, ys->req_hdr_len);
end = ynl_nlmsg_end_addr(ys->nlh);
nest_pol = ys->req_policy;
if (tb[NLMSGERR_ATTR_MISS_NEST]) {
off = ynl_attr_get_u32(tb[NLMSGERR_ATTR_MISS_NEST]);
off -= sizeof(struct nlmsghdr);
- off -= ys->family->hdr_len;
+ off -= ys->req_hdr_len;
n += ynl_err_walk(ys, start, end, off, ys->req_policy,
&miss_attr[n], sizeof(miss_attr) - n,
diff --git a/tools/net/ynl/pyynl/ynl_gen_c.py b/tools/net/ynl/pyynl/ynl_gen_c.py
index fbcd3da72b3d..be7aaa3eef41 100755
--- a/tools/net/ynl/pyynl/ynl_gen_c.py
+++ b/tools/net/ynl/pyynl/ynl_gen_c.py
@@ -1313,8 +1313,15 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S
self.op = op
self.fixed_hdr = None
+ self.fixed_hdr_len = 'ys->family->hdr_len'
if op and op.fixed_header:
self.fixed_hdr = 'struct ' + c_lower(op.fixed_header)
+ if op.fixed_header != family.fixed_header:
+ if family.is_classic():
+ self.fixed_hdr_len = f"sizeof({self.fixed_hdr})"
+ else:
+ raise Exception(f"Per-op fixed header not supported, yet")
+
# 'do' and 'dump' response parsing is identical
self.type_consistent = True
@@ -1801,6 +1808,11 @@ _C_KW = {
if ri.fixed_hdr:
local_vars += ['void *hdr;']
iter_line = "ynl_attr_for_each(attr, nlh, yarg->ys->family->hdr_len)"
+ if ri.op.fixed_header != ri.family.fixed_header:
+ if ri.family.is_classic():
+ iter_line = f"ynl_attr_for_each(attr, nlh, sizeof({ri.fixed_hdr}))"
+ else:
+ raise Exception(f"Per-op fixed header not supported, yet")
array_nests = set()
multi_attrs = set()
@@ -2018,6 +2030,7 @@ _C_KW = {
ri.cw.p(f"nlh = ynl_gemsg_start_req(ys, {ri.nl.get_family_id()}, {ri.op.enum_name}, 1);")
ri.cw.p(f"ys->req_policy = &{ri.struct['request'].render_name}_nest;")
+ ri.cw.p(f"ys->req_hdr_len = {ri.fixed_hdr_len};")
if 'reply' in ri.op[ri.op_mode]:
ri.cw.p(f"yrs.yarg.rsp_policy = &{ri.struct['reply'].render_name}_nest;")
ri.cw.nl()
@@ -2097,6 +2110,7 @@ _C_KW = {
if "request" in ri.op[ri.op_mode]:
ri.cw.p(f"ys->req_policy = &{ri.struct['request'].render_name}_nest;")
+ ri.cw.p(f"ys->req_hdr_len = {ri.fixed_hdr_len};")
ri.cw.nl()
for _, attr in ri.struct["request"].member_list():
attr.attr_put(ri, "req")
@@ -2916,7 +2930,8 @@ _C_KW = {
cw.p(f'.is_classic\t= true,')
cw.p(f'.classic_id\t= {family.get("protonum")},')
if family.is_classic():
- cw.p(f'.hdr_len\t= sizeof(struct {c_lower(family.fixed_header)}),')
+ if family.fixed_header:
+ cw.p(f'.hdr_len\t= sizeof(struct {c_lower(family.fixed_header)}),')
elif family.fixed_header:
cw.p(f'.hdr_len\t= sizeof(struct genlmsghdr) + sizeof(struct {c_lower(family.fixed_header)}),')
else:
--
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 ` [PATCH net-next 08/12] tools: ynl-gen: mutli-attr: support binary types with struct Jakub Kicinski
2025-04-24 15:55 ` 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 ` Jakub Kicinski [this message]
2025-04-24 16:00 ` [PATCH net-next 12/12] tools: ynl: allow fixed-header to be specified per op 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-13-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).