From c5a536a1796846be97e5ba6ce9c20d73b6fd0052 Mon Sep 17 00:00:00 2001 From: stevenvanrossem Date: Tue, 16 Feb 2016 14:52:39 +0100 Subject: [PATCH] merge network/monitoring cli commands --- emuvim/api/zerorpcapi_DCNetwork.py | 112 +++++++++++++++++++++++++++++ emuvim/cli/monitor.py | 53 ++++++++++++++ emuvim/cli/network.py | 73 ++++++++++++++++--- emuvim/cli/son-emu-cli | 4 +- emuvim/dcemulator/monitoring.py | 62 ++++++++++++++++ emuvim/dcemulator/net.py | 84 ++++++++++++++++++++-- start_dcnetwork | 7 ++ start_example_chain | 16 +++++ 8 files changed, 397 insertions(+), 14 deletions(-) create mode 100644 emuvim/api/zerorpcapi_DCNetwork.py create mode 100644 emuvim/cli/monitor.py create mode 100644 emuvim/dcemulator/monitoring.py create mode 100644 start_dcnetwork create mode 100644 start_example_chain diff --git a/emuvim/api/zerorpcapi_DCNetwork.py b/emuvim/api/zerorpcapi_DCNetwork.py new file mode 100644 index 0000000..7402f3d --- /dev/null +++ b/emuvim/api/zerorpcapi_DCNetwork.py @@ -0,0 +1,112 @@ +""" +Distributed Cloud Emulator (dcemulator) +(c) 2015 by Manuel Peuster +""" + +import logging +import threading +import zerorpc +import site +from subprocess import Popen + +logging.basicConfig(level=logging.INFO) + + +class ZeroRpcApiEndpointDCNetwork(object): + """ + Simple API endpoint that offers a zerorpc-based + interface. This interface will be used by the + default command line client. + It can be used as a reference to implement + REST interfaces providing the same semantics, + like e.g. OpenStack compute API. + """ + + def __init__(self, listenip, port, DCNetwork=None): + if DCNetwork : + self.connectDCNetwork(DCNetwork) + self.ip = listenip + self.port = port + logging.debug("Created monitoring API endpoint %s(%s:%d)" % ( + self.__class__.__name__, self.ip, self.port)) + + # start Ryu controller with rest-API + python_install_path = site.getsitepackages()[0] + ryu_path = python_install_path + '/ryu/app/ofctl_rest.py' + ryu_cmd = 'ryu-manager' + self.ryu_process = Popen([ryu_cmd,ryu_path]) + + + def connectDCNetwork(self, net): + self.net = net + logging.info("Connected DCNetwork(%s) to API endpoint %s(%s:%d)" % ( + net.name, self.__class__.__name__, self.ip, self.port)) + + def start(self): + thread = threading.Thread(target=self._api_server_thread, args=()) + thread.daemon = True + thread.start() + logging.debug("Started API endpoint %s(%s:%d)" % ( + self.__class__.__name__, self.ip, self.port)) + + def _api_server_thread(self): + s = zerorpc.Server(DCNetworkApi(self.net)) + s.bind("tcp://%s:%d" % (self.ip, self.port)) + s.run() + + def stop(self): + # stop ryu controller + logging.info("Stop the monitoring API endpoint") + self.ryu_process.terminate() + #self.ryu_process.kill() + return + + +class DCNetworkApi(object): + """ + Just pass through the corresponding request to the + selected data center. Do not implement provisioning + logic here because will will have multiple API + endpoint implementations at the end. + """ + + def __init__(self, net): + self.net = net + + def network_action_start(self, vnf_src_name, vnf_dst_name): + # 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) + 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): + # 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') + 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): + logging.debug("RPC CALL: get rate") + try: + c = self.net.monitor_agent.get_rate(vnf_name, direction) + return c + except Exception as ex: + logging.exception("RPC error.") + return ex.message + + diff --git a/emuvim/cli/monitor.py b/emuvim/cli/monitor.py new file mode 100644 index 0000000..6885a3c --- /dev/null +++ b/emuvim/cli/monitor.py @@ -0,0 +1,53 @@ +""" +son-emu network CLI +(c) 2016 by Manuel Peuster +""" + +import argparse +import pprint +from tabulate import tabulate +import zerorpc + + +pp = pprint.PrettyPrinter(indent=4) + +class ZeroRpcClient(object): + + def __init__(self): + self.c = zerorpc.Client() + # TODO connect to DCNetwork API + #self.c.connect("tcp://127.0.0.1:4242") # TODO hard coded for now. we'll change this later + self.c.connect("tcp://127.0.0.1:5151") + self.cmds = {} + + def execute_command(self, args): + if getattr(self, args["command"]) is not None: + # call the local method with the same name as the command arg + getattr(self, args["command"])(args) + else: + print "Command not implemented." + + def get_rate(self, args): + r = self.c.monitor_get_rate( + args.get("vnf_name"), + args.get("direction")) + pp.pprint(r) + + +parser = argparse.ArgumentParser(description='son-emu network') +parser.add_argument( + "command", + help="Action to be executed: get_rate") +parser.add_argument( + "--vnf_name", "-vnf", dest="vnf_name", + help="vnf name to be monitored") +parser.add_argument( + "--direction", "-d", dest="direction", + help="in (ingress rate) or out (egress rate)") + +def main(argv): + print "This is the son-emu monitor CLI." + print "Arguments: %s" % str(argv) + args = vars(parser.parse_args(argv)) + c = ZeroRpcClient() + c.execute_command(args) diff --git a/emuvim/cli/network.py b/emuvim/cli/network.py index 080b0ac..fd7851a 100755 --- a/emuvim/cli/network.py +++ b/emuvim/cli/network.py @@ -1,9 +1,64 @@ -""" -son-emu network CLI -(c) 2016 by Manuel Peuster -""" - - -def main(argv): - print "This is the son-emu network CLI." - print "Arguments: %s" % str(argv) +""" +son-emu network CLI +(c) 2016 by Manuel Peuster +""" + +import argparse +import pprint +from tabulate import tabulate +import zerorpc + + +pp = pprint.PrettyPrinter(indent=4) + +class ZeroRpcClient(object): + + def __init__(self): + self.c = zerorpc.Client() + # TODO connect to DCNetwork API + #self.c.connect("tcp://127.0.0.1:4242") # TODO hard coded for now. we'll change this later + self.c.connect("tcp://127.0.0.1:5151") + self.cmds = {} + + def execute_command(self, args): + if getattr(self, args["command"]) is not None: + # call the local method with the same name as the command arg + getattr(self, args["command"])(args) + else: + print "Command not implemented." + + def add(self, args): + r = self.c.network_action_start( + #args.get("datacenter"), + args.get("source"), + args.get("destination")) + pp.pprint(r) + + def remove(self, args): + r = self.c.network_action_stop( + #args.get("datacenter"), + args.get("source"), + args.get("destination")) + pp.pprint(r) + + +parser = argparse.ArgumentParser(description='son-emu network') +parser.add_argument( + "command", + help="Action to be executed: add|remove") +parser.add_argument( + "--datacenter", "-d", dest="datacenter", + help="Data center to in which the network action should be initiated") +parser.add_argument( + "--source", "-src", dest="source", + help="vnf name of the source of the chain") +parser.add_argument( + "--destination", "-dst", dest="destination", + help="vnf name of the destination of the chain") + +def main(argv): + print "This is the son-emu network CLI." + print "Arguments: %s" % str(argv) + args = vars(parser.parse_args(argv)) + c = ZeroRpcClient() + c.execute_command(args) diff --git a/emuvim/cli/son-emu-cli b/emuvim/cli/son-emu-cli index ba7a292..2a1e598 100755 --- a/emuvim/cli/son-emu-cli +++ b/emuvim/cli/son-emu-cli @@ -16,7 +16,7 @@ import sys import compute import network import datacenter - +import monitor def main(): if len(sys.argv) < 2: @@ -28,6 +28,8 @@ def main(): network.main(sys.argv[2:]) elif sys.argv[1] == "datacenter": datacenter.main(sys.argv[2:]) + elif sys.argv[1] == "monitor": + monitor.main(sys.argv[2:]) if __name__ == '__main__': main() diff --git a/emuvim/dcemulator/monitoring.py b/emuvim/dcemulator/monitoring.py new file mode 100644 index 0000000..094c09b --- /dev/null +++ b/emuvim/dcemulator/monitoring.py @@ -0,0 +1,62 @@ +__author__ = 'Administrator' + +import urllib2 +import logging +from mininet.node import OVSSwitch +import ast +logging.basicConfig(level=logging.INFO) + +""" +class to read openflow stats from the Ryu controller of the DCNEtwork +""" + +class DCNetworkMonitor(): + def __init__(self, net): + self.net = net + # link to REST_API + self.ip = '0.0.0.0' + self.port = '8080' + self.REST_api = 'http://{0}:{1}'.format(self.ip,self.port) + + + def get_rate(self, vnf_name, direction='tx'): + try: + vnf_switch = self.net.DCNetwork_graph.neighbors(str(vnf_name)) + + if len(vnf_switch) > 1: + logging.info("vnf: {0} has multiple ports".format(vnf_name)) + return + elif len(vnf_switch) == 0: + logging.info("vnf: {0} is not connected".format(vnf_name)) + return + else: + vnf_switch = vnf_switch[0] + next_node = self.net.getNodeByName(vnf_switch) + + if not isinstance( next_node, OVSSwitch ): + logging.info("vnf: {0} is not connected to switch".format(vnf_name)) + return + + mon_port = self.net.DCNetwork_graph[vnf_name][vnf_switch]['dst_port'] + switch_dpid = x = int(str(next_node.dpid),16) + + ret = self.REST_cmd('stats/port', switch_dpid) + port_stat_dict = ast.literal_eval(ret) + for port_stat in port_stat_dict[str(switch_dpid)]: + if port_stat['port_no'] == mon_port: + return port_stat + break + + return ret + + except Exception as ex: + logging.exception("get_txrate error.") + return ex.message + + + + def REST_cmd(self, prefix, dpid): + url = self.REST_api + '/' + str(prefix) + '/' + str(dpid) + req = urllib2.Request(url) + ret = urllib2.urlopen(req).read() + return ret \ No newline at end of file diff --git a/emuvim/dcemulator/net.py b/emuvim/dcemulator/net.py index 7b238b8..8379cd3 100755 --- a/emuvim/dcemulator/net.py +++ b/emuvim/dcemulator/net.py @@ -9,6 +9,8 @@ from mininet.node import Controller, OVSKernelSwitch, Switch, Docker, Host from mininet.cli import CLI from mininet.log import setLogLevel, info from mininet.link import TCLink, Link +import networkx as nx +from monitoring import DCNetworkMonitor from node import Datacenter, EmulatorCompute @@ -26,8 +28,15 @@ class DCNetwork(Dockernet): # create a Mininet/Dockernet network # call original Docker.__init__ and setup default controller Dockernet.__init__( - self, controller=Controller, switch=OVSKernelSwitch, **kwargs) - self.addController('c0') + self, controller=RemoteController, switch=OVSKernelSwitch, **kwargs) + #self.addController('c0') + + # graph of the complete DC network + self.DCNetwork_graph=nx.DiGraph() + + # monitoring agent + self.monitor_agent = DCNetworkMonitor(self) + def addDatacenter(self, label, metadata={}): """ @@ -74,13 +83,37 @@ class DCNetwork(Dockernet): if not "ip" in params["params2"]: params["params2"]["ip"] = self.getNextIp() - return Dockernet.addLink(self, node1, node2, **params) # TODO we need TCLinks with user defined performance here + link = Dockernet.addLink(self, node1, node2, **params) # TODO we need TCLinks with user defined performance here + + # add edge and assigned port number to graph in both directions between node1 and node2 + self.DCNetwork_graph.add_edge(node1.name, node2.name, \ + {'src_port': node1.ports[link.intf1], 'dst_port': node2.ports[link.intf2]}) + self.DCNetwork_graph.add_edge(node2.name, node1.name, \ + {'src_port': node2.ports[link.intf2], 'dst_port': node1.ports[link.intf1]}) + + return link def addDocker( self, label, **params ): """ Wrapper for addDocker method to use custom container class. """ - return Dockernet.addDocker(self, label, cls=EmulatorCompute, **params) + self.DCNetwork_graph.add_node(name) + return Dockernet.addDocker(self, name, cls=EmulatorCompute, **params) + + def removeDocker( self, name, **params ): + """ + Wrapper for removeDocker method to update graph. + """ + self.DCNetwork_graph.remove_node(name) + return Dockernet.removeDocker(self, name, **params) + + def addSwitch( self, name, add_to_graph=True, **params ): + """ + Wrapper for addSwitch method to store switch also in graph. + """ + if add_to_graph: + self.DCNetwork_graph.add_node(name) + return Dockernet.addSwitch(self, name, protocols='OpenFlow10,OpenFlow12,OpenFlow13', **params) def getAllContainers(self): """ @@ -102,3 +135,46 @@ class DCNetwork(Dockernet): def CLI(self): CLI(self) + + # to remove chain do setChain( src, dst, cmd='del-flows') + def setChain(self, vnf_src_name, vnf_dst_name, cmd='add-flow'): + # get shortest path + path = nx.shortest_path(self.DCNetwork_graph, vnf_src_name, vnf_dst_name) + logging.info("Path between {0} and {1}: {2}".format(vnf_src_name, vnf_dst_name, path)) + + current_hop = vnf_src_name + for i in range(0,len(path)): + next_hop = path[path.index(current_hop)+1] + next_node = self.getNodeByName(next_hop) + + if next_hop == vnf_dst_name: + return 0 + elif not isinstance( next_node, OVSSwitch ): + logging.info("Next node: {0} is not a switch".format(next_hop1)) + return 0 + + + switch_inport = self.DCNetwork_graph[current_hop][next_hop]['dst_port'] + next2_hop = path[path.index(current_hop)+2] + switch_outport = self.DCNetwork_graph[next_hop][next2_hop]['src_port'] + + logging.info("add flow in switch: {0} in_port: {1} out_port: {2}".format(next_node.name, switch_inport, switch_outport)) + # set of entry via ovs-ofctl + # TODO use rest API of ryu to set flow entries to correct witch dpid + if isinstance( next_node, OVSSwitch ): + match = 'in_port=%s' % switch_inport + + if cmd=='add-flow': + action = 'action=%s' % switch_outport + s = ',' + ofcmd = s.join([match,action]) + elif cmd=='del-flows': + ofcmd = match + else: + ofcmd='' + + next_node.dpctl(cmd, ofcmd) + + current_hop = next_hop + + return 1 \ No newline at end of file diff --git a/start_dcnetwork b/start_dcnetwork new file mode 100644 index 0000000..aca405b --- /dev/null +++ b/start_dcnetwork @@ -0,0 +1,7 @@ +#!/bin/bash + +# start DC Network +cd emuvim/ +python example_topology.py + + diff --git a/start_example_chain b/start_example_chain new file mode 100644 index 0000000..8ec8a1f --- /dev/null +++ b/start_example_chain @@ -0,0 +1,16 @@ +#!/bin/bash + + +# deploy VNFs +cd emuvim/cli/ +./son-emu-cli compute start -d dc1 -n tsrc -i traffic_source -c ./start.sh +./son-emu-cli compute start -d dc2 -n fw -i firewall -c ./start.sh +./son-emu-cli compute start -d dc3 -n tsink -i traffic_sink -c ./start.sh + +# setup links in the chain +./son-emu-cli network add -src tsrc -dst fw +./son-emu-cli network add -src fw -dst tsink +./son-emu-cli network add -src tsink -dst tsrc + + + -- 2.25.1