Skip to contentSkip to conversation

tools: ynl-gen: fix parse multi-attr enum attribute

PATCH Superseded Arkadiusz Kubalewski 4 participants 11 messages Jul 18, 2023 – Jul 25, 2023

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover tools: ynl-gen: fix parse multi-attr enum attribute

Fix the issues with parsing enums in ynl.py script.

v2->v3:
- Restore i iterator helper for decoding 'enum-as-flag' attributes in _decode_enum(..) function.
- Fix _decode_enum(..) usage in case of binary & struct type attributes by moving it to as_struct() funtion while calling _decode_enum(..) for individual struct members.

v1->v2:
Initially this was one patch, but review shown there is need to fix also leftover issues with indexing in _decode_enum(..) function ("tools: ynl-gen: fix enum index in _decode_enum(..)").

Arkadiusz Kubalewski (2):
tools: ynl-gen: fix enum index in _decode_enum(..)
tools: ynl-gen: fix parse multi-attr enum attribute

 tools/net/ynl/lib/ynl.py | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
Signature · 1 line
2.38.1
#1 lore raw

PATCH v3 1/2 tools: ynl-gen: fix enum index in _decode_enum(..)

Remove wrong index adjustement, which is leftover from adding support for sparse enums.
enum.entries_by_val() function shall not subtract the start-value, as it is indexed with real enum value.

Fixes c311aaa74ca1 ("tools: ynl: fix enum-as-flags in the generic CLI")
---
 tools/net/ynl/lib/ynl.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/net/ynl/lib/ynl.py +2 -2
index 1b3a36fbb1c3..5db7d47067f9 100644
@@ -420,8 +420,8 @@ class YnlFamily(SpecFamily):
     def _decode_enum(self, rsp, attr_spec):
         raw = rsp[attr_spec['name']]
         enum = self.consts[attr_spec['enum']]
-        i = attr_spec.get('value-start', 0)
         if 'enum-as-flags' in attr_spec and attr_spec['enum-as-flags']:
+            i = attr_spec.get('value-start', 0)
             value = set()
             while raw:
                 if raw & 1:
@@ -429,7 +429,7 @@ class YnlFamily(SpecFamily):
                 raw >>= 1
                 i += 1
         else:
-            value = enum.entries_by_val[raw - i].name
+            value = enum.entries_by_val[raw].name
         rsp[attr_spec['name']] = value
 
     def _decode_binary(self, attr, attr_spec):
Signature · 1 line
2.38.1
maintainer
#2 lore raw

Re: [PATCH net-next v3 1/2] tools: ynl-gen: fix enum index in _decode_enum(..)

tools/net/ynl/lib/ynl.py class YnlFamily(SpecFamily): · line 424

Quoted 3 lines · On Tue, 18 Jul 2023 18:22:24 +0200 Arkadiusz Kubalewski wrote:
tools/net/ynl/lib/ynl.py +1 -1
-        i = attr_spec.get('value-start', 0)
         if 'enum-as-flags' in attr_spec and attr_spec['enum-as-flags']:
+            i = attr_spec.get('value-start', 0)

Just:

		i = 0
Signature · 1 line
pw-bot: cr
#3 lore raw

RE: [PATCH net-next v3 1/2] tools: ynl-gen: fix enum index in _decode_enum(..)

Quoted 10 lines

From: Jakub Kicinski <[email protected]>
Sent: Wednesday, July 19, 2023 5:17 AM

tools/net/ynl/lib/ynl.py class YnlFamily(SpecFamily): · line 424

Quoted 3 lines · On Tue, 18 Jul 2023 18:22:24 +0200 Arkadiusz Kubalewski wrote:
tools/net/ynl/lib/ynl.py +1 -1
-        i = attr_spec.get('value-start', 0)
         if 'enum-as-flags' in attr_spec and attr_spec['enum-as-flags']:
+            i = attr_spec.get('value-start', 0)

