Skip to contentSkip to conversation

ynl-gen: update check format

PATCH In review Hangbin Liu 3 participants 15 messages Dec 15, 2023 – Dec 19, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover ynl-gen: update check format

Hi Jakub,

Looks support defines in len check is not as complex as I thought :)

The first patch update the len checks. Add len part, and correct the usage of min, max, exact len.
The second patch add the support of using defines instead of hard code only. The third patch fix the IPv6 addr len definition.

BTW, I'm not sure whether the 1st and 3rd patches should be fixes or not. Please tell me if I should update.

Hangbin Liu (3):
tools: ynl-gen: use correct len for string and binary
tools: ynl-gen: support using defines in checks
netlink: specs: use exact-len for IPv6 addr

 Documentation/netlink/genetlink-c.yaml      |  9 ++++--
 Documentation/netlink/genetlink-legacy.yaml |  9 ++++--
 Documentation/netlink/genetlink.yaml        |  9 ++++--
 Documentation/netlink/netlink-raw.yaml      |  9 ++++--
 Documentation/netlink/specs/fou.yaml        |  4 +--
 Documentation/netlink/specs/mptcp.yaml      |  4 +--
 include/net/netlink.h                       |  1 +
 tools/net/ynl/ynl-gen-c.py                  | 32 ++++++++++++---------
 8 files changed, 48 insertions(+), 29 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/3 tools: ynl-gen: use correct len for string and binary

As the description of 'struct nla_policy', the len means the maximum length of string, binary. Or minimum length of attribute payload for others. But most time we only use it for string and binary.

On the other hand, the NLA_POLICY_{EXACT_LEN, MIN_LEN, MAX_LEN} should only be used for binary only.

---
 Documentation/netlink/genetlink-c.yaml      |  7 ++++--
 Documentation/netlink/genetlink-legacy.yaml |  7 ++++--
 Documentation/netlink/genetlink.yaml        |  7 ++++--
 Documentation/netlink/netlink-raw.yaml      |  7 ++++--
 include/net/netlink.h                       |  1 +
 tools/net/ynl/ynl-gen-c.py                  | 25 ++++++++++-----------
 6 files changed, 33 insertions(+), 21 deletions(-)
Documentation/netlink/genetlink-c.yaml +5 -2
index c58f7153fcf8..66083cdbf43e 100644
@@ -199,14 +199,17 @@ properties:
                   max:
                     description: Max value for an integer attribute.
                     $ref: '#/$defs/len-or-limit'
+                  len:
+                    description: Max length for a string or a binary attribute.
+                    $ref: '#/$defs/len-or-define'
                   min-len:
                     description: Min length for a binary attribute.
                     $ref: '#/$defs/len-or-define'
                   max-len:
-                    description: Max length for a string or a binary attribute.
+                    description: Max length for a binary attribute.
                     $ref: '#/$defs/len-or-define'
                   exact-len:
-                    description: Exact length for a string or a binary attribute.
+                    description: Exact length for a binary attribute.
                     $ref: '#/$defs/len-or-define'
               sub-type: *attr-type
               display-hint: &display-hint
Documentation/netlink/genetlink-legacy.yaml +5 -2
index 938703088306..9a9ab7468469 100644
@@ -242,14 +242,17 @@ properties:
                   max:
                     description: Max value for an integer attribute.
                     $ref: '#/$defs/len-or-limit'
+                  len:
+                    description: Max length for a string or a binary attribute.
+                    $ref: '#/$defs/len-or-define'
                   min-len:
                     description: Min length for a binary attribute.
                     $ref: '#/$defs/len-or-define'
                   max-len:
-                    description: Max length for a string or a binary attribute.
+                    description: Max length for a binary attribute.
                     $ref: '#/$defs/len-or-define'
                   exact-len:
-                    description: Exact length for a string or a binary attribute.
+                    description: Exact length for a binary attribute.
                     $ref: '#/$defs/len-or-define'
               sub-type: *attr-type
               display-hint: *display-hint
