Skip to content

Commit

Permalink
tools: ynl: Add struct parsing to nlspec
Browse files Browse the repository at this point in the history
Add python classes for struct definitions to nlspec

Signed-off-by: Donald Hunter <donald.hunter@gmail.com>
Reviewed-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
  • Loading branch information
Donald Hunter authored and Jakub Kicinski committed Mar 29, 2023
1 parent de74945 commit bec0b7a
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions tools/net/ynl/lib/nlspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,44 @@ def items(self):
return self.attrs.items()


class SpecStructMember(SpecElement):
"""Struct member attribute
Represents a single struct member attribute.
Attributes:
type string, type of the member attribute
"""
def __init__(self, family, yaml):
super().__init__(family, yaml)
self.type = yaml['type']


class SpecStruct(SpecElement):
"""Netlink struct type
Represents a C struct definition.
Attributes:
members ordered list of struct members
"""
def __init__(self, family, yaml):
super().__init__(family, yaml)

self.members = []
for member in yaml.get('members', []):
self.members.append(self.new_member(family, member))

def new_member(self, family, elem):
return SpecStructMember(family, elem)

def __iter__(self):
yield from self.members

def items(self):
return self.members.items()


class SpecOperation(SpecElement):
"""Netlink Operation
Expand Down Expand Up @@ -344,6 +382,9 @@ def new_enum(self, elem):
def new_attr_set(self, elem):
return SpecAttrSet(self, elem)

def new_struct(self, elem):
return SpecStruct(self, elem)

def new_operation(self, elem, req_val, rsp_val):
return SpecOperation(self, elem, req_val, rsp_val)

Expand Down Expand Up @@ -399,6 +440,8 @@ def resolve(self):
for elem in definitions:
if elem['type'] == 'enum' or elem['type'] == 'flags':
self.consts[elem['name']] = self.new_enum(elem)
elif elem['type'] == 'struct':
self.consts[elem['name']] = self.new_struct(elem)
else:
self.consts[elem['name']] = elem

Expand Down

0 comments on commit bec0b7a

Please sign in to comment.