Just:

		i = 0

Sure, will do.

Thank you!
Arkadiusz

Quoted 2 lines

--
pw-bot: cr

#4 lore raw

Re: [PATCH net-next v3 1/2] tools: ynl-gen: fix enum index in _decode_enum(..)

In reply to Arkadiusz Kubalewski
Quoted 1 line · Arkadiusz Kubalewski

Remove wrong index adjustement, which is leftover from adding

nit: adjustement -> adjustment

...

#5 lore raw

RE: [PATCH net-next v3 1/2] tools: ynl-gen: fix enum index in _decode_enum(..)

Quoted 8 lines

From: Simon Horman <[email protected]>
Sent: Friday, July 21, 2023 10:55 AM

Quoted 1 line · Arkadiusz Kubalewski

Remove wrong index adjustement, which is leftover from adding

nit: adjustement -> adjustment

Yes, fixed.

Thank you!
Arkadiusz

Quoted 1 line

...

#6 lore raw

PATCH v3 2/2 tools: ynl-gen: fix parse multi-attr enum attribute

In reply to Arkadiusz Kubalewski

When attribute is enum type and marked as multi-attr, the netlink respond is not parsed, fails with stack trace:
Traceback (most recent call last):
File "/net-next/tools/net/ynl/./test.py", line 520, in <module>

    main()
  File "/net-next/tools/net/ynl/./test.py", line 488, in main
    dplls=dplls_get(282574471561216)

File "/net-next/tools/net/ynl/./test.py", line 48, in dplls_get
reply=act(args)
File "/net-next/tools/net/ynl/./test.py", line 41, in act
reply = ynl.dump(args.dump, attrs)
File "/net-next/tools/net/ynl/lib/ynl.py", line 598, in dump
return self._op(method, vals, dump=True)
File "/net-next/tools/net/ynl/lib/ynl.py", line 584, in _op
rsp_msg = self._decode(gm.raw_attrs, op.attr_set.name)
File "/net-next/tools/net/ynl/lib/ynl.py", line 451, in _decode
self._decode_enum(rsp, attr_spec)
File "/net-next/tools/net/ynl/lib/ynl.py", line 408, in _decode_enum
value = enum.entries_by_val[raw].name
TypeError: unhashable type: 'list'
error: 1

Redesign _decode_enum(..) to take a enum int value and translate it to either a bitmask or enum name as expected.

---
 tools/net/ynl/lib/ynl.py | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
tools/net/ynl/lib/ynl.py +10 -10
index 5db7d47067f9..671ef4b5eaa6 100644
@@ -135,7 +135,7 @@ class NlAttr:
         format = self.get_format(type)
         return [ x[0] for x in format.iter_unpack(self.raw) ]
 
-    def as_struct(self, members):
+    def as_struct(self, members, attr_spec):
         value = dict()
         offset = 0
         for m in members:
@@ -147,6 +147,9 @@ class NlAttr:
                 format = self.get_format(m.type, m.byte_order)
                 [ decoded ] = format.unpack_from(self.raw, offset)
                 offset += format.size
+
+            if m.enum:
+                decoded = self._decode_enum(decoded, attr_spec)
             if m.display_hint:
                 decoded = self.formatted_string(decoded, m.display_hint)
             value[m.name] = decoded
@@ -417,8 +420,7 @@ class YnlFamily(SpecFamily):
         pad = b'\x00' * ((4 - len(attr_payload) % 4) % 4)
         return struct.pack('HH', len(attr_payload) + 4, nl_type) + attr_payload + pad
 
-    def _decode_enum(self, rsp, attr_spec):
-        raw = rsp[attr_spec['name']]
+    def _decode_enum(self, raw, attr_spec):
         enum = self.consts[attr_spec['enum']]
         if 'enum-as-flags' in attr_spec and attr_spec['enum-as-flags']:
             i = attr_spec.get('value-start', 0)
