Skip to contentSkip to conversation

tools: ynl-gen: support full range of min/max checks

PATCH Applied Jakub Kicinski 2 participants 5 messages Oct 18, 2023 – Oct 19, 2023

Subsystems (3)
lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

cover tools: ynl-gen: support full range of min/max checks

YNL code gen currently supports only very simple range checks within the range of s16. Add support for full range of u64 / s64 which is good to have, and will be even more important with uint / sint.

Jakub Kicinski (3):
tools: ynl-gen: track attribute use
tools: ynl-gen: support full range of min/max checks for integer
values
tools: ynl-gen: support limit names

 Documentation/netlink/genetlink-c.yaml      |  10 +-
 Documentation/netlink/genetlink-legacy.yaml |  10 +-
 Documentation/netlink/genetlink.yaml        |  10 +-
 tools/net/ynl/ynl-gen-c.py                  | 115 ++++++++++++++++++--
 4 files changed, 132 insertions(+), 13 deletions(-)
Signature · 1 line
2.41.0
author maintainer
#1 lore raw

PATCH 1/3 tools: ynl-gen: track attribute use

For range validation we'll need to know if any individual attribute is used on input (i.e. whether we will generate a policy for it). Track this information.

---
 tools/net/ynl/ynl-gen-c.py | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
tools/net/ynl/ynl-gen-c.py +20 -0
index f125b5f704ba..7f4ad4014d17 100755
@@ -42,6 +42,9 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S
         self.type = attr['type']
         self.checks = attr.get('checks', {})
 
+        self.request = False
+        self.reply = False
+
         if 'len' in attr:
             self.len = attr['len']
         if 'nested-attributes' in attr:
@@ -846,6 +849,7 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S
 
         self._load_root_sets()
         self._load_nested_sets()
+        self._load_attr_use()
         self._load_hooks()
 
         self.kernel_policy = self.yaml.get('kernel-policy', 'split')
@@ -966,6 +970,22 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S
                         child.request |= struct.request
                         child.reply |= struct.reply
 
+    def _load_attr_use(self):
+        for _, struct in self.pure_nested_structs.items():
+            if struct.request:
+                for _, arg in struct.member_list():
+                    arg.request = True
+            if struct.reply:
+                for _, arg in struct.member_list():
+                    arg.reply = True
+
+        for root_set, rs_members in self.root_sets.items():
+            for attr, spec in self.attr_sets[root_set].items():
+                if attr in rs_members['request']:
+                    spec.request = True
+                if attr in rs_members['reply']:
+                    spec.reply = True
+
     def _load_global_policy(self):
         global_set = set()
         attr_set_name = None
Signature · 1 line
2.41.0
author maintainer
#2 lore raw

PATCH 2/3 tools: ynl-gen: support full range of min/max checks for integer values

In reply to Jakub Kicinski

Extend the support to full range of min/max checks.
None of the existing YNL families required complex integer validation.

The support is less than trivial, because we try to keep struct nla_policy tiny the min/max members it holds in place are s16. Meaning we can only express checks in range of s16. For larger ranges we need to define a structure and link it in the policy.

---
 Documentation/netlink/genetlink-c.yaml      |  3 +
 Documentation/netlink/genetlink-legacy.yaml |  3 +
 Documentation/netlink/genetlink.yaml        |  3 +
 tools/net/ynl/ynl-gen-c.py                  | 66 ++++++++++++++++++---
 4 files changed, 68 insertions(+), 7 deletions(-)
Documentation/netlink/genetlink-c.yaml +3 -0
index f9366aaddd21..75af0b51f3d7 100644
@@ -184,6 +184,9 @@ additionalProperties: False
                   min:
                     description: Min value for an integer attribute.
                     type: integer
+                  max:
+                    description: Max value for an integer attribute.
+                    type: integer
                   min-len:
                     description: Min length for a binary attribute.
                     $ref: '#/$defs/len-or-define'
Documentation/netlink/genetlink-legacy.yaml +3 -0
index a6a490333a1a..c0f17a8bfe0d 100644
@@ -227,6 +227,9 @@ additionalProperties: False
                   min:
                     description: Min value for an integer attribute.
                     type: integer
+                  max:
+                    description: Max value for an integer attribute.
+                    type: integer
                   min-len:
                     description: Min length for a binary attribute.
                     $ref: '#/$defs/len-or-define'
Documentation/netlink/genetlink.yaml +3 -0
index 2b788e607a14..4fd56e3b1553 100644
@@ -157,6 +157,9 @@ additionalProperties: False
                   min:
                     description: Min value for an integer attribute.
                     type: integer
+                  max:
+                    description: Max value for an integer attribute.
+                    type: integer
                   min-len:
                     description: Min length for a binary attribute.
                     $ref: '#/$defs/len-or-define'
tools/net/ynl/ynl-gen-c.py +59 -7

104 lines not on this page · raw message

