6 from io import StringIO
9 process_imports = False
11 datestring = datetime.datetime.utcfromtimestamp(
12 int(os.environ.get('SOURCE_DATE_EPOCH', time.time())))
13 input_filename = 'inputfil'
14 top_boilerplate = '''\
16 * VLIB API definitions {datestring}
17 * Input file: {input_filename}
18 * Automatically generated: please edit the input file NOT this file!
22 #if defined(vl_msg_id)||defined(vl_union_id) \\
23 || defined(vl_printfun) ||defined(vl_endianfun) \\
24 || defined(vl_api_version)||defined(vl_typedefs) \\
25 || defined(vl_msg_name)||defined(vl_msg_name_crc_list) \\
26 || defined(vl_api_version_tuple)
27 /* ok, something was selected */
29 #warning no content included from {input_filename}
32 #define VL_API_PACKED(x) x __attribute__ ((packed))
35 bottom_boilerplate = '''\
36 /****** API CRC (whole file) *****/
39 vl_api_version({input_filename}, {file_crc:#08x})
48 /****** Message ID / handler enum ******/
54 output += "vl_msg_id(VL_API_%s, vl_api_%s_t_handler)\n" % \
55 (t.name.upper(), t.name)
64 /****** Message names ******/
70 dont_trace = 0 if t.dont_trace else 1
71 output += "vl_msg_name(vl_api_%s_t, %d)\n" % (t.name, dont_trace)
77 def msg_name_crc_list(s, suffix):
80 /****** Message name, crc list ******/
82 #ifdef vl_msg_name_crc_list
84 output += "#define foreach_vl_msg_name_crc_%s " % suffix
87 output += "\\\n_(VL_API_%s, %s, %08x) " % \
88 (t.name.upper(), t.name, t.crc)
95 mappingtable = {'string': 'vl_api_string_t', }
96 if fieldtype in mappingtable:
97 return mappingtable[fieldtype]
101 def typedefs(filename):
105 /****** Typedefs ******/
108 #include "{include}.api_types.h"
110 '''.format(include=filename)
114 format_strings = {'u8': '%u',
125 noprint_fields = {'_vl_msg_id': None,
126 'client_index': None,
133 def __init__(self, stream):
136 def print_string(self, o, stream):
139 write(' if (vl_api_string_len(&a->{f}) > 0) {{\n'
140 .format(f=o.fieldname))
141 write(' s = format(s, "\\n%U{f}: %U", '
142 'format_white_space, indent, '
143 'vl_api_format_string, (&a->{f}));\n'.format(f=o.fieldname))
145 write(' s = format(s, "\\n%U{f}:", '
146 'format_white_space, indent);\n'.format(f=o.fieldname))
149 write(' s = format(s, "\\n%U{f}: %s", '
150 'format_white_space, indent, a->{f});\n'
151 .format(f=o.fieldname))
153 def print_field(self, o, stream):
155 if o.fieldname in noprint_fields:
157 if o.fieldtype in format_strings:
158 f = format_strings[o.fieldtype]
159 write(' s = format(s, "\\n%U{n}: {f}", '
160 'format_white_space, indent, a->{n});\n'
161 .format(n=o.fieldname, f=f))
163 write(' s = format(s, "\\n%U{n}: %U", '
164 'format_white_space, indent, '
165 'format_{t}, &a->{n}, indent);\n'
166 .format(n=o.fieldname, t=o.fieldtype))
168 _dispatch['Field'] = print_field
170 def print_array(self, o, stream):
174 for (i = 0; i < {lfield}; i++) {{
175 s = format(s, "\\n%U{n}: %U",
176 format_white_space, indent, format_{t}, &a->{n}[i], indent);
180 forloop_format = '''\
181 for (i = 0; i < {lfield}; i++) {{
182 s = format(s, "\\n%U{n}: {t}",
183 format_white_space, indent, a->{n}[i]);
187 if o.fieldtype == 'string':
188 return self.print_string(o, stream)
190 if o.fieldtype == 'u8':
192 write(' s = format(s, "\\n%U{n}: %U", format_white_space, '
193 'indent, format_hex_bytes, a->{n}, a->{lfield});\n'
194 .format(n=o.fieldname, lfield=o.lengthfield))
196 write(' s = format(s, "\\n%U{n}: %U", format_white_space, '
197 'indent, format_hex_bytes, a, {lfield});\n'
198 .format(n=o.fieldname, lfield=o.length))
201 lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
202 if o.fieldtype in format_strings:
203 write(forloop_format.format(lfield=lfield,
204 t=format_strings[o.fieldtype],
207 write(forloop.format(lfield=lfield, t=o.fieldtype, n=o.fieldname))
209 _dispatch['Array'] = print_array
211 def print_alias(self, k, v, stream):
213 if ('length' in v.alias and v.alias['length'] and
214 v.alias['type'] == 'u8'):
215 write(' return format(s, "%U", format_hex_bytes, a, {});\n'
216 .format(v.alias['length']))
217 elif v.alias['type'] in format_strings:
218 write(' return format(s, "{}", *a);\n'
219 .format(format_strings[v.alias['type']]))
221 write(' return format(s, "{} (print not implemented)");\n'
224 def print_enum(self, o, stream):
226 write(" switch(*a) {\n")
228 write(" case %s:\n" % b[1])
229 write(' return format(s, "{}");\n'.format(b[0]))
232 _dispatch['Enum'] = print_enum
234 def print_obj(self, o, stream):
237 if o.type in self._dispatch:
238 self._dispatch[o.type](self, o, stream)
240 write(' s = format(s, "\\n{} {} {} (print not implemented");\n'
241 .format(o.type, o.fieldtype, o.fieldname))
244 def printfun(objs, stream, modulename):
248 /****** Print functions *****/
250 #ifndef included_{module}_printfun
251 #define included_{module}_printfun
254 #define _uword_fmt \"%lld\"
255 #define _uword_cast (long long)
257 #define _uword_fmt \"%ld\"
258 #define _uword_cast long
264 static inline void *vl_api_{name}_t_print (vl_api_{name}_t *a, void *handle)
267 u32 indent __attribute__((unused)) = 2;
268 int i __attribute__((unused));
271 h = h.format(module=modulename)
274 pp = Printfun(stream)
277 write("/***** manual: vl_api_%s_t_print *****/\n\n" % t.name)
279 write(signature.format(name=t.name))
280 write(' /* Message definition: vl_api_{}_t: */\n'.format(t.name))
281 write(" s = format(s, \"vl_api_%s_t:\");\n" % t.name)
283 pp.print_obj(o, stream)
284 write(' vec_add1(s, 0);\n')
285 write(' vl_print (handle, (char *)s);\n')
286 write(' vec_free (s);\n')
287 write(' return handle;\n')
291 write("\n#endif /* vl_printfun */\n")
296 def printfun_types(objs, stream, modulename):
298 pp = Printfun(stream)
301 /****** Print functions *****/
303 #ifndef included_{module}_printfun_types
304 #define included_{module}_printfun_types
307 h = h.format(module=modulename)
311 static inline u8 *format_vl_api_{name}_t (u8 *s, va_list * args)
313 vl_api_{name}_t *a = va_arg (*args, vl_api_{name}_t *);
314 u32 indent __attribute__((unused)) = va_arg (*args, u32);
315 int i __attribute__((unused));
320 if t.__class__.__name__ == 'Enum':
321 write(signature.format(name=t.name))
322 pp.print_enum(t.block, stream)
323 write(' return s;\n')
328 write("/***** manual: vl_api_%s_t_print *****/\n\n" % t.name)
331 if t.__class__.__name__ == 'Using':
332 write(signature.format(name=t.name))
333 pp.print_alias(t.name, t, stream)
337 write(signature.format(name=t.name))
339 pp.print_obj(o, stream)
341 write(' return s;\n')
345 write("\n#endif /* vl_printfun_types */\n")
348 def imports(imports):
349 output = '/* Imported API files */\n'
350 output += '#ifndef vl_api_version\n'
353 s = i.filename.replace('plugins/', '')
354 output += '#include <{}.h>\n'.format(s)
360 'u16': 'clib_net_to_host_u16',
361 'u32': 'clib_net_to_host_u32',
362 'u64': 'clib_net_to_host_u64',
363 'i16': 'clib_net_to_host_i16',
364 'i32': 'clib_net_to_host_i32',
365 'i64': 'clib_net_to_host_i64',
366 'f64': 'clib_net_to_host_f64',
370 def endianfun_array(o):
372 for (i = 0; i < {length}; i++) {{
373 a->{name}[i] = {format}(a->{name}[i]);
377 forloop_format = '''\
378 for (i = 0; i < {length}; i++) {{
379 {type}_endian(&a->{name}[i]);
384 if o.fieldtype == 'u8' or o.fieldtype == 'string':
385 output += ' /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname)
387 lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
388 if o.fieldtype in endian_strings:
390 .format(length=lfield,
391 format=endian_strings[o.fieldtype],
394 output += (forloop_format
395 .format(length=lfield, type=o.fieldtype,
399 no_endian_conversion = {'client_index': None}
401 def endianfun_obj(o):
403 if o.type == 'Array':
404 return endianfun_array(o)
405 elif o.type != 'Field':
406 output += (' s = format(s, "\\n{} {} {} (print not implemented");\n'
407 .format(o.type, o.fieldtype, o.fieldname))
409 if o.fieldname in no_endian_conversion:
410 output += ' /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname)
412 if o.fieldtype in endian_strings:
413 output += (' a->{name} = {format}(a->{name});\n'
414 .format(name=o.fieldname,
415 format=endian_strings[o.fieldtype]))
416 elif o.fieldtype.startswith('vl_api_'):
417 output += (' {type}_endian(&a->{name});\n'
418 .format(type=o.fieldtype, name=o.fieldname))
420 output += ' /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname)
425 def endianfun(objs, modulename):
428 /****** Endian swap functions *****/\n\
430 #ifndef included_{module}_endianfun
431 #define included_{module}_endianfun
433 #undef clib_net_to_host_uword
435 #define clib_net_to_host_uword clib_net_to_host_u64
437 #define clib_net_to_host_uword clib_net_to_host_u32
441 output = output.format(module=modulename)
444 static inline void vl_api_{name}_t_endian (vl_api_{name}_t *a)
446 int i __attribute__((unused));
450 if t.__class__.__name__ == 'Enum':
451 output += signature.format(name=t.name)
452 if t.enumtype in endian_strings:
453 output += (' *a = {}(*a);\n'
454 .format(endian_strings[t.enumtype]))
456 output += (' /* a->{name} = a->{name} (no-op) */\n'
457 .format(name=t.name))
463 output += "/***** manual: vl_api_%s_t_endian *****/\n\n" % t.name
466 if t.__class__.__name__ == 'Using':
467 output += signature.format(name=t.name)
468 if ('length' in t.alias and t.alias['length'] and
469 t.alias['type'] == 'u8'):
470 output += (' /* a->{name} = a->{name} (no-op) */\n'
471 .format(name=t.name))
472 elif t.alias['type'] in format_strings:
473 output += (' *a = {}(*a);\n'
474 .format(endian_strings[t.alias['type']]))
476 output += ' /* Not Implemented yet {} */'.format(t.name)
480 output += signature.format(name=t.name)
483 output += endianfun_obj(o)
487 output += "\n#endif /* vl_endianfun */\n\n"
492 def version_tuple(s, module):
494 /****** Version tuple *****/
496 #ifdef vl_api_version_tuple
499 if 'version' in s['Option']:
500 v = s['Option']['version']
501 (major, minor, patch) = v.split('.')
502 output += "vl_api_version_tuple(%s, %s, %s, %s)\n" % \
503 (module, major, minor, patch)
505 output += "\n#endif /* vl_api_version_tuple */\n\n"
510 def generate_include_enum(s, module, stream):
514 write('typedef enum {\n')
515 for t in s['Define']:
516 write(' VL_API_{},\n'.format(t.name.upper()))
517 write(' VL_MSG_{}_LAST\n'.format(module.upper()))
518 write('}} vl_api_{}_enum_t;\n'.format(module))
521 def generate_include_counters(s, module, stream):
525 csetname = counters.name
526 write('typedef enum {\n')
527 for c in counters.block:
528 write(' {}_ERROR_{},\n'
529 .format(csetname.upper(), c['name'].upper()))
530 write(' {}_N_ERROR\n'.format(csetname.upper()))
531 write('}} vl_counter_{}_enum_t;\n'.format(csetname))
533 # write('extern char *{}_error_strings[];\n'.format(csetname))
534 # write('extern char *{}_description_strings[];\n'.format(csetname))
535 write('extern vl_counter_t {}_error_counters[];\n'.format(csetname))
538 # Generate separate API _types file.
540 def generate_include_types(s, module, stream):
543 write('#ifndef included_{module}_api_types_h\n'.format(module=module))
544 write('#define included_{module}_api_types_h\n'.format(module=module))
546 if 'version' in s['Option']:
547 v = s['Option']['version']
548 (major, minor, patch) = v.split('.')
549 write('#define VL_API_{m}_API_VERSION_MAJOR {v}\n'.format(m=module.upper(), v=major))
550 write('#define VL_API_{m}_API_VERSION_MINOR {v}\n'.format(m=module.upper(), v=minor))
551 write('#define VL_API_{m}_API_VERSION_PATCH {v}\n'.format(m=module.upper(), v=patch))
554 write('/* Imported API files */\n')
555 for i in s['Import']:
556 filename = i.filename.replace('plugins/', '')
557 write('#include <{}_types.h>\n'.format(filename))
559 for o in s['types'] + s['Define']:
560 tname = o.__class__.__name__
562 if 'length' in o.alias:
563 write('typedef %s vl_api_%s_t[%s];\n' % (o.alias['type'], o.name, o.alias['length']))
565 write('typedef %s vl_api_%s_t;\n' % (o.alias['type'], o.name))
566 elif tname == 'Enum':
567 if o.enumtype == 'u32':
568 write("typedef enum {\n")
570 write("typedef enum __attribute__((packed)) {\n")
573 write(" %s = %s,\n" % (b[0], b[1]))
574 write('} vl_api_%s_t;\n' % o.name)
575 if o.enumtype != 'u32':
576 size1 = 'sizeof(vl_api_%s_t)' % o.name
577 size2 = 'sizeof(%s)' % o.enumtype
578 err_str = 'size of API enum %s is wrong' % o.name
579 write('STATIC_ASSERT(%s == %s, "%s");\n'
580 % (size1, size2, err_str))
583 write("typedef union __attribute__ ((packed)) _vl_api_%s {\n" % o.name)
585 write(("typedef struct __attribute__ ((packed)) _vl_api_%s {\n")
588 if b.type == 'Option':
590 if b.type == 'Field':
591 write(" %s %s;\n" % (api2c(b.fieldtype),
593 elif b.type == 'Array':
595 write(" %s %s[0];\n" % (api2c(b.fieldtype),
598 # Fixed length strings decay to nul terminated u8
599 if b.fieldtype == 'string':
602 .format(api2c(b.fieldtype),
605 write(' u8 {}[{}];\n'
606 .format(b.fieldname, b.length))
608 write(" %s %s[%s];\n" %
609 (api2c(b.fieldtype), b.fieldname,
612 raise ValueError("Error in processing type {} for {}"
615 write('} vl_api_%s_t;\n' % o.name)
617 for t in s['Define']:
618 write('#define VL_API_{ID}_CRC "{n}_{crc:08x}"\n'
619 .format(n=t.name, ID=t.name.upper(), crc=t.crc))
624 def generate_c_boilerplate(services, defines, counters, file_crc,
627 define_hash = {d.name: d for d in defines}
630 #define vl_endianfun /* define message structures */
631 #include "{module}.api.h"
634 /* instantiate all the print functions we know about */
635 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
637 #include "{module}.api.h"
642 write(hdr.format(module=module))
643 write('static u16\n')
644 write('setup_message_id_table (void) {\n')
645 write(' api_main_t *am = my_api_main;\n')
646 write(' vl_msg_api_msg_config_t c;\n')
647 write(' u16 msg_id_base = vl_msg_api_get_msg_ids ("{}_{crc:08x}", VL_MSG_{m}_LAST);\n'
648 .format(module, crc=file_crc, m=module.upper()))
652 write(' vl_msg_api_add_msg_name_crc (am, "{n}_{crc:08x}",\n'
653 ' VL_API_{ID} + msg_id_base);\n'
654 .format(n=d.name, ID=d.name.upper(), crc=d.crc))
656 d = define_hash[s.caller]
657 write(' c = (vl_msg_api_msg_config_t) {{.id = VL_API_{ID} + msg_id_base,\n'
659 ' .handler = vl_api_{n}_t_handler,\n'
660 ' .cleanup = vl_noop_handler,\n'
661 ' .endian = vl_api_{n}_t_endian,\n'
662 ' .print = vl_api_{n}_t_print,\n'
663 ' .is_autoendian = 0}};\n'
664 .format(n=s.caller, ID=s.caller.upper()))
665 write(' vl_msg_api_config (&c);\n')
667 d = define_hash[s.reply]
668 write(' c = (vl_msg_api_msg_config_t) {{.id = VL_API_{ID} + msg_id_base,\n'
671 ' .cleanup = vl_noop_handler,\n'
672 ' .endian = vl_api_{n}_t_endian,\n'
673 ' .print = vl_api_{n}_t_print,\n'
674 ' .is_autoendian = 0}};\n'
675 .format(n=s.reply, ID=s.reply.upper()))
676 write(' vl_msg_api_config (&c);\n')
680 write(' return msg_id_base;\n')
683 severity = {'error': 'VL_COUNTER_SEVERITY_ERROR',
684 'info': 'VL_COUNTER_SEVERITY_INFO',
685 'warn': 'VL_COUNTER_SEVERITY_WARN'}
690 write('char *{}_error_strings[] = {{\n'.format(csetname))
692 write(' "{}",\n'.format(c['name']))
694 write('char *{}_description_strings[] = {{\n'.format(csetname))
696 write(' "{}",\n'.format(c['description']))
699 write('vl_counter_t {}_error_counters[] = {{\n'.format(csetname))
702 write(' .name = "{}",\n'.format(c['name']))
703 write(' .desc = "{}",\n'.format(c['description']))
704 write(' .severity = {},\n'.format(severity[c['severity']]))
708 def generate_c_test_boilerplate(services, defines, file_crc, module, plugin, stream):
711 define_hash = {d.name:d for d in defines}
715 #define vl_endianfun /* define message structures */
716 #include "{module}.api.h"
719 /* instantiate all the print functions we know about */
720 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
722 #include "{module}.api.h"
727 write(hdr.format(module=module))
730 d = define_hash[s.reply]
734 write('/* Manual definition requested for: vl_api_{n}_t_handler() */\n'
737 if not define_hash[s.caller].autoreply:
738 write('/* Only autoreply is supported (vl_api_{n}_t_handler()) */\n'
741 write('#ifndef VL_API_{n}_T_HANDLER\n'.format(n=s.reply.upper()))
742 write('static void\n')
743 write('vl_api_{n}_t_handler (vl_api_{n}_t * mp) {{\n'.format(n=s.reply))
744 write(' vat_main_t * vam = {}_test_main.vat_main;\n'.format(module))
745 write(' i32 retval = ntohl(mp->retval);\n')
746 write(' if (vam->async_mode) {\n')
747 write(' vam->async_errors += (retval < 0);\n')
749 write(' vam->retval = retval;\n')
750 write(' vam->result_ready = 1;\n')
756 if define_hash[e].manual_print:
758 write('static void\n')
759 write('vl_api_{n}_t_handler (vl_api_{n}_t * mp) {{\n'.format(n=e))
760 write(' vl_print(0, "{n} event called:");\n'.format(n=e))
761 write(' vl_api_{n}_t_print(mp, 0);\n'.format(n=e))
764 write('static void\n')
765 write('setup_message_id_table (vat_main_t * vam, u16 msg_id_base) {\n')
767 write(' vl_msg_api_set_handlers(VL_API_{ID} + msg_id_base, "{n}",\n'
768 ' vl_api_{n}_t_handler, vl_noop_handler,\n'
769 ' vl_api_{n}_t_endian, vl_api_{n}_t_print,\n'
770 ' sizeof(vl_api_{n}_t), 1);\n'
771 .format(n=s.reply, ID=s.reply.upper()))
772 write(' hash_set_mem (vam->function_by_name, "{n}", api_{n});\n'.format(n=s.caller))
774 write(' hash_set_mem (vam->help_by_name, "{n}", "{help}");\n'
775 .format(n=s.caller, help=define_hash[s.caller].options['vat_help']))
781 write(' vl_msg_api_set_handlers(VL_API_{ID} + msg_id_base, "{n}",\n'
782 ' vl_api_{n}_t_handler, vl_noop_handler,\n'
783 ' vl_api_{n}_t_endian, vl_api_{n}_t_print,\n'
784 ' sizeof(vl_api_{n}_t), 1);\n'
785 .format(n=e, ID=e.upper()))
789 write('clib_error_t * vat_plugin_register (vat_main_t *vam)\n')
791 write('clib_error_t * vat_{}_plugin_register (vat_main_t *vam)\n'.format(module))
793 write(' {n}_test_main_t * mainp = &{n}_test_main;\n'.format(n=module))
794 write(' mainp->vat_main = vam;\n')
795 write(' mainp->msg_id_base = vl_client_get_first_plugin_msg_id ("{n}_{crc:08x}");\n'
796 .format(n=module, crc=file_crc))
797 write(' if (mainp->msg_id_base == (u16) ~0)\n')
798 write(' return clib_error_return (0, "{} plugin not loaded...");\n'.format(module))
799 write(' setup_message_id_table (vam, mainp->msg_id_base);\n')
800 write('#ifdef VL_API_LOCAL_SETUP_MESSAGE_ID_TABLE\n')
801 write(' VL_API_LOCAL_SETUP_MESSAGE_ID_TABLE(vam);\n')
803 write(' return 0;\n')
809 def run(args, input_filename, s):
812 if not args.outputdir:
813 sys.stderr.write('Missing --outputdir argument')
816 basename = os.path.basename(input_filename)
817 filename, file_extension = os.path.splitext(basename)
818 modulename = filename.replace('.', '_')
819 filename_enum = os.path.join(args.outputdir + '/' + basename + '_enum.h')
820 filename_types = os.path.join(args.outputdir + '/' + basename + '_types.h')
821 filename_c = os.path.join(args.outputdir + '/' + basename + '.c')
822 filename_c_test = os.path.join(args.outputdir + '/' + basename + '_test.c')
824 # Generate separate types file
826 generate_include_types(s, modulename, st)
827 with open (filename_types, 'w') as fd:
829 shutil.copyfileobj (st, fd)
832 # Generate separate enum file
834 st.write('#ifndef included_{}_api_enum_h\n'.format(modulename))
835 st.write('#define included_{}_api_enum_h\n'.format(modulename))
836 generate_include_enum(s, modulename, st)
837 generate_include_counters(s['Counters'], modulename, st)
839 with open (filename_enum, 'w') as fd:
841 shutil.copyfileobj (st, fd)
844 # Generate separate C file
846 generate_c_boilerplate(s['Service'], s['Define'], s['Counters'],
847 s['file_crc'], modulename, st)
848 with open (filename_c, 'w') as fd:
850 shutil.copyfileobj(st, fd)
853 # Generate separate C test file
855 plugin = True if 'plugin' in input_filename else False
856 generate_c_test_boilerplate(s['Service'], s['Define'], s['file_crc'],
857 modulename, plugin, st)
858 with open (filename_c_test, 'w') as fd:
860 shutil.copyfileobj(st, fd)
863 output = top_boilerplate.format(datestring=datestring,
864 input_filename=basename)
865 output += imports(s['Import'])
867 output += msg_names(s)
868 output += msg_name_crc_list(s, filename)
869 output += typedefs(modulename)
870 printfun_types(s['types'], stream, modulename)
871 printfun(s['Define'], stream, modulename)
872 output += stream.getvalue()
874 output += endianfun(s['types'] + s['Define'], modulename)
875 output += version_tuple(s, basename)
876 output += bottom_boilerplate.format(input_filename=basename,
877 file_crc=s['file_crc'])