This implements the functionality which is same to quantum openvswitch plugin
without tunneling. (i.e. OVSQuantumAgent)

Signed-off-by: Isaku Yamahata <[email protected]>
---
 ryu/app/simple_vlan.py |  146 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 146 insertions(+), 0 deletions(-)
 create mode 100644 ryu/app/simple_vlan.py

diff --git a/ryu/app/simple_vlan.py b/ryu/app/simple_vlan.py
new file mode 100644
index 0000000..a0a8bea
--- /dev/null
+++ b/ryu/app/simple_vlan.py
@@ -0,0 +1,146 @@
+# Copyright (C) 2012 Isaku Yamahata <yamahata at private email ne jp>
+#
+# 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 ryu.exception as ryu_exc
+from ryu.app import conf_switch_key
+from ryu.base import app_manager
+from ryu.controller import (conf_switch,
+                            dpset,
+                            handler,
+                            network,
+                            tunnels)
+from ryu.lib.ovs_bridge import OVSBridge
+from ryu.ofproto import nx_match
+
+
+LOG = logging.getLogger(__name__)
+
+
+class SimpleVLAN(app_manager.RyuApp):
+    _CONTEXTS = {
+        'netowrk': app_manager.noarg_app_context_factory(network.Network),
+        'tunnels': app_manager.noarg_app_context_factory(tunnels.Tunnels),
+        'conf_switch':
+        app_manager.noarg_app_context_factory(conf_switch.ConfSwitchSet),
+        }
+
+    _PRIORITY_CATCHALL = 1
+    _PRIORITY_DROP = 2
+    _COOKIE_DEFAULT = 0
+
+    def __init__(self, *args, **kwargs):
+        super(SimpleVLAN, self).__init__(*args, **kwargs)
+        self.nw = kwargs['network']
+        self.tunnels = kwargs['tunnels']
+        self.conf_sw = kwargs['conf_switch']
+
+    @handler.set_ev_cls(dpset.EventDP, dpset.DPSET_EV_DISPATCHER)
+    def dp_handler(self, ev):
+        if not ev.enter_leave:
+            return
+
+        dp = ev.datapath
+        rule = nx_match.ClsRule()
+        ofproto = dp.ofproto
+        actions = [dp.ofproto_parser.OFPActionOutput(ofproto.OFPP_NORMAL)]
+        dp.send_flow_mod(rule=rule, cookie=self._COOKIE_DEFAULT,
+                         command=ofproto.OFPFC_ADD,
+                         idle_timeout=0, hard_timeout=0,
+                         priority=self._PRIORITY_CATCHALL, actions=actions)
+
+    # There is no ordering between those events
+    #   port creation: PortAdd event
+    #   network_id assignment: NetworkPort event
+    #   tunnel_key assignment: TunnelKeyAdd event
+    # So on each events, check all necessary parameters are setup
+
+    def _port_setup(self, dpid, port_no, tunnel_key):
+        dp = self.dpset.get(dpid)
+        if dp is None:
+            return
+
+        try:
+            port = self.dpset.get_port(dpid, port_no)
+        except ryu_exc.PortNotFound:
+            return
+
+        try:
+            ovsdb_addr = self.conf_sw.get_key(dpid, conf_switch_key.OVSDB_ADDR)
+        except KeyError:
+            return
+
+        # TODO:??? should use ovsdb protocol directly instead of
+        # invoking ovs-vsctl command?
+        #
+        # ovs-vsctl --db=ovsdb_addr --timeout=2
+        # set Port port.name tag=tunnel_key
+        ovs_br = OVSBridge(ovsdb_addr)
+        ovs_br.set_db_attribute("Port", port.name, "tag", tunnel_key)
+
+        self._port_flow_del(dp, port.port_no)
+        return True
+
+    def _port_setup_netid(self, dpid, port_no, network_id):
+        try:
+            tunnel_key = self.tunnels.get_key(network_id)
+        except ryu_exc.TunnelKeyNotFound:
+            return
+
+        return self._port_setup(dpid, port_no, tunnel_key)
+
+    def _port_flow_del(self, dp, port_no):
+        rule = nx_match.ClsRule()
+        rule.set_in_port(port_no)
+        dp.send_flow_del(rule=rule, cookie=self._COOKIE_DEFAULT)
+
+    def _port_drop_all(self, dp, port_no):
+        rule = nx_match.ClsRule()
+        rule.set_in_port(port_no)
+        ofproto = dp.ofproto
+        dp.send_flow_mod(rule=rule, cookie=self._COOKIE_DEFAULT,
+                         command=ofproto.OFPFC_ADD,
+                         idle_timeout=0, hard_timeout=0,
+                         priority=self._PRIORITY_DROP, actions=[])
+
+    @handler.set_ev_cls(dpset.EventPortAdd, dpset.DPSET_EV_DISPATCHER)
+    def port_add_handler(self, ev):
+        try:
+            network_id = self.nw.get_network(ev.dpid, ev.port.port_no)
+        except ryu_exc.PortUnknown:
+            self._port_drop_all()
+            return
+
+        if not self._port_setup_netid(ev.dp.id, ev.port.port_no, network_id):
+            self._port_drop_all()
+
+    @handler.set_ev_cls(dpset.EventPortDelete, dpset.DPSET_EV_DISPATCHER)
+    def port_del_handler(self, ev):
+        self._port_flow_del(ev.dp, ev.port.port_no)
+
+    @handler.set_ev_cls(network.EventNetworkPort,
+                        network.NETWORK_TENANT_EV_DISPATCHER)
+    def network_port_handler(self, ev):
+        if not ev.add_del:
+            return
+        self._port_setup_netid(ev.dpid, ev.port_no, ev.network_id)
+
+    @handler.set_ev_cls(tunnels.EventTunnelKeyAdd,
+                        tunnels.TUNNEL_EV_DISPATCHER)
+    def tunnel_key_add_handler(self, ev):
+        tunnel_key = ev.tunnel_key
+        for (dpid, port_no) in self.nw.list_ports(ev.network_id):
+            self._port_setup(dpid, port_no, tunnel_key)
-- 
1.7.1.1


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to