1 # Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
17 An OpenFlow 1.0 L2 learning switch implementation.
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
32 class SimpleSwitch(app_manager.RyuApp):
33 OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]
35 def __init__(self, *args, **kwargs):
36 super(SimpleSwitch, self).__init__(*args, **kwargs)
39 def add_flow(self, datapath, in_port, dst, src, actions):
40 ofproto = datapath.ofproto
42 match = datapath.ofproto_parser.OFPMatch(
44 dl_dst=haddr_to_bin(dst), dl_src=haddr_to_bin(src))
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)
53 @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
54 def _packet_in_handler(self, ev):
56 datapath = msg.datapath
57 ofproto = datapath.ofproto
59 pkt = packet.Packet(msg.data)
60 eth = pkt.get_protocol(ethernet.ethernet)
62 if eth.ethertype == ether_types.ETH_TYPE_LLDP:
69 self.mac_to_port.setdefault(dpid, {})
71 self.logger.info("packet in %s %s %s %s", dpid, src, dst, msg.in_port)
73 # learn a mac address to avoid FLOOD next time.
74 self.mac_to_port[dpid][src] = msg.in_port
76 if dst in self.mac_to_port[dpid]:
77 out_port = self.mac_to_port[dpid][dst]
79 out_port = ofproto.OFPP_FLOOD
81 actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]
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)
88 if msg.buffer_id == ofproto.OFP_NO_BUFFER:
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)
96 @set_ev_cls(ofp_event.EventOFPPortStatus, MAIN_DISPATCHER)
97 def _port_status_handler(self, ev):
100 port_no = msg.desc.port_no
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)
110 self.logger.info("Illeagal port state %s %s", port_no, reason)