Commit 9f7cc57f authored by Stanislav Fomichev's avatar Stanislav Fomichev Committed by Jakub Kicinski

tools: ynl: support byte-order in cli

Used by ethtool spec.
Signed-off-by: default avatarStanislav Fomichev <sdf@google.com>
Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent 709d0b88
...@@ -163,6 +163,7 @@ class SpecAttr(SpecElement): ...@@ -163,6 +163,7 @@ class SpecAttr(SpecElement):
self.is_multi = yaml.get('multi-attr', False) self.is_multi = yaml.get('multi-attr', False)
self.struct_name = yaml.get('struct') self.struct_name = yaml.get('struct')
self.sub_type = yaml.get('sub-type') self.sub_type = yaml.get('sub-type')
self.byte_order = yaml.get('byte-order')
class SpecAttrSet(SpecElement): class SpecAttrSet(SpecElement):
......
...@@ -80,17 +80,25 @@ class NlAttr: ...@@ -80,17 +80,25 @@ class NlAttr:
self.full_len = (self.payload_len + 3) & ~3 self.full_len = (self.payload_len + 3) & ~3
self.raw = raw[offset + 4:offset + self.payload_len] 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): def as_u8(self):
return struct.unpack("B", self.raw)[0] return struct.unpack("B", self.raw)[0]
def as_u16(self): def as_u16(self, byte_order=None):
return struct.unpack("H", self.raw)[0] endian = NlAttr.format_byte_order(byte_order)
return struct.unpack(f"{endian}H", self.raw)[0]
def as_u32(self): def as_u32(self, byte_order=None):
return struct.unpack("I", self.raw)[0] endian = NlAttr.format_byte_order(byte_order)
return struct.unpack(f"{endian}I", self.raw)[0]
def as_u64(self): def as_u64(self, byte_order=None):
return struct.unpack("Q", self.raw)[0] endian = NlAttr.format_byte_order(byte_order)
return struct.unpack(f"{endian}Q", self.raw)[0]
def as_strz(self): def as_strz(self):
return self.raw.decode('ascii')[:-1] return self.raw.decode('ascii')[:-1]
...@@ -365,11 +373,14 @@ class YnlFamily(SpecFamily): ...@@ -365,11 +373,14 @@ class YnlFamily(SpecFamily):
elif attr["type"] == 'u8': elif attr["type"] == 'u8':
attr_payload = struct.pack("B", int(value)) attr_payload = struct.pack("B", int(value))
elif attr["type"] == 'u16': elif attr["type"] == 'u16':
attr_payload = struct.pack("H", int(value)) endian = NlAttr.format_byte_order(attr.byte_order)
attr_payload = struct.pack(f"{endian}H", int(value))
elif attr["type"] == 'u32': elif attr["type"] == 'u32':
attr_payload = struct.pack("I", int(value)) endian = NlAttr.format_byte_order(attr.byte_order)
attr_payload = struct.pack(f"{endian}I", int(value))
elif attr["type"] == 'u64': elif attr["type"] == 'u64':
attr_payload = struct.pack("Q", int(value)) endian = NlAttr.format_byte_order(attr.byte_order)
attr_payload = struct.pack(f"{endian}Q", int(value))
elif attr["type"] == 'string': elif attr["type"] == 'string':
attr_payload = str(value).encode('ascii') + b'\x00' attr_payload = str(value).encode('ascii') + b'\x00'
elif attr["type"] == 'binary': elif attr["type"] == 'binary':
...@@ -415,11 +426,11 @@ class YnlFamily(SpecFamily): ...@@ -415,11 +426,11 @@ class YnlFamily(SpecFamily):
elif attr_spec['type'] == 'u8': elif attr_spec['type'] == 'u8':
decoded = attr.as_u8() decoded = attr.as_u8()
elif attr_spec['type'] == 'u16': elif attr_spec['type'] == 'u16':
decoded = attr.as_u16() decoded = attr.as_u16(attr_spec.byte_order)
elif attr_spec['type'] == 'u32': elif attr_spec['type'] == 'u32':
decoded = attr.as_u32() decoded = attr.as_u32(attr_spec.byte_order)
elif attr_spec['type'] == 'u64': elif attr_spec['type'] == 'u64':
decoded = attr.as_u64() decoded = attr.as_u64(attr_spec.byte_order)
elif attr_spec["type"] == 'string': elif attr_spec["type"] == 'string':
decoded = attr.as_strz() decoded = attr.as_strz()
elif attr_spec["type"] == 'binary': elif attr_spec["type"] == 'binary':
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment