2 # SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)
9 from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, SpecEnumEntry
13 return name.upper().replace('-', '_')
17 return name.lower().replace('-', '_')
21 def get_family_id(self):
22 return 'ys->family_id'
24 def parse_cb_run(self, cb, data, is_dump=False, indent=1):
25 ind = '\n\t\t' + '\t' * indent + ' '
27 return f"mnl_cb_run2(ys->rx_buf, len, 0, 0, {cb}, {data},{ind}ynl_cb_array, NLMSG_MIN_TYPE)"
29 return f"mnl_cb_run2(ys->rx_buf, len, ys->seq, ys->portid,{ind}{cb}, {data},{ind}" + \
30 "ynl_cb_array, NLMSG_MIN_TYPE)"
34 def __init__(self, family, attr_set, attr, value):
35 super().__init__(family, attr_set, attr, value)
38 self.attr_set = attr_set
39 self.type = attr['type']
40 self.checks = attr.get('checks', {})
43 self.len = attr['len']
44 if 'nested-attributes' in attr:
45 self.nested_attrs = attr['nested-attributes']
46 if self.nested_attrs == family.name:
47 self.nested_render_name = f"{family.name}"
49 self.nested_render_name = f"{family.name}_{c_lower(self.nested_attrs)}"
51 self.c_name = c_lower(self.name)
52 if self.c_name in _C_KW:
57 delattr(self, "enum_name")
60 self.enum_name = f"{self.attr_set.name_prefix}{self.name}"
61 self.enum_name = c_upper(self.enum_name)
63 def is_multi_val(self):
67 return self.type in {'u8', 'u16', 'u32', 'u64', 's32', 's64'}
69 def presence_type(self):
72 def presence_member(self, space, type_filter):
73 if self.presence_type() != type_filter:
76 if self.presence_type() == 'bit':
77 pfx = '__' if space == 'user' else ''
78 return f"{pfx}u32 {self.c_name}:1;"
80 if self.presence_type() == 'len':
81 pfx = '__' if space == 'user' else ''
82 return f"{pfx}u32 {self.c_name}_len;"
84 def _complex_member_type(self, ri):
87 def free_needs_iter(self):
90 def free(self, ri, var, ref):
91 if self.is_multi_val() or self.presence_type() == 'len':
92 ri.cw.p(f'free({var}->{ref}{self.c_name});')
94 def arg_member(self, ri):
95 member = self._complex_member_type(ri)
97 arg = [member + ' *' + self.c_name]
98 if self.presence_type() == 'count':
99 arg += ['unsigned int n_' + self.c_name]
101 raise Exception(f"Struct member not implemented for class type {self.type}")
103 def struct_member(self, ri):
104 if self.is_multi_val():
105 ri.cw.p(f"unsigned int n_{self.c_name};")
106 member = self._complex_member_type(ri)
108 ptr = '*' if self.is_multi_val() else ''
109 ri.cw.p(f"{member} {ptr}{self.c_name};")
111 members = self.arg_member(ri)
115 def _attr_policy(self, policy):
116 return '{ .type = ' + policy + ', }'
118 def attr_policy(self, cw):
119 policy = c_upper('nla-' + self.attr['type'])
121 spec = self._attr_policy(policy)
122 cw.p(f"\t[{self.enum_name}] = {spec},")
124 def _attr_typol(self):
125 raise Exception(f"Type policy not implemented for class type {self.type}")
127 def attr_typol(self, cw):
128 typol = self._attr_typol()
129 cw.p(f'[{self.enum_name}] = {"{"} .name = "{self.name}", {typol}{"}"},')
131 def _attr_put_line(self, ri, var, line):
132 if self.presence_type() == 'bit':
133 ri.cw.p(f"if ({var}->_present.{self.c_name})")
134 elif self.presence_type() == 'len':
135 ri.cw.p(f"if ({var}->_present.{self.c_name}_len)")
138 def _attr_put_simple(self, ri, var, put_type):
139 line = f"mnl_attr_put_{put_type}(nlh, {self.enum_name}, {var}->{self.c_name})"
140 self._attr_put_line(ri, var, line)
142 def attr_put(self, ri, var):
143 raise Exception(f"Put not implemented for class type {self.type}")
145 def _attr_get(self, ri, var):
146 raise Exception(f"Attr get not implemented for class type {self.type}")
148 def attr_get(self, ri, var, first):
149 lines, init_lines, local_vars = self._attr_get(ri, var)
150 if type(lines) is str:
152 if type(init_lines) is str:
153 init_lines = [init_lines]
155 kw = 'if' if first else 'else if'
156 ri.cw.block_start(line=f"{kw} (type == {self.enum_name})")
158 for local in local_vars:
162 if not self.is_multi_val():
163 ri.cw.p("if (ynl_attr_validate(yarg, attr))")
164 ri.cw.p("return MNL_CB_ERROR;")
165 if self.presence_type() == 'bit':
166 ri.cw.p(f"{var}->_present.{self.c_name} = 1;")
170 for line in init_lines:
178 def _setter_lines(self, ri, member, presence):
179 raise Exception(f"Setter not implemented for class type {self.type}")
181 def setter(self, ri, space, direction, deref=False, ref=None):
182 ref = (ref if ref else []) + [self.c_name]
184 member = f"{var}->{'.'.join(ref)}"
188 for i in range(0, len(ref)):
189 presence = f"{var}->{'.'.join(ref[:i] + [''])}_present.{ref[i]}"
190 if self.presence_type() == 'bit':
191 code.append(presence + ' = 1;')
192 code += self._setter_lines(ri, member, presence)
194 func_name = f"{op_prefix(ri, direction, deref=deref)}_set_{'_'.join(ref)}"
195 free = bool([x for x in code if 'free(' in x])
196 alloc = bool([x for x in code if 'alloc(' in x])
197 if free and not alloc:
198 func_name = '__' + func_name
199 ri.cw.write_func('static inline void', func_name, body=code,
200 args=[f'{type_name(ri, direction, deref=deref)} *{var}'] + self.arg_member(ri))
203 class TypeUnused(Type):
204 def presence_type(self):
207 def arg_member(self, ri):
210 def _attr_get(self, ri, var):
211 return ['return MNL_CB_ERROR;'], None, None
213 def _attr_typol(self):
214 return '.type = YNL_PT_REJECT, '
216 def attr_policy(self, cw):
221 def presence_type(self):
224 def arg_member(self, ri):
227 def _attr_typol(self):
228 return '.type = YNL_PT_IGNORE, '
230 def attr_get(self, ri, var, first):
233 def attr_policy(self, cw):
237 class TypeScalar(Type):
238 def __init__(self, family, attr_set, attr, value):
239 super().__init__(family, attr_set, attr, value)
241 self.byte_order_comment = ''
242 if 'byte-order' in attr:
243 self.byte_order_comment = f" /* {attr['byte-order']} */"
245 # Added by resolve():
246 self.is_bitfield = None
247 delattr(self, "is_bitfield")
248 self.type_name = None
249 delattr(self, "type_name")
252 self.resolve_up(super())
254 if 'enum-as-flags' in self.attr and self.attr['enum-as-flags']:
255 self.is_bitfield = True
256 elif 'enum' in self.attr:
257 self.is_bitfield = self.family.consts[self.attr['enum']]['type'] == 'flags'
259 self.is_bitfield = False
261 if 'enum' in self.attr and not self.is_bitfield:
262 self.type_name = f"enum {self.family.name}_{c_lower(self.attr['enum'])}"
264 self.type_name = '__' + self.type
268 # mnl does not have a helper for signed types
273 def _attr_policy(self, policy):
274 if 'flags-mask' in self.checks or self.is_bitfield:
276 enum = self.family.consts[self.attr['enum']]
277 mask = enum.get_mask(as_flags=True)
279 flags = self.family.consts[self.checks['flags-mask']]
280 flag_cnt = len(flags['entries'])
281 mask = (1 << flag_cnt) - 1
282 return f"NLA_POLICY_MASK({policy}, 0x{mask:x})"
283 elif 'min' in self.checks:
284 return f"NLA_POLICY_MIN({policy}, {self.checks['min']})"
285 elif 'enum' in self.attr:
286 enum = self.family.consts[self.attr['enum']]
287 cnt = len(enum['entries'])
288 return f"NLA_POLICY_MAX({policy}, {cnt - 1})"
289 return super()._attr_policy(policy)
291 def _attr_typol(self):
292 return f'.type = YNL_PT_U{self.type[1:]}, '
294 def arg_member(self, ri):
295 return [f'{self.type_name} {self.c_name}{self.byte_order_comment}']
297 def attr_put(self, ri, var):
298 self._attr_put_simple(ri, var, self._mnl_type())
300 def _attr_get(self, ri, var):
301 return f"{var}->{self.c_name} = mnl_attr_get_{self._mnl_type()}(attr);", None, None
303 def _setter_lines(self, ri, member, presence):
304 return [f"{member} = {self.c_name};"]
307 class TypeFlag(Type):
308 def arg_member(self, ri):
311 def _attr_typol(self):
312 return '.type = YNL_PT_FLAG, '
314 def attr_put(self, ri, var):
315 self._attr_put_line(ri, var, f"mnl_attr_put(nlh, {self.enum_name}, 0, NULL)")
317 def _attr_get(self, ri, var):
318 return [], None, None
320 def _setter_lines(self, ri, member, presence):
324 class TypeString(Type):
325 def arg_member(self, ri):
326 return [f"const char *{self.c_name}"]
328 def presence_type(self):
331 def struct_member(self, ri):
332 ri.cw.p(f"char *{self.c_name};")
334 def _attr_typol(self):
335 return f'.type = YNL_PT_NUL_STR, '
337 def _attr_policy(self, policy):
338 mem = '{ .type = ' + policy
339 if 'max-len' in self.checks:
340 mem += ', .len = ' + str(self.checks['max-len'])
344 def attr_policy(self, cw):
345 if self.checks.get('unterminated-ok', False):
346 policy = 'NLA_STRING'
348 policy = 'NLA_NUL_STRING'
350 spec = self._attr_policy(policy)
351 cw.p(f"\t[{self.enum_name}] = {spec},")
353 def attr_put(self, ri, var):
354 self._attr_put_simple(ri, var, 'strz')
356 def _attr_get(self, ri, var):
357 len_mem = var + '->_present.' + self.c_name + '_len'
358 return [f"{len_mem} = len;",
359 f"{var}->{self.c_name} = malloc(len + 1);",
360 f"memcpy({var}->{self.c_name}, mnl_attr_get_str(attr), len);",
361 f"{var}->{self.c_name}[len] = 0;"], \
362 ['len = strnlen(mnl_attr_get_str(attr), mnl_attr_get_payload_len(attr));'], \
363 ['unsigned int len;']
365 def _setter_lines(self, ri, member, presence):
366 return [f"free({member});",
367 f"{presence}_len = strlen({self.c_name});",
368 f"{member} = malloc({presence}_len + 1);",
369 f'memcpy({member}, {self.c_name}, {presence}_len);',
370 f'{member}[{presence}_len] = 0;']
373 class TypeBinary(Type):
374 def arg_member(self, ri):
375 return [f"const void *{self.c_name}", 'size_t len']
377 def presence_type(self):
380 def struct_member(self, ri):
381 ri.cw.p(f"void *{self.c_name};")
383 def _attr_typol(self):
384 return f'.type = YNL_PT_BINARY,'
386 def _attr_policy(self, policy):
388 if len(self.checks) == 1 and 'min-len' in self.checks:
389 mem += '.len = ' + str(self.checks['min-len'])
390 elif len(self.checks) == 0:
391 mem += '.type = NLA_BINARY'
393 raise Exception('One or more of binary type checks not implemented, yet')
397 def attr_put(self, ri, var):
398 self._attr_put_line(ri, var, f"mnl_attr_put(nlh, {self.enum_name}, " +
399 f"{var}->_present.{self.c_name}_len, {var}->{self.c_name})")
401 def _attr_get(self, ri, var):
402 len_mem = var + '->_present.' + self.c_name + '_len'
403 return [f"{len_mem} = len;",
404 f"{var}->{self.c_name} = malloc(len);",
405 f"memcpy({var}->{self.c_name}, mnl_attr_get_payload(attr), len);"], \
406 ['len = mnl_attr_get_payload_len(attr);'], \
407 ['unsigned int len;']
409 def _setter_lines(self, ri, member, presence):
410 return [f"free({member});",
411 f"{member} = malloc({presence}_len);",
412 f'memcpy({member}, {self.c_name}, {presence}_len);']
415 class TypeNest(Type):
416 def _complex_member_type(self, ri):
417 return f"struct {self.nested_render_name}"
419 def free(self, ri, var, ref):
420 ri.cw.p(f'{self.nested_render_name}_free(&{var}->{ref}{self.c_name});')
422 def _attr_typol(self):
423 return f'.type = YNL_PT_NEST, .nest = &{self.nested_render_name}_nest, '
425 def _attr_policy(self, policy):
426 return 'NLA_POLICY_NESTED(' + self.nested_render_name + '_nl_policy)'
428 def attr_put(self, ri, var):
429 self._attr_put_line(ri, var, f"{self.nested_render_name}_put(nlh, " +
430 f"{self.enum_name}, &{var}->{self.c_name})")
432 def _attr_get(self, ri, var):
433 get_lines = [f"if ({self.nested_render_name}_parse(&parg, attr))",
434 "return MNL_CB_ERROR;"]
435 init_lines = [f"parg.rsp_policy = &{self.nested_render_name}_nest;",
436 f"parg.data = &{var}->{self.c_name};"]
437 return get_lines, init_lines, None
439 def setter(self, ri, space, direction, deref=False, ref=None):
440 ref = (ref if ref else []) + [self.c_name]
442 for _, attr in ri.family.pure_nested_structs[self.nested_attrs].member_list():
443 attr.setter(ri, self.nested_attrs, direction, deref=deref, ref=ref)
446 class TypeMultiAttr(Type):
447 def is_multi_val(self):
450 def presence_type(self):
455 # mnl does not have a helper for signed types
460 def _complex_member_type(self, ri):
461 if 'type' not in self.attr or self.attr['type'] == 'nest':
462 return f"struct {self.nested_render_name}"
463 elif self.attr['type'] in scalars:
464 scalar_pfx = '__' if ri.ku_space == 'user' else ''
465 return scalar_pfx + self.attr['type']
467 raise Exception(f"Sub-type {self.attr['type']} not supported yet")
469 def free_needs_iter(self):
470 return 'type' not in self.attr or self.attr['type'] == 'nest'
472 def free(self, ri, var, ref):
473 if self.attr['type'] in scalars:
474 ri.cw.p(f"free({var}->{ref}{self.c_name});")
475 elif 'type' not in self.attr or self.attr['type'] == 'nest':
476 ri.cw.p(f"for (i = 0; i < {var}->{ref}n_{self.c_name}; i++)")
477 ri.cw.p(f'{self.nested_render_name}_free(&{var}->{ref}{self.c_name}[i]);')
478 ri.cw.p(f"free({var}->{ref}{self.c_name});")
480 raise Exception(f"Free of MultiAttr sub-type {self.attr['type']} not supported yet")
482 def _attr_typol(self):
483 if 'type' not in self.attr or self.attr['type'] == 'nest':
484 return f'.type = YNL_PT_NEST, .nest = &{self.nested_render_name}_nest, '
485 elif self.attr['type'] in scalars:
486 return f".type = YNL_PT_U{self.attr['type'][1:]}, "
488 raise Exception(f"Sub-type {self.attr['type']} not supported yet")
490 def _attr_get(self, ri, var):
491 return f'n_{self.c_name}++;', None, None
493 def attr_put(self, ri, var):
494 if self.attr['type'] in scalars:
495 put_type = self._mnl_type()
496 ri.cw.p(f"for (unsigned int i = 0; i < {var}->n_{self.c_name}; i++)")
497 ri.cw.p(f"mnl_attr_put_{put_type}(nlh, {self.enum_name}, {var}->{self.c_name}[i]);")
498 elif 'type' not in self.attr or self.attr['type'] == 'nest':
499 ri.cw.p(f"for (unsigned int i = 0; i < {var}->n_{self.c_name}; i++)")
500 self._attr_put_line(ri, var, f"{self.nested_render_name}_put(nlh, " +
501 f"{self.enum_name}, &{var}->{self.c_name}[i])")
503 raise Exception(f"Put of MultiAttr sub-type {self.attr['type']} not supported yet")
505 def _setter_lines(self, ri, member, presence):
506 # For multi-attr we have a count, not presence, hack up the presence
507 presence = presence[:-(len('_present.') + len(self.c_name))] + "n_" + self.c_name
508 return [f"free({member});",
509 f"{member} = {self.c_name};",
510 f"{presence} = n_{self.c_name};"]
513 class TypeArrayNest(Type):
514 def is_multi_val(self):
517 def presence_type(self):
520 def _complex_member_type(self, ri):
521 if 'sub-type' not in self.attr or self.attr['sub-type'] == 'nest':
522 return f"struct {self.nested_render_name}"
523 elif self.attr['sub-type'] in scalars:
524 scalar_pfx = '__' if ri.ku_space == 'user' else ''
525 return scalar_pfx + self.attr['sub-type']
527 raise Exception(f"Sub-type {self.attr['sub-type']} not supported yet")
529 def _attr_typol(self):
530 return f'.type = YNL_PT_NEST, .nest = &{self.nested_render_name}_nest, '
532 def _attr_get(self, ri, var):
533 local_vars = ['const struct nlattr *attr2;']
534 get_lines = [f'attr_{self.c_name} = attr;',
535 'mnl_attr_for_each_nested(attr2, attr)',
536 f'\t{var}->n_{self.c_name}++;']
537 return get_lines, None, local_vars
540 class TypeNestTypeValue(Type):
541 def _complex_member_type(self, ri):
542 return f"struct {self.nested_render_name}"
544 def _attr_typol(self):
545 return f'.type = YNL_PT_NEST, .nest = &{self.nested_render_name}_nest, '
547 def _attr_get(self, ri, var):
552 init_lines = [f"parg.rsp_policy = &{self.nested_render_name}_nest;",
553 f"parg.data = &{var}->{self.c_name};"]
554 if 'type-value' in self.attr:
555 tv_names = [c_lower(x) for x in self.attr["type-value"]]
556 local_vars += [f'const struct nlattr *attr_{", *attr_".join(tv_names)};']
557 local_vars += [f'__u32 {", ".join(tv_names)};']
558 for level in self.attr["type-value"]:
559 level = c_lower(level)
560 get_lines += [f'attr_{level} = mnl_attr_get_payload({prev});']
561 get_lines += [f'{level} = mnl_attr_get_type(attr_{level});']
562 prev = 'attr_' + level
564 tv_args = f", {', '.join(tv_names)}"
566 get_lines += [f"{self.nested_render_name}_parse(&parg, {prev}{tv_args});"]
567 return get_lines, init_lines, local_vars
571 def __init__(self, family, space_name, type_list=None, inherited=None):
573 self.space_name = space_name
574 self.attr_set = family.attr_sets[space_name]
575 # Use list to catch comparisons with empty sets
576 self._inherited = inherited if inherited is not None else []
579 self.nested = type_list is None
580 if family.name == c_lower(space_name):
581 self.render_name = f"{family.name}"
583 self.render_name = f"{family.name}_{c_lower(space_name)}"
584 self.struct_name = 'struct ' + self.render_name
585 self.ptr_name = self.struct_name + ' *'
594 self.attr_list.append((t, self.attr_set[t]),)
596 for t in self.attr_set:
597 self.attr_list.append((t, self.attr_set[t]),)
600 self.attr_max_val = None
601 for name, attr in self.attr_list:
602 if attr.value >= max_val:
604 self.attr_max_val = attr
605 self.attrs[name] = attr
608 yield from self.attrs
610 def __getitem__(self, key):
611 return self.attrs[key]
613 def member_list(self):
614 return self.attr_list
616 def set_inherited(self, new_inherited):
617 if self._inherited != new_inherited:
618 raise Exception("Inheriting different members not supported")
619 self.inherited = [c_lower(x) for x in sorted(self._inherited)]
622 class EnumEntry(SpecEnumEntry):
623 def __init__(self, enum_set, yaml, prev, value_start):
624 super().__init__(enum_set, yaml, prev, value_start)
627 self.value_change = (self.value != prev.value + 1)
629 self.value_change = (self.value != 0)
630 self.value_change = self.value_change or self.enum_set['type'] == 'flags'
634 delattr(self, "c_name")
637 self.resolve_up(super())
639 self.c_name = c_upper(self.enum_set.value_pfx + self.name)
642 class EnumSet(SpecEnumSet):
643 def __init__(self, family, yaml):
644 self.render_name = c_lower(family.name + '-' + yaml['name'])
645 self.enum_name = 'enum ' + self.render_name
647 self.value_pfx = yaml.get('name-prefix', f"{family.name}-{yaml['name']}-")
649 super().__init__(family, yaml)
651 def new_entry(self, entry, prev_entry, value_start):
652 return EnumEntry(self, entry, prev_entry, value_start)
655 class AttrSet(SpecAttrSet):
656 def __init__(self, family, yaml):
657 super().__init__(family, yaml)
659 if self.subset_of is None:
660 if 'name-prefix' in yaml:
661 pfx = yaml['name-prefix']
662 elif self.name == family.name:
663 pfx = family.name + '-a-'
665 pfx = f"{family.name}-a-{self.name}-"
666 self.name_prefix = c_upper(pfx)
667 self.max_name = c_upper(self.yaml.get('attr-max-name', f"{self.name_prefix}max"))
669 self.name_prefix = family.attr_sets[self.subset_of].name_prefix
670 self.max_name = family.attr_sets[self.subset_of].max_name
674 delattr(self, "c_name")
677 self.c_name = c_lower(self.name)
678 if self.c_name in _C_KW:
680 if self.c_name == self.family.c_name:
683 def new_attr(self, elem, value):
684 if 'multi-attr' in elem and elem['multi-attr']:
685 return TypeMultiAttr(self.family, self, elem, value)
686 elif elem['type'] in scalars:
687 return TypeScalar(self.family, self, elem, value)
688 elif elem['type'] == 'unused':
689 return TypeUnused(self.family, self, elem, value)
690 elif elem['type'] == 'pad':
691 return TypePad(self.family, self, elem, value)
692 elif elem['type'] == 'flag':
693 return TypeFlag(self.family, self, elem, value)
694 elif elem['type'] == 'string':
695 return TypeString(self.family, self, elem, value)
696 elif elem['type'] == 'binary':
697 return TypeBinary(self.family, self, elem, value)
698 elif elem['type'] == 'nest':
699 return TypeNest(self.family, self, elem, value)
700 elif elem['type'] == 'array-nest':
701 return TypeArrayNest(self.family, self, elem, value)
702 elif elem['type'] == 'nest-type-value':
703 return TypeNestTypeValue(self.family, self, elem, value)
705 raise Exception(f"No typed class for type {elem['type']}")
708 class Operation(SpecOperation):
709 def __init__(self, family, yaml, req_value, rsp_value):
710 super().__init__(family, yaml, req_value, rsp_value)
712 self.render_name = family.name + '_' + c_lower(self.name)
714 self.dual_policy = ('do' in yaml and 'request' in yaml['do']) and \
715 ('dump' in yaml and 'request' in yaml['dump'])
718 self.enum_name = None
719 delattr(self, "enum_name")
722 self.resolve_up(super())
724 if not self.is_async:
725 self.enum_name = self.family.op_prefix + c_upper(self.name)
727 self.enum_name = self.family.async_op_prefix + c_upper(self.name)
729 def add_notification(self, op):
730 if 'notify' not in self.yaml:
731 self.yaml['notify'] = dict()
732 self.yaml['notify']['reply'] = self.yaml['do']['reply']
733 self.yaml['notify']['cmds'] = []
734 self.yaml['notify']['cmds'].append(op)
737 class Family(SpecFamily):
738 def __init__(self, file_name):
741 delattr(self, "c_name")
742 self.op_prefix = None
743 delattr(self, "op_prefix")
744 self.async_op_prefix = None
745 delattr(self, "async_op_prefix")
747 delattr(self, "mcgrps")
749 delattr(self, "consts")
751 delattr(self, "hooks")
753 super().__init__(file_name)
755 self.fam_key = c_upper(self.yaml.get('c-family-name', self.yaml["name"] + '_FAMILY_NAME'))
756 self.ver_key = c_upper(self.yaml.get('c-version-name', self.yaml["name"] + '_FAMILY_VERSION'))
758 if 'definitions' not in self.yaml:
759 self.yaml['definitions'] = []
761 if 'uapi-header' in self.yaml:
762 self.uapi_header = self.yaml['uapi-header']
764 self.uapi_header = f"linux/{self.name}.h"
767 self.resolve_up(super())
769 if self.yaml.get('protocol', 'genetlink') not in {'genetlink', 'genetlink-c', 'genetlink-legacy'}:
770 raise Exception("Codegen only supported for genetlink")
772 self.c_name = c_lower(self.name)
773 if 'name-prefix' in self.yaml['operations']:
774 self.op_prefix = c_upper(self.yaml['operations']['name-prefix'])
776 self.op_prefix = c_upper(self.yaml['name'] + '-cmd-')
777 if 'async-prefix' in self.yaml['operations']:
778 self.async_op_prefix = c_upper(self.yaml['operations']['async-prefix'])
780 self.async_op_prefix = self.op_prefix
782 self.mcgrps = self.yaml.get('mcast-groups', {'list': []})
785 for when in ['pre', 'post']:
786 self.hooks[when] = dict()
787 for op_mode in ['do', 'dump']:
788 self.hooks[when][op_mode] = dict()
789 self.hooks[when][op_mode]['set'] = set()
790 self.hooks[when][op_mode]['list'] = []
792 # dict space-name -> 'request': set(attrs), 'reply': set(attrs)
793 self.root_sets = dict()
794 # dict space-name -> set('request', 'reply')
795 self.pure_nested_structs = dict()
796 self.all_notify = dict()
798 self._mock_up_events()
801 self._load_root_sets()
802 self._load_nested_sets()
803 self._load_all_notify()
806 self.kernel_policy = self.yaml.get('kernel-policy', 'split')
807 if self.kernel_policy == 'global':
808 self._load_global_policy()
810 def new_enum(self, elem):
811 return EnumSet(self, elem)
813 def new_attr_set(self, elem):
814 return AttrSet(self, elem)
816 def new_operation(self, elem, req_value, rsp_value):
817 return Operation(self, elem, req_value, rsp_value)
819 # Fake a 'do' equivalent of all events, so that we can render their response parsing
820 def _mock_up_events(self):
821 for op in self.yaml['operations']['list']:
825 'attributes': op['event']['attributes']
831 for msg in self.msgs.values():
835 self.ops[n['notify']].add_notification(n)
837 def _load_root_sets(self):
838 for op_name, op in self.ops.items():
839 if 'attribute-set' not in op:
844 for op_mode in ['do', 'dump']:
845 if op_mode in op and 'request' in op[op_mode]:
846 req_attrs.update(set(op[op_mode]['request']['attributes']))
847 if op_mode in op and 'reply' in op[op_mode]:
848 rsp_attrs.update(set(op[op_mode]['reply']['attributes']))
850 if op['attribute-set'] not in self.root_sets:
851 self.root_sets[op['attribute-set']] = {'request': req_attrs, 'reply': rsp_attrs}
853 self.root_sets[op['attribute-set']]['request'].update(req_attrs)
854 self.root_sets[op['attribute-set']]['reply'].update(rsp_attrs)
856 def _load_nested_sets(self):
857 attr_set_queue = list(self.root_sets.keys())
858 attr_set_seen = set(self.root_sets.keys())
860 while len(attr_set_queue):
861 a_set = attr_set_queue.pop(0)
862 for attr, spec in self.attr_sets[a_set].items():
863 if 'nested-attributes' not in spec:
866 nested = spec['nested-attributes']
867 if nested not in attr_set_seen:
868 attr_set_queue.append(nested)
869 attr_set_seen.add(nested)
872 if nested not in self.root_sets:
873 if nested not in self.pure_nested_structs:
874 self.pure_nested_structs[nested] = Struct(self, nested, inherited=inherit)
876 raise Exception(f'Using attr set as root and nested not supported - {nested}')
878 if 'type-value' in spec:
879 if nested in self.root_sets:
880 raise Exception("Inheriting members to a space used as root not supported")
881 inherit.update(set(spec['type-value']))
882 elif spec['type'] == 'array-nest':
884 self.pure_nested_structs[nested].set_inherited(inherit)
886 for root_set, rs_members in self.root_sets.items():
887 for attr, spec in self.attr_sets[root_set].items():
888 if 'nested-attributes' in spec:
889 nested = spec['nested-attributes']
890 if attr in rs_members['request']:
891 self.pure_nested_structs[nested].request = True
892 if attr in rs_members['reply']:
893 self.pure_nested_structs[nested].reply = True
895 # Try to reorder according to dependencies
896 pns_key_list = list(self.pure_nested_structs.keys())
898 rounds = len(pns_key_list)**2 # it's basically bubble sort
899 for _ in range(rounds):
900 if len(pns_key_list) == 0:
902 name = pns_key_list.pop(0)
904 for _, spec in self.attr_sets[name].items():
905 if 'nested-attributes' in spec:
906 if spec['nested-attributes'] not in pns_key_seen:
907 # Dicts are sorted, this will make struct last
908 struct = self.pure_nested_structs.pop(name)
909 self.pure_nested_structs[name] = struct
913 pns_key_seen.add(name)
915 pns_key_list.append(name)
916 # Propagate the request / reply
917 for attr_set, struct in reversed(self.pure_nested_structs.items()):
918 for _, spec in self.attr_sets[attr_set].items():
919 if 'nested-attributes' in spec:
920 child = self.pure_nested_structs.get(spec['nested-attributes'])
922 child.request |= struct.request
923 child.reply |= struct.reply
925 def _load_all_notify(self):
926 for op_name, op in self.ops.items():
931 self.all_notify[op_name] = op['notify']['cmds']
933 def _load_global_policy(self):
936 for op_name, op in self.ops.items():
939 if 'attribute-set' not in op:
942 if attr_set_name is None:
943 attr_set_name = op['attribute-set']
944 if attr_set_name != op['attribute-set']:
945 raise Exception('For a global policy all ops must use the same set')
947 for op_mode in ['do', 'dump']:
949 global_set.update(op[op_mode].get('request', []))
951 self.global_policy = []
952 self.global_policy_set = attr_set_name
953 for attr in self.attr_sets[attr_set_name]:
954 if attr in global_set:
955 self.global_policy.append(attr)
957 def _load_hooks(self):
958 for op in self.ops.values():
959 for op_mode in ['do', 'dump']:
960 if op_mode not in op:
962 for when in ['pre', 'post']:
963 if when not in op[op_mode]:
965 name = op[op_mode][when]
966 if name in self.hooks[when][op_mode]['set']:
968 self.hooks[when][op_mode]['set'].add(name)
969 self.hooks[when][op_mode]['list'].append(name)
971 def has_notifications(self):
972 for op in self.ops.values():
973 if 'notify' in op or 'event' in op:
979 def __init__(self, cw, family, ku_space, op, op_name, op_mode, attr_set=None):
982 self.ku_space = ku_space
984 self.op_name = op_name
985 self.op_mode = op_mode
987 # 'do' and 'dump' response parsing is identical
988 self.type_consistent = True
989 if op_mode != 'do' and 'dump' in op and 'do' in op:
990 if ('reply' in op['do']) != ('reply' in op["dump"]):
991 self.type_consistent = False
992 elif 'reply' in op['do'] and op["do"]["reply"] != op["dump"]["reply"]:
993 self.type_consistent = False
995 self.attr_set = attr_set
996 if not self.attr_set:
997 self.attr_set = op['attribute-set']
1000 self.type_name = c_lower(op_name)
1002 self.type_name = c_lower(attr_set)
1006 self.struct = dict()
1007 for op_dir in ['request', 'reply']:
1008 if op and op_dir in op[op_mode]:
1009 self.struct[op_dir] = Struct(family, self.attr_set,
1010 type_list=op[op_mode][op_dir]['attributes'])
1011 if op_mode == 'event':
1012 self.struct['reply'] = Struct(family, self.attr_set, type_list=op['event']['attributes'])
1016 def __init__(self, nlib, out_file):
1020 self._block_end = False
1021 self._silent_block = False
1023 self._out = out_file
1026 def _is_cond(cls, line):
1027 return line.startswith('if') or line.startswith('while') or line.startswith('for')
1029 def p(self, line, add_ind=0):
1031 self._block_end = False
1032 if line.startswith('else'):
1035 self._out.write('\t' * self._ind + '}\n')
1038 self._out.write('\n')
1044 if self._silent_block:
1046 self._silent_block = line.endswith(')') and CodeWriter._is_cond(line)
1049 self._out.write('\t' * ind + line + '\n')
1054 def block_start(self, line=''):
1060 def block_end(self, line=''):
1061 if line and line[0] not in {';', ','}:
1066 # Delay printing closing bracket in case "else" comes next
1068 self._out.write('\t' * (self._ind + 1) + '}\n')
1069 self._block_end = True
1073 def write_doc_line(self, doc, indent=True):
1077 if len(line) + len(word) >= 79:
1085 def write_func_prot(self, qual_ret, name, args=None, doc=None, suffix=''):
1095 if qual_ret[-1] != '*':
1097 oneline += f"{name}({', '.join(args)}){suffix}"
1099 if len(oneline) < 80:
1107 elif qual_ret[-1] != '*':
1110 ind = '\t' * (len(v) // 8) + ' ' * (len(v) % 8)
1111 delta_ind = len(v) - len(ind)
1114 while i < len(args):
1115 next_len = len(v) + len(args[i])
1117 next_len += delta_ind
1125 self.p(v + ')' + suffix)
1127 def write_func_lvar(self, local_vars):
1131 if type(local_vars) is str:
1132 local_vars = [local_vars]
1134 local_vars.sort(key=len, reverse=True)
1135 for var in local_vars:
1139 def write_func(self, qual_ret, name, body, args=None, local_vars=None):
1140 self.write_func_prot(qual_ret=qual_ret, name=name, args=args)
1141 self.write_func_lvar(local_vars=local_vars)
1148 def writes_defines(self, defines):
1150 for define in defines:
1151 if len(define[0]) > longest:
1152 longest = len(define[0])
1153 longest = ((longest + 8) // 8) * 8
1154 for define in defines:
1155 line = '#define ' + define[0]
1156 line += '\t' * ((longest - len(define[0]) + 7) // 8)
1157 if type(define[1]) is int:
1158 line += str(define[1])
1159 elif type(define[1]) is str:
1160 line += '"' + define[1] + '"'
1163 def write_struct_init(self, members):
1164 longest = max([len(x[0]) for x in members])
1165 longest += 1 # because we prepend a .
1166 longest = ((longest + 8) // 8) * 8
1169 line += '\t' * ((longest - len(one[0]) - 1 + 7) // 8)
1170 line += '= ' + one[1] + ','
1174 scalars = {'u8', 'u16', 'u32', 'u64', 's32', 's64'}
1176 direction_to_suffix = {
1182 op_mode_to_wrapper = {
1227 def rdir(direction):
1228 if direction == 'reply':
1230 if direction == 'request':
1235 def op_prefix(ri, direction, deref=False):
1236 suffix = f"_{ri.type_name}"
1238 if not ri.op_mode or ri.op_mode == 'do':
1239 suffix += f"{direction_to_suffix[direction]}"
1241 if direction == 'request':
1242 suffix += '_req_dump'
1244 if ri.type_consistent:
1246 suffix += f"{direction_to_suffix[direction]}"
1248 suffix += op_mode_to_wrapper[ri.op_mode]
1251 suffix += '_dump' if deref else '_list'
1253 return f"{ri.family['name']}{suffix}"
1256 def type_name(ri, direction, deref=False):
1257 return f"struct {op_prefix(ri, direction, deref=deref)}"
1260 def print_prototype(ri, direction, terminate=True, doc=None):
1261 suffix = ';' if terminate else ''
1263 fname = ri.op.render_name
1264 if ri.op_mode == 'dump':
1267 args = ['struct ynl_sock *ys']
1268 if 'request' in ri.op[ri.op_mode]:
1269 args.append(f"{type_name(ri, direction)} *" + f"{direction_to_suffix[direction][1:]}")
1272 if 'reply' in ri.op[ri.op_mode]:
1273 ret = f"{type_name(ri, rdir(direction))} *"
1275 ri.cw.write_func_prot(ret, fname, args, doc=doc, suffix=suffix)
1278 def print_req_prototype(ri):
1279 print_prototype(ri, "request", doc=ri.op['doc'])
1282 def print_dump_prototype(ri):
1283 print_prototype(ri, "request")
1286 def put_typol(cw, struct):
1287 type_max = struct.attr_set.max_name
1288 cw.block_start(line=f'struct ynl_policy_attr {struct.render_name}_policy[{type_max} + 1] =')
1290 for _, arg in struct.member_list():
1293 cw.block_end(line=';')
1296 cw.block_start(line=f'struct ynl_policy_nest {struct.render_name}_nest =')
1297 cw.p(f'.max_attr = {type_max},')
1298 cw.p(f'.table = {struct.render_name}_policy,')
1299 cw.block_end(line=';')
1303 def _put_enum_to_str_helper(cw, render_name, map_name, arg_name, enum=None):
1304 args = [f'int {arg_name}']
1305 if enum and not ('enum-name' in enum and not enum['enum-name']):
1306 args = [f'enum {render_name} {arg_name}']
1307 cw.write_func_prot('const char *', f'{render_name}_str', args)
1309 if enum and enum.type == 'flags':
1310 cw.p(f'{arg_name} = ffs({arg_name}) - 1;')
1311 cw.p(f'if ({arg_name} < 0 || {arg_name} >= (int)MNL_ARRAY_SIZE({map_name}))')
1312 cw.p('return NULL;')
1313 cw.p(f'return {map_name}[{arg_name}];')
1318 def put_op_name_fwd(family, cw):
1319 cw.write_func_prot('const char *', f'{family.name}_op_str', ['int op'], suffix=';')
1322 def put_op_name(family, cw):
1323 map_name = f'{family.name}_op_strmap'
1324 cw.block_start(line=f"static const char * const {map_name}[] =")
1325 for op_name, op in family.msgs.items():
1327 if op.req_value == op.rsp_value:
1328 cw.p(f'[{op.enum_name}] = "{op_name}",')
1330 cw.p(f'[{op.rsp_value}] = "{op_name}",')
1331 cw.block_end(line=';')
1334 _put_enum_to_str_helper(cw, family.name + '_op', map_name, 'op')
1337 def put_enum_to_str_fwd(family, cw, enum):
1338 args = [f'enum {enum.render_name} value']
1339 if 'enum-name' in enum and not enum['enum-name']:
1340 args = ['int value']
1341 cw.write_func_prot('const char *', f'{enum.render_name}_str', args, suffix=';')
1344 def put_enum_to_str(family, cw, enum):
1345 map_name = f'{enum.render_name}_strmap'
1346 cw.block_start(line=f"static const char * const {map_name}[] =")
1347 for entry in enum.entries.values():
1348 cw.p(f'[{entry.value}] = "{entry.name}",')
1349 cw.block_end(line=';')
1352 _put_enum_to_str_helper(cw, enum.render_name, map_name, 'value', enum=enum)
1355 def put_req_nested(ri, struct):
1356 func_args = ['struct nlmsghdr *nlh',
1357 'unsigned int attr_type',
1358 f'{struct.ptr_name}obj']
1360 ri.cw.write_func_prot('int', f'{struct.render_name}_put', func_args)
1362 ri.cw.write_func_lvar('struct nlattr *nest;')
1364 ri.cw.p("nest = mnl_attr_nest_start(nlh, attr_type);")
1366 for _, arg in struct.member_list():
1367 arg.attr_put(ri, "obj")
1369 ri.cw.p("mnl_attr_nest_end(nlh, nest);")
1372 ri.cw.p('return 0;')
1377 def _multi_parse(ri, struct, init_lines, local_vars):
1379 iter_line = "mnl_attr_for_each_nested(attr, nested)"
1381 iter_line = "mnl_attr_for_each(attr, nlh, sizeof(struct genlmsghdr))"
1386 for arg, aspec in struct.member_list():
1387 if aspec['type'] == 'array-nest':
1388 local_vars.append(f'const struct nlattr *attr_{aspec.c_name};')
1389 array_nests.add(arg)
1390 if 'multi-attr' in aspec:
1391 multi_attrs.add(arg)
1392 needs_parg |= 'nested-attributes' in aspec
1393 if array_nests or multi_attrs:
1394 local_vars.append('int i;')
1396 local_vars.append('struct ynl_parse_arg parg;')
1397 init_lines.append('parg.ys = yarg->ys;')
1399 all_multi = array_nests | multi_attrs
1401 for anest in sorted(all_multi):
1402 local_vars.append(f"unsigned int n_{struct[anest].c_name} = 0;")
1405 ri.cw.write_func_lvar(local_vars)
1407 for line in init_lines:
1411 for arg in struct.inherited:
1412 ri.cw.p(f'dst->{arg} = {arg};')
1414 for anest in sorted(all_multi):
1415 aspec = struct[anest]
1416 ri.cw.p(f"if (dst->{aspec.c_name})")
1417 ri.cw.p(f'return ynl_error_parse(yarg, "attribute already present ({struct.attr_set.name}.{aspec.name})");')
1420 ri.cw.block_start(line=iter_line)
1421 ri.cw.p('unsigned int type = mnl_attr_get_type(attr);')
1425 for _, arg in struct.member_list():
1426 good = arg.attr_get(ri, 'dst', first=first)
1427 # First may be 'unused' or 'pad', ignore those
1433 for anest in sorted(array_nests):
1434 aspec = struct[anest]
1436 ri.cw.block_start(line=f"if (n_{aspec.c_name})")
1437 ri.cw.p(f"dst->{aspec.c_name} = calloc({aspec.c_name}, sizeof(*dst->{aspec.c_name}));")
1438 ri.cw.p(f"dst->n_{aspec.c_name} = n_{aspec.c_name};")
1440 ri.cw.p(f"parg.rsp_policy = &{aspec.nested_render_name}_nest;")
1441 ri.cw.block_start(line=f"mnl_attr_for_each_nested(attr, attr_{aspec.c_name})")
1442 ri.cw.p(f"parg.data = &dst->{aspec.c_name}[i];")
1443 ri.cw.p(f"if ({aspec.nested_render_name}_parse(&parg, attr, mnl_attr_get_type(attr)))")
1444 ri.cw.p('return MNL_CB_ERROR;')
1450 for anest in sorted(multi_attrs):
1451 aspec = struct[anest]
1452 ri.cw.block_start(line=f"if (n_{aspec.c_name})")
1453 ri.cw.p(f"dst->{aspec.c_name} = calloc(n_{aspec.c_name}, sizeof(*dst->{aspec.c_name}));")
1454 ri.cw.p(f"dst->n_{aspec.c_name} = n_{aspec.c_name};")
1456 if 'nested-attributes' in aspec:
1457 ri.cw.p(f"parg.rsp_policy = &{aspec.nested_render_name}_nest;")
1458 ri.cw.block_start(line=iter_line)
1459 ri.cw.block_start(line=f"if (mnl_attr_get_type(attr) == {aspec.enum_name})")
1460 if 'nested-attributes' in aspec:
1461 ri.cw.p(f"parg.data = &dst->{aspec.c_name}[i];")
1462 ri.cw.p(f"if ({aspec.nested_render_name}_parse(&parg, attr))")
1463 ri.cw.p('return MNL_CB_ERROR;')
1464 elif aspec['type'] in scalars:
1468 ri.cw.p(f"dst->{aspec.c_name}[i] = mnl_attr_get_{t}(attr);")
1470 raise Exception('Nest parsing type not supported yet')
1478 ri.cw.p('return 0;')
1480 ri.cw.p('return MNL_CB_OK;')
1485 def parse_rsp_nested(ri, struct):
1486 func_args = ['struct ynl_parse_arg *yarg',
1487 'const struct nlattr *nested']
1488 for arg in struct.inherited:
1489 func_args.append('__u32 ' + arg)
1491 local_vars = ['const struct nlattr *attr;',
1492 f'{struct.ptr_name}dst = yarg->data;']
1495 ri.cw.write_func_prot('int', f'{struct.render_name}_parse', func_args)
1497 _multi_parse(ri, struct, init_lines, local_vars)
1500 def parse_rsp_msg(ri, deref=False):
1501 if 'reply' not in ri.op[ri.op_mode] and ri.op_mode != 'event':
1504 func_args = ['const struct nlmsghdr *nlh',
1507 local_vars = [f'{type_name(ri, "reply", deref=deref)} *dst;',
1508 'struct ynl_parse_arg *yarg = data;',
1509 'const struct nlattr *attr;']
1510 init_lines = ['dst = yarg->data;']
1512 ri.cw.write_func_prot('int', f'{op_prefix(ri, "reply", deref=deref)}_parse', func_args)
1514 _multi_parse(ri, ri.struct["reply"], init_lines, local_vars)
1520 direction = "request"
1521 local_vars = ['struct nlmsghdr *nlh;',
1524 if 'reply' in ri.op[ri.op_mode]:
1527 local_vars += [f'{type_name(ri, rdir(direction))} *rsp;',
1528 'struct ynl_req_state yrs = { .yarg = { .ys = ys, }, };']
1530 print_prototype(ri, direction, terminate=False)
1532 ri.cw.write_func_lvar(local_vars)
1534 ri.cw.p(f"nlh = ynl_gemsg_start_req(ys, {ri.nl.get_family_id()}, {ri.op.enum_name}, 1);")
1536 ri.cw.p(f"ys->req_policy = &{ri.struct['request'].render_name}_nest;")
1537 if 'reply' in ri.op[ri.op_mode]:
1538 ri.cw.p(f"yrs.yarg.rsp_policy = &{ri.struct['reply'].render_name}_nest;")
1540 for _, attr in ri.struct["request"].member_list():
1541 attr.attr_put(ri, "req")
1545 if 'reply' in ri.op[ri.op_mode]:
1546 ri.cw.p('rsp = calloc(1, sizeof(*rsp));')
1547 ri.cw.p('yrs.yarg.data = rsp;')
1548 ri.cw.p(f"yrs.cb = {op_prefix(ri, 'reply')}_parse;")
1549 if ri.op.value is not None:
1550 ri.cw.p(f'yrs.rsp_cmd = {ri.op.enum_name};')
1552 ri.cw.p(f'yrs.rsp_cmd = {ri.op.rsp_value};')
1555 ri.cw.p(f"err = ynl_exec(ys, nlh, {parse_arg});")
1556 ri.cw.p('if (err < 0)')
1557 if 'reply' in ri.op[ri.op_mode]:
1558 ri.cw.p('goto err_free;')
1560 ri.cw.p('return -1;')
1563 ri.cw.p(f"return {ret_ok};")
1566 if 'reply' in ri.op[ri.op_mode]:
1567 ri.cw.p('err_free:')
1568 ri.cw.p(f"{call_free(ri, rdir(direction), 'rsp')}")
1569 ri.cw.p(f"return {ret_err};")
1575 direction = "request"
1576 print_prototype(ri, direction, terminate=False)
1578 local_vars = ['struct ynl_dump_state yds = {};',
1579 'struct nlmsghdr *nlh;',
1582 for var in local_vars:
1586 ri.cw.p('yds.ys = ys;')
1587 ri.cw.p(f"yds.alloc_sz = sizeof({type_name(ri, rdir(direction))});")
1588 ri.cw.p(f"yds.cb = {op_prefix(ri, 'reply', deref=True)}_parse;")
1589 if ri.op.value is not None:
1590 ri.cw.p(f'yds.rsp_cmd = {ri.op.enum_name};')
1592 ri.cw.p(f'yds.rsp_cmd = {ri.op.rsp_value};')
1593 ri.cw.p(f"yds.rsp_policy = &{ri.struct['reply'].render_name}_nest;")
1595 ri.cw.p(f"nlh = ynl_gemsg_start_dump(ys, {ri.nl.get_family_id()}, {ri.op.enum_name}, 1);")
1597 if "request" in ri.op[ri.op_mode]:
1598 ri.cw.p(f"ys->req_policy = &{ri.struct['request'].render_name}_nest;")
1600 for _, attr in ri.struct["request"].member_list():
1601 attr.attr_put(ri, "req")
1604 ri.cw.p('err = ynl_exec_dump(ys, nlh, &yds);')
1605 ri.cw.p('if (err < 0)')
1606 ri.cw.p('goto free_list;')
1609 ri.cw.p('return yds.first;')
1611 ri.cw.p('free_list:')
1612 ri.cw.p(call_free(ri, rdir(direction), 'yds.first'))
1613 ri.cw.p('return NULL;')
1617 def call_free(ri, direction, var):
1618 return f"{op_prefix(ri, direction)}_free({var});"
1621 def free_arg_name(direction):
1623 return direction_to_suffix[direction][1:]
1627 def print_alloc_wrapper(ri, direction):
1628 name = op_prefix(ri, direction)
1629 ri.cw.write_func_prot(f'static inline struct {name} *', f"{name}_alloc", [f"void"])
1631 ri.cw.p(f'return calloc(1, sizeof(struct {name}));')
1635 def print_free_prototype(ri, direction, suffix=';'):
1636 name = op_prefix(ri, direction)
1637 arg = free_arg_name(direction)
1638 ri.cw.write_func_prot('void', f"{name}_free", [f"struct {name} *{arg}"], suffix=suffix)
1641 def _print_type(ri, direction, struct):
1642 suffix = f'_{ri.type_name}{direction_to_suffix[direction]}'
1644 if ri.op_mode == 'dump':
1647 ri.cw.block_start(line=f"struct {ri.family['name']}{suffix}")
1649 meta_started = False
1650 for _, attr in struct.member_list():
1651 for type_filter in ['len', 'bit']:
1652 line = attr.presence_member(ri.ku_space, type_filter)
1654 if not meta_started:
1655 ri.cw.block_start(line=f"struct")
1659 ri.cw.block_end(line='_present;')
1662 for arg in struct.inherited:
1663 ri.cw.p(f"__u32 {arg};")
1665 for _, attr in struct.member_list():
1666 attr.struct_member(ri)
1668 ri.cw.block_end(line=';')
1672 def print_type(ri, direction):
1673 _print_type(ri, direction, ri.struct[direction])
1676 def print_type_full(ri, struct):
1677 _print_type(ri, "", struct)
1680 def print_type_helpers(ri, direction, deref=False):
1681 print_free_prototype(ri, direction)
1684 if ri.ku_space == 'user' and direction == 'request':
1685 for _, attr in ri.struct[direction].member_list():
1686 attr.setter(ri, ri.attr_set, direction, deref=deref)
1690 def print_req_type_helpers(ri):
1691 print_alloc_wrapper(ri, "request")
1692 print_type_helpers(ri, "request")
1695 def print_rsp_type_helpers(ri):
1696 if 'reply' not in ri.op[ri.op_mode]:
1698 print_type_helpers(ri, "reply")
1701 def print_parse_prototype(ri, direction, terminate=True):
1702 suffix = "_rsp" if direction == "reply" else "_req"
1703 term = ';' if terminate else ''
1705 ri.cw.write_func_prot('void', f"{ri.op.render_name}{suffix}_parse",
1706 ['const struct nlattr **tb',
1707 f"struct {ri.op.render_name}{suffix} *req"],
1711 def print_req_type(ri):
1712 print_type(ri, "request")
1715 def print_req_free(ri):
1716 if 'request' not in ri.op[ri.op_mode]:
1718 _free_type(ri, 'request', ri.struct['request'])
1721 def print_rsp_type(ri):
1722 if (ri.op_mode == 'do' or ri.op_mode == 'dump') and 'reply' in ri.op[ri.op_mode]:
1724 elif ri.op_mode == 'event':
1728 print_type(ri, direction)
1731 def print_wrapped_type(ri):
1732 ri.cw.block_start(line=f"{type_name(ri, 'reply')}")
1733 if ri.op_mode == 'dump':
1734 ri.cw.p(f"{type_name(ri, 'reply')} *next;")
1735 elif ri.op_mode == 'notify' or ri.op_mode == 'event':
1736 ri.cw.p('__u16 family;')
1737 ri.cw.p('__u8 cmd;')
1738 ri.cw.p('struct ynl_ntf_base_type *next;')
1739 ri.cw.p(f"void (*free)({type_name(ri, 'reply')} *ntf);")
1740 ri.cw.p(f"{type_name(ri, 'reply', deref=True)} obj __attribute__ ((aligned (8)));")
1741 ri.cw.block_end(line=';')
1743 print_free_prototype(ri, 'reply')
1747 def _free_type_members_iter(ri, struct):
1748 for _, attr in struct.member_list():
1749 if attr.free_needs_iter():
1750 ri.cw.p('unsigned int i;')
1755 def _free_type_members(ri, var, struct, ref=''):
1756 for _, attr in struct.member_list():
1757 attr.free(ri, var, ref)
1760 def _free_type(ri, direction, struct):
1761 var = free_arg_name(direction)
1763 print_free_prototype(ri, direction, suffix='')
1765 _free_type_members_iter(ri, struct)
1766 _free_type_members(ri, var, struct)
1768 ri.cw.p(f'free({var});')
1773 def free_rsp_nested(ri, struct):
1774 _free_type(ri, "", struct)
1777 def print_rsp_free(ri):
1778 if 'reply' not in ri.op[ri.op_mode]:
1780 _free_type(ri, 'reply', ri.struct['reply'])
1783 def print_dump_type_free(ri):
1784 sub_type = type_name(ri, 'reply')
1786 print_free_prototype(ri, 'reply', suffix='')
1788 ri.cw.p(f"{sub_type} *next = rsp;")
1790 ri.cw.block_start(line='while ((void *)next != YNL_LIST_END)')
1791 _free_type_members_iter(ri, ri.struct['reply'])
1792 ri.cw.p('rsp = next;')
1793 ri.cw.p('next = rsp->next;')
1796 _free_type_members(ri, 'rsp', ri.struct['reply'], ref='obj.')
1797 ri.cw.p(f'free(rsp);')
1803 def print_ntf_type_free(ri):
1804 print_free_prototype(ri, 'reply', suffix='')
1806 _free_type_members_iter(ri, ri.struct['reply'])
1807 _free_type_members(ri, 'rsp', ri.struct['reply'], ref='obj.')
1808 ri.cw.p(f'free(rsp);')
1813 def print_req_policy_fwd(cw, struct, ri=None, terminate=True):
1814 if terminate and ri and kernel_can_gen_family_struct(struct.family):
1820 if kernel_can_gen_family_struct(struct.family) and ri:
1825 suffix = ';' if terminate else ' = {'
1827 max_attr = struct.attr_max_val
1829 name = ri.op.render_name
1830 if ri.op.dual_policy:
1831 name += '_' + ri.op_mode
1833 name = struct.render_name
1834 cw.p(f"{prefix}const struct nla_policy {name}_nl_policy[{max_attr.enum_name} + 1]{suffix}")
1837 def print_req_policy(cw, struct, ri=None):
1838 print_req_policy_fwd(cw, struct, ri=ri, terminate=False)
1839 for _, arg in struct.member_list():
1844 def kernel_can_gen_family_struct(family):
1845 return family.proto == 'genetlink'
1848 def print_kernel_op_table_fwd(family, cw, terminate):
1849 exported = not kernel_can_gen_family_struct(family)
1851 if not terminate or exported:
1852 cw.p(f"/* Ops table for {family.name} */")
1854 pol_to_struct = {'global': 'genl_small_ops',
1855 'per-op': 'genl_ops',
1856 'split': 'genl_split_ops'}
1857 struct_type = pol_to_struct[family.kernel_policy]
1861 elif family.kernel_policy == 'split':
1863 for op in family.ops.values():
1869 cnt = len(family.ops)
1871 qual = 'static const' if not exported else 'const'
1872 line = f"{qual} struct {struct_type} {family.name}_nl_ops[{cnt}]"
1874 cw.p(f"extern {line};")
1876 cw.block_start(line=line + ' =')
1882 for name in family.hooks['pre']['do']['list']:
1883 cw.write_func_prot('int', c_lower(name),
1884 ['const struct genl_split_ops *ops',
1885 'struct sk_buff *skb', 'struct genl_info *info'], suffix=';')
1886 for name in family.hooks['post']['do']['list']:
1887 cw.write_func_prot('void', c_lower(name),
1888 ['const struct genl_split_ops *ops',
1889 'struct sk_buff *skb', 'struct genl_info *info'], suffix=';')
1890 for name in family.hooks['pre']['dump']['list']:
1891 cw.write_func_prot('int', c_lower(name),
1892 ['struct netlink_callback *cb'], suffix=';')
1893 for name in family.hooks['post']['dump']['list']:
1894 cw.write_func_prot('int', c_lower(name),
1895 ['struct netlink_callback *cb'], suffix=';')
1899 for op_name, op in family.ops.items():
1904 name = c_lower(f"{family.name}-nl-{op_name}-doit")
1905 cw.write_func_prot('int', name,
1906 ['struct sk_buff *skb', 'struct genl_info *info'], suffix=';')
1909 name = c_lower(f"{family.name}-nl-{op_name}-dumpit")
1910 cw.write_func_prot('int', name,
1911 ['struct sk_buff *skb', 'struct netlink_callback *cb'], suffix=';')
1915 def print_kernel_op_table_hdr(family, cw):
1916 print_kernel_op_table_fwd(family, cw, terminate=True)
1919 def print_kernel_op_table(family, cw):
1920 print_kernel_op_table_fwd(family, cw, terminate=False)
1921 if family.kernel_policy == 'global' or family.kernel_policy == 'per-op':
1922 for op_name, op in family.ops.items():
1927 members = [('cmd', op.enum_name)]
1928 if 'dont-validate' in op:
1929 members.append(('validate',
1930 ' | '.join([c_upper('genl-dont-validate-' + x)
1931 for x in op['dont-validate']])), )
1932 for op_mode in ['do', 'dump']:
1934 name = c_lower(f"{family.name}-nl-{op_name}-{op_mode}it")
1935 members.append((op_mode + 'it', name))
1936 if family.kernel_policy == 'per-op':
1937 struct = Struct(family, op['attribute-set'],
1938 type_list=op['do']['request']['attributes'])
1940 name = c_lower(f"{family.name}-{op_name}-nl-policy")
1941 members.append(('policy', name))
1942 members.append(('maxattr', struct.attr_max_val.enum_name))
1944 members.append(('flags', ' | '.join([c_upper('genl-' + x) for x in op['flags']])))
1945 cw.write_struct_init(members)
1946 cw.block_end(line=',')
1947 elif family.kernel_policy == 'split':
1948 cb_names = {'do': {'pre': 'pre_doit', 'post': 'post_doit'},
1949 'dump': {'pre': 'start', 'post': 'done'}}
1951 for op_name, op in family.ops.items():
1952 for op_mode in ['do', 'dump']:
1953 if op.is_async or op_mode not in op:
1957 members = [('cmd', op.enum_name)]
1958 if 'dont-validate' in op:
1959 members.append(('validate',
1960 ' | '.join([c_upper('genl-dont-validate-' + x)
1961 for x in op['dont-validate']])), )
1962 name = c_lower(f"{family.name}-nl-{op_name}-{op_mode}it")
1963 if 'pre' in op[op_mode]:
1964 members.append((cb_names[op_mode]['pre'], c_lower(op[op_mode]['pre'])))
1965 members.append((op_mode + 'it', name))
1966 if 'post' in op[op_mode]:
1967 members.append((cb_names[op_mode]['post'], c_lower(op[op_mode]['post'])))
1968 if 'request' in op[op_mode]:
1969 struct = Struct(family, op['attribute-set'],
1970 type_list=op[op_mode]['request']['attributes'])
1973 name = c_lower(f"{family.name}-{op_name}-{op_mode}-nl-policy")
1975 name = c_lower(f"{family.name}-{op_name}-nl-policy")
1976 members.append(('policy', name))
1977 members.append(('maxattr', struct.attr_max_val.enum_name))
1978 flags = (op['flags'] if 'flags' in op else []) + ['cmd-cap-' + op_mode]
1979 members.append(('flags', ' | '.join([c_upper('genl-' + x) for x in flags])))
1980 cw.write_struct_init(members)
1981 cw.block_end(line=',')
1983 cw.block_end(line=';')
1987 def print_kernel_mcgrp_hdr(family, cw):
1988 if not family.mcgrps['list']:
1991 cw.block_start('enum')
1992 for grp in family.mcgrps['list']:
1993 grp_id = c_upper(f"{family.name}-nlgrp-{grp['name']},")
1999 def print_kernel_mcgrp_src(family, cw):
2000 if not family.mcgrps['list']:
2003 cw.block_start('static const struct genl_multicast_group ' + family.name + '_nl_mcgrps[] =')
2004 for grp in family.mcgrps['list']:
2006 grp_id = c_upper(f"{family.name}-nlgrp-{name}")
2007 cw.p('[' + grp_id + '] = { "' + name + '", },')
2012 def print_kernel_family_struct_hdr(family, cw):
2013 if not kernel_can_gen_family_struct(family):
2016 cw.p(f"extern struct genl_family {family.name}_nl_family;")
2020 def print_kernel_family_struct_src(family, cw):
2021 if not kernel_can_gen_family_struct(family):
2024 cw.block_start(f"struct genl_family {family.name}_nl_family __ro_after_init =")
2025 cw.p('.name\t\t= ' + family.fam_key + ',')
2026 cw.p('.version\t= ' + family.ver_key + ',')
2027 cw.p('.netnsok\t= true,')
2028 cw.p('.parallel_ops\t= true,')
2029 cw.p('.module\t\t= THIS_MODULE,')
2030 if family.kernel_policy == 'per-op':
2031 cw.p(f'.ops\t\t= {family.name}_nl_ops,')
2032 cw.p(f'.n_ops\t\t= ARRAY_SIZE({family.name}_nl_ops),')
2033 elif family.kernel_policy == 'split':
2034 cw.p(f'.split_ops\t= {family.name}_nl_ops,')
2035 cw.p(f'.n_split_ops\t= ARRAY_SIZE({family.name}_nl_ops),')
2036 if family.mcgrps['list']:
2037 cw.p(f'.mcgrps\t\t= {family.name}_nl_mcgrps,')
2038 cw.p(f'.n_mcgrps\t= ARRAY_SIZE({family.name}_nl_mcgrps),')
2042 def uapi_enum_start(family, cw, obj, ckey='', enum_name='enum-name'):
2044 if enum_name in obj:
2046 start_line = 'enum ' + c_lower(obj[enum_name])
2047 elif ckey and ckey in obj:
2048 start_line = 'enum ' + family.name + '_' + c_lower(obj[ckey])
2049 cw.block_start(line=start_line)
2052 def render_uapi(family, cw):
2053 hdr_prot = f"_UAPI_LINUX_{family.name.upper()}_H"
2054 cw.p('#ifndef ' + hdr_prot)
2055 cw.p('#define ' + hdr_prot)
2058 defines = [(family.fam_key, family["name"]),
2059 (family.ver_key, family.get('version', 1))]
2060 cw.writes_defines(defines)
2064 for const in family['definitions']:
2065 if const['type'] != 'const':
2066 cw.writes_defines(defines)
2070 # Write kdoc for enum and flags (one day maybe also structs)
2071 if const['type'] == 'enum' or const['type'] == 'flags':
2072 enum = family.consts[const['name']]
2078 doc = ' - ' + enum['doc']
2079 cw.write_doc_line(enum.enum_name + doc)
2080 for entry in enum.entries.values():
2082 doc = '@' + entry.c_name + ': ' + entry['doc']
2083 cw.write_doc_line(doc)
2086 uapi_enum_start(family, cw, const, 'name')
2087 name_pfx = const.get('name-prefix', f"{family.name}-{const['name']}-")
2088 for entry in enum.entries.values():
2090 if entry.value_change:
2091 suffix = f" = {entry.user_value()}" + suffix
2092 cw.p(entry.c_name + suffix)
2094 if const.get('render-max', False):
2096 if const['type'] == 'flags':
2097 max_name = c_upper(name_pfx + 'mask')
2098 max_val = f' = {enum.get_mask()},'
2099 cw.p(max_name + max_val)
2101 max_name = c_upper(name_pfx + 'max')
2102 cw.p('__' + max_name + ',')
2103 cw.p(max_name + ' = (__' + max_name + ' - 1)')
2104 cw.block_end(line=';')
2106 elif const['type'] == 'const':
2107 defines.append([c_upper(family.get('c-define-name',
2108 f"{family.name}-{const['name']}")),
2112 cw.writes_defines(defines)
2115 max_by_define = family.get('max-by-define', False)
2117 for _, attr_set in family.attr_sets.items():
2118 if attr_set.subset_of:
2121 cnt_name = c_upper(family.get('attr-cnt-name', f"__{attr_set.name_prefix}MAX"))
2122 max_value = f"({cnt_name} - 1)"
2125 uapi_enum_start(family, cw, attr_set.yaml, 'enum-name')
2126 for _, attr in attr_set.items():
2128 if attr.value != val:
2129 suffix = f" = {attr.value},"
2132 cw.p(attr.enum_name + suffix)
2134 cw.p(cnt_name + ('' if max_by_define else ','))
2135 if not max_by_define:
2136 cw.p(f"{attr_set.max_name} = {max_value}")
2137 cw.block_end(line=';')
2139 cw.p(f"#define {attr_set.max_name} {max_value}")
2143 separate_ntf = 'async-prefix' in family['operations']
2145 max_name = c_upper(family.get('cmd-max-name', f"{family.op_prefix}MAX"))
2146 cnt_name = c_upper(family.get('cmd-cnt-name', f"__{family.op_prefix}MAX"))
2147 max_value = f"({cnt_name} - 1)"
2149 uapi_enum_start(family, cw, family['operations'], 'enum-name')
2151 for op in family.msgs.values():
2152 if separate_ntf and ('notify' in op or 'event' in op):
2157 suffix = f" = {op.value},"
2159 cw.p(op.enum_name + suffix)
2162 cw.p(cnt_name + ('' if max_by_define else ','))
2163 if not max_by_define:
2164 cw.p(f"{max_name} = {max_value}")
2165 cw.block_end(line=';')
2167 cw.p(f"#define {max_name} {max_value}")
2171 uapi_enum_start(family, cw, family['operations'], enum_name='async-enum')
2172 for op in family.msgs.values():
2173 if separate_ntf and not ('notify' in op or 'event' in op):
2178 suffix = f" = {op['value']},"
2179 cw.p(op.enum_name + suffix)
2180 cw.block_end(line=';')
2185 for grp in family.mcgrps['list']:
2187 defines.append([c_upper(grp.get('c-define-name', f"{family.name}-mcgrp-{name}")),
2191 cw.writes_defines(defines)
2194 cw.p(f'#endif /* {hdr_prot} */')
2197 def _render_user_ntf_entry(ri, op):
2198 ri.cw.block_start(line=f"[{op.enum_name}] = ")
2199 ri.cw.p(f".alloc_sz\t= sizeof({type_name(ri, 'event')}),")
2200 ri.cw.p(f".cb\t\t= {op_prefix(ri, 'reply', deref=True)}_parse,")
2201 ri.cw.p(f".policy\t\t= &{ri.struct['reply'].render_name}_nest,")
2202 ri.cw.p(f".free\t\t= (void *){op_prefix(ri, 'notify')}_free,")
2203 ri.cw.block_end(line=',')
2206 def render_user_family(family, cw, prototype):
2207 symbol = f'const struct ynl_family ynl_{family.c_name}_family'
2209 cw.p(f'extern {symbol};')
2212 ntf = family.has_notifications()
2214 cw.block_start(line=f"static const struct ynl_ntf_info {family['name']}_ntf_info[] = ")
2215 for ntf_op in sorted(family.all_notify.keys()):
2216 op = family.ops[ntf_op]
2217 ri = RenderInfo(cw, family, "user", op, ntf_op, "notify")
2218 for ntf in op['notify']['cmds']:
2219 _render_user_ntf_entry(ri, ntf)
2220 for op_name, op in family.ops.items():
2221 if 'event' not in op:
2223 ri = RenderInfo(cw, family, "user", op, op_name, "event")
2224 _render_user_ntf_entry(ri, op)
2225 cw.block_end(line=";")
2228 cw.block_start(f'{symbol} = ')
2229 cw.p(f'.name\t\t= "{family.name}",')
2231 cw.p(f".ntf_info\t= {family['name']}_ntf_info,")
2232 cw.p(f".ntf_info_size\t= MNL_ARRAY_SIZE({family['name']}_ntf_info),")
2233 cw.block_end(line=';')
2236 def find_kernel_root(full_path):
2239 sub_path = os.path.join(os.path.basename(full_path), sub_path)
2240 full_path = os.path.dirname(full_path)
2241 maintainers = os.path.join(full_path, "MAINTAINERS")
2242 if os.path.exists(maintainers):
2243 return full_path, sub_path[:-1]
2247 parser = argparse.ArgumentParser(description='Netlink simple parsing generator')
2248 parser.add_argument('--mode', dest='mode', type=str, required=True)
2249 parser.add_argument('--spec', dest='spec', type=str, required=True)
2250 parser.add_argument('--header', dest='header', action='store_true', default=None)
2251 parser.add_argument('--source', dest='header', action='store_false')
2252 parser.add_argument('--user-header', nargs='+', default=[])
2253 parser.add_argument('-o', dest='out_file', type=str)
2254 args = parser.parse_args()
2256 out_file = open(args.out_file, 'w+') if args.out_file else os.sys.stdout
2258 if args.header is None:
2259 parser.error("--header or --source is required")
2262 parsed = Family(args.spec)
2263 if parsed.license != '((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)':
2264 print('Spec license:', parsed.license)
2265 print('License must be: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)')
2267 except yaml.YAMLError as exc:
2272 supported_models = ['unified']
2273 if args.mode == 'user':
2274 supported_models += ['directional']
2275 if parsed.msg_id_model not in supported_models:
2276 print(f'Message enum-model {parsed.msg_id_model} not supported for {args.mode} generation')
2279 cw = CodeWriter(BaseNlLib(), out_file)
2281 _, spec_kernel = find_kernel_root(args.spec)
2282 if args.mode == 'uapi' or args.header:
2283 cw.p(f'/* SPDX-License-Identifier: {parsed.license} */')
2285 cw.p(f'// SPDX-License-Identifier: {parsed.license}')
2286 cw.p("/* Do not edit directly, auto-generated from: */")
2287 cw.p(f"/*\t{spec_kernel} */")
2288 cw.p(f"/* YNL-GEN {args.mode} {'header' if args.header else 'source'} */")
2291 if args.mode == 'uapi':
2292 render_uapi(parsed, cw)
2295 hdr_prot = f"_LINUX_{parsed.name.upper()}_GEN_H"
2297 cw.p('#ifndef ' + hdr_prot)
2298 cw.p('#define ' + hdr_prot)
2301 if args.mode == 'kernel':
2302 cw.p('#include <net/netlink.h>')
2303 cw.p('#include <net/genetlink.h>')
2307 cw.p(f'#include "{os.path.basename(args.out_file[:-2])}.h"')
2309 headers = ['uapi/' + parsed.uapi_header]
2311 cw.p('#include <stdlib.h>')
2312 cw.p('#include <string.h>')
2314 cw.p('#include <linux/types.h>')
2316 cw.p(f'#include "{parsed.name}-user.h"')
2317 cw.p('#include "ynl.h"')
2318 headers = [parsed.uapi_header]
2319 for definition in parsed['definitions']:
2320 if 'header' in definition:
2321 headers.append(definition['header'])
2323 cw.p(f"#include <{one}>")
2326 if args.mode == "user":
2328 cw.p("#include <libmnl/libmnl.h>")
2329 cw.p("#include <linux/genetlink.h>")
2331 for one in args.user_header:
2332 cw.p(f'#include "{one}"')
2334 cw.p('struct ynl_sock;')
2336 render_user_family(parsed, cw, True)
2339 if args.mode == "kernel":
2341 for _, struct in sorted(parsed.pure_nested_structs.items()):
2343 cw.p('/* Common nested types */')
2345 for attr_set, struct in sorted(parsed.pure_nested_structs.items()):
2347 print_req_policy_fwd(cw, struct)
2350 if parsed.kernel_policy == 'global':
2351 cw.p(f"/* Global operation policy for {parsed.name} */")
2353 struct = Struct(parsed, parsed.global_policy_set, type_list=parsed.global_policy)
2354 print_req_policy_fwd(cw, struct)
2357 if parsed.kernel_policy in {'per-op', 'split'}:
2358 for op_name, op in parsed.ops.items():
2359 if 'do' in op and 'event' not in op:
2360 ri = RenderInfo(cw, parsed, args.mode, op, op_name, "do")
2361 print_req_policy_fwd(cw, ri.struct['request'], ri=ri)
2364 print_kernel_op_table_hdr(parsed, cw)
2365 print_kernel_mcgrp_hdr(parsed, cw)
2366 print_kernel_family_struct_hdr(parsed, cw)
2368 for _, struct in sorted(parsed.pure_nested_structs.items()):
2370 cw.p('/* Common nested types */')
2372 for attr_set, struct in sorted(parsed.pure_nested_structs.items()):
2374 print_req_policy(cw, struct)
2377 if parsed.kernel_policy == 'global':
2378 cw.p(f"/* Global operation policy for {parsed.name} */")
2380 struct = Struct(parsed, parsed.global_policy_set, type_list=parsed.global_policy)
2381 print_req_policy(cw, struct)
2384 for op_name, op in parsed.ops.items():
2385 if parsed.kernel_policy in {'per-op', 'split'}:
2386 for op_mode in ['do', 'dump']:
2387 if op_mode in op and 'request' in op[op_mode]:
2388 cw.p(f"/* {op.enum_name} - {op_mode} */")
2389 ri = RenderInfo(cw, parsed, args.mode, op, op_name, op_mode)
2390 print_req_policy(cw, ri.struct['request'], ri=ri)
2393 print_kernel_op_table(parsed, cw)
2394 print_kernel_mcgrp_src(parsed, cw)
2395 print_kernel_family_struct_src(parsed, cw)
2397 if args.mode == "user":
2400 put_op_name_fwd(parsed, cw)
2402 for name, const in parsed.consts.items():
2403 if isinstance(const, EnumSet):
2404 put_enum_to_str_fwd(parsed, cw, const)
2407 cw.p('/* Common nested types */')
2408 for attr_set, struct in parsed.pure_nested_structs.items():
2409 ri = RenderInfo(cw, parsed, args.mode, "", "", "", attr_set)
2410 print_type_full(ri, struct)
2412 for op_name, op in parsed.ops.items():
2413 cw.p(f"/* ============== {op.enum_name} ============== */")
2415 if 'do' in op and 'event' not in op:
2416 cw.p(f"/* {op.enum_name} - do */")
2417 ri = RenderInfo(cw, parsed, args.mode, op, op_name, "do")
2419 print_req_type_helpers(ri)
2422 print_rsp_type_helpers(ri)
2424 print_req_prototype(ri)
2428 cw.p(f"/* {op.enum_name} - dump */")
2429 ri = RenderInfo(cw, parsed, args.mode, op, op_name, 'dump')
2430 if 'request' in op['dump']:
2432 print_req_type_helpers(ri)
2433 if not ri.type_consistent:
2435 print_wrapped_type(ri)
2436 print_dump_prototype(ri)
2440 cw.p(f"/* {op.enum_name} - notify */")
2441 ri = RenderInfo(cw, parsed, args.mode, op, op_name, 'notify')
2442 if not ri.type_consistent:
2443 raise Exception(f'Only notifications with consistent types supported ({op.name})')
2444 print_wrapped_type(ri)
2447 ri = RenderInfo(cw, parsed, args.mode, op, op_name, 'event')
2448 cw.p(f"/* {op.enum_name} - event */")
2451 print_wrapped_type(ri)
2455 put_op_name(parsed, cw)
2457 for name, const in parsed.consts.items():
2458 if isinstance(const, EnumSet):
2459 put_enum_to_str(parsed, cw, const)
2462 cw.p('/* Policies */')
2463 for name in parsed.pure_nested_structs:
2464 struct = Struct(parsed, name)
2465 put_typol(cw, struct)
2466 for name in parsed.root_sets:
2467 struct = Struct(parsed, name)
2468 put_typol(cw, struct)
2470 cw.p('/* Common nested types */')
2471 for attr_set, struct in parsed.pure_nested_structs.items():
2472 ri = RenderInfo(cw, parsed, args.mode, "", "", "", attr_set)
2474 free_rsp_nested(ri, struct)
2476 put_req_nested(ri, struct)
2478 parse_rsp_nested(ri, struct)
2480 for op_name, op in parsed.ops.items():
2481 cw.p(f"/* ============== {op.enum_name} ============== */")
2482 if 'do' in op and 'event' not in op:
2483 cw.p(f"/* {op.enum_name} - do */")
2484 ri = RenderInfo(cw, parsed, args.mode, op, op_name, "do")
2492 cw.p(f"/* {op.enum_name} - dump */")
2493 ri = RenderInfo(cw, parsed, args.mode, op, op_name, "dump")
2494 if not ri.type_consistent:
2495 parse_rsp_msg(ri, deref=True)
2496 print_dump_type_free(ri)
2501 cw.p(f"/* {op.enum_name} - notify */")
2502 ri = RenderInfo(cw, parsed, args.mode, op, op_name, 'notify')
2503 if not ri.type_consistent:
2504 raise Exception(f'Only notifications with consistent types supported ({op.name})')
2505 print_ntf_type_free(ri)
2508 cw.p(f"/* {op.enum_name} - event */")
2510 ri = RenderInfo(cw, parsed, args.mode, op, op_name, "do")
2513 ri = RenderInfo(cw, parsed, args.mode, op, op_name, "event")
2514 print_ntf_type_free(ri)
2516 render_user_family(parsed, cw, False)
2519 cw.p(f'#endif /* {hdr_prot} */')
2522 if __name__ == "__main__":