1 # Copyright (C) 2016 Nippon Telegraph and Telephone Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 from ryu.base import app_manager
17 from ryu.controller import ofp_event
18 from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER
19 from ryu.controller.handler import set_ev_cls
20 from ryu.ofproto import ofproto_v1_3
21 from ryu.lib import dpid as dpid_lib
22 from ryu.lib import stplib
23 from ryu.lib.packet import packet
24 from ryu.lib.packet import ethernet
25 from ryu.app import simple_switch_13
28 class SimpleSwitch13(simple_switch_13.SimpleSwitch13):
29 OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
30 _CONTEXTS = {'stplib': stplib.Stp}
32 def __init__(self, *args, **kwargs):
33 super(SimpleSwitch13, self).__init__(*args, **kwargs)
35 self.stp = kwargs['stplib']
37 # Sample of stplib config.
38 # please refer to stplib.Stp.set_config() for details.
39 config = {dpid_lib.str_to_dpid('0000000000000001'):
40 {'bridge': {'priority': 0x8000}},
41 dpid_lib.str_to_dpid('0000000000000002'):
42 {'bridge': {'priority': 0x9000}},
43 dpid_lib.str_to_dpid('0000000000000003'):
44 {'bridge': {'priority': 0xa000}}}
45 self.stp.set_config(config)
47 def delete_flow(self, datapath):
48 ofproto = datapath.ofproto
49 parser = datapath.ofproto_parser
51 for dst in self.mac_to_port[datapath.id].keys():
52 match = parser.OFPMatch(eth_dst=dst)
53 mod = parser.OFPFlowMod(
54 datapath, command=ofproto.OFPFC_DELETE,
55 out_port=ofproto.OFPP_ANY, out_group=ofproto.OFPG_ANY,
56 priority=1, match=match)
57 datapath.send_msg(mod)
59 @set_ev_cls(stplib.EventPacketIn, MAIN_DISPATCHER)
60 def _packet_in_handler(self, ev):
62 datapath = msg.datapath
63 ofproto = datapath.ofproto
64 parser = datapath.ofproto_parser
65 in_port = msg.match['in_port']
67 pkt = packet.Packet(msg.data)
68 eth = pkt.get_protocols(ethernet.ethernet)[0]
74 self.mac_to_port.setdefault(dpid, {})
76 self.logger.info("packet in %s %s %s %s", dpid, src, dst, in_port)
78 # learn a mac address to avoid FLOOD next time.
79 self.mac_to_port[dpid][src] = in_port
81 if dst in self.mac_to_port[dpid]:
82 out_port = self.mac_to_port[dpid][dst]
84 out_port = ofproto.OFPP_FLOOD
86 actions = [parser.OFPActionOutput(out_port)]
88 # install a flow to avoid packet_in next time
89 if out_port != ofproto.OFPP_FLOOD:
90 match = parser.OFPMatch(in_port=in_port, eth_dst=dst)
91 self.add_flow(datapath, 1, match, actions)
94 if msg.buffer_id == ofproto.OFP_NO_BUFFER:
97 out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id,
98 in_port=in_port, actions=actions, data=data)
99 datapath.send_msg(out)
101 @set_ev_cls(stplib.EventTopologyChange, MAIN_DISPATCHER)
102 def _topology_change_handler(self, ev):
104 dpid_str = dpid_lib.dpid_to_str(dp.id)
105 msg = 'Receive topology change event. Flush MAC table.'
106 self.logger.debug("[dpid=%s] %s", dpid_str, msg)
108 if dp.id in self.mac_to_port:
110 del self.mac_to_port[dp.id]
112 @set_ev_cls(stplib.EventPortStateChange, MAIN_DISPATCHER)
113 def _port_state_change_handler(self, ev):
114 dpid_str = dpid_lib.dpid_to_str(ev.dp.id)
115 of_state = {stplib.PORT_STATE_DISABLE: 'DISABLE',
116 stplib.PORT_STATE_BLOCK: 'BLOCK',
117 stplib.PORT_STATE_LISTEN: 'LISTEN',
118 stplib.PORT_STATE_LEARN: 'LEARN',
119 stplib.PORT_STATE_FORWARD: 'FORWARD'}
120 self.logger.debug("[dpid=%s][port=%d] state=%s",
121 dpid_str, ev.port_no, of_state[ev.port_state])