public inbox for netdev@vger.kernel.org 
 help / color / mirror / Atom feed
From: Stanislav Fomichev <sdf@google•com>
To: netdev@vger•kernel.org
Cc: davem@davemloft•net, edumazet@google•com, kuba@kernel•org,
	pabeni@redhat•com, Stanislav Fomichev <sdf@google•com>
Subject: [PATCH net-next v2 1/4] tools: ynl: support byte-order in cli
Date: Fri, 24 Mar 2023 15:56:53 -0700	[thread overview]
Message-ID: <20230324225656.3999785-2-sdf@google.com> (raw)
In-Reply-To: <20230324225656.3999785-1-sdf@google.com>

Used by ethtool spec.

Signed-off-by: Stanislav Fomichev <sdf@google•com>
---
 tools/net/ynl/lib/ynl.py | 47 +++++++++++++++++++++++++---------------
 1 file changed, 29 insertions(+), 18 deletions(-)

diff --git a/tools/net/ynl/lib/ynl.py b/tools/net/ynl/lib/ynl.py
index 788f130a7cc3..1220314d3303 100644
--- a/tools/net/ynl/lib/ynl.py
+++ b/tools/net/ynl/lib/ynl.py
@@ -75,17 +75,25 @@ from .nlspec import SpecFamily
         self.full_len = (self.payload_len + 3) & ~3
         self.raw = raw[offset + 4:offset + self.payload_len]
 
+    def format_byte_order(byte_order):
+        if byte_order:
+            return ">" if byte_order == "big-endian" else "<"
+        return ""
+
     def as_u8(self):
         return struct.unpack("B", self.raw)[0]
 
-    def as_u16(self):
-        return struct.unpack("H", self.raw)[0]
+    def as_u16(self, byte_order):
+        endian = NlAttr.format_byte_order(byte_order)
+        return struct.unpack(f"{endian}H", self.raw)[0]
 
-    def as_u32(self):
-        return struct.unpack("I", self.raw)[0]
+    def as_u32(self, byte_order):
+        endian = NlAttr.format_byte_order(byte_order)
+        return struct.unpack(f"{endian}I", self.raw)[0]
 
-    def as_u64(self):
-        return struct.unpack("Q", self.raw)[0]
+    def as_u64(self, byte_order):
+        endian = NlAttr.format_byte_order(byte_order)
+        return struct.unpack(f"{endian}Q", self.raw)[0]
 
     def as_strz(self):
         return self.raw.decode('ascii')[:-1]
@@ -148,11 +156,11 @@ from .nlspec import SpecFamily
                 if extack.type == Netlink.NLMSGERR_ATTR_MSG:
                     self.extack['msg'] = extack.as_strz()
                 elif extack.type == Netlink.NLMSGERR_ATTR_MISS_TYPE:
-                    self.extack['miss-type'] = extack.as_u32()
+                    self.extack['miss-type'] = extack.as_u32(None)
                 elif extack.type == Netlink.NLMSGERR_ATTR_MISS_NEST:
-                    self.extack['miss-nest'] = extack.as_u32()
+                    self.extack['miss-nest'] = extack.as_u32(None)
                 elif extack.type == Netlink.NLMSGERR_ATTR_OFFS:
-                    self.extack['bad-attr-offs'] = extack.as_u32()
+                    self.extack['bad-attr-offs'] = extack.as_u32(None)
                 else:
                     if 'unknown' not in self.extack:
                         self.extack['unknown'] = []
@@ -236,11 +244,11 @@ genl_family_name_to_id = None
                 fam = dict()
                 for attr in gm.raw_attrs:
                     if attr.type == Netlink.CTRL_ATTR_FAMILY_ID:
-                        fam['id'] = attr.as_u16()
+                        fam['id'] = attr.as_u16(None)
                     elif attr.type == Netlink.CTRL_ATTR_FAMILY_NAME:
                         fam['name'] = attr.as_strz()
                     elif attr.type == Netlink.CTRL_ATTR_MAXATTR:
