-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqos_route_app.py
305 lines (259 loc) · 10.8 KB
/
qos_route_app.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
# conding=utf-8
import logging
import random
import struct
import time
from collections import defaultdict
import networkx as nx
from operator import attrgetter
from ryu import cfg
from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import MAIN_DISPATCHER, DEAD_DISPATCHER
from ryu.controller.handler import CONFIG_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_v1_3
from ryu.lib import hub
from ryu.lib.packet import packet
from ryu.lib.packet import ethernet
from ryu.lib.packet import ipv4, ipv6
from ryu.lib.packet import arp
from ryu.topology import event, switches
from ryu.topology.api import get_switch, get_link
import network_awareness
import network_monitor
import network_delay_detector
import network_route_detector
import settings
CONF = cfg.CONF
class QoSRouteApp(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
_CONTEXTS = {
"network_awareness": network_awareness.NetworkAwareness,
"network_route_detector": network_route_detector.NetworkRouteDetector,
"network_monitor": network_monitor.NetworkMonitor,
"network_delay_detector": network_delay_detector.NetworkDelayDetector,
}
WEIGHT_MODEL = {'hop': 'weight', 'delay': "delay", "bw": "bw"}
def __init__(self, *args, **kwargs):
super(QoSRouteApp, self).__init__(*args, **kwargs)
self.name = 'shortest_forwarding'
self.awareness = kwargs["network_awareness"]
self.monitor = kwargs["network_monitor"]
self.delay_detector = kwargs["network_delay_detector"]
self.route_detector = kwargs['network_route_detector']
self.hosts = {}
self.datapaths = {}
self.arp_table = {}
self.threads = []
self.switches = []
self.adjacency = defaultdict(dict)
self.multipath_group_ids = {}
self.group_ids = []
# self.weight = self.WEIGHT_MODEL[CONF.weight]
# self.monitor = hub.spawn(self._monitor)
def add_ports_to_paths(self, paths, first_port, last_port):
'''
Add the ports that connects the switches for all paths
'''
paths_p = []
for path in paths:
p = {}
in_port = first_port
for s1, s2 in zip(path[:-1], path[1:]):
out_port = self.adjacency[s1][s2]
p[s1] = (in_port, out_port)
in_port = self.adjacency[s2][s1]
# print("sw -> %s , %s" % (s1, s2))
# print("port -> %s -> %s" % (p[s1][0], p[s1][1]))
p[path[-1]] = (in_port, last_port)
paths_p.append(p)
return paths_p
def generate_openflow_gid(self):
'''
Returns a random OpenFlow group id
'''
n = random.randint(0, 2 ** 32)
while n in self.group_ids:
n = random.randint(0, 2 ** 32)
return n
def install_paths(self, src, first_port, dst, last_port, ip_src, ip_dst):
computation_start = time.time()
paths = [self.route_detector.find_optimal_path(src, dst)]
# paths = self.get_optimal_paths(src, dst)
# pw = []
# for path in paths:
# pw.append(self.get_path_cost(path))
# print(path, "cost = ", pw[len(pw) - 1])
# sum_of_pw = sum(pw) * 1.0
# print( paths )
paths_with_ports = self.add_ports_to_paths(paths, first_port, last_port)
# print(paths_with_ports)
switches_in_paths = set().union(*paths)
for node in switches_in_paths:
dp = self.datapaths[node]
ofp = dp.ofproto
ofp_parser = dp.ofproto_parser
ports = defaultdict(list)
actions = []
i = 0
for path in paths_with_ports:
if node in path:
in_port = path[node][0]
out_port = path[node][1]
if (out_port, 1) not in ports[in_port]:
ports[in_port].append((out_port, 1))
i += 1
for in_port in ports:
match_ip = ofp_parser.OFPMatch(
eth_type=0x0800,
ipv4_src=ip_src,
ipv4_dst=ip_dst
)
match_arp = ofp_parser.OFPMatch(
eth_type=0x0806,
arp_spa=ip_src,
arp_tpa=ip_dst
)
out_ports = ports[in_port]
# print out_ports
if len(out_ports) > 1:
group_id = None
group_new = False
if (node, src, dst) not in self.multipath_group_ids:
group_new = True
self.multipath_group_ids[
node, src, dst] = self.generate_openflow_gid()
group_id = self.multipath_group_ids[node, src, dst]
buckets = []
# print "node at ",node," out ports : ",out_ports
for port, weight in out_ports:
bucket_weight = int(round(10))
bucket_action = [ofp_parser.OFPActionOutput(port)]
buckets.append(
ofp_parser.OFPBucket(
weight=bucket_weight,
watch_port=port,
watch_group=ofp.OFPG_ANY,
actions=bucket_action
)
)
if group_new:
req = ofp_parser.OFPGroupMod(
dp, ofp.OFPGC_ADD, ofp.OFPGT_SELECT, group_id,
buckets
)
dp.send_msg(req)
else:
req = ofp_parser.OFPGroupMod(
dp, ofp.OFPGC_MODIFY, ofp.OFPGT_SELECT,
group_id, buckets)
dp.send_msg(req)
actions = [ofp_parser.OFPActionGroup(group_id)]
self.add_flow(dp, 32768, match_ip, actions)
self.add_flow(dp, 1, match_arp, actions)
elif len(out_ports) == 1:
actions = [ofp_parser.OFPActionOutput(out_ports[0][0])]
self.add_flow(dp, 32768, match_ip, actions)
self.add_flow(dp, 1, match_arp, actions)
print("Path installation finished in ", time.time() - computation_start)
return paths_with_ports[0][src][1]
def add_flow(self, datapath, priority, match, actions, buffer_id=None):
# print "Adding flow ", match, actions
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):
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_protocol(ethernet.ethernet)
arp_pkt = pkt.get_protocol(arp.arp)
# avoid broadcast from LLDP
if eth.ethertype == 35020:
return
if pkt.get_protocol(ipv6.ipv6): # Drop the IPV6 Packets.
match = parser.OFPMatch(eth_type=eth.ethertype)
actions = []
self.add_flow(datapath, 1, match, actions)
return None
dst = eth.dst
src = eth.src
dpid = datapath.id
if src not in self.hosts:
self.hosts[src] = (dpid, in_port)
out_port = ofproto.OFPP_FLOOD
if arp_pkt:
# print dpid, pkt
src_ip = arp_pkt.src_ip
dst_ip = arp_pkt.dst_ip
if arp_pkt.opcode == arp.ARP_REPLY:
self.arp_table[src_ip] = src
h1 = self.hosts[src]
h2 = self.hosts[dst]
out_port = self.install_paths(h1[0], h1[1], h2[0], h2[1], src_ip, dst_ip)
self.install_paths(h2[0], h2[1], h1[0], h1[1], dst_ip, src_ip) # reverse
elif arp_pkt.opcode == arp.ARP_REQUEST:
if dst_ip in self.arp_table:
self.arp_table[src_ip] = src
dst_mac = self.arp_table[dst_ip]
h1 = self.hosts[src]
h2 = self.hosts[dst_mac]
out_port = self.install_paths(h1[0], h1[1], h2[0], h2[1], src_ip, dst_ip)
self.install_paths(h2[0], h2[1], h1[0], h1[1], dst_ip, src_ip) # reverse
# print pkt
actions = [parser.OFPActionOutput(out_port)]
data = None
if msg.buffer_id == ofproto.OFP_NO_BUFFER:
data = msg.data
out = parser.OFPPacketOut(
datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port,
actions=actions, data=data)
datapath.send_msg(out)
@set_ev_cls(event.EventSwitchEnter)
def switch_enter_handler(self, ev):
switch = ev.switch.dp
ofp_parser = switch.ofproto_parser
if switch.id not in self.switches:
self.switches.append(switch.id)
self.datapaths[switch.id] = switch
# Request port/link descriptions, useful for obtaining bandwidth
# req = ofp_parser.OFPPortDescStatsRequest(switch)
# switch.send_msg(req)
@set_ev_cls(event.EventSwitchLeave, MAIN_DISPATCHER)
def switch_leave_handler(self, ev):
print(ev)
switch = ev.switch.dp.id
if switch in self.switches:
self.switches.remove(switch)
del self.datapaths[switch]
del self.adjacency[switch]
@set_ev_cls(event.EventLinkAdd, MAIN_DISPATCHER)
def link_add_handler(self, ev):
s1 = ev.link.src
s2 = ev.link.dst
self.adjacency[s1.dpid][s2.dpid] = s1.port_no
self.adjacency[s2.dpid][s1.dpid] = s2.port_no
@set_ev_cls(event.EventLinkDelete, MAIN_DISPATCHER)
def link_delete_handler(self, ev):
s1 = ev.link.src
s2 = ev.link.dst
# Exception handling if switch already deleted
try:
del self.adjacency[s1.dpid][s2.dpid]
del self.adjacency[s2.dpid][s1.dpid]
except KeyError:
pass