blob: ae47625a474b5f830fe0bc5f94d047c300cc6bfc [file] [log] [blame]
Ole Troandf87f802020-11-18 19:17:48 +01001#
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:
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
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.
14#
15
16#
17# Provide two classes FromJSON and TOJSON that converts between JSON and VPP's
18# binary API format
19#
20
21'''
22This module creates C code for core VPP, VPP plugins and client side VAT and
23VAT2 tests.
24'''
25
Ole Troan9d420872017-10-12 13:06:35 +020026import datetime
27import os
Nirmoy Dasc9f40222018-06-28 10:18:43 +020028import time
Ole Troan33a58172019-09-04 09:12:29 +020029import sys
30from io import StringIO
Ole Troan2a1ca782019-09-19 01:08:30 +020031import shutil
Ole Troan9d420872017-10-12 13:06:35 +020032
Paul Vinciguerra9046e442020-11-20 23:10:09 -050033process_imports = False
34
Paul Vinciguerraa51f9b32020-11-24 23:26:06 -050035
Ole Troandf87f802020-11-18 19:17:48 +010036###############################################################################
37class ToJSON():
38 '''Class to generate functions converting from VPP binary API to JSON.'''
39 _dispatch = {}
40 noprint_fields = {'_vl_msg_id': None,
41 'client_index': None,
42 'context': None}
43 is_number = {'u8': None,
44 'i8': None,
45 'u16': None,
46 'i16': None,
47 'u32': None,
48 'i32': None,
49 'u64': None,
50 'i64': None,
51 'f64': None,
52 }
53
54 def __init__(self, module, types, defines, imported_types, stream):
55 self.stream = stream
56 self.module = module
57 self.defines = defines
58 self.types = types
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}
62
63 def header(self):
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')
Filip Tehlar36217e32021-07-23 08:51:10 +000069 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')
Ole Troandf87f802020-11-18 19:17:48 +010073
74 def footer(self):
75 '''Output the bottom boilerplate.'''
76 write = self.stream.write
77 write('#endif\n')
78
Ole Troan18327be2021-01-12 21:49:38 +010079 def get_base_type(self, t):
Ole Troandf87f802020-11-18 19:17:48 +010080 vt_type = None
81 try:
82 vt = self.types_hash[t]
83 if vt.type == 'Using' and 'length' not in vt.alias:
84 vt_type = vt.alias['type']
85 except KeyError:
86 vt = t
Ole Troan18327be2021-01-12 21:49:38 +010087 return vt, vt_type
88
89 def get_json_func(self, t):
90 '''Given the type, returns the function to use to create a
91 cJSON object'''
92 vt, vt_type = self.get_base_type(t)
Ole Troandf87f802020-11-18 19:17:48 +010093
94 if t in self.is_number or vt_type in self.is_number:
95 return 'cJSON_AddNumberToObject', '', False
96 if t == 'bool':
97 return 'cJSON_AddBoolToObject', '', False
98
99 # Lookup type name check if it's enum
Paul Vinciguerraa51f9b32020-11-24 23:26:06 -0500100 if vt.type == 'Enum' or vt.type == 'EnumFlag':
Ole Troandf87f802020-11-18 19:17:48 +0100101 return '{t}_tojson'.format(t=t), '', True
102 return '{t}_tojson'.format(t=t), '&', True
103
104 def get_json_array_func(self, t):
105 '''Given a type returns the function to create a cJSON object
106 for arrays.'''
107 if t in self.is_number:
108 return 'cJSON_CreateNumber', ''
109 if t == 'bool':
110 return 'cJSON_CreateBool', ''
Ole Troan18327be2021-01-12 21:49:38 +0100111 vt, vt_type = self.get_base_type(t)
112 if vt.type == 'Enum' or vt.type == 'EnumFlag':
113 return '{t}_tojson'.format(t=t), ''
Ole Troandf87f802020-11-18 19:17:48 +0100114 return '{t}_tojson'.format(t=t), '&'
115
116 def print_string(self, o):
117 '''Create cJSON object from vl_api_string_t'''
118 write = self.stream.write
119 if o.modern_vla:
120 write(' vl_api_string_cJSON_AddToObject(o, "{n}", &a->{n});\n'
121 .format(n=o.fieldname))
122 else:
123
124 write(' cJSON_AddStringToObject(o, "{n}", (char *)a->{n});\n'
125 .format(n=o.fieldname))
126
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:
131 return
132
133 f, p, newobj = self.get_json_func(o.fieldtype)
134
135 if newobj:
136 write(' cJSON_AddItemToObject(o, "{n}", {f}({p}a->{n}));\n'
137 .format(f=f, p=p, n=o.fieldname))
138 else:
139 write(' {f}(o, "{n}", {p}a->{n});\n'
140 .format(f=f, p=p, n=o.fieldname))
141
142 _dispatch['Field'] = print_field
143
144 def print_array(self, o):
145 '''Converts a VPP API array to cJSON array.'''
146 write = self.stream.write
147
148 forloop = '''\
149 {{
150 int i;
151 cJSON *array = cJSON_AddArrayToObject(o, "{n}");
152 for (i = 0; i < {lfield}; i++) {{
153 cJSON_AddItemToArray(array, {f}({p}a->{n}[i]));
154 }}
155 }}
156'''
157
158 if o.fieldtype == 'string':
159 self.print_string(o)
160 return
161
162 lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
163 if o.fieldtype == 'u8':
164 write(' {\n')
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')
172 write(' }\n')
173 return
174
175 f, p = self.get_json_array_func(o.fieldtype)
176 write(forloop.format(lfield=lfield,
177 t=o.fieldtype,
178 n=o.fieldname,
179 f=f,
180 p=p))
181
182 _dispatch['Array'] = print_array
183
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))
189
190 write(" switch(a) {\n")
191 for b in o.block:
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')
195 write(' }\n')
196 write(' return 0;\n')
197 write('}\n')
198
199 _dispatch['Enum'] = print_enum
Ole Troan793be462020-12-04 13:15:30 +0100200
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')
207
208 for b in o.block:
209 write(' if (a & {})\n'.format(b[0]))
210 write(' cJSON_AddItemToArray(array, cJSON_CreateString("{}"));\n'.format(b[0]))
211 write(' return array;\n')
212 write('}\n')
213
214 _dispatch['EnumFlag'] = print_enum_flag
Ole Troandf87f802020-11-18 19:17:48 +0100215
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')
222
223 for t in o.block:
224 self._dispatch[t.type](self, t)
225
226 write(' return o;\n')
227 write('}\n')
228
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'
236 .format(o.name))
Filip Tehlar36217e32021-07-23 08:51:10 +0000237 write(' cJSON_AddStringToObject(o, "_crc", "{crc:08x}");\n'
238 .format(crc=o.crc))
Ole Troandf87f802020-11-18 19:17:48 +0100239
240 for t in o.block:
241 self._dispatch[t.type](self, t)
242
243 write(' return o;\n')
244 write('}\n')
245
246 def print_using(self, o):
247 '''Create cJSON (dictionary) object from VPP API aliased type'''
248 if o.manual_print:
249 return
250
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))
254
255 write(' u8 *s = format(0, "%U", format_vl_api_{}_t, a);\n'
256 .format(o.name))
257 write(' cJSON *o = cJSON_CreateString((char *)s);\n')
258 write(' vec_free(s);\n')
259 write(' return o;\n')
260 write('}\n')
261
262 _dispatch['Typedef'] = print_typedef
263 _dispatch['Define'] = print_define
264 _dispatch['Using'] = print_using
265 _dispatch['Union'] = print_typedef
266
267 def generate_function(self, t):
268 '''Main entry point'''
269 write = self.stream.write
270 if t.manual_print:
271 write('/* Manual print {} */\n'.format(t.name))
272 return
273 self._dispatch[t.type](self, t)
274
275 def generate_types(self):
276 '''Main entry point'''
277 for t in self.types:
278 self.generate_function(t)
279
280 def generate_defines(self):
281 '''Main entry point'''
282 for t in self.defines:
283 self.generate_function(t)
284
285
286class FromJSON():
287 '''
288 Parse JSON objects into VPP API binary message structures.
289 '''
290 _dispatch = {}
291 noprint_fields = {'_vl_msg_id': None,
292 'client_index': None,
293 'context': None}
294 is_number = {'u8': None,
295 'i8': None,
296 'u16': None,
297 'i16': None,
298 'u32': None,
299 'i32': None,
300 'u64': None,
301 'i64': None,
302 'f64': None,
303 }
304
305 def __init__(self, module, types, defines, imported_types, stream):
306 self.stream = stream
307 self.module = module
308 self.defines = defines
309 self.types = types
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}
313
314 def header(self):
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')
Filip Tehlar36217e32021-07-23 08:51:10 +0000320 write('#include <vppinfra/jsonformat.h>\n\n')
Ole Troanfb0afab2021-02-11 11:13:46 +0100321 write('#pragma GCC diagnostic ignored "-Wunused-label"\n')
Ole Troandf87f802020-11-18 19:17:48 +0100322
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:
326 return True
327 if t == 'bool':
328 return True
329 return False
330
331 def footer(self):
332 '''Output the bottom boilerplate.'''
333 write = self.stream.write
334 write('#endif\n')
335
336 def print_string(self, o, toplevel=False):
337 '''Convert JSON string to vl_api_string_t'''
338 write = self.stream.write
339
Ole Troanfb0afab2021-02-11 11:13:46 +0100340 msgvar = "a" if toplevel else "*mp"
Ole Troandf87f802020-11-18 19:17:48 +0100341 msgsize = "l" if toplevel else "*len"
342
343 if o.modern_vla:
344 write(' char *p = cJSON_GetStringValue(item);\n')
345 write(' size_t plen = strlen(p);\n')
Filip Tehlar36217e32021-07-23 08:51:10 +0000346 write(' {msgvar} = cJSON_realloc({msgvar}, {msgsize} + plen, {msgsize});\n'
Ole Troandf87f802020-11-18 19:17:48 +0100347 .format(msgvar=msgvar, msgsize=msgsize))
Ole Troanfb0afab2021-02-11 11:13:46 +0100348 write(' if ({msgvar} == 0) goto error;\n'.format(msgvar=msgvar))
Ole Troandf87f802020-11-18 19:17:48 +0100349 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))
353 else:
354 write(' strncpy_s((char *)a->{n}, sizeof(a->{n}), '
355 'cJSON_GetStringValue(item), sizeof(a->{n}) - 1);\n'
356 .format(n=o.fieldname))
357
358 def print_field(self, o, toplevel=False):
359 '''Called for every field in a typedef or define.'''
360 write = self.stream.write
Ole Troandf87f802020-11-18 19:17:48 +0100361 if o.fieldname in self.noprint_fields:
362 return
363 is_bt = self.is_base_type(o.fieldtype)
364 t = 'vl_api_{}'.format(o.fieldtype) if is_bt else o.fieldtype
365
Ole Troanfb0afab2021-02-11 11:13:46 +0100366 msgvar = "(void **)&a" if toplevel else "mp"
Ole Troandf87f802020-11-18 19:17:48 +0100367 msgsize = "&l" if toplevel else "len"
368
369 if is_bt:
370 write(' vl_api_{t}_fromjson(item, &a->{n});\n'
371 .format(t=o.fieldtype, n=o.fieldname))
372 else:
Ole Troanfb0afab2021-02-11 11:13:46 +0100373 write(' if ({t}_fromjson({msgvar}, '
374 '{msgsize}, item, &a->{n}) < 0) goto error;\n'
Ole Troandf87f802020-11-18 19:17:48 +0100375 .format(t=t, n=o.fieldname, msgvar=msgvar, msgsize=msgsize))
Ole Troandf87f802020-11-18 19:17:48 +0100376
377 _dispatch['Field'] = print_field
378
379 def print_array(self, o, toplevel=False):
380 '''Convert JSON array to VPP API array'''
381 write = self.stream.write
382
383 forloop = '''\
384 {{
385 int i;
386 cJSON *array = cJSON_GetObjectItem(o, "{n}");
387 int size = cJSON_GetArraySize(array);
Ole Troan384c72f2021-02-17 13:46:54 +0100388 if (size != {lfield}) goto error;
Ole Troandf87f802020-11-18 19:17:48 +0100389 for (i = 0; i < size; i++) {{
390 cJSON *e = cJSON_GetArrayItem(array, i);
391 {call}
392 }}
393 }}
394'''
395 forloop_vla = '''\
396 {{
397 int i;
398 cJSON *array = cJSON_GetObjectItem(o, "{n}");
399 int size = cJSON_GetArraySize(array);
400 {lfield} = size;
Filip Tehlar36217e32021-07-23 08:51:10 +0000401 {realloc} = cJSON_realloc({realloc}, {msgsize} + sizeof({t}) * size, {msgsize});
Ole Troancf0102b2021-02-12 11:48:12 +0100402 {t} *d = (void *){realloc} + {msgsize};
Ole Troandf87f802020-11-18 19:17:48 +0100403 {msgsize} += sizeof({t}) * size;
404 for (i = 0; i < size; i++) {{
405 cJSON *e = cJSON_GetArrayItem(array, i);
406 {call}
407 }}
408 }}
409'''
410 t = o.fieldtype
411 if o.fieldtype == 'string':
412 self.print_string(o, toplevel)
413 return
414
415 lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
Ole Troanfb0afab2021-02-11 11:13:46 +0100416 msgvar = "(void **)&a" if toplevel else "mp"
Ole Troancf0102b2021-02-12 11:48:12 +0100417 realloc = "a" if toplevel else "*mp"
Ole Troandf87f802020-11-18 19:17:48 +0100418 msgsize = "l" if toplevel else "*len"
419
420 if o.fieldtype == 'u8':
421 if o.lengthfield:
422 write(' s = u8string_fromjson(o, "{}");\n'
423 .format(o.fieldname))
Ole Troan93c4b1b2021-02-16 18:09:51 +0100424 write(' if (!s) goto error;\n')
Ole Troandf87f802020-11-18 19:17:48 +0100425 write(' {} = vec_len(s);\n'.format(lfield))
426
Filip Tehlar36217e32021-07-23 08:51:10 +0000427 write(' {realloc} = cJSON_realloc({realloc}, {msgsize} + '
428 'vec_len(s), {msgsize});\n'.format(msgvar=msgvar, msgsize=msgsize, realloc=realloc))
Ole Troancf0102b2021-02-12 11:48:12 +0100429 write(' memcpy((void *){realloc} + {msgsize}, s, '
430 'vec_len(s));\n'.format(realloc=realloc, msgsize=msgsize))
Ole Troandf87f802020-11-18 19:17:48 +0100431 write(' {msgsize} += vec_len(s);\n'.format(msgsize=msgsize))
432
433 write(' vec_free(s);\n')
434 else:
Ole Troan93c4b1b2021-02-16 18:09:51 +0100435 write(' if (u8string_fromjson2(o, "{n}", a->{n}) < 0) goto error;\n'
Ole Troandf87f802020-11-18 19:17:48 +0100436 .format(n=o.fieldname))
437 return
438
439 is_bt = self.is_base_type(o.fieldtype)
440
441 if o.lengthfield:
442 if is_bt:
443 call = ('vl_api_{t}_fromjson(e, &d[i]);'
444 .format(t=o.fieldtype))
445 else:
Ole Troan93c4b1b2021-02-16 18:09:51 +0100446 call = ('if ({t}_fromjson({msgvar}, len, e, &d[i]) < 0) goto error; '
Ole Troandf87f802020-11-18 19:17:48 +0100447 .format(t=o.fieldtype, msgvar=msgvar))
448 write(forloop_vla.format(lfield=lfield,
449 t=o.fieldtype,
450 n=o.fieldname,
451 call=call,
Ole Troancf0102b2021-02-12 11:48:12 +0100452 realloc=realloc,
Ole Troandf87f802020-11-18 19:17:48 +0100453 msgsize=msgsize))
454 else:
455 if is_bt:
456 call = ('vl_api_{t}_fromjson(e, &a->{n}[i]);'
457 .format(t=t, n=o.fieldname))
458 else:
Ole Troanfb0afab2021-02-11 11:13:46 +0100459 call = ('if ({}_fromjson({}, len, e, &a->{}[i]) < 0) goto error;'
Ole Troandf87f802020-11-18 19:17:48 +0100460 .format(t, msgvar, o.fieldname))
461 write(forloop.format(lfield=lfield,
462 t=t,
463 n=o.fieldname,
464 call=call,
465 msgvar=msgvar,
Ole Troancf0102b2021-02-12 11:48:12 +0100466 realloc=realloc,
Ole Troandf87f802020-11-18 19:17:48 +0100467 msgsize=msgsize))
468
469 _dispatch['Array'] = print_array
470
471 def print_enum(self, o):
472 '''Convert to JSON enum(string) to VPP API enum (int)'''
473 write = self.stream.write
Ole Troanfb0afab2021-02-11 11:13:46 +0100474 write('static inline int vl_api_{n}_t_fromjson'
475 '(void **mp, int *len, cJSON *o, vl_api_{n}_t *a) {{\n'
Ole Troandf87f802020-11-18 19:17:48 +0100476 .format(n=o.name))
477 write(' char *p = cJSON_GetStringValue(o);\n')
478 for b in o.block:
Ole Troanfb0afab2021-02-11 11:13:46 +0100479 write(' if (strcmp(p, "{}") == 0) {{*a = {}; return 0;}}\n'
Ole Troandf87f802020-11-18 19:17:48 +0100480 .format(b[0], b[1]))
Ole Troanfb0afab2021-02-11 11:13:46 +0100481 write(' *a = 0;\n')
482 write(' return -1;\n')
Ole Troandf87f802020-11-18 19:17:48 +0100483 write('}\n')
484
485 _dispatch['Enum'] = print_enum
Ole Troan793be462020-12-04 13:15:30 +0100486
487 def print_enum_flag(self, o):
488 '''Convert to JSON enum(string) to VPP API enum (int)'''
489 write = self.stream.write
Ole Troancf0102b2021-02-12 11:48:12 +0100490 write('static inline int vl_api_{n}_t_fromjson '
491 '(void **mp, int *len, cJSON *o, vl_api_{n}_t *a) {{\n'
Ole Troan793be462020-12-04 13:15:30 +0100492 .format(n=o.name))
493 write(' int i;\n')
494 write(' *a = 0;\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')
Ole Troancf0102b2021-02-12 11:48:12 +0100498 write(' if (!p) return -1;\n')
Ole Troan793be462020-12-04 13:15:30 +0100499 for b in o.block:
500 write(' if (strcmp(p, "{}") == 0) *a |= {};\n'
501 .format(b[0], b[1]))
502 write(' }\n')
Ole Troancf0102b2021-02-12 11:48:12 +0100503 write(' return 0;\n')
Ole Troan793be462020-12-04 13:15:30 +0100504 write('}\n')
505
506 _dispatch['EnumFlag'] = print_enum_flag
Ole Troandf87f802020-11-18 19:17:48 +0100507
508 def print_typedef(self, o):
509 '''Convert from JSON object to VPP API binary representation'''
510 write = self.stream.write
511
Ole Troanfb0afab2021-02-11 11:13:46 +0100512 write('static inline int vl_api_{name}_t_fromjson (void **mp, '
Ole Troandf87f802020-11-18 19:17:48 +0100513 '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')
517 for t in o.block:
518 if t.type == 'Field' and t.is_lengthfield:
519 continue
Ole Troanfb0afab2021-02-11 11:13:46 +0100520 write('\n item = cJSON_GetObjectItem(o, "{}");\n'
Ole Troandf87f802020-11-18 19:17:48 +0100521 .format(t.fieldname))
Ole Troanfb0afab2021-02-11 11:13:46 +0100522 write(' if (!item) goto error;\n')
Ole Troandf87f802020-11-18 19:17:48 +0100523 self._dispatch[t.type](self, t)
524
Ole Troanfb0afab2021-02-11 11:13:46 +0100525 write('\n return 0;\n')
526 write('\n error:\n')
527 write(' return -1;\n')
Ole Troandf87f802020-11-18 19:17:48 +0100528 write('}\n')
529
530 def print_union(self, o):
531 '''Convert JSON object to VPP API binary union'''
532 write = self.stream.write
533
Ole Troanfb0afab2021-02-11 11:13:46 +0100534 write('static inline int vl_api_{name}_t_fromjson (void **mp, '
Ole Troandf87f802020-11-18 19:17:48 +0100535 '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')
539 for t in o.block:
540 if t.type == 'Field' and t.is_lengthfield:
541 continue
542 write(' item = cJSON_GetObjectItem(o, "{}");\n'
543 .format(t.fieldname))
544 write(' if (item) {\n')
545 self._dispatch[t.type](self, t)
546 write(' };\n')
Ole Troanfb0afab2021-02-11 11:13:46 +0100547 write('\n return 0;\n')
548 write('\n error:\n')
549 write(' return -1;\n')
Ole Troandf87f802020-11-18 19:17:48 +0100550 write('}\n')
551
552 def print_define(self, o):
553 '''Convert JSON object to VPP API message'''
554 write = self.stream.write
Ole Troan93c4b1b2021-02-16 18:09:51 +0100555 error = 0
Ole Troandf87f802020-11-18 19:17:48 +0100556 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))
Filip Tehlar36217e32021-07-23 08:51:10 +0000561 write(' vl_api_{}_t *a = cJSON_malloc(l);\n'.format(o.name))
Ole Troanfb0afab2021-02-11 11:13:46 +0100562 write('\n')
Ole Troandf87f802020-11-18 19:17:48 +0100563
564 for t in o.block:
565 if t.fieldname in self.noprint_fields:
566 continue
567 if t.type == 'Field' and t.is_lengthfield:
568 continue
Ole Troandf87f802020-11-18 19:17:48 +0100569 write(' item = cJSON_GetObjectItem(o, "{}");\n'
570 .format(t.fieldname))
Ole Troanfb0afab2021-02-11 11:13:46 +0100571 write(' if (!item) goto error;\n')
Ole Troan93c4b1b2021-02-16 18:09:51 +0100572 error += 1
Ole Troandf87f802020-11-18 19:17:48 +0100573 self._dispatch[t.type](self, t, toplevel=True)
574 write('\n')
575
Ole Troandf87f802020-11-18 19:17:48 +0100576 write(' *len = l;\n')
577 write(' return a;\n')
Ole Troan93c4b1b2021-02-16 18:09:51 +0100578
579 if error:
580 write('\n error:\n')
Filip Tehlar36217e32021-07-23 08:51:10 +0000581 write(' cJSON_free(a);\n')
Ole Troan93c4b1b2021-02-16 18:09:51 +0100582 write(' return 0;\n')
Ole Troandf87f802020-11-18 19:17:48 +0100583 write('}\n')
584
585 def print_using(self, o):
586 '''Convert JSON field to VPP type alias'''
587 write = self.stream.write
588
589 if o.manual_print:
590 return
591
592 t = o.using
Ole Troancf0102b2021-02-12 11:48:12 +0100593 write('static inline int vl_api_{name}_t_fromjson (void **mp, '
Ole Troandf87f802020-11-18 19:17:48 +0100594 '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']))
602 else:
603 write(' vl_api_{t}_fromjson(o, ({t} *)a);\n'
604 .format(t=t.fieldtype))
605
Ole Troancf0102b2021-02-12 11:48:12 +0100606 write(' return 0;\n')
Ole Troandf87f802020-11-18 19:17:48 +0100607 write('}\n')
608
609 _dispatch['Typedef'] = print_typedef
610 _dispatch['Define'] = print_define
611 _dispatch['Using'] = print_using
612 _dispatch['Union'] = print_union
613
614 def generate_function(self, t):
615 '''Main entry point'''
616 write = self.stream.write
617 if t.manual_print:
618 write('/* Manual print {} */\n'.format(t.name))
619 return
620 self._dispatch[t.type](self, t)
621
622 def generate_types(self):
623 '''Main entry point'''
624 for t in self.types:
625 self.generate_function(t)
626
627 def generate_defines(self):
628 '''Main entry point'''
629 for t in self.defines:
630 self.generate_function(t)
631
632
633def generate_tojson(s, modulename, stream):
634 '''Generate all functions to convert from API to JSON'''
635 write = stream.write
636
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))
641
642 pp = ToJSON(modulename, s['types'], s['Define'], s['imported']['types'],
643 stream)
644 pp.header()
645 pp.generate_types()
646 pp.generate_defines()
647 pp.footer()
648 return ''
649
650
651def generate_fromjson(s, modulename, stream):
652 '''Generate all functions to convert from JSON to API'''
653 write = stream.write
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))
658
659 pp = FromJSON(modulename, s['types'], s['Define'], s['imported']['types'],
660 stream)
661 pp.header()
662 pp.generate_types()
663 pp.generate_defines()
664 pp.footer()
665
666 return ''
667
668###############################################################################
669
670
671DATESTRING = datetime.datetime.utcfromtimestamp(
Ole Troan413f4a52018-11-28 11:36:05 +0100672 int(os.environ.get('SOURCE_DATE_EPOCH', time.time())))
Ole Troandf87f802020-11-18 19:17:48 +0100673TOP_BOILERPLATE = '''\
Ole Troan9d420872017-10-12 13:06:35 +0200674/*
675 * VLIB API definitions {datestring}
676 * Input file: {input_filename}
677 * Automatically generated: please edit the input file NOT this file!
678 */
679
Ole Troan288e0932019-05-29 12:30:05 +0200680#include <stdbool.h>
Ole Troan9d420872017-10-12 13:06:35 +0200681#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 */
687#else
688#warning no content included from {input_filename}
689#endif
690
691#define VL_API_PACKED(x) x __attribute__ ((packed))
692'''
693
Ole Troandf87f802020-11-18 19:17:48 +0100694BOTTOM_BOILERPLATE = '''\
Ole Troan9d420872017-10-12 13:06:35 +0200695/****** API CRC (whole file) *****/
696
697#ifdef vl_api_version
698vl_api_version({input_filename}, {file_crc:#08x})
699
700#endif
701'''
702
703
704def msg_ids(s):
Ole Troandf87f802020-11-18 19:17:48 +0100705 '''Generate macro to map API message id to handler'''
Ole Troan9d420872017-10-12 13:06:35 +0200706 output = '''\
707
708/****** Message ID / handler enum ******/
709
710#ifdef vl_msg_id
711'''
712
Ole Troan2c2feab2018-04-24 00:02:37 -0400713 for t in s['Define']:
Ole Troan9d420872017-10-12 13:06:35 +0200714 output += "vl_msg_id(VL_API_%s, vl_api_%s_t_handler)\n" % \
715 (t.name.upper(), t.name)
716 output += "#endif"
717
718 return output
719
720
721def msg_names(s):
Ole Troandf87f802020-11-18 19:17:48 +0100722 '''Generate calls to name mapping macro'''
Ole Troan9d420872017-10-12 13:06:35 +0200723 output = '''\
724
725/****** Message names ******/
726
727#ifdef vl_msg_name
728'''
729
Ole Troan2c2feab2018-04-24 00:02:37 -0400730 for t in s['Define']:
Ole Troan9d420872017-10-12 13:06:35 +0200731 dont_trace = 0 if t.dont_trace else 1
732 output += "vl_msg_name(vl_api_%s_t, %d)\n" % (t.name, dont_trace)
733 output += "#endif"
734
735 return output
736
737
738def msg_name_crc_list(s, suffix):
Ole Troandf87f802020-11-18 19:17:48 +0100739 '''Generate list of names to CRC mappings'''
Ole Troan9d420872017-10-12 13:06:35 +0200740 output = '''\
741
742/****** Message name, crc list ******/
743
744#ifdef vl_msg_name_crc_list
745'''
746 output += "#define foreach_vl_msg_name_crc_%s " % suffix
747
Ole Troan2c2feab2018-04-24 00:02:37 -0400748 for t in s['Define']:
Ole Troan9d420872017-10-12 13:06:35 +0200749 output += "\\\n_(VL_API_%s, %s, %08x) " % \
750 (t.name.upper(), t.name, t.crc)
751 output += "\n#endif"
752
753 return output
754
755
Ole Troan413f4a52018-11-28 11:36:05 +0100756def api2c(fieldtype):
Ole Troandf87f802020-11-18 19:17:48 +0100757 '''Map between API type names and internal VPP type names'''
Ole Troan413f4a52018-11-28 11:36:05 +0100758 mappingtable = {'string': 'vl_api_string_t', }
759 if fieldtype in mappingtable:
760 return mappingtable[fieldtype]
761 return fieldtype
762
763
Ole Troan2a1ca782019-09-19 01:08:30 +0200764def typedefs(filename):
Ole Troandf87f802020-11-18 19:17:48 +0100765 '''Include in the main files to the types file'''
Ole Troan2a1ca782019-09-19 01:08:30 +0200766 output = '''\
Ole Troan9d420872017-10-12 13:06:35 +0200767
768/****** Typedefs ******/
769
770#ifdef vl_typedefs
Ole Troan2a1ca782019-09-19 01:08:30 +0200771#include "{include}.api_types.h"
772#endif
773'''.format(include=filename)
Ole Troan9d420872017-10-12 13:06:35 +0200774 return output
775
776
Ole Troandf87f802020-11-18 19:17:48 +0100777FORMAT_STRINGS = {'u8': '%u',
Ole Troan33a58172019-09-04 09:12:29 +0200778 'bool': '%u',
Ole Troan9d420872017-10-12 13:06:35 +0200779 'i8': '%d',
780 'u16': '%u',
781 'i16': '%d',
782 'u32': '%u',
783 'i32': '%ld',
784 'u64': '%llu',
Paul Vinciguerrab307c712019-11-23 10:13:39 -0500785 'i64': '%lld',
Ole Troan33a58172019-09-04 09:12:29 +0200786 'f64': '%.2f'}
787
Ole Troan9d420872017-10-12 13:06:35 +0200788
Ole Troan33a58172019-09-04 09:12:29 +0200789class Printfun():
Ole Troandf87f802020-11-18 19:17:48 +0100790 '''Functions for pretty printing VPP API messages'''
Ole Troan33a58172019-09-04 09:12:29 +0200791 _dispatch = {}
Ole Troandf87f802020-11-18 19:17:48 +0100792 noprint_fields = {'_vl_msg_id': None,
793 'client_index': None,
794 'context': None}
Ole Troan33a58172019-09-04 09:12:29 +0200795
796 def __init__(self, stream):
797 self.stream = stream
798
Ole Troandf87f802020-11-18 19:17:48 +0100799 @staticmethod
800 def print_string(o, stream):
801 '''Pretty print a vl_api_string_t'''
Ole Troan33a58172019-09-04 09:12:29 +0200802 write = stream.write
803 if o.modern_vla:
804 write(' if (vl_api_string_len(&a->{f}) > 0) {{\n'
805 .format(f=o.fieldname))
Jakub Grajciar2dbee932020-02-07 11:30:26 +0100806 write(' s = format(s, "\\n%U{f}: %U", '
Ole Troan33a58172019-09-04 09:12:29 +0200807 'format_white_space, indent, '
Jakub Grajciar2dbee932020-02-07 11:30:26 +0100808 'vl_api_format_string, (&a->{f}));\n'.format(f=o.fieldname))
Ole Troan33a58172019-09-04 09:12:29 +0200809 write(' } else {\n')
810 write(' s = format(s, "\\n%U{f}:", '
811 'format_white_space, indent);\n'.format(f=o.fieldname))
812 write(' }\n')
813 else:
814 write(' s = format(s, "\\n%U{f}: %s", '
815 'format_white_space, indent, a->{f});\n'
816 .format(f=o.fieldname))
817
818 def print_field(self, o, stream):
Ole Troandf87f802020-11-18 19:17:48 +0100819 '''Pretty print API field'''
Ole Troan33a58172019-09-04 09:12:29 +0200820 write = stream.write
Ole Troandf87f802020-11-18 19:17:48 +0100821 if o.fieldname in self.noprint_fields:
Ole Troan33a58172019-09-04 09:12:29 +0200822 return
Ole Troandf87f802020-11-18 19:17:48 +0100823 if o.fieldtype in FORMAT_STRINGS:
824 f = FORMAT_STRINGS[o.fieldtype]
Ole Troan33a58172019-09-04 09:12:29 +0200825 write(' s = format(s, "\\n%U{n}: {f}", '
826 'format_white_space, indent, a->{n});\n'
827 .format(n=o.fieldname, f=f))
828 else:
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))
833
834 _dispatch['Field'] = print_field
835
836 def print_array(self, o, stream):
Ole Troandf87f802020-11-18 19:17:48 +0100837 '''Pretty print API array'''
Ole Troan33a58172019-09-04 09:12:29 +0200838 write = stream.write
839
840 forloop = '''\
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);
844 }}
845'''
846
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]);
851 }}
852'''
853
854 if o.fieldtype == 'string':
Ole Troandf87f802020-11-18 19:17:48 +0100855 self.print_string(o, stream)
856 return
Ole Troan33a58172019-09-04 09:12:29 +0200857
858 if o.fieldtype == 'u8':
859 if o.lengthfield:
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))
863 else:
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))
867 return
868
869 lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
Ole Troandf87f802020-11-18 19:17:48 +0100870 if o.fieldtype in FORMAT_STRINGS:
Ole Troan33a58172019-09-04 09:12:29 +0200871 write(forloop_format.format(lfield=lfield,
Ole Troandf87f802020-11-18 19:17:48 +0100872 t=FORMAT_STRINGS[o.fieldtype],
Ole Troan33a58172019-09-04 09:12:29 +0200873 n=o.fieldname))
874 else:
875 write(forloop.format(lfield=lfield, t=o.fieldtype, n=o.fieldname))
876
877 _dispatch['Array'] = print_array
878
Ole Troandf87f802020-11-18 19:17:48 +0100879 @staticmethod
880 def print_alias(k, v, stream):
881 '''Pretty print type alias'''
Ole Troan33a58172019-09-04 09:12:29 +0200882 write = stream.write
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']))
Ole Troandf87f802020-11-18 19:17:48 +0100887 elif v.alias['type'] in FORMAT_STRINGS:
Ole Troan33a58172019-09-04 09:12:29 +0200888 write(' return format(s, "{}", *a);\n'
Ole Troandf87f802020-11-18 19:17:48 +0100889 .format(FORMAT_STRINGS[v.alias['type']]))
Ole Troan33a58172019-09-04 09:12:29 +0200890 else:
Ole Troan75761b92019-09-11 17:49:08 +0200891 write(' return format(s, "{} (print not implemented)");\n'
Ole Troan33a58172019-09-04 09:12:29 +0200892 .format(k))
893
Ole Troandf87f802020-11-18 19:17:48 +0100894 @staticmethod
895 def print_enum(o, stream):
896 '''Pretty print API enum'''
Ole Troan33a58172019-09-04 09:12:29 +0200897 write = stream.write
898 write(" switch(*a) {\n")
899 for b in o:
900 write(" case %s:\n" % b[1])
901 write(' return format(s, "{}");\n'.format(b[0]))
902 write(' }\n')
903
904 _dispatch['Enum'] = print_enum
Paul Vinciguerraa51f9b32020-11-24 23:26:06 -0500905 _dispatch['EnumFlag'] = print_enum
Ole Troan33a58172019-09-04 09:12:29 +0200906
907 def print_obj(self, o, stream):
Ole Troandf87f802020-11-18 19:17:48 +0100908 '''Entry point'''
Ole Troan33a58172019-09-04 09:12:29 +0200909 write = stream.write
910
911 if o.type in self._dispatch:
912 self._dispatch[o.type](self, o, stream)
913 else:
914 write(' s = format(s, "\\n{} {} {} (print not implemented");\n'
915 .format(o.type, o.fieldtype, o.fieldname))
916
917
918def printfun(objs, stream, modulename):
Ole Troandf87f802020-11-18 19:17:48 +0100919 '''Main entry point for pretty print function generation'''
Ole Troan33a58172019-09-04 09:12:29 +0200920 write = stream.write
921
922 h = '''\
Ole Troan9d420872017-10-12 13:06:35 +0200923/****** Print functions *****/
924#ifdef vl_printfun
Ole Troan33a58172019-09-04 09:12:29 +0200925#ifndef included_{module}_printfun
926#define included_{module}_printfun
Ole Troan9d420872017-10-12 13:06:35 +0200927
928#ifdef LP64
929#define _uword_fmt \"%lld\"
930#define _uword_cast (long long)
931#else
932#define _uword_fmt \"%ld\"
933#define _uword_cast long
934#endif
935
Filip Tehlar36217e32021-07-23 08:51:10 +0000936#include "{module}.api_tojson.h"
937#include "{module}.api_fromjson.h"
938
Ole Troan9d420872017-10-12 13:06:35 +0200939'''
Ole Troan33a58172019-09-04 09:12:29 +0200940
941 signature = '''\
Filip Tehlar36217e32021-07-23 08:51:10 +0000942static inline void *vl_api_{name}_t_print{suffix} (vl_api_{name}_t *a, void *handle)
Ole Troan33a58172019-09-04 09:12:29 +0200943{{
944 u8 *s = 0;
945 u32 indent __attribute__((unused)) = 2;
946 int i __attribute__((unused));
947'''
948
949 h = h.format(module=modulename)
950 write(h)
951
952 pp = Printfun(stream)
953 for t in objs:
954 if t.manual_print:
955 write("/***** manual: vl_api_%s_t_print *****/\n\n" % t.name)
956 continue
Filip Tehlar36217e32021-07-23 08:51:10 +0000957 write(signature.format(name=t.name, suffix=''))
Ole Troan33a58172019-09-04 09:12:29 +0200958 write(' /* Message definition: vl_api_{}_t: */\n'.format(t.name))
959 write(" s = format(s, \"vl_api_%s_t:\");\n" % t.name)
960 for o in t.block:
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')
966 write('}\n\n')
967
Filip Tehlar36217e32021-07-23 08:51:10 +0000968 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')
976 write('}\n\n');
977
Ole Troan33a58172019-09-04 09:12:29 +0200978 write("\n#endif")
979 write("\n#endif /* vl_printfun */\n")
980
981 return ''
982
983
Ole Troan75761b92019-09-11 17:49:08 +0200984def printfun_types(objs, stream, modulename):
Ole Troandf87f802020-11-18 19:17:48 +0100985 '''Pretty print API types'''
Ole Troan33a58172019-09-04 09:12:29 +0200986 write = stream.write
987 pp = Printfun(stream)
988
989 h = '''\
990/****** Print functions *****/
991#ifdef vl_printfun
992#ifndef included_{module}_printfun_types
993#define included_{module}_printfun_types
994
995'''
996 h = h.format(module=modulename)
997 write(h)
998
999 signature = '''\
1000static inline u8 *format_vl_api_{name}_t (u8 *s, va_list * args)
1001{{
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));
1005 indent += 2;
1006'''
1007
Ole Troan2c2feab2018-04-24 00:02:37 -04001008 for t in objs:
Paul Vinciguerraa51f9b32020-11-24 23:26:06 -05001009 if t.__class__.__name__ == 'Enum' or t.__class__.__name__ == 'EnumFlag':
Ole Troan33a58172019-09-04 09:12:29 +02001010 write(signature.format(name=t.name))
1011 pp.print_enum(t.block, stream)
1012 write(' return s;\n')
1013 write('}\n\n')
Ole Troan2c2feab2018-04-24 00:02:37 -04001014 continue
Ole Troan33a58172019-09-04 09:12:29 +02001015
Ole Troan9d420872017-10-12 13:06:35 +02001016 if t.manual_print:
Ole Troan33a58172019-09-04 09:12:29 +02001017 write("/***** manual: vl_api_%s_t_print *****/\n\n" % t.name)
Ole Troan9d420872017-10-12 13:06:35 +02001018 continue
Ole Troan9d420872017-10-12 13:06:35 +02001019
Ole Troan75761b92019-09-11 17:49:08 +02001020 if t.__class__.__name__ == 'Using':
1021 write(signature.format(name=t.name))
1022 pp.print_alias(t.name, t, stream)
1023 write('}\n\n')
1024 continue
1025
Ole Troan33a58172019-09-04 09:12:29 +02001026 write(signature.format(name=t.name))
Ole Troan9d420872017-10-12 13:06:35 +02001027 for o in t.block:
Ole Troan33a58172019-09-04 09:12:29 +02001028 pp.print_obj(o, stream)
Ole Troan9d420872017-10-12 13:06:35 +02001029
Ole Troan33a58172019-09-04 09:12:29 +02001030 write(' return s;\n')
1031 write('}\n\n')
Ole Troan9d420872017-10-12 13:06:35 +02001032
Ole Troan33a58172019-09-04 09:12:29 +02001033 write("\n#endif")
1034 write("\n#endif /* vl_printfun_types */\n")
Ole Troan9d420872017-10-12 13:06:35 +02001035
Ole Troan33a58172019-09-04 09:12:29 +02001036
Ole Troandf87f802020-11-18 19:17:48 +01001037def generate_imports(imports):
1038 '''Add #include matching the API import statements'''
Ole Troan33a58172019-09-04 09:12:29 +02001039 output = '/* Imported API files */\n'
1040 output += '#ifndef vl_api_version\n'
1041
1042 for i in imports:
1043 s = i.filename.replace('plugins/', '')
1044 output += '#include <{}.h>\n'.format(s)
1045 output += '#endif\n'
Ole Troan9d420872017-10-12 13:06:35 +02001046 return output
1047
1048
Ole Troandf87f802020-11-18 19:17:48 +01001049ENDIAN_STRINGS = {
Ole Troan9d420872017-10-12 13:06:35 +02001050 'u16': 'clib_net_to_host_u16',
1051 'u32': 'clib_net_to_host_u32',
1052 'u64': 'clib_net_to_host_u64',
Paul Vinciguerra1c200dc2019-11-19 23:30:53 -05001053 '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',
Ole Troan9d420872017-10-12 13:06:35 +02001057}
1058
1059
Ole Troan33a58172019-09-04 09:12:29 +02001060def endianfun_array(o):
Ole Troandf87f802020-11-18 19:17:48 +01001061 '''Generate endian functions for arrays'''
Ole Troan33a58172019-09-04 09:12:29 +02001062 forloop = '''\
1063 for (i = 0; i < {length}; i++) {{
1064 a->{name}[i] = {format}(a->{name}[i]);
1065 }}
1066'''
1067
1068 forloop_format = '''\
1069 for (i = 0; i < {length}; i++) {{
1070 {type}_endian(&a->{name}[i]);
1071 }}
1072'''
1073
1074 output = ''
Ole Troan18327be2021-01-12 21:49:38 +01001075 if o.fieldtype == 'u8' or o.fieldtype == 'string' or o.fieldtype == 'bool':
Ole Troan33a58172019-09-04 09:12:29 +02001076 output += ' /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname)
1077 else:
1078 lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
Ole Troandf87f802020-11-18 19:17:48 +01001079 if o.fieldtype in ENDIAN_STRINGS:
Ole Troan33a58172019-09-04 09:12:29 +02001080 output += (forloop
1081 .format(length=lfield,
Ole Troandf87f802020-11-18 19:17:48 +01001082 format=ENDIAN_STRINGS[o.fieldtype],
Ole Troan33a58172019-09-04 09:12:29 +02001083 name=o.fieldname))
1084 else:
1085 output += (forloop_format
1086 .format(length=lfield, type=o.fieldtype,
1087 name=o.fieldname))
1088 return output
1089
Ole Troandf87f802020-11-18 19:17:48 +01001090
1091NO_ENDIAN_CONVERSION = {'client_index': None}
1092
Ole Troan33a58172019-09-04 09:12:29 +02001093
1094def endianfun_obj(o):
Ole Troandf87f802020-11-18 19:17:48 +01001095 '''Generate endian conversion function for type'''
Ole Troan33a58172019-09-04 09:12:29 +02001096 output = ''
1097 if o.type == 'Array':
1098 return endianfun_array(o)
Ole Troandf87f802020-11-18 19:17:48 +01001099 if o.type != 'Field':
Ole Troan33a58172019-09-04 09:12:29 +02001100 output += (' s = format(s, "\\n{} {} {} (print not implemented");\n'
1101 .format(o.type, o.fieldtype, o.fieldname))
1102 return output
Ole Troandf87f802020-11-18 19:17:48 +01001103 if o.fieldname in NO_ENDIAN_CONVERSION:
Ole Troane796a182020-05-18 11:14:05 +02001104 output += ' /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname)
1105 return output
Ole Troandf87f802020-11-18 19:17:48 +01001106 if o.fieldtype in ENDIAN_STRINGS:
Ole Troan33a58172019-09-04 09:12:29 +02001107 output += (' a->{name} = {format}(a->{name});\n'
1108 .format(name=o.fieldname,
Ole Troandf87f802020-11-18 19:17:48 +01001109 format=ENDIAN_STRINGS[o.fieldtype]))
Ole Troan33a58172019-09-04 09:12:29 +02001110 elif o.fieldtype.startswith('vl_api_'):
1111 output += (' {type}_endian(&a->{name});\n'
1112 .format(type=o.fieldtype, name=o.fieldname))
1113 else:
1114 output += ' /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname)
1115
1116 return output
1117
1118
Ole Troan75761b92019-09-11 17:49:08 +02001119def endianfun(objs, modulename):
Ole Troandf87f802020-11-18 19:17:48 +01001120 '''Main entry point for endian function generation'''
Ole Troan9d420872017-10-12 13:06:35 +02001121 output = '''\
1122
1123/****** Endian swap functions *****/\n\
1124#ifdef vl_endianfun
Ole Troan33a58172019-09-04 09:12:29 +02001125#ifndef included_{module}_endianfun
1126#define included_{module}_endianfun
Ole Troan9d420872017-10-12 13:06:35 +02001127
1128#undef clib_net_to_host_uword
1129#ifdef LP64
1130#define clib_net_to_host_uword clib_net_to_host_u64
1131#else
1132#define clib_net_to_host_uword clib_net_to_host_u32
1133#endif
1134
1135'''
Ole Troan33a58172019-09-04 09:12:29 +02001136 output = output.format(module=modulename)
1137
1138 signature = '''\
1139static inline void vl_api_{name}_t_endian (vl_api_{name}_t *a)
1140{{
1141 int i __attribute__((unused));
1142'''
1143
Ole Troan2c2feab2018-04-24 00:02:37 -04001144 for t in objs:
Paul Vinciguerraa51f9b32020-11-24 23:26:06 -05001145 if t.__class__.__name__ == 'Enum' or t.__class__.__name__ == 'EnumFlag' :
Ole Troan33a58172019-09-04 09:12:29 +02001146 output += signature.format(name=t.name)
Ole Troandf87f802020-11-18 19:17:48 +01001147 if t.enumtype in ENDIAN_STRINGS:
Ole Troan33a58172019-09-04 09:12:29 +02001148 output += (' *a = {}(*a);\n'
Ole Troandf87f802020-11-18 19:17:48 +01001149 .format(ENDIAN_STRINGS[t.enumtype]))
Ole Troan33a58172019-09-04 09:12:29 +02001150 else:
1151 output += (' /* a->{name} = a->{name} (no-op) */\n'
1152 .format(name=t.name))
1153
1154 output += '}\n\n'
Ole Troan2c2feab2018-04-24 00:02:37 -04001155 continue
Ole Troan33a58172019-09-04 09:12:29 +02001156
Ole Troan9d420872017-10-12 13:06:35 +02001157 if t.manual_endian:
1158 output += "/***** manual: vl_api_%s_t_endian *****/\n\n" % t.name
1159 continue
Ole Troan33a58172019-09-04 09:12:29 +02001160
Ole Troan75761b92019-09-11 17:49:08 +02001161 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))
Ole Troandf87f802020-11-18 19:17:48 +01001167 elif t.alias['type'] in FORMAT_STRINGS:
Ole Troan75761b92019-09-11 17:49:08 +02001168 output += (' *a = {}(*a);\n'
Ole Troandf87f802020-11-18 19:17:48 +01001169 .format(ENDIAN_STRINGS[t.alias['type']]))
Ole Troan75761b92019-09-11 17:49:08 +02001170 else:
1171 output += ' /* Not Implemented yet {} */'.format(t.name)
1172 output += '}\n\n'
1173 continue
1174
Ole Troan33a58172019-09-04 09:12:29 +02001175 output += signature.format(name=t.name)
Ole Troan9d420872017-10-12 13:06:35 +02001176
1177 for o in t.block:
Ole Troan33a58172019-09-04 09:12:29 +02001178 output += endianfun_obj(o)
Ole Troan9d420872017-10-12 13:06:35 +02001179 output += '}\n\n'
Ole Troan33a58172019-09-04 09:12:29 +02001180
1181 output += "\n#endif"
Ole Troan9d420872017-10-12 13:06:35 +02001182 output += "\n#endif /* vl_endianfun */\n\n"
1183
1184 return output
1185
1186
1187def version_tuple(s, module):
Ole Troandf87f802020-11-18 19:17:48 +01001188 '''Generate semantic version string'''
Ole Troan9d420872017-10-12 13:06:35 +02001189 output = '''\
1190/****** Version tuple *****/
1191
1192#ifdef vl_api_version_tuple
1193
1194'''
Ole Troan2c2feab2018-04-24 00:02:37 -04001195 if 'version' in s['Option']:
1196 v = s['Option']['version']
Ole Troan9d420872017-10-12 13:06:35 +02001197 (major, minor, patch) = v.split('.')
1198 output += "vl_api_version_tuple(%s, %s, %s, %s)\n" % \
1199 (module, major, minor, patch)
1200
1201 output += "\n#endif /* vl_api_version_tuple */\n\n"
1202
1203 return output
1204
1205
Ole Troan2a1ca782019-09-19 01:08:30 +02001206def generate_include_enum(s, module, stream):
Ole Troandf87f802020-11-18 19:17:48 +01001207 '''Generate <name>.api_enum.h'''
Ole Troan2a1ca782019-09-19 01:08:30 +02001208 write = stream.write
1209
Ole Troandf87f802020-11-18 19:17:48 +01001210 if 'Define' in s:
Ole Troan2a1ca782019-09-19 01:08:30 +02001211 write('typedef enum {\n')
1212 for t in s['Define']:
1213 write(' VL_API_{},\n'.format(t.name.upper()))
Ole Troan3d812672020-09-15 10:53:34 +02001214 write(' VL_MSG_{}_LAST\n'.format(module.upper()))
Ole Troan2a1ca782019-09-19 01:08:30 +02001215 write('}} vl_api_{}_enum_t;\n'.format(module))
1216
Paul Vinciguerra7c8803d2019-11-21 17:16:18 -05001217
Ole Troandf87f802020-11-18 19:17:48 +01001218def generate_include_counters(s, stream):
1219 '''Include file for the counter data model types.'''
Ole Troan148c7b72020-10-07 18:05:37 +02001220 write = stream.write
1221
1222 for counters in s:
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))
1230
Ole Troan2c4acdd2021-05-06 14:09:50 +02001231 write('extern vlib_error_desc_t {}_error_counters[];\n'.format(csetname))
Ole Troan148c7b72020-10-07 18:05:37 +02001232
Ole Troandf87f802020-11-18 19:17:48 +01001233
Ole Troan2a1ca782019-09-19 01:08:30 +02001234def generate_include_types(s, module, stream):
Ole Troandf87f802020-11-18 19:17:48 +01001235 '''Generate separate API _types file.'''
Ole Troan2a1ca782019-09-19 01:08:30 +02001236 write = stream.write
1237
1238 write('#ifndef included_{module}_api_types_h\n'.format(module=module))
1239 write('#define included_{module}_api_types_h\n'.format(module=module))
1240
Ole Troanf92bfb12020-02-28 13:45:42 +01001241 if 'version' in s['Option']:
1242 v = s['Option']['version']
1243 (major, minor, patch) = v.split('.')
Ole Troandf87f802020-11-18 19:17:48 +01001244 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))
Ole Troanf92bfb12020-02-28 13:45:42 +01001250
Ole Troandf87f802020-11-18 19:17:48 +01001251 if 'Import' in s:
Ole Troan2a1ca782019-09-19 01:08:30 +02001252 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))
1256
1257 for o in s['types'] + s['Define']:
1258 tname = o.__class__.__name__
1259 if tname == 'Using':
1260 if 'length' in o.alias:
Ole Troandf87f802020-11-18 19:17:48 +01001261 write('typedef %s vl_api_%s_t[%s];\n' %
1262 (o.alias['type'], o.name, o.alias['length']))
Ole Troan2a1ca782019-09-19 01:08:30 +02001263 else:
1264 write('typedef %s vl_api_%s_t;\n' % (o.alias['type'], o.name))
Paul Vinciguerraa51f9b32020-11-24 23:26:06 -05001265 elif tname == 'Enum' or tname == 'EnumFlag':
Ole Troan2a1ca782019-09-19 01:08:30 +02001266 if o.enumtype == 'u32':
1267 write("typedef enum {\n")
1268 else:
1269 write("typedef enum __attribute__((packed)) {\n")
1270
1271 for b in o.block:
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))
1280 else:
1281 if tname == 'Union':
Ole Troandf87f802020-11-18 19:17:48 +01001282 write("typedef union __attribute__ ((packed)) _vl_api_%s {\n"
1283 % o.name)
Ole Troan2a1ca782019-09-19 01:08:30 +02001284 else:
1285 write(("typedef struct __attribute__ ((packed)) _vl_api_%s {\n")
Ole Troandf87f802020-11-18 19:17:48 +01001286 % o.name)
Ole Troan2a1ca782019-09-19 01:08:30 +02001287 for b in o.block:
1288 if b.type == 'Option':
1289 continue
1290 if b.type == 'Field':
Ole Troandf87f802020-11-18 19:17:48 +01001291 write(" %s %s;\n" % (api2c(b.fieldtype),
1292 b.fieldname))
Ole Troan2a1ca782019-09-19 01:08:30 +02001293 elif b.type == 'Array':
1294 if b.lengthfield:
Ole Troandf87f802020-11-18 19:17:48 +01001295 write(" %s %s[0];\n" % (api2c(b.fieldtype),
1296 b.fieldname))
Ole Troan2a1ca782019-09-19 01:08:30 +02001297 else:
1298 # Fixed length strings decay to nul terminated u8
1299 if b.fieldtype == 'string':
1300 if b.modern_vla:
1301 write(' {} {};\n'
1302 .format(api2c(b.fieldtype),
1303 b.fieldname))
1304 else:
1305 write(' u8 {}[{}];\n'
1306 .format(b.fieldname, b.length))
1307 else:
1308 write(" %s %s[%s];\n" %
1309 (api2c(b.fieldtype), b.fieldname,
1310 b.length))
1311 else:
1312 raise ValueError("Error in processing type {} for {}"
1313 .format(b, o.name))
1314
1315 write('} vl_api_%s_t;\n' % o.name)
1316
Ole Troan3f2d5712019-12-07 00:39:49 +01001317 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))
1320
Ole Troan2a1ca782019-09-19 01:08:30 +02001321 write("\n#endif\n")
1322
1323
Ole Troan148c7b72020-10-07 18:05:37 +02001324def generate_c_boilerplate(services, defines, counters, file_crc,
1325 module, stream):
Ole Troandf87f802020-11-18 19:17:48 +01001326 '''VPP side plugin.'''
Ole Troan2a1ca782019-09-19 01:08:30 +02001327 write = stream.write
Ole Troan148c7b72020-10-07 18:05:37 +02001328 define_hash = {d.name: d for d in defines}
Ole Troan2a1ca782019-09-19 01:08:30 +02001329
1330 hdr = '''\
1331#define vl_endianfun /* define message structures */
1332#include "{module}.api.h"
1333#undef vl_endianfun
1334
1335/* instantiate all the print functions we know about */
1336#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
1337#define vl_printfun
1338#include "{module}.api.h"
1339#undef vl_printfun
1340
1341'''
1342
1343 write(hdr.format(module=module))
1344 write('static u16\n')
1345 write('setup_message_id_table (void) {\n')
Dave Barach39d69112019-11-27 11:42:13 -05001346 write(' api_main_t *am = my_api_main;\n')
Ole Troane796a182020-05-18 11:14:05 +02001347 write(' vl_msg_api_msg_config_t c;\n')
Ole Troandf87f802020-11-18 19:17:48 +01001348 write(' u16 msg_id_base = vl_msg_api_get_msg_ids ("{}_{crc:08x}", '
1349 'VL_MSG_{m}_LAST);\n'
Ole Troan3d812672020-09-15 10:53:34 +02001350 .format(module, crc=file_crc, m=module.upper()))
Ole Troan2a1ca782019-09-19 01:08:30 +02001351
Ole Troan2a1ca782019-09-19 01:08:30 +02001352 for d in defines:
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))
1356 for s in services:
Ole Troane796a182020-05-18 11:14:05 +02001357 d = define_hash[s.caller]
Ole Troandf87f802020-11-18 19:17:48 +01001358 write(' c = (vl_msg_api_msg_config_t) '
1359 ' {{.id = VL_API_{ID} + msg_id_base,\n'
1360 ' .name = "{n}",\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'
Ole Troan976a0ed2021-05-18 11:52:47 +02001365 ' .traced = 1,\n'
1366 ' .replay = 1,\n'
Filip Tehlar36217e32021-07-23 08:51:10 +00001367 ' .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'
Neale Ranns9302cfe2021-02-02 09:21:52 +00001370 ' .is_autoendian = {auto}}};\n'
1371 .format(n=s.caller, ID=s.caller.upper(),
1372 auto=d.autoendian))
Ole Troane796a182020-05-18 11:14:05 +02001373 write(' vl_msg_api_config (&c);\n')
Ole Troanbad67922020-08-24 12:22:01 +02001374 try:
1375 d = define_hash[s.reply]
Ole Troandf87f802020-11-18 19:17:48 +01001376 write(' c = (vl_msg_api_msg_config_t) '
1377 '{{.id = VL_API_{ID} + msg_id_base,\n'
1378 ' .name = "{n}",\n'
1379 ' .handler = 0,\n'
1380 ' .cleanup = vl_noop_handler,\n'
1381 ' .endian = vl_api_{n}_t_endian,\n'
1382 ' .print = vl_api_{n}_t_print,\n'
Filip Tehlar36217e32021-07-23 08:51:10 +00001383 ' .traced = 1,\n'
1384 ' .replay = 1,\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'
Neale Ranns9302cfe2021-02-02 09:21:52 +00001388 ' .is_autoendian = {auto}}};\n'
1389 .format(n=s.reply, ID=s.reply.upper(),
1390 auto=d.autoendian))
Ole Troanbad67922020-08-24 12:22:01 +02001391 write(' vl_msg_api_config (&c);\n')
1392 except KeyError:
1393 pass
Ole Troan2a1ca782019-09-19 01:08:30 +02001394
1395 write(' return msg_id_base;\n')
1396 write('}\n')
1397
Ole Troan148c7b72020-10-07 18:05:37 +02001398 severity = {'error': 'VL_COUNTER_SEVERITY_ERROR',
1399 'info': 'VL_COUNTER_SEVERITY_INFO',
1400 'warn': 'VL_COUNTER_SEVERITY_WARN'}
1401
1402 for cnt in counters:
1403 csetname = cnt.name
Ole Troan2c4acdd2021-05-06 14:09:50 +02001404 write('vlib_error_desc_t {}_error_counters[] = {{\n'.format(csetname))
Ole Troan148c7b72020-10-07 18:05:37 +02001405 for c in cnt.block:
1406 write(' {\n')
1407 write(' .name = "{}",\n'.format(c['name']))
1408 write(' .desc = "{}",\n'.format(c['description']))
1409 write(' .severity = {},\n'.format(severity[c['severity']]))
1410 write(' },\n')
1411 write('};\n')
Ole Troan2a1ca782019-09-19 01:08:30 +02001412
Ole Troandf87f802020-11-18 19:17:48 +01001413
1414def generate_c_test_boilerplate(services, defines, file_crc, module, plugin,
1415 stream):
1416 '''Generate code for legacy style VAT. To be deleted.'''
Ole Troan2a1ca782019-09-19 01:08:30 +02001417 write = stream.write
1418
Ole Troandf87f802020-11-18 19:17:48 +01001419 define_hash = {d.name: d for d in defines}
Ole Troan2a1ca782019-09-19 01:08:30 +02001420
1421 hdr = '''\
Ole Troandf87f802020-11-18 19:17:48 +01001422#define vl_endianfun /* define message structures */
Ole Troan2a1ca782019-09-19 01:08:30 +02001423#include "{module}.api.h"
1424#undef vl_endianfun
1425
1426/* instantiate all the print functions we know about */
1427#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
1428#define vl_printfun
1429#include "{module}.api.h"
1430#undef vl_printfun
1431
1432'''
1433
1434 write(hdr.format(module=module))
1435 for s in services:
1436 try:
1437 d = define_hash[s.reply]
Ole Troandf87f802020-11-18 19:17:48 +01001438 except KeyError:
Ole Troan2a1ca782019-09-19 01:08:30 +02001439 continue
1440 if d.manual_print:
Ole Troandf87f802020-11-18 19:17:48 +01001441 write('/*\n'
1442 ' * Manual definition requested for: \n'
1443 ' * vl_api_{n}_t_handler()\n'
1444 ' */\n'
Ole Troan2a1ca782019-09-19 01:08:30 +02001445 .format(n=s.reply))
1446 continue
1447 if not define_hash[s.caller].autoreply:
Ole Troandf87f802020-11-18 19:17:48 +01001448 write('/* Generation not supported (vl_api_{n}_t_handler()) */\n'
Ole Troan2a1ca782019-09-19 01:08:30 +02001449 .format(n=s.reply))
1450 continue
Paul Vinciguerra7c8803d2019-11-21 17:16:18 -05001451 write('#ifndef VL_API_{n}_T_HANDLER\n'.format(n=s.reply.upper()))
Ole Troan2a1ca782019-09-19 01:08:30 +02001452 write('static void\n')
Ole Troandf87f802020-11-18 19:17:48 +01001453 write('vl_api_{n}_t_handler (vl_api_{n}_t * mp) {{\n'
1454 .format(n=s.reply))
Ole Troan2a1ca782019-09-19 01:08:30 +02001455 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')
1462 write(' }\n')
1463 write('}\n')
Ole Troan3ae9f5a2019-10-09 12:39:32 +02001464 write('#endif\n')
Ole Troan2a1ca782019-09-19 01:08:30 +02001465
Ole Troanb126ebc2019-10-07 16:22:00 +02001466 for e in s.events:
1467 if define_hash[e].manual_print:
1468 continue
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))
1473 write('}\n')
1474
Ole Troan2a1ca782019-09-19 01:08:30 +02001475 write('static void\n')
1476 write('setup_message_id_table (vat_main_t * vam, u16 msg_id_base) {\n')
1477 for s in services:
Ole Troandf87f802020-11-18 19:17:48 +01001478 write(' vl_msg_api_set_handlers(VL_API_{ID} + msg_id_base, '
1479 ' "{n}",\n'
1480 ' vl_api_{n}_t_handler, '
1481 ' vl_noop_handler,\n'
1482 ' vl_api_{n}_t_endian, '
1483 ' vl_api_{n}_t_print,\n'
Filip Tehlar36217e32021-07-23 08:51:10 +00001484 ' 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'
Ole Troan2a1ca782019-09-19 01:08:30 +02001488 .format(n=s.reply, ID=s.reply.upper()))
Ole Troandf87f802020-11-18 19:17:48 +01001489 write(' hash_set_mem (vam->function_by_name, "{n}", api_{n});\n'
1490 .format(n=s.caller))
Ole Troan2a1ca782019-09-19 01:08:30 +02001491 try:
1492 write(' hash_set_mem (vam->help_by_name, "{n}", "{help}");\n'
Ole Troandf87f802020-11-18 19:17:48 +01001493 .format(n=s.caller,
1494 help=define_hash[s.caller].options['vat_help']))
1495 except KeyError:
Ole Troan2a1ca782019-09-19 01:08:30 +02001496 pass
1497
Ole Troanb126ebc2019-10-07 16:22:00 +02001498 # Events
1499 for e in s.events:
Ole Troandf87f802020-11-18 19:17:48 +01001500 write(' vl_msg_api_set_handlers(VL_API_{ID} + msg_id_base, '
1501 ' "{n}",\n'
1502 ' vl_api_{n}_t_handler, '
1503 ' vl_noop_handler,\n'
1504 ' vl_api_{n}_t_endian, '
1505 ' vl_api_{n}_t_print,\n'
Filip Tehlar36217e32021-07-23 08:51:10 +00001506 ' 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'
Ole Troanb126ebc2019-10-07 16:22:00 +02001510 .format(n=e, ID=e.upper()))
1511
Ole Troan2a1ca782019-09-19 01:08:30 +02001512 write('}\n')
Florin Coras248210c2021-09-14 18:54:45 -07001513 write('clib_error_t * vat_plugin_register (vat_main_t *vam)\n')
Ole Troan2a1ca782019-09-19 01:08:30 +02001514 write('{\n')
1515 write(' {n}_test_main_t * mainp = &{n}_test_main;\n'.format(n=module))
1516 write(' mainp->vat_main = vam;\n')
Ole Troandf87f802020-11-18 19:17:48 +01001517 write(' mainp->msg_id_base = vl_client_get_first_plugin_msg_id '
1518 ' ("{n}_{crc:08x}");\n'
Ole Troan2a1ca782019-09-19 01:08:30 +02001519 .format(n=module, crc=file_crc))
1520 write(' if (mainp->msg_id_base == (u16) ~0)\n')
Ole Troandf87f802020-11-18 19:17:48 +01001521 write(' return clib_error_return (0, "{} plugin not loaded...");\n'
1522 .format(module))
Ole Troan2a1ca782019-09-19 01:08:30 +02001523 write(' setup_message_id_table (vam, mainp->msg_id_base);\n')
Ole Troan709dad32019-10-09 14:38:14 +02001524 write('#ifdef VL_API_LOCAL_SETUP_MESSAGE_ID_TABLE\n')
1525 write(' VL_API_LOCAL_SETUP_MESSAGE_ID_TABLE(vam);\n')
1526 write('#endif\n')
Ole Troan2a1ca782019-09-19 01:08:30 +02001527 write(' return 0;\n')
1528 write('}\n')
1529
Ole Troandf87f802020-11-18 19:17:48 +01001530
1531def apifunc(func):
1532 '''Check if a method is generated already.'''
1533 def _f(module, d, processed, *args):
1534 if d.name in processed:
1535 return None
1536 processed[d.name] = True
1537 return func(module, d, *args)
1538 return _f
1539
1540
1541def c_test_api_service(s, dump, stream):
1542 '''Generate JSON code for a service.'''
1543 write = stream.write
1544
1545 req_reply_template = '''\
1546static cJSON *
1547api_{n} (cJSON *o)
1548{{
1549 vl_api_{n}_t *mp;
1550 int len;
1551 if (!o) return 0;
1552 mp = vl_api_{n}_t_fromjson(o, &len);
1553 if (!mp) {{
1554 fprintf(stderr, "Failed converting JSON to API\\n");
1555 return 0;
1556 }}
1557
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);
Filip Tehlar36217e32021-07-23 08:51:10 +00001561 cJSON_free(mp);
Ole Troandf87f802020-11-18 19:17:48 +01001562
1563 /* Read reply */
1564 char *p;
1565 int l;
1566 vac_read(&p, &l, 5); // XXX: Fix timeout
Ole Troanedc73fd2021-02-17 13:26:53 +01001567 if (p == 0 || l == 0) return 0;
Ole Troandf87f802020-11-18 19:17:48 +01001568 // 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");
1571 return 0;
1572 }}
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);
1576}}
1577
1578'''
1579 dump_details_template = '''\
1580static cJSON *
1581api_{n} (cJSON *o)
1582{{
1583 u16 msg_id = vac_get_msg_index(VL_API_{N}_CRC);
1584 int len;
1585 if (!o) return 0;
1586 vl_api_{n}_t *mp = vl_api_{n}_t_fromjson(o, &len);
1587 if (!mp) {{
1588 fprintf(stderr, "Failed converting JSON to API\\n");
1589 return 0;
1590 }}
1591 mp->_vl_msg_id = msg_id;
1592 vl_api_{n}_t_endian(mp);
1593 vac_write((char *)mp, len);
Filip Tehlar36217e32021-07-23 08:51:10 +00001594 cJSON_free(mp);
Ole Troandf87f802020-11-18 19:17:48 +01001595
1596 vat2_control_ping(123); // FIX CONTEXT
1597 cJSON *reply = cJSON_CreateArray();
1598
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);
1601
1602 while (1) {{
1603 /* Read reply */
1604 char *p;
1605 int l;
1606 vac_read(&p, &l, 5); // XXX: Fix timeout
Ole Troanedc73fd2021-02-17 13:26:53 +01001607 if (p == 0 || l == 0) {{
1608 cJSON_free(reply);
1609 return 0;
1610 }}
Ole Troandf87f802020-11-18 19:17:48 +01001611
1612 /* Message can be one of [_details, control_ping_reply
1613 * or unrelated event]
1614 */
1615 u16 reply_msg_id = ntohs(*((u16 *)p));
1616 if (reply_msg_id == ping_reply_msg_id) {{
1617 break;
1618 }}
1619
1620 if (reply_msg_id == details_msg_id) {{
Ole Troanedc73fd2021-02-17 13:26:53 +01001621 if (l < sizeof(vl_api_{r}_t)) {{
1622 cJSON_free(reply);
1623 return 0;
1624 }}
Ole Troandf87f802020-11-18 19:17:48 +01001625 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));
1628 }}
1629 }}
1630 return reply;
1631}}
1632
1633'''
1634 gets_details_reply_template = '''\
1635static cJSON *
1636api_{n} (cJSON *o)
1637{{
1638 u16 msg_id = vac_get_msg_index(VL_API_{N}_CRC);
1639 int len = 0;
1640 if (!o) return 0;
1641 vl_api_{n}_t *mp = vl_api_{n}_t_fromjson(o, &len);
1642 if (!mp) {{
1643 fprintf(stderr, "Failed converting JSON to API\\n");
1644 return 0;
1645 }}
1646 mp->_vl_msg_id = msg_id;
1647
1648 vl_api_{n}_t_endian(mp);
1649 vac_write((char *)mp, len);
Filip Tehlar36217e32021-07-23 08:51:10 +00001650 cJSON_free(mp);
Ole Troandf87f802020-11-18 19:17:48 +01001651
1652 cJSON *reply = cJSON_CreateArray();
1653
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);
1656
1657 while (1) {{
1658 /* Read reply */
1659 char *p;
1660 int l;
1661 vac_read(&p, &l, 5); // XXX: Fix timeout
1662
1663 /* Message can be one of [_details, control_ping_reply
1664 * or unrelated event]
1665 */
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));
1671 break;
1672 }}
1673
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));
1678 }}
1679 }}
1680 return reply;
1681}}
1682
1683'''
1684
1685 if dump:
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()))
1691 else:
1692 write(dump_details_template.format(n=s.caller, r=s.reply,
1693 N=s.caller.upper(),
1694 R=s.reply.upper()))
1695 else:
1696 write(req_reply_template.format(n=s.caller, r=s.reply,
1697 N=s.caller.upper(),
1698 R=s.reply.upper()))
1699
1700
1701def generate_c_test2_boilerplate(services, defines, module, stream):
1702 '''Generate code for VAT2 plugin.'''
1703 write = stream.write
1704
1705 define_hash = {d.name: d for d in defines}
1706 # replies = {}
1707
1708 hdr = '''\
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>
1714
1715#define vl_typedefs /* define message structures */
Filip Tehlarb7e4d442021-07-08 18:44:19 +00001716#include <vlibmemory/vl_memory_api_h.h>
Florin Corasa1400ce2021-09-15 09:02:08 -07001717#include <vlibmemory/vlib.api_types.h>
1718#include <vlibmemory/vlib.api.h>
Ole Troandf87f802020-11-18 19:17:48 +01001719#undef vl_typedefs
1720
1721#include "{module}.api_enum.h"
1722#include "{module}.api_types.h"
1723
1724#define vl_endianfun /* define message structures */
1725#include "{module}.api.h"
1726#undef vl_endianfun
1727
1728#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
1729#define vl_printfun
1730#include "{module}.api.h"
1731#undef vl_printfun
1732
1733#include "{module}.api_tojson.h"
1734#include "{module}.api_fromjson.h"
1735#include <vpp-api/client/vppapiclient.h>
1736
1737#include <vat2/vat2_helpers.h>
1738
1739'''
1740
1741 write(hdr.format(module=module))
1742
1743 for s in services:
1744 if s.reply not in define_hash:
1745 continue
1746 c_test_api_service(s, s.stream, stream)
1747
Filip Tehlar36217e32021-07-23 08:51:10 +00001748 write('void vat2_register_function(char *, cJSON * (*)(cJSON *), cJSON * (*)(void *), u32);\n')
Ole Troandf87f802020-11-18 19:17:48 +01001749 # write('__attribute__((constructor))')
1750 write('clib_error_t *\n')
1751 write('vat2_register_plugin (void) {\n')
1752 for s in services:
Filip Tehlar36217e32021-07-23 08:51:10 +00001753 if s.reply not in define_hash:
1754 continue
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))
Ole Troandf87f802020-11-18 19:17:48 +01001758 write(' return 0;\n')
1759 write('}\n')
1760
1761
Ole Troan9d420872017-10-12 13:06:35 +02001762#
1763# Plugin entry point
1764#
Ole Troandf87f802020-11-18 19:17:48 +01001765def run(args, apifilename, s):
1766 '''Main plugin entry point.'''
Ole Troan33a58172019-09-04 09:12:29 +02001767 stream = StringIO()
Ole Troan2a1ca782019-09-19 01:08:30 +02001768
1769 if not args.outputdir:
1770 sys.stderr.write('Missing --outputdir argument')
1771 return None
1772
Ole Troandf87f802020-11-18 19:17:48 +01001773 basename = os.path.basename(apifilename)
1774 filename, _ = os.path.splitext(basename)
Ole Troan33a58172019-09-04 09:12:29 +02001775 modulename = filename.replace('.', '_')
Ole Troan2a1ca782019-09-19 01:08:30 +02001776 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')
Ole Troandf87f802020-11-18 19:17:48 +01001780 filename_c_test2 = (os.path.join(args.outputdir + '/' + basename +
1781 '_test2.c'))
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'))
Ole Troan2a1ca782019-09-19 01:08:30 +02001786
1787 # Generate separate types file
1788 st = StringIO()
1789 generate_include_types(s, modulename, st)
Ole Troandf87f802020-11-18 19:17:48 +01001790 with open(filename_types, 'w') as fd:
1791 st.seek(0)
1792 shutil.copyfileobj(st, fd)
Ole Troan2a1ca782019-09-19 01:08:30 +02001793 st.close()
1794
1795 # Generate separate enum file
1796 st = StringIO()
Ole Troan148c7b72020-10-07 18:05:37 +02001797 st.write('#ifndef included_{}_api_enum_h\n'.format(modulename))
1798 st.write('#define included_{}_api_enum_h\n'.format(modulename))
Ole Troan2a1ca782019-09-19 01:08:30 +02001799 generate_include_enum(s, modulename, st)
Ole Troandf87f802020-11-18 19:17:48 +01001800 generate_include_counters(s['Counters'], st)
Ole Troan148c7b72020-10-07 18:05:37 +02001801 st.write('#endif\n')
Ole Troandf87f802020-11-18 19:17:48 +01001802 with open(filename_enum, 'w') as fd:
1803 st.seek(0)
1804 shutil.copyfileobj(st, fd)
Ole Troan2a1ca782019-09-19 01:08:30 +02001805 st.close()
1806
1807 # Generate separate C file
1808 st = StringIO()
Ole Troan148c7b72020-10-07 18:05:37 +02001809 generate_c_boilerplate(s['Service'], s['Define'], s['Counters'],
1810 s['file_crc'], modulename, st)
Ole Troandf87f802020-11-18 19:17:48 +01001811 with open(filename_c, 'w') as fd:
1812 st.seek(0)
Ole Troan2a1ca782019-09-19 01:08:30 +02001813 shutil.copyfileobj(st, fd)
1814 st.close()
1815
1816 # Generate separate C test file
Ole Troan2a1ca782019-09-19 01:08:30 +02001817 st = StringIO()
Ole Troandf87f802020-11-18 19:17:48 +01001818 plugin = bool('plugin' in apifilename)
1819 generate_c_test_boilerplate(s['Service'], s['Define'],
1820 s['file_crc'],
Ole Troan3d812672020-09-15 10:53:34 +02001821 modulename, plugin, st)
Ole Troandf87f802020-11-18 19:17:48 +01001822 with open(filename_c_test, 'w') as fd:
1823 st.seek(0)
Ole Troan2a1ca782019-09-19 01:08:30 +02001824 shutil.copyfileobj(st, fd)
1825 st.close()
Ole Troan33a58172019-09-04 09:12:29 +02001826
Ole Troandf87f802020-11-18 19:17:48 +01001827 # Fully autogenerated VATv2 C test file
1828 st = StringIO()
1829 generate_c_test2_boilerplate(s['Service'], s['Define'],
1830 modulename, st)
1831 with open(filename_c_test2, 'w') as fd:
1832 st.seek(0)
1833 shutil.copyfileobj(st, fd)
1834 st.close() #
1835
1836 # Generate separate JSON file
1837 st = StringIO()
1838 generate_tojson(s, modulename, st)
1839 with open(filename_c_tojson, 'w') as fd:
1840 st.seek(0)
1841 shutil.copyfileobj(st, fd)
1842 st.close()
1843 st = StringIO()
1844 generate_fromjson(s, modulename, st)
1845 with open(filename_c_fromjson, 'w') as fd:
1846 st.seek(0)
1847 shutil.copyfileobj(st, fd)
1848 st.close()
1849
1850 output = TOP_BOILERPLATE.format(datestring=DATESTRING,
Ole Troan9d420872017-10-12 13:06:35 +02001851 input_filename=basename)
Ole Troandf87f802020-11-18 19:17:48 +01001852 output += generate_imports(s['Import'])
Ole Troan9d420872017-10-12 13:06:35 +02001853 output += msg_ids(s)
1854 output += msg_names(s)
1855 output += msg_name_crc_list(s, filename)
Ole Troan2a1ca782019-09-19 01:08:30 +02001856 output += typedefs(modulename)
Ole Troan75761b92019-09-11 17:49:08 +02001857 printfun_types(s['types'], stream, modulename)
Ole Troan33a58172019-09-04 09:12:29 +02001858 printfun(s['Define'], stream, modulename)
1859 output += stream.getvalue()
Ole Troan2a1ca782019-09-19 01:08:30 +02001860 stream.close()
Ole Troan75761b92019-09-11 17:49:08 +02001861 output += endianfun(s['types'] + s['Define'], modulename)
Ole Troan9d420872017-10-12 13:06:35 +02001862 output += version_tuple(s, basename)
Ole Troandf87f802020-11-18 19:17:48 +01001863 output += BOTTOM_BOILERPLATE.format(input_filename=basename,
Ole Troan8dbfb432019-04-24 14:31:18 +02001864 file_crc=s['file_crc'])
Ole Troan9d420872017-10-12 13:06:35 +02001865
1866 return output