Skip to content

Commit d8eea68

Browse files
donaldhkuba-moo
authored andcommitted
tools: ynl: add display-hint support to ynl
Add support to the ynl tool for rendering output based on display-hint properties. Signed-off-by: Donald Hunter <donald.hunter@gmail.com> Link: https://lore.kernel.org/r/20230623201928.14275-3-donald.hunter@gmail.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 737eab7 commit d8eea68

2 files changed

Lines changed: 39 additions & 5 deletions

File tree

tools/net/ynl/lib/nlspec.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ class SpecAttr(SpecElement):
154154
is_multi bool, attr may repeat multiple times
155155
struct_name string, name of struct definition
156156
sub_type string, name of sub type
157+
len integer, optional byte length of binary types
158+
display_hint string, hint to help choose format specifier
159+
when displaying the value
157160
"""
158161
def __init__(self, family, attr_set, yaml, value):
159162
super().__init__(family, yaml)
@@ -164,6 +167,8 @@ def __init__(self, family, attr_set, yaml, value):
164167
self.struct_name = yaml.get('struct')
165168
self.sub_type = yaml.get('sub-type')
166169
self.byte_order = yaml.get('byte-order')
170+
self.len = yaml.get('len')
171+
self.display_hint = yaml.get('display-hint')
167172

168173

169174
class SpecAttrSet(SpecElement):
@@ -229,12 +234,17 @@ class SpecStructMember(SpecElement):
229234
type string, type of the member attribute
230235
byte_order string or None for native byte order
231236
enum string, name of the enum definition
237+
len integer, optional byte length of binary types
238+
display_hint string, hint to help choose format specifier
239+
when displaying the value
232240
"""
233241
def __init__(self, family, yaml):
234242
super().__init__(family, yaml)
235243
self.type = yaml['type']
236244
self.byte_order = yaml.get('byte-order')
237245
self.enum = yaml.get('enum')
246+
self.len = yaml.get('len')
247+
self.display_hint = yaml.get('display-hint')
238248

239249

240250
class SpecStruct(SpecElement):

tools/net/ynl/lib/ynl.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import struct
99
from struct import Struct
1010
import yaml
11+
import ipaddress
12+
import uuid
1113

1214
from .nlspec import SpecFamily
1315

@@ -105,6 +107,20 @@ def get_format(cls, attr_type, byte_order=None):
105107
else format.little
106108
return format.native
107109

110+
@classmethod
111+
def formatted_string(cls, raw, display_hint):
112+
if display_hint == 'mac':
113+
formatted = ':'.join('%02x' % b for b in raw)
114+
elif display_hint == 'hex':
115+
formatted = bytes.hex(raw, ' ')
116+
elif display_hint in [ 'ipv4', 'ipv6' ]:
117+
formatted = format(ipaddress.ip_address(raw))
118+
elif display_hint == 'uuid':
119+
formatted = str(uuid.UUID(bytes=raw))
120+
else:
121+
formatted = raw
122+
return formatted
123+
108124
def as_scalar(self, attr_type, byte_order=None):
109125
format = self.get_format(attr_type, byte_order)
110126
return format.unpack(self.raw)[0]
@@ -124,10 +140,16 @@ def as_struct(self, members):
124140
offset = 0
125141
for m in members:
126142
# TODO: handle non-scalar members
127-
format = self.get_format(m.type, m.byte_order)
128-
decoded = format.unpack_from(self.raw, offset)
129-
offset += format.size
130-
value[m.name] = decoded[0]
143+
if m.type == 'binary':
144+
decoded = self.raw[offset:offset+m['len']]
145+
offset += m['len']
146+
elif m.type in NlAttr.type_formats:
147+
format = self.get_format(m.type, m.byte_order)
148+
[ decoded ] = format.unpack_from(self.raw, offset)
149+
offset += format.size
150+
if m.display_hint:
151+
decoded = self.formatted_string(decoded, m.display_hint)
152+
value[m.name] = decoded
131153
return value
132154

133155
def __repr__(self):
@@ -385,7 +407,7 @@ def _add_attr(self, space, name, value):
385407
elif attr["type"] == 'string':
386408
attr_payload = str(value).encode('ascii') + b'\x00'
387409
elif attr["type"] == 'binary':
388-
attr_payload = value
410+
attr_payload = bytes.fromhex(value)
389411
elif attr['type'] in NlAttr.type_formats:
390412
format = NlAttr.get_format(attr['type'], attr.byte_order)
391413
attr_payload = format.pack(int(value))
@@ -421,6 +443,8 @@ def _decode_binary(self, attr, attr_spec):
421443
decoded = attr.as_c_array(attr_spec.sub_type)
422444
else:
423445
decoded = attr.as_bin()
446+
if attr_spec.display_hint:
447+
decoded = NlAttr.formatted_string(decoded, attr_spec.display_hint)
424448
return decoded
425449

426450
def _decode(self, attrs, space):

0 commit comments

Comments
 (0)