backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / app / simple_switch.py
1 # Copyright (C) 2011 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 """
17 An OpenFlow 1.0 L2 learning switch implementation.
18 """
19
20
21 from ryu.base import app_manager
22 from ryu.controller import ofp_event
23 from ryu.controller.handler import MAIN_DISPATCHER
24 from ryu.controller.handler import set_ev_cls
25 from ryu.ofproto import ofproto_v1_0
26 from ryu.lib.mac import haddr_to_bin
27 from ryu.lib.packet import packet
28 from ryu.lib.packet import ethernet
29 from ryu.lib.packet import ether_types
30
31
32 class SimpleSwitch(app_manager.RyuApp):
33     OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]
34
35     def __init__(self, *args, **kwargs):
36         super(SimpleSwitch, self).__init__(*args, **kwargs)
37         self.mac_to_port = {}
38
39     def add_flow(self, datapath, in_port, dst, src, actions):
40         ofproto = datapath.ofproto
41
42         match = datapath.ofproto_parser.OFPMatch(
43             in_port=in_port,
44             dl_dst=haddr_to_bin(dst), dl_src=haddr_to_bin(src))
45
46         mod = datapath.ofproto_parser.OFPFlowMod(
47             datapath=datapath, match=match, cookie=0,
48             command=ofproto.OFPFC_ADD, idle_timeout=0, hard_timeout=0,
49             priority=ofproto.OFP_DEFAULT_PRIORITY,
50             flags=ofproto.OFPFF_SEND_FLOW_REM, actions=actions)
51         datapath.send_msg(mod)
52
53     @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
54     def _packet_in_handler(self, ev):
55         msg = ev.msg
56         datapath = msg.datapath
57         ofproto = datapath.ofproto
58
59         pkt = packet.Packet(msg.data)
60         eth = pkt.get_protocol(ethernet.ethernet)
61
62         if eth.ethertype == ether_types.ETH_TYPE_LLDP:
63             # ignore lldp packet
64             return
65         dst = eth.dst
66         src = eth.src
67
68         dpid = datapath.id
69         self.mac_to_port.setdefault(dpid, {})
70
71         self.logger.info("packet in %s %s %s %s", dpid, src, dst, msg.in_port)
72
73         # learn a mac address to avoid FLOOD next time.
74         self.mac_to_port[dpid][src] = msg.in_port
75
76         if dst in self.mac_to_port[dpid]:
77             out_port = self.mac_to_port[dpid][dst]
78         else:
79             out_port = ofproto.OFPP_FLOOD
80
81         actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]
82
83         # install a flow to avoid packet_in next time
84         if out_port != ofproto.OFPP_FLOOD:
85             self.add_flow(datapath, msg.in_port, dst, src, actions)
86
87         data = None
88         if msg.buffer_id == ofproto.OFP_NO_BUFFER:
89             data = msg.data
90
91         out = datapath.ofproto_parser.OFPPacketOut(
92             datapath=datapath, buffer_id=msg.buffer_id, in_port=msg.in_port,
93             actions=actions, data=data)
94         datapath.send_msg(out)
95
96     @set_ev_cls(ofp_event.EventOFPPortStatus, MAIN_DISPATCHER)
97     def _port_status_handler(self, ev):
98         msg = ev.msg
99         reason = msg.reason
100         port_no = msg.desc.port_no
101
102         ofproto = msg.datapath.ofproto
103         if reason == ofproto.OFPPR_ADD:
104             self.logger.info("port added %s", port_no)
105         elif reason == ofproto.OFPPR_DELETE:
106             self.logger.info("port deleted %s", port_no)
107         elif reason == ofproto.OFPPR_MODIFY:
108             self.logger.info("port modified %s", port_no)
109         else:
110             self.logger.info("Illeagal port state %s %s", port_no, reason)