netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Donald Hunter <donald.hunter@gmail.com>
To: netdev@vger.kernel.org, Jakub Kicinski <kuba@kernel.org>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Paolo Abeni <pabeni@redhat.com>, Jonathan Corbet <corbet@lwn.net>,
	linux-doc@vger.kernel.org,
	Jacob Keller <jacob.e.keller@intel.com>,
	Breno Leitao <leitao@debian.org>, Jiri Pirko <jiri@resnulli.us>,
	Alessandro Marcolini <alessandromarcolini99@gmail.com>
Cc: donald.hunter@redhat.com, Donald Hunter <donald.hunter@gmail.com>
Subject: [PATCH net-next v1 06/12] tools/net/ynl: Combine struct decoding logic in ynl
Date: Tue, 23 Jan 2024 16:05:32 +0000	[thread overview]
Message-ID: <20240123160538.172-7-donald.hunter@gmail.com> (raw)
In-Reply-To: <20240123160538.172-1-donald.hunter@gmail.com>

_decode_fixed_header() and NlAttr.as_struct() both implemented struct
decoding logic. Deduplicate the code into newly named _decode_struct()
method.

Signed-off-by: Donald Hunter <donald.hunter@gmail.com>
---
 tools/net/ynl/lib/ynl.py | 47 ++++++++++++----------------------------
 1 file changed, 14 insertions(+), 33 deletions(-)

diff --git a/tools/net/ynl/lib/ynl.py b/tools/net/ynl/lib/ynl.py
index ea4638c56802..cc1106cbe8a6 100644
--- a/tools/net/ynl/lib/ynl.py
+++ b/tools/net/ynl/lib/ynl.py
@@ -148,23 +148,6 @@ class NlAttr:
         format = self.get_format(type)
         return [ x[0] for x in format.iter_unpack(self.raw) ]
 
-    def as_struct(self, members):
-        value = dict()
-        offset = 0
-        for m in members:
-            # TODO: handle non-scalar members
-            if m.type == 'binary':
-                decoded = self.raw[offset : offset + m['len']]
-                offset += m['len']
-            elif m.type in NlAttr.type_formats:
-                format = self.get_format(m.type, m.byte_order)
-                [ decoded ] = format.unpack_from(self.raw, offset)
-                offset += format.size
-            if m.display_hint:
-                decoded = self.formatted_string(decoded, m.display_hint)
-            value[m.name] = decoded
-        return value
-
     def __repr__(self):
         return f"[type:{self.type} len:{self._len}] {self.raw}"
 
@@ -521,11 +504,7 @@ class YnlFamily(SpecFamily):
 
     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:
-                    decoded[m.name] = self._decode_enum(decoded[m.name], m)
+            decoded = self._decode_struct(attr.raw, attr_spec.struct_name)
         elif attr_spec.sub_type:
             decoded = attr.as_c_array(attr_spec.sub_type)
         else:
@@ -587,7 +566,7 @@ class YnlFamily(SpecFamily):
         decoded = {}
         offset = 0
         if msg_format.fixed_header:
-            decoded.update(self._decode_fixed_header(attr, msg_format.fixed_header));
+            decoded.update(self._decode_struct(attr.raw, msg_format.fixed_header));
             offset = self._fixed_header_size(msg_format.fixed_header)
         if msg_format.attr_set:
             if msg_format.attr_set in self.attr_sets:
@@ -698,26 +677,28 @@ class YnlFamily(SpecFamily):
         else:
             return 0
 
-    def _decode_fixed_header(self, msg, name):
-        fixed_header_members = self.consts[name].members
-        fixed_header_attrs = dict()
+    def _decode_struct(self, data, name):
+        members = self.consts[name].members
+        attrs = dict()
         offset = 0
-        for m in fixed_header_members:
+        for m in members:
             value = None
             if m.type == 'pad':
                 offset += m.len
             elif m.type == 'binary':
-                value = msg.raw[offset : offset + m.len]
+                value = data[offset : offset + m.len]
                 offset += m.len
             else:
                 format = NlAttr.get_format(m.type, m.byte_order)
-                [ value ] = format.unpack_from(msg.raw, offset)
+                [ value ] = format.unpack_from(data, offset)
                 offset += format.size
             if value is not None:
                 if m.enum:
                     value = self._decode_enum(value, m)
