Skip to contentSkip to conversation

tools: ynl-gen: improvements for DPLL

PATCH Applied Jakub Kicinski 3 participants 6 messages Jun 12, 2023 – Jun 13, 2023

Subsystems (3)
lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

cover tools: ynl-gen: improvements for DPLL

A set of improvements needed to generate the kernel code for DPLL.

Jakub Kicinski (2):
tools: ynl-gen: correct enum policies
tools: ynl-gen: inherit policy in multi-attr

 tools/net/ynl/ynl-gen-c.py | 57 +++++++++++++++++++++++++-------------
 1 file changed, 37 insertions(+), 20 deletions(-)
Signature · 1 line
2.40.1
author maintainer
#1 lore raw

PATCH 1/2 tools: ynl-gen: correct enum policies

Scalar range validation assumes enums start at 0.
Teach it to properly calculate the value range.

---
 tools/net/ynl/ynl-gen-c.py | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
tools/net/ynl/ynl-gen-c.py +13 -2
index 870f98d0e12c..54777d529f5e 100755
@@ -300,8 +300,10 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S
             return f"NLA_POLICY_MIN({policy}, {self.checks['min']})"
         elif 'enum' in self.attr:
             enum = self.family.consts[self.attr['enum']]
-            cnt = len(enum['entries'])
-            return f"NLA_POLICY_MAX({policy}, {cnt - 1})"
+            low, high = enum.value_range()
+            if low == 0:
+                return f"NLA_POLICY_MAX({policy}, {high})"
+            return f"NLA_POLICY_RANGE({policy}, {low}, {high})"
         return super()._attr_policy(policy)
 
     def _attr_typol(self):
@@ -676,6 +678,15 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S
     def new_entry(self, entry, prev_entry, value_start):
         return EnumEntry(self, entry, prev_entry, value_start)
 
+    def value_range(self):
+        low = min([x.value for x in self.entries.values()])
+        high = max([x.value for x in self.entries.values()])
+
+        if high - low + 1 != len(self.entries):
+            raise Exception("Can't get value range for a noncontiguous enum")
+
+        return low, high
+
 
 class AttrSet(SpecAttrSet):
     def __init__(self, family, yaml):
Signature · 1 line
2.40.1
Reviewed-by on v1 1/2
#2 lore raw

RE: [PATCH net-next 1/2] tools: ynl-gen: correct enum policies

Quoted 11 lines

-----Original Message-----
From: Jakub Kicinski <[email protected]>
Sent: Monday, June 12, 2023 8:59 AM
To: [email protected]
Cc: [email protected]; [email protected]; [email protected]; Kubalewski, Arkadiusz <[email protected]>; Jakub Kicinski <[email protected]>
Subject: [PATCH net-next 1/2] tools: ynl-gen: correct enum policies

Quoted 5 lines

Scalar range validation assumes enums start at 0.
Teach it to properly calculate the value range.

---

Quoted 26 lines
 tools/net/ynl/ynl-gen-c.py | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
tools/net/ynl/ynl-gen-c.py +0 -0
index 870f98d0e12c..54777d529f5e 100755
@@ -300,8 +300,10 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr,

SpecOperation, SpecEnumSet, S
return f"NLA_POLICY_MIN({policy}, {self.checks['min']})"
elif 'enum' in self.attr:
enum = self.family.consts[self.attr['enum']]

tools/net/ynl/ynl-gen-c.py +4 -2
-            cnt = len(enum['entries'])
-            return f"NLA_POLICY_MAX({policy}, {cnt - 1})"
+            low, high = enum.value_range()
+            if low == 0:
+                return f"NLA_POLICY_MAX({policy}, {high})"
+            return f"NLA_POLICY_RANGE({policy}, {low}, {high})"
         return super()._attr_policy(policy)
 
     def _attr_typol(self):
@@ -676,6 +678,15 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr,

SpecOperation, SpecEnumSet, S
def new_entry(self, entry, prev_entry, value_start):
return EnumEntry(self, entry, prev_entry, value_start)

tools/net/ynl/ynl-gen-c.py from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S · line 687

Quoted 7 lines
tools/net/ynl/ynl-gen-c.py +7 -0
+    def value_range(self):
+        low = min([x.value for x in self.entries.values()])
+        high = max([x.value for x in self.entries.values()])
+
+        if high - low + 1 != len(self.entries):
+            raise Exception("Can't get value range for a noncontiguous enum")
+

Guessing we don't anticipate any non-contiguous enums so an exception here is fine

Quoted 8 lines
tools/net/ynl/ynl-gen-c.py +2 -1
+        return low, high
+
 
 class AttrSet(SpecAttrSet):
     def __init__(self, family, yaml):