Signature · 1 line
2.41.0
author maintainer
#3 lore raw

PATCH 3/3 tools: ynl-gen: support limit names

In reply to Jakub Kicinski

Support the use of symbolic names like s8-min or u32-max in checks to make writing specs less painful.

---
 Documentation/netlink/genetlink-c.yaml      |  9 ++++-
 Documentation/netlink/genetlink-legacy.yaml |  9 ++++-
 Documentation/netlink/genetlink.yaml        |  9 ++++-
 tools/net/ynl/ynl-gen-c.py                  | 45 ++++++++++++++++-----
 4 files changed, 55 insertions(+), 17 deletions(-)
Documentation/netlink/genetlink-c.yaml +7 -2
index 75af0b51f3d7..dee11c514896 100644
@@ -13,6 +13,11 @@ $schema: https://json-schema.org/draft-07/schema
     type: [ string, integer ]
     pattern: ^[0-9A-Za-z_]+( - 1)?$
     minimum: 0
+  len-or-limit:
+    # literal int or limit based on fixed-width type e.g. u8-min, u16-max, etc.
+    type: [ string, integer ]
+    pattern: ^[su](8|16|32|64)-(min|max)$
+    minimum: 0
 
 # Schema for specs
 title: Protocol
@@ -183,10 +188,10 @@ additionalProperties: False
                     type: string
                   min:
                     description: Min value for an integer attribute.
-                    type: integer
+                    $ref: '#/$defs/len-or-limit'
                   max:
                     description: Max value for an integer attribute.
-                    type: integer
+                    $ref: '#/$defs/len-or-limit'
                   min-len:
                     description: Min length for a binary attribute.
                     $ref: '#/$defs/len-or-define'
Documentation/netlink/genetlink-legacy.yaml +7 -2
index c0f17a8bfe0d..9194f3e223ef 100644
@@ -13,6 +13,11 @@ $schema: https://json-schema.org/draft-07/schema
     type: [ string, integer ]
     pattern: ^[0-9A-Za-z_]+( - 1)?$
     minimum: 0
+  len-or-limit:
+    # literal int or limit based on fixed-width type e.g. u8-min, u16-max, etc.
+    type: [ string, integer ]
+    pattern: ^[su](8|16|32|64)-(min|max)$
+    minimum: 0
 
 # Schema for specs
 title: Protocol
@@ -226,10 +231,10 @@ additionalProperties: False
                     type: string
                   min:
                     description: Min value for an integer attribute.
-                    type: integer
+                    $ref: '#/$defs/len-or-limit'
                   max:
                     description: Max value for an integer attribute.
-                    type: integer
+                    $ref: '#/$defs/len-or-limit'
                   min-len:
                     description: Min length for a binary attribute.
                     $ref: '#/$defs/len-or-define'
Documentation/netlink/genetlink.yaml +7 -2
index 4fd56e3b1553..0a4ae861d011 100644
@@ -13,6 +13,11 @@ $schema: https://json-schema.org/draft-07/schema
     type: [ string, integer ]
     pattern: ^[0-9A-Za-z_]+( - 1)?$
     minimum: 0
+  len-or-limit:
+    # literal int or limit based on fixed-width type e.g. u8-min, u16-max, etc.
+    type: [ string, integer ]
+    pattern: ^[su](8|16|32|64)-(min|max)$
+    minimum: 0
 
 # Schema for specs
 title: Protocol
@@ -156,10 +161,10 @@ additionalProperties: False
                     type: string
                   min:
                     description: Min value for an integer attribute.
-                    type: integer
+                    $ref: '#/$defs/len-or-limit'
                   max:
                     description: Max value for an integer attribute.
-                    type: integer
+                    $ref: '#/$defs/len-or-limit'
                   min-len:
                     description: Min length for a binary attribute.
                     $ref: '#/$defs/len-or-define'
tools/net/ynl/ynl-gen-c.py +34 -11

92 lines not on this page · raw message

Signature · 1 line
2.41.0
#4 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, 18 Oct 2023 09:39:14 -0700 you wrote:

YNL code gen currently supports only very simple range checks within the range of s16. Add support for full range of u64 / s64 which is good to have, and will be even more important with uint / sint.

Jakub Kicinski (3):
tools: ynl-gen: track attribute use
tools: ynl-gen: support full range of min/max checks for integer
values
tools: ynl-gen: support limit names

[...]

Here is the summary with links:
- [net-next,1/3] tools: ynl-gen: track attribute use
https://git.kernel.org/netdev/net-next/c/ee0a4cfcbdcc
- [net-next,2/3] tools: ynl-gen: support full range of min/max checks for integer values

    https://git.kernel.org/netdev/net-next/c/668c1ac828fb
  - [net-next,3/3] tools: ynl-gen: support limit names
    https://git.kernel.org/netdev/net-next/c/f9bc3cbc20d0

You are awesome, thank you!

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