import time
import sys
from io import StringIO
+import shutil
datestring = datetime.datetime.utcfromtimestamp(
int(os.environ.get('SOURCE_DATE_EPOCH', time.time())))
return fieldtype
-def typedefs(objs, aliases, filename):
- name = filename.replace('.', '_')
- output = '''\
+def typedefs(filename):
+ output = '''\
/****** Typedefs ******/
#ifdef vl_typedefs
-#ifndef included_{module}_typedef
-#define included_{module}_typedef
-'''
- output = output.format(module=name)
-
- for k, v in aliases.items():
- if 'length' in v.alias:
- output += ('typedef %s vl_api_%s_t[%s];\n'
- % (v.alias['type'], k, v.alias['length']))
- else:
- output += 'typedef %s vl_api_%s_t;\n' % (v.alias['type'], k)
-
- for o in objs:
- tname = o.__class__.__name__
- if tname == 'Enum':
- if o.enumtype == 'u32':
- output += "typedef enum {\n"
- else:
- output += "typedef enum __attribute__((__packed__)) {\n"
-
- for b in o.block:
- output += " %s = %s,\n" % (b[0], b[1])
- output += '} vl_api_%s_t;\n' % o.name
- if o.enumtype != 'u32':
- size1 = 'sizeof(vl_api_%s_t)' % o.name
- size2 = 'sizeof(%s)' % o.enumtype
- err_str = 'size of API enum %s is wrong' % o.name
- output += ('STATIC_ASSERT(%s == %s, "%s");\n'
- % (size1, size2, err_str))
- else:
- if tname == 'Union':
- output += "typedef VL_API_PACKED(union _vl_api_%s {\n" % o.name
- else:
- output += ("typedef VL_API_PACKED(struct _vl_api_%s {\n"
- % o.name)
- for b in o.block:
- if b.type == 'Option':
- continue
- if b.type == 'Field':
- output += " %s %s;\n" % (api2c(b.fieldtype),
- b.fieldname)
- elif b.type == 'Array':
- if b.lengthfield:
- output += " %s %s[0];\n" % (api2c(b.fieldtype),
- b.fieldname)
- else:
- # Fixed length strings decay to nul terminated u8
- if b.fieldtype == 'string':
- if b.modern_vla:
- output += (' {} {};\n'
- .format(api2c(b.fieldtype),
- b.fieldname))
- else:
- output += (' u8 {}[{}];\n'
- .format(b.fieldname, b.length))
- else:
- output += (" %s %s[%s];\n" %
- (api2c(b.fieldtype), b.fieldname,
- b.length))
- else:
- raise ValueError("Error in processing type {} for {}"
- .format(b, o.name))
-
- output += '}) vl_api_%s_t;\n' % o.name
-
- output += "\n#endif"
- output += "\n#endif\n\n"
-
+#include "{include}.api_types.h"
+#endif
+'''.format(include=filename)
return output
write(' return format(s, "{}", *a);\n'
.format(format_strings[v.alias['type']]))
else:
- write(' return format(s, "{} (print not implemented)"'
+ write(' return format(s, "{} (print not implemented)");\n'
.format(k))
def print_enum(self, o, stream):
return ''
-def printfun_types(objs, aliases, stream, modulename):
+def printfun_types(objs, stream, modulename):
write = stream.write
pp = Printfun(stream)
indent += 2;
'''
- for k, v in aliases.items():
- if v.manual_print:
- write("/***** manual: vl_api_%s_t_print *****/\n\n" % k)
- continue
-
- write(signature.format(name=k))
- pp.print_alias(k, v, stream)
- write('}\n\n')
-
for t in objs:
if t.__class__.__name__ == 'Enum':
write(signature.format(name=t.name))
write("/***** manual: vl_api_%s_t_print *****/\n\n" % t.name)
continue
+ if t.__class__.__name__ == 'Using':
+ write(signature.format(name=t.name))
+ pp.print_alias(t.name, t, stream)
+ write('}\n\n')
+ continue
+
write(signature.format(name=t.name))
for o in t.block:
pp.print_obj(o, stream)
'u16': 'clib_net_to_host_u16',
'u32': 'clib_net_to_host_u32',
'u64': 'clib_net_to_host_u64',
- 'i16': 'clib_net_to_host_u16',
- 'i32': 'clib_net_to_host_u32',
- 'i64': 'clib_net_to_host_u64',
- 'f64': 'clib_net_to_host_u64',
+ 'i16': 'clib_net_to_host_i16',
+ 'i32': 'clib_net_to_host_i32',
+ 'i64': 'clib_net_to_host_i64',
+ 'f64': 'clib_net_to_host_f64',
}
return output
-def endianfun(objs, aliases, modulename):
+def endianfun(objs, modulename):
output = '''\
/****** Endian swap functions *****/\n\
int i __attribute__((unused));
'''
- for k, v in aliases.items():
- if v.manual_endian:
- output += "/***** manual: vl_api_%s_t_endian *****/\n\n" % k
- continue
-
- output += signature.format(name=k)
- if ('length' in v.alias and v.alias['length'] and
- v.alias['type'] == 'u8'):
- output += (' /* a->{name} = a->{name} (no-op) */\n'
- .format(name=k))
- elif v.alias['type'] in format_strings:
- output += (' *a = {}(*a);\n'
- .format(endian_strings[v.alias['type']]))
- else:
- output += ' /* Not Implemented yet {} */'.format(k)
- output += '}\n\n'
-
for t in objs:
if t.__class__.__name__ == 'Enum':
output += signature.format(name=t.name)
output += "/***** manual: vl_api_%s_t_endian *****/\n\n" % t.name
continue
+
+ if t.__class__.__name__ == 'Using':
+ output += signature.format(name=t.name)
+ if ('length' in t.alias and t.alias['length'] and
+ t.alias['type'] == 'u8'):
+ output += (' /* a->{name} = a->{name} (no-op) */\n'
+ .format(name=t.name))
+ elif t.alias['type'] in format_strings:
+ output += (' *a = {}(*a);\n'
+ .format(endian_strings[t.alias['type']]))
+ else:
+ output += ' /* Not Implemented yet {} */'.format(t.name)
+ output += '}\n\n'
+ continue
+
output += signature.format(name=t.name)
for o in t.block:
return output
+def generate_include_enum(s, module, stream):
+ write = stream.write
+
+ if len(s['Define']):
+ write('typedef enum {\n')
+ for t in s['Define']:
+ write(' VL_API_{},\n'.format(t.name.upper()))
+ write(' VL_MSG_FIRST_AVAILABLE\n')
+ write('}} vl_api_{}_enum_t;\n'.format(module))
+
+#
+# Generate separate API _types file.
+#
+def generate_include_types(s, module, stream):
+ write = stream.write
+
+ write('#ifndef included_{module}_api_types_h\n'.format(module=module))
+ write('#define included_{module}_api_types_h\n'.format(module=module))
+
+ if len(s['Import']):
+ write('/* Imported API files */\n')
+ for i in s['Import']:
+ filename = i.filename.replace('plugins/', '')
+ write('#include <{}_types.h>\n'.format(filename))
+
+ for o in s['types'] + s['Define']:
+ tname = o.__class__.__name__
+ if tname == 'Using':
+ if 'length' in o.alias:
+ write('typedef %s vl_api_%s_t[%s];\n' % (o.alias['type'], o.name, o.alias['length']))
+ else:
+ write('typedef %s vl_api_%s_t;\n' % (o.alias['type'], o.name))
+ elif tname == 'Enum':
+ if o.enumtype == 'u32':
+ write("typedef enum {\n")
+ else:
+ write("typedef enum __attribute__((packed)) {\n")
+
+ for b in o.block:
+ write(" %s = %s,\n" % (b[0], b[1]))
+ write('} vl_api_%s_t;\n' % o.name)
+ if o.enumtype != 'u32':
+ size1 = 'sizeof(vl_api_%s_t)' % o.name
+ size2 = 'sizeof(%s)' % o.enumtype
+ err_str = 'size of API enum %s is wrong' % o.name
+ write('STATIC_ASSERT(%s == %s, "%s");\n'
+ % (size1, size2, err_str))
+ else:
+ if tname == 'Union':
+ write("typedef union __attribute__ ((packed)) _vl_api_%s {\n" % o.name)
+ else:
+ write(("typedef struct __attribute__ ((packed)) _vl_api_%s {\n")
+ % o.name)
+ for b in o.block:
+ if b.type == 'Option':
+ continue
+ if b.type == 'Field':
+ write(" %s %s;\n" % (api2c(b.fieldtype),
+ b.fieldname))
+ elif b.type == 'Array':
+ if b.lengthfield:
+ write(" %s %s[0];\n" % (api2c(b.fieldtype),
+ b.fieldname))
+ else:
+ # Fixed length strings decay to nul terminated u8
+ if b.fieldtype == 'string':
+ if b.modern_vla:
+ write(' {} {};\n'
+ .format(api2c(b.fieldtype),
+ b.fieldname))
+ else:
+ write(' u8 {}[{}];\n'
+ .format(b.fieldname, b.length))
+ else:
+ write(" %s %s[%s];\n" %
+ (api2c(b.fieldtype), b.fieldname,
+ b.length))
+ else:
+ raise ValueError("Error in processing type {} for {}"
+ .format(b, o.name))
+
+ write('} vl_api_%s_t;\n' % o.name)
+
+ write("\n#endif\n")
+
+
+def generate_c_boilerplate(services, defines, file_crc, module, stream):
+ write = stream.write
+
+ hdr = '''\
+#define vl_endianfun /* define message structures */
+#include "{module}.api.h"
+#undef vl_endianfun
+
+/* instantiate all the print functions we know about */
+#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
+#define vl_printfun
+#include "{module}.api.h"
+#undef vl_printfun
+
+'''
+
+ write(hdr.format(module=module))
+ write('static u16\n')
+ write('setup_message_id_table (void) {\n')
+ write(' api_main_t *am = &api_main;\n')
+ write(' u16 msg_id_base = vl_msg_api_get_msg_ids ("{}_{crc:08x}", VL_MSG_FIRST_AVAILABLE);\n'
+ .format(module, crc=file_crc))
+
+
+ for d in defines:
+ write(' vl_msg_api_add_msg_name_crc (am, "{n}_{crc:08x}",\n'
+ ' VL_API_{ID} + msg_id_base);\n'
+ .format(n=d.name, ID=d.name.upper(), crc=d.crc))
+ for s in services:
+ write(' vl_msg_api_set_handlers(VL_API_{ID} + msg_id_base, "{n}",\n'
+ ' vl_api_{n}_t_handler, vl_noop_handler,\n'
+ ' vl_api_{n}_t_endian, vl_api_{n}_t_print,\n'
+ ' sizeof(vl_api_{n}_t), 1);\n'
+ .format(n=s.caller, ID=s.caller.upper()))
+
+ write(' return msg_id_base;\n')
+ write('}\n')
+
+
+def generate_c_test_plugin_boilerplate(services, defines, file_crc, module, stream):
+ write = stream.write
+
+ define_hash = {d.name:d for d in defines}
+ replies = {}
+
+ hdr = '''\
+#define vl_endianfun /* define message structures */
+#include "{module}.api.h"
+#undef vl_endianfun
+
+/* instantiate all the print functions we know about */
+#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
+#define vl_printfun
+#include "{module}.api.h"
+#undef vl_printfun
+
+'''
+
+ write(hdr.format(module=module))
+ for s in services:
+ try:
+ d = define_hash[s.reply]
+ except:
+ continue
+ if d.manual_print:
+ write('/* Manual definition requested for: vl_api_{n}_t_hander() */\n'
+ .format(n=s.reply))
+ continue
+ if not define_hash[s.caller].autoreply:
+ write('/* Only autoreply is supported (vl_api_{n}_t_hander()) */\n'
+ .format(n=s.reply))
+ continue
+ write('#ifndef VL_API_{n}_T_HANLDER\n'.format(n=s.reply.upper()))
+ write('static void\n')
+ write('vl_api_{n}_t_handler (vl_api_{n}_t * mp) {{\n'.format(n=s.reply))
+ write(' vat_main_t * vam = {}_test_main.vat_main;\n'.format(module))
+ write(' i32 retval = ntohl(mp->retval);\n')
+ write(' if (vam->async_mode) {\n')
+ write(' vam->async_errors += (retval < 0);\n')
+ write(' } else {\n')
+ write(' vam->retval = retval;\n')
+ write(' vam->result_ready = 1;\n')
+ write(' }\n')
+ write('}\n')
+ write('#endif\n')
+
+ for e in s.events:
+ if define_hash[e].manual_print:
+ continue
+ write('static void\n')
+ write('vl_api_{n}_t_handler (vl_api_{n}_t * mp) {{\n'.format(n=e))
+ write(' vl_print(0, "{n} event called:");\n'.format(n=e))
+ write(' vl_api_{n}_t_print(mp, 0);\n'.format(n=e))
+ write('}\n')
+
+ write('static void\n')
+ write('setup_message_id_table (vat_main_t * vam, u16 msg_id_base) {\n')
+ for s in services:
+ write(' vl_msg_api_set_handlers(VL_API_{ID} + msg_id_base, "{n}",\n'
+ ' vl_api_{n}_t_handler, vl_noop_handler,\n'
+ ' vl_api_{n}_t_endian, vl_api_{n}_t_print,\n'
+ ' sizeof(vl_api_{n}_t), 1);\n'
+ .format(n=s.reply, ID=s.reply.upper()))
+ write(' hash_set_mem (vam->function_by_name, "{n}", api_{n});\n'.format(n=s.caller))
+ try:
+ write(' hash_set_mem (vam->help_by_name, "{n}", "{help}");\n'
+ .format(n=s.caller, help=define_hash[s.caller].options['vat_help']))
+ except:
+ pass
+
+ # Events
+ for e in s.events:
+ write(' vl_msg_api_set_handlers(VL_API_{ID} + msg_id_base, "{n}",\n'
+ ' vl_api_{n}_t_handler, vl_noop_handler,\n'
+ ' vl_api_{n}_t_endian, vl_api_{n}_t_print,\n'
+ ' sizeof(vl_api_{n}_t), 1);\n'
+ .format(n=e, ID=e.upper()))
+
+ write('}\n')
+
+ write('clib_error_t * vat_plugin_register (vat_main_t *vam)\n')
+ write('{\n')
+ write(' {n}_test_main_t * mainp = &{n}_test_main;\n'.format(n=module))
+ write(' mainp->vat_main = vam;\n')
+ write(' mainp->msg_id_base = vl_client_get_first_plugin_msg_id ("{n}_{crc:08x}");\n'
+ .format(n=module, crc=file_crc))
+ write(' if (mainp->msg_id_base == (u16) ~0)\n')
+ write(' return clib_error_return (0, "{} plugin not loaded...");\n'.format(module))
+ write(' setup_message_id_table (vam, mainp->msg_id_base);\n')
+ write('#ifdef VL_API_LOCAL_SETUP_MESSAGE_ID_TABLE\n')
+ write(' VL_API_LOCAL_SETUP_MESSAGE_ID_TABLE(vam);\n')
+ write('#endif\n')
+ write(' return 0;\n')
+ write('}\n')
+
+
#
# Plugin entry point
#
-def run(input_filename, s):
+def run(args, input_filename, s):
stream = StringIO()
+
+ if not args.outputdir:
+ sys.stderr.write('Missing --outputdir argument')
+ return None
+
basename = os.path.basename(input_filename)
filename, file_extension = os.path.splitext(basename)
modulename = filename.replace('.', '_')
+ filename_enum = os.path.join(args.outputdir + '/' + basename + '_enum.h')
+ filename_types = os.path.join(args.outputdir + '/' + basename + '_types.h')
+ filename_c = os.path.join(args.outputdir + '/' + basename + '.c')
+ filename_c_test = os.path.join(args.outputdir + '/' + basename + '_test.c')
+
+ # Generate separate types file
+ st = StringIO()
+ generate_include_types(s, modulename, st)
+ with open (filename_types, 'w') as fd:
+ st.seek (0)
+ shutil.copyfileobj (st, fd)
+ st.close()
+
+ # Generate separate enum file
+ st = StringIO()
+ generate_include_enum(s, modulename, st)
+ with open (filename_enum, 'w') as fd:
+ st.seek (0)
+ shutil.copyfileobj (st, fd)
+ st.close()
+
+ # Generate separate C file
+ st = StringIO()
+ generate_c_boilerplate(s['Service'], s['Define'], s['file_crc'],
+ modulename, st)
+ with open (filename_c, 'w') as fd:
+ st.seek (0)
+ shutil.copyfileobj(st, fd)
+ st.close()
+
+ # Generate separate C test file
+ # This is only supported for plugins at the moment
+ st = StringIO()
+ generate_c_test_plugin_boilerplate(s['Service'], s['Define'], s['file_crc'],
+ modulename, st)
+ with open (filename_c_test, 'w') as fd:
+ st.seek (0)
+ shutil.copyfileobj(st, fd)
+ st.close()
output = top_boilerplate.format(datestring=datestring,
input_filename=basename)
output += msg_ids(s)
output += msg_names(s)
output += msg_name_crc_list(s, filename)
- output += typedefs(s['types'] + s['Define'], s['Alias'],
- filename + file_extension)
- printfun_types(s['types'], s['Alias'], stream, modulename)
+ output += typedefs(modulename)
+ printfun_types(s['types'], stream, modulename)
printfun(s['Define'], stream, modulename)
output += stream.getvalue()
- output += endianfun(s['types'] + s['Define'], s['Alias'], modulename)
+ stream.close()
+ output += endianfun(s['types'] + s['Define'], modulename)
output += version_tuple(s, basename)
output += bottom_boilerplate.format(input_filename=basename,
file_crc=s['file_crc'])