backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / app / example_switch_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, MAIN_DISPATCHER
19 from ryu.controller.handler import set_ev_cls
20 from ryu.ofproto import ofproto_v1_3
21 from ryu.lib.packet import packet
22 from ryu.lib.packet import ethernet
23
24
25 class ExampleSwitch13(app_manager.RyuApp):
26     OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
27
28     def __init__(self, *args, **kwargs):
29         super(ExampleSwitch13, self).__init__(*args, **kwargs)
30         # initialize mac address table.
31         self.mac_to_port = {}
32
33     @set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
34     def switch_features_handler(self, ev):
35         datapath = ev.msg.datapath
36         ofproto = datapath.ofproto
37         parser = datapath.ofproto_parser
38
39         # install the table-miss flow entry.
40         match = parser.OFPMatch()
41         actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
42                                           ofproto.OFPCML_NO_BUFFER)]
43         self.add_flow(datapath, 0, match, actions)
44
45     def add_flow(self, datapath, priority, match, actions):
46         ofproto = datapath.ofproto
47         parser = datapath.ofproto_parser
48
49         # construct flow_mod message and send it.
50         inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
51                                              actions)]
52         mod = parser.OFPFlowMod(datapath=datapath, priority=priority,
53                                 match=match, instructions=inst)
54         datapath.send_msg(mod)
55
56     @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
57     def _packet_in_handler(self, ev):
58         msg = ev.msg
59         datapath = msg.datapath
60         ofproto = datapath.ofproto
61         parser = datapath.ofproto_parser
62
63         # get Datapath ID to identify OpenFlow switches.
64         dpid = datapath.id
65         self.mac_to_port.setdefault(dpid, {})
66
67         # analyse the received packets using the packet library.
68         pkt = packet.Packet(msg.data)
69         eth_pkt = pkt.get_protocol(ethernet.ethernet)
70         dst = eth_pkt.dst
71         src = eth_pkt.src
72
73         # get the received port number from packet_in message.
74         in_port = msg.match['in_port']
75
76         self.logger.info("packet in %s %s %s %s", dpid, src, dst, in_port)
77
78         # learn a mac address to avoid FLOOD next time.
79         self.mac_to_port[dpid][src] = in_port
80
81         # if the destination mac address is already learned,
82         # decide which port to output the packet, otherwise FLOOD.
83         if dst in self.mac_to_port[dpid]:
84             out_port = self.mac_to_port[dpid][dst]
85         else:
86             out_port = ofproto.OFPP_FLOOD
87
88         # construct action list.
89         actions = [parser.OFPActionOutput(out_port)]
90
91         # install a flow to avoid packet_in next time.
92         if out_port != ofproto.OFPP_FLOOD:
93             match = parser.OFPMatch(in_port=in_port, eth_dst=dst)
94             self.add_flow(datapath, 1, match, actions)
95
96         # construct packet_out message and send it.
97         out = parser.OFPPacketOut(datapath=datapath,
98                                   buffer_id=ofproto.OFP_NO_BUFFER,
99                                   in_port=in_port, actions=actions,
100                                   data=msg.data)
101         datapath.send_msg(out)