/
strategies.py
341 lines (299 loc) · 10 KB
/
strategies.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#Samuel Jero <sjero@purdue.edu>
#SDN Testing Strategy Generation
import os
import sys
import time
from datetime import datetime
import openflow
import manipulations
import re
import pprint
from types import NoneType
system_home = os.path.split(os.path.dirname(os.path.realpath(__file__)))[0]
config_path = os.path.abspath(os.path.join(system_home, 'config'))
sys.path.insert(0,config_path)
import config
class StrategyGenerator:
#Constructor
def __init__(self, lg, res_lg):
self.lg = lg
self.results = res_lg
self.ck_file = None
self.do_ckpt = False
self.single_strat = []
self.strat_lst = []
self.strat_ptr = 0
self.failed_lst = []
self.failed_ptr = 0
def next_strategy(self):
# Check for new failed strategies that need to be retried
if (self.failed_ptr < len(self.failed_lst)):
strat = self.failed_lst[self.failed_ptr]
self.failed_ptr+=1
return strat
#Check if all strategies have been tested
if (self.strat_ptr >= len(self.strat_lst)):
return None
#Select next strategy
strat = self.strat_lst[self.strat_ptr]
self.strat_ptr+=1
if self.strat_ptr % 100 == 0:
self.lg.write("[%s] Returning Strategy: %d/%d\n" % (str(datetime.today()),self.strat_ptr, len(self.strat_lst)))
print "[%s] Testing Strategy: %d/%d" % (str(datetime.today()),self.strat_ptr, len(self.strat_lst))
self.checkpoint()
return strat
def return_strategy(self, strat):
self.strat_lst.insert(self.strat_ptr + 1, strat)
def strategy_result(self, strat, res):
if res[0] == False:
if strat not in self.failed_lst:
#Most failed strategies will be retried once, to avoid false positives
self.failed_lst.append(strat)
else:
#Final failure, record in result file
lst = ["FAILED", str(datetime.today()),strat, res[1]]
self.results.write("%s\n" %(str(lst)))
self.results.flush()
self.lg.write("[%s] Strategy HARD FAILED: %s\n" % (str(datetime.today()),str(strat)))
print "[%s] Strategy HARD FAILED: %s" % (str(datetime.today()),str(strat))
else:
#Test Succeeded
pass
def strategy_feedback(self, strat, feedback, result = None):
changed = False
if type(feedback)!=dict:
return
#Switch Strategies
if 'msg_types' in feedback:
msg_types = feedback['msg_types']
#Prioritize Strategies with these messges types
if result is None or result is True:
for st in self.single_strat:
switch_strat = st['switch']
priority = st['priority']
for t in msg_types:
if t in str(switch_strat):
if priority < 100:
priority = 100
st['priority'] = priority
if strat is not None:
#New message under this strategy
tmp = dict(strat)
tmp['switch'] = strat['switch'] + switch_strat
if not isinstance(tmp['switch'],dict) or not isinstance(tmp['switch'],list) or len(tmp['switch'] > 2):
print "Warning. Possible runaway strategy"
print str(tmp)
self.lg.write("Warning. Possible runaway strategy\n")
self.lg.write(str(tmp)+"\n")
self.lg.flush()
else:
self.strat_lst.append(tmp)
else:
#Baseline messages
self.strat_lst.append(st)
changed = True
break
if changed:
#Sort Strategies by priority
remaining = self.strat_lst[self.strat_ptr:]
remaining.sort(reverse=True, key = lambda s: s['priority'])
self.strat_lst = self.strat_lst[0: self.strat_ptr] + remaining
return
def build_strategies(self):
#Switch Only Strategies
for pkt in openflow.openflow:
print "Creating Strategies for " + pkt[0] + "..."
mtype = pkt[0]
fields = pkt[1]
#Message Delivery Strategies
for c in config.coord_test_controllers:
for switch in config.coord_test_switches:
for t in manipulations.message_actions:
act = t[1]
for v in t[2]:
strat = "{controllers[" +str(c) + "]}" + ",{sw},*,{pkt_type},*,{action}".format(sw=switch, pkt_type=mtype,action=act.format(v))
self.single_strat.append({'topo':config.coord_test_case, 'switch':[strat], 'host':None, 'priority':0})
#Message Modification Strategies
flds = self.build_field_list(fields)
for c in config.coord_test_controllers:
for switch in config.coord_test_switches:
for f in flds:
for a in manipulations.field_actions:
for m in manipulations.field_lies:
for v in manipulations.field_lie_values[f['type']]:
strat = "{controllers[" +str(c) + "]}" + ",{sw},*,{pkt_type},{fld},{action}".format(sw=switch, pkt_type=mtype,fld=f['field'],action=a[1].format(m,v))
self.single_strat.append({'topo':config.coord_test_case, 'switch':[strat], 'host':None, 'priority':0})
self.lg.write("[%s] Single Strategies: %d\n" % (str(datetime.today()),len(self.single_strat)))
print "[%s] Single Strategies: %d" % (str(datetime.today()),len(self.single_strat))
def build_field_list(self,fields):
lst = []
if len(fields)==1 and 'type' in fields[0] and (fields[0]['type']=='list' or fields[0]['type']=='TLV'):
#Lists
l = self.build_field_list(fields[0]['fields'])
iterations = config.coord_test_list_iters
if('max' in fields[0]):
iterations = fields[0]['max']
for i in range(1,iterations + 1):
for elm in l:
lst.append({'field':str(i) + "." + elm['field'], 'type':elm['type']})
else:
#Normal structures
for i,f in enumerate(fields):
string = str(i + 1)
#Ignore fields we can't change
if 'impl' in f and f['impl'] is False:
continue
#Subfields
if 'fields' in f:
l = self.build_field_list(f['fields'])
for i in l:
lst.append({'field':string + "." + i['field'], 'type':i['type']})
else:
lst.append({'field':string, 'type':f['type']})
return lst
@staticmethod
def pretty_print_strat(strat):
if type(strat) != dict:
#Old style strats, 3-17-2016
return StrategyGenerator.pretty_print_switch_strat(strat)
switch = strat['switch']
host = strat['host']
topo = strat['topo']
priority = 0
if priority in strat:
priority = strat['priority']
return "topo: {0}, switch: {1}, priority: {3}".format(str(topo),StrategyGenerator.pretty_print_switch_strat(switch),str(host),str(priority))
@staticmethod
def pretty_print_switch_strat(strat):
if type(strat) != str:
return ""
flist = strat.split(",")
if len(flist) < 7:
return ""
#Controller
ctlr = ""
mo = re.search("\{controllers\[([0-9]+)\]\}", flist[0])
if type(mo) is not NoneType:
ctlr = mo.group(1)
#Switch
sw = ""
sw = flist[1]
#Version
ver = ""
ver = flist[2]
#Message type
msgtype = ""
msgtype = flist[3]
#Field
field = ""
field = StrategyGenerator._pretty_print_switch_field(msgtype, flist[4])
#Action
act = ""
act = flist[5]
#Args
args = ""
args = flist[6]
return "{0}, {1}, {2}, {3}, {4}, {5}, {6}".format(ctlr,sw,ver,msgtype,field,act,args)
@staticmethod
def _pkt_type_from_switch_strat(strat):
if type(strat)!=str:
return ""
flist = strat.split(",")
if len(flist) < 4:
return ""
return flist[3]
@staticmethod
def _pretty_print_switch_field(msg_type, field):
if type(field)!=str or type(msg_type)!=str:
return ""
if field == "*":
return field
flist = field.split(".")
fdata = []
for i in openflow.openflow:
if i[0]==msg_type:
fdata = i[1]
break
if len(fdata)==0:
return ""
full=""
for i in flist:
v = int(i) - 1
if len(fdata)==1 and 'type' in fdata[0] and (fdata[0]['type']=='list' or fdata[0]['type']=='TLV'):
full+=i
full+="."
fdata = fdata[0]['fields']
elif len(fdata) <= v:
full+= "..."
break
else:
full +=fdata[v]['name']
full +="."
if 'fields' in fdata[v]:
fdata = fdata[v]['fields']
else:
fdata = []
if full[len(full)-1]=='.':
full = full[0:len(full)-1]
return full
def enable_checkpointing(self, f):
self.ck_file = f
self.do_ckpt = True
self.checkpoint()
def checkpoint(self):
if self.do_ckpt and self.ck_file is not None:
self.lg.write("[%s] Making Checkpoint\n" % (str(datetime.today())))
print "[%s] Making Checkpoint" % (str(datetime.today()))
#Create backup
bkup = {}
bkup['version'] = 3
bkup['strat_lst'] = self.strat_lst
bkup['strat_ptr'] = self.strat_ptr
bkup['failed_lst'] = self.failed_lst
bkup['failed_ptr'] = self.failed_ptr
bkup['single_strat'] = self.single_strat
#Format
pp = pprint.PrettyPrinter()
fmtbkup = pp.pformat(bkup)
#Write backup
try:
self.ck_file.seek(0)
self.ck_file.truncate()
self.ck_file.write(fmtbkup)
self.ck_file.flush()
except Exception as e:
print "[%s] Checkpoint Failed: %s" % (str(datetime.today()),str(e))
return
self.lg.write("[%s] Checkpoint Finished\n" % (str(datetime.today())))
print "[%s] Checkpoint Finished" % (str(datetime.today()))
def restore(self, f):
#Read backup
try:
inp = f.readlines()
inp = "\n".join(inp)
bkup = eval(inp)
except Exception as e:
print "[%s] Failed to read checkpoint: %s" % (str(datetime.today()),str(e))
f.close()
return False
#Restore Backup
if bkup['version'] != 3:
print "Warning: Checkpoint is incompatable!!!"
f.close()
return False
self.strat_lst = bkup['strat_lst']
self.strat_ptr = bkup['strat_ptr']
self.failed_lst = bkup['failed_lst']
self.failed_ptr = bkup['failed_ptr']
self.single_strat = bkup['single_strat']
f.close()
self.lg.write("[%s] Restore Finished\n" % (str(datetime.today())))
print "[%s] Restore Finished" % (str(datetime.today()))
return True
def load_from_file(self, f):
strat = None
for line in f:
if line.find("#") >= 0:
continue
strat = eval(line)
self.strat_lst.append(strat)