-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_switch.py
More file actions
82 lines (69 loc) · 3.45 KB
/
simple_switch.py
File metadata and controls
82 lines (69 loc) · 3.45 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
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
from ryu.lib.packet import packet
from ryu.lib.packet import ethernet
class SimpleSwitch13(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
def __init__(self, *args, **kwargs):
super(SimpleSwitch13, self).__init__(*args, **kwargs)
# This table will store MAC addresses and which switch port they are on
# Format: { dpid: { mac_addr: port } }
self.mac_to_port = {}
@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 a "table-miss" flow entry.
# This rule sends any packet that doesn't match other rules to the controller.
match = parser.OFPMatch()
actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
ofproto.OFPCML_NO_BUFFER)]
self.add_flow(datapath, 0, match, actions) # Priority 0 (lowest)
self.logger.info("Switch %s connected. Table-miss rule installed.", datapath.id)
def add_flow(self, datapath, priority, match, actions):
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions)]
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)
# Ignore non-ethernet packets
if not eth:
return
dst = eth.dst
src = eth.src
dpid = datapath.id
self.mac_to_port.setdefault(dpid, {})
# Learn the MAC address of the source host.
self.mac_to_port[dpid][src] = in_port
self.logger.info("Learned: host %s is on switch %s, port %s", src, dpid, in_port)
# If we know the destination MAC's port, install a specific flow rule.
if dst in self.mac_to_port[dpid]:
out_port = self.mac_to_port[dpid][dst]
actions = [parser.OFPActionOutput(out_port)]
match = parser.OFPMatch(in_port=in_port, eth_dst=dst)
# Install a flow to avoid sending to controller next time. Priority 1 > 0.
self.add_flow(datapath, 1, match, actions)
self.logger.info("Installing flow: %s -> %s via port %s", src, dst, out_port)
else:
# If we don't know where the destination is, flood it to all ports.
out_port = ofproto.OFPP_FLOOD
actions = [parser.OFPActionOutput(out_port)]
self.logger.info("Flooding packet from %s to unknown %s", src, dst)
# Instruct the switch to send the current packet out.
out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id,
in_port=in_port, actions=actions, data=msg.data)
datapath.send_msg(out)