Skip to contentSkip to conversation

tools: ynl: couple of cmdline enhancements

PATCH Applied Jiri Pirko 3 participants 7 messages Feb 22, 2024 – Feb 24, 2024

Subsystems (3)
lore
Share & tools
lore

Jiri Pirko

author
#0 lore raw

v3 cover tools: ynl: couple of cmdline enhancements

From: Jiri Pirko <[email protected]>

This is part of the original "netlink: specs: devlink: add the rest of missing attribute definitions" set which was rejected [1]. These three patches enhances the cmdline user comfort, allowing to pass flag attribute with bool values and enum names instead of scalars.

[1] https://lore.kernel.org/all/[email protected]/

---
v2->v3:
- see changelog of individual patches (1,3)
v1->v2:
- only first 3 patches left, the rest it cut out
- see changelog of individual patches

Jiri Pirko (3):
tools: ynl: allow user to specify flag attr with bool values
tools: ynl: process all scalar types encoding in single elif statement
tools: ynl: allow user to pass enum string instead of scalar value

 tools/net/ynl/lib/ynl.py | 43 +++++++++++++++++++++++++++++++---------
 1 file changed, 34 insertions(+), 9 deletions(-)
Signature · 1 line
2.43.2
author
#1 lore raw

PATCH v3 1/3 tools: ynl: allow user to specify flag attr with bool values

From: Jiri Pirko <[email protected]>

The flag attr presence in Netlink message indicates value "true", if it is missing in the message it means "false".

Allow user to specify attrname with value "true"/"false" in json for flag attrs, treat "false" value properly.

---
v2->V3:
- add comment
v1->v2:
- accept other values than "False"

---
 tools/net/ynl/lib/ynl.py | 3 +++
 1 file changed, 3 insertions(+)
tools/net/ynl/lib/ynl.py +3 -0
index f45ee5f29bed..1c5c7662dc9a 100644
@@ -459,6 +459,9 @@ class YnlFamily(SpecFamily):
                 attr_payload += self._add_attr(attr['nested-attributes'],
                                                subname, subvalue, sub_attrs)
         elif attr["type"] == 'flag':
+            if not value:
+                # If value is absent or false then skip attribute creation.
+                return b''
             attr_payload = b''
         elif attr["type"] == 'string':
             attr_payload = str(value).encode('ascii') + b'\x00'
Signature · 1 line
2.43.2
maintainer Reviewed-by on v3 1/3
#2 lore raw

Re: [patch net-next v3 1/3] tools: ynl: allow user to specify flag attr with bool values

Quoted 10 lines · Jiri Pirko

From: Jiri Pirko <[email protected]>

The flag attr presence in Netlink message indicates value "true", if it is missing in the message it means "false".

Allow user to specify attrname with value "true"/"false" in json for flag attrs, treat "false" value properly.

author
#3 lore raw

PATCH v3 2/3 tools: ynl: process all scalar types encoding in single elif statement

In reply to Jiri Pirko

From: Jiri Pirko <[email protected]>

As a preparation to handle enums for scalar values, unify the processing of all scalar types in a single elif statement.

---
 tools/net/ynl/lib/ynl.py | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
tools/net/ynl/lib/ynl.py +7 -7
index 1c5c7662dc9a..e459a130170b 100644
@@ -474,14 +474,14 @@ class YnlFamily(SpecFamily):
                 attr_payload = self._encode_struct(attr.struct_name, value)
             else:
                 raise Exception(f'Unknown type for binary attribute, value: {value}')
-        elif attr.is_auto_scalar:
+        elif attr['type'] in NlAttr.type_formats or attr.is_auto_scalar:
             scalar = int(value)
-            real_type = attr["type"][0] + ('32' if scalar.bit_length() <= 32 else '64')
-            format = NlAttr.get_format(real_type, attr.byte_order)
-            attr_payload = format.pack(int(value))
-        elif attr['type'] in NlAttr.type_formats:
-            format = NlAttr.get_format(attr['type'], attr.byte_order)
-            attr_payload = format.pack(int(value))
+            if attr.is_auto_scalar:
+                attr_type = attr["type"][0] + ('32' if scalar.bit_length() <= 32 else '64')
+            else:
+                attr_type = attr["type"]
+            format = NlAttr.get_format(attr_type, attr.byte_order)
+            attr_payload = format.pack(scalar)
         elif attr['type'] in "bitfield32":
             attr_payload = struct.pack("II", int(value["value"]), int(value["selector"]))
         elif attr['type'] == 'sub-message':
Signature · 1 line
2.43.2
author
#4 lore raw

PATCH v3 3/3 tools: ynl: allow user to pass enum string instead of scalar value

In reply to Jiri Pirko

From: Jiri Pirko <[email protected]>

During decoding of messages coming from kernel, attribute values are converted to enum names in case the attribute type is enum of bitfield32.

However, when user constructs json message, he has to pass plain scalar values. See "state" "selector" and "value" attributes in following examples:

$ sudo ./tools/net/ynl/cli.py --spec Documentation/netlink/specs/dpll.yaml --do pin-set --json '{"id": 0, "parent-device": {"parent-id": 0, "state": 1}}'
$ sudo ./tools/net/ynl/cli.py --spec Documentation/netlink/specs/devlink.yaml --do port-set --json '{"bus-name": "pci", "dev-name": "0000:08:00.1", "port-index": 98304, "port-function": {"caps": {"selector": 1, "value": 1 }}}'

Allow user to pass strings containing enum names, convert them to scalar values to be encoded into Netlink message:

