Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tools: ynl: Add struct attr decoding to ynl

Add support for decoding attributes that contain C structs.

Signed-off-by: Donald Hunter <donald.hunter@gmail.com>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Donald Hunter and committed by
Jakub Kicinski
26071913 b423c3c8

+21 -1
+2
tools/net/ynl/lib/nlspec.py
··· 152 152 value numerical ID when serialized 153 153 attr_set Attribute Set containing this attr 154 154 is_multi bool, attr may repeat multiple times 155 + struct_name string, name of struct definition 155 156 sub_type string, name of sub type 156 157 """ 157 158 def __init__(self, family, attr_set, yaml, value): ··· 161 160 self.value = value 162 161 self.attr_set = attr_set 163 162 self.is_multi = yaml.get('multi-attr', False) 163 + self.struct_name = yaml.get('struct') 164 164 self.sub_type = yaml.get('sub-type') 165 165 166 166
+14 -1
tools/net/ynl/lib/ynl.py
··· 102 102 format, _ = self.type_formats[type] 103 103 return list({ x[0] for x in struct.iter_unpack(format, self.raw) }) 104 104 105 + def as_struct(self, members): 106 + value = dict() 107 + offset = 0 108 + for m in members: 109 + # TODO: handle non-scalar members 110 + format, size = self.type_formats[m.type] 111 + decoded = struct.unpack_from(format, self.raw, offset) 112 + offset += size 113 + value[m.name] = decoded[0] 114 + return value 115 + 105 116 def __repr__(self): 106 117 return f"[type:{self.type} len:{self._len}] {self.raw}" 107 118 ··· 388 377 rsp[attr_spec['name']] = value 389 378 390 379 def _decode_binary(self, attr, attr_spec): 391 - if attr_spec.sub_type: 380 + if attr_spec.struct_name: 381 + decoded = attr.as_struct(self.consts[attr_spec.struct_name]) 382 + elif attr_spec.sub_type: 392 383 decoded = attr.as_c_array(attr_spec.sub_type) 393 384 else: 394 385 decoded = attr.as_bin()