backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / app / simple_switch_igmp.py
1 # Copyright (C) 2013 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 import struct
17
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
25
26
27 class SimpleSwitchIgmp(app_manager.RyuApp):
28     OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]
29     _CONTEXTS = {'igmplib': igmplib.IgmpLib}
30
31     def __init__(self, *args, **kwargs):
32         super(SimpleSwitchIgmp, self).__init__(*args, **kwargs)
33         self.mac_to_port = {}
34         self._snoop = kwargs['igmplib']
35         # if you want a switch to operate as a querier,
36         # set up as follows:
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
41         #              server.
42         #
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.
46
47     def add_flow(self, datapath, in_port, dst, actions):
48         ofproto = datapath.ofproto
49         parser = datapath.ofproto_parser
50
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)
57
58     @set_ev_cls(igmplib.EventPacketIn, MAIN_DISPATCHER)
59     def _packet_in_handler(self, ev):
60         msg = ev.msg
61         datapath = msg.datapath
62         ofproto = datapath.ofproto
63
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_)
68
69         dpid = datapath.id
70         self.mac_to_port.setdefault(dpid, {})
71
72         self.logger.info("packet in %s %s %s %s",
73                          dpid, src, dst, msg.in_port)
74
75         # learn a mac address to avoid FLOOD next time.
76         self.mac_to_port[dpid][src] = msg.in_port
77
78         if dst in self.mac_to_port[dpid]:
79             out_port = self.mac_to_port[dpid][dst]
80         else:
81             out_port = ofproto.OFPP_FLOOD
82
83         actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]
84
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)
88
89         out = datapath.ofproto_parser.OFPPacketOut(
90             datapath=datapath, buffer_id=msg.buffer_id, in_port=msg.in_port,
91             actions=actions)
92         datapath.send_msg(out)
93
94     @set_ev_cls(igmplib.EventMulticastGroupStateChanged,
95                 MAIN_DISPATCHER)
96     def _status_changed(self, ev):
97         msg = {
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',
101         }
102         self.logger.info("%s: [%s] querier:[%s] hosts:%s",
103                          msg.get(ev.reason), ev.address, ev.src,
104                          ev.dsts)