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
19 from ryu.controller.handler import MAIN_DISPATCHER
20 from ryu.controller.handler import set_ev_cls
21 from ryu.ofproto import ofproto_v1_3
22 from ryu.lib import lacplib
23 from ryu.lib.dpid import str_to_dpid
24 from ryu.lib.packet import packet
25 from ryu.lib.packet import ethernet
26 from ryu.app import simple_switch_13
29 class SimpleSwitchLacp13(simple_switch_13.SimpleSwitch13):
30 OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
31 _CONTEXTS = {'lacplib': lacplib.LacpLib}
33 def __init__(self, *args, **kwargs):
34 super(SimpleSwitchLacp13, self).__init__(*args, **kwargs)
36 self._lacp = kwargs['lacplib']
38 dpid=str_to_dpid('0000000000000001'), ports=[1, 2])
40 def del_flow(self, datapath, match):
41 ofproto = datapath.ofproto
42 parser = datapath.ofproto_parser
44 mod = parser.OFPFlowMod(datapath=datapath,
45 command=ofproto.OFPFC_DELETE,
46 out_port=ofproto.OFPP_ANY,
47 out_group=ofproto.OFPG_ANY,
49 datapath.send_msg(mod)
51 @set_ev_cls(lacplib.EventPacketIn, MAIN_DISPATCHER)
52 def _packet_in_handler(self, ev):
54 datapath = msg.datapath
55 ofproto = datapath.ofproto
56 parser = datapath.ofproto_parser
57 in_port = msg.match['in_port']
59 pkt = packet.Packet(msg.data)
60 eth = pkt.get_protocols(ethernet.ethernet)[0]
66 self.mac_to_port.setdefault(dpid, {})
68 self.logger.info("packet in %s %s %s %s", dpid, src, dst, in_port)
70 # learn a mac address to avoid FLOOD next time.
71 self.mac_to_port[dpid][src] = in_port
73 if dst in self.mac_to_port[dpid]:
74 out_port = self.mac_to_port[dpid][dst]
76 out_port = ofproto.OFPP_FLOOD
78 actions = [parser.OFPActionOutput(out_port)]
80 # install a flow to avoid packet_in next time
81 if out_port != ofproto.OFPP_FLOOD:
82 match = parser.OFPMatch(in_port=in_port, eth_dst=dst)
83 self.add_flow(datapath, 1, match, actions)
86 if msg.buffer_id == ofproto.OFP_NO_BUFFER:
89 out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id,
90 in_port=in_port, actions=actions, data=data)
91 datapath.send_msg(out)
93 @set_ev_cls(lacplib.EventSlaveStateChanged, MAIN_DISPATCHER)
94 def _slave_state_changed_handler(self, ev):
95 datapath = ev.datapath
99 self.logger.info("slave state changed port: %d enabled: %s",
101 if dpid in self.mac_to_port:
102 for mac in self.mac_to_port[dpid]:
103 match = datapath.ofproto_parser.OFPMatch(eth_dst=mac)
104 self.del_flow(datapath, match)
105 del self.mac_to_port[dpid]
106 self.mac_to_port.setdefault(dpid, {})