@@ -430,15 +432,12 @@ class YnlFamily(SpecFamily):
                 i += 1
         else:
             value = enum.entries_by_val[raw].name
-        rsp[attr_spec['name']] = value
+        return value
 
     def _decode_binary(self, attr, attr_spec):
         if attr_spec.struct_name:
             members = self.consts[attr_spec.struct_name]
-            decoded = attr.as_struct(members)
-            for m in members:
-                if m.enum:
-                    self._decode_enum(decoded, m)
+            decoded = attr.as_struct(members, attr_spec)
         elif attr_spec.sub_type:
             decoded = attr.as_c_array(attr_spec.sub_type)
         else:
@@ -466,6 +465,9 @@ class YnlFamily(SpecFamily):
             else:
                 raise Exception(f'Unknown {attr_spec["type"]} with name {attr_spec["name"]}')
 
+            if 'enum' in attr_spec:
+                decoded = self._decode_enum(int.from_bytes(attr.raw, "big"), attr_spec)
+
             if not attr_spec.is_multi:
                 rsp[attr_spec['name']] = decoded
             elif attr_spec.name in rsp:
@@ -473,8 +475,6 @@ class YnlFamily(SpecFamily):
             else:
                 rsp[attr_spec.name] = [decoded]
 
-            if 'enum' in attr_spec:
-                self._decode_enum(rsp, attr_spec)
         return rsp
 
     def _decode_extack_path(self, attrs, attr_set, offset, target):
Signature · 1 line
2.38.1
maintainer
#7 lore raw

tools/net/ynl/lib/ynl.py class YnlFamily(SpecFamily): · line 469

Quoted 2 lines · On Tue, 18 Jul 2023 18:22:25 +0200 Arkadiusz Kubalewski wrote:
tools/net/ynl/lib/ynl.py +2 -0
+            if 'enum' in attr_spec:
+                decoded = self._decode_enum(int.from_bytes(attr.raw, "big"), attr_spec)

why int.from_bytes(attr.raw, "big")?
'decoded' already holds the integer at this point

#8 lore raw
Quoted 10 lines

From: Jakub Kicinski <[email protected]>
Sent: Wednesday, July 19, 2023 5:19 AM

Review

Quoted 2 lines · On Tue, 18 Jul 2023 18:22:25 +0200 Arkadiusz Kubalewski wrote:
(quoted diff) +2 -0
+            if 'enum' in attr_spec:
+                decoded = self._decode_enum(int.from_bytes(attr.raw,

"big"), attr_spec)

why int.from_bytes(attr.raw, "big")?
'decoded' already holds the integer at this point

Yes, will fix.

Thank you!
Arkadiusz

maintainer
#9 lore raw
In reply to Arkadiusz Kubalewski
Quoted 9 lines · Arkadiusz Kubalewski
 tools/net/ynl/lib/ynl.py | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
tools/net/ynl/lib/ynl.py +0 -0
index 5db7d47067f9..671ef4b5eaa6 100644

tools/net/ynl/lib/ynl.py class NlAttr: · line 138

Quoted 5 lines
tools/net/ynl/lib/ynl.py +1 -1
@@ -135,7 +135,7 @@ class NlAttr:
         format = self.get_format(type)
         return [ x[0] for x in format.iter_unpack(self.raw) ]
 
-    def as_struct(self, members):
+    def as_struct(self, members, attr_spec):

No need to pass attr_spec - it's the spec for the struct, not for the members.

tools/net/ynl/lib/ynl.py class NlAttr: · line 152

Quoted 10 lines
tools/net/ynl/lib/ynl.py +3 -0
         value = dict()
         offset = 0
         for m in members:
@@ -147,6 +147,9 @@ class NlAttr:
                 format = self.get_format(m.type, m.byte_order)
                 [ decoded ] = format.unpack_from(self.raw, offset)
                 offset += format.size
+
+            if m.enum:
+                decoded = self._decode_enum(decoded, attr_spec)

