netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jakub Kicinski <kuba@kernel.org>
To: davem@davemloft.net
Cc: netdev@vger.kernel.org, edumazet@google.com, pabeni@redhat.com,
	andrew+netdev@lunn.ch, horms@kernel.org, donald.hunter@gmail.com,
	jacob.e.keller@intel.com, Jakub Kicinski <kuba@kernel.org>
Subject: [PATCH net-next 1/3] tools: ynl-gen: support sub-type for binary attributes
Date: Wed,  7 May 2025 19:28:37 -0700	[thread overview]
Message-ID: <20250508022839.1256059-2-kuba@kernel.org> (raw)
In-Reply-To: <20250508022839.1256059-1-kuba@kernel.org>

Sub-type annotation on binary attributes may indicate that the attribute
carries an array of simple types (also referred to as "C array" in docs).
Support rendering them as such in the C user code. For example for u32,
instead of:

  struct {
    u32 arr;
  } _len;

  void *arr;

render:

  struct {
    u32 arr;
  } _count;

  __u32 *arr;

Note that count is the number of elements while len was the length in bytes.

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
 tools/net/ynl/pyynl/ynl_gen_c.py | 56 ++++++++++++++++++++++++++------
 1 file changed, 46 insertions(+), 10 deletions(-)

diff --git a/tools/net/ynl/pyynl/ynl_gen_c.py b/tools/net/ynl/pyynl/ynl_gen_c.py
index 4a2d3cc07e14..2ae5eaf2611d 100755
--- a/tools/net/ynl/pyynl/ynl_gen_c.py
+++ b/tools/net/ynl/pyynl/ynl_gen_c.py
@@ -163,7 +163,7 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S
         return False
 
     def _free_lines(self, ri, var, ref):
-        if self.is_multi_val() or self.presence_type() == 'len':
+        if self.is_multi_val() or self.presence_type() in {'count', 'len'}:
             return [f'free({var}->{ref}{self.c_name});']
         return []
 
@@ -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:
+            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


  reply	other threads:[~2025-05-08  2:28 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-08  2:28 [PATCH net-next 0/3] tools: ynl-gen: support sub-types for binary attributes Jakub Kicinski
2025-05-08  2:28 ` Jakub Kicinski [this message]
2025-05-08 11:33   ` [PATCH net-next 1/3] tools: ynl-gen: support sub-type " Donald Hunter
2025-05-08 16:54     ` Jakub Kicinski
2025-05-09  9:05       ` Donald Hunter
2025-05-08  2:28 ` [PATCH net-next 2/3] tools: ynl-gen: auto-indent else Jakub Kicinski
2025-05-08 11:34   ` Donald Hunter
2025-05-08  2:28 ` [PATCH net-next 3/3] tools: ynl-gen: support struct for binary attributes Jakub Kicinski
2025-05-08 11:36   ` Donald Hunter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250508022839.1256059-2-kuba@kernel.org \
    --to=kuba@kernel.org \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=donald.hunter@gmail.com \
    --cc=edumazet@google.com \
    --cc=horms@kernel.org \
    --cc=jacob.e.keller@intel.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).