Skip to contentSkip to conversation

tools: ynl: c: basic netlink-raw support

PATCH Superseded Jakub Kicinski 3 participants 45 messages Apr 9, 2025

Subsystems (6)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

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

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 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. We could replace dashes with underscores in the codegen but making sure the family name matches the spec name may be more generally useful.

---
 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
Reviewed-by on v1 1/13
#2 lore raw

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

Quoted 8 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. We could replace dashes with underscores in the codegen but making sure the family name matches the spec name may be more generally useful.

---

+1 to being more useful to have the family name match the spec name, I agree.

maintainer Reviewed-by
#3 lore raw

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

Quoted 7 lines · Jacob Keller
Quoted 6 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. We could replace dashes with underscores in the codegen but making sure the family name matches the spec name may be more generally useful.

Quoted 6 lines
Quoted 2 lines

---

+1 to being more useful to have the family name match the spec name, I agree.

Agree that it's preferable that they match, and that ynl --family ... searches by filename so they really ought to match.

I'll just note that the genl convention is underscores in family names, if you wanted consistency across all families.

author maintainer
#4 lore raw

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

I thought about it some more.

Quoted 2 lines · On Wed, 09 Apr 2025 13:15:42 +0100 Donald Hunter wrote:

I'll just note that the genl convention is underscores in family names, if you wanted consistency across all families.

I wasn't clear on whether it's a convention or just how we did things historically. We recommend the use of dashes in the spec itself so it stands to reason to also use dashes in the family name.
But replacing dashes with underscores is a "C thing", and family name string is used by all languages, so we shouldn't s/-/_/ when we output the family name for the kernel's code struct genl_family.

IOW I think that either we
- 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 _.

