[PATCH net-next] tools: ynl-gen: use big-endian netlink attribute types

Asbjørn Sloth Tønnesen posted 1 patch 2 months, 2 weeks ago
There is a newer version of this series
net/ipv4/fou_nl.c          | 4 ++--
net/mptcp/mptcp_pm_gen.c   | 2 +-
tools/net/ynl/ynl-gen-c.py | 6 +++++-
3 files changed, 8 insertions(+), 4 deletions(-)
[PATCH net-next] tools: ynl-gen: use big-endian netlink attribute types
Posted by Asbjørn Sloth Tønnesen 2 months, 2 weeks ago
Change ynl-gen-c.py to use NLA_BE16 and NLA_BE32 types to represent
big-endian u16 and u32 ynl types.

Doing this enables those attributes to have range checks applied, as
the validator will then convert to host endianness prior to validation.

The autogenerated kernel/uapi code have been regenerated by running:
  ./tools/net/ynl/ynl-regen.sh -f

This changes the policy types of the following attributes:

  FOU_ATTR_PORT (NLA_U16 -> NLA_BE16)
  FOU_ATTR_PEER_PORT (NLA_U16 -> NLA_BE16)
    These two are used with nla_get_be16/nla_put_be16().

  MPTCP_PM_ADDR_ATTR_ADDR4 (NLA_U32 -> NLA_BE32)
    This one is used with nla_get_in_addr/nla_put_in_addr(),
    which uses nla_get_be32/nla_put_be32().

IOWs the generated changes are AFAICT aligned with their implementations.

The generated userspace code remains identical, and have been verified
by comparing the output generated by the following command:
  make -C tools/net/ynl/generated

Signed-off-by: Asbjørn Sloth Tønnesen <ast@fiberby.net>
---
 net/ipv4/fou_nl.c          | 4 ++--
 net/mptcp/mptcp_pm_gen.c   | 2 +-
 tools/net/ynl/ynl-gen-c.py | 6 +++++-
 3 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/net/ipv4/fou_nl.c b/net/ipv4/fou_nl.c
