diff mbox series

[net-next,2/2] ynl: support un-nest sub-type for indexed-array

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

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 8 this patch: 8
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers success CCed 5 of 5 maintainers
netdev/build_clang success Errors and warnings before: 8 this patch: 8
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 8 this patch: 8
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 29 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-03-27--00-00 (tests: 948)

Commit Message

Hangbin Liu March 26, 2024, 6:37 a.m. UTC
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(+)

Comments

Hangbin Liu March 28, 2024, 7:41 a.m. UTC | #1
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
Jakub Kicinski March 28, 2024, 4:04 p.m. UTC | #2
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 mbox series

Patch

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: