Commit 7fa217d4 authored by Jakub Kicinski's avatar Jakub Kicinski

Merge branch 'tools-ynl-gen-dust-off-the-user-space-code'

Jakub Kicinski says:

====================
tools: ynl-gen: dust off the user space code

Every now and then I wish I finished the user space part of
the netlink specs, Python scripts kind of stole the show but
C is useful for selftests and stuff which needs to be fast.
Recently someone asked me how to access devlink and ethtool
from C++ which pushed me over the edge.

Fix things which bit rotted and finish notification handling.
This series contains code gen changes only. I'll follow up
with the fixed component, samples and docs as soon as it's
merged.
====================

Link: https://lore.kernel.org/r/20230602023548.463441-1-kuba@kernel.orgSigned-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parents 3f06760c 59d814f0
......@@ -170,6 +170,7 @@ class Type(SpecAttr):
for line in lines:
ri.cw.p(line)
ri.cw.block_end()
return True
def _setter_lines(self, ri, member, presence):
raise Exception(f"Setter not implemented for class type {self.type}")
......@@ -197,6 +198,12 @@ class TypeUnused(Type):
def presence_type(self):
return ''
def arg_member(self, ri):
return []
def _attr_get(self, ri, var):
return ['return MNL_CB_ERROR;'], None, None
def _attr_typol(self):
return '.type = YNL_PT_REJECT, '
......@@ -208,8 +215,14 @@ class TypePad(Type):
def presence_type(self):
return ''
def arg_member(self, ri):
return []
def _attr_typol(self):
return '.type = YNL_PT_REJECT, '
return '.type = YNL_PT_IGNORE, '
def attr_get(self, ri, var, first):
pass
def attr_policy(self, cw):
pass
......@@ -411,7 +424,8 @@ class TypeNest(Type):
f"{self.enum_name}, &{var}->{self.c_name})")
def _attr_get(self, ri, var):
get_lines = [f"{self.nested_render_name}_parse(&parg, attr);"]
get_lines = [f"if ({self.nested_render_name}_parse(&parg, attr))",
"return MNL_CB_ERROR;"]
init_lines = [f"parg.rsp_policy = &{self.nested_render_name}_nest;",
f"parg.data = &{var}->{self.c_name};"]
return get_lines, init_lines, None
......@@ -812,6 +826,7 @@ class Family(SpecFamily):
inherit = set()
nested = spec['nested-attributes']
if nested not in self.root_sets:
if nested not in self.pure_nested_structs:
self.pure_nested_structs[nested] = Struct(self, nested, inherited=inherit)
if attr in rs_members['request']:
self.pure_nested_structs[nested].request = True
......@@ -872,6 +887,12 @@ class Family(SpecFamily):
self.hooks[when][op_mode]['set'].add(name)
self.hooks[when][op_mode]['list'].append(name)
def has_notifications(self):
for op in self.ops.values():
if 'notify' in op or 'event' in op:
return True
return False
class RenderInfo:
def __init__(self, cw, family, ku_space, op, op_name, op_mode, attr_set=None):
......@@ -883,11 +904,12 @@ class RenderInfo:
self.op_mode = op_mode
# 'do' and 'dump' response parsing is identical
if op_mode != 'do' and 'dump' in op and 'do' in op and 'reply' in op['do'] and \
op["do"]["reply"] == op["dump"]["reply"]:
self.type_consistent = True
else:
self.type_consistent = op_mode == 'event'
if op_mode != 'do' and 'dump' in op and 'do' in op:
if ('reply' in op['do']) != ('reply' in op["dump"]):
self.type_consistent = False
elif 'reply' in op['do'] and op["do"]["reply"] != op["dump"]["reply"]:
self.type_consistent = False
self.attr_set = attr_set
if not self.attr_set:
......@@ -1152,6 +1174,56 @@ def put_typol(cw, struct):
cw.nl()
def put_op_name_fwd(family, cw):
cw.write_func_prot('const char *', f'{family.name}_op_str', ['int op'], suffix=';')
def put_op_name(family, cw):
map_name = f'{family.name}_op_strmap'
cw.block_start(line=f"static const char * const {map_name}[] =")
for op_name, op in family.msgs.items():
cw.p(f'[{op.enum_name}] = "{op_name}",')
cw.block_end(line=';')
cw.nl()
cw.write_func_prot('const char *', f'{family.name}_op_str', ['int op'])
cw.block_start()
cw.p(f'if (op < 0 || op >= (int)MNL_ARRAY_SIZE({map_name}))')
cw.p('return NULL;')
cw.p(f'return {map_name}[op];')
cw.block_end()
cw.nl()
def put_enum_to_str_fwd(family, cw, enum):
args = [f'enum {enum.render_name} value']
if 'enum-name' in enum and not enum['enum-name']:
args = ['int value']
cw.write_func_prot('const char *', f'{enum.render_name}_str', args, suffix=';')
def put_enum_to_str(family, cw, enum):
map_name = f'{enum.render_name}_strmap'
cw.block_start(line=f"static const char * const {map_name}[] =")
for entry in enum.entries.values():
cw.p(f'[{entry.value}] = "{entry.name}",')
cw.block_end(line=';')
cw.nl()
args = [f'enum {enum.render_name} value']
if 'enum-name' in enum and not enum['enum-name']:
args = ['int value']
cw.write_func_prot('const char *', f'{enum.render_name}_str', args)
cw.block_start()
if enum.type == 'flags':
cw.p('value = ffs(value) - 1;')
cw.p(f'if (value < 0 || value >= (int)MNL_ARRAY_SIZE({map_name}))')
cw.p('return NULL;')
cw.p(f'return {map_name}[value];')
cw.block_end()
cw.nl()
def put_req_nested(ri, struct):
func_args = ['struct nlmsghdr *nlh',
'unsigned int attr_type',
......@@ -1211,8 +1283,9 @@ def _multi_parse(ri, struct, init_lines, local_vars):
first = True
for _, arg in struct.member_list():
arg.attr_get(ri, 'dst', first=first)
first = False
good = arg.attr_get(ri, 'dst', first=first)
# First may be 'unused' or 'pad', ignore those
first &= not good
ri.cw.block_end()
ri.cw.nl()
......@@ -1304,13 +1377,13 @@ def print_req(ri):
ret_err = '-1'
direction = "request"
local_vars = ['struct nlmsghdr *nlh;',
'int len, err;']
'int err;']
if 'reply' in ri.op[ri.op_mode]:
ret_ok = 'rsp'
ret_err = 'NULL'
local_vars += [f'{type_name(ri, rdir(direction))} *rsp;',
'struct ynl_parse_arg yarg = { .ys = ys, };']
'struct ynl_req_state yrs = { .yarg = { .ys = ys, }, };']
print_prototype(ri, direction, terminate=False)
ri.cw.block_start()
......@@ -1320,41 +1393,39 @@ def print_req(ri):
ri.cw.p(f"ys->req_policy = &{ri.struct['request'].render_name}_nest;")
if 'reply' in ri.op[ri.op_mode]:
ri.cw.p(f"yarg.rsp_policy = &{ri.struct['reply'].render_name}_nest;")
ri.cw.p(f"yrs.yarg.rsp_policy = &{ri.struct['reply'].render_name}_nest;")
ri.cw.nl()
for _, attr in ri.struct["request"].member_list():
attr.attr_put(ri, "req")
ri.cw.nl()
ri.cw.p('err = mnl_socket_sendto(ys->sock, nlh, nlh->nlmsg_len);')
ri.cw.p('if (err < 0)')
ri.cw.p(f"return {ret_err};")
ri.cw.nl()
ri.cw.p('len = mnl_socket_recvfrom(ys->sock, ys->rx_buf, MNL_SOCKET_BUFFER_SIZE);')
ri.cw.p('if (len < 0)')
ri.cw.p(f"return {ret_err};")
ri.cw.nl()
parse_arg = "NULL"
if 'reply' in ri.op[ri.op_mode]:
ri.cw.p('rsp = calloc(1, sizeof(*rsp));')
ri.cw.p('yarg.data = rsp;')
ri.cw.p('yrs.yarg.data = rsp;')
ri.cw.p(f"yrs.cb = {op_prefix(ri, 'reply')}_parse;")
if ri.op.value is not None:
ri.cw.p(f'yrs.rsp_cmd = {ri.op.enum_name};')
else:
ri.cw.p(f'yrs.rsp_cmd = {ri.op.rsp_value};')
ri.cw.nl()
ri.cw.p(f"err = {ri.nl.parse_cb_run(op_prefix(ri, 'reply') + '_parse', '&yarg', False)};")
parse_arg = '&yrs'
ri.cw.p(f"err = ynl_exec(ys, nlh, {parse_arg});")
ri.cw.p('if (err < 0)')
if 'reply' in ri.op[ri.op_mode]:
ri.cw.p('goto err_free;')
else:
ri.cw.p('return -1;')
ri.cw.nl()
ri.cw.p('err = ynl_recv_ack(ys, err);')
ri.cw.p('if (err)')
ri.cw.p('goto err_free;')
ri.cw.nl()
ri.cw.p(f"return {ret_ok};")
ri.cw.nl()
ri.cw.p('err_free:')
if 'reply' in ri.op[ri.op_mode]:
ri.cw.p('err_free:')
ri.cw.p(f"{call_free(ri, rdir(direction), 'rsp')}")
ri.cw.p(f"return {ret_err};")
ri.cw.block_end()
......@@ -1364,7 +1435,7 @@ def print_dump(ri):
ri.cw.block_start()
local_vars = ['struct ynl_dump_state yds = {};',
'struct nlmsghdr *nlh;',
'int len, err;']
'int err;']
for var in local_vars:
ri.cw.p(f'{var}')
......@@ -1373,6 +1444,10 @@ def print_dump(ri):
ri.cw.p('yds.ys = ys;')
ri.cw.p(f"yds.alloc_sz = sizeof({type_name(ri, rdir(direction))});")
ri.cw.p(f"yds.cb = {op_prefix(ri, 'reply', deref=True)}_parse;")
if ri.op.value is not None:
ri.cw.p(f'yds.rsp_cmd = {ri.op.enum_name};')
else:
ri.cw.p(f'yds.rsp_cmd = {ri.op.rsp_value};')
ri.cw.p(f"yds.rsp_policy = &{ri.struct['reply'].render_name}_nest;")
ri.cw.nl()
ri.cw.p(f"nlh = ynl_gemsg_start_dump(ys, {ri.nl.get_family_id()}, {ri.op.enum_name}, 1);")
......@@ -1384,20 +1459,9 @@ def print_dump(ri):
attr.attr_put(ri, "req")
ri.cw.nl()
ri.cw.p('err = mnl_socket_sendto(ys->sock, nlh, nlh->nlmsg_len);')
ri.cw.p('if (err < 0)')
ri.cw.p('return NULL;')
ri.cw.nl()
ri.cw.block_start(line='do')
ri.cw.p('len = mnl_socket_recvfrom(ys->sock, ys->rx_buf, MNL_SOCKET_BUFFER_SIZE);')
ri.cw.p('if (len < 0)')
ri.cw.p('goto free_list;')
ri.cw.nl()
ri.cw.p(f"err = {ri.nl.parse_cb_run('ynl_dump_trampoline', '&yds', False, indent=2)};")
ri.cw.p('err = ynl_exec_dump(ys, nlh, &yds);')
ri.cw.p('if (err < 0)')
ri.cw.p('goto free_list;')
ri.cw.block_end(line='while (err > 0);')
ri.cw.nl()
ri.cw.p('return yds.first;')
......@@ -1418,6 +1482,14 @@ def free_arg_name(direction):
return 'obj'
def print_alloc_wrapper(ri, direction):
name = op_prefix(ri, direction)
ri.cw.write_func_prot(f'static inline struct {name} *', f"{name}_alloc", [f"void"])
ri.cw.block_start()
ri.cw.p(f'return calloc(1, sizeof(struct {name}));')
ri.cw.block_end()
def print_free_prototype(ri, direction, suffix=';'):
name = op_prefix(ri, direction)
arg = free_arg_name(direction)
......@@ -1465,6 +1537,7 @@ def print_type_full(ri, struct):
def print_type_helpers(ri, direction, deref=False):
print_free_prototype(ri, direction)
ri.cw.nl()
if ri.ku_space == 'user' and direction == 'request':
for _, attr in ri.struct[direction].member_list():
......@@ -1473,6 +1546,7 @@ def print_type_helpers(ri, direction, deref=False):
def print_req_type_helpers(ri):
print_alloc_wrapper(ri, "request")
print_type_helpers(ri, "request")
......@@ -1496,6 +1570,12 @@ def print_req_type(ri):
print_type(ri, "request")
def print_req_free(ri):
if 'request' not in ri.op[ri.op_mode]:
return
_free_type(ri, 'request', ri.struct['request'])
def print_rsp_type(ri):
if (ri.op_mode == 'do' or ri.op_mode == 'dump') and 'reply' in ri.op[ri.op_mode]:
direction = 'reply'
......@@ -1513,6 +1593,7 @@ def print_wrapped_type(ri):
elif ri.op_mode == 'notify' or ri.op_mode == 'event':
ri.cw.p('__u16 family;')
ri.cw.p('__u8 cmd;')
ri.cw.p('struct ynl_ntf_base_type *next;')
ri.cw.p(f"void (*free)({type_name(ri, 'reply')} *ntf);")
ri.cw.p(f"{type_name(ri, 'reply', deref=True)} obj __attribute__ ((aligned (8)));")
ri.cw.block_end(line=';')
......@@ -1564,7 +1645,7 @@ def print_dump_type_free(ri):
ri.cw.block_start()
ri.cw.p(f"{sub_type} *next = rsp;")
ri.cw.nl()
ri.cw.block_start(line='while (next)')
ri.cw.block_start(line='while ((void *)next != YNL_LIST_END)')
_free_type_members_iter(ri, ri.struct['reply'])
ri.cw.p('rsp = next;')
ri.cw.p('next = rsp->next;')
......@@ -2035,6 +2116,45 @@ def render_uapi(family, cw):
cw.p(f'#endif /* {hdr_prot} */')
def _render_user_ntf_entry(ri, op):
ri.cw.block_start(line=f"[{op.enum_name}] = ")
ri.cw.p(f".alloc_sz\t= sizeof({type_name(ri, 'event')}),")
ri.cw.p(f".cb\t\t= {op_prefix(ri, 'reply', deref=True)}_parse,")
ri.cw.p(f".policy\t\t= &{ri.struct['reply'].render_name}_nest,")
ri.cw.p(f".free\t\t= (void *){op_prefix(ri, 'notify')}_free,")
ri.cw.block_end(line=',')
def render_user_family(family, cw, prototype):
symbol = f'const struct ynl_family ynl_{family.c_name}_family'
if prototype:
cw.p(f'extern {symbol};')
return
ntf = family.has_notifications()
if ntf:
cw.block_start(line=f"static const struct ynl_ntf_info {family['name']}_ntf_info[] = ")
for ntf_op in sorted(family.all_notify.keys()):
op = family.ops[ntf_op]
ri = RenderInfo(cw, family, "user", op, ntf_op, "notify")
for ntf in op['notify']['cmds']:
_render_user_ntf_entry(ri, ntf)
for op_name, op in family.ops.items():
if 'event' not in op:
continue
ri = RenderInfo(cw, family, "user", op, op_name, "event")
_render_user_ntf_entry(ri, op)
cw.block_end(line=";")
cw.nl()
cw.block_start(f'{symbol} = ')
cw.p(f'.name\t\t= "{family.name}",')
if ntf:
cw.p(f".ntf_info\t= {family['name']}_ntf_info,")
cw.p(f".ntf_info_size\t= MNL_ARRAY_SIZE({family['name']}_ntf_info),")
cw.block_end(line=';')
def find_kernel_root(full_path):
sub_path = ''
while True:
......@@ -2103,6 +2223,13 @@ def main():
cw.nl()
headers = ['uapi/' + parsed.uapi_header]
else:
cw.p('#include <stdlib.h>')
if args.header:
cw.p('#include <string.h>')
cw.p('#include <linux/types.h>')
else:
cw.p(f'#include "{parsed.name}-user.h"')
cw.p('#include "ynl.h"')
headers = [parsed.uapi_header]
for definition in parsed['definitions']:
if 'header' in definition:
......@@ -2124,6 +2251,8 @@ def main():
else:
cw.p('struct ynl_sock;')
cw.nl()
render_user_family(parsed, cw, True)
cw.nl()
if args.mode == "kernel":
if args.header:
......@@ -2184,8 +2313,15 @@ def main():
print_kernel_family_struct_src(parsed, cw)
if args.mode == "user":
has_ntf = False
if args.header:
cw.p('/* Enums */')
put_op_name_fwd(parsed, cw)
for name, const in parsed.consts.items():
if isinstance(const, EnumSet):
put_enum_to_str_fwd(parsed, cw, const)
cw.nl()
cw.p('/* Common nested types */')
for attr_set, struct in sorted(parsed.pure_nested_structs.items()):
ri = RenderInfo(cw, parsed, args.mode, "", "", "", attr_set)
......@@ -2221,9 +2357,8 @@ def main():
if 'notify' in op:
cw.p(f"/* {op.enum_name} - notify */")
ri = RenderInfo(cw, parsed, args.mode, op, op_name, 'notify')
has_ntf = True
if not ri.type_consistent:
raise Exception('Only notifications with consistent types supported')
raise Exception(f'Only notifications with consistent types supported ({op.name})')
print_wrapped_type(ri)
if 'event' in op:
......@@ -2233,11 +2368,19 @@ def main():
cw.nl()
print_wrapped_type(ri)
if has_ntf:
if parsed.has_notifications():
cw.p('/* --------------- Common notification parsing --------------- */')
print_ntf_parse_prototype(parsed, cw)
cw.nl()
else:
cw.p('/* Enums */')
put_op_name(parsed, cw)
for name, const in parsed.consts.items():
if isinstance(const, EnumSet):
put_enum_to_str(parsed, cw, const)
cw.nl()
cw.p('/* Policies */')
for name, _ in parsed.attr_sets.items():
struct = Struct(parsed, name)
......@@ -2263,6 +2406,7 @@ def main():
if 'do' in op and 'event' not in op:
cw.p(f"/* {op.enum_name} - do */")
ri = RenderInfo(cw, parsed, args.mode, op, op_name, "do")
print_req_free(ri)
print_rsp_free(ri)
parse_rsp_msg(ri)
print_req(ri)
......@@ -2280,14 +2424,12 @@ def main():
if 'notify' in op:
cw.p(f"/* {op.enum_name} - notify */")
ri = RenderInfo(cw, parsed, args.mode, op, op_name, 'notify')
has_ntf = True
if not ri.type_consistent:
raise Exception('Only notifications with consistent types supported')
raise Exception(f'Only notifications with consistent types supported ({op.name})')
print_ntf_type_free(ri)
if 'event' in op:
cw.p(f"/* {op.enum_name} - event */")
has_ntf = True
ri = RenderInfo(cw, parsed, args.mode, op, op_name, "do")
parse_rsp_msg(ri)
......@@ -2295,10 +2437,13 @@ def main():
ri = RenderInfo(cw, parsed, args.mode, op, op_name, "event")
print_ntf_type_free(ri)
if has_ntf:
if parsed.has_notifications():
cw.p('/* --------------- Common notification parsing --------------- */')
print_ntf_type_parse(parsed, cw, args.mode)
cw.nl()
render_user_family(parsed, cw, False)
if args.header:
cw.p(f'#endif /* {hdr_prot} */')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment