Skip to content

Commit 9f7cc57

Browse files
fomichevkuba-moo
authored andcommitted
tools: ynl: support byte-order in cli
Used by ethtool spec. Signed-off-by: Stanislav Fomichev <sdf@google.com> Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 709d0b8 commit 9f7cc57

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

tools/net/ynl/lib/nlspec.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ def __init__(self, family, attr_set, yaml, value):
163163
self.is_multi = yaml.get('multi-attr', False)
164164
self.struct_name = yaml.get('struct')
165165
self.sub_type = yaml.get('sub-type')
166+
self.byte_order = yaml.get('byte-order')
166167

167168

168169
class SpecAttrSet(SpecElement):

tools/net/ynl/lib/ynl.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,25 @@ def __init__(self, raw, offset):
8080
self.full_len = (self.payload_len + 3) & ~3
8181
self.raw = raw[offset + 4:offset + self.payload_len]
8282

83+
def format_byte_order(byte_order):
84+
if byte_order:
85+
return ">" if byte_order == "big-endian" else "<"
86+
return ""
87+
8388
def as_u8(self):
8489
return struct.unpack("B", self.raw)[0]
8590

86-
def as_u16(self):
87-
return struct.unpack("H", self.raw)[0]
91+
def as_u16(self, byte_order=None):
92+
endian = NlAttr.format_byte_order(byte_order)
93+
return struct.unpack(f"{endian}H", self.raw)[0]
8894

89-
def as_u32(self):
90-
return struct.unpack("I", self.raw)[0]
95+
def as_u32(self, byte_order=None):
96+
endian = NlAttr.format_byte_order(byte_order)
97+
return struct.unpack(f"{endian}I", self.raw)[0]
9198

92-
def as_u64(self):
93-
return struct.unpack("Q", self.raw)[0]
99+
def as_u64(self, byte_order=None):
100+
endian = NlAttr.format_byte_order(byte_order)
101+
return struct.unpack(f"{endian}Q", self.raw)[0]
94102

95103
def as_strz(self):
96104
return self.raw.decode('ascii')[:-1]
@@ -365,11 +373,14 @@ def _add_attr(self, space, name, value):
365373
elif attr["type"] == 'u8':
366374
attr_payload = struct.pack("B", int(value))
367375
elif attr["type"] == 'u16':
368-
attr_payload = struct.pack("H", int(value))
376+
endian = NlAttr.format_byte_order(attr.byte_order)
377+
attr_payload = struct.pack(f"{endian}H", int(value))
369378
elif attr["type"] == 'u32':
370-
attr_payload = struct.pack("I", int(value))
379+
endian = NlAttr.format_byte_order(attr.byte_order)
380+
attr_payload = struct.pack(f"{endian}I", int(value))
371381
elif attr["type"] == 'u64':
372-
attr_payload = struct.pack("Q", int(value))
382+
endian = NlAttr.format_byte_order(attr.byte_order)
383+
attr_payload = struct.pack(f"{endian}Q", int(value))
373384
elif attr["type"] == 'string':
374385
attr_payload = str(value).encode('ascii') + b'\x00'
375386
elif attr["type"] == 'binary':
@@ -415,11 +426,11 @@ def _decode(self, attrs, space):
415426
elif attr_spec['type'] == 'u8':
416427
decoded = attr.as_u8()
417428
elif attr_spec['type'] == 'u16':
418-
decoded = attr.as_u16()
429+
decoded = attr.as_u16(attr_spec.byte_order)
419430
elif attr_spec['type'] == 'u32':
420-
decoded = attr.as_u32()
431+
decoded = attr.as_u32(attr_spec.byte_order)
421432
elif attr_spec['type'] == 'u64':
422-
decoded = attr.as_u64()
433+
decoded = attr.as_u64(attr_spec.byte_order)
423434
elif attr_spec["type"] == 'string':
424435
decoded = attr.as_strz()
425436
elif attr_spec["type"] == 'binary':

0 commit comments

Comments
 (0)