Example #1
0
class MonitorServer(object):
    monitor = None
    server = None
    logger = None

    def __init__(self, monitor):
        self.logger = logging.getLogger(self.__class__.__name__)
        self.monitor = monitor

    def _register_func(self, func):
        def wrap(fun, *args, **kwargs):
            try:
                return True, fun(*args, **kwargs)
            except Exception as e:
                if isinstance(e, MonitorException):
                    return False, e.message
                else:
                    import traceback
                    self.logger.error("%s" % traceback.format_exc())
                    raise e

        partial_func = partial(wrap, func)
        update_wrapper(partial_func, func)
        self.server.register_function(partial_func)

    def start(self):
        self.logger.info("Starting...")
        self.server = SimpleJSONRPCServer(('localhost', 8080),
                                          logRequests=False)

        for maybeCommand in self.monitor.__class__.__dict__.values():
            if hasattr(maybeCommand, 'is_command'):
                command_name = maybeCommand.__name__
                self.logger.debug("Registering command %s" % command_name)
                command = getattr(self.monitor, command_name)
                self._register_func(command)

    def stop(self):
        if self.server:
            self.logger.info("Stopping server")
            self.server.shutdown()
            self.server.server_close()

    def serve_forever(self):
        self.logger.info("Entering  main loop")
        self.server.serve_forever()
    def startservers(self):
        server = SimpleJSONRPCServer((IP, self.serverport))
        server.register_function(self.deviceConnected)
        server.register_function(self.deviceDisconnected)
        server.register_function(self.portAdded)
        server.register_function(self.portRemoved)
        server.register_function(self.linkup)
        server.register_function(self.linkdown)

        try:
            self.fvthread = threading.Thread(target=server.serve_forever)
            print "Start callback server"
            self.fvthread.start()
            self.httpd.serve_forever()
        except KeyboardInterrupt:
            server.shutdown()
            self.unregisterFromFV()
    def startservers(self):
        server = SimpleJSONRPCServer((IP, self.serverport))
        server.register_function(self.deviceConnected)
        server.register_function(self.deviceDisconnected)
        server.register_function(self.portAdded)
        server.register_function(self.portRemoved)
        server.register_function(self.linkup)
        server.register_function(self.linkdown)


        try:
            self.fvthread = threading.Thread(target=server.serve_forever)
            print "Start callback server"
            self.fvthread.start()
            self.httpd.serve_forever()
        except KeyboardInterrupt:
            server.shutdown()
            self.unregisterFromFV()
Example #4
0
class MockRPCServiceProvider(Thread):
    """Implement a mock of a remote 3rd party JSON-RPC service provider."""

    url = ('localhost', 8484)
    messages = []
    name = ''
    ident = 0

    def __init__(self, *args, **kwargs):
        Thread.__init__(self)
        self.server = SimpleJSONRPCServer(self.url)
        self.server.register_function(self.push, 'push')
        self.server.verbose = False
        self.messages = []

    def run(self):
        logging.debug("MockRPCServiceProvider thread %i start" % self.ident)
        try:
            self.server.serve_forever()
        except:
            pass
        logging.debug("MockRPCServiceProvider thread %i exit" % self.ident)
        exit()

    def stop(self):
        logging.debug("Stop MockRPCServiceProvider")
        self.server.shutdown()
        self.server.server_close()

    def register_function(self,*args,**kwargs):
        return self.server.register_function(*args,**kwargs)

    def push(self, msg, *args):
        logging.debug("saving message: %s" % msg)
        self.messages.append(msg)
        return "ok"

    def get_messages(self):
        return self.messages
Example #5
0
    def test_host_only(self):
        """
        Starts a Unix socket server, giving with a relative path to the socket
        """
        # Ensure we have a new socket
        socket_name = "test_local.socket"
        if os.path.exists(socket_name):
            os.remove(socket_name)

        # Use a random int as result
        awaited_result = random.randint(1, 100)

        try:
            # Prepare the server
            srv = SimpleJSONRPCServer(socket_name,
                                      address_family=socket.AF_UNIX)
            srv.register_function(lambda: awaited_result, "test")

            # Run the server in a thread
            thread = threading.Thread(target=srv.serve_forever)
            thread.start()

            try:
                # Run the request
                client = ServerProxy("unix+http://{}".format(socket_name))
                result = client.test()
                self.assertEqual(result, awaited_result)
            finally:
                # Stop the server
                srv.shutdown()
                srv.server_close()
                thread.join(5)
        finally:
            # Clean up
            try:
                os.remove(socket_name)
            except:
                pass
Example #6
0
class JSONRPCServer(SingletonClass):
    
    def __initialize__(self, context_root='/', server_port=8181):
        
        SimpleJSONRPCRequestHandler.log_request = log_request
        SimpleJSONRPCRequestHandler.rpc_paths = (context_root)
        self.server = SimpleJSONRPCServer(('', server_port))
        
    def registerService(self, serviceName, function):
        self.server.register_function(function, name=serviceName)
        
    def start(self):
        self.server_proc = Thread(target=self.server.serve_forever)
        self.server_proc.daemon = True
        self.server_proc.start()
        self.started = True
    
    def stop(self):
        try:
            if self.started:
                self.server.shutdown()
            self.server.server_close()
        except:
            pass
Example #7
0
    def shutdown(self):
        shutdown_list = self.registered_services.keys()
        for svc in shutdown_list:
            self.unregister_service(svc)

        SimpleJSONRPCServer.shutdown(self)
