Skip to contentSkip to conversation

tools: ynl: c: basic netlink-raw support

PATCH In review Jakub Kicinski 5 participants 20 messages Apr 10, 2025 – Apr 18, 2025

Subsystems (6)
lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

v2 cover tools: ynl: c: basic netlink-raw support

Basic support for netlink-raw AKA classic netlink in user space C codegen. This series is enough to read routes and addresses from the kernel (see the samples in patches 12 and 13).

Specs need to be slightly adjusted and decorated with the c naming info.

In terms of codegen this series includes just the basic plumbing required to skip genlmsghdr and handle request types which may technically also be legal in genetlink-legacy but are very uncommon there.

Subsequent series will add support for:
- handling CRUD-style notifications
- code gen for array types classic netlink uses
- sub-message support

v2:
- extend commit messages
- [patch 10] fix typo in member name
- [patch 12] remove unused arg
v1: https://lore.kernel.org/[email protected]

Jakub Kicinski (13):
netlink: specs: rename rtnetlink specs in accordance with family name
netlink: specs: rt-route: specify fixed-header at operations level
netlink: specs: rt-addr: remove the fixed members from attrs
netlink: specs: rt-route: remove the fixed members from attrs
netlink: specs: rt-addr: add C naming info
netlink: specs: rt-route: add C naming info
tools: ynl: support creating non-genl sockets
tools: ynl-gen: don't consider requests with fixed hdr empty
tools: ynl: don't use genlmsghdr in classic netlink
tools: ynl-gen: consider dump ops without a do "type-consistent"
tools: ynl-gen: use family c-name in notifications
tools: ynl: generate code for rt-addr and add a sample
tools: ynl: generate code for rt-route and add a sample

 .../specs/{rt_addr.yaml => rt-addr.yaml}      | 24 ++----
 .../specs/{rt_link.yaml => rt-link.yaml}      |  0
 .../specs/{rt_neigh.yaml => rt-neigh.yaml}    |  0
 .../specs/{rt_route.yaml => rt-route.yaml}    | 22 ++---
 .../specs/{rt_rule.yaml => rt-rule.yaml}      |  0
 .../userspace-api/netlink/netlink-raw.rst     |  2 +-
 tools/net/ynl/Makefile.deps                   |  2 +
 tools/net/ynl/generated/Makefile              |  2 +-
 tools/net/ynl/lib/ynl-priv.h                  |  3 +
 tools/net/ynl/lib/ynl.h                       |  3 +
 tools/net/ynl/lib/ynl.c                       | 59 +++++++++-----
 tools/net/ynl/samples/rt-addr.c               | 80 +++++++++++++++++++
 tools/net/ynl/samples/rt-route.c              | 80 +++++++++++++++++++
 tools/net/ynl/pyynl/ynl_gen_c.py              | 55 +++++++++----
 tools/net/ynl/samples/.gitignore              |  4 +-
 tools/testing/selftests/net/lib/py/ynl.py     |  4 +-
 16 files changed, 263 insertions(+), 77 deletions(-)
 rename Documentation/netlink/specs/{rt_addr.yaml => rt-addr.yaml} (89%)
 rename Documentation/netlink/specs/{rt_link.yaml => rt-link.yaml} (100%)
 rename Documentation/netlink/specs/{rt_neigh.yaml => rt-neigh.yaml} (100%)
 rename Documentation/netlink/specs/{rt_route.yaml => rt-route.yaml} (93%)
 rename Documentation/netlink/specs/{rt_rule.yaml => rt-rule.yaml} (100%)
 create mode 100644 tools/net/ynl/samples/rt-addr.c
 create mode 100644 tools/net/ynl/samples/rt-route.c
Signature · 1 line
2.49.0
author maintainer
#1 lore raw

PATCH v2 1/13 netlink: specs: rename rtnetlink specs in accordance with family name

The rtnetlink family names are set to rt-$name within the YAML but the files are called rt_$name. C codegen assumes that the generated file name will match the family. The use of dashes is in line with our general expectation that name properties in the spec use dashes not underscores (even tho, as Donald points out most genl families use underscores in the name).

We have 3 un-ideal options to choose from:

- accept the slight inconsistency with old families using _, or
- accept the slight annoyance with all languages having to do s/-/_/ when looking up family ID, or
- accept the inconsistency with all name properties in new YAML spec being separated with - and just the family name always using _.

Pick option 1 and rename the rtnl spec files.

---
v2: extend commit msg

---
 Documentation/netlink/specs/{rt_addr.yaml => rt-addr.yaml}   | 0
 Documentation/netlink/specs/{rt_link.yaml => rt-link.yaml}   | 0
 Documentation/netlink/specs/{rt_neigh.yaml => rt-neigh.yaml} | 0
 Documentation/netlink/specs/{rt_route.yaml => rt-route.yaml} | 0
 Documentation/netlink/specs/{rt_rule.yaml => rt-rule.yaml}   | 0
 Documentation/userspace-api/netlink/netlink-raw.rst          | 2 +-
 tools/testing/selftests/net/lib/py/ynl.py                    | 4 ++--
 7 files changed, 3 insertions(+), 3 deletions(-)
 rename Documentation/netlink/specs/{rt_addr.yaml => rt-addr.yaml} (100%)
 rename Documentation/netlink/specs/{rt_link.yaml => rt-link.yaml} (100%)
 rename Documentation/netlink/specs/{rt_neigh.yaml => rt-neigh.yaml} (100%)
 rename Documentation/netlink/specs/{rt_route.yaml => rt-route.yaml} (100%)
 rename Documentation/netlink/specs/{rt_rule.yaml => rt-rule.yaml} (100%)
