updated SDN chaining commands
authorstevenvanrossem <steven.vanrossem@intec.ugent.be>
Mon, 11 Apr 2016 14:59:29 +0000 (16:59 +0200)
committerstevenvanrossem <steven.vanrossem@intec.ugent.be>
Mon, 11 Apr 2016 14:59:29 +0000 (16:59 +0200)
src/emuvim/api/zerorpc/network.py
src/emuvim/cli/monitor.py
src/emuvim/cli/network.py
src/emuvim/dcemulator/monitoring.py
src/emuvim/dcemulator/net.py

index f232166..a64b09e 100644 (file)
@@ -64,37 +64,47 @@ class DCNetworkApi(object):
     def __init__(self, net):
         self.net = net
 
-    def network_action_start(self, vnf_src_name, vnf_dst_name):
+    def network_action_start(self, vnf_src_name, vnf_dst_name, vnf_src_interface=None, vnf_dst_interface=None):
         # call DCNetwork method, not really datacenter specific API for now...
         # provided dc name needs to be part of API endpoint
         # no check if vnfs are really connected to this datacenter...
         logging.debug("RPC CALL: network chain start")
         try:
             c = self.net.setChain(
-                vnf_src_name, vnf_dst_name)
+                vnf_src_name, vnf_dst_name, vnf_src_interface, vnf_dst_interface)
             return str(c)
         except Exception as ex:
             logging.exception("RPC error.")
             return ex.message
 
-    def network_action_stop(self, vnf_src_name, vnf_dst_name):
+    def network_action_stop(self, vnf_src_name, vnf_dst_name, vnf_src_interface=None, vnf_dst_interface=None):
         # call DCNetwork method, not really datacenter specific API for now...
         # provided dc name needs to be part of API endpoint
         # no check if vnfs are really connected to this datacenter...
         logging.debug("RPC CALL: network chain stop")
         try:
             c = self.net.setChain(
-                vnf_src_name, vnf_dst_name, cmd='del-flows')
+                vnf_src_name, vnf_dst_name, vnf_src_interface, vnf_dst_interface, cmd='del-flows')
+            return c
+        except Exception as ex:
+            logging.exception("RPC error.")
+            return ex.message
+
+    # setup the rate measurement for a vnf interface
+    def monitor_setup_rate_measurement(self, vnf_name, vnf_interface, direction, metric):
+        logging.debug("RPC CALL: get rate")
+        try:
+            c = self.net.monitor_agent.setup_rate_measurement(vnf_name, vnf_interface, direction, metric)
             return c
         except Exception as ex:
             logging.exception("RPC error.")
             return ex.message
 
     # get egress(default) or ingress rate of a vnf
-    def monitor_get_rate(self, vnf_name, direction):
+    def monitor_get_rate(self, vnf_name, vnf_interface, direction, metric):
         logging.debug("RPC CALL: get rate")
         try:
-            c = self.net.monitor_agent.get_rate(vnf_name, direction)
+            c = self.net.monitor_agent.get_rate(vnf_name, vnf_interface, direction, metric)
             return c
         except Exception as ex:
             logging.exception("RPC error.")
index 2caca2c..0c3c515 100755 (executable)
@@ -29,13 +29,33 @@ class ZeroRpcClient(object):
             print "Command not implemented."\r
 \r
     def get_rate(self, args):\r
+        vnf_name = self._parse_vnf_name(args.get("vnf_name"))\r
+        vnf_interface = self._parse_vnf_interface(args.get("vnf_name"))\r
+        self.c.monitor_setup_rate_measurement(\r
+            vnf_name,\r
+            vnf_interface,\r
+            args.get("direction"),\r
+            args.get("metric"))\r
         while True:\r
             r = self.c.monitor_get_rate(\r
-                args.get("vnf_name"),\r
-                args.get("direction"))\r
+                vnf_name,\r
+                vnf_interface,\r
+                args.get("direction"),\r
+                args.get("metric"))\r
             pp.pprint(r)\r
             time.sleep(1)\r
 \r