--

2.40.1

author maintainer
#3 lore raw

PATCH 2/2 tools: ynl-gen: inherit policy in multi-attr

In reply to Jakub Kicinski

Instead of reimplementing policies in MutliAttr for every underlying type forward the calls to the base type. This will be needed for DPLL which uses a multi-attr nest, and currently gets an invalid NLA_NEST policy generated.

---
 tools/net/ynl/ynl-gen-c.py | 42 ++++++++++++++++++++++----------------
 1 file changed, 24 insertions(+), 18 deletions(-)
tools/net/ynl/ynl-gen-c.py +24 -18
index 54777d529f5e..71c5e79e877f 100755
@@ -462,6 +462,11 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S
 
 
 class TypeMultiAttr(Type):
+    def __init__(self, family, attr_set, attr, value, base_type):
+        super().__init__(family, attr_set, attr, value)
+
+        self.base_type = base_type
+
     def is_multi_val(self):
         return True
 
@@ -497,13 +502,11 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S
         else:
             raise Exception(f"Free of MultiAttr sub-type {self.attr['type']} not supported yet")
 
+    def _attr_policy(self, policy):
+        return self.base_type._attr_policy(policy)
+
     def _attr_typol(self):
-        if 'type' not in self.attr or self.attr['type'] == 'nest':
-            return f'.type = YNL_PT_NEST, .nest = &{self.nested_render_name}_nest, '
-        elif self.attr['type'] in scalars:
-            return f".type = YNL_PT_U{self.attr['type'][1:]}, "
-        else:
-            raise Exception(f"Sub-type {self.attr['type']} not supported yet")
+        return self.base_type._attr_typol()
 
     def _attr_get(self, ri, var):
         return f'n_{self.c_name}++;', None, None
@@ -717,29 +720,32 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S
             self.c_name = ''
 
     def new_attr(self, elem, value):
-        if 'multi-attr' in elem and elem['multi-attr']:
-            return TypeMultiAttr(self.family, self, elem, value)
-        elif elem['type'] in scalars:
-            return TypeScalar(self.family, self, elem, value)
+        if elem['type'] in scalars:
+            t = TypeScalar(self.family, self, elem, value)
         elif elem['type'] == 'unused':
-            return TypeUnused(self.family, self, elem, value)
+            t = TypeUnused(self.family, self, elem, value)
         elif elem['type'] == 'pad':
-            return TypePad(self.family, self, elem, value)
+            t = TypePad(self.family, self, elem, value)
         elif elem['type'] == 'flag':
-            return TypeFlag(self.family, self, elem, value)
+            t = TypeFlag(self.family, self, elem, value)
         elif elem['type'] == 'string':
-            return TypeString(self.family, self, elem, value)
+            t = TypeString(self.family, self, elem, value)
         elif elem['type'] == 'binary':
-            return TypeBinary(self.family, self, elem, value)
+            t = TypeBinary(self.family, self, elem, value)
         elif elem['type'] == 'nest':
-            return TypeNest(self.family, self, elem, value)
+            t = TypeNest(self.family, self, elem, value)
         elif elem['type'] == 'array-nest':
-            return TypeArrayNest(self.family, self, elem, value)
+            t = TypeArrayNest(self.family, self, elem, value)
         elif elem['type'] == 'nest-type-value':
-            return TypeNestTypeValue(self.family, self, elem, value)
+            t = TypeNestTypeValue(self.family, self, elem, value)
         else:
             raise Exception(f"No typed class for type {elem['type']}")
 
+        if 'multi-attr' in elem and elem['multi-attr']:
+            t = TypeMultiAttr(self.family, self, elem, value, t)
+
+        return t
+
 
 class Operation(SpecOperation):
     def __init__(self, family, yaml, req_value, rsp_value):
Signature · 1 line
2.40.1
Reviewed-by on v1 2/2
#4 lore raw

RE: [PATCH net-next 2/2] tools: ynl-gen: inherit policy in multi-attr

Quoted 11 lines

-----Original Message-----
From: Jakub Kicinski <[email protected]>
Sent: Monday, June 12, 2023 8:59 AM
To: [email protected]
Cc: [email protected]; [email protected]; [email protected]; Kubalewski, Arkadiusz <[email protected]>; Jakub Kicinski <[email protected]>
Subject: [PATCH net-next 2/2] tools: ynl-gen: inherit policy in multi-attr

Quoted 6 lines

Instead of reimplementing policies in MutliAttr for every underlying type forward the calls to the base type. This will be needed for DPLL which uses a multi-attr nest, and currently gets an invalid NLA_NEST policy generated.

