-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
277 lines (225 loc) · 8.26 KB
/
main.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
# Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
An OpenFlow 1.0 L2 learning switch implementation.
"""
from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_v1_0
from ryu.lib.mac import haddr_to_bin
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
from ryu.app.wsgi import ControllerBase
from ryu.topology import event, switches
import time
T0 = time.time()
import json
def load_wights():
with open('weights.config') as f:
return json.load(f)
def make_flow_trace(ft):
# ft = [datapath.id, src, dst, in_port, out_port, time.time()]
SPACE = " "
EQUAL = "="
names = ['dpid', 'src', 'dst', 'in_port', 'out_port', 'time']
ENDL = '\n'
rep = ''
for i in range(len(names)):
rep = rep + names[i] + EQUAL + str(ft[i]) + SPACE
return rep + ENDL
class SimpleSwitch(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]
def __init__(self, *args, **kwargs):
super(SimpleSwitch, self).__init__(*args, **kwargs)
self.mac_to_port = {}
self.topology_api_app = self
self.datapath_list = []
self.switches = []
self.adjacency = {} #map[sw1][sw2] -> port
self.mymac = {}
self.weights = {} #map[sw1][sw2] -> w12
def get_min(self, distances, remining_nodes):
min = float('inf')
min_node = None
for node in remining_nodes:
if distances[node] < min:
min = distances[node]
min_node = node
return min_node
def dijkestra(self, src,dst):
distances = {}
prev = {}
#init table
for dpid in self.switches:
distances[dpid] = float('inf')
prev[dpid] = None
distances[src] = 0
# print 'initial distances', distances
remaining_nodes = set(self.switches[::])
while len(remaining_nodes)>0:
# print 'len remaining nodes ', len(remaining_nodes)
node = self.get_min(distances, remaining_nodes)
# print 'selected node', node
remaining_nodes.remove(node)
for r_node in remaining_nodes:
if r_node in self.adjacency[node]:
if distances[node] + self.weights[node][r_node] < distances[r_node]:
distances[r_node] = distances[node] + self.weights[node][r_node]
prev[r_node] = node
#back track in path
path = []
# print 'before loop kiri'
x = dst
path.append(x)
while (not prev[x] == src) and (not prev[x] is None):
x = prev[x]
path.append(x)
# print 'after loop kiri'
path.append(src)
# print 'path= ', path[::-1]
return self.adjacency[src][x], path
def add_flow(self, datapath, in_port, dst, src, actions, out_port):
flowtabeltrace = [datapath.id, src, dst, in_port, out_port, float(time.time()-T0)]
with open('flowtable.trace', 'a') as f:
f.write(make_flow_trace(flowtabeltrace))
ofproto = datapath.ofproto
match = datapath.ofproto_parser.OFPMatch(
in_port=in_port,
dl_dst=haddr_to_bin(dst), dl_src=haddr_to_bin(src))
mod = datapath.ofproto_parser.OFPFlowMod(
datapath=datapath, match=match, cookie=0,
command=ofproto.OFPFC_ADD, idle_timeout=0, hard_timeout=0,
priority=ofproto.OFP_DEFAULT_PRIORITY,
flags=ofproto.OFPFF_SEND_FLOW_REM, actions=actions)
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
pkt = packet.Packet(msg.data)
eth = pkt.get_protocol(ethernet.ethernet)
if eth.ethertype == ether_types.ETH_TYPE_LLDP:
# ignore lldp packet
return
dst = eth.dst
src = eth.src
dpid = datapath.id
self.mac_to_port.setdefault(dpid, {})
# learn a mac address to avoid FLOOD next time.
self.mac_to_port[dpid][src] = msg.in_port
# print 'mac to port', self.mac_to_port
if not src in self.mymac:
self.mymac[src] = (dpid, msg.in_port)
if dst in self.mymac and self.mymac[dst][0] == dpid:
# print '_______ONE HUP__________'
# self.logger.info("packet in %s %s %s %s", dpid, src, dst, msg.in_port)
# print 'my mac', self.mymac
out_port = self.mymac[dst][1]
if self.mymac[src] == (dpid, msg.in_port):
with open('path.trace', 'a') as f:
SPACE = " "
rep = "SEND" + SPACE + 'time=' +str(time.time()-T0) + SPACE +"src=" + src + SPACE + "dst=" + dst + SPACE + 'path='+str([dpid]) + '\n'
f.write(rep)
with open('path.trace', 'a') as f:
SPACE = " "
rep = "RECV" + SPACE + 'time=' +str(time.time()-T0) + SPACE +"src=" + src + SPACE + "dst=" + dst + SPACE+ '\n'
f.write(rep)
elif dst in self.mymac:
# print '___________ perform dijkestra ___________'
# self.logger.info("packet in %s %s %s %s", dpid, src, dst, msg.in_port)
# print 'my mac', self.mymac
# sw_src = self.mymac[src][0]
sw_src = dpid
sw_dst = self.mymac[dst][0]
# print 'sw_srd: ', sw_src, 'sw_dst:', sw_dst
# print '*********befor dij'
out_port, path = self.dijkestra(sw_src, sw_dst)
if self.mymac[src] == (dpid, msg.in_port):
with open('path.trace', 'a') as f:
SPACE = " "
rep = "SEND" + SPACE + 'time=' +str(time.time()-T0) + SPACE +"src=" + src + SPACE + "dst=" + dst + SPACE + 'path='+str(path) + '\n'
f.write(rep)
# print '*********after dij'
# print 'dij out port', out_port
else:
out_port = ofproto.OFPP_FLOOD
actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]
# install a flow to avoid packet_in next time
if out_port != ofproto.OFPP_FLOOD:
self.add_flow(datapath, msg.in_port, dst, src, actions, out_port)
data = None
if msg.buffer_id == ofproto.OFP_NO_BUFFER:
data = msg.data
out = datapath.ofproto_parser.OFPPacketOut(
datapath=datapath, buffer_id=msg.buffer_id, in_port=msg.in_port,
actions=actions, data=data)
datapath.send_msg(out)
@set_ev_cls(ofp_event.EventOFPPortStatus, MAIN_DISPATCHER)
def _port_status_handler(self, ev):
msg = ev.msg
reason = msg.reason
port_no = msg.desc.port_no
ofproto = msg.datapath.ofproto
if reason == ofproto.OFPPR_ADD:
self.logger.info("port added %s", port_no)
elif reason == ofproto.OFPPR_DELETE:
self.logger.info("port deleted %s", port_no)
elif reason == ofproto.OFPPR_MODIFY:
self.logger.info("port modified %s", port_no)
else:
self.logger.info("Illeagal port state %s %s", port_no, reason)
@set_ev_cls(event.EventSwitchEnter)
def get_topology_data(self, ev):
print 'entered event.EvnetSwitchEnter'
temp_dict = load_wights()
# print 'temp_dict', temp_dict
self.weights = {}
for key1 in temp_dict:
self.weights[int(key1)] = {}
for key2 in temp_dict[key1]:
self.weights[int(key1)][int(key2)] = float(temp_dict[key1][key2])
switch_list = get_switch(self.topology_api_app, None)
switches=[switch.dp.id for switch in switch_list]
self.switches = [switch.dp.id for switch in switch_list]
self.datapath_list=[switch.dp for switch in switch_list]
links_list = get_link(self.topology_api_app, None)
srcs = [link.src for link in links_list]
mylinks=[(link.src.dpid,link.dst.dpid,link.src.port_no,link.dst.port_no) for link in links_list]
print 'my links', mylinks
for s1,s2,port1,port2 in mylinks:
# check if keys(s1 and s2 are valid)
if not s1 in self.adjacency:
self.adjacency[s1] = {}
if not s2 in self.adjacency:
self.adjacency[s2] = {}
if not s1 in self.weights:
self.weights[s1] = {}
if not s2 in self.weights:
self.weights[s2]={}
self.adjacency[s1][s2]=port1
self.adjacency[s2][s1]=port2
#@TODO add real costs to self.weights
if (not s1 in self.weights) or (not s2 in self.weights[s1]):
self.weights[s1][s2]=1
if (not s2 in self.weights) or (not s1 in self.weights[s2]):
self.weights[s2][s1]=1
print 'switches = ', self.switches
print 'adjecency = ', self.adjacency
print 'weights = ', self.weights