1
|
|
|
"""Main module of amlight/sdntrace_cp Kytos Network Application. |
2
|
|
|
|
3
|
|
|
Run tracepaths on OpenFlow in the Control Plane |
4
|
|
|
""" |
5
|
|
|
|
6
|
1 |
|
from datetime import datetime |
7
|
|
|
|
8
|
1 |
|
from flask import jsonify, request |
9
|
1 |
|
from kytos.core import KytosNApp, log, rest |
10
|
1 |
|
from kytos.core.helpers import listen_to |
11
|
1 |
|
from napps.amlight.flow_stats.main import Main as FlowManager |
12
|
1 |
|
from napps.amlight.sdntrace_cp import settings |
13
|
1 |
|
from napps.amlight.sdntrace_cp.automate import Automate |
14
|
1 |
|
from napps.amlight.sdntrace_cp.utils import (convert_entries, find_endpoint, |
15
|
|
|
prepare_json) |
16
|
|
|
|
17
|
|
|
|
18
|
1 |
|
class Main(KytosNApp): |
19
|
|
|
"""Main class of amlight/sdntrace_cp NApp. |
20
|
|
|
|
21
|
|
|
This application gets the list of flows from the switches |
22
|
|
|
and uses it to trace paths without using the data plane. |
23
|
|
|
""" |
24
|
|
|
|
25
|
1 |
|
def setup(self): |
26
|
|
|
"""Replace the '__init__' method for the KytosNApp subclass. |
27
|
|
|
|
28
|
|
|
The setup method is automatically called by the controller when your |
29
|
|
|
application is loaded. |
30
|
|
|
|
31
|
|
|
""" |
32
|
1 |
|
log.info("Starting Kytos SDNTrace CP App!") |
33
|
|
|
|
34
|
1 |
|
self.traces = {} |
35
|
1 |
|
self.last_id = 30000 |
36
|
1 |
|
self.automate = Automate(self) |
37
|
1 |
|
self.automate.schedule_traces() |
38
|
1 |
|
self.automate.schedule_important_traces() |
39
|
|
|
|
40
|
1 |
|
def execute(self): |
41
|
|
|
"""This method is executed right after the setup method execution. |
42
|
|
|
|
43
|
|
|
You can also use this method in loop mode if you add to the above setup |
44
|
|
|
method a line like the following example: |
45
|
|
|
|
46
|
|
|
self.execute_as_loop(30) # 30-second interval. |
47
|
|
|
""" |
48
|
|
|
|
49
|
1 |
|
def shutdown(self): |
50
|
|
|
"""This method is executed when your napp is unloaded. |
51
|
|
|
|
52
|
|
|
If you have some cleanup procedure, insert it here. |
53
|
|
|
""" |
54
|
|
|
self.automate.unschedule_ids() |
55
|
|
|
self.automate.sheduler_shutdown(wait=False) |
56
|
|
|
|
57
|
1 |
|
@rest('/trace', methods=['PUT']) |
58
|
1 |
|
def trace(self): |
59
|
|
|
"""Trace a path.""" |
60
|
1 |
|
entries = request.get_json() |
61
|
1 |
|
result = self.tracepath(entries) |
62
|
1 |
|
return jsonify(prepare_json(result)) |
63
|
|
|
|
64
|
1 |
|
def tracepath(self, entries): |
65
|
|
|
"""Trace a path for a packet represented by entries.""" |
66
|
1 |
|
self.last_id += 1 |
67
|
1 |
|
trace_id = self.last_id |
68
|
1 |
|
entries = convert_entries(entries) |
69
|
1 |
|
trace_result = [] |
70
|
1 |
|
trace_type = 'starting' |
71
|
|
|
|
72
|
1 |
|
do_trace = True |
73
|
1 |
|
while do_trace: |
74
|
1 |
|
trace_step = {'in': {'dpid': entries['dpid'], |
75
|
|
|
'port': entries['in_port'], |
76
|
|
|
'time': str(datetime.now()), |
77
|
|
|
'type': trace_type}} |
78
|
1 |
|
if 'vlan_vid' in entries: |
79
|
1 |
|
trace_step['in'].update({'vlan': entries['vlan_vid'][-1]}) |
80
|
1 |
|
switch = self.controller.get_switch_by_dpid(entries['dpid']) |
81
|
1 |
|
result = self.trace_step(switch, entries) |
82
|
1 |
|
if result: |
83
|
1 |
|
out = {'port': result['out_port']} |
84
|
1 |
|
if 'vlan_vid' in result['entries']: |
85
|
|
|
out.update({'vlan': result['entries']['vlan_vid'][-1]}) |
86
|
1 |
|
trace_step.update({ |
87
|
|
|
'out': out |
88
|
|
|
}) |
89
|
1 |
|
if 'dpid' in result: |
90
|
1 |
|
next_step = {'dpid': result['dpid'], |
91
|
|
|
'port': result['in_port']} |
92
|
1 |
|
if self.has_loop(next_step, trace_result): |
93
|
1 |
|
do_trace = False |
94
|
|
|
else: |
95
|
1 |
|
entries = result['entries'] |
96
|
1 |
|
entries['dpid'] = result['dpid'] |
97
|
1 |
|
entries['in_port'] = result['in_port'] |
98
|
1 |
|
trace_type = 'trace' |
99
|
|
|
else: |
100
|
|
|
do_trace = False |
101
|
|
|
else: |
102
|
1 |
|
do_trace = False |
103
|
1 |
|
trace_result.append(trace_step) |
104
|
1 |
|
self.traces.update({ |
105
|
|
|
trace_id: trace_result |
106
|
|
|
}) |
107
|
1 |
|
return trace_result |
108
|
|
|
|
109
|
1 |
|
@staticmethod |
110
|
1 |
|
def has_loop(trace_step, trace_result): |
111
|
|
|
"""Check if there is a loop in the trace result.""" |
112
|
1 |
|
for trace in trace_result: |
113
|
1 |
|
if trace['in']['dpid'] == trace_step['dpid'] and \ |
114
|
|
|
trace['in']['port'] == trace_step['port']: |
115
|
1 |
|
return True |
116
|
1 |
|
return False |
117
|
|
|
|
118
|
1 |
|
@staticmethod |
119
|
1 |
|
def trace_step(switch, entries): |
120
|
|
|
"""Perform a trace step. |
121
|
|
|
|
122
|
|
|
Match the given fields against the switch's list of flows.""" |
123
|
1 |
|
flow, entries, port = FlowManager.match_and_apply(switch, entries) |
124
|
1 |
|
if not flow or not port: |
125
|
1 |
|
return None |
126
|
|
|
|
127
|
1 |
|
endpoint = find_endpoint(switch, port) |
128
|
1 |
|
if endpoint is None: |
129
|
1 |
|
return {'out_port': port, |
130
|
|
|
'entries': entries} |
131
|
|
|
|
132
|
1 |
|
return {'dpid': endpoint.switch.dpid, |
133
|
|
|
'in_port': endpoint.port_number, |
134
|
|
|
'out_port': port, |
135
|
|
|
'entries': entries} |
136
|
|
|
|
137
|
1 |
|
@listen_to('amlight/flow_stats.flows_updated') |
138
|
1 |
|
def update_circuits(self, event): |
139
|
|
|
"""Update the list of circuits after a flow change.""" |
140
|
|
|
# pylint: disable=unused-argument |
141
|
1 |
|
if settings.FIND_CIRCUITS_IN_FLOWS: |
142
|
|
|
self.automate.find_circuits() |
143
|
|
|
|