backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / app / simple_switch_lacp.py
1 # Copyright (C) 2013 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 import struct
17
18 from ryu.base import app_manager
19 from ryu.controller.handler import MAIN_DISPATCHER
20 from ryu.controller.handler import set_ev_cls
21 from ryu.ofproto import ofproto_v1_0
22 from ryu.lib import addrconv
23 from ryu.lib import lacplib
24 from ryu.lib.dpid import str_to_dpid
25
26
27 class SimpleSwitchLacp(app_manager.RyuApp):
28     OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]
29     _CONTEXTS = {'lacplib': lacplib.LacpLib}
30
31     def __init__(self, *args, **kwargs):
32         super(SimpleSwitchLacp, self).__init__(*args, **kwargs)
33         self.mac_to_port = {}
34         self._lacp = kwargs['lacplib']
35         # in this sample application, bonding i/fs of the switchs
36         # shall be set up as follows:
37         # - the port 1 and 2 of the datapath 1 face the slave i/fs.
38         # - the port 3, 4 and 5 of the datapath 1 face the others.
39         # - the port 1 and 2 of the datapath 2 face the others.
40         self._lacp.add(
41             dpid=str_to_dpid('0000000000000001'), ports=[1, 2])
42         self._lacp.add(
43             dpid=str_to_dpid('0000000000000001'), ports=[3, 4, 5])
44         self._lacp.add(
45             dpid=str_to_dpid('0000000000000002'), ports=[1, 2])
46
47     def add_flow(self, datapath, in_port, dst, actions):
48         ofproto = datapath.ofproto
49         parser = datapath.ofproto_parser
50
51         match = parser.OFPMatch(in_port=in_port,
52                                 dl_dst=addrconv.mac.text_to_bin(dst))
53         mod = parser.OFPFlowMod(
54             datapath=datapath, match=match, cookie=0,
55             command=ofproto.OFPFC_ADD, actions=actions)
56         datapath.send_msg(mod)
57
58     def del_flow(self, datapath, dst):
59         ofproto = datapath.ofproto
60         parser = datapath.ofproto_parser
61
62         match = parser.OFPMatch(dl_dst=addrconv.mac.text_to_bin(dst))
63         mod = parser.OFPFlowMod(
64             datapath=datapath, match=match, cookie=0,
65             command=ofproto.OFPFC_DELETE)
66         datapath.send_msg(mod)
67
68     @set_ev_cls(lacplib.EventPacketIn, MAIN_DISPATCHER)
69     def _packet_in_handler(self, ev):
70         msg = ev.msg
71         datapath = msg.datapath
72         ofproto = datapath.ofproto
73
74         (dst_, src_, _eth_type) = struct.unpack_from(
75             '!6s6sH', buffer(msg.data), 0)
76         src = addrconv.mac.bin_to_text(src_)
77         dst = addrconv.mac.bin_to_text(dst_)
78
79         dpid = datapath.id
80         self.mac_to_port.setdefault(dpid, {})
81
82         self.logger.info("packet in %s %s %s %s",
83                          dpid, src, dst, msg.in_port)
84
85         # learn a mac address to avoid FLOOD next time.
86         self.mac_to_port[dpid][src] = msg.in_port
87
88         if dst in self.mac_to_port[dpid]:
89             out_port = self.mac_to_port[dpid][dst]
90         else:
91             out_port = ofproto.OFPP_FLOOD
92
93         actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]
94
95         # install a flow to avoid packet_in next time
96         if out_port != ofproto.OFPP_FLOOD:
97             self.add_flow(datapath, msg.in_port, dst, actions)
98
99         out = datapath.ofproto_parser.OFPPacketOut(
100             datapath=datapath, buffer_id=msg.buffer_id, in_port=msg.in_port,
101             actions=actions)
102         datapath.send_msg(out)
103
104     @set_ev_cls(lacplib.EventSlaveStateChanged, MAIN_DISPATCHER)
105     def _slave_state_changed_handler(self, ev):
106         datapath = ev.datapath
107         dpid = datapath.id
108         port_no = ev.port
109         enabled = ev.enabled
110         self.logger.info("slave state changed port: %d enabled: %s",
111                          port_no, enabled)
112         if dpid in self.mac_to_port:
113             for mac in self.mac_to_port[dpid]:
114                 self.del_flow(datapath, mac)
115             del self.mac_to_port[dpid]
116         self.mac_to_port.setdefault(dpid, {})