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 igmplib
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 SimpleSwitchIgmp13(simple_switch_13.SimpleSwitch13):
30 OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
31 _CONTEXTS = {'igmplib': igmplib.IgmpLib}
33 def __init__(self, *args, **kwargs):
34 super(SimpleSwitchIgmp13, self).__init__(*args, **kwargs)
36 self._snoop = kwargs['igmplib']
37 self._snoop.set_querier_mode(
38 dpid=str_to_dpid('0000000000000001'), server_port=2)
40 @set_ev_cls(igmplib.EventPacketIn, MAIN_DISPATCHER)
41 def _packet_in_handler(self, ev):
43 datapath = msg.datapath
44 ofproto = datapath.ofproto
45 parser = datapath.ofproto_parser
46 in_port = msg.match['in_port']
48 pkt = packet.Packet(msg.data)
49 eth = pkt.get_protocols(ethernet.ethernet)[0]
55 self.mac_to_port.setdefault(dpid, {})
57 self.logger.info("packet in %s %s %s %s", dpid, src, dst, in_port)
59 # learn a mac address to avoid FLOOD next time.
60 self.mac_to_port[dpid][src] = in_port
62 if dst in self.mac_to_port[dpid]:
63 out_port = self.mac_to_port[dpid][dst]
65 out_port = ofproto.OFPP_FLOOD
67 actions = [parser.OFPActionOutput(out_port)]
69 # install a flow to avoid packet_in next time
70 if out_port != ofproto.OFPP_FLOOD:
71 match = parser.OFPMatch(in_port=in_port, eth_dst=dst)
72 self.add_flow(datapath, 1, match, actions)
75 if msg.buffer_id == ofproto.OFP_NO_BUFFER:
78 out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id,
79 in_port=in_port, actions=actions, data=data)
80 datapath.send_msg(out)
82 @set_ev_cls(igmplib.EventMulticastGroupStateChanged,
84 def _status_changed(self, ev):
86 igmplib.MG_GROUP_ADDED: 'Multicast Group Added',
87 igmplib.MG_MEMBER_CHANGED: 'Multicast Group Member Changed',
88 igmplib.MG_GROUP_REMOVED: 'Multicast Group Removed',
90 self.logger.info("%s: [%s] querier:[%s] hosts:%s",
91 msg.get(ev.reason), ev.address, ev.src,