-                fixed_header_attrs[m.name] = value
-        return fixed_header_attrs
+                elif m.display_hint:
+                    value = NlAttr.formatted_string(value, m.display_hint)
+                attrs[m.name] = value
+        return attrs
 
     def _encode_struct(self, name, vals):
         members = self.consts[name].members
@@ -745,7 +726,7 @@ class YnlFamily(SpecFamily):
         op = self.rsp_by_value[decoded.cmd()]
         attrs = self._decode(decoded.raw_attrs, op.attr_set.name)
         if op.fixed_header:
-            attrs.update(self._decode_fixed_header(decoded, op.fixed_header))
+            attrs.update(self._decode_struct(decoded.raw, op.fixed_header))
 
         msg['name'] = op['name']
         msg['msg'] = attrs
@@ -836,7 +817,7 @@ class YnlFamily(SpecFamily):
 
                 rsp_msg = self._decode(decoded.raw_attrs, op.attr_set.name)
                 if op.fixed_header:
-                    rsp_msg.update(self._decode_fixed_header(decoded, op.fixed_header))
+                    rsp_msg.update(self._decode_struct(decoded.raw, op.fixed_header))
                 rsp.append(rsp_msg)
 
         if not rsp:
-- 
2.42.0


  parent reply	other threads:[~2024-01-23 16:05 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-23 16:05 [PATCH net-next v1 00/12] tools/net/ynl: Add features for tc family Donald Hunter
2024-01-23 16:05 ` [PATCH net-next v1 01/12] tools/net/ynl: Add --output-json arg to ynl cli Donald Hunter
2024-01-25 13:50   ` Breno Leitao
2024-01-23 16:05 ` [PATCH net-next v1 02/12] tools/net/ynl: Support sub-messages in nested attribute spaces Donald Hunter
2024-01-24  0:18   ` Jakub Kicinski
2024-01-24  9:37     ` Donald Hunter
2024-01-24 15:32       ` Jakub Kicinski
2024-01-26 12:44         ` Donald Hunter
2024-01-26 18:50           ` Jakub Kicinski
2024-01-27 17:18             ` Donald Hunter
2024-01-27 18:52               ` Alessandro Marcolini
2024-01-28 19:36                 ` Donald Hunter
2024-01-29 20:35                   ` Alessandro Marcolini
2024-01-30  1:32                   ` Jakub Kicinski
2024-01-30  1:42               ` Jakub Kicinski
2024-01-30  9:12                 ` Donald Hunter
2024-02-01 20:53                 ` Jacob Keller
2024-02-02  0:04                   ` Jakub Kicinski
2024-02-02 17:12                     ` Jacob Keller
2024-01-23 16:05 ` [PATCH net-next v1 03/12] tools/net/ynl: Refactor fixed header encoding into separate method Donald Hunter
2024-01-23 16:05 ` [PATCH net-next v1 04/12] tools/net/ynl: Add support for encoding sub-messages Donald Hunter
2024-01-23 16:05 ` [PATCH net-next v1 05/12] tools/net/ynl: Encode default values for binary blobs Donald Hunter
2024-01-23 16:05 ` Donald Hunter [this message]
2024-01-23 16:05 ` [PATCH net-next v1 07/12] tools/net/ynl: Rename _fixed_header_size() to _struct_size() Donald Hunter
2024-01-23 16:05 ` [PATCH net-next v1 08/12] tools/net/ynl: Move formatted_string method out of NlAttr Donald Hunter
2024-01-25 14:24   ` Breno Leitao
2024-01-23 16:05 ` [PATCH net-next v1 09/12] tools/net/ynl: Add support for nested structs Donald Hunter
2024-01-23 16:05 ` [PATCH net-next v1 10/12] doc/netlink: Describe nested structs in netlink raw docs Donald Hunter
2024-01-23 16:05 ` [PATCH net-next v1 11/12] tools/net/ynl: Add type info to struct members in generated docs Donald Hunter
2024-01-25 13:59   ` Breno Leitao
2024-01-23 16:05 ` [PATCH net-next v1 12/12] doc/netlink/specs: Update the tc spec Donald Hunter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240123160538.172-7-donald.hunter@gmail.com \
    --to=donald.hunter@gmail.com \
    --cc=alessandromarcolini99@gmail.com \
    --cc=corbet@lwn.net \
    --cc=davem@davemloft.net \
    --cc=donald.hunter@redhat.com \
    --cc=edumazet@google.com \
    --cc=jacob.e.keller@intel.com \
    --cc=jiri@resnulli.us \
    --cc=kuba@kernel.org \
    --cc=leitao@debian.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).