diff options
author | Shinpei Muraoka <shinpei.muraoka@gmail.com> | 2016-09-20 13:49:18 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2016-10-01 21:22:00 +0900 |
commit | 57056d1a0f614b3a58734aeb70baecfb4621d85a (patch) | |
tree | e8060bcd0df4f45db2655dcd03c2d00ec193fbe8 | |
parent | b302d725a00f992ae74e12c4db355f04774ac700 (diff) |
app: Integrate aplication of Ryu-book
Henceforth, Ryu-Book includes the source files for application of Ryu.
This patch Integrates the source files for application of Ryu-Book.
Just for information, the source files for application of Ryu-Book
will Integrate as for OpenFlow1.3.
Signed-off-by: Shinpei Muraoka <shinpei.muraoka@gmail.com>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r-- | ryu/app/simple_monitor_13.py | 95 | ||||
-rw-r--r-- | ryu/app/simple_switch_igmp_13.py | 92 | ||||
-rw-r--r-- | ryu/app/simple_switch_lacp_13.py | 106 | ||||
-rw-r--r-- | ryu/app/simple_switch_rest_13.py | 114 | ||||
-rw-r--r-- | ryu/app/simple_switch_stp_13.py | 121 |
5 files changed, 528 insertions, 0 deletions
diff --git a/ryu/app/simple_monitor_13.py b/ryu/app/simple_monitor_13.py new file mode 100644 index 00000000..630828b4 --- /dev/null +++ b/ryu/app/simple_monitor_13.py @@ -0,0 +1,95 @@ +# Copyright (C) 2016 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. + +from operator import attrgetter + +from ryu.app import simple_switch_13 +from ryu.controller import ofp_event +from ryu.controller.handler import MAIN_DISPATCHER, DEAD_DISPATCHER +from ryu.controller.handler import set_ev_cls +from ryu.lib import hub + + +class SimpleMonitor13(simple_switch_13.SimpleSwitch13): + + def __init__(self, *args, **kwargs): + super(SimpleMonitor13, self).__init__(*args, **kwargs) + self.datapaths = {} + self.monitor_thread = hub.spawn(self._monitor) + + @set_ev_cls(ofp_event.EventOFPStateChange, + [MAIN_DISPATCHER, DEAD_DISPATCHER]) + def _state_change_handler(self, ev): + datapath = ev.datapath + if ev.state == MAIN_DISPATCHER: + if datapath.id not in self.datapaths: + self.logger.debug('register datapath: %016x', datapath.id) + self.datapaths[datapath.id] = datapath + elif ev.state == DEAD_DISPATCHER: + if datapath.id in self.datapaths: + self.logger.debug('unregister datapath: %016x', datapath.id) + del self.datapaths[datapath.id] + + def _monitor(self): + while True: + for dp in self.datapaths.values(): + self._request_stats(dp) + hub.sleep(10) + + def _request_stats(self, datapath): + self.logger.debug('send stats request: %016x', datapath.id) + ofproto = datapath.ofproto + parser = datapath.ofproto_parser + + req = parser.OFPFlowStatsRequest(datapath) + datapath.send_msg(req) + + req = parser.OFPPortStatsRequest(datapath, 0, ofproto.OFPP_ANY) + datapath.send_msg(req) + + @set_ev_cls(ofp_event.EventOFPFlowStatsReply, MAIN_DISPATCHER) + def _flow_stats_reply_handler(self, ev): + body = ev.msg.body + + self.logger.info('datapath ' + 'in-port eth-dst ' + 'out-port packets bytes') + self.logger.info('---------------- ' + '-------- ----------------- ' + '-------- -------- --------') + for stat in sorted([flow for flow in body if flow.priority == 1], + key=lambda flow: (flow.match['in_port'], + flow.match['eth_dst'])): + self.logger.info('%016x %8x %17s %8x %8d %8d', + ev.msg.datapath.id, + stat.match['in_port'], stat.match['eth_dst'], + stat.instructions[0].actions[0].port, + stat.packet_count, stat.byte_count) + + @set_ev_cls(ofp_event.EventOFPPortStatsReply, MAIN_DISPATCHER) + def _port_stats_reply_handler(self, ev): + body = ev.msg.body + + self.logger.info('datapath port ' + 'rx-pkts rx-bytes rx-error ' + 'tx-pkts tx-bytes tx-error') + self.logger.info('---------------- -------- ' + '-------- -------- -------- ' + '-------- -------- --------') + for stat in sorted(body, key=attrgetter('port_no')): + self.logger.info('%016x %8x %8d %8d %8d %8d %8d %8d', + ev.msg.datapath.id, stat.port_no, + stat.rx_packets, stat.rx_bytes, stat.rx_errors, + stat.tx_packets, stat.tx_bytes, stat.tx_errors) diff --git a/ryu/app/simple_switch_igmp_13.py b/ryu/app/simple_switch_igmp_13.py new file mode 100644 index 00000000..4d119fb5 --- /dev/null +++ b/ryu/app/simple_switch_igmp_13.py @@ -0,0 +1,92 @@ +# Copyright (C) 2016 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. + +from ryu.base import app_manager +from ryu.controller import ofp_event +from ryu.controller.handler import CONFIG_DISPATCHER +from ryu.controller.handler import MAIN_DISPATCHER +from ryu.controller.handler import set_ev_cls +from ryu.ofproto import ofproto_v1_3 +from ryu.lib import igmplib +from ryu.lib.dpid import str_to_dpid +from ryu.lib.packet import packet +from ryu.lib.packet import ethernet +from ryu.app import simple_switch_13 + + +class SimpleSwitchIgmp13(simple_switch_13.SimpleSwitch13): + OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION] + _CONTEXTS = {'igmplib': igmplib.IgmpLib} + + def __init__(self, *args, **kwargs): + super(SimpleSwitchIgmp13, self).__init__(*args, **kwargs) + self.mac_to_port = {} + self._snoop = kwargs['igmplib'] + self._snoop.set_querier_mode( + dpid=str_to_dpid('0000000000000001'), server_port=2) + + @set_ev_cls(igmplib.EventPacketIn, MAIN_DISPATCHER) + def _packet_in_handler(self, ev): + msg = ev.msg + datapath = msg.datapath + ofproto = datapath.ofproto + parser = datapath.ofproto_parser + in_port = msg.match['in_port'] + + pkt = packet.Packet(msg.data) + eth = pkt.get_protocols(ethernet.ethernet)[0] + + dst = eth.dst + src = eth.src + + dpid = datapath.id + self.mac_to_port.setdefault(dpid, {}) + + self.logger.info("packet in %s %s %s %s", dpid, src, 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 = [parser.OFPActionOutput(out_port)] + + # install a flow to avoid packet_in next time + if out_port != ofproto.OFPP_FLOOD: + match = parser.OFPMatch(in_port=in_port, eth_dst=dst) + self.add_flow(datapath, 1, match, actions) + + data = None + if msg.buffer_id == ofproto.OFP_NO_BUFFER: + data = msg.data + + out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id, + in_port=in_port, actions=actions, data=data) + datapath.send_msg(out) + + @set_ev_cls(igmplib.EventMulticastGroupStateChanged, + MAIN_DISPATCHER) + def _status_changed(self, ev): + msg = { + igmplib.MG_GROUP_ADDED: 'Multicast Group Added', + igmplib.MG_MEMBER_CHANGED: 'Multicast Group Member Changed', + igmplib.MG_GROUP_REMOVED: 'Multicast Group Removed', + } + self.logger.info("%s: [%s] querier:[%s] hosts:%s", + msg.get(ev.reason), ev.address, ev.src, + ev.dsts) diff --git a/ryu/app/simple_switch_lacp_13.py b/ryu/app/simple_switch_lacp_13.py new file mode 100644 index 00000000..a3907392 --- /dev/null +++ b/ryu/app/simple_switch_lacp_13.py @@ -0,0 +1,106 @@ +# Copyright (C) 2016 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. + +from ryu.base import app_manager +from ryu.controller import ofp_event +from ryu.controller.handler import CONFIG_DISPATCHER +from ryu.controller.handler import MAIN_DISPATCHER +from ryu.controller.handler import set_ev_cls +from ryu.ofproto import ofproto_v1_3 +from ryu.lib import lacplib +from ryu.lib.dpid import str_to_dpid +from ryu.lib.packet import packet +from ryu.lib.packet import ethernet +from ryu.app import simple_switch_13 + + +class SimpleSwitchLacp13(simple_switch_13.SimpleSwitch13): + OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION] + _CONTEXTS = {'lacplib': lacplib.LacpLib} + + def __init__(self, *args, **kwargs): + super(SimpleSwitchLacp13, self).__init__(*args, **kwargs) + self.mac_to_port = {} + self._lacp = kwargs['lacplib'] + self._lacp.add( + dpid=str_to_dpid('0000000000000001'), ports=[1, 2]) + + def del_flow(self, datapath, match): + ofproto = datapath.ofproto + parser = datapath.ofproto_parser + + mod = parser.OFPFlowMod(datapath=datapath, + command=ofproto.OFPFC_DELETE, + out_port=ofproto.OFPP_ANY, + out_group=ofproto.OFPG_ANY, + match=match) + datapath.send_msg(mod) + + @set_ev_cls(lacplib.EventPacketIn, MAIN_DISPATCHER) + def _packet_in_handler(self, ev): + msg = ev.msg + datapath = msg.datapath + ofproto = datapath.ofproto + parser = datapath.ofproto_parser + in_port = msg.match['in_port'] + + pkt = packet.Packet(msg.data) + eth = pkt.get_protocols(ethernet.ethernet)[0] + + dst = eth.dst + src = eth.src + + dpid = datapath.id + self.mac_to_port.setdefault(dpid, {}) + + self.logger.info("packet in %s %s %s %s", dpid, src, 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 = [parser.OFPActionOutput(out_port)] + + # install a flow to avoid packet_in next time + if out_port != ofproto.OFPP_FLOOD: + match = parser.OFPMatch(in_port=in_port, eth_dst=dst) + self.add_flow(datapath, 1, match, actions) + + data = None + if msg.buffer_id == ofproto.OFP_NO_BUFFER: + data = msg.data + + out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id, + in_port=in_port, actions=actions, data=data) + datapath.send_msg(out) + + @set_ev_cls(lacplib.EventSlaveStateChanged, MAIN_DISPATCHER) + def _slave_state_changed_handler(self, ev): + datapath = ev.datapath + dpid = datapath.id + port_no = ev.port + enabled = ev.enabled + self.logger.info("slave state changed port: %d enabled: %s", + port_no, enabled) + if dpid in self.mac_to_port: + for mac in self.mac_to_port[dpid]: + match = datapath.ofproto_parser.OFPMatch(eth_dst=mac) + self.del_flow(datapath, match) + del self.mac_to_port[dpid] + self.mac_to_port.setdefault(dpid, {}) diff --git a/ryu/app/simple_switch_rest_13.py b/ryu/app/simple_switch_rest_13.py new file mode 100644 index 00000000..c37ee09d --- /dev/null +++ b/ryu/app/simple_switch_rest_13.py @@ -0,0 +1,114 @@ +# Copyright (C) 2016 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 json + +from ryu.app import simple_switch_13 +from webob import Response +from ryu.controller import ofp_event +from ryu.controller.handler import CONFIG_DISPATCHER +from ryu.controller.handler import set_ev_cls +from ryu.app.wsgi import ControllerBase, WSGIApplication, route +from ryu.lib import dpid as dpid_lib + +simple_switch_instance_name = 'simple_switch_api_app' +url = '/simpleswitch/mactable/{dpid}' + + +class SimpleSwitchRest13(simple_switch_13.SimpleSwitch13): + + _CONTEXTS = {'wsgi': WSGIApplication} + + def __init__(self, *args, **kwargs): + super(SimpleSwitchRest13, self).__init__(*args, **kwargs) + self.switches = {} + wsgi = kwargs['wsgi'] + wsgi.register(SimpleSwitchController, + {simple_switch_instance_name: self}) + + @set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER) + def switch_features_handler(self, ev): + super(SimpleSwitchRest13, self).switch_features_handler(ev) + datapath = ev.msg.datapath + self.switches[datapath.id] = datapath + self.mac_to_port.setdefault(datapath.id, {}) + + def set_mac_to_port(self, dpid, entry): + mac_table = self.mac_to_port.setdefault(dpid, {}) + datapath = self.switches.get(dpid) + + entry_port = entry['port'] + entry_mac = entry['mac'] + + if datapath is not None: + parser = datapath.ofproto_parser + if entry_port not in mac_table.values(): + + for mac, port in mac_table.items(): + + # from known device to new device + actions = [parser.OFPActionOutput(entry_port)] + match = parser.OFPMatch(in_port=port, eth_dst=entry_mac) + self.add_flow(datapath, 1, match, actions) + + # from new device to known device + actions = [parser.OFPActionOutput(port)] + match = parser.OFPMatch(in_port=entry_port, eth_dst=mac) + self.add_flow(datapath, 1, match, actions) + + mac_table.update({entry_mac: entry_port}) + return mac_table + + +class SimpleSwitchController(ControllerBase): + + def __init__(self, req, link, data, **config): + super(SimpleSwitchController, self).__init__(req, link, data, **config) + self.simple_switch_app = data[simple_switch_instance_name] + + @route('simpleswitch', url, methods=['GET'], + requirements={'dpid': dpid_lib.DPID_PATTERN}) + def list_mac_table(self, req, **kwargs): + + simple_switch = self.simple_switch_app + dpid = dpid_lib.str_to_dpid(kwargs['dpid']) + + if dpid not in simple_switch.mac_to_port: + return Response(status=404) + + mac_table = simple_switch.mac_to_port.get(dpid, {}) + body = json.dumps(mac_table) + return Response(content_type='application/json', body=body) + + @route('simpleswitch', url, methods=['PUT'], + requirements={'dpid': dpid_lib.DPID_PATTERN}) + def put_mac_table(self, req, **kwargs): + + simple_switch = self.simple_switch_app + dpid = dpid_lib.str_to_dpid(kwargs['dpid']) + try: + new_entry = req.json if req.body else {} + except ValueError: + raise Response(status=400) + + if dpid not in simple_switch.mac_to_port: + return Response(status=404) + + try: + mac_table = simple_switch.set_mac_to_port(dpid, new_entry) + body = json.dumps(mac_table) + return Response(content_type='application/json', body=body) + except Exception as e: + return Response(status=500) diff --git a/ryu/app/simple_switch_stp_13.py b/ryu/app/simple_switch_stp_13.py new file mode 100644 index 00000000..97b2fd21 --- /dev/null +++ b/ryu/app/simple_switch_stp_13.py @@ -0,0 +1,121 @@ +# Copyright (C) 2016 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. + +from ryu.base import app_manager +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_3 +from ryu.lib import dpid as dpid_lib +from ryu.lib import stplib +from ryu.lib.packet import packet +from ryu.lib.packet import ethernet +from ryu.app import simple_switch_13 + + +class SimpleSwitch13(simple_switch_13.SimpleSwitch13): + OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION] + _CONTEXTS = {'stplib': stplib.Stp} + + def __init__(self, *args, **kwargs): + super(SimpleSwitch13, self).__init__(*args, **kwargs) + self.mac_to_port = {} + self.stp = kwargs['stplib'] + + # Sample of stplib config. + # please refer to stplib.Stp.set_config() for details. + config = {dpid_lib.str_to_dpid('0000000000000001'): + {'bridge': {'priority': 0x8000}}, + dpid_lib.str_to_dpid('0000000000000002'): + {'bridge': {'priority': 0x9000}}, + dpid_lib.str_to_dpid('0000000000000003'): + {'bridge': {'priority': 0xa000}}} + self.stp.set_config(config) + + def delete_flow(self, datapath): + ofproto = datapath.ofproto + parser = datapath.ofproto_parser + + for dst in self.mac_to_port[datapath.id].keys(): + match = parser.OFPMatch(eth_dst=dst) + mod = parser.OFPFlowMod( + datapath, command=ofproto.OFPFC_DELETE, + out_port=ofproto.OFPP_ANY, out_group=ofproto.OFPG_ANY, + priority=1, match=match) + datapath.send_msg(mod) + + @set_ev_cls(stplib.EventPacketIn, MAIN_DISPATCHER) + def _packet_in_handler(self, ev): + msg = ev.msg + datapath = msg.datapath + ofproto = datapath.ofproto + parser = datapath.ofproto_parser + in_port = msg.match['in_port'] + + pkt = packet.Packet(msg.data) + eth = pkt.get_protocols(ethernet.ethernet)[0] + + dst = eth.dst + src = eth.src + + dpid = datapath.id + self.mac_to_port.setdefault(dpid, {}) + + self.logger.info("packet in %s %s %s %s", dpid, src, 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 = [parser.OFPActionOutput(out_port)] + + # install a flow to avoid packet_in next time + if out_port != ofproto.OFPP_FLOOD: + match = parser.OFPMatch(in_port=in_port, eth_dst=dst) + self.add_flow(datapath, 1, match, actions) + + data = None + if msg.buffer_id == ofproto.OFP_NO_BUFFER: + data = msg.data + + out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id, + in_port=in_port, actions=actions, data=data) + datapath.send_msg(out) + + @set_ev_cls(stplib.EventTopologyChange, MAIN_DISPATCHER) + def _topology_change_handler(self, ev): + dp = ev.dp + dpid_str = dpid_lib.dpid_to_str(dp.id) + msg = 'Receive topology change event. Flush MAC table.' + self.logger.debug("[dpid=%s] %s", dpid_str, msg) + + if dp.id in self.mac_to_port: + self.delete_flow(dp) + del self.mac_to_port[dp.id] + + @set_ev_cls(stplib.EventPortStateChange, MAIN_DISPATCHER) + def _port_state_change_handler(self, ev): + dpid_str = dpid_lib.dpid_to_str(ev.dp.id) + of_state = {stplib.PORT_STATE_DISABLE: 'DISABLE', + stplib.PORT_STATE_BLOCK: 'BLOCK', + stplib.PORT_STATE_LISTEN: 'LISTEN', + stplib.PORT_STATE_LEARN: 'LEARN', + stplib.PORT_STATE_FORWARD: 'FORWARD'} + self.logger.debug("[dpid=%s][port=%d] state=%s", + dpid_str, ev.port_no, of_state[ev.port_state]) |