backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / app / simple_switch_stp_13.py
1 # Copyright (C) 2016 Nippon Telegraph and Telephone Corporation.
2 #
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
6 #
7 #    http://www.apache.org/licenses/LICENSE-2.0
8 #
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
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
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
26
27
28 class SimpleSwitch13(simple_switch_13.SimpleSwitch13):
29     OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
30     _CONTEXTS = {'stplib': stplib.Stp}
31
32     def __init__(self, *args, **kwargs):
33         super(SimpleSwitch13, self).__init__(*args, **kwargs)
34         self.mac_to_port = {}
35         self.stp = kwargs['stplib']
36
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)
46
47     def delete_flow(self, datapath):
48         ofproto = datapath.ofproto
49         parser = datapath.ofproto_parser
50
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)
58
59     @set_ev_cls(stplib.EventPacketIn, MAIN_DISPATCHER)
60     def _packet_in_handler(self, ev):
61         msg = ev.msg
62         datapath = msg.datapath
63         ofproto = datapath.ofproto
64         parser = datapath.ofproto_parser
65         in_port = msg.match['in_port']
66
67         pkt = packet.Packet(msg.data)
68         eth = pkt.get_protocols(ethernet.ethernet)[0]
69
70         dst = eth.dst
71         src = eth.src
72
73         dpid = datapath.id
74         self.mac_to_port.setdefault(dpid, {})
75
76         self.logger.info("packet in %s %s %s %s", dpid, src, dst, in_port)
77
78         # learn a mac address to avoid FLOOD next time.
79         self.mac_to_port[dpid][src] = in_port
80
81         if dst in self.mac_to_port[dpid]:
82             out_port = self.mac_to_port[dpid][dst]
83         else:
84             out_port = ofproto.OFPP_FLOOD
85
86         actions = [parser.OFPActionOutput(out_port)]
87
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)
92
93         data = None
94         if msg.buffer_id == ofproto.OFP_NO_BUFFER:
95             data = msg.data
96
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)
100
101     @set_ev_cls(stplib.EventTopologyChange, MAIN_DISPATCHER)
102     def _topology_change_handler(self, ev):
103         dp = ev.dp
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)
107
108         if dp.id in self.mac_to_port:
109             self.delete_flow(dp)
110             del self.mac_to_port[dp.id]
111
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])