backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / app / simple_switch_igmp_13.py
1 # Copyright (C) 2016 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 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
27
28
29 class SimpleSwitchIgmp13(simple_switch_13.SimpleSwitch13):
30     OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
31     _CONTEXTS = {'igmplib': igmplib.IgmpLib}
32
33     def __init__(self, *args, **kwargs):
34         super(SimpleSwitchIgmp13, self).__init__(*args, **kwargs)
35         self.mac_to_port = {}
36         self._snoop = kwargs['igmplib']
37         self._snoop.set_querier_mode(
38             dpid=str_to_dpid('0000000000000001'), server_port=2)
39
40     @set_ev_cls(igmplib.EventPacketIn, MAIN_DISPATCHER)
41     def _packet_in_handler(self, ev):
42         msg = ev.msg
43         datapath = msg.datapath
44         ofproto = datapath.ofproto
45         parser = datapath.ofproto_parser
46         in_port = msg.match['in_port']
47
48         pkt = packet.Packet(msg.data)
49         eth = pkt.get_protocols(ethernet.ethernet)[0]
50
51         dst = eth.dst
52         src = eth.src
53
54         dpid = datapath.id
55         self.mac_to_port.setdefault(dpid, {})
56
57         self.logger.info("packet in %s %s %s %s", dpid, src, dst, in_port)
58
59         # learn a mac address to avoid FLOOD next time.
60         self.mac_to_port[dpid][src] = in_port
61
62         if dst in self.mac_to_port[dpid]:
63             out_port = self.mac_to_port[dpid][dst]
64         else:
65             out_port = ofproto.OFPP_FLOOD
66
67         actions = [parser.OFPActionOutput(out_port)]
68
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)
73
74         data = None
75         if msg.buffer_id == ofproto.OFP_NO_BUFFER:
76             data = msg.data
77
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)
81
82     @set_ev_cls(igmplib.EventMulticastGroupStateChanged,
83                 MAIN_DISPATCHER)
84     def _status_changed(self, ev):
85         msg = {
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',
89         }
90         self.logger.info("%s: [%s] querier:[%s] hosts:%s",
91                          msg.get(ev.reason), ev.address, ev.src,
92                          ev.dsts)