-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdntrace.py
399 lines (353 loc) · 13.7 KB
/
sdntrace.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
from ryu import utils
from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import DEAD_DISPATCHER
from ryu.controller.handler import MAIN_DISPATCHER, CONFIG_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.lib import hub
from ryu.ofproto import ofproto_v1_0
from ryu.ofproto.ofproto_v1_0_parser import OFPPhyPort
from Coloring import prepare, topology
from Exceptions import brocade
from Tracing import trace_pkt, tracing
class OFSwitch:
"""
Used to keep track of each node
This object is used in the SDNTrace.node_list
"""
def __init__(self, ev):
self.obj = ev
self.dpid = ev.msg.datapath_id
self.ports = self._extract_ports()
# To be used for coloring
self.adjacencies_list = []
self.color = "0"
self.old_color = "0"
self.name = self.datapath_id
# To avoid issues when deleting flows
global MINIMUM_COOKIE_ID
self.cookie = MINIMUM_COOKIE_ID + 1
MINIMUM_COOKIE_ID += 1
@property
def datapath_id(self):
"""
Property to print datapath_id in the hex format
Returns:
dpid in hex
"""
return '%016x' % self.dpid
def _extract_ports(self):
"""
Extract ports from FeatureReply message
Ports are added to the self.ports
Returns:
list of ports found for a specific node
"""
num_ports = len(self.obj.msg.ports)
ofproto = self.obj.msg.datapath.ofproto
offset = ofproto.OFP_SWITCH_FEATURES_SIZE
port_list = []
for _i in range(num_ports):
if _i < ofproto.OFPP_MAX:
port = OFPPhyPort.parser(self.obj.msg.buf, offset)
if port.port_no < 65280: # Special ports are 65280+
port_list.append(port.port_no)
offset += ofproto.OFP_PHY_PORT_SIZE
return port_list
# These will become part of a configuration file
MINIMUM_COOKIE_ID = 2000000
PACKET_OUT_INTERVAL = 5
PUSH_COLORS_INTERVAL = 10
COLLECT_INTERVAL = 30
HAS_OFPP_TABLE_SUPPORT = True
VLAN_DISCOVERY = 100
FLOW_PRIORITY = 50000
# TODO Print 'System Ready' once the topology was discovery
# TODO Do not accept any request before that
class SDNTrace(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]
def __init__(self, *args, **kwargs):
super(SDNTrace, self).__init__(*args, **kwargs)
self.node_list = []
self.links = []
self.colors = []
self.old_colors = []
# Threads
self.topo_disc = hub.spawn(self._topology_discovery)
self.push_colors = hub.spawn(self._push_colors)
# self.collect_stats = hub.spawn(self._collect_stats)
# Trace
# self.active_traces = [] # list of Probes class
self.trace_pktIn = [] # list of received PacketIn not LLDP
self.print_ready = False # Just to print System Ready once
def _topology_discovery(self):
"""
Keeps looping node_list every PACKET_OUT_INTERVAL seconds
Send a packet_out w/ LLDP to every port found
Args:
self
"""
while True:
for node in self.node_list:
for port in node.ports:
pkt = topology.prepare_lldp_packet(node, port, VLAN_DISCOVERY)
self.send_packet_out(node, port, pkt.data, True)
hub.sleep(PACKET_OUT_INTERVAL)
def _push_colors(self):
"""
This routine will run each PUSH_COLORS_INTERVAL interval
and process the self.links to associate colors to OFSwitches.
Flows will be pushed to the switch with the VLAN_PCP field set
to the defined color outputing to controller
Args:
self
"""
while True:
if len(self.node_list) > 1:
if len(self.links) is not 0:
self.get_topology_data()
hub.sleep(PUSH_COLORS_INTERVAL)
# def _collect_stats(self):
# """
# This method will send FLOW_STAT_REQ each
# COLLECT_INTERVAL interval
# Args:
# self
# """
# while True:
# for node in self.node_list:
# brocade.send_stat_req(self, node)
# hub.sleep(COLLECT_INTERVAL)
@staticmethod
def send_packet_out(node, port, data, lldp=False):
"""
Sends PacketOut - Serializes Traces and LLDP packets for topology discovery
Args:
node: node to send PacketOut
port: if LLDP: port to send PacketOut out. if Trace: in_port field
data: Ethernet frame to be send
lldp: used in case of LLDP packets (used by _topology_discovery)
"""
parser = node.obj.msg.datapath.ofproto_parser
datapath = node.obj.msg.datapath
ofproto = datapath.ofproto
if lldp:
in_port = ofproto.OFPP_NONE
out_port = port
else:
in_port = port
out_port = ofproto.OFPP_TABLE
actions = [parser.OFPActionOutput(out_port)]
buffer_id = ofproto.OFP_NO_BUFFER
out = parser.OFPPacketOut(datapath=datapath, in_port=in_port,
buffer_id=buffer_id, actions=actions,
data=data)
datapath.send_msg(out)
@set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
def switch_features_handler(self, ev):
"""
FeatureReply - For each new switch that connects, add to the
node_list array. This array will be used for sending packetOut
and generate topology and colors
Args:
ev: event triggered
"""
self.node_list.append(OFSwitch(ev))
self.add_default_flow(ev)
def add_default_flow(self, ev):
"""
Push default flow
Args:
ev: event triggered
"""
vlan = VLAN_DISCOVERY
datapath, match, actions = topology.prepare_default_flow(self, ev, vlan)
ofproto = datapath.ofproto
self.push_flow(datapath, 0, 0, ofproto.OFPFC_ADD, match, actions)
@set_ev_cls(ofp_event.EventOFPPortStatus, MAIN_DISPATCHER)
def port_status(self, ev):
"""
Process OFP_Port_Status
Add or Remove ports from OFSwitch.ports
Args:
ev: packet captured
"""
topology.process_port_status(self, ev)
@set_ev_cls(ofp_event.EventOFPStateChange, DEAD_DISPATCHER)
def remove_switch(self, ev):
"""
If DEAD_DISPATCHER received, remove switch from self.node_list
Args:
ev: packet captured
"""
topology.remove_switch(self, ev)
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def packet_in_handler(self, ev):
"""
Process PacketIn
PacketIN messages are used for topology discovery and traces
Args:
ev: event triggered
"""
action, result = topology.process_packetIn(self, ev, self.links)
if action is 0:
# NO_MATCH, ignore
pass
elif action is 1:
self.links = result
elif action is 2:
# To be finished.
pkt = tracing.process_probe_packet(ev, result)
if pkt is not False:
self.trace_pktIn.append(pkt)
# @set_ev_cls(ofp_event.EventOFPFlowStatsReply, MAIN_DISPATCHER)
# def flow_stats_reply(self, ev):
# """
# Process Flow Stats
# Args:
# ev: packet captured
# """
# brocade.flow_stats_reply(ev)
@set_ev_cls(ofp_event.EventOFPErrorMsg, MAIN_DISPATCHER)
def openflow_error(self, ev):
"""
Print Error Received. Useful for troubleshooting, specially with Brocade
"""
msg = ev.msg
print ('OFPErrorMsg received: type=0x%02x code=0x%02x message=%s' %
(msg.type, msg.code, utils.hex_array(msg.data)))
def get_topology_data(self):
"""
First define colors for each node
Then push flows
Args:
self
"""
# TODO: if it is the first time, make sure the flows were not installed \
# TODO: in the previous executions. Delete all legacy flows (wildcard 0x2fffef)
colors = prepare.define_color(self, self.links)
# print 'colors'
# print colors
# print 'self.colors'
# print self.colors
# print 'self.old_colors'
# print self.old_colors
# Compare received colors with self.old_colors
# If the same, ignore
if colors is not None:
self.colors = colors
if len(self.old_colors) is 0:
self.old_colors = self.colors
else:
if self.colors == self.old_colors:
if not self.print_ready:
self.print_ready = True
print 'System Ready!'
return
# Check all colors in use
# For each switch:
# 1 - Delete colored flows
# 2 - Check colors of all other switches
# 3 - Install all colors from other switches
for node in self.node_list:
self.delete_colored_flows(node)
neighbor_colors = []
for color in self.colors:
# Get Dict Key. Just one Key
for key in color:
if key != node.name:
idx = prepare.get_node_from_name(self, key)
if self.node_list[idx] in node.adjacencies_list:
neighbor_colors.append(color[key])
# in some cases, if two neighbors have the same color, the same flow
# will be installed twice. It is not an issue.
for color in neighbor_colors:
self.install_color(node, color)
del neighbor_colors
node.old_color = node.color
self.old_colors = self.colors
def delete_colored_flows(self, node):
"""
Remove old colored flows from the node
TODO: add cookies to the filter
Args:
node: node to be removed
"""
# Test this method!!!!!
datapath = node.obj.msg.datapath
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
op = ofproto.OFPP_CONTROLLER
actions = [datapath.ofproto_parser.OFPActionOutput(op)]
color = int(node.old_color)
if color is 0:
# It means no color was previously associated to this node
return
match = parser.OFPMatch(dl_type=0x8100, dl_vlan_pcp=color)
cookie = node.cookie
flags = 0
self.push_flow(datapath, cookie, FLOW_PRIORITY, ofproto.OFPFC_DELETE_STRICT,
match, actions, flags)
return
def install_color(self, node, color):
"""
Prepare to send the FlowMod to install the colored flow
Args:
node: datapath
color: VLAN_PCP to be used
"""
datapath = node.obj.msg.datapath
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
op = ofproto.OFPP_CONTROLLER
actions = [datapath.ofproto_parser.OFPActionOutput(op)]
color = int(color)
# VLANS CAN NOT BE DEFINED OR IT WILL HAVE TO BE DEFINED ON DEMAND?? Brocade CES
# match = parser.OFPMatch(dl_type=0x8100, dl_vlan=100, dl_vlan_pcp=color)
match = parser.OFPMatch(dl_type=0x800, dl_vlan_pcp=color)
cookie = node.cookie
self.push_flow(datapath, cookie, FLOW_PRIORITY, ofproto.OFPFC_ADD, match, actions)
@staticmethod
def push_flow(datapath, cookie, priority, command, match, actions, flags=1):
"""
Send the FlowMod to datapath. Send BarrierReq after to confirm
Args:
datapath: switch class
cookie: cookie to be used on the flow
priority: flow priority
command: action (Add, Delete, modify)
match: flow match
actions: flow action
"""
if flags is not 0:
flags = datapath.ofproto.OFPFF_SEND_FLOW_REM
parser = datapath.ofproto_parser
mod = parser.OFPFlowMod(datapath=datapath, match=match, cookie=cookie,
out_port=datapath.ofproto.OFPP_CONTROLLER,
flags=flags,
command=command, priority=priority,
actions=actions)
# DEBUG:
# print mod
datapath.send_msg(mod)
datapath.send_barrier()
def process_trace_req(self, entries, r_id):
"""
Receives the REST/PUT to generate a PacketOut
template_trace.json is an example
Args:
entries: entries provided by user received from REST interface
r_id: request ID created by sdntraceRest and sent back to user
"""
# print 'process_trace_req'
dpid = entries['trace']['switch']['dpid']
node, color = trace_pkt.get_node_from_dpid(self.node_list, dpid)
if not node or not color:
print 'WARN: System Not Ready Yet'
return 0
if HAS_OFPP_TABLE_SUPPORT is True:
# create a thread to handle this request
return tracing.handle_trace(self, entries, node, color, r_id)
else:
# Find a solution for Brocade
return brocade.send_trace_probes(self, entries, node, color, r_id)