Re: [PATCH net-next v1 1/2] tools: ynl: add user-header and struct attr support
From: Donald Hunter <hidden>
Date: 2023-03-18 16:47:54
On Sat, 18 Mar 2023 at 04:50, Jakub Kicinski [off-list ref] wrote:
On Thu, 16 Mar 2023 12:01:41 +0000 Donald Hunter wrote:quoted
Subject: [PATCH net-next v1 1/2] tools: ynl: add user-header and struct attr supportThe use of "and" usually indicates it should be 2 separate patches ;)
Ack. I'll try and split it into two.
quoted
+ user-header: + description: Name of the struct definition for the user header for the family. + type: stringTook me a minute to remember this is header as in protocol header not header as in C header file :) Would it possibly be better to call it fixed-header ? Can't really decide myself.
I went with user header because the generic netlink howto calls it the "optional user specific message header" but happy to go with fixed-header.
But the description definitely need to be more verbose: description: | Name of the structure defining the fixed-length protocol header. This header is placed in a message after the netlink and genetlink headers and before any attributes.
Agreed, this is a much clearer description.
quoted
+ def as_array(self, type): + format, _ = self.type_formats[type] + return list({ x[0] for x in struct.iter_unpack(format, self.raw) })The Python is strong within you :)quoted
+ def as_struct(self, members): + value = dict() + offset = 0 + for m in members: + type = m['type']Accessing the spec components directly is a bit of an anti-pattern, can we parse the struct description into Python objects in tools/net/ynl/lib/nlspec.py ?
Ack, will do.
quoted
+ format, size = self.type_formats[type] + decoded = struct.unpack_from(format, self.raw, offset) + offset += size + value[m['name']] = decoded[0] + return value + def __repr__(self): return f"[type:{self.type} len:{self._len}] {self.raw}"@@ -200,7 +220,7 @@ def _genl_msg(nl_type, nl_flags, genl_cmd, genl_version, seq=None): if seq is None: seq = random.randint(1, 1024) nlmsg = struct.pack("HHII", nl_type, nl_flags, seq, 0) - genlmsg = struct.pack("bbH", genl_cmd, genl_version, 0) + genlmsg = struct.pack("BBH", genl_cmd, genl_version, 0)Should also be a separate patch
Yep, I will separate this into its own patch.
quoted
return nlmsg + genlmsg@@ -258,14 +278,22 @@ def _genl_load_families(): class GenlMsg: - def __init__(self, nl_msg): + def __init__(self, nl_msg, extra_headers = []): self.nl = nl_msg self.hdr = nl_msg.raw[0:4] - self.raw = nl_msg.raw[4:] + offset = 4 - self.genl_cmd, self.genl_version, _ = struct.unpack("bbH", self.hdr) + self.genl_cmd, self.genl_version, _ = struct.unpack("BBH", self.hdr) + self.user_attrs = dict() + for m in extra_headers: + format, size = NlAttr.type_formats[m['type']] + decoded = struct.unpack_from(format, nl_msg.raw, offset) + offset += size + self.user_attrs[m['name']] = decoded[0]user_attrs?
Um, attrs of the user-header. I'll try to name this better.
quoted
+ self.raw = nl_msg.raw[offset:] self.raw_attrs = NlAttrs(self.raw) def __repr__(self):@@ -315,6 +343,7 @@ class YnlFamily(SpecFamily): setattr(self, op.ident_name, bound_f) self.family = GenlFamily(self.yaml['name']) + self._user_header = self.yaml.get('user-header', None) def ntf_subscribe(self, mcast_name): if mcast_name not in self.family.genl_family['mcast']:@@ -358,7 +387,7 @@ class YnlFamily(SpecFamily): raw >>= 1 i += 1 else: - value = enum['entries'][raw - i] + value = enum.entries_by_val[raw - i]['name']Also a separate fix :S
Ack, will do.