-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-bmv2-test.py
executable file
·317 lines (283 loc) · 10.5 KB
/
run-bmv2-test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env python
# Copyright 2013-present Barefoot Networks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Runs the compiler on a sample P4 program generating code for the BMv2
# behavioral model simulator
from __future__ import print_function
from subprocess import Popen
from threading import Thread
import json
import sys
import re
import os
import stat
import tempfile
import shutil
import difflib
import subprocess
import time
import random
import errno
from string import maketrans
try:
from scapy.layers.all import *
from scapy.utils import *
except ImportError:
pass
from bmv2stf import RunBMV2
SUCCESS = 0
FAILURE = 1
class Options(object):
def __init__(self):
self.binary = "" # this program's name
self.cleanupTmp = True # if false do not remote tmp folder created
self.p4Filename = "" # file that is being compiled
self.compilerSrcDir = "" # path to compiler source tree
self.verbose = False
self.replace = False # replace previous outputs
self.compilerOptions = []
self.hasBMv2 = False # Is the behavioral model installed?
self.runDebugger = False
self.observationLog = None # Log packets produced by the BMV2 model if path to log is supplied
def nextWord(text, sep = " "):
# Split a text at the indicated separator.
# Note that the separator can be a string.
# Separator is discarded.
pos = text.find(sep)
if pos < 0:
return text, ""
l, r = text[0:pos].strip(), text[pos+len(sep):len(text)].strip()
# print(text, "/", sep, "->", l, "#", r)
return l, r
class ConfigH(object):
# Represents an autoconf config.h file
# fortunately the structure of these files is very constrained
def __init__(self, file):
self.file = file
self.vars = {}
with open(file) as a:
self.text = a.read()
self.ok = False
self.parse()
def parse(self):
while self.text != "":
self.text = self.text.strip()
if self.text.startswith("/*"):
end = self.text.find("*/")
if end < 1:
reportError("Unterminated comment in config file")
return
self.text = self.text[end+2:len(self.text)]
elif self.text.startswith("#define"):
define, self.text = nextWord(self.text)
macro, self.text = nextWord(self.text)
value, self.text = nextWord(self.text, "\n")
self.vars[macro] = value
elif self.text.startswith("#ifndef"):
junk, self.text = nextWord(self.text, "#endif")
else:
reportError("Unexpected text:", self.text)
return
self.ok = True
def __str__(self):
return str(self.vars)
def usage(options):
name = options.binary
print(name, "usage:")
print(name, "rootdir [options] file.p4")
print("Invokes compiler on the supplied file, possibly adding extra arguments")
print("`rootdir` is the root directory of the compiler source tree")
print("options:")
print(" -b: do not remove temporary results for failing tests")
print(" -v: verbose operation")
print(" -f: replace reference outputs with newly generated ones")
print(" -observation-log <file>: save packet output to <file>")
def isError(p4filename):
# True if the filename represents a p4 program that should fail
return "_errors" in p4filename
def reportError(*message):
print("***", *message)
class Local(object):
# object to hold local vars accessable to nested functions
pass
def run_timeout(options, args, timeout, stderr):
if options.verbose:
print("Executing ", " ".join(args))
local = Local()
local.process = None
def target():
procstderr = None
if stderr is not None:
procstderr = open(stderr, "w")
local.process = Popen(args, stderr=procstderr)
local.process.wait()
thread = Thread(target=target)
thread.start()
thread.join(timeout)
if thread.is_alive():
print("Timeout ", " ".join(args), file=sys.stderr)
local.process.terminate()
thread.join()
if local.process is None:
# never even started
reportError("Process failed to start")
return -1
if options.verbose:
print("Exit code ", local.process.returncode)
return local.process.returncode
timeout = 10 * 60
def run_model(options, tmpdir, jsonfile):
if not options.hasBMv2:
return SUCCESS
# We can do this if an *.stf file is present
basename = os.path.basename(options.p4filename)
base, ext = os.path.splitext(basename)
dirname = os.path.dirname(options.p4filename)
testfile = dirname + "/" + base + ".stf"
print("Check for ", testfile)
if not os.path.isfile(testfile):
# If no stf file is present just use the empty file
testfile = dirname + "/empty.stf"
if not os.path.isfile(testfile):
# If no empty.stf present, don't try to run the model at all
return SUCCESS
bmv2 = RunBMV2(tmpdir, options, jsonfile)
result = bmv2.generate_model_inputs(testfile)
if result != SUCCESS:
return result
result = bmv2.run()
if result != SUCCESS:
return result
result = bmv2.checkOutputs()
return result
def process_file(options, argv):
assert isinstance(options, Options)
tmpdir = tempfile.mkdtemp(dir=".")
basename = os.path.basename(options.p4filename)
base, ext = os.path.splitext(basename)
dirname = os.path.dirname(options.p4filename)
expected_dirname = dirname + "_outputs" # expected outputs are here
if options.verbose:
print("Writing temporary files into ", tmpdir)
if options.testName:
jsonfile = options.testName + ".json"
else:
jsonfile = tmpdir + "/" + base + ".json"
stderr = tmpdir + "/" + basename + "-stderr"
if not os.path.isfile(options.p4filename):
raise Exception("No such file " + options.p4filename)
args = ["./p4c-bm2-ss", "-o", jsonfile] + options.compilerOptions
if "p4_14" in options.p4filename or "v1_samples" in options.p4filename:
args.extend(["--p4v", "1.0"]);
args.extend(argv) # includes p4filename
if options.runDebugger:
args[0:0] = options.runDebugger.split()
os.execvp(args[0], args)
result = run_timeout(options, args, timeout, stderr)
if result != SUCCESS:
print("Error compiling")
print("".join(open(stderr).readlines()))
expected_error = isError(options.p4filename)
if expected_error:
# invert result
if result == SUCCESS:
result = FAILURE
else:
result = SUCCESS
if result == SUCCESS and not expected_error:
result = run_model(options, tmpdir, jsonfile);
if options.cleanupTmp:
if options.verbose:
print("Removing", tmpdir)
shutil.rmtree(tmpdir)
return result
######################### main
def main(argv):
options = Options()
options.binary = argv[0]
if len(argv) <= 2:
usage(options)
sys.exit(FAILURE)
options.compilerSrcDir = argv[1]
argv = argv[2:]
if not os.path.isdir(options.compilerSrcDir):
print(options.compilerSrcDir + " is not a folder", file=sys.stderr)
usage(options)
sys.exit(FAILURE)
while argv[0][0] == '-':
if argv[0] == "-b":
options.cleanupTmp = False
elif argv[0] == "-v":
options.verbose = True
elif argv[0] == "-f":
options.replace = True
elif argv[0] == "-a":
if len(argv) == 0:
reportError("Missing argument for -a option")
usage(options)
sys.exit(FAILURE)
else:
options.compilerOptions += argv[1].split();
argv = argv[1:]
elif argv[0][1] == 'D' or argv[0][1] == 'I' or argv[0][1] == 'T':
options.compilerOptions.append(argv[0])
elif argv[0] == "-gdb":
options.runDebugger = "gdb --args"
elif argv[0] == '-observation-log':
if len(argv) == 0:
reportError("Missing argument for -observation-log option")
usage(options)
sys.exit(FAILURE)
else:
options.observationLog = argv[1]
argv = argv[1:]
elif argv[0] == "--pp":
options.compilerOptions.append(argv[0])
argv = argv[1:]
options.compilerOptions.append(argv[0])
else:
reportError("Unknown option ", argv[0])
usage(options)
sys.exit(FAILURE)
argv = argv[1:]
config = ConfigH("config.h")
if not config.ok:
print("Error parsing config.h")
sys.exit(FAILURE)
options.hasBMv2 = "HAVE_SIMPLE_SWITCH" in config.vars
if not options.hasBMv2:
reportError("config.h indicates that BMv2 is not installed; will skip running BMv2 tests")
options.p4filename=argv[-1]
options.testName = None
if options.p4filename.startswith(options.compilerSrcDir):
options.testName = options.p4filename[len(options.compilerSrcDir):];
if options.testName.startswith('/'):
options.testName = options.testName[1:]
if options.testName.endswith('.p4'):
options.testName = options.testName[:-3]
options.testName = "bmv2/" + options.testName
if not options.observationLog:
if options.testName:
options.observationLog = os.path.join('%s.p4.obs' % options.testName)
else:
basename = os.path.basename(options.p4filename)
base, ext = os.path.splitext(basename)
dirname = os.path.dirname(options.p4filename)
options.observationLog = os.path.join(dirname, '%s.p4.obs' % base)
result = process_file(options, argv)
if result != SUCCESS:
reportError("Test failed")
sys.exit(result)
if __name__ == "__main__":
main(sys.argv)