Documentation/netlink/specs/rt-addr.yaml renamed from Documentation/netlink/specs/rt_addr.yaml +0 -0
similarity index 100%
rename from Documentation/netlink/specs/rt_addr.yaml
rename to Documentation/netlink/specs/rt-addr.yaml
Documentation/netlink/specs/rt-link.yaml renamed from Documentation/netlink/specs/rt_link.yaml +0 -0
similarity index 100%
rename from Documentation/netlink/specs/rt_link.yaml
rename to Documentation/netlink/specs/rt-link.yaml
Documentation/netlink/specs/rt-neigh.yaml renamed from Documentation/netlink/specs/rt_neigh.yaml +0 -0
similarity index 100%
rename from Documentation/netlink/specs/rt_neigh.yaml
rename to Documentation/netlink/specs/rt-neigh.yaml
Documentation/netlink/specs/rt-route.yaml renamed from Documentation/netlink/specs/rt_route.yaml +0 -0
similarity index 100%
rename from Documentation/netlink/specs/rt_route.yaml
rename to Documentation/netlink/specs/rt-route.yaml
Documentation/netlink/specs/rt-rule.yaml renamed from Documentation/netlink/specs/rt_rule.yaml +0 -0
similarity index 100%
rename from Documentation/netlink/specs/rt_rule.yaml
rename to Documentation/netlink/specs/rt-rule.yaml
Documentation/userspace-api/netlink/netlink-raw.rst +1 -1
index 1990eea772d0..31fc91020eb3 100644
@@ -62,7 +62,7 @@ Sub-messages
 ------------
 
 Several raw netlink families such as
-:doc:`rt_link<../../networking/netlink_spec/rt_link>` and
+:doc:`rt-link<../../networking/netlink_spec/rt-link>` and
 :doc:`tc<../../networking/netlink_spec/tc>` use attribute nesting as an
 abstraction to carry module specific information.
 
tools/testing/selftests/net/lib/py/ynl.py +2 -2
index 8986c584cb37..6329ae805abf 100644
@@ -39,12 +39,12 @@ from .ksft import ksft_pr, ktap_result
 
 class RtnlFamily(YnlFamily):
     def __init__(self, recv_size=0):
