I'm not sure we should have this in the tree but some asked me. = >From a897a6311b1fee1f436cd1f7cc0c331e7737392e Mon Sep 17 00:00:00 2001 From: FUJITA Tomonori <[email protected]> Date: Sun, 20 Jan 2013 21:54:08 -0800 Subject: [PATCH] add simple_switch_v1_2
This is the simplest example, learning switch application, for OF 1.2. Signed-off-by: FUJITA Tomonori <[email protected]> --- ryu/app/simple_switch_v1_2.py | 105 +++++++++++++++++++++++++++++++++++++++++ 1 files changed, 105 insertions(+), 0 deletions(-) create mode 100644 ryu/app/simple_switch_v1_2.py diff --git a/ryu/app/simple_switch_v1_2.py b/ryu/app/simple_switch_v1_2.py new file mode 100644 index 0000000..c56b049 --- /dev/null +++ b/ryu/app/simple_switch_v1_2.py @@ -0,0 +1,105 @@ +# Copyright (C) 2011 Nippon Telegraph and Telephone Corporation. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import logging +import struct + +from ryu.base import app_manager +from ryu.controller import mac_to_port +from ryu.controller import ofp_event +from ryu.controller.handler import MAIN_DISPATCHER +from ryu.controller.handler import set_ev_cls +from ryu.ofproto import ofproto_v1_2 +from ryu.lib.mac import haddr_to_str + + +LOG = logging.getLogger('ryu.app.simple_switch') + + +class SimpleSwitch(app_manager.RyuApp): + OFP_VERSIONS = [ofproto_v1_2.OFP_VERSION] + + def __init__(self, *args, **kwargs): + super(SimpleSwitch, self).__init__(*args, **kwargs) + self.mac_to_port = {} + + def add_flow(self, dp, in_port, dst, actions): + ofproto = dp.ofproto + match = dp.ofproto_parser.OFPMatch() + match.set_in_port(in_port) + match.set_dl_dst(dst) + + inst = [dp.ofproto_parser.OFPInstructionActions( + ofproto_v1_2.OFPIT_APPLY_ACTIONS, actions)] + + mod = dp.ofproto_parser.OFPFlowMod(dp, 0, 0, 0, + ofproto.OFPFC_ADD, + 0, 0, 0, 0xffffffff, + ofproto.OFPP_ANY, 0xffffffff, + 0, match, inst) + dp.send_msg(mod) + + @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER) + def _packet_in_handler(self, ev): + msg = ev.msg + datapath = msg.datapath + ofproto = datapath.ofproto + + dst, src, _eth_type = struct.unpack_from('!6s6sH', buffer(msg.data), 0) + + dpid = datapath.id + self.mac_to_port.setdefault(dpid, {}) + + for field in msg.match.fields: + if field.header == ofproto_v1_2.OXM_OF_IN_PORT: + in_port = field.value + + LOG.info("packet in %s %s %s %s", + dpid, haddr_to_str(src), haddr_to_str(dst), in_port) + + # learn a mac address to avoid FLOOD next time. + self.mac_to_port[dpid][src] = in_port + + if dst in self.mac_to_port[dpid]: + out_port = self.mac_to_port[dpid][dst] + else: + out_port = ofproto.OFPP_FLOOD + + actions = [datapath.ofproto_parser.OFPActionOutput(out_port, 0)] + + # install a flow to avoid packet_in next time + if out_port != ofproto.OFPP_FLOOD: + self.add_flow(datapath, in_port, dst, actions) + + out = datapath.ofproto_parser.OFPPacketOut( + datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port, + actions=actions) + datapath.send_msg(out) + + @set_ev_cls(ofp_event.EventOFPPortStatus, MAIN_DISPATCHER) + def _port_status_handler(self, ev): + msg = ev.msg + reason = msg.reason + port_no = msg.desc.port_no + + ofproto = msg.datapath.ofproto + if reason == ofproto.OFPPR_ADD: + LOG.info("port added %s", port_no) + elif reason == ofproto.OFPPR_DELETE: + LOG.info("port deleted %s", port_no) + elif reason == ofproto.OFPPR_MODIFY: + LOG.info("port modified %s", port_no) + else: + LOG.info("Illeagal port state %s %s", port_no, reason) -- 1.7.4.4 ------------------------------------------------------------------------------ Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS, MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft MVPs and experts. SALE $99.99 this month only -- learn more at: http://p.sf.net/sfu/learnmore_122412 _______________________________________________ Ryu-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/ryu-devel
