Skip to contentSkip to conversation

tools: ynl: rework policy access to support recursion

PATCH Applied Jakub Kicinski 3 participants 5 messages Mar 13 – Mar 18

Subsystems (4)
lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

PATCH 1/1 tools: ynl: rework policy access to support recursion

Donald points out that the current naive implementation using dicts breaks if policy is recursive (child nest uses policy idx already used by its parent).

Lean more into the NlPolicy class. This lets us "render" the policy on demand, when user accesses it. If someone wants to do an infinite walk that's on them :) Show policy info as attributes of the class and use dict format to descend into sub-policies for extra neatness.

---
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]

---
 tools/net/ynl/pyynl/cli.py               |   4 +-
 tools/net/ynl/pyynl/lib/ynl.py           | 152 ++++++++++++++++-------
 tools/testing/selftests/net/nl_nlctrl.py |  26 ++--
 3 files changed, 120 insertions(+), 62 deletions(-)
tools/net/ynl/pyynl/cli.py +2 -2
index fc9e84754e4b..8275a806cf73 100755
@@ -313,11 +313,11 @@ RELATIVE_SCHEMA_DIR='../../../../Documentation/netlink'
     if args.policy:
         if args.do:
             pol = ynl.get_policy(args.do, 'do')
-            output(pol.attrs if pol else None)
+            output(pol.to_dict() if pol else None)
             args.do = None
         if args.dump:
             pol = ynl.get_policy(args.dump, 'dump')
-            output(pol.attrs if pol else None)
+            output(pol.to_dict() if pol else None)
             args.dump = None
 
     if args.ntf:
tools/net/ynl/pyynl/lib/ynl.py +107 -45

180 lines not on this page · raw message

tools/testing/selftests/net/nl_nlctrl.py +11 -15
index d19e206c2c02..fe1f66dc9435 100755
@@ -68,39 +68,35 @@ from lib.py import NetdevFamily, EthtoolFamily, NlctrlFamily
     # dev-get: do has a real policy with ifindex, dump has no policy
     # (only the reject-all policy with maxattr=0)
     pol = ndev.get_policy('dev-get', 'do')
-    ksft_in('ifindex', pol.attrs,
-            comment="dev-get do policy should have ifindex")
-    ksft_eq(pol.attrs.get('ifindex', {}).get('type'), 'u32')
+    ksft_in('ifindex', pol, comment="dev-get do policy should have ifindex")
+    ksft_eq(pol['ifindex'].type, 'u32')
 
     pol_dump = ndev.get_policy('dev-get', 'dump')
-    ksft_eq(len(pol_dump.attrs), 0,
-            comment="dev-get should not accept any attrs")
+    ksft_eq(len(pol_dump), 0, comment="dev-get should not accept any attrs")
 
     # napi-get: both do and dump have real policies
     pol_do = ndev.get_policy('napi-get', 'do')
-    ksft_ge(len(pol_do.attrs), 1)
+    ksft_ge(len(pol_do), 1)
 
     pol_dump = ndev.get_policy('napi-get', 'dump')
-    ksft_ge(len(pol_dump.attrs), 1)
+    ksft_ge(len(pol_dump), 1)
 
     # -- ethtool (full ops) --
     et = EthtoolFamily()
 
     # strset-get (has both do and dump, full ops share policy)
     pol_do = et.get_policy('strset-get', 'do')
-    ksft_ge(len(pol_do.attrs), 1, comment="strset-get should have a do policy")
+    ksft_ge(len(pol_do), 1, comment="strset-get should have a do policy")
 
     pol_dump = et.get_policy('strset-get', 'dump')
-    ksft_ge(len(pol_dump.attrs), 1,
-            comment="strset-get should have a dump policy")
+    ksft_ge(len(pol_dump), 1, comment="strset-get should have a dump policy")
 
     # Same policy means same attribute names
-    ksft_eq(set(pol_do.attrs.keys()), set(pol_dump.attrs.keys()))
+    ksft_eq(set(pol_do.keys()), set(pol_dump.keys()))
 
     # linkinfo-set is do-only (SET command), no dump
     pol_do = et.get_policy('linkinfo-set', 'do')
-    ksft_ge(len(pol_do.attrs), 1,
-            comment="linkinfo-set should have a do policy")
+    ksft_ge(len(pol_do), 1, comment="linkinfo-set should have a do policy")
 
     pol_dump = et.get_policy('linkinfo-set', 'dump')
     ksft_eq(pol_dump, None,
@@ -113,9 +109,9 @@ from lib.py import NetdevFamily, EthtoolFamily, NlctrlFamily
 
     # dev-get do policy should have named attributes from the spec
     pol = ndev.get_policy('dev-get', 'do')
-    ksft_ge(len(pol.attrs), 1)
+    ksft_ge(len(pol), 1)
     # All attr names should be resolved (no 'attr-N' fallbacks)
-    for name in pol.attrs:
+    for name in pol:
         ksft_true(not name.startswith('attr-'),
                   comment=f"unresolved attr name: {name}")
 
Signature · 1 line
2.53.0
author maintainer
#1 lore raw
Quoted 8 lines · On Fri, 13 Mar 2026 16:20:47 -0700 Jakub Kicinski wrote:

Donald points out that the current naive implementation using dicts breaks if policy is recursive (child nest uses policy idx already used by its parent).

Lean more into the NlPolicy class. This lets us "render" the policy on demand, when user accesses it. If someone wants to do an infinite walk that's on them :) Show policy info as attributes of the class and use dict format to descend into sub-policies for extra neatness.

Hi Donald, you promised to take a look when officially posted?

maintainer
#2 lore raw
Quoted 6 lines · Jakub Kicinski
Quoted 4 lines · On Fri, 13 Mar 2026 16:20:47 -0700 Jakub Kicinski wrote:

Donald points out that the current naive implementation using dicts breaks if policy is recursive (child nest uses policy idx already used by its parent).

Quoted 6 lines
Quoted 4 lines

Lean more into the NlPolicy class. This lets us "render" the policy on demand, when user accesses it. If someone wants to do an infinite walk that's on them :) Show policy info as attributes of the class and use dict format to descend into sub-policies for extra neatness.

Hi Donald, you promised to take a look when officially posted?

Apologies, my response was sat in drafts :-/

maintainer Reviewed-by on v1 1/1
#3 lore raw
In reply to Jakub Kicinski
Quoted 10 lines · Jakub Kicinski

Donald points out that the current naive implementation using dicts breaks if policy is recursive (child nest uses policy idx already used by its parent).

Lean more into the NlPolicy class. This lets us "render" the policy on demand, when user accesses it. If someone wants to do an infinite walk that's on them :) Show policy info as attributes of the class and use dict format to descend into sub-policies for extra neatness.

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

Hello:

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

Quoted 10 lines · On Fri, 13 Mar 2026 16:20:47 -0700 you wrote:

Donald points out that the current naive implementation using dicts breaks if policy is recursive (child nest uses policy idx already used by its parent).

Lean more into the NlPolicy class. This lets us "render" the policy on demand, when user accesses it. If someone wants to do an infinite walk that's on them :) Show policy info as attributes of the class and use dict format to descend into sub-policies for extra neatness.

[...]

Here is the summary with links:
- [net-next] tools: ynl: rework policy access to support recursion

    https://git.kernel.org/netdev/net-next/c/17a55ddb1956

You are awesome, thank you!

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