netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Donald Hunter <donald.hunter@gmail.com>
To: netdev@vger.kernel.org, Jakub Kicinski <kuba@kernel.org>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Paolo Abeni <pabeni@redhat.com>, Jonathan Corbet <corbet@lwn.net>,
	linux-doc@vger.kernel.org,
	Jacob Keller <jacob.e.keller@intel.com>,
	Breno Leitao <leitao@debian.org>, Jiri Pirko <jiri@resnulli.us>,
	Alessandro Marcolini <alessandromarcolini99@gmail.com>
Cc: donald.hunter@redhat.com, Donald Hunter <donald.hunter@gmail.com>
Subject: [PATCH net-next v1 09/12] tools/net/ynl: Add support for nested structs
Date: Tue, 23 Jan 2024 16:05:35 +0000	[thread overview]
Message-ID: <20240123160538.172-10-donald.hunter@gmail.com> (raw)
In-Reply-To: <20240123160538.172-1-donald.hunter@gmail.com>

Make it possible for struct definitions to reference other struct
definitions ofr binary members. For example, the tbf qdisc uses this
struct definition for its parms attribute:

  -
    name: tc-tbf-qopt
    type: struct
    members:
      -
        name: rate
        type: binary
        struct: tc-ratespec
      -
        name: peakrate
        type: binary
        struct: tc-ratespec
      -
        name: limit
        type: u32
      -
        name: buffer
        type: u32
      -
        name: mtu
        type: u32

This adds the necessary schema changes and adds nested struct encoding
and decoding to ynl.

Signed-off-by: Donald Hunter <donald.hunter@gmail.com>
---
 Documentation/netlink/netlink-raw.yaml | 15 ++++++++++++---
 tools/net/ynl/lib/nlspec.py            |  2 ++
 tools/net/ynl/lib/ynl.py               | 26 ++++++++++++++++++++------
 3 files changed, 34 insertions(+), 9 deletions(-)

diff --git a/Documentation/netlink/netlink-raw.yaml b/Documentation/netlink/netlink-raw.yaml
index 04b92f1a5cd6..ac4e05415f2f 100644
--- a/Documentation/netlink/netlink-raw.yaml
+++ b/Documentation/netlink/netlink-raw.yaml
@@ -152,14 +152,23 @@ properties:
                   the right formatting mechanism when displaying values of this
                   type.
                 enum: [ hex, mac, fddi, ipv4, ipv6, uuid ]
+              struct:
+                description: Name of the nested struct type.
+                type: string
             if:
               properties:
                 type:
-                  oneOf:
-                    - const: binary
-                    - const: pad
+                  const: pad
             then:
               required: [ len ]
+            if:
+              properties:
+                type:
+                  const: binary
+            then:
+              oneOf:
+                - required: [ len ]
+                - required: [ struct ]
         # End genetlink-legacy
 
   attribute-sets:
diff --git a/tools/net/ynl/lib/nlspec.py b/tools/net/ynl/lib/nlspec.py
index 44f13e383e8a..5d197a12ab8d 100644
--- a/tools/net/ynl/lib/nlspec.py
+++ b/tools/net/ynl/lib/nlspec.py
@@ -248,6 +248,7 @@ class SpecStructMember(SpecElement):
         len         integer, optional byte length of binary types
         display_hint  string, hint to help choose format specifier
                       when displaying the value
+        struct      string, name of nested struct type
     """
     def __init__(self, family, yaml):
         super().__init__(family, yaml)
@@ -256,6 +257,7 @@ class SpecStructMember(SpecElement):
         self.enum = yaml.get('enum')
         self.len = yaml.get('len')
         self.display_hint = yaml.get('display-hint')
+        self.struct = yaml.get('struct')
 
 
 class SpecStruct(SpecElement):
diff --git a/tools/net/ynl/lib/ynl.py b/tools/net/ynl/lib/ynl.py
index c9c5b1fcc6f4..dff2c042e6c3 100644
--- a/tools/net/ynl/lib/ynl.py
+++ b/tools/net/ynl/lib/ynl.py
@@ -655,7 +655,10 @@ class YnlFamily(SpecFamily):
             size = 0
             for m in members:
                 if m.type in ['pad', 'binary']:
-                    size += m.len
+                    if m.struct:
+                        size += self._struct_size(m.struct)
+                    else:
+                        size += m.len
                 else:
                     format = NlAttr.get_format(m.type, m.byte_order)
                     size += format.size
@@ -672,8 +675,14 @@ class YnlFamily(SpecFamily):
             if m.type == 'pad':
                 offset += m.len
             elif m.type == 'binary':
-                value = data[offset : offset + m.len]
-                offset += m.len
+                if m.struct:
+                    len = self._struct_size(m.struct)
+                    value = self._decode_struct(data[offset : offset + len],
+                                                m.struct)
+                    offset += len
+                else:
+                    value = data[offset : offset + m.len]
+                    offset += m.len
             else:
                 format = NlAttr.get_format(m.type, m.byte_order)
                 [ value ] = format.unpack_from(data, offset)
@@ -694,10 +703,15 @@ class YnlFamily(SpecFamily):
             if m.type == 'pad':
                 attr_payload += bytearray(m.len)
             elif m.type == 'binary':
-                if value is None:
-                    attr_payload += bytearray(m.len)
+                if m.struct:
+                    if value is None:
+                        value = dict()
+                    attr_payload += self._encode_struct(m.struct, value)
                 else:
-                    attr_payload += bytes.fromhex(value)
+                    if value is None:
+                        attr_payload += bytearray(m.len)
+                    else:
+                        attr_payload += bytes.fromhex(value)
             else:
                 if value is None:
                     value = 0
-- 
2.42.0


  parent reply	other threads:[~2024-01-23 16:06 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-23 16:05 [PATCH net-next v1 00/12] tools/net/ynl: Add features for tc family Donald Hunter
2024-01-23 16:05 ` [PATCH net-next v1 01/12] tools/net/ynl: Add --output-json arg to ynl cli Donald Hunter
2024-01-25 13:50   ` Breno Leitao
2024-01-23 16:05 ` [PATCH net-next v1 02/12] tools/net/ynl: Support sub-messages in nested attribute spaces Donald Hunter
2024-01-24  0:18   ` Jakub Kicinski
2024-01-24  9:37     ` Donald Hunter
2024-01-24 15:32       ` Jakub Kicinski
2024-01-26 12:44         ` Donald Hunter
2024-01-26 18:50           ` Jakub Kicinski
2024-01-27 17:18             ` Donald Hunter
2024-01-27 18:52               ` Alessandro Marcolini
2024-01-28 19:36                 ` Donald Hunter
2024-01-29 20:35                   ` Alessandro Marcolini
2024-01-30  1:32                   ` Jakub Kicinski
2024-01-30  1:42               ` Jakub Kicinski
2024-01-30  9:12                 ` Donald Hunter
2024-02-01 20:53                 ` Jacob Keller
2024-02-02  0:04                   ` Jakub Kicinski
2024-02-02 17:12                     ` Jacob Keller
2024-01-23 16:05 ` [PATCH net-next v1 03/12] tools/net/ynl: Refactor fixed header encoding into separate method Donald Hunter
2024-01-23 16:05 ` [PATCH net-next v1 04/12] tools/net/ynl: Add support for encoding sub-messages Donald Hunter
2024-01-23 16:05 ` [PATCH net-next v1 05/12] tools/net/ynl: Encode default values for binary blobs Donald Hunter
2024-01-23 16:05 ` [PATCH net-next v1 06/12] tools/net/ynl: Combine struct decoding logic in ynl Donald Hunter
2024-01-23 16:05 ` [PATCH net-next v1 07/12] tools/net/ynl: Rename _fixed_header_size() to _struct_size() Donald Hunter
2024-01-23 16:05 ` [PATCH net-next v1 08/12] tools/net/ynl: Move formatted_string method out of NlAttr Donald Hunter
2024-01-25 14:24   ` Breno Leitao
2024-01-23 16:05 ` Donald Hunter [this message]
2024-01-23 16:05 ` [PATCH net-next v1 10/12] doc/netlink: Describe nested structs in netlink raw docs Donald Hunter
2024-01-23 16:05 ` [PATCH net-next v1 11/12] tools/net/ynl: Add type info to struct members in generated docs Donald Hunter
2024-01-25 13:59   ` Breno Leitao
2024-01-23 16:05 ` [PATCH net-next v1 12/12] doc/netlink/specs: Update the tc spec 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=20240123160538.172-10-donald.hunter@gmail.com \
    --to=donald.hunter@gmail.com \
    --cc=alessandromarcolini99@gmail.com \
    --cc=corbet@lwn.net \
    --cc=davem@davemloft.net \
    --cc=donald.hunter@redhat.com \
    --cc=edumazet@google.com \
    --cc=jacob.e.keller@intel.com \
    --cc=jiri@resnulli.us \
    --cc=kuba@kernel.org \
    --cc=leitao@debian.org \
    --cc=linux-doc@vger.kernel.org \
    --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).