[PATCH net-next 11/11] tools: ynl: add ipv4-or-v6 display hint

Asbjørn Sloth Tønnesen posted 11 patches 4 weeks ago
There is a newer version of this series
[PATCH net-next 11/11] tools: ynl: add ipv4-or-v6 display hint
Posted by Asbjørn Sloth Tønnesen 4 weeks ago
The attribute WGALLOWEDIP_A_IPADDR can contain either an IPv4
or an IPv6 address depending on WGALLOWEDIP_A_FAMILY, however
in practice it is enough to look at the attribute length.

This patch implements an ipv4-or-v6 display hint, that can
deal with this kind of attribute.

It only implements this display hint for genetlink-legacy, it
can be added to other protocol variants if needed, but we don't
want to encourage it's use.

Signed-off-by: Asbjørn Sloth Tønnesen <ast@fiberby.net>
---
 Documentation/netlink/genetlink-legacy.yaml | 2 +-
 tools/net/ynl/pyynl/lib/ynl.py              | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/Documentation/netlink/genetlink-legacy.yaml b/Documentation/netlink/genetlink-legacy.yaml
index b29d62eefa16..66fb8653a344 100644
--- a/Documentation/netlink/genetlink-legacy.yaml
+++ b/Documentation/netlink/genetlink-legacy.yaml
@@ -154,7 +154,7 @@ properties:
                   Optional format indicator that is intended only for choosing
                   the right formatting mechanism when displaying values of this
                   type.
-                enum: [ hex, mac, fddi, ipv4, ipv6, uuid ]
+                enum: [ hex, mac, fddi, ipv4, ipv6, ipv4-or-v6, uuid ]
               struct:
                 description: Name of the nested struct type.
                 type: string
diff --git a/tools/net/ynl/pyynl/lib/ynl.py b/tools/net/ynl/pyynl/lib/ynl.py
index 78c0245ca587..6d2f12d43a08 100644
--- a/tools/net/ynl/pyynl/lib/ynl.py
+++ b/tools/net/ynl/pyynl/lib/ynl.py
@@ -958,7 +958,7 @@ class YnlFamily(SpecFamily):
                 formatted = hex(raw)
             else:
                 formatted = bytes.hex(raw, ' ')
-        elif display_hint in [ 'ipv4', 'ipv6' ]:
+        elif display_hint in [ 'ipv4', 'ipv6', 'ipv4-or-v6' ]:
             formatted = format(ipaddress.ip_address(raw))
         elif display_hint == 'uuid':
             formatted = str(uuid.UUID(bytes=raw))
@@ -967,7 +967,7 @@ class YnlFamily(SpecFamily):
         return formatted
 
     def _from_string(self, string, attr_spec):
-        if attr_spec.display_hint in ['ipv4', 'ipv6']:
+        if attr_spec.display_hint in ['ipv4', 'ipv6', 'ipv4-or-v6']:
             ip = ipaddress.ip_address(string)
             if attr_spec['type'] == 'binary':
                 raw = ip.packed
-- 
2.51.0

Re: [PATCH net-next 11/11] tools: ynl: add ipv4-or-v6 display hint
Posted by Donald Hunter 3 weeks, 6 days ago
Asbjørn Sloth Tønnesen <ast@fiberby.net> writes:

> The attribute WGALLOWEDIP_A_IPADDR can contain either an IPv4
> or an IPv6 address depending on WGALLOWEDIP_A_FAMILY, however
> in practice it is enough to look at the attribute length.
>
> This patch implements an ipv4-or-v6 display hint, that can
> deal with this kind of attribute.
>
> It only implements this display hint for genetlink-legacy, it
> can be added to other protocol variants if needed, but we don't
> want to encourage it's use.
>
> Signed-off-by: Asbjørn Sloth Tønnesen <ast@fiberby.net>

Reviewed-by: Donald Hunter <donald.hunter@gmail.com>

I suspect there are occurrences of ipv4 or ipv6 in the existing specs
that really should be ipv4-or-ipv6 but the python code doesn't care.
Re: [PATCH net-next 11/11] tools: ynl: add ipv4-or-v6 display hint
Posted by Asbjørn Sloth Tønnesen 3 weeks, 5 days ago
On 9/5/25 10:53 AM, Donald Hunter wrote:
> Asbjørn Sloth Tønnesen <ast@fiberby.net> writes:
>> The attribute WGALLOWEDIP_A_IPADDR can contain either an IPv4
>> or an IPv6 address depending on WGALLOWEDIP_A_FAMILY, however
>> in practice it is enough to look at the attribute length.
>>
>> This patch implements an ipv4-or-v6 display hint, that can
>> deal with this kind of attribute.
>>
>> It only implements this display hint for genetlink-legacy, it
>> can be added to other protocol variants if needed, but we don't
>> want to encourage it's use.
>>
>> Signed-off-by: Asbjørn Sloth Tønnesen <ast@fiberby.net>
> 
> Reviewed-by: Donald Hunter <donald.hunter@gmail.com>
> 
> I suspect there are occurrences of ipv4 or ipv6 in the existing specs
> that really should be ipv4-or-ipv6 but the python code doesn't care.

I haven't been able to find any, containing 'ip' or 'addr'.

Speaking of display hints, then WGPEER_A_ENDPOINT is another interesting
case, it is struct sockaddr_in or struct sockaddr_in6, I have left that
as a plain binary for now, but maybe that could be a struct map, based
on struct length.

attribute-sets:
   -
     name: wgpeer
     [..]
     attributes:
       [..]
       -
         name: endpoint
         type: binary
         struct-map:
           - sockaddr_in
           - sockaddr_in6

With the requirement being, that all structs must have a unique length.