Documentation/netlink/genetlink.yaml +5 -2
index 3283bf458ff1..9be071a622cf 100644
@@ -166,14 +166,17 @@ properties:
                   max:
                     description: Max value for an integer attribute.
                     $ref: '#/$defs/len-or-limit'
+                  len:
+                    description: Max length for a string or a binary attribute.
+                    $ref: '#/$defs/len-or-define'
                   min-len:
                     description: Min length for a binary attribute.
                     $ref: '#/$defs/len-or-define'
                   max-len:
-                    description: Max length for a string or a binary attribute.
+                    description: Max length for a binary attribute.
                     $ref: '#/$defs/len-or-define'
                   exact-len:
-                    description: Exact length for a string or a binary attribute.
+                    description: Exact length for a binary attribute.
                     $ref: '#/$defs/len-or-define'
               sub-type: *attr-type
               display-hint: &display-hint
Documentation/netlink/netlink-raw.yaml +5 -2
index ad5395040765..2c393b234511 100644
@@ -241,14 +241,17 @@ properties:
                   min:
                     description: Min value for an integer attribute.
                     type: integer
+                  len:
+                    description: Max length for a string or a binary attribute.
+                    $ref: '#/$defs/len-or-define'
                   min-len:
                     description: Min length for a binary attribute.
                     $ref: '#/$defs/len-or-define'
                   max-len:
-                    description: Max length for a string or a binary attribute.
+                    description: Max length for a binary attribute.
                     $ref: '#/$defs/len-or-define'
                   exact-len:
-                    description: Exact length for a string or a binary attribute.
+                    description: Exact length for a binary attribute.
                     $ref: '#/$defs/len-or-define'
               sub-type: *attr-type
               display-hint: *display-hint
include/net/netlink.h +1 -0
index 28039e57070a..e7f6e22282d3 100644
@@ -464,6 +464,7 @@ struct nla_policy {
 	.max = _len						\
 }
 #define NLA_POLICY_MIN_LEN(_len)	NLA_POLICY_MIN(NLA_BINARY, _len)
