ce2f544885e124d8ebd0fa9304ce26d4612c4b59
[osm/vim-emu.git] / src / emuvim / dcemulator / son_emu_simple_switch_13.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 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 from ryu.lib.packet import ether_types
24 from ryu.topology.event import EventSwitchEnter, EventSwitchLeave, EventSwitchReconnected
25
26 class SimpleSwitch13(app_manager.RyuApp):
27 OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
28
29 def __init__(self, *args, **kwargs):
30 super(SimpleSwitch13, self).__init__(*args, **kwargs)
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 table-miss flow entry
40 #
41 # We specify NO BUFFER to max_len of the output action due to
42 # OVS bug. At this moment, if we specify a lesser number, e.g.,
43 # 128, OVS will send Packet-In with invalid buffer_id and
44 # truncated packet data. In that case, we cannot output packets
45 # correctly. The bug has been fixed in OVS v2.1.0.
46 match = parser.OFPMatch()
47 actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
48 ofproto.OFPCML_NO_BUFFER)]
49 self.add_flow(datapath, 0, match, actions)
50
51 def add_flow(self, datapath, priority, match, actions, buffer_id=None):
52 ofproto = datapath.ofproto
53 parser = datapath.ofproto_parser
54
55 inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
56 actions)]
57 if buffer_id:
58 mod = parser.OFPFlowMod(datapath=datapath, buffer_id=buffer_id,
59 priority=priority, match=match,
60 instructions=inst)
61 else:
62 mod = parser.OFPFlowMod(datapath=datapath, priority=priority,
63 match=match, instructions=inst)
64 datapath.send_msg(mod)
65
66 # new switch detected
67
68 @set_ev_cls([EventSwitchEnter, EventSwitchReconnected])
69 def _ev_switch_enter_handler(self, ev):
70 datapath = ev.switch.dp
71 self.logger.info('registered OF switch id: %s' % datapath.id)
72 ofproto = datapath.ofproto
73 self.logger.info('OF version: {0}'.format(ofproto))
74 # send NORMAL action for all undefined flows
75 ofp_parser = datapath.ofproto_parser
76 actions = [ofp_parser.OFPActionOutput(ofproto_v1_3.OFPP_NORMAL)]
77 self.add_flow(datapath, 0, None, actions)
78
79
80 @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
81 def _packet_in_handler(self, ev):
82 # If you hit this you might want to increase
83 # the "miss_send_length" of your switch
84 if ev.msg.msg_len < ev.msg.total_len:
85 self.logger.debug("packet truncated: only %s of %s bytes",
86 ev.msg.msg_len, ev.msg.total_len)
87 msg = ev.msg
88 datapath = msg.datapath
89 ofproto = datapath.ofproto
90 parser = datapath.ofproto_parser
91 in_port = msg.match['in_port']
92
93 pkt = packet.Packet(msg.data)
94 eth = pkt.get_protocols(ethernet.ethernet)[0]
95
96 if eth.ethertype == ether_types.ETH_TYPE_LLDP:
97 # ignore lldp packet
98 return
99 dst = eth.dst
100 src = eth.src
101
102 dpid = datapath.id
103 self.mac_to_port.setdefault(dpid, {})
104
105 self.logger.info("packet in %s %s %s %s", dpid, src, dst, in_port)
106
107 # learn a mac address to avoid FLOOD next time.
108 self.mac_to_port[dpid][src] = in_port
109
110 if dst in self.mac_to_port[dpid]:
111 out_port = self.mac_to_port[dpid][dst]
112 else:
113 out_port = ofproto.OFPP_FLOOD
114
115 actions = [parser.OFPActionOutput(out_port)]
116
117 # install a flow to avoid packet_in next time
118 if out_port != ofproto.OFPP_FLOOD:
119 match = parser.OFPMatch(in_port=in_port, eth_dst=dst)
120 # verify if we have a valid buffer_id, if yes avoid to send both
121 # flow_mod & packet_out
122 if msg.buffer_id != ofproto.OFP_NO_BUFFER:
123 self.add_flow(datapath, 1, match, actions, msg.buffer_id)
124 return
125 else:
126 self.add_flow(datapath, 1, match, actions)
127 data = None
128 if msg.buffer_id == ofproto.OFP_NO_BUFFER:
129 data = msg.data
130
131 out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id,
132 in_port=in_port, actions=actions, data=data)
133 datapath.send_msg(out)