index 98b90107b5abc..3d9614609b2d3 100644
--- a/net/ipv4/fou_nl.c
+++ b/net/ipv4/fou_nl.c
@@ -12,7 +12,7 @@
 
 /* Global operation policy for fou */
 const struct nla_policy fou_nl_policy[FOU_ATTR_IFINDEX + 1] = {
-	[FOU_ATTR_PORT] = { .type = NLA_U16, },
+	[FOU_ATTR_PORT] = { .type = NLA_BE16, },
 	[FOU_ATTR_AF] = { .type = NLA_U8, },
 	[FOU_ATTR_IPPROTO] = { .type = NLA_U8, },
 	[FOU_ATTR_TYPE] = { .type = NLA_U8, },
@@ -21,7 +21,7 @@ const struct nla_policy fou_nl_policy[FOU_ATTR_IFINDEX + 1] = {
 	[FOU_ATTR_LOCAL_V6] = { .len = 16, },
 	[FOU_ATTR_PEER_V4] = { .type = NLA_U32, },
 	[FOU_ATTR_PEER_V6] = { .len = 16, },
-	[FOU_ATTR_PEER_PORT] = { .type = NLA_U16, },
+	[FOU_ATTR_PEER_PORT] = { .type = NLA_BE16, },
 	[FOU_ATTR_IFINDEX] = { .type = NLA_S32, },
 };
 
diff --git a/net/mptcp/mptcp_pm_gen.c b/net/mptcp/mptcp_pm_gen.c
index c30a2a90a1925..5a6b2b4510d37 100644
--- a/net/mptcp/mptcp_pm_gen.c
+++ b/net/mptcp/mptcp_pm_gen.c
@@ -14,7 +14,7 @@
 const struct nla_policy mptcp_pm_address_nl_policy[MPTCP_PM_ADDR_ATTR_IF_IDX + 1] = {
 	[MPTCP_PM_ADDR_ATTR_FAMILY] = { .type = NLA_U16, },
 	[MPTCP_PM_ADDR_ATTR_ID] = { .type = NLA_U8, },
-	[MPTCP_PM_ADDR_ATTR_ADDR4] = { .type = NLA_U32, },
+	[MPTCP_PM_ADDR_ATTR_ADDR4] = { .type = NLA_BE32, },
 	[MPTCP_PM_ADDR_ATTR_ADDR6] = NLA_POLICY_EXACT_LEN(16),
 	[MPTCP_PM_ADDR_ATTR_PORT] = { .type = NLA_U16, },
 	[MPTCP_PM_ADDR_ATTR_FLAGS] = { .type = NLA_U32, },
diff --git a/tools/net/ynl/ynl-gen-c.py b/tools/net/ynl/ynl-gen-c.py
index 717530bc9c52e..e26f2c3c40891 100755
--- a/tools/net/ynl/ynl-gen-c.py
+++ b/tools/net/ynl/ynl-gen-c.py
@@ -48,6 +48,7 @@ class Type(SpecAttr):
         self.attr = attr
         self.attr_set = attr_set
         self.type = attr['type']
+        self.nla_type = self.type
         self.checks = attr.get('checks', {})
 
         self.request = False
@@ -157,7 +158,7 @@ class Type(SpecAttr):
         return '{ .type = ' + policy + ', }'
 
     def attr_policy(self, cw):
-        policy = c_upper('nla-' + self.attr['type'])
+        policy = c_upper('nla-' + self.nla_type)
 
         spec = self._attr_policy(policy)
         cw.p(f"\t[{self.enum_name}] = {spec},")
@@ -300,6 +301,9 @@ class TypeScalar(Type):
         self.byte_order_comment = ''
         if 'byte-order' in attr:
             self.byte_order_comment = f" /* {attr['byte-order']} */"
+            if self.attr['byte-order'] == 'big-endian':
+                if self.type in {'u16', 'u32'}:
+                    self.nla_type = f'be{self.type[1:]}'
 
         if 'enum' in self.attr:
             enum = self.family.consts[self.attr['enum']]
-- 
2.45.2

Re: [PATCH net-next] tools: ynl-gen: use big-endian netlink attribute types
Posted by Jakub Kicinski 2 months, 2 weeks ago
Nice improvement! Since it technically missed net-next closing by a few
hours, let me nit pick a little..

On Fri, 13 Sep 2024 08:55:54 +0000 Asbjørn Sloth Tønnesen wrote:
> diff --git a/tools/net/ynl/ynl-gen-c.py b/tools/net/ynl/ynl-gen-c.py
> index 717530bc9c52e..e26f2c3c40891 100755
> --- a/tools/net/ynl/ynl-gen-c.py
> +++ b/tools/net/ynl/ynl-gen-c.py
> @@ -48,6 +48,7 @@ class Type(SpecAttr):
>          self.attr = attr
>          self.attr_set = attr_set
>          self.type = attr['type']
> +        self.nla_type = self.type

is it worth introducing nla_type as Type attribute just for one user?
inside a netlink code generator meaning of nla_type may not be crystal
clear

>          self.checks = attr.get('checks', {})
>  
>          self.request = False
> @@ -157,7 +158,7 @@ class Type(SpecAttr):
>          return '{ .type = ' + policy + ', }'
>  
>      def attr_policy(self, cw):
> -        policy = c_upper('nla-' + self.attr['type'])
> +        policy = c_upper('nla-' + self.nla_type)

We could just swap the type directly here?
-- 
pw-bot: defer
Re: [PATCH net-next] tools: ynl-gen: use big-endian netlink attribute types
Posted by Asbjørn Sloth Tønnesen 2 months, 2 weeks ago
Hi Kuba,

On 9/14/24 4:39 AM, Jakub Kicinski wrote:
> Nice improvement! Since it technically missed net-next closing by a few
> hours, let me nit pick a little..

Yeah, sorry about that, first realized that net-next had closed after posting.
I had just waited for my net patch to make its way to net-next before posting, so
MPTCP_PM_ADDR_ATTR_PORT wouldn't change to NLA_BE16.

> On Fri, 13 Sep 2024 08:55:54 +0000 Asbjørn Sloth Tønnesen wrote:
>> diff --git a/tools/net/ynl/ynl-gen-c.py b/tools/net/ynl/ynl-gen-c.py
>> index 717530bc9c52e..e26f2c3c40891 100755
>> --- a/tools/net/ynl/ynl-gen-c.py
>> +++ b/tools/net/ynl/ynl-gen-c.py
>> @@ -48,6 +48,7 @@ class Type(SpecAttr):
>>           self.attr = attr
>>           self.attr_set = attr_set
>>           self.type = attr['type']
>> +        self.nla_type = self.type
> 
> is it worth introducing nla_type as Type attribute just for one user?
> inside a netlink code generator meaning of nla_type may not be crystal
> clear

Maybe not, I just took the same approach as byte_order_comment, and
co-located it with the existing byte-order condition in TypeScalar.

>>           self.checks = attr.get('checks', {})
>>   
>>           self.request = False
>> @@ -157,7 +158,7 @@ class Type(SpecAttr):
>>           return '{ .type = ' + policy + ', }'
>>   
>>       def attr_policy(self, cw):
>> -        policy = c_upper('nla-' + self.attr['type'])
>> +        policy = c_upper('nla-' + self.nla_type)
> 
> We could just swap the type directly here?

That could work too, WDYT?

diff --git a/tools/net/ynl/ynl-gen-c.py b/tools/net/ynl/ynl-gen-c.py
index 717530bc9c52e..e8706f36e5e7b 100755
--- a/tools/net/ynl/ynl-gen-c.py
+++ b/tools/net/ynl/ynl-gen-c.py
@@ -157,7 +157,10 @@ class Type(SpecAttr):
          return '{ .type = ' + policy + ', }'

      def attr_policy(self, cw):
-        policy = c_upper('nla-' + self.attr['type'])
+        policy = f'NLA_{c_upper(self.type)}'
+        if self.attr.get('byte-order', '') == 'big-endian':
+            if self.type in {'u16', 'u32'}:
+                policy = f'NLA_BE{self.type[1:]}'

          spec = self._attr_policy(policy)
          cw.p(f"\t[{self.enum_name}] = {spec},")

Re: [PATCH net-next] tools: ynl-gen: use big-endian netlink attribute types
Posted by Jakub Kicinski 2 months, 2 weeks ago
On Sat, 14 Sep 2024 19:03:38 +0000 Asbjørn Sloth Tønnesen wrote:
> > We could just swap the type directly here?  
> 
> That could work too, WDYT?
> 
> diff --git a/tools/net/ynl/ynl-gen-c.py b/tools/net/ynl/ynl-gen-c.py
> index 717530bc9c52e..e8706f36e5e7b 100755
> --- a/tools/net/ynl/ynl-gen-c.py
> +++ b/tools/net/ynl/ynl-gen-c.py
> @@ -157,7 +157,10 @@ class Type(SpecAttr):
>           return '{ .type = ' + policy + ', }'
> 
>       def attr_policy(self, cw):
> -        policy = c_upper('nla-' + self.attr['type'])
> +        policy = f'NLA_{c_upper(self.type)}'
> +        if self.attr.get('byte-order', '') == 'big-endian':
> +            if self.type in {'u16', 'u32'}:
> +                policy = f'NLA_BE{self.type[1:]}'

LGTM!