_decode_enum is not a method on NlAttr so I'm pretty sure this will fail. Looks like we need to move _decode_enum() into NlAttr?

The second param to _decode_enum should be 'm' which is the attr spec for the member.

Quoted 32 lines
tools/net/ynl/lib/ynl.py +3 -7
             if m.display_hint:
                 decoded = self.formatted_string(decoded, m.display_hint)
             value[m.name] = decoded
@@ -417,8 +420,7 @@ class YnlFamily(SpecFamily):
         pad = b'\x00' * ((4 - len(attr_payload) % 4) % 4)
         return struct.pack('HH', len(attr_payload) + 4, nl_type) + attr_payload + pad
 
-    def _decode_enum(self, rsp, attr_spec):
-        raw = rsp[attr_spec['name']]
+    def _decode_enum(self, raw, attr_spec):
         enum = self.consts[attr_spec['enum']]
         if 'enum-as-flags' in attr_spec and attr_spec['enum-as-flags']:
             i = attr_spec.get('value-start', 0)
@@ -430,15 +432,12 @@ class YnlFamily(SpecFamily):
                 i += 1
         else:
             value = enum.entries_by_val[raw].name
-        rsp[attr_spec['name']] = value
+        return value
 
     def _decode_binary(self, attr, attr_spec):
         if attr_spec.struct_name:
             members = self.consts[attr_spec.struct_name]
-            decoded = attr.as_struct(members)
-            for m in members:
-                if m.enum:
-                    self._decode_enum(decoded, m)
+            decoded = attr.as_struct(members, attr_spec)
         elif attr_spec.sub_type:
             decoded = attr.as_c_array(attr_spec.sub_type)
         else:

tools/net/ynl/lib/ynl.py class YnlFamily(SpecFamily): · line 469

Quoted 5 lines
tools/net/ynl/lib/ynl.py +2 -0
@@ -466,6 +465,9 @@ class YnlFamily(SpecFamily):
             else:
                 raise Exception(f'Unknown {attr_spec["type"]} with name {attr_spec["name"]}')
 
+            if 'enum' in attr_spec:
+                decoded = self._decode_enum(int.from_bytes(attr.raw, "big"), attr_spec)

As Jakub said, this should just be self._decode_enum(decoded, attr_spec)

Quoted 15 lines
tools/net/ynl/lib/ynl.py +1 -3
+
             if not attr_spec.is_multi:
                 rsp[attr_spec['name']] = decoded
             elif attr_spec.name in rsp:
@@ -473,8 +475,6 @@ class YnlFamily(SpecFamily):
             else:
                 rsp[attr_spec.name] = [decoded]
 
-            if 'enum' in attr_spec:
-                self._decode_enum(rsp, attr_spec)
         return rsp
 
     def _decode_extack_path(self, attrs, attr_set, offset, target):
--

2.38.1

#10 lore raw
Quoted 17 lines

From: Donald Hunter <[email protected]>
Sent: Wednesday, July 19, 2023 1:18 PM

Quoted 12 lines · Arkadiusz Kubalewski
 tools/net/ynl/lib/ynl.py | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
tools/net/ynl/lib/ynl.py +0 -0
index 5db7d47067f9..671ef4b5eaa6 100644
@@ -135,7 +135,7 @@ class NlAttr:
         format = self.get_format(type)
         return [ x[0] for x in format.iter_unpack(self.raw) ]
 
Quoted 5 lines

tools/net/ynl/lib/ynl.py class NlAttr: · line 138

Quoted 2 lines
tools/net/ynl/lib/ynl.py +1 -1
-    def as_struct(self, members):
+    def as_struct(self, members, attr_spec):

No need to pass attr_spec - it's the spec for the struct, not for the members.

Yep, that what I was thinking in previous version thread.

Quoted 15 lines

tools/net/ynl/lib/ynl.py class NlAttr: · line 152

