os-ken/ryu/app/simple_switch.py
FUJITA Tomonori d9856db46f discard sample/test_simple_switch because of unittest fail
unittest crashes on commit e5e4b8442:

ERROR: testInit (ryu.tests.unit.sample.test_simple_switch.TestSimpleSwitch)

Traceback (most recent call last):
  File "/var/lib/jenkins/jobs/ryu-test-upstream/workspace/ryu/tests/unit/sample/test_simple_switch.py", line 20, in testInit
    ss = SimpleSwitch()
  File "/var/lib/jenkins/jobs/ryu-test-upstream/workspace/ryu/app/simple_switch.py", line 44, in __init__
    self.mac2port = kwargs['mac2port']
KeyError: 'mac2port'

The simple_switch unittest doesn't test anything anyway so let's
remove it for now.

This also fixes a pip8 error of simple_switch.py that the commit
e5e4b8442 added.

Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
2012-06-02 05:20:12 +09:00

98 lines
3.3 KiB
Python

# 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 nx_match
from ryu.lib.mac import haddr_to_str
LOG = logging.getLogger('ryu.app.simple_switch')
# TODO: we should split the handler into two parts, protocol
# independent and dependant parts.
# TODO: can we use dpkt python library?
# TODO: we need to move the followings to something like db
class SimpleSwitch(app_manager.RyuApp):
_CONTEXTS = {
'mac2port': mac_to_port.MacToPortTable,
}
def __init__(self, *args, **kwargs):
super(SimpleSwitch, self).__init__(*args, **kwargs)
self.mac2port = kwargs['mac2port']
@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.mac2port.dpid_add(dpid)
LOG.info("packet in %s %s %s %s",
dpid, haddr_to_str(src), haddr_to_str(dst), msg.in_port)
self.mac2port.port_add(dpid, msg.in_port, src)
out_port = self.mac2port.port_get(dpid, dst)
if out_port == None:
LOG.info("out_port not found")
out_port = ofproto.OFPP_FLOOD
actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]
if out_port != ofproto.OFPP_FLOOD:
rule = nx_match.ClsRule()
rule.set_in_port(msg.in_port)
rule.set_dl_dst(dst)
rule.set_dl_type(nx_match.ETH_TYPE_IP)
rule.set_nw_dscp(0)
datapath.send_flow_mod(
rule=rule, cookie=0, command=ofproto.OFPFC_ADD,
idle_timeout=0, hard_timeout=0,
priority=ofproto.OFP_DEFAULT_PRIORITY,
flags=ofproto.OFPFF_SEND_FLOW_REM, actions=actions)
datapath.send_packet_out(msg.buffer_id, msg.in_port, actions)
@set_ev_cls(ofp_event.EventOFPPortStatus, MAIN_DISPATCHER)
def _port_status_handler(self, ev):
msg = ev.msg
reason = msg.reason
port_no = msg.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)