:( I picked the first option, assuming the genl family names don't have much of a convention. Admittedly I don't know of any with dashes but some of them use capital letters :S

LMK if you think we should pick differently. In my mind picking option 1 is prioritizing consistency of the spec language over the consistency of user experience. We can alleviate the annoyance of typing --family .. with bash completions?

maintainer
#5 lore raw

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

Quoted 8 lines · Jakub Kicinski

IOW I think that either we
- 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 _.

Quoted 8 lines

:( I picked the first option, assuming the genl family names don't have much of a convention. Admittedly I don't know of any with dashes but some of them use capital letters :S

LMK if you think we should pick differently. In my mind picking option 1 is prioritizing consistency of the spec language over the consistency of user experience. We can alleviate the annoyance of typing --family .. with bash completions?

That seems fair to me. FWIW, dashes seem more CLI friendly too. And you make a good point that existing genl names really are all over the place.

Donald.

author maintainer
#6 lore raw

PATCH 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
Reviewed-by on v1 2/13
#7 lore raw

Re: [PATCH net-next 02/13] netlink: specs: rt-route: specify fixed-header at operations level

Quoted 18 lines · 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 +0 -0
index 292469c7d4b9..6fa3fa24305e 100644
@@ -245,12 +245,12 @@ protonum: 0
 
 operations:
   enum-model: directional

Documentation/netlink/specs/rt-route.yaml protonum: 0 · line 256

Quoted 10 lines
Documentation/netlink/specs/rt-route.yaml +1 -1
+  fixed-header: rtmsg
   list:
     -
       name: getroute
       doc: Dump route information.
       attribute-set: route-attrs
-      fixed-header: rtmsg
       do:
         request:
           value: 26

Nice. This seems simpler.

maintainer Reviewed-by on v1 2/13
#8 lore raw

Re: [PATCH net-next 02/13] netlink: specs: rt-route: specify fixed-header at operations level

In reply to Jakub Kicinski
Quoted 15 lines · 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 +0 -0
index 292469c7d4b9..6fa3fa24305e 100644

Documentation/netlink/specs/rt-route.yaml protonum: 0 · line 248

Quoted 4 lines
Documentation/netlink/specs/rt-route.yaml +1 -0
@@ -245,12 +245,12 @@ protonum: 0
 
 operations:
   enum-model: directional
+  fixed-header: rtmsg

It's a cleaner spec this way too :)

author maintainer
#9 lore raw

PATCH 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 structs are always present in their entirety so there's no point in listing their members. Current C codegen doesn't expect them and tries to look up the names in the attribute space.

---
 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
#10 lore raw

Re: [PATCH net-next 03/13] netlink: specs: rt-addr: remove the fixed members from attrs

Quoted 17 lines · 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 structs are always present in their entirety so there's no point in listing their members. Current C codegen doesn't expect them and tries to look up the names in the attribute space.

---
 Documentation/netlink/specs/rt-addr.yaml | 20 +++-----------------
 1 file changed, 3 insertions(+), 17 deletions(-)
Documentation/netlink/specs/rt-addr.yaml +0 -0
index df6b23f06a22..0488ce87506c 100644

Documentation/netlink/specs/rt-addr.yaml protonum: 0 · line 140

Quoted 8 lines
Documentation/netlink/specs/rt-addr.yaml +0 -5
@@ -133,11 +133,6 @@ protonum: 0
         request:
           value: 20
           attributes: &ifaddr-all
-            - ifa-family
-            - ifa-flags
-            - ifa-prefixlen
-            - ifa-scope
-            - ifa-index

From the wording of the commit message, I interpreted it that if the attribute list contains a struct, then its members are implicit and don't have to be listed..?

I guess I'm missing something here since It doesn't seem like that is the case.

Does the commit mean that structs are always available regardless of whats listed in the attributes?

Reviewed-by
#11 lore raw

Re: [PATCH net-next 03/13] netlink: specs: rt-addr: remove the fixed members from attrs

Quoted 33 lines · Jacob Keller
Quoted 17 lines · 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 structs are always present in their entirety so there's no point in listing their members. Current C codegen doesn't expect them and tries to look up the names in the attribute space.

---
 Documentation/netlink/specs/rt-addr.yaml | 20 +++-----------------
 1 file changed, 3 insertions(+), 17 deletions(-)
Documentation/netlink/specs/rt-addr.yaml +0 -0
index df6b23f06a22..0488ce87506c 100644

Documentation/netlink/specs/rt-addr.yaml protonum: 0 · line 140

Quoted 8 lines
Documentation/netlink/specs/rt-addr.yaml +0 -5
@@ -133,11 +133,6 @@ protonum: 0
         request:
           value: 20
           attributes: &ifaddr-all
-            - ifa-family
-            - ifa-flags
-            - ifa-prefixlen
-            - ifa-scope
-            - ifa-index

From the wording of the commit message, I interpreted it that if the attribute list contains a struct, then its members are implicit and don't have to be listed..?

Quoted 6 lines

I guess I'm missing something here since It doesn't seem like that is the case.

Does the commit mean that structs are always available regardless of whats listed in the attributes?

Ah. These messages have a fixed header which has its members defined, and thats why we don't need to repeat them in the attribute list. Only the struct that defines the header is used here. Ok.

maintainer Reviewed-by on v1 3/13
#12 lore raw

Re: [PATCH net-next 03/13] netlink: specs: rt-addr: remove the fixed members from attrs

In reply to Jakub Kicinski
Quoted 18 lines · 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 structs are always present in their entirety so there's no point in listing their members. Current C codegen doesn't expect them and tries to look up the names in the attribute space.

---
 Documentation/netlink/specs/rt-addr.yaml | 20 +++-----------------
 1 file changed, 3 insertions(+), 17 deletions(-)
Documentation/netlink/specs/rt-addr.yaml +0 -0
index df6b23f06a22..0488ce87506c 100644
@@ -133,11 +133,6 @@ protonum: 0
         request:

Documentation/netlink/specs/rt-addr.yaml protonum: 0 · line 138

Quoted 10 lines
Documentation/netlink/specs/rt-addr.yaml +0 -5
           value: 20
           attributes: &ifaddr-all
-            - ifa-family
-            - ifa-flags
-            - ifa-prefixlen
-            - ifa-scope
-            - ifa-index
             - address
             - label
             - local

Yeah, that's a consequence of me not really grokking the intended purpose of listing message attrs when I wrote the spec.

author maintainer
#13 lore raw

PATCH 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 structs are always present in their entirety so there's no point in listing their members. Current C codegen doesn't expect them and tries to look up the names 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
Reviewed-by on v1 4/13
#14 lore raw

Re: [PATCH net-next 04/13] netlink: specs: rt-route: remove the fixed members from attrs

Quoted 8 lines · 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 structs are always present in their entirety so there's no point in listing their members. Current C codegen doesn't expect them and tries to look up the names in the attribute space.

maintainer Reviewed-by on v1 4/13
#15 lore raw

Re: [PATCH net-next 04/13] netlink: specs: rt-route: remove the fixed members from attrs

In reply to Jakub Kicinski
Quoted 8 lines · 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 structs are always present in their entirety so there's no point in listing their members. Current C codegen doesn't expect them and tries to look up the names in the attribute space.

author maintainer
#16 lore raw

PATCH 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
#19 lore raw

PATCH 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
#22 lore raw

PATCH 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
Reviewed-by on v1 7/13
#23 lore raw

Re: [PATCH net-next 07/13] tools: ynl: support creating non-genl sockets

Quoted 8 lines · 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.

---

maintainer Reviewed-by on v1 7/13
#24 lore raw

Re: [PATCH net-next 07/13] tools: ynl: support creating non-genl sockets

In reply to Jakub Kicinski
Quoted 7 lines · 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.

author maintainer
#25 lore raw

PATCH 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
Reviewed-by on v1 8/13
#26 lore raw

Re: [PATCH net-next 08/13] tools: ynl-gen: don't consider requests with fixed hdr empty

Quoted 17 lines · 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 +0 -0
index 9e00aac4801c..04f1ac62cb01 100755

tools/net/ynl/pyynl/ynl_gen_c.py from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S · line 1253

Quoted 7 lines
tools/net/ynl/pyynl/ynl_gen_c.py +3 -0
@@ -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
+
 

Ahh.. I think I see where my mistake from earlier regarding the attributes. classic netlink has a fixed struct + sometimes attributes. We don't need to list struct members as part of attributes because the header will always be there. Ok.

author maintainer
#27 lore raw

PATCH 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
Reviewed-by on v1 9/13
#28 lore raw

Re: [PATCH net-next 09/13] tools: ynl: don't use genlmsghdr in classic netlink

Quoted 8 lines · 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.

---

maintainer Reviewed-by on v1 9/13
#29 lore raw

Re: [PATCH net-next 09/13] tools: ynl: don't use genlmsghdr in classic netlink

In reply to Jakub Kicinski
Quoted 7 lines · 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.

author maintainer
#30 lore raw

PATCH 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.

---
 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..c97cda43a604 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_onside = 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_onside = 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_onside:
+                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_onside:
         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_onside:
                         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_onside:
                         parse_rsp_msg(ri, deref=True)
                     print_req_free(ri)
                     print_dump_type_free(ri)
Signature · 1 line
2.49.0
Reviewed-by on v1 10/13
#31 lore raw

Re: [PATCH net-next 10/13] tools: ynl-gen: consider dump ops without a do "type-consistent"

Quoted 8 lines · 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.

---

Makes sense.

maintainer
#32 lore raw

Re: [PATCH net-next 10/13] tools: ynl-gen: consider dump ops without a do "type-consistent"

In reply to Jakub Kicinski
Quoted 16 lines · 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.

---
 tools/net/ynl/pyynl/ynl_gen_c.py | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
tools/net/ynl/pyynl/ynl_gen_c.py +0 -0
index b0b47a493a86..c97cda43a604 100755

tools/net/ynl/pyynl/ynl_gen_c.py from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S · line 1215

Quoted 4 lines
tools/net/ynl/pyynl/ynl_gen_c.py +1 -0
@@ -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_onside = False

I'm not understanding what type_onside is meant to mean.

author maintainer
#33 lore raw

Re: [PATCH net-next 10/13] tools: ynl-gen: consider dump ops without a do "type-consistent"

Quoted 5 lines · On Wed, 09 Apr 2025 13:38:01 +0100 Donald Hunter wrote:
Quoted 3 lines

# 'do' and 'dump' response parsing is identical self.type_consistent = True
+ self.type_onside = False

I'm not understanding what type_onside is meant to mean.

Damn, I have fallen into the IDE auto-completion trap. It was supposed to say "oneside".
Not that the solution is super clean either way :(

Signature · 1 line
pw-bot: cr
#34 lore raw

Re: [PATCH net-next 10/13] tools: ynl-gen: consider dump ops without a do "type-consistent"

Quoted 10 lines · Jakub Kicinski
Quoted 5 lines · On Wed, 09 Apr 2025 13:38:01 +0100 Donald Hunter wrote:
Quoted 3 lines

# 'do' and 'dump' response parsing is identical self.type_consistent = True
+ self.type_onside = False

I'm not understanding what type_onside is meant to mean.

Damn, I have fallen into the IDE auto-completion trap. It was supposed to say "oneside".
Not that the solution is super clean either way :(

Lol.. I saw this and read it as "type_oneside" myself.. didn't even notice the typo.

author maintainer
#35 lore raw

PATCH 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 c97cda43a604..5fa1db3d5cf3 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
#38 lore raw

PATCH 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
---
 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
Reviewed-by on v1 12/13
#39 lore raw

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

Quoted 33 lines · Jakub Kicinski
tools/net/ynl/samples/rt-addr.c new file +27 -0
new file mode 100644
index 000000000000..c9a6436ad420
@@ -0,0 +1,80 @@
+int main(int argc, char **argv)
+{
+	struct rt_addr_getaddr_list *rsp;
+	struct rt_addr_getaddr_req *req;
+	struct ynl_error yerr;
+	struct ynl_sock *ys;
+
+	ys = ynl_sock_create(&ynl_rt_addr_family, &yerr);
+	if (!ys) {
+		fprintf(stderr, "YNL: %s\n", yerr.msg);
+		return 1;
+	}
+
+	req = rt_addr_getaddr_req_alloc();
+	if (!req)
+		goto err_destroy;
+
+	rsp = rt_addr_getaddr_dump(ys, req);
+	rt_addr_getaddr_req_free(req);
+	if (!rsp)
+		goto err_close;
+
+	if (ynl_dump_empty(rsp))
+		fprintf(stderr, "Error: no addresses reported\n");
+	ynl_dump_foreach(rsp, addr)
+		rt_addr_print(addr, 0);
+	rt_addr_getaddr_list_free(rsp);

tools/net/ynl/samples/rt-addr.c line 37

Quoted 10 lines
tools/net/ynl/samples/rt-addr.c new file +10 -0
+
+	ynl_sock_destroy(ys);
+	return 0;
+
+err_close:
+	fprintf(stderr, "YNL: %s\n", ys->err.msg);
+err_destroy:
+	ynl_sock_destroy(ys);
+	return 2;
+}

Nice! This makes it a lot simpler to write small tools for dedicated tasks or debugging vs trying to use one of the other existing libraries. I think it helps make netlink more accessible, and appreciate the work to support the classic netlink families, even with their quirks.

Thanks,
Jake

author maintainer
#40 lore raw

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

Quoted 4 lines · On Tue, 8 Apr 2025 22:04:46 -0700 Jacob Keller wrote:

Nice! This makes it a lot simpler to write small tools for dedicated tasks or debugging vs trying to use one of the other existing libraries. I think it helps make netlink more accessible, and appreciate the work to support the classic netlink families, even with their quirks.

I hope so! I must say, I'm a bit surprised that we don't see people writing code gens for other languages. It is quite fun.

maintainer Reviewed-by on v1 12/13
#41 lore raw

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

In reply to Jakub Kicinski
Quoted 11 lines · 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
author maintainer
#42 lore raw

PATCH 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
Reviewed-by on v1 13/13
#43 lore raw

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

Quoted 10 lines · 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
Quoted 2 lines

---

maintainer Reviewed-by on v1 13/13
#44 lore raw

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

In reply to Jakub Kicinski
Quoted 11 lines · 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