-        super().__init__((SPEC_PATH / Path('rt_link.yaml')).as_posix(),
+        super().__init__((SPEC_PATH / Path('rt-link.yaml')).as_posix(),
                          schema='', recv_size=recv_size)
 
 class RtnlAddrFamily(YnlFamily):
     def __init__(self, recv_size=0):
-        super().__init__((SPEC_PATH / Path('rt_addr.yaml')).as_posix(),
+        super().__init__((SPEC_PATH / Path('rt-addr.yaml')).as_posix(),
                          schema='', recv_size=recv_size)
 
 class NetdevFamily(YnlFamily):
Signature · 1 line
2.49.0
author maintainer
#5 lore raw

PATCH v2 2/13 netlink: specs: rt-route: specify fixed-header at operations level

In reply to Jakub Kicinski

The C codegen currently stores the fixed-header as part of family info, so it only supports one fixed-header type per spec. Luckily all rtm route message have the same fixed header so just move it up to the higher level.

---
 Documentation/netlink/specs/rt-route.yaml | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Documentation/netlink/specs/rt-route.yaml +1 -3
index 292469c7d4b9..6fa3fa24305e 100644
@@ -245,12 +245,12 @@ protonum: 0
 
 operations:
   enum-model: directional
+  fixed-header: rtmsg
   list:
     -
       name: getroute
       doc: Dump route information.
       attribute-set: route-attrs
-      fixed-header: rtmsg
       do:
         request:
           value: 26
@@ -320,7 +320,6 @@ protonum: 0
       name: newroute
       doc: Create a new route
       attribute-set: route-attrs
-      fixed-header: rtmsg
       do:
         request:
           value: 24
@@ -329,7 +328,6 @@ protonum: 0
       name: delroute
       doc: Delete an existing route
       attribute-set: route-attrs
-      fixed-header: rtmsg
       do:
         request:
           value: 25
Signature · 1 line
2.49.0
author maintainer
#6 lore raw

PATCH v2 3/13 netlink: specs: rt-addr: remove the fixed members from attrs

In reply to Jakub Kicinski

The purpose of the attribute list is to list the attributes which will be included in a given message to shrink the objects for families with huge attr spaces. Fixed headers are always present in their entirety (between netlink header and the attrs) so there's no point in listing their members. Current C codegen doesn't expect them and tries to look them up in the attribute space.

---
v2: reword the commit msg slightly

---
 Documentation/netlink/specs/rt-addr.yaml | 20 +++-----------------
 1 file changed, 3 insertions(+), 17 deletions(-)
Documentation/netlink/specs/rt-addr.yaml +3 -17
index df6b23f06a22..0488ce87506c 100644
@@ -133,11 +133,6 @@ protonum: 0
         request:
           value: 20
           attributes: &ifaddr-all
-            - ifa-family
-            - ifa-flags
-            - ifa-prefixlen
-            - ifa-scope
-            - ifa-index
             - address
             - label
             - local
@@ -150,11 +145,6 @@ protonum: 0
         request:
           value: 21
           attributes:
-            - ifa-family
-            - ifa-flags
-            - ifa-prefixlen
-            - ifa-scope
-            - ifa-index
             - address
             - local
     -
@@ -164,8 +154,7 @@ protonum: 0
       dump:
         request:
           value: 22
-          attributes:
-            - ifa-index
+          attributes: []
         reply:
           value: 20
           attributes: *ifaddr-all
@@ -177,9 +166,7 @@ protonum: 0
       do:
         request:
           value: 58
-          attributes:
-            - ifa-family
-            - ifa-index
+          attributes: []
         reply:
           value: 58
           attributes: &mcaddr-attrs
@@ -188,8 +175,7 @@ protonum: 0
       dump:
         request:
           value: 58
-          attributes:
-            - ifa-family
+          attributes: []
         reply:
           value: 58
           attributes: *mcaddr-attrs
Signature · 1 line
2.49.0
author maintainer
#7 lore raw

PATCH v2 4/13 netlink: specs: rt-route: remove the fixed members from attrs

In reply to Jakub Kicinski

The purpose of the attribute list is to list the attributes which will be included in a given message to shrink the objects for families with huge attr spaces. Fixed headers are always present in their entirety (between netlink header and the attrs) so there's no point in listing their members. Current C codegen doesn't expect them and tries to look them up in the attribute space.

---
 Documentation/netlink/specs/rt-route.yaml | 15 +--------------
 1 file changed, 1 insertion(+), 14 deletions(-)
Documentation/netlink/specs/rt-route.yaml +1 -14
index 6fa3fa24305e..c7c6f776ab2f 100644
@@ -255,11 +255,8 @@ protonum: 0
         request:
           value: 26
           attributes:
-            - rtm-family
             - src
-            - rtm-src-len
             - dst
-            - rtm-dst-len
             - iif
             - oif
             - ip-proto
@@ -271,15 +268,6 @@ protonum: 0
         reply:
           value: 24
           attributes: &all-route-attrs
-            - rtm-family
-            - rtm-dst-len
-            - rtm-src-len
-            - rtm-tos
-            - rtm-table
-            - rtm-protocol
-            - rtm-scope
-            - rtm-type
-            - rtm-flags
             - dst
             - src
             - iif
@@ -311,8 +299,7 @@ protonum: 0
       dump:
         request:
           value: 26
-          attributes:
-            - rtm-family
+          attributes: []
         reply:
           value: 24
           attributes: *all-route-attrs
Signature · 1 line
2.49.0
author maintainer
#8 lore raw

PATCH v2 5/13 netlink: specs: rt-addr: add C naming info

In reply to Jakub Kicinski

Add properties needed for C codegen to match names with uAPI headers.

---
 Documentation/netlink/specs/rt-addr.yaml | 4 ++++
 1 file changed, 4 insertions(+)
Documentation/netlink/specs/rt-addr.yaml +4 -0
index 0488ce87506c..4f86aa1075da 100644
@@ -2,6 +2,7 @@
 
 name: rt-addr
 protocol: netlink-raw
+uapi-header: linux/rtnetlink.h
 protonum: 0
 
 doc:
@@ -49,6 +50,8 @@ protonum: 0
   -
     name: ifa-flags
     type: flags
+    name-prefix: ifa-f-
+    enum-name:
     entries:
       -
         name: secondary
@@ -124,6 +127,7 @@ protonum: 0
 operations:
   fixed-header: ifaddrmsg
   enum-model: directional
+  name-prefix: rtm-
   list:
     -
       name: newaddr
Signature · 1 line
2.49.0
author maintainer
#9 lore raw

PATCH v2 6/13 netlink: specs: rt-route: add C naming info

In reply to Jakub Kicinski

Add properties needed for C codegen to match names with uAPI headers.

---
 Documentation/netlink/specs/rt-route.yaml | 3 +++
 1 file changed, 3 insertions(+)
Documentation/netlink/specs/rt-route.yaml +3 -0
index c7c6f776ab2f..800f3a823d47 100644
@@ -2,6 +2,7 @@
 
 name: rt-route
 protocol: netlink-raw
+uapi-header: linux/rtnetlink.h
 protonum: 0
 
 doc:
@@ -11,6 +12,7 @@ protonum: 0
   -
     name: rtm-type
     name-prefix: rtn-
+    enum-name:
     type: enum
     entries:
       - unspec
@@ -246,6 +248,7 @@ protonum: 0
 operations:
   enum-model: directional
   fixed-header: rtmsg
+  name-prefix: rtm-
   list:
     -
       name: getroute
Signature · 1 line
2.49.0
author maintainer
#10 lore raw

PATCH v2 7/13 tools: ynl: support creating non-genl sockets

In reply to Jakub Kicinski

Classic netlink has static family IDs specified in YAML, there is no family name -> ID lookup. Support providing the ID info to the library via the generated struct and make library use it. Since NETLINK_ROUTE is ID 0 we need an extra boolean to indicate classic_id is to be used.

---
 tools/net/ynl/lib/ynl.h          |  3 ++
 tools/net/ynl/lib/ynl.c          | 51 +++++++++++++++++++++-----------
 tools/net/ynl/pyynl/ynl_gen_c.py |  9 ++++--
 3 files changed, 43 insertions(+), 20 deletions(-)
tools/net/ynl/lib/ynl.h +3 -0
index 6cd570b283ea..59256e258130 100644
@@ -2,6 +2,7 @@
 #ifndef __YNL_C_H
 #define __YNL_C_H 1
 
+#include <stdbool.h>
 #include <stddef.h>
 #include <linux/genetlink.h>
 #include <linux/types.h>
@@ -48,6 +49,8 @@ struct ynl_family {
 /* private: */
 	const char *name;
 	size_t hdr_len;
+	bool is_classic;
+	__u16 classic_id;
 	const struct ynl_ntf_info *ntf_info;
 	unsigned int ntf_info_size;
 };
tools/net/ynl/lib/ynl.c +34 -17

93 lines not on this page · raw message

tools/net/ynl/pyynl/ynl_gen_c.py +6 -3
index a1427c537030..9e00aac4801c 100755
@@ -971,9 +971,6 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S
     def resolve(self):
         self.resolve_up(super())
 
-        if self.yaml.get('protocol', 'genetlink') not in {'genetlink', 'genetlink-c', 'genetlink-legacy'}:
-            raise Exception("Codegen only supported for genetlink")
-
         self.c_name = c_lower(self.ident_name)
         if 'name-prefix' in self.yaml['operations']:
             self.op_prefix = c_upper(self.yaml['operations']['name-prefix'])
@@ -1020,6 +1017,9 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S
     def new_operation(self, elem, req_value, rsp_value):
         return Operation(self, elem, req_value, rsp_value)
 
+    def is_classic(self):
+        return self.proto == 'netlink-raw'
+
     def _mark_notify(self):
         for op in self.msgs.values():
             if 'notify' in op:
@@ -2730,6 +2730,9 @@ _C_KW = {
 
     cw.block_start(f'{symbol} = ')
     cw.p(f'.name\t\t= "{family.c_name}",')
+    if family.is_classic():
+        cw.p(f'.is_classic\t= true,')
+        cw.p(f'.classic_id\t= {family.get("protonum")},')
     if family.fixed_header:
         cw.p(f'.hdr_len\t= sizeof(struct genlmsghdr) + sizeof(struct {c_lower(family.fixed_header)}),')
     else:
Signature · 1 line
2.49.0
author maintainer
#11 lore raw

PATCH v2 8/13 tools: ynl-gen: don't consider requests with fixed hdr empty

In reply to Jakub Kicinski

C codegen skips generating the structs if request/reply has no attrs. In such cases the request op takes no argument and return int (rather than response struct). In case of classic netlink a lot of information gets passed using the fixed struct, however, so adjust the logic to consider a request empty only if it has no attrs _and_ no fixed struct.

---
 tools/net/ynl/pyynl/ynl_gen_c.py | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
tools/net/ynl/pyynl/ynl_gen_c.py +5 -2
index 9e00aac4801c..04f1ac62cb01 100755
@@ -1247,6 +1247,9 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S
         if op_mode == 'event':
             self.struct['reply'] = Struct(family, self.attr_set, type_list=op['event']['attributes'])
 
+    def type_empty(self, key):
+        return len(self.struct[key].attr_list) == 0 and self.fixed_hdr is None
+
 
 class CodeWriter:
     def __init__(self, nlib, out_file=None, overwrite=True):
@@ -2034,7 +2037,7 @@ _C_KW = {
 
 
 def print_req_type_helpers(ri):
-    if len(ri.struct["request"].attr_list) == 0:
+    if ri.type_empty("request"):
         return
     print_alloc_wrapper(ri, "request")
     print_type_helpers(ri, "request")
@@ -2057,7 +2060,7 @@ _C_KW = {
 
 
 def print_req_type(ri):
-    if len(ri.struct["request"].attr_list) == 0:
+    if ri.type_empty("request"):
         return
     print_type(ri, "request")
 
Signature · 1 line
2.49.0
author maintainer
#12 lore raw

PATCH v2 9/13 tools: ynl: don't use genlmsghdr in classic netlink

In reply to Jakub Kicinski

Make sure the codegen calls the right YNL lib helper to start the request based on family type. Classic netlink request must not include the genl header.

Conversely don't expect genl headers in the responses.

---
 tools/net/ynl/lib/ynl-priv.h     |  3 +++
 tools/net/ynl/lib/ynl.c          |  8 ++++----
 tools/net/ynl/pyynl/ynl_gen_c.py | 19 +++++++++++++++----
 3 files changed, 22 insertions(+), 8 deletions(-)
tools/net/ynl/lib/ynl-priv.h +3 -0
index 3c09a7bbfba5..634eb16548b9 100644
@@ -94,6 +94,9 @@ struct ynl_ntf_base_type {
 	unsigned char data[] __attribute__((aligned(8)));
 };
 
+struct nlmsghdr *ynl_msg_start_req(struct ynl_sock *ys, __u32 id);
+struct nlmsghdr *ynl_msg_start_dump(struct ynl_sock *ys, __u32 id);
+
 struct nlmsghdr *
 ynl_gemsg_start_req(struct ynl_sock *ys, __u32 id, __u8 cmd, __u8 version);
 struct nlmsghdr *
tools/net/ynl/lib/ynl.c +4 -4
index b9fda1a99453..70f899a54007 100644
@@ -451,14 +451,14 @@ ynl_gemsg_start(struct ynl_sock *ys, __u32 id, __u16 flags,
 	return nlh;
 }
 
-void ynl_msg_start_req(struct ynl_sock *ys, __u32 id)
+struct nlmsghdr *ynl_msg_start_req(struct ynl_sock *ys, __u32 id)
 {
-	ynl_msg_start(ys, id, NLM_F_REQUEST | NLM_F_ACK);
+	return ynl_msg_start(ys, id, NLM_F_REQUEST | NLM_F_ACK);
 }
 
-void ynl_msg_start_dump(struct ynl_sock *ys, __u32 id)
+struct nlmsghdr *ynl_msg_start_dump(struct ynl_sock *ys, __u32 id)
 {
-	ynl_msg_start(ys, id, NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP);
+	return ynl_msg_start(ys, id, NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP);
 }
 
 struct nlmsghdr *
tools/net/ynl/pyynl/ynl_gen_c.py +15 -4
index 04f1ac62cb01..b0b47a493a86 100755
@@ -1710,7 +1710,10 @@ _C_KW = {
         ri.cw.p(f'dst->{arg} = {arg};')
 
     if ri.fixed_hdr:
-        ri.cw.p('hdr = ynl_nlmsg_data_offset(nlh, sizeof(struct genlmsghdr));')
+        if ri.family.is_classic():
+            ri.cw.p('hdr = ynl_nlmsg_data(nlh);')
+        else:
+            ri.cw.p('hdr = ynl_nlmsg_data_offset(nlh, sizeof(struct genlmsghdr));')
         ri.cw.p(f"memcpy(&dst->_hdr, hdr, sizeof({ri.fixed_hdr}));")
     for anest in sorted(all_multi):
         aspec = struct[anest]
@@ -1857,7 +1860,10 @@ _C_KW = {
     ri.cw.block_start()
     ri.cw.write_func_lvar(local_vars)
 
-    ri.cw.p(f"nlh = ynl_gemsg_start_req(ys, {ri.nl.get_family_id()}, {ri.op.enum_name}, 1);")
+    if ri.family.is_classic():
+        ri.cw.p(f"nlh = ynl_msg_start_req(ys, {ri.op.enum_name});")
+    else:
+        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;")
     if 'reply' in ri.op[ri.op_mode]:
@@ -1926,7 +1932,10 @@ _C_KW = {
     else:
         ri.cw.p(f'yds.rsp_cmd = {ri.op.rsp_value};')
     ri.cw.nl()
-    ri.cw.p(f"nlh = ynl_gemsg_start_dump(ys, {ri.nl.get_family_id()}, {ri.op.enum_name}, 1);")
+    if ri.family.is_classic():
+        ri.cw.p(f"nlh = ynl_msg_start_dump(ys, {ri.op.enum_name});")
+    else:
+        ri.cw.p(f"nlh = ynl_gemsg_start_dump(ys, {ri.nl.get_family_id()}, {ri.op.enum_name}, 1);")
 
     if ri.fixed_hdr:
         ri.cw.p("hdr_len = sizeof(req->_hdr);")
@@ -2736,7 +2745,9 @@ _C_KW = {
     if family.is_classic():
         cw.p(f'.is_classic\t= true,')
         cw.p(f'.classic_id\t= {family.get("protonum")},')
-    if family.fixed_header:
+    if family.is_classic():
+        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:
         cw.p('.hdr_len\t= sizeof(struct genlmsghdr),')
Signature · 1 line
2.49.0
author maintainer
#13 lore raw

PATCH v2 10/13 tools: ynl-gen: consider dump ops without a do "type-consistent"

In reply to Jakub Kicinski

If the type for the response to do and dump are the same we don't generate it twice. This is called "type_consistent" in the generator. Consider operations which only have dump to also be consistent. This removes unnecessary "_dump" from the names. There's a number of GET ops in classic Netlink which only have dump handlers.

Make sure we output the "onesided" types, normally if the type is consistent we only output it when we render the do structures.

---
v2:
- s/onside/oneside/
- extend commit msg

---
 tools/net/ynl/pyynl/ynl_gen_c.py | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
tools/net/ynl/pyynl/ynl_gen_c.py +9 -5
index b0b47a493a86..efed498ccb35 100755
@@ -1212,6 +1212,7 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S
 
         # 'do' and 'dump' response parsing is identical
         self.type_consistent = True
+        self.type_oneside = False
         if op_mode != 'do' and 'dump' in op:
             if 'do' in op:
                 if ('reply' in op['do']) != ('reply' in op["dump"]):
@@ -1219,7 +1220,8 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S
                 elif 'reply' in op['do'] and op["do"]["reply"] != op["dump"]["reply"]:
                     self.type_consistent = False
             else:
-                self.type_consistent = False
+                self.type_consistent = True
+                self.type_oneside = True
 
         self.attr_set = attr_set
         if not self.attr_set:
@@ -1516,7 +1518,9 @@ _C_KW = {
         suffix += f"{direction_to_suffix[direction]}"
     else:
         if direction == 'request':
-            suffix += '_req_dump'
+            suffix += '_req'
+            if not ri.type_oneside:
+                suffix += '_dump'
         else:
             if ri.type_consistent:
                 if deref:
@@ -1995,7 +1999,7 @@ _C_KW = {
     if not direction and ri.type_name_conflict:
         suffix += '_'
 
-    if ri.op_mode == 'dump':
+    if ri.op_mode == 'dump' and not ri.type_oneside:
         suffix += '_dump'
 
     ri.cw.block_start(line=f"struct {ri.family.c_name}{suffix}")
@@ -2979,7 +2983,7 @@ _C_KW = {
                     ri = RenderInfo(cw, parsed, args.mode, op, 'dump')
                     print_req_type(ri)
                     print_req_type_helpers(ri)
-                    if not ri.type_consistent:
+                    if not ri.type_consistent or ri.type_oneside:
                         print_rsp_type(ri)
                     print_wrapped_type(ri)
                     print_dump_prototype(ri)
@@ -3057,7 +3061,7 @@ _C_KW = {
                 if 'dump' in op:
                     cw.p(f"/* {op.enum_name} - dump */")
                     ri = RenderInfo(cw, parsed, args.mode, op, "dump")
-                    if not ri.type_consistent:
+                    if not ri.type_consistent or ri.type_oneside:
                         parse_rsp_msg(ri, deref=True)
                     print_req_free(ri)
                     print_dump_type_free(ri)
Signature · 1 line
2.49.0
author maintainer
#14 lore raw

PATCH v2 11/13 tools: ynl-gen: use family c-name in notifications

In reply to Jakub Kicinski

Family names may include dashes. Fix notification handling code gen to the c-compatible name.

---
 tools/net/ynl/pyynl/ynl_gen_c.py | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/net/ynl/pyynl/ynl_gen_c.py +3 -3
index efed498ccb35..a9966ff2a143 100755
@@ -2726,7 +2726,7 @@ _C_KW = {
         return
 
     if family.ntfs:
-        cw.block_start(line=f"static const struct ynl_ntf_info {family['name']}_ntf_info[] = ")
+        cw.block_start(line=f"static const struct ynl_ntf_info {family.c_name}_ntf_info[] = ")
         for ntf_op_name, ntf_op in family.ntfs.items():
             if 'notify' in ntf_op:
                 op = family.ops[ntf_op['notify']]
@@ -2756,8 +2756,8 @@ _C_KW = {
     else:
         cw.p('.hdr_len\t= sizeof(struct genlmsghdr),')
     if family.ntfs:
-        cw.p(f".ntf_info\t= {family['name']}_ntf_info,")
-        cw.p(f".ntf_info_size\t= YNL_ARRAY_SIZE({family['name']}_ntf_info),")
+        cw.p(f".ntf_info\t= {family.c_name}_ntf_info,")
+        cw.p(f".ntf_info_size\t= YNL_ARRAY_SIZE({family.c_name}_ntf_info),")
     cw.block_end(line=';')
 
 
Signature · 1 line
2.49.0
author maintainer
#15 lore raw

PATCH v2 12/13 tools: ynl: generate code for rt-addr and add a sample

In reply to Jakub Kicinski

YNL C can now generate code for simple classic netlink families. Include rt-addr in the Makefile for generation and add a sample.

  $ ./tools/net/ynl/samples/rt-addr
              lo: 127.0.0.1
       wlp0s20f3: 192.168.1.101
              lo: ::
       wlp0s20f3: fe80::6385:be6:746e:8116
            vpn0: fe80::3597:d353:b5a7:66dd

---
v2: remove unused function argument

---
 tools/net/ynl/Makefile.deps      |  1 +
 tools/net/ynl/generated/Makefile |  2 +-
 tools/net/ynl/samples/rt-addr.c  | 80 ++++++++++++++++++++++++++++++++
 tools/net/ynl/samples/.gitignore |  3 +-
 4 files changed, 84 insertions(+), 2 deletions(-)
 create mode 100644 tools/net/ynl/samples/rt-addr.c
tools/net/ynl/Makefile.deps +1 -0
index f3269ce39e5b..e55d94211df6 100644
@@ -29,4 +29,5 @@ CFLAGS_nfsd:=$(call get_hdr_inc,_LINUX_NFSD_NETLINK_H,nfsd_netlink.h)
 CFLAGS_ovs_datapath:=$(call get_hdr_inc,__LINUX_OPENVSWITCH_H,openvswitch.h)
 CFLAGS_ovs_flow:=$(call get_hdr_inc,__LINUX_OPENVSWITCH_H,openvswitch.h)
 CFLAGS_ovs_vport:=$(call get_hdr_inc,__LINUX_OPENVSWITCH_H,openvswitch.h)
+CFLAGS_rt-addr:=$(call get_hdr_inc,__LINUX_RTNETLINK_H,rtnetlink.h)
 CFLAGS_tcp_metrics:=$(call get_hdr_inc,_LINUX_TCP_METRICS_H,tcp_metrics.h)
tools/net/ynl/generated/Makefile +1 -1
index 21f9e299dc75..67ce3b8988ef 100644
@@ -25,7 +25,7 @@ SPECS_DIR:=../../../../Documentation/netlink/specs
 GENS_PATHS=$(shell grep -nrI --files-without-match \
 		'protocol: netlink' \
 		$(SPECS_DIR))
-GENS=$(patsubst $(SPECS_DIR)/%.yaml,%,${GENS_PATHS})
+GENS=$(patsubst $(SPECS_DIR)/%.yaml,%,${GENS_PATHS}) rt-addr
 SRCS=$(patsubst %,%-user.c,${GENS})
 HDRS=$(patsubst %,%-user.h,${GENS})
 OBJS=$(patsubst %,%-user.o,${GENS})
tools/net/ynl/samples/rt-addr.c new file +80 -0

80 lines not on this page · raw message

tools/net/ynl/samples/.gitignore +2 -1
index dda6686257a7..2bc8721d6144 100644
@@ -2,4 +2,5 @@ ethtool
 devlink
 netdev
 ovs
-page-pool
\ No newline at end of file
+page-pool
+rt-addr
Signature · 1 line
2.49.0
author maintainer
#18 lore raw

PATCH v2 13/13 tools: ynl: generate code for rt-route and add a sample

In reply to Jakub Kicinski

YNL C can now generate code for simple classic netlink families. Include rt-route in the Makefile for generation and add a sample.

    $ ./tools/net/ynl/samples/rt-route
    oif: wlp0s20f3        gateway: 192.168.1.1
    oif: wlp0s20f3        dst: 192.168.1.0/24
    oif: vpn0             dst: fe80::/64
    oif: wlp0s20f3        dst: fe80::/64
    oif: wlp0s20f3        gateway: fe80::200:5eff:fe00:201
---
 tools/net/ynl/Makefile.deps      |  1 +
 tools/net/ynl/generated/Makefile |  2 +-
 tools/net/ynl/samples/rt-route.c | 80 ++++++++++++++++++++++++++++++++
 tools/net/ynl/samples/.gitignore |  1 +
 4 files changed, 83 insertions(+), 1 deletion(-)
 create mode 100644 tools/net/ynl/samples/rt-route.c
tools/net/ynl/Makefile.deps +1 -0
index e55d94211df6..385783489f84 100644
@@ -30,4 +30,5 @@ CFLAGS_ovs_datapath:=$(call get_hdr_inc,__LINUX_OPENVSWITCH_H,openvswitch.h)
 CFLAGS_ovs_flow:=$(call get_hdr_inc,__LINUX_OPENVSWITCH_H,openvswitch.h)
 CFLAGS_ovs_vport:=$(call get_hdr_inc,__LINUX_OPENVSWITCH_H,openvswitch.h)
 CFLAGS_rt-addr:=$(call get_hdr_inc,__LINUX_RTNETLINK_H,rtnetlink.h)
+CFLAGS_rt-route:=$(call get_hdr_inc,__LINUX_RTNETLINK_H,rtnetlink.h)
 CFLAGS_tcp_metrics:=$(call get_hdr_inc,_LINUX_TCP_METRICS_H,tcp_metrics.h)
tools/net/ynl/generated/Makefile +1 -1
index 67ce3b8988ef..6603ad8d4ce1 100644
@@ -25,7 +25,7 @@ SPECS_DIR:=../../../../Documentation/netlink/specs
 GENS_PATHS=$(shell grep -nrI --files-without-match \
 		'protocol: netlink' \
 		$(SPECS_DIR))
-GENS=$(patsubst $(SPECS_DIR)/%.yaml,%,${GENS_PATHS}) rt-addr
+GENS=$(patsubst $(SPECS_DIR)/%.yaml,%,${GENS_PATHS}) rt-addr rt-route
 SRCS=$(patsubst %,%-user.c,${GENS})
 HDRS=$(patsubst %,%-user.h,${GENS})
 OBJS=$(patsubst %,%-user.o,${GENS})
tools/net/ynl/samples/rt-route.c new file +80 -0

80 lines not on this page · raw message

tools/net/ynl/samples/.gitignore +1 -0
index 2bc8721d6144..7f9781cf532f 100644
@@ -4,3 +4,4 @@ netdev
 ovs
 page-pool
 rt-addr
+rt-route
Signature · 1 line
2.49.0
maintainer
#2 lore raw

Re: [PATCH net-next v2 01/13] netlink: specs: rename rtnetlink specs in accordance with family name

In reply to Jakub Kicinski
Quoted 22 lines · Jakub Kicinski

The rtnetlink family names are set to rt-$name within the YAML but the files are called rt_$name. C codegen assumes that the generated file name will match the family. The use of dashes is in line with our general expectation that name properties in the spec use dashes not underscores (even tho, as Donald points out most genl families use underscores in the name).

We have 3 un-ideal options to choose from:

- accept the slight inconsistency with old families using _, or
- accept the slight annoyance with all languages having to do s/-/_/ when looking up family ID, or
- accept the inconsistency with all name properties in new YAML spec being separated with - and just the family name always using _.

Pick option 1 and rename the rtnl spec files.

---
v2: extend commit msg

Quoted 11 lines
---
 Documentation/netlink/specs/{rt_addr.yaml => rt-addr.yaml}   | 0
 Documentation/netlink/specs/{rt_link.yaml => rt-link.yaml}   | 0
 Documentation/netlink/specs/{rt_neigh.yaml => rt-neigh.yaml} | 0
 Documentation/netlink/specs/{rt_route.yaml => rt-route.yaml} | 0
 Documentation/netlink/specs/{rt_rule.yaml => rt-rule.yaml}   | 0
 Documentation/userspace-api/netlink/netlink-raw.rst          | 2 +-
 tools/testing/selftests/net/lib/py/ynl.py                    | 4 ++--
 7 files changed, 3 insertions(+), 3 deletions(-)
 rename Documentation/netlink/specs/{rt_addr.yaml => rt-addr.yaml} (100%)
 rename Documentation/netlink/specs/{rt_link.yaml => rt-link.yaml} (100%)

My understanding is that this rename triggers rebuild of the related doc, which in turns leads to quite a large number of htmldoc warning, but it's really unharmful/pre-existing issue.

/P

maintainer
#3 lore raw

Re: [PATCH net-next v2 01/13] netlink: specs: rename rtnetlink specs in accordance with family name

Yes, Documentation/Makefile goes the extra mile to only try deleting a list of .rst files generated from the list of source .yaml files. It would be easier to just delete Documentation/networking/netlink_spec/*.rst which would be able to clean up old generated files in situations like this.

Quoted 44 lines · Paolo Abeni
Quoted 22 lines · Jakub Kicinski

The rtnetlink family names are set to rt-$name within the YAML but the files are called rt_$name. C codegen assumes that the generated file name will match the family. The use of dashes is in line with our general expectation that name properties in the spec use dashes not underscores (even tho, as Donald points out most genl families use underscores in the name).

We have 3 un-ideal options to choose from:

- accept the slight inconsistency with old families using _, or
- accept the slight annoyance with all languages having to do s/-/_/ when looking up family ID, or
- accept the inconsistency with all name properties in new YAML spec being separated with - and just the family name always using _.

Pick option 1 and rename the rtnl spec files.

---
v2: extend commit msg

Quoted 11 lines
---
 Documentation/netlink/specs/{rt_addr.yaml => rt-addr.yaml}   | 0
 Documentation/netlink/specs/{rt_link.yaml => rt-link.yaml}   | 0
 Documentation/netlink/specs/{rt_neigh.yaml => rt-neigh.yaml} | 0
 Documentation/netlink/specs/{rt_route.yaml => rt-route.yaml} | 0
 Documentation/netlink/specs/{rt_rule.yaml => rt-rule.yaml}   | 0
 Documentation/userspace-api/netlink/netlink-raw.rst          | 2 +-
 tools/testing/selftests/net/lib/py/ynl.py                    | 4 ++--
 7 files changed, 3 insertions(+), 3 deletions(-)
 rename Documentation/netlink/specs/{rt_addr.yaml => rt-addr.yaml} (100%)
 rename Documentation/netlink/specs/{rt_link.yaml => rt-link.yaml} (100%)

My understanding is that this rename triggers rebuild of the related doc, which in turns leads to quite a large number of htmldoc warning, but it's really unharmful/pre-existing issue.

/P

author maintainer
#4 lore raw

Re: [PATCH net-next v2 01/13] netlink: specs: rename rtnetlink specs in accordance with family name

Quoted 5 lines · On Thu, 10 Apr 2025 13:39:17 +0100 Donald Hunter wrote:

Yes, Documentation/Makefile goes the extra mile to only try deleting a list of .rst files generated from the list of source .yaml files. It would be easier to just delete Documentation/networking/netlink_spec/*.rst which would be able to clean up old generated files in situations like this.

Hm, that would work. I think it's only the second time we hit this problem, tho, so I'm just going to apply and clean up manually. If it happens again I can change the build script..

#19 lore raw
In reply to Jakub Kicinski
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 11 lines · On Wed, 9 Apr 2025 18:46:45 -0700 you wrote:

Basic support for netlink-raw AKA classic netlink in user space C codegen. This series is enough to read routes and addresses from the kernel (see the samples in patches 12 and 13).

Specs need to be slightly adjusted and decorated with the c naming info.

In terms of codegen this series includes just the basic plumbing required to skip genlmsghdr and handle request types which may technically also be legal in genetlink-legacy but are very uncommon there.

[...]

Here is the summary with links:
- [net-next,v2,01/13] netlink: specs: rename rtnetlink specs in accordance with family name

    https://git.kernel.org/netdev/net-next/c/cd5e64fb959a
  - [net-next,v2,02/13] netlink: specs: rt-route: specify fixed-header at operations level
    https://git.kernel.org/netdev/net-next/c/97a33caa9071
  - [net-next,v2,03/13] netlink: specs: rt-addr: remove the fixed members from attrs
    https://git.kernel.org/netdev/net-next/c/d460016e7bca
  - [net-next,v2,04/13] netlink: specs: rt-route: remove the fixed members from attrs
    https://git.kernel.org/netdev/net-next/c/295ff1e95201
  - [net-next,v2,05/13] netlink: specs: rt-addr: add C naming info
    https://git.kernel.org/netdev/net-next/c/52d062362c05
  - [net-next,v2,06/13] netlink: specs: rt-route: add C naming info
    https://git.kernel.org/netdev/net-next/c/1652e1f35dfb
  - [net-next,v2,07/13] tools: ynl: support creating non-genl sockets
    (no matching commit)
  - [net-next,v2,08/13] tools: ynl-gen: don't consider requests with fixed hdr empty
    https://git.kernel.org/netdev/net-next/c/e0a7903c323f
  - [net-next,v2,09/13] tools: ynl: don't use genlmsghdr in classic netlink
    https://git.kernel.org/netdev/net-next/c/7e8ba0c7de2b
  - [net-next,v2,10/13] tools: ynl-gen: consider dump ops without a do "type-consistent"
    https://git.kernel.org/netdev/net-next/c/e8025e72aad6
  - [net-next,v2,11/13] tools: ynl-gen: use family c-name in notifications
    https://git.kernel.org/netdev/net-next/c/882e7b1365ce

- [net-next,v2,12/13] tools: ynl: generate code for rt-addr and add a sample (no matching commit)
- [net-next,v2,13/13] tools: ynl: generate code for rt-route and add a sample (no matching commit)

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
#16 lore raw

Re: [PATCH net-next v2 12/13] tools: ynl: generate code for rt-addr and add a sample

In reply to Jakub Kicinski
Quoted 10 lines · On Wed, 9 Apr 2025 18:46:57 -0700 Jakub Kicinski <[email protected]> wrote:

YNL C can now generate code for simple classic netlink families. Include rt-addr in the Makefile for generation and add a sample.

  $ ./tools/net/ynl/samples/rt-addr
              lo: 127.0.0.1
       wlp0s20f3: 192.168.1.101
              lo: ::
       wlp0s20f3: fe80::6385:be6:746e:8116
            vpn0: fe80::3597:d353:b5a7:66dd
Quoted 3 lines

Hello,

This seems to broke the check-spec:

$ make -C tools/net/ynl    
...
rt-addr-user.c:62:10: error: ‘IFA_PROTO’ undeclared here (not in a function); did you mean ‘IFA_RTA’?
   62 |         [IFA_PROTO] = { .name = "proto", .type = YNL_PT_U8, },
      |          ^~~~~~~~~
      |          IFA_RTA
rt-addr-user.c:62:10: error: array index in initializer not of integer type
rt-addr-user.c:62:10: note: (near initialization for ‘rt_addr_addr_attrs_policy’)
rt-addr-user.c:62:23: warning: excess elements in array initializer
   62 |         [IFA_PROTO] = { .name = "proto", .type = YNL_PT_U8, },
      |                       ^
rt-addr-user.c:62:23: note: (near initialization for ‘rt_addr_addr_attrs_policy’)

I found it through git bisect.

Regards,

Signature · 3 lines
Köry Maincent, Bootlin
Embedded Linux and kernel engineering
https://bootlin.com
author maintainer
#17 lore raw

Re: [PATCH net-next v2 12/13] tools: ynl: generate code for rt-addr and add a sample

Quoted 21 lines · On Fri, 18 Apr 2025 19:04:31 +0200 Kory Maincent wrote:
Quoted 3 lines

Hello,

This seems to broke the check-spec:

$ make -C tools/net/ynl    
...
rt-addr-user.c:62:10: error: ‘IFA_PROTO’ undeclared here (not in a function); did you mean ‘IFA_RTA’?
   62 |         [IFA_PROTO] = { .name = "proto", .type = YNL_PT_U8, },
      |          ^~~~~~~~~
      |          IFA_RTA
rt-addr-user.c:62:10: error: array index in initializer not of integer type
rt-addr-user.c:62:10: note: (near initialization for ‘rt_addr_addr_attrs_policy’)
rt-addr-user.c:62:23: warning: excess elements in array initializer
   62 |         [IFA_PROTO] = { .name = "proto", .type = YNL_PT_U8, },
      |                       ^
rt-addr-user.c:62:23: note: (near initialization for ‘rt_addr_addr_attrs_policy’)
Quoted 1 line

I found it through git bisect.

Sorry about that. I will send a fix shortly. We needed this first:
https://lore.kernel.org/all/[email protected]/
without it YNL couldn't include if_addr.h directly.