diff mbox series

[PATCHv3,net-next,2/2] ynl: support binary/u32 sub-type for indexed-array

Message ID 20240401035651.1251874-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 warning 2 maintainers not CCed: linux-doc@vger.kernel.org corbet@lwn.net
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-04-01--06-00 (tests: 952)

Commit Message

Hangbin Liu April 1, 2024, 3:56 a.m. UTC
Add binary/u32 sub-type support for indexed-array to display bond
arp and ns targets. Here is what the result looks 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": [
      "192.168.1.1",
      "192.168.1.2"
    ],
    [...]
    "ns-ip6-target": [
      "2001::1",
      "2001::2"
    ],

Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
---
 .../userspace-api/netlink/genetlink-legacy.rst       | 12 +++++++++---
 tools/net/ynl/lib/ynl.py                             |  5 +++++
 2 files changed, 14 insertions(+), 3 deletions(-)

Comments

Jakub Kicinski April 2, 2024, 4:43 a.m. UTC | #1
On Mon,  1 Apr 2024 11:56:51 +0800 Hangbin Liu wrote:
> +Other ``sub-type`` like ``u32`` means there is only one member as described
> +in ``sub-type`` in the ``ENTRY``. The structure looks like::
> +
> +  [SOME-OTHER-ATTR]
> +  [ARRAY-ATTR]
> +    [ENTRY]
> +      [MEMBER1]
> +    [ENTRY]
> +      [MEMBER1]

I think that elsewhere in the doc we use [SOMETHING] to mean
TLV of type SOMETHING, here MEMBER1/2 are presumably just
payloads of each ENTRY? Maybe this is better:

  [SOME-OTHER-ATTR]
  [ARRAY-ATTR]
    [ENTRY u32]
    [ENTRY u32]

?

>  type-value
>  ~~~~~~~~~~
> diff --git a/tools/net/ynl/lib/ynl.py b/tools/net/ynl/lib/ynl.py
> index e5ad415905c7..aa7077cffe74 100644
> --- a/tools/net/ynl/lib/ynl.py
> +++ b/tools/net/ynl/lib/ynl.py
> @@ -640,6 +640,11 @@ class YnlFamily(SpecFamily):
>              if attr_spec["sub-type"] == 'nest':
>                  subattrs = self._decode(NlAttrs(item.raw), attr_spec['nested-attributes'])
>                  decoded.append({ item.type: subattrs })
> +            elif attr_spec["sub-type"] == 'binary' or attr_spec["sub-type"] == 'u32':
> +                subattrs = item.as_bin()

Are you sure that as_bin() will work for all u32s?
Or just when there's a hint...

> +                if attr_spec.display_hint:
> +                    subattrs = self._formatted_string(subattrs, attr_spec.display_hint)
> +                decoded.append(subattrs)
Hangbin Liu April 3, 2024, 2:28 a.m. UTC | #2
On Mon, Apr 01, 2024 at 09:43:31PM -0700, Jakub Kicinski wrote:
> I think that elsewhere in the doc we use [SOMETHING] to mean
> TLV of type SOMETHING, here MEMBER1/2 are presumably just
> payloads of each ENTRY? Maybe this is better:
> 
>   [SOME-OTHER-ATTR]
>   [ARRAY-ATTR]
>     [ENTRY u32]
>     [ENTRY u32]
> 

Thanks, I will update the doc.
> ?
> 
> >  type-value
> >  ~~~~~~~~~~
> > diff --git a/tools/net/ynl/lib/ynl.py b/tools/net/ynl/lib/ynl.py
> > index e5ad415905c7..aa7077cffe74 100644
> > --- a/tools/net/ynl/lib/ynl.py
> > +++ b/tools/net/ynl/lib/ynl.py
> > @@ -640,6 +640,11 @@ class YnlFamily(SpecFamily):
> >              if attr_spec["sub-type"] == 'nest':
> >                  subattrs = self._decode(NlAttrs(item.raw), attr_spec['nested-attributes'])
> >                  decoded.append({ item.type: subattrs })
> > +            elif attr_spec["sub-type"] == 'binary' or attr_spec["sub-type"] == 'u32':
> > +                subattrs = item.as_bin()
> 
> Are you sure that as_bin() will work for all u32s?
> Or just when there's a hint...

I didn't check other subsystem. For bonding only, if we don't have the hint.
e.g.

  -
    name: arp-ip-target
    type: indexed-array
    sub-type: u32

The result will looks like:

    "arp-ip-target": [
      "c0a80101",
      "c0a80102"
    ],

Which looks good to me. Do you have other suggestion?

Thanks
Hangbin
Jakub Kicinski April 3, 2024, 2:35 a.m. UTC | #3
On Wed, 3 Apr 2024 10:28:34 +0800 Hangbin Liu wrote:
> I didn't check other subsystem. For bonding only, if we don't have the hint.
> e.g.
> 
>   -
>     name: arp-ip-target
>     type: indexed-array
>     sub-type: u32
> 
> The result will looks like:
> 
>     "arp-ip-target": [
>       "c0a80101",
>       "c0a80102"
>     ],
> 
> Which looks good to me. Do you have other suggestion?