+#define NLA_POLICY_MAX_LEN(_len)	NLA_POLICY_MAX(NLA_BINARY, _len)
 
 /**
  * struct nl_info - netlink source information
tools/net/ynl/ynl-gen-c.py +12 -13
index 7fc1aa788f6f..88a2048d668d 100755
@@ -427,13 +427,10 @@ class TypeString(Type):
         return f'.type = YNL_PT_NUL_STR, '
 
     def _attr_policy(self, policy):
-        if 'exact-len' in self.checks:
-            mem = 'NLA_POLICY_EXACT_LEN(' + str(self.checks['exact-len']) + ')'
-        else:
-            mem = '{ .type = ' + policy
-            if 'max-len' in self.checks:
-                mem += ', .len = ' + str(self.get_limit('max-len'))
-            mem += ', }'
+        mem = '{ .type = ' + policy
+        if 'len' in self.checks:
+            mem += ', .len = ' + str(self.get_limit('len'))
+        mem += ', }'
         return mem
 
     def attr_policy(self, cw):
@@ -481,13 +478,15 @@ class TypeBinary(Type):
     def _attr_policy(self, policy):
         if 'exact-len' in self.checks:
             mem = 'NLA_POLICY_EXACT_LEN(' + str(self.checks['exact-len']) + ')'
+        elif 'max-len' in self.checks:
+            mem = 'NLA_POLICY_MAX_LEN(' + str(self.checks['max-len']) + ')'
+        elif 'min-len' in self.checks:
+            mem = 'NLA_POLICY_MIN_LEN(' + str(self.checks['min-len']) + ')'
         else:
-            mem = '{ '
-            if len(self.checks) == 1 and 'min-len' in self.checks:
-                mem += '.len = ' + str(self.get_limit('min-len'))
-            elif len(self.checks) == 0:
-                mem += '.type = NLA_BINARY'
-            else:
+            mem = '{ .type = ' + policy
+            if len(self.checks) == 1 and 'len' in self.checks:
+                mem += '.len = ' + str(self.get_limit('len'))
+            elif len(self.checks) > 1:
                 raise Exception('One or more of binary type checks not implemented, yet')
             mem += ', }'
         return mem
Signature · 1 line
2.43.0
#6 lore raw

PATCH 2/3 tools: ynl-gen: support using defines in checks

In reply to Hangbin Liu

Support using defines in checks so we don't need to use hard code number for the string, binary length.

---
 Documentation/netlink/genetlink-c.yaml      | 2 +-
 Documentation/netlink/genetlink-legacy.yaml | 2 +-
 Documentation/netlink/genetlink.yaml        | 2 +-
 Documentation/netlink/netlink-raw.yaml      | 2 +-
 tools/net/ynl/ynl-gen-c.py                  | 7 +++++++
 5 files changed, 11 insertions(+), 4 deletions(-)
Documentation/netlink/genetlink-c.yaml +1 -1
index 66083cdbf43e..82a891e6ed68 100644
@@ -11,7 +11,7 @@ $defs:
     minimum: 0
   len-or-define:
     type: [ string, integer ]
-    pattern: ^[0-9A-Za-z_]+( - 1)?$
+    pattern: ^[0-9A-Za-z_-]+( - 1)?$
     minimum: 0
   len-or-limit:
     # literal int or limit based on fixed-width type e.g. u8-min, u16-max, etc.
Documentation/netlink/genetlink-legacy.yaml +1 -1
index 9a9ab7468469..28317b1818ad 100644
@@ -11,7 +11,7 @@ $defs:
     minimum: 0
   len-or-define:
     type: [ string, integer ]
-    pattern: ^[0-9A-Za-z_]+( - 1)?$
+    pattern: ^[0-9A-Za-z_-]+( - 1)?$
     minimum: 0
   len-or-limit:
     # literal int or limit based on fixed-width type e.g. u8-min, u16-max, etc.
Documentation/netlink/genetlink.yaml +1 -1
index 9be071a622cf..813cd4eb47df 100644
@@ -11,7 +11,7 @@ $defs:
     minimum: 0
   len-or-define:
     type: [ string, integer ]
-    pattern: ^[0-9A-Za-z_]+( - 1)?$
+    pattern: ^[0-9A-Za-z_-]+( - 1)?$
     minimum: 0
   len-or-limit:
     # literal int or limit based on fixed-width type e.g. u8-min, u16-max, etc.
Documentation/netlink/netlink-raw.yaml +1 -1
index 2c393b234511..d59670743d10 100644
@@ -11,7 +11,7 @@ $defs:
     minimum: 0
   len-or-define:
     type: [ string, integer ]
-    pattern: ^[0-9A-Za-z_]+( - 1)?$
+    pattern: ^[0-9A-Za-z_-]+( - 1)?$
     minimum: 0
 
 # Schema for specs
tools/net/ynl/ynl-gen-c.py +7 -0
index 88a2048d668d..fba65ba2c716 100755
@@ -85,9 +85,16 @@ class Type(SpecAttr):
         delattr(self, "enum_name")
 
     def get_limit(self, limit, default=None):
+        defines = []
+        for const in self.family['definitions']:
+            if const['type'] == 'const':
+                defines.append(const['name'])
+
         value = self.checks.get(limit, default)
         if value is None:
             return value
+        elif value in defines:
+            return c_upper(f"{self.family['name']}-{value}")
         if not isinstance(value, int):
             value = limit_to_number(value)
         return value
Signature · 1 line
2.43.0
#10 lore raw

PATCH 3/3 netlink: specs: use exact-len for IPv6 addr

In reply to Hangbin Liu

We should use the exact-len instead of min-len for IPv6 address.

---
 Documentation/netlink/specs/fou.yaml   | 4 ++--
 Documentation/netlink/specs/mptcp.yaml | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
Documentation/netlink/specs/fou.yaml +2 -2
index 0af5ab842c04..d472fd5055bd 100644
@@ -52,7 +52,7 @@ attribute-sets:
         name: local_v6
         type: binary
         checks:
-          min-len: 16
+          exact-len: 16
       -
         name: peer_v4
         type: u32
@@ -60,7 +60,7 @@ attribute-sets:
         name: peer_v6
         type: binary
         checks:
-          min-len: 16
+          exact-len: 16
       -
         name: peer_port
         type: u16
Documentation/netlink/specs/mptcp.yaml +2 -2
index 49f90cfb4698..2f694b79c3a7 100644
@@ -223,7 +223,7 @@ attribute-sets:
         name: saddr6
         type: binary
         checks:
-          min-len: 16
+          exact-len: 16
       -
         name: daddr4
         type: u32
@@ -232,7 +232,7 @@ attribute-sets:
         name: daddr6
         type: binary
         checks:
-          min-len: 16
+          exact-len: 16
       -
         name: sport
         type: u16
Signature · 1 line
2.43.0
maintainer
#2 lore raw

Re: [PATCH net-next 1/3] tools: ynl-gen: use correct len for string and binary

In reply to Hangbin Liu
Quoted 3 lines · On Fri, 15 Dec 2023 11:50:07 +0800 Hangbin Liu wrote:

As the description of 'struct nla_policy', the len means the maximum length of string, binary. Or minimum length of attribute payload for others. But most time we only use it for string and binary.

The meaning of 'len' in nla_policy is confusing to people writing new families. IIRC I used max-len / min-len / extact-len and not len on purpose in the YAML, so that there's no confusion what means what for which type...

Obviously it is slightly confusing for people like you who convert the existing families to YAML specs, but the risk of bugs seems lower there. So I'd prefer to stick to the existing set of options.

Is the existing code gen incorrect or just hard to wrap one's head around?

maintainer
#7 lore raw

Re: [PATCH net-next 2/3] tools: ynl-gen: support using defines in checks

In reply to Hangbin Liu

Review

Quoted 2 lines · On Fri, 15 Dec 2023 11:50:08 +0800 Hangbin Liu wrote:
(quoted diff) +1 -1
-    pattern: ^[0-9A-Za-z_]+( - 1)?$
+    pattern: ^[0-9A-Za-z_-]+( - 1)?$

Why the '-' ? Could you add an example of the define you're trying to match to the commit message?

maintainer
#11 lore raw

Re: [PATCH net-next 3/3] netlink: specs: use exact-len for IPv6 addr

In reply to Hangbin Liu
Quoted 1 line · On Fri, 15 Dec 2023 11:50:09 +0800 Hangbin Liu wrote:

We should use the exact-len instead of min-len for IPv6 address.

It does make sense, but these families historically used min-len.. Not sure if it's worth changing this now or we risk regressions.

#3 lore raw

Re: [PATCH net-next 1/3] tools: ynl-gen: use correct len for string and binary

In reply to Jakub Kicinski
Quoted 10 lines · Jakub Kicinski
Quoted 3 lines · On Fri, 15 Dec 2023 11:50:07 +0800 Hangbin Liu wrote:

As the description of 'struct nla_policy', the len means the maximum length of string, binary. Or minimum length of attribute payload for others. But most time we only use it for string and binary.

The meaning of 'len' in nla_policy is confusing to people writing new families. IIRC I used max-len / min-len / extact-len and not len on purpose in the YAML, so that there's no confusion what means what for which type...

Quoted 7 lines

Obviously it is slightly confusing for people like you who convert the existing families to YAML specs, but the risk of bugs seems lower there. So I'd prefer to stick to the existing set of options.

Is the existing code gen incorrect or just hard to wrap one's head around?

The max-len / min-len / extact-len micro are used by binary. For string we need to use "len" to define the max length. e.g.

static const struct nla_policy team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
[TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
[TEAM_ATTR_OPTION_NAME] = {
.type = NLA_STRING,
.len = TEAM_STRING_MAX_LEN,
},

Thanks
Hangbin

#8 lore raw

Re: [PATCH net-next 2/3] tools: ynl-gen: support using defines in checks

In reply to Jakub Kicinski
Quoted 6 lines · Jakub Kicinski

Review

Quoted 2 lines · On Fri, 15 Dec 2023 11:50:08 +0800 Hangbin Liu wrote:
(quoted diff) +1 -1
-    pattern: ^[0-9A-Za-z_]+( - 1)?$
+    pattern: ^[0-9A-Za-z_-]+( - 1)?$

Why the '-' ? Could you add an example of the define you're trying to match to the commit message?

For team driver, there is a define like:

#define TEAM_STRING_MAX_LEN 32

So I'd like to define it in yaml like:

definitions:
-

    name: string-max-len
    type: const
    value: 32

And use it in the attribute-sets like

attribute-sets:
-

    name: attr-option
    name-prefix: team-attr-option-
    attributes:
      -
        name: unspec
        type: unused
        value: 0
      -
        name: name
        type: string
        checks:
          len: string-max-len

With this patch it will be converted to

[TEAM_ATTR_OPTION_NAME] = { .type = NLA_STRING, .len = TEAM_STRING_MAX_LEN, }

Thanks
Hangbin

#12 lore raw

Re: [PATCH net-next 3/3] netlink: specs: use exact-len for IPv6 addr

In reply to Jakub Kicinski
Quoted 5 lines · Jakub Kicinski
Quoted 1 line · On Fri, 15 Dec 2023 11:50:09 +0800 Hangbin Liu wrote:

We should use the exact-len instead of min-len for IPv6 address.

It does make sense, but these families historically used min-len.. Not sure if it's worth changing this now or we risk regressions.

The addr6 in mptcp.yaml also use exact-len. I don't think the IPv6 address could be larger than 16 bytes. So the min-len check looks incorrect.

Thanks
Hangbin

Acked-by on v1 3/3
#14 lore raw

Re: [PATCH net-next 3/3] netlink: specs: use exact-len for IPv6 addr

In reply to Hangbin Liu

hello Hangbin!

for the mptcp part: I didn't convert mptcp events, here transition from min-len to exact-len should not generate issues at all.

Quoted 57 lines · Hangbin Liu

We should use the exact-len instead of min-len for IPv6 address.

---
 Documentation/netlink/specs/fou.yaml   | 4 ++--
 Documentation/netlink/specs/mptcp.yaml | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
Documentation/netlink/specs/fou.yaml +2 -2
index 0af5ab842c04..d472fd5055bd 100644
@@ -52,7 +52,7 @@ attribute-sets:
         name: local_v6
         type: binary
         checks:
-          min-len: 16
+          exact-len: 16
       -
         name: peer_v4
         type: u32
@@ -60,7 +60,7 @@ attribute-sets:
         name: peer_v6
         type: binary
         checks:
-          min-len: 16
+          exact-len: 16
       -
         name: peer_port
         type: u16
Documentation/netlink/specs/mptcp.yaml +2 -3
index 49f90cfb4698..2f694b79c3a7 100644
@@ -223,7 +223,7 @@ attribute-sets:
         name: saddr6
         type: binary
         checks:
-          min-len: 16
+          exact-len: 16
       -
         name: daddr4
         type: u32
@@ -232,7 +232,7 @@ attribute-sets:
         name: daddr6
         type: binary
         checks:
-          min-len: 16
+          exact-len: 16
       -
         name: sport
         type: u16
--

2.43.0

maintainer
#4 lore raw

Re: [PATCH net-next 1/3] tools: ynl-gen: use correct len for string and binary

In reply to Hangbin Liu
Quoted 10 lines · On Sat, 16 Dec 2023 16:35:40 +0800 Hangbin Liu wrote:

The max-len / min-len / extact-len micro are used by binary. For string we need to use "len" to define the max length. e.g.

static const struct nla_policy team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
[TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
[TEAM_ATTR_OPTION_NAME] = {
.type = NLA_STRING,
.len = TEAM_STRING_MAX_LEN,
},

max-len / min-len / extact-len are just the names in the spec. We can put the value provided in the spec as max-len inside nla_policy as len, given that for string spec::max-len == policy::len

Am I confused?

maintainer Reviewed-by
#9 lore raw

Re: [PATCH net-next 2/3] tools: ynl-gen: support using defines in checks

In reply to Hangbin Liu
Quoted 36 lines · On Sat, 16 Dec 2023 16:44:30 +0800 Hangbin Liu wrote:
Quoted 6 lines · Jakub Kicinski

Review

Quoted 2 lines · On Fri, 15 Dec 2023 11:50:08 +0800 Hangbin Liu wrote:
(quoted diff) +1 -1
-    pattern: ^[0-9A-Za-z_]+( - 1)?$
+    pattern: ^[0-9A-Za-z_-]+( - 1)?$  

Why the '-' ? Could you add an example of the define you're trying to match to the commit message?

For team driver, there is a define like:

#define TEAM_STRING_MAX_LEN 32

So I'd like to define it in yaml like:

definitions:
-

    name: string-max-len
    type: const
    value: 32

And use it in the attribute-sets like

attribute-sets:
-

    name: attr-option
    name-prefix: team-attr-option-
    attributes:
      -
        name: unspec
        type: unused
        value: 0
      -
        name: name
        type: string
        checks:
          len: string-max-len
Quoted 3 lines

With this patch it will be converted to

[TEAM_ATTR_OPTION_NAME] = { .type = NLA_STRING, .len = TEAM_STRING_MAX_LEN, }

Ah, I see. The spec match needs to work on names before they go thru c_upper(). The patch makes sense:

maintainer
#13 lore raw

Re: [PATCH net-next 3/3] netlink: specs: use exact-len for IPv6 addr

In reply to Hangbin Liu
Quoted 9 lines · On Sat, 16 Dec 2023 16:48:01 +0800 Hangbin Liu wrote:
Quoted 5 lines · Jakub Kicinski
Quoted 1 line · On Fri, 15 Dec 2023 11:50:09 +0800 Hangbin Liu wrote:

We should use the exact-len instead of min-len for IPv6 address.

It does make sense, but these families historically used min-len.. Not sure if it's worth changing this now or we risk regressions.

The addr6 in mptcp.yaml also use exact-len. I don't think the IPv6 address could be larger than 16 bytes. So the min-len check looks incorrect.

I understand, what I'm saying is that the nla_policy before we started using specs was buggy, so we kept the bugginess in the conversion. But it should be fine, so we can change it now and hope for the best..

#5 lore raw

Re: [PATCH net-next 1/3] tools: ynl-gen: use correct len for string and binary

In reply to Jakub Kicinski
Quoted 12 lines · Jakub Kicinski
Quoted 10 lines · On Sat, 16 Dec 2023 16:35:40 +0800 Hangbin Liu wrote:

The max-len / min-len / extact-len micro are used by binary. For string we need to use "len" to define the max length. e.g.

static const struct nla_policy team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
[TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
[TEAM_ATTR_OPTION_NAME] = {
.type = NLA_STRING,
.len = TEAM_STRING_MAX_LEN,
},

Quoted 5 lines

max-len / min-len / extact-len are just the names in the spec. We can put the value provided in the spec as max-len inside nla_policy as len, given that for string spec::max-len == policy::len

Am I confused?

Yes, we can do that. While this looks like another magic. When user set max-len for a string type in the yaml spec. After converting to c code, it's .len attribute. This still makes user confused.

Anyway, this is just a matter of choosing apple or banana. I'm OK to using the current YAML spec policy.

Thanks
Hangbin