+    def _parse_vnf_name(self, vnf_name_str):\r
+        vnf_name = vnf_name_str.split(':')[0]\r
+        return vnf_name\r
+\r
+    def _parse_vnf_interface(self, vnf_name_str):\r
+        try:\r
+            vnf_interface = vnf_name_str.split(':')[1]\r
+        except:\r
+            vnf_interface = None\r
+\r
+        return vnf_interface\r
 \r
 parser = argparse.ArgumentParser(description='son-emu network')\r
 parser.add_argument(\r
@@ -46,7 +66,10 @@ parser.add_argument(
     help="vnf name to be monitored")\r
 parser.add_argument(\r
     "--direction", "-d", dest="direction",\r
-    help="in (ingress rate) or out (egress rate)")\r
+    help="rx (ingress rate) or tx (egress rate)")\r
+parser.add_argument(\r
+    "--metric", "-m", dest="metric",\r
+    help="bytes (byte rate), packets (packet rate)")\r
 \r
 def main(argv):\r
     print "This is the son-emu monitor CLI."\r
index 8d4219b..c27da59 100755 (executable)
@@ -28,19 +28,43 @@ class ZeroRpcClient(object):
             print "Command not implemented."\r
 \r
     def add(self, args):\r
+        vnf_src_name = self._parse_vnf_name(args.get("source"))\r
+        vnf_src_interface = self._parse_vnf_interface(args.get("source"))\r
+        vnf_dst_name = self._parse_vnf_name(args.get("destination"))\r
+        vnf_dst_interface = self._parse_vnf_interface(args.get("destination"))\r
         r = self.c.network_action_start(\r
             #args.get("datacenter"),\r
-            args.get("source"),\r
-            args.get("destination"))\r
+            vnf_src_name,\r
+            vnf_dst_name,\r
+            vnf_src_interface,\r
+            vnf_dst_interface)\r
         pp.pprint(r)\r
 \r
     def remove(self, args):\r
+        vnf_src_name = self._parse_vnf_name(args.get("source"))\r
+        vnf_src_interface = self._parse_vnf_interface(args.get("source"))\r
+        vnf_dst_name = self._parse_vnf_name(args.get("destination"))\r
+        vnf_dst_interface = self._parse_vnf_interface(args.get("destination"))\r
         r = self.c.network_action_stop(\r
             #args.get("datacenter"),\r
-            args.get("source"),\r
-            args.get("destination"))\r
+            vnf_src_name,\r
+            vnf_dst_name,\r
+            vnf_src_interface,\r
+            vnf_dst_interface)\r
         pp.pprint(r)\r
 \r
