From a2a634fc8b5bac450ea37f29dde521b7d9e740c8 Mon Sep 17 00:00:00 2001 From: Dan Klein Date: Thu, 22 Oct 2015 13:44:25 +0300 Subject: minor updates --- .../trex_control_plane/client/trex_hltapi.py | 6 + .../client/trex_stateless_client.py | 140 +-------------------- 2 files changed, 7 insertions(+), 139 deletions(-) mode change 100644 => 100755 scripts/automation/trex_control_plane/client/trex_hltapi.py (limited to 'scripts/automation/trex_control_plane/client') diff --git a/scripts/automation/trex_control_plane/client/trex_hltapi.py b/scripts/automation/trex_control_plane/client/trex_hltapi.py old mode 100644 new mode 100755 index 46c283f8..b180eb5b --- a/scripts/automation/trex_control_plane/client/trex_hltapi.py +++ b/scripts/automation/trex_control_plane/client/trex_hltapi.py @@ -2,6 +2,7 @@ import trex_root_path from client_utils.packet_builder import CTRexPktBuilder +from trex_stateless_client import CTRexStatelessClient print "done!" @@ -10,6 +11,11 @@ class CTRexHltApi(object): def __init__(self): pass + def connect_status(self): + pass + + + def config_traffic(self): pass diff --git a/scripts/automation/trex_control_plane/client/trex_stateless_client.py b/scripts/automation/trex_control_plane/client/trex_stateless_client.py index 334496d1..8956aaf5 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -10,6 +10,7 @@ from client_utils.jsonrpc_client import JsonRpcClient, BatchMessage from client_utils.packet_builder import CTRexPktBuilder import json from common.trex_stats import * +from common.trex_streams import * from collections import namedtuple @@ -352,144 +353,5 @@ class CTRexStatelessClient(object): port=self.port) -class CStream(object): - """docstring for CStream""" - DEFAULTS = {"rx_stats": CRxStats, - "mode": CTxMode, - "isg": 5.0, - "next_stream": -1, - "self_start": True, - "enabled": True} - - def __init__(self, **kwargs): - super(CStream, self).__init__() - for k, v in kwargs.items(): - setattr(self, k, v) - # set default values to unset attributes, according to DEFAULTS dict - set_keys = set(kwargs.keys()) - keys_to_set = [x - for x in self.DEFAULTS - if x not in set_keys] - for key in keys_to_set: - default = self.DEFAULTS.get(key) - if type(default) == type: - setattr(self, key, default()) - else: - setattr(self, key, default) - - @property - def packet(self): - return self._packet - - @packet.setter - def packet(self, packet_obj): - assert isinstance(packet_obj, CTRexPktBuilder) - self._packet = packet_obj - - @property - def enabled(self): - return self._enabled - - @enabled.setter - def enabled(self, bool_value): - self._enabled = bool(bool_value) - - @property - def self_start(self): - return self._self_start - - @self_start.setter - def self_start(self, bool_value): - self._self_start = bool(bool_value) - - @property - def next_stream(self): - return self._next_stream - - @next_stream.setter - def next_stream(self, value): - self._next_stream = int(value) - - def dump(self): - pass - return {"enabled": self.enabled, - "self_start": self.self_start, - "isg": self.isg, - "next_stream": self.next_stream, - "packet": self.packet.dump_pkt(), - "mode": self.mode.dump(), - "vm": self.packet.get_vm_data(), - "rx_stats": self.rx_stats.dump()} - -class CRxStats(object): - - def __init__(self, enabled=False, seq_enabled=False, latency_enabled=False): - self._rx_dict = {"enabled": enabled, - "seq_enabled": seq_enabled, - "latency_enabled": latency_enabled} - - @property - def enabled(self): - return self._rx_dict.get("enabled") - - @enabled.setter - def enabled(self, bool_value): - self._rx_dict['enabled'] = bool(bool_value) - - @property - def seq_enabled(self): - return self._rx_dict.get("seq_enabled") - - @seq_enabled.setter - def seq_enabled(self, bool_value): - self._rx_dict['seq_enabled'] = bool(bool_value) - - @property - def latency_enabled(self): - return self._rx_dict.get("latency_enabled") - - @latency_enabled.setter - def latency_enabled(self, bool_value): - self._rx_dict['latency_enabled'] = bool(bool_value) - - def dump(self): - return {k: v - for k, v in self._rx_dict.items() - if v - } - - -class CTxMode(object): - """docstring for CTxMode""" - def __init__(self, tx_mode, pps): - super(CTxMode, self).__init__() - if tx_mode not in ["continuous", "single_burst", "multi_burst"]: - raise ValueError("Unknown TX mode ('{0}')has been initialized.".format(tx_mode)) - self._tx_mode = tx_mode - self._fields = {'pps': float(pps)} - if tx_mode == "single_burst": - self._fields['total_pkts'] = 0 - elif tx_mode == "multi_burst": - self._fields['pkts_per_burst'] = 0 - self._fields['ibg'] = 0.0 - self._fields['count'] = 0 - else: - pass - - def set_tx_mode_attr(self, attr, val): - if attr in self._fields: - self._fields[attr] = type(self._fields.get(attr))(val) - else: - raise ValueError("The provided attribute ('{0}') is not a legal attribute in selected TX mode ('{1}')". - format(attr, self._tx_mode)) - - def dump(self): - dump = {"type": self._tx_mode} - dump.update({k: v - for k, v in self._fields.items() - }) - return dump - - if __name__ == "__main__": pass -- cgit From ebb0b48faca96bad7bfe8da0bf80df7c7c80350d Mon Sep 17 00:00:00 2001 From: Dan Klein Date: Sun, 25 Oct 2015 11:33:48 +0200 Subject: HLTAPI progress... --- .../trex_control_plane/client/trex_hltapi.py | 30 ++++++++++++++++++++-- .../client/trex_stateless_client.py | 5 ++++ 2 files changed, 33 insertions(+), 2 deletions(-) (limited to 'scripts/automation/trex_control_plane/client') diff --git a/scripts/automation/trex_control_plane/client/trex_hltapi.py b/scripts/automation/trex_control_plane/client/trex_hltapi.py index b180eb5b..7453d8ec 100755 --- a/scripts/automation/trex_control_plane/client/trex_hltapi.py +++ b/scripts/automation/trex_control_plane/client/trex_hltapi.py @@ -11,14 +11,40 @@ class CTRexHltApi(object): def __init__(self): pass - def connect_status(self): + def connect(self, device, port_list, username, reset=False, break_locks=False): pass + def interface_config(self, port_handle, mode="config"): + pass + + def get_status(self): + pass + + def get_port_handler(self): + pass + + def traffic_config(self, mode, port_handle, + mac_src, mac_dst, + l3_protocol, ip_src_addr, ip_dst_addr, l3_length, + transmit_mode, rate_pps): + pass + def traffic_control(self, action, port_handle): + pass - def config_traffic(self): + def traffic_stats(self, port_handle, mode): pass + def get_aggregate_port_stats(self, port_handle): + return self.traffic_stats(port_handle, mode="aggregate") + + def get_stream_stats(self, port_handle): + return self.traffic_stats(port_handle, mode="stream") + + + + + if __name__ == "__main__": pass diff --git a/scripts/automation/trex_control_plane/client/trex_stateless_client.py b/scripts/automation/trex_control_plane/client/trex_stateless_client.py index 8956aaf5..7d287a5e 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -63,6 +63,9 @@ class CTRexStatelessClient(object): return self._system_info # ----- user-access methods ----- # + def connect(self): + self.tx_link.connect() + def ping(self): return self.transmit("ping") @@ -323,6 +326,8 @@ class CTRexStatelessClient(object): self.server = server self.port = port self.rpc_link = JsonRpcClient(self.server, self.port) + + def connect(self): if not self.virtual: self.rpc_link.connect() -- cgit From 7c35235f67b009cb2a9a605c3c89ea39d6f10871 Mon Sep 17 00:00:00 2001 From: Dan Klein Date: Sun, 25 Oct 2015 06:17:33 +0200 Subject: refactor tx_link -> comm_link --- .../trex_control_plane/client/trex_stateless_client.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'scripts/automation/trex_control_plane/client') diff --git a/scripts/automation/trex_control_plane/client/trex_stateless_client.py b/scripts/automation/trex_control_plane/client/trex_stateless_client.py index 7d287a5e..8856a793 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -21,7 +21,7 @@ class CTRexStatelessClient(object): def __init__(self, username, server="localhost", port=5050, virtual=False): super(CTRexStatelessClient, self).__init__() self.user = username - self.tx_link = CTRexStatelessClient.CTxLink(server, port, virtual) + self.comm_link = CTRexStatelessClient.CCommLink(server, port, virtual) self._conn_handler = {} self._active_ports = set() self._stats = CTRexStatsManager("port", "stream") @@ -64,7 +64,7 @@ class CTRexStatelessClient(object): # ----- user-access methods ----- # def connect(self): - self.tx_link.connect() + self.comm_link.connect() def ping(self): return self.transmit("ping") @@ -238,10 +238,10 @@ class CTRexStatelessClient(object): # ----- internal methods ----- # def transmit(self, method_name, params={}): - return self.tx_link.transmit(method_name, params) + return self.comm_link.transmit(method_name, params) def transmit_batch(self, batch_list): - return self.tx_link.transmit_batch(batch_list) + return self.comm_link.transmit_batch(batch_list) @staticmethod def _object_decoder(obj_type, obj_data): @@ -318,10 +318,10 @@ class CTRexStatelessClient(object): # ------ private classes ------ # - class CTxLink(object): + class CCommLink(object): """describes the connectivity of the stateless client method""" def __init__(self, server="localhost", port=5050, virtual=False): - super(CTRexStatelessClient.CTxLink, self).__init__() + super(CTRexStatelessClient.CCommLink, self).__init__() self.virtual = virtual self.server = server self.port = port -- cgit From b44239e4c6019f10fa7cf4fe0fef8c3726435033 Mon Sep 17 00:00:00 2001 From: Dan Klein Date: Mon, 26 Oct 2015 09:41:51 +0200 Subject: add disconnect functionality --- scripts/automation/trex_control_plane/client/trex_stateless_client.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'scripts/automation/trex_control_plane/client') diff --git a/scripts/automation/trex_control_plane/client/trex_stateless_client.py b/scripts/automation/trex_control_plane/client/trex_stateless_client.py index 8856a793..b976db66 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -66,6 +66,9 @@ class CTRexStatelessClient(object): def connect(self): self.comm_link.connect() + def disconnect(self): + self.comm_link.disconnect() + def ping(self): return self.transmit("ping") -- cgit From a39e4416cd78fc3b147695465c4de1c896b3face Mon Sep 17 00:00:00 2001 From: Dan Klein Date: Tue, 27 Oct 2015 10:00:18 +0200 Subject: more hltapi progress connect working --- .../trex_control_plane/client/trex_hltapi.py | 72 +++++++- .../client/trex_stateless_client.py | 185 ++++++++++++++++----- 2 files changed, 212 insertions(+), 45 deletions(-) (limited to 'scripts/automation/trex_control_plane/client') diff --git a/scripts/automation/trex_control_plane/client/trex_hltapi.py b/scripts/automation/trex_control_plane/client/trex_hltapi.py index 7453d8ec..6194d376 100755 --- a/scripts/automation/trex_control_plane/client/trex_hltapi.py +++ b/scripts/automation/trex_control_plane/client/trex_hltapi.py @@ -4,14 +4,54 @@ import trex_root_path from client_utils.packet_builder import CTRexPktBuilder from trex_stateless_client import CTRexStatelessClient -print "done!" class CTRexHltApi(object): def __init__(self): + self.trex_client = None + self.connected = False + + pass + + # ----- session functions ----- # + + def connect(self, device, port_list, username, port=5050, reset=False, break_locks=False): + ret_dict = {"status": 0} + self.trex_client = CTRexStatelessClient(username, device, port) + res_ok, msg = self.trex_client.connect() + if not res_ok: + self.trex_client = None + ret_dict.update({"log": msg}) + return ret_dict + # arrived here, connection successfully created with server + # next, try acquiring ports of TRex + port_list = self.parse_port_list(port_list) + response = self.trex_client.acquire(port_list, force=break_locks) + res_ok, log = CTRexHltApi.process_response(port_list, response) + if not res_ok: + self.trex_client.disconnect() + self.trex_client = None + ret_dict.update({"log": log}) + # TODO: should revert taken ports?! + return ret_dict + # arrived here, all desired ports were successfully acquired + print log + if reset: + # remove all port traffic configuration from TRex + response = self.trex_client.remove_all_streams(port_list) + res_ok, log = CTRexHltApi.process_response(port_list, response) + if not res_ok: + self.trex_client.disconnect() + self.trex_client = None + ret_dict.update({"log": log}) + return ret_dict + print log + ret_dict.update({"status": 1}) + self.trex_client.disconnect() + pass - def connect(self, device, port_list, username, reset=False, break_locks=False): + def cleanup_session(self, port_list, maintain_lock=False): pass def interface_config(self, port_handle, mode="config"): @@ -41,8 +81,32 @@ class CTRexHltApi(object): def get_stream_stats(self, port_handle): return self.traffic_stats(port_handle, mode="stream") - - + # ----- internal functions ----- # + @staticmethod + def process_response(port_list, response): + if isinstance(port_list, list): + res_ok, response = response + log = CTRexHltApi.join_batch_response(response) + else: + res_ok = response.success + log = str(response) + return res_ok, log + + @staticmethod + def parse_port_list(port_list): + if isinstance(port_list, str): + return [int(port) + for port in port_list.split()] + elif isinstance(port_list, list): + return [int(port) + for port in port_list] + else: + return port_list + + @staticmethod + def join_batch_response(responses): + return "\n".join([str(response) + for response in responses]) if __name__ == "__main__": diff --git a/scripts/automation/trex_control_plane/client/trex_stateless_client.py b/scripts/automation/trex_control_plane/client/trex_stateless_client.py index b976db66..97d3ec0a 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -13,10 +13,19 @@ from common.trex_stats import * from common.trex_streams import * from collections import namedtuple +RpcCmdData = namedtuple('RpcCmdData', ['method', 'params']) + +class RpcResponseStatus(namedtuple('RpcResponseStatus', ['success', 'id', 'msg'])): + __slots__ = () + def __str__(self): + return "{id:^3} - {msg} ({stat})".format(id=self.id, + msg=self.msg, + stat="success" if self.success else "fail") + +# RpcResponseStatus = namedtuple('RpcResponseStatus', ['success', 'id', 'msg']) class CTRexStatelessClient(object): """docstring for CTRexStatelessClient""" - RpcCmdData = namedtuple('RpcCmdData', ['method', 'params']) def __init__(self, username, server="localhost", port=5050, virtual=False): super(CTRexStatelessClient, self).__init__() @@ -26,48 +35,69 @@ class CTRexStatelessClient(object): self._active_ports = set() self._stats = CTRexStatsManager("port", "stream") self._system_info = None + self._server_version = None + self.__err_log = None # ----- decorator methods ----- # def force_status(owned=True, active_and_owned=False): def wrapper(func): def wrapper_f(self, *args, **kwargs): port_ids = kwargs.get("port_id") + if not port_ids: + port_ids = args[0] if isinstance(port_ids, int): # make sure port_ids is a list port_ids = [port_ids] bad_ids = set() for port_id in port_ids: - port_owned = self._conn_handler.get(kwargs.get(port_id)) + port_owned = self._conn_handler.get(port_id) if owned and not port_owned: - bad_ids.add(port_ids) + bad_ids.add(port_id) elif active_and_owned: # stronger condition than just owned, hence gets precedence if port_owned and port_id in self._active_ports: continue else: - bad_ids.add(port_ids) + bad_ids.add(port_id) else: continue if bad_ids: # Some port IDs are not according to desires status raise RuntimeError("The requested method ('{0}') cannot be invoked since port IDs {1} are not" - "at allowed stated".format(func.__name__)) + "at allowed stated".format(func.__name__, list(bad_ids))) else: - func(self, *args, **kwargs) + return func(self, *args, **kwargs) return wrapper_f return wrapper @property def system_info(self): if not self._system_info: - self._system_info = self.get_system_info() - return self._system_info + rc, info = self.get_system_info() + if rc: + self._system_info = info + else: + self.__err_log = info + return self._system_info if self._system_info else "Unknown" + + @property + def server_version(self): + if not self._server_version: + rc, ver_info = self.get_version() + if rc: + self._server_version = ver_info + else: + self.__err_log = ver_info + return self._server_version if self._server_version else "Unknown" # ----- user-access methods ----- # def connect(self): - self.comm_link.connect() + rc, err = self.comm_link.connect() + if not rc: + return rc, err + return self._init_sync() def disconnect(self): - self.comm_link.disconnect() + return self.comm_link.disconnect() def ping(self): return self.transmit("ping") @@ -90,18 +120,19 @@ class CTRexStatelessClient(object): if isinstance(port_id, list) or isinstance(port_id, set): # handle as batch mode port_ids = set(port_id) # convert to set to avoid duplications - commands = [self.RpcCmdData("acquire", {"port_id": p_id, "user": self.user, "force": force}) + commands = [RpcCmdData("acquire", {"port_id": p_id, "user": self.user, "force": force}) for p_id in port_ids] rc, resp_list = self.transmit_batch(commands) if rc: - self._process_batch_result(commands, resp_list, self._handle_acquire_response) + return self._process_batch_result(commands, resp_list, self._handle_acquire_response) else: params = {"port_id": port_id, "user": self.user, "force": force} - command = self.RpcCmdData("acquire", params) - self._handle_acquire_response(command, self.transmit(command.method, command.params)) - return self._conn_handler.get(port_id) + command = RpcCmdData("acquire", params) + return self._handle_acquire_response(command, + self.transmit(command.method, command.params), + self.default_success_test) @force_status(owned=True) def release(self, port_id=None): @@ -110,7 +141,7 @@ class CTRexStatelessClient(object): if isinstance(port_id, list) or isinstance(port_id, set): # handle as batch mode port_ids = set(port_id) # convert to set to avoid duplications - commands = [self.RpcCmdData("release", {"handler": self._conn_handler.get(p_id), "port_id": p_id}) + commands = [RpcCmdData("release", {"handler": self._conn_handler.get(p_id), "port_id": p_id}) for p_id in port_ids] rc, resp_list = self.transmit_batch(commands) if rc: @@ -119,7 +150,7 @@ class CTRexStatelessClient(object): self._conn_handler.pop(port_id) params = {"handler": self._conn_handler.get(port_id), "port_id": port_id} - command = self.RpcCmdData("release", params) + command = RpcCmdData("release", params) self._handle_release_response(command, self.transmit(command.method, command.params)) return @@ -143,6 +174,28 @@ class CTRexStatelessClient(object): "stream_id": stream_id} return self.transmit("remove_stream", params) + @force_status(owned=True) + def remove_all_streams(self, port_id=None): + if not self._is_ports_valid(port_id): + raise ValueError("Provided illegal port id input") + if isinstance(port_id, list) or isinstance(port_id, set): + # handle as batch mode + port_ids = set(port_id) # convert to set to avoid duplications + commands = [RpcCmdData("remove_all_streams", {"port_id": p_id, "handler": self._conn_handler.get(p_id)}) + for p_id in port_ids] + rc, resp_list = self.transmit_batch(commands) + if rc: + return self._process_batch_result(commands, resp_list, self._handle_remove_streams_response, + success_test=self.ack_success_test) + else: + params = {"port_id": port_id, + "handler": self._conn_handler.get(port_id)} + command = RpcCmdData("remove_all_streams", params) + return self._handle_remove_streams_response(command, + self.transmit(command.method, command.params), + self.ack_success_test) + pass + @force_status(owned=True, active_and_owned=True) def get_stream_id_list(self, port_id=None): if not self._is_ports_valid(port_id): @@ -167,7 +220,7 @@ class CTRexStatelessClient(object): if isinstance(port_id, list) or isinstance(port_id, set): # handle as batch mode port_ids = set(port_id) # convert to set to avoid duplications - commands = [self.RpcCmdData("start_traffic", {"handler": self._conn_handler.get(p_id), "port_id": p_id}) + commands = [RpcCmdData("start_traffic", {"handler": self._conn_handler.get(p_id), "port_id": p_id}) for p_id in port_ids] rc, resp_list = self.transmit_batch(commands) if rc: @@ -175,7 +228,7 @@ class CTRexStatelessClient(object): else: params = {"handler": self._conn_handler.get(port_id), "port_id": port_id} - command = self.RpcCmdData("start_traffic", params) + command = RpcCmdData("start_traffic", params) self._handle_start_traffic_response(command, self.transmit(command.method, command.params)) return @@ -186,7 +239,7 @@ class CTRexStatelessClient(object): if isinstance(port_id, list) or isinstance(port_id, set): # handle as batch mode port_ids = set(port_id) # convert to set to avoid duplications - commands = [self.RpcCmdData("stop_traffic", {"handler": self._conn_handler.get(p_id), "port_id": p_id}) + commands = [RpcCmdData("stop_traffic", {"handler": self._conn_handler.get(p_id), "port_id": p_id}) for p_id in port_ids] rc, resp_list = self.transmit_batch(commands) if rc: @@ -194,12 +247,12 @@ class CTRexStatelessClient(object): else: params = {"handler": self._conn_handler.get(port_id), "port_id": port_id} - command = self.RpcCmdData("stop_traffic", params) + command = RpcCmdData("stop_traffic", params) self._handle_start_traffic_response(command, self.transmit(command.method, command.params)) return def get_global_stats(self): - command = self.RpcCmdData("get_global_stats", {}) + command = RpcCmdData("get_global_stats", {}) return self._handle_get_global_stats_response(command, self.transmit(command.method, command.params)) # return self.transmit("get_global_stats") @@ -210,7 +263,7 @@ class CTRexStatelessClient(object): if isinstance(port_id, list) or isinstance(port_id, set): # handle as batch mode port_ids = set(port_id) # convert to set to avoid duplications - commands = [self.RpcCmdData("get_port_stats", {"handler": self._conn_handler.get(p_id), "port_id": p_id}) + commands = [RpcCmdData("get_port_stats", {"handler": self._conn_handler.get(p_id), "port_id": p_id}) for p_id in port_ids] rc, resp_list = self.transmit_batch(commands) if rc: @@ -218,7 +271,7 @@ class CTRexStatelessClient(object): else: params = {"handler": self._conn_handler.get(port_id), "port_id": port_id} - command = self.RpcCmdData("get_port_stats", params) + command = RpcCmdData("get_port_stats", params) return self._handle_get_port_stats_response(command, self.transmit(command.method, command.params)) @force_status(owned=True, active_and_owned=True) @@ -228,7 +281,7 @@ class CTRexStatelessClient(object): if isinstance(port_id, list) or isinstance(port_id, set): # handle as batch mode port_ids = set(port_id) # convert to set to avoid duplications - commands = [self.RpcCmdData("get_stream_stats", {"handler": self._conn_handler.get(p_id), "port_id": p_id}) + commands = [RpcCmdData("get_stream_stats", {"handler": self._conn_handler.get(p_id), "port_id": p_id}) for p_id in port_ids] rc, resp_list = self.transmit_batch(commands) if rc: @@ -236,10 +289,18 @@ class CTRexStatelessClient(object): else: params = {"handler": self._conn_handler.get(port_id), "port_id": port_id} - command = self.RpcCmdData("get_stream_stats", params) + command = RpcCmdData("get_stream_stats", params) return self._handle_get_stream_stats_response(command, self.transmit(command.method, command.params)) # ----- internal methods ----- # + def _init_sync(self): + # get server version and system info + err = False + if self.server_version == "Unknown" or self.system_info == "Unknown": + self.disconnect() + return False, self.__err_log + return True, "" + def transmit(self, method_name, params={}): return self.comm_link.transmit(method_name, params) @@ -265,16 +326,46 @@ class CTRexStatelessClient(object): else: return False + @staticmethod + def ack_success_test(result_obj): + if result_obj.success and result_obj.data == "ACK": + return True + else: + return False + + # ----- handler internal methods ----- # - def _handle_acquire_response(self, request, response): - if response.success: - self._conn_handler[request.get("port_id")] = response.data + def _handle_general_response(self, request, response, msg, success_test=None): + port_id = request.params.get("port_id") + if not success_test: + success_test = self.default_success_test + if success_test(response): + self._conn_handler[port_id] = response.data + return RpcResponseStatus(True, port_id, msg) + else: + return RpcResponseStatus(False, port_id, response.data) + - def _handle_release_response(self, request, response): + def _handle_acquire_response(self, request, response, success_test): + port_id = request.params.get("port_id") + if success_test(response): + self._conn_handler[port_id] = response.data + return RpcResponseStatus(True, port_id, "Acquired") + else: + return RpcResponseStatus(False, port_id, response.data) + + def _handle_remove_streams_response(self, request, response, success_test): + port_id = request.params.get("port_id") + if success_test(response): + return RpcResponseStatus(True, port_id, "Removed") + else: + return RpcResponseStatus(False, port_id, response.data) + + def _handle_release_response(self, request, response, success_test): if response.success: del self._conn_handler[request.get("port_id")] - def _handle_start_traffic_response(self, request, response): + def _handle_start_traffic_response(self, request, response, success_test): if response.success: self._active_ports.add(request.get("port_id")) @@ -282,19 +373,19 @@ class CTRexStatelessClient(object): if response.success: self._active_ports.remove(request.get("port_id")) - def _handle_get_global_stats_response(self, request, response): + def _handle_get_global_stats_response(self, request, response, success_test): if response.success: return CGlobalStats(**response.success) else: return False - def _handle_get_port_stats_response(self, request, response): + def _handle_get_port_stats_response(self, request, response, success_test): if response.success: return CPortStats(**response.success) else: return False - def _handle_get_stream_stats_response(self, request, response): + def _handle_get_stream_stats_response(self, request, response, success_test): if response.success: return CStreamStats(**response.success) else: @@ -311,13 +402,21 @@ class CTRexStatelessClient(object): return False def _process_batch_result(self, req_list, resp_list, handler_func=None, success_test=default_success_test): + res_ok = True + responses = [] + if isinstance(success_test, staticmethod): + success_test = success_test.__func__ for i, response in enumerate(resp_list): - # testing each result with success test so that a conclusion report could be deployed in future. - if success_test(response): - # run handler method with its params - handler_func(req_list[i], response) - else: - continue # TODO: mark in this case somehow the bad result + # run handler method with its params + processed_response = handler_func(req_list[i], response, success_test) + responses.append(processed_response) + if not processed_response.success: + res_ok = False + # else: + # res_ok = False # TODO: mark in this case somehow the bad result + # print res_ok + # print responses + return res_ok, responses # ------ private classes ------ # @@ -332,7 +431,11 @@ class CTRexStatelessClient(object): def connect(self): if not self.virtual: - self.rpc_link.connect() + return self.rpc_link.connect() + + def disconnect(self): + if not self.virtual: + self.rpc_link.disconnect() def transmit(self, method_name, params={}): if self.virtual: -- cgit From 3bebf60874ec07d157944088ee3cfd5909eccc0f Mon Sep 17 00:00:00 2001 From: Dan Klein Date: Tue, 27 Oct 2015 16:02:29 +0200 Subject: More HLTAPI --- .../trex_control_plane/client/trex_hltapi.py | 67 +++++++++++++++++----- .../client/trex_stateless_client.py | 19 ++++-- 2 files changed, 68 insertions(+), 18 deletions(-) (limited to 'scripts/automation/trex_control_plane/client') diff --git a/scripts/automation/trex_control_plane/client/trex_hltapi.py b/scripts/automation/trex_control_plane/client/trex_hltapi.py index 6194d376..b237071f 100755 --- a/scripts/automation/trex_control_plane/client/trex_hltapi.py +++ b/scripts/automation/trex_control_plane/client/trex_hltapi.py @@ -3,6 +3,7 @@ import trex_root_path from client_utils.packet_builder import CTRexPktBuilder from trex_stateless_client import CTRexStatelessClient +import dpkt class CTRexHltApi(object): @@ -46,28 +47,68 @@ class CTRexHltApi(object): ret_dict.update({"log": log}) return ret_dict print log - ret_dict.update({"status": 1}) - self.trex_client.disconnect() + port_handle = {device: {port: port # since only supporting single TRex at the moment, 1:1 map + for port in port_list} + } - pass + ret_dict.update({"status": 1, + "log": None, + "port_handle": port_handle, + "offline": 0}) # ports are online + return ret_dict def cleanup_session(self, port_list, maintain_lock=False): - pass + ret_dict = {"status": 0} + if not maintain_lock: + # release taken ports + if port_list == "all": + port_list = self.trex_client.get_acquired_ports() + else: + port_list = self.parse_port_list(port_list) + response = self.trex_client.release(port_list) + res_ok, log = CTRexHltApi.process_response(port_list, response) + print log + if not res_ok: + ret_dict.update({"log": log}) + return ret_dict + ret_dict.update({"status": 1, + "log": None}) + self.trex_client.disconnect() + self.trex_client = None + return ret_dict def interface_config(self, port_handle, mode="config"): - pass - - def get_status(self): - pass - - def get_port_handler(self): - pass + allowed_modes = ["config", "modify", "destroy"] + if mode not in allowed_modes: + raise ValueError("mode must be one of the following values: {modes}".format(modes=allowed_modes)) + # pass this function for now... + return {"status": 1, "log": None} + # ----- traffic functions ----- # def traffic_config(self, mode, port_handle, - mac_src, mac_dst, + l2_encap, mac_src, mac_dst, l3_protocol, ip_src_addr, ip_dst_addr, l3_length, transmit_mode, rate_pps): - pass + allowed_modes = ["create", "modify", "remove", "enable", "disable", "reset"] + if mode not in allowed_modes: + raise ValueError("mode must be one of the following values: {modes}".format(modes=allowed_modes)) + if mode == "create": + # create a new stream with desired attributes + pkt_bld = CTRexPktBuilder() + if l2_encap == "ethernet_ii": + pkt_bld.add_pkt_layer("l2", dpkt.ethernet.Ethernet()) + # set Ethernet layer attributes + pkt_bld.set_eth_layer_addr("l2", "src", mac_src) + pkt_bld.set_eth_layer_addr("l2", "dst", mac_dst) + else: + raise NotImplementedError("l2_encap does not support the desired encapsulation '{0}'".format(l2_encap)) + # set l3 attrubutes + if l3_protocol == "ipv4": + + + pass + else: + raise NotImplementedError("mode '{0}' is not supported yet on TRex".format(mode)) def traffic_control(self, action, port_handle): pass diff --git a/scripts/automation/trex_control_plane/client/trex_stateless_client.py b/scripts/automation/trex_control_plane/client/trex_stateless_client.py index 97d3ec0a..3079ee5e 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -114,6 +114,9 @@ class CTRexStatelessClient(object): def get_port_count(self): return self.system_info.get("port_count") + def get_acquired_ports(self): + return self._conn_handler.keys() + def acquire(self, port_id, force=False): if not self._is_ports_valid(port_id): raise ValueError("Provided illegal port id input") @@ -145,14 +148,16 @@ class CTRexStatelessClient(object): for p_id in port_ids] rc, resp_list = self.transmit_batch(commands) if rc: - self._process_batch_result(commands, resp_list, self._handle_release_response) + return self._process_batch_result(commands, resp_list, self._handle_release_response, + success_test=self.ack_success_test) else: self._conn_handler.pop(port_id) params = {"handler": self._conn_handler.get(port_id), "port_id": port_id} command = RpcCmdData("release", params) - self._handle_release_response(command, self.transmit(command.method, command.params)) - return + return self._handle_release_response(command, + self.transmit(command.method, command.params), + self.ack_success_test) @force_status(owned=True) def add_stream(self, stream_id, stream_obj, port_id=None): @@ -362,8 +367,12 @@ class CTRexStatelessClient(object): return RpcResponseStatus(False, port_id, response.data) def _handle_release_response(self, request, response, success_test): - if response.success: - del self._conn_handler[request.get("port_id")] + port_id = request.params.get("port_id") + if success_test(response): + del self._conn_handler[port_id] + return RpcResponseStatus(True, port_id, "Released") + else: + return RpcResponseStatus(False, port_id, response.data) def _handle_start_traffic_response(self, request, response, success_test): if response.success: -- cgit From fe6e03366eae72376f1201ed68744cb1206773de Mon Sep 17 00:00:00 2001 From: Dan Klein Date: Wed, 28 Oct 2015 07:36:08 +0200 Subject: more HLTAPI 2 --- .../trex_control_plane/client/trex_hltapi.py | 64 ++++++++++++++++------ 1 file changed, 48 insertions(+), 16 deletions(-) (limited to 'scripts/automation/trex_control_plane/client') diff --git a/scripts/automation/trex_control_plane/client/trex_hltapi.py b/scripts/automation/trex_control_plane/client/trex_hltapi.py index b237071f..cc7005e8 100755 --- a/scripts/automation/trex_control_plane/client/trex_hltapi.py +++ b/scripts/automation/trex_control_plane/client/trex_hltapi.py @@ -78,9 +78,9 @@ class CTRexHltApi(object): return ret_dict def interface_config(self, port_handle, mode="config"): - allowed_modes = ["config", "modify", "destroy"] - if mode not in allowed_modes: - raise ValueError("mode must be one of the following values: {modes}".format(modes=allowed_modes)) + ALLOWED_MODES = ["config", "modify", "destroy"] + if mode not in ALLOWED_MODES: + raise ValueError("mode must be one of the following values: {modes}".format(modes=ALLOWED_MODES)) # pass this function for now... return {"status": 1, "log": None} @@ -89,21 +89,15 @@ class CTRexHltApi(object): l2_encap, mac_src, mac_dst, l3_protocol, ip_src_addr, ip_dst_addr, l3_length, transmit_mode, rate_pps): - allowed_modes = ["create", "modify", "remove", "enable", "disable", "reset"] - if mode not in allowed_modes: - raise ValueError("mode must be one of the following values: {modes}".format(modes=allowed_modes)) + ALLOWED_MODES = ["create", "modify", "remove", "enable", "disable", "reset"] + if mode not in ALLOWED_MODES: + raise ValueError("mode must be one of the following values: {modes}".format(modes=ALLOWED_MODES)) if mode == "create": # create a new stream with desired attributes - pkt_bld = CTRexPktBuilder() - if l2_encap == "ethernet_ii": - pkt_bld.add_pkt_layer("l2", dpkt.ethernet.Ethernet()) - # set Ethernet layer attributes - pkt_bld.set_eth_layer_addr("l2", "src", mac_src) - pkt_bld.set_eth_layer_addr("l2", "dst", mac_dst) - else: - raise NotImplementedError("l2_encap does not support the desired encapsulation '{0}'".format(l2_encap)) - # set l3 attrubutes - if l3_protocol == "ipv4": + stream = CTRexHltApi.generate_stream(l2_encap, mac_src, mac_dst, + l3_protocol, ip_src_addr, ip_dst_addr, l3_length, + transmit_mode, rate_pps) + pass @@ -149,6 +143,44 @@ class CTRexHltApi(object): return "\n".join([str(response) for response in responses]) + @staticmethod + def generate_stream(l2_encap, mac_src, mac_dst, + l3_protocol, ip_src_addr, ip_dst_addr, l3_length, + transmit_mode, rate_pps): + ALLOWED_L3_PROTOCOL = {"ipv4": dpkt.ethernet.ETH_TYPE_IP, + "ipv6": dpkt.ethernet.ETH_TYPE_IP6, + "arp": dpkt.ethernet.ETH_TYPE_ARP} + ALLOWED_L4_PROTOCOL = {"tcp": dpkt.ip.IP_PROTO_TCP, + "udp": dpkt.ip.IP_PROTO_UDP, + "icmp": dpkt.ip.IP_PROTO_ICMP, + "icmpv6": dpkt.ip.IP_PROTO_ICMP6, + "igmp": dpkt.ip.IP_PROTO_IGMP, + "rtp": dpkt.ip.IP_PROTO_IRTP, + "isis": dpkt.ip.IP_PROTO_ISIS, + "ospf": dpkt.ip.IP_PROTO_OSPF} + + pkt_bld = CTRexPktBuilder() + if l2_encap == "ethernet_ii": + pkt_bld.add_pkt_layer("l2", dpkt.ethernet.Ethernet()) + # set Ethernet layer attributes + pkt_bld.set_eth_layer_addr("l2", "src", mac_src) + pkt_bld.set_eth_layer_addr("l2", "dst", mac_dst) + else: + raise NotImplementedError("l2_encap does not support the desired encapsulation '{0}'".format(l2_encap)) + # set l3 on l2 + if l3_protocol not in ALLOWED_L3_PROTOCOL: + raise ValueError("l3_protocol must be one of the following: {0}".format(ALLOWED_L3_PROTOCOL)) + pkt_bld.set_layer_attr("l2", "type", ALLOWED_L3_PROTOCOL[l3_protocol]) + + # set l3 attributes + if l3_protocol == "ipv4": + pkt_bld.add_pkt_layer("l3", dpkt.ip.IP()) + pkt_bld.set_ip_layer_addr("l3", "src", ip_src_addr) + pkt_bld.set_ip_layer_addr("l3", "dst", ip_dst_addr) + else: + raise NotImplementedError("l3_protocol '{0}' is not supported by TRex yet.".format(l3_protocol)) + + if __name__ == "__main__": pass -- cgit From 0c2b3c83f9cc0c25277c39660dce132aad55c3d7 Mon Sep 17 00:00:00 2001 From: Dan Klein Date: Wed, 28 Oct 2015 07:28:37 +0200 Subject: updated more HLTAPI functionality and fixed found bugs. Working: Start/stop traffic, traffic config (semi), connect, clean Missing: stats Next: boost console --- .../trex_control_plane/client/trex_hltapi.py | 158 ++++++++++++++++++--- .../client/trex_stateless_client.py | 36 +++-- 2 files changed, 163 insertions(+), 31 deletions(-) (limited to 'scripts/automation/trex_control_plane/client') diff --git a/scripts/automation/trex_control_plane/client/trex_hltapi.py b/scripts/automation/trex_control_plane/client/trex_hltapi.py index cc7005e8..92768ca4 100755 --- a/scripts/automation/trex_control_plane/client/trex_hltapi.py +++ b/scripts/automation/trex_control_plane/client/trex_hltapi.py @@ -3,6 +3,8 @@ import trex_root_path from client_utils.packet_builder import CTRexPktBuilder from trex_stateless_client import CTRexStatelessClient +from common.trex_streams import * +from client_utils.general_utils import id_count_gen import dpkt @@ -11,8 +13,8 @@ class CTRexHltApi(object): def __init__(self): self.trex_client = None self.connected = False - - pass + # self._stream_db = CStreamList() + self._port_data = {} # ----- session functions ----- # @@ -50,11 +52,12 @@ class CTRexHltApi(object): port_handle = {device: {port: port # since only supporting single TRex at the moment, 1:1 map for port in port_list} } - ret_dict.update({"status": 1, "log": None, "port_handle": port_handle, "offline": 0}) # ports are online + self.connected = True + self._port_data_init(port_list) return ret_dict def cleanup_session(self, port_list, maintain_lock=False): @@ -75,6 +78,7 @@ class CTRexHltApi(object): "log": None}) self.trex_client.disconnect() self.trex_client = None + self.connected = False return ret_dict def interface_config(self, port_handle, mode="config"): @@ -86,37 +90,150 @@ class CTRexHltApi(object): # ----- traffic functions ----- # def traffic_config(self, mode, port_handle, - l2_encap, mac_src, mac_dst, - l3_protocol, ip_src_addr, ip_dst_addr, l3_length, - transmit_mode, rate_pps): + l2_encap="ethernet_ii", mac_src="00:00:01:00:00:01", mac_dst="00:00:00:00:00:00", + l3_protocol="ipv4", ip_src_addr="0.0.0.0", ip_dst_addr="192.0.0.1", l3_length=110, + transmit_mode="continuous", rate_pps=100, + **kwargs): ALLOWED_MODES = ["create", "modify", "remove", "enable", "disable", "reset"] if mode not in ALLOWED_MODES: raise ValueError("mode must be one of the following values: {modes}".format(modes=ALLOWED_MODES)) if mode == "create": - # create a new stream with desired attributes - stream = CTRexHltApi.generate_stream(l2_encap, mac_src, mac_dst, - l3_protocol, ip_src_addr, ip_dst_addr, l3_length, - transmit_mode, rate_pps) - - - - pass + # create a new stream with desired attributes, starting by creating packet + try: + packet = CTRexHltApi.generate_stream(l2_encap, mac_src, mac_dst, + l3_protocol, ip_src_addr, ip_dst_addr, l3_length) + # set transmission attributes + tx_mode = CTxMode(transmit_mode, rate_pps, **kwargs) + # set rx_stats + rx_stats = CRxStats() # defaults with disabled + # join the generated data into stream + stream_obj = CStream() + stream_obj_params = {"enabled": True, + "self_start": True, + "next_stream_id": -1, + "isg": 0.0, + "mode": tx_mode, + "rx_stats": rx_stats, + "packet": packet} # vm is excluded from this list since CTRexPktBuilder obj is passed + stream_obj.load_data(**stream_obj_params) + except Exception as e: + # some exception happened during the stream creation + return {"status": 0, "log": str(e)} + # try adding the stream, until free stream_id is found + port_data = self._port_data.get(port_handle) + id_candidate = None + # TODO: change this to better implementation + while True: + id_candidate = port_data["stream_id_gen"].next() + response = self.trex_client.add_stream(stream_id=id_candidate, + stream_obj=stream_obj, + port_id=port_handle) + res_ok, log = CTRexHltApi.process_response(port_handle, response) + if res_ok: + # found non-taken stream_id on server + # save it for modifying needs + port_data["streams"].update({id_candidate: stream_obj}) + break + else: + # proceed to another iteration to use another id + continue + return {"status": 1, + "stream_id": id_candidate, + "log": None} else: raise NotImplementedError("mode '{0}' is not supported yet on TRex".format(mode)) def traffic_control(self, action, port_handle): - pass + ALLOWED_ACTIONS = ["clear_stats", "run", "stop", "sync_run"] + if action not in ALLOWED_ACTIONS: + raise ValueError("action must be one of the following values: {actions}".format(actions=ALLOWED_ACTIONS)) + # ret_dict = {"status": 0, "stopped": 1} + port_list = self.parse_port_list(port_handle) + if action == "run": + response = self.trex_client.start_traffic(port_id=port_list) + res_ok, log = CTRexHltApi.process_response(port_list, response) + if res_ok: + return {"status": 1, + "stopped": 0, + "log": None} + elif action == "stop": + response = self.trex_client.stop_traffic(port_id=port_list) + res_ok, log = CTRexHltApi.process_response(port_list, response) + if res_ok: + return {"status": 1, + "stopped": 1, + "log": None} + else: + raise NotImplementedError("action '{0}' is not supported yet on TRex".format(action)) + + # if we arrived here, this means that operation FAILED! + return {"status": 0, + "log": log} + def traffic_stats(self, port_handle, mode): - pass + ALLOWED_MODES = ["aggregate", "streams", "all"] + if mode not in ALLOWED_MODES: + raise ValueError("mode must be one of the following values: {modes}".format(modes=ALLOWED_MODES)) + # pass this function for now... + if mode == "aggregate": + # create a new stream with desired attributes, starting by creating packet + try: + packet = CTRexHltApi.generate_stream(l2_encap, mac_src, mac_dst, + l3_protocol, ip_src_addr, ip_dst_addr, l3_length) + # set transmission attributes + tx_mode = CTxMode(transmit_mode, rate_pps, **kwargs) + # set rx_stats + rx_stats = CRxStats() # defaults with disabled + # join the generated data into stream + stream_obj = CStream() + stream_obj_params = {"enabled": True, + "self_start": True, + "next_stream_id": -1, + "isg": 0.0, + "mode": tx_mode, + "rx_stats": rx_stats, + "packet": packet} # vm is excluded from this list since CTRexPktBuilder obj is passed + stream_obj.load_data(**stream_obj_params) + except Exception as e: + # some exception happened during the stream creation + return {"status": 0, "log": str(e)} + # try adding the stream, until free stream_id is found + port_data = self._port_data.get(port_handle) + id_candidate = None + # TODO: change this to better implementation + while True: + id_candidate = port_data["stream_id_gen"].next() + response = self.trex_client.add_stream(stream_id=id_candidate, + stream_obj=stream_obj, + port_id=port_handle) + res_ok, log = CTRexHltApi.process_response(port_handle, response) + if res_ok: + # found non-taken stream_id on server + # save it for modifying needs + port_data["streams"].update({id_candidate: stream_obj}) + break + else: + # proceed to another iteration to use another id + continue + return {"status": 1, + "stream_id": id_candidate, + "log": None} + else: + raise NotImplementedError("mode '{0}' is not supported yet on TRex".format(mode)) def get_aggregate_port_stats(self, port_handle): return self.traffic_stats(port_handle, mode="aggregate") def get_stream_stats(self, port_handle): - return self.traffic_stats(port_handle, mode="stream") + return self.traffic_stats(port_handle, mode="streams") # ----- internal functions ----- # + def _port_data_init(self, port_list): + for port in port_list: + self._port_data[port] = {"stream_id_gen": id_count_gen(), + "streams": {}} + @staticmethod def process_response(port_list, response): if isinstance(port_list, list): @@ -145,8 +262,7 @@ class CTRexHltApi(object): @staticmethod def generate_stream(l2_encap, mac_src, mac_dst, - l3_protocol, ip_src_addr, ip_dst_addr, l3_length, - transmit_mode, rate_pps): + l3_protocol, ip_src_addr, ip_dst_addr, l3_length): ALLOWED_L3_PROTOCOL = {"ipv4": dpkt.ethernet.ETH_TYPE_IP, "ipv6": dpkt.ethernet.ETH_TYPE_IP6, "arp": dpkt.ethernet.ETH_TYPE_ARP} @@ -177,9 +293,13 @@ class CTRexHltApi(object): pkt_bld.add_pkt_layer("l3", dpkt.ip.IP()) pkt_bld.set_ip_layer_addr("l3", "src", ip_src_addr) pkt_bld.set_ip_layer_addr("l3", "dst", ip_dst_addr) + pkt_bld.set_layer_attr("l3", "len", l3_length) else: raise NotImplementedError("l3_protocol '{0}' is not supported by TRex yet.".format(l3_protocol)) + pkt_bld.dump_pkt_to_pcap("stream_test.pcap") + return pkt_bld + if __name__ == "__main__": diff --git a/scripts/automation/trex_control_plane/client/trex_stateless_client.py b/scripts/automation/trex_control_plane/client/trex_stateless_client.py index 3079ee5e..bbdcddbe 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -62,7 +62,7 @@ class CTRexStatelessClient(object): continue if bad_ids: # Some port IDs are not according to desires status - raise RuntimeError("The requested method ('{0}') cannot be invoked since port IDs {1} are not" + raise RuntimeError("The requested method ('{0}') cannot be invoked since port IDs {1} are not " "at allowed stated".format(func.__name__, list(bad_ids))) else: return func(self, *args, **kwargs) @@ -229,13 +229,15 @@ class CTRexStatelessClient(object): for p_id in port_ids] rc, resp_list = self.transmit_batch(commands) if rc: - self._process_batch_result(commands, resp_list, self._handle_start_traffic_response) + return self._process_batch_result(commands, resp_list, self._handle_start_traffic_response, + success_test=self.ack_success_test) else: params = {"handler": self._conn_handler.get(port_id), "port_id": port_id} command = RpcCmdData("start_traffic", params) - self._handle_start_traffic_response(command, self.transmit(command.method, command.params)) - return + return self._handle_start_traffic_response(command, + self.transmit(command.method, command.params), + self.ack_success_test) @force_status(owned=False, active_and_owned=True) def stop_traffic(self, port_id=None): @@ -248,13 +250,15 @@ class CTRexStatelessClient(object): for p_id in port_ids] rc, resp_list = self.transmit_batch(commands) if rc: - self._process_batch_result(commands, resp_list, self._handle_stop_traffic_response) + return self._process_batch_result(commands, resp_list, self._handle_stop_traffic_response, + success_test=self.ack_success_test) else: params = {"handler": self._conn_handler.get(port_id), "port_id": port_id} command = RpcCmdData("stop_traffic", params) - self._handle_start_traffic_response(command, self.transmit(command.method, command.params)) - return + return self._handle_start_traffic_response(command, + self.transmit(command.method, command.params), + self.ack_success_test) def get_global_stats(self): command = RpcCmdData("get_global_stats", {}) @@ -375,12 +379,20 @@ class CTRexStatelessClient(object): return RpcResponseStatus(False, port_id, response.data) def _handle_start_traffic_response(self, request, response, success_test): - if response.success: - self._active_ports.add(request.get("port_id")) + port_id = request.params.get("port_id") + if success_test(response): + self._active_ports.add(port_id) + return RpcResponseStatus(True, port_id, "Traffic started") + else: + return RpcResponseStatus(False, port_id, response.data) - def _handle_stop_traffic_response(self, request, response): - if response.success: - self._active_ports.remove(request.get("port_id")) + def _handle_stop_traffic_response(self, request, response, success_test): + port_id = request.params.get("port_id") + if success_test(response): + self._active_ports.remove(port_id) + return RpcResponseStatus(True, port_id, "Traffic stopped") + else: + return RpcResponseStatus(False, port_id, response.data) def _handle_get_global_stats_response(self, request, response, success_test): if response.success: -- cgit From f963facdc949c087c863b8ad81aae537bcf3767b Mon Sep 17 00:00:00 2001 From: Dan Klein Date: Wed, 28 Oct 2015 23:59:11 +0200 Subject: console progress, still SHAKY! --- .../client/trex_stateless_client.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'scripts/automation/trex_control_plane/client') diff --git a/scripts/automation/trex_control_plane/client/trex_stateless_client.py b/scripts/automation/trex_control_plane/client/trex_stateless_client.py index bbdcddbe..1df07e2a 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -89,6 +89,9 @@ class CTRexStatelessClient(object): self.__err_log = ver_info return self._server_version if self._server_version else "Unknown" + def is_connected(self): + return self.comm_link.is_connected + # ----- user-access methods ----- # def connect(self): rc, err = self.comm_link.connect() @@ -114,6 +117,13 @@ class CTRexStatelessClient(object): def get_port_count(self): return self.system_info.get("port_count") + def get_port_ids(self, as_str=False): + port_ids = range(self.get_port_count()) + if as_str: + return " ".join(str(p) for p in port_ids) + else: + return port_ids + def get_acquired_ports(self): return self._conn_handler.keys() @@ -417,7 +427,7 @@ class CTRexStatelessClient(object): # check each item of the sequence return all([self._is_ports_valid(port) for port in port_id]) - elif (isinstance(port_id, int)) and (port_id > 0) and (port_id <= self.get_port_count()): + elif (isinstance(port_id, int)) and (port_id >= 0) and (port_id <= self.get_port_count()): return True else: return False @@ -450,13 +460,20 @@ class CTRexStatelessClient(object): self.port = port self.rpc_link = JsonRpcClient(self.server, self.port) + @property + def is_connected(self): + if not self.virtual: + return self.rpc_link.connected + else: + return True + def connect(self): if not self.virtual: return self.rpc_link.connect() def disconnect(self): if not self.virtual: - self.rpc_link.disconnect() + return self.rpc_link.disconnect() def transmit(self, method_name, params={}): if self.virtual: -- cgit From aa37a0abb00cbf4cb1611f9c0eefcb1ab850bc45 Mon Sep 17 00:00:00 2001 From: Dan Klein Date: Thu, 29 Oct 2015 11:34:03 +0200 Subject: Console redesign using trex_stateless_client module --- .../client/trex_stateless_client.py | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'scripts/automation/trex_control_plane/client') diff --git a/scripts/automation/trex_control_plane/client/trex_stateless_client.py b/scripts/automation/trex_control_plane/client/trex_stateless_client.py index 1df07e2a..0fff9b36 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -180,6 +180,27 @@ class CTRexStatelessClient(object): "stream": stream_obj.dump()} return self.transmit("add_stream", params) + @force_status(owned=True) + def add_stream_pack(self, port_id=None, *stream_packs): + if not self._is_ports_valid(port_id): + raise ValueError("Provided illegal port id input") + + # since almost every run contains more than one transaction with server, handle all as batch mode + port_ids = set(port_id) # convert to set to avoid duplications + commands = [] + for stream_pack in stream_packs: + commands.extend([RpcCmdData("add_stream", {"port_id": p_id, + "handler": self._conn_handler.get(p_id), + "stream_id": stream_pack.stream_id, + "stream": stream_pack.stream} + ) + for p_id in port_ids] + ) + res_ok, resp_list = self.transmit_batch(commands) + if res_ok: + return self._process_batch_result(commands, resp_list, self._handle_add_stream_response, + success_test=self.ack_success_test) + @force_status(owned=True) def remove_stream(self, stream_id, port_id=None): if not self._is_ports_valid(port_id): @@ -373,6 +394,14 @@ class CTRexStatelessClient(object): else: return RpcResponseStatus(False, port_id, response.data) + def _handle_add_stream_response(self, request, response, success_test): + port_id = request.params.get("port_id") + stream_id = request.params.get("stream_id") + if success_test(response): + return RpcResponseStatus(True, port_id, "Stream {0} added".format(stream_id)) + else: + return RpcResponseStatus(False, port_id, response.data) + def _handle_remove_streams_response(self, request, response, success_test): port_id = request.params.get("port_id") if success_test(response): -- cgit From d78150a66de591a77df2496e5de828d3232a931a Mon Sep 17 00:00:00 2001 From: Dan Klein Date: Thu, 29 Oct 2015 06:18:54 +0200 Subject: Awesome working start/stop traffic console Fixed more stability issues :) Ready for merging. --- .../automation/trex_control_plane/client/trex_stateless_client.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'scripts/automation/trex_control_plane/client') diff --git a/scripts/automation/trex_control_plane/client/trex_stateless_client.py b/scripts/automation/trex_control_plane/client/trex_stateless_client.py index 0fff9b36..4e861585 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -62,8 +62,8 @@ class CTRexStatelessClient(object): continue if bad_ids: # Some port IDs are not according to desires status - raise RuntimeError("The requested method ('{0}') cannot be invoked since port IDs {1} are not " - "at allowed stated".format(func.__name__, list(bad_ids))) + raise ValueError("The requested method ('{0}') cannot be invoked since port IDs {1} are not " + "at allowed states".format(func.__name__, list(bad_ids))) else: return func(self, *args, **kwargs) return wrapper_f @@ -127,6 +127,9 @@ class CTRexStatelessClient(object): def get_acquired_ports(self): return self._conn_handler.keys() + def get_active_ports(self): + return list(self._active_ports) + def acquire(self, port_id, force=False): if not self._is_ports_valid(port_id): raise ValueError("Provided illegal port id input") -- cgit From 2636c09cfb74c7981c27d84bcc72d00929fdbbbb Mon Sep 17 00:00:00 2001 From: Dan Klein Date: Thu, 29 Oct 2015 07:14:58 +0200 Subject: Fixed verbose mode More stability fixes removed irrelevand methods --- .../trex_control_plane/client/trex_stateless_client.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'scripts/automation/trex_control_plane/client') diff --git a/scripts/automation/trex_control_plane/client/trex_stateless_client.py b/scripts/automation/trex_control_plane/client/trex_stateless_client.py index 4e861585..9e1c7cf3 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -31,6 +31,7 @@ class CTRexStatelessClient(object): super(CTRexStatelessClient, self).__init__() self.user = username self.comm_link = CTRexStatelessClient.CCommLink(server, port, virtual) + self.verbose = False self._conn_handler = {} self._active_ports = set() self._stats = CTRexStatsManager("port", "stream") @@ -130,6 +131,10 @@ class CTRexStatelessClient(object): def get_active_ports(self): return list(self._active_ports) + def set_verbose(self, mode): + self.comm_link.set_verbose(mode) + self.verbose = mode + def acquire(self, port_id, force=False): if not self._is_ports_valid(port_id): raise ValueError("Provided illegal port id input") @@ -490,6 +495,7 @@ class CTRexStatelessClient(object): self.virtual = virtual self.server = server self.port = port + self.verbose = False self.rpc_link = JsonRpcClient(self.server, self.port) @property @@ -499,6 +505,10 @@ class CTRexStatelessClient(object): else: return True + def set_verbose(self, mode): + self.verbose = mode + return self.rpc_link.set_verbose(mode) + def connect(self): if not self.virtual: return self.rpc_link.connect() -- cgit From eacf2829c309011bf15d56b7b531b22ebeaf4d7d Mon Sep 17 00:00:00 2001 From: imarom Date: Sun, 1 Nov 2015 17:29:07 +0200 Subject: added support for 'mul' field in start_traffic RPC command --- .../automation/trex_control_plane/client/trex_stateless_client.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'scripts/automation/trex_control_plane/client') diff --git a/scripts/automation/trex_control_plane/client/trex_stateless_client.py b/scripts/automation/trex_control_plane/client/trex_stateless_client.py index 9e1c7cf3..8231fe33 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -264,7 +264,7 @@ class CTRexStatelessClient(object): if isinstance(port_id, list) or isinstance(port_id, set): # handle as batch mode port_ids = set(port_id) # convert to set to avoid duplications - commands = [RpcCmdData("start_traffic", {"handler": self._conn_handler.get(p_id), "port_id": p_id}) + commands = [RpcCmdData("start_traffic", {"handler": self._conn_handler.get(p_id), "port_id": p_id, "mul": 1.0}) for p_id in port_ids] rc, resp_list = self.transmit_batch(commands) if rc: @@ -272,7 +272,8 @@ class CTRexStatelessClient(object): success_test=self.ack_success_test) else: params = {"handler": self._conn_handler.get(port_id), - "port_id": port_id} + "port_id": port_id, + "mul": 1.0} command = RpcCmdData("start_traffic", params) return self._handle_start_traffic_response(command, self.transmit(command.method, command.params), -- cgit From 1586ab131f28c03ea65373d9e702e4051ffb9a56 Mon Sep 17 00:00:00 2001 From: imarom Date: Mon, 2 Nov 2015 16:14:02 +0200 Subject: status is back online + ZMQ async stats --- .../trex_control_plane/client/trex_async_client.py | 172 +++++++++++++++++++++ .../client/trex_stateless_client.py | 23 ++- 2 files changed, 189 insertions(+), 6 deletions(-) create mode 100644 scripts/automation/trex_control_plane/client/trex_async_client.py (limited to 'scripts/automation/trex_control_plane/client') diff --git a/scripts/automation/trex_control_plane/client/trex_async_client.py b/scripts/automation/trex_control_plane/client/trex_async_client.py new file mode 100644 index 00000000..ea716057 --- /dev/null +++ b/scripts/automation/trex_control_plane/client/trex_async_client.py @@ -0,0 +1,172 @@ +#!/router/bin/python + +try: + # support import for Python 2 + import outer_packages +except ImportError: + # support import for Python 3 + import client.outer_packages +from client_utils.jsonrpc_client import JsonRpcClient, BatchMessage + +import json +import threading +import time +import zmq +import re + +from common.trex_stats import * +from common.trex_streams import * + +# basic async stats class +class TrexAsyncStats(object): + def __init__ (self): + self.ref_point = None + + def update (self, snapshot): + + self.current = snapshot + + if self.ref_point == None: + self.ref_point = self.current + + + def get (self, field): + + if not field in self.current: + return None + + return self.current[field] + + def get_rel (self, field): + if not field in self.current: + return None + + return self.current[field] - self.ref_point[field] + + +# describes the general stats provided by TRex +class TrexAsyncStatsGeneral(TrexAsyncStats): + def __init__ (self): + super(TrexAsyncStatsGeneral, self).__init__() + + +# per port stats +class TrexAsyncStatsPort(TrexAsyncStats): + def __init__ (self): + super(TrexAsyncStatsPort, self).__init__() + + +# stats manager +class TrexAsyncStatsManager(): + def __init__ (self, port_count): + self.port_count = port_count + + self.general_stats = TrexAsyncStatsGeneral() + self.port_stats = {} + + def get_general_stats (self): + return self.general_stats + + def get_port_stats (self, port_id): + + if not port_id in self.port_stats: + return None + + return self.port_stats[port_id] + + + def update (self, snapshot): + + if snapshot['name'] == 'trex-global': + self.__handle_snapshot(snapshot['data']) + else: + # for now ignore the rest + return + + def __handle_snapshot (self, snapshot): + + general_stats = {} + port_stats = {} + + # filter the values per port and general + for key, value in snapshot.iteritems(): + + # match a pattern of ports + m = re.search('.*\-([0-8])', key) + if m: + port_id = m.group(1) + + if not port_id in port_stats: + port_stats[port_id] = {} + + port_stats[port_id][key] = value + + else: + # no port match - general stats + general_stats[key] = value + + # update the general object with the snapshot + self.general_stats.update(general_stats) + + # update all ports + for port_id, data in port_stats.iteritems(): + + if not port_id in self.port_stats: + self.port_stats[port_id] = TrexAsyncStatsPort() + + self.port_stats[port_id].update(data) + + + + + +class TrexAsyncClient(): + def __init__ (self, port): + + self.port = port + + self.raw_snapshot = {} + + self.stats = TrexAsyncStatsManager(1) + + self.active = True + self.t = threading.Thread(target = self._run) + + # kill this thread on exit and don't add it to the join list + self.t.setDaemon(True) + self.t.start() + + + def _run (self): + + # Socket to talk to server + self.context = zmq.Context() + self.socket = self.context.socket(zmq.SUB) + + self.c = "tcp://localhost:{0}".format(self.port) + print "Connecting To ZMQ Publisher At {0}".format(self.c) + + self.socket.connect(self.c) + self.socket.setsockopt(zmq.SUBSCRIBE, '') + + while self.active: + msg = json.loads(self.socket.recv_string()) + + key = msg['name'] + self.raw_snapshot[key] = msg['data'] + + self.stats.update(msg) + + + def get_stats (self): + return self.stats + + def get_raw_snapshot (self): + #return str(self.stats.global_stats.get('m_total_tx_bytes')) + " / " + str(self.stats.global_stats.get_rel('m_total_tx_bytes')) + return self.raw_snapshot + + + def stop (self): + self.active = False + self.t.join() + diff --git a/scripts/automation/trex_control_plane/client/trex_stateless_client.py b/scripts/automation/trex_control_plane/client/trex_stateless_client.py index 8231fe33..c180e0d1 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -13,6 +13,8 @@ from common.trex_stats import * from common.trex_streams import * from collections import namedtuple +from trex_async_client import TrexAsyncClient + RpcCmdData = namedtuple('RpcCmdData', ['method', 'params']) class RpcResponseStatus(namedtuple('RpcResponseStatus', ['success', 'id', 'msg'])): @@ -27,10 +29,10 @@ class RpcResponseStatus(namedtuple('RpcResponseStatus', ['success', 'id', 'msg'] class CTRexStatelessClient(object): """docstring for CTRexStatelessClient""" - def __init__(self, username, server="localhost", port=5050, virtual=False): + def __init__(self, username, server="localhost", sync_port=5050, async_port = 4500, virtual=False): super(CTRexStatelessClient, self).__init__() self.user = username - self.comm_link = CTRexStatelessClient.CCommLink(server, port, virtual) + self.comm_link = CTRexStatelessClient.CCommLink(server, sync_port, virtual) self.verbose = False self._conn_handler = {} self._active_ports = set() @@ -39,6 +41,9 @@ class CTRexStatelessClient(object): self._server_version = None self.__err_log = None + self._async_client = TrexAsyncClient(async_port) + + # ----- decorator methods ----- # def force_status(owned=True, active_and_owned=False): def wrapper(func): @@ -100,6 +105,12 @@ class CTRexStatelessClient(object): return rc, err return self._init_sync() + def get_stats_async (self): + return self._async_client.get_stats() + + def get_connection_port (self): + return self.comm_link.port + def disconnect(self): return self.comm_link.disconnect() @@ -300,10 +311,10 @@ class CTRexStatelessClient(object): self.transmit(command.method, command.params), self.ack_success_test) - def get_global_stats(self): - command = RpcCmdData("get_global_stats", {}) - return self._handle_get_global_stats_response(command, self.transmit(command.method, command.params)) - # return self.transmit("get_global_stats") +# def get_global_stats(self): +# command = RpcCmdData("get_global_stats", {}) +# return self._handle_get_global_stats_response(command, self.transmit(command.method, command.params)) +# # return self.transmit("get_global_stats") @force_status(owned=True, active_and_owned=True) def get_port_stats(self, port_id=None): -- cgit From 3fb4e4c130da10e58af07e1f783f093515e90f96 Mon Sep 17 00:00:00 2001 From: imarom Date: Mon, 2 Nov 2015 16:40:34 +0200 Subject: few bug fixes for last commit --- .../trex_control_plane/client/trex_async_client.py | 30 +++++++++++++++------- 1 file changed, 21 insertions(+), 9 deletions(-) (limited to 'scripts/automation/trex_control_plane/client') diff --git a/scripts/automation/trex_control_plane/client/trex_async_client.py b/scripts/automation/trex_control_plane/client/trex_async_client.py index ea716057..1ce10288 100644 --- a/scripts/automation/trex_control_plane/client/trex_async_client.py +++ b/scripts/automation/trex_control_plane/client/trex_async_client.py @@ -11,6 +11,7 @@ from client_utils.jsonrpc_client import JsonRpcClient, BatchMessage import json import threading import time +import datetime import zmq import re @@ -21,9 +22,14 @@ from common.trex_streams import * class TrexAsyncStats(object): def __init__ (self): self.ref_point = None + self.current = {} + self.last_update_ts = datetime.datetime.now() def update (self, snapshot): + #update + self.last_update_ts = datetime.datetime.now() + self.current = snapshot if self.ref_point == None: @@ -33,17 +39,22 @@ class TrexAsyncStats(object): def get (self, field): if not field in self.current: - return None + return 0 return self.current[field] def get_rel (self, field): if not field in self.current: - return None + return 0 return self.current[field] - self.ref_point[field] + # return true if new data has arrived in the past 2 seconds + def is_online (self): + delta_ms = (datetime.datetime.now() - self.last_update_ts).total_seconds() * 1000 + return (delta_ms < 2000) + # describes the general stats provided by TRex class TrexAsyncStatsGeneral(TrexAsyncStats): def __init__ (self): @@ -58,12 +69,12 @@ class TrexAsyncStatsPort(TrexAsyncStats): # stats manager class TrexAsyncStatsManager(): - def __init__ (self, port_count): - self.port_count = port_count + def __init__ (self): self.general_stats = TrexAsyncStatsGeneral() self.port_stats = {} + def get_general_stats (self): return self.general_stats @@ -127,7 +138,11 @@ class TrexAsyncClient(): self.raw_snapshot = {} - self.stats = TrexAsyncStatsManager(1) + self.stats = TrexAsyncStatsManager() + + + self.tr = "tcp://localhost:{0}".format(self.port) + print "\nConnecting To ZMQ Publisher At {0}".format(self.tr) self.active = True self.t = threading.Thread(target = self._run) @@ -143,10 +158,7 @@ class TrexAsyncClient(): self.context = zmq.Context() self.socket = self.context.socket(zmq.SUB) - self.c = "tcp://localhost:{0}".format(self.port) - print "Connecting To ZMQ Publisher At {0}".format(self.c) - - self.socket.connect(self.c) + self.socket.connect(self.tr) self.socket.setsockopt(zmq.SUBSCRIBE, '') while self.active: -- cgit From 0ceddc74c938a023c515be4ed2c37198fd66e87e Mon Sep 17 00:00:00 2001 From: Dan Klein Date: Tue, 3 Nov 2015 09:37:42 +0200 Subject: first commit for advnaced options --- scripts/automation/trex_control_plane/client/trex_async_client.py | 2 +- .../automation/trex_control_plane/client/trex_stateless_client.py | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) (limited to 'scripts/automation/trex_control_plane/client') diff --git a/scripts/automation/trex_control_plane/client/trex_async_client.py b/scripts/automation/trex_control_plane/client/trex_async_client.py index 1ce10288..49ef9506 100644 --- a/scripts/automation/trex_control_plane/client/trex_async_client.py +++ b/scripts/automation/trex_control_plane/client/trex_async_client.py @@ -131,7 +131,7 @@ class TrexAsyncStatsManager(): -class TrexAsyncClient(): +class CTRexAsyncClient(): def __init__ (self, port): self.port = port diff --git a/scripts/automation/trex_control_plane/client/trex_stateless_client.py b/scripts/automation/trex_control_plane/client/trex_stateless_client.py index c180e0d1..6b1b7b94 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -13,7 +13,7 @@ from common.trex_stats import * from common.trex_streams import * from collections import namedtuple -from trex_async_client import TrexAsyncClient +from trex_async_client import CTRexAsyncClient RpcCmdData = namedtuple('RpcCmdData', ['method', 'params']) @@ -41,7 +41,7 @@ class CTRexStatelessClient(object): self._server_version = None self.__err_log = None - self._async_client = TrexAsyncClient(async_port) + self._async_client = CTRexAsyncClient(async_port) # ----- decorator methods ----- # @@ -136,6 +136,10 @@ class CTRexStatelessClient(object): else: return port_ids + def sync_user(self): + return self.transmit("sync_user") + + def get_acquired_ports(self): return self._conn_handler.keys() -- cgit From c2928039f199d3cef6d130201cf825d5b6b67937 Mon Sep 17 00:00:00 2001 From: imarom Date: Tue, 3 Nov 2015 10:10:52 +0200 Subject: fields for specific ports on the status screen --- .../trex_control_plane/client/trex_async_client.py | 40 ++++++++++++++++------ 1 file changed, 29 insertions(+), 11 deletions(-) (limited to 'scripts/automation/trex_control_plane/client') diff --git a/scripts/automation/trex_control_plane/client/trex_async_client.py b/scripts/automation/trex_control_plane/client/trex_async_client.py index 1ce10288..4c17603d 100644 --- a/scripts/automation/trex_control_plane/client/trex_async_client.py +++ b/scripts/automation/trex_control_plane/client/trex_async_client.py @@ -25,6 +25,15 @@ class TrexAsyncStats(object): self.current = {} self.last_update_ts = datetime.datetime.now() + def __format_num (self, size, suffix = ""): + + for unit in ['','K','M','G','T','P']: + if abs(size) < 1000.0: + return "%3.2f %s%s" % (size, unit, suffix) + size /= 1000.0 + + return "NaN" + def update (self, snapshot): #update @@ -36,18 +45,25 @@ class TrexAsyncStats(object): self.ref_point = self.current - def get (self, field): + def get (self, field, format = False, suffix = ""): if not field in self.current: - return 0 + return "N/A" - return self.current[field] + if not format: + return self.current[field] + else: + return self.__format_num(self.current[field], suffix) - def get_rel (self, field): + + def get_rel (self, field, format = False, suffix = ""): if not field in self.current: - return 0 + return "N/A" - return self.current[field] - self.ref_point[field] + if not format: + return (self.current[field] - self.ref_point[field]) + else: + return self.__format_num(self.current[field] - self.ref_point[field], suffix) # return true if new data has arrived in the past 2 seconds @@ -80,10 +96,10 @@ class TrexAsyncStatsManager(): def get_port_stats (self, port_id): - if not port_id in self.port_stats: + if not str(port_id) in self.port_stats: return None - return self.port_stats[port_id] + return self.port_stats[str(port_id)] def update (self, snapshot): @@ -103,14 +119,16 @@ class TrexAsyncStatsManager(): for key, value in snapshot.iteritems(): # match a pattern of ports - m = re.search('.*\-([0-8])', key) + m = re.search('(.*)\-([0-8])', key) if m: - port_id = m.group(1) + + port_id = m.group(2) + field_name = m.group(1) if not port_id in port_stats: port_stats[port_id] = {} - port_stats[port_id][key] = value + port_stats[port_id][field_name] = value else: # no port match - general stats -- cgit From 597f74d8ed10abc3dd9df7e81ecea5ac2f5c714e Mon Sep 17 00:00:00 2001 From: Dan Klein Date: Tue, 3 Nov 2015 11:27:48 +0200 Subject: updated sync_client --- .../trex_control_plane/client/trex_stateless_client.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'scripts/automation/trex_control_plane/client') diff --git a/scripts/automation/trex_control_plane/client/trex_stateless_client.py b/scripts/automation/trex_control_plane/client/trex_stateless_client.py index 6b1b7b94..aeb25422 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -136,9 +136,8 @@ class CTRexStatelessClient(object): else: return port_ids - def sync_user(self): - return self.transmit("sync_user") - + def sync_user(self, sync_streams=False): + return self.transmit("sync_user", {"user": self.user, "sync_streams": sync_streams}) def get_acquired_ports(self): return self._conn_handler.keys() @@ -363,8 +362,21 @@ class CTRexStatelessClient(object): if self.server_version == "Unknown" or self.system_info == "Unknown": self.disconnect() return False, self.__err_log + # sync with previous session + res_ok, port_info = self.sync_user() + if not res_ok: + self.disconnect() + return False, port_info + else: + # handle sync data + for port in port_info: + self._conn_handler[port.get("port_id")] = port.get("handler") + if port.get("state") == "transmitting": + # port is active + self._active_ports.add(port.get("port_id")) return True, "" + def transmit(self, method_name, params={}): return self.comm_link.transmit(method_name, params) -- cgit From e92507617ed8069b674fa5729b1e6a0c5d4b2662 Mon Sep 17 00:00:00 2001 From: imarom Date: Thu, 5 Nov 2015 09:55:03 +0200 Subject: bug fixes in status window --- .../trex_control_plane/client/trex_async_client.py | 3 ++- .../trex_control_plane/client/trex_stateless_client.py | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) (limited to 'scripts/automation/trex_control_plane/client') diff --git a/scripts/automation/trex_control_plane/client/trex_async_client.py b/scripts/automation/trex_control_plane/client/trex_async_client.py index 419448bb..72cce5aa 100644 --- a/scripts/automation/trex_control_plane/client/trex_async_client.py +++ b/scripts/automation/trex_control_plane/client/trex_async_client.py @@ -82,6 +82,8 @@ class TrexAsyncStatsPort(TrexAsyncStats): def __init__ (self): super(TrexAsyncStatsPort, self).__init__() + def get_stream_stats (self, stream_id): + return None # stats manager class TrexAsyncStatsManager(): @@ -101,7 +103,6 @@ class TrexAsyncStatsManager(): return self.port_stats[str(port_id)] - def update (self, snapshot): if snapshot['name'] == 'trex-global': diff --git a/scripts/automation/trex_control_plane/client/trex_stateless_client.py b/scripts/automation/trex_control_plane/client/trex_stateless_client.py index aeb25422..db51683a 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -254,7 +254,17 @@ class CTRexStatelessClient(object): self.ack_success_test) pass - @force_status(owned=True, active_and_owned=True) + + @force_status(owned=True)#, active_and_owned=True) + def get_all_streams(self, port_id, get_pkt = False): + if not self._is_ports_valid(port_id): + raise ValueError("Provided illegal port id input") + params = {"handler": self._conn_handler.get(port_id), + "port_id": port_id, + "get_pkt": get_pkt} + return self.transmit("get_all_streams", params) + + @force_status(owned=True)#, active_and_owned=True) def get_stream_id_list(self, port_id=None): if not self._is_ports_valid(port_id): raise ValueError("Provided illegal port id input") @@ -263,12 +273,13 @@ class CTRexStatelessClient(object): return self.transmit("get_stream_list", params) @force_status(owned=True, active_and_owned=True) - def get_stream(self, stream_id, port_id=None): + def get_stream(self, stream_id, port_id, get_pkt = False): if not self._is_ports_valid(port_id): raise ValueError("Provided illegal port id input") params = {"handler": self._conn_handler.get(port_id), "port_id": port_id, - "stream_id": stream_id} + "stream_id": stream_id, + "get_pkt": get_pkt} return self.transmit("get_stream_list", params) @force_status(owned=True) -- cgit From 953a250e6cbaea3040920e7441d2d019705efe51 Mon Sep 17 00:00:00 2001 From: Dan Klein Date: Sat, 7 Nov 2015 13:37:31 +0200 Subject: Extended line parsing options, didn't apply all changes on console YET --- .../client/trex_stateless_client.py | 51 ++++++++++++++++++++-- 1 file changed, 47 insertions(+), 4 deletions(-) (limited to 'scripts/automation/trex_control_plane/client') diff --git a/scripts/automation/trex_control_plane/client/trex_stateless_client.py b/scripts/automation/trex_control_plane/client/trex_stateless_client.py index aeb25422..dc6e6be2 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -45,16 +45,57 @@ class CTRexStatelessClient(object): # ----- decorator methods ----- # + def acquired(func): + def wrapper_f(self, *args, **kwargs): + # print func.__name__ + # print args + # print kwargs + port_ids = kwargs.get("port_id") + if not port_ids: + # print "FROM ARGS!" + # print args + port_ids = args[0] + if isinstance(port_ids, int): + # make sure port_ids is a list + port_ids = [port_ids] + bad_ids = set() + # print "=============" + # print port_ids + for port_id in port_ids: + port_owned = self._conn_handler.get(port_id) + if not port_owned: + bad_ids.add(port_id) + # elif active_and_owned: # stronger condition than just owned, hence gets precedence + # if port_owned and port_id in self._active_ports: + # continue + # else: + # bad_ids.add(port_id) + else: + continue + if bad_ids: + # Some port IDs are not according to desires status + raise ValueError("The requested method ('{0}') cannot be invoked since port IDs {1} are not " + "at allowed states".format(func.__name__, list(bad_ids))) + else: + return func(self, *args, **kwargs) + return wrapper_f + def force_status(owned=True, active_and_owned=False): def wrapper(func): def wrapper_f(self, *args, **kwargs): + # print args + # print kwargs port_ids = kwargs.get("port_id") if not port_ids: + print "FROM ARGS!" + print args port_ids = args[0] if isinstance(port_ids, int): # make sure port_ids is a list port_ids = [port_ids] bad_ids = set() + # print "=============" + # print port_ids for port_id in port_ids: port_owned = self._conn_handler.get(port_id) if owned and not port_owned: @@ -271,14 +312,16 @@ class CTRexStatelessClient(object): "stream_id": stream_id} return self.transmit("get_stream_list", params) - @force_status(owned=True) - def start_traffic(self, port_id=None): + @acquired + def start_traffic(self, multiplier, port_id=None): if not self._is_ports_valid(port_id): raise ValueError("Provided illegal port id input") if isinstance(port_id, list) or isinstance(port_id, set): # handle as batch mode port_ids = set(port_id) # convert to set to avoid duplications - commands = [RpcCmdData("start_traffic", {"handler": self._conn_handler.get(p_id), "port_id": p_id, "mul": 1.0}) + commands = [RpcCmdData("start_traffic", {"handler": self._conn_handler.get(p_id), + "port_id": p_id, + "mul": multiplier}) for p_id in port_ids] rc, resp_list = self.transmit_batch(commands) if rc: @@ -287,7 +330,7 @@ class CTRexStatelessClient(object): else: params = {"handler": self._conn_handler.get(port_id), "port_id": port_id, - "mul": 1.0} + "mul": multiplier} command = RpcCmdData("start_traffic", params) return self._handle_start_traffic_response(command, self.transmit(command.method, command.params), -- cgit From bc7d9ee81604fd33607569ac4f03ca8b91777b29 Mon Sep 17 00:00:00 2001 From: imarom Date: Sun, 8 Nov 2015 11:39:09 +0200 Subject: code review notes: 1. performance improvement for stateless DP core object (direct object) 2. exit scheduler loop is now using a scheduled message and not IF 3. duration for inifinite is negative number 4. fixed stop_traffic scheduled node time --- scripts/automation/trex_control_plane/client/trex_stateless_client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'scripts/automation/trex_control_plane/client') diff --git a/scripts/automation/trex_control_plane/client/trex_stateless_client.py b/scripts/automation/trex_control_plane/client/trex_stateless_client.py index e65a923e..627c3365 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -87,8 +87,8 @@ class CTRexStatelessClient(object): # print kwargs port_ids = kwargs.get("port_id") if not port_ids: - print "FROM ARGS!" - print args + #print "FROM ARGS!" + #print args port_ids = args[0] if isinstance(port_ids, int): # make sure port_ids is a list -- cgit