Example #8
0
class CTRexServer(object):
    """This class defines the server side of the RESTfull interaction with TRex"""
    DEFAULT_TREX_PATH = '/auto/proj-pcube-b/apps/PL-b/tools/bp_sim2/v1.55/' #'/auto/proj-pcube-b/apps/PL-b/tools/nightly/trex_latest'
    TREX_START_CMD    = './t-rex-64'
    DEFAULT_FILE_PATH = '/tmp/trex_files/'

    def __init__(self, trex_path, trex_files_path, trex_host='0.0.0.0', trex_daemon_port=8090, trex_zmq_port=4500):
        """ 
        Parameters
        ----------
        trex_host : str
            a string of the t-rex ip address or hostname.
            default value: machine hostname as fetched from socket.gethostname()
        trex_daemon_port : int
            the port number on which the trex-daemon server can be reached
            default value: 8090
        trex_zmq_port : int
            the port number on which trex's zmq module will interact with daemon server
            default value: 4500

        Instantiate a TRex client object, and connecting it to listening daemon-server
        """
        self.TREX_PATH          = os.path.abspath(os.path.dirname(trex_path+'/'))
        self.trex_files_path    = os.path.abspath(os.path.dirname(trex_files_path+'/'))
        self.__check_trex_path_validity()
        self.__check_files_path_validity()
        self.trex               = CTRex()
        self.trex_host          = trex_host
        self.trex_daemon_port   = trex_daemon_port
        self.trex_zmq_port      = trex_zmq_port
        self.trex_server_path   = "http://{hostname}:{port}".format( hostname = trex_host, port = trex_daemon_port )
        self.start_lock         = threading.Lock()
        self.__reservation      = None
        self.zmq_monitor        = ZmqMonitorSession(self.trex, self.trex_zmq_port)    # intiate single ZMQ monitor thread for server usage
    
    def add(self, x, y):
        print "server function add ",x,y
        logger.info("Processing add function. Parameters are: {0}, {1} ".format( x, y ))
        return x + y
        # return Fault(-10, "")

    def push_file (self, filename, bin_data):
        logger.info("Processing push_file() command.")
        try:
            filepath = os.path.abspath(os.path.join(self.trex_files_path, filename))
            with open(filepath, 'wb') as f:
                f.write(binascii.a2b_base64(bin_data))
            logger.info("push_file() command finished. `{name}` was saved at {fpath}".format( name = filename, fpath = self.trex_files_path))
            return True
        except IOError as inst:
            logger.error("push_file method failed. " + str(inst))
            return False

    def connectivity_check (self):
        logger.info("Processing connectivity_check function.")
        return True

    def start(self):
        """This method fires up the daemon server based on initialized parameters of the class"""
        # initialize the server instance with given resources
        try:
            print "Firing up TRex REST daemon @ port {trex_port} ...\n".format( trex_port = self.trex_daemon_port )
            logger.info("Firing up TRex REST daemon @ port {trex_port} ...".format( trex_port = self.trex_daemon_port ))
            logger.info("current working dir is: {0}".format(self.TREX_PATH) )
            logger.info("current files dir is  : {0}".format(self.trex_files_path) )
            logger.debug("Starting TRex server. Registering methods to process.")
            self.server = SimpleJSONRPCServer( (self.trex_host, self.trex_daemon_port) )
        except socket.error as e:
            if e.errno == errno.EADDRINUSE:
                logger.error("TRex server requested address already in use. Aborting server launching.")
                print "TRex server requested address already in use. Aborting server launching."
                raise socket.error(errno.EADDRINUSE, "TRex daemon requested address already in use. "
                                                     "Server launch aborted. Please make sure no other process is "
                                                     "using the desired server properties.")
            elif isinstance(e, socket.gaierror) and e.errno == -3:
                # handling Temporary failure in name resolution exception
                raise socket.gaierror(-3, "Temporary failure in name resolution.\n"
                                          "Make sure provided hostname has DNS resolving.")
            else:
                raise

        # set further functionality and peripherals to server instance 
        try:
            self.server.register_function(self.add)
            self.server.register_function(self.connectivity_check)
            self.server.register_function(self.start_trex)
            self.server.register_function(self.stop_trex)
            self.server.register_function(self.wait_until_kickoff_finish)
            self.server.register_function(self.get_running_status)
            self.server.register_function(self.is_running)
            self.server.register_function(self.get_running_info)
            self.server.register_function(self.is_reserved)
            self.server.register_function(self.get_files_path)
            self.server.register_function(self.push_file)
            self.server.register_function(self.reserve_trex)
            self.server.register_function(self.cancel_reservation)
            self.server.register_function(self.force_trex_kill)
            signal.signal(signal.SIGTSTP, self.stop_handler)
            signal.signal(signal.SIGTERM, self.stop_handler)
            self.zmq_monitor.start()
            self.server.serve_forever()
        except KeyboardInterrupt:
            logger.info("Daemon shutdown request detected." )
        finally:
            self.zmq_monitor.join()            # close ZMQ monitor thread resources
            self.server.shutdown()
            pass

    def stop_handler (self, signum, frame):
        logger.info("Daemon STOP request detected.")
        if self.is_running():
            # in case TRex process is currently running, stop it before terminating server process
            self.stop_trex(self.trex.get_seq())
        sys.exit(0)

    def is_running (self):
        run_status = self.trex.get_status()
        logger.info("Processing is_running() command. Running status is: {stat}".format(stat = run_status) )
        if run_status==TRexStatus.Running:
            return True
        else:
            return False

    def is_reserved (self):
        logger.info("Processing is_reserved() command.")
        return bool(self.__reservation)

    def get_running_status (self):
        run_status = self.trex.get_status()
        logger.info("Processing get_running_status() command. Running status is: {stat}".format(stat = run_status) )
        return { 'state' : run_status.value, 'verbose' : self.trex.get_verbose_status() }

    def get_files_path (self):
        logger.info("Processing get_files_path() command." )
        return self.trex_files_path

    def reserve_trex (self, user):
        if user == "":
            logger.info("TRex reservation cannot apply to empty string user. Request denied.")
            return Fault(-33, "TRex reservation cannot apply to empty string user. Request denied.")

        with self.start_lock:
            logger.info("Processing reserve_trex() command.")
            if self.is_reserved():
                if user == self.__reservation['user']:  
                    # return True is the same user is asking and already has the resrvation
                    logger.info("the same user is asking and already has the resrvation. Re-reserving TRex.")
                    return True

                logger.info("TRex is already reserved to another user ({res_user}), cannot reserve to another user.".format( res_user = self.__reservation['user'] ))
                return Fault(-33, "TRex is already reserved to another user ({res_user}). Please make sure TRex is free before reserving it.".format(
                    res_user = self.__reservation['user']) )  # raise at client TRexInUseError
            elif self.trex.get_status() != TRexStatus.Idle:
                logger.info("TRex is currently running, cannot reserve TRex unless in Idle state.")
                return Fault(-13, 'TRex is currently running, cannot reserve TRex unless in Idle state. Please try again when TRex run finished.')  # raise at client TRexInUseError
            else:
                logger.info("TRex is now reserved for user ({res_user}).".format( res_user = user ))
                self.__reservation = {'user' : user, 'since' : time.ctime()}
                logger.debug("Reservation details: "+ str(self.__reservation))
                return True

    def cancel_reservation (self, user):
        with self.start_lock:
            logger.info("Processing cancel_reservation() command.")
            if self.is_reserved():
                if self.__reservation['user'] == user:
                    logger.info("TRex reservation to {res_user} has been canceled successfully.".format(res_user = self.__reservation['user']))
                    self.__reservation = None
                    return True
                else:
                    logger.warning("TRex is reserved to different user than the provided one. Reservation wasn't canceled.")
                    return Fault(-33, "Cancel reservation request is available to the user that holds the reservation. Request denied")  # raise at client TRexRequestDenied
            
            else:
                logger.info("TRex is not reserved to anyone. No need to cancel anything")
                assert(self.__reservation is None)
                return False

            
    def start_trex(self, trex_cmd_options, user, block_to_success = True, timeout = 30):
        with self.start_lock:
            logger.info("Processing start_trex() command.")
            if self.is_reserved():
                # check if this is not the user to which TRex is reserved
                if self.__reservation['user'] != user:  
                    logger.info("TRex is reserved to another user ({res_user}). Only that user is allowed to initiate new runs.".format(res_user = self.__reservation['user']))
                    return Fault(-33, "TRex is reserved to another user ({res_user}). Only that user is allowed to initiate new runs.".format(res_user = self.__reservation['user']))  # raise at client TRexRequestDenied
            elif self.trex.get_status() != TRexStatus.Idle:
                logger.info("TRex is already taken, cannot create another run until done.")
                return Fault(-13, '')  # raise at client TRexInUseError
            
            try: 
                server_cmd_data = self.generate_run_cmd(**trex_cmd_options)
                self.zmq_monitor.first_dump = True
                self.trex.start_trex(self.TREX_PATH, server_cmd_data)
                logger.info("TRex session has been successfully initiated.")
                if block_to_success:
                    # delay server response until TRex is at 'Running' state.
                    start_time = time.time()
                    trex_state = None
                    while (time.time() - start_time) < timeout :
                        trex_state = self.trex.get_status()
                        if trex_state != TRexStatus.Starting:
                            break
                        else:
                            time.sleep(0.5)

                    # check for TRex run started normally
                    if trex_state == TRexStatus.Starting:   # reached timeout
                        logger.warning("TimeoutError: TRex initiation outcome could not be obtained, since TRex stays at Starting state beyond defined timeout.")
                        return Fault(-12, 'TimeoutError: TRex initiation outcome could not be obtained, since TRex stays at Starting state beyond defined timeout.') # raise at client TRexWarning
                    elif trex_state == TRexStatus.Idle:
                        return Fault(-11, self.trex.get_verbose_status())   # raise at client TRexError
                
                # reach here only if TRex is at 'Running' state
                self.trex.gen_seq()
                return self.trex.get_seq()          # return unique seq number to client
                        
            except TypeError as e:
                logger.error("TRex command generation failed, probably because either -f (traffic generation .yaml file) and -c (num of cores) was not specified correctly.\nReceived params: {params}".format( params = trex_cmd_options) )
                raise TypeError('TRex -f (traffic generation .yaml file) and -c (num of cores) must be specified.')


    def stop_trex(self, seq):
        logger.info("Processing stop_trex() command.")
        if self.trex.get_seq()== seq:
            logger.debug("Abort request legit since seq# match")
            return self.trex.stop_trex()
        else:
            if self.trex.get_status() != TRexStatus.Idle:
                logger.warning("Abort request is only allowed to process initiated the run. Request denied.")

                return Fault(-33, 'Abort request is only allowed to process initiated the run. Request denied.')  # raise at client TRexRequestDenied
            else:
                return False

    def force_trex_kill (self):
        logger.info("Processing force_trex_kill() command. --> Killing TRex session indiscriminately.")
        return self.trex.stop_trex()

    def wait_until_kickoff_finish (self, timeout = 40):
        # block until TRex exits Starting state
        logger.info("Processing wait_until_kickoff_finish() command.")
        trex_state = None
        start_time = time.time()
        while (time.time() - start_time) < timeout :
            trex_state = self.trex.get_status()
            if trex_state != TRexStatus.Starting:
                return
        return Fault(-12, 'TimeoutError: TRex initiation outcome could not be obtained, since TRex stays at Starting state beyond defined timeout.') # raise at client TRexWarning

    def get_running_info (self):
        logger.info("Processing get_running_info() command.")
        return self.trex.get_running_info()

    def generate_run_cmd (self, f, d, iom = 0, export_path="/tmp/trex.txt", **kwargs):
        """ generate_run_cmd(self, trex_cmd_options, export_path) -> str

        Generates a custom running command for the kick-off of the TRex traffic generator.
        Returns a tuple of command (string) and export path (string) to be issued on the trex server

        Parameters
        ----------
        trex_cmd_options : str
            Defines the exact command to run on the t-rex
            Example: "-c 2 -m 0.500000 -d 100 -f cap2/sfr.yaml --nc  -p -l 1000"
        export_path : str
            a full system path to which the results of the trex-run will be logged.

        """
        if 'results_file_path' in kwargs:
            export_path = kwargs['results_file_path']
            del kwargs['results_file_path']


        # adding additional options to the command
        trex_cmd_options = ''
        for key, value in kwargs.iteritems():
            tmp_key = key.replace('_','-')
            dash = ' -' if (len(key)==1) else ' --'
            if (value == True) and (str(value) != '1'):       # checking also int(value) to excape from situation that 1 translates by python to 'True'
                trex_cmd_options += (dash + tmp_key)
            else:
                trex_cmd_options += (dash + '{k} {val}'.format( k = tmp_key, val =  value ))

        cmd = "{run_command} -f {gen_file} -d {duration} --iom {io} {cmd_options} --no-key > {export}".format( # -- iom 0 disables the periodic log to the screen (not needed)
            run_command = self.TREX_START_CMD,
            gen_file    = f,
            duration    = d,
            cmd_options = trex_cmd_options,
            io          = iom, 
            export = export_path )

        logger.info("TREX FULL COMMAND: {command}".format(command = cmd) )

        return (cmd, export_path, long(d))

    def __check_trex_path_validity(self):
        # check for executable existance
        if not os.path.exists(self.TREX_PATH+'/t-rex-64'):
            print "The provided TRex path do not contain an executable TRex file.\nPlease check the path and retry."
            logger.error("The provided TRex path do not contain an executable TRex file")
            exit(-1)
        # check for executable permissions
        st = os.stat(self.TREX_PATH+'/t-rex-64')
        if not bool(st.st_mode & (stat.S_IXUSR ) ):
            print "The provided TRex path do not contain an TRex file with execution privileges.\nPlease check the files permissions and retry."
            logger.error("The provided TRex path do not contain an TRex file with execution privileges")
            exit(-1)
        else:
            return

    def __check_files_path_validity(self):
        # first, check for path existance. otherwise, try creating it with appropriate credentials
        if not os.path.exists(self.trex_files_path):
            try:
                os.makedirs(self.trex_files_path, 0660)
                return
            except os.error as inst:
                print "The provided files path does not exist and cannot be created with needed access credentials using root user.\nPlease check the path's permissions and retry."
                logger.error("The provided files path does not exist and cannot be created with needed access credentials using root user.")
                exit(-1)
        elif os.access(self.trex_files_path, os.W_OK):
            return
        else:
            print "The provided files path has insufficient access credentials for root user.\nPlease check the path's permissions and retry."
            logger.error("The provided files path has insufficient access credentials for root user")
            exit(-1)
