All of lore.kernel.org
 help / color / mirror / Atom feed
From: Donald Hunter <donald.hunter@gmail.com>
To: Alessandro Marcolini <alessandromarcolini99@gmail.com>
Cc: davem@davemloft.net,  edumazet@google.com,  kuba@kernel.org,
	pabeni@redhat.com,  sdf@google.com,  chuck.lever@oracle.com,
	lorenzo@kernel.org,  jacob.e.keller@intel.com,  jiri@resnulli.us,
	netdev@vger.kernel.org
Subject: Re: [PATCH net-next 3/3] tools: ynl: add encoding support for 'sub-message' to ynl
Date: Tue, 23 Jan 2024 16:44:54 +0000	[thread overview]
Message-ID: <m2v87kxam1.fsf@gmail.com> (raw)
In-Reply-To: <0eedc19860e9b84f105c57d17219b3d0af3100d2.1705950652.git.alessandromarcolini99@gmail.com> (Alessandro Marcolini's message of "Mon, 22 Jan 2024 20:19:41 +0100")

Alessandro Marcolini <alessandromarcolini99@gmail.com> writes:

> Add encoding support for 'sub-message' attribute and for resolving
> sub-message selectors at different nesting level from the key
> attribute.

I think the relevant patches in my series are:

https://lore.kernel.org/netdev/20240123160538.172-3-donald.hunter@gmail.com/T/#u
https://lore.kernel.org/netdev/20240123160538.172-5-donald.hunter@gmail.com/T/#u

>
> Also, add encoding support for multi-attr attributes.

This would be better as a separate patch since it is unrelated to the
other changes.

> Signed-off-by: Alessandro Marcolini <alessandromarcolini99@gmail.com>
> ---
>  tools/net/ynl/lib/ynl.py | 54 +++++++++++++++++++++++++++++++++++-----
>  1 file changed, 48 insertions(+), 6 deletions(-)
>
> diff --git a/tools/net/ynl/lib/ynl.py b/tools/net/ynl/lib/ynl.py
> index 1e10512b2117..f8c56944f7e7 100644
> --- a/tools/net/ynl/lib/ynl.py
> +++ b/tools/net/ynl/lib/ynl.py
> @@ -449,7 +449,7 @@ class YnlFamily(SpecFamily):
>          self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_ADD_MEMBERSHIP,
>                               mcast_id)
>  
> -    def _add_attr(self, space, name, value):
> +    def _add_attr(self, space, name, value, vals):
>          try:
>              attr = self.attr_sets[space][name]
>          except KeyError:
> @@ -458,8 +458,13 @@ class YnlFamily(SpecFamily):
>          if attr["type"] == 'nest':
>              nl_type |= Netlink.NLA_F_NESTED
>              attr_payload = b''
> -            for subname, subvalue in value.items():
> -                attr_payload += self._add_attr(attr['nested-attributes'], subname, subvalue)
> +            # Check if it's a list of values (i.e. it contains multi-attr elements)
> +            for subname, subvalue in (
> +                ((k, v) for item in value for k, v in item.items())
> +                if isinstance(value, list)
> +                else value.items()
> +            ):
> +                attr_payload += self._add_attr(attr['nested-attributes'], subname, subvalue, vals)

Should really check whether multi-attr is true in the spec before
processing the json input as a list of values.

>          elif attr["type"] == 'flag':
>              attr_payload = b''
>          elif attr["type"] == 'string':
> @@ -481,6 +486,12 @@ class YnlFamily(SpecFamily):
>              attr_payload = format.pack(int(value))
>          elif attr['type'] in "bitfield32":
>              attr_payload = struct.pack("II", int(value["value"]), int(value["selector"]))
> +        elif attr['type'] == "sub-message":
> +            spec = self._resolve_selector(attr, vals)
> +            attr_spec = spec["attribute-set"]
> +            attr_payload = b''
> +            for subname, subvalue in value.items():
> +                attr_payload += self._add_attr(attr_spec, subname, subvalue, vals)
>          else:
>              raise Exception(f'Unknown type at {space} {name} {value} {attr["type"]}')
>  
> @@ -555,9 +566,40 @@ class YnlFamily(SpecFamily):
>          sub_msg_spec = self.sub_msgs[sub_msg]
>  
>          selector = attr_spec.selector
> -        if selector not in vals:
> +
> +        def _find_attr_path(attr, vals, path=None):
> +            if path is None:
> +                path = []
> +            if isinstance(vals, dict):
> +                if attr in vals:
> +                    return path
> +                for k, v in vals.items():
> +                    result = _find_attr_path(attr, v, path + [k])
> +                    if result is not None:
> +                        return result
> +            elif isinstance(vals, list):
> +                for idx, v in enumerate(vals):
> +                    result = _find_attr_path(attr, v, path + [idx])
> +                    if result is not None:
> +                        return result
> +            return None
> +
> +        def _find_selector_val(sel, vals, path):
> +            while path != []:
> +                v = vals.copy()
> +                for step in path:
> +                    v = v[step]
> +                if sel in v:
> +                    return v[sel]
> +                path.pop()
> +            return vals[sel] if sel in vals else None
> +
> +        attr_path = _find_attr_path(attr_spec.name, vals)
> +        value = _find_selector_val(selector, vals, attr_path)
> +
> +        if value is None:
>              raise Exception(f"There is no value for {selector} to resolve '{attr_spec.name}'")
> -        value = vals[selector]
> +
>          if value not in sub_msg_spec.formats:
>              raise Exception(f"No message format for '{value}' in sub-message spec '{sub_msg}'")
>  
> @@ -772,7 +814,7 @@ class YnlFamily(SpecFamily):
>                      format = NlAttr.get_format(m.type, m.byte_order)
>                      msg += format.pack(value)
>          for name, value in vals.items():
> -            msg += self._add_attr(op.attr_set.name, name, value)
> +            msg += self._add_attr(op.attr_set.name, name, value, vals)
>          msg = _genl_msg_finalize(msg)
>  
>          self.sock.send(msg, 0)

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

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-22 19:19 [PATCH net-next 0/3] tools: ynl: Add sub-message and multi-attr encoding support Alessandro Marcolini
2024-01-22 19:19 ` [PATCH net-next 1/3] tools: ynl: correct typo and docstring Alessandro Marcolini
2024-01-23 16:28   ` Donald Hunter
2024-01-22 19:19 ` [PATCH net-next 2/3] doc: netlink: specs: tc: add multi-attr to tc-taprio-sched-entry Alessandro Marcolini
2024-01-23 16:35   ` Donald Hunter
2024-01-22 19:19 ` [PATCH net-next 3/3] tools: ynl: add encoding support for 'sub-message' to ynl Alessandro Marcolini
2024-01-22 19:46   ` Breno Leitao
2024-01-22 20:31     ` Alessandro Marcolini
2024-01-23 16:44   ` Donald Hunter [this message]
2024-01-24  9:12     ` Alessandro Marcolini
2024-01-24  9:45       ` Donald Hunter
2024-01-22 21:43 ` [PATCH net-next 0/3] tools: ynl: Add sub-message and multi-attr encoding support Donald Hunter
2024-01-23  0:40   ` Alessandro Marcolini

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=m2v87kxam1.fsf@gmail.com \
    --to=donald.hunter@gmail.com \
    --cc=alessandromarcolini99@gmail.com \
    --cc=chuck.lever@oracle.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=jacob.e.keller@intel.com \
    --cc=jiri@resnulli.us \
    --cc=kuba@kernel.org \
    --cc=lorenzo@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sdf@google.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.