$ sudo ./tools/net/ynl/cli.py --spec Documentation/netlink/specs/dpll.yaml --do pin-set --json '{"id": 0, "parent-device": {"parent-id": 0, "state": "connected"}}'
$ sudo ./tools/net/ynl/cli.py --spec Documentation/netlink/specs/devlink.yaml --do port-set --json '{"bus-name": "pci", "dev-name": "0000:08:00.1", "port-index": 98304, "port-function": {"caps": {"selector": ["roce-bit"], "value": ["roce-bit"] }}}'

---
v2->v3:
- s/_encode_enum/_get_scalar/ back
- push out enum related code from _get_scalar() to new _encode_enum() function
v1->v2:
- s/_get_scalar/_encode_enum/
- accept flat name not in a list

---
 tools/net/ynl/lib/ynl.py | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)
tools/net/ynl/lib/ynl.py +24 -2
index e459a130170b..ac55aa5a3083 100644
@@ -438,6 +438,26 @@ class YnlFamily(SpecFamily):
         self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_ADD_MEMBERSHIP,
                              mcast_id)
 
+    def _encode_enum(self, attr_spec, value):
+        enum = self.consts[attr_spec['enum']]
+        if enum.type == 'flags' or attr_spec.get('enum-as-flags', False):
+            scalar = 0
+            if isinstance(value, str):
+                value = [value]
+            for single_value in value:
+                scalar += enum.entries[single_value].user_value(as_flags = True)
+            return scalar
+        else:
+            return enum.entries[value].user_value()
+
+    def _get_scalar(self, attr_spec, value):
+        try:
+            return int(value)
+        except (ValueError, TypeError) as e:
+            if 'enum' not in attr_spec:
+                raise e
+        return self._encode_enum(attr_spec, value);
+
     def _add_attr(self, space, name, value, search_attrs):
         try:
             attr = self.attr_sets[space][name]
@@ -475,7 +495,7 @@ class YnlFamily(SpecFamily):
             else:
                 raise Exception(f'Unknown type for binary attribute, value: {value}')
         elif attr['type'] in NlAttr.type_formats or attr.is_auto_scalar:
-            scalar = int(value)
+            scalar = self._get_scalar(attr, value)
             if attr.is_auto_scalar:
                 attr_type = attr["type"][0] + ('32' if scalar.bit_length() <= 32 else '64')
             else:
@@ -483,7 +503,9 @@ class YnlFamily(SpecFamily):
             format = NlAttr.get_format(attr_type, attr.byte_order)
             attr_payload = format.pack(scalar)
         elif attr['type'] in "bitfield32":
-            attr_payload = struct.pack("II", int(value["value"]), int(value["selector"]))
+            scalar_value = self._get_scalar(attr, value["value"])
+            scalar_selector = self._get_scalar(attr, value["selector"])
+            attr_payload = struct.pack("II", scalar_value, scalar_selector)
         elif attr['type'] == 'sub-message':
             msg_format = self._resolve_selector(attr, search_attrs)
             attr_payload = b''
Signature · 1 line
2.43.2
maintainer Reviewed-by on v3 3/3
#5 lore raw

Re: [patch net-next v3 3/3] tools: ynl: allow user to pass enum string instead of scalar value

Quoted 13 lines · Jiri Pirko

From: Jiri Pirko <[email protected]>

During decoding of messages coming from kernel, attribute values are converted to enum names in case the attribute type is enum of bitfield32.

However, when user constructs json message, he has to pass plain scalar values. See "state" "selector" and "value" attributes in following examples:

$ sudo ./tools/net/ynl/cli.py --spec Documentation/netlink/specs/dpll.yaml --do pin-set --json '{"id": 0, "parent-device": {"parent-id": 0, "state": 1}}'
$ sudo ./tools/net/ynl/cli.py --spec Documentation/netlink/specs/devlink.yaml --do port-set --json '{"bus-name": "pci", "dev-name": "0000:08:00.1", "port-index": 98304, "port-function": {"caps": {"selector": 1, "value": 1 }}}'
Quoted 7 lines

Allow user to pass strings containing enum names, convert them to scalar values to be encoded into Netlink message:

$ sudo ./tools/net/ynl/cli.py --spec Documentation/netlink/specs/dpll.yaml --do pin-set --json '{"id": 0, "parent-device": {"parent-id": 0, "state": "connected"}}'
$ sudo ./tools/net/ynl/cli.py --spec Documentation/netlink/specs/devlink.yaml --do port-set --json '{"bus-name": "pci", "dev-name": "0000:08:00.1", "port-index": 98304, "port-function": {"caps": {"selector": ["roce-bit"], "value": ["roce-bit"] }}}'
#6 lore raw
In reply to Jiri Pirko
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 8 lines · On Thu, 22 Feb 2024 14:43:48 +0100 you wrote:

From: Jiri Pirko <[email protected]>

This is part of the original "netlink: specs: devlink: add the rest of missing attribute definitions" set which was rejected [1]. These three patches enhances the cmdline user comfort, allowing to pass flag attribute with bool values and enum names instead of scalars.

[...]

Here is the summary with links:
- [net-next,v3,1/3] tools: ynl: allow user to specify flag attr with bool values

    https://git.kernel.org/netdev/net-next/c/ac95b1fca034
  - [net-next,v3,2/3] tools: ynl: process all scalar types encoding in single elif statement
    https://git.kernel.org/netdev/net-next/c/ffe10a4546fe
  - [net-next,v3,3/3] tools: ynl: allow user to pass enum string instead of scalar value
    https://git.kernel.org/netdev/net-next/c/e8a6c515ff5f

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html