-                        fam['maxattr'] = attr.as_u32()
+                        fam['maxattr'] = attr.as_u32(None)
                     elif attr.type == Netlink.CTRL_ATTR_MCAST_GROUPS:
                         fam['mcast'] = dict()
                         for entry in NlAttrs(attr.raw):
@@ -250,7 +258,7 @@ genl_family_name_to_id = None
                                 if entry_attr.type == Netlink.CTRL_ATTR_MCAST_GRP_NAME:
                                     mcast_name = entry_attr.as_strz()
                                 elif entry_attr.type == Netlink.CTRL_ATTR_MCAST_GRP_ID:
-                                    mcast_id = entry_attr.as_u32()
+                                    mcast_id = entry_attr.as_u32(None)
                             if mcast_name and mcast_id is not None:
                                 fam['mcast'][mcast_name] = mcast_id
                 if 'name' in fam and 'id' in fam:
@@ -337,11 +345,14 @@ genl_family_name_to_id = None
         elif attr["type"] == 'u8':
             attr_payload = struct.pack("B", int(value))
         elif attr["type"] == 'u16':
-            attr_payload = struct.pack("H", int(value))
+            endian = NlAttr.format_byte_order(attr.get('byte-order'))
+            attr_payload = struct.pack(f"{endian}H", int(value))
         elif attr["type"] == 'u32':
-            attr_payload = struct.pack("I", int(value))
+            endian = NlAttr.format_byte_order(attr.get('byte-order'))
+            attr_payload = struct.pack(f"{endian}I", int(value))
         elif attr["type"] == 'u64':
-            attr_payload = struct.pack("Q", int(value))
+            endian = NlAttr.format_byte_order(attr.get('byte-order'))
+            attr_payload = struct.pack(f"{endian}Q", int(value))
         elif attr["type"] == 'string':
             attr_payload = str(value).encode('ascii') + b'\x00'
         elif attr["type"] == 'binary':
@@ -378,11 +389,11 @@ genl_family_name_to_id = None
             elif attr_spec['type'] == 'u8':
                 decoded = attr.as_u8()
             elif attr_spec['type'] == 'u16':
-                decoded = attr.as_u16()
+                decoded = attr.as_u16(attr_spec.get('byte-order'))
             elif attr_spec['type'] == 'u32':
-                decoded = attr.as_u32()
+                decoded = attr.as_u32(attr_spec.get('byte-order'))
             elif attr_spec['type'] == 'u64':
-                decoded = attr.as_u64()
+                decoded = attr.as_u64(attr_spec.get('byte-order'))
             elif attr_spec["type"] == 'string':
                 decoded = attr.as_strz()
             elif attr_spec["type"] == 'binary':
-- 
2.40.0.348.gf938b09366-goog


  reply	other threads:[~2023-03-24 22:57 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-24 22:56 [PATCH net-next v2 0/4] tools: ynl: fill in some gaps of ethtool spec Stanislav Fomichev
2023-03-24 22:56 ` Stanislav Fomichev [this message]
2023-03-25  3:33   ` [PATCH net-next v2 1/4] tools: ynl: support byte-order in cli Jakub Kicinski
2023-03-29  0:07     ` Stanislav Fomichev
2023-03-24 22:56 ` [PATCH net-next v2 2/4] tools: ynl: populate most of the ethtool spec Stanislav Fomichev
2023-03-24 22:56 ` [PATCH net-next v2 3/4] tools: ynl: replace print with NlError Stanislav Fomichev
2023-03-24 22:56 ` [PATCH net-next v2 4/4] tools: ynl: ethtool testing tool Stanislav Fomichev
2023-03-25  3:35   ` Jakub Kicinski
2023-03-29  0:06     ` Stanislav Fomichev

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=20230324225656.3999785-2-sdf@google.com \
    --to=sdf@google$(echo .)com \
    --cc=davem@davemloft$(echo .)net \
    --cc=edumazet@google$(echo .)com \
    --cc=kuba@kernel$(echo .)org \
    --cc=netdev@vger$(echo .)kernel.org \
    --cc=pabeni@redhat$(echo .)com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
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