forked from Roman-Simone/network-slice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
273 lines (225 loc) · 10.9 KB
/
Copy pathcontroller.py
File metadata and controls
273 lines (225 loc) · 10.9 KB
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
from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.cmd.manager import main as ryu_run
from ryu import cfg
from ryu.lib.packet import packet
from ryu.lib.packet import ethernet
from ryu.ofproto import ofproto_v1_3
from ryu.lib.packet import ether_types
from ryu.lib.packet import udp
from ryu.lib.packet import tcp
from ryu.lib.packet import icmp
from ryu.lib import hub
from mininet.log import info, setLogLevel
from subprocess import check_output
import shlex
import time
import paramiko
import requests
import socket
class MyController(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
def __init__(self, *args, **kwargs):
super(MyController, self).__init__(*args, **kwargs)
setLogLevel("info")
#Bind host MAC adresses to interface
self.services_slicing_hosts = ["00:00:00:00:00:01","00:00:00:00:00:02","00:00:00:00:00:03","00:00:00:00:00:04"]
self.host_connects_to_switch = {
1 : ["00:00:00:00:00:01","00:00:00:00:00:02"],
2 : ["00:00:00:00:00:07"],
3 : ["00:00:00:00:00:05"],
4 : ["00:00:00:00:00:03","00:00:00:00:00:04"]
}
self.slice_host = {
"00:00:00:00:00:01" : 1,
"00:00:00:00:00:02" : 1,
"00:00:00:00:00:03" : 1,
"00:00:00:00:00:04" : 1,
"00:00:00:00:00:05" : 2,
"00:00:00:00:00:06" : 2,
"00:00:00:00:00:07" : 3,
"00:00:00:00:00:08" : 3,
}
self.mac_to_port = {
1: {
"00:00:00:00:00:01": 3,
"00:00:00:00:00:02": 4,
"00:00:00:00:00:05": 2,
"00:00:00:00:00:06": 5,
},
2: {
"00:00:00:00:00:01": 1,
"00:00:00:00:00:02": 1,
"00:00:00:00:00:03": 2,
"00:00:00:00:00:04": 2,
"00:00:00:00:00:07": 3,
"00:00:00:00:00:08": 2
},
3: {
"00:00:00:00:00:01": 1,
"00:00:00:00:00:02": 1,
"00:00:00:00:00:03": 2,
"00:00:00:00:00:04": 2,
"00:00:00:00:00:05": 3,
"00:00:00:00:00:06": 1
},
4: {
"00:00:00:00:00:03": 3,
"00:00:00:00:00:04": 4,
"00:00:00:00:00:07": 1,
"00:00:00:00:00:08": 5,
},
}
#9998 used for iperf testing, 9999 used for service packets
self.slice_TCport = [9998, 9999]
#Associate interface to slice
self.slice_ports = {
1: {1: 1, 2: 2},
4: {1: 1, 2: 2},
}
self.end_swtiches = [1, 4]
self.link_stats = {}
@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
#Install the table-miss flow entry.
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):
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
#Construct flow_mod message and send it.
inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions)]
mod = parser.OFPFlowMod(
datapath=datapath, priority=priority, match=match, instructions=inst
)
datapath.send_msg(mod)
def _send_package(self, msg, datapath, in_port, actions):
data = None
ofproto = datapath.ofproto
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=in_port,
actions=actions,
data=data,
)
datapath.send_msg(out)
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def _packet_in_handler(self, ev):
#Get packet info
msg = ev.msg
datapath = msg.datapath
ofproto = datapath.ofproto
in_port = msg.match["in_port"]
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
# print(src,dst,dpid)
#check stesso slice
if dpid in self.mac_to_port and ((src in self.slice_host.keys() and dst in self.slice_host.keys()) and self.slice_host[src] == self.slice_host[dst]):
# check se gli host di partenza e arrivo sono tra quelli che fanno lo slicing di servizio
if src in self.services_slicing_hosts and dst in self.services_slicing_hosts:
# check se l'host di arrivo è direttamente connesso allo switch (dpid)
if dst in self.host_connects_to_switch[dpid]:
print("Invio diretto")
out_port = self.mac_to_port[dpid][dst]
print("outport " + str(out_port))
actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]
match = datapath.ofproto_parser.OFPMatch(eth_dst=dst)
self.add_flow(datapath, 1, match, actions) #ok
self._send_package(msg, datapath, in_port, actions)
elif dpid in self.end_swtiches :
print("siamo nello switch 1 o 4")
# siamo nello switch 1 o 4
#decidere se andare sulla slice video (UDP) o non-video (NON UDP)
if (pkt.get_protocol(udp.udp)):
print("siamo nella slice 1 video")
#siamo nella slice 1
slice_number = 1
out_port = self.slice_ports[dpid][slice_number]
match = datapath.ofproto_parser.OFPMatch(
in_port=in_port,
eth_dst=dst,
eth_type=ether_types.ETH_TYPE_IP,
ip_proto=0x11, # udp
udp_dst=pkt.get_protocol(udp.udp).dst_port,
)
actions = [datapath.ofproto_parser.OFPActionSetQueue(12),datapath.ofproto_parser.OFPActionOutput(out_port)]
self.add_flow(datapath, 2, match, actions) #ok
self._send_package(msg, datapath, in_port, actions)
else :
print("siamo nella slice 1 non video")
#siamo nella slice 2
# mandare pacchetti degli altri tipi
if pkt.get_protocol(tcp.tcp):
print("tcp")
#Create new flow automatically and send to low performance slice
slice_number = 2
out_port = self.slice_ports[dpid][slice_number]
match = datapath.ofproto_parser.OFPMatch(
in_port=in_port,
eth_dst=dst,
eth_src=src,
eth_type=ether_types.ETH_TYPE_IP,
ip_proto=0x06, # tcp
)
actions = [datapath.ofproto_parser.OFPActionSetQueue(34),datapath.ofproto_parser.OFPActionOutput(out_port)]
self.add_flow(datapath, 1, match, actions) #ok
self._send_package(msg, datapath, in_port, actions)
elif pkt.get_protocol(icmp.icmp):
print("icmp")
#Create new flow automatically and send to low performance slice
slice_number = 2
out_port = self.slice_ports[dpid][slice_number]
match = datapath.ofproto_parser.OFPMatch(
in_port=in_port,
eth_dst=dst,
eth_src=src,
eth_type=ether_types.ETH_TYPE_IP,
ip_proto=0x01, # icmp
)
actions = [datapath.ofproto_parser.OFPActionSetQueue(34),datapath.ofproto_parser.OFPActionOutput(out_port)]
self.add_flow(datapath, 1, match, actions) #ok
self._send_package(msg, datapath, in_port, actions)
out_port = self.slice_ports[dpid][slice_number]
else :
# siamo nello switch 2 o 3 oppure
print("siamo nello switch 2 o 3")
# Extract the output port
out_port = self.mac_to_port[dpid][dst]
# Define a list of actions that are executed if the new flow entry is matched
actions = [datapath.ofproto_parser.OFPActionSetQueue(12),datapath.ofproto_parser.OFPActionOutput(out_port)]
# Creating a new OFPMatch object to match incoming packets based on the destination MAC address
match = datapath.ofproto_parser.OFPMatch(eth_dst=dst)
# Add a new flow entry to the flow table of the switch
self.add_flow(datapath, 1, match, actions)#ok
# Send the packet
self._send_package(msg, datapath, in_port, actions)
elif src not in self.services_slicing_hosts and dst not in self.services_slicing_hosts:
# Extract the output port
if dst in self.mac_to_port[dpid] and self.slice_host[src] == self.slice_host[dst]:
print("caso slice 2 o 3")
out_port = self.mac_to_port[dpid][dst]
# Define a list of actions that are executed if the new flow entry is matched
actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]
# Creating a new OFPMatch object to match incoming packets based on the destination MAC address
match = datapath.ofproto_parser.OFPMatch(eth_dst=dst)
# Add a new flow entry to the flow table of the switch
# self.add_flow(datapath, 1, match, actions)
# Send the packet
self._send_package(msg, datapath, in_port, actions)