-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontroller.py
269 lines (238 loc) · 9.46 KB
/
controller.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
import copy
import logging
from ryu.app.wsgi import WSGIApplication
from ryu.base import app_manager
from ryu.cmd import manager
from ryu.controller import ofp_event
from ryu.controller.handler import set_ev_cls, CONFIG_DISPATCHER, MAIN_DISPATCHER
from ryu.ofproto import ofproto_v1_3
from ryu.topology import event
from ryu.topology.api import get_switch, get_link
from rest import RestController
from ryu.lib import hub
import utils
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)
self.topology_api_app = self
wsgi = kwargs["wsgi"]
self.myapp = wsgi
wsgi.register(RestController, {"controller": self})
self.lock = hub.Event()
self.flows = []
self.orig_link_data = utils.load_data("topology.txt")
self.link_data = copy.deepcopy(self.orig_link_data)
# self.link_data = utils.load_data("sample1.txt")
self.hosts = {} # h1: host object
self.switches = {} # dpid: datapath object
self.adj = {} # adjacency lists
self.rules = {} # dpid: flow rules
def get_host_by_mac(self, host_mac):
host_mac = host_mac.replace(":", "")
return f"h{int(host_mac, 16)}"
def get_host_by_ip(self, host_ip):
for k, v in self.hosts.items():
if v.ipv4 and host_ip in v.ipv4:
print(k)
return k
if v.ipv6 and host_ip in v.ipv6:
print(k)
return k
print("host not found")
return None
@set_ev_cls(event.EventHostAdd)
def host_add_handler(self, ev):
print(f"added {self.get_host_by_mac(ev.host.mac)}")
self.hosts[self.get_host_by_mac(ev.host.mac)] = ev.host
host_name, switch_name = (
self.get_host_by_mac(ev.host.mac),
f"s{ev.host.port.dpid}",
)
# each host connected to exactly one switch
self.adj[host_name] = {switch_name}
if switch_name not in self.adj:
self.adj[switch_name] = {host_name}
else:
self.adj[switch_name].add(host_name)
print("adj:", self.adj)
@set_ev_cls(event.EventSwitchEnter)
def switch_add_handler(self, ev):
print(f"added s{ev.switch.dp.id}")
self.switches[ev.switch.dp.id] = ev.switch.dp
switch_name = f"s{ev.switch.dp.id}"
links = get_link(self.topology_api_app, ev.switch.dp.id)
links = {f"s{link.dst.dpid}" for link in links}
for link in links.union({switch_name}):
if link not in self.adj:
self.adj[link] = set()
self.adj[switch_name].update(links)
for link in links:
self.adj[link].add(switch_name)
print("adj:", self.adj)
def add_rule(self, datapath, in_port, src_mac, dst_mac, out_port):
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
# Create a match object that matches incoming packets on the specified input port
match = parser.OFPMatch(in_port=in_port, eth_src=src_mac, eth_dst=dst_mac)
# Create an action object that sends packets out of the specified output port
action = parser.OFPActionOutput(out_port)
# Create a list of instructions that includes the action to take when a packet matches the match criteria
instructions = [
parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, [action])
]
# Add the rule to the list of rules for this switch
if datapath.id not in self.rules.keys():
self.rules[datapath.id] = []
obj = {
"match": {
"in_port": in_port,
"src_mac": src_mac,
"dst_mac": dst_mac,
},
"action": {
"out_port": out_port,
},
}
if obj not in self.rules[datapath.id]:
self.rules[datapath.id].append(obj)
# Create a flow mod message that installs the rule on the switch
flow_mod = parser.OFPFlowMod(
datapath=datapath,
match=match,
instructions=instructions,
out_port=out_port,
priority=5,
idle_timeout=0,
hard_timeout=0,
buffer_id=ofproto.OFP_NO_BUFFER,
out_group=ofproto.OFPG_ANY,
)
# Send the flow mod message to the switch
datapath.send_msg(flow_mod)
def add_path_rules(self, path, req_bw, new_connection=False):
rules = {}
if len(path) < 3:
return rules
src, dst = self.hosts[path[0]], self.hosts[path[-1]]
in_port = src.port.port_no
for i in range(1, len(path) - 2):
dpid = int(path[i][1:])
links = get_link(self.topology_api_app, dpid)
link = [link for link in links if link.dst.dpid == int(path[i + 1][1:])][0]
if new_connection:
rules[path[i]] = [
{
"match": {
"in_port": in_port,
"src_mac": src.mac,
"dst_mac": dst.mac,
},
"action": {
"out_port": link.src.port_no,
},
}
]
else:
dp = get_switch(self.topology_api_app, dpid)[0].dp
self.add_rule(
datapath=dp,
in_port=in_port,
src_mac=src.mac,
dst_mac=dst.mac,
out_port=link.src.port_no,
)
self.link_data[(path[i - 1], path[i])][0] -= req_bw
self.link_data[(path[i], path[i - 1])][0] -= req_bw
rules[path[i]] = self.rules[dpid]
in_port = link.dst.port_no
dpid = int(path[-2][1:])
if new_connection:
rules[path[-2]] = [
{
"match": {
"in_port": in_port,
"src_mac": src.mac,
"dst_mac": dst.mac,
},
"action": {
"out_port": dst.port.port_no,
},
}
]
else:
dp = get_switch(self.topology_api_app, dpid)[0].dp
self.add_rule(
datapath=dp,
in_port=in_port,
src_mac=src.mac,
dst_mac=dst.mac,
out_port=dst.port.port_no,
)
self.link_data[(path[-3], path[-2])][0] -= req_bw
self.link_data[(path[-2], path[-3])][0] -= req_bw
rules[path[-2]] = self.rules[dpid]
self.link_data[(path[-2], path[-1])][0] -= req_bw
self.link_data[(path[-1], path[-2])][0] -= req_bw
return rules
def send_flow_request(self, datapath):
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
req = parser.OFPFlowStatsRequest(
datapath, 0, ofproto.OFPTT_ALL, ofproto.OFPP_ANY, ofproto.OFPG_ANY
)
datapath.send_msg(req)
@set_ev_cls(ofp_event.EventOFPFlowStatsReply, MAIN_DISPATCHER)
def flow_stats_reply_handler(self, ev):
flows = []
for stat in ev.msg.body:
flows.append(
{
"match": stat.match.to_jsondict(),
"actions": [
action.to_jsondict() for action in stat.instructions[0].actions
],
}
)
self.flows = flows
self.lock.set()
@set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
def switch_features_handler(self, ev):
datapath = ev.msg.datapath
self.add_flow(datapath, 0)
def add_flow(self, datapath, priority, buffer_id=None):
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
# install table-miss flow entry
#
# We specify NO BUFFER to max_len of the output action due to
# OVS bug. At this moment, if we specify a lesser number, e.g.,
# 128, OVS will send Packet-In with invalid buffer_id and
# truncated packet data. In that case, we cannot output packets
# correctly. The bug has been fixed in OVS v2.1.0.
match = parser.OFPMatch()
action = parser.OFPActionOutput(
ofproto.OFPP_CONTROLLER, ofproto.OFPCML_NO_BUFFER
)
instructions = [
parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, [action])
]
if buffer_id:
mod = parser.OFPFlowMod(
datapath=datapath,
buffer_id=buffer_id,
priority=priority,
match=match,
instructions=instructions,
)
else:
mod = parser.OFPFlowMod(
datapath=datapath,
priority=priority,
match=match,
instructions=instructions,
)
datapath.send_msg(mod)
if __name__ == "__main__":
manager.main(args=[__file__, "--observe-links"])