tools: ynl: generate code for the handshake family
Subsystems (3)
Jakub Kicinski
maintainercover tools: ynl: generate code for the handshake family
Add necessary features and generate user space C code for serializing / deserializing messages of the handshake family.
In addition to basics already present in netdev and fou, handshake has nested attrs and multi-attr u32.
Jakub Kicinski (3):
tools: ynl-gen: fill in support for MultiAttr scalars
tools: ynl-gen: improve unwind on parsing errors
tools: ynl: generate code for the handshake family
tools/net/ynl/generated/Makefile | 2 +- tools/net/ynl/generated/handshake-user.c | 385 +++++++++++++++++++++++ tools/net/ynl/generated/handshake-user.h | 148 +++++++++ tools/net/ynl/ynl-gen-c.py | 69 +++- 4 files changed, 593 insertions(+), 11 deletions(-) create mode 100644 tools/net/ynl/generated/handshake-user.c create mode 100644 tools/net/ynl/generated/handshake-user.h
Signature · 1 line
2.40.1
Jakub Kicinski
maintainerPATCH 1/3 tools: ynl-gen: fill in support for MultiAttr scalars
The handshake family needs support for MultiAttr scalars. Right now we only support code gen for MultiAttr nested types.
--- tools/net/ynl/ynl-gen-c.py | 47 ++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 5 deletions(-)
tools/net/ynl/ynl-gen-c.py +42 -5
@@ -94,7 +94,10 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S def arg_member(self, ri): member = self._complex_member_type(ri) if member: - return [member + ' *' + self.c_name] + arg = [member + ' *' + self.c_name] + if self.presence_type() == 'count': + arg += ['unsigned int n_' + self.c_name] + return arg raise Exception(f"Struct member not implemented for class type {self.type}") def struct_member(self, ri):
@@ -188,9 +191,12 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S code.append(presence + ' = 1;') code += self._setter_lines(ri, member, presence) - ri.cw.write_func('static inline void', - f"{op_prefix(ri, direction, deref=deref)}_set_{'_'.join(ref)}", - body=code, + func_name = f"{op_prefix(ri, direction, deref=deref)}_set_{'_'.join(ref)}" + free = bool([x for x in code if 'free(' in x]) + alloc = bool([x for x in code if 'alloc(' in x]) + if free and not alloc: + func_name = '__' + func_name + ri.cw.write_func('static inline void', func_name, body=code, args=[f'{type_name(ri, direction, deref=deref)} *{var}'] + self.arg_member(ri))
@@ -444,6 +450,13 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S def presence_type(self): return 'count' + def _mnl_type(self): + t = self.type + # mnl does not have a helper for signed types + if t[0] == 's': + t = 'u' + t[1:] + return t + def _complex_member_type(self, ri): if 'type' not in self.attr or self.attr['type'] == 'nest': return f"struct {self.nested_render_name}"
@@ -457,9 +470,14 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S return 'type' not in self.attr or self.attr['type'] == 'nest' def free(self, ri, var, ref): - if 'type' not in self.attr or self.attr['type'] == 'nest': + if self.attr['type'] in scalars: + ri.cw.p(f"free({var}->{ref}{self.c_name});") + elif 'type' not in self.attr or self.attr['type'] == 'nest': ri.cw.p(f"for (i = 0; i < {var}->{ref}n_{self.c_name}; i++)") ri.cw.p(f'{self.nested_render_name}_free(&{var}->{ref}{self.c_name}[i]);') + ri.cw.p(f"free({var}->{ref}{self.c_name});") + else: + raise Exception(f"Free of MultiAttr sub-type {self.attr['type']} not supported yet") def _attr_typol(self): if 'type' not in self.attr or self.attr['type'] == 'nest':
@@ -472,6 +490,25 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S def _attr_get(self, ri, var): return f'{var}->n_{self.c_name}++;', None, None + def attr_put(self, ri, var): + if self.attr['type'] in scalars: + put_type = self._mnl_type() + ri.cw.p(f"for (unsigned int i = 0; i < {var}->n_{self.c_name}; i++)") + ri.cw.p(f"mnl_attr_put_{put_type}(nlh, {self.enum_name}, {var}->{self.c_name}[i]);") + elif 'type' not in self.attr or self.attr['type'] == 'nest': + ri.cw.p(f"for (unsigned int i = 0; i < {var}->n_{self.c_name}; i++)") + self._attr_put_line(ri, var, f"{self.nested_render_name}_put(nlh, " + + f"{self.enum_name}, &{var}->{self.c_name}[i])") + else: + raise Exception(f"Put of MultiAttr sub-type {self.attr['type']} not supported yet") + + def _setter_lines(self, ri, member, presence): + # For multi-attr we have a count, not presence, hack up the presence + presence = presence[:-(len('_present.') + len(self.c_name))] + "n_" + self.c_name + return [f"free({member});", + f"{member} = {self.c_name};", + f"{presence} = n_{self.c_name};"] + class TypeArrayNest(Type): def is_multi_val(self):
Signature · 1 line
2.40.1
Jakub Kicinski
maintainerPATCH 2/3 tools: ynl-gen: improve unwind on parsing errors
In reply to Jakub KicinskiWhen parsing multi-attr we count the objects and then allocate an array to hold the parsed objects. If an attr space has multiple multi-attr objects, however, if parsing the first array fails we'll leave the object count for the second even tho the second array was never allocated.
This may cause crashes when freeing objects on error.
Count attributes to a variable on the stack and only set the count in the object once the memory was allocated.
--- tools/net/ynl/ynl-gen-c.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-)
tools/net/ynl/ynl-gen-c.py +17 -5
@@ -488,7 +488,7 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S raise Exception(f"Sub-type {self.attr['type']} not supported yet") def _attr_get(self, ri, var): - return f'{var}->n_{self.c_name}++;', None, None + return f'n_{self.c_name}++;', None, None def attr_put(self, ri, var): if self.attr['type'] in scalars:
@@ -1306,6 +1306,11 @@ _C_KW = { local_vars.append('struct ynl_parse_arg parg;') init_lines.append('parg.ys = yarg->ys;') + all_multi = array_nests | multi_attrs + + for anest in sorted(all_multi): + local_vars.append(f"unsigned int n_{struct[anest].c_name} = 0;") + ri.cw.block_start() ri.cw.write_func_lvar(local_vars)
@@ -1316,6 +1321,11 @@ _C_KW = { for arg in struct.inherited: ri.cw.p(f'dst->{arg} = {arg};') + for anest in sorted(all_multi): + aspec = struct[anest] + ri.cw.p(f"if (dst->{aspec.c_name})") + ri.cw.p(f'return ynl_error_parse(yarg, "attribute already present ({struct.attr_set.name}.{aspec.name})");') + ri.cw.nl() ri.cw.block_start(line=iter_line)
@@ -1331,8 +1341,9 @@ _C_KW = { for anest in sorted(array_nests): aspec = struct[anest] - ri.cw.block_start(line=f"if (dst->n_{aspec.c_name})") - ri.cw.p(f"dst->{aspec.c_name} = calloc(dst->n_{aspec.c_name}, sizeof(*dst->{aspec.c_name}));") + ri.cw.block_start(line=f"if (n_{aspec.c_name})") + ri.cw.p(f"dst->{aspec.c_name} = calloc({aspec.c_name}, sizeof(*dst->{aspec.c_name}));") + ri.cw.p(f"dst->n_{aspec.c_name} = n_{aspec.c_name};") ri.cw.p('i = 0;') ri.cw.p(f"parg.rsp_policy = &{aspec.nested_render_name}_nest;") ri.cw.block_start(line=f"mnl_attr_for_each_nested(attr, attr_{aspec.c_name})")
@@ -1346,8 +1357,9 @@ _C_KW = { for anest in sorted(multi_attrs): aspec = struct[anest] - ri.cw.block_start(line=f"if (dst->n_{aspec.c_name})") - ri.cw.p(f"dst->{aspec.c_name} = calloc(dst->n_{aspec.c_name}, sizeof(*dst->{aspec.c_name}));") + ri.cw.block_start(line=f"if (n_{aspec.c_name})") + ri.cw.p(f"dst->{aspec.c_name} = calloc(n_{aspec.c_name}, sizeof(*dst->{aspec.c_name}));") + ri.cw.p(f"dst->n_{aspec.c_name} = n_{aspec.c_name};") ri.cw.p('i = 0;') if 'nested-attributes' in aspec: ri.cw.p(f"parg.rsp_policy = &{aspec.nested_render_name}_nest;")
Signature · 1 line
2.40.1
Jakub Kicinski
maintainerPATCH 3/3 tools: ynl: generate code for the handshake family
In reply to Jakub KicinskiGenerate support for the handshake family.
--- tools/net/ynl/generated/Makefile | 2 +- tools/net/ynl/generated/handshake-user.c | 385 +++++++++++++++++++++++ tools/net/ynl/generated/handshake-user.h | 148 +++++++++ 3 files changed, 534 insertions(+), 1 deletion(-) create mode 100644 tools/net/ynl/generated/handshake-user.c create mode 100644 tools/net/ynl/generated/handshake-user.h
tools/net/ynl/generated/Makefile +1 -1
@@ -9,7 +9,7 @@ endif TOOL:=../ynl-gen-c.py -GENS:=fou netdev +GENS:=handshake fou netdev SRCS=$(patsubst %,%-user.c,${GENS}) HDRS=$(patsubst %,%-user.h,${GENS}) OBJS=$(patsubst %,%-user.o,${GENS})
tools/net/ynl/generated/handshake-user.c new file +385 -0
385 lines not on this page · raw message
tools/net/ynl/generated/handshake-user.h new file +148 -0
148 lines not on this page · raw message
Signature · 1 line
2.40.1
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 6 lines · On Tue, 6 Jun 2023 12:42:59 -0700 you wrote:
Add necessary features and generate user space C code for serializing / deserializing messages of the handshake family.
In addition to basics already present in netdev and fou, handshake has nested attrs and multi-attr u32.
Quoted 6 lines
Jakub Kicinski (3):
tools: ynl-gen: fill in support for MultiAttr scalars
tools: ynl-gen: improve unwind on parsing errors
tools: ynl: generate code for the handshake family[...]
Here is the summary with links:
- [net-next,1/3] tools: ynl-gen: fill in support for MultiAttr scalars
https://git.kernel.org/netdev/net-next/c/2cc9671a82e3 - [net-next,2/3] tools: ynl-gen: improve unwind on parsing errors https://git.kernel.org/netdev/net-next/c/58da455b31ba - [net-next,3/3] tools: ynl: generate code for the handshake family https://git.kernel.org/netdev/net-next/c/7a11f70ce882
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html