-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsar_application.py
514 lines (432 loc) · 21.5 KB
/
sar_application.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller import dpset
from ryu.controller import ofp_event
from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER
from ryu.app.wsgi import ControllerBase, WSGIApplication, route
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_v1_3
from ryu.lib.packet import packet
from ryu.lib.packet import ethernet
from ryu.lib.packet import ether_types
from ryu.topology.api import get_switch, get_link, get_host, get_all_host
from ryu.topology import event, switches
import networkx as nx
import json
import logging
import struct
from webob import Response
from ryu.lib.mac import haddr_to_bin
from ryu.lib.packet.packet import Packet
from ryu.lib.packet import arp
from ryu.lib.packet import ipv4
from ryu.lib.packet import tcp
from ryu.lib.packet import udp
from ryu.ofproto import ether
from ryu.app.ofctl.api import get_datapath
from timeit import default_timer
import BinaryNode
import MultibitNode
import CompressedNode
# Packet Classification parameters
SRC_IP = 0
DST_IP = 1
PROTO = 2
SPORT = 3
DPORT = 4
ACTION = 5
TRIE = 1 # choosing the type of trie to use: 0 for Binary, 1 for Multibit, 2 for Compressed
# IP lookup parameters
IP = 0
SUBNET = 1
DPID = 2
# Topologies
TOPO = 2
class SimpleSwitch13(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
_CONTEXTS = {'wsgi': WSGIApplication}
def __init__(self, *args, **kwargs):
super(SimpleSwitch13, self).__init__(*args, **kwargs)
wsgi = kwargs['wsgi']
self.topology_api_app = self
self.net = nx.DiGraph()
self.nodes = {}
self.links = {}
self.no_of_nodes = 0
self.no_of_links = 0
self.datapaths = []
self.switch_id = []
self.mac_to_port = {}
self.mac_to_dpid = {}
self.port_to_mac = {}
self.i = 0
self.trie_root = None
# Packet Classification initial parameters
self.classify = {}
self.classify["r1"] = ["195.0.0.1", "128.128.0.1", "6", "*", "1234", "allow"]
self.classify["r2"] = ["128.128.0.1", "195.0.0.1", "6", "1234", "*", "allow"]
self.classify["r3"] = ["195.0.0.1", "128.128.0.1", "1", "*", "*", "allow"]
self.classify["r4"] = ["128.128.0.1", "195.0.0.1", "1", "*", "*", "allow"]
self.classify["r5"] = ["*", "*", "*", "*", "*", "deny"]
self.counters = {}
self.counters["r1"] = 0
self.counters["r2"] = 0
self.counters["r3"] = 0
self.counters["r4"] = 0
self.counters["r5"] = 0
if TOPO == 1:
self.switch = {}
self.switch["195.0.0.254"] = ["195.0.0.254", "8", "1"]
self.switch["128.128.0.254"] = ["128.128.0.254", "12", "2"]
self.switch["154.128.0.254"] = ["154.128.0.254", "16", "3"]
self.lookup = {}
self.lookup["195.0.0.1"] = "195.0.0.254"
self.lookup["195.0.0.2"] = "195.0.0.254"
self.lookup["128.128.0.1"] = "128.128.0.254"
self.lookup["128.128.0.2"] = "128.128.0.254"
self.lookup["154.128.0.1"] = "154.128.0.254"
self.lookup["154.128.0.2"] = "154.128.0.254"
self.ip_to_mac = {}
self.ip_to_mac["195.0.0.1"] = "00:00:00:00:00:01"
self.ip_to_mac["195.0.0.2"] = "00:00:00:00:00:02"
self.ip_to_mac["128.128.0.1"] = "00:00:00:00:00:03"
self.ip_to_mac["128.128.0.2"] = "00:00:00:00:00:04"
self.ip_to_mac["154.128.0.1"] = "00:00:00:00:00:05"
self.ip_to_mac["154.128.0.2"] = "00:00:00:00:00:06"
elif TOPO == 2:
self.switch = {}
self.switch["195.0.0.254"] = ["195.0.0.254", "8", "1"]
self.switch["128.128.0.254"] = ["128.128.0.254", "12", "2"]
self.switch["154.128.0.254"] = ["154.128.0.254", "16", "3"]
self.switch["197.160.0.254"] = ["197.160.0.254", "24", "4"]
self.switch["192.168.0.254"] = ["192.168.0.254", "24", "5"]
self.switch["192.169.0.254"] = ["192.169.0.254", "24", "6"]
self.switch["192.170.0.254"] = ["192.170.0.254", "24", "7"]
self.lookup = {}
self.lookup["195.0.0.1"] = "195.0.0.254"
self.lookup["195.0.0.2"] = "195.0.0.254"
self.lookup["128.128.0.1"] = "128.128.0.254"
self.lookup["154.128.0.1"] = "154.128.0.254"
self.lookup["197.160.0.1"] = "197.160.0.254"
self.lookup["192.168.0.1"] = "192.168.0.254"
self.lookup["192.169.0.1"] = "192.169.0.254"
self.lookup["192.170.0.1"] = "192.170.0.254"
self.ip_to_mac = {}
self.ip_to_mac["195.0.0.1"] = "00:00:00:00:00:01"
self.ip_to_mac["195.0.0.2"] = "00:00:00:00:00:02"
self.ip_to_mac["128.128.0.1"] = "00:00:00:00:00:03"
self.ip_to_mac["154.128.0.1"] = "00:00:00:00:00:04"
self.ip_to_mac["197.160.0.1"] = "00:00:00:00:00:05"
self.ip_to_mac["192.168.0.1"] = "00:00:00:00:00:06"
self.ip_to_mac["192.169.0.1"] = "00:00:00:00:00:07"
self.ip_to_mac["192.170.0.1"] = "00:00:00:00:00:08"
if TRIE == 0: # Binary Trie
self.trie_root = BinaryNode.BinaryNode('0')
for key, value in self.switch.iteritems():
ip = value[0]
mask = int(value[1])
binary_address = BinaryNode.convert_in_bin(key)[:mask] # take only the network part of address
self.trie_root.AddChild(ip, binary_address)
self.logger.info("BinaryTrie created")
elif TRIE == 1: # Multibit Trie
self.trie_root = MultibitNode.MultibitNode()
for entry in self.order_switch(): # order_switch is used to have and ordinated list of ips base on mask length
ip, mask = entry
binary_address = MultibitNode.convert_in_bin(ip)[:mask] # take only the network part of address
self.trie_root.AddChild(ip, binary_address)
self.logger.info("MultibitTrie created")
elif TRIE == 2: # Compressed Trie
self.trie_root = CompressedNode.CompressedNode("0")
for key, value in self.switch.iteritems():
ip = value[0]
mask = int(value[1])
binary_address = CompressedNode.convert_in_bin(key)[:mask] # take only the network part of address
self.trie_root.AddChild(ip, binary_address)
self.logger.info("CompressedTrie created")
def order_switch(self):
'''
helper method to build a sorted list of tuples
:return: list of tuples (ip, mask) sorted by mask length
'''
tuples = []
for key, value in self.switch.iteritems():
tuples.append((value[0], value[1])) # append ip, mask
return sorted(tuples, key=lambda x: x[1])
# END OF CUSTOM CODE
def ls(self, obj):
print("\n".join([x for x in dir(obj) if x[0] != "_"]))
def send_arp(self, datapath, opcode, srcMac, srcIp, dstMac, dstIp, outPort):
if opcode == 1:
targetMac = "00:00:00:00:00:00"
targetIp = dstIp
elif opcode == 2:
targetMac = dstMac
targetIp = dstIp
e = ethernet.ethernet(dstMac, srcMac, ether.ETH_TYPE_ARP)
a = arp.arp(1, 0x0800, 6, 4, opcode, srcMac, srcIp, targetMac, targetIp)
p = Packet()
p.add_protocol(e)
p.add_protocol(a)
p.serialize()
actions = [datapath.ofproto_parser.OFPActionOutput(outPort, 0)]
out = datapath.ofproto_parser.OFPPacketOut(
datapath=datapath,
buffer_id=0xffffffff,
in_port=datapath.ofproto.OFPP_CONTROLLER,
actions=actions,
data=p.data)
datapath.send_msg(out)
@set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
def switch_features_handler(self, ev):
datapath = ev.msg.datapath
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
msg = ev.msg
self.datapaths.append(msg.datapath)
self.switch_id.append(msg.datapath_id)
match = parser.OFPMatch()
actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
ofproto.OFPCML_NO_BUFFER)]
self.add_flow(datapath, 0, match, actions)
def add_flow(self, datapath, priority, match, actions, buffer_id=None):
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
actions)]
if buffer_id:
mod = parser.OFPFlowMod(datapath=datapath, buffer_id=buffer_id,
priority=priority, match=match,
instructions=inst)
else:
mod = parser.OFPFlowMod(datapath=datapath, priority=priority,
match=match, instructions=inst)
datapath.send_msg(mod)
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def _packet_in_handler(self, ev):
if ev.msg.msg_len < ev.msg.total_len:
self.logger.debug("packet truncated: only %s of %s bytes",
ev.msg.msg_len, ev.msg.total_len)
msg = ev.msg
datapath = msg.datapath
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
in_port = msg.match['in_port']
pkt = packet.Packet(msg.data)
eth = pkt.get_protocols(ethernet.ethernet)[0]
if eth.ethertype == ether_types.ETH_TYPE_LLDP:
# ignore lldp packet
return
dst = eth.dst
src = eth.src
dpid_src = datapath.id
# TOPOLOGY DISCOVERY------------------------------------------
switch_list = get_switch(self.topology_api_app, None)
switches = [switch.dp.id for switch in switch_list]
self.net.add_nodes_from(switches)
links_list = get_link(self.topology_api_app, None)
links = [(link.src.dpid, link.dst.dpid, {'port': link.src.port_no}) for link in links_list]
self.net.add_edges_from(links)
links = [(link.dst.dpid, link.src.dpid, {'port': link.dst.port_no}) for link in links_list]
self.net.add_edges_from(links)
# print links
# MAC LEARNING-------------------------------------------------
self.mac_to_port.setdefault(dpid_src, {})
self.mac_to_port.setdefault(src, {})
self.port_to_mac.setdefault(dpid_src, {})
self.mac_to_port[dpid_src][src] = in_port
self.mac_to_dpid[src] = dpid_src
self.port_to_mac[dpid_src][in_port] = src
self.logger.info("Packet in the controller from switch: %s", dpid_src)
# print self.mac_to_port
# HANDLE ARP PACKETS--------------------------------------------
if eth.ethertype == ether_types.ETH_TYPE_ARP:
arp_packet = pkt.get_protocol(arp.arp)
arp_dst_ip = arp_packet.dst_ip
arp_src_ip = arp_packet.src_ip
# self.logger.info("ARP packet from switch: %s source IP: %s destination IP: %s from port: %s", dpid_src, arp_src_ip, arp_dst_ip, in_port)
# self.logger.info("ARP packet from switch: %s source MAC: %s destination MAC:%s from port: %s", dpid_src, src, dst, in_port)
if arp_dst_ip in self.ip_to_mac:
if arp_packet.opcode == 1:
# send arp reply (SAME SUBNET)
dstIp = arp_src_ip
srcIp = arp_dst_ip
dstMac = src
srcMac = self.ip_to_mac[arp_dst_ip]
outPort = in_port
opcode = 2 # arp reply packet
self.send_arp(datapath, opcode, srcMac, srcIp, dstMac, dstIp, outPort)
else:
if arp_packet.opcode == 1:
# send arp reply (GATEWAY)
dstIp = arp_src_ip
srcIp = arp_dst_ip
dstMac = src
srcMac = self.port_to_mac[dpid_src][in_port]
outPort = in_port
opcode = 2 # arp reply packet
self.send_arp(datapath, opcode, srcMac, srcIp, dstMac, dstIp, outPort)
# HANDLE IP PACKETS-----------------------------------------------
ip4_pkt = pkt.get_protocol(ipv4.ipv4)
if ip4_pkt:
src_ip = ip4_pkt.src
dst_ip = ip4_pkt.dst
proto = str(ip4_pkt.proto)
sport = "0"
dport = "0"
if proto == "6":
tcp_pkt = pkt.get_protocol(tcp.tcp)
sport = str(tcp_pkt.src_port)
dport = str(tcp_pkt.dst_port)
if proto == "17":
udp_pkt = pkt.get_protocol(udp.udp)
sport = str(udp_pkt.src_port)
dport = str(udp_pkt.dst_port)
self.logger.info("Packet from the switch: %s, source IP: %s, destination IP: %s, From the port: %s",
dpid_src, src_ip, dst_ip, in_port)
# PACKET CLASSIFICATION FUNCTION: it returns action: "allow" or "deny"
# action_rule = self.linear_classification(src_ip, dst_ip, proto, sport, dport)
action_rule = "allow"
if action_rule == "allow":
# IP LOOKUP FUNCTION: it is zero if it didn't find a solution
# destination_switch_IP = self.linear_search(dst_ip)
destination_switch_IP = self.binary_search(dst_ip)
if destination_switch_IP != "0":
datapath_dst = get_datapath(self, int(self.switch[destination_switch_IP][DPID]))
dpid_dst = datapath_dst.id
self.logger.info(" --- Destination present on switch: %s", dpid_dst)
# Shortest path computation
path = nx.shortest_path(self.net, dpid_src, dpid_dst)
self.logger.info(" --- Shortest path: %s", path)
if len(path) == 1:
In_Port = self.mac_to_port[dpid_src][src]
Out_Port = self.mac_to_port[dpid_dst][dst]
actions_1 = [datapath.ofproto_parser.OFPActionOutput(Out_Port)]
actions_2 = [datapath.ofproto_parser.OFPActionOutput(In_Port)]
match_1 = parser.OFPMatch(in_port=In_Port, eth_dst=dst)
self.add_flow(datapath, 1, match_1, actions_1)
actions = [datapath.ofproto_parser.OFPActionOutput(Out_Port)]
data = msg.data
pkt = packet.Packet(data)
eth = pkt.get_protocols(ethernet.ethernet)[0]
# self.logger.info(" --- Changing destination mac to %s" % (eth.dst))
pkt.serialize()
out = datapath.ofproto_parser.OFPPacketOut(
datapath=datapath, buffer_id=0xffffffff, in_port=datapath.ofproto.OFPP_CONTROLLER,
actions=actions, data=pkt.data)
datapath.send_msg(out)
elif len(path) == 2:
path_port = self.net[path[0]][path[1]]['port']
actions = [datapath.ofproto_parser.OFPActionOutput(path_port)]
data = msg.data
pkt = packet.Packet(data)
eth = pkt.get_protocols(ethernet.ethernet)[0]
eth.src = self.ip_to_mac[src_ip]
eth.dst = self.ip_to_mac[dst_ip]
# self.logger.info(" --- Changing destination mac to %s" % (eth.dst))
pkt.serialize()
out = datapath.ofproto_parser.OFPPacketOut(
datapath=datapath, buffer_id=0xffffffff, in_port=datapath.ofproto.OFPP_CONTROLLER,
actions=actions, data=pkt.data)
datapath.send_msg(out)
elif len(path) > 2:
# Add flows in the middle of the network path
for i in range(1, len(path) - 1):
In_Port = self.net[path[i]][path[i - 1]]['port']
Out_Port = self.net[path[i]][path[i + 1]]['port']
dp = get_datapath(self, path[i])
# self.logger.info("Matched OpenFlow Rule = switch: %s, from in port: %s, to out port: %s, source IP: %s, and destination IP: %s", path[i], In_Port, Out_Port, src_ip, dst_ip)
actions_1 = [dp.ofproto_parser.OFPActionOutput(Out_Port)]
match_1 = parser.OFPMatch(in_port=In_Port, eth_type=0x0800, ipv4_src=src_ip,
ipv4_dst=dst_ip)
self.add_flow(dp, 1, match_1, actions_1)
path_port = self.net[path[0]][path[1]]['port']
actions = [datapath.ofproto_parser.OFPActionOutput(path_port)]
data = msg.data
pkt = packet.Packet(data)
eth = pkt.get_protocols(ethernet.ethernet)[0]
# change the mac address of packet
eth.src = self.ip_to_mac[src_ip]
eth.dst = self.ip_to_mac[dst_ip]
# self.logger.info(" --- Changing destination mac to %s" % (eth.dst))
pkt.serialize()
out = datapath.ofproto_parser.OFPPacketOut(
datapath=datapath, buffer_id=0xffffffff, in_port=datapath.ofproto.OFPP_CONTROLLER,
actions=actions, data=pkt.data)
datapath.send_msg(out)
@set_ev_cls(event.EventSwitchEnter)
def get_topology_data(self, ev):
switch_list = get_switch(self.topology_api_app, None)
switches = [switch.dp.id for switch in switch_list]
self.net.add_nodes_from(switches)
links_list = get_link(self.topology_api_app, None)
links = [(link.src.dpid, link.dst.dpid, {'port': link.src.port_no}) for link in links_list]
self.net.add_edges_from(links)
links = [(link.dst.dpid, link.src.dpid, {'port': link.dst.port_no}) for link in links_list]
self.net.add_edges_from(links)
# print "**********List of links"
# print self.net.edges()
# for link in links_list:
# print link.dst
# print link.src
# print "Novo link"
# self.no_of_links += 1
# -------------------------------------------------------------------------------------------------------
# CUSTOM CODE
def binary_search(self, dst_ip):
destination_switch_ip = ''
if TRIE == 0:
self.logger.info(" --- IP Lookup with Binary Trie")
binary_dst = BinaryNode.convert_in_bin(dst_ip)
start = default_timer()
destination_switch_ip = self.trie_root.Lookup(binary_dst)
end = default_timer() - start
self.logger.info("Binary trie returned: " + str(destination_switch_ip))
self.logger.info("time elapsed: " + str(end*1000) + "ms")
elif TRIE == 1:
self.logger.info(" --- IP Lookup with Multibit Trie")
binary_dst = MultibitNode.convert_in_bin(dst_ip)
start = default_timer()
destination_switch_ip = self.trie_root.Lookup(binary_dst, "0")
end = default_timer() - start
self.logger.info("Multibit trie returned: " + str(destination_switch_ip))
self.logger.info("time elapsed: " + str(end*1000) + "ms")
elif TRIE == 2:
self.logger.info(" --- IP Lookup with Compressed Trie")
binary_dst = CompressedNode.convert_in_bin(dst_ip)
start = default_timer()
destination_switch_ip = self.trie_root.Lookup(binary_dst)
end = default_timer() - start
self.logger.info("compressed trie returned: " + str(destination_switch_ip))
self.logger.info("time elapsed: " + str(end*1000) + "ms")
if destination_switch_ip != '':
return destination_switch_ip
else:
return "0"
# END OF CUSTOM CODE
def linear_search(self, dst_ip):
self.logger.info(" --- IP address Lookup")
if dst_ip in self.lookup:
destination_switch_IP = self.lookup[dst_ip]
return destination_switch_IP
else:
destination_switch_IP = "0"
return destination_switch_IP
def linear_classification(self, src_ip, dst_ip, proto, sport, dport):
action = "deny"
self.logger.info(" --- Packet classification")
# check matching rule
for rule in sorted(self.classify):
match = self.classify[rule]
if (match[SRC_IP] == src_ip or match[SRC_IP] == "*") and \
(match[DST_IP] == dst_ip or match[DST_IP] == "*") and \
(match[PROTO] == proto or match[PROTO] == "*") and \
(match[SPORT] == sport or match[SPORT] == "*") and \
(match[DPORT] == dport or match[DPORT] == "*"):
self.logger.info(" --- Packet matched rule %s. Action is %s" % (rule, match[ACTION]))
action = match[ACTION]
self.counters[rule] = self.counters[rule] + 1
return action
return action
app_manager.require_app('ryu.app.ws_topology')
app_manager.require_app('ryu.app.ofctl_rest')
app_manager.require_app('ryu.app.gui_topology.gui_topology')