Example #9
0
class CTRexServer(object):
    """This class defines the server side of the RESTfull interaction with TRex"""
    DEFAULT_TREX_PATH = '/auto/proj-pcube-b/apps/PL-b/tools/bp_sim2/v1.55/'  #'/auto/proj-pcube-b/apps/PL-b/tools/nightly/trex_latest'
    TREX_START_CMD = './t-rex-64'
    DEFAULT_FILE_PATH = '/tmp/trex_files/'

    def __init__(self,
                 trex_path,
                 trex_files_path,
                 trex_host='0.0.0.0',
                 trex_daemon_port=8090,
                 trex_zmq_port=4500):
        """ 
        Parameters
        ----------
        trex_host : str
            a string of the t-rex ip address or hostname.
            default value: machine hostname as fetched from socket.gethostname()
        trex_daemon_port : int
            the port number on which the trex-daemon server can be reached
            default value: 8090
        trex_zmq_port : int
            the port number on which trex's zmq module will interact with daemon server
            default value: 4500

        Instantiate a TRex client object, and connecting it to listening daemon-server
        """
        self.TREX_PATH = os.path.abspath(os.path.dirname(trex_path + '/'))
        self.trex_files_path = os.path.abspath(
            os.path.dirname(trex_files_path + '/'))
        self.__check_trex_path_validity()
        self.__check_files_path_validity()
        self.trex = CTRex()
        self.trex_version = None
        self.trex_host = trex_host
        self.trex_daemon_port = trex_daemon_port
        self.trex_zmq_port = trex_zmq_port
        self.trex_server_path = "http://{hostname}:{port}".format(
            hostname=trex_host, port=trex_daemon_port)
        self.start_lock = threading.Lock()
        self.__reservation = None
        self.zmq_monitor = ZmqMonitorSession(
            self.trex, self.trex_zmq_port
        )  # intiate single ZMQ monitor thread for server usage

    def add(self, x, y):
        print "server function add ", x, y
        logger.info(
            "Processing add function. Parameters are: {0}, {1} ".format(x, y))
        return x + y
        # return Fault(-10, "")

    def push_file(self, filename, bin_data):
        logger.info("Processing push_file() command.")
        try:
            filepath = os.path.abspath(
                os.path.join(self.trex_files_path, filename))
            with open(filepath, 'wb') as f:
                f.write(binascii.a2b_base64(bin_data))
            logger.info(
                "push_file() command finished. `{name}` was saved at {fpath}".
                format(name=filename, fpath=self.trex_files_path))
            return True
        except IOError as inst:
            logger.error("push_file method failed. " + str(inst))
            return False

    def connectivity_check(self):
        logger.info("Processing connectivity_check function.")
        return True

    def start(self):
        """This method fires up the daemon server based on initialized parameters of the class"""
        # initialize the server instance with given resources
        try:
            print "Firing up TRex REST daemon @ port {trex_port} ...\n".format(
                trex_port=self.trex_daemon_port)
            logger.info(
                "Firing up TRex REST daemon @ port {trex_port} ...".format(
                    trex_port=self.trex_daemon_port))
            logger.info("current working dir is: {0}".format(self.TREX_PATH))
            logger.info("current files dir is  : {0}".format(
                self.trex_files_path))
            logger.debug(
                "Starting TRex server. Registering methods to process.")
            logger.info(self.get_trex_version(base64=False))
            self.server = SimpleJSONRPCServer(
                (self.trex_host, self.trex_daemon_port))
        except socket.error as e:
            if e.errno == errno.EADDRINUSE:
                logger.error(
                    "TRex server requested address already in use. Aborting server launching."
                )
                print "TRex server requested address already in use. Aborting server launching."
                raise socket.error(
                    errno.EADDRINUSE,
                    "TRex daemon requested address already in use. "
                    "Server launch aborted. Please make sure no other process is "
                    "using the desired server properties.")
            elif isinstance(e, socket.gaierror) and e.errno == -3:
                # handling Temporary failure in name resolution exception
                raise socket.gaierror(
                    -3, "Temporary failure in name resolution.\n"
                    "Make sure provided hostname has DNS resolving.")
            else:
                raise

        # set further functionality and peripherals to server instance
        try:
            self.server.register_function(self.add)
            self.server.register_function(self.get_trex_log)
            self.server.register_function(self.get_trex_daemon_log)
            self.server.register_function(self.get_trex_version)
            self.server.register_function(self.connectivity_check)
            self.server.register_function(self.start_trex)
            self.server.register_function(self.stop_trex)
            self.server.register_function(self.wait_until_kickoff_finish)
            self.server.register_function(self.get_running_status)
            self.server.register_function(self.is_running)
            self.server.register_function(self.get_running_info)
            self.server.register_function(self.is_reserved)
            self.server.register_function(self.get_files_path)
            self.server.register_function(self.push_file)
            self.server.register_function(self.reserve_trex)
            self.server.register_function(self.cancel_reservation)
            self.server.register_function(self.force_trex_kill)
            signal.signal(signal.SIGTSTP, self.stop_handler)
            signal.signal(signal.SIGTERM, self.stop_handler)
            self.zmq_monitor.start()
            self.server.serve_forever()
        except KeyboardInterrupt:
            logger.info("Daemon shutdown request detected.")
        finally:
            self.zmq_monitor.join()  # close ZMQ monitor thread resources
            self.server.shutdown()
            pass

    # get files from Trex server and return their content (mainly for logs)
    @staticmethod
    def _pull_file(filepath):
        try:
            with open(filepath, 'rb') as f:
                file_content = f.read()
                return binascii.b2a_base64(file_content)
        except Exception as e:
            err_str = "Can't get requested file: {0}, possibly due to TRex that did not run".format(
                filepath)
            logger.error('{0}, error: {1}'.format(err_str, e))
            return Fault(-33, err_str)

    # get Trex log /tmp/trex.txt
    def get_trex_log(self):
        logger.info("Processing get_trex_log() command.")
        return self._pull_file('/tmp/trex.txt')

    # get daemon log /var/log/trex/trex_daemon_server.log
    def get_trex_daemon_log(self):
        logger.info("Processing get_trex_daemon_log() command.")
        return self._pull_file('/var/log/trex/trex_daemon_server.log')

    # get Trex version from ./t-rex-64 --help (last 4 lines)
    def get_trex_version(self, base64=True):
        try:
            logger.info("Processing get_trex_version() command.")
            if not self.trex_version:
                help_print = subprocess.Popen(['./t-rex-64', '--help'],
                                              cwd=self.TREX_PATH,
                                              stdout=subprocess.PIPE,
                                              stderr=subprocess.PIPE)
                help_print.wait()
                help_print_stdout = help_print.stdout.read()
                self.trex_version = binascii.b2a_base64('\n'.join(
                    help_print_stdout.split('\n')[-5:-1]))
            if base64:
                return self.trex_version
            else:
                return binascii.a2b_base64(self.trex_version)
        except Exception as e:
            err_str = "Can't get trex version, error: {0}".format(e)
            logger.error(err_str)
            return Fault(-33, err_str)

    def stop_handler(self, signum, frame):
        logger.info("Daemon STOP request detected.")
        if self.is_running():
            # in case TRex process is currently running, stop it before terminating server process
            self.stop_trex(self.trex.get_seq())
        sys.exit(0)

    def is_running(self):
        run_status = self.trex.get_status()
        logger.info(
            "Processing is_running() command. Running status is: {stat}".
            format(stat=run_status))
        if run_status == TRexStatus.Running:
            return True
        else:
            return False

    def is_reserved(self):
        logger.info("Processing is_reserved() command.")
        return bool(self.__reservation)

    def get_running_status(self):
        run_status = self.trex.get_status()
        logger.info(
            "Processing get_running_status() command. Running status is: {stat}"
            .format(stat=run_status))
        return {
            'state': run_status.value,
            'verbose': self.trex.get_verbose_status()
        }

    def get_files_path(self):
        logger.info("Processing get_files_path() command.")
        return self.trex_files_path

    def reserve_trex(self, user):
        if user == "":
            logger.info(
                "TRex reservation cannot apply to empty string user. Request denied."
            )
            return Fault(
                -33,
                "TRex reservation cannot apply to empty string user. Request denied."
            )

        with self.start_lock:
            logger.info("Processing reserve_trex() command.")
            if self.is_reserved():
                if user == self.__reservation['user']:
                    # return True is the same user is asking and already has the resrvation
                    logger.info(
                        "the same user is asking and already has the resrvation. Re-reserving TRex."
                    )
                    return True

                logger.info(
                    "TRex is already reserved to another user ({res_user}), cannot reserve to another user."
                    .format(res_user=self.__reservation['user']))
                return Fault(
                    -33,
                    "TRex is already reserved to another user ({res_user}). Please make sure TRex is free before reserving it."
                    .format(res_user=self.__reservation['user']
                            ))  # raise at client TRexInUseError
            elif self.trex.get_status() != TRexStatus.Idle:
                logger.info(
                    "TRex is currently running, cannot reserve TRex unless in Idle state."
                )
                return Fault(
                    -13,
                    'TRex is currently running, cannot reserve TRex unless in Idle state. Please try again when TRex run finished.'
                )  # raise at client TRexInUseError
            else:
                logger.info(
                    "TRex is now reserved for user ({res_user}).".format(
                        res_user=user))
                self.__reservation = {'user': user, 'since': time.ctime()}
                logger.debug("Reservation details: " + str(self.__reservation))
                return True

    def cancel_reservation(self, user):
        with self.start_lock:
            logger.info("Processing cancel_reservation() command.")
            if self.is_reserved():
                if self.__reservation['user'] == user:
                    logger.info(
                        "TRex reservation to {res_user} has been canceled successfully."
                        .format(res_user=self.__reservation['user']))
                    self.__reservation = None
                    return True
                else:
                    logger.warning(
                        "TRex is reserved to different user than the provided one. Reservation wasn't canceled."
                    )
                    return Fault(
                        -33,
                        "Cancel reservation request is available to the user that holds the reservation. Request denied"
                    )  # raise at client TRexRequestDenied

            else:
                logger.info(
                    "TRex is not reserved to anyone. No need to cancel anything"
                )
                assert (self.__reservation is None)
                return False

    def start_trex(self,
                   trex_cmd_options,
                   user,
                   block_to_success=True,
                   timeout=30):
        with self.start_lock:
            logger.info("Processing start_trex() command.")
            if self.is_reserved():
                # check if this is not the user to which TRex is reserved
                if self.__reservation['user'] != user:
                    logger.info(
                        "TRex is reserved to another user ({res_user}). Only that user is allowed to initiate new runs."
                        .format(res_user=self.__reservation['user']))
                    return Fault(
                        -33,
                        "TRex is reserved to another user ({res_user}). Only that user is allowed to initiate new runs."
                        .format(res_user=self.__reservation['user']
                                ))  # raise at client TRexRequestDenied
            elif self.trex.get_status() != TRexStatus.Idle:
                logger.info(
                    "TRex is already taken, cannot create another run until done."
                )
                return Fault(-13, '')  # raise at client TRexInUseError

            try:
                server_cmd_data = self.generate_run_cmd(**trex_cmd_options)
                self.zmq_monitor.first_dump = True
                self.trex.start_trex(self.TREX_PATH, server_cmd_data)
                logger.info("TRex session has been successfully initiated.")
                if block_to_success:
                    # delay server response until TRex is at 'Running' state.
                    start_time = time.time()
                    trex_state = None
                    while (time.time() - start_time) < timeout:
                        trex_state = self.trex.get_status()
                        if trex_state != TRexStatus.Starting:
                            break
                        else:
                            time.sleep(0.5)

                    # check for TRex run started normally
                    if trex_state == TRexStatus.Starting:  # reached timeout
                        logger.warning(
                            "TimeoutError: TRex initiation outcome could not be obtained, since TRex stays at Starting state beyond defined timeout."
                        )
                        return Fault(
                            -12,
                            'TimeoutError: TRex initiation outcome could not be obtained, since TRex stays at Starting state beyond defined timeout.'
                        )  # raise at client TRexWarning
                    elif trex_state == TRexStatus.Idle:
                        return Fault(-11, self.trex.get_verbose_status()
                                     )  # raise at client TRexError

                # reach here only if TRex is at 'Running' state
                self.trex.gen_seq()
                return self.trex.get_seq(
                )  # return unique seq number to client

            except TypeError as e:
                logger.error(
                    "TRex command generation failed, probably because either -f (traffic generation .yaml file) and -c (num of cores) was not specified correctly.\nReceived params: {params}"
                    .format(params=trex_cmd_options))
                raise TypeError(
                    'TRex -f (traffic generation .yaml file) and -c (num of cores) must be specified.'
                )

    def stop_trex(self, seq):
        logger.info("Processing stop_trex() command.")
        if self.trex.get_seq() == seq:
            logger.debug("Abort request legit since seq# match")
            return self.trex.stop_trex()
        else:
            if self.trex.get_status() != TRexStatus.Idle:
                logger.warning(
                    "Abort request is only allowed to process initiated the run. Request denied."
                )

                return Fault(
                    -33,
                    'Abort request is only allowed to process initiated the run. Request denied.'
                )  # raise at client TRexRequestDenied
            else:
                return False

    def force_trex_kill(self):
        logger.info(
            "Processing force_trex_kill() command. --> Killing TRex session indiscriminately."
        )
        return self.trex.stop_trex()

    def wait_until_kickoff_finish(self, timeout=40):
        # block until TRex exits Starting state
        logger.info("Processing wait_until_kickoff_finish() command.")
        trex_state = None
        start_time = time.time()
        while (time.time() - start_time) < timeout:
            trex_state = self.trex.get_status()
            if trex_state != TRexStatus.Starting:
                return
        return Fault(
            -12,
            'TimeoutError: TRex initiation outcome could not be obtained, since TRex stays at Starting state beyond defined timeout.'
        )  # raise at client TRexWarning

    def get_running_info(self):
        logger.info("Processing get_running_info() command.")
        return self.trex.get_running_info()

    def generate_run_cmd(self,
                         f,
                         d,
                         iom=0,
                         export_path="/tmp/trex.txt",
                         **kwargs):
        """ generate_run_cmd(self, trex_cmd_options, export_path) -> str

        Generates a custom running command for the kick-off of the TRex traffic generator.
        Returns a tuple of command (string) and export path (string) to be issued on the trex server

        Parameters
        ----------
        trex_cmd_options : str
            Defines the exact command to run on the t-rex
            Example: "-c 2 -m 0.500000 -d 100 -f cap2/sfr.yaml --nc  -p -l 1000"
        export_path : str
            a full system path to which the results of the trex-run will be logged.

        """
        if 'results_file_path' in kwargs:
            export_path = kwargs['results_file_path']
            del kwargs['results_file_path']

        # adding additional options to the command
        trex_cmd_options = ''
        for key, value in kwargs.iteritems():
            tmp_key = key.replace('_', '-')
            dash = ' -' if (len(key) == 1) else ' --'
            if (value == True) and (
                    str(value) != '1'
            ):  # checking also int(value) to excape from situation that 1 translates by python to 'True'
                trex_cmd_options += (dash + tmp_key)
            else:
                trex_cmd_options += (dash +
                                     '{k} {val}'.format(k=tmp_key, val=value))

        cmd = "{run_command} -f {gen_file} -d {duration} --iom {io} {cmd_options} --no-key > {export}".format(  # -- iom 0 disables the periodic log to the screen (not needed)
            run_command=self.TREX_START_CMD,
            gen_file=f,
            duration=d,
            cmd_options=trex_cmd_options,
            io=iom,
            export=export_path)

        logger.info("TREX FULL COMMAND: {command}".format(command=cmd))

        return (cmd, export_path, long(d))

    def __check_trex_path_validity(self):
        # check for executable existance
        if not os.path.exists(self.TREX_PATH + '/t-rex-64'):
            print "The provided TRex path do not contain an executable TRex file.\nPlease check the path and retry."
            logger.error(
                "The provided TRex path do not contain an executable TRex file"
            )
            exit(-1)
        # check for executable permissions
        st = os.stat(self.TREX_PATH + '/t-rex-64')
        if not bool(st.st_mode & (stat.S_IXUSR)):
            print "The provided TRex path do not contain an TRex file with execution privileges.\nPlease check the files permissions and retry."
            logger.error(
                "The provided TRex path do not contain an TRex file with execution privileges"
            )
            exit(-1)
        else:
            return

    def __check_files_path_validity(self):
        # first, check for path existance. otherwise, try creating it with appropriate credentials
        if not os.path.exists(self.trex_files_path):
            try:
                os.makedirs(self.trex_files_path, 0660)
                return
            except os.error as inst:
                print "The provided files path does not exist and cannot be created with needed access credentials using root user.\nPlease check the path's permissions and retry."
                logger.error(
                    "The provided files path does not exist and cannot be created with needed access credentials using root user."
                )
                exit(-1)
        elif os.access(self.trex_files_path, os.W_OK):
            return
        else:
            print "The provided files path has insufficient access credentials for root user.\nPlease check the path's permissions and retry."
            logger.error(
                "The provided files path has insufficient access credentials for root user"
            )
            exit(-1)
