diff mbox series

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

Message ID 20231215035009.498049-3-liuhangbin@gmail.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series ynl-gen: update check format | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for net-next
netdev/apply fail Patch does not apply to net-next

Commit Message

Hangbin Liu Dec. 15, 2023, 3:50 a.m. UTC
Support using defines in checks so we don't need to use hard code
number for the string, binary length.

Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
---
 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(-)

Comments

Jakub Kicinski Dec. 16, 2023, 2:08 a.m. UTC | #1
On Fri, 15 Dec 2023 11:50:08 +0800 Hangbin Liu wrote:
> -    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?
Hangbin Liu Dec. 16, 2023, 8:44 a.m. UTC | #2
On Fri, Dec 15, 2023 at 06:08:24PM -0800, Jakub Kicinski wrote:
> On Fri, 15 Dec 2023 11:50:08 +0800 Hangbin Liu wrote:
> > -    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
Jakub Kicinski Dec. 18, 2023, 10:23 p.m. UTC | #3
On Sat, 16 Dec 2023 16:44:30 +0800 Hangbin Liu wrote:
> On Fri, Dec 15, 2023 at 06:08:24PM -0800, Jakub Kicinski wrote:
> > On Fri, 15 Dec 2023 11:50:08 +0800 Hangbin Liu wrote:  
> > > -    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, }

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

Reviewed-by: Jakub Kicinski <kuba@kernel.org>
diff mbox series

Patch

diff --git a/Documentation/netlink/genetlink-c.yaml b/Documentation/netlink/genetlink-c.yaml
index 66083cdbf43e..82a891e6ed68 100644
--- a/Documentation/netlink/genetlink-c.yaml
+++ b/Documentation/netlink/genetlink-c.yaml
@@ -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.
diff --git a/Documentation/netlink/genetlink-legacy.yaml b/Documentation/netlink/genetlink-legacy.yaml
index 9a9ab7468469..28317b1818ad 100644
--- a/Documentation/netlink/genetlink-legacy.yaml
+++ b/Documentation/netlink/genetlink-legacy.yaml
@@ -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.
diff --git a/Documentation/netlink/genetlink.yaml b/Documentation/netlink/genetlink.yaml
index 9be071a622cf..813cd4eb47df 100644
--- a/Documentation/netlink/genetlink.yaml
+++ b/Documentation/netlink/genetlink.yaml
@@ -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.
diff --git a/Documentation/netlink/netlink-raw.yaml b/Documentation/netlink/netlink-raw.yaml
index 2c393b234511..d59670743d10 100644
--- a/Documentation/netlink/netlink-raw.yaml
+++ b/Documentation/netlink/netlink-raw.yaml
@@ -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
diff --git a/tools/net/ynl/ynl-gen-c.py b/tools/net/ynl/ynl-gen-c.py
index 88a2048d668d..fba65ba2c716 100755
--- a/tools/net/ynl/ynl-gen-c.py
+++ b/tools/net/ynl/ynl-gen-c.py
@@ -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