1
|
|
|
"""Main module of amlight/sdntrace_cp Kytos Network Application. |
2
|
|
|
|
3
|
|
|
Run tracepaths on OpenFlow in the Control Plane |
4
|
|
|
""" |
5
|
|
|
|
6
|
1 |
|
import ipaddress |
7
|
1 |
|
from datetime import datetime |
8
|
|
|
|
9
|
1 |
|
from flask import jsonify, request |
10
|
1 |
|
from kytos.core import KytosNApp, log, rest |
11
|
1 |
|
from napps.amlight.sdntrace_cp import settings |
12
|
1 |
|
from napps.amlight.sdntrace_cp.automate import Automate |
13
|
1 |
|
from napps.amlight.sdntrace_cp.utils import (convert_entries, |
14
|
|
|
convert_list_entries, |
15
|
|
|
find_endpoint, get_stored_flows, |
16
|
|
|
prepare_json) |
17
|
|
|
|
18
|
|
|
|
19
|
1 |
|
class Main(KytosNApp): |
20
|
|
|
"""Main class of amlight/sdntrace_cp NApp. |
21
|
|
|
|
22
|
|
|
This application gets the list of flows from the switches |
23
|
|
|
and uses it to trace paths without using the data plane. |
24
|
|
|
""" |
25
|
|
|
|
26
|
1 |
|
def setup(self): |
27
|
|
|
"""Replace the '__init__' method for the KytosNApp subclass. |
28
|
|
|
|
29
|
|
|
The setup method is automatically called by the controller when your |
30
|
|
|
application is loaded. |
31
|
|
|
|
32
|
|
|
""" |
33
|
1 |
|
log.info("Starting Kytos SDNTrace CP App!") |
34
|
|
|
|
35
|
1 |
|
self.traces = {} |
36
|
1 |
|
self.last_id = 30000 |
37
|
1 |
|
self.automate = Automate(self) |
38
|
1 |
|
self.automate.schedule_traces() |
39
|
1 |
|
self.automate.schedule_important_traces() |
40
|
|
|
|
41
|
1 |
|
def execute(self): |
42
|
|
|
"""This method is executed right after the setup method execution. |
43
|
|
|
|
44
|
|
|
You can also use this method in loop mode if you add to the above setup |
45
|
|
|
method a line like the following example: |
46
|
|
|
|
47
|
|
|
self.execute_as_loop(30) # 30-second interval. |
48
|
|
|
""" |
49
|
|
|
|
50
|
1 |
|
def shutdown(self): |
51
|
|
|
"""This method is executed when your napp is unloaded. |
52
|
|
|
|
53
|
|
|
If you have some cleanup procedure, insert it here. |
54
|
|
|
""" |
55
|
|
|
self.automate.unschedule_ids() |
56
|
|
|
self.automate.sheduler_shutdown(wait=False) |
57
|
|
|
|
58
|
1 |
|
@rest('/trace', methods=['PUT']) |
59
|
1 |
|
def trace(self): |
60
|
|
|
"""Trace a path.""" |
61
|
1 |
|
entries = request.get_json() |
62
|
1 |
|
entries = convert_entries(entries) |
63
|
1 |
|
stored_flows = get_stored_flows() |
64
|
1 |
|
result = self.tracepath(entries, stored_flows) |
65
|
1 |
|
return jsonify(prepare_json(result)) |
66
|
|
|
|
67
|
1 |
|
@rest('/traces', methods=['PUT']) |
68
|
1 |
|
def get_traces(self): |
69
|
|
|
"""For bulk requests.""" |
70
|
1 |
|
entries = request.get_json() |
71
|
1 |
|
entries = convert_list_entries(entries) |
72
|
1 |
|
stored_flows = get_stored_flows() |
73
|
1 |
|
results = [] |
74
|
1 |
|
for entry in entries: |
75
|
1 |
|
results.append(self.tracepath(entry, stored_flows)) |
76
|
1 |
|
return jsonify(prepare_json(results)) |
77
|
|
|
|
78
|
1 |
|
def tracepath(self, entries, stored_flows): |
79
|
|
|
"""Trace a path for a packet represented by entries.""" |
80
|
1 |
|
self.last_id += 1 |
81
|
1 |
|
trace_id = self.last_id |
82
|
1 |
|
trace_result = [] |
83
|
1 |
|
trace_type = 'starting' |
84
|
1 |
|
do_trace = True |
85
|
1 |
|
while do_trace: |
86
|
1 |
|
if 'dpid' not in entries or 'in_port' not in entries: |
87
|
|
|
break |
88
|
1 |
|
trace_step = {'in': {'dpid': entries['dpid'], |
89
|
|
|
'port': entries['in_port'], |
90
|
|
|
'time': str(datetime.now()), |
91
|
|
|
'type': trace_type}} |
92
|
1 |
|
if 'dl_vlan' in entries: |
93
|
1 |
|
trace_step['in'].update({'vlan': entries['dl_vlan'][-1]}) |
94
|
|
|
|
95
|
1 |
|
switch = self.controller.get_switch_by_dpid(entries['dpid']) |
96
|
1 |
|
if not switch: |
97
|
|
|
break |
98
|
1 |
|
result = self.trace_step(switch, entries, stored_flows) |
99
|
1 |
|
if result: |
100
|
1 |
|
out = {'port': result['out_port']} |
101
|
1 |
|
if 'dl_vlan' in result['entries']: |
102
|
1 |
|
out.update({'vlan': result['entries']['dl_vlan'][-1]}) |
103
|
1 |
|
trace_step.update({ |
104
|
|
|
'out': out |
105
|
|
|
}) |
106
|
1 |
|
if 'dpid' in result: |
107
|
1 |
|
next_step = {'dpid': result['dpid'], |
108
|
|
|
'port': result['in_port']} |
109
|
1 |
|
if self.has_loop(next_step, trace_result): |
110
|
|
|
# Loop |
111
|
1 |
|
do_trace = False |
112
|
|
|
else: |
113
|
1 |
|
entries = result['entries'] |
114
|
1 |
|
entries['dpid'] = result['dpid'] |
115
|
1 |
|
entries['in_port'] = result['in_port'] |
116
|
1 |
|
trace_type = 'trace' |
117
|
|
|
else: |
118
|
1 |
|
trace_step['in']['type'] = 'last' |
119
|
1 |
|
do_trace = False |
120
|
|
|
else: |
121
|
|
|
# Incomplete |
122
|
|
|
break |
123
|
1 |
|
trace_result.append(trace_step) |
124
|
1 |
|
self.traces.update({ |
125
|
|
|
trace_id: trace_result |
126
|
|
|
}) |
127
|
1 |
|
return trace_result |
128
|
|
|
|
129
|
1 |
|
@staticmethod |
130
|
1 |
|
def has_loop(trace_step, trace_result): |
131
|
|
|
"""Check if there is a loop in the trace result.""" |
132
|
1 |
|
for trace in trace_result: |
133
|
1 |
|
if trace['in']['dpid'] == trace_step['dpid'] and \ |
134
|
|
|
trace['in']['port'] == trace_step['port']: |
135
|
1 |
|
return True |
136
|
1 |
|
return False |
137
|
|
|
|
138
|
1 |
|
def trace_step(self, switch, entries, stored_flows): |
139
|
|
|
"""Perform a trace step. |
140
|
|
|
|
141
|
|
|
Match the given fields against the switch's list of flows.""" |
142
|
1 |
|
flow, entries, port = self.match_and_apply( |
143
|
|
|
switch, |
144
|
|
|
entries, |
145
|
|
|
stored_flows |
146
|
|
|
) |
147
|
|
|
|
148
|
1 |
|
if not flow or not port: |
149
|
1 |
|
return None |
150
|
|
|
|
151
|
1 |
|
endpoint = find_endpoint(switch, port) |
152
|
1 |
|
if endpoint is None: |
153
|
1 |
|
return {'out_port': port, |
154
|
|
|
'entries': entries} |
155
|
|
|
|
156
|
1 |
|
return {'dpid': endpoint.switch.dpid, |
157
|
|
|
'in_port': endpoint.port_number, |
158
|
|
|
'out_port': port, |
159
|
|
|
'entries': entries} |
160
|
|
|
|
161
|
1 |
|
def update_circuits(self): |
162
|
|
|
"""Update the list of circuits after a flow change.""" |
163
|
|
|
# pylint: disable=unused-argument |
164
|
1 |
|
if settings.FIND_CIRCUITS_IN_FLOWS: |
165
|
1 |
|
self.automate.find_circuits() |
166
|
|
|
|
167
|
1 |
|
@classmethod |
168
|
1 |
|
def do_match(cls, flow, args): |
169
|
|
|
"""Match a packet against this flow (OF1.3).""" |
170
|
|
|
# pylint: disable=consider-using-dict-items |
171
|
1 |
|
if ('match' not in flow['flow']) or (len(flow['flow']['match']) == 0): |
172
|
1 |
|
return False |
173
|
1 |
|
for name in flow['flow']['match']: |
174
|
1 |
|
field_flow = flow['flow']['match'][name] |
175
|
1 |
|
if name not in args: |
176
|
|
|
return False |
177
|
1 |
|
if name == 'dl_vlan': |
178
|
1 |
|
field = args[name][-1] |
179
|
|
|
else: |
180
|
1 |
|
field = args[name] |
181
|
1 |
|
if name not in ('ipv4_src', 'ipv4_dst', 'ipv6_src', 'ipv6_dst'): |
182
|
1 |
|
if field_flow != field: |
183
|
|
|
return False |
184
|
|
|
else: |
185
|
|
|
packet_ip = int(ipaddress.ip_address(field)) |
186
|
|
|
ip_addr = flow['flow']['match'][name] |
187
|
|
|
if packet_ip & ip_addr.netmask != ip_addr.address: |
188
|
|
|
return False |
189
|
1 |
|
return flow |
190
|
|
|
|
191
|
1 |
|
def match_flows(self, switch, args, stored_flows, many=True): |
192
|
|
|
# pylint: disable=bad-staticmethod-argument |
193
|
|
|
""" |
194
|
|
|
Match the packet in request against the stored flows from flow_manager. |
195
|
|
|
Try the match with each flow, in other. If many is True, tries the |
196
|
|
|
match with all flows, if False, tries until the first match. |
197
|
|
|
:param args: packet data |
198
|
|
|
:param many: Boolean, indicating whether to continue after matching the |
199
|
|
|
first flow or not |
200
|
|
|
:return: If many, the list of matched flows, or the matched flow |
201
|
|
|
""" |
202
|
1 |
|
response = [] |
203
|
1 |
|
if switch.dpid not in stored_flows: |
204
|
|
|
return None |
205
|
1 |
|
try: |
206
|
1 |
|
for flow in stored_flows[switch.dpid]: |
207
|
1 |
|
match = Main.do_match(flow, args) |
208
|
1 |
|
if match: |
209
|
1 |
|
if many: |
210
|
|
|
response.append(match) |
211
|
|
|
else: |
212
|
1 |
|
response = match |
213
|
1 |
|
break |
214
|
|
|
except AttributeError: |
215
|
|
|
return None |
216
|
1 |
|
if not many and isinstance(response, list): |
217
|
1 |
|
return None |
218
|
1 |
|
return response |
219
|
|
|
|
220
|
|
|
# pylint: disable=redefined-outer-name |
221
|
1 |
|
def match_and_apply(self, switch, args, stored_flows): |
222
|
|
|
# pylint: disable=bad-staticmethod-argument |
223
|
|
|
"""Match flows and apply actions. |
224
|
|
|
Match given packet (in args) against |
225
|
|
|
the stored flows (from flow_manager) and, |
226
|
|
|
if a match flow is found, apply its actions.""" |
227
|
1 |
|
flow = self.match_flows(switch, args, stored_flows, False) |
228
|
1 |
|
port = None |
229
|
1 |
|
actions = None |
230
|
|
|
# pylint: disable=too-many-nested-blocks |
231
|
1 |
|
if not flow or switch.ofp_version != '0x04': |
232
|
1 |
|
return flow, args, port |
233
|
1 |
|
actions = flow['flow']['actions'] |
234
|
1 |
|
for action in actions: |
235
|
1 |
|
action_type = action['action_type'] |
236
|
1 |
|
if action_type == 'output': |
237
|
1 |
|
port = action['port'] |
238
|
1 |
|
if action_type == 'push_vlan': |
239
|
1 |
|
if 'dl_vlan' not in args: |
240
|
|
|
args['dl_vlan'] = [] |
241
|
1 |
|
args['dl_vlan'].append(0) |
242
|
1 |
|
if action_type == 'pop_vlan': |
243
|
1 |
|
if 'dl_vlan' in args: |
244
|
1 |
|
args['dl_vlan'].pop() |
245
|
1 |
|
if len(args['dl_vlan']) == 0: |
246
|
1 |
|
del args['dl_vlan'] |
247
|
1 |
|
if action_type == 'set_vlan': |
248
|
1 |
|
args['dl_vlan'][-1] = action['vlan_id'] |
249
|
|
|
return flow, args, port |
250
|
|
|
|