Skip to content

Commit

Permalink
tools/net/ynl: Add support for encoding sub-messages
Browse files Browse the repository at this point in the history
Add sub-message encoding to ynl. This makes it possible to create
tc qdiscs and other polymorphic netlink objects.

Signed-off-by: Donald Hunter <donald.hunter@gmail.com>
Reviewed-by: Jiri Pirko <jiri@nvidia.com>
Link: https://lore.kernel.org/r/20240129223458.52046-6-donald.hunter@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
  • Loading branch information
donaldh authored and kuba-moo committed Feb 1, 2024
1 parent 5f2823c commit ab463c4
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions tools/net/ynl/lib/ynl.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ def ntf_subscribe(self, mcast_name):
self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_ADD_MEMBERSHIP,
mcast_id)

def _add_attr(self, space, name, value):
def _add_attr(self, space, name, value, search_attrs):
try:
attr = self.attr_sets[space][name]
except KeyError:
Expand All @@ -478,8 +478,10 @@ def _add_attr(self, space, name, value):
if attr["type"] == 'nest':
nl_type |= Netlink.NLA_F_NESTED
attr_payload = b''
sub_attrs = SpaceAttrs(self.attr_sets[space], value, search_attrs)
for subname, subvalue in value.items():
attr_payload += self._add_attr(attr['nested-attributes'], subname, subvalue)
attr_payload += self._add_attr(attr['nested-attributes'],
subname, subvalue, sub_attrs)
elif attr["type"] == 'flag':
attr_payload = b''
elif attr["type"] == 'string':
Expand All @@ -489,6 +491,8 @@ def _add_attr(self, space, name, value):
attr_payload = value
elif isinstance(value, str):
attr_payload = bytes.fromhex(value)
elif isinstance(value, dict) and attr.struct_name:
attr_payload = self._encode_struct(attr.struct_name, value)
else:
raise Exception(f'Unknown type for binary attribute, value: {value}')
elif attr.is_auto_scalar:
Expand All @@ -501,6 +505,20 @@ def _add_attr(self, space, name, value):
attr_payload = format.pack(int(value))
elif attr['type'] in "bitfield32":
attr_payload = struct.pack("II", int(value["value"]), int(value["selector"]))
elif attr['type'] == 'sub-message':
msg_format = self._resolve_selector(attr, search_attrs)
attr_payload = b''
if msg_format.fixed_header:
attr_payload += self._encode_struct(msg_format.fixed_header, value)
if msg_format.attr_set:
if msg_format.attr_set in self.attr_sets:
nl_type |= Netlink.NLA_F_NESTED
sub_attrs = SpaceAttrs(msg_format.attr_set, value, search_attrs)
for subname, subvalue in value.items():
attr_payload += self._add_attr(msg_format.attr_set,
subname, subvalue, sub_attrs)
else:
raise Exception(f"Unknown attribute-set '{msg_format.attr_set}'")
else:
raise Exception(f'Unknown type at {space} {name} {value} {attr["type"]}')

Expand Down Expand Up @@ -637,7 +655,7 @@ def _decode(self, attrs, space, outer_attrs = None):
selector = self._decode_enum(selector, attr_spec)
decoded = {"value": value, "selector": selector}
elif attr_spec["type"] == 'sub-message':
decoded = self._decode_sub_msg(attr, attr_spec, vals)
decoded = self._decode_sub_msg(attr, attr_spec, search_attrs)
else:
if not self.process_unknown:
raise Exception(f'Unknown {attr_spec["type"]} with name {attr_spec["name"]}')
Expand Down Expand Up @@ -795,8 +813,9 @@ def _op(self, method, vals, flags=None, dump=False):
msg = self.nlproto.message(nl_flags, op.req_value, 1, req_seq)
if op.fixed_header:
msg += self._encode_struct(op.fixed_header, vals)
search_attrs = SpaceAttrs(op.attr_set, vals)
for name, value in vals.items():
msg += self._add_attr(op.attr_set.name, name, value)
msg += self._add_attr(op.attr_set.name, name, value, search_attrs)
msg = _genl_msg_finalize(msg)

self.sock.send(msg, 0)
Expand Down

0 comments on commit ab463c4

Please sign in to comment.