Quoted 90 lines
---
 tools/net/ynl/ynl-gen-c.py | 42 ++++++++++++++++++++++----------------
 1 file changed, 24 insertions(+), 18 deletions(-)
tools/net/ynl/ynl-gen-c.py +0 -0
index 54777d529f5e..71c5e79e877f 100755
@@ -462,6 +462,11 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr,

SpecOperation, SpecEnumSet, S

class TypeMultiAttr(Type):

tools/net/ynl/ynl-gen-c.py +5 -0
+    def __init__(self, family, attr_set, attr, value, base_type):
+        super().__init__(family, attr_set, attr, value)
+
+        self.base_type = base_type
+
     def is_multi_val(self):
         return True
@@ -497,13 +502,11 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr,

SpecOperation, SpecEnumSet, S
else:
raise Exception(f"Free of MultiAttr sub-type {self.attr['type']} not
supported yet")

tools/net/ynl/ynl-gen-c.py +4 -6
+    def _attr_policy(self, policy):
+        return self.base_type._attr_policy(policy)
+
     def _attr_typol(self):
-        if 'type' not in self.attr or self.attr['type'] == 'nest':
-            return f'.type = YNL_PT_NEST, .nest = &{self.nested_render_name}_nest, '
-        elif self.attr['type'] in scalars:
-            return f".type = YNL_PT_U{self.attr['type'][1:]}, "
-        else:
-            raise Exception(f"Sub-type {self.attr['type']} not supported yet")
+        return self.base_type._attr_typol()
 
     def _attr_get(self, ri, var):
         return f'n_{self.c_name}++;', None, None
@@ -717,29 +720,32 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr,

SpecOperation, SpecEnumSet, S
self.c_name = ''

def new_attr(self, elem, value):

tools/net/ynl/ynl-gen-c.py +15 -13
-        if 'multi-attr' in elem and elem['multi-attr']:
-            return TypeMultiAttr(self.family, self, elem, value)
-        elif elem['type'] in scalars:
-            return TypeScalar(self.family, self, elem, value)
+        if elem['type'] in scalars:
+            t = TypeScalar(self.family, self, elem, value)
         elif elem['type'] == 'unused':
-            return TypeUnused(self.family, self, elem, value)
+            t = TypeUnused(self.family, self, elem, value)
         elif elem['type'] == 'pad':
-            return TypePad(self.family, self, elem, value)
+            t = TypePad(self.family, self, elem, value)
         elif elem['type'] == 'flag':
-            return TypeFlag(self.family, self, elem, value)
+            t = TypeFlag(self.family, self, elem, value)
         elif elem['type'] == 'string':
-            return TypeString(self.family, self, elem, value)
+            t = TypeString(self.family, self, elem, value)
         elif elem['type'] == 'binary':
-            return TypeBinary(self.family, self, elem, value)
+            t = TypeBinary(self.family, self, elem, value)
         elif elem['type'] == 'nest':
-            return TypeNest(self.family, self, elem, value)
+            t = TypeNest(self.family, self, elem, value)
         elif elem['type'] == 'array-nest':
-            return TypeArrayNest(self.family, self, elem, value)
+            t = TypeArrayNest(self.family, self, elem, value)
         elif elem['type'] == 'nest-type-value':
-            return TypeNestTypeValue(self.family, self, elem, value)
+            t = TypeNestTypeValue(self.family, self, elem, value)
         else:
             raise Exception(f"No typed class for type {elem['type']}")
 
+        if 'multi-attr' in elem and elem['multi-attr']:
+            t = TypeMultiAttr(self.family, self, elem, value, t)
+
+        return t
+
 
 class Operation(SpecOperation):
     def __init__(self, family, yaml, req_value, rsp_value):
--

2.40.1

#5 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 8 lines · On Mon, 12 Jun 2023 08:59:18 -0700 you wrote:

A set of improvements needed to generate the kernel code for DPLL.

Jakub Kicinski (2):
tools: ynl-gen: correct enum policies
tools: ynl-gen: inherit policy in multi-attr

 tools/net/ynl/ynl-gen-c.py | 57 +++++++++++++++++++++++++-------------
 1 file changed, 37 insertions(+), 20 deletions(-)

Here is the summary with links:
- [net-next,1/2] tools: ynl-gen: correct enum policies

    https://git.kernel.org/netdev/net-next/c/10c4d2a7b88d
  - [net-next,2/2] tools: ynl-gen: inherit policy in multi-attr
    https://git.kernel.org/netdev/net-next/c/be093a80dff0

You are awesome, thank you!

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