2 # Copyright (c) 2020 Cisco and/or its affiliates.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 # Provide two classes FromJSON and TOJSON that converts between JSON and VPP's
22 This module creates C code for core VPP, VPP plugins and client side VAT and
30 from io import StringIO
33 process_imports = False
36 ###############################################################################
38 '''Class to generate functions converting from VPP binary API to JSON.'''
40 noprint_fields = {'_vl_msg_id': None,
43 is_number = {'u8': None,
54 def __init__(self, module, types, defines, imported_types, stream):
57 self.defines = defines
59 self.types_hash = {'vl_api_'+d.name+'_t':
60 d for d in types + imported_types}
61 self.defines_hash = {d.name: d for d in defines}
64 '''Output the top boilerplate.'''
65 write = self.stream.write
66 write('#ifndef included_{}_api_tojson_h\n'.format(self.module))
67 write('#define included_{}_api_tojson_h\n'.format(self.module))
68 write('#include <vppinfra/cJSON.h>\n\n')
69 write('#include <vppinfra/jsonformat.h>\n\n')
70 if self.module == 'interface_types':
71 write('#define vl_printfun\n')
72 write('#include <vnet/interface_types.api.h>\n\n')
75 '''Output the bottom boilerplate.'''
76 write = self.stream.write
79 def get_base_type(self, t):
82 vt = self.types_hash[t]
83 if vt.type == 'Using' and 'length' not in vt.alias:
84 vt_type = vt.alias['type']
89 def get_json_func(self, t):
90 '''Given the type, returns the function to use to create a
92 vt, vt_type = self.get_base_type(t)
94 if t in self.is_number or vt_type in self.is_number:
95 return 'cJSON_AddNumberToObject', '', False
97 return 'cJSON_AddBoolToObject', '', False
99 # Lookup type name check if it's enum
100 if vt.type == 'Enum' or vt.type == 'EnumFlag':
101 return '{t}_tojson'.format(t=t), '', True
102 return '{t}_tojson'.format(t=t), '&', True
104 def get_json_array_func(self, t):
105 '''Given a type returns the function to create a cJSON object
107 if t in self.is_number:
108 return 'cJSON_CreateNumber', ''
110 return 'cJSON_CreateBool', ''
111 vt, vt_type = self.get_base_type(t)
112 if vt.type == 'Enum' or vt.type == 'EnumFlag':
113 return '{t}_tojson'.format(t=t), ''
114 return '{t}_tojson'.format(t=t), '&'
116 def print_string(self, o):
117 '''Create cJSON object from vl_api_string_t'''
118 write = self.stream.write
120 write(' vl_api_string_cJSON_AddToObject(o, "{n}", &a->{n});\n'
121 .format(n=o.fieldname))
124 write(' cJSON_AddStringToObject(o, "{n}", (char *)a->{n});\n'
125 .format(n=o.fieldname))
127 def print_field(self, o):
128 '''Called for every field in a typedef or define.'''
129 write = self.stream.write
130 if o.fieldname in self.noprint_fields:
133 f, p, newobj = self.get_json_func(o.fieldtype)
136 write(' cJSON_AddItemToObject(o, "{n}", {f}({p}a->{n}));\n'
137 .format(f=f, p=p, n=o.fieldname))
139 write(' {f}(o, "{n}", {p}a->{n});\n'
140 .format(f=f, p=p, n=o.fieldname))
142 _dispatch['Field'] = print_field
144 def print_array(self, o):
145 '''Converts a VPP API array to cJSON array.'''
146 write = self.stream.write
151 cJSON *array = cJSON_AddArrayToObject(o, "{n}");
152 for (i = 0; i < {lfield}; i++) {{
153 cJSON_AddItemToArray(array, {f}({p}a->{n}[i]));
158 if o.fieldtype == 'string':
162 lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
163 if o.fieldtype == 'u8':
165 # What is length field doing here?
166 write(' u8 *s = format(0, "0x%U", format_hex_bytes, '
167 '&a->{n}, {lfield});\n'
168 .format(n=o.fieldname, lfield=lfield))
169 write(' cJSON_AddStringToObject(o, "{n}", (char *)s);\n'
170 .format(n=o.fieldname))
171 write(' vec_free(s);\n')
175 f, p = self.get_json_array_func(o.fieldtype)
176 write(forloop.format(lfield=lfield,
182 _dispatch['Array'] = print_array
184 def print_enum(self, o):
185 '''Create cJSON object (string) for VPP API enum'''
186 write = self.stream.write
187 write('static inline cJSON *vl_api_{name}_t_tojson '
188 '(vl_api_{name}_t a) {{\n'.format(name=o.name))
190 write(" switch(a) {\n")
192 write(" case %s:\n" % b[1])
193 write(' return cJSON_CreateString("{}");\n'.format(b[0]))
194 write(' default: return cJSON_CreateString("Invalid ENUM");\n')
196 write(' return 0;\n')
199 _dispatch['Enum'] = print_enum
201 def print_enum_flag(self, o):
202 '''Create cJSON object (string) for VPP API enum'''
203 write = self.stream.write
204 write('static inline cJSON *vl_api_{name}_t_tojson '
205 '(vl_api_{name}_t a) {{\n'.format(name=o.name))
206 write(' cJSON *array = cJSON_CreateArray();\n')
209 write(' if (a & {})\n'.format(b[0]))
210 write(' cJSON_AddItemToArray(array, cJSON_CreateString("{}"));\n'.format(b[0]))
211 write(' return array;\n')
214 _dispatch['EnumFlag'] = print_enum_flag
216 def print_typedef(self, o):
217 '''Create cJSON (dictionary) object from VPP API typedef'''
218 write = self.stream.write
219 write('static inline cJSON *vl_api_{name}_t_tojson '
220 '(vl_api_{name}_t *a) {{\n'.format(name=o.name))
221 write(' cJSON *o = cJSON_CreateObject();\n')
224 self._dispatch[t.type](self, t)
226 write(' return o;\n')
229 def print_define(self, o):
230 '''Create cJSON (dictionary) object from VPP API define'''
231 write = self.stream.write
232 write('static inline cJSON *vl_api_{name}_t_tojson '
233 '(vl_api_{name}_t *a) {{\n'.format(name=o.name))
234 write(' cJSON *o = cJSON_CreateObject();\n')
235 write(' cJSON_AddStringToObject(o, "_msgname", "{}");\n'
237 write(' cJSON_AddStringToObject(o, "_crc", "{crc:08x}");\n'
241 self._dispatch[t.type](self, t)
243 write(' return o;\n')
246 def print_using(self, o):
247 '''Create cJSON (dictionary) object from VPP API aliased type'''
251 write = self.stream.write
252 write('static inline cJSON *vl_api_{name}_t_tojson '
253 '(vl_api_{name}_t *a) {{\n'.format(name=o.name))
255 write(' u8 *s = format(0, "%U", format_vl_api_{}_t, a);\n'
257 write(' cJSON *o = cJSON_CreateString((char *)s);\n')
258 write(' vec_free(s);\n')
259 write(' return o;\n')
262 _dispatch['Typedef'] = print_typedef
263 _dispatch['Define'] = print_define
264 _dispatch['Using'] = print_using
265 _dispatch['Union'] = print_typedef
267 def generate_function(self, t):
268 '''Main entry point'''
269 write = self.stream.write
271 write('/* Manual print {} */\n'.format(t.name))
273 self._dispatch[t.type](self, t)
275 def generate_types(self):
276 '''Main entry point'''
278 self.generate_function(t)
280 def generate_defines(self):
281 '''Main entry point'''
282 for t in self.defines:
283 self.generate_function(t)
288 Parse JSON objects into VPP API binary message structures.
291 noprint_fields = {'_vl_msg_id': None,
292 'client_index': None,
294 is_number = {'u8': None,
305 def __init__(self, module, types, defines, imported_types, stream):
308 self.defines = defines
310 self.types_hash = {'vl_api_'+d.name+'_t':
311 d for d in types + imported_types}
312 self.defines_hash = {d.name: d for d in defines}
315 '''Output the top boilerplate.'''
316 write = self.stream.write
317 write('#ifndef included_{}_api_fromjson_h\n'.format(self.module))
318 write('#define included_{}_api_fromjson_h\n'.format(self.module))
319 write('#include <vppinfra/cJSON.h>\n\n')
320 write('#include <vppinfra/jsonformat.h>\n\n')
321 write('#pragma GCC diagnostic ignored "-Wunused-label"\n')
323 def is_base_type(self, t):
324 '''Check if a type is one of the VPP API base types'''
325 if t in self.is_number:
332 '''Output the bottom boilerplate.'''
333 write = self.stream.write
336 def print_string(self, o, toplevel=False):
337 '''Convert JSON string to vl_api_string_t'''
338 write = self.stream.write
340 msgvar = "a" if toplevel else "*mp"
341 msgsize = "l" if toplevel else "*len"
344 write(' char *p = cJSON_GetStringValue(item);\n')
345 write(' size_t plen = strlen(p);\n')
346 write(' {msgvar} = cJSON_realloc({msgvar}, {msgsize} + plen, {msgsize});\n'
347 .format(msgvar=msgvar, msgsize=msgsize))
348 write(' if ({msgvar} == 0) goto error;\n'.format(msgvar=msgvar))
349 write(' vl_api_c_string_to_api_string(p, (void *){msgvar} + '
350 '{msgsize} - sizeof(vl_api_string_t));\n'
351 .format(msgvar=msgvar, msgsize=msgsize))
352 write(' {msgsize} += plen;\n'.format(msgsize=msgsize))
354 write(' strncpy_s((char *)a->{n}, sizeof(a->{n}), '
355 'cJSON_GetStringValue(item), sizeof(a->{n}) - 1);\n'
356 .format(n=o.fieldname))
358 def print_field(self, o, toplevel=False):
359 '''Called for every field in a typedef or define.'''
360 write = self.stream.write
361 if o.fieldname in self.noprint_fields:
363 is_bt = self.is_base_type(o.fieldtype)
364 t = 'vl_api_{}'.format(o.fieldtype) if is_bt else o.fieldtype
366 msgvar = "(void **)&a" if toplevel else "mp"
367 msgsize = "&l" if toplevel else "len"
370 write(' vl_api_{t}_fromjson(item, &a->{n});\n'
371 .format(t=o.fieldtype, n=o.fieldname))
373 write(' if ({t}_fromjson({msgvar}, '
374 '{msgsize}, item, &a->{n}) < 0) goto error;\n'
375 .format(t=t, n=o.fieldname, msgvar=msgvar, msgsize=msgsize))
377 _dispatch['Field'] = print_field
379 def print_array(self, o, toplevel=False):
380 '''Convert JSON array to VPP API array'''
381 write = self.stream.write
386 cJSON *array = cJSON_GetObjectItem(o, "{n}");
387 int size = cJSON_GetArraySize(array);
388 if (size != {lfield}) goto error;
389 for (i = 0; i < size; i++) {{
390 cJSON *e = cJSON_GetArrayItem(array, i);
398 cJSON *array = cJSON_GetObjectItem(o, "{n}");
399 int size = cJSON_GetArraySize(array);
401 {realloc} = cJSON_realloc({realloc}, {msgsize} + sizeof({t}) * size, {msgsize});
402 {t} *d = (void *){realloc} + {msgsize};
403 {msgsize} += sizeof({t}) * size;
404 for (i = 0; i < size; i++) {{
405 cJSON *e = cJSON_GetArrayItem(array, i);
411 if o.fieldtype == 'string':
412 self.print_string(o, toplevel)
415 lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
416 msgvar = "(void **)&a" if toplevel else "mp"
417 realloc = "a" if toplevel else "*mp"
418 msgsize = "l" if toplevel else "*len"
420 if o.fieldtype == 'u8':
422 write(' s = u8string_fromjson(o, "{}");\n'
423 .format(o.fieldname))
424 write(' if (!s) goto error;\n')
425 write(' {} = vec_len(s);\n'.format(lfield))
427 write(' {realloc} = cJSON_realloc({realloc}, {msgsize} + '
428 'vec_len(s), {msgsize});\n'.format(msgvar=msgvar, msgsize=msgsize, realloc=realloc))
429 write(' memcpy((void *){realloc} + {msgsize}, s, '
430 'vec_len(s));\n'.format(realloc=realloc, msgsize=msgsize))
431 write(' {msgsize} += vec_len(s);\n'.format(msgsize=msgsize))
433 write(' vec_free(s);\n')
435 write(' if (u8string_fromjson2(o, "{n}", a->{n}) < 0) goto error;\n'
436 .format(n=o.fieldname))
439 is_bt = self.is_base_type(o.fieldtype)
443 call = ('vl_api_{t}_fromjson(e, &d[i]);'
444 .format(t=o.fieldtype))
446 call = ('if ({t}_fromjson({msgvar}, len, e, &d[i]) < 0) goto error; '
447 .format(t=o.fieldtype, msgvar=msgvar))
448 write(forloop_vla.format(lfield=lfield,
456 call = ('vl_api_{t}_fromjson(e, &a->{n}[i]);'
457 .format(t=t, n=o.fieldname))
459 call = ('if ({}_fromjson({}, len, e, &a->{}[i]) < 0) goto error;'
460 .format(t, msgvar, o.fieldname))
461 write(forloop.format(lfield=lfield,
469 _dispatch['Array'] = print_array
471 def print_enum(self, o):
472 '''Convert to JSON enum(string) to VPP API enum (int)'''
473 write = self.stream.write
474 write('static inline int vl_api_{n}_t_fromjson'
475 '(void **mp, int *len, cJSON *o, vl_api_{n}_t *a) {{\n'
477 write(' char *p = cJSON_GetStringValue(o);\n')
479 write(' if (strcmp(p, "{}") == 0) {{*a = {}; return 0;}}\n'
482 write(' return -1;\n')
485 _dispatch['Enum'] = print_enum
487 def print_enum_flag(self, o):
488 '''Convert to JSON enum(string) to VPP API enum (int)'''
489 write = self.stream.write
490 write('static inline int vl_api_{n}_t_fromjson '
491 '(void **mp, int *len, cJSON *o, vl_api_{n}_t *a) {{\n'
495 write(' for (i = 0; i < cJSON_GetArraySize(o); i++) {\n')
496 write(' cJSON *e = cJSON_GetArrayItem(o, i);\n')
497 write(' char *p = cJSON_GetStringValue(e);\n')
498 write(' if (!p) return -1;\n')
500 write(' if (strcmp(p, "{}") == 0) *a |= {};\n'
503 write(' return 0;\n')
506 _dispatch['EnumFlag'] = print_enum_flag
508 def print_typedef(self, o):
509 '''Convert from JSON object to VPP API binary representation'''
510 write = self.stream.write
512 write('static inline int vl_api_{name}_t_fromjson (void **mp, '
513 'int *len, cJSON *o, vl_api_{name}_t *a) {{\n'
514 .format(name=o.name))
515 write(' cJSON *item __attribute__ ((unused));\n')
516 write(' u8 *s __attribute__ ((unused));\n')
518 if t.type == 'Field' and t.is_lengthfield:
520 write('\n item = cJSON_GetObjectItem(o, "{}");\n'
521 .format(t.fieldname))
522 write(' if (!item) goto error;\n')
523 self._dispatch[t.type](self, t)
525 write('\n return 0;\n')
527 write(' return -1;\n')
530 def print_union(self, o):
531 '''Convert JSON object to VPP API binary union'''
532 write = self.stream.write
534 write('static inline int vl_api_{name}_t_fromjson (void **mp, '
535 'int *len, cJSON *o, vl_api_{name}_t *a) {{\n'
536 .format(name=o.name))
537 write(' cJSON *item __attribute__ ((unused));\n')
538 write(' u8 *s __attribute__ ((unused));\n')
540 if t.type == 'Field' and t.is_lengthfield:
542 write(' item = cJSON_GetObjectItem(o, "{}");\n'
543 .format(t.fieldname))
544 write(' if (item) {\n')
545 self._dispatch[t.type](self, t)
547 write('\n return 0;\n')
549 write(' return -1;\n')
552 def print_define(self, o):
553 '''Convert JSON object to VPP API message'''
554 write = self.stream.write
556 write('static inline vl_api_{name}_t *vl_api_{name}_t_fromjson '
557 '(cJSON *o, int *len) {{\n'.format(name=o.name))
558 write(' cJSON *item __attribute__ ((unused));\n')
559 write(' u8 *s __attribute__ ((unused));\n')
560 write(' int l = sizeof(vl_api_{}_t);\n'.format(o.name))
561 write(' vl_api_{}_t *a = cJSON_malloc(l);\n'.format(o.name))
565 if t.fieldname in self.noprint_fields:
567 if t.type == 'Field' and t.is_lengthfield:
569 write(' item = cJSON_GetObjectItem(o, "{}");\n'
570 .format(t.fieldname))
571 write(' if (!item) goto error;\n')
573 self._dispatch[t.type](self, t, toplevel=True)
576 write(' *len = l;\n')
577 write(' return a;\n')
581 write(' cJSON_free(a);\n')
582 write(' return 0;\n')
585 def print_using(self, o):
586 '''Convert JSON field to VPP type alias'''
587 write = self.stream.write
593 write('static inline int vl_api_{name}_t_fromjson (void **mp, '
594 'int *len, cJSON *o, vl_api_{name}_t *a) {{\n'
595 .format(name=o.name))
596 if 'length' in o.alias:
597 if t.fieldtype != 'u8':
598 raise ValueError("Error in processing type {} for {}"
599 .format(t.fieldtype, o.name))
600 write(' vl_api_u8_string_fromjson(o, (u8 *)a, {});\n'
601 .format(o.alias['length']))
603 write(' vl_api_{t}_fromjson(o, ({t} *)a);\n'
604 .format(t=t.fieldtype))
606 write(' return 0;\n')
609 _dispatch['Typedef'] = print_typedef
610 _dispatch['Define'] = print_define
611 _dispatch['Using'] = print_using
612 _dispatch['Union'] = print_union
614 def generate_function(self, t):
615 '''Main entry point'''
616 write = self.stream.write
618 write('/* Manual print {} */\n'.format(t.name))
620 self._dispatch[t.type](self, t)
622 def generate_types(self):
623 '''Main entry point'''
625 self.generate_function(t)
627 def generate_defines(self):
628 '''Main entry point'''
629 for t in self.defines:
630 self.generate_function(t)
633 def generate_tojson(s, modulename, stream):
634 '''Generate all functions to convert from API to JSON'''
637 write('/* Imported API files */\n')
638 for i in s['Import']:
639 f = i.filename.replace('plugins/', '')
640 write('#include <{}_tojson.h>\n'.format(f))
642 pp = ToJSON(modulename, s['types'], s['Define'], s['imported']['types'],
646 pp.generate_defines()
651 def generate_fromjson(s, modulename, stream):
652 '''Generate all functions to convert from JSON to API'''
654 write('/* Imported API files */\n')
655 for i in s['Import']:
656 f = i.filename.replace('plugins/', '')
657 write('#include <{}_fromjson.h>\n'.format(f))
659 pp = FromJSON(modulename, s['types'], s['Define'], s['imported']['types'],
663 pp.generate_defines()
668 ###############################################################################
671 DATESTRING = datetime.datetime.utcfromtimestamp(
672 int(os.environ.get('SOURCE_DATE_EPOCH', time.time())))
673 TOP_BOILERPLATE = '''\
675 * VLIB API definitions {datestring}
676 * Input file: {input_filename}
677 * Automatically generated: please edit the input file NOT this file!
681 #if defined(vl_msg_id)||defined(vl_union_id) \\
682 || defined(vl_printfun) ||defined(vl_endianfun) \\
683 || defined(vl_api_version)||defined(vl_typedefs) \\
684 || defined(vl_msg_name)||defined(vl_msg_name_crc_list) \\
685 || defined(vl_api_version_tuple)
686 /* ok, something was selected */
688 #warning no content included from {input_filename}
691 #define VL_API_PACKED(x) x __attribute__ ((packed))
694 BOTTOM_BOILERPLATE = '''\
695 /****** API CRC (whole file) *****/
697 #ifdef vl_api_version
698 vl_api_version({input_filename}, {file_crc:#08x})
705 '''Generate macro to map API message id to handler'''
708 /****** Message ID / handler enum ******/
713 for t in s['Define']:
714 output += "vl_msg_id(VL_API_%s, vl_api_%s_t_handler)\n" % \
715 (t.name.upper(), t.name)
722 '''Generate calls to name mapping macro'''
725 /****** Message names ******/
730 for t in s['Define']:
731 dont_trace = 0 if t.dont_trace else 1
732 output += "vl_msg_name(vl_api_%s_t, %d)\n" % (t.name, dont_trace)
738 def msg_name_crc_list(s, suffix):
739 '''Generate list of names to CRC mappings'''
742 /****** Message name, crc list ******/
744 #ifdef vl_msg_name_crc_list
746 output += "#define foreach_vl_msg_name_crc_%s " % suffix
748 for t in s['Define']:
749 output += "\\\n_(VL_API_%s, %s, %08x) " % \
750 (t.name.upper(), t.name, t.crc)
756 def api2c(fieldtype):
757 '''Map between API type names and internal VPP type names'''
758 mappingtable = {'string': 'vl_api_string_t', }
759 if fieldtype in mappingtable:
760 return mappingtable[fieldtype]
764 def typedefs(filename):
765 '''Include in the main files to the types file'''
768 /****** Typedefs ******/
771 #include "{include}.api_types.h"
773 '''.format(include=filename)
777 FORMAT_STRINGS = {'u8': '%u',
790 '''Functions for pretty printing VPP API messages'''
792 noprint_fields = {'_vl_msg_id': None,
793 'client_index': None,
796 def __init__(self, stream):
800 def print_string(o, stream):
801 '''Pretty print a vl_api_string_t'''
804 write(' if (vl_api_string_len(&a->{f}) > 0) {{\n'
805 .format(f=o.fieldname))
806 write(' s = format(s, "\\n%U{f}: %U", '
807 'format_white_space, indent, '
808 'vl_api_format_string, (&a->{f}));\n'.format(f=o.fieldname))
810 write(' s = format(s, "\\n%U{f}:", '
811 'format_white_space, indent);\n'.format(f=o.fieldname))
814 write(' s = format(s, "\\n%U{f}: %s", '
815 'format_white_space, indent, a->{f});\n'
816 .format(f=o.fieldname))
818 def print_field(self, o, stream):
819 '''Pretty print API field'''
821 if o.fieldname in self.noprint_fields:
823 if o.fieldtype in FORMAT_STRINGS:
824 f = FORMAT_STRINGS[o.fieldtype]
825 write(' s = format(s, "\\n%U{n}: {f}", '
826 'format_white_space, indent, a->{n});\n'
827 .format(n=o.fieldname, f=f))
829 write(' s = format(s, "\\n%U{n}: %U", '
830 'format_white_space, indent, '
831 'format_{t}, &a->{n}, indent);\n'
832 .format(n=o.fieldname, t=o.fieldtype))
834 _dispatch['Field'] = print_field
836 def print_array(self, o, stream):
837 '''Pretty print API array'''
841 for (i = 0; i < {lfield}; i++) {{
842 s = format(s, "\\n%U{n}: %U",
843 format_white_space, indent, format_{t}, &a->{n}[i], indent);
847 forloop_format = '''\
848 for (i = 0; i < {lfield}; i++) {{
849 s = format(s, "\\n%U{n}: {t}",
850 format_white_space, indent, a->{n}[i]);
854 if o.fieldtype == 'string':
855 self.print_string(o, stream)
858 if o.fieldtype == 'u8':
860 write(' s = format(s, "\\n%U{n}: %U", format_white_space, '
861 'indent, format_hex_bytes, a->{n}, a->{lfield});\n'
862 .format(n=o.fieldname, lfield=o.lengthfield))
864 write(' s = format(s, "\\n%U{n}: %U", format_white_space, '
865 'indent, format_hex_bytes, a, {lfield});\n'
866 .format(n=o.fieldname, lfield=o.length))
869 lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
870 if o.fieldtype in FORMAT_STRINGS:
871 write(forloop_format.format(lfield=lfield,
872 t=FORMAT_STRINGS[o.fieldtype],
875 write(forloop.format(lfield=lfield, t=o.fieldtype, n=o.fieldname))
877 _dispatch['Array'] = print_array
880 def print_alias(k, v, stream):
881 '''Pretty print type alias'''
883 if ('length' in v.alias and v.alias['length'] and
884 v.alias['type'] == 'u8'):
885 write(' return format(s, "%U", format_hex_bytes, a, {});\n'
886 .format(v.alias['length']))
887 elif v.alias['type'] in FORMAT_STRINGS:
888 write(' return format(s, "{}", *a);\n'
889 .format(FORMAT_STRINGS[v.alias['type']]))
891 write(' return format(s, "{} (print not implemented)");\n'
895 def print_enum(o, stream):
896 '''Pretty print API enum'''
898 write(" switch(*a) {\n")
900 write(" case %s:\n" % b[1])
901 write(' return format(s, "{}");\n'.format(b[0]))
904 _dispatch['Enum'] = print_enum
905 _dispatch['EnumFlag'] = print_enum
907 def print_obj(self, o, stream):
911 if o.type in self._dispatch:
912 self._dispatch[o.type](self, o, stream)
914 write(' s = format(s, "\\n{} {} {} (print not implemented");\n'
915 .format(o.type, o.fieldtype, o.fieldname))
918 def printfun(objs, stream, modulename):
919 '''Main entry point for pretty print function generation'''
923 /****** Print functions *****/
925 #ifndef included_{module}_printfun
926 #define included_{module}_printfun
929 #define _uword_fmt \"%lld\"
930 #define _uword_cast (long long)
932 #define _uword_fmt \"%ld\"
933 #define _uword_cast long
936 #include "{module}.api_tojson.h"
937 #include "{module}.api_fromjson.h"
942 static inline void *vl_api_{name}_t_print{suffix} (vl_api_{name}_t *a, void *handle)
945 u32 indent __attribute__((unused)) = 2;
946 int i __attribute__((unused));
949 h = h.format(module=modulename)
952 pp = Printfun(stream)
955 write("/***** manual: vl_api_%s_t_print *****/\n\n" % t.name)
957 write(signature.format(name=t.name, suffix=''))
958 write(' /* Message definition: vl_api_{}_t: */\n'.format(t.name))
959 write(" s = format(s, \"vl_api_%s_t:\");\n" % t.name)
961 pp.print_obj(o, stream)
962 write(' vec_add1(s, 0);\n')
963 write(' vl_print (handle, (char *)s);\n')
964 write(' vec_free (s);\n')
965 write(' return handle;\n')
968 write(signature.format(name=t.name, suffix='_json'))
969 write(' cJSON * o = vl_api_{}_t_tojson(a);\n'.format(t.name))
970 write(' (void)s;\n');
971 write(' char *out = cJSON_Print(o);\n')
972 write(' vl_print(handle, out);\n');
973 write(' cJSON_Delete(o);\n')
974 write(' cJSON_free(out);\n');
975 write(' return handle;\n')
979 write("\n#endif /* vl_printfun */\n")
984 def printfun_types(objs, stream, modulename):
985 '''Pretty print API types'''
987 pp = Printfun(stream)
990 /****** Print functions *****/
992 #ifndef included_{module}_printfun_types
993 #define included_{module}_printfun_types
996 h = h.format(module=modulename)
1000 static inline u8 *format_vl_api_{name}_t (u8 *s, va_list * args)
1002 vl_api_{name}_t *a = va_arg (*args, vl_api_{name}_t *);
1003 u32 indent __attribute__((unused)) = va_arg (*args, u32);
1004 int i __attribute__((unused));
1009 if t.__class__.__name__ == 'Enum' or t.__class__.__name__ == 'EnumFlag':
1010 write(signature.format(name=t.name))
1011 pp.print_enum(t.block, stream)
1012 write(' return s;\n')
1017 write("/***** manual: vl_api_%s_t_print *****/\n\n" % t.name)
1020 if t.__class__.__name__ == 'Using':
1021 write(signature.format(name=t.name))
1022 pp.print_alias(t.name, t, stream)
1026 write(signature.format(name=t.name))
1028 pp.print_obj(o, stream)
1030 write(' return s;\n')
1034 write("\n#endif /* vl_printfun_types */\n")
1037 def generate_imports(imports):
1038 '''Add #include matching the API import statements'''
1039 output = '/* Imported API files */\n'
1040 output += '#ifndef vl_api_version\n'
1043 s = i.filename.replace('plugins/', '')
1044 output += '#include <{}.h>\n'.format(s)
1045 output += '#endif\n'
1050 'u16': 'clib_net_to_host_u16',
1051 'u32': 'clib_net_to_host_u32',
1052 'u64': 'clib_net_to_host_u64',
1053 'i16': 'clib_net_to_host_i16',
1054 'i32': 'clib_net_to_host_i32',
1055 'i64': 'clib_net_to_host_i64',
1056 'f64': 'clib_net_to_host_f64',
1060 def endianfun_array(o):
1061 '''Generate endian functions for arrays'''
1063 for (i = 0; i < {length}; i++) {{
1064 a->{name}[i] = {format}(a->{name}[i]);
1068 forloop_format = '''\
1069 for (i = 0; i < {length}; i++) {{
1070 {type}_endian(&a->{name}[i]);
1075 if o.fieldtype == 'u8' or o.fieldtype == 'string' or o.fieldtype == 'bool':
1076 output += ' /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname)
1078 lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
1079 if o.fieldtype in ENDIAN_STRINGS:
1081 .format(length=lfield,
1082 format=ENDIAN_STRINGS[o.fieldtype],
1085 output += (forloop_format
1086 .format(length=lfield, type=o.fieldtype,
1091 NO_ENDIAN_CONVERSION = {'client_index': None}
1094 def endianfun_obj(o):
1095 '''Generate endian conversion function for type'''
1097 if o.type == 'Array':
1098 return endianfun_array(o)
1099 if o.type != 'Field':
1100 output += (' s = format(s, "\\n{} {} {} (print not implemented");\n'
1101 .format(o.type, o.fieldtype, o.fieldname))
1103 if o.fieldname in NO_ENDIAN_CONVERSION:
1104 output += ' /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname)
1106 if o.fieldtype in ENDIAN_STRINGS:
1107 output += (' a->{name} = {format}(a->{name});\n'
1108 .format(name=o.fieldname,
1109 format=ENDIAN_STRINGS[o.fieldtype]))
1110 elif o.fieldtype.startswith('vl_api_'):
1111 output += (' {type}_endian(&a->{name});\n'
1112 .format(type=o.fieldtype, name=o.fieldname))
1114 output += ' /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname)
1119 def endianfun(objs, modulename):
1120 '''Main entry point for endian function generation'''
1123 /****** Endian swap functions *****/\n\
1125 #ifndef included_{module}_endianfun
1126 #define included_{module}_endianfun
1128 #undef clib_net_to_host_uword
1130 #define clib_net_to_host_uword clib_net_to_host_u64
1132 #define clib_net_to_host_uword clib_net_to_host_u32
1136 output = output.format(module=modulename)
1139 static inline void vl_api_{name}_t_endian (vl_api_{name}_t *a)
1141 int i __attribute__((unused));
1145 if t.__class__.__name__ == 'Enum' or t.__class__.__name__ == 'EnumFlag' :
1146 output += signature.format(name=t.name)
1147 if t.enumtype in ENDIAN_STRINGS:
1148 output += (' *a = {}(*a);\n'
1149 .format(ENDIAN_STRINGS[t.enumtype]))
1151 output += (' /* a->{name} = a->{name} (no-op) */\n'
1152 .format(name=t.name))
1158 output += "/***** manual: vl_api_%s_t_endian *****/\n\n" % t.name
1161 if t.__class__.__name__ == 'Using':
1162 output += signature.format(name=t.name)
1163 if ('length' in t.alias and t.alias['length'] and
1164 t.alias['type'] == 'u8'):
1165 output += (' /* a->{name} = a->{name} (no-op) */\n'
1166 .format(name=t.name))
1167 elif t.alias['type'] in FORMAT_STRINGS:
1168 output += (' *a = {}(*a);\n'
1169 .format(ENDIAN_STRINGS[t.alias['type']]))
1171 output += ' /* Not Implemented yet {} */'.format(t.name)
1175 output += signature.format(name=t.name)
1178 output += endianfun_obj(o)
1181 output += "\n#endif"
1182 output += "\n#endif /* vl_endianfun */\n\n"
1187 def version_tuple(s, module):
1188 '''Generate semantic version string'''
1190 /****** Version tuple *****/
1192 #ifdef vl_api_version_tuple
1195 if 'version' in s['Option']:
1196 v = s['Option']['version']
1197 (major, minor, patch) = v.split('.')
1198 output += "vl_api_version_tuple(%s, %s, %s, %s)\n" % \
1199 (module, major, minor, patch)
1201 output += "\n#endif /* vl_api_version_tuple */\n\n"
1206 def generate_include_enum(s, module, stream):
1207 '''Generate <name>.api_enum.h'''
1208 write = stream.write
1211 write('typedef enum {\n')
1212 for t in s['Define']:
1213 write(' VL_API_{},\n'.format(t.name.upper()))
1214 write(' VL_MSG_{}_LAST\n'.format(module.upper()))
1215 write('}} vl_api_{}_enum_t;\n'.format(module))
1218 def generate_include_counters(s, stream):
1219 '''Include file for the counter data model types.'''
1220 write = stream.write
1223 csetname = counters.name
1224 write('typedef enum {\n')
1225 for c in counters.block:
1226 write(' {}_ERROR_{},\n'
1227 .format(csetname.upper(), c['name'].upper()))
1228 write(' {}_N_ERROR\n'.format(csetname.upper()))
1229 write('}} vl_counter_{}_enum_t;\n'.format(csetname))
1231 write('extern vlib_error_desc_t {}_error_counters[];\n'.format(csetname))
1234 def generate_include_types(s, module, stream):
1235 '''Generate separate API _types file.'''
1236 write = stream.write
1238 write('#ifndef included_{module}_api_types_h\n'.format(module=module))
1239 write('#define included_{module}_api_types_h\n'.format(module=module))
1241 if 'version' in s['Option']:
1242 v = s['Option']['version']
1243 (major, minor, patch) = v.split('.')
1244 write('#define VL_API_{m}_API_VERSION_MAJOR {v}\n'
1245 .format(m=module.upper(), v=major))
1246 write('#define VL_API_{m}_API_VERSION_MINOR {v}\n'
1247 .format(m=module.upper(), v=minor))
1248 write('#define VL_API_{m}_API_VERSION_PATCH {v}\n'
1249 .format(m=module.upper(), v=patch))
1252 write('/* Imported API files */\n')
1253 for i in s['Import']:
1254 filename = i.filename.replace('plugins/', '')
1255 write('#include <{}_types.h>\n'.format(filename))
1257 for o in s['types'] + s['Define']:
1258 tname = o.__class__.__name__
1259 if tname == 'Using':
1260 if 'length' in o.alias:
1261 write('typedef %s vl_api_%s_t[%s];\n' %
1262 (o.alias['type'], o.name, o.alias['length']))
1264 write('typedef %s vl_api_%s_t;\n' % (o.alias['type'], o.name))
1265 elif tname == 'Enum' or tname == 'EnumFlag':
1266 if o.enumtype == 'u32':
1267 write("typedef enum {\n")
1269 write("typedef enum __attribute__((packed)) {\n")
1272 write(" %s = %s,\n" % (b[0], b[1]))
1273 write('} vl_api_%s_t;\n' % o.name)
1274 if o.enumtype != 'u32':
1275 size1 = 'sizeof(vl_api_%s_t)' % o.name
1276 size2 = 'sizeof(%s)' % o.enumtype
1277 err_str = 'size of API enum %s is wrong' % o.name
1278 write('STATIC_ASSERT(%s == %s, "%s");\n'
1279 % (size1, size2, err_str))
1281 if tname == 'Union':
1282 write("typedef union __attribute__ ((packed)) _vl_api_%s {\n"
1285 write(("typedef struct __attribute__ ((packed)) _vl_api_%s {\n")
1288 if b.type == 'Option':
1290 if b.type == 'Field':
1291 write(" %s %s;\n" % (api2c(b.fieldtype),
1293 elif b.type == 'Array':
1295 write(" %s %s[0];\n" % (api2c(b.fieldtype),
1298 # Fixed length strings decay to nul terminated u8
1299 if b.fieldtype == 'string':
1302 .format(api2c(b.fieldtype),
1305 write(' u8 {}[{}];\n'
1306 .format(b.fieldname, b.length))
1308 write(" %s %s[%s];\n" %
1309 (api2c(b.fieldtype), b.fieldname,
1312 raise ValueError("Error in processing type {} for {}"
1315 write('} vl_api_%s_t;\n' % o.name)
1317 for t in s['Define']:
1318 write('#define VL_API_{ID}_CRC "{n}_{crc:08x}"\n'
1319 .format(n=t.name, ID=t.name.upper(), crc=t.crc))
1324 def generate_c_boilerplate(services, defines, counters, file_crc,
1326 '''VPP side plugin.'''
1327 write = stream.write
1328 define_hash = {d.name: d for d in defines}
1331 #define vl_endianfun /* define message structures */
1332 #include "{module}.api.h"
1335 /* instantiate all the print functions we know about */
1336 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
1338 #include "{module}.api.h"
1343 write(hdr.format(module=module))
1344 write('static u16\n')
1345 write('setup_message_id_table (void) {\n')
1346 write(' api_main_t *am = my_api_main;\n')
1347 write(' vl_msg_api_msg_config_t c;\n')
1348 write(' u16 msg_id_base = vl_msg_api_get_msg_ids ("{}_{crc:08x}", '
1349 'VL_MSG_{m}_LAST);\n'
1350 .format(module, crc=file_crc, m=module.upper()))
1353 write(' vl_msg_api_add_msg_name_crc (am, "{n}_{crc:08x}",\n'
1354 ' VL_API_{ID} + msg_id_base);\n'
1355 .format(n=d.name, ID=d.name.upper(), crc=d.crc))
1357 d = define_hash[s.caller]
1358 write(' c = (vl_msg_api_msg_config_t) '
1359 ' {{.id = VL_API_{ID} + msg_id_base,\n'
1361 ' .handler = vl_api_{n}_t_handler,\n'
1362 ' .cleanup = vl_noop_handler,\n'
1363 ' .endian = vl_api_{n}_t_endian,\n'
1364 ' .print = vl_api_{n}_t_print,\n'
1367 ' .print_json = vl_api_{n}_t_print_json,\n'
1368 ' .tojson = vl_api_{n}_t_tojson,\n'
1369 ' .fromjson = vl_api_{n}_t_fromjson,\n'
1370 ' .is_autoendian = {auto}}};\n'
1371 .format(n=s.caller, ID=s.caller.upper(),
1373 write(' vl_msg_api_config (&c);\n')
1375 d = define_hash[s.reply]
1376 write(' c = (vl_msg_api_msg_config_t) '
1377 '{{.id = VL_API_{ID} + msg_id_base,\n'
1380 ' .cleanup = vl_noop_handler,\n'
1381 ' .endian = vl_api_{n}_t_endian,\n'
1382 ' .print = vl_api_{n}_t_print,\n'
1385 ' .print_json = vl_api_{n}_t_print_json,\n'
1386 ' .tojson = vl_api_{n}_t_tojson,\n'
1387 ' .fromjson = vl_api_{n}_t_fromjson,\n'
1388 ' .is_autoendian = {auto}}};\n'
1389 .format(n=s.reply, ID=s.reply.upper(),
1391 write(' vl_msg_api_config (&c);\n')
1395 write(' return msg_id_base;\n')
1398 severity = {'error': 'VL_COUNTER_SEVERITY_ERROR',
1399 'info': 'VL_COUNTER_SEVERITY_INFO',
1400 'warn': 'VL_COUNTER_SEVERITY_WARN'}
1402 for cnt in counters:
1404 write('vlib_error_desc_t {}_error_counters[] = {{\n'.format(csetname))
1407 write(' .name = "{}",\n'.format(c['name']))
1408 write(' .desc = "{}",\n'.format(c['description']))
1409 write(' .severity = {},\n'.format(severity[c['severity']]))
1414 def generate_c_test_boilerplate(services, defines, file_crc, module, plugin,
1416 '''Generate code for legacy style VAT. To be deleted.'''
1417 write = stream.write
1419 define_hash = {d.name: d for d in defines}
1422 #define vl_endianfun /* define message structures */
1423 #include "{module}.api.h"
1426 /* instantiate all the print functions we know about */
1427 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
1429 #include "{module}.api.h"
1434 write(hdr.format(module=module))
1437 d = define_hash[s.reply]
1442 ' * Manual definition requested for: \n'
1443 ' * vl_api_{n}_t_handler()\n'
1447 if not define_hash[s.caller].autoreply:
1448 write('/* Generation not supported (vl_api_{n}_t_handler()) */\n'
1451 write('#ifndef VL_API_{n}_T_HANDLER\n'.format(n=s.reply.upper()))
1452 write('static void\n')
1453 write('vl_api_{n}_t_handler (vl_api_{n}_t * mp) {{\n'
1455 write(' vat_main_t * vam = {}_test_main.vat_main;\n'.format(module))
1456 write(' i32 retval = ntohl(mp->retval);\n')
1457 write(' if (vam->async_mode) {\n')
1458 write(' vam->async_errors += (retval < 0);\n')
1459 write(' } else {\n')
1460 write(' vam->retval = retval;\n')
1461 write(' vam->result_ready = 1;\n')
1467 if define_hash[e].manual_print:
1469 write('static void\n')
1470 write('vl_api_{n}_t_handler (vl_api_{n}_t * mp) {{\n'.format(n=e))
1471 write(' vl_print(0, "{n} event called:");\n'.format(n=e))
1472 write(' vl_api_{n}_t_print(mp, 0);\n'.format(n=e))
1475 write('static void\n')
1476 write('setup_message_id_table (vat_main_t * vam, u16 msg_id_base) {\n')
1478 write(' vl_msg_api_set_handlers(VL_API_{ID} + msg_id_base, '
1480 ' vl_api_{n}_t_handler, '
1481 ' vl_noop_handler,\n'
1482 ' vl_api_{n}_t_endian, '
1483 ' vl_api_{n}_t_print,\n'
1484 ' sizeof(vl_api_{n}_t), 1,\n'
1485 ' vl_api_{n}_t_print_json,\n'
1486 ' vl_api_{n}_t_tojson,\n'
1487 ' vl_api_{n}_t_fromjson);\n'
1488 .format(n=s.reply, ID=s.reply.upper()))
1489 write(' hash_set_mem (vam->function_by_name, "{n}", api_{n});\n'
1490 .format(n=s.caller))
1492 write(' hash_set_mem (vam->help_by_name, "{n}", "{help}");\n'
1494 help=define_hash[s.caller].options['vat_help']))
1500 write(' vl_msg_api_set_handlers(VL_API_{ID} + msg_id_base, '
1502 ' vl_api_{n}_t_handler, '
1503 ' vl_noop_handler,\n'
1504 ' vl_api_{n}_t_endian, '
1505 ' vl_api_{n}_t_print,\n'
1506 ' sizeof(vl_api_{n}_t), 1,\n'
1507 ' vl_api_{n}_t_print_json,\n'
1508 ' vl_api_{n}_t_tojson,\n'
1509 ' vl_api_{n}_t_fromjson);\n'
1510 .format(n=e, ID=e.upper()))
1513 write('clib_error_t * vat_plugin_register (vat_main_t *vam)\n')
1515 write(' {n}_test_main_t * mainp = &{n}_test_main;\n'.format(n=module))
1516 write(' mainp->vat_main = vam;\n')
1517 write(' mainp->msg_id_base = vl_client_get_first_plugin_msg_id '
1518 ' ("{n}_{crc:08x}");\n'
1519 .format(n=module, crc=file_crc))
1520 write(' if (mainp->msg_id_base == (u16) ~0)\n')
1521 write(' return clib_error_return (0, "{} plugin not loaded...");\n'
1523 write(' setup_message_id_table (vam, mainp->msg_id_base);\n')
1524 write('#ifdef VL_API_LOCAL_SETUP_MESSAGE_ID_TABLE\n')
1525 write(' VL_API_LOCAL_SETUP_MESSAGE_ID_TABLE(vam);\n')
1527 write(' return 0;\n')
1532 '''Check if a method is generated already.'''
1533 def _f(module, d, processed, *args):
1534 if d.name in processed:
1536 processed[d.name] = True
1537 return func(module, d, *args)
1541 def c_test_api_service(s, dump, stream):
1542 '''Generate JSON code for a service.'''
1543 write = stream.write
1545 req_reply_template = '''\
1552 mp = vl_api_{n}_t_fromjson(o, &len);
1554 fprintf(stderr, "Failed converting JSON to API\\n");
1558 mp->_vl_msg_id = vac_get_msg_index(VL_API_{N}_CRC);
1559 vl_api_{n}_t_endian(mp);
1560 vac_write((char *)mp, len);
1566 vac_read(&p, &l, 5); // XXX: Fix timeout
1567 if (p == 0 || l == 0) return 0;
1568 // XXX Will fail in case of event received. Do loop
1569 if (ntohs(*((u16 *)p)) != vac_get_msg_index(VL_API_{R}_CRC)) {{
1570 fprintf(stderr, "Mismatched reply\\n");
1573 vl_api_{r}_t *rmp = (vl_api_{r}_t *)p;
1574 vl_api_{r}_t_endian(rmp);
1575 return vl_api_{r}_t_tojson(rmp);
1579 dump_details_template = '''\
1583 u16 msg_id = vac_get_msg_index(VL_API_{N}_CRC);
1586 vl_api_{n}_t *mp = vl_api_{n}_t_fromjson(o, &len);
1588 fprintf(stderr, "Failed converting JSON to API\\n");
1591 mp->_vl_msg_id = msg_id;
1592 vl_api_{n}_t_endian(mp);
1593 vac_write((char *)mp, len);
1596 vat2_control_ping(123); // FIX CONTEXT
1597 cJSON *reply = cJSON_CreateArray();
1599 u16 ping_reply_msg_id = vac_get_msg_index(VL_API_CONTROL_PING_REPLY_CRC);
1600 u16 details_msg_id = vac_get_msg_index(VL_API_{R}_CRC);
1606 vac_read(&p, &l, 5); // XXX: Fix timeout
1607 if (p == 0 || l == 0) {{
1612 /* Message can be one of [_details, control_ping_reply
1613 * or unrelated event]
1615 u16 reply_msg_id = ntohs(*((u16 *)p));
1616 if (reply_msg_id == ping_reply_msg_id) {{
1620 if (reply_msg_id == details_msg_id) {{
1621 if (l < sizeof(vl_api_{r}_t)) {{
1625 vl_api_{r}_t *rmp = (vl_api_{r}_t *)p;
1626 vl_api_{r}_t_endian(rmp);
1627 cJSON_AddItemToArray(reply, vl_api_{r}_t_tojson(rmp));
1634 gets_details_reply_template = '''\
1638 u16 msg_id = vac_get_msg_index(VL_API_{N}_CRC);
1641 vl_api_{n}_t *mp = vl_api_{n}_t_fromjson(o, &len);
1643 fprintf(stderr, "Failed converting JSON to API\\n");
1646 mp->_vl_msg_id = msg_id;
1648 vl_api_{n}_t_endian(mp);
1649 vac_write((char *)mp, len);
1652 cJSON *reply = cJSON_CreateArray();
1654 u16 reply_msg_id = vac_get_msg_index(VL_API_{R}_CRC);
1655 u16 details_msg_id = vac_get_msg_index(VL_API_{D}_CRC);
1661 vac_read(&p, &l, 5); // XXX: Fix timeout
1663 /* Message can be one of [_details, control_ping_reply
1664 * or unrelated event]
1666 u16 msg_id = ntohs(*((u16 *)p));
1667 if (msg_id == reply_msg_id) {{
1668 vl_api_{r}_t *rmp = (vl_api_{r}_t *)p;
1669 vl_api_{r}_t_endian(rmp);
1670 cJSON_AddItemToArray(reply, vl_api_{r}_t_tojson(rmp));
1674 if (msg_id == details_msg_id) {{
1675 vl_api_{d}_t *rmp = (vl_api_{d}_t *)p;
1676 vl_api_{d}_t_endian(rmp);
1677 cJSON_AddItemToArray(reply, vl_api_{d}_t_tojson(rmp));
1686 if s.stream_message:
1687 write(gets_details_reply_template
1688 .format(n=s.caller, r=s.reply, N=s.caller.upper(),
1689 R=s.reply.upper(), d=s.stream_message,
1690 D=s.stream_message.upper()))
1692 write(dump_details_template.format(n=s.caller, r=s.reply,
1696 write(req_reply_template.format(n=s.caller, r=s.reply,
1701 def generate_c_test2_boilerplate(services, defines, module, stream):
1702 '''Generate code for VAT2 plugin.'''
1703 write = stream.write
1705 define_hash = {d.name: d for d in defines}
1709 #include <vlibapi/api.h>
1710 #include <vlibmemory/api.h>
1711 #include <vppinfra/error.h>
1712 #include <vnet/ip/ip_format_fns.h>
1713 #include <vnet/ethernet/ethernet_format_fns.h>
1715 #define vl_typedefs /* define message structures */
1716 #include <vlibmemory/vl_memory_api_h.h>
1717 #include <vlibmemory/vlib.api_types.h>
1718 #include <vlibmemory/vlib.api.h>
1721 #include "{module}.api_enum.h"
1722 #include "{module}.api_types.h"
1724 #define vl_endianfun /* define message structures */
1725 #include "{module}.api.h"
1728 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
1730 #include "{module}.api.h"
1733 #include "{module}.api_tojson.h"
1734 #include "{module}.api_fromjson.h"
1735 #include <vpp-api/client/vppapiclient.h>
1737 #include <vat2/vat2_helpers.h>
1741 write(hdr.format(module=module))
1744 if s.reply not in define_hash:
1746 c_test_api_service(s, s.stream, stream)
1748 write('void vat2_register_function(char *, cJSON * (*)(cJSON *), cJSON * (*)(void *), u32);\n')
1749 # write('__attribute__((constructor))')
1750 write('clib_error_t *\n')
1751 write('vat2_register_plugin (void) {\n')
1753 if s.reply not in define_hash:
1755 crc = define_hash[s.caller].crc
1756 write(' vat2_register_function("{n}", api_{n}, (cJSON * (*)(void *))vl_api_{n}_t_tojson, 0x{crc:08x});\n'
1757 .format(n=s.caller, crc=crc))
1758 write(' return 0;\n')
1763 # Plugin entry point
1765 def run(args, apifilename, s):
1766 '''Main plugin entry point.'''
1769 if not args.outputdir:
1770 sys.stderr.write('Missing --outputdir argument')
1773 basename = os.path.basename(apifilename)
1774 filename, _ = os.path.splitext(basename)
1775 modulename = filename.replace('.', '_')
1776 filename_enum = os.path.join(args.outputdir + '/' + basename + '_enum.h')
1777 filename_types = os.path.join(args.outputdir + '/' + basename + '_types.h')
1778 filename_c = os.path.join(args.outputdir + '/' + basename + '.c')
1779 filename_c_test = os.path.join(args.outputdir + '/' + basename + '_test.c')
1780 filename_c_test2 = (os.path.join(args.outputdir + '/' + basename +
1782 filename_c_tojson = (os.path.join(args.outputdir +
1783 '/' + basename + '_tojson.h'))
1784 filename_c_fromjson = (os.path.join(args.outputdir + '/' +
1785 basename + '_fromjson.h'))
1787 # Generate separate types file
1789 generate_include_types(s, modulename, st)
1790 with open(filename_types, 'w') as fd:
1792 shutil.copyfileobj(st, fd)
1795 # Generate separate enum file
1797 st.write('#ifndef included_{}_api_enum_h\n'.format(modulename))
1798 st.write('#define included_{}_api_enum_h\n'.format(modulename))
1799 generate_include_enum(s, modulename, st)
1800 generate_include_counters(s['Counters'], st)
1801 st.write('#endif\n')
1802 with open(filename_enum, 'w') as fd:
1804 shutil.copyfileobj(st, fd)
1807 # Generate separate C file
1809 generate_c_boilerplate(s['Service'], s['Define'], s['Counters'],
1810 s['file_crc'], modulename, st)
1811 with open(filename_c, 'w') as fd:
1813 shutil.copyfileobj(st, fd)
1816 # Generate separate C test file
1818 plugin = bool('plugin' in apifilename)
1819 generate_c_test_boilerplate(s['Service'], s['Define'],
1821 modulename, plugin, st)
1822 with open(filename_c_test, 'w') as fd:
1824 shutil.copyfileobj(st, fd)
1827 # Fully autogenerated VATv2 C test file
1829 generate_c_test2_boilerplate(s['Service'], s['Define'],
1831 with open(filename_c_test2, 'w') as fd:
1833 shutil.copyfileobj(st, fd)
1836 # Generate separate JSON file
1838 generate_tojson(s, modulename, st)
1839 with open(filename_c_tojson, 'w') as fd:
1841 shutil.copyfileobj(st, fd)
1844 generate_fromjson(s, modulename, st)
1845 with open(filename_c_fromjson, 'w') as fd:
1847 shutil.copyfileobj(st, fd)
1850 output = TOP_BOILERPLATE.format(datestring=DATESTRING,
1851 input_filename=basename)
1852 output += generate_imports(s['Import'])
1853 output += msg_ids(s)
1854 output += msg_names(s)
1855 output += msg_name_crc_list(s, filename)
1856 output += typedefs(modulename)
1857 printfun_types(s['types'], stream, modulename)
1858 printfun(s['Define'], stream, modulename)
1859 output += stream.getvalue()
1861 output += endianfun(s['types'] + s['Define'], modulename)
1862 output += version_tuple(s, basename)
1863 output += BOTTOM_BOILERPLATE.format(input_filename=basename,
1864 file_crc=s['file_crc'])