diff options
author | Donald Hunter <donald.hunter@gmail.com> | 2024-01-29 22:34:49 +0000 |
---|---|---|
committer | Jakub Kicinski <kuba@kernel.org> | 2024-01-31 21:19:18 -0800 |
commit | 5f2823c48ad6f63fc033e8ec5e995fcd5cbab409 (patch) | |
tree | ae0b6ba16295796049a8ab9d14f67ec00bb4ad75 /tools/net/ynl/lib/ynl.py | |
parent | 78d23416979500c749049d5d20bac457bcca2fb5 (diff) |
tools/net/ynl: Refactor fixed header encoding into separate method
Refactor the fixed header encoding into a separate _encode_struct method
so that it can be reused for fixed headers in sub-messages and for
encoding structs.
Signed-off-by: Donald Hunter <donald.hunter@gmail.com>
Reviewed-by: Breno Leitao <leitao@debian.org>
Reviewed-by: Jiri Pirko <jiri@nvidia.com>
Link: https://lore.kernel.org/r/20240129223458.52046-5-donald.hunter@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Diffstat (limited to 'tools/net/ynl/lib/ynl.py')
-rw-r--r-- | tools/net/ynl/lib/ynl.py | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/tools/net/ynl/lib/ynl.py b/tools/net/ynl/lib/ynl.py index f24581759acf..b22ddedb801b 100644 --- a/tools/net/ynl/lib/ynl.py +++ b/tools/net/ynl/lib/ynl.py @@ -720,6 +720,20 @@ class YnlFamily(SpecFamily): fixed_header_attrs[m.name] = value return fixed_header_attrs + def _encode_struct(self, name, vals): + members = self.consts[name].members + attr_payload = b'' + for m in members: + value = vals.pop(m.name) if m.name in vals else 0 + if m.type == 'pad': + attr_payload += bytearray(m.len) + elif m.type == 'binary': + attr_payload += bytes.fromhex(value) + else: + format = NlAttr.get_format(m.type, m.byte_order) + attr_payload += format.pack(value) + return attr_payload + def handle_ntf(self, decoded): msg = dict() if self.include_raw: @@ -779,18 +793,8 @@ class YnlFamily(SpecFamily): req_seq = random.randint(1024, 65535) msg = self.nlproto.message(nl_flags, op.req_value, 1, req_seq) - fixed_header_members = [] if op.fixed_header: - fixed_header_members = self.consts[op.fixed_header].members - for m in fixed_header_members: - value = vals.pop(m.name) if m.name in vals else 0 - if m.type == 'pad': - msg += bytearray(m.len) - elif m.type == 'binary': - msg += bytes.fromhex(value) - else: - format = NlAttr.get_format(m.type, m.byte_order) - msg += format.pack(value) + msg += self._encode_struct(op.fixed_header, vals) for name, value in vals.items(): msg += self._add_attr(op.attr_set.name, name, value) msg = _genl_msg_finalize(msg) |