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 igmplib
24 from ryu.lib.dpid import str_to_dpid
27 class SimpleSwitchIgmp(app_manager.RyuApp):
28 OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]
29 _CONTEXTS = {'igmplib': igmplib.IgmpLib}
31 def __init__(self, *args, **kwargs):
32 super(SimpleSwitchIgmp, self).__init__(*args, **kwargs)
34 self._snoop = kwargs['igmplib']
35 # if you want a switch to operate as a querier,
37 self._snoop.set_querier_mode(
38 dpid=str_to_dpid('0000000000000001'), server_port=2)
39 # dpid the datapath id that will operate as a querier.
40 # server_port a port number which connect to the multicast
43 # NOTE: you can set up only the one querier.
44 # when you called this method several times,
45 # only the last one becomes effective.
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 @set_ev_cls(igmplib.EventPacketIn, MAIN_DISPATCHER)
59 def _packet_in_handler(self, ev):
61 datapath = msg.datapath
62 ofproto = datapath.ofproto
64 (dst_, src_, _eth_type) = struct.unpack_from(
65 '!6s6sH', buffer(msg.data), 0)
66 src = addrconv.mac.bin_to_text(src_)
67 dst = addrconv.mac.bin_to_text(dst_)
70 self.mac_to_port.setdefault(dpid, {})
72 self.logger.info("packet in %s %s %s %s",
73 dpid, src, dst, msg.in_port)
75 # learn a mac address to avoid FLOOD next time.
76 self.mac_to_port[dpid][src] = msg.in_port
78 if dst in self.mac_to_port[dpid]:
79 out_port = self.mac_to_port[dpid][dst]
81 out_port = ofproto.OFPP_FLOOD
83 actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]
85 # install a flow to avoid packet_in next time
86 if out_port != ofproto.OFPP_FLOOD:
87 self.add_flow(datapath, msg.in_port, dst, actions)
89 out = datapath.ofproto_parser.OFPPacketOut(
90 datapath=datapath, buffer_id=msg.buffer_id, in_port=msg.in_port,
92 datapath.send_msg(out)
94 @set_ev_cls(igmplib.EventMulticastGroupStateChanged,
96 def _status_changed(self, ev):
98 igmplib.MG_GROUP_ADDED: 'Multicast Group Added',
99 igmplib.MG_MEMBER_CHANGED: 'Multicast Group Member Changed',
100 igmplib.MG_GROUP_REMOVED: 'Multicast Group Removed',
102 self.logger.info("%s: [%s] querier:[%s] hosts:%s",
103 msg.get(ev.reason), ev.address, ev.src,