That doesn't look right, without the format hint if the type is u32 
the members should be plain integers not hex strings.
Hangbin Liu April 3, 2024, 3:27 a.m. UTC | #4
On Tue, Apr 02, 2024 at 07:35:51PM -0700, Jakub Kicinski wrote:
> On Wed, 3 Apr 2024 10:28:34 +0800 Hangbin Liu wrote:
> > I didn't check other subsystem. For bonding only, if we don't have the hint.
> > e.g.
> > 
> >   -
> >     name: arp-ip-target
> >     type: indexed-array
> >     sub-type: u32
> > 
> > The result will looks like:
> > 
> >     "arp-ip-target": [
> >       "c0a80101",
> >       "c0a80102"
> >     ],
> > 
> > Which looks good to me. Do you have other suggestion?
> 
> That doesn't look right, without the format hint if the type is u32 
> the members should be plain integers not hex strings.

OK, I can separate the binary and u32 dealing. How about like

diff --git a/tools/net/ynl/lib/ynl.py b/tools/net/ynl/lib/ynl.py
index e5ad415905c7..be42e4fc1037 100644
--- a/tools/net/ynl/lib/ynl.py
+++ b/tools/net/ynl/lib/ynl.py
@@ -640,6 +640,16 @@ class YnlFamily(SpecFamily):
             if attr_spec["sub-type"] == 'nest':
                 subattrs = self._decode(NlAttrs(item.raw), attr_spec['nested-attributes'])
                 decoded.append({ item.type: subattrs })
+            elif attr_spec["sub-type"] == 'binary':
+                subattrs = item.as_bin()
+                if attr_spec.display_hint:
+                    subattrs = self._formatted_string(subattrs, attr_spec.display_hint)
+                decoded.append(subattrs)
+            elif attr_spec["sub-type"] in NlAttr.type_formats:
+                subattrs = item.as_scalar(attr_spec['sub-type'], attr_spec.byte_order)
+                if attr_spec.display_hint:
+                    subattrs = self._formatted_string(subattrs, attr_spec.display_hint)
+                decoded.append(subattrs)
             else:
                 raise Exception(f'Unknown {attr_spec["sub-type"]} with name {attr_spec["name"]}')
         return decoded


With only sub-type: u32 it shows like

    "arp-ip-target": [
      3232235777,
      3232235778
    ],

Thanks
Hangbin
Jakub Kicinski April 4, 2024, 12:07 a.m. UTC | #5
On Wed, 3 Apr 2024 11:27:22 +0800 Hangbin Liu wrote:
> OK, I can separate the binary and u32 dealing. How about like
> 
> diff --git a/tools/net/ynl/lib/ynl.py b/tools/net/ynl/lib/ynl.py
> index e5ad415905c7..be42e4fc1037 100644
> --- a/tools/net/ynl/lib/ynl.py
> +++ b/tools/net/ynl/lib/ynl.py
> @@ -640,6 +640,16 @@ class YnlFamily(SpecFamily):
>              if attr_spec["sub-type"] == 'nest':
>                  subattrs = self._decode(NlAttrs(item.raw), attr_spec['nested-attributes'])
>                  decoded.append({ item.type: subattrs })
> +            elif attr_spec["sub-type"] == 'binary':
> +                subattrs = item.as_bin()
> +                if attr_spec.display_hint:
> +                    subattrs = self._formatted_string(subattrs, attr_spec.display_hint)
> +                decoded.append(subattrs)
> +            elif attr_spec["sub-type"] in NlAttr.type_formats:
> +                subattrs = item.as_scalar(attr_spec['sub-type'], attr_spec.byte_order)
> +                if attr_spec.display_hint:
> +                    subattrs = self._formatted_string(subattrs, attr_spec.display_hint)
> +                decoded.append(subattrs)

Aha, this look right!
diff mbox series

Patch

diff --git a/Documentation/userspace-api/netlink/genetlink-legacy.rst b/Documentation/userspace-api/netlink/genetlink-legacy.rst
index 54e8fb25e093..6525ef6ca62f 100644
--- a/Documentation/userspace-api/netlink/genetlink-legacy.rst
+++ b/Documentation/userspace-api/netlink/genetlink-legacy.rst
@@ -66,9 +66,15 @@  looks like::
       [MEMBER1]
       [MEMBER2]
 
-It wraps the entire array in an extra attribute (hence limiting its size
-to 64kB). The ``ENTRY`` nests are special and have the index of the entry
-as their type instead of normal attribute type.
+Other ``sub-type`` like ``u32`` means there is only one member as described
+in ``sub-type`` in the ``ENTRY``. The structure looks like::
+
+  [SOME-OTHER-ATTR]
+  [ARRAY-ATTR]
+    [ENTRY]
+      [MEMBER1]
+    [ENTRY]
+      [MEMBER1]
 
 type-value
 ~~~~~~~~~~
diff --git a/tools/net/ynl/lib/ynl.py b/tools/net/ynl/lib/ynl.py
index e5ad415905c7..aa7077cffe74 100644
--- a/tools/net/ynl/lib/ynl.py
+++ b/tools/net/ynl/lib/ynl.py
@@ -640,6 +640,11 @@  class YnlFamily(SpecFamily):
             if attr_spec["sub-type"] == 'nest':
                 subattrs = self._decode(NlAttrs(item.raw), attr_spec['nested-attributes'])
                 decoded.append({ item.type: subattrs })
+            elif attr_spec["sub-type"] == 'binary' or attr_spec["sub-type"] == 'u32':
+                subattrs = item.as_bin()
+                if attr_spec.display_hint:
+                    subattrs = self._formatted_string(subattrs, attr_spec.display_hint)
+                decoded.append(subattrs)
             else:
                 raise Exception(f'Unknown {attr_spec["sub-type"]} with name {attr_spec["name"]}')
         return decoded