Created
March 16, 2015 09:22
-
-
Save haxelion/c7a929f7ed9188264efe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. | |
""" | |
An OpenFlow 1.0 L2 learning switch implementation. | |
""" | |
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 CONFIG_DISPATCHER, MAIN_DISPATCHER | |
from ryu.controller.handler import set_ev_cls | |
from ryu.ofproto import ofproto_v1_0, ether | |
from ryu.lib.mac import haddr_to_bin | |
from ryu.lib.packet import packet | |
from ryu.lib.packet import ethernet, ipv4, ipv6, tcp, udp | |
class SimpleSwitch(app_manager.RyuApp): | |
OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION] | |
def __init__(self, *args, **kwargs): | |
super(SimpleSwitch, self).__init__(*args, **kwargs) | |
self.mac_to_port = {} | |
def add_flow(self, datapath, in_port, dst, action): | |
self.logger.info('New host {} at port {}'.format(dst, in_port)) | |
ofproto = datapath.ofproto | |
parser = datapath.ofproto_parser | |
# Prioritise UDP port 1337 traffic | |
actions = [parser.OFPActionVlanPcp(7), | |
action] | |
match = parser.OFPMatch(in_port=in_port, | |
dl_type = 0x800, | |
nw_proto = 0x11, | |
tp_dst = 1337) | |
mod = parser.OFPFlowMod(datapath=datapath, | |
match=match, | |
cookie=0, | |
command=ofproto.OFPFC_ADD, | |
idle_timeout=20, | |
hard_timeout=0, | |
priority=100, | |
flags=ofproto.OFPFF_SEND_FLOW_REM, | |
actions=actions) | |
datapath.send_msg(mod) | |
# Normal traffic | |
actions = [parser.OFPActionVlanPcp(1), | |
action] | |
match = parser.OFPMatch(in_port=in_port, | |
dl_type = 0x800, | |
nw_proto = 0x11, | |
tp_dst = 4242) | |
#dl_type = 0x01) | |
#dl_dst=haddr_to_bin(dst)) | |
mod = parser.OFPFlowMod(datapath=datapath, | |
match=match, | |
cookie=0, | |
command=ofproto.OFPFC_ADD, | |
idle_timeout=20, | |
hard_timeout=0, | |
priority=50, | |
flags=ofproto.OFPFF_SEND_FLOW_REM, | |
actions=actions) | |
datapath.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 | |
parser = datapath.ofproto_parser | |
pkt = packet.Packet(msg.data) | |
eth = pkt.get_protocol(ethernet.ethernet) | |
dst = eth.dst | |
src = eth.src | |
dpid = datapath.id | |
self.mac_to_port.setdefault(dpid, {}) | |
# learn a mac address to avoid FLOOD next time. | |
self.mac_to_port[dpid][src] = msg.in_port | |
if dst in self.mac_to_port[dpid]: | |
out_port = self.mac_to_port[dpid][dst] | |
else: | |
out_port = ofproto.OFPP_FLOOD | |
action = parser.OFPActionOutput(out_port) | |
# install a flow to avoid packet_in next time | |
if out_port != ofproto.OFPP_FLOOD: | |
self.add_flow(datapath, msg.in_port, dst, action) | |
data = None | |
if msg.buffer_id == ofproto.OFP_NO_BUFFER: | |
data = msg.data | |
out = datapath.ofproto_parser.OFPPacketOut( | |
datapath=datapath, buffer_id=msg.buffer_id, in_port=msg.in_port, | |
actions=[action], data=data) | |
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: | |
self.logger.info("port added %s", port_no) | |
elif reason == ofproto.OFPPR_DELETE: | |
self.logger.info("port deleted %s", port_no) | |
elif reason == ofproto.OFPPR_MODIFY: | |
self.logger.info("port modified %s", port_no) | |
else: | |
self.logger.info("Illeagal port state %s %s", port_no, reason) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment