diff mbox series

[net-next] tools: ynl-gen: fix NLA_POLICY_MAX on enums with value

Message ID 20230613231709.150622-2-arkadiusz.kubalewski@intel.com (mailing list archive)
State Not Applicable
Delegated to: Netdev Maintainers
Headers show
Series [net-next] tools: ynl-gen: fix NLA_POLICY_MAX on enums with value | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for net-next
netdev/apply fail Patch does not apply to net-next

Commit Message

Arkadiusz Kubalewski June 13, 2023, 11:17 p.m. UTC
Policy defines max as length of enum list, if enum attribute is
defined with a value, generated netlink policy max value is wrong.
Calculate proper max value for policy where enums that are given
explicit value.

Signed-off-by: Arkadiusz Kubalewski <arkadiusz.kubalewski@intel.com>
---
 tools/net/ynl/ynl-gen-c.py | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/tools/net/ynl/ynl-gen-c.py b/tools/net/ynl/ynl-gen-c.py
index 4c12c6f8968e..9adcd785db0b 100755
--- a/tools/net/ynl/ynl-gen-c.py
+++ b/tools/net/ynl/ynl-gen-c.py
@@ -279,7 +279,16 @@  class TypeScalar(Type):
         elif 'enum' in self.attr:
             enum = self.family.consts[self.attr['enum']]
             cnt = len(enum['entries'])
-            return f"NLA_POLICY_MAX({policy}, {cnt - 1})"
+            values = [0] * cnt
+            for i in range(cnt):
+                if 'value' in enum['entries'][i]:
+                    values[i] = enum['entries'][i]['value']
+                else:
+                    if i == 0:
+                        values[i] = 0;
+                    else:
+                        values[i] = values[i - 1] + 1
+            return f"NLA_POLICY_MAX({policy}, {max(values)})"
         return super()._attr_policy(policy)
 
     def _attr_typol(self):