tools: ynl-gen: use names of constants in generated limits

YNL specs can use string expressions for limits, like s32-min
or u16-max. We convert all of those into their numeric values
when generating the code, which isn't always helpful. Try to
retain the string representations in the output. Any sort of
calculations still need the integers.

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Reviewed-by: Joe Damato <jdamato@fastly.com>
Link: https://patch.msgid.link/20241010151248.2049755-1-kuba@kernel.org
[pabeni@redhat.com: regenerated netdev-genl-gen.c]
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
This commit is contained in:
Jakub Kicinski
2024-10-10 08:12:48 -07:00
committed by Paolo Abeni
parent 97802ffca7
commit bcbbfaa261
2 changed files with 26 additions and 16 deletions
+3 -3
View File
@@ -14,16 +14,16 @@
/* Integer value ranges */ /* Integer value ranges */
static const struct netlink_range_validation netdev_a_page_pool_id_range = { static const struct netlink_range_validation netdev_a_page_pool_id_range = {
.min = 1ULL, .min = 1ULL,
.max = 4294967295ULL, .max = U32_MAX,
}; };
static const struct netlink_range_validation netdev_a_page_pool_ifindex_range = { static const struct netlink_range_validation netdev_a_page_pool_ifindex_range = {
.min = 1ULL, .min = 1ULL,
.max = 2147483647ULL, .max = S32_MAX,
}; };
static const struct netlink_range_validation netdev_a_napi_defer_hard_irqs_range = { static const struct netlink_range_validation netdev_a_napi_defer_hard_irqs_range = {
.max = 2147483647ULL, .max = S32_MAX,
}; };
/* Common nested types */ /* Common nested types */
+23 -13
View File
@@ -80,11 +80,21 @@ class Type(SpecAttr):
value = self.checks.get(limit, default) value = self.checks.get(limit, default)
if value is None: if value is None:
return value return value
elif value in self.family.consts: if isinstance(value, int):
return value
if value in self.family.consts:
raise Exception("Resolving family constants not implemented, yet")
return limit_to_number(value)
def get_limit_str(self, limit, default=None, suffix=''):
value = self.checks.get(limit, default)
if value is None:
return ''
if isinstance(value, int):
return str(value) + suffix
if value in self.family.consts:
return c_upper(f"{self.family['name']}-{value}") return c_upper(f"{self.family['name']}-{value}")
if not isinstance(value, int): return c_upper(value)
value = limit_to_number(value)
return value
def resolve(self): def resolve(self):
if 'name-prefix' in self.attr: if 'name-prefix' in self.attr:
@@ -358,11 +368,11 @@ class TypeScalar(Type):
elif 'full-range' in self.checks: elif 'full-range' in self.checks:
return f"NLA_POLICY_FULL_RANGE({policy}, &{c_lower(self.enum_name)}_range)" return f"NLA_POLICY_FULL_RANGE({policy}, &{c_lower(self.enum_name)}_range)"
elif 'range' in self.checks: elif 'range' in self.checks:
return f"NLA_POLICY_RANGE({policy}, {self.get_limit('min')}, {self.get_limit('max')})" return f"NLA_POLICY_RANGE({policy}, {self.get_limit_str('min')}, {self.get_limit_str('max')})"
elif 'min' in self.checks: elif 'min' in self.checks:
return f"NLA_POLICY_MIN({policy}, {self.get_limit('min')})" return f"NLA_POLICY_MIN({policy}, {self.get_limit_str('min')})"
elif 'max' in self.checks: elif 'max' in self.checks:
return f"NLA_POLICY_MAX({policy}, {self.get_limit('max')})" return f"NLA_POLICY_MAX({policy}, {self.get_limit_str('max')})"
return super()._attr_policy(policy) return super()._attr_policy(policy)
def _attr_typol(self): def _attr_typol(self):
@@ -413,11 +423,11 @@ class TypeString(Type):
def _attr_policy(self, policy): def _attr_policy(self, policy):
if 'exact-len' in self.checks: if 'exact-len' in self.checks:
mem = 'NLA_POLICY_EXACT_LEN(' + str(self.get_limit('exact-len')) + ')' mem = 'NLA_POLICY_EXACT_LEN(' + self.get_limit_str('exact-len') + ')'
else: else:
mem = '{ .type = ' + policy mem = '{ .type = ' + policy
if 'max-len' in self.checks: if 'max-len' in self.checks:
mem += ', .len = ' + str(self.get_limit('max-len')) mem += ', .len = ' + self.get_limit_str('max-len')
mem += ', }' mem += ', }'
return mem return mem
@@ -476,9 +486,9 @@ class TypeBinary(Type):
if len(self.checks) == 0: if len(self.checks) == 0:
mem = '{ .type = NLA_BINARY, }' mem = '{ .type = NLA_BINARY, }'
elif 'exact-len' in self.checks: elif 'exact-len' in self.checks:
mem = 'NLA_POLICY_EXACT_LEN(' + str(self.get_limit('exact-len')) + ')' mem = 'NLA_POLICY_EXACT_LEN(' + self.get_limit_str('exact-len') + ')'
elif 'min-len' in self.checks: elif 'min-len' in self.checks:
mem = '{ .len = ' + str(self.get_limit('min-len')) + ', }' mem = '{ .len = ' + self.get_limit_str('min-len') + ', }'
return mem return mem
@@ -2166,9 +2176,9 @@ def print_kernel_policy_ranges(family, cw):
cw.block_start(line=f'static const struct netlink_range_validation{sign} {c_lower(attr.enum_name)}_range =') cw.block_start(line=f'static const struct netlink_range_validation{sign} {c_lower(attr.enum_name)}_range =')
members = [] members = []
if 'min' in attr.checks: if 'min' in attr.checks:
members.append(('min', str(attr.get_limit('min')) + suffix)) members.append(('min', attr.get_limit_str('min', suffix=suffix)))
if 'max' in attr.checks: if 'max' in attr.checks:
members.append(('max', str(attr.get_limit('max')) + suffix)) members.append(('max', attr.get_limit_str('max', suffix=suffix)))
cw.write_struct_init(members) cw.write_struct_init(members)
cw.block_end(line=';') cw.block_end(line=';')
cw.nl() cw.nl()