Example #10
0
class CTRexServer(object):
    """This class defines the server side of the RESTfull interaction with TRex"""
    TREX_START_CMD = './t-rex-64'
    DEFAULT_FILE_PATH = '/tmp/trex_files/'

    def __init__(self,
                 trex_path,
                 trex_files_path,
                 trex_host='0.0.0.0',
                 trex_daemon_port=8090,
                 trex_zmq_port=4500,
                 trex_nice=-19):
        """ 
        Parameters
        ----------
        trex_host : str
            a string of the TRex ip address or hostname.
            default value: machine hostname as fetched from socket.gethostname()
        trex_daemon_port : int
            the port number on which the trex-daemon server can be reached
            default value: 8090
        trex_zmq_port : int
            default TRex ZMQ publisher port to listen to (if not specified in config file).
            default value: 4500
        nice: int
            priority of the TRex process

        Instantiate a TRex client object, and connecting it to listening daemon-server
        """
        self.TREX_PATH = os.path.abspath(os.path.dirname(trex_path + '/'))
        self.trex_files_path = os.path.abspath(
            os.path.dirname(trex_files_path + '/'))
        self.__check_trex_path_validity()
        self.__check_files_path_validity()
        self.trex = CTRex()
        self.trex_version = None
        self.trex_host = trex_host
        self.trex_daemon_port = trex_daemon_port
        self.trex_zmq_port = trex_zmq_port
        self.trex_server_path = "http://{hostname}:{port}".format(
            hostname=trex_host, port=trex_daemon_port)
        self.start_lock = threading.Lock()
        self.__reservation = None
        self.trex_nice = int(trex_nice)
        if self.trex_nice < -20 or self.trex_nice > 19:
            err = "Parameter 'nice' should be integer in range [-20, 19]"
            print(err)
            logger.error(err)
            raise Exception(err)

    def add(self, x, y):
        logger.info(
            "Processing add function. Parameters are: {0}, {1} ".format(x, y))
        return x + y
        # return Fault(-10, "")

    # Get information about available network interfaces
    def get_devices_info(self):
        logger.info("Processing get_devices_info() command.")
        try:
            args = [
                os.path.join(self.TREX_PATH, 'dpdk_nic_bind.py'), '-s',
                '--json'
            ]
            result = subprocess.check_output(args,
                                             cwd=self.TREX_PATH,
                                             universal_newlines=True)
            return json.loads(result)
        except Exception as e:
            err_str = "Error processing get_devices_info(): %s" % e
            logger.error(e)
            return Fault(-33, err_str)

    def push_file(self, filename, bin_data):
        logger.info("Processing push_file() command.")
        try:
            filepath = os.path.join(self.trex_files_path,
                                    os.path.basename(filename))
            with open(filepath, 'wb') as f:
                f.write(binascii.a2b_base64(bin_data))
            logger.info("push_file() command finished. File is saved as %s" %
                        filepath)
            return True
        except IOError as inst:
            logger.error("push_file method failed. " + str(inst))
            return False

    def connectivity_check(self):
        logger.info("Processing connectivity_check function.")
        return True

    def start(self):
        """This method fires up the daemon server based on initialized parameters of the class"""
        # initialize the server instance with given resources
        try:
            print("Firing up TRex REST daemon @ port {trex_port} ...\n".format(
                trex_port=self.trex_daemon_port))
            logger.info(
                "Firing up TRex REST daemon @ port {trex_port} ...".format(
                    trex_port=self.trex_daemon_port))
            logger.info("current working dir is: {0}".format(self.TREX_PATH))
            logger.info("current files dir is  : {0}".format(
                self.trex_files_path))
            logger.debug(
                "Starting TRex server. Registering methods to process.")
            logger.info(self.get_trex_version(base64=False))
            self.server = SimpleJSONRPCServer(
                (self.trex_host, self.trex_daemon_port))
        except socket.error as e:
            if e.errno == errno.EADDRINUSE:
                logger.error(
                    "TRex server requested address already in use. Aborting server launching."
                )
                print(
                    "TRex server requested address already in use. Aborting server launching."
                )
                raise socket.error(
                    errno.EADDRINUSE,
                    "TRex daemon requested address already in use. "
                    "Server launch aborted. Please make sure no other process is "
                    "using the desired server properties.")
            elif isinstance(e, socket.gaierror) and e.errno == -3:
                # handling Temporary failure in name resolution exception
                raise socket.gaierror(
                    -3, "Temporary failure in name resolution.\n"
                    "Make sure provided hostname has DNS resolving.")
            else:
                raise

        # set further functionality and peripherals to server instance
        self.server.register_function(self.add)
        self.server.register_function(self.get_devices_info)
        self.server.register_function(self.cancel_reservation)
        self.server.register_function(self.connectivity_check)
        self.server.register_function(self.connectivity_check,
                                      'check_connectivity')  # alias
        self.server.register_function(self.force_trex_kill)
        self.server.register_function(self.get_file)
        self.server.register_function(self.get_files_list)
        self.server.register_function(self.get_files_path)
        self.server.register_function(self.get_latest_dump)
        self.server.register_function(self.get_running_info)
        self.server.register_function(self.get_running_status)
        self.server.register_function(self.get_trex_cmds)
        self.server.register_function(self.get_trex_config)
        self.server.register_function(self.get_trex_config_metadata)
        self.server.register_function(self.get_trex_daemon_log)
        self.server.register_function(self.get_trex_log)
        self.server.register_function(self.get_trex_version)
        self.server.register_function(self.is_reserved)
        self.server.register_function(self.is_running)
        self.server.register_function(self.kill_all_trexes)
        self.server.register_function(self.push_file)
        self.server.register_function(self.reserve_trex)
        self.server.register_function(self.start_trex)
        self.server.register_function(self.stop_trex)
        self.server.register_function(self.wait_until_kickoff_finish)
        signal.signal(signal.SIGTSTP, self.stop_handler)
        signal.signal(signal.SIGTERM, self.stop_handler)
        try:
            self.server.serve_forever()
        except KeyboardInterrupt:
            logger.info("Daemon shutdown request detected.")
        finally:
            self.server.shutdown()
            #self.server.server_close()

    # get files from Trex server and return their content (mainly for logs)
    @staticmethod
    def _pull_file(filepath):
        try:
            with open(filepath, 'rb') as f:
                file_content = f.read()
                return binascii.b2a_base64(file_content).decode(
                    errors='replace')
        except Exception as e:
            err_str = "Can't get requested file %s: %s" % (filepath, e)
            logger.error(err_str)
            return Fault(-33, err_str)

    # returns True if given path is under TRex package or under /tmp/trex_files
    def _check_path_under_TRex_or_temp(self, path):
        if not os.path.relpath(path, self.trex_files_path).startswith(
                os.pardir):
            return True
        if not os.path.relpath(path, self.TREX_PATH).startswith(os.pardir):
            return True
        return False

    # gets the file content encoded base64 either from /tmp/trex_files or TRex server dir
    def get_file(self, filepath):
        try:
            logger.info("Processing get_file() command.")
            if not self._check_path_under_TRex_or_temp(filepath):
                raise Exception(
                    'Given path should be under current TRex package or /tmp/trex_files'
                )
            return self._pull_file(filepath)
        except Exception as e:
            err_str = "Can't get requested file %s: %s" % (filepath, e)
            logger.error(err_str)
            return Fault(-33, err_str)

    # get tuple (dirs, files) with directories and files lists from given path (limited under TRex package or /tmp/trex_files)
    def get_files_list(self, path):
        try:
            logger.info("Processing get_files_list() command, given path: %s" %
                        path)
            if not self._check_path_under_TRex_or_temp(path):
                raise Exception(
                    'Given path should be under current TRex package or /tmp/trex_files'
                )
            return os.walk(path).next()[1:3]
        except Exception as e:
            err_str = "Error processing get_files_list(): %s" % e
            logger.error(err_str)
            return Fault(-33, err_str)

    # get Trex log /tmp/trex.txt
    def get_trex_log(self):
        logger.info("Processing get_trex_log() command.")
        return self._pull_file('/tmp/trex.txt')

    # get /etc/trex_cfg.yaml
    def get_trex_config(self):
        logger.info("Processing get_trex_config() command.")
        return self._pull_file('/etc/trex_cfg.yaml')

    #get metadata used to generate trex_cfg.yaml
    def get_trex_config_metadata(self):
        logger.info("Processing get_trex_config_metadata() command.")
        metadata_json_path = os.path.abspath(
            os.path.join(os.path.dirname(__file__), 'config_metadata.json'))
        try:
            with open(metadata_json_path) as f:
                return json.load(f)
        except Exception as e:
            return Fault(-33, "Can't load config metadata contents: %s" % e)

    # get daemon log /var/log/trex/trex_daemon_server.log
    def get_trex_daemon_log(self):
        logger.info("Processing get_trex_daemon_log() command.")
        return self._pull_file('/var/log/trex/trex_daemon_server.log')

    # get Trex version from ./t-rex-64 --help (last lines starting with "Version : ...")
    def get_trex_version(self, base64=True):
        try:
            logger.info("Processing get_trex_version() command.")
            if not self.trex_version:
                ret_code, stdout, stderr = run_command('./t-rex-64 --help',
                                                       cwd=self.TREX_PATH)
                search_result = re.search('\n\s*(Version\s*:.+)', stdout,
                                          re.DOTALL)
                if not search_result:
                    raise Exception(
                        'Could not determine version from ./t-rex-64 --help. Stdout: %s. Stderr: %s'
                        % (stdout, stderr))
                self.trex_version = binascii.b2a_base64(
                    search_result.group(1).encode(errors='replace'))
            if base64:
                return self.trex_version.decode(errors='replace')
            else:
                return binascii.a2b_base64(
                    self.trex_version).decode(errors='replace')
        except Exception as e:
            err_str = "Can't get trex version, error: %s" % e
            logger.error(err_str)
            return Fault(-33, err_str)

    def stop_handler(self, *args, **kwargs):
        logger.info("Daemon STOP request detected.")
        if self.is_running():
            # in case TRex process is currently running, stop it before terminating server process
            self.stop_trex(self.trex.get_seq())
        sys.exit(0)

    def assert_zmq_ok(self, check_alive=True):
        if self.trex.zmq_error:
            self.trex.zmq_error, err = None, self.trex.zmq_error
            raise Exception('ZMQ thread got error: %s' % err)
        if check_alive and self.trex.zmq_monitor and not self.trex.zmq_monitor.is_alive(
        ):
            if self.trex.get_status() != TRexStatus.Idle:
                self.force_trex_kill()
            raise Exception('ZMQ thread is dead.')

    def is_running(self):
        run_status = self.trex.get_status()
        logger.info(
            "Processing is_running() command. Running status is: {stat}".
            format(stat=run_status))
        if run_status == TRexStatus.Running:
            return True
        else:
            return False

    def is_reserved(self):
        logger.info("Processing is_reserved() command.")
        return bool(self.__reservation)

    def get_running_status(self):
        self.assert_zmq_ok(check_alive=False)
        run_status = self.trex.get_status()
        logger.info(
            "Processing get_running_status() command. Running status is: {stat}"
            .format(stat=run_status))
        return {
            'state': run_status.value,
            'verbose': self.trex.get_verbose_status()
        }

    def get_files_path(self):
        logger.info("Processing get_files_path() command.")
        return self.trex_files_path

    def reserve_trex(self, user):
        if user == "":
            logger.info(
                "TRex reservation cannot apply to empty string user. Request denied."
            )
            return Fault(
                -33,
                "TRex reservation cannot apply to empty string user. Request denied."
            )

        with self.start_lock:
            logger.info("Processing reserve_trex() command.")
            if self.is_reserved():
                if user == self.__reservation['user']:
                    # return True is the same user is asking and already has the resrvation
                    logger.info(
                        "the same user is asking and already has the resrvation. Re-reserving TRex."
                    )
                    return True

                logger.info(
                    "TRex is already reserved to another user ({res_user}), cannot reserve to another user."
                    .format(res_user=self.__reservation['user']))
                return Fault(
                    -33,
                    "TRex is already reserved to another user ({res_user}). Please make sure TRex is free before reserving it."
                    .format(res_user=self.__reservation['user']
                            ))  # raise at client TRexInUseError
            elif self.trex.get_status() != TRexStatus.Idle:
                logger.info(
                    "TRex is currently running, cannot reserve TRex unless in Idle state."
                )
                return Fault(
                    -13,
                    'TRex is currently running, cannot reserve TRex unless in Idle state. Please try again when TRex run finished.'
                )  # raise at client TRexInUseError
            else:
                logger.info(
                    "TRex is now reserved for user ({res_user}).".format(
                        res_user=user))
                self.__reservation = {'user': user, 'since': time.ctime()}
                logger.debug("Reservation details: " + str(self.__reservation))
                return True

    def cancel_reservation(self, user):
        with self.start_lock:
            logger.info("Processing cancel_reservation() command.")
            if self.is_reserved():
                if self.__reservation['user'] == user:
                    logger.info(
                        "TRex reservation to {res_user} has been canceled successfully."
                        .format(res_user=self.__reservation['user']))
                    self.__reservation = None
                    return True
                else:
                    logger.warning(
                        "TRex is reserved to different user than the provided one. Reservation wasn't canceled."
                    )
                    return Fault(
                        -33,
                        "Cancel reservation request is available to the user that holds the reservation. Request denied"
                    )  # raise at client TRexRequestDenied

            else:
                logger.info(
                    "TRex is not reserved to anyone. No need to cancel anything"
                )
                assert (self.__reservation is None)
                return False

    def start_trex(self,
                   trex_cmd_options,
                   user,
                   block_to_success=True,
                   timeout=40,
                   stateless=False,
                   debug_image=False,
                   trex_args=''):
        self.trex.zmq_error = None
        with self.start_lock:
            logger.info("Processing start_trex() command.")
            if self.is_reserved():
                # check if this is not the user to which TRex is reserved
                if self.__reservation['user'] != user:
                    logger.info(
                        "TRex is reserved to another user ({res_user}). Only that user is allowed to initiate new runs."
                        .format(res_user=self.__reservation['user']))
                    return Fault(
                        -33,
                        "TRex is reserved to another user ({res_user}). Only that user is allowed to initiate new runs."
                        .format(res_user=self.__reservation['user']
                                ))  # raise at client TRexRequestDenied
            elif self.trex.get_status() != TRexStatus.Idle:
                err = 'TRex is already taken, cannot create another run until done.'
                logger.info(err)
                return Fault(-13, err)  # raise at client TRexInUseError

            try:
                server_cmd_data, zmq_port = self.generate_run_cmd(
                    stateless=stateless,
                    debug_image=debug_image,
                    trex_args=trex_args,
                    **trex_cmd_options)
                self.trex.start_trex(self.TREX_PATH, server_cmd_data, zmq_port)
                logger.info("TRex session has been successfully initiated.")
                if block_to_success:
                    # delay server response until TRex is at 'Running' state.
                    start_time = time.time()
                    trex_state = None
                    while (time.time() - start_time) < timeout:
                        trex_state = self.trex.get_status()
                        if trex_state != TRexStatus.Starting:
                            break
                        else:
                            time.sleep(0.5)
                            self.assert_zmq_ok()

                    # check for TRex run started normally
                    if trex_state == TRexStatus.Starting:  # reached timeout
                        logger.warning(
                            "TimeoutError: TRex initiation outcome could not be obtained, since TRex stays at Starting state beyond defined timeout."
                        )
                        return Fault(
                            -12,
                            'TimeoutError: TRex initiation outcome could not be obtained, since TRex stays at Starting state beyond defined timeout.'
                        )  # raise at client TRexWarning
                    elif trex_state == TRexStatus.Idle:
                        return Fault(-11, self.trex.get_verbose_status()
                                     )  # raise at client TRexError

                # reach here only if TRex is at 'Running' state
                self.trex.gen_seq()
                return self.trex.get_seq(
                )  # return unique seq number to client

            except TypeError as e:
                logger.error(
                    "TRex command generation failed, probably because either -f (traffic generation .yaml file) and -c (num of cores) was not specified correctly.\nReceived params: {params}"
                    .format(params=trex_cmd_options))
                raise TypeError(
                    'TRex -f (traffic generation .yaml file) and -c (num of cores) must be specified. %s'
                    % e)

    def stop_trex(self, seq):
        logger.info("Processing stop_trex() command.")
        if self.trex.get_seq() == seq:
            logger.debug("Abort request legit since seq# match")
            return self.trex.stop_trex()
        else:
            if self.trex.get_status() != TRexStatus.Idle:
                logger.warning(
                    "Abort request is only allowed to process initiated the run. Request denied."
                )

                return Fault(
                    -33,
                    'Abort request is only allowed to process initiated the run. Request denied.'
                )  # raise at client TRexRequestDenied
            else:
                return False

    def force_trex_kill(self):
        logger.info(
            "Processing force_trex_kill() command. --> Killing TRex session indiscriminately."
        )
        return self.trex.stop_trex()

    # returns list of tuples (pid, command line) of running TRex(es)
    def get_trex_cmds(self):
        logger.info('Processing get_trex_cmds() command.')
        ret_code, stdout, stderr = run_command(
            'ps -u root --format pid,comm,cmd')
        if ret_code:
            raise Exception(
                'Failed to determine running processes. Stdout: %s. Stderr: %s'
                % (stdout, stderr))
        trex_cmds_list = []
        for line in stdout.splitlines():
            pid, proc_name, full_cmd = line.strip().split(' ', 2)
            pid = pid.strip()
            full_cmd = full_cmd.strip()
            if proc_name.find('_t-rex-64') >= 0:
                trex_cmds_list.append((pid, full_cmd))
        return trex_cmds_list

    # Silently tries to kill TRexes with given signal.
    # Responsibility of client to verify with get_trex_cmds.
    def kill_all_trexes(self, signal_name):
        logger.info('Processing kill_all_trexes() command.')
        trex_cmds_list = self.get_trex_cmds()
        for pid, cmd in trex_cmds_list:
            logger.info('Killing with signal %s process %s %s' %
                        (signal_name, pid, cmd))
            try:
                os.kill(int(pid), signal_name)
            except OSError as e:
                if e.errno == errno.ESRCH:
                    logger.info('No such process, ignoring.')
                raise

    def wait_until_kickoff_finish(self, timeout=40):
        # block until TRex exits Starting state
        logger.info("Processing wait_until_kickoff_finish() command.")
        start_time = time.time()
        while (time.time() - start_time) < timeout:
            self.assert_zmq_ok()
            trex_state = self.trex.get_status()
            if trex_state != TRexStatus.Starting:
                return
            time.sleep(0.1)
        return Fault(
            -12,
            'TimeoutError: TRex initiation outcome could not be obtained, since TRex stays at Starting state beyond defined timeout.'
        )  # raise at client TRexWarning

    def get_running_info(self):
        logger.info("Processing get_running_info() command.")
        self.assert_zmq_ok(check_alive=False)
        return self.trex.get_running_info()

    def get_latest_dump(self):
        logger.info("Processing get_latest_dump() command.")
        self.assert_zmq_ok(check_alive=False)
        return self.trex.get_latest_dump()

    def generate_run_cmd(self,
                         iom=0,
                         export_path="/tmp/trex.txt",
                         stateless=False,
                         debug_image=False,
                         trex_args='',
                         **kwargs):
        """ generate_run_cmd(self, iom, export_path, kwargs) -> str

        Generates a custom running command for the kick-off of the TRex traffic generator.
        Returns a tuple of command (string) and export path (string) to be issued on the trex server

        Parameters
        ----------
        iom: int
            0 = don't print stats screen to log, 1 = print stats (can generate huge logs)
        stateless: boolean
            True = run as stateless, False = require -f and -d arguments
        kwargs: dictionary
            Dictionary of parameters for trex. For example: (c=1, nc=True, l_pkt_mode=3).
            Notice that when sending command line parameters that has -, you need to replace it with _.
            for example, to have on command line "--l-pkt-mode 3", you need to send l_pkt_mode=3
        export_path : str
            Full system path to which the results of the trex-run will be logged.

        """
        if 'results_file_path' in kwargs:
            export_path = kwargs['results_file_path']
            del kwargs['results_file_path']
        if stateless:
            kwargs['i'] = True

        # adding additional options to the command
        trex_cmd_options = ''
        for key, value in kwargs.items():
            tmp_key = key.replace('_', '-').lstrip('-')
            dash = ' -' if (len(key) == 1) else ' --'
            if value is True:
                trex_cmd_options += (dash + tmp_key)
            elif value is False:
                continue
            else:
                trex_cmd_options += (dash +
                                     '{k} {val}'.format(k=tmp_key, val=value))
        if trex_args:
            trex_cmd_options += ' %s' % trex_args

        zmq_port = self._check_zmq_port(trex_cmd_options)

        if not stateless:
            if 'f' not in kwargs:
                raise Exception(
                    'Argument -f should be specified in stateful command')
            if 'd' not in kwargs:
                raise Exception(
                    'Argument -d should be specified in stateful command')

        cmd = "{nice}{run_command}{debug_image} --iom {io} {cmd_options} --no-key".format(  # -- iom 0 disables the periodic log to the screen (not needed)
            nice='' if self.trex_nice == 0 else 'nice -n %s ' % self.trex_nice,
            run_command=self.TREX_START_CMD,
            debug_image='-debug' if debug_image else '',
            cmd_options=trex_cmd_options,
            io=iom)

        logger.info("TREX FULL COMMAND: {command}".format(command=cmd))

        return (cmd, export_path, kwargs.get('d', 0)), zmq_port

    def _check_zmq_port(self, trex_cmd_options):
        parser = ArgumentParser()
        parser.add_argument('--cfg', default='/etc/trex_cfg.yaml')
        args, _ = parser.parse_known_args(shlex.split(trex_cmd_options))
        if not os.path.exists(args.cfg):
            raise Exception('Platform config file "%s" does not exist!' %
                            args.cfg)
        with open(args.cfg) as f:
            trex_cfg = yaml.safe_load(f.read())
        if type(trex_cfg) is not list:
            raise Exception(
                'Platform config file "%s" content should be array.' %
                args.cfg)
        if not len(trex_cfg):
            raise Exception(
                'Platform config file "%s" content should be array with one element.'
                % args.cfg)
        trex_cfg = trex_cfg[0]
        if 'enable_zmq_pub' in trex_cfg and trex_cfg['enable_zmq_pub'] == False:
            raise Exception(
                'TRex daemon expects ZMQ publisher to be enabled. Please change "enable_zmq_pub" to true.'
            )
        if 'zmq_pub_port' in trex_cfg:
            return trex_cfg['zmq_pub_port']
        return self.trex_zmq_port

    def __check_trex_path_validity(self):
        # check for executable existance
        if not os.path.exists(self.TREX_PATH + '/t-rex-64'):
            print(
                "The provided TRex path do not contain an executable TRex file.\nPlease check the path and retry."
            )
            logger.error(
                "The provided TRex path do not contain an executable TRex file"
            )
            exit(-1)
        # check for executable permissions
        st = os.stat(self.TREX_PATH + '/t-rex-64')
        if not bool(st.st_mode & (stat.S_IXUSR)):
            print(
                "The provided TRex path do not contain an TRex file with execution privileges.\nPlease check the files permissions and retry."
            )
            logger.error(
                "The provided TRex path do not contain an TRex file with execution privileges"
            )
            exit(-1)
        else:
            return

    def __check_files_path_validity(self):
        # first, check for path existance. otherwise, try creating it with appropriate credentials
        if not os.path.exists(self.trex_files_path):
            try:
                os.makedirs(self.trex_files_path, 0o660)
                return
            except os.error as inst:
                print(
                    "The provided files path does not exist and cannot be created with needed access credentials using root user.\nPlease check the path's permissions and retry."
                )
                logger.error(
                    "The provided files path does not exist and cannot be created with needed access credentials using root user."
                )
                exit(-1)
        elif os.access(self.trex_files_path, os.W_OK):
            return
        else:
            print(
                "The provided files path has insufficient access credentials for root user.\nPlease check the path's permissions and retry."
            )
            logger.error(
                "The provided files path has insufficient access credentials for root user"
            )
            exit(-1)
