6 from io import StringIO
8 datestring = datetime.datetime.utcfromtimestamp(
9 int(os.environ.get('SOURCE_DATE_EPOCH', time.time())))
10 input_filename = 'inputfil'
11 top_boilerplate = '''\
13 * VLIB API definitions {datestring}
14 * Input file: {input_filename}
15 * Automatically generated: please edit the input file NOT this file!
19 #if defined(vl_msg_id)||defined(vl_union_id) \\
20 || defined(vl_printfun) ||defined(vl_endianfun) \\
21 || defined(vl_api_version)||defined(vl_typedefs) \\
22 || defined(vl_msg_name)||defined(vl_msg_name_crc_list) \\
23 || defined(vl_api_version_tuple)
24 /* ok, something was selected */
26 #warning no content included from {input_filename}
29 #define VL_API_PACKED(x) x __attribute__ ((packed))
32 bottom_boilerplate = '''\
33 /****** API CRC (whole file) *****/
36 vl_api_version({input_filename}, {file_crc:#08x})
45 /****** Message ID / handler enum ******/
51 output += "vl_msg_id(VL_API_%s, vl_api_%s_t_handler)\n" % \
52 (t.name.upper(), t.name)
61 /****** Message names ******/
67 dont_trace = 0 if t.dont_trace else 1
68 output += "vl_msg_name(vl_api_%s_t, %d)\n" % (t.name, dont_trace)
74 def msg_name_crc_list(s, suffix):
77 /****** Message name, crc list ******/
79 #ifdef vl_msg_name_crc_list
81 output += "#define foreach_vl_msg_name_crc_%s " % suffix
84 output += "\\\n_(VL_API_%s, %s, %08x) " % \
85 (t.name.upper(), t.name, t.crc)
92 mappingtable = {'string': 'vl_api_string_t', }
93 if fieldtype in mappingtable:
94 return mappingtable[fieldtype]
98 def typedefs(objs, aliases, filename):
99 name = filename.replace('.', '_')
103 /****** Typedefs ******/
106 #ifndef included_{module}_typedef
107 #define included_{module}_typedef
109 output = output.format(module=name)
111 for k, v in aliases.items():
112 if 'length' in v.alias:
113 output += ('typedef %s vl_api_%s_t[%s];\n'
114 % (v.alias['type'], k, v.alias['length']))
116 output += 'typedef %s vl_api_%s_t;\n' % (v.alias['type'], k)
119 tname = o.__class__.__name__
121 if o.enumtype == 'u32':
122 output += "typedef enum {\n"
124 output += "typedef enum __attribute__((__packed__)) {\n"
127 output += " %s = %s,\n" % (b[0], b[1])
128 output += '} vl_api_%s_t;\n' % o.name
129 if o.enumtype != 'u32':
130 size1 = 'sizeof(vl_api_%s_t)' % o.name
131 size2 = 'sizeof(%s)' % o.enumtype
132 err_str = 'size of API enum %s is wrong' % o.name
133 output += ('STATIC_ASSERT(%s == %s, "%s");\n'
134 % (size1, size2, err_str))
137 output += "typedef VL_API_PACKED(union _vl_api_%s {\n" % o.name
139 output += ("typedef VL_API_PACKED(struct _vl_api_%s {\n"
142 if b.type == 'Option':
144 if b.type == 'Field':
145 output += " %s %s;\n" % (api2c(b.fieldtype),
147 elif b.type == 'Array':
149 output += " %s %s[0];\n" % (api2c(b.fieldtype),
152 # Fixed length strings decay to nul terminated u8
153 if b.fieldtype == 'string':
155 output += (' {} {};\n'
156 .format(api2c(b.fieldtype),
159 output += (' u8 {}[{}];\n'
160 .format(b.fieldname, b.length))
162 output += (" %s %s[%s];\n" %
163 (api2c(b.fieldtype), b.fieldname,
166 raise ValueError("Error in processing type {} for {}"
169 output += '}) vl_api_%s_t;\n' % o.name
172 output += "\n#endif\n\n"
177 format_strings = {'u8': '%u',
188 noprint_fields = {'_vl_msg_id': None,
189 'client_index': None,
196 def __init__(self, stream):
199 def print_string(self, o, stream):
202 write(' if (vl_api_string_len(&a->{f}) > 0) {{\n'
203 .format(f=o.fieldname))
204 write(' s = format(s, "\\n%U{f}: %.*s", '
205 'format_white_space, indent, '
206 'vl_api_string_len(&a->{f}) - 1, '
207 'vl_api_from_api_string(&a->{f}));\n'.format(f=o.fieldname))
209 write(' s = format(s, "\\n%U{f}:", '
210 'format_white_space, indent);\n'.format(f=o.fieldname))
213 write(' s = format(s, "\\n%U{f}: %s", '
214 'format_white_space, indent, a->{f});\n'
215 .format(f=o.fieldname))
217 def print_field(self, o, stream):
219 if o.fieldname in noprint_fields:
221 if o.fieldtype in format_strings:
222 f = format_strings[o.fieldtype]
223 write(' s = format(s, "\\n%U{n}: {f}", '
224 'format_white_space, indent, a->{n});\n'
225 .format(n=o.fieldname, f=f))
227 write(' s = format(s, "\\n%U{n}: %U", '
228 'format_white_space, indent, '
229 'format_{t}, &a->{n}, indent);\n'
230 .format(n=o.fieldname, t=o.fieldtype))
232 _dispatch['Field'] = print_field
234 def print_array(self, o, stream):
238 for (i = 0; i < {lfield}; i++) {{
239 s = format(s, "\\n%U{n}: %U",
240 format_white_space, indent, format_{t}, &a->{n}[i], indent);
244 forloop_format = '''\
245 for (i = 0; i < {lfield}; i++) {{
246 s = format(s, "\\n%U{n}: {t}",
247 format_white_space, indent, a->{n}[i]);
251 if o.fieldtype == 'string':
252 return self.print_string(o, stream)
254 if o.fieldtype == 'u8':
256 write(' s = format(s, "\\n%U{n}: %U", format_white_space, '
257 'indent, format_hex_bytes, a->{n}, a->{lfield});\n'
258 .format(n=o.fieldname, lfield=o.lengthfield))
260 write(' s = format(s, "\\n%U{n}: %U", format_white_space, '
261 'indent, format_hex_bytes, a, {lfield});\n'
262 .format(n=o.fieldname, lfield=o.length))
265 lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
266 if o.fieldtype in format_strings:
267 write(forloop_format.format(lfield=lfield,
268 t=format_strings[o.fieldtype],
271 write(forloop.format(lfield=lfield, t=o.fieldtype, n=o.fieldname))
273 _dispatch['Array'] = print_array
275 def print_alias(self, k, v, stream):
277 if ('length' in v.alias and v.alias['length'] and
278 v.alias['type'] == 'u8'):
279 write(' return format(s, "%U", format_hex_bytes, a, {});\n'
280 .format(v.alias['length']))
281 elif v.alias['type'] in format_strings:
282 write(' return format(s, "{}", *a);\n'
283 .format(format_strings[v.alias['type']]))
285 write(' return format(s, "{} (print not implemented)"'
288 def print_enum(self, o, stream):
290 write(" switch(*a) {\n")
292 write(" case %s:\n" % b[1])
293 write(' return format(s, "{}");\n'.format(b[0]))
296 _dispatch['Enum'] = print_enum
298 def print_obj(self, o, stream):
301 if o.type in self._dispatch:
302 self._dispatch[o.type](self, o, stream)
304 write(' s = format(s, "\\n{} {} {} (print not implemented");\n'
305 .format(o.type, o.fieldtype, o.fieldname))
308 def printfun(objs, stream, modulename):
312 /****** Print functions *****/
314 #ifndef included_{module}_printfun
315 #define included_{module}_printfun
318 #define _uword_fmt \"%lld\"
319 #define _uword_cast (long long)
321 #define _uword_fmt \"%ld\"
322 #define _uword_cast long
328 static inline void *vl_api_{name}_t_print (vl_api_{name}_t *a, void *handle)
331 u32 indent __attribute__((unused)) = 2;
332 int i __attribute__((unused));
335 h = h.format(module=modulename)
338 pp = Printfun(stream)
341 write("/***** manual: vl_api_%s_t_print *****/\n\n" % t.name)
343 write(signature.format(name=t.name))
344 write(' /* Message definition: vl_api_{}_t: */\n'.format(t.name))
345 write(" s = format(s, \"vl_api_%s_t:\");\n" % t.name)
347 pp.print_obj(o, stream)
348 write(' vec_add1(s, 0);\n')
349 write(' vl_print (handle, (char *)s);\n')
350 write(' vec_free (s);\n')
351 write(' return handle;\n')
355 write("\n#endif /* vl_printfun */\n")
360 def printfun_types(objs, aliases, stream, modulename):
362 pp = Printfun(stream)
365 /****** Print functions *****/
367 #ifndef included_{module}_printfun_types
368 #define included_{module}_printfun_types
371 h = h.format(module=modulename)
375 static inline u8 *format_vl_api_{name}_t (u8 *s, va_list * args)
377 vl_api_{name}_t *a = va_arg (*args, vl_api_{name}_t *);
378 u32 indent __attribute__((unused)) = va_arg (*args, u32);
379 int i __attribute__((unused));
383 for k, v in aliases.items():
385 write("/***** manual: vl_api_%s_t_print *****/\n\n" % k)
388 write(signature.format(name=k))
389 pp.print_alias(k, v, stream)
393 if t.__class__.__name__ == 'Enum':
394 write(signature.format(name=t.name))
395 pp.print_enum(t.block, stream)
396 write(' return s;\n')
401 write("/***** manual: vl_api_%s_t_print *****/\n\n" % t.name)
404 write(signature.format(name=t.name))
406 pp.print_obj(o, stream)
408 write(' return s;\n')
412 write("\n#endif /* vl_printfun_types */\n")
415 def imports(imports):
416 output = '/* Imported API files */\n'
417 output += '#ifndef vl_api_version\n'
420 s = i.filename.replace('plugins/', '')
421 output += '#include <{}.h>\n'.format(s)
427 'u16': 'clib_net_to_host_u16',
428 'u32': 'clib_net_to_host_u32',
429 'u64': 'clib_net_to_host_u64',
430 'i16': 'clib_net_to_host_u16',
431 'i32': 'clib_net_to_host_u32',
432 'i64': 'clib_net_to_host_u64',
433 'f64': 'clib_net_to_host_u64',
437 def endianfun_array(o):
439 for (i = 0; i < {length}; i++) {{
440 a->{name}[i] = {format}(a->{name}[i]);
444 forloop_format = '''\
445 for (i = 0; i < {length}; i++) {{
446 {type}_endian(&a->{name}[i]);
451 if o.fieldtype == 'u8' or o.fieldtype == 'string':
452 output += ' /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname)
454 lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
455 if o.fieldtype in endian_strings:
457 .format(length=lfield,
458 format=endian_strings[o.fieldtype],
461 output += (forloop_format
462 .format(length=lfield, type=o.fieldtype,
467 def endianfun_obj(o):
469 if o.type == 'Array':
470 return endianfun_array(o)
471 elif o.type != 'Field':
472 output += (' s = format(s, "\\n{} {} {} (print not implemented");\n'
473 .format(o.type, o.fieldtype, o.fieldname))
475 if o.fieldtype in endian_strings:
476 output += (' a->{name} = {format}(a->{name});\n'
477 .format(name=o.fieldname,
478 format=endian_strings[o.fieldtype]))
479 elif o.fieldtype.startswith('vl_api_'):
480 output += (' {type}_endian(&a->{name});\n'
481 .format(type=o.fieldtype, name=o.fieldname))
483 output += ' /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname)
488 def endianfun(objs, aliases, modulename):
491 /****** Endian swap functions *****/\n\
493 #ifndef included_{module}_endianfun
494 #define included_{module}_endianfun
496 #undef clib_net_to_host_uword
498 #define clib_net_to_host_uword clib_net_to_host_u64
500 #define clib_net_to_host_uword clib_net_to_host_u32
504 output = output.format(module=modulename)
507 static inline void vl_api_{name}_t_endian (vl_api_{name}_t *a)
509 int i __attribute__((unused));
512 for k, v in aliases.items():
514 output += "/***** manual: vl_api_%s_t_endian *****/\n\n" % k
517 output += signature.format(name=k)
518 if ('length' in v.alias and v.alias['length'] and
519 v.alias['type'] == 'u8'):
520 output += (' /* a->{name} = a->{name} (no-op) */\n'
522 elif v.alias['type'] in format_strings:
523 output += (' *a = {}(*a);\n'
524 .format(endian_strings[v.alias['type']]))
526 output += ' /* Not Implemented yet {} */'.format(k)
530 if t.__class__.__name__ == 'Enum':
531 output += signature.format(name=t.name)
532 if t.enumtype in endian_strings:
533 output += (' *a = {}(*a);\n'
534 .format(endian_strings[t.enumtype]))
536 output += (' /* a->{name} = a->{name} (no-op) */\n'
537 .format(name=t.name))
543 output += "/***** manual: vl_api_%s_t_endian *****/\n\n" % t.name
546 output += signature.format(name=t.name)
549 output += endianfun_obj(o)
553 output += "\n#endif /* vl_endianfun */\n\n"
558 def version_tuple(s, module):
560 /****** Version tuple *****/
562 #ifdef vl_api_version_tuple
565 if 'version' in s['Option']:
566 v = s['Option']['version']
567 (major, minor, patch) = v.split('.')
568 output += "vl_api_version_tuple(%s, %s, %s, %s)\n" % \
569 (module, major, minor, patch)
571 output += "\n#endif /* vl_api_version_tuple */\n\n"
579 def run(input_filename, s):
581 basename = os.path.basename(input_filename)
582 filename, file_extension = os.path.splitext(basename)
583 modulename = filename.replace('.', '_')
585 output = top_boilerplate.format(datestring=datestring,
586 input_filename=basename)
587 output += imports(s['Import'])
589 output += msg_names(s)
590 output += msg_name_crc_list(s, filename)
591 output += typedefs(s['types'] + s['Define'], s['Alias'],
592 filename + file_extension)
593 printfun_types(s['types'], s['Alias'], stream, modulename)
594 printfun(s['Define'], stream, modulename)
595 output += stream.getvalue()
596 output += endianfun(s['types'] + s['Define'], s['Alias'], modulename)
597 output += version_tuple(s, basename)
598 output += bottom_boilerplate.format(input_filename=basename,
599 file_crc=s['file_crc'])