Message ID | 20240326063728.2369353-3-liuhangbin@gmail.com (mailing list archive) |
---|---|
State | Superseded |
Delegated to: | Netdev Maintainers |
Headers | show |
Series | ynl: rename array-nest to indexed-array | expand |
Hi Jakub, On Tue, Mar 26, 2024 at 02:37:28PM +0800, Hangbin Liu wrote: > Support un-nest sub-type for indexed-array. Since all the attr types are > same for un-nest sub-ype, the index number is used as attr name. > The result would look like: > > # ip link add bond0 type bond mode 1 \ > arp_ip_target 192.168.1.1,192.168.1.2 ns_ip6_target 2001::1,2001::2 > # ./tools/net/ynl/cli.py --spec Documentation/netlink/specs/rt_link.yaml \ > --do getlink --json '{"ifname": "bond0"}' --output-json | jq '.linkinfo' > > "arp-ip-target": [ > { > "1": "192.168.1.1" > }, > { > "2": "192.168.1.2" > } > ], For index array, do you think if we need to add the index in the result like upper example? Or we just omit the index and show it like: "arp-ip-target": [ "192.168.1.1", "192.168.1.2" ], "ns-ip6-target": [ "2001::1", "2001::2" ], Thanks Hangbin
On Thu, 28 Mar 2024 15:41:53 +0800 Hangbin Liu wrote: > > # ip link add bond0 type bond mode 1 \ > > arp_ip_target 192.168.1.1,192.168.1.2 ns_ip6_target 2001::1,2001::2 > > # ./tools/net/ynl/cli.py --spec Documentation/netlink/specs/rt_link.yaml \ > > --do getlink --json '{"ifname": "bond0"}' --output-json | jq '.linkinfo' > > > > "arp-ip-target": [ > > { > > "1": "192.168.1.1" > > }, > > { > > "2": "192.168.1.2" > > } > > ], > > For index array, do you think if we need to add the index in the result > like upper example? Or we just omit the index and show it like: Yes, the index in some funny dumps can actually be non-contiguous. You should use the value from the attr, like the nest does.
diff --git a/tools/net/ynl/lib/ynl.py b/tools/net/ynl/lib/ynl.py index 7239e673a28a..58a602ff9544 100644 --- a/tools/net/ynl/lib/ynl.py +++ b/tools/net/ynl/lib/ynl.py @@ -595,6 +595,21 @@ class YnlFamily(SpecFamily): decoded.append({ item.type: subattrs }) return decoded + def _decode_index_array(self, attr, attr_spec): + decoded = [] + offset = 0 + index = 0 + while offset < len(attr.raw): + index = index + 1 + item = NlAttr(attr.raw, offset) + offset += item.full_len + + subattrs = item.as_bin() + if attr_spec.display_hint: + subattrs = self._formatted_string(subattrs, attr_spec.display_hint) + decoded.append({ index: subattrs }) + return decoded + def _decode_nest_type_value(self, attr, attr_spec): decoded = {} value = attr @@ -689,6 +704,8 @@ class YnlFamily(SpecFamily): elif attr_spec["type"] == 'indexed-array' and 'sub-type' in attr_spec: if attr_spec["sub-type"] == 'nest': decoded = self._decode_array_nest(attr, attr_spec) + else: + decoded = self._decode_index_array(attr, attr_spec) elif attr_spec["type"] == 'bitfield32': value, selector = struct.unpack("II", attr.raw) if 'enum' in attr_spec:
Support un-nest sub-type for indexed-array. Since all the attr types are same for un-nest sub-ype, the index number is used as attr name. The result would look like: # ip link add bond0 type bond mode 1 \ arp_ip_target 192.168.1.1,192.168.1.2 ns_ip6_target 2001::1,2001::2 # ./tools/net/ynl/cli.py --spec Documentation/netlink/specs/rt_link.yaml \ --do getlink --json '{"ifname": "bond0"}' --output-json | jq '.linkinfo' "arp-ip-target": [ { "1": "192.168.1.1" }, { "2": "192.168.1.2" } ], [...] "ns-ip6-target": [ { "1": "2001::1" }, { "2": "2001::2" } ], Signed-off-by: Hangbin Liu <liuhangbin@gmail.com> --- tools/net/ynl/lib/ynl.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)