blob: b828706f814b499b7518d70ed5ec4c18bff730ef [file] [log] [blame]
Nathan Skrzypczak5c2f9642019-09-09 16:45:06 +02001#!/usr/bin/env python3
Ole Troan9d420872017-10-12 13:06:35 +02002
Ole Troan9d420872017-10-12 13:06:35 +02003import ply.lex as lex
4import ply.yacc as yacc
5import sys
6import argparse
Paul Vinciguerraff47fb62019-08-06 19:58:24 -04007import keyword
Ole Troan9d420872017-10-12 13:06:35 +02008import logging
9import binascii
10import os
Ole Troan5c318c72020-05-05 12:23:47 +020011from subprocess import Popen, PIPE
Ole Troan9d420872017-10-12 13:06:35 +020012
BenoƮt Ganne3f0ae662020-09-09 12:50:07 +020013assert sys.version_info >= (3, 5), \
Ole Troan14a6c0e2020-05-13 11:47:43 +020014 "Not supported Python version: {}".format(sys.version)
Paul Vinciguerra2cd3cc82019-08-06 22:02:45 -040015log = logging.getLogger('vppapigen')
16
Ole Troand6743b12018-03-07 08:40:58 +010017# Ensure we don't leave temporary files around
18sys.dont_write_bytecode = True
19
Ole Troan9d420872017-10-12 13:06:35 +020020#
21# VPP API language
22#
23
24# Global dictionary of new types (including enums)
25global_types = {}
26
Paul Vinciguerra4bf84902019-07-31 00:34:05 -040027seen_imports = {}
28
Ole Troan9d420872017-10-12 13:06:35 +020029
Ole Troan8dbfb432019-04-24 14:31:18 +020030def global_type_add(name, obj):
Ole Troan9d420872017-10-12 13:06:35 +020031 '''Add new type to the dictionary of types '''
32 type_name = 'vl_api_' + name + '_t'
Paul Vinciguerra4bf84902019-07-31 00:34:05 -040033 if type_name in global_types:
34 raise KeyError("Attempted redefinition of {!r} with {!r}.".format(
35 name, obj))
Ole Troan8dbfb432019-04-24 14:31:18 +020036 global_types[type_name] = obj
Ole Troan9d420872017-10-12 13:06:35 +020037
38
39# All your trace are belong to us!
40def exception_handler(exception_type, exception, traceback):
Ole Troan17225df2018-04-11 09:50:03 +020041 print("%s: %s" % (exception_type.__name__, exception))
Ole Troan9d420872017-10-12 13:06:35 +020042
43
44#
45# Lexer
46#
47class VPPAPILexer(object):
48 def __init__(self, filename):
49 self.filename = filename
50
51 reserved = {
52 'service': 'SERVICE',
53 'rpc': 'RPC',
54 'returns': 'RETURNS',
Marek Gradzki51e59682018-03-06 10:05:44 +010055 'null': 'NULL',
Ole Troan9d420872017-10-12 13:06:35 +020056 'stream': 'STREAM',
57 'events': 'EVENTS',
58 'define': 'DEFINE',
59 'typedef': 'TYPEDEF',
60 'enum': 'ENUM',
61 'typeonly': 'TYPEONLY',
62 'manual_print': 'MANUAL_PRINT',
63 'manual_endian': 'MANUAL_ENDIAN',
64 'dont_trace': 'DONT_TRACE',
65 'autoreply': 'AUTOREPLY',
66 'option': 'OPTION',
67 'u8': 'U8',
68 'u16': 'U16',
69 'u32': 'U32',
70 'u64': 'U64',
71 'i8': 'I8',
72 'i16': 'I16',
73 'i32': 'I32',
74 'i64': 'I64',
75 'f64': 'F64',
76 'bool': 'BOOL',
77 'string': 'STRING',
78 'import': 'IMPORT',
79 'true': 'TRUE',
80 'false': 'FALSE',
Ole Troan2c2feab2018-04-24 00:02:37 -040081 'union': 'UNION',
Ole Troan148c7b72020-10-07 18:05:37 +020082 'counters': 'COUNTERS',
83 'paths': 'PATHS',
84 'units': 'UNITS',
85 'severity': 'SEVERITY',
86 'type': 'TYPE',
87 'description': 'DESCRIPTION',
Ole Troan9d420872017-10-12 13:06:35 +020088 }
89
90 tokens = ['STRING_LITERAL',
91 'ID', 'NUM'] + list(reserved.values())
92
93 t_ignore_LINE_COMMENT = '//.*'
94
Ole Troan33a58172019-09-04 09:12:29 +020095 def t_FALSE(self, t):
96 r'false'
97 t.value = False
98 return t
99
100 def t_TRUE(self, t):
101 r'false'
102 t.value = True
103 return t
104
Ole Troan9d420872017-10-12 13:06:35 +0200105 def t_NUM(self, t):
Paul Vinciguerra063f3742019-07-02 13:00:58 -0400106 r'0[xX][0-9a-fA-F]+|-?\d+\.?\d*'
Ole Troan9d420872017-10-12 13:06:35 +0200107 base = 16 if t.value.startswith('0x') else 10
Paul Vinciguerra063f3742019-07-02 13:00:58 -0400108 if '.' in t.value:
109 t.value = float(t.value)
110 else:
111 t.value = int(t.value, base)
Ole Troan9d420872017-10-12 13:06:35 +0200112 return t
113
114 def t_ID(self, t):
115 r'[a-zA-Z_][a-zA-Z_0-9]*'
116 # Check for reserved words
117 t.type = VPPAPILexer.reserved.get(t.value, 'ID')
118 return t
119
120 # C string
121 def t_STRING_LITERAL(self, t):
122 r'\"([^\\\n]|(\\.))*?\"'
123 t.value = str(t.value).replace("\"", "")
124 return t
125
126 # C or C++ comment (ignore)
127 def t_comment(self, t):
128 r'(/\*(.|\n)*?\*/)|(//.*)'
129 t.lexer.lineno += t.value.count('\n')
130
131 # Error handling rule
132 def t_error(self, t):
133 raise ParseError("Illegal character '{}' ({})"
134 "in {}: line {}".format(t.value[0],
135 hex(ord(t.value[0])),
136 self.filename,
137 t.lexer.lineno))
Ole Troan9d420872017-10-12 13:06:35 +0200138
139 # Define a rule so we can track line numbers
140 def t_newline(self, t):
141 r'\n+'
142 t.lexer.lineno += len(t.value)
143
144 literals = ":{}[];=.,"
145
146 # A string containing ignored characters (spaces and tabs)
147 t_ignore = ' \t'
148
Ole Troan17225df2018-04-11 09:50:03 +0200149
Ole Troand5a78a52019-09-18 12:12:47 +0200150def vla_is_last_check(name, block):
151 vla = False
152 for i, b in enumerate(block):
153 if isinstance(b, Array) and b.vla:
154 vla = True
155 if i + 1 < len(block):
156 raise ValueError(
157 'VLA field "{}" must be the last field in message "{}"'
158 .format(b.fieldname, name))
159 elif b.fieldtype.startswith('vl_api_'):
160 if global_types[b.fieldtype].vla:
161 vla = True
162 if i + 1 < len(block):
163 raise ValueError(
164 'VLA field "{}" must be the last '
165 'field in message "{}"'
166 .format(b.fieldname, name))
167 elif b.fieldtype == 'string' and b.length == 0:
168 vla = True
169 if i + 1 < len(block):
170 raise ValueError(
171 'VLA field "{}" must be the last '
172 'field in message "{}"'
173 .format(b.fieldname, name))
174 return vla
175
176
Ole Troan9d420872017-10-12 13:06:35 +0200177class Service():
Ole Troanf5db3712020-05-20 15:47:06 +0200178 def __init__(self, caller, reply, events=None, stream_message=None, stream=False):
Ole Troan9d420872017-10-12 13:06:35 +0200179 self.caller = caller
180 self.reply = reply
181 self.stream = stream
Ole Troanf5db3712020-05-20 15:47:06 +0200182 self.stream_message = stream_message
Paul Vinciguerra7e0c48e2019-02-01 19:37:45 -0800183 self.events = [] if events is None else events
Ole Troan9d420872017-10-12 13:06:35 +0200184
185
186class Typedef():
187 def __init__(self, name, flags, block):
188 self.name = name
189 self.flags = flags
190 self.block = block
Ole Troan8dbfb432019-04-24 14:31:18 +0200191 self.crc = str(block).encode()
Ole Troan2c2feab2018-04-24 00:02:37 -0400192 self.manual_print = False
193 self.manual_endian = False
194 for f in flags:
195 if f == 'manual_print':
196 self.manual_print = True
197 elif f == 'manual_endian':
198 self.manual_endian = True
Ole Troan8dbfb432019-04-24 14:31:18 +0200199 global_type_add(name, self)
Ole Troan9d420872017-10-12 13:06:35 +0200200
Ole Troand5a78a52019-09-18 12:12:47 +0200201 self.vla = vla_is_last_check(name, block)
Ole Troane5ff5a32019-08-23 22:55:18 +0200202
Ole Troan9d420872017-10-12 13:06:35 +0200203 def __repr__(self):
Vratko Polak7520e172019-08-01 10:31:49 +0200204 return self.name + str(self.flags) + str(self.block)
Ole Troan9d420872017-10-12 13:06:35 +0200205
206
Ole Troan53fffa12018-11-13 12:36:56 +0100207class Using():
Ole Troan33a58172019-09-04 09:12:29 +0200208 def __init__(self, name, flags, alias):
Ole Troan53fffa12018-11-13 12:36:56 +0100209 self.name = name
Ole Troane5ff5a32019-08-23 22:55:18 +0200210 self.vla = False
Ole Troan75761b92019-09-11 17:49:08 +0200211 self.block = []
212 self.manual_print = True
213 self.manual_endian = True
Ole Troan53fffa12018-11-13 12:36:56 +0100214
Ole Troan33a58172019-09-04 09:12:29 +0200215 self.manual_print = False
216 self.manual_endian = False
217 for f in flags:
218 if f == 'manual_print':
219 self.manual_print = True
220 elif f == 'manual_endian':
221 self.manual_endian = True
222
Ole Troan53fffa12018-11-13 12:36:56 +0100223 if isinstance(alias, Array):
Ole Troane5ff5a32019-08-23 22:55:18 +0200224 a = {'type': alias.fieldtype,
225 'length': alias.length}
Ole Troan53fffa12018-11-13 12:36:56 +0100226 else:
Ole Troane5ff5a32019-08-23 22:55:18 +0200227 a = {'type': alias.fieldtype}
Ole Troan53fffa12018-11-13 12:36:56 +0100228 self.alias = a
Ole Troan6006ca82020-08-31 13:54:47 +0200229 #
230 # Should have been:
231 # self.crc = str(alias).encode()
232 # but to be backwards compatible use the block ([])
233 #
234 self.crc = str(self.block).encode()
Ole Troan8dbfb432019-04-24 14:31:18 +0200235 global_type_add(name, self)
Ole Troan53fffa12018-11-13 12:36:56 +0100236
237 def __repr__(self):
Vratko Polak7520e172019-08-01 10:31:49 +0200238 return self.name + str(self.alias)
Ole Troan53fffa12018-11-13 12:36:56 +0100239
240
Ole Troan2c2feab2018-04-24 00:02:37 -0400241class Union():
Ole Troan33a58172019-09-04 09:12:29 +0200242 def __init__(self, name, flags, block):
Ole Troan2c2feab2018-04-24 00:02:37 -0400243 self.type = 'Union'
244 self.manual_print = False
245 self.manual_endian = False
Ole Troan2c2feab2018-04-24 00:02:37 -0400246 self.name = name
Ole Troan33a58172019-09-04 09:12:29 +0200247
Ole Troan33a58172019-09-04 09:12:29 +0200248 for f in flags:
249 if f == 'manual_print':
250 self.manual_print = True
251 elif f == 'manual_endian':
252 self.manual_endian = True
253
Ole Troan2c2feab2018-04-24 00:02:37 -0400254 self.block = block
Ole Troan8dbfb432019-04-24 14:31:18 +0200255 self.crc = str(block).encode()
Ole Troand5a78a52019-09-18 12:12:47 +0200256 self.vla = vla_is_last_check(name, block)
257
Ole Troan8dbfb432019-04-24 14:31:18 +0200258 global_type_add(name, self)
Ole Troan2c2feab2018-04-24 00:02:37 -0400259
260 def __repr__(self):
Vratko Polak7520e172019-08-01 10:31:49 +0200261 return str(self.block)
Ole Troan2c2feab2018-04-24 00:02:37 -0400262
263
Ole Troan9d420872017-10-12 13:06:35 +0200264class Define():
265 def __init__(self, name, flags, block):
266 self.name = name
267 self.flags = flags
268 self.block = block
Ole Troan9d420872017-10-12 13:06:35 +0200269 self.dont_trace = False
270 self.manual_print = False
271 self.manual_endian = False
272 self.autoreply = False
273 self.singular = False
Ole Troan2a1ca782019-09-19 01:08:30 +0200274 self.options = {}
Ole Troan9d420872017-10-12 13:06:35 +0200275 for f in flags:
Ole Troan2c2feab2018-04-24 00:02:37 -0400276 if f == 'dont_trace':
Ole Troan9d420872017-10-12 13:06:35 +0200277 self.dont_trace = True
278 elif f == 'manual_print':
279 self.manual_print = True
280 elif f == 'manual_endian':
281 self.manual_endian = True
282 elif f == 'autoreply':
283 self.autoreply = True
284
Ole Troan5c318c72020-05-05 12:23:47 +0200285 remove = []
Ole Troand5a78a52019-09-18 12:12:47 +0200286 for b in block:
Ole Troan9d420872017-10-12 13:06:35 +0200287 if isinstance(b, Option):
288 if b[1] == 'singular' and b[2] == 'true':
289 self.singular = True
Ole Troan2a1ca782019-09-19 01:08:30 +0200290 else:
291 self.options[b.option] = b.value
Ole Troan5c318c72020-05-05 12:23:47 +0200292 remove.append(b)
Ole Troan2a1ca782019-09-19 01:08:30 +0200293
Ole Troan14a6c0e2020-05-13 11:47:43 +0200294 block = [x for x in block if x not in remove]
Ole Troan5c318c72020-05-05 12:23:47 +0200295 self.block = block
Ole Troand5a78a52019-09-18 12:12:47 +0200296 self.vla = vla_is_last_check(name, block)
Ole Troan2a1ca782019-09-19 01:08:30 +0200297 self.crc = str(block).encode()
Ole Troane5ff5a32019-08-23 22:55:18 +0200298
Ole Troan9d420872017-10-12 13:06:35 +0200299 def __repr__(self):
Vratko Polak7520e172019-08-01 10:31:49 +0200300 return self.name + str(self.flags) + str(self.block)
Ole Troan9d420872017-10-12 13:06:35 +0200301
302
303class Enum():
304 def __init__(self, name, block, enumtype='u32'):
305 self.name = name
306 self.enumtype = enumtype
Ole Troane5ff5a32019-08-23 22:55:18 +0200307 self.vla = False
Ole Troan2c2feab2018-04-24 00:02:37 -0400308
Ole Troan9d420872017-10-12 13:06:35 +0200309 count = 0
Ole Troan6006ca82020-08-31 13:54:47 +0200310 block2 = []
311 block3 = []
312 bc_set = False
313
314 for b in block:
315 if 'value' in b:
316 count = b['value']
Ole Troan9d420872017-10-12 13:06:35 +0200317 else:
318 count += 1
Ole Troan6006ca82020-08-31 13:54:47 +0200319 block2.append([b['id'], count])
320 try:
321 if b['option']['backwards_compatible']:
322 pass
323 bc_set = True
324 except KeyError:
325 block3.append([b['id'], count])
326 if bc_set:
327 raise ValueError("Backward compatible enum must be last {!r} {!r}"
328 .format(name, b['id']))
329 self.block = block2
330 self.crc = str(block3).encode()
Ole Troan8dbfb432019-04-24 14:31:18 +0200331 global_type_add(name, self)
Ole Troan9d420872017-10-12 13:06:35 +0200332
333 def __repr__(self):
Vratko Polak7520e172019-08-01 10:31:49 +0200334 return self.name + str(self.block)
Ole Troan9d420872017-10-12 13:06:35 +0200335
336
337class Import():
Paul Vinciguerra4bf84902019-07-31 00:34:05 -0400338
339 def __new__(cls, *args, **kwargs):
340 if args[0] not in seen_imports:
341 instance = super().__new__(cls)
342 instance._initialized = False
343 seen_imports[args[0]] = instance
344
345 return seen_imports[args[0]]
346
Ole Troan5c318c72020-05-05 12:23:47 +0200347 def __init__(self, filename, revision):
Paul Vinciguerra4bf84902019-07-31 00:34:05 -0400348 if self._initialized:
349 return
350 else:
351 self.filename = filename
352 # Deal with imports
Ole Troan5c318c72020-05-05 12:23:47 +0200353 parser = VPPAPI(filename=filename, revision=revision)
Paul Vinciguerra4bf84902019-07-31 00:34:05 -0400354 dirlist = dirlist_get()
355 f = filename
356 for dir in dirlist:
357 f = os.path.join(dir, filename)
358 if os.path.exists(f):
359 break
Ole Troan5c318c72020-05-05 12:23:47 +0200360 self.result = parser.parse_filename(f, None)
Paul Vinciguerra4bf84902019-07-31 00:34:05 -0400361 self._initialized = True
Ole Troan9d420872017-10-12 13:06:35 +0200362
363 def __repr__(self):
Vratko Polak7520e172019-08-01 10:31:49 +0200364 return self.filename
Ole Troan9d420872017-10-12 13:06:35 +0200365
366
367class Option():
Ole Troan68ebcd52020-08-10 17:06:44 +0200368 def __init__(self, option, value=None):
Ole Troan33a58172019-09-04 09:12:29 +0200369 self.type = 'Option'
Ole Troan9d420872017-10-12 13:06:35 +0200370 self.option = option
Ole Troan33a58172019-09-04 09:12:29 +0200371 self.value = value
Ole Troan8dbfb432019-04-24 14:31:18 +0200372 self.crc = str(option).encode()
Ole Troan9d420872017-10-12 13:06:35 +0200373
374 def __repr__(self):
Vratko Polak7520e172019-08-01 10:31:49 +0200375 return str(self.option)
Ole Troan9d420872017-10-12 13:06:35 +0200376
377 def __getitem__(self, index):
378 return self.option[index]
379
380
381class Array():
Ole Troane5ff5a32019-08-23 22:55:18 +0200382 def __init__(self, fieldtype, name, length, modern_vla=False):
Ole Troan9d420872017-10-12 13:06:35 +0200383 self.type = 'Array'
384 self.fieldtype = fieldtype
385 self.fieldname = name
Ole Troane5ff5a32019-08-23 22:55:18 +0200386 self.modern_vla = modern_vla
Ole Troan9d420872017-10-12 13:06:35 +0200387 if type(length) is str:
388 self.lengthfield = length
389 self.length = 0
Ole Troane5ff5a32019-08-23 22:55:18 +0200390 self.vla = True
Ole Troan9d420872017-10-12 13:06:35 +0200391 else:
392 self.length = length
393 self.lengthfield = None
Ole Troane5ff5a32019-08-23 22:55:18 +0200394 self.vla = False
Ole Troan9d420872017-10-12 13:06:35 +0200395
396 def __repr__(self):
Vratko Polak7520e172019-08-01 10:31:49 +0200397 return str([self.fieldtype, self.fieldname, self.length,
398 self.lengthfield])
Ole Troan9d420872017-10-12 13:06:35 +0200399
400
401class Field():
Ole Troan9ac11382019-04-23 17:11:01 +0200402 def __init__(self, fieldtype, name, limit=None):
Ole Troan9d420872017-10-12 13:06:35 +0200403 self.type = 'Field'
404 self.fieldtype = fieldtype
Ole Troane5ff5a32019-08-23 22:55:18 +0200405
406 if self.fieldtype == 'string':
407 raise ValueError("The string type {!r} is an "
408 "array type ".format(name))
409
Paul Vinciguerraff47fb62019-08-06 19:58:24 -0400410 if name in keyword.kwlist:
411 raise ValueError("Fieldname {!r} is a python keyword and is not "
412 "accessible via the python API. ".format(name))
Ole Troan9d420872017-10-12 13:06:35 +0200413 self.fieldname = name
Ole Troan9ac11382019-04-23 17:11:01 +0200414 self.limit = limit
Ole Troan9d420872017-10-12 13:06:35 +0200415
416 def __repr__(self):
Vratko Polak7520e172019-08-01 10:31:49 +0200417 return str([self.fieldtype, self.fieldname])
Ole Troan9d420872017-10-12 13:06:35 +0200418
419
Ole Troan148c7b72020-10-07 18:05:37 +0200420class Counter():
421 def __init__(self, path, counter):
422 self.type = 'Counter'
423 self.name = path
424 self.block = counter
425
426
427class Paths():
428 def __init__(self, pathset):
429 self.type = 'Paths'
430 self.paths = pathset
431
432
Ole Troan9d420872017-10-12 13:06:35 +0200433class Coord(object):
434 """ Coordinates of a syntactic element. Consists of:
435 - File name
436 - Line number
437 - (optional) column number, for the Lexer
438 """
439 __slots__ = ('file', 'line', 'column', '__weakref__')
440
441 def __init__(self, file, line, column=None):
442 self.file = file
443 self.line = line
444 self.column = column
445
446 def __str__(self):
447 str = "%s:%s" % (self.file, self.line)
448 if self.column:
449 str += ":%s" % self.column
450 return str
451
452
453class ParseError(Exception):
454 pass
455
456
457#
458# Grammar rules
459#
460class VPPAPIParser(object):
461 tokens = VPPAPILexer.tokens
462
Ole Troan5c318c72020-05-05 12:23:47 +0200463 def __init__(self, filename, logger, revision=None):
Ole Troan9d420872017-10-12 13:06:35 +0200464 self.filename = filename
465 self.logger = logger
466 self.fields = []
Ole Troan5c318c72020-05-05 12:23:47 +0200467 self.revision = revision
Ole Troan9d420872017-10-12 13:06:35 +0200468
469 def _parse_error(self, msg, coord):
470 raise ParseError("%s: %s" % (coord, msg))
471
472 def _parse_warning(self, msg, coord):
473 if self.logger:
474 self.logger.warning("%s: %s" % (coord, msg))
475
476 def _coord(self, lineno, column=None):
477 return Coord(
478 file=self.filename,
479 line=lineno, column=column)
480
481 def _token_coord(self, p, token_idx):
482 """ Returns the coordinates for the YaccProduction object 'p' indexed
483 with 'token_idx'. The coordinate includes the 'lineno' and
484 'column'. Both follow the lex semantic, starting from 1.
485 """
486 last_cr = p.lexer.lexdata.rfind('\n', 0, p.lexpos(token_idx))
487 if last_cr < 0:
488 last_cr = -1
489 column = (p.lexpos(token_idx) - (last_cr))
490 return self._coord(p.lineno(token_idx), column)
491
492 def p_slist(self, p):
493 '''slist : stmt
494 | slist stmt'''
495 if len(p) == 2:
496 p[0] = [p[1]]
497 else:
498 p[0] = p[1] + [p[2]]
499
500 def p_stmt(self, p):
501 '''stmt : define
502 | typedef
503 | option
504 | import
505 | enum
Ole Troan2c2feab2018-04-24 00:02:37 -0400506 | union
Ole Troan148c7b72020-10-07 18:05:37 +0200507 | service
508 | paths
509 | counters'''
Ole Troan9d420872017-10-12 13:06:35 +0200510 p[0] = p[1]
511
512 def p_import(self, p):
513 '''import : IMPORT STRING_LITERAL ';' '''
Ole Troan5c318c72020-05-05 12:23:47 +0200514 p[0] = Import(p[2], revision=self.revision)
Ole Troan9d420872017-10-12 13:06:35 +0200515
Ole Troan148c7b72020-10-07 18:05:37 +0200516 def p_path_elements(self, p):
517 '''path_elements : path_element
518 | path_elements path_element'''
519 if len(p) == 2:
520 p[0] = p[1]
521 else:
522 if type(p[1]) is dict:
523 p[0] = [p[1], p[2]]
524 else:
525 p[0] = p[1] + [p[2]]
526
527 def p_path_element(self, p):
528 '''path_element : STRING_LITERAL STRING_LITERAL ';' '''
529 p[0] = {'path': p[1], 'counter': p[2]}
530
531 def p_paths(self, p):
532 '''paths : PATHS '{' path_elements '}' ';' '''
533 p[0] = Paths(p[3])
534
535 def p_counters(self, p):
536 '''counters : COUNTERS ID '{' counter_elements '}' ';' '''
537 p[0] = Counter(p[2], p[4])
538
539 def p_counter_elements(self, p):
540 '''counter_elements : counter_element
541 | counter_elements counter_element'''
542 if len(p) == 2:
543 p[0] = p[1]
544 else:
545 if type(p[1]) is dict:
546 p[0] = [p[1], p[2]]
547 else:
548 p[0] = p[1] + [p[2]]
549
550 def p_counter_element(self, p):
551 '''counter_element : ID '{' counter_statements '}' ';' '''
552 p[0] = {**{'name': p[1]}, **p[3]}
553
554 def p_counter_statements(self, p):
555 '''counter_statements : counter_statement
556 | counter_statements counter_statement'''
557 if len(p) == 2:
558 p[0] = p[1]
559 else:
560 p[0] = {**p[1], **p[2]}
561
562 def p_counter_statement(self, p):
563 '''counter_statement : SEVERITY ID ';'
564 | UNITS STRING_LITERAL ';'
565 | DESCRIPTION STRING_LITERAL ';'
566 | TYPE ID ';' '''
567 p[0] = {p[1]: p[2]}
568
Ole Troan9d420872017-10-12 13:06:35 +0200569 def p_service(self, p):
570 '''service : SERVICE '{' service_statements '}' ';' '''
571 p[0] = p[3]
572
573 def p_service_statements(self, p):
574 '''service_statements : service_statement
575 | service_statements service_statement'''
576 if len(p) == 2:
577 p[0] = [p[1]]
578 else:
579 p[0] = p[1] + [p[2]]
580
581 def p_service_statement(self, p):
Marek Gradzki51e59682018-03-06 10:05:44 +0100582 '''service_statement : RPC ID RETURNS NULL ';'
583 | RPC ID RETURNS ID ';'
Ole Troan9d420872017-10-12 13:06:35 +0200584 | RPC ID RETURNS STREAM ID ';'
585 | RPC ID RETURNS ID EVENTS event_list ';' '''
Marek Gradzkifc70e3a2018-03-06 10:56:26 +0100586 if p[2] == p[4]:
587 # Verify that caller and reply differ
Ole Troan17225df2018-04-11 09:50:03 +0200588 self._parse_error(
589 'Reply ID ({}) should not be equal to Caller ID'.format(p[2]),
590 self._token_coord(p, 1))
Ole Troan9d420872017-10-12 13:06:35 +0200591 if len(p) == 8:
592 p[0] = Service(p[2], p[4], p[6])
593 elif len(p) == 7:
594 p[0] = Service(p[2], p[5], stream=True)
595 else:
596 p[0] = Service(p[2], p[4])
597
Ole Troanf5db3712020-05-20 15:47:06 +0200598 def p_service_statement2(self, p):
599 '''service_statement : RPC ID RETURNS ID STREAM ID ';' '''
600 p[0] = Service(p[2], p[4], stream_message=p[6], stream=True)
601
Ole Troan9d420872017-10-12 13:06:35 +0200602 def p_event_list(self, p):
603 '''event_list : events
604 | event_list events '''
605 if len(p) == 2:
606 p[0] = [p[1]]
607 else:
608 p[0] = p[1] + [p[2]]
609
610 def p_event(self, p):
611 '''events : ID
612 | ID ',' '''
613 p[0] = p[1]
614
615 def p_enum(self, p):
616 '''enum : ENUM ID '{' enum_statements '}' ';' '''
617 p[0] = Enum(p[2], p[4])
618
619 def p_enum_type(self, p):
620 ''' enum : ENUM ID ':' enum_size '{' enum_statements '}' ';' '''
621 if len(p) == 9:
622 p[0] = Enum(p[2], p[6], enumtype=p[4])
623 else:
624 p[0] = Enum(p[2], p[4])
625
626 def p_enum_size(self, p):
627 ''' enum_size : U8
628 | U16
629 | U32 '''
630 p[0] = p[1]
631
632 def p_define(self, p):
633 '''define : DEFINE ID '{' block_statements_opt '}' ';' '''
634 self.fields = []
635 p[0] = Define(p[2], [], p[4])
636
637 def p_define_flist(self, p):
638 '''define : flist DEFINE ID '{' block_statements_opt '}' ';' '''
Ole Troan2c2feab2018-04-24 00:02:37 -0400639 # Legacy typedef
640 if 'typeonly' in p[1]:
Paul Vinciguerrae7174822019-08-07 00:05:59 -0400641 self._parse_error('legacy typedef. use typedef: {} {}[{}];'
642 .format(p[1], p[2], p[4]),
643 self._token_coord(p, 1))
Ole Troan2c2feab2018-04-24 00:02:37 -0400644 else:
645 p[0] = Define(p[3], p[1], p[5])
Ole Troan9d420872017-10-12 13:06:35 +0200646
647 def p_flist(self, p):
648 '''flist : flag
649 | flist flag'''
650 if len(p) == 2:
651 p[0] = [p[1]]
652 else:
653 p[0] = p[1] + [p[2]]
654
655 def p_flag(self, p):
656 '''flag : MANUAL_PRINT
657 | MANUAL_ENDIAN
658 | DONT_TRACE
659 | TYPEONLY
660 | AUTOREPLY'''
661 if len(p) == 1:
662 return
663 p[0] = p[1]
664
665 def p_typedef(self, p):
666 '''typedef : TYPEDEF ID '{' block_statements_opt '}' ';' '''
667 p[0] = Typedef(p[2], [], p[4])
668
Ole Troan33a58172019-09-04 09:12:29 +0200669 def p_typedef_flist(self, p):
670 '''typedef : flist TYPEDEF ID '{' block_statements_opt '}' ';' '''
671 p[0] = Typedef(p[3], p[1], p[5])
672
Ole Troan53fffa12018-11-13 12:36:56 +0100673 def p_typedef_alias(self, p):
674 '''typedef : TYPEDEF declaration '''
Ole Troan33a58172019-09-04 09:12:29 +0200675 p[0] = Using(p[2].fieldname, [], p[2])
676
677 def p_typedef_alias_flist(self, p):
678 '''typedef : flist TYPEDEF declaration '''
679 p[0] = Using(p[3].fieldname, p[1], p[3])
Ole Troan53fffa12018-11-13 12:36:56 +0100680
Ole Troan9d420872017-10-12 13:06:35 +0200681 def p_block_statements_opt(self, p):
Ole Troan2c2feab2018-04-24 00:02:37 -0400682 '''block_statements_opt : block_statements '''
Ole Troan9d420872017-10-12 13:06:35 +0200683 p[0] = p[1]
684
685 def p_block_statements(self, p):
686 '''block_statements : block_statement
687 | block_statements block_statement'''
688 if len(p) == 2:
689 p[0] = [p[1]]
690 else:
691 p[0] = p[1] + [p[2]]
692
693 def p_block_statement(self, p):
694 '''block_statement : declaration
695 | option '''
696 p[0] = p[1]
697
698 def p_enum_statements(self, p):
699 '''enum_statements : enum_statement
Ole Troan9ac11382019-04-23 17:11:01 +0200700 | enum_statements enum_statement'''
Ole Troan9d420872017-10-12 13:06:35 +0200701 if len(p) == 2:
702 p[0] = [p[1]]
703 else:
704 p[0] = p[1] + [p[2]]
705
706 def p_enum_statement(self, p):
707 '''enum_statement : ID '=' NUM ','
Ole Troan6006ca82020-08-31 13:54:47 +0200708 | ID ','
709 | ID '[' field_options ']' ','
710 | ID '=' NUM '[' field_options ']' ',' '''
711 if len(p) == 3:
712 p[0] = {'id': p[1]}
713 elif len(p) == 5:
714 p[0] = {'id': p[1], 'value': p[3]}
715 elif len(p) == 6:
716 p[0] = {'id': p[1], 'option': p[3]}
717 elif len(p) == 8:
718 p[0] = {'id': p[1], 'value': p[3], 'option': p[5]}
Ole Troan9d420872017-10-12 13:06:35 +0200719 else:
Ole Troan6006ca82020-08-31 13:54:47 +0200720 self._parse_error('ERROR', self._token_coord(p, 1))
Ole Troan9d420872017-10-12 13:06:35 +0200721
Ole Troan85465582019-04-30 10:04:36 +0200722 def p_field_options(self, p):
723 '''field_options : field_option
724 | field_options field_option'''
725 if len(p) == 2:
726 p[0] = p[1]
727 else:
Ole Troane5ff5a32019-08-23 22:55:18 +0200728 p[0] = {**p[1], **p[2]}
Ole Troan85465582019-04-30 10:04:36 +0200729
730 def p_field_option(self, p):
Ole Troane5ff5a32019-08-23 22:55:18 +0200731 '''field_option : ID
732 | ID '=' assignee ','
Ole Troan85465582019-04-30 10:04:36 +0200733 | ID '=' assignee
Ole Troane5ff5a32019-08-23 22:55:18 +0200734
Ole Troan85465582019-04-30 10:04:36 +0200735 '''
Ole Troane5ff5a32019-08-23 22:55:18 +0200736 if len(p) == 2:
737 p[0] = {p[1]: None}
738 else:
739 p[0] = {p[1]: p[3]}
Ole Troan85465582019-04-30 10:04:36 +0200740
Ole Troan148c7b72020-10-07 18:05:37 +0200741 def p_variable_name(self, p):
742 '''variable_name : ID
743 | TYPE
744 | SEVERITY
745 | DESCRIPTION
746 | COUNTERS
747 | PATHS
748 '''
749 p[0] = p[1]
750
Ole Troan9d420872017-10-12 13:06:35 +0200751 def p_declaration(self, p):
Ole Troan148c7b72020-10-07 18:05:37 +0200752 '''declaration : type_specifier variable_name ';'
753 | type_specifier variable_name '[' field_options ']' ';'
754 '''
Ole Troan85465582019-04-30 10:04:36 +0200755 if len(p) == 7:
756 p[0] = Field(p[1], p[2], p[4])
Ole Troan9ac11382019-04-23 17:11:01 +0200757 elif len(p) == 4:
758 p[0] = Field(p[1], p[2])
759 else:
Paul Vinciguerra582eac52020-04-03 12:18:40 -0400760 self._parse_error('ERROR', self._token_coord(p, 1))
Ole Troan9d420872017-10-12 13:06:35 +0200761 self.fields.append(p[2])
Ole Troan9ac11382019-04-23 17:11:01 +0200762
Ole Troane5ff5a32019-08-23 22:55:18 +0200763 def p_declaration_array_vla(self, p):
Ole Troan148c7b72020-10-07 18:05:37 +0200764 '''declaration : type_specifier variable_name '[' ']' ';' '''
Ole Troane5ff5a32019-08-23 22:55:18 +0200765 p[0] = Array(p[1], p[2], 0, modern_vla=True)
766
Ole Troan9d420872017-10-12 13:06:35 +0200767 def p_declaration_array(self, p):
Ole Troan148c7b72020-10-07 18:05:37 +0200768 '''declaration : type_specifier variable_name '[' NUM ']' ';'
769 | type_specifier variable_name '[' ID ']' ';' '''
Ole Troane5ff5a32019-08-23 22:55:18 +0200770
Ole Troan9d420872017-10-12 13:06:35 +0200771 if len(p) != 7:
772 return self._parse_error(
773 'array: %s' % p.value,
774 self._coord(lineno=p.lineno))
775
776 # Make this error later
777 if type(p[4]) is int and p[4] == 0:
778 # XXX: Line number is wrong
779 self._parse_warning('Old Style VLA: {} {}[{}];'
780 .format(p[1], p[2], p[4]),
781 self._token_coord(p, 1))
782
783 if type(p[4]) is str and p[4] not in self.fields:
784 # Verify that length field exists
785 self._parse_error('Missing length field: {} {}[{}];'
786 .format(p[1], p[2], p[4]),
787 self._token_coord(p, 1))
788 p[0] = Array(p[1], p[2], p[4])
789
790 def p_option(self, p):
Ole Troan68ebcd52020-08-10 17:06:44 +0200791 '''option : OPTION ID '=' assignee ';'
792 | OPTION ID ';' '''
793 if len(p) == 4:
794 p[0] = Option(p[2])
795 else:
796 p[0] = Option(p[2], p[4])
Ole Troan9d420872017-10-12 13:06:35 +0200797
798 def p_assignee(self, p):
799 '''assignee : NUM
800 | TRUE
801 | FALSE
802 | STRING_LITERAL '''
803 p[0] = p[1]
804
805 def p_type_specifier(self, p):
806 '''type_specifier : U8
807 | U16
808 | U32
809 | U64
810 | I8
811 | I16
812 | I32
813 | I64
814 | F64
815 | BOOL
816 | STRING'''
817 p[0] = p[1]
818
819 # Do a second pass later to verify that user defined types are defined
820 def p_typedef_specifier(self, p):
821 '''type_specifier : ID '''
822 if p[1] not in global_types:
823 self._parse_error('Undefined type: {}'.format(p[1]),
824 self._token_coord(p, 1))
825 p[0] = p[1]
826
Ole Troan2c2feab2018-04-24 00:02:37 -0400827 def p_union(self, p):
828 '''union : UNION ID '{' block_statements_opt '}' ';' '''
Ole Troan33a58172019-09-04 09:12:29 +0200829 p[0] = Union(p[2], [], p[4])
830
831 def p_union_flist(self, p):
832 '''union : flist UNION ID '{' block_statements_opt '}' ';' '''
833 p[0] = Union(p[3], p[1], p[5])
Ole Troan2c2feab2018-04-24 00:02:37 -0400834
Ole Troan9d420872017-10-12 13:06:35 +0200835 # Error rule for syntax errors
836 def p_error(self, p):
837 if p:
838 self._parse_error(
839 'before: %s' % p.value,
840 self._coord(lineno=p.lineno))
841 else:
842 self._parse_error('At end of input', self.filename)
843
844
845class VPPAPI(object):
846
Ole Troan5c318c72020-05-05 12:23:47 +0200847 def __init__(self, debug=False, filename='', logger=None, revision=None):
Ole Troan9d420872017-10-12 13:06:35 +0200848 self.lexer = lex.lex(module=VPPAPILexer(filename), debug=debug)
Ole Troan5c318c72020-05-05 12:23:47 +0200849 self.parser = yacc.yacc(module=VPPAPIParser(filename, logger,
850 revision=revision),
Ole Troand6743b12018-03-07 08:40:58 +0100851 write_tables=False, debug=debug)
Ole Troan9d420872017-10-12 13:06:35 +0200852 self.logger = logger
Ole Troan5c318c72020-05-05 12:23:47 +0200853 self.revision = revision
854 self.filename = filename
Ole Troan9d420872017-10-12 13:06:35 +0200855
856 def parse_string(self, code, debug=0, lineno=1):
857 self.lexer.lineno = lineno
858 return self.parser.parse(code, lexer=self.lexer, debug=debug)
859
Ole Troan5c318c72020-05-05 12:23:47 +0200860 def parse_fd(self, fd, debug=0):
Ole Troan9d420872017-10-12 13:06:35 +0200861 data = fd.read()
862 return self.parse_string(data, debug=debug)
863
Ole Troan5c318c72020-05-05 12:23:47 +0200864 def parse_filename(self, filename, debug=0):
865 if self.revision:
BenoƮt Ganne3f0ae662020-09-09 12:50:07 +0200866 git_show = 'git show {}:{}'.format(self.revision, filename)
Ole Troandeecc932020-05-19 12:33:00 +0200867 proc = Popen(git_show.split(), stdout=PIPE, encoding='utf-8')
868 try:
869 data, errs = proc.communicate()
870 if proc.returncode != 0:
BenoƮt Ganne3f0ae662020-09-09 12:50:07 +0200871 print('File not found: {}:{}'.format(self.revision,
872 filename), file=sys.stderr)
Ole Troandeecc932020-05-19 12:33:00 +0200873 sys.exit(2)
874 return self.parse_string(data, debug=debug)
875 except Exception as e:
876 sys.exit(3)
Ole Troan5c318c72020-05-05 12:23:47 +0200877 else:
878 try:
879 with open(filename, encoding='utf-8') as fd:
880 return self.parse_fd(fd, None)
881 except FileNotFoundError:
BenoƮt Ganne3f0ae662020-09-09 12:50:07 +0200882 print('File not found: {}'.format(filename), file=sys.stderr)
Ole Troan5c318c72020-05-05 12:23:47 +0200883 sys.exit(2)
884
885 def autoreply_block(self, name, parent):
Ole Troan9d420872017-10-12 13:06:35 +0200886 block = [Field('u32', 'context'),
887 Field('i32', 'retval')]
Ole Troan5c318c72020-05-05 12:23:47 +0200888 # inherhit the parent's options
Ole Troan14a6c0e2020-05-13 11:47:43 +0200889 for k, v in parent.options.items():
Ole Troan5c318c72020-05-05 12:23:47 +0200890 block.append(Option(k, v))
Ole Troan9d420872017-10-12 13:06:35 +0200891 return Define(name + '_reply', [], block)
892
893 def process(self, objs):
894 s = {}
Ole Troan2c2feab2018-04-24 00:02:37 -0400895 s['Option'] = {}
896 s['Define'] = []
897 s['Service'] = []
898 s['types'] = []
899 s['Import'] = []
Ole Troan148c7b72020-10-07 18:05:37 +0200900 s['Counters'] = []
901 s['Paths'] = []
Ole Troan8dbfb432019-04-24 14:31:18 +0200902 crc = 0
Ole Troan9d420872017-10-12 13:06:35 +0200903 for o in objs:
Ole Troan2c2feab2018-04-24 00:02:37 -0400904 tname = o.__class__.__name__
Ole Troan8dbfb432019-04-24 14:31:18 +0200905 try:
Mark Nelsonea2abba2020-03-04 15:32:09 -0500906 crc = binascii.crc32(o.crc, crc) & 0xffffffff
Ole Troan8dbfb432019-04-24 14:31:18 +0200907 except AttributeError:
908 pass
Ole Troan9d420872017-10-12 13:06:35 +0200909 if isinstance(o, Define):
Ole Troan2c2feab2018-04-24 00:02:37 -0400910 s[tname].append(o)
911 if o.autoreply:
Ole Troan5c318c72020-05-05 12:23:47 +0200912 s[tname].append(self.autoreply_block(o.name, o))
Ole Troan9d420872017-10-12 13:06:35 +0200913 elif isinstance(o, Option):
Ole Troan59b6c0c2020-02-04 09:12:00 +0100914 s[tname][o.option] = o.value
Ole Troan9d420872017-10-12 13:06:35 +0200915 elif type(o) is list:
916 for o2 in o:
917 if isinstance(o2, Service):
Ole Troan2c2feab2018-04-24 00:02:37 -0400918 s['Service'].append(o2)
Ole Troan58914252018-10-23 10:50:07 +0200919 elif (isinstance(o, Enum) or
920 isinstance(o, Typedef) or
Ole Troan75761b92019-09-11 17:49:08 +0200921 isinstance(o, Using) or
Ole Troan58914252018-10-23 10:50:07 +0200922 isinstance(o, Union)):
Ole Troan2c2feab2018-04-24 00:02:37 -0400923 s['types'].append(o)
Ole Troan148c7b72020-10-07 18:05:37 +0200924 elif (isinstance(o, Counter)):
925 s['Counters'].append(o)
926 elif (isinstance(o, Paths)):
927 s['Paths'].append(o)
Ole Troan2c2feab2018-04-24 00:02:37 -0400928 else:
929 if tname not in s:
Ole Troan58914252018-10-23 10:50:07 +0200930 raise ValueError('Unknown class type: {} {}'
931 .format(tname, o))
Ole Troan2c2feab2018-04-24 00:02:37 -0400932 s[tname].append(o)
Ole Troan9d420872017-10-12 13:06:35 +0200933
Ole Troan2c2feab2018-04-24 00:02:37 -0400934 msgs = {d.name: d for d in s['Define']}
935 svcs = {s.caller: s for s in s['Service']}
936 replies = {s.reply: s for s in s['Service']}
Marek Gradzki51e59682018-03-06 10:05:44 +0100937 seen_services = {}
Ole Troan9d420872017-10-12 13:06:35 +0200938
Ole Troan8dbfb432019-04-24 14:31:18 +0200939 s['file_crc'] = crc
940
Ole Troan9d420872017-10-12 13:06:35 +0200941 for service in svcs:
942 if service not in msgs:
Ole Troan17225df2018-04-11 09:50:03 +0200943 raise ValueError(
944 'Service definition refers to unknown message'
945 ' definition: {}'.format(service))
946 if svcs[service].reply != 'null' and \
947 svcs[service].reply not in msgs:
Ole Troan9d420872017-10-12 13:06:35 +0200948 raise ValueError('Service definition refers to unknown message'
949 ' definition in reply: {}'
950 .format(svcs[service].reply))
Marek Gradzkib533f3f2018-03-06 11:10:56 +0100951 if service in replies:
952 raise ValueError('Service definition refers to message'
953 ' marked as reply: {}'.format(service))
Ole Troan9d420872017-10-12 13:06:35 +0200954 for event in svcs[service].events:
955 if event not in msgs:
956 raise ValueError('Service definition refers to unknown '
957 'event: {} in message: {}'
958 .format(event, service))
Marek Gradzki51e59682018-03-06 10:05:44 +0100959 seen_services[event] = True
Ole Troan9d420872017-10-12 13:06:35 +0200960
Marek Gradzki51e59682018-03-06 10:05:44 +0100961 # Create services implicitly
Ole Troan9d420872017-10-12 13:06:35 +0200962 for d in msgs:
Marek Gradzki51e59682018-03-06 10:05:44 +0100963 if d in seen_services:
964 continue
Ole Troan9d420872017-10-12 13:06:35 +0200965 if msgs[d].singular is True:
966 continue
Ole Troan9d420872017-10-12 13:06:35 +0200967 if d.endswith('_reply'):
968 if d[:-6] in svcs:
969 continue
970 if d[:-6] not in msgs:
Marek Gradzkicc134712018-03-06 12:25:02 +0100971 raise ValueError('{} missing calling message'
972 .format(d))
Ole Troan9d420872017-10-12 13:06:35 +0200973 continue
974 if d.endswith('_dump'):
975 if d in svcs:
976 continue
977 if d[:-5]+'_details' in msgs:
Ole Troan2c2feab2018-04-24 00:02:37 -0400978 s['Service'].append(Service(d, d[:-5]+'_details',
Ole Troan58914252018-10-23 10:50:07 +0200979 stream=True))
Ole Troan9d420872017-10-12 13:06:35 +0200980 else:
Marek Gradzkicc134712018-03-06 12:25:02 +0100981 raise ValueError('{} missing details message'
982 .format(d))
Ole Troan9d420872017-10-12 13:06:35 +0200983 continue
984
985 if d.endswith('_details'):
Jon Loeligerc0b19542020-05-11 08:43:51 -0500986 if d[:-8]+'_get' in msgs:
987 if d[:-8]+'_get' in svcs:
988 continue
989 else:
990 raise ValueError('{} should be in a stream service'
991 .format(d[:-8]+'_get'))
992 if d[:-8]+'_dump' in msgs:
993 continue
994 raise ValueError('{} missing dump or get message'
995 .format(d))
Ole Troan9d420872017-10-12 13:06:35 +0200996
997 if d in svcs:
998 continue
999 if d+'_reply' in msgs:
Ole Troan2c2feab2018-04-24 00:02:37 -04001000 s['Service'].append(Service(d, d+'_reply'))
Ole Troan9d420872017-10-12 13:06:35 +02001001 else:
Ole Troan17225df2018-04-11 09:50:03 +02001002 raise ValueError(
1003 '{} missing reply message ({}) or service definition'
1004 .format(d, d+'_reply'))
Ole Troan9d420872017-10-12 13:06:35 +02001005
1006 return s
1007
Ole Troan2c2feab2018-04-24 00:02:37 -04001008 def process_imports(self, objs, in_import, result):
Marek Gradzki51e59682018-03-06 10:05:44 +01001009 imported_objs = []
Ole Troan9d420872017-10-12 13:06:35 +02001010 for o in objs:
Ole Troan2c2feab2018-04-24 00:02:37 -04001011 # Only allow the following object types from imported file
1012 if in_import and not (isinstance(o, Enum) or
1013 isinstance(o, Union) or
Ole Troan10a09892018-06-29 11:32:33 +02001014 isinstance(o, Typedef) or
Ole Troan53fffa12018-11-13 12:36:56 +01001015 isinstance(o, Import) or
1016 isinstance(o, Using)):
Ole Troan2c2feab2018-04-24 00:02:37 -04001017 continue
Ole Troan2c2feab2018-04-24 00:02:37 -04001018 if isinstance(o, Import):
Ole Troan33a58172019-09-04 09:12:29 +02001019 result.append(o)
Paul Vinciguerra4bf84902019-07-31 00:34:05 -04001020 result = self.process_imports(o.result, True, result)
Ole Troan10a09892018-06-29 11:32:33 +02001021 else:
1022 result.append(o)
Paul Vinciguerra4bf84902019-07-31 00:34:05 -04001023 return result
Ole Troan9d420872017-10-12 13:06:35 +02001024
Ole Troan58914252018-10-23 10:50:07 +02001025
Ole Troan9d420872017-10-12 13:06:35 +02001026# Add message ids to each message.
1027def add_msg_id(s):
1028 for o in s:
1029 o.block.insert(0, Field('u16', '_vl_msg_id'))
1030 return s
1031
1032
Ole Troan9d420872017-10-12 13:06:35 +02001033dirlist = []
1034
1035
1036def dirlist_add(dirs):
1037 global dirlist
1038 if dirs:
1039 dirlist = dirlist + dirs
1040
1041
1042def dirlist_get():
1043 return dirlist
1044
Paul Vinciguerra2cd3cc82019-08-06 22:02:45 -04001045
Ole Troan8dbfb432019-04-24 14:31:18 +02001046def foldup_blocks(block, crc):
1047 for b in block:
1048 # Look up CRC in user defined types
1049 if b.fieldtype.startswith('vl_api_'):
1050 # Recursively
1051 t = global_types[b.fieldtype]
1052 try:
Ole Troan6006ca82020-08-31 13:54:47 +02001053 crc = binascii.crc32(t.crc, crc) & 0xffffffff
Ole Troan9f84e702020-06-25 14:27:46 +02001054 crc = foldup_blocks(t.block, crc)
Ole Troane5ff5a32019-08-23 22:55:18 +02001055 except AttributeError:
Ole Troan8dbfb432019-04-24 14:31:18 +02001056 pass
1057 return crc
1058
Paul Vinciguerra2cd3cc82019-08-06 22:02:45 -04001059
Ole Troan9f84e702020-06-25 14:27:46 +02001060# keep the CRCs of the existing types of messages compatible with the
1061# old "erroneous" way of calculating the CRC. For that - make a pointed
1062# adjustment of the CRC function.
1063# This is the purpose of the first element of the per-message dictionary.
1064# The second element is there to avoid weakening the duplicate-detecting
1065# properties of crc32. This way, if the new way of calculating the CRC
1066# happens to collide with the old (buggy) way - we will still get
1067# a different result and fail the comparison.
1068
1069fixup_crc_dict = {
1070 "abf_policy_add_del": { 0xc6131197: 0xee66f93e },
1071 "abf_policy_details": { 0xb7487fa4: 0x6769e504 },
1072 "acl_add_replace": { 0xee5c2f18: 0x1cabdeab },
1073 "acl_details": { 0x95babae0: 0x7a97f21c },
1074 "macip_acl_add": { 0xce6fbad0: 0xd648fd0a },
1075 "macip_acl_add_replace": { 0x2a461dd4: 0xe34402a7 },
1076 "macip_acl_details": { 0x27135b59: 0x57c7482f },
1077 "dhcp_proxy_config": { 0x4058a689: 0x6767230e },
1078 "dhcp_client_config": { 0x1af013ea: 0x959b80a3 },
1079 "dhcp_compl_event": { 0x554a44e5: 0xe908fd1d },
1080 "dhcp_client_details": { 0x3c5cd28a: 0xacd82f5a },
1081 "dhcp_proxy_details": { 0xdcbaf540: 0xce16f044 },
1082 "dhcp6_send_client_message": { 0xf8222476: 0xf6f14ef0 },
1083 "dhcp6_pd_send_client_message": { 0x3739fd8d: 0x64badb8 },
1084 "dhcp6_reply_event": { 0x85b7b17e: 0x9f3af9e5 },
1085 "dhcp6_pd_reply_event": { 0x5e878029: 0xcb3e462b },
1086 "ip6_add_del_address_using_prefix": { 0x3982f30a: 0x9b3d11e0 },
1087 "gbp_bridge_domain_add": { 0x918e8c01: 0x8454bfdf },
1088 "gbp_bridge_domain_details": { 0x51d51be9: 0x2acd15f9 },
1089 "gbp_route_domain_add": { 0x204c79e1: 0x2d0afe38 },
1090 "gbp_route_domain_details": { 0xa78bfbca: 0x8ab11375 },
1091 "gbp_endpoint_add": { 0x7b3af7de: 0x9ce16d5a },
1092 "gbp_endpoint_details": { 0x8dd8fbd3: 0x8aecb60 },
1093 "gbp_endpoint_group_add": { 0x301ddf15: 0x8e0f4054 },
1094 "gbp_endpoint_group_details": { 0xab71d723: 0x8f38292c },
1095 "gbp_subnet_add_del": { 0xa8803c80: 0x888aca35 },
1096 "gbp_subnet_details": { 0xcbc5ca18: 0x4ed84156 },
1097 "gbp_contract_add_del": { 0xaa8d652d: 0x553e275b },
1098 "gbp_contract_details": { 0x65dec325: 0x2a18db6e },
1099 "gbp_ext_itf_add_del": { 0x7606d0e1: 0x12ed5700 },
1100 "gbp_ext_itf_details": { 0x519c3d3c: 0x408a45c0 },
1101 "gtpu_add_del_tunnel": { 0xca983a2b: 0x9a9c0426 },
1102 "gtpu_tunnel_update_tteid": { 0x79f33816: 0x8a2db108 },
1103 "gtpu_tunnel_details": { 0x27f434ae: 0x4535cf95 },
1104 "igmp_listen": { 0x19a49f1e: 0x3f93a51a },
1105 "igmp_details": { 0x38f09929: 0x52f12a89 },
1106 "igmp_event": { 0x85fe93ec: 0xd7696eaf },
1107 "igmp_group_prefix_set": { 0x5b14a5ce: 0xd4f20ac5 },
1108 "igmp_group_prefix_details": { 0x259ccd81: 0xc3b3c526 },
1109 "ikev2_set_responder": { 0xb9aa4d4e: 0xf0d3dc80 },
1110 "vxlan_gpe_ioam_export_enable_disable": { 0xd4c76d3a: 0xe4d4ebfa },
1111 "ioam_export_ip6_enable_disable": { 0xd4c76d3a: 0xe4d4ebfa },
1112 "vxlan_gpe_ioam_vni_enable": { 0xfbb5fb1: 0x997161fb },
1113 "vxlan_gpe_ioam_vni_disable": { 0xfbb5fb1: 0x997161fb },
1114 "vxlan_gpe_ioam_transit_enable": { 0x3d3ec657: 0x553f5b7b },
1115 "vxlan_gpe_ioam_transit_disable": { 0x3d3ec657: 0x553f5b7b },
1116 "udp_ping_add_del": { 0xfa2628fc: 0xc692b188 },
1117 "l3xc_update": { 0xe96aabdf: 0x787b1d3 },
1118 "l3xc_details": { 0xbc5bf852: 0xd4f69627 },
1119 "sw_interface_lacp_details": { 0xd9a83d2f: 0x745ae0ba },
1120 "lb_conf": { 0x56cd3261: 0x22ddb739 },
1121 "lb_add_del_vip": { 0x6fa569c7: 0xd15b7ddc },
1122 "lb_add_del_as": { 0x35d72500: 0x78628987 },
1123 "lb_vip_dump": { 0x56110cb7: 0xc7bcb124 },
1124 "lb_vip_details": { 0x1329ec9b: 0x8f39bed },
1125 "lb_as_details": { 0x8d24c29e: 0x9c39f60e },
1126 "mactime_add_del_range": { 0xcb56e877: 0x101858ef },
1127 "mactime_details": { 0xda25b13a: 0x44921c06 },
1128 "map_add_domain": { 0x249f195c: 0x7a5a18c9 },
1129 "map_domain_details": { 0x796edb50: 0xfc1859dd },
1130 "map_param_add_del_pre_resolve": { 0xdae5af03: 0x17008c66 },
1131 "map_param_get_reply": { 0x26272c90: 0x28092156 },
1132 "memif_details": { 0xda34feb9: 0xd0382c4c },
1133 "dslite_add_del_pool_addr_range": { 0xde2a5b02: 0xc448457a },
1134 "dslite_set_aftr_addr": { 0x78b50fdf: 0x1e955f8d },
1135 "dslite_get_aftr_addr_reply": { 0x8e23608e: 0x38e30db1 },
1136 "dslite_set_b4_addr": { 0x78b50fdf: 0x1e955f8d },
1137 "dslite_get_b4_addr_reply": { 0x8e23608e: 0x38e30db1 },
1138 "nat44_add_del_address_range": { 0x6f2b8055: 0xd4c7568c },
1139 "nat44_address_details": { 0xd1beac1: 0x45410ac4 },
1140 "nat44_add_del_static_mapping": { 0x5ae5f03e: 0xe165e83b },
1141 "nat44_static_mapping_details": { 0x6cb40b2: 0x1a433ef7 },
1142 "nat44_add_del_identity_mapping": { 0x2faaa22: 0x8e12743f },
1143 "nat44_identity_mapping_details": { 0x2a52a030: 0x36d21351 },
1144 "nat44_add_del_interface_addr": { 0x4aed50c0: 0xfc835325 },
1145 "nat44_interface_addr_details": { 0xe4aca9ca: 0x3e687514 },
1146 "nat44_user_session_details": { 0x2cf6e16d: 0x1965fd69 },
1147 "nat44_add_del_lb_static_mapping": { 0x4f68ee9d: 0x53b24611 },
1148 "nat44_lb_static_mapping_add_del_local": { 0x7ca47547: 0x2910a151 },
1149 "nat44_lb_static_mapping_details": { 0xed5ce876: 0x2267b9e8 },
1150 "nat44_del_session": { 0x15a5bf8c: 0x4c49c387 },
1151 "nat_det_add_del_map": { 0x1150a190: 0x112fde05 },
1152 "nat_det_map_details": { 0xad91dc83: 0x88000ee1 },
1153 "nat_det_close_session_out": { 0xf6b259d1: 0xc1b6cbfb },
1154 "nat_det_close_session_in": { 0x3c68e073: 0xa10ef64 },
1155 "nat64_add_del_pool_addr_range": { 0xa3b944e3: 0x21234ef3 },
1156 "nat64_add_del_static_bib": { 0x1c404de5: 0x90fae58a },
1157 "nat64_bib_details": { 0x43bc3ddf: 0x62c8541d },
1158 "nat64_st_details": { 0xdd3361ed: 0xc770d620 },
1159 "nat66_add_del_static_mapping": { 0x3ed88f71: 0xfb64e50b },
1160 "nat66_static_mapping_details": { 0xdf39654b: 0x5c568448 },
1161 "nsh_add_del_map": { 0xa0f42b0: 0x898d857d },
1162 "nsh_map_details": { 0x2fefcf49: 0xb34ac8a1 },
1163 "nsim_cross_connect_enable_disable": { 0x9c3ead86: 0x16f70bdf },
1164 "pppoe_add_del_session": { 0xf6fd759e: 0x46ace853 },
1165 "pppoe_session_details": { 0x4b8e8a4a: 0x332bc742 },
1166 "stn_add_del_rule": { 0x224c6edd: 0x53f751e6 },
1167 "stn_rules_details": { 0xa51935a6: 0xb0f6606c },
1168 "svs_route_add_del": { 0xe49bc63c: 0xd39e31fc },
1169 "svs_details": { 0x6282cd55: 0xb8523d64 },
1170 "vmxnet3_details": { 0x6a1a5498: 0x829ba055 },
1171 "vrrp_vr_add_del": { 0xc5cf15aa: 0x6dc4b881 },
1172 "vrrp_vr_details": { 0x46edcebd: 0x412fa71 },
1173 "vrrp_vr_set_peers": { 0x20bec71f: 0xbaa2e52b },
1174 "vrrp_vr_peer_details": { 0x3d99c108: 0xabd9145e },
1175 "vrrp_vr_track_if_add_del": { 0xd67df299: 0x337f4ba4 },
1176 "vrrp_vr_track_if_details": { 0x73c36f81: 0x99bcca9c },
1177 "proxy_arp_add_del": { 0x1823c3e7: 0x85486cbd },
1178 "proxy_arp_details": { 0x5b948673: 0x9228c150 },
1179 "bfd_udp_get_echo_source_reply": { 0xe3d736a1: 0x1e00cfce },
1180 "bfd_udp_add": { 0x939cd26a: 0x7a6d1185 },
1181 "bfd_udp_mod": { 0x913df085: 0x783a3ff6 },
1182 "bfd_udp_del": { 0xdcb13a89: 0x8096514d },
1183 "bfd_udp_session_details": { 0x9fb2f2d: 0x60653c02 },
1184 "bfd_udp_session_set_flags": { 0x4b4bdfd: 0xcf313851 },
1185 "bfd_udp_auth_activate": { 0x21fd1bdb: 0x493ee0ec },
1186 "bfd_udp_auth_deactivate": { 0x9a05e2e0: 0x99978c32 },
1187 "bier_route_add_del": { 0xfd02f3ea: 0xf29edca0 },
1188 "bier_route_details": { 0x4008caee: 0x39ee6a56 },
1189 "bier_disp_entry_add_del": { 0x9eb80cb4: 0x648323eb },
1190 "bier_disp_entry_details": { 0x84c218f1: 0xe5b039a9 },
1191 "bond_create": { 0xf1dbd4ff: 0x48883c7e },
1192 "bond_enslave": { 0xe7d14948: 0x76ecfa7 },
1193 "sw_interface_bond_details": { 0xbb7c929b: 0xf5ef2106 },
1194 "pipe_create_reply": { 0xb7ce310c: 0xd4c2c2b3 },
1195 "pipe_details": { 0xc52b799d: 0x43ac107a },
1196 "tap_create_v2": { 0x2d0d6570: 0x445835fd },
1197 "sw_interface_tap_v2_details": { 0x1e2b2a47: 0xe53c16de },
1198 "sw_interface_vhost_user_details": { 0xcee1e53: 0x98530df1 },
1199 "virtio_pci_create": { 0x1944f8db: 0xa9f1370c },
1200 "sw_interface_virtio_pci_details": { 0x6ca9c167: 0x16187f3a },
1201 "p2p_ethernet_add": { 0x36a1a6dc: 0xeeb8e717 },
1202 "p2p_ethernet_del": { 0x62f81c8c: 0xb62c386 },
1203 "geneve_add_del_tunnel": { 0x99445831: 0x976693b5 },
1204 "geneve_tunnel_details": { 0x6b16eb24: 0xe27e2748 },
1205 "gre_tunnel_add_del": { 0xa27d7f17: 0x6efc9c22 },
1206 "gre_tunnel_details": { 0x24435433: 0x3bfbf1 },
1207 "sw_interface_set_flags": { 0xf5aec1b8: 0x6a2b491a },
1208 "sw_interface_event": { 0x2d3d95a7: 0xf709f78d },
1209 "sw_interface_details": { 0x6c221fc7: 0x17b69fa2 },
1210 "sw_interface_add_del_address": { 0x5463d73b: 0x5803d5c4 },
1211 "sw_interface_set_unnumbered": { 0x154a6439: 0x938ef33b },
1212 "sw_interface_set_mac_address": { 0xc536e7eb: 0x6aca746a },
1213 "sw_interface_set_rx_mode": { 0xb04d1cfe: 0x780f5cee },
1214 "sw_interface_rx_placement_details": { 0x9e44a7ce: 0xf6d7d024 },
1215 "create_subif": { 0x790ca755: 0xcb371063 },
1216 "ip_neighbor_add_del": { 0x607c257: 0x105518b6 },
1217 "ip_neighbor_dump": { 0xd817a484: 0xcd831298 },
1218 "ip_neighbor_details": { 0xe29d79f0: 0x870e80b9 },
1219 "want_ip_neighbor_events": { 0x73e70a86: 0x1a312870 },
1220 "ip_neighbor_event": { 0xbdb092b2: 0x83933131 },
1221 "ip_route_add_del": { 0xb8ecfe0d: 0xc1ff832d },
1222 "ip_route_details": { 0xbda8f315: 0xd1ffaae1 },
1223 "ip_route_lookup": { 0x710d6471: 0xe2986185 },
1224 "ip_route_lookup_reply": { 0x5d8febcb: 0xae99de8e },
1225 "ip_mroute_add_del": { 0x85d762f3: 0xf6627d17 },
1226 "ip_mroute_details": { 0x99341a45: 0xc1cb4b44 },
1227 "ip_address_details": { 0xee29b797: 0xb1199745 },
1228 "ip_unnumbered_details": { 0xcc59bd42: 0xaa12a483 },
1229 "mfib_signal_details": { 0x6f4a4cfb: 0x64398a9a },
1230 "ip_punt_redirect": { 0x6580f635: 0xa9a5592c },
1231 "ip_punt_redirect_details": { 0x2cef63e7: 0x3924f5d3 },
1232 "ip_container_proxy_add_del": { 0x7df1dff1: 0x91189f40 },
1233 "ip_container_proxy_details": { 0xa8085523: 0xee460e8 },
1234 "ip_source_and_port_range_check_add_del": { 0x92a067e3: 0x8bfc76f2 },
1235 "sw_interface_ip6_set_link_local_address": { 0x1c10f15f: 0x2931d9fa },
1236 "ip_reassembly_enable_disable": { 0xeb77968d: 0x885c85a6 },
1237 "set_punt": { 0xaa83d523: 0x83799618 },
1238 "punt_socket_register": { 0x95268cbf: 0xc8cd10fa },
1239 "punt_socket_details": { 0xde575080: 0x1de0ce75 },
1240 "punt_socket_deregister": { 0x98fc9102: 0x98a444f4 },
1241 "sw_interface_ip6nd_ra_prefix": { 0x82cc1b28: 0xe098785f },
1242 "ip6nd_proxy_add_del": { 0xc2e4a686: 0x3fdf6659 },
1243 "ip6nd_proxy_details": { 0x30b9ff4a: 0xd35be8ff },
1244 "ip6_ra_event": { 0x364c1c5: 0x47e8cfbe },
1245 "set_ipfix_exporter": { 0x5530c8a0: 0x69284e07 },
1246 "ipfix_exporter_details": { 0xdedbfe4: 0x11e07413 },
1247 "ipip_add_tunnel": { 0x2ac399f5: 0xa9decfcd },
1248 "ipip_6rd_add_tunnel": { 0xb9ec1863: 0x56e93cc0 },
1249 "ipip_tunnel_details": { 0xd31cb34e: 0x53236d75 },
1250 "ipsec_spd_entry_add_del": { 0x338b7411: 0x9f384b8d },
1251 "ipsec_spd_details": { 0x5813d7a2: 0xf2222790 },
1252 "ipsec_sad_entry_add_del": { 0xab64b5c6: 0xb8def364 },
1253 "ipsec_tunnel_protect_update": { 0x30d5f133: 0x143f155d },
1254 "ipsec_tunnel_protect_del": { 0xcd239930: 0xddd2ba36 },
1255 "ipsec_tunnel_protect_details": { 0x21663a50: 0xac6c823b },
1256 "ipsec_tunnel_if_add_del": { 0x20e353fa: 0x2b135e68 },
1257 "ipsec_sa_details": { 0x345d14a7: 0xb30c7f41 },
1258 "l2_xconnect_details": { 0x472b6b67: 0xc8aa6b37 },
1259 "l2_fib_table_details": { 0xa44ef6b8: 0xe8d2fc72 },
1260 "l2fib_add_del": { 0xeddda487: 0xf29d796c },
1261 "l2_macs_event": { 0x44b8fd64: 0x2eadfc8b },
1262 "bridge_domain_details": { 0xfa506fd: 0x979f549d },
1263 "l2_interface_pbb_tag_rewrite": { 0x38e802a8: 0x612efa5a },
1264 "l2_patch_add_del": { 0xa1f6a6f3: 0x522f3445 },
1265 "sw_interface_set_l2_xconnect": { 0x4fa28a85: 0x1aaa2dbb },
1266 "sw_interface_set_l2_bridge": { 0xd0678b13: 0x2e483cd0 },
1267 "bd_ip_mac_add_del": { 0x257c869: 0x5f2b84e2 },
1268 "bd_ip_mac_details": { 0x545af86a: 0xa52f8044 },
1269 "l2_arp_term_event": { 0x6963e07a: 0x85ff71ea },
1270 "l2tpv3_create_tunnel": { 0x15bed0c2: 0x596892cb },
1271 "sw_if_l2tpv3_tunnel_details": { 0x50b88993: 0x1dab5c7e },
1272 "lisp_add_del_local_eid": { 0x4e5a83a2: 0x21f573bd },
1273 "lisp_add_del_map_server": { 0xce19e32d: 0x6598ea7c },
1274 "lisp_add_del_map_resolver": { 0xce19e32d: 0x6598ea7c },
1275 "lisp_use_petr": { 0xd87dbad9: 0x9e141831 },
1276 "show_lisp_use_petr_reply": { 0x22b9a4b0: 0xdcad8a81 },
1277 "lisp_add_del_remote_mapping": { 0x6d5c789e: 0xfae8ed77 },
1278 "lisp_add_del_adjacency": { 0x2ce0e6f6: 0xcf5edb61 },
1279 "lisp_locator_details": { 0x2c620ffe: 0xc0c4c2a7 },
1280 "lisp_eid_table_details": { 0x1c29f792: 0x4bc32e3a },
1281 "lisp_eid_table_dump": { 0x629468b5: 0xb959b73b },
1282 "lisp_adjacencies_get_reply": { 0x807257bf: 0x3f97bcdd },
1283 "lisp_map_resolver_details": { 0x3e78fc57: 0x82a09deb },
1284 "lisp_map_server_details": { 0x3e78fc57: 0x82a09deb },
1285 "one_add_del_local_eid": { 0x4e5a83a2: 0x21f573bd },
1286 "one_add_del_map_server": { 0xce19e32d: 0x6598ea7c },
1287 "one_add_del_map_resolver": { 0xce19e32d: 0x6598ea7c },
1288 "one_use_petr": { 0xd87dbad9: 0x9e141831 },
1289 "show_one_use_petr_reply": { 0x84a03528: 0x10e744a6 },
1290 "one_add_del_remote_mapping": { 0x6d5c789e: 0xfae8ed77 },
1291 "one_add_del_l2_arp_entry": { 0x1aa5e8b3: 0x33209078 },
1292 "one_l2_arp_entries_get_reply": { 0xb0dd200f: 0xb0a47bbe },
1293 "one_add_del_ndp_entry": { 0xf8a287c: 0xd1629a2f },
1294 "one_ndp_entries_get_reply": { 0x70719b1a: 0xbd34161 },
1295 "one_add_del_adjacency": { 0x9e830312: 0xe48e7afe },
1296 "one_locator_details": { 0x2c620ffe: 0xc0c4c2a7 },
1297 "one_eid_table_details": { 0x1c29f792: 0x4bc32e3a },
1298 "one_eid_table_dump": { 0xbd190269: 0x95151038 },
1299 "one_adjacencies_get_reply": { 0x85bab89: 0xa8ed89a5 },
1300 "one_map_resolver_details": { 0x3e78fc57: 0x82a09deb },
1301 "one_map_server_details": { 0x3e78fc57: 0x82a09deb },
1302 "one_stats_details": { 0x2eb74678: 0xff6ef238 },
1303 "gpe_add_del_fwd_entry": { 0xf0847644: 0xde6df50f },
1304 "gpe_fwd_entries_get_reply": { 0xc4844876: 0xf9f53f1b },
1305 "gpe_fwd_entry_path_details": { 0x483df51a: 0xee80b19a },
1306 "gpe_add_del_native_fwd_rpath": { 0x43fc8b54: 0x812da2f2 },
1307 "gpe_native_fwd_rpaths_get_reply": { 0x7a1ca5a2: 0x79d54eb9 },
1308 "sw_interface_set_lldp": { 0x57afbcd4: 0xd646ae0f },
1309 "mpls_ip_bind_unbind": { 0xc7533b32: 0x48249a27 },
1310 "mpls_tunnel_add_del": { 0x44350ac1: 0xe57ce61d },
1311 "mpls_tunnel_details": { 0x57118ae3: 0xf3c0928e },
1312 "mpls_route_add_del": { 0x8e1d1e07: 0x343cff54 },
1313 "mpls_route_details": { 0x9b5043dc: 0xd0ac384c },
1314 "policer_add_del": { 0x2b31dd38: 0xcb948f6e },
1315 "policer_details": { 0x72d0e248: 0xa43f781a },
1316 "qos_store_enable_disable": { 0xf3abcc8b: 0x3507235e },
1317 "qos_store_details": { 0x3ee0aad7: 0x38a6d48 },
1318 "qos_record_enable_disable": { 0x2f1a4a38: 0x25b33f88 },
1319 "qos_record_details": { 0xa425d4d3: 0x4956ccdd },
1320 "session_rule_add_del": { 0xe4895422: 0xe31f9443 },
1321 "session_rules_details": { 0x28d71830: 0x304b91f0 },
1322 "sw_interface_span_enable_disable": { 0x23ddd96b: 0xacc8fea1 },
1323 "sw_interface_span_details": { 0x8a20e79f: 0x55643fc },
1324 "sr_mpls_steering_add_del": { 0x64acff63: 0x7d1b0a0b },
1325 "sr_mpls_policy_assign_endpoint_color": { 0xe7eb978: 0x5e1c5c13 },
1326 "sr_localsid_add_del": { 0x5a36c324: 0x26fa3309 },
1327 "sr_policy_add": { 0x44ac92e8: 0xec79ee6a },
1328 "sr_policy_mod": { 0xb97bb56e: 0xe531a102 },
1329 "sr_steering_add_del": { 0xe46b0a0f: 0x3711dace },
1330 "sr_localsids_details": { 0x2e9221b9: 0x6a6c0265 },
1331 "sr_policies_details": { 0xdb6ff2a1: 0x7ec2d93 },
1332 "sr_steering_pol_details": { 0xd41258c9: 0x1c1ee786 },
1333 "syslog_set_sender": { 0xb8011d0b: 0xbb641285 },
1334 "syslog_get_sender_reply": { 0x424cfa4e: 0xd3da60ac },
1335 "tcp_configure_src_addresses": { 0x67eede0d: 0x4b02b946 },
1336 "teib_entry_add_del": { 0x8016cfd2: 0x5aa0a538 },
1337 "teib_details": { 0x981ee1a1: 0xe3b6a503 },
1338 "udp_encap_add": { 0xf74a60b1: 0x61d5fc48 },
1339 "udp_encap_details": { 0x8cfb9c76: 0x87c82821 },
1340 "vxlan_gbp_tunnel_add_del": { 0x6c743427: 0x8c819166 },
1341 "vxlan_gbp_tunnel_details": { 0x66e94a89: 0x1da24016 },
1342 "vxlan_gpe_add_del_tunnel": { 0xa645b2b0: 0x7c6da6ae },
1343 "vxlan_gpe_tunnel_details": { 0x968fc8b: 0x57712346 },
1344 "vxlan_add_del_tunnel": { 0xc09dc80: 0xa35dc8f5 },
1345 "vxlan_tunnel_details": { 0xc3916cb1: 0xe782f70f },
1346 "vxlan_offload_rx": { 0x9cc95087: 0x89a1564b },
1347 "log_details": { 0x3d61cc0: 0x255827a1 },
1348}
1349
1350
Ole Troan8dbfb432019-04-24 14:31:18 +02001351def foldup_crcs(s):
1352 for f in s:
1353 f.crc = foldup_blocks(f.block,
Mark Nelsonea2abba2020-03-04 15:32:09 -05001354 binascii.crc32(f.crc) & 0xffffffff)
Ole Troan9d420872017-10-12 13:06:35 +02001355
Ole Troan9f84e702020-06-25 14:27:46 +02001356 # fixup the CRCs to make the fix seamless
1357 if f.name in fixup_crc_dict:
1358 if f.crc in fixup_crc_dict.get(f.name):
1359 f.crc = fixup_crc_dict.get(f.name).get(f.crc)
Paul Vinciguerra2cd3cc82019-08-06 22:02:45 -04001360
Ole Troan9d420872017-10-12 13:06:35 +02001361#
1362# Main
1363#
1364def main():
Paul Vinciguerra2cd3cc82019-08-06 22:02:45 -04001365 if sys.version_info < (3, 5,):
1366 log.exception('vppapigen requires a supported version of python. '
1367 'Please use version 3.5 or greater. '
1368 'Using {}'.format(sys.version))
1369 return 1
1370
Ole Troan9d420872017-10-12 13:06:35 +02001371 cliparser = argparse.ArgumentParser(description='VPP API generator')
1372 cliparser.add_argument('--pluginpath', default=""),
1373 cliparser.add_argument('--includedir', action='append'),
Ole Troan2a1ca782019-09-19 01:08:30 +02001374 cliparser.add_argument('--outputdir', action='store'),
Ole Troan5c318c72020-05-05 12:23:47 +02001375 cliparser.add_argument('--input')
Paul Vinciguerra2cd3cc82019-08-06 22:02:45 -04001376 cliparser.add_argument('--output', nargs='?',
1377 type=argparse.FileType('w', encoding='UTF-8'),
1378 default=sys.stdout)
Ole Troan9d420872017-10-12 13:06:35 +02001379
1380 cliparser.add_argument('output_module', nargs='?', default='C')
1381 cliparser.add_argument('--debug', action='store_true')
1382 cliparser.add_argument('--show-name', nargs=1)
Ole Troan5c318c72020-05-05 12:23:47 +02001383 cliparser.add_argument('--git-revision',
1384 help="Git revision to use for opening files")
Ole Troan9d420872017-10-12 13:06:35 +02001385 args = cliparser.parse_args()
1386
1387 dirlist_add(args.includedir)
1388 if not args.debug:
1389 sys.excepthook = exception_handler
1390
1391 # Filename
1392 if args.show_name:
1393 filename = args.show_name[0]
Ole Troan5c318c72020-05-05 12:23:47 +02001394 elif args.input:
1395 filename = args.input
Ole Troan9d420872017-10-12 13:06:35 +02001396 else:
1397 filename = ''
1398
Marek Gradzki51e59682018-03-06 10:05:44 +01001399 if args.debug:
1400 logging.basicConfig(stream=sys.stdout, level=logging.WARNING)
1401 else:
1402 logging.basicConfig()
Marek Gradzki51e59682018-03-06 10:05:44 +01001403
Ole Troan9d420872017-10-12 13:06:35 +02001404 #
1405 # Generate representation
1406 #
Paul Vinciguerraf4647ed2019-02-12 12:21:01 -08001407 from importlib.machinery import SourceFileLoader
Ole Troan9d420872017-10-12 13:06:35 +02001408
1409 # Default path
Ole Troan30787372018-03-01 13:33:39 +01001410 pluginpath = ''
Ole Troan9d420872017-10-12 13:06:35 +02001411 if not args.pluginpath:
Ole Troan30787372018-03-01 13:33:39 +01001412 cand = []
1413 cand.append(os.path.dirname(os.path.realpath(__file__)))
Ole Troan17225df2018-04-11 09:50:03 +02001414 cand.append(os.path.dirname(os.path.realpath(__file__)) +
Ole Troan30787372018-03-01 13:33:39 +01001415 '/../share/vpp/')
1416 for c in cand:
1417 c += '/'
Ole Troan58914252018-10-23 10:50:07 +02001418 if os.path.isfile('{}vppapigen_{}.py'
1419 .format(c, args.output_module.lower())):
Ole Troan30787372018-03-01 13:33:39 +01001420 pluginpath = c
1421 break
Ole Troan9d420872017-10-12 13:06:35 +02001422 else:
1423 pluginpath = args.pluginpath + '/'
Ole Troan30787372018-03-01 13:33:39 +01001424 if pluginpath == '':
Paul Vinciguerra2cd3cc82019-08-06 22:02:45 -04001425 log.exception('Output plugin not found')
1426 return 1
Ole Troan58914252018-10-23 10:50:07 +02001427 module_path = '{}vppapigen_{}.py'.format(pluginpath,
1428 args.output_module.lower())
Ole Troan9d420872017-10-12 13:06:35 +02001429
1430 try:
Paul Vinciguerraf4647ed2019-02-12 12:21:01 -08001431 plugin = SourceFileLoader(args.output_module,
1432 module_path).load_module()
Ole Troan58914252018-10-23 10:50:07 +02001433 except Exception as err:
Paul Vinciguerra2cd3cc82019-08-06 22:02:45 -04001434 log.exception('Error importing output plugin: {}, {}'
1435 .format(module_path, err))
1436 return 1
Ole Troan9d420872017-10-12 13:06:35 +02001437
Paul Vinciguerra9046e442020-11-20 23:10:09 -05001438 parser = VPPAPI(debug=args.debug, filename=filename, logger=log,
1439 revision=args.git_revision)
1440
1441 try:
1442 if not args.input:
1443 parsed_objects = parser.parse_fd(sys.stdin, log)
1444 else:
1445 parsed_objects = parser.parse_filename(args.input, log)
1446 except ParseError as e:
1447 print('Parse error: ', e, file=sys.stderr)
1448 sys.exit(1)
1449
1450 # Build a list of objects. Hash of lists.
1451 result = []
1452
1453 # if the variable is not set in the plugin, assume it to be false.
1454 try:
1455 plugin.process_imports
1456 except AttributeError:
1457 plugin.process_imports = False
1458
1459 if plugin.process_imports:
1460 result = parser.process_imports(parsed_objects, False, result)
1461 s = parser.process(result)
1462 else:
1463 s = parser.process(parsed_objects)
1464
1465 # Add msg_id field
1466 s['Define'] = add_msg_id(s['Define'])
1467
1468 # Fold up CRCs
1469 foldup_crcs(s['Define'])
1470
1471 #
1472 # Debug
1473 if args.debug:
1474 import pprint
1475 pp = pprint.PrettyPrinter(indent=4, stream=sys.stderr)
1476 for t in s['Define']:
1477 pp.pprint([t.name, t.flags, t.block])
1478 for t in s['types']:
1479 pp.pprint([t.name, t.block])
1480
Ole Troan2a1ca782019-09-19 01:08:30 +02001481 result = plugin.run(args, filename, s)
Ole Troan9d420872017-10-12 13:06:35 +02001482 if result:
Ole Troan17225df2018-04-11 09:50:03 +02001483 print(result, file=args.output)
Ole Troan9d420872017-10-12 13:06:35 +02001484 else:
Paul Vinciguerra2cd3cc82019-08-06 22:02:45 -04001485 log.exception('Running plugin failed: {} {}'
1486 .format(filename, result))
1487 return 1
1488 return 0
Ole Troan9d420872017-10-12 13:06:35 +02001489
1490
1491if __name__ == '__main__':
Paul Vinciguerra2cd3cc82019-08-06 22:02:45 -04001492 sys.exit(main())