forked from Resonance-SDN/pyresonance
/
main.py
217 lines (177 loc) · 7.4 KB
/
main.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
from pyretic.lib.corelib import *
from pyretic.lib.std import *
from pyretic.modules.mac_learner import mac_learner
from dos.flow_scanner import scan
from .globals import *
from multiprocessing import Process, Queue
import threading
import time
import subprocess
from importlib import import_module
import sys
import re
# Dynamic resonance policy ######
#def resonance(self, app_to_module_map, app_composition_str):
class resonance(DynamicPolicy):
def __init__(self, app_to_module_map, app_composition_str):
super(resonance,self).__init__()
self.app_composition_str = app_composition_str
self.app_to_module_map = app_to_module_map
self.app_to_policy_map = {}
self.user_fsm_list = []
self.fsm_to_policy_map = {}
self.user_policy_list = []
# Create queue for receiving state transition notification
queue = Queue()
# Get user-defined FSMs, make them, make eventListeners
for idx, app in enumerate(self.app_to_module_map):
user_fsm, user_policy = self.app_to_module_map[app].main(queue)
self.user_fsm_list.append(user_fsm)
self.fsm_to_policy_map[user_fsm] = user_policy
self.user_policy_list.append(user_policy)
self.app_to_policy_map[app] = user_policy
## Adding the feature to determine the comp variable
## to determine action for the module while turning it off
for pname in self.app_to_policy_map.keys():
po = self.app_to_policy_map[pname]
self.update_comp(po,pname, self.app_composition_str)
# Start signal catcher thread
t1 = threading.Thread(target=self.transition_signal_catcher, args=(queue,))
t1.daemon = True
t1.start()
# Set the policy
self.update_policy()
# Composing department policies, switch-based
def compose_policy_departments_switchbased(self):
final_policy = parallel([(fsm.get_match_switch() >> self.fsm_to_policy_map[fsm].action()) \
for fsm in self.fsm_to_policy_map])
return final_policy
# Composing policy
def compose_policy(self):
policy = drop
policy_str = self.app_composition_str
# Get composition string, replace with relevant ones.
for app in self.app_to_policy_map:
id = policy_str.find(app)
if id != -1:
if self.app_to_policy_map[app] in self.user_policy_list:
policy_index = self.user_policy_list.index(self.app_to_policy_map[app])
replace_str = 'self.user_policy_list[' + str(policy_index) + '].action()'
policy_str = policy_str.replace(app, replace_str)
# print 'Raw string: ' + policy_str
# print 'Evaluated: \n' + str(eval(policy_str))
return eval(policy_str)
# Updating policy
def update_policy(self):
if self.app_composition_str == '':
self.policy = self.compose_policy_departments_switchbased()
else:
# self.policy = self.compose_policy() + if_(match(ethtype=2054), passthrough, drop)
self.policy = self.compose_policy()
# self.policy = union([if_(match(srcip='10.0.0.1'), passthrough,drop), if_(match(srcip='10.0.0.2'), passthrough,drop)])
# # Record
# ts = time.time()
# subprocess.call("echo %.7f >> /home/mininet/hyojoon/benchmark/pyresonance-benchmark/event_test/output/process_time/of.txt"%(ts), shell=True)
# print 'Policy:'
# print self.policy
# Listen for state transitions.
def transition_signal_catcher(self,queue):
while 1:
try:
line = queue.get(timeout=.1)
except:
continue
else: # Got line
self.update_policy()
def update_comp(self, po,pname,strn):
if strn == '': # probably auto mode.
po.fsm.comp.value = 0
else:
temp = strn.split(' ')
if temp.count(pname) > 0:
ind = temp.index(pname)
pre=''
post=''
if ind-1>0:
pre=temp[ind-1]
if ind+1<len(temp):
post=temp[ind+1]
if pre =='+' or post=='+':
po.fsm.comp.value = 1
else:
po.fsm.comp.value = 0
else:
pass
# Parsing configuration file
def parse_configuration_file(content, mode, repeat):
app_to_module_map = {} # {app name : module object} dictionary
app_composition_str = '' # Policy composition in string format
# Get application list and import
match = re.search('APPLICATIONS = \{(.*)\}\n+COMPOSITION = \{',content, flags=re.DOTALL)
if match:
apps = match.group(1).split(',')
### TEST
if repeat != 0:
apps = [apps[0]]*int(repeat)
### TEST
print '\n*** Specified Modules are: ***'
for app in apps:
app_fix = app.strip('\n').strip()
if app_fix != '' and app_fix.startswith('#') is False:
try:
module = import_module(app_fix)
except Exception as err:
print 'Import Exception: ', err
sys.exit(1)
split_list = app.split('.')
app_to_module_map[split_list[-1]] = module
print app + ' (' + split_list[-1] + ')'
# Get application composition
if mode.__eq__('auto'):
app_composition_str = ''
elif mode.__eq__('manual'):
### TEST
if repeat != 0:
app_composition_str = 'passthrough >> ' *int(repeat)
app_composition_str = app_composition_str.rstrip(' >> ')
print '\n\n*** The Policy Composition is: ***\n' + app_composition_str + '\n'
### TEST
else:
# Get Composition.
match = re.search('COMPOSITION = \{(.*)\}',content, flags=re.DOTALL)
if match:
app_compose_list = match.group(1).split('\n')
for app_compose_item in app_compose_list:
app_composition_str = app_compose_item.strip('\n').strip()
if app_composition_str != '' and app_composition_str.startswith('#') is False:
print '\n\n*** The Policy Composition is: ***\n' + app_composition_str + '\n'
break
return app_to_module_map, app_composition_str
# Main ######
def main(config, mode, modrepeat=None):
# Open configuration file
try:
fd = open(config, 'r')
except IOError as err:
print 'IO Exception: ', err
sys.exit(1)
# Get mode, check validity
if mode != 'auto' and mode != 'manual':
print 'Wrong mode value. Exiting!'
sys.exit(1)
# Check test mode.
repeat = 0
if modrepeat is not None:
if modrepeat != 0:
repeat = modrepeat
# Read configuration file
content = fd.read()
fd.close()
# Parse configuration file
app_to_module_map, app_composition_str = parse_configuration_file(content, mode, repeat)
if len(app_to_module_map) == 0:
print 'Configuration file seems incorrect. Exiting.'
sys.exit(1)
# Run resonance
# return resonance(app_to_module_map, app_composition_str) >> mac_learner()
return resonance(app_to_module_map, app_composition_str) >> (mac_learner() + scan())