Quoted 11 lines
tools/net/ynl/lib/ynl.py +3 -0
         value = dict()
         offset = 0
         for m in members:
@@ -147,6 +147,9 @@ class NlAttr:
                 format = self.get_format(m.type, m.byte_order)
                 [ decoded ] = format.unpack_from(self.raw, offset)
                 offset += format.size
+
+            if m.enum:
+                decoded = self._decode_enum(decoded, attr_spec)

_decode_enum is not a method on NlAttr so I'm pretty sure this will fail. Looks like we need to move _decode_enum() into NlAttr?

Are you sure about moving it? it seems all the decode function are members of YnlFamily.

Quoted 2 lines

The second param to _decode_enum should be 'm' which is the attr spec for the member.

Ok, will do. Altough I think of moving back _decode_enum() to the _decode_binary(), where it was initially, but with proper usage like:
for m in members:
if m.enum:
decoded[m.name] = self._decode_enum(decoded[m.name], m)

This way all the decode function would be kept in YnlFamily. Does it make sense?

Quoted 38 lines
Quoted 38 lines
tools/net/ynl/lib/ynl.py +0 -0
             if m.display_hint:
                 decoded = self.formatted_string(decoded, m.display_hint)
             value[m.name] = decoded
@@ -417,8 +420,7 @@ class YnlFamily(SpecFamily):
         pad = b'\x00' * ((4 - len(attr_payload) % 4) % 4)
         return struct.pack('HH', len(attr_payload) + 4, nl_type) +

attr_payload + pad

tools/net/ynl/lib/ynl.py +3 -7
-    def _decode_enum(self, rsp, attr_spec):
-        raw = rsp[attr_spec['name']]
+    def _decode_enum(self, raw, attr_spec):
         enum = self.consts[attr_spec['enum']]
         if 'enum-as-flags' in attr_spec and attr_spec['enum-as-flags']:
             i = attr_spec.get('value-start', 0)
@@ -430,15 +432,12 @@ class YnlFamily(SpecFamily):
                 i += 1
         else:
             value = enum.entries_by_val[raw].name
-        rsp[attr_spec['name']] = value
+        return value
 
     def _decode_binary(self, attr, attr_spec):
         if attr_spec.struct_name:
             members = self.consts[attr_spec.struct_name]
-            decoded = attr.as_struct(members)
-            for m in members:
-                if m.enum:
-                    self._decode_enum(decoded, m)
+            decoded = attr.as_struct(members, attr_spec)
         elif attr_spec.sub_type:
             decoded = attr.as_c_array(attr_spec.sub_type)
         else:
@@ -466,6 +465,9 @@ class YnlFamily(SpecFamily):
             else:
                 raise Exception(f'Unknown {attr_spec["type"]} with name
 {attr_spec["name"]}')
 
Quoted 6 lines

tools/net/ynl/lib/ynl.py class YnlFamily(SpecFamily): · line 471

Quoted 3 lines
tools/net/ynl/lib/ynl.py +2 -0
+            if 'enum' in attr_spec:
+                decoded = self._decode_enum(int.from_bytes(attr.raw,
 "big"), attr_spec)

As Jakub said, this should just be self._decode_enum(decoded, attr_spec)

Yes, will fix.

Thank you!
Arkadiusz

Quoted 15 lines
Quoted 15 lines
tools/net/ynl/lib/ynl.py +1 -0
+
             if not attr_spec.is_multi:

rsp[attr_spec['name']] = decoded
elif attr_spec.name in rsp:

tools/net/ynl/lib/ynl.py +0 -3
@@ -473,8 +475,6 @@ class YnlFamily(SpecFamily):
             else:
                 rsp[attr_spec.name] = [decoded]
 
-            if 'enum' in attr_spec:
-                self._decode_enum(rsp, attr_spec)
         return rsp
 
     def _decode_extack_path(self, attrs, attr_set, offset, target):
--

2.38.1