Skip to content

Commit

Permalink
tools: ynl: fix enum-as-flags in the generic CLI
Browse files Browse the repository at this point in the history
Lorenzo points out that the generic CLI is broken for the netdev
family. When I added the support for documentation of enums
(and sparse enums) the client script was not updated.
It expects the values in enum to be a list of names,
now it can also be a dict (YAML object).

Reported-by: Lorenzo Bianconi <lorenzo@kernel.org>
Fixes: e4b48ed ("tools: ynl: add a completely generic client")
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
  • Loading branch information
Jakub Kicinski committed Mar 9, 2023
1 parent 6517a60 commit c311aaa
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
7 changes: 5 additions & 2 deletions tools/net/ynl/lib/nlspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ class SpecEnumSet(SpecElement):
as declared in the "definitions" section of the spec.
Attributes:
type enum or flags
entries entries by name
type enum or flags
entries entries by name
entries_by_val entries by value
Methods:
get_mask for flags compute the mask of all defined values
"""
Expand All @@ -117,9 +118,11 @@ def __init__(self, family, yaml):
prev_entry = None
value_start = self.yaml.get('value-start', 0)
self.entries = dict()
self.entries_by_val = dict()
for entry in self.yaml['entries']:
e = self.new_entry(entry, prev_entry, value_start)
self.entries[e.name] = e
self.entries_by_val[e.raw_value()] = e
prev_entry = e

def new_entry(self, entry, prev_entry, value_start):
Expand Down
9 changes: 2 additions & 7 deletions tools/net/ynl/lib/ynl.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,6 @@ def __init__(self, def_path, schema=None):
self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_CAP_ACK, 1)
self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_EXT_ACK, 1)

self._types = dict()

for elem in self.yaml.get('definitions', []):
self._types[elem['name']] = elem

self.async_msg_ids = set()
self.async_msg_queue = []

Expand Down Expand Up @@ -353,13 +348,13 @@ def _add_attr(self, space, name, value):

def _decode_enum(self, rsp, attr_spec):
raw = rsp[attr_spec['name']]
enum = self._types[attr_spec['enum']]
enum = self.consts[attr_spec['enum']]
i = attr_spec.get('value-start', 0)
if 'enum-as-flags' in attr_spec and attr_spec['enum-as-flags']:
value = set()
while raw:
if raw & 1:
value.add(enum['entries'][i])
value.add(enum.entries_by_val[i].name)
raw >>= 1
i += 1
else:
Expand Down

0 comments on commit c311aaa

Please sign in to comment.