Example #11
0
    def shutdown(self):
        for service in self.registered_services:
            self.unregister_service(service)

        SimpleJSONRPCServer.shutdown(self)
Example #12
0
class CTRexServer(object):
    """This class defines the server side of the RESTfull interaction with TRex"""
    DEFAULT_TREX_PATH = '/auto/proj-pcube-b/apps/PL-b/tools/bp_sim2/v1.55/' #'/auto/proj-pcube-b/apps/PL-b/tools/nightly/trex_latest'
    TREX_START_CMD    = './t-rex-64'
    DEFAULT_FILE_PATH = '/tmp/trex_files/'

    def __init__(self, trex_path, trex_files_path, trex_host='0.0.0.0', trex_daemon_port=8090, trex_zmq_port=4500, trex_nice=-19):
        """ 
        Parameters
        ----------
        trex_host : str
            a string of the TRex ip address or hostname.
            default value: machine hostname as fetched from socket.gethostname()
        trex_daemon_port : int
            the port number on which the trex-daemon server can be reached
            default value: 8090
        trex_zmq_port : int
            the port number on which trex's zmq module will interact with daemon server
            default value: 4500
        nice: int
            priority of the TRex process

        Instantiate a TRex client object, and connecting it to listening daemon-server
        """
        self.TREX_PATH          = os.path.abspath(os.path.dirname(trex_path+'/'))
        self.trex_files_path    = os.path.abspath(os.path.dirname(trex_files_path+'/'))
        self.__check_trex_path_validity()
        self.__check_files_path_validity()
        self.trex               = CTRex()
        self.trex_version       = None
        self.trex_host          = trex_host
        self.trex_daemon_port   = trex_daemon_port
        self.trex_zmq_port      = trex_zmq_port
        self.trex_server_path   = "http://{hostname}:{port}".format( hostname = trex_host, port = trex_daemon_port )
        self.start_lock         = threading.Lock()
        self.__reservation      = None
        self.zmq_monitor        = ZmqMonitorSession(self.trex, self.trex_zmq_port)    # intiate single ZMQ monitor thread for server usage
        self.trex_nice          = int(trex_nice)
        if self.trex_nice < -20 or self.trex_nice > 19:
            err = "Parameter 'nice' should be integer in range [-20, 19]"
            print(err)
            logger.error(err)
            raise Exception(err)
    
    def add(self, x, y):
        logger.info("Processing add function. Parameters are: {0}, {1} ".format( x, y ))
        return x + y
        # return Fault(-10, "")

    def push_file (self, filename, bin_data):
        logger.info("Processing push_file() command.")
        try:
            filepath = os.path.join(self.trex_files_path, os.path.basename(filename))
            with open(filepath, 'wb') as f:
                f.write(binascii.a2b_base64(bin_data))
            logger.info("push_file() command finished. File is saved as %s" % filepath)
            return True
        except IOError as inst:
            logger.error("push_file method failed. " + str(inst))
            return False

    def connectivity_check (self):
        logger.info("Processing connectivity_check function.")
        return True

    def start(self):
        """This method fires up the daemon server based on initialized parameters of the class"""
        # initialize the server instance with given resources
        register_socket('trex_daemon_server')
        try:
            print "Firing up TRex REST daemon @ port {trex_port} ...\n".format( trex_port = self.trex_daemon_port )
            logger.info("Firing up TRex REST daemon @ port {trex_port} ...".format( trex_port = self.trex_daemon_port ))
            logger.info("current working dir is: {0}".format(self.TREX_PATH) )
            logger.info("current files dir is  : {0}".format(self.trex_files_path) )
            logger.debug("Starting TRex server. Registering methods to process.")
            logger.info(self.get_trex_version(base64 = False))
            self.server = SimpleJSONRPCServer( (self.trex_host, self.trex_daemon_port) )
        except socket.error as e:
            if e.errno == errno.EADDRINUSE:
                logger.error("TRex server requested address already in use. Aborting server launching.")
                print "TRex server requested address already in use. Aborting server launching."
                raise socket.error(errno.EADDRINUSE, "TRex daemon requested address already in use. "
                                                     "Server launch aborted. Please make sure no other process is "
                                                     "using the desired server properties.")
            elif isinstance(e, socket.gaierror) and e.errno == -3:
                # handling Temporary failure in name resolution exception
                raise socket.gaierror(-3, "Temporary failure in name resolution.\n"
                                          "Make sure provided hostname has DNS resolving.")
            else:
                raise

        # set further functionality and peripherals to server instance 
        self.server.register_function(self.add)
        self.server.register_function(self.cancel_reservation)
        self.server.register_function(self.connectivity_check)
        self.server.register_function(self.connectivity_check, 'check_connectivity') # alias
        self.server.register_function(self.force_trex_kill)
        self.server.register_function(self.get_file)
        self.server.register_function(self.get_files_list)
        self.server.register_function(self.get_files_path)
        self.server.register_function(self.get_running_info)
        self.server.register_function(self.get_running_status)
        self.server.register_function(self.get_trex_cmds)
        self.server.register_function(self.get_trex_daemon_log)
        self.server.register_function(self.get_trex_log)
        self.server.register_function(self.get_trex_version)
        self.server.register_function(self.is_reserved)
        self.server.register_function(self.is_running)
        self.server.register_function(self.kill_all_trexes)
        self.server.register_function(self.push_file)
        self.server.register_function(self.reserve_trex)
        self.server.register_function(self.start_trex)
        self.server.register_function(self.stop_trex)
        self.server.register_function(self.wait_until_kickoff_finish)
        signal.signal(signal.SIGTSTP, self.stop_handler)
        signal.signal(signal.SIGTERM, self.stop_handler)
        try:
            self.zmq_monitor.start()
            self.server.serve_forever()
        except KeyboardInterrupt:
            logger.info("Daemon shutdown request detected." )
        finally:
            self.zmq_monitor.join()            # close ZMQ monitor thread resources
            self.server.shutdown()
            #self.server.server_close()


    # get files from Trex server and return their content (mainly for logs)
    @staticmethod
    def _pull_file(filepath):
        try:
            with open(filepath, 'rb') as f:
                file_content = f.read()
                return binascii.b2a_base64(file_content)
        except Exception as e:
            err_str = "Can't get requested file %s: %s" % (filepath, e)
            logger.error(err_str)
            return Fault(-33, err_str)

    # returns True if given path is under TRex package or under /tmp/trex_files
    def _check_path_under_TRex_or_temp(self, path):
        if not os.path.relpath(path, self.trex_files_path).startswith(os.pardir):
            return True
        if not os.path.relpath(path, self.TREX_PATH).startswith(os.pardir):
            return True
        return False

    # gets the file content encoded base64 either from /tmp/trex_files or TRex server dir
    def get_file(self, filepath):
        try:
            logger.info("Processing get_file() command.")
            if not self._check_path_under_TRex_or_temp(filepath):
                raise Exception('Given path should be under current TRex package or /tmp/trex_files')
            return self._pull_file(filepath)
        except Exception as e:
            err_str = "Can't get requested file %s: %s" % (filepath, e)
            logger.error(err_str)
            return Fault(-33, err_str)

    # get tuple (dirs, files) with directories and files lists from given path (limited under TRex package or /tmp/trex_files)
    def get_files_list(self, path):
        try:
            logger.info("Processing get_files_list() command, given path: %s" % path)
            if not self._check_path_under_TRex_or_temp(path):
                raise Exception('Given path should be under current TRex package or /tmp/trex_files')
            return os.walk(path).next()[1:3]
        except Exception as e:
            err_str = "Error processing get_files_list(): %s" % e
            logger.error(err_str)
            return Fault(-33, err_str)

    # get Trex log /tmp/trex.txt
    def get_trex_log(self):
        logger.info("Processing get_trex_log() command.")
        return self._pull_file('/tmp/trex.txt')

    # get daemon log /var/log/trex/trex_daemon_server.log
    def get_trex_daemon_log (self):
        logger.info("Processing get_trex_daemon_log() command.")
        return self._pull_file('/var/log/trex/trex_daemon_server.log')
        
    # get Trex version from ./t-rex-64 --help (last lines starting with "Version : ...")
    def get_trex_version (self, base64 = True):
        try:
            logger.info("Processing get_trex_version() command.")
            if not self.trex_version:
                ret_code, stdout, stderr = run_command('./t-rex-64 --help', cwd = self.TREX_PATH)
                search_result = re.search('\n\s*(Version\s*:.+)', stdout, re.DOTALL)
                if not search_result:
                    raise Exception('Could not determine version from ./t-rex-64 --help')
                self.trex_version = binascii.b2a_base64(search_result.group(1))
            if base64:
                return self.trex_version
            else:
                return binascii.a2b_base64(self.trex_version)
        except Exception as e:
            err_str = "Can't get trex version, error: %s" % e
            logger.error(err_str)
            return Fault(-33, err_str)

    def stop_handler (self, *args, **kwargs):
        logger.info("Daemon STOP request detected.")
        if self.is_running():
            # in case TRex process is currently running, stop it before terminating server process
            self.stop_trex(self.trex.get_seq())
        sys.exit(0)

    def is_running (self):
        run_status = self.trex.get_status()
        logger.info("Processing is_running() command. Running status is: {stat}".format(stat = run_status) )
        if run_status==TRexStatus.Running:
            return True
        else:
            return False

    def is_reserved (self):
        logger.info("Processing is_reserved() command.")
        return bool(self.__reservation)

    def get_running_status (self):
        run_status = self.trex.get_status()
        logger.info("Processing get_running_status() command. Running status is: {stat}".format(stat = run_status) )
        return { 'state' : run_status.value, 'verbose' : self.trex.get_verbose_status() }

    def get_files_path (self):
        logger.info("Processing get_files_path() command." )
        return self.trex_files_path

    def reserve_trex (self, user):
        if user == "":
            logger.info("TRex reservation cannot apply to empty string user. Request denied.")
            return Fault(-33, "TRex reservation cannot apply to empty string user. Request denied.")

        with self.start_lock:
            logger.info("Processing reserve_trex() command.")
            if self.is_reserved():
                if user == self.__reservation['user']:  
                    # return True is the same user is asking and already has the resrvation
                    logger.info("the same user is asking and already has the resrvation. Re-reserving TRex.")
                    return True

                logger.info("TRex is already reserved to another user ({res_user}), cannot reserve to another user.".format( res_user = self.__reservation['user'] ))
                return Fault(-33, "TRex is already reserved to another user ({res_user}). Please make sure TRex is free before reserving it.".format(
                    res_user = self.__reservation['user']) )  # raise at client TRexInUseError
            elif self.trex.get_status() != TRexStatus.Idle:
                logger.info("TRex is currently running, cannot reserve TRex unless in Idle state.")
                return Fault(-13, 'TRex is currently running, cannot reserve TRex unless in Idle state. Please try again when TRex run finished.')  # raise at client TRexInUseError
            else:
                logger.info("TRex is now reserved for user ({res_user}).".format( res_user = user ))
                self.__reservation = {'user' : user, 'since' : time.ctime()}
                logger.debug("Reservation details: "+ str(self.__reservation))
                return True

    def cancel_reservation (self, user):
        with self.start_lock:
            logger.info("Processing cancel_reservation() command.")
            if self.is_reserved():
                if self.__reservation['user'] == user:
                    logger.info("TRex reservation to {res_user} has been canceled successfully.".format(res_user = self.__reservation['user']))
                    self.__reservation = None
                    return True
                else:
                    logger.warning("TRex is reserved to different user than the provided one. Reservation wasn't canceled.")
                    return Fault(-33, "Cancel reservation request is available to the user that holds the reservation. Request denied")  # raise at client TRexRequestDenied
            
            else:
                logger.info("TRex is not reserved to anyone. No need to cancel anything")
                assert(self.__reservation is None)
                return False

            
    def start_trex(self, trex_cmd_options, user, block_to_success = True, timeout = 40, stateless = False, debug_image = False, trex_args = ''):
        with self.start_lock:
            logger.info("Processing start_trex() command.")
            if self.is_reserved():
                # check if this is not the user to which TRex is reserved
                if self.__reservation['user'] != user:  
                    logger.info("TRex is reserved to another user ({res_user}). Only that user is allowed to initiate new runs.".format(res_user = self.__reservation['user']))
                    return Fault(-33, "TRex is reserved to another user ({res_user}). Only that user is allowed to initiate new runs.".format(res_user = self.__reservation['user']))  # raise at client TRexRequestDenied
            elif self.trex.get_status() != TRexStatus.Idle:
                logger.info("TRex is already taken, cannot create another run until done.")
                return Fault(-13, '')  # raise at client TRexInUseError
            
            try:
                server_cmd_data = self.generate_run_cmd(stateless = stateless, debug_image = debug_image, trex_args = trex_args, **trex_cmd_options)
                self.zmq_monitor.first_dump = True
                self.trex.start_trex(self.TREX_PATH, server_cmd_data)
                logger.info("TRex session has been successfully initiated.")
                if block_to_success:
                    # delay server response until TRex is at 'Running' state.
                    start_time = time.time()
                    trex_state = None
                    while (time.time() - start_time) < timeout :
                        trex_state = self.trex.get_status()
                        if trex_state != TRexStatus.Starting:
                            break
                        else:
                            time.sleep(0.5)

                    # check for TRex run started normally
                    if trex_state == TRexStatus.Starting:   # reached timeout
                        logger.warning("TimeoutError: TRex initiation outcome could not be obtained, since TRex stays at Starting state beyond defined timeout.")
                        return Fault(-12, 'TimeoutError: TRex initiation outcome could not be obtained, since TRex stays at Starting state beyond defined timeout.') # raise at client TRexWarning
                    elif trex_state == TRexStatus.Idle:
                        return Fault(-11, self.trex.get_verbose_status())   # raise at client TRexError
                
                # reach here only if TRex is at 'Running' state
                self.trex.gen_seq()
                return self.trex.get_seq()          # return unique seq number to client
                        
            except TypeError as e:
                logger.error("TRex command generation failed, probably because either -f (traffic generation .yaml file) and -c (num of cores) was not specified correctly.\nReceived params: {params}".format( params = trex_cmd_options) )
                raise TypeError('TRex -f (traffic generation .yaml file) and -c (num of cores) must be specified. %s' % e)


    def stop_trex(self, seq):
        logger.info("Processing stop_trex() command.")
        if self.trex.get_seq()== seq:
            logger.debug("Abort request legit since seq# match")
            return self.trex.stop_trex()
        else:
            if self.trex.get_status() != TRexStatus.Idle:
                logger.warning("Abort request is only allowed to process initiated the run. Request denied.")

                return Fault(-33, 'Abort request is only allowed to process initiated the run. Request denied.')  # raise at client TRexRequestDenied
            else:
                return False

    def force_trex_kill (self):
        logger.info("Processing force_trex_kill() command. --> Killing TRex session indiscriminately.")
        return self.trex.stop_trex()

    # returns list of tuples (pid, command line) of running TRex(es)
    def get_trex_cmds(self):
        logger.info('Processing get_trex_cmds() command.')
        ret_code, stdout, stderr = run_command('ps -u root --format pid,comm,cmd')
        if ret_code:
            raise Exception('Failed to determine running processes, stderr: %s' % stderr)
        trex_cmds_list = []
        for line in stdout.splitlines():
            pid, proc_name, full_cmd = line.strip().split(' ', 2)
            pid = pid.strip()
            full_cmd = full_cmd.strip()
            if proc_name.find('t-rex-64') >= 0:
                trex_cmds_list.append((pid, full_cmd))
        return trex_cmds_list


    # Silently tries to kill TRexes with given signal.
    # Responsibility of client to verify with get_trex_cmds.
    def kill_all_trexes(self, signal_name):
        logger.info('Processing kill_all_trexes() command.')
        trex_cmds_list = self.get_trex_cmds()
        for pid, cmd in trex_cmds_list:
            logger.info('Killing with signal %s process %s %s' % (signal_name, pid, cmd))
            os.kill(int(pid), signal_name)


    def wait_until_kickoff_finish (self, timeout = 40):
        # block until TRex exits Starting state
        logger.info("Processing wait_until_kickoff_finish() command.")
        trex_state = None
        start_time = time.time()
        while (time.time() - start_time) < timeout :
            trex_state = self.trex.get_status()
            if trex_state != TRexStatus.Starting:
                return
        return Fault(-12, 'TimeoutError: TRex initiation outcome could not be obtained, since TRex stays at Starting state beyond defined timeout.') # raise at client TRexWarning

    def get_running_info (self):
        logger.info("Processing get_running_info() command.")
        return self.trex.get_running_info()


    def generate_run_cmd (self, iom = 0, export_path="/tmp/trex.txt", stateless = False, debug_image = False, trex_args = '', **kwargs):
        """ generate_run_cmd(self, iom, export_path, kwargs) -> str

        Generates a custom running command for the kick-off of the TRex traffic generator.
        Returns a tuple of command (string) and export path (string) to be issued on the trex server

        Parameters
        ----------
        iom: int
            0 = don't print stats screen to log, 1 = print stats (can generate huge logs)
        stateless: boolean
            True = run as stateless, False = require -f and -d arguments
        kwargs: dictionary
            Dictionary of parameters for trex. For example: (c=1, nc=True, l_pkt_mode=3).
            Notice that when sending command line parameters that has -, you need to replace it with _.
            for example, to have on command line "--l-pkt-mode 3", you need to send l_pkt_mode=3
        export_path : str
            Full system path to which the results of the trex-run will be logged.

        """
        if 'results_file_path' in kwargs:
            export_path = kwargs['results_file_path']
            del kwargs['results_file_path']
        if stateless:
            kwargs['i'] = True

        # adding additional options to the command
        trex_cmd_options = ''
        for key, value in kwargs.iteritems():
            tmp_key = key.replace('_','-').lstrip('-')
            dash = ' -' if (len(key)==1) else ' --'
            if value is True:
                trex_cmd_options += (dash + tmp_key)
            elif value is False:
                continue
            else:
                trex_cmd_options += (dash + '{k} {val}'.format( k = tmp_key, val =  value ))
        if trex_args:
            trex_cmd_options += ' %s' % trex_args

        if not stateless:
            if 'f' not in kwargs:
                raise Exception('Argument -f should be specified in stateful command')
            if 'd' not in kwargs:
                raise Exception('Argument -d should be specified in stateful command')

        cmd = "{nice}{run_command}{debug_image} --iom {io} {cmd_options} --no-key".format( # -- iom 0 disables the periodic log to the screen (not needed)
            nice = '' if self.trex_nice == 0 else 'nice -n %s ' % self.trex_nice,
            run_command = self.TREX_START_CMD,
            debug_image = '-debug' if debug_image else '',
            cmd_options = trex_cmd_options,
            io          = iom)

        logger.info("TREX FULL COMMAND: {command}".format(command = cmd) )

        return (cmd, export_path, kwargs.get('d', 0))


    def __check_trex_path_validity(self):
        # check for executable existance
        if not os.path.exists(self.TREX_PATH+'/t-rex-64'):
            print "The provided TRex path do not contain an executable TRex file.\nPlease check the path and retry."
            logger.error("The provided TRex path do not contain an executable TRex file")
            exit(-1)
        # check for executable permissions
        st = os.stat(self.TREX_PATH+'/t-rex-64')
        if not bool(st.st_mode & (stat.S_IXUSR ) ):
            print "The provided TRex path do not contain an TRex file with execution privileges.\nPlease check the files permissions and retry."
            logger.error("The provided TRex path do not contain an TRex file with execution privileges")
            exit(-1)
        else:
            return

    def __check_files_path_validity(self):
        # first, check for path existance. otherwise, try creating it with appropriate credentials
        if not os.path.exists(self.trex_files_path):
            try:
                os.makedirs(self.trex_files_path, 0660)
                return
            except os.error as inst:
                print "The provided files path does not exist and cannot be created with needed access credentials using root user.\nPlease check the path's permissions and retry."
                logger.error("The provided files path does not exist and cannot be created with needed access credentials using root user.")
                exit(-1)
        elif os.access(self.trex_files_path, os.W_OK):
            return
        else:
            print "The provided files path has insufficient access credentials for root user.\nPlease check the path's permissions and retry."
            logger.error("The provided files path has insufficient access credentials for root user")
            exit(-1)