+    def _parse_vnf_name(self, vnf_name_str):\r
+        vnf_name = vnf_name_str.split(':')[0]\r
+        return vnf_name\r
+\r
+    def _parse_vnf_interface(self, vnf_name_str):\r
+        try:\r
+            vnf_interface = vnf_name_str.split(':')[1]\r
+        except:\r
+            vnf_interface = None\r
+\r
+        return vnf_interface\r
+\r
 \r
 parser = argparse.ArgumentParser(description='son-emu network')\r
 parser.add_argument(\r
index f5da2a1..50393ea 100755 (executable)
@@ -21,38 +21,35 @@ class DCNetworkMonitor():
 \r
         self.previous_measurement = 0\r
         self.previous_monitor_time = 0\r
+        self.switch_dpid = 0\r
+        self.metric_key = None\r
+        self.mon_port = None\r
 \r
-    def get_rate(self, vnf_name, direction='tx', metric='packets'):\r
+    # first set some parameters, before measurement can start\r
+    def setup_rate_measurement(self, vnf_name, vnf_interface=None, direction='tx', metric='packets'):\r
         # check if port is specified (vnf:port)\r
-        try:\r
-            vnf_interface = vnf_name.split(':')[1]\r
-        except:\r
+        if vnf_interface is None:\r
             # take first interface by default\r
             connected_sw = self.net.DCNetwork_graph.neighbors(vnf_name)[0]\r
             link_dict = self.net.DCNetwork_graph[vnf_name][connected_sw]\r
             vnf_interface = link_dict[0]['src_port_id']\r
-            # vnf_source_interface = 0\r
-\r
-        vnf_name = vnf_name.split(':')[0]\r
-        # take into account that this is a MultiGraph\r
-        #mon_port = self.net.DCNetwork_graph[vnf_name][connected_sw][0]['dst_port']\r
 \r
         for connected_sw in self.net.DCNetwork_graph.neighbors(vnf_name):\r
             link_dict = self.net.DCNetwork_graph[vnf_name][connected_sw]\r
             for link in link_dict:\r
-                # logging.info("{0},{1}".format(link_dict[link],vnf_source_interface))\r
+                # logging.info("{0},{1}".format(link_dict[link],vnf_interface))\r
                 if link_dict[link]['src_port_id'] == vnf_interface:\r
                     # found the right link and connected switch\r
                     # logging.info("{0},{1}".format(link_dict[link]['src_port_id'], vnf_source_interface))\r
-                    #src_sw = connected_sw\r
-\r
-                    mon_port = link_dict[link]['dst_port']\r
+                    self.mon_port = link_dict[link]['dst_port']\r
                     break\r
 \r
         try:\r
             # default port direction to monitor\r
             if direction is None:\r
                 direction = 'tx'\r
+            if metric is None:\r
+                metric = 'packets'\r
 \r
             vnf_switch = self.net.DCNetwork_graph.neighbors(str(vnf_name))\r
 \r
@@ -66,21 +63,31 @@ class DCNetworkMonitor():
                 vnf_switch = vnf_switch[0]\r
             next_node = self.net.getNodeByName(vnf_switch)\r
 \r
-            if not isinstance( next_node, OVSSwitch ):\r
+            if not isinstance(next_node, OVSSwitch):\r
                 logging.info("vnf: {0} is not connected to switch".format(vnf_name))\r
                 return\r
 \r
+            self.previous_measurement = 0\r
+            self.previous_monitor_time = 0\r
+\r
+            #self.switch_dpid = x = int(str(next_node.dpid), 16)\r
+            self.switch_dpid = int(str(next_node.dpid), 16)\r
+            self.metric_key = '{0}_{1}'.format(direction, metric)\r
+\r
+        except Exception as ex:\r
+            logging.exception("get_txrate error.")\r
+            return ex.message\r
 \r
-            switch_dpid = x = int(str(next_node.dpid),16)\r
 \r
-            # TODO get metric name from arg\r
-            key = '{0}_{1}'.format(direction, metric)\r
+    # call this function repeatedly for streaming measurements\r
+    def get_rate(self, vnf_name, vnf_interface=None, direction='tx', metric='packets'):\r
 \r
+            key = self.metric_key\r
 \r
-            ret = self.REST_cmd('stats/port', switch_dpid)\r
+            ret = self.REST_cmd('stats/port', self.switch_dpid)\r
             port_stat_dict = ast.literal_eval(ret)\r
-            for port_stat in port_stat_dict[str(switch_dpid)]:\r
-                if port_stat['port_no'] == mon_port:\r
+            for port_stat in port_stat_dict[str(self.switch_dpid)]:\r
+                if port_stat['port_no'] == self.mon_port:\r
                     port_uptime = port_stat['duration_sec'] + port_stat['duration_nsec'] * 10 ** (-9)\r
                     this_measurement = port_stat[key]\r
 \r
@@ -89,13 +96,12 @@ class DCNetworkMonitor():
                         self.previous_monitor_time = port_uptime\r
                         # do first measurement\r
                         time.sleep(1)\r
-                        byte_rate = self.get_rate(vnf_name,direction)\r
+                        byte_rate = self.get_rate(vnf_name, vnf_interface, direction, metric)\r
                         return byte_rate\r
                     else:\r
                         time_delta = (port_uptime - self.previous_monitor_time)\r
                         byte_rate = (this_measurement - self.previous_measurement) / float(time_delta)\r
                         #logging.info('uptime:{2} delta:{0} rate:{1}'.format(time_delta,byte_rate,port_uptime))\r
-                        #return byte_rate\r
 \r
                     self.previous_measurement = this_measurement\r
                     self.previous_monitor_time = port_uptime\r
@@ -103,12 +109,6 @@ class DCNetworkMonitor():
 \r
             return ret\r
 \r
-        except Exception as ex:\r
-            logging.exception("get_txrate error.")\r
-            return ex.message\r
-\r
-\r
-\r
     def REST_cmd(self, prefix, dpid):\r
         url = self.REST_api + '/' + str(prefix) + '/' + str(dpid)\r
         req = urllib2.Request(url)\r
index 47b4a7e..d6bb5bb 100755 (executable)
@@ -184,24 +184,21 @@ class DCNetwork(Dockernet):
         CLI(self)
 
     # to remove chain do setChain( src, dst, cmd='del-flows')
-    def setChain(self, vnf_src_name, vnf_dst_name, cmd='add-flow'):
+    def setChain(self, vnf_src_name, vnf_dst_name, vnf_src_interface=None, vnf_dst_interface=None, cmd='add-flow'):
 
         #check if port is specified (vnf:port)
-        try:
-            vnf_source_interface = vnf_src_name.split(':')[1]
-        except:
+        if vnf_src_interface is None:
             # take first interface by default
             connected_sw = self.DCNetwork_graph.neighbors(vnf_src_name)[0]
             link_dict = self.DCNetwork_graph[vnf_src_name][connected_sw]
-            vnf_source_interface = link_dict[0]['src_port_id']
+            vnf_src_interface = link_dict[0]['src_port_id']
             #vnf_source_interface = 0
 
-        vnf_src_name = vnf_src_name.split(':')[0]
         for connected_sw in self.DCNetwork_graph.neighbors(vnf_src_name):
             link_dict = self.DCNetwork_graph[vnf_src_name][connected_sw]
             for link in link_dict:
                 #logging.info("{0},{1}".format(link_dict[link],vnf_source_interface))
-                if link_dict[link]['src_port_id'] == vnf_source_interface:
+                if link_dict[link]['src_port_id'] == vnf_src_interface:
                     # found the right link and connected switch
                     #logging.info("{0},{1}".format(link_dict[link]['src_port_id'], vnf_source_interface))
                     src_sw = connected_sw
@@ -209,20 +206,18 @@ class DCNetwork(Dockernet):
                     src_sw_inport = link_dict[link]['dst_port']
                     break
 
-        try:
-            vnf_dest_interface = vnf_dst_name.split(':')[1]
-        except:
+        if vnf_dst_interface is None:
             # take first interface by default
             connected_sw = self.DCNetwork_graph.neighbors(vnf_dst_name)[0]
             link_dict = self.DCNetwork_graph[connected_sw][vnf_dst_name]
-            vnf_dest_interface = link_dict[0]['dst_port_id']
+            vnf_dst_interface = link_dict[0]['dst_port_id']
             #vnf_dest_interface = 0
 
         vnf_dst_name = vnf_dst_name.split(':')[0]
         for connected_sw in self.DCNetwork_graph.neighbors(vnf_dst_name):
             link_dict = self.DCNetwork_graph[connected_sw][vnf_dst_name]
             for link in link_dict:
-                if link_dict[link]['dst_port_id'] == vnf_dest_interface:
+                if link_dict[link]['dst_port_id'] == vnf_dst_interface:
                     # found the right link and connected switch
                     dst_sw = connected_sw
                     dst_sw_outport = link_dict[link]['src_port']
@@ -260,6 +255,7 @@ class DCNetwork(Dockernet):
                 logging.info("Next node: {0} is not a switch".format(next_hop))
                 return "Next node: {0} is not a switch".format(next_hop)
             else:
+                # take first link between switches by default
                 index_edge_out = 0
                 switch_outport = self.DCNetwork_graph[current_hop][next_hop][index_edge_out]['src_port']
 
@@ -292,9 +288,10 @@ class DCNetwork(Dockernet):
                 current_node.dpctl(cmd, ofcmd)
                 logging.info("add flow in switch: {0} in_port: {1} out_port: {2}".format(current_node.name, switch_inport,
                                                                                      switch_outport))
-
-            switch_inport = self.DCNetwork_graph[current_hop][next_hop][index_edge_out]['dst_port']
-            current_hop = next_hop
+            # take first link between switches by default
+            if isinstance( next_node, OVSSwitch ):
+                switch_inport = self.DCNetwork_graph[current_hop][next_hop][0]['dst_port']
+                current_hop = next_hop
 
         return "path added between {0} and {1}".format(vnf_src_name, vnf_dst_name)
         #return "destination node: {0} not reached".format(vnf_dst_name)
@@ -311,9 +308,9 @@ class DCNetwork(Dockernet):
         ryu_of_port = '6653'
         ryu_cmd = 'ryu-manager'
         FNULL = open("/tmp/ryu.log", 'w')
-        self.ryu_process = Popen([ryu_cmd, ryu_path, ryu_path2, ryu_option, ryu_of_port], stdout=FNULL, stderr=FNULL)
+        #self.ryu_process = Popen([ryu_cmd, ryu_path, ryu_path2, ryu_option, ryu_of_port], stdout=FNULL, stderr=FNULL)
         # no learning switch
-        #self.ryu_process = Popen([ryu_cmd, ryu_path2, ryu_option, ryu_of_port], stdout=FNULL, stderr=FNULL)
+        self.ryu_process = Popen([ryu_cmd, ryu_path2, ryu_option, ryu_of_port], stdout=FNULL, stderr=FNULL)
         time.sleep(1)
 
     def stopRyu(self):