1 # Copyright (C) 2013 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.
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
27 class SimpleSwitchLacp(app_manager.RyuApp):
28 OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]
29 _CONTEXTS = {'lacplib': lacplib.LacpLib}
31 def __init__(self, *args, **kwargs):
32 super(SimpleSwitchLacp, self).__init__(*args, **kwargs)
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.
41 dpid=str_to_dpid('0000000000000001'), ports=[1, 2])
43 dpid=str_to_dpid('0000000000000001'), ports=[3, 4, 5])
45 dpid=str_to_dpid('0000000000000002'), ports=[1, 2])
47 def add_flow(self, datapath, in_port, dst, actions):
48 ofproto = datapath.ofproto
49 parser = datapath.ofproto_parser
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)
58 def del_flow(self, datapath, dst):
59 ofproto = datapath.ofproto
60 parser = datapath.ofproto_parser
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)
68 @set_ev_cls(lacplib.EventPacketIn, MAIN_DISPATCHER)
69 def _packet_in_handler(self, ev):
71 datapath = msg.datapath
72 ofproto = datapath.ofproto
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_)
80 self.mac_to_port.setdefault(dpid, {})
82 self.logger.info("packet in %s %s %s %s",
83 dpid, src, dst, msg.in_port)
85 # learn a mac address to avoid FLOOD next time.
86 self.mac_to_port[dpid][src] = msg.in_port
88 if dst in self.mac_to_port[dpid]:
89 out_port = self.mac_to_port[dpid][dst]
91 out_port = ofproto.OFPP_FLOOD
93 actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]
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)
99 out = datapath.ofproto_parser.OFPPacketOut(
100 datapath=datapath, buffer_id=msg.buffer_id, in_port=msg.in_port,
102 datapath.send_msg(out)
104 @set_ev_cls(lacplib.EventSlaveStateChanged, MAIN_DISPATCHER)
105 def _slave_state_changed_handler(self, ev):
106 datapath = ev.datapath
110 self.logger.info("slave state changed port: %d enabled: %s",
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, {})