Example #13
0
    def shutdown(self):
        for service in self.registered_services:
            self.unregister_service(service)

        SimpleJSONRPCServer.shutdown(self)
Example #14
0
    def shutdown(self):
        shutdown_list = self.registered_services.keys()
        for svc in shutdown_list:
            self.unregister_service(svc)

        SimpleJSONRPCServer.shutdown(self)
class JsonrpcServer():
    def __init__(self, port):
        self.server = SimpleJSONRPCServer(('', port), logRequests=False)
        self.cmd_handler = CmdHandler()
        self.dct_observer = {}
        self.publisher = self.cmd_handler.get_publisher()

    def register(self):
        # register all rpc callback
        rf = self.server.register_function
        rf(self.add_image_observer, "add_image_observer")
        rf(self.set_image_observer, "set_image_observer")
        rf(self.remove_image_observer, "remove_image_observer")
        rf(self.get_params_media, "get_params_media")
        rf(self.get_param_media, "get_param_media")
        rf(self.get_params_filterchain, "get_params_filterchain")
        rf(self.get_param_filterchain, "get_param_filterchain")

        rf(self.cmd_handler.is_connected, "is_connected")
        rf(self.cmd_handler.start_filterchain_execution,
           "start_filterchain_execution")
        rf(self.cmd_handler.stop_filterchain_execution,
           "stop_filterchain_execution")
        rf(self.cmd_handler.get_fps_execution, "get_fps_execution")
        rf(self.cmd_handler.add_output_observer, "add_output_observer")
        rf(self.cmd_handler.remove_output_observer, "remove_output_observer")
        rf(self.cmd_handler.get_execution_list, "get_execution_list")
        rf(self.cmd_handler.get_execution_info, "get_execution_info")
        rf(self.cmd_handler.get_media_list, "get_media_list")
        rf(self.cmd_handler.get_default_media_name, "get_default_media_name")
        rf(self.cmd_handler.start_record, "start_record")
        rf(self.cmd_handler.stop_record, "stop_record")
        rf(self.cmd_handler.cmd_to_media, "cmd_to_media")
        rf(self.cmd_handler.get_info_media, "get_info_media")
        rf(self.cmd_handler.save_params_media, "save_params_media")
        rf(self.cmd_handler.get_filterchain_list, "get_filterchain_list")
        rf(self.cmd_handler.delete_filterchain, "delete_filterchain")
        rf(self.cmd_handler.upload_filterchain, "upload_filterchain")
        rf(self.cmd_handler.modify_filterchain, "modify_filterchain")
        rf(self.cmd_handler.reload_filter, "reload_filter")
        rf(self.cmd_handler.save_params, "save_params")
        rf(self.cmd_handler.get_filter_list, "get_filter_list")
        rf(self.cmd_handler.get_filterchain_info, "get_filterchain_info")
        rf(self.cmd_handler.update_param_media, "update_param_media")
        rf(self.cmd_handler.reset_param_media, "reset_param_media")
        rf(self.cmd_handler.set_as_default_param_media,
           "set_as_default_param_media")
        rf(self.cmd_handler.update_param, "update_param")
        rf(self.cmd_handler.reset_param, "reset_param")
        rf(self.cmd_handler.set_as_default_param, "set_as_default_param")
        rf(self.cmd_handler.subscribe, "subscribe")
        rf(self.cmd_handler.get_lst_record_historic, "get_lst_record_historic")
        rf(self.cmd_handler.cut_video, "cut_video")
        rf(self.cmd_handler.get_lst_old_record_historic,
           "get_lst_old_record_historic")
        rf(self.cmd_handler.get_count_keys, "get_count_keys")

    def run(self):
        self.server.serve_forever()

    def close(self):
        logger.info("Close jsonrpc server.")
        self.cmd_handler.close()
        self.server.shutdown()

    def get_params_filterchain(self, execution_name, filter_name):
        lst_param = self.cmd_handler.get_params_filterchain(
            execution_name, filter_name)
        return self._serialize_param(lst_param)

    def get_param_filterchain(self, execution_name, filter_name, param_name):
        param = self.cmd_handler.get_param_filterchain(
            execution_name, filter_name, param_name)
        return self._serialize_param(param)

    def get_params_media(self, media_name):
        lst_param = self.cmd_handler.get_params_media(media_name)
        return self._serialize_param(lst_param)

    def get_param_media(self, media_name, param_name):
        param = self.cmd_handler.get_param_media(media_name, param_name)
        return self._serialize_param(param)

    @staticmethod
    def _serialize_param(param_obj):
        if isinstance(param_obj, list):
            return [param.serialize() for param in param_obj]
        if isinstance(param_obj, dict):
            return {name: param.serialize() for name, param in
                    param_obj.items()}
        elif isinstance(param_obj, Param):
            return param_obj.serialize()

    #
    # OBSERVATOR ################################
    #
    def add_image_observer(self, execution_name, filter_name):
        key = keys.create_unique_exec_filter_name(execution_name, filter_name)
        observer = self._cb_send_image(key)
        if self.cmd_handler.add_image_observer(observer, execution_name,
                                               filter_name):
            if key not in self.dct_observer:
                self.dct_observer[key] = observer
            return True
        return False

    def set_image_observer(self, execution_name, filter_name_old,
                           filter_name_new):
        old_key = keys.create_unique_exec_filter_name(
            execution_name, filter_name_old)
        new_key = keys.create_unique_exec_filter_name(
            execution_name, filter_name_new)
        observer = self.dct_observer[old_key]
        new_observer = self._cb_send_image(new_key)
        if self.cmd_handler.set_image_observer(observer,
                                               execution_name,
                                               filter_name_old,
                                               filter_name_new,
                                               new_observer=new_observer):
            del self.dct_observer[old_key]
            self.dct_observer[new_key] = new_observer
            return True
        return False

    def remove_image_observer(self, execution_name, filter_name):
        key = keys.create_unique_exec_filter_name(execution_name, filter_name)
        observer = self.dct_observer.get(key, None)
        if observer is None:
            logger.warning("Missing image observer : %s" % key)
        return self.cmd_handler.remove_image_observer(observer, execution_name,
                                                      filter_name)

    @staticmethod
    def _compress_cvmat(image):
        compress_img = cv2.imencode(
            ".jpeg", image, (cv.CV_IMWRITE_JPEG_QUALITY, 95))
        return compress_img[1].dumps()

    def _cb_send_image(self, key):
        def _publish_image(image):
            compress_cvmat = self._compress_cvmat(image)
            self.publisher.publish(key, compress_cvmat)

        return _publish_image