Skip to contentSkip to conversation

tools: ynl-gen: support using pre-defined values in attr checks

PATCH Superseded Hangbin Liu 2 participants 2 messages Mar 7, 2024 – Mar 8, 2024

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 tools: ynl-gen: support using pre-defined values in attr checks

Support using pre-defined values in checks so we don't need to use hard code number for the string, binary length. e.g. we have a definition like

#define TEAM_STRING_MAX_LEN 32

Which defined in yaml like:

definitions:
-

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

It can be used in the attribute-sets like

attribute-sets:
-

    name: attr-option
    name-prefix: team-attr-option-
    attributes:
      -
        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, }

---
v2: Update the commit description. Drop other controversial patches. v1 link: lore.kernel.org/netdev/[email protected]

---
 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 c58f7153fcf8..8c2363ddee03 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 938703088306..75ed5c773a0c 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 3283bf458ff1..d7edb8855563 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 ac4e05415f2f..4917beeb046f 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 2f5febfe66a1..745eb48e8628 100755
@@ -77,9 +77,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
maintainer
#1 lore raw
Quoted 1 line · On Thu, 7 Mar 2024 17:23:57 +0800 Hangbin Liu wrote:

+ for const in self.family['definitions']:

Looks like this breaks the build, the 'definitions' attribute is optional. Try iterating over self.family.consts instead. It should be initialized by nlspec.py to either the definitions or an empty list, always.

Signature · 1 line
pw-bot: cr