Commit 649bde90 authored by Jakub Kicinski's avatar Jakub Kicinski
Browse files

tools: ynl: allow passing binary data



Recent changes made us assume that input for binary data is in hex.
When using YNL as a Python library it's possible to pass in raw bytes.
Bring the ability to do that back.

Reviewed-by: default avatarDonald Hunter <donald.hunter@gmail.com>
Link: https://lore.kernel.org/r/20230824003056.1436637-2-kuba@kernel.org


Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent 10ea77e4
Loading
Loading
Loading
Loading
+6 −1
Original line number Original line Diff line number Diff line
@@ -410,7 +410,12 @@ class YnlFamily(SpecFamily):
        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':
            if isinstance(value, bytes):
                attr_payload = value
            elif isinstance(value, str):
                attr_payload = bytes.fromhex(value)
                attr_payload = bytes.fromhex(value)
            else:
                raise Exception(f'Unknown type for binary attribute, value: {value}')
        elif attr['type'] in NlAttr.type_formats:
        elif attr['type'] in NlAttr.type_formats:
            format = NlAttr.get_format(attr['type'], attr.byte_order)
            format = NlAttr.get_format(attr['type'], attr.byte_order)
            attr_payload = format.pack(int(value))
            attr_payload = format.pack(int(value))