diff mbox series

[RFC,net-next,v1,6/6] tools/net/ynl: Add optional fixed-header to dynamic nests

Message ID 20231129101159.99197-7-donald.hunter@gmail.com (mailing list archive)
State RFC
Delegated to: Netdev Maintainers
Headers show
Series tools/net/ynl: Add dynamic selector for options attrs | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/codegen success Generated files up to date
netdev/tree_selection success Clearly marked for net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 8 this patch: 8
netdev/cc_maintainers success CCed 5 of 5 maintainers
netdev/build_clang success Errors and warnings before: 8 this patch: 8
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 8 this patch: 8
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 65 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Donald Hunter Nov. 29, 2023, 10:11 a.m. UTC
Add support for an optional fixed-header to dynamic nested attribute
spaces. Several of the tc qdiscs have a binary struct for their
'options' instead of nested attributes. But the 'netem' qdisc has a
struct followed by nlattrs in its 'options'.

If a nest can have an optional fixed-header followed by zero or more
nlattrs then all cases can be supported.

Signed-off-by: Donald Hunter <donald.hunter@gmail.com>
---
 Documentation/netlink/netlink-raw.yaml |  2 ++
 tools/net/ynl/lib/ynl.py               | 24 +++++++++++++++---------
 2 files changed, 17 insertions(+), 9 deletions(-)
diff mbox series

Patch

diff --git a/Documentation/netlink/netlink-raw.yaml b/Documentation/netlink/netlink-raw.yaml
index 62061e180f8f..b5295057dcea 100644
--- a/Documentation/netlink/netlink-raw.yaml
+++ b/Documentation/netlink/netlink-raw.yaml
@@ -292,6 +292,8 @@  properties:
                           description:
                             Name of the sub-space used inside the attribute.
                           type: string
+                        fixed-header:
+                          type: string
                         struct:
                           description:
                             Name of the struct type used for the attribute.
diff --git a/tools/net/ynl/lib/ynl.py b/tools/net/ynl/lib/ynl.py
index 5ce01ce37573..86d591cb0047 100644
--- a/tools/net/ynl/lib/ynl.py
+++ b/tools/net/ynl/lib/ynl.py
@@ -170,10 +170,9 @@  class NlAttr:
 
 
 class NlAttrs:
-    def __init__(self, msg):
+    def __init__(self, msg, offset=0):
         self.attrs = []
 
-        offset = 0
         while offset < len(msg):
             attr = NlAttr(msg, offset)
             offset += attr.full_len
@@ -371,8 +370,8 @@  class NetlinkProtocol:
         fixed_header_size = 0
         if ynl:
             op = ynl.rsp_by_value[msg.cmd()]
-            fixed_header_size = ynl._fixed_header_size(op)
-        msg.raw_attrs = NlAttrs(msg.raw[fixed_header_size:])
+            fixed_header_size = ynl._fixed_header_size(op.fixed_header)
+        msg.raw_attrs = NlAttrs(msg.raw, fixed_header_size)
         return msg
 
     def get_mcast_id(self, mcast_name, mcast_groups):
@@ -571,8 +570,15 @@  class YnlFamily(SpecFamily):
             decoded = self._decode_binary(attr, dyn_spec)
         elif dyn_spec['type'] == 'nest':
             attr_space = dyn_spec['nested-attributes']
+            fixed_header_name = dyn_spec.yaml.get('fixed-header')
             if attr_space in self.attr_sets:
-                decoded = self._decode(NlAttrs(attr.raw), attr_space)
+                decoded = {}
+                offset = 0
+                if fixed_header_name:
+                    decoded.update(self._decode_fixed_header(attr, fixed_header_name));
+                    offset = self._fixed_header_size(fixed_header_name)
+                subdict = self._decode(NlAttrs(attr.raw, offset), attr_space)
+                decoded.update(subdict)
             else:
                 raise Exception(f"Unknown attribute-set '{attr_space}'")
         else:
@@ -658,16 +664,16 @@  class YnlFamily(SpecFamily):
             return
 
         msg = self.nlproto.decode(self, NlMsg(request, 0, op.attr_set))
-        offset = 20 + self._fixed_header_size(op)
+        offset = 20 + self._fixed_header_size(op.fixed_header)
         path = self._decode_extack_path(msg.raw_attrs, op.attr_set, offset,
                                         extack['bad-attr-offs'])
         if path:
             del extack['bad-attr-offs']
             extack['bad-attr'] = path
 
-    def _fixed_header_size(self, op):
-        if op.fixed_header:
-            fixed_header_members = self.consts[op.fixed_header].members
+    def _fixed_header_size(self, name):
+        if name:
+            fixed_header_members = self.consts[name].members
             size = 0
             for m in fixed_header_members:
                 format = NlAttr.get_format(m.type, m.byte_order)