[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAD4GDZzR7DV-z7HA7=r9tmXmgkQu30K5QE9nAdz2eZfvKPOusA@mail.gmail.com>
Date: Thu, 8 May 2025 12:33:10 +0100
From: Donald Hunter <donald.hunter@...il.com>
To: Jakub Kicinski <kuba@...nel.org>
Cc: davem@...emloft.net, netdev@...r.kernel.org, edumazet@...gle.com,
pabeni@...hat.com, andrew+netdev@...n.ch, horms@...nel.org,
jacob.e.keller@...el.com
Subject: Re: [PATCH net-next 1/3] tools: ynl-gen: support sub-type for binary attributes
On Thu, 8 May 2025 at 03:28, Jakub Kicinski <kuba@...nel.org> wrote:
>
> @@ -516,13 +516,21 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S
>
> class TypeBinary(Type):
> def arg_member(self, ri):
> + if self.get('sub-type') and self.get('sub-type') in scalars:
This check is repeated a lot, so maybe it would benefit from a
_has_scalar_sub_type() helper?
> + return [f'__{self.get("sub-type")} *{self.c_name}', 'size_t count']
> return [f"const void *{self.c_name}", 'size_t len']
>
> def presence_type(self):
> - return 'len'
> + if self.get('sub-type') and self.get('sub-type') in scalars:
> + return 'count'
> + else:
> + return 'len'
>
> def struct_member(self, ri):
> - ri.cw.p(f"void *{self.c_name};")
> + if self.get('sub-type') and self.get('sub-type') in scalars:
> + ri.cw.p(f'__{self.get("sub-type")} *{self.c_name};')
> + else:
> + ri.cw.p(f"void *{self.c_name};")
>
> def _attr_typol(self):
> return f'.type = YNL_PT_BINARY,'
> @@ -549,18 +557,46 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S
> return mem
>
> def attr_put(self, ri, var):
> - self._attr_put_line(ri, var, f"ynl_attr_put(nlh, {self.enum_name}, " +
> - f"{var}->{self.c_name}, {var}->_len.{self.c_name})")
> + if self.get('sub-type') and self.get('sub-type') in scalars:
> + presence = self.presence_type()
> + ri.cw.block_start(line=f"if ({var}->_{presence}.{self.c_name})")
> + ri.cw.p(f"i = {var}->_{presence}.{self.c_name} * sizeof(__{self.get('sub-type')});")
> + ri.cw.p(f"ynl_attr_put(nlh, {self.enum_name}, " +
> + f"{var}->{self.c_name}, i);")
> + ri.cw.block_end()
> + pass
> + else:
> + self._attr_put_line(ri, var, f"ynl_attr_put(nlh, {self.enum_name}, "
> + f"{var}->{self.c_name}, {var}->_len.{self.c_name})")
>
> def _attr_get(self, ri, var):
> - len_mem = var + '->_len.' + self.c_name
> - return [f"{len_mem} = len;",
> - f"{var}->{self.c_name} = malloc(len);",
> - f"memcpy({var}->{self.c_name}, ynl_attr_data(attr), len);"], \
> + get_lines = []
> + len_mem = var + '->_' + self.presence_type() + '.' + self.c_name
> +
> + if self.get('sub-type') and self.get('sub-type') in scalars:
> + get_lines = [
> + f"{len_mem} = len / sizeof(__{self.get('sub-type')});",
> + f"len = {len_mem} * sizeof(__{self.get('sub-type')});",
> + ]
> + else:
> + get_lines += [f"{len_mem} = len;"]
> +
> + get_lines += [
> + f"{var}->{self.c_name} = malloc(len);",
> + f"memcpy({var}->{self.c_name}, ynl_attr_data(attr), len);"
> + ]
> +
> + return get_lines, \
> ['len = ynl_attr_data_len(attr);'], \
> ['unsigned int len;']
>
> def _setter_lines(self, ri, member, presence):
> + if self.get('sub-type') and self.get('sub-type') in scalars:
> + return [f"{presence} = count;",
> + f"count *= sizeof(__{self.get('sub-type')});",
> + f"{member} = malloc(count);",
> + f'memcpy({member}, {self.c_name}, count);']
> +
> return [f"{presence} = len;",
> f"{member} = malloc({presence});",
> f'memcpy({member}, {self.c_name}, {presence});']
> @@ -672,7 +708,7 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S
> lines = []
> if self.attr['type'] in scalars:
> lines += [f"free({var}->{ref}{self.c_name});"]
> - elif self.attr['type'] == 'binary' and 'struct' in self.attr:
> + elif self.attr['type'] == 'binary':
> lines += [f"free({var}->{ref}{self.c_name});"]
> elif self.attr['type'] == 'string':
> lines += [
> --
> 2.49.0
>
Powered by blists - more mailing lists