-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopology_slicing.py
74 lines (61 loc) · 2.49 KB
/
topology_slicing.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
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.ofproto import ofproto_v1_3
class TrafficSlicing(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
def __init__(self, *args, **kwargs):
super(TrafficSlicing, self).__init__(*args, **kwargs)
# out_port = slice_to_port[dpid][in_port]
self.slice_to_port = {
1: {1: 3, 3: 1, 2: 4, 4: 2},
4: {1: 3, 3: 1, 2: 4, 4: 2},
2: {1: 3, 3: 1, 2: 4, 4: 2},
5: {1: 2, 2: 1},
3: {1: 2, 2: 1},
}
@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):
msg = ev.msg
datapath = msg.datapath
in_port = msg.match["in_port"]
dpid = datapath.id
out_port = self.slice_to_port[dpid][in_port]
actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]
match = datapath.ofproto_parser.OFPMatch(in_port=in_port)
self.add_flow(datapath, 1, match, actions)
self._send_package(msg, datapath, in_port, actions)