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/console/trex_status.py | 44 +++++++++------------- 1 file changed, 18 insertions(+), 26 deletions(-) (limited to 'scripts/automation/trex_control_plane/console') diff --git a/scripts/automation/trex_control_plane/console/trex_status.py b/scripts/automation/trex_control_plane/console/trex_status.py index 4e73e0bb..ac8e7411 100644 --- a/scripts/automation/trex_control_plane/console/trex_status.py +++ b/scripts/automation/trex_control_plane/console/trex_status.py @@ -18,15 +18,6 @@ def percentage (a, total): x = int ((float(a) / total) * 100) return str(x) + "%" -# simple float to human readable -def float_to_human_readable (size, suffix = "bps"): - for unit in ['','K','M','G','T']: - if abs(size) < 1000.0: - return "%3.2f %s%s" % (size, unit, suffix) - size /= 1000.0 - return "NaN" - - ################### panels ################# # panel object @@ -37,6 +28,8 @@ class TrexStatusPanel(object): self.log = status_obj.log self.stateless_client = status_obj.stateless_client + + self.stats = status_obj.stats self.general_stats = status_obj.general_stats self.h = h @@ -120,17 +113,17 @@ class GeneralInfoPanel(TrexStatusPanel): self.getwin().addstr(3, 2, "{:<30} {:0.2f} %".format("CPU util.:", self.general_stats.get("m_cpu_util"))) self.getwin().addstr(5, 2, "{:<30} {:} / {:}".format("Total Tx. rate:", - float_to_human_readable(self.general_stats.get("m_tx_bps")), - float_to_human_readable(self.general_stats.get("m_tx_pps"), suffix = "pps"))) + self.general_stats.get("m_tx_bps", format = True, suffix = "bps"), + self.general_stats.get("m_tx_pps", format = True, suffix = "pps"))) # missing RX field #self.getwin().addstr(5, 2, "{:<30} {:} / {:}".format("Total Rx. rate:", - # float_to_human_readable(self.general_stats.get("m_rx_bps")), - # float_to_human_readable(self.general_stats.get("m_rx_pps"), suffix = "pps"))) + # self.general_stats.get("m_rx_bps"), + # self.general_stats.get("m_rx_pps"), suffix = "pps")) self.getwin().addstr(7, 2, "{:<30} {:} / {:}".format("Total Tx:", - float_to_human_readable(self.general_stats.get_rel("m_total_tx_bytes"), suffix = "B"), - float_to_human_readable(self.general_stats.get_rel("m_total_tx_pkts"), suffix = "pkts"))) + self.general_stats.get_rel("m_total_tx_bytes", format = True, suffix = "B"), + self.general_stats.get_rel("m_total_tx_pkts", format = True, suffix = "pkts"))) # all ports stats class PortsStatsPanel(TrexStatusPanel): @@ -142,7 +135,6 @@ class PortsStatsPanel(TrexStatusPanel): def draw (self): self.clear() - return owned_ports = self.status_obj.owned_ports if not owned_ports: @@ -153,23 +145,22 @@ class PortsStatsPanel(TrexStatusPanel): self.getwin().addstr(3, 2, "{:^15} {:^15} {:^15} {:^15} {:^15} {:^15} {:^15}".format( "Port ID", "Tx [pps]", "Tx [bps]", "Tx [bytes]", "Rx [pps]", "Rx [bps]", "Rx [bytes]")) - # port loop - self.status_obj.stats.query_sync() + for i, port_index in enumerate(owned_ports): port_stats = self.status_obj.stats.get_port_stats(port_index) if port_stats: - self.getwin().addstr(5 + (i * 4), 2, "{:^15} {:^15,.2f} {:^15,.2f} {:^15,} {:^15,.2f} {:^15,.2f} {:^15,}".format( + self.getwin().addstr(5 + (i * 4), 2, "{:^15} {:^15} {:^15} {:^15} {:^15} {:^15} {:^15}".format( "{0} ({1})".format(str(port_index), self.status_obj.server_sys_info["ports"][port_index]["speed"]), - port_stats["tx_pps"], - port_stats["tx_bps"], - port_stats["total_tx_bytes"], - port_stats["rx_pps"], - port_stats["rx_bps"], - port_stats["total_rx_bytes"])) - + port_stats.get("m_total_tx_pps", format = True, suffix = "pps"), + port_stats.get("m_total_tx_bps", format = True, suffix = "bps"), + port_stats.get_rel("obytes", format = True, suffix = "B"), + port_stats.get("m_total_rx_pps", format = True, suffix = "pps"), + port_stats.get("m_total_rx_bps", format = True, suffix = "bps"), + port_stats.get_rel("ibytes", format = True, suffix = "B"))) + else: self.getwin().addstr(5 + (i * 4), 2, "{:^15} {:^15} {:^15} {:^15} {:^15} {:^15} {:^15}".format( "{0} ({1})".format(str(port_index), self.status_obj.server_sys_info["ports"][port_index]["speed"]), @@ -374,6 +365,7 @@ class TrexStatus(): self.log = TrexStatusLog() self.cmds = TrexStatusCommands(self) + self.stats = stateless_client.get_stats_async() self.general_stats = stateless_client.get_stats_async().get_general_stats() # fetch server info -- cgit 1.2.3-korg From 30b87959dc2d8da7f2f2a471a53485d600d8735a Mon Sep 17 00:00:00 2001 From: imarom Date: Wed, 4 Nov 2015 11:31:30 +0200 Subject: some fields were missing from the ZMQ publisher --- .../trex_control_plane/console/trex_console.py | 4 +- .../trex_control_plane/console/trex_status.py | 19 ++++++---- src/main_dpdk.cpp | 43 +++++++++++++++++----- 3 files changed, 48 insertions(+), 18 deletions(-) (limited to 'scripts/automation/trex_control_plane/console') diff --git a/scripts/automation/trex_control_plane/console/trex_console.py b/scripts/automation/trex_control_plane/console/trex_console.py index e707a9e1..8a5b29cc 100755 --- a/scripts/automation/trex_control_plane/console/trex_console.py +++ b/scripts/automation/trex_control_plane/console/trex_console.py @@ -257,7 +257,9 @@ class TRexConsole(cmd.Cmd): # make sure that the user wants to acquire all args = line.split() if len(args) < 1: - print "Please provide a list of ports separated by spaces, or specify 'all' to acquire all available ports" + print magenta("Please provide a list of ports separated by spaces, or specify 'all' to acquire all available ports") + return + if args[0] == "all": ask = ConfirmMenu('Are you sure you want to acquire all ports ? ') rc = ask.show() diff --git a/scripts/automation/trex_control_plane/console/trex_status.py b/scripts/automation/trex_control_plane/console/trex_status.py index ac8e7411..a19587a3 100644 --- a/scripts/automation/trex_control_plane/console/trex_status.py +++ b/scripts/automation/trex_control_plane/console/trex_status.py @@ -112,19 +112,24 @@ class GeneralInfoPanel(TrexStatusPanel): self.getwin().addstr(3, 2, "{:<30} {:0.2f} %".format("CPU util.:", self.general_stats.get("m_cpu_util"))) - self.getwin().addstr(5, 2, "{:<30} {:} / {:}".format("Total Tx. rate:", + self.getwin().addstr(6, 2, "{:<30} {:} / {:}".format("Total Tx. rate:", self.general_stats.get("m_tx_bps", format = True, suffix = "bps"), self.general_stats.get("m_tx_pps", format = True, suffix = "pps"))) - # missing RX field - #self.getwin().addstr(5, 2, "{:<30} {:} / {:}".format("Total Rx. rate:", - # self.general_stats.get("m_rx_bps"), - # self.general_stats.get("m_rx_pps"), suffix = "pps")) - - self.getwin().addstr(7, 2, "{:<30} {:} / {:}".format("Total Tx:", + + self.getwin().addstr(8, 2, "{:<30} {:} / {:}".format("Total Tx:", self.general_stats.get_rel("m_total_tx_bytes", format = True, suffix = "B"), self.general_stats.get_rel("m_total_tx_pkts", format = True, suffix = "pkts"))) + self.getwin().addstr(11, 2, "{:<30} {:} / {:}".format("Total Rx. rate:", + self.general_stats.get("m_rx_bps", format = True, suffix = "bps"), + self.general_stats.get("m_rx_pps", format = True, suffix = "pps"))) + + + self.getwin().addstr(13, 2, "{:<30} {:} / {:}".format("Total Rx:", + self.general_stats.get_rel("m_total_rx_bytes", format = True, suffix = "B"), + self.general_stats.get_rel("m_total_rx_pkts", format = True, suffix = "pkts"))) + # all ports stats class PortsStatsPanel(TrexStatusPanel): def __init__ (self, h, l, y, x, status_obj): diff --git a/src/main_dpdk.cpp b/src/main_dpdk.cpp index ed7f5e90..9da89121 100755 --- a/src/main_dpdk.cpp +++ b/src/main_dpdk.cpp @@ -1177,7 +1177,7 @@ public: m_port_id = portid; m_last_rx_rate = 0.0; m_last_tx_rate = 0.0; - m_last_pps=0.0; + m_last_tx_pps = 0.0; return (true); } void Delete(); @@ -1253,8 +1253,12 @@ public: return (m_last_rx_rate); } - float get_last_pps_rate(){ - return (m_last_pps); + float get_last_tx_pps_rate(){ + return (m_last_tx_pps); + } + + float get_last_rx_pps_rate(){ + return (m_last_rx_pps); } CPhyEthIFStats & get_stats(){ @@ -1307,12 +1311,14 @@ private: CBwMeasure m_bw_tx; CBwMeasure m_bw_rx; CPPSMeasure m_pps_tx; + CPPSMeasure m_pps_rx; CPhyEthIFStats m_stats; - float m_last_rx_rate; float m_last_tx_rate; - float m_last_pps; + float m_last_rx_rate; + float m_last_tx_pps; + float m_last_rx_pps; public: struct rte_eth_dev_info m_dev_info; }; @@ -1648,7 +1654,8 @@ void CPhyEthIF::get_stats_1g(CPhyEthIFStats *stats){ m_last_tx_rate = m_bw_tx.add(stats->obytes); m_last_rx_rate = m_bw_rx.add(stats->ibytes); - m_last_pps = m_pps_tx.add(stats->opackets); + m_last_tx_pps = m_pps_tx.add(stats->opackets); + m_last_rx_pps = m_pps_rx.add(stats->ipackets); } @@ -1658,7 +1665,8 @@ void CPhyEthIF::get_stats(CPhyEthIFStats *stats){ m_last_tx_rate = m_bw_tx.add(stats->obytes); m_last_rx_rate = m_bw_rx.add(stats->ibytes); - m_last_pps = m_pps_tx.add(stats->opackets); + m_last_tx_pps = m_pps_tx.add(stats->opackets); + m_last_rx_pps = m_pps_rx.add(stats->ipackets); } @@ -2468,6 +2476,10 @@ public: uint64_t oerrors; float m_total_tx_bps; + float m_total_tx_pps; + + float m_total_rx_bps; + float m_total_rx_pps; }; class CGlobalStats { @@ -2504,6 +2516,7 @@ public: float m_tx_bps; float m_rx_bps; float m_tx_pps; + float m_rx_pps; float m_tx_cps; float m_tx_expected_cps; float m_tx_expected_pps; @@ -2564,6 +2577,7 @@ void CGlobalStats::dump_json(std::string & json){ json+=GET_FIELD(m_tx_bps); json+=GET_FIELD(m_rx_bps); json+=GET_FIELD(m_tx_pps); + json+=GET_FIELD(m_rx_pps); json+=GET_FIELD(m_tx_cps); json+=GET_FIELD(m_tx_expected_cps); json+=GET_FIELD(m_tx_expected_pps); @@ -2598,6 +2612,9 @@ void CGlobalStats::dump_json(std::string & json){ json+=GET_FIELD_PORT(i,ierrors) ; json+=GET_FIELD_PORT(i,oerrors) ; json+=GET_FIELD_PORT(i,m_total_tx_bps); + json+=GET_FIELD_PORT(i,m_total_tx_pps); + json+=GET_FIELD_PORT(i,m_total_rx_bps); + json+=GET_FIELD_PORT(i,m_total_rx_pps); } json+=m_template.dump_as_json("template"); json+="\"unknown\":0}}" ; @@ -3728,7 +3745,8 @@ void CGlobalTRex::get_stats(CGlobalStats & stats){ int i; float total_tx=0.0; float total_rx=0.0; - float total_pps=0.0; + float total_tx_pps=0.0; + float total_rx_pps=0.0; stats.m_total_tx_pkts = 0; stats.m_total_rx_pkts = 0; @@ -3756,6 +3774,9 @@ void CGlobalTRex::get_stats(CGlobalStats & stats){ stp->ierrors = st.ierrors; stp->oerrors = st.oerrors; stp->m_total_tx_bps = _if->get_last_tx_rate()*_1Mb_DOUBLE; + stp->m_total_tx_pps = _if->get_last_tx_pps_rate(); + stp->m_total_rx_bps = _if->get_last_rx_rate()*_1Mb_DOUBLE; + stp->m_total_rx_pps = _if->get_last_rx_pps_rate(); stats.m_total_tx_pkts += st.opackets; stats.m_total_rx_pkts += st.ipackets; @@ -3764,7 +3785,8 @@ void CGlobalTRex::get_stats(CGlobalStats & stats){ total_tx +=_if->get_last_tx_rate(); total_rx +=_if->get_last_rx_rate(); - total_pps +=_if->get_last_pps_rate(); + total_tx_pps +=_if->get_last_tx_pps_rate(); + total_rx_pps +=_if->get_last_rx_pps_rate(); } @@ -3847,7 +3869,8 @@ void CGlobalTRex::get_stats(CGlobalStats & stats){ stats.m_tx_bps = total_tx*pf*_1Mb_DOUBLE; stats.m_rx_bps = total_rx*pf*_1Mb_DOUBLE; - stats.m_tx_pps = total_pps*pf; + stats.m_tx_pps = total_tx_pps*pf; + stats.m_rx_pps = total_rx_pps*pf; stats.m_tx_cps = m_last_total_cps*pf; stats.m_tx_expected_cps = m_expected_cps*pf; -- cgit 1.2.3-korg From 530625493e92ac300b8a1135d061a3ecd428008d Mon Sep 17 00:00:00 2001 From: imarom Date: Wed, 4 Nov 2015 13:35:08 +0200 Subject: new format for the status per port --- .../trex_control_plane/console/trex_status.py | 45 ++++++++++++++++------ 1 file changed, 33 insertions(+), 12 deletions(-) (limited to 'scripts/automation/trex_control_plane/console') diff --git a/scripts/automation/trex_control_plane/console/trex_status.py b/scripts/automation/trex_control_plane/console/trex_status.py index a19587a3..c410e7c5 100644 --- a/scripts/automation/trex_control_plane/console/trex_status.py +++ b/scripts/automation/trex_control_plane/console/trex_status.py @@ -147,8 +147,8 @@ class PortsStatsPanel(TrexStatusPanel): return # table header - self.getwin().addstr(3, 2, "{:^15} {:^15} {:^15} {:^15} {:^15} {:^15} {:^15}".format( - "Port ID", "Tx [pps]", "Tx [bps]", "Tx [bytes]", "Rx [pps]", "Rx [bps]", "Rx [bytes]")) + self.getwin().addstr(3, 2, "{:^15} {:^30} {:^30} {:^30}".format( + "Port ID", "Tx Rate [bps/pps]", "Rx Rate [bps/pps]", "Total Bytes [tx/rx]")) @@ -157,25 +157,46 @@ class PortsStatsPanel(TrexStatusPanel): port_stats = self.status_obj.stats.get_port_stats(port_index) if port_stats: - self.getwin().addstr(5 + (i * 4), 2, "{:^15} {:^15} {:^15} {:^15} {:^15} {:^15} {:^15}".format( + self.getwin().addstr(5 + (i * 4), 2, "{:^15} {:^30} {:^30} {:^30}".format( "{0} ({1})".format(str(port_index), self.status_obj.server_sys_info["ports"][port_index]["speed"]), - port_stats.get("m_total_tx_pps", format = True, suffix = "pps"), - port_stats.get("m_total_tx_bps", format = True, suffix = "bps"), - port_stats.get_rel("obytes", format = True, suffix = "B"), - port_stats.get("m_total_rx_pps", format = True, suffix = "pps"), - port_stats.get("m_total_rx_bps", format = True, suffix = "bps"), - port_stats.get_rel("ibytes", format = True, suffix = "B"))) + "{0} / {1}".format(port_stats.get("m_total_tx_bps", format = True, suffix = "bps"), + port_stats.get("m_total_tx_pps", format = True, suffix = "pps")), + + "{0} / {1}".format(port_stats.get("m_total_rx_bps", format = True, suffix = "bps"), + port_stats.get("m_total_rx_pps", format = True, suffix = "pps")), + "{0} / {1}".format(port_stats.get_rel("obytes", format = True, suffix = "B"), + port_stats.get_rel("ibytes", format = True, suffix = "B")))) else: - self.getwin().addstr(5 + (i * 4), 2, "{:^15} {:^15} {:^15} {:^15} {:^15} {:^15} {:^15}".format( + self.getwin().addstr(5 + (i * 4), 2, 2, "{:^15} {:^30} {:^30} {:^30}".format( "{0} ({1})".format(str(port_index), self.status_obj.server_sys_info["ports"][port_index]["speed"]), "N/A", "N/A", "N/A", - "N/A", - "N/A", "N/A")) + + # old format +# if port_stats: +# self.getwin().addstr(5 + (i * 4), 2, "{:^15} {:^15} {:^15} {:^15} {:^15} {:^15} {:^15}".format( +# "{0} ({1})".format(str(port_index), self.status_obj.server_sys_info["ports"][port_index]["speed"]), +# port_stats.get("m_total_tx_pps", format = True, suffix = "pps"), +# port_stats.get("m_total_tx_bps", format = True, suffix = "bps"), +# port_stats.get_rel("obytes", format = True, suffix = "B"), +# port_stats.get("m_total_rx_pps", format = True, suffix = "pps"), +# port_stats.get("m_total_rx_bps", format = True, suffix = "bps"), +# port_stats.get_rel("ibytes", format = True, suffix = "B"))) +# +# else: +# self.getwin().addstr(5 + (i * 4), 2, "{:^15} {:^15} {:^15} {:^15} {:^15} {:^15} {:^15}".format( +# "{0} ({1})".format(str(port_index), self.status_obj.server_sys_info["ports"][port_index]["speed"]), +# "N/A", +# "N/A", +# "N/A", +# "N/A", +# "N/A", +# "N/A")) + # control panel class ControlPanel(TrexStatusPanel): def __init__ (self, h, l, y, x, status_obj): -- cgit 1.2.3-korg From 36a9677c0abc038235e7bf706cb2b3dc9e720284 Mon Sep 17 00:00:00 2001 From: Dan Klein Date: Thu, 5 Nov 2015 07:35:15 +0200 Subject: added custom line parsing class and methods --- .../trex_control_plane/console/line_parsing.py | 47 +++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) (limited to 'scripts/automation/trex_control_plane/console') diff --git a/scripts/automation/trex_control_plane/console/line_parsing.py b/scripts/automation/trex_control_plane/console/line_parsing.py index 34776424..c1227a39 100644 --- a/scripts/automation/trex_control_plane/console/line_parsing.py +++ b/scripts/automation/trex_control_plane/console/line_parsing.py @@ -1,5 +1,50 @@ -__author__ = 'danklei' +import argparse +from collections import namedtuple +import sys +ArgumentPack = namedtuple('ArgumentPack', ['name_or_flags', 'options']) +# class ArgumentPack(namedtuple('ArgumentPack', ['name_or_flags', 'options'])): +# +# @property +# def name_or_flags(self): +# return self.name_or_flags +# +# @name_or_flags.setter +# def name_or_flags(self, val): +# print "bla" +# if not isinstance(val, list): +# self.name_or_flags = [val] +# else: +# self.name_or_flags = val + + +OPTIONS_DB = {'-m': ArgumentPack(['-m', '--multiplier'], + {'help': "Set multiplier for stream", 'dest':"mult"}), + 'file_path': ArgumentPack(['file'], + {'help': "File path to yaml file"})} + + +class CCmdArgParser(argparse.ArgumentParser): + + def __init__(self, *args, **kwargs): + super(CCmdArgParser, self).__init__(*args, **kwargs) + pass + + def error(self, message): + # self.print_usage(sys.stderr) + self.print_help() + return + +def gen_parser(op_name, *args): + parser = CCmdArgParser(prog=op_name, conflict_handler='resolve') + for param in args: + try: + parser.add_argument(*OPTIONS_DB[param].name_or_flags, + **OPTIONS_DB[param].options) + except KeyError, e: + cause = e.args[0] + raise KeyError("The attribute '{0}' is missing as a field of the {1} option.\n".format(cause, param)) + return parser if __name__ == "__main__": pass \ No newline at end of file -- cgit 1.2.3-korg 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 +- .../client/trex_stateless_client.py | 17 ++- .../trex_control_plane/console/trex_console.py | 1 + .../trex_control_plane/console/trex_status.py | 122 ++++++++++++++------- src/rpc-server/commands/trex_rpc_cmd_stream.cpp | 51 ++++++++- src/rpc-server/commands/trex_rpc_cmds.h | 5 +- src/rpc-server/trex_rpc_cmds_table.cpp | 1 + 7 files changed, 151 insertions(+), 49 deletions(-) (limited to 'scripts/automation/trex_control_plane/console') 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) diff --git a/scripts/automation/trex_control_plane/console/trex_console.py b/scripts/automation/trex_control_plane/console/trex_console.py index 8a5b29cc..52effa7e 100755 --- a/scripts/automation/trex_control_plane/console/trex_console.py +++ b/scripts/automation/trex_control_plane/console/trex_console.py @@ -251,6 +251,7 @@ class TRexConsole(cmd.Cmd): return port_list + def do_acquire(self, line, force=False): '''Acquire ports\n''' diff --git a/scripts/automation/trex_control_plane/console/trex_status.py b/scripts/automation/trex_control_plane/console/trex_status.py index c410e7c5..2b97d7d3 100644 --- a/scripts/automation/trex_control_plane/console/trex_status.py +++ b/scripts/automation/trex_control_plane/console/trex_status.py @@ -90,7 +90,7 @@ class ServerInfoPanel(TrexStatusPanel): self.getwin().addstr(9, 2, "{:<30} {:<30}".format("Ports Count:", self.status_obj.server_sys_info["port_count"])) - ports_owned = " ".join(str(x) for x in self.status_obj.owned_ports) + ports_owned = " ".join(str(x) for x in self.status_obj.owned_ports_list) if not ports_owned: ports_owned = "None" @@ -141,7 +141,7 @@ class PortsStatsPanel(TrexStatusPanel): self.clear() - owned_ports = self.status_obj.owned_ports + owned_ports = self.status_obj.owned_ports_list if not owned_ports: self.getwin().addstr(3, 2, "No Owned Ports - Please Acquire One Or More Ports") return @@ -225,7 +225,7 @@ class SinglePortPanel(TrexStatusPanel): self.clear() - if not self.port_id in self.status_obj.stateless_client.get_owned_ports(): + if not self.port_id in self.status_obj.owned_ports_list: self.getwin().addstr(y, 2, "Port {0} is not owned by you, please acquire the port for more info".format(self.port_id)) return @@ -239,16 +239,19 @@ class SinglePortPanel(TrexStatusPanel): y += 2 # streams - if 'streams' in self.status_obj.snapshot[self.port_id]: - for stream_id, stream in self.status_obj.snapshot[self.port_id]['streams'].iteritems(): + + if 'streams' in self.status_obj.owned_ports[str(self.port_id)]: + stream_info = self.status_obj.owned_ports[str(self.port_id)]['streams'] + + for stream_id, stream in sorted(stream_info.iteritems(), key=operator.itemgetter(0)): self.getwin().addstr(y, 2, "{:^15} {:^15} {:^15} {:^15} {:^15} {:^15} {:^15}".format( stream_id, - ("True" if stream['stream']['enabled'] else "False"), - stream['stream']['mode']['type'], - ("True" if stream['stream']['self_start'] else "False"), - stream['stream']['isg'], - (stream['stream']['next_stream_id'] if stream['stream']['next_stream_id'] != -1 else "None"), - ("{0} instr.".format(len(stream['stream']['vm'])) if stream['stream']['vm'] else "None"))) + ("True" if stream['enabled'] else "False"), + stream['mode']['type'], + ("True" if stream['self_start'] else "False"), + stream['isg'], + (stream['next_stream_id'] if stream['next_stream_id'] != -1 else "None"), + ("{0} instr.".format(len(stream['vm'])) if stream['vm'] else "None"))) y += 1 @@ -258,37 +261,36 @@ class SinglePortPanel(TrexStatusPanel): self.getwin().addstr(y, 2, "Traffic:", curses.A_UNDERLINE) y += 2 - self.status_obj.stats.query_sync() - port_stats = self.status_obj.stats.get_port_stats(self.port_id) - # table header - self.getwin().addstr(y, 2, "{:^15} {:^15} {:^15} {:^15} {:^15} {:^15} {:^15}".format( - "Port ID", "Tx [pps]", "Tx [bps]", "Tx [bytes]", "Rx [pps]", "Rx [bps]", "Rx [bytes]")) + # table header + self.getwin().addstr(y, 2, "{:^15} {:^30} {:^30} {:^30}".format( + "Port ID", "Tx Rate [bps/pps]", "Rx Rate [bps/pps]", "Total Bytes [tx/rx]")) + y += 2 - if port_stats: - self.getwin().addstr(y, 2, "{:^15} {:^15,} {:^15,} {:^15,} {:^15,} {:^15,} {:^15,}".format( - "{0} ({1})".format(str(self.port_id), self.status_obj.server_sys_info["ports"][self.port_id]["speed"]), - port_stats["tx_pps"], - port_stats["tx_bps"], - port_stats["total_tx_bytes"], - port_stats["rx_pps"], - port_stats["rx_bps"], - port_stats["total_rx_bytes"])) + port_stats = self.status_obj.stats.get_port_stats(self.port_id) + if port_stats: + self.getwin().addstr(y, 2, "{:^15} {:^30} {:^30} {:^30}".format( + "{0} ({1})".format(str(self.port_id), self.status_obj.server_sys_info["ports"][self.port_id]["speed"]), + "{0} / {1}".format(port_stats.get("m_total_tx_bps", format = True, suffix = "bps"), + port_stats.get("m_total_tx_pps", format = True, suffix = "pps")), + + "{0} / {1}".format(port_stats.get("m_total_rx_bps", format = True, suffix = "bps"), + port_stats.get("m_total_rx_pps", format = True, suffix = "pps")), + "{0} / {1}".format(port_stats.get_rel("obytes", format = True, suffix = "B"), + port_stats.get_rel("ibytes", format = True, suffix = "B")))) + else: - self.getwin().addstr(y, 2, "{:^15} {:^15} {:^15} {:^15} {:^15} {:^15} {:^15}".format( + self.getwin().addstr(y + (i * 4), 2, 2, "{:^15} {:^30} {:^30} {:^30}".format( "{0} ({1})".format(str(self.port_id), self.status_obj.server_sys_info["ports"][self.port_id]["speed"]), "N/A", "N/A", "N/A", - "N/A", - "N/A", "N/A")) - y += 2 ################### main objects ################# @@ -388,7 +390,7 @@ class TrexStatus(): self.stateless_client = stateless_client - self.log = TrexStatusLog() + self.log = TrexStatusLog() self.cmds = TrexStatusCommands(self) self.stats = stateless_client.get_stats_async() @@ -403,9 +405,35 @@ class TrexStatus(): if not rc: return - self.owned_ports = self.stateless_client.get_acquired_ports() + # list of owned ports + self.owned_ports_list = self.stateless_client.get_acquired_ports() + + # data per port + self.owned_ports = {} + + for port_id in self.owned_ports_list: + self.owned_ports[str(port_id)] = {} + self.owned_ports[str(port_id)]['streams'] = {} + + rc, stream_list = self.stateless_client.get_all_streams(port_id) + if not rc: + raise Exception("unable to get streams") + self.owned_ports[str(port_id)] = stream_list + + try: + curses.curs_set(0) + except: + pass + + curses.use_default_colors() + self.stdscr.nodelay(1) + curses.nonl() + curses.noecho() + + self.generate_layout() + def generate_layout (self): self.max_y = self.stdscr.getmaxyx()[0] @@ -441,17 +469,22 @@ class TrexStatus(): # main run entry point def run (self): - try: - curses.curs_set(0) - except: - pass - curses.use_default_colors() - self.stdscr.nodelay(1) - curses.nonl() - curses.noecho() + # list of owned ports + self.owned_ports_list = self.stateless_client.get_acquired_ports() - self.generate_layout() + # data per port + self.owned_ports = {} + + for port_id in self.owned_ports_list: + self.owned_ports[str(port_id)] = {} + self.owned_ports[str(port_id)]['streams'] = {} + + rc, stream_list = self.stateless_client.get_all_streams(port_id) + if not rc: + raise Exception("unable to get streams") + + self.owned_ports[str(port_id)] = stream_list self.update_active = True while (True): @@ -473,8 +506,15 @@ class TrexStatus(): sleep(0.01) +# global container +trex_status = None + def show_trex_status_internal (stdscr, stateless_client): - trex_status = TrexStatus(stdscr, stateless_client) + global trex_status + + if trex_status == None: + trex_status = TrexStatus(stdscr, stateless_client) + trex_status.run() def show_trex_status (stateless_client): diff --git a/src/rpc-server/commands/trex_rpc_cmd_stream.cpp b/src/rpc-server/commands/trex_rpc_cmd_stream.cpp index 9854cad7..d7138f7b 100644 --- a/src/rpc-server/commands/trex_rpc_cmd_stream.cpp +++ b/src/rpc-server/commands/trex_rpc_cmd_stream.cpp @@ -397,8 +397,8 @@ TrexRpcCmdGetStreamList::_run(const Json::Value ¶ms, Json::Value &result) { **************************/ trex_rpc_cmd_rc_e TrexRpcCmdGetStream::_run(const Json::Value ¶ms, Json::Value &result) { - uint8_t port_id = parse_byte(params, "port_id", result); - + uint8_t port_id = parse_byte(params, "port_id", result); + bool get_pkt = parse_bool(params, "get_pkt", result); uint32_t stream_id = parse_int(params, "stream_id", result); if (port_id >= get_stateless_obj()->get_port_count()) { @@ -418,7 +418,12 @@ TrexRpcCmdGetStream::_run(const Json::Value ¶ms, Json::Value &result) { } /* return the stored stream json (instead of decoding it all over again) */ - result["result"]["stream"] = stream->get_stream_json(); + Json::Value j = stream->get_stream_json(); + if (!get_pkt) { + j.removeMember("packet"); + } + + result["result"]["stream"] = j; return (TREX_RPC_CMD_OK); @@ -488,3 +493,43 @@ TrexRpcCmdStopTraffic::_run(const Json::Value ¶ms, Json::Value &result) { return (TREX_RPC_CMD_OK); } +/*************************** + * get all streams + * + **************************/ +trex_rpc_cmd_rc_e +TrexRpcCmdGetAllStreams::_run(const Json::Value ¶ms, Json::Value &result) { + uint8_t port_id = parse_byte(params, "port_id", result); + bool get_pkt = parse_bool(params, "get_pkt", result); + + if (port_id >= get_stateless_obj()->get_port_count()) { + std::stringstream ss; + ss << "invalid port id - should be between 0 and " << (int)get_stateless_obj()->get_port_count() - 1; + generate_execute_err(result, ss.str()); + } + + TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); + + std::vector streams; + port->get_stream_table()->get_object_list(streams); + + Json::Value streams_json = Json::objectValue; + for (auto stream : streams) { + + Json::Value j = stream->get_stream_json(); + + /* should we include the packet as well ? */ + if (!get_pkt) { + j.removeMember("packet"); + } + + std::stringstream ss; + ss << stream->m_stream_id; + + streams_json[ss.str()] = j; + } + + result["result"]["streams"] = streams_json; + + return (TREX_RPC_CMD_OK); +} diff --git a/src/rpc-server/commands/trex_rpc_cmds.h b/src/rpc-server/commands/trex_rpc_cmds.h index 91c29548..a604d9a1 100644 --- a/src/rpc-server/commands/trex_rpc_cmds.h +++ b/src/rpc-server/commands/trex_rpc_cmds.h @@ -99,8 +99,11 @@ void parse_vm_instr_write_flow_var(const Json::Value &inst, TrexStream *stream, TREX_RPC_CMD_DEFINE(TrexRpcCmdGetStreamList, "get_stream_list", 1, true); +TREX_RPC_CMD_DEFINE(TrexRpcCmdGetAllStreams, "get_all_streams", 2, true); + +TREX_RPC_CMD_DEFINE(TrexRpcCmdGetStream, "get_stream", 3, true); + -TREX_RPC_CMD_DEFINE(TrexRpcCmdGetStream, "get_stream", 2, true); TREX_RPC_CMD_DEFINE(TrexRpcCmdStartTraffic, "start_traffic", 2, true); TREX_RPC_CMD_DEFINE(TrexRpcCmdStopTraffic, "stop_traffic", 1, true); diff --git a/src/rpc-server/trex_rpc_cmds_table.cpp b/src/rpc-server/trex_rpc_cmds_table.cpp index 46281aff..e3bd7848 100644 --- a/src/rpc-server/trex_rpc_cmds_table.cpp +++ b/src/rpc-server/trex_rpc_cmds_table.cpp @@ -50,6 +50,7 @@ TrexRpcCommandsTable::TrexRpcCommandsTable() { register_command(new TrexRpcCmdRemoveAllStreams()); register_command(new TrexRpcCmdGetStreamList()); register_command(new TrexRpcCmdGetStream()); + register_command(new TrexRpcCmdGetAllStreams()); register_command(new TrexRpcCmdStartTraffic()); register_command(new TrexRpcCmdStopTraffic()); } -- cgit 1.2.3-korg 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 +++++++++- .../trex_control_plane/console/line_parsing.py | 50 ---------- .../trex_control_plane/console/parsing_opts.py | 103 +++++++++++++++++++ .../trex_control_plane/console/trex_console.py | 111 +++++++++++++-------- 4 files changed, 222 insertions(+), 93 deletions(-) delete mode 100644 scripts/automation/trex_control_plane/console/line_parsing.py create mode 100755 scripts/automation/trex_control_plane/console/parsing_opts.py (limited to 'scripts/automation/trex_control_plane/console') 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), diff --git a/scripts/automation/trex_control_plane/console/line_parsing.py b/scripts/automation/trex_control_plane/console/line_parsing.py deleted file mode 100644 index c1227a39..00000000 --- a/scripts/automation/trex_control_plane/console/line_parsing.py +++ /dev/null @@ -1,50 +0,0 @@ -import argparse -from collections import namedtuple -import sys - -ArgumentPack = namedtuple('ArgumentPack', ['name_or_flags', 'options']) -# class ArgumentPack(namedtuple('ArgumentPack', ['name_or_flags', 'options'])): -# -# @property -# def name_or_flags(self): -# return self.name_or_flags -# -# @name_or_flags.setter -# def name_or_flags(self, val): -# print "bla" -# if not isinstance(val, list): -# self.name_or_flags = [val] -# else: -# self.name_or_flags = val - - -OPTIONS_DB = {'-m': ArgumentPack(['-m', '--multiplier'], - {'help': "Set multiplier for stream", 'dest':"mult"}), - 'file_path': ArgumentPack(['file'], - {'help': "File path to yaml file"})} - - -class CCmdArgParser(argparse.ArgumentParser): - - def __init__(self, *args, **kwargs): - super(CCmdArgParser, self).__init__(*args, **kwargs) - pass - - def error(self, message): - # self.print_usage(sys.stderr) - self.print_help() - return - -def gen_parser(op_name, *args): - parser = CCmdArgParser(prog=op_name, conflict_handler='resolve') - for param in args: - try: - parser.add_argument(*OPTIONS_DB[param].name_or_flags, - **OPTIONS_DB[param].options) - except KeyError, e: - cause = e.args[0] - raise KeyError("The attribute '{0}' is missing as a field of the {1} option.\n".format(cause, param)) - return parser - -if __name__ == "__main__": - pass \ No newline at end of file diff --git a/scripts/automation/trex_control_plane/console/parsing_opts.py b/scripts/automation/trex_control_plane/console/parsing_opts.py new file mode 100755 index 00000000..c94a7461 --- /dev/null +++ b/scripts/automation/trex_control_plane/console/parsing_opts.py @@ -0,0 +1,103 @@ +import argparse +from collections import namedtuple +import sys + +ArgumentPack = namedtuple('ArgumentPack', ['name_or_flags', 'options']) +ArgumentGroup = namedtuple('ArgumentGroup', ['type', 'args', 'options']) + + +# list of available parsing options +MULTIPLIER = 1 +PORT_LIST = 2 +ALL_PORTS = 3 +PORT_LIST_WITH_ALL = 4 +FILE_PATH = 5 +FILE_FROM_DB = 6 +STREAM_FROM_PATH_OR_FILE = 7 + +# list of ArgumentGroup types +MUTEX = 1 + + + + +OPTIONS_DB = {MULTIPLIER: ArgumentPack(['-m', '--multiplier'], + {'help': "Set multiplier for stream", 'dest': "mult", 'type': float}), + PORT_LIST: ArgumentPack(['--port'], + {"nargs": '+', + # "action": "store_" + 'help': "A list of ports on which to apply the command", + 'default': []}), + ALL_PORTS: ArgumentPack(['-a'], + {"action": "store_true", + "dest": "all", + 'help': "Set this flag to apply the command on all available ports"}), + + FILE_PATH: ArgumentPack(['-f'], + {'help': "File path to YAML file that describes a stream pack"}), + FILE_FROM_DB: ArgumentPack(['--db'], + {'help': "A stream pack which already loaded into console cache."}), + # advanced options + PORT_LIST_WITH_ALL: ArgumentGroup(MUTEX, [PORT_LIST, + ALL_PORTS], + {'required': True}), + STREAM_FROM_PATH_OR_FILE: ArgumentGroup(MUTEX, [FILE_PATH, + FILE_FROM_DB], + {'required': True}) + } + + +class CCmdArgParser(argparse.ArgumentParser): + + def __init__(self, *args, **kwargs): + super(CCmdArgParser, self).__init__(*args, **kwargs) + pass + + # def error(self, message): + # try: + # super(CCmdArgParser, self).error(message) # this will trigger system exit! + # except SystemExit: + # return -1 + # + # # self.print_usage(sys.stderr) + # # print ('%s: error: %s\n') % (self.prog, message) + # # self.print_help() + # return + + def exit(self, status=0, message=None): + try: + super(CCmdArgParser, self).exit(status, message) # this will trigger system exit! + except SystemExit: + return -1 + return + +def gen_parser(op_name, description, *args): + parser = CCmdArgParser(prog=op_name, conflict_handler='resolve', + # add_help=False, + description=description) + for param in args: + try: + argument = OPTIONS_DB[param] + if isinstance(argument, ArgumentGroup): + if argument.type == MUTEX: + # handle as mutually exclusive group + group = parser.add_mutually_exclusive_group(**argument.options) + for sub_argument in argument.args: + group.add_argument(*OPTIONS_DB[sub_argument].name_or_flags, + **OPTIONS_DB[sub_argument].options) + else: + # ignore invalid objects + continue + elif isinstance(argument, ArgumentPack): + parser.add_argument(*argument.name_or_flags, + **argument.options) + else: + # ignore invalid objects + continue + except KeyError as e: + cause = e.args[0] + raise KeyError("The attribute '{0}' is missing as a field of the {1} option.\n".format(cause, param)) + return parser + +if __name__ == "__main__": + pass \ No newline at end of file diff --git a/scripts/automation/trex_control_plane/console/trex_console.py b/scripts/automation/trex_control_plane/console/trex_console.py index 8a5b29cc..33b4e164 100755 --- a/scripts/automation/trex_control_plane/console/trex_console.py +++ b/scripts/automation/trex_control_plane/console/trex_console.py @@ -32,7 +32,7 @@ from common.trex_streams import * from client.trex_stateless_client import CTRexStatelessClient from common.text_opts import * from client_utils.general_utils import user_input, get_current_user - +import parsing_opts import trex_status from collections import namedtuple @@ -234,6 +234,9 @@ class TRexConsole(cmd.Cmd): def extract_port_ids_from_line(self, line): return {int(x) for x in line.split()} + def extract_port_ids_from_list(self, port_list): + return {int(x) for x in port_list} + def parse_ports_from_line (self, line): port_list = set() if line: @@ -449,42 +452,42 @@ class TRexConsole(cmd.Cmd): def default(self, line): print "'{0}' is an unrecognized command. type 'help' or '?' for a list\n".format(line) - def do_help (self, line): - '''Shows This Help Screen\n''' - if line: - try: - func = getattr(self, 'help_' + line) - except AttributeError: - try: - doc = getattr(self, 'do_' + line).__doc__ - if doc: - self.stdout.write("%s\n"%str(doc)) - return - except AttributeError: - pass - self.stdout.write("%s\n"%str(self.nohelp % (line,))) - return - func() - return - - print "\nSupported Console Commands:" - print "----------------------------\n" - - cmds = [x[3:] for x in self.get_names() if x.startswith("do_")] - for cmd in cmds: - if cmd == "EOF": - continue - - try: - doc = getattr(self, 'do_' + cmd).__doc__ - if doc: - help = str(doc) - else: - help = "*** Undocumented Function ***\n" - except AttributeError: - help = "*** Undocumented Function ***\n" - - print "{:<30} {:<30}".format(cmd + " - ", help) + # def do_help (self, line): + # '''Shows This Help Screen\n''' + # if line: + # try: + # func = getattr(self, 'help_' + line) + # except AttributeError: + # try: + # doc = getattr(self, 'do_' + line).__doc__ + # if doc: + # self.stdout.write("%s\n"%str(doc)) + # return + # except AttributeError: + # pass + # self.stdout.write("%s\n"%str(self.nohelp % (line,))) + # return + # func() + # return + # + # print "\nSupported Console Commands:" + # print "----------------------------\n" + # + # cmds = [x[3:] for x in self.get_names() if x.startswith("do_")] + # for cmd in cmds: + # if cmd == "EOF": + # continue + # + # try: + # doc = getattr(self, 'do_' + cmd).__doc__ + # if doc: + # help = str(doc) + # else: + # help = "*** Undocumented Function ***\n" + # except AttributeError: + # help = "*** Undocumented Function ***\n" + # + # print "{:<30} {:<30}".format(cmd + " - ", help) def do_stream_db_add(self, line): '''Loads a YAML stream list serialization into user console \n''' @@ -594,7 +597,7 @@ class TRexConsole(cmd.Cmd): print "Provided stream list name '{0}' doesn't exists.".format(stream_pack_name) print format_text("[FAILED]\n", 'red', 'bold') return - if args[0] == "all": + if args[1] == "all": ask = ConfirmMenu('Are you sure you want to release all acquired ports? ') rc = ask.show() if rc == False: @@ -698,6 +701,33 @@ class TRexConsole(cmd.Cmd): def do_start_traffic(self, line): '''Start pre-submitted traffic in specified ports on TRex\n''' # make sure that the user wants to acquire all + # parser = parsing_opts.gen_parser("start_traffic", self.do_start_traffic.__doc__, + # parsing_opts.PORT_LIST_WITH_ALL, parsing_opts.MULTIPLIER) + # opts = parser.parse_args(line.split()) + # + # print opts + # return + # # return + # # if not opts.port_list: + # # print magenta("Please provide a list of ports separated by spaces, " + # # "or specify 'all' to start traffic on all acquired ports") + # # return + # + # if "all" in opts.port_list: + # ask = ConfirmMenu('Are you sure you want to start traffic at all acquired ports? ') + # rc = ask.show() + # if rc == False: + # print yellow("[ABORTED]\n") + # return + # else: + # port_list = self.stateless_client.get_acquired_ports() + # else: + # try: + # port_list = self.extract_port_ids_from_list(opts.port_list) + # except ValueError as e: + # print magenta(e) + # return + args = line.split() if len(args) < 1: print magenta("Please provide a list of ports separated by spaces, " @@ -715,7 +745,7 @@ class TRexConsole(cmd.Cmd): port_list = self.extract_port_ids_from_line(line) try: - res_ok, log = self.stateless_client.start_traffic(port_list) + res_ok, log = self.stateless_client.start_traffic(1.0, port_id=port_list) self.prompt_response(log) if not res_ok: print format_text("[FAILED]\n", 'red', 'bold') @@ -728,6 +758,9 @@ class TRexConsole(cmd.Cmd): def complete_start_traffic(self, text, line, begidx, endidx): return self.port_auto_complete(text, line, begidx, endidx) + def help_start_traffic(self): + self.do_start_traffic("-h") + def do_stop_traffic(self, line): '''Stop active traffic in specified ports on TRex\n''' # make sure that the user wants to acquire all -- cgit 1.2.3-korg From 9c32c36b6006d2a81e1a5658a1fb1616eff650f3 Mon Sep 17 00:00:00 2001 From: imarom Date: Mon, 9 Nov 2015 17:10:24 +0200 Subject: moved logic to the port class also implemented the state machine for CP port --- .../trex_control_plane/console/trex_console.py | 3 + src/gtest/rpc_test.cpp | 101 +++++++++++++ src/rpc-server/commands/trex_rpc_cmd_general.cpp | 28 ++-- src/rpc-server/commands/trex_rpc_cmd_stream.cpp | 160 ++++++++++++++++----- src/rpc-server/commands/trex_rpc_cmds.h | 8 +- src/rpc-server/trex_rpc_cmds_table.cpp | 3 + src/stateless/cp/trex_stateless_port.cpp | 143 +++++++++++++----- src/stateless/cp/trex_stateless_port.h | 128 +++++++++++++---- 8 files changed, 468 insertions(+), 106 deletions(-) (limited to 'scripts/automation/trex_control_plane/console') diff --git a/scripts/automation/trex_control_plane/console/trex_console.py b/scripts/automation/trex_control_plane/console/trex_console.py index a2c738ab..68050fc0 100755 --- a/scripts/automation/trex_control_plane/console/trex_console.py +++ b/scripts/automation/trex_control_plane/console/trex_console.py @@ -778,6 +778,9 @@ class TRexConsole(cmd.Cmd): return else: port_list = self.stateless_client.get_active_ports() + if not port_list: + print magenta("no active ports - operation aborted\n") + return else: port_list = self.extract_port_ids_from_line(line) diff --git a/src/gtest/rpc_test.cpp b/src/gtest/rpc_test.cpp index 6b8e3eff..34bb02a8 100644 --- a/src/gtest/rpc_test.cpp +++ b/src/gtest/rpc_test.cpp @@ -478,6 +478,7 @@ TEST_F(RpcTestOwned, add_remove_stream) { create_request(request, "get_stream", 1, 1); request["params"]["stream_id"] = 5; + request["params"]["get_pkt"] = true; send_request(request, response); @@ -501,6 +502,7 @@ TEST_F(RpcTestOwned, add_remove_stream) { create_request(request, "get_stream", 1, 1); request["params"]["stream_id"] = 5; + request["params"]["get_pkt"] = true; send_request(request, response); @@ -607,17 +609,21 @@ TEST_F(RpcTestOwned, start_stop_traffic) { /* start port 1 */ create_request(request, "start_traffic", 1, 1); + request["params"]["mul"] = 1.0; send_request(request, response); + EXPECT_EQ(response["result"], "ACK"); /* start port 3 */ create_request(request, "start_traffic", 1, 3); + request["params"]["mul"] = 1.0; send_request(request, response); EXPECT_EQ(response["result"], "ACK"); /* start not configured port */ create_request(request, "start_traffic", 1, 2); + request["params"]["mul"] = 1.0; send_request(request, response); EXPECT_EQ(response["error"]["code"], -32000); @@ -633,11 +639,13 @@ TEST_F(RpcTestOwned, start_stop_traffic) { /* start 1 again */ create_request(request, "start_traffic", 1, 1); + request["params"]["mul"] = 1.0; send_request(request, response); EXPECT_EQ(response["result"], "ACK"); /* start 1 twice (error) */ create_request(request, "start_traffic", 1, 1); + request["params"]["mul"] = 1.0; send_request(request, response); EXPECT_EQ(response["error"]["code"], -32000); @@ -657,3 +665,96 @@ TEST_F(RpcTestOwned, start_stop_traffic) { EXPECT_EQ(response["result"], "ACK"); } + + +TEST_F(RpcTestOwned, states_check) { + Json::Value request; + Json::Value response; + + /* add stream #1 */ + create_request(request, "add_stream", 1, 1); + request["params"]["stream_id"] = 5; + + Json::Value stream; + create_simple_stream(stream); + + request["params"]["stream"] = stream; + + send_request(request, response); + EXPECT_EQ(response["result"], "ACK"); + + /* start traffic */ + create_request(request, "start_traffic", 1, 1); + request["params"]["mul"] = 1.0; + send_request(request, response); + EXPECT_EQ(response["result"], "ACK"); + + /* now we cannot add streams */ + create_request(request, "add_stream", 1, 1); + request["params"]["stream_id"] = 15; + + create_simple_stream(stream); + + request["params"]["stream"] = stream; + + send_request(request, response); + EXPECT_EQ(response["error"]["code"], -32000); + + /* we cannot remove streams */ + create_request(request, "remove_stream", 1, 1); + request["params"]["stream_id"] = 15; + send_request(request, response); + EXPECT_EQ(response["error"]["code"], -32000); + + /* cannot start again */ + create_request(request, "start_traffic", 1, 1); + request["params"]["mul"] = 1.0; + send_request(request, response); + EXPECT_EQ(response["error"]["code"], -32000); + + /* we can stop and add stream / remove */ + + create_request(request, "stop_traffic", 1, 1); + send_request(request, response); + EXPECT_EQ(response["result"], "ACK"); + + create_request(request, "add_stream", 1, 1); + request["params"]["stream_id"] = 328; + + create_simple_stream(stream); + + request["params"]["stream"] = stream; + + send_request(request, response); + EXPECT_EQ(response["result"], "ACK"); + + + create_request(request, "remove_stream", 1, 1); + request["params"]["stream_id"] = 15; + send_request(request, response); + EXPECT_EQ(response["error"]["code"], -32000); + + /* we cannot pause now */ + create_request(request, "pause_traffic", 1, 1); + send_request(request, response); + EXPECT_EQ(response["error"]["code"], -32000); + + + /* start */ + create_request(request, "start_traffic", 1, 1); + request["params"]["mul"] = 1.0; + send_request(request, response); + EXPECT_EQ(response["result"], "ACK"); + + /* now can pause */ + create_request(request, "pause_traffic", 1, 1); + send_request(request, response); + EXPECT_EQ(response["result"], "ACK"); + + /* also we can resume*/ + create_request(request, "resume_traffic", 1, 1); + send_request(request, response); + EXPECT_EQ(response["result"], "ACK"); + + +} diff --git a/src/rpc-server/commands/trex_rpc_cmd_general.cpp b/src/rpc-server/commands/trex_rpc_cmd_general.cpp index b40e996f..b6d06cfc 100644 --- a/src/rpc-server/commands/trex_rpc_cmd_general.cpp +++ b/src/rpc-server/commands/trex_rpc_cmd_general.cpp @@ -222,12 +222,12 @@ TrexRpcCmdAcquire::_run(const Json::Value ¶ms, Json::Value &result) { /* if not free and not you and not force - fail */ TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); - if ( (!port->is_free_to_aquire()) && (port->get_owner() != new_owner) && (!force)) { - generate_execute_err(result, "port is already taken by '" + port->get_owner() + "'"); + try { + port->acquire(new_owner, force); + } catch (const TrexRpcException &ex) { + generate_execute_err(result, ex.what()); } - port->set_owner(new_owner); - result["result"] = port->get_owner_handler(); return (TREX_RPC_CMD_OK); @@ -244,12 +244,12 @@ TrexRpcCmdRelease::_run(const Json::Value ¶ms, Json::Value &result) { TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); - if (port->get_state() == TrexStatelessPort::PORT_STATE_TRANSMITTING) { - generate_execute_err(result, "cannot release a port during transmission"); + try { + port->release(); + } catch (const TrexRpcException &ex) { + generate_execute_err(result, ex.what()); } - port->clear_owner(); - result["result"] = "ACK"; return (TREX_RPC_CMD_OK); @@ -266,13 +266,13 @@ TrexRpcCmdGetPortStats::_run(const Json::Value ¶ms, Json::Value &result) { TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); - if (port->get_state() == TrexStatelessPort::PORT_STATE_DOWN) { - generate_execute_err(result, "cannot get stats - port is down"); - } - result["result"]["status"] = port->get_state_as_string(); - port->encode_stats(result["result"]); + try { + port->encode_stats(result["result"]); + } catch (const TrexRpcException &ex) { + generate_execute_err(result, ex.what()); + } return (TREX_RPC_CMD_OK); } @@ -303,7 +303,7 @@ TrexRpcCmdSyncUser::_run(const Json::Value ¶ms, Json::Value &result) { owned_port["streams"] = Json::arrayValue; std::vector streams; - port->get_stream_table()->get_object_list(streams); + port->get_object_list(streams); for (auto stream : streams) { owned_port["streams"].append(stream->get_stream_json()); diff --git a/src/rpc-server/commands/trex_rpc_cmd_stream.cpp b/src/rpc-server/commands/trex_rpc_cmd_stream.cpp index 4fa0956d..fffc800a 100644 --- a/src/rpc-server/commands/trex_rpc_cmd_stream.cpp +++ b/src/rpc-server/commands/trex_rpc_cmd_stream.cpp @@ -115,7 +115,12 @@ TrexRpcCmdAddStream::_run(const Json::Value ¶ms, Json::Value &result) { validate_stream(stream, result); TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(stream->m_port_id); - port->get_stream_table()->add_stream(stream); + + try { + port->add_stream(stream); + } catch (const TrexRpcException &ex) { + generate_execute_err(result, ex.what()); + } result["result"] = "ACK"; @@ -293,7 +298,7 @@ TrexRpcCmdAddStream::validate_stream(const TrexStream *stream, Json::Value &resu TrexStatelessPort * port = get_stateless_obj()->get_port_by_id(stream->m_port_id); /* does such a stream exists ? */ - if (port->get_stream_table()->get_stream_by_id(stream->m_stream_id)) { + if (port->get_stream_by_id(stream->m_stream_id)) { std::stringstream ss; ss << "stream " << stream->m_stream_id << " already exists"; delete stream; @@ -319,7 +324,7 @@ TrexRpcCmdRemoveStream::_run(const Json::Value ¶ms, Json::Value &result) { } TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); - TrexStream *stream = port->get_stream_table()->get_stream_by_id(stream_id); + TrexStream *stream = port->get_stream_by_id(stream_id); if (!stream) { std::stringstream ss; @@ -327,7 +332,12 @@ TrexRpcCmdRemoveStream::_run(const Json::Value ¶ms, Json::Value &result) { generate_execute_err(result, ss.str()); } - port->get_stream_table()->remove_stream(stream); + try { + port->remove_stream(stream); + } catch (const TrexRpcException &ex) { + generate_execute_err(result, ex.what()); + } + delete stream; result["result"] = "ACK"; @@ -350,12 +360,18 @@ TrexRpcCmdRemoveAllStreams::_run(const Json::Value ¶ms, Json::Value &result) generate_execute_err(result, ss.str()); } - TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); - port->get_stream_table()->remove_and_delete_all_streams(); + TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); - result["result"] = "ACK"; + try { + port->remove_and_delete_all_streams(); + } catch (const TrexRpcException &ex) { + generate_execute_err(result, ex.what()); + } - return (TREX_RPC_CMD_OK); + + result["result"] = "ACK"; + + return (TREX_RPC_CMD_OK); } /*************************** @@ -377,7 +393,7 @@ TrexRpcCmdGetStreamList::_run(const Json::Value ¶ms, Json::Value &result) { TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); - port->get_stream_table()->get_id_list(stream_list); + port->get_id_list(stream_list); Json::Value json_list = Json::arrayValue; @@ -409,7 +425,7 @@ TrexRpcCmdGetStream::_run(const Json::Value ¶ms, Json::Value &result) { TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); - TrexStream *stream = port->get_stream_table()->get_stream_by_id(stream_id); + TrexStream *stream = port->get_stream_by_id(stream_id); if (!stream) { std::stringstream ss; @@ -447,32 +463,19 @@ TrexRpcCmdStartTraffic::_run(const Json::Value ¶ms, Json::Value &result) { TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); - TrexStatelessPort::rc_e rc = port->start_traffic(mul); - - if (rc == TrexStatelessPort::RC_OK) { - result["result"] = "ACK"; - } else { - std::stringstream ss; - switch (rc) { - case TrexStatelessPort::RC_ERR_BAD_STATE_FOR_OP: - ss << "bad state for operations: port is either transmitting traffic or down"; - break; - case TrexStatelessPort::RC_ERR_NO_STREAMS: - ss << "no active streams on that port"; - break; - default: - ss << "failed to start traffic"; - break; - } - - generate_execute_err(result, ss.str()); + try { + port->start_traffic(mul); + } catch (const TrexRpcException &ex) { + generate_execute_err(result, ex.what()); } - return (TREX_RPC_CMD_OK); + result["result"] = "ACK"; + + return (TREX_RPC_CMD_OK); } /*************************** - * start traffic on port + * stop traffic on port * **************************/ trex_rpc_cmd_rc_e @@ -487,7 +490,12 @@ TrexRpcCmdStopTraffic::_run(const Json::Value ¶ms, Json::Value &result) { TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); - port->stop_traffic(); + try { + port->stop_traffic(); + } catch (const TrexRpcException &ex) { + generate_execute_err(result, ex.what()); + } + result["result"] = "ACK"; return (TREX_RPC_CMD_OK); @@ -511,7 +519,7 @@ TrexRpcCmdGetAllStreams::_run(const Json::Value ¶ms, Json::Value &result) { TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); std::vector streams; - port->get_stream_table()->get_object_list(streams); + port->get_object_list(streams); Json::Value streams_json = Json::objectValue; for (auto stream : streams) { @@ -533,3 +541,89 @@ TrexRpcCmdGetAllStreams::_run(const Json::Value ¶ms, Json::Value &result) { return (TREX_RPC_CMD_OK); } + +/*************************** + * pause traffic + * + **************************/ +trex_rpc_cmd_rc_e +TrexRpcCmdPauseTraffic::_run(const Json::Value ¶ms, Json::Value &result) { + + uint8_t port_id = parse_byte(params, "port_id", result); + + if (port_id >= get_stateless_obj()->get_port_count()) { + std::stringstream ss; + ss << "invalid port id - should be between 0 and " << (int)get_stateless_obj()->get_port_count() - 1; + generate_execute_err(result, ss.str()); + } + + TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); + + try { + port->pause_traffic(); + } catch (const TrexRpcException &ex) { + generate_execute_err(result, ex.what()); + } + + result["result"] = "ACK"; + + return (TREX_RPC_CMD_OK); +} + +/*************************** + * resume traffic + * + **************************/ +trex_rpc_cmd_rc_e +TrexRpcCmdResumeTraffic::_run(const Json::Value ¶ms, Json::Value &result) { + + uint8_t port_id = parse_byte(params, "port_id", result); + + if (port_id >= get_stateless_obj()->get_port_count()) { + std::stringstream ss; + ss << "invalid port id - should be between 0 and " << (int)get_stateless_obj()->get_port_count() - 1; + generate_execute_err(result, ss.str()); + } + + TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); + + try { + port->resume_traffic(); + } catch (const TrexRpcException &ex) { + generate_execute_err(result, ex.what()); + } + + result["result"] = "ACK"; + + return (TREX_RPC_CMD_OK); +} + +/*************************** + * update traffic + * + **************************/ +trex_rpc_cmd_rc_e +TrexRpcCmdUpdateTraffic::_run(const Json::Value ¶ms, Json::Value &result) { + + uint8_t port_id = parse_byte(params, "port_id", result); + double mul = parse_double(params, "mul", result); + + if (port_id >= get_stateless_obj()->get_port_count()) { + std::stringstream ss; + ss << "invalid port id - should be between 0 and " << (int)get_stateless_obj()->get_port_count() - 1; + generate_execute_err(result, ss.str()); + } + + TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); + + try { + port->update_traffic(mul); + } catch (const TrexRpcException &ex) { + generate_execute_err(result, ex.what()); + } + + result["result"] = "ACK"; + + return (TREX_RPC_CMD_OK); +} + diff --git a/src/rpc-server/commands/trex_rpc_cmds.h b/src/rpc-server/commands/trex_rpc_cmds.h index a604d9a1..8b173cb9 100644 --- a/src/rpc-server/commands/trex_rpc_cmds.h +++ b/src/rpc-server/commands/trex_rpc_cmds.h @@ -105,8 +105,12 @@ TREX_RPC_CMD_DEFINE(TrexRpcCmdGetStream, "get_stream", 3, true); -TREX_RPC_CMD_DEFINE(TrexRpcCmdStartTraffic, "start_traffic", 2, true); -TREX_RPC_CMD_DEFINE(TrexRpcCmdStopTraffic, "stop_traffic", 1, true); +TREX_RPC_CMD_DEFINE(TrexRpcCmdStartTraffic, "start_traffic", 2, true); +TREX_RPC_CMD_DEFINE(TrexRpcCmdStopTraffic, "stop_traffic", 1, true); +TREX_RPC_CMD_DEFINE(TrexRpcCmdPauseTraffic, "pause_traffic", 1, true); +TREX_RPC_CMD_DEFINE(TrexRpcCmdResumeTraffic, "resume_traffic", 1, true); + +TREX_RPC_CMD_DEFINE(TrexRpcCmdUpdateTraffic, "update_traffic", 1, true); TREX_RPC_CMD_DEFINE(TrexRpcCmdSyncUser, "sync_user", 2, false); diff --git a/src/rpc-server/trex_rpc_cmds_table.cpp b/src/rpc-server/trex_rpc_cmds_table.cpp index e3bd7848..f38b4df4 100644 --- a/src/rpc-server/trex_rpc_cmds_table.cpp +++ b/src/rpc-server/trex_rpc_cmds_table.cpp @@ -51,8 +51,11 @@ TrexRpcCommandsTable::TrexRpcCommandsTable() { register_command(new TrexRpcCmdGetStreamList()); register_command(new TrexRpcCmdGetStream()); register_command(new TrexRpcCmdGetAllStreams()); + register_command(new TrexRpcCmdStartTraffic()); register_command(new TrexRpcCmdStopTraffic()); + register_command(new TrexRpcCmdPauseTraffic()); + register_command(new TrexRpcCmdResumeTraffic()); } TrexRpcCommandsTable::~TrexRpcCommandsTable() { diff --git a/src/stateless/cp/trex_stateless_port.cpp b/src/stateless/cp/trex_stateless_port.cpp index cb6fcc0e..b9206775 100644 --- a/src/stateless/cp/trex_stateless_port.cpp +++ b/src/stateless/cp/trex_stateless_port.cpp @@ -53,29 +53,47 @@ using namespace std; * **************************/ TrexStatelessPort::TrexStatelessPort(uint8_t port_id) : m_port_id(port_id) { - m_port_state = PORT_STATE_UP_IDLE; + m_port_state = PORT_STATE_IDLE; clear_owner(); } +/** + * acquire the port + * + * @author imarom (09-Nov-15) + * + * @param user + * @param force + */ +void +TrexStatelessPort::acquire(const std::string &user, bool force) { + if ( (!is_free_to_aquire()) && (get_owner() != user) && (!force)) { + throw TrexRpcException("port is already taken by '" + get_owner() + "'"); + } + + set_owner(user); +} + +void +TrexStatelessPort::release(void) { + verify_state( ~(PORT_STATE_TX | PORT_STATE_PAUSE) ); + clear_owner(); +} + /** * starts the traffic on the port * */ -TrexStatelessPort::rc_e +void TrexStatelessPort::start_traffic(double mul) { - if (m_port_state != PORT_STATE_UP_IDLE) { - return (RC_ERR_BAD_STATE_FOR_OP); - } - - if (get_stream_table()->size() == 0) { - return (RC_ERR_NO_STREAMS); - } + /* command allowed only on state stream */ + verify_state(PORT_STATE_STREAMS); /* fetch all the streams from the table */ vector streams; - get_stream_table()->get_object_list(streams); + get_object_list(streams); /* compiler it */ TrexStreamsCompiler compiler; @@ -83,7 +101,7 @@ TrexStatelessPort::start_traffic(double mul) { bool rc = compiler.compile(streams, *compiled_obj); if (!rc) { - return (RC_ERR_FAILED_TO_COMPILE_STREAMS); + throw TrexRpcException("Failed to compile streams"); } /* generate a message to all the relevant DP cores to start transmitting */ @@ -91,51 +109,94 @@ TrexStatelessPort::start_traffic(double mul) { send_message_to_dp(start_msg); - /* move the state to transmiting */ - m_port_state = PORT_STATE_TRANSMITTING; - - return (RC_OK); + change_state(PORT_STATE_TX); } -TrexStatelessPort::rc_e +/** + * stop traffic on port + * + * @author imarom (09-Nov-15) + * + * @return TrexStatelessPort::rc_e + */ +void TrexStatelessPort::stop_traffic(void) { - /* real code goes here */ - if (m_port_state != PORT_STATE_TRANSMITTING) { - return (RC_ERR_BAD_STATE_FOR_OP); - } + verify_state(PORT_STATE_TX); /* generate a message to all the relevant DP cores to start transmitting */ TrexStatelessCpToDpMsgBase *stop_msg = new TrexStatelessDpStop(m_port_id); send_message_to_dp(stop_msg); - m_port_state = PORT_STATE_UP_IDLE; + change_state(PORT_STATE_STREAMS); +} + +void +TrexStatelessPort::pause_traffic(void) { + + verify_state(PORT_STATE_TX); + + #if 0 + /* generate a message to all the relevant DP cores to start transmitting */ + TrexStatelessCpToDpMsgBase *stop_msg = new TrexStatelessDpStop(m_port_id); - return (RC_OK); + send_message_to_dp(stop_msg); + + m_port_state = PORT_STATE_UP_IDLE; + #endif + change_state(PORT_STATE_PAUSE); } -/** -* access the stream table -* -*/ -TrexStreamTable * TrexStatelessPort::get_stream_table() { - return &m_stream_table; +void +TrexStatelessPort::resume_traffic(void) { + + verify_state(PORT_STATE_PAUSE); + + #if 0 + /* generate a message to all the relevant DP cores to start transmitting */ + TrexStatelessCpToDpMsgBase *stop_msg = new TrexStatelessDpStop(m_port_id); + + send_message_to_dp(stop_msg); + + m_port_state = PORT_STATE_UP_IDLE; + #endif + change_state(PORT_STATE_TX); } +void +TrexStatelessPort::update_traffic(double mul) { + + verify_state(PORT_STATE_STREAMS | PORT_STATE_TX | PORT_STATE_PAUSE); + + #if 0 + /* generate a message to all the relevant DP cores to start transmitting */ + TrexStatelessCpToDpMsgBase *stop_msg = new TrexStatelessDpStop(m_port_id); + + send_message_to_dp(stop_msg); + + m_port_state = PORT_STATE_UP_IDLE; + #endif +} std::string -TrexStatelessPort::get_state_as_string() { +TrexStatelessPort::get_state_as_string() const { switch (get_state()) { case PORT_STATE_DOWN: return "down"; - case PORT_STATE_UP_IDLE: - return "idle"; + case PORT_STATE_IDLE: + return "no streams"; + + case PORT_STATE_STREAMS: + return "with streams, idle"; - case PORT_STATE_TRANSMITTING: + case PORT_STATE_TX: return "transmitting"; + + case PORT_STATE_PAUSE: + return "paused"; } return "unknown"; @@ -149,6 +210,24 @@ TrexStatelessPort::get_properties(string &driver, string &speed) { speed = "1 Gbps"; } +bool +TrexStatelessPort::verify_state(int state, bool should_throw) const { + if ( (state & m_port_state) == 0 ) { + if (should_throw) { + throw TrexRpcException("command cannot be executed on current state: '" + get_state_as_string() + "'"); + } else { + return false; + } + } + + return true; +} + +void +TrexStatelessPort::change_state(port_state_e new_state) { + + m_port_state = new_state; +} /** * generate a random connection handler diff --git a/src/stateless/cp/trex_stateless_port.h b/src/stateless/cp/trex_stateless_port.h index 09183768..90bf936e 100644 --- a/src/stateless/cp/trex_stateless_port.h +++ b/src/stateless/cp/trex_stateless_port.h @@ -37,9 +37,11 @@ public: * port state */ enum port_state_e { - PORT_STATE_DOWN, - PORT_STATE_UP_IDLE, - PORT_STATE_TRANSMITTING + PORT_STATE_DOWN = 0x1, + PORT_STATE_IDLE = 0x2, + PORT_STATE_STREAMS = 0x4, + PORT_STATE_TX = 0x8, + PORT_STATE_PAUSE = 0x10, }; /** @@ -53,30 +55,54 @@ public: }; TrexStatelessPort(uint8_t port_id); + + /** + * acquire port + * throws TrexException in case of an error + */ + void acquire(const std::string &user, bool force = false); + + /** + * release the port from the current user + * throws TrexException in case of an error + */ + void release(void); /** * start traffic - * + * throws TrexException in case of an error */ - rc_e start_traffic(double mul); + void start_traffic(double mul); /** * stop traffic - * + * throws TrexException in case of an error + */ + void stop_traffic(void); + + /** + * pause traffic + * throws TrexException in case of an error */ - rc_e stop_traffic(void); + void pause_traffic(void); /** - * access the stream table + * resume traffic + * throws TrexException in case of an error + */ + void resume_traffic(void); + + /** + * update current traffic on port * */ - TrexStreamTable *get_stream_table(); + void update_traffic(double mul); /** * get the port state * */ - port_state_e get_state() { + port_state_e get_state() const { return m_port_state; } @@ -84,7 +110,7 @@ public: * port state as string * */ - std::string get_state_as_string(); + std::string get_state_as_string() const; /** * fill up properties of the port @@ -96,6 +122,7 @@ public: */ void get_properties(std::string &driver, std::string &speed); + /** * query for ownership * @@ -113,10 +140,69 @@ public: return m_owner_handler; } - bool is_free_to_aquire() { - return (m_owner == "none"); + + bool verify_owner_handler(const std::string &handler) { + + return ( (m_owner != "none") && (m_owner_handler == handler) ); + } + /** + * encode stats as JSON + */ + void encode_stats(Json::Value &port); + + uint8_t get_port_id() { + return m_port_id; + } + + /** + * delegators + * + */ + + void add_stream(TrexStream *stream) { + verify_state(PORT_STATE_IDLE | PORT_STATE_STREAMS); + + m_stream_table.add_stream(stream); + + change_state(PORT_STATE_STREAMS); + } + + void remove_stream(TrexStream *stream) { + verify_state(PORT_STATE_STREAMS); + + m_stream_table.remove_stream(stream); + + if (m_stream_table.size() == 0) { + change_state(PORT_STATE_IDLE); + } + } + + void remove_and_delete_all_streams() { + verify_state(PORT_STATE_IDLE | PORT_STATE_STREAMS); + + m_stream_table.remove_and_delete_all_streams(); + + change_state(PORT_STATE_IDLE); + } + + TrexStream * get_stream_by_id(uint32_t stream_id) { + return m_stream_table.get_stream_by_id(stream_id); + } + + void get_id_list(std::vector &id_list) { + m_stream_table.get_id_list(id_list); + } + + void get_object_list(std::vector &object_list) { + m_stream_table.get_object_list(object_list); + } + +private: + + + /** * take ownership of the server array * this is static @@ -133,22 +219,14 @@ public: m_owner_handler = ""; } - bool verify_owner_handler(const std::string &handler) { - - return ( (m_owner != "none") && (m_owner_handler == handler) ); - + bool is_free_to_aquire() { + return (m_owner == "none"); } - /** - * encode stats as JSON - */ - void encode_stats(Json::Value &port); - uint8_t get_port_id() { - return m_port_id; - } + bool verify_state(int state, bool should_throw = true) const; -private: + void change_state(port_state_e new_state); std::string generate_handler(); -- cgit 1.2.3-korg From d04fb533c0843ebcd3eac5fbefa6f418582db7fc Mon Sep 17 00:00:00 2001 From: Dan Klein Date: Tue, 10 Nov 2015 10:16:28 +0200 Subject: Major progress in parsing, not stable yet Most advanced: start, stop functionality --- .../client/trex_stateless_client.py | 31 +- .../trex_control_plane/console/parsing_opts.py | 92 +++- .../trex_control_plane/console/trex_console.py | 495 ++++++++++++++++----- 3 files changed, 488 insertions(+), 130 deletions(-) (limited to 'scripts/automation/trex_control_plane/console') 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 627c3365..11728965 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -51,10 +51,10 @@ class CTRexStatelessClient(object): # print args # print kwargs port_ids = kwargs.get("port_id") - if not port_ids: - # print "FROM ARGS!" - # print args - port_ids = args[0] + # 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] @@ -74,8 +74,8 @@ class CTRexStatelessClient(object): 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))) + raise ValueError("The requested method ('{0}') cannot be invoked since port IDs {1} aren't " + "acquired".format(func.__name__, list(bad_ids))) else: return func(self, *args, **kwargs) return wrapper_f @@ -232,7 +232,8 @@ class CTRexStatelessClient(object): self.transmit(command.method, command.params), self.ack_success_test) - @force_status(owned=True) + # @force_status(owned=True) + @acquired def add_stream(self, stream_id, stream_obj, port_id=None): if not self._is_ports_valid(port_id): raise ValueError("Provided illegal port id input") @@ -243,15 +244,16 @@ 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): + # @force_status(owned=True) + @acquired + def add_stream_pack(self, stream_pack_list, port_id=None): 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: + for stream_pack in stream_pack_list: commands.extend([RpcCmdData("add_stream", {"port_id": p_id, "handler": self._conn_handler.get(p_id), "stream_id": stream_pack.stream_id, @@ -273,7 +275,8 @@ class CTRexStatelessClient(object): "stream_id": stream_id} return self.transmit("remove_stream", params) - @force_status(owned=True) + # @force_status(owned=True) + @acquired def remove_all_streams(self, port_id=None): if not self._is_ports_valid(port_id): raise ValueError("Provided illegal port id input") @@ -347,13 +350,17 @@ class CTRexStatelessClient(object): self.transmit(command.method, command.params), self.ack_success_test) - @force_status(owned=False, active_and_owned=True) + # @force_status(owned=False, active_and_owned=True) + @acquired def stop_traffic(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 + if not port_ids: + # don't invoke if port ids is empty + return True, [] 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) diff --git a/scripts/automation/trex_control_plane/console/parsing_opts.py b/scripts/automation/trex_control_plane/console/parsing_opts.py index c94a7461..e701b7db 100755 --- a/scripts/automation/trex_control_plane/console/parsing_opts.py +++ b/scripts/automation/trex_control_plane/console/parsing_opts.py @@ -1,6 +1,7 @@ import argparse from collections import namedtuple import sys +import re ArgumentPack = namedtuple('ArgumentPack', ['name_or_flags', 'options']) ArgumentGroup = namedtuple('ArgumentGroup', ['type', 'args', 'options']) @@ -14,29 +15,88 @@ PORT_LIST_WITH_ALL = 4 FILE_PATH = 5 FILE_FROM_DB = 6 STREAM_FROM_PATH_OR_FILE = 7 +DURATION = 8 +FORCE = 9 # list of ArgumentGroup types MUTEX = 1 +def match_time_unit(val): + '''match some val against time shortcut inputs ''' + match = re.match("^(\d+)([m|h]?)$", val) + if match: + digit = int(match.group(1)) + unit = match.group(2) + if not unit: + return digit + elif unit == 'm': + return digit*60 + else: + return digit*60*60 + else: + raise argparse.ArgumentTypeError("Duration should be passed in the following format: \n" + "-d 100 : in sec \n" + "-d 10m : in min \n" + "-d 1h : in hours") + +def match_multiplier(val): + '''match some val against multiplier shortcut inputs ''' + match = re.match("^(\d+)(gb|kpps|%?)$", val) + if match: + digit = int(match.group(1)) + unit = match.group(2) + if not unit: + return digit + elif unit == 'gb': + raise NotImplementedError("gb units are not supported yet") + else: + raise NotImplementedError("kpps units are not supported yet") + else: + raise argparse.ArgumentTypeError("Multiplier should be passed in the following format: \n" + "-m 100 : multiply stream file by this factor \n" + "-m 10gb : from graph calculate the maximum rate as this bandwidth (for each port)\n" + "-m 10kpps : from graph calculate the maximum rate as this pps (for each port)\n" + "-m 40% : from graph calculate the maximum rate as this percent from total port (for each port)") + + OPTIONS_DB = {MULTIPLIER: ArgumentPack(['-m', '--multiplier'], - {'help': "Set multiplier for stream", 'dest': "mult", 'type': float}), + {'help': "Set multiplier for stream", + 'dest': "mult", + 'default': 1.0, + 'type': match_multiplier}), PORT_LIST: ArgumentPack(['--port'], {"nargs": '+', # "action": "store_" + 'dest':'ports', + 'metavar': 'PORTS', + # 'type': int, 'help': "A list of ports on which to apply the command", 'default': []}), ALL_PORTS: ArgumentPack(['-a'], {"action": "store_true", - "dest": "all", + "dest": "all_ports", 'help': "Set this flag to apply the command on all available ports"}), - + DURATION: ArgumentPack(['-d'], + {"action": "store", + 'metavar': 'TIME', + "type": match_time_unit, + 'help': "Set duration time for TRex."}), + FORCE: ArgumentPack(['--force'], + {"action": "store_true", + 'default': False, + 'help': "Set if you want to stop active ports before applying new TRex run on them."}), FILE_PATH: ArgumentPack(['-f'], - {'help': "File path to YAML file that describes a stream pack"}), + {'metavar': ('FILE', 'DB_NAME'), + 'dest': 'file', + 'nargs': 2, + 'help': "File path to YAML file that describes a stream pack. " + "Second argument is a name to store the loaded yaml file into db."}), FILE_FROM_DB: ArgumentPack(['--db'], - {'help': "A stream pack which already loaded into console cache."}), + {'metavar': 'LOADED_STREAM_PACK', + 'help': "A stream pack which already loaded into console cache."}), # advanced options PORT_LIST_WITH_ALL: ArgumentGroup(MUTEX, [PORT_LIST, ALL_PORTS], @@ -51,25 +111,23 @@ class CCmdArgParser(argparse.ArgumentParser): def __init__(self, *args, **kwargs): super(CCmdArgParser, self).__init__(*args, **kwargs) - pass - # def error(self, message): + # def exit(self, status=0, message=None): # try: - # super(CCmdArgParser, self).error(message) # this will trigger system exit! + # return super(CCmdArgParser, self).exit(status, message) # this will trigger system exit! # except SystemExit: + # print "Caught system exit!!" # return -1 - # - # # self.print_usage(sys.stderr) - # # print ('%s: error: %s\n') % (self.prog, message) - # # self.print_help() - # return + # # return - def exit(self, status=0, message=None): + def parse_args(self, args=None, namespace=None): try: - super(CCmdArgParser, self).exit(status, message) # this will trigger system exit! + return super(CCmdArgParser, self).parse_args(args, namespace) except SystemExit: - return -1 - return + # recover from system exit scenarios, such as "help", or bad arguments. + return None + + def gen_parser(op_name, description, *args): parser = CCmdArgParser(prog=op_name, conflict_handler='resolve', diff --git a/scripts/automation/trex_control_plane/console/trex_console.py b/scripts/automation/trex_control_plane/console/trex_console.py index 68050fc0..a61881a1 100755 --- a/scripts/automation/trex_control_plane/console/trex_console.py +++ b/scripts/automation/trex_control_plane/console/trex_console.py @@ -163,6 +163,7 @@ class TRexConsole(cmd.Cmd): self.intro += "\nType 'help' or '?' for supported actions\n" self.verbose = False + self._silent = True self.postcmd(False, "") @@ -177,7 +178,7 @@ class TRexConsole(cmd.Cmd): # set verbose on / off - def do_verbose (self, line): + def do_verbose(self, line): '''Shows or set verbose mode\n''' if line == "": print "\nverbose is " + ("on\n" if self.verbose else "off\n") @@ -211,7 +212,7 @@ class TRexConsole(cmd.Cmd): print format_text("[SUCCESS]\n", 'green', 'bold') return - def do_ping (self, line): + def do_ping(self, line): '''Pings the RPC server\n''' print "\n-> Pinging RPC server" @@ -223,7 +224,7 @@ class TRexConsole(cmd.Cmd): print "\n*** " + msg + "\n" return - def do_force_acquire (self, line): + def do_force_acquire(self, line): '''Acquires ports by force\n''' self.do_acquire(line, True) @@ -367,49 +368,49 @@ class TRexConsole(cmd.Cmd): self.supported_rpc = self.stateless_client.get_supported_cmds().data - def do_rpc (self, line): - '''Launches a RPC on the server\n''' - - if line == "": - print "\nUsage: [method name] [param dict as string]\n" - print "Example: rpc test_add {'x': 12, 'y': 17}\n" - return - - sp = line.split(' ', 1) - method = sp[0] - - params = None - bad_parse = False - if len(sp) > 1: - - try: - params = ast.literal_eval(sp[1]) - if not isinstance(params, dict): - bad_parse = True - - except ValueError as e1: - bad_parse = True - except SyntaxError as e2: - bad_parse = True - - if bad_parse: - print "\nValue should be a valid dict: '{0}'".format(sp[1]) - print "\nUsage: [method name] [param dict as string]\n" - print "Example: rpc test_add {'x': 12, 'y': 17}\n" - return - - res_ok, msg = self.stateless_client.transmit(method, params) - if res_ok: - print "\nServer Response:\n\n" + pretty_json(json.dumps(msg)) + "\n" - else: - print "\n*** " + msg + "\n" - #print "Please try 'reconnect' to reconnect to server" - - - def complete_rpc (self, text, line, begidx, endidx): - return [x - for x in self.supported_rpc - if x.startswith(text)] + # def do_rpc (self, line): + # '''Launches a RPC on the server\n''' + # + # if line == "": + # print "\nUsage: [method name] [param dict as string]\n" + # print "Example: rpc test_add {'x': 12, 'y': 17}\n" + # return + # + # sp = line.split(' ', 1) + # method = sp[0] + # + # params = None + # bad_parse = False + # if len(sp) > 1: + # + # try: + # params = ast.literal_eval(sp[1]) + # if not isinstance(params, dict): + # bad_parse = True + # + # except ValueError as e1: + # bad_parse = True + # except SyntaxError as e2: + # bad_parse = True + # + # if bad_parse: + # print "\nValue should be a valid dict: '{0}'".format(sp[1]) + # print "\nUsage: [method name] [param dict as string]\n" + # print "Example: rpc test_add {'x': 12, 'y': 17}\n" + # return + # + # res_ok, msg = self.stateless_client.transmit(method, params) + # if res_ok: + # print "\nServer Response:\n\n" + pretty_json(json.dumps(msg)) + "\n" + # else: + # print "\n*** " + msg + "\n" + # #print "Please try 'reconnect' to reconnect to server" + # + # + # def complete_rpc (self, text, line, begidx, endidx): + # return [x + # for x in self.supported_rpc + # if x.startswith(text)] def do_status (self, line): '''Shows a graphical console\n''' @@ -611,7 +612,7 @@ class TRexConsole(cmd.Cmd): owned = set(self.stateless_client.get_acquired_ports()) try: if set(port_list).issubset(owned): - res_ok, log = self.stateless_client.add_stream_pack(port_list, *stream_list.compiled) + res_ok, log = self.stateless_client.add_stream_pack(stream_list.compiled, port_id=port_list) # res_ok, msg = self.stateless_client.add_stream(port_list, stream_list.compiled) self.prompt_response(log) if not res_ok: @@ -699,14 +700,302 @@ class TRexConsole(cmd.Cmd): def complete_remove_all_streams(self, text, line, begidx, endidx): return self.port_auto_complete(text, line, begidx, endidx) + def do_start(self, line): + '''Start selected traffic in specified ports on TRex\n''' + # make sure that the user wants to acquire all + parser = parsing_opts.gen_parser("start", self.do_start.__doc__, + parsing_opts.PORT_LIST_WITH_ALL, + parsing_opts.FORCE, + parsing_opts.STREAM_FROM_PATH_OR_FILE, + parsing_opts.DURATION, + parsing_opts.MULTIPLIER) + opts = parser.parse_args(line.split()) + if opts is None: + # avoid further processing in this command + return + # print opts + port_list = self.extract_port_list(opts) + # print port_list + if opts.force: + # stop all active ports, if any + res_ok = self.stop_traffic(set(self.stateless_client.get_active_ports()).intersection(port_list)) + if not res_ok: + print yellow("[ABORTED]\n") + return + # remove all traffic from ports + res_ok = self.remove_all_streams(port_list) + if not res_ok: + print yellow("[ABORTED]\n") + return + # decide which traffic to use + stream_pack_name = None + if opts.db: + # use pre-loaded traffic + print format_text('{:<30}'.format("Load stream pack (from DB):"), 'bold'), + if opts.db not in self.streams_db.get_loaded_streams_names(): + print format_text("[FAILED]\n", 'red', 'bold') + print yellow("[ABORTED]\n") + return + else: + stream_pack_name = opts.db + else: + # try loading a YAML file + print format_text('{:<30}'.format("Load stream pack (from file):"), 'bold'), + stream_list = CStreamList() + loaded_obj = stream_list.load_yaml(opts.file[0]) + # print self.stateless_client.pretty_json(json.dumps(loaded_obj)) + try: + compiled_streams = stream_list.compile_streams() + res_ok = self.streams_db.load_streams(opts.file[1], + LoadedStreamList(loaded_obj, + [StreamPack(v.stream_id, v.stream.dump()) + for k, v in compiled_streams.items()])) + if not res_ok: + print format_text("[FAILED]\n", 'red', 'bold') + print yellow("[ABORTED]\n") + return + print format_text("[SUCCESS]\n", 'green', 'bold') + stream_pack_name = opts.file[1] + except Exception as e: + print format_text("[FAILED]\n", 'red', 'bold') + print yellow("[ABORTED]\n") + res_ok = self.attach_to_port(stream_pack_name, port_list) + if not res_ok: + print yellow("[ABORTED]\n") + return + # finally, start the traffic + res_ok = self.start_traffic(opts.mult, port_list) + if not res_ok: + print yellow("[ABORTED]\n") + return + return + + def help_start(self): + self.do_start("-h") + + def do_stop(self, line): + '''Stop active traffic in specified ports on TRex\n''' + parser = parsing_opts.gen_parser("stop", self.do_stop.__doc__, + parsing_opts.PORT_LIST_WITH_ALL) + opts = parser.parse_args(line.split()) + if opts is None: + # avoid further processing in this command + return + port_list = self.extract_port_list(opts) + res_ok = self.stop_traffic(port_list) + return + + + def help_stop(self): + self.do_stop("-h") + + + def do_debug(self, line): + '''Enter DEBUG mode of the console to invoke smaller building blocks with server''' + i = DebugTRexConsole(self) + i.prompt = self.prompt[:-3] + ':' + blue('debug') + ' > ' + i.cmdloop() + + # aliasing + do_exit = do_EOF = do_q = do_quit + + # ----- utility methods ----- # + + def start_traffic(self, multiplier, port_list):#, silent=True): + print format_text('{:<30}'.format("Start traffic:"), 'bold'), + try: + res_ok, log = self.stateless_client.start_traffic(multiplier, port_id=port_list) + if not self._silent: + print '' + self.prompt_response(log) + if not res_ok: + print format_text("[FAILED]\n", 'red', 'bold') + return False + print format_text("[SUCCESS]\n", 'green', 'bold') + return True + except ValueError as e: + print '' + print magenta(str(e)) + print format_text("[FAILED]\n", 'red', 'bold') + return False + + def attach_to_port(self, stream_pack_name, port_list): + print format_text('{:<30}'.format("Attaching traffic to ports:"), 'bold'), + stream_list = self.streams_db.get_stream_pack(stream_pack_name) #user_streams[args[0]] + if not stream_list: + print "Provided stream list name '{0}' doesn't exists.".format(stream_pack_name) + print format_text("[FAILED]\n", 'red', 'bold') + return + try: + res_ok, log = self.stateless_client.add_stream_pack(stream_list.compiled, port_id=port_list) + if not self._silent: + print '' + self.prompt_response(log) + if not res_ok: + print format_text("[FAILED]\n", 'red', 'bold') + return False + print format_text("[SUCCESS]\n", 'green', 'bold') + return True + except ValueError as e: + print '' + print magenta(str(e)) + print format_text("[FAILED]\n", 'red', 'bold') + return False + + def stop_traffic(self, port_list): + print format_text('{:<30}'.format("Stop traffic:"), 'bold'), + try: + res_ok, log = self.stateless_client.stop_traffic(port_id=port_list) + if not self._silent: + print '' + self.prompt_response(log) + if not res_ok: + print format_text("[FAILED]\n", 'red', 'bold') + return + print format_text("[SUCCESS]\n", 'green', 'bold') + return True + except ValueError as e: + print '' + print magenta(str(e)) + print format_text("[FAILED]\n", 'red', 'bold') + + def remove_all_streams(self, port_list): + '''Remove all streams from given port_list''' + print format_text('{:<30}'.format("Remove all streams:"), 'bold'), + try: + res_ok, log = self.stateless_client.remove_all_streams(port_id=port_list) + if not self._silent: + print '' + self.prompt_response(log) + if not res_ok: + print format_text("[FAILED]\n", 'red', 'bold') + return + print format_text("[SUCCESS]\n", 'green', 'bold') + return True + except ValueError as e: + print '' + print magenta(str(e)) + print format_text("[FAILED]\n", 'red', 'bold') + + + + + + def extract_port_list(self, opts): + if opts.all_ports or "all" in opts.ports: + # handling all ports + port_list = self.stateless_client.get_acquired_ports() + else: + port_list = self.extract_port_ids_from_list(opts.ports) + return port_list + + def decode_multiplier(self, opts_mult): + pass + + +class DebugTRexConsole(cmd.Cmd): + + def __init__(self, trex_main_console): + cmd.Cmd.__init__(self) + self.trex_console = trex_main_console + self.stateless_client = self.trex_console.stateless_client + self.streams_db = self.trex_console.streams_db + self.register_main_console_methods() + self.do_silent("on") + pass + + # ----- super methods overriding ----- # + def completenames(self, text, *ignored): + dotext = 'do_'+text + return [a[3:]+' ' for a in self.get_names() if a.startswith(dotext)] + + def get_names(self): + result = cmd.Cmd.get_names(self) + result += self.trex_console.get_names() + return list(set(result)) + + def register_main_console_methods(self): + main_names = set(self.trex_console.get_names()).difference(set(dir(self.__class__))) + for name in main_names: + for prefix in 'do_', 'help_', 'complete_': + if name.startswith(prefix): + self.__dict__[name] = getattr(self.trex_console, name) + + # if (name[:3] == 'do_') or (name[:5] == 'help_') or (name[:9] == 'complete_'): + # chosen.append(name) + # self.__dict__[name] = getattr(self.trex_console, name) + # # setattr(self, name, classmethod(getattr(self.trex_console, name))) + + # print chosen + # self.get_names() + + # return result + + + # ----- DEBUGGING methods ----- # + # set silent on / off + def do_silent(self, line): + '''Shows or set silent mode\n''' + if line == "": + print "\nsilent mode is " + ("on\n" if self.trex_console._silent else "off\n") + + elif line == "on": + self.verbose = True + self.stateless_client.set_verbose(True) + print green("\nsilent set to on\n") + + elif line == "off": + self.verbose = False + self.stateless_client.set_verbose(False) + print green("\nsilent set to off\n") + + else: + print magenta("\nplease specify 'on' or 'off'\n") + + def do_quit(self, line): + '''Exit the debug client back to main console\n''' + self.do_silent("off") + return True + def do_start_traffic(self, line): '''Start pre-submitted traffic in specified ports on TRex\n''' # make sure that the user wants to acquire all - # parser = parsing_opts.gen_parser("start_traffic", self.do_start_traffic.__doc__, - # parsing_opts.PORT_LIST_WITH_ALL, parsing_opts.MULTIPLIER) - # opts = parser.parse_args(line.split()) - # + parser = parsing_opts.gen_parser("start_traffic", self.do_start_traffic.__doc__, + parsing_opts.PORT_LIST_WITH_ALL, parsing_opts.MULTIPLIER) + opts = parser.parse_args(line.split()) # print opts + # return + if opts is None: + # avoid further processing in this command + return + try: + port_list = self.trex_console.extract_port_list(opts) + return self.trex_console.start_traffic(opts.mult, port_list) + except Exception as e: + print e + return + + def do_stop_traffic(self, line): + '''Stop active traffic in specified ports on TRex\n''' + parser = parsing_opts.gen_parser("stop_traffic", self.do_stop_traffic.__doc__, + parsing_opts.PORT_LIST_WITH_ALL) + opts = parser.parse_args(line.split()) + # print opts + # return + if opts is None: + # avoid further processing in this command + return + try: + port_list = self.trex_console.extract_port_list(opts) + return self.trex_console.stop_traffic(port_list) + except Exception as e: + print e + return + + + def complete_stop_traffic(self, text, line, begidx, endidx): + return self.port_auto_complete(text, line, begidx, endidx, active=True) + # return # # return # # if not opts.port_list: @@ -714,21 +1003,9 @@ class TRexConsole(cmd.Cmd): # # "or specify 'all' to start traffic on all acquired ports") # # return # - # if "all" in opts.port_list: - # ask = ConfirmMenu('Are you sure you want to start traffic at all acquired ports? ') - # rc = ask.show() - # if rc == False: - # print yellow("[ABORTED]\n") - # return - # else: - # port_list = self.stateless_client.get_acquired_ports() - # else: - # try: - # port_list = self.extract_port_ids_from_list(opts.port_list) - # except ValueError as e: - # print magenta(e) - # return + + return args = line.split() if len(args) < 1: print magenta("Please provide a list of ports separated by spaces, " @@ -757,46 +1034,60 @@ class TRexConsole(cmd.Cmd): print format_text("[FAILED]\n", 'red', 'bold') def complete_start_traffic(self, text, line, begidx, endidx): - return self.port_auto_complete(text, line, begidx, endidx) + # return self.port_auto_complete(text, line, begidx, endidx) + return [text] def help_start_traffic(self): self.do_start_traffic("-h") - def do_stop_traffic(self, line): - '''Stop active traffic in specified ports on TRex\n''' - # make sure that the user wants to acquire all - args = line.split() - if len(args) < 1: - print magenta("Please provide a list of ports separated by spaces, " - "or specify 'all' to stop traffic on all acquired ports") + def help_stop_traffic(self): + self.do_stop_traffic("-h") + + # def do_help(self): + + def do_rpc (self, line): + '''Launches a RPC on the server\n''' + + if line == "": + print "\nUsage: [method name] [param dict as string]\n" + print "Example: rpc test_add {'x': 12, 'y': 17}\n" return - if args[0] == "all": - ask = ConfirmMenu('Are you sure you want to start traffic at all acquired ports? ') - rc = ask.show() - if rc == False: - print yellow("[ABORTED]\n") - return - else: - port_list = self.stateless_client.get_active_ports() - if not port_list: - print magenta("no active ports - operation aborted\n") - return + + sp = line.split(' ', 1) + method = sp[0] + + params = None + bad_parse = False + if len(sp) > 1: + + try: + params = ast.literal_eval(sp[1]) + if not isinstance(params, dict): + bad_parse = True + + except ValueError as e1: + bad_parse = True + except SyntaxError as e2: + bad_parse = True + + if bad_parse: + print "\nValue should be a valid dict: '{0}'".format(sp[1]) + print "\nUsage: [method name] [param dict as string]\n" + print "Example: rpc test_add {'x': 12, 'y': 17}\n" + return + + res_ok, msg = self.stateless_client.transmit(method, params) + if res_ok: + print "\nServer Response:\n\n" + pretty_json(json.dumps(msg)) + "\n" else: - port_list = self.extract_port_ids_from_line(line) + print "\n*** " + msg + "\n" + #print "Please try 'reconnect' to reconnect to server" - try: - res_ok, log = self.stateless_client.stop_traffic(port_list) - self.prompt_response(log) - if not res_ok: - print format_text("[FAILED]\n", 'red', 'bold') - return - print format_text("[SUCCESS]\n", 'green', 'bold') - except ValueError as e: - print magenta(str(e)) - print format_text("[FAILED]\n", 'red', 'bold') - def complete_stop_traffic(self, text, line, begidx, endidx): - return self.port_auto_complete(text, line, begidx, endidx, active=True) + def complete_rpc (self, text, line, begidx, endidx): + return [x + for x in self.trex_console.supported_rpc + if x.startswith(text)] # aliasing do_exit = do_EOF = do_q = do_quit @@ -808,12 +1099,13 @@ def setParserOptions(): default = "localhost", type = str) - parser.add_argument("-p", "--port", help = "TRex Server Port [default is 5050]\n", - default = 5050, + parser.add_argument("-p", "--port", help = "TRex Server Port [default is 4505]\n", + default = 4505, type = int) - parser.add_argument("-z", "--pub", help = "TRex Async Publisher Port [default is 4500]\n", - default = 4500, + parser.add_argument("--async_port", help = "TRex ASync Publisher Port [default is 4506]\n", + default = 4506, + dest='pub', type = int) parser.add_argument("-u", "--user", help = "User Name [default is currently logged in user]\n", @@ -826,6 +1118,7 @@ def setParserOptions(): return parser + def main(): parser = setParserOptions() options = parser.parse_args() -- cgit 1.2.3-korg From 467382a7611f478d66cf58c1307f079239f7bac6 Mon Sep 17 00:00:00 2001 From: imarom Date: Wed, 11 Nov 2015 13:48:02 +0200 Subject: lightweight console --- .../client/trex_stateless_client.py | 102 +- .../trex_control_plane/console/old_console.py | 946 +++++++++++++++++ .../trex_control_plane/console/parsing_opts.py | 74 +- .../trex_control_plane/console/trex_console.py | 1111 ++++---------------- .../trex_control_plane/console/trex_status.py | 5 +- src/rpc-server/trex_rpc_req_resp_server.h | 2 +- src/stateless/cp/trex_stateless_port.cpp | 4 +- 7 files changed, 1288 insertions(+), 956 deletions(-) create mode 100644 scripts/automation/trex_control_plane/console/old_console.py (limited to 'scripts/automation/trex_control_plane/console') 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 11728965..168853b3 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -232,7 +232,6 @@ class CTRexStatelessClient(object): self.transmit(command.method, command.params), self.ack_success_test) - # @force_status(owned=True) @acquired def add_stream(self, stream_id, stream_obj, port_id=None): if not self._is_ports_valid(port_id): @@ -244,7 +243,6 @@ class CTRexStatelessClient(object): "stream": stream_obj.dump()} return self.transmit("add_stream", params) - # @force_status(owned=True) @acquired def add_stream_pack(self, stream_pack_list, port_id=None): if not self._is_ports_valid(port_id): @@ -262,9 +260,11 @@ class CTRexStatelessClient(object): 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) + if not res_ok: + return res_ok, resp_list + + 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): @@ -275,8 +275,6 @@ class CTRexStatelessClient(object): "stream_id": stream_id} return self.transmit("remove_stream", params) - # @force_status(owned=True) - @acquired def remove_all_streams(self, port_id=None): if not self._is_ports_valid(port_id): raise ValueError("Provided illegal port id input") @@ -326,7 +324,6 @@ class CTRexStatelessClient(object): "get_pkt": get_pkt} return self.transmit("get_stream_list", params) - @acquired def start_traffic(self, multiplier, port_id=None): if not self._is_ports_valid(port_id): raise ValueError("Provided illegal port id input") @@ -350,8 +347,6 @@ class CTRexStatelessClient(object): self.transmit(command.method, command.params), self.ack_success_test) - # @force_status(owned=False, active_and_owned=True) - @acquired def stop_traffic(self, port_id=None): if not self._is_ports_valid(port_id): raise ValueError("Provided illegal port id input") @@ -371,7 +366,7 @@ class CTRexStatelessClient(object): params = {"handler": self._conn_handler.get(port_id), "port_id": port_id} command = RpcCmdData("stop_traffic", params) - return self._handle_start_traffic_response(command, + return self._handle_stop_traffic_response(command, self.transmit(command.method, command.params), self.ack_success_test) @@ -471,6 +466,88 @@ class CTRexStatelessClient(object): return False + ######################### Console (high level) API ######################### + + # reset + # acquire, stop, remove streams and clear stats + # + # + def cmd_reset (self, annotate_func): + + ports = self.get_port_ids() + + # sync with the server + rc, log = self._init_sync() + annotate_func("Syncing with the server:", rc, log) + if not rc: + return False + + + # force acquire all ports + rc, log = self.acquire(ports, force = True) + annotate_func("Force acquiring all ports:", rc, log) + if not rc: + return False + + # force stop + rc, log = self.stop_traffic(ports) + annotate_func("Stop traffic on all ports:", rc, log) + if not rc: + return False + + # remove all streams + rc, log = self.remove_all_streams(ports) + annotate_func("Removing all streams from all ports:", rc, log) + if not rc: + return False + + # TODO: clear stats + return True + + + # stop cmd + def cmd_stop (self, ports, annotate_func): + + # find the relveant ports + active_ports = set(self.get_active_ports()).intersection(ports) + if not active_ports: + annotate_func("No active traffic on porivded ports") + return True + + rc, log = self.stop_traffic(active_ports) + annotate_func("Stopping traffic on ports {0}:".format([port for port in active_ports]), rc, log) + if not rc: + return False + + return True + + # start cmd + def cmd_start (self, ports, stream_list, mult, force, annotate_func): + + if force: + rc = self.cmd_stop(ports, annotate_func) + if not rc: + return False + + rc, log = self.remove_all_streams(ports) + annotate_func("Removing all streams from ports {0}:".format([port for port in ports]), rc, log, + "Please either retry with --force or stop traffic") + if not rc: + return False + + rc, log = self.add_stream_pack(stream_list.compiled, port_id= ports) + annotate_func("Attaching streams to port {0}:".format([port for port in ports]), rc, log) + if not rc: + return False + + # finally, start the traffic + rc, log = self.start_traffic(mult, ports) + annotate_func("Starting traffic on ports {0}:".format([port for port in ports]), rc, log) + if not rc: + return False + + return True + # ----- handler internal methods ----- # def _handle_general_response(self, request, response, msg, success_test=None): port_id = request.params.get("port_id") @@ -525,7 +602,8 @@ class CTRexStatelessClient(object): 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) + if port_id in self._active_ports: + self._active_ports.remove(port_id) return RpcResponseStatus(True, port_id, "Traffic stopped") else: return RpcResponseStatus(False, port_id, response.data) diff --git a/scripts/automation/trex_control_plane/console/old_console.py b/scripts/automation/trex_control_plane/console/old_console.py new file mode 100644 index 00000000..93c7e3f4 --- /dev/null +++ b/scripts/automation/trex_control_plane/console/old_console.py @@ -0,0 +1,946 @@ + +# main console object +class TRexConsole1(cmd.Cmd): + """Trex Console""" + + def __init__(self, stateless_client, verbose): + cmd.Cmd.__init__(self) + + self.stateless_client = stateless_client + + self.do_connect("") + + self.intro = "\n-=TRex Console v{ver}=-\n".format(ver=__version__) + self.intro += "\nType 'help' or '?' for supported actions\n" + + self.verbose = False + self._silent = True + + self.postcmd(False, "") + + self.user_streams = {} + self.streams_db = CStreamsDB() + + + # a cool hack - i stole this function and added space + def completenames(self, text, *ignored): + dotext = 'do_'+text + return [a[3:]+' ' for a in self.get_names() if a.startswith(dotext)] + + + # set verbose on / off + def do_verbose(self, line): + '''Shows or set verbose mode\n''' + if line == "": + print "\nverbose is " + ("on\n" if self.verbose else "off\n") + + elif line == "on": + self.verbose = True + self.stateless_client.set_verbose(True) + print green("\nverbose set to on\n") + + elif line == "off": + self.verbose = False + self.stateless_client.set_verbose(False) + print green("\nverbose set to off\n") + + else: + print magenta("\nplease specify 'on' or 'off'\n") + + # query the server for registered commands + def do_query_server(self, line): + '''query the RPC server for supported remote commands\n''' + + res_ok, msg = self.stateless_client.get_supported_cmds() + if not res_ok: + print format_text("[FAILED]\n", 'red', 'bold') + return + print "\nRPC server supports the following commands:\n" + for func in msg: + if func: + print func + print '' + print format_text("[SUCCESS]\n", 'green', 'bold') + return + + def do_ping(self, line): + '''Pings the RPC server\n''' + + print "\n-> Pinging RPC server" + + res_ok, msg = self.stateless_client.ping() + if res_ok: + print format_text("[SUCCESS]\n", 'green', 'bold') + else: + print "\n*** " + msg + "\n" + return + + def do_force_acquire(self, line): + '''Acquires ports by force\n''' + + self.do_acquire(line, True) + + def complete_force_acquire(self, text, line, begidx, endidx): + return self.port_auto_complete(text, line, begidx, endidx, acquired=False) + + def extract_port_ids_from_line(self, line): + return {int(x) for x in line.split()} + + def extract_port_ids_from_list(self, port_list): + return {int(x) for x in port_list} + + def parse_ports_from_line (self, line): + port_list = set() + if line: + for port_id in line.split(' '): + if (not port_id.isdigit()) or (int(port_id) < 0) or (int(port_id) >= self.stateless_client.get_port_count()): + print "Please provide a list of ports separated by spaces between 0 and {0}".format(self.stateless_client.get_port_count() - 1) + return None + + port_list.add(int(port_id)) + + port_list = list(port_list) + + else: + port_list = [i for i in xrange(0, self.stateless_client.get_port_count())] + + return port_list + + + def do_acquire(self, line, force=False): + '''Acquire ports\n''' + + # make sure that the user wants to acquire all + args = line.split() + if len(args) < 1: + print magenta("Please provide a list of ports separated by spaces, or specify 'all' to acquire all available ports") + return + + if args[0] == "all": + ask = ConfirmMenu('Are you sure you want to acquire all ports ? ') + rc = ask.show() + if rc == False: + print yellow("[ABORTED]\n") + return + else: + port_list = self.stateless_client.get_port_ids() + else: + port_list = self.extract_port_ids_from_line(line) + + # rc, resp_list = self.stateless_client.take_ownership(port_list, force) + try: + res_ok, log = self.stateless_client.acquire(port_list, force) + self.prompt_response(log) + if not res_ok: + print format_text("[FAILED]\n", 'red', 'bold') + return + print format_text("[SUCCESS]\n", 'green', 'bold') + except ValueError as e: + print magenta(str(e)) + print format_text("[FAILED]\n", 'red', 'bold') + + + def port_auto_complete(self, text, line, begidx, endidx, acquired=True, active=False): + if acquired: + if not active: + ret_list = [x + for x in map(str, self.stateless_client.get_acquired_ports()) + if x.startswith(text)] + else: + ret_list = [x + for x in map(str, self.stateless_client.get_active_ports()) + if x.startswith(text)] + else: + ret_list = [x + for x in map(str, self.stateless_client.get_port_ids()) + if x.startswith(text)] + ret_list.append("all") + return ret_list + + + def complete_acquire(self, text, line, begidx, endidx): + return self.port_auto_complete(text, line, begidx, endidx, acquired=False) + + def do_release (self, line): + '''Release ports\n''' + + # if line: + # port_list = self.parse_ports_from_line(line) + # else: + # port_list = self.stateless_client.get_owned_ports() + args = line.split() + if len(args) < 1: + print "Please provide a list of ports separated by spaces, or specify 'all' to acquire all available ports" + if args[0] == "all": + ask = ConfirmMenu('Are you sure you want to release all acquired ports? ') + rc = ask.show() + if rc == False: + print yellow("[ABORTED]\n") + return + else: + port_list = self.stateless_client.get_acquired_ports() + else: + port_list = self.extract_port_ids_from_line(line) + + try: + res_ok, log = self.stateless_client.release(port_list) + self.prompt_response(log) + if not res_ok: + print format_text("[FAILED]\n", 'red', 'bold') + return + print format_text("[SUCCESS]\n", 'green', 'bold') + except ValueError as e: + print magenta(str(e)) + print format_text("[FAILED]\n", 'red', 'bold') + return + + def complete_release(self, text, line, begidx, endidx): + return self.port_auto_complete(text, line, begidx, endidx) + + def do_connect (self, line): + '''Connects to the server\n''' + + if line == "": + res_ok, msg = self.stateless_client.connect() + else: + sp = line.split() + if (len(sp) != 2): + print "\n[usage] connect [server] [port] or without parameters\n" + return + + res_ok, msg = self.stateless_client.connect(sp[0], sp[1]) + + if res_ok: + print format_text("[SUCCESS]\n", 'green', 'bold') + else: + print "\n*** " + msg + "\n" + print format_text("[FAILED]\n", 'red', 'bold') + return + + self.supported_rpc = self.stateless_client.get_supported_cmds().data + + # def do_rpc (self, line): + # '''Launches a RPC on the server\n''' + # + # if line == "": + # print "\nUsage: [method name] [param dict as string]\n" + # print "Example: rpc test_add {'x': 12, 'y': 17}\n" + # return + # + # sp = line.split(' ', 1) + # method = sp[0] + # + # params = None + # bad_parse = False + # if len(sp) > 1: + # + # try: + # params = ast.literal_eval(sp[1]) + # if not isinstance(params, dict): + # bad_parse = True + # + # except ValueError as e1: + # bad_parse = True + # except SyntaxError as e2: + # bad_parse = True + # + # if bad_parse: + # print "\nValue should be a valid dict: '{0}'".format(sp[1]) + # print "\nUsage: [method name] [param dict as string]\n" + # print "Example: rpc test_add {'x': 12, 'y': 17}\n" + # return + # + # res_ok, msg = self.stateless_client.transmit(method, params) + # if res_ok: + # print "\nServer Response:\n\n" + pretty_json(json.dumps(msg)) + "\n" + # else: + # print "\n*** " + msg + "\n" + # #print "Please try 'reconnect' to reconnect to server" + # + # + # def complete_rpc (self, text, line, begidx, endidx): + # return [x + # for x in self.supported_rpc + # if x.startswith(text)] + + def do_status (self, line): + '''Shows a graphical console\n''' + + if not self.stateless_client.is_connected(): + print "Not connected to server\n" + return + + self.do_verbose('off') + trex_status.show_trex_status(self.stateless_client) + + def do_quit(self, line): + '''Exit the client\n''' + return True + + def do_disconnect (self, line): + '''Disconnect from the server\n''' + if not self.stateless_client.is_connected(): + print "Not connected to server\n" + return + + res_ok, msg = self.stateless_client.disconnect() + if res_ok: + print format_text("[SUCCESS]\n", 'green', 'bold') + else: + print msg + "\n" + + def do_whoami (self, line): + '''Prints console user name\n''' + print "\n" + self.stateless_client.user + "\n" + + def postcmd(self, stop, line): + if self.stateless_client.is_connected(): + self.prompt = "TRex > " + else: + self.supported_rpc = None + self.prompt = "TRex (offline) > " + + return stop + + def default(self, line): + print "'{0}' is an unrecognized command. type 'help' or '?' for a list\n".format(line) + + # def do_help (self, line): + # '''Shows This Help Screen\n''' + # if line: + # try: + # func = getattr(self, 'help_' + line) + # except AttributeError: + # try: + # doc = getattr(self, 'do_' + line).__doc__ + # if doc: + # self.stdout.write("%s\n"%str(doc)) + # return + # except AttributeError: + # pass + # self.stdout.write("%s\n"%str(self.nohelp % (line,))) + # return + # func() + # return + # + # print "\nSupported Console Commands:" + # print "----------------------------\n" + # + # cmds = [x[3:] for x in self.get_names() if x.startswith("do_")] + # for cmd in cmds: + # if cmd == "EOF": + # continue + # + # try: + # doc = getattr(self, 'do_' + cmd).__doc__ + # if doc: + # help = str(doc) + # else: + # help = "*** Undocumented Function ***\n" + # except AttributeError: + # help = "*** Undocumented Function ***\n" + # + # print "{:<30} {:<30}".format(cmd + " - ", help) + + def do_stream_db_add(self, line): + '''Loads a YAML stream list serialization into user console \n''' + args = line.split() + if len(args) >= 2: + name = args[0] + yaml_path = args[1] + try: + multiplier = args[2] + except IndexError: + multiplier = 1 + stream_list = CStreamList() + loaded_obj = stream_list.load_yaml(yaml_path, multiplier) + # print self.stateless_client.pretty_json(json.dumps(loaded_obj)) + try: + compiled_streams = stream_list.compile_streams() + res_ok = self.streams_db.load_streams(name, LoadedStreamList(loaded_obj, + [StreamPack(v.stream_id, v.stream.dump()) + for k, v in compiled_streams.items()])) + if res_ok: + print green("Stream pack '{0}' loaded and added successfully\n".format(name)) + else: + print magenta("Picked name already exist. Please pick another name.\n") + except Exception as e: + print "adding new stream failed due to the following error:\n", str(e) + print format_text("[FAILED]\n", 'red', 'bold') + + return + else: + print magenta("please provide load name and YAML path, separated by space.\n" + "Optionally, you may provide a third argument to specify multiplier.\n") + + @staticmethod + def tree_autocomplete(text): + dir = os.path.dirname(text) + if dir: + path = dir + else: + path = "." + start_string = os.path.basename(text) + return [x + for x in os.listdir(path) + if x.startswith(start_string)] + + + def complete_stream_db_add(self, text, line, begidx, endidx): + arg_num = len(line.split()) - 1 + if arg_num == 2: + return TRexConsole.tree_autocomplete(line.split()[-1]) + else: + return [text] + + def do_stream_db_show(self, line): + '''Shows the loaded stream list named [name] \n''' + args = line.split() + if args: + list_name = args[0] + try: + stream = self.streams_db.get_stream_pack(list_name)#user_streams[list_name] + if len(args) >= 2 and args[1] == "full": + print pretty_json(json.dumps(stream.compiled)) + else: + print pretty_json(json.dumps(stream.loaded)) + except KeyError as e: + print "Unknown stream list name provided" + else: + print "Available stream packs:\n{0}".format(', '.join(sorted(self.streams_db.get_loaded_streams_names()))) + + def complete_stream_db_show(self, text, line, begidx, endidx): + return [x + for x in self.streams_db.get_loaded_streams_names() + if x.startswith(text)] + + def do_stream_db_remove(self, line): + '''Removes a single loaded stream packs from loaded stream pack repository\n''' + args = line.split() + if args: + removed_streams = self.streams_db.remove_stream_packs(*args) + if removed_streams: + print green("The following stream packs were removed:") + print bold(", ".join(sorted(removed_streams))) + print format_text("[SUCCESS]\n", 'green', 'bold') + else: + print red("No streams were removed. Make sure to provide valid stream pack names.") + else: + print magenta("Please provide stream pack name(s), separated with spaces.") + + def do_stream_db_clear(self, line): + '''Clears all loaded stream packs from loaded stream pack repository\n''' + self.streams_db.clear() + print format_text("[SUCCESS]\n", 'green', 'bold') + + + def complete_stream_db_remove(self, text, line, begidx, endidx): + return [x + for x in self.streams_db.get_loaded_streams_names() + if x.startswith(text)] + + + def do_attach(self, line): + '''Assign loaded stream pack into specified ports on TRex\n''' + args = line.split() + if len(args) >= 2: + stream_pack_name = args[0] + stream_list = self.streams_db.get_stream_pack(stream_pack_name) #user_streams[args[0]] + if not stream_list: + print "Provided stream list name '{0}' doesn't exists.".format(stream_pack_name) + print format_text("[FAILED]\n", 'red', 'bold') + return + if args[1] == "all": + ask = ConfirmMenu('Are you sure you want to release all acquired ports? ') + rc = ask.show() + if rc == False: + print yellow("[ABORTED]\n") + return + else: + port_list = self.stateless_client.get_acquired_ports() + else: + port_list = self.extract_port_ids_from_line(' '.join(args[1:])) + owned = set(self.stateless_client.get_acquired_ports()) + try: + if set(port_list).issubset(owned): + res_ok, log = self.stateless_client.add_stream_pack(stream_list.compiled, port_id=port_list) + # res_ok, msg = self.stateless_client.add_stream(port_list, stream_list.compiled) + self.prompt_response(log) + if not res_ok: + print format_text("[FAILED]\n", 'red', 'bold') + return + print format_text("[SUCCESS]\n", 'green', 'bold') + return + else: + print "Not all desired ports are acquired.\n" \ + "Acquired ports are: {acq}\n" \ + "Requested ports: {req}\n" \ + "Missing ports: {miss}".format(acq=list(owned), + req=port_list, + miss=list(set(port_list).difference(owned))) + print format_text("[FAILED]\n", 'red', 'bold') + except ValueError as e: + print magenta(str(e)) + print format_text("[FAILED]\n", 'red', 'bold') + else: + print magenta("Please provide list name and ports to attach to, " + "or specify 'all' to attach all owned ports.\n") + + def complete_attach(self, text, line, begidx, endidx): + arg_num = len(line.split()) - 1 + if arg_num == 1: + # return optional streams packs + if line.endswith(" "): + return self.port_auto_complete(text, line, begidx, endidx) + return [x + for x in self.streams_db.get_loaded_streams_names() + if x.startswith(text)] + elif arg_num >= 2: + # return optional ports to attach to + return self.port_auto_complete(text, line, begidx, endidx) + else: + return [text] + + def prompt_response(self, response_obj): + resp_list = response_obj if isinstance(response_obj, list) else [response_obj] + def format_return_status(return_status): + if return_status: + return green("OK") + else: + return red("FAIL") + + for response in resp_list: + response_str = "{id:^3} - {msg} ({stat})".format(id=response.id, + msg=response.msg, + stat=format_return_status(response.success)) + print response_str + return + + def do_remove_all_streams(self, line): + '''Acquire ports\n''' + + # make sure that the user wants to acquire all + args = line.split() + if len(args) < 1: + print magenta("Please provide a list of ports separated by spaces, " + "or specify 'all' to remove from all acquired ports") + return + if args[0] == "all": + ask = ConfirmMenu('Are you sure you want to remove all stream packs from all acquired ports? ') + rc = ask.show() + if rc == False: + print yellow("[ABORTED]\n") + return + else: + port_list = self.stateless_client.get_acquired_ports() + else: + port_list = self.extract_port_ids_from_line(line) + + # rc, resp_list = self.stateless_client.take_ownership(port_list, force) + try: + res_ok, log = self.stateless_client.remove_all_streams(port_list) + self.prompt_response(log) + if not res_ok: + print format_text("[FAILED]\n", 'red', 'bold') + return + print format_text("[SUCCESS]\n", 'green', 'bold') + except ValueError as e: + print magenta(str(e)) + print format_text("[FAILED]\n", 'red', 'bold') + + def complete_remove_all_streams(self, text, line, begidx, endidx): + return self.port_auto_complete(text, line, begidx, endidx) + + def do_start(self, line): + '''Start selected traffic in specified ports on TRex\n''' + # make sure that the user wants to acquire all + parser = parsing_opts.gen_parser("start", self.do_start.__doc__, + parsing_opts.PORT_LIST_WITH_ALL, + parsing_opts.FORCE, + parsing_opts.STREAM_FROM_PATH_OR_FILE, + parsing_opts.DURATION, + parsing_opts.MULTIPLIER) + opts = parser.parse_args(line.split()) + if opts is None: + # avoid further processing in this command + return + # print opts + port_list = self.extract_port_list(opts) + # print port_list + if opts.force: + # stop all active ports, if any + res_ok = self.stop_traffic(set(self.stateless_client.get_active_ports()).intersection(port_list)) + if not res_ok: + print yellow("[ABORTED]\n") + return + # remove all traffic from ports + res_ok = self.remove_all_streams(port_list) + if not res_ok: + print yellow("[ABORTED]\n") + return + # decide which traffic to use + stream_pack_name = None + if opts.db: + # use pre-loaded traffic + print format_text('{:<30}'.format("Load stream pack (from DB):"), 'bold'), + if opts.db not in self.streams_db.get_loaded_streams_names(): + print format_text("[FAILED]\n", 'red', 'bold') + print yellow("[ABORTED]\n") + return + else: + stream_pack_name = opts.db + else: + # try loading a YAML file + print format_text('{:<30}'.format("Load stream pack (from file):"), 'bold'), + stream_list = CStreamList() + loaded_obj = stream_list.load_yaml(opts.file[0]) + # print self.stateless_client.pretty_json(json.dumps(loaded_obj)) + try: + compiled_streams = stream_list.compile_streams() + res_ok = self.streams_db.load_streams(opts.file[1], + LoadedStreamList(loaded_obj, + [StreamPack(v.stream_id, v.stream.dump()) + for k, v in compiled_streams.items()])) + if not res_ok: + print format_text("[FAILED]\n", 'red', 'bold') + print yellow("[ABORTED]\n") + return + print format_text("[SUCCESS]\n", 'green', 'bold') + stream_pack_name = opts.file[1] + except Exception as e: + print format_text("[FAILED]\n", 'red', 'bold') + print yellow("[ABORTED]\n") + res_ok = self.attach_to_port(stream_pack_name, port_list) + if not res_ok: + print yellow("[ABORTED]\n") + return + # finally, start the traffic + res_ok = self.start_traffic(opts.mult, port_list) + if not res_ok: + print yellow("[ABORTED]\n") + return + return + + def help_start(self): + self.do_start("-h") + + def do_stop(self, line): + '''Stop active traffic in specified ports on TRex\n''' + parser = parsing_opts.gen_parser("stop", self.do_stop.__doc__, + parsing_opts.PORT_LIST_WITH_ALL) + opts = parser.parse_args(line.split()) + if opts is None: + # avoid further processing in this command + return + port_list = self.extract_port_list(opts) + res_ok = self.stop_traffic(port_list) + return + + + def help_stop(self): + self.do_stop("-h") + + + def do_debug(self, line): + '''Enter DEBUG mode of the console to invoke smaller building blocks with server''' + i = DebugTRexConsole(self) + i.prompt = self.prompt[:-3] + ':' + blue('debug') + ' > ' + i.cmdloop() + + # aliasing + do_exit = do_EOF = do_q = do_quit + + # ----- utility methods ----- # + + def start_traffic(self, multiplier, port_list):#, silent=True): + print format_text('{:<30}'.format("Start traffic:"), 'bold'), + try: + res_ok, log = self.stateless_client.start_traffic(multiplier, port_id=port_list) + if not self._silent: + print '' + self.prompt_response(log) + if not res_ok: + print format_text("[FAILED]\n", 'red', 'bold') + return False + print format_text("[SUCCESS]\n", 'green', 'bold') + return True + except ValueError as e: + print '' + print magenta(str(e)) + print format_text("[FAILED]\n", 'red', 'bold') + return False + + def attach_to_port(self, stream_pack_name, port_list): + print format_text('{:<30}'.format("Attaching traffic to ports:"), 'bold'), + stream_list = self.streams_db.get_stream_pack(stream_pack_name) #user_streams[args[0]] + if not stream_list: + print "Provided stream list name '{0}' doesn't exists.".format(stream_pack_name) + print format_text("[FAILED]\n", 'red', 'bold') + return + try: + res_ok, log = self.stateless_client.add_stream_pack(stream_list.compiled, port_id=port_list) + if not self._silent: + print '' + self.prompt_response(log) + if not res_ok: + print format_text("[FAILED]\n", 'red', 'bold') + return False + print format_text("[SUCCESS]\n", 'green', 'bold') + return True + except ValueError as e: + print '' + print magenta(str(e)) + print format_text("[FAILED]\n", 'red', 'bold') + return False + + def stop_traffic(self, port_list): + print format_text('{:<30}'.format("Stop traffic:"), 'bold'), + try: + res_ok, log = self.stateless_client.stop_traffic(port_id=port_list) + if not self._silent: + print '' + self.prompt_response(log) + if not res_ok: + print format_text("[FAILED]\n", 'red', 'bold') + return + print format_text("[SUCCESS]\n", 'green', 'bold') + return True + except ValueError as e: + print '' + print magenta(str(e)) + print format_text("[FAILED]\n", 'red', 'bold') + + def remove_all_streams(self, port_list): + '''Remove all streams from given port_list''' + print format_text('{:<30}'.format("Remove all streams:"), 'bold'), + try: + res_ok, log = self.stateless_client.remove_all_streams(port_id=port_list) + if not self._silent: + print '' + self.prompt_response(log) + if not res_ok: + print format_text("[FAILED]\n", 'red', 'bold') + return + print format_text("[SUCCESS]\n", 'green', 'bold') + return True + except ValueError as e: + print '' + print magenta(str(e)) + print format_text("[FAILED]\n", 'red', 'bold') + + + + + + def extract_port_list(self, opts): + if opts.all_ports or "all" in opts.ports: + # handling all ports + port_list = self.stateless_client.get_acquired_ports() + else: + port_list = self.extract_port_ids_from_list(opts.ports) + return port_list + + def decode_multiplier(self, opts_mult): + pass + + +class DebugTRexConsole(cmd.Cmd): + + def __init__(self, trex_main_console): + cmd.Cmd.__init__(self) + self.trex_console = trex_main_console + self.stateless_client = self.trex_console.stateless_client + self.streams_db = self.trex_console.streams_db + self.register_main_console_methods() + self.do_silent("on") + pass + + # ----- super methods overriding ----- # + def completenames(self, text, *ignored): + dotext = 'do_'+text + return [a[3:]+' ' for a in self.get_names() if a.startswith(dotext)] + + def get_names(self): + result = cmd.Cmd.get_names(self) + result += self.trex_console.get_names() + return list(set(result)) + + def register_main_console_methods(self): + main_names = set(self.trex_console.get_names()).difference(set(dir(self.__class__))) + for name in main_names: + for prefix in 'do_', 'help_', 'complete_': + if name.startswith(prefix): + self.__dict__[name] = getattr(self.trex_console, name) + + # if (name[:3] == 'do_') or (name[:5] == 'help_') or (name[:9] == 'complete_'): + # chosen.append(name) + # self.__dict__[name] = getattr(self.trex_console, name) + # # setattr(self, name, classmethod(getattr(self.trex_console, name))) + + # print chosen + # self.get_names() + + # return result + + + # ----- DEBUGGING methods ----- # + # set silent on / off + def do_silent(self, line): + '''Shows or set silent mode\n''' + if line == "": + print "\nsilent mode is " + ("on\n" if self.trex_console._silent else "off\n") + + elif line == "on": + self.verbose = True + self.stateless_client.set_verbose(True) + print green("\nsilent set to on\n") + + elif line == "off": + self.verbose = False + self.stateless_client.set_verbose(False) + print green("\nsilent set to off\n") + + else: + print magenta("\nplease specify 'on' or 'off'\n") + + def do_quit(self, line): + '''Exit the debug client back to main console\n''' + self.do_silent("off") + return True + + def do_start_traffic(self, line): + '''Start pre-submitted traffic in specified ports on TRex\n''' + # make sure that the user wants to acquire all + parser = parsing_opts.gen_parser("start_traffic", self.do_start_traffic.__doc__, + parsing_opts.PORT_LIST_WITH_ALL, parsing_opts.MULTIPLIER) + opts = parser.parse_args(line.split()) + # print opts + # return + if opts is None: + # avoid further processing in this command + return + try: + port_list = self.trex_console.extract_port_list(opts) + return self.trex_console.start_traffic(opts.mult, port_list) + except Exception as e: + print e + return + + def do_stop_traffic(self, line): + '''Stop active traffic in specified ports on TRex\n''' + parser = parsing_opts.gen_parser("stop_traffic", self.do_stop_traffic.__doc__, + parsing_opts.PORT_LIST_WITH_ALL) + opts = parser.parse_args(line.split()) + # print opts + # return + if opts is None: + # avoid further processing in this command + return + try: + port_list = self.trex_console.extract_port_list(opts) + return self.trex_console.stop_traffic(port_list) + except Exception as e: + print e + return + + + def complete_stop_traffic(self, text, line, begidx, endidx): + return self.port_auto_complete(text, line, begidx, endidx, active=True) + + # return + # # return + # # if not opts.port_list: + # # print magenta("Please provide a list of ports separated by spaces, " + # # "or specify 'all' to start traffic on all acquired ports") + # # return + # + + + return + args = line.split() + if len(args) < 1: + print magenta("Please provide a list of ports separated by spaces, " + "or specify 'all' to start traffic on all acquired ports") + return + if args[0] == "all": + ask = ConfirmMenu('Are you sure you want to start traffic at all acquired ports? ') + rc = ask.show() + if rc == False: + print yellow("[ABORTED]\n") + return + else: + port_list = self.stateless_client.get_acquired_ports() + else: + port_list = self.extract_port_ids_from_line(line) + + try: + res_ok, log = self.stateless_client.start_traffic(1.0, port_id=port_list) + self.prompt_response(log) + if not res_ok: + print format_text("[FAILED]\n", 'red', 'bold') + return + print format_text("[SUCCESS]\n", 'green', 'bold') + except ValueError as e: + print magenta(str(e)) + print format_text("[FAILED]\n", 'red', 'bold') + + def complete_start_traffic(self, text, line, begidx, endidx): + # return self.port_auto_complete(text, line, begidx, endidx) + return [text] + + def help_start_traffic(self): + self.do_start_traffic("-h") + + def help_stop_traffic(self): + self.do_stop_traffic("-h") + + # def do_help(self): + + def do_rpc (self, line): + '''Launches a RPC on the server\n''' + + if line == "": + print "\nUsage: [method name] [param dict as string]\n" + print "Example: rpc test_add {'x': 12, 'y': 17}\n" + return + + sp = line.split(' ', 1) + method = sp[0] + + params = None + bad_parse = False + if len(sp) > 1: + + try: + params = ast.literal_eval(sp[1]) + if not isinstance(params, dict): + bad_parse = True + + except ValueError as e1: + bad_parse = True + except SyntaxError as e2: + bad_parse = True + + if bad_parse: + print "\nValue should be a valid dict: '{0}'".format(sp[1]) + print "\nUsage: [method name] [param dict as string]\n" + print "Example: rpc test_add {'x': 12, 'y': 17}\n" + return + + res_ok, msg = self.stateless_client.transmit(method, params) + if res_ok: + print "\nServer Response:\n\n" + pretty_json(json.dumps(msg)) + "\n" + else: + print "\n*** " + msg + "\n" + #print "Please try 'reconnect' to reconnect to server" + + + def complete_rpc (self, text, line, begidx, endidx): + return [x + for x in self.trex_console.supported_rpc + if x.startswith(text)] + + # aliasing + do_exit = do_EOF = do_q = do_quit + +# diff --git a/scripts/automation/trex_control_plane/console/parsing_opts.py b/scripts/automation/trex_control_plane/console/parsing_opts.py index e701b7db..f983d837 100755 --- a/scripts/automation/trex_control_plane/console/parsing_opts.py +++ b/scripts/automation/trex_control_plane/console/parsing_opts.py @@ -2,6 +2,7 @@ import argparse from collections import namedtuple import sys import re +import os ArgumentPack = namedtuple('ArgumentPack', ['name_or_flags', 'options']) ArgumentGroup = namedtuple('ArgumentGroup', ['type', 'args', 'options']) @@ -14,9 +15,10 @@ ALL_PORTS = 3 PORT_LIST_WITH_ALL = 4 FILE_PATH = 5 FILE_FROM_DB = 6 -STREAM_FROM_PATH_OR_FILE = 7 -DURATION = 8 -FORCE = 9 +SERVER_IP = 7 +STREAM_FROM_PATH_OR_FILE = 8 +DURATION = 9 +FORCE = 10 # list of ArgumentGroup types MUTEX = 1 @@ -61,20 +63,27 @@ def match_multiplier(val): +def is_valid_file(filename): + if not os.path.isfile(filename): + raise argparse.ArgumentTypeError("The file '%s' does not exist" % filename) + + return filename + OPTIONS_DB = {MULTIPLIER: ArgumentPack(['-m', '--multiplier'], {'help': "Set multiplier for stream", 'dest': "mult", 'default': 1.0, 'type': match_multiplier}), + PORT_LIST: ArgumentPack(['--port'], {"nargs": '+', - # "action": "store_" 'dest':'ports', 'metavar': 'PORTS', - # 'type': int, + 'type': int, 'help': "A list of ports on which to apply the command", 'default': []}), + ALL_PORTS: ArgumentPack(['-a'], {"action": "store_true", "dest": "all_ports", @@ -88,15 +97,22 @@ OPTIONS_DB = {MULTIPLIER: ArgumentPack(['-m', '--multiplier'], {"action": "store_true", 'default': False, 'help': "Set if you want to stop active ports before applying new TRex run on them."}), + FILE_PATH: ArgumentPack(['-f'], - {'metavar': ('FILE', 'DB_NAME'), + {'metavar': 'FILE', 'dest': 'file', - 'nargs': 2, - 'help': "File path to YAML file that describes a stream pack. " - "Second argument is a name to store the loaded yaml file into db."}), + 'nargs': 1, + 'type': is_valid_file, + 'help': "File path to YAML file that describes a stream pack. "}), + FILE_FROM_DB: ArgumentPack(['--db'], {'metavar': 'LOADED_STREAM_PACK', 'help': "A stream pack which already loaded into console cache."}), + + SERVER_IP: ArgumentPack(['--server'], + {'metavar': 'SERVER', + 'help': "server IP"}), + # advanced options PORT_LIST_WITH_ALL: ArgumentGroup(MUTEX, [PORT_LIST, ALL_PORTS], @@ -109,33 +125,44 @@ OPTIONS_DB = {MULTIPLIER: ArgumentPack(['-m', '--multiplier'], class CCmdArgParser(argparse.ArgumentParser): - def __init__(self, *args, **kwargs): + def __init__(self, stateless_client, *args, **kwargs): super(CCmdArgParser, self).__init__(*args, **kwargs) - - # def exit(self, status=0, message=None): - # try: - # return super(CCmdArgParser, self).exit(status, message) # this will trigger system exit! - # except SystemExit: - # print "Caught system exit!!" - # return -1 - # # return + self.stateless_client = stateless_client def parse_args(self, args=None, namespace=None): try: - return super(CCmdArgParser, self).parse_args(args, namespace) + opts = super(CCmdArgParser, self).parse_args(args, namespace) + if opts is None: + return None + + if opts.all_ports: + opts.ports = self.stateless_client.get_port_ids() + + for port in opts.ports: + if not self.stateless_client._is_ports_valid(port): + self.error("port id {0} is not a valid\n".format(port)) + + return opts + except SystemExit: # recover from system exit scenarios, such as "help", or bad arguments. return None +def get_flags (opt): + return OPTIONS_DB[opt].name_or_flags -def gen_parser(op_name, description, *args): - parser = CCmdArgParser(prog=op_name, conflict_handler='resolve', - # add_help=False, +def gen_parser(stateless_client, op_name, description, *args): + parser = CCmdArgParser(stateless_client, prog=op_name, conflict_handler='resolve', description=description) for param in args: try: - argument = OPTIONS_DB[param] + + if isinstance(param, int): + argument = OPTIONS_DB[param] + else: + argument = param + if isinstance(argument, ArgumentGroup): if argument.type == MUTEX: # handle as mutually exclusive group @@ -157,5 +184,6 @@ def gen_parser(op_name, description, *args): raise KeyError("The attribute '{0}' is missing as a field of the {1} option.\n".format(cause, param)) return parser + if __name__ == "__main__": pass \ No newline at end of file diff --git a/scripts/automation/trex_control_plane/console/trex_console.py b/scripts/automation/trex_control_plane/console/trex_console.py index a61881a1..06ae762a 100755 --- a/scripts/automation/trex_control_plane/console/trex_console.py +++ b/scripts/automation/trex_control_plane/console/trex_console.py @@ -30,6 +30,7 @@ import tty, termios import trex_root_path from common.trex_streams import * from client.trex_stateless_client import CTRexStatelessClient +from client.trex_stateless_client import RpcResponseStatus from common.text_opts import * from client_utils.general_utils import user_input, get_current_user import parsing_opts @@ -40,43 +41,32 @@ __version__ = "1.0" LoadedStreamList = namedtuple('LoadedStreamList', ['loaded', 'compiled']) - -def readch(choices=[]): - - fd = sys.stdin.fileno() - old_settings = termios.tcgetattr(fd) - try: - tty.setraw(sys.stdin.fileno()) - while True: - ch = sys.stdin.read(1) - if (ord(ch) == 3) or (ord(ch) == 4): - return None - if ch in choices: - return ch - finally: - termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) - - return None - -class ConfirmMenu(object): - def __init__ (self, caption): - self.caption = "{cap} [confirm] : ".format(cap=caption) - - def show(self): - sys.stdout.write(self.caption) - input = user_input() - if input: - return False - else: - # user hit Enter - return True - - class CStreamsDB(object): def __init__(self): self.stream_packs = {} + def load_yaml_file (self, filename): + + stream_pack_name = filename + if stream_pack_name in self.get_loaded_streams_names(): + self.remove_stream_packs(stream_pack_name) + + stream_list = CStreamList() + loaded_obj = stream_list.load_yaml(filename) + + try: + compiled_streams = stream_list.compile_streams() + rc = self.load_streams(stream_pack_name, + LoadedStreamList(loaded_obj, + [StreamPack(v.stream_id, v.stream.dump()) + for k, v in compiled_streams.items()])) + + except Exception as e: + return None + + return self.get_stream_pack(stream_pack_name) + def load_streams(self, name, LoadedStreamList_obj): if name in self.stream_packs: return False @@ -98,84 +88,79 @@ class CStreamsDB(object): def get_loaded_streams_names(self): return self.stream_packs.keys() - def get_stream_pack(self, name): - return self.stream_packs.get(name) - - -# multi level cmd menu -class CmdMenu(object): - def __init__ (self): - self.menus = [] - - - def add_menu (self, caption, options): - menu = {} - menu['caption'] = caption - menu['options'] = options - self.menus.append(menu) - - def show (self): - cur_level = 0 - print "\n" - - selected_path = [] - for menu in self.menus: - # show all the options - print "{0}\n".format(menu['caption']) - for i, option in enumerate(menu['options']): - print "{0}. {1}".format(i + 1, option) - - #print "\nPlease select an option: " - - choices = range(0, len(menu['options'])) - choices = [ chr(x + 48) for x in choices] - - print "" - ch = readch(choices) - print "" - - if ch == None: - return None - - selected_path.append(int(ch) - 1) - - return selected_path + def stream_pack_exists (self, name): + return name in self.get_loaded_streams_names() + def get_stream_pack(self, name): + if not self.stream_pack_exists(name): + return None + else: + return self.stream_packs.get(name) -class AddStreamMenu(CmdMenu): - def __init__ (self): - super(AddStreamMenu, self).__init__() - self.add_menu('Please select type of stream', ['a', 'b', 'c']) - self.add_menu('Please select ISG', ['d', 'e', 'f']) +# # main console object class TRexConsole(cmd.Cmd): """Trex Console""" - def __init__(self, stateless_client, verbose): + def __init__(self, stateless_client, acquire_all_ports = True, verbose = False): cmd.Cmd.__init__(self) self.stateless_client = stateless_client + self.verbose = verbose + self.acquire_all_ports = acquire_all_ports + self.do_connect("") self.intro = "\n-=TRex Console v{ver}=-\n".format(ver=__version__) self.intro += "\nType 'help' or '?' for supported actions\n" - self.verbose = False - self._silent = True - self.postcmd(False, "") - self.user_streams = {} self.streams_db = CStreamsDB() + ################### internal section ######################## + # a cool hack - i stole this function and added space def completenames(self, text, *ignored): dotext = 'do_'+text return [a[3:]+' ' for a in self.get_names() if a.startswith(dotext)] + + def register_main_console_methods(self): + main_names = set(self.trex_console.get_names()).difference(set(dir(self.__class__))) + for name in main_names: + for prefix in 'do_', 'help_', 'complete_': + if name.startswith(prefix): + self.__dict__[name] = getattr(self.trex_console, name) + + def postcmd(self, stop, line): + if self.stateless_client.is_connected(): + self.prompt = "TRex > " + else: + self.supported_rpc = None + self.prompt = "TRex (offline) > " + + return stop + + def default(self, line): + print "'{0}' is an unrecognized command. type 'help' or '?' for a list\n".format(line) + + @staticmethod + def tree_autocomplete(text): + dir = os.path.dirname(text) + if dir: + path = dir + else: + path = "." + start_string = os.path.basename(text) + return [x + for x in os.listdir(path) + if x.startswith(start_string)] + + ####################### shell commands ####################### # set verbose on / off def do_verbose(self, line): @@ -196,169 +181,11 @@ class TRexConsole(cmd.Cmd): else: print magenta("\nplease specify 'on' or 'off'\n") - # query the server for registered commands - def do_query_server(self, line): - '''query the RPC server for supported remote commands\n''' - - res_ok, msg = self.stateless_client.get_supported_cmds() - if not res_ok: - print format_text("[FAILED]\n", 'red', 'bold') - return - print "\nRPC server supports the following commands:\n" - for func in msg: - if func: - print func - print '' - print format_text("[SUCCESS]\n", 'green', 'bold') - return - - def do_ping(self, line): - '''Pings the RPC server\n''' - - print "\n-> Pinging RPC server" - - res_ok, msg = self.stateless_client.ping() - if res_ok: - print format_text("[SUCCESS]\n", 'green', 'bold') - else: - print "\n*** " + msg + "\n" - return - - def do_force_acquire(self, line): - '''Acquires ports by force\n''' - - self.do_acquire(line, True) - - def complete_force_acquire(self, text, line, begidx, endidx): - return self.port_auto_complete(text, line, begidx, endidx, acquired=False) - - def extract_port_ids_from_line(self, line): - return {int(x) for x in line.split()} - - def extract_port_ids_from_list(self, port_list): - return {int(x) for x in port_list} - - def parse_ports_from_line (self, line): - port_list = set() - if line: - for port_id in line.split(' '): - if (not port_id.isdigit()) or (int(port_id) < 0) or (int(port_id) >= self.stateless_client.get_port_count()): - print "Please provide a list of ports separated by spaces between 0 and {0}".format(self.stateless_client.get_port_count() - 1) - return None - - port_list.add(int(port_id)) - - port_list = list(port_list) - - else: - port_list = [i for i in xrange(0, self.stateless_client.get_port_count())] - - return port_list - - - def do_acquire(self, line, force=False): - '''Acquire ports\n''' - - # make sure that the user wants to acquire all - args = line.split() - if len(args) < 1: - print magenta("Please provide a list of ports separated by spaces, or specify 'all' to acquire all available ports") - return - - if args[0] == "all": - ask = ConfirmMenu('Are you sure you want to acquire all ports ? ') - rc = ask.show() - if rc == False: - print yellow("[ABORTED]\n") - return - else: - port_list = self.stateless_client.get_port_ids() - else: - port_list = self.extract_port_ids_from_line(line) - - # rc, resp_list = self.stateless_client.take_ownership(port_list, force) - try: - res_ok, log = self.stateless_client.acquire(port_list, force) - self.prompt_response(log) - if not res_ok: - print format_text("[FAILED]\n", 'red', 'bold') - return - print format_text("[SUCCESS]\n", 'green', 'bold') - except ValueError as e: - print magenta(str(e)) - print format_text("[FAILED]\n", 'red', 'bold') - - - def port_auto_complete(self, text, line, begidx, endidx, acquired=True, active=False): - if acquired: - if not active: - ret_list = [x - for x in map(str, self.stateless_client.get_acquired_ports()) - if x.startswith(text)] - else: - ret_list = [x - for x in map(str, self.stateless_client.get_active_ports()) - if x.startswith(text)] - else: - ret_list = [x - for x in map(str, self.stateless_client.get_port_ids()) - if x.startswith(text)] - ret_list.append("all") - return ret_list - - - def complete_acquire(self, text, line, begidx, endidx): - return self.port_auto_complete(text, line, begidx, endidx, acquired=False) - - def do_release (self, line): - '''Release ports\n''' - - # if line: - # port_list = self.parse_ports_from_line(line) - # else: - # port_list = self.stateless_client.get_owned_ports() - args = line.split() - if len(args) < 1: - print "Please provide a list of ports separated by spaces, or specify 'all' to acquire all available ports" - if args[0] == "all": - ask = ConfirmMenu('Are you sure you want to release all acquired ports? ') - rc = ask.show() - if rc == False: - print yellow("[ABORTED]\n") - return - else: - port_list = self.stateless_client.get_acquired_ports() - else: - port_list = self.extract_port_ids_from_line(line) - - try: - res_ok, log = self.stateless_client.release(port_list) - self.prompt_response(log) - if not res_ok: - print format_text("[FAILED]\n", 'red', 'bold') - return - print format_text("[SUCCESS]\n", 'green', 'bold') - except ValueError as e: - print magenta(str(e)) - print format_text("[FAILED]\n", 'red', 'bold') - return - - def complete_release(self, text, line, begidx, endidx): - return self.port_auto_complete(text, line, begidx, endidx) - + ############### connect def do_connect (self, line): '''Connects to the server\n''' - if line == "": - res_ok, msg = self.stateless_client.connect() - else: - sp = line.split() - if (len(sp) != 2): - print "\n[usage] connect [server] [port] or without parameters\n" - return - - res_ok, msg = self.stateless_client.connect(sp[0], sp[1]) - + res_ok, msg = self.stateless_client.connect() if res_ok: print format_text("[SUCCESS]\n", 'green', 'bold') else: @@ -368,730 +195,175 @@ class TRexConsole(cmd.Cmd): self.supported_rpc = self.stateless_client.get_supported_cmds().data - # def do_rpc (self, line): - # '''Launches a RPC on the server\n''' - # - # if line == "": - # print "\nUsage: [method name] [param dict as string]\n" - # print "Example: rpc test_add {'x': 12, 'y': 17}\n" - # return - # - # sp = line.split(' ', 1) - # method = sp[0] - # - # params = None - # bad_parse = False - # if len(sp) > 1: - # - # try: - # params = ast.literal_eval(sp[1]) - # if not isinstance(params, dict): - # bad_parse = True - # - # except ValueError as e1: - # bad_parse = True - # except SyntaxError as e2: - # bad_parse = True - # - # if bad_parse: - # print "\nValue should be a valid dict: '{0}'".format(sp[1]) - # print "\nUsage: [method name] [param dict as string]\n" - # print "Example: rpc test_add {'x': 12, 'y': 17}\n" - # return - # - # res_ok, msg = self.stateless_client.transmit(method, params) - # if res_ok: - # print "\nServer Response:\n\n" + pretty_json(json.dumps(msg)) + "\n" - # else: - # print "\n*** " + msg + "\n" - # #print "Please try 'reconnect' to reconnect to server" - # - # - # def complete_rpc (self, text, line, begidx, endidx): - # return [x - # for x in self.supported_rpc - # if x.startswith(text)] - - def do_status (self, line): - '''Shows a graphical console\n''' - - if not self.stateless_client.is_connected(): - print "Not connected to server\n" - return - - self.do_verbose('off') - trex_status.show_trex_status(self.stateless_client) - - def do_quit(self, line): - '''Exit the client\n''' - return True + if self.acquire_all_ports: + res_ok, log = self.stateless_client.acquire(self.stateless_client.get_port_ids()) + if not res_ok: + print "\n*** Failed to acquire all ports... exiting...""" - def do_disconnect (self, line): - '''Disconnect from the server\n''' - if not self.stateless_client.is_connected(): - print "Not connected to server\n" + @staticmethod + def annotate (desc, rc = None, err_log = None, ext_err_msg = None): + print format_text('\n{:<40}'.format(desc), 'bold'), + if rc == None: + print "\n" return - res_ok, msg = self.stateless_client.disconnect() - if res_ok: - print format_text("[SUCCESS]\n", 'green', 'bold') - else: - print msg + "\n" - - def do_whoami (self, line): - '''Prints console user name\n''' - print "\n" + self.stateless_client.user + "\n" - - def postcmd(self, stop, line): - if self.stateless_client.is_connected(): - self.prompt = "TRex > " - else: - self.supported_rpc = None - self.prompt = "TRex (offline) > " - - return stop - - def default(self, line): - print "'{0}' is an unrecognized command. type 'help' or '?' for a list\n".format(line) + if rc == False: + # do we have a complex log object ? + if isinstance(err_log, list): + print "" + for func in err_log: + if func: + print func + print "" - # def do_help (self, line): - # '''Shows This Help Screen\n''' - # if line: - # try: - # func = getattr(self, 'help_' + line) - # except AttributeError: - # try: - # doc = getattr(self, 'do_' + line).__doc__ - # if doc: - # self.stdout.write("%s\n"%str(doc)) - # return - # except AttributeError: - # pass - # self.stdout.write("%s\n"%str(self.nohelp % (line,))) - # return - # func() - # return - # - # print "\nSupported Console Commands:" - # print "----------------------------\n" - # - # cmds = [x[3:] for x in self.get_names() if x.startswith("do_")] - # for cmd in cmds: - # if cmd == "EOF": - # continue - # - # try: - # doc = getattr(self, 'do_' + cmd).__doc__ - # if doc: - # help = str(doc) - # else: - # help = "*** Undocumented Function ***\n" - # except AttributeError: - # help = "*** Undocumented Function ***\n" - # - # print "{:<30} {:<30}".format(cmd + " - ", help) - - def do_stream_db_add(self, line): - '''Loads a YAML stream list serialization into user console \n''' - args = line.split() - if len(args) >= 2: - name = args[0] - yaml_path = args[1] - try: - multiplier = args[2] - except IndexError: - multiplier = 1 - stream_list = CStreamList() - loaded_obj = stream_list.load_yaml(yaml_path, multiplier) - # print self.stateless_client.pretty_json(json.dumps(loaded_obj)) - try: - compiled_streams = stream_list.compile_streams() - res_ok = self.streams_db.load_streams(name, LoadedStreamList(loaded_obj, - [StreamPack(v.stream_id, v.stream.dump()) - for k, v in compiled_streams.items()])) - if res_ok: - print green("Stream pack '{0}' loaded and added successfully\n".format(name)) - else: - print magenta("Picked name already exist. Please pick another name.\n") - except Exception as e: - print "adding new stream failed due to the following error:\n", str(e) - print format_text("[FAILED]\n", 'red', 'bold') - - return - else: - print magenta("please provide load name and YAML path, separated by space.\n" - "Optionally, you may provide a third argument to specify multiplier.\n") - - @staticmethod - def tree_autocomplete(text): - dir = os.path.dirname(text) - if dir: - path = dir - else: - path = "." - start_string = os.path.basename(text) - return [x - for x in os.listdir(path) - if x.startswith(start_string)] + elif isinstance(err_log, str): + print "\n" + err_log + "\n" + print format_text("[FAILED]\n", 'red', 'bold') + if ext_err_msg: + print format_text(ext_err_msg + "\n", 'blue', 'bold') - def complete_stream_db_add(self, text, line, begidx, endidx): - arg_num = len(line.split()) - 1 - if arg_num == 2: - return TRexConsole.tree_autocomplete(line.split()[-1]) - else: - return [text] - - def do_stream_db_show(self, line): - '''Shows the loaded stream list named [name] \n''' - args = line.split() - if args: - list_name = args[0] - try: - stream = self.streams_db.get_stream_pack(list_name)#user_streams[list_name] - if len(args) >= 2 and args[1] == "full": - print pretty_json(json.dumps(stream.compiled)) - else: - print pretty_json(json.dumps(stream.loaded)) - except KeyError as e: - print "Unknown stream list name provided" - else: - print "Available stream packs:\n{0}".format(', '.join(sorted(self.streams_db.get_loaded_streams_names()))) + return False - def complete_stream_db_show(self, text, line, begidx, endidx): - return [x - for x in self.streams_db.get_loaded_streams_names() - if x.startswith(text)] - - def do_stream_db_remove(self, line): - '''Removes a single loaded stream packs from loaded stream pack repository\n''' - args = line.split() - if args: - removed_streams = self.streams_db.remove_stream_packs(*args) - if removed_streams: - print green("The following stream packs were removed:") - print bold(", ".join(sorted(removed_streams))) - print format_text("[SUCCESS]\n", 'green', 'bold') - else: - print red("No streams were removed. Make sure to provide valid stream pack names.") else: - print magenta("Please provide stream pack name(s), separated with spaces.") - - def do_stream_db_clear(self, line): - '''Clears all loaded stream packs from loaded stream pack repository\n''' - self.streams_db.clear() - print format_text("[SUCCESS]\n", 'green', 'bold') + print format_text("[SUCCESS]\n", 'green', 'bold') + return True - def complete_stream_db_remove(self, text, line, begidx, endidx): - return [x - for x in self.streams_db.get_loaded_streams_names() - if x.startswith(text)] - - - def do_attach(self, line): - '''Assign loaded stream pack into specified ports on TRex\n''' - args = line.split() - if len(args) >= 2: - stream_pack_name = args[0] - stream_list = self.streams_db.get_stream_pack(stream_pack_name) #user_streams[args[0]] - if not stream_list: - print "Provided stream list name '{0}' doesn't exists.".format(stream_pack_name) - print format_text("[FAILED]\n", 'red', 'bold') - return - if args[1] == "all": - ask = ConfirmMenu('Are you sure you want to release all acquired ports? ') - rc = ask.show() - if rc == False: - print yellow("[ABORTED]\n") - return - else: - port_list = self.stateless_client.get_acquired_ports() - else: - port_list = self.extract_port_ids_from_line(' '.join(args[1:])) - owned = set(self.stateless_client.get_acquired_ports()) - try: - if set(port_list).issubset(owned): - res_ok, log = self.stateless_client.add_stream_pack(stream_list.compiled, port_id=port_list) - # res_ok, msg = self.stateless_client.add_stream(port_list, stream_list.compiled) - self.prompt_response(log) - if not res_ok: - print format_text("[FAILED]\n", 'red', 'bold') - return - print format_text("[SUCCESS]\n", 'green', 'bold') - return - else: - print "Not all desired ports are acquired.\n" \ - "Acquired ports are: {acq}\n" \ - "Requested ports: {req}\n" \ - "Missing ports: {miss}".format(acq=list(owned), - req=port_list, - miss=list(set(port_list).difference(owned))) - print format_text("[FAILED]\n", 'red', 'bold') - except ValueError as e: - print magenta(str(e)) - print format_text("[FAILED]\n", 'red', 'bold') - else: - print magenta("Please provide list name and ports to attach to, " - "or specify 'all' to attach all owned ports.\n") - - def complete_attach(self, text, line, begidx, endidx): - arg_num = len(line.split()) - 1 - if arg_num == 1: - # return optional streams packs - if line.endswith(" "): - return self.port_auto_complete(text, line, begidx, endidx) - return [x - for x in self.streams_db.get_loaded_streams_names() - if x.startswith(text)] - elif arg_num >= 2: - # return optional ports to attach to - return self.port_auto_complete(text, line, begidx, endidx) - else: - return [text] - - def prompt_response(self, response_obj): - resp_list = response_obj if isinstance(response_obj, list) else [response_obj] - def format_return_status(return_status): - if return_status: - return green("OK") - else: - return red("FAIL") - - for response in resp_list: - response_str = "{id:^3} - {msg} ({stat})".format(id=response.id, - msg=response.msg, - stat=format_return_status(response.success)) - print response_str - return + ############### start - def do_remove_all_streams(self, line): - '''Acquire ports\n''' + def complete_start(self, text, line, begidx, endidx): + s = line.split() + l = len(s) - # make sure that the user wants to acquire all - args = line.split() - if len(args) < 1: - print magenta("Please provide a list of ports separated by spaces, " - "or specify 'all' to remove from all acquired ports") - return - if args[0] == "all": - ask = ConfirmMenu('Are you sure you want to remove all stream packs from all acquired ports? ') - rc = ask.show() - if rc == False: - print yellow("[ABORTED]\n") - return - else: - port_list = self.stateless_client.get_acquired_ports() - else: - port_list = self.extract_port_ids_from_line(line) + file_flags = parsing_opts.get_flags(parsing_opts.FILE_PATH) - # rc, resp_list = self.stateless_client.take_ownership(port_list, force) - try: - res_ok, log = self.stateless_client.remove_all_streams(port_list) - self.prompt_response(log) - if not res_ok: - print format_text("[FAILED]\n", 'red', 'bold') - return - print format_text("[SUCCESS]\n", 'green', 'bold') - except ValueError as e: - print magenta(str(e)) - print format_text("[FAILED]\n", 'red', 'bold') + if (l > 1) and (s[l - 1] in file_flags): + return TRexConsole.tree_autocomplete("") - def complete_remove_all_streams(self, text, line, begidx, endidx): - return self.port_auto_complete(text, line, begidx, endidx) + if (l > 2) and (s[l - 2] in file_flags): + return TRexConsole.tree_autocomplete(s[l - 1]) def do_start(self, line): '''Start selected traffic in specified ports on TRex\n''' + # make sure that the user wants to acquire all - parser = parsing_opts.gen_parser("start", self.do_start.__doc__, + parser = parsing_opts.gen_parser(self.stateless_client, + "start", + self.do_start.__doc__, parsing_opts.PORT_LIST_WITH_ALL, parsing_opts.FORCE, parsing_opts.STREAM_FROM_PATH_OR_FILE, parsing_opts.DURATION, parsing_opts.MULTIPLIER) + opts = parser.parse_args(line.split()) + if opts is None: - # avoid further processing in this command - return - # print opts - port_list = self.extract_port_list(opts) - # print port_list - if opts.force: - # stop all active ports, if any - res_ok = self.stop_traffic(set(self.stateless_client.get_active_ports()).intersection(port_list)) - if not res_ok: - print yellow("[ABORTED]\n") - return - # remove all traffic from ports - res_ok = self.remove_all_streams(port_list) - if not res_ok: - print yellow("[ABORTED]\n") return - # decide which traffic to use - stream_pack_name = None + if opts.db: - # use pre-loaded traffic - print format_text('{:<30}'.format("Load stream pack (from DB):"), 'bold'), - if opts.db not in self.streams_db.get_loaded_streams_names(): - print format_text("[FAILED]\n", 'red', 'bold') - print yellow("[ABORTED]\n") + stream_list = self.stream_db.get_stream_pack(opts.db) + self.annotate("Load stream pack (from DB):", (stream_list != None)) + if stream_list == None: return - else: - stream_pack_name = opts.db + else: - # try loading a YAML file - print format_text('{:<30}'.format("Load stream pack (from file):"), 'bold'), - stream_list = CStreamList() - loaded_obj = stream_list.load_yaml(opts.file[0]) - # print self.stateless_client.pretty_json(json.dumps(loaded_obj)) - try: - compiled_streams = stream_list.compile_streams() - res_ok = self.streams_db.load_streams(opts.file[1], - LoadedStreamList(loaded_obj, - [StreamPack(v.stream_id, v.stream.dump()) - for k, v in compiled_streams.items()])) - if not res_ok: - print format_text("[FAILED]\n", 'red', 'bold') - print yellow("[ABORTED]\n") - return - print format_text("[SUCCESS]\n", 'green', 'bold') - stream_pack_name = opts.file[1] - except Exception as e: - print format_text("[FAILED]\n", 'red', 'bold') - print yellow("[ABORTED]\n") - res_ok = self.attach_to_port(stream_pack_name, port_list) - if not res_ok: - print yellow("[ABORTED]\n") - return - # finally, start the traffic - res_ok = self.start_traffic(opts.mult, port_list) - if not res_ok: - print yellow("[ABORTED]\n") - return + # load streams from file + stream_list = self.streams_db.load_yaml_file(opts.file[0]) + self.annotate("Load stream pack (from file):", (stream_list != None)) + if stream_list == None: + return + + + self.stateless_client.cmd_start(opts.ports, stream_list, opts.mult, opts.force, self.annotate) return + def help_start(self): self.do_start("-h") + ############# stop def do_stop(self, line): '''Stop active traffic in specified ports on TRex\n''' - parser = parsing_opts.gen_parser("stop", self.do_stop.__doc__, + parser = parsing_opts.gen_parser(self.stateless_client, + "stop", + self.do_stop.__doc__, parsing_opts.PORT_LIST_WITH_ALL) + opts = parser.parse_args(line.split()) if opts is None: - # avoid further processing in this command return - port_list = self.extract_port_list(opts) - res_ok = self.stop_traffic(port_list) - return + self.stateless_client.cmd_stop(opts.ports, self.annotate) + return def help_stop(self): self.do_stop("-h") + ########## reset + def do_reset (self, line): + '''force stop all ports\n''' + self.stateless_client.cmd_reset(self.annotate) - def do_debug(self, line): - '''Enter DEBUG mode of the console to invoke smaller building blocks with server''' - i = DebugTRexConsole(self) - i.prompt = self.prompt[:-3] + ':' + blue('debug') + ' > ' - i.cmdloop() - - # aliasing - do_exit = do_EOF = do_q = do_quit - - # ----- utility methods ----- # - - def start_traffic(self, multiplier, port_list):#, silent=True): - print format_text('{:<30}'.format("Start traffic:"), 'bold'), - try: - res_ok, log = self.stateless_client.start_traffic(multiplier, port_id=port_list) - if not self._silent: - print '' - self.prompt_response(log) - if not res_ok: - print format_text("[FAILED]\n", 'red', 'bold') - return False - print format_text("[SUCCESS]\n", 'green', 'bold') - return True - except ValueError as e: - print '' - print magenta(str(e)) - print format_text("[FAILED]\n", 'red', 'bold') - return False + + # tui + def do_tui (self, line): + '''Shows a graphical console\n''' - def attach_to_port(self, stream_pack_name, port_list): - print format_text('{:<30}'.format("Attaching traffic to ports:"), 'bold'), - stream_list = self.streams_db.get_stream_pack(stream_pack_name) #user_streams[args[0]] - if not stream_list: - print "Provided stream list name '{0}' doesn't exists.".format(stream_pack_name) - print format_text("[FAILED]\n", 'red', 'bold') + if not self.stateless_client.is_connected(): + print "Not connected to server\n" return - try: - res_ok, log = self.stateless_client.add_stream_pack(stream_list.compiled, port_id=port_list) - if not self._silent: - print '' - self.prompt_response(log) - if not res_ok: - print format_text("[FAILED]\n", 'red', 'bold') - return False - print format_text("[SUCCESS]\n", 'green', 'bold') - return True - except ValueError as e: - print '' - print magenta(str(e)) - print format_text("[FAILED]\n", 'red', 'bold') - return False - - def stop_traffic(self, port_list): - print format_text('{:<30}'.format("Stop traffic:"), 'bold'), - try: - res_ok, log = self.stateless_client.stop_traffic(port_id=port_list) - if not self._silent: - print '' - self.prompt_response(log) - if not res_ok: - print format_text("[FAILED]\n", 'red', 'bold') - return - print format_text("[SUCCESS]\n", 'green', 'bold') - return True - except ValueError as e: - print '' - print magenta(str(e)) - print format_text("[FAILED]\n", 'red', 'bold') - - def remove_all_streams(self, port_list): - '''Remove all streams from given port_list''' - print format_text('{:<30}'.format("Remove all streams:"), 'bold'), - try: - res_ok, log = self.stateless_client.remove_all_streams(port_id=port_list) - if not self._silent: - print '' - self.prompt_response(log) - if not res_ok: - print format_text("[FAILED]\n", 'red', 'bold') - return - print format_text("[SUCCESS]\n", 'green', 'bold') - return True - except ValueError as e: - print '' - print magenta(str(e)) - print format_text("[FAILED]\n", 'red', 'bold') - - - - - - def extract_port_list(self, opts): - if opts.all_ports or "all" in opts.ports: - # handling all ports - port_list = self.stateless_client.get_acquired_ports() - else: - port_list = self.extract_port_ids_from_list(opts.ports) - return port_list - - def decode_multiplier(self, opts_mult): - pass - - -class DebugTRexConsole(cmd.Cmd): - - def __init__(self, trex_main_console): - cmd.Cmd.__init__(self) - self.trex_console = trex_main_console - self.stateless_client = self.trex_console.stateless_client - self.streams_db = self.trex_console.streams_db - self.register_main_console_methods() - self.do_silent("on") - pass - - # ----- super methods overriding ----- # - def completenames(self, text, *ignored): - dotext = 'do_'+text - return [a[3:]+' ' for a in self.get_names() if a.startswith(dotext)] - def get_names(self): - result = cmd.Cmd.get_names(self) - result += self.trex_console.get_names() - return list(set(result)) - - def register_main_console_methods(self): - main_names = set(self.trex_console.get_names()).difference(set(dir(self.__class__))) - for name in main_names: - for prefix in 'do_', 'help_', 'complete_': - if name.startswith(prefix): - self.__dict__[name] = getattr(self.trex_console, name) - - # if (name[:3] == 'do_') or (name[:5] == 'help_') or (name[:9] == 'complete_'): - # chosen.append(name) - # self.__dict__[name] = getattr(self.trex_console, name) - # # setattr(self, name, classmethod(getattr(self.trex_console, name))) - - # print chosen - # self.get_names() - - # return result - - - # ----- DEBUGGING methods ----- # - # set silent on / off - def do_silent(self, line): - '''Shows or set silent mode\n''' - if line == "": - print "\nsilent mode is " + ("on\n" if self.trex_console._silent else "off\n") - - elif line == "on": - self.verbose = True - self.stateless_client.set_verbose(True) - print green("\nsilent set to on\n") - - elif line == "off": - self.verbose = False - self.stateless_client.set_verbose(False) - print green("\nsilent set to off\n") - - else: - print magenta("\nplease specify 'on' or 'off'\n") + self.do_verbose('off') + trex_status.show_trex_status(self.stateless_client) + # quit function def do_quit(self, line): - '''Exit the debug client back to main console\n''' - self.do_silent("off") + '''Exit the client\n''' return True - def do_start_traffic(self, line): - '''Start pre-submitted traffic in specified ports on TRex\n''' - # make sure that the user wants to acquire all - parser = parsing_opts.gen_parser("start_traffic", self.do_start_traffic.__doc__, - parsing_opts.PORT_LIST_WITH_ALL, parsing_opts.MULTIPLIER) - opts = parser.parse_args(line.split()) - # print opts - # return - if opts is None: - # avoid further processing in this command - return - try: - port_list = self.trex_console.extract_port_list(opts) - return self.trex_console.start_traffic(opts.mult, port_list) - except Exception as e: - print e - return - - def do_stop_traffic(self, line): - '''Stop active traffic in specified ports on TRex\n''' - parser = parsing_opts.gen_parser("stop_traffic", self.do_stop_traffic.__doc__, - parsing_opts.PORT_LIST_WITH_ALL) - opts = parser.parse_args(line.split()) - # print opts - # return - if opts is None: - # avoid further processing in this command - return - try: - port_list = self.trex_console.extract_port_list(opts) - return self.trex_console.stop_traffic(port_list) - except Exception as e: - print e - return - - - def complete_stop_traffic(self, text, line, begidx, endidx): - return self.port_auto_complete(text, line, begidx, endidx, active=True) - - # return - # # return - # # if not opts.port_list: - # # print magenta("Please provide a list of ports separated by spaces, " - # # "or specify 'all' to start traffic on all acquired ports") - # # return - # - - - return - args = line.split() - if len(args) < 1: - print magenta("Please provide a list of ports separated by spaces, " - "or specify 'all' to start traffic on all acquired ports") - return - if args[0] == "all": - ask = ConfirmMenu('Are you sure you want to start traffic at all acquired ports? ') - rc = ask.show() - if rc == False: - print yellow("[ABORTED]\n") - return - else: - port_list = self.stateless_client.get_acquired_ports() - else: - port_list = self.extract_port_ids_from_line(line) - - try: - res_ok, log = self.stateless_client.start_traffic(1.0, port_id=port_list) - self.prompt_response(log) - if not res_ok: - print format_text("[FAILED]\n", 'red', 'bold') - return - print format_text("[SUCCESS]\n", 'green', 'bold') - except ValueError as e: - print magenta(str(e)) - print format_text("[FAILED]\n", 'red', 'bold') - - def complete_start_traffic(self, text, line, begidx, endidx): - # return self.port_auto_complete(text, line, begidx, endidx) - return [text] - - def help_start_traffic(self): - self.do_start_traffic("-h") - - def help_stop_traffic(self): - self.do_stop_traffic("-h") - - # def do_help(self): - - def do_rpc (self, line): - '''Launches a RPC on the server\n''' + + def do_help (self, line): + '''Shows This Help Screen\n''' + if line: + try: + func = getattr(self, 'help_' + line) + except AttributeError: + try: + doc = getattr(self, 'do_' + line).__doc__ + if doc: + self.stdout.write("%s\n"%str(doc)) + return + except AttributeError: + pass + self.stdout.write("%s\n"%str(self.nohelp % (line,))) + return + func() + return + + print "\nSupported Console Commands:" + print "----------------------------\n" + + cmds = [x[3:] for x in self.get_names() if x.startswith("do_")] + for cmd in cmds: + if ( (cmd == "EOF") or (cmd == "q") or (cmd == "exit")): + continue + + try: + doc = getattr(self, 'do_' + cmd).__doc__ + if doc: + help = str(doc) + else: + help = "*** Undocumented Function ***\n" + except AttributeError: + help = "*** Undocumented Function ***\n" + + print "{:<30} {:<30}".format(cmd + " - ", help) - if line == "": - print "\nUsage: [method name] [param dict as string]\n" - print "Example: rpc test_add {'x': 12, 'y': 17}\n" - return - - sp = line.split(' ', 1) - method = sp[0] - - params = None - bad_parse = False - if len(sp) > 1: - - try: - params = ast.literal_eval(sp[1]) - if not isinstance(params, dict): - bad_parse = True - - except ValueError as e1: - bad_parse = True - except SyntaxError as e2: - bad_parse = True - - if bad_parse: - print "\nValue should be a valid dict: '{0}'".format(sp[1]) - print "\nUsage: [method name] [param dict as string]\n" - print "Example: rpc test_add {'x': 12, 'y': 17}\n" - return - - res_ok, msg = self.stateless_client.transmit(method, params) - if res_ok: - print "\nServer Response:\n\n" + pretty_json(json.dumps(msg)) + "\n" - else: - print "\n*** " + msg + "\n" - #print "Please try 'reconnect' to reconnect to server" - - - def complete_rpc (self, text, line, begidx, endidx): - return [x - for x in self.trex_console.supported_rpc - if x.startswith(text)] - - # aliasing do_exit = do_EOF = do_q = do_quit + def setParserOptions(): parser = argparse.ArgumentParser(prog="trex_console.py") @@ -1099,12 +371,12 @@ def setParserOptions(): default = "localhost", type = str) - parser.add_argument("-p", "--port", help = "TRex Server Port [default is 4505]\n", - default = 4505, + parser.add_argument("-p", "--port", help = "TRex Server Port [default is 5505]\n", + default = 5505, type = int) - parser.add_argument("--async_port", help = "TRex ASync Publisher Port [default is 4506]\n", - default = 4506, + parser.add_argument("--async_port", help = "TRex ASync Publisher Port [default is 4505]\n", + default = 4505, dest='pub', type = int) @@ -1116,6 +388,11 @@ def setParserOptions(): action="store_true", help="Switch ON verbose option. Default is: OFF.", default = False) + + parser.add_argument("--no_acquire", dest="acquire", + action="store_false", help="Acquire all ports on connect. Default is: ON.", + default = True) + return parser @@ -1128,7 +405,7 @@ def main(): # console try: - console = TRexConsole(stateless_client, options.verbose) + console = TRexConsole(stateless_client, options.acquire, options.verbose) console.cmdloop() except KeyboardInterrupt as e: print "\n\n*** Caught Ctrl + C... Exiting...\n\n" diff --git a/scripts/automation/trex_control_plane/console/trex_status.py b/scripts/automation/trex_control_plane/console/trex_status.py index 2b97d7d3..a54b718e 100644 --- a/scripts/automation/trex_control_plane/console/trex_status.py +++ b/scripts/automation/trex_control_plane/console/trex_status.py @@ -168,7 +168,8 @@ class PortsStatsPanel(TrexStatusPanel): port_stats.get_rel("ibytes", format = True, suffix = "B")))) else: - self.getwin().addstr(5 + (i * 4), 2, 2, "{:^15} {:^30} {:^30} {:^30}".format( + + self.getwin().addstr(5 + (i * 4), 2, "{:^15} {:^30} {:^30} {:^30}".format( "{0} ({1})".format(str(port_index), self.status_obj.server_sys_info["ports"][port_index]["speed"]), "N/A", "N/A", @@ -284,7 +285,7 @@ class SinglePortPanel(TrexStatusPanel): port_stats.get_rel("ibytes", format = True, suffix = "B")))) else: - self.getwin().addstr(y + (i * 4), 2, 2, "{:^15} {:^30} {:^30} {:^30}".format( + self.getwin().addstr(y, 2, "{:^15} {:^30} {:^30} {:^30}".format( "{0} ({1})".format(str(self.port_id), self.status_obj.server_sys_info["ports"][self.port_id]["speed"]), "N/A", "N/A", diff --git a/src/rpc-server/trex_rpc_req_resp_server.h b/src/rpc-server/trex_rpc_req_resp_server.h index 1f638adf..bc38c0ef 100644 --- a/src/rpc-server/trex_rpc_req_resp_server.h +++ b/src/rpc-server/trex_rpc_req_resp_server.h @@ -43,7 +43,7 @@ private: void handle_request(const std::string &request); void handle_server_error(const std::string &specific_err); - static const int RPC_MAX_MSG_SIZE = (20 * 1024); + static const int RPC_MAX_MSG_SIZE = (200 * 1024); void *m_context; void *m_socket; uint8_t m_msg_buffer[RPC_MAX_MSG_SIZE]; diff --git a/src/stateless/cp/trex_stateless_port.cpp b/src/stateless/cp/trex_stateless_port.cpp index b9206775..907b9cf4 100644 --- a/src/stateless/cp/trex_stateless_port.cpp +++ b/src/stateless/cp/trex_stateless_port.cpp @@ -122,7 +122,9 @@ TrexStatelessPort::start_traffic(double mul) { void TrexStatelessPort::stop_traffic(void) { - verify_state(PORT_STATE_TX); + if (m_port_state != PORT_STATE_TX) { + return; + } /* generate a message to all the relevant DP cores to start transmitting */ TrexStatelessCpToDpMsgBase *stop_msg = new TrexStatelessDpStop(m_port_id); -- cgit 1.2.3-korg From 45b71cff9d0465b77f82e4cd40b64a9f3183c1c7 Mon Sep 17 00:00:00 2001 From: Hanoh Haim Date: Thu, 12 Nov 2015 15:33:30 +0200 Subject: refactor stream object --- .../trex_control_plane/client/trex_async_client.py | 3 +- .../trex_control_plane/console/trex_console.py | 8 +- src/gtest/trex_stateless_gtest.cpp | 34 ++++-- src/platform_cfg.cpp | 2 + src/platform_cfg.h | 2 +- src/rpc-server/commands/trex_rpc_cmd_stream.cpp | 14 ++- src/stateless/cp/trex_stream.cpp | 9 +- src/stateless/cp/trex_stream.h | 132 ++++++++++++--------- src/stateless/cp/trex_streams_compiler.cpp | 7 +- src/stateless/dp/trex_stateless_dp_core.cpp | 18 +-- 10 files changed, 138 insertions(+), 91 deletions(-) (limited to 'scripts/automation/trex_control_plane/console') 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 72cce5aa..d13513bf 100644 --- a/scripts/automation/trex_control_plane/client/trex_async_client.py +++ b/scripts/automation/trex_control_plane/client/trex_async_client.py @@ -181,7 +181,8 @@ class CTRexAsyncClient(): self.socket.setsockopt(zmq.SUBSCRIBE, '') while self.active: - msg = json.loads(self.socket.recv_string()) + line = self.socket.recv_string(); + msg = json.loads(line) key = msg['name'] self.raw_snapshot[key] = msg['data'] diff --git a/scripts/automation/trex_control_plane/console/trex_console.py b/scripts/automation/trex_control_plane/console/trex_console.py index 06ae762a..5470e694 100755 --- a/scripts/automation/trex_control_plane/console/trex_console.py +++ b/scripts/automation/trex_control_plane/console/trex_console.py @@ -371,12 +371,12 @@ def setParserOptions(): default = "localhost", type = str) - parser.add_argument("-p", "--port", help = "TRex Server Port [default is 5505]\n", - default = 5505, + parser.add_argument("-p", "--port", help = "TRex Server Port [default is 4501]\n", + default = 4501, type = int) - parser.add_argument("--async_port", help = "TRex ASync Publisher Port [default is 4505]\n", - default = 4505, + parser.add_argument("--async_port", help = "TRex ASync Publisher Port [default is 4500]\n", + default = 4500, dest='pub', type = int) diff --git a/src/gtest/trex_stateless_gtest.cpp b/src/gtest/trex_stateless_gtest.cpp index 1ea0373c..8b96ef88 100644 --- a/src/gtest/trex_stateless_gtest.cpp +++ b/src/gtest/trex_stateless_gtest.cpp @@ -237,7 +237,9 @@ TEST_F(basic_stl, single_pkt_burst1) { std::vector streams; - TrexStream * stream1 = new TrexStreamBurst(0,0,5, 1.0); + TrexStream * stream1 = new TrexStream(TrexStream::stSINGLE_BURST, 0,0); + stream1->set_pps(1.0); + stream1->set_signle_burtst(5); stream1->m_enabled = true; stream1->m_self_start = true; @@ -281,7 +283,10 @@ TEST_F(basic_stl, single_pkt) { std::vector streams; - TrexStream * stream1 = new TrexStreamContinuous(0,0,1.0); + TrexStream * stream1 = new TrexStream(TrexStream::stCONTINUOUS,0,0); + stream1->set_pps(1.0); + + stream1->m_enabled = true; stream1->m_self_start = true; @@ -327,7 +332,10 @@ TEST_F(basic_stl, multi_pkt1) { std::vector streams; - TrexStream * stream1 = new TrexStreamContinuous(0,0,1.0); + TrexStream * stream1 = new TrexStream(TrexStream::stCONTINUOUS,0,0); + stream1->set_pps(1.0); + + stream1->m_enabled = true; stream1->m_self_start = true; @@ -338,8 +346,9 @@ TEST_F(basic_stl, multi_pkt1) { streams.push_back(stream1); + TrexStream * stream2 = new TrexStream(TrexStream::stCONTINUOUS,0,0); + stream2->set_pps(2.0); - TrexStream * stream2 = new TrexStreamContinuous(0,1,2.0); stream2->m_enabled = true; stream2->m_self_start = true; stream2->m_isg_usec = 1000.0; /* 1 msec */ @@ -385,7 +394,11 @@ TEST_F(basic_stl, multi_pkt2) { std::vector streams; - TrexStream * stream1 = new TrexStreamContinuous(0,0,1.0); + + TrexStream * stream1 = new TrexStream(TrexStream::stCONTINUOUS,0,0); + stream1->set_pps(1.0); + + stream1->m_enabled = true; stream1->m_self_start = true; @@ -397,7 +410,9 @@ TEST_F(basic_stl, multi_pkt2) { streams.push_back(stream1); - TrexStream * stream2 = new TrexStreamContinuous(0,1,2.0); + TrexStream * stream2 = new TrexStream(TrexStream::stCONTINUOUS,0,1); + stream2->set_pps(2.0); + stream2->m_enabled = false; stream2->m_self_start = false; stream2->m_isg_usec = 1000.0; /* 1 msec */ @@ -439,9 +454,12 @@ TEST_F(basic_stl, multi_burst1) { std::vector streams; + TrexStream * stream1 = new TrexStream(TrexStream::stMULTI_BURST,0,0); + stream1->set_pps(1.0); + stream1->set_multi_burst(5, + 3, + 2000000.0); - - TrexStream * stream1 = new TrexStreamMultiBurst(0,0,5, 1.0,3,2000000.0); stream1->m_enabled = true; stream1->m_self_start = true; diff --git a/src/platform_cfg.cpp b/src/platform_cfg.cpp index df04cd89..547cc3ad 100755 --- a/src/platform_cfg.cpp +++ b/src/platform_cfg.cpp @@ -414,7 +414,9 @@ void CPlatformYamlInfo::Dump(FILE *fd){ } if ( m_telnet_exist ){ fprintf(fd," telnet_port : %d \n",m_telnet_port); + } + fprintf(fd," m_zmq_rpc_port : %d \n",m_zmq_rpc_port); if ( m_mac_info_exist ){ int i; diff --git a/src/platform_cfg.h b/src/platform_cfg.h index b4b03b10..4fc3c3dd 100755 --- a/src/platform_cfg.h +++ b/src/platform_cfg.h @@ -180,11 +180,11 @@ public: m_enable_zmq_pub_exist=false; m_enable_zmq_pub=true; m_zmq_pub_port=4500; + m_zmq_rpc_port = 4501; m_telnet_exist=false; m_telnet_port=4502 ; - m_zmq_rpc_port = 5050; m_mac_info_exist=false; m_port_bandwidth_gb = 10; diff --git a/src/rpc-server/commands/trex_rpc_cmd_stream.cpp b/src/rpc-server/commands/trex_rpc_cmd_stream.cpp index fffc800a..e32073b0 100644 --- a/src/rpc-server/commands/trex_rpc_cmd_stream.cpp +++ b/src/rpc-server/commands/trex_rpc_cmd_stream.cpp @@ -140,14 +140,18 @@ TrexRpcCmdAddStream::allocate_new_stream(const Json::Value §ion, uint8_t por if (type == "continuous") { double pps = parse_double(mode, "pps", result); - stream = new TrexStreamContinuous(port_id, stream_id, pps); + stream = new TrexStream( TrexStream::stCONTINUOUS, port_id, stream_id); + stream->set_pps(pps); } else if (type == "single_burst") { uint32_t total_pkts = parse_int(mode, "total_pkts", result); double pps = parse_double(mode, "pps", result); - stream = new TrexStreamBurst(port_id, stream_id, total_pkts, pps); + stream = new TrexStream(TrexStream::stSINGLE_BURST,port_id, stream_id); + stream->set_pps(pps); + stream->set_signle_burtst(total_pkts); + } else if (type == "multi_burst") { @@ -156,8 +160,10 @@ TrexRpcCmdAddStream::allocate_new_stream(const Json::Value §ion, uint8_t por uint32_t num_bursts = parse_int(mode, "number_of_bursts", result); uint32_t pkts_per_burst = parse_int(mode, "pkts_per_burst", result); - stream = new TrexStreamMultiBurst(port_id, stream_id, pkts_per_burst, pps, num_bursts, ibg_usec); - + stream = new TrexStream(TrexStream::stMULTI_BURST,port_id, stream_id ); + stream->set_pps(pps); + stream->set_multi_burst(pkts_per_burst,num_bursts,ibg_usec); + } else { generate_parse_err(result, "bad stream type provided: '" + type + "'"); diff --git a/src/stateless/cp/trex_stream.cpp b/src/stateless/cp/trex_stream.cpp index ba306137..1a05257c 100644 --- a/src/stateless/cp/trex_stream.cpp +++ b/src/stateless/cp/trex_stream.cpp @@ -25,9 +25,11 @@ limitations under the License. /************************************** * stream *************************************/ -TrexStream::TrexStream(uint8_t port_id, uint32_t stream_id) : m_port_id(port_id), m_stream_id(stream_id) { +TrexStream::TrexStream(uint8_t type, + uint8_t port_id, uint32_t stream_id) : m_port_id(port_id), m_stream_id(stream_id) { /* default values */ + m_type = type; m_isg_usec = 0; m_next_stream_id = -1; m_enabled = false; @@ -38,6 +40,11 @@ TrexStream::TrexStream(uint8_t port_id, uint32_t stream_id) : m_port_id(port_id) m_rx_check.m_enable = false; + + m_pps=-1.0; + m_burst_total_pkts=0; + m_num_bursts=1; + m_ibg_usec=0.0; } TrexStream::~TrexStream() { diff --git a/src/stateless/cp/trex_stream.h b/src/stateless/cp/trex_stream.h index 5e6ac19a..151723ad 100644 --- a/src/stateless/cp/trex_stream.h +++ b/src/stateless/cp/trex_stream.h @@ -29,9 +29,28 @@ limitations under the License. #include #include +#include +#include class TrexRpcCmdAddStream; + +struct CStreamPktData { + uint8_t *binary; + uint16_t len; + + std::string meta; + +public: + inline void clone(uint8_t * in_binary, + uint32_t in_pkt_size){ + binary = new uint8_t[in_pkt_size]; + len = in_pkt_size; + memcpy(binary,in_binary,in_pkt_size); + } +}; + + /** * Stateless Stream * @@ -48,7 +67,7 @@ public: public: - TrexStream(uint8_t port_id, uint32_t stream_id); + TrexStream(uint8_t type,uint8_t port_id, uint32_t stream_id); virtual ~TrexStream(); /* defines the min max per packet supported */ @@ -65,6 +84,53 @@ public: return m_pps; } + void set_pps(double pps){ + m_pps = pps; + } + + void set_type(uint8_t type){ + m_type = type; + } + + uint8_t get_type(void){ + return ( m_type ); + } + + + + void set_multi_burst(uint32_t burst_total_pkts, + uint32_t num_bursts, + double ibg_usec){ + m_burst_total_pkts = burst_total_pkts; + m_num_bursts = num_bursts; + m_ibg_usec = ibg_usec; + } + + void set_signle_burtst(uint32_t burst_total_pkts){ + set_multi_burst(burst_total_pkts,1,0.0); + } + + /* create new stream */ + TrexStream * clone_as_dp(){ + TrexStream * dp=new TrexStream(m_type,m_port_id,m_stream_id); + + + dp->m_isg_usec = m_isg_usec; + dp->m_next_stream_id = m_next_stream_id; + + dp->m_enabled = m_enabled; + dp->m_self_start = m_self_start; + + /* deep copy */ + dp->m_pkt.clone(m_pkt.binary,m_pkt.len); + + dp->m_rx_check = m_rx_check; + dp->m_pps = m_pps; + dp->m_burst_total_pkts = m_burst_total_pkts; + dp->m_num_bursts = m_num_bursts; + dp->m_ibg_usec = m_ibg_usec ; + return (dp); + } public: /* basic */ @@ -80,13 +146,9 @@ public: /* indicators */ bool m_enabled; bool m_self_start; - + + CStreamPktData m_pkt; /* pkt */ - struct { - uint8_t *binary; - uint16_t len; - std::string meta; - } m_pkt; /* VM */ StreamVm m_vm; @@ -102,63 +164,17 @@ public: double m_pps; + uint32_t m_burst_total_pkts; /* valid in case of burst stSINGLE_BURST,stMULTI_BURST*/ - /* original template provided by requester */ - Json::Value m_stream_json; - - -}; + uint32_t m_num_bursts; /* valid in case of stMULTI_BURST */ -/** - * continuous stream - * - */ -class TrexStreamContinuous : public TrexStream { -public: - TrexStreamContinuous(uint8_t port_id, uint32_t stream_id, double pps) : TrexStream(port_id, stream_id) { - m_type= TrexStream::stCONTINUOUS; - m_pps=pps; - } -}; - -/** - * single burst - * - */ -class TrexStreamBurst : public TrexStream { -public: - TrexStreamBurst(uint8_t port_id, uint32_t stream_id, uint32_t total_pkts, double pps) : - TrexStream(port_id, stream_id), - m_total_pkts(total_pkts){ - m_type= TrexStream::stSINGLE_BURST; - m_pps=pps; + double m_ibg_usec; /* valid in case of stMULTI_BURST */ - } + /* original template provided by requester */ + Json::Value m_stream_json; -public: - uint32_t m_total_pkts; }; -/** - * multi burst - * - */ -class TrexStreamMultiBurst : public TrexStreamBurst { -public: - TrexStreamMultiBurst(uint8_t port_id, - uint32_t stream_id, - uint32_t pkts_per_burst, - double pps, - uint32_t num_bursts, - double ibg_usec) : TrexStreamBurst(port_id, stream_id, pkts_per_burst, pps), m_num_bursts(num_bursts), m_ibg_usec(ibg_usec) { - m_type= TrexStream::stMULTI_BURST; - } - -public: - uint32_t m_num_bursts; - double m_ibg_usec; - -}; /** * holds all the streams diff --git a/src/stateless/cp/trex_streams_compiler.cpp b/src/stateless/cp/trex_streams_compiler.cpp index c7b881c5..80cdb31c 100644 --- a/src/stateless/cp/trex_streams_compiler.cpp +++ b/src/stateless/cp/trex_streams_compiler.cpp @@ -27,18 +27,21 @@ limitations under the License. * stream compiled object *************************************/ TrexStreamsCompiledObj::TrexStreamsCompiledObj(uint8_t port_id, double mul) : m_port_id(port_id), m_mul(mul) { + m_duration_sim=-1.0; } TrexStreamsCompiledObj::~TrexStreamsCompiledObj() { + for (auto obj : m_objs) { + delete obj.m_stream; + } m_objs.clear(); - m_duration_sim=-1.0; } void TrexStreamsCompiledObj::add_compiled_stream(TrexStream * stream) { obj_st obj; - obj.m_stream = stream; + obj.m_stream = stream->clone_as_dp(); m_objs.push_back(obj); } diff --git a/src/stateless/dp/trex_stateless_dp_core.cpp b/src/stateless/dp/trex_stateless_dp_core.cpp index 899e14be..96c18dbd 100644 --- a/src/stateless/dp/trex_stateless_dp_core.cpp +++ b/src/stateless/dp/trex_stateless_dp_core.cpp @@ -146,9 +146,6 @@ TrexStatelessDpCore::add_cont_stream(TrexStream * stream, uint16_t pkt_size = stream->m_pkt.len; const uint8_t *stream_pkt = stream->m_pkt.binary; - TrexStreamBurst * lpburst; - TrexStreamMultiBurst * lpmulti; - node->m_stream_type = stream->m_type; node->m_next_time_offset = 1.0 / (stream->get_pps() * comp->get_multiplier()) ; @@ -161,20 +158,17 @@ TrexStatelessDpCore::add_cont_stream(TrexStream * stream, case TrexStream::stSINGLE_BURST : node->m_stream_type = TrexStream::stMULTI_BURST; - lpburst = (TrexStreamBurst *)stream; - node->m_single_burst = lpburst->m_total_pkts; - node->m_single_burst_refill = lpburst->m_total_pkts; + node->m_single_burst = stream->m_burst_total_pkts; + node->m_single_burst_refill = stream->m_burst_total_pkts; node->m_multi_bursts = 1; /* single burst in multi burst of 1 */ node->m_ibg_sec = 0.0; break; case TrexStream::stMULTI_BURST : - lpmulti =(TrexStreamMultiBurst *)stream; - - node->m_single_burst = lpmulti->m_total_pkts; - node->m_single_burst_refill = lpmulti->m_total_pkts ; - node->m_multi_bursts = lpmulti->m_num_bursts; - node->m_ibg_sec = usec_to_sec( lpmulti->m_ibg_usec ); + node->m_single_burst = stream->m_burst_total_pkts; + node->m_single_burst_refill = stream->m_burst_total_pkts; + node->m_multi_bursts = stream->m_num_bursts; + node->m_ibg_sec = usec_to_sec( stream->m_ibg_usec ); break; default: -- cgit 1.2.3-korg From 78c6593c5a2d3d2242be7fc659d15eac6b869358 Mon Sep 17 00:00:00 2001 From: imarom Date: Thu, 12 Nov 2015 16:33:05 +0200 Subject: DRAFT - only for internal purpose --- .../client/trex_stateless_client.py | 804 ++++++++++++--------- .../client_utils/jsonrpc_client.py | 273 +------ .../trex_control_plane/console/trex_console.py | 96 ++- src/stateless/cp/trex_stateless_port.cpp | 12 +- 4 files changed, 521 insertions(+), 664 deletions(-) (limited to 'scripts/automation/trex_control_plane/console') 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 168853b3..5a7b1873 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -24,148 +24,331 @@ class RpcResponseStatus(namedtuple('RpcResponseStatus', ['success', 'id', 'msg'] msg=self.msg, stat="success" if self.success else "fail") -# RpcResponseStatus = namedtuple('RpcResponseStatus', ['success', 'id', 'msg']) +# simple class to represent complex return value +class RC: + + def __init__ (self, rc, data): + self.rc = rc + self.data = data + + def good (self): + return self.rc + + def bad (self): + return not self.rc + + def data (self): + if self.good(): + return self.data + else: + return "" + + def err (self): + if self.bad(): + return self.data + else: + return "" + +RC_OK = RC(True, "") +def RC_ERR (err): + return RC(False, err) + +class RC_LIST: + def __init__ (self): + self.rc_list = [] + + def add (self, rc): + self.rc_list.append(rc) + + def good(self): + return all([x.good() for x in self.rc_list]) + + def bad (self): + not self.good() + + def data (self): + return [x.data() for x in self.rc_list] + + def err (self): + return [x.err() for x in self.rc_list] + + +# describes a single port +class Port: + + STATE_DOWN = 0 + STATE_IDLE = 1 + STATE_STREAMS = 2 + STATE_TX = 3 + STATE_PAUSE = 4 + + def __init__ (self, port_id, user, transmit): + self.port_id = port_id + self.state = self.STATE_IDLE + self.handler = None + self.transmit = transmit + self.user = user + + self.streams = {} + + def err (self, msg): + return RC_ERR("port {0} : {1}".format(self.port_id, msg)) + + # take the port + def acquire (self, force = False): + params = {"port_id": self.port_id, + "user": self.user, + "force": force} + + command = RpcCmdData("acquire", params) + rc = self.transmit(command.method, command.params) + if rc.success: + self.handler = rc.data + return RC_OK + else: + return RC_ERR(rc.data) + + + # release the port + def release (self): + params = {"port_id": self.port_id, + "handler": self.handler} + + command = RpcCmdData("release", params) + rc = self.transmit(command.method, command.params) + if rc.success: + self.handler = rc.data + return RC_OK + else: + return RC_ERR(rc.data) + + def is_acquired (self): + return (self.handler != None) + + def is_active (self): + return (self.state == self.STATE_TX ) or (self.state == self.STATE_PAUSE) + + def sync (self, sync_data): + + self.handler = sync_data['handler'] + + if sync_data['state'] == "DOWN": + self.state = self.STATE_DOWN + elif sync_data['state'] == "IDLE": + self.state = self.STATE_IDLE + elif sync_data['state'] == "STREAMS": + self.state = self.STATE_STREAMS + elif sync_data['state'] == "TX": + self.state = self.STATE_TX + elif sync_data['state'] == "PAUSE": + self.state = self.STATE_PAUSE + else: + raise Exception("port {0}: bad state received from server '{1}'".format(self.port_id, sync_data['state'])) + + return RC_OK + + + # return TRUE if write commands + def is_port_writeable (self): + # operations on port can be done on state idle or state sreams + return ((self.state == STATE_IDLE) or (self.state == STATE_STREAMS)) + + # add stream to the port + def add_stream (self, stream_id, stream_obj): + + if not self.is_port_writeable(): + return self.err("Please stop port before attempting to add streams") + + + params = {"handler": self.handler, + "port_id": self.port_id, + "stream_id": stream_id, + "stream": stream_obj.dump()} + + rc, data = self.transmit("add_stream", params) + if not rc: + return self.err(data) + + # add the stream + self.streams[stream_id] = stream_obj + + # the only valid state now + self.state = self.STATE_STREAMS + + return RC_OK + + # remove stream from port + def remove_stream (self, stream_id): + + if not stream_id in self.streams: + return self.err("stream {0} does not exists".format(stream_id)) + + params = {"handler": self.handler, + "port_id": self.port_id, + "stream_id": stream_id} + + + rc, data = self.transmit("remove_stream", params) + if not rc: + return self.err(data) + + self.streams[stream_id] = None + + return RC_OK + + # remove all the streams + def remove_all_streams (self): + for stream_id in self.streams.keys(): + rc = self.remove_stream(stream_id) + if rc.bad(): + return rc + + return RC_OK + + # start traffic + def start (self, mul): + if self.state == self.STATE_DOWN: + return self.err("Unable to start traffic - port is down") + + if self.state == self.STATE_IDLE: + return self.err("Unable to start traffic - no streams attached to port") + + if self.state == self.STATE_TX: + return self.err("Unable to start traffic - port is already transmitting") + + params = {"handler": self.handler, + "port_id": self.port_id, + "mul": mul} + + rc, data = self.transmit("remove_stream", params) + if not rc: + return self.err(data) + + self.state = self.STATE_TX + + return RC_OK + + def stop (self): + if (self.state != self.STATE_TX) and (self.state != self.STATE_PAUSE): + return self.err("Unable to stop traffic - port is down") + + params = {"handler": self.handler, + "port_id": self.port_id} + + rc, data = self.transmit("stop_traffic", params) + if not rc: + return self.err(data) + + # only valid state after stop + self.state = self.STREAMS + + return RC_OK + class CTRexStatelessClient(object): """docstring for CTRexStatelessClient""" - def __init__(self, username, server="localhost", sync_port=5050, async_port = 4500, 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, sync_port, virtual) self.verbose = False self._conn_handler = {} self._active_ports = set() - self._stats = CTRexStatsManager("port", "stream") self._system_info = None self._server_version = None self.__err_log = None self._async_client = CTRexAsyncClient(async_port) + self.connected = False - # ----- 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} aren't " - "acquired".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: - 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 - return wrapper - - @property - def system_info(self): - if not 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" + ############# helper functions section ############## - @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" + def __validate_port_list(self, port_id): + if isinstance(port_id, list) or isinstance(port_id, set): + # 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()): + return True + else: + return False + + def __ports (self, port_id_list): + if port_id_list == None: + return range(0, self.get_port_count()) + + for port_id in port_id_list: + if not isinstance(port_id, int) or (port_id < 0) or (port_id > self.get_port_count()): + raise ValueError("bad port id {0}".format(port_id)) + + return [port_id_list] if isinstance(port_id_list, int) else port_id_list + + ############ boot up section ################ - def is_connected(self): - return self.comm_link.is_connected + # connection sequence + def connect (self): - # ----- user-access methods ----- # - def connect(self): - rc, err = self.comm_link.connect() + self.connected = False + + # connect + rc, data = self.comm_link.connect() if not rc: - return rc, err - return self._init_sync() + return RC_ERR(data) - def get_stats_async (self): - return self._async_client.get_stats() - def get_connection_port (self): - return self.comm_link.port + # cache system info + rc, data = self.transmit("get_system_info") + if not rc: + return RC_ERR(data) + + self.system_info = data + + # cache supported cmds + rc, data = self.transmit("get_supported_cmds") + if not rc: + return RC_ERR(data) + + self.supported_cmds = data + + # create ports + self.ports = [] + for port_id in xrange(0, self.get_port_count()): + self.ports.append(Port(port_id, self.user, self.transmit)) + + # acquire all ports + rc = self.acquire() + if rc.bad(): + return rc + + rc = self.sync_with_server() + if rc.bad(): + return rc + + self.connected = True + + return RC_OK + + def is_connected (self): + return self.connected + def disconnect(self): - return self.comm_link.disconnect() + self.connected = False + self.comm_link.disconnect() - def ping(self): - return self.transmit("ping") + + ########### cached queries (no server traffic) ########### def get_supported_cmds(self): - return self.transmit("get_supported_cmds") + return self.supported_cmds def get_version(self): - return self.transmit("get_version") + return self.server_version def get_system_info(self): - return self.transmit("get_system_info") + return self.system_info def get_port_count(self): return self.system_info.get("port_count") @@ -177,205 +360,167 @@ class CTRexStatelessClient(object): else: return port_ids - def sync_user(self, sync_streams=False): - return self.transmit("sync_user", {"user": self.user, "sync_streams": sync_streams}) + def get_stats_async (self): + return self._async_client.get_stats() + + def get_connection_port (self): + return self.comm_link.port def get_acquired_ports(self): - return self._conn_handler.keys() + return [port for port in self.ports if port.is_acquired()] + def get_active_ports(self): - return list(self._active_ports) + return [port for port in self.ports if port.is_active()] 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") - 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("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: - return self._process_batch_result(commands, resp_list, self._handle_acquire_response) - else: - params = {"port_id": port_id, - "user": self.user, - "force": force} - 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): - 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("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: - 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) - return self._handle_release_response(command, - self.transmit(command.method, command.params), - self.ack_success_test) + ############# server actions ################ - @acquired - def add_stream(self, stream_id, stream_obj, port_id=None): - if not self._is_ports_valid(port_id): - raise ValueError("Provided illegal port id input") + # ping server + def ping(self): + rc, info = self.transmit("ping") + return RC(rc, info) + + + def sync_with_server(self, sync_streams=False): + rc, data = self.transmit("sync_user", {"user": self.user, "sync_streams": sync_streams}) + if not rc: + return RC_ERR(data) + + for port_info in data: + + rc = self.ports[port_info['port_id']].sync(port_info) + if rc.bad(): + return rc + + return RC_OK + + + + ########## port commands ############## + # acquire ports, if port_list is none - get all + def acquire (self, port_id_list = None, force = False): + port_id_list = self.__ports(port_id_list) + + rc_list = RC_LIST() + + for port_id in port_id_list: + rc = self.ports[port_id].acquire(force) + rc_list.add(rc) + + return rc_list + + # release ports + def release (self, port_id_list = None): + port_id_list = self.__ports(port_id_list) + + rc_list = RC_LIST() + + for port_id in port_id_list: + rc, msg = self.ports[port_id].release(force) + rc_list.add(rc) + + return rc_list + + + def add_stream(self, stream_id, stream_obj, port_id_list = None): assert isinstance(stream_obj, CStream) - params = {"handler": self._conn_handler.get(port_id), - "port_id": port_id, - "stream_id": stream_id, - "stream": stream_obj.dump()} - return self.transmit("add_stream", params) - @acquired - def add_stream_pack(self, stream_pack_list, port_id=None): - if not self._is_ports_valid(port_id): - raise ValueError("Provided illegal port id input") + port_id_list = self.__ports(port_id_list) + + rc_list = RC_LIST() + + for port_id in port_id_list: + rc = self.ports[port_id].add_stream(stream_id, stream_obj) + rc_list.add(rc) + + return rc_list + + + def add_stream_pack(self, stream_pack_list, port_id_list = None): + + port_id_list = self.__ports(port_id_list) + + rc_list = RC_LIST() - # 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_pack_list: - 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 not res_ok: - return res_ok, resp_list - - 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): - raise ValueError("Provided illegal port id input") - params = {"handler": self._conn_handler.get(port_id), - "port_id": port_id, - "stream_id": stream_id} - return self.transmit("remove_stream", params) + rc = self.add_stream(stream_pack.stream_id, stream_pack.stream, port_id_list) + rc_list.add(rc) + + return rc_list - 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_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) + def remove_stream(self, stream_id, port_id_list = None): + port_id_list = self.__ports(port_id_list) + + rc_list = RC_LIST() + + for port_id in port_id_list: + rc = self.ports[port_id].remove_stream(stream_id) + rc_list.add(rc) + + return rc_list + - @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") - params = {"handler": self._conn_handler.get(port_id), - "port_id": port_id} - return self.transmit("get_stream_list", params) - @force_status(owned=True, active_and_owned=True) + def remove_all_streams(self, port_id_list = None): + port_id_list = self.__ports(port_id_list) + + rc_list = RC_LIST() + + for port_id in port_id_list: + rc = self.ports[port_id].remove_all_streams() + rc_list.add(rc) + + return rc_list + + 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, - "get_pkt": get_pkt} - return self.transmit("get_stream_list", params) - 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": multiplier}) - 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_start_traffic_response, - success_test=self.ack_success_test) - else: - params = {"handler": self._conn_handler.get(port_id), - "port_id": port_id, - "mul": multiplier} - command = RpcCmdData("start_traffic", params) - return self._handle_start_traffic_response(command, - self.transmit(command.method, command.params), - self.ack_success_test) - - def stop_traffic(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 - if not port_ids: - # don't invoke if port ids is empty - return True, [] - 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: - 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) - return self._handle_stop_traffic_response(command, - self.transmit(command.method, command.params), - self.ack_success_test) + return self.ports[port_id].get_stream(stream_id) + + + def get_all_streams(self, port_id, get_pkt = False): + + return self.ports[port_id].get_all_streams() + + + def get_stream_id_list(self, port_id): + + return self.ports[port_id].get_stream_id_list() + + + def start_traffic (self, multiplier, port_id_list = None): + + port_id_list = self.__ports(port_id_list) + + rc_list = RC_LIST() + + for port_id in port_id_list: + rc = self.ports[port_id].start(multiplier) + rc_list.add(rc) + + return rc_list + + + + def stop_traffic (self, port_id_list = None): + + port_id_list = self.__ports(port_id_list) + + rc_list = RC_LIST() + + for port_id in port_id_list: + rc = self.ports[port_id].stop() + rc_list.add(rc) + + return rc_list -# 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): if not self._is_ports_valid(port_id): raise ValueError("Provided illegal port id input") @@ -393,7 +538,6 @@ class CTRexStatelessClient(object): 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) def get_stream_stats(self, port_id=None): if not self._is_ports_valid(port_id): raise ValueError("Provided illegal port id input") @@ -411,26 +555,6 @@ class CTRexStatelessClient(object): 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 - # 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={}): @@ -439,17 +563,6 @@ class CTRexStatelessClient(object): def transmit_batch(self, batch_list): return self.comm_link.transmit_batch(batch_list) - @staticmethod - def _object_decoder(obj_type, obj_data): - if obj_type == "global": - return CGlobalStats(**obj_data) - elif obj_type == "port": - return CPortStats(**obj_data) - elif obj_type == "stream": - return CStreamStats(**obj_data) - else: - # Do not serialize the data into class - return obj_data @staticmethod def default_success_test(result_obj): @@ -474,35 +587,37 @@ class CTRexStatelessClient(object): # def cmd_reset (self, annotate_func): - ports = self.get_port_ids() # sync with the server - rc, log = self._init_sync() - annotate_func("Syncing with the server:", rc, log) - if not rc: - return False - + rc = self.sync_with_server() + annotate_func("Syncing with the server:", rc.good(), rc.err()) + if rc.bad(): + return rc # force acquire all ports - rc, log = self.acquire(ports, force = True) - annotate_func("Force acquiring all ports:", rc, log) - if not rc: - return False + rc = self.acquire(force = True) + annotate_func("Force acquiring all ports:", rc.good(), rc.err()) + if rc.bad(): + return rc - # force stop - rc, log = self.stop_traffic(ports) - annotate_func("Stop traffic on all ports:", rc, log) - if not rc: - return False + + # force stop all ports + port_id_list = self.get_active_ports() + rc = self.stop_traffic(port_id_list) + annotate_func("Stop traffic on all ports:", rc.good(), rc.err()) + if rc.bad(): + return rc + + return # remove all streams - rc, log = self.remove_all_streams(ports) - annotate_func("Removing all streams from all ports:", rc, log) - if not rc: - return False + rc = self.remove_all_streams(ports) + annotate_func("Removing all streams from all ports:", rc.good(), rc.err()) + if rc.bad(): + return rc # TODO: clear stats - return True + return RC_OK # stop cmd @@ -511,7 +626,7 @@ class CTRexStatelessClient(object): # find the relveant ports active_ports = set(self.get_active_ports()).intersection(ports) if not active_ports: - annotate_func("No active traffic on porivded ports") + annotate_func("No active traffic on porvided ports") return True rc, log = self.stop_traffic(active_ports) @@ -524,7 +639,7 @@ class CTRexStatelessClient(object): # start cmd def cmd_start (self, ports, stream_list, mult, force, annotate_func): - if force: + if (force and set(self.get_active_ports()).intersection(ports)): rc = self.cmd_stop(ports, annotate_func) if not rc: return False @@ -626,15 +741,6 @@ class CTRexStatelessClient(object): else: return False - def _is_ports_valid(self, port_id): - if isinstance(port_id, list) or isinstance(port_id, set): - # 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()): - return True - else: - return False def _process_batch_result(self, req_list, resp_list, handler_func=None, success_test=default_success_test): res_ok = True diff --git a/scripts/automation/trex_control_plane/client_utils/jsonrpc_client.py b/scripts/automation/trex_control_plane/client_utils/jsonrpc_client.py index 58491aba..077c82ad 100755 --- a/scripts/automation/trex_control_plane/client_utils/jsonrpc_client.py +++ b/scripts/automation/trex_control_plane/client_utils/jsonrpc_client.py @@ -110,7 +110,7 @@ class JsonRpcClient(object): return id, msg - def invoke_rpc_method (self, method_name, params = {}, block = False): + def invoke_rpc_method (self, method_name, params = {}, block = True): if not self.connected: return False, "Not connected to server" @@ -120,7 +120,7 @@ class JsonRpcClient(object): # low level send of string message - def send_raw_msg (self, msg, block = False): + def send_raw_msg (self, msg, block = True): self.verbose_msg("Sending Request To Server:\n\n" + self.pretty_json(msg) + "\n") if block: @@ -248,272 +248,3 @@ class JsonRpcClient(object): if hasattr(self, "context"): self.context.destroy(linger=0) -# MOVE THIS TO DAN'S FILE -class TrexStatelessClient(JsonRpcClient): - - def __init__ (self, server, port, user): - - super(TrexStatelessClient, self).__init__(server, port) - - self.user = user - self.port_handlers = {} - - self.supported_cmds = [] - self.system_info = None - self.server_version = None - - - def whoami (self): - return self.user - - def ping_rpc_server(self): - - return self.invoke_rpc_method("ping", block = False) - - def get_rpc_server_version (self): - return self.server_version - - def get_system_info (self): - if not self.system_info: - return {} - - return self.system_info - - def get_supported_cmds(self): - if not self.supported_cmds: - return {} - - return self.supported_cmds - - def get_port_count (self): - if not self.system_info: - return 0 - - return self.system_info["port_count"] - - # sync the client with all the server required data - def sync (self): - - # get server version - rc, msg = self.invoke_rpc_method("get_version") - if not rc: - self.disconnect() - return rc, msg - - self.server_version = msg - - # get supported commands - rc, msg = self.invoke_rpc_method("get_supported_cmds") - if not rc: - self.disconnect() - return rc, msg - - self.supported_cmds = [str(x) for x in msg if x] - - # get system info - rc, msg = self.invoke_rpc_method("get_system_info") - if not rc: - self.disconnect() - return rc, msg - - self.system_info = msg - - return True, "" - - def connect (self): - rc, err = super(TrexStatelessClient, self).connect() - if not rc: - return rc, err - - return self.sync() - - - # take ownership over ports - def take_ownership (self, port_id_array, force = False): - if not self.connected: - return False, "Not connected to server" - - batch = self.create_batch() - - for port_id in port_id_array: - batch.add("acquire", params = {"port_id":port_id, "user":self.user, "force":force}) - - rc, resp_list = batch.invoke() - if not rc: - return rc, resp_list - - for i, rc in enumerate(resp_list): - if rc[0]: - self.port_handlers[port_id_array[i]] = rc[1] - - return True, resp_list - - - def release_ports (self, port_id_array): - batch = self.create_batch() - - for port_id in port_id_array: - - # let the server handle un-acquired errors - if self.port_handlers.get(port_id): - handler = self.port_handlers[port_id] - else: - handler = "" - - batch.add("release", params = {"port_id":port_id, "handler":handler}) - - - rc, resp_list = batch.invoke() - if not rc: - return rc, resp_list - - for i, rc in enumerate(resp_list): - if rc[0]: - self.port_handlers.pop(port_id_array[i]) - - return True, resp_list - - def get_owned_ports (self): - return self.port_handlers.keys() - - # fetch port stats - def get_port_stats (self, port_id_array): - if not self.connected: - return False, "Not connected to server" - - batch = self.create_batch() - - # empty list means all - if port_id_array == []: - port_id_array = list([x for x in xrange(0, self.system_info["port_count"])]) - - for port_id in port_id_array: - - # let the server handle un-acquired errors - if self.port_handlers.get(port_id): - handler = self.port_handlers[port_id] - else: - handler = "" - - batch.add("get_port_stats", params = {"port_id":port_id, "handler":handler}) - - - rc, resp_list = batch.invoke() - - return rc, resp_list - - # snapshot will take a snapshot of all your owned ports for streams and etc. - def snapshot(self): - - - if len(self.get_owned_ports()) == 0: - return {} - - snap = {} - - batch = self.create_batch() - - for port_id in self.get_owned_ports(): - - batch.add("get_port_stats", params = {"port_id": port_id, "handler": self.port_handlers[port_id]}) - batch.add("get_stream_list", params = {"port_id": port_id, "handler": self.port_handlers[port_id]}) - - rc, resp_list = batch.invoke() - if not rc: - return rc, resp_list - - # split the list to 2s - index = 0 - for port_id in self.get_owned_ports(): - if not resp_list[index] or not resp_list[index + 1]: - snap[port_id] = None - continue - - # fetch the first two - stats = resp_list[index][1] - stream_list = resp_list[index + 1][1] - - port = {} - port['status'] = stats['status'] - port['stream_list'] = [] - - # get all the streams - if len(stream_list) > 0: - batch = self.create_batch() - for stream_id in stream_list: - batch.add("get_stream", params = {"port_id": port_id, "stream_id": stream_id, "handler": self.port_handlers[port_id]}) - - rc, stream_resp_list = batch.invoke() - if not rc: - port = {} - - port['streams'] = {} - for i, resp in enumerate(stream_resp_list): - if resp[0]: - port['streams'][stream_list[i]] = resp[1] - - snap[port_id] = port - - # move to next one - index += 2 - - - return snap - - # add stream - # def add_stream (self, port_id, stream_id, isg, next_stream_id, packet, vm=[]): - # if not port_id in self.get_owned_ports(): - # return False, "Port {0} is not owned... please take ownership before adding streams".format(port_id) - # - # handler = self.port_handlers[port_id] - # - # stream = {} - # stream['enabled'] = True - # stream['self_start'] = True - # stream['isg'] = isg - # stream['next_stream_id'] = next_stream_id - # stream['packet'] = {} - # stream['packet']['binary'] = packet - # stream['packet']['meta'] = "" - # stream['vm'] = vm - # stream['rx_stats'] = {} - # stream['rx_stats']['enabled'] = False - # - # stream['mode'] = {} - # stream['mode']['type'] = 'continuous' - # stream['mode']['pps'] = 10.0 - # - # params = {} - # params['handler'] = handler - # params['stream'] = stream - # params['port_id'] = port_id - # params['stream_id'] = stream_id - # - # print params - # return self.invoke_rpc_method('add_stream', params = params) - - def add_stream(self, port_id_array, stream_pack_list): - batch = self.create_batch() - - for port_id in port_id_array: - for stream_pack in stream_pack_list: - params = {"port_id": port_id, - "handler": self.port_handlers[port_id], - "stream_id": stream_pack.stream_id, - "stream": stream_pack.stream} - batch.add("add_stream", params=params) - rc, resp_list = batch.invoke() - if not rc: - return rc, resp_list - - for i, rc in enumerate(resp_list): - if rc[0]: - print "Stream {0} - {1}".format(i, rc[1]) - # self.port_handlers[port_id_array[i]] = rc[1] - - return True, resp_list - - # return self.invoke_rpc_method('add_stream', params = params) - -if __name__ == "__main__": - pass \ No newline at end of file diff --git a/scripts/automation/trex_control_plane/console/trex_console.py b/scripts/automation/trex_control_plane/console/trex_console.py index 06ae762a..2be643ab 100755 --- a/scripts/automation/trex_control_plane/console/trex_console.py +++ b/scripts/automation/trex_control_plane/console/trex_console.py @@ -160,7 +160,51 @@ class TRexConsole(cmd.Cmd): for x in os.listdir(path) if x.startswith(start_string)] + # annotation method + @staticmethod + def annotate (desc, rc = None, err_log = None, ext_err_msg = None): + print format_text('\n{:<40}'.format(desc), 'bold'), + if rc == None: + print "\n" + return + + if rc == False: + # do we have a complex log object ? + if isinstance(err_log, list): + print "" + for func in err_log: + if func: + print func + print "" + + elif isinstance(err_log, str): + print "\n" + err_log + "\n" + + print format_text("[FAILED]\n", 'red', 'bold') + if ext_err_msg: + print format_text(ext_err_msg + "\n", 'blue', 'bold') + + return False + + else: + print format_text("[SUCCESS]\n", 'green', 'bold') + return True + + ####################### shell commands ####################### + def do_ping (self, line): + '''Ping the server\n''' + + rc = self.stateless_client.ping() + if rc.good(): + print format_text("[SUCCESS]\n", 'green', 'bold') + else: + print "\n*** " + rc.err() + "\n" + print format_text("[FAILED]\n", 'red', 'bold') + return + + def do_test (self, line): + print self.stateless_client.get_acquired_ports() # set verbose on / off def do_verbose(self, line): @@ -171,65 +215,41 @@ class TRexConsole(cmd.Cmd): elif line == "on": self.verbose = True self.stateless_client.set_verbose(True) - print green("\nverbose set to on\n") + print format_text("\nverbose set to on\n", 'green', 'bold') elif line == "off": self.verbose = False self.stateless_client.set_verbose(False) - print green("\nverbose set to off\n") + print format_text("\nverbose set to off\n", 'green', 'bold') else: - print magenta("\nplease specify 'on' or 'off'\n") + print format_text("\nplease specify 'on' or 'off'\n", 'bold') + ############### connect def do_connect (self, line): '''Connects to the server\n''' - res_ok, msg = self.stateless_client.connect() - if res_ok: + rc = self.stateless_client.connect() + if rc.good(): print format_text("[SUCCESS]\n", 'green', 'bold') else: - print "\n*** " + msg + "\n" + print "\n*** " + rc.err() + "\n" print format_text("[FAILED]\n", 'red', 'bold') return - self.supported_rpc = self.stateless_client.get_supported_cmds().data - if self.acquire_all_ports: - res_ok, log = self.stateless_client.acquire(self.stateless_client.get_port_ids()) - if not res_ok: - print "\n*** Failed to acquire all ports... exiting...""" + def do_disconnect (self, line): + '''Disconnect from the server\n''' - @staticmethod - def annotate (desc, rc = None, err_log = None, ext_err_msg = None): - print format_text('\n{:<40}'.format(desc), 'bold'), - if rc == None: - print "\n" + if not self.stateless_client.is_connected(): + print "Not connected to server\n" return - if rc == False: - # do we have a complex log object ? - if isinstance(err_log, list): - print "" - for func in err_log: - if func: - print func - print "" - - elif isinstance(err_log, str): - print "\n" + err_log + "\n" - - print format_text("[FAILED]\n", 'red', 'bold') - if ext_err_msg: - print format_text(ext_err_msg + "\n", 'blue', 'bold') - - return False - - else: - print format_text("[SUCCESS]\n", 'green', 'bold') - return True - + self.stateless_client.disconnect() + print format_text("[SUCCESS]\n", 'green', 'bold') + ############### start def complete_start(self, text, line, begidx, endidx): diff --git a/src/stateless/cp/trex_stateless_port.cpp b/src/stateless/cp/trex_stateless_port.cpp index 907b9cf4..7f2382d3 100644 --- a/src/stateless/cp/trex_stateless_port.cpp +++ b/src/stateless/cp/trex_stateless_port.cpp @@ -186,22 +186,22 @@ TrexStatelessPort::get_state_as_string() const { switch (get_state()) { case PORT_STATE_DOWN: - return "down"; + return "DOWN"; case PORT_STATE_IDLE: - return "no streams"; + return "IDLE"; case PORT_STATE_STREAMS: - return "with streams, idle"; + return "STREAMS"; case PORT_STATE_TX: - return "transmitting"; + return "TX"; case PORT_STATE_PAUSE: - return "paused"; + return "PAUSE"; } - return "unknown"; + return "UNKNOWN"; } void -- cgit 1.2.3-korg From 513581840e5787e73161de049aa59552f23e719d Mon Sep 17 00:00:00 2001 From: imarom Date: Thu, 12 Nov 2015 18:28:21 +0200 Subject: modifying stateless client to a simpler lightweight module --- .../client/trex_stateless_client.py | 406 +++++++-------------- .../trex_control_plane/console/parsing_opts.py | 4 +- .../trex_control_plane/console/trex_console.py | 6 +- 3 files changed, 144 insertions(+), 272 deletions(-) (limited to 'scripts/automation/trex_control_plane/console') 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 5a7b1873..93b36f82 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -12,6 +12,7 @@ import json from common.trex_stats import * from common.trex_streams import * from collections import namedtuple +from common.text_opts import * from trex_async_client import CTRexAsyncClient @@ -27,50 +28,48 @@ class RpcResponseStatus(namedtuple('RpcResponseStatus', ['success', 'id', 'msg'] # simple class to represent complex return value class RC: - def __init__ (self, rc, data): - self.rc = rc - self.data = data + def __init__ (self, rc = None, data = None): + self.rc_list = [] + + if (rc != None) and (data != None): + tuple_rc = namedtuple('RC', ['rc', 'data']) + self.rc_list.append(tuple_rc(rc, data)) + + def add (self, rc): + self.rc_list += rc.rc_list def good (self): - return self.rc + return all([x.rc for x in self.rc_list]) def bad (self): - return not self.rc + return not self.good() def data (self): - if self.good(): - return self.data - else: - return "" + return all([x.data if x.rc else "" for x in self.rc_list]) def err (self): - if self.bad(): - return self.data - else: - return "" + return all([x.data if not x.rc else "" for x in self.rc_list]) -RC_OK = RC(True, "") -def RC_ERR (err): - return RC(False, err) + def annotate (self, desc): + print format_text('\n{:<40}'.format(desc), 'bold'), -class RC_LIST: - def __init__ (self): - self.rc_list = [] + if self.bad(): + # print all the errors + for x in self.rc_list: + if not x.rc: + print format_text("\n{0}".format(x.data), 'bold') - def add (self, rc): - self.rc_list.append(rc) + print format_text("[FAILED]\n", 'red', 'bold') - def good(self): - return all([x.good() for x in self.rc_list]) - def bad (self): - not self.good() + else: + print format_text("[SUCCESS]\n", 'green', 'bold') - def data (self): - return [x.data() for x in self.rc_list] - def err (self): - return [x.err() for x in self.rc_list] +def RC_OK(): + return RC(True, "") +def RC_ERR (err): + return RC(False, err) # describes a single port @@ -104,7 +103,7 @@ class Port: rc = self.transmit(command.method, command.params) if rc.success: self.handler = rc.data - return RC_OK + return RC_OK() else: return RC_ERR(rc.data) @@ -118,7 +117,7 @@ class Port: rc = self.transmit(command.method, command.params) if rc.success: self.handler = rc.data - return RC_OK + return RC_OK() else: return RC_ERR(rc.data) @@ -145,13 +144,13 @@ class Port: else: raise Exception("port {0}: bad state received from server '{1}'".format(self.port_id, sync_data['state'])) - return RC_OK + return RC_OK() # return TRUE if write commands def is_port_writeable (self): # operations on port can be done on state idle or state sreams - return ((self.state == STATE_IDLE) or (self.state == STATE_STREAMS)) + return ((self.state == self.STATE_IDLE) or (self.state == self.STATE_STREAMS)) # add stream to the port def add_stream (self, stream_id, stream_obj): @@ -163,11 +162,12 @@ class Port: params = {"handler": self.handler, "port_id": self.port_id, "stream_id": stream_id, - "stream": stream_obj.dump()} + "stream": stream_obj} rc, data = self.transmit("add_stream", params) if not rc: - return self.err(data) + r = self.err(data) + print r.good() # add the stream self.streams[stream_id] = stream_obj @@ -175,7 +175,7 @@ class Port: # the only valid state now self.state = self.STATE_STREAMS - return RC_OK + return RC_OK() # remove stream from port def remove_stream (self, stream_id): @@ -194,16 +194,21 @@ class Port: self.streams[stream_id] = None - return RC_OK + return RC_OK() # remove all the streams def remove_all_streams (self): - for stream_id in self.streams.keys(): - rc = self.remove_stream(stream_id) - if rc.bad(): - return rc - return RC_OK + params = {"handler": self.handler, + "port_id": self.port_id} + + rc, data = self.transmit("remove_all_streams", params) + if not rc: + return self.err(data) + + self.streams = {} + + return RC_OK() # start traffic def start (self, mul): @@ -220,17 +225,20 @@ class Port: "port_id": self.port_id, "mul": mul} - rc, data = self.transmit("remove_stream", params) + rc, data = self.transmit("start_traffic", params) if not rc: return self.err(data) self.state = self.STATE_TX - return RC_OK + return RC_OK() + + # stop traffic + # with force ignores the cached state and sends the command + def stop (self, force = False): - def stop (self): - if (self.state != self.STATE_TX) and (self.state != self.STATE_PAUSE): - return self.err("Unable to stop traffic - port is down") + if (not force) and (self.state != self.STATE_TX) and (self.state != self.STATE_PAUSE): + return self.err("port is not transmitting") params = {"handler": self.handler, "port_id": self.port_id} @@ -240,9 +248,10 @@ class Port: return self.err(data) # only valid state after stop - self.state = self.STREAMS + self.state = self.STATE_STREAMS + + return RC_OK() - return RC_OK class CTRexStatelessClient(object): @@ -265,7 +274,7 @@ class CTRexStatelessClient(object): ############# helper functions section ############## - def __validate_port_list(self, port_id): + def validate_port_list(self, port_id): if isinstance(port_id, list) or isinstance(port_id, set): # check each item of the sequence return all([self._is_ports_valid(port) @@ -275,15 +284,25 @@ class CTRexStatelessClient(object): else: return False + # some preprocessing for port argument def __ports (self, port_id_list): + + # none means all if port_id_list == None: return range(0, self.get_port_count()) + # always list + if isinstance(port_id_list, int): + port_id_list = [port_id_list] + + if not isinstance(port_id_list, list): + raise ValueError("bad port id list: {0}".format(port_id_list)) + for port_id in port_id_list: if not isinstance(port_id, int) or (port_id < 0) or (port_id > self.get_port_count()): raise ValueError("bad port id {0}".format(port_id)) - return [port_id_list] if isinstance(port_id_list, int) else port_id_list + return port_id_list ############ boot up section ################ @@ -297,7 +316,6 @@ class CTRexStatelessClient(object): if not rc: return RC_ERR(data) - # cache system info rc, data = self.transmit("get_system_info") if not rc: @@ -328,7 +346,7 @@ class CTRexStatelessClient(object): self.connected = True - return RC_OK + return RC_OK() def is_connected (self): return self.connected @@ -367,11 +385,11 @@ class CTRexStatelessClient(object): return self.comm_link.port def get_acquired_ports(self): - return [port for port in self.ports if port.is_acquired()] + return [port.port_id for port in self.ports if port.is_acquired()] def get_active_ports(self): - return [port for port in self.ports if port.is_active()] + return [port.port_id for port in self.ports if port.is_active()] def set_verbose(self, mode): self.comm_link.set_verbose(mode) @@ -396,7 +414,7 @@ class CTRexStatelessClient(object): if rc.bad(): return rc - return RC_OK + return RC_OK() @@ -405,78 +423,71 @@ class CTRexStatelessClient(object): def acquire (self, port_id_list = None, force = False): port_id_list = self.__ports(port_id_list) - rc_list = RC_LIST() + rc = RC() for port_id in port_id_list: - rc = self.ports[port_id].acquire(force) - rc_list.add(rc) - - return rc_list + rc.add(self.ports[port_id].acquire(force)) + + return rc # release ports def release (self, port_id_list = None): port_id_list = self.__ports(port_id_list) - rc_list = RC_LIST() + rc = RC() for port_id in port_id_list: - rc, msg = self.ports[port_id].release(force) - rc_list.add(rc) + rc.add(self.ports[port_id].release(force)) - return rc_list + return rc def add_stream(self, stream_id, stream_obj, port_id_list = None): - assert isinstance(stream_obj, CStream) port_id_list = self.__ports(port_id_list) - rc_list = RC_LIST() + rc = RC() for port_id in port_id_list: - rc = self.ports[port_id].add_stream(stream_id, stream_obj) - rc_list.add(rc) + rc.add(self.ports[port_id].add_stream(stream_id, stream_obj)) - return rc_list + return rc def add_stream_pack(self, stream_pack_list, port_id_list = None): port_id_list = self.__ports(port_id_list) - rc_list = RC_LIST() + rc = RC() for stream_pack in stream_pack_list: - rc = self.add_stream(stream_pack.stream_id, stream_pack.stream, port_id_list) - rc_list.add(rc) + rc.add(self.add_stream(stream_pack.stream_id, stream_pack.stream, port_id_list)) - return rc_list + return rc def remove_stream(self, stream_id, port_id_list = None): port_id_list = self.__ports(port_id_list) - rc_list = RC_LIST() + rc = RC() for port_id in port_id_list: - rc = self.ports[port_id].remove_stream(stream_id) - rc_list.add(rc) + rc.add(self.ports[port_id].remove_stream(stream_id)) - return rc_list + return rc def remove_all_streams(self, port_id_list = None): port_id_list = self.__ports(port_id_list) - rc_list = RC_LIST() + rc = RC() for port_id in port_id_list: - rc = self.ports[port_id].remove_all_streams() - rc_list.add(rc) + rc.add(self.ports[port_id].remove_all_streams()) - return rc_list + return rc def get_stream(self, stream_id, port_id, get_pkt = False): @@ -498,85 +509,36 @@ class CTRexStatelessClient(object): port_id_list = self.__ports(port_id_list) - rc_list = RC_LIST() + rc = RC() for port_id in port_id_list: - rc = self.ports[port_id].start(multiplier) - rc_list.add(rc) + rc.add(self.ports[port_id].start(multiplier)) - return rc_list + return rc - def stop_traffic (self, port_id_list = None): + def stop_traffic (self, port_id_list = None, force = False): port_id_list = self.__ports(port_id_list) - - rc_list = RC_LIST() + rc = RC() for port_id in port_id_list: - rc = self.ports[port_id].stop() - rc_list.add(rc) + rc.add(self.ports[port_id].stop(force)) - return rc_list + return rc def get_port_stats(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("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: - self._process_batch_result(commands, resp_list, self._handle_get_port_stats_response) - else: - params = {"handler": self._conn_handler.get(port_id), - "port_id": port_id} - command = RpcCmdData("get_port_stats", params) - return self._handle_get_port_stats_response(command, self.transmit(command.method, command.params)) + pass def get_stream_stats(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("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: - self._process_batch_result(commands, resp_list, self._handle_get_stream_stats_response) - else: - params = {"handler": self._conn_handler.get(port_id), - "port_id": port_id} - command = RpcCmdData("get_stream_stats", params) - return self._handle_get_stream_stats_response(command, self.transmit(command.method, command.params)) - + pass def transmit(self, method_name, params={}): return self.comm_link.transmit(method_name, params) - def transmit_batch(self, batch_list): - return self.comm_link.transmit_batch(batch_list) - - - @staticmethod - def default_success_test(result_obj): - if result_obj.success: - return True - else: - return False - - @staticmethod - def ack_success_test(result_obj): - if result_obj.success and result_obj.data == "ACK": - return True - else: - return False ######################### Console (high level) API ######################### @@ -585,181 +547,91 @@ class CTRexStatelessClient(object): # acquire, stop, remove streams and clear stats # # - def cmd_reset (self, annotate_func): + def cmd_reset (self): # sync with the server rc = self.sync_with_server() - annotate_func("Syncing with the server:", rc.good(), rc.err()) + rc.annotate("Syncing with the server:") if rc.bad(): return rc - # force acquire all ports rc = self.acquire(force = True) - annotate_func("Force acquiring all ports:", rc.good(), rc.err()) + rc.annotate("Force acquiring all ports:") if rc.bad(): return rc # force stop all ports - port_id_list = self.get_active_ports() - rc = self.stop_traffic(port_id_list) - annotate_func("Stop traffic on all ports:", rc.good(), rc.err()) + rc = self.stop_traffic(self.get_port_ids(), True) + rc.annotate("Stop traffic on all ports:") if rc.bad(): return rc - return # remove all streams - rc = self.remove_all_streams(ports) - annotate_func("Removing all streams from all ports:", rc.good(), rc.err()) + rc = self.remove_all_streams(self.get_port_ids()) + rc.annotate("Removing all streams from all ports:") if rc.bad(): return rc # TODO: clear stats - return RC_OK + return RC_OK() # stop cmd - def cmd_stop (self, ports, annotate_func): + def cmd_stop (self, port_id_list): # find the relveant ports - active_ports = set(self.get_active_ports()).intersection(ports) + active_ports = list(set(self.get_active_ports()).intersection(port_id_list)) + if not active_ports: - annotate_func("No active traffic on porvided ports") + print format_text("No active traffic on porvided ports", 'bold') return True - rc, log = self.stop_traffic(active_ports) - annotate_func("Stopping traffic on ports {0}:".format([port for port in active_ports]), rc, log) - if not rc: + rc = self.stop_traffic(active_ports) + rc.annotate("Stopping traffic on port(s) {0}:".format(port_id_list)) + if rc.bad(): return False return True # start cmd - def cmd_start (self, ports, stream_list, mult, force, annotate_func): + def cmd_start (self, port_id_list, stream_list, mult, force): - if (force and set(self.get_active_ports()).intersection(ports)): - rc = self.cmd_stop(ports, annotate_func) - if not rc: - return False + active_ports = list(set(self.get_active_ports()).intersection(port_id_list)) - rc, log = self.remove_all_streams(ports) - annotate_func("Removing all streams from ports {0}:".format([port for port in ports]), rc, log, - "Please either retry with --force or stop traffic") - if not rc: - return False + if active_ports: + if not force: + print format_text("Port(s) {0} are active - please stop them or add '--force'".format(active_ports), 'bold') + return False + else: + rc = self.cmd_stop(active_ports) + if not rc: + return False - rc, log = self.add_stream_pack(stream_list.compiled, port_id= ports) - annotate_func("Attaching streams to port {0}:".format([port for port in ports]), rc, log) - if not rc: - return False - # finally, start the traffic - rc, log = self.start_traffic(mult, ports) - annotate_func("Starting traffic on ports {0}:".format([port for port in ports]), rc, log) - if not rc: + rc = self.remove_all_streams(port_id_list) + rc.annotate("Removing all streams from ports {0}:".format(port_id_list)) + if rc.bad(): return False - return True - - # ----- handler internal methods ----- # - 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_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_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): - return RpcResponseStatus(True, port_id, "Removed") - else: - return RpcResponseStatus(False, port_id, response.data) - - def _handle_release_response(self, request, response, success_test): - 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): - 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, success_test): - port_id = request.params.get("port_id") - if success_test(response): - if port_id in self._active_ports: - 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: - return CGlobalStats(**response.success) - else: + rc = self.add_stream_pack(stream_list.compiled, port_id_list) + rc.annotate("Attaching streams to port {0}:".format(port_id_list)) + if rc.bad(): return False - 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, success_test): - if response.success: - return CStreamStats(**response.success) - else: + # finally, start the traffic + rc = self.start_traffic(mult, port_id_list) + rc.annotate("Starting traffic on ports {0}:".format(port_id_list)) + if rc.bad(): return False + return True - 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): - # 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 ------ # class CCommLink(object): """describes the connectivity of the stateless client method""" diff --git a/scripts/automation/trex_control_plane/console/parsing_opts.py b/scripts/automation/trex_control_plane/console/parsing_opts.py index f983d837..252d33bf 100755 --- a/scripts/automation/trex_control_plane/console/parsing_opts.py +++ b/scripts/automation/trex_control_plane/console/parsing_opts.py @@ -139,8 +139,8 @@ class CCmdArgParser(argparse.ArgumentParser): opts.ports = self.stateless_client.get_port_ids() for port in opts.ports: - if not self.stateless_client._is_ports_valid(port): - self.error("port id {0} is not a valid\n".format(port)) + if not self.stateless_client.validate_port_list(port): + self.error("port id {0} is not a valid port id\n".format(port)) return opts diff --git a/scripts/automation/trex_control_plane/console/trex_console.py b/scripts/automation/trex_control_plane/console/trex_console.py index 2be643ab..5ba82dcb 100755 --- a/scripts/automation/trex_control_plane/console/trex_console.py +++ b/scripts/automation/trex_control_plane/console/trex_console.py @@ -296,7 +296,7 @@ class TRexConsole(cmd.Cmd): return - self.stateless_client.cmd_start(opts.ports, stream_list, opts.mult, opts.force, self.annotate) + self.stateless_client.cmd_start(opts.ports, stream_list, opts.mult, opts.force) return @@ -315,7 +315,7 @@ class TRexConsole(cmd.Cmd): if opts is None: return - self.stateless_client.cmd_stop(opts.ports, self.annotate) + self.stateless_client.cmd_stop(opts.ports) return def help_stop(self): @@ -324,7 +324,7 @@ class TRexConsole(cmd.Cmd): ########## reset def do_reset (self, line): '''force stop all ports\n''' - self.stateless_client.cmd_reset(self.annotate) + self.stateless_client.cmd_reset() # tui -- cgit 1.2.3-korg From 57e67fd2ae248039951798978cc8c1c219c3d752 Mon Sep 17 00:00:00 2001 From: imarom Date: Fri, 13 Nov 2015 14:21:55 +0200 Subject: few mods few fixes TUI is not working yet... need to fix more stuff a checkpoint for now --- .../client/trex_stateless_client.py | 47 +++++++++++++++------- .../trex_control_plane/console/parsing_opts.py | 4 +- .../trex_control_plane/console/trex_console.py | 25 ++++-------- 3 files changed, 42 insertions(+), 34 deletions(-) (limited to 'scripts/automation/trex_control_plane/console') 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 93b36f82..011c9426 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -50,15 +50,18 @@ class RC: def err (self): return all([x.data if not x.rc else "" for x in self.rc_list]) - def annotate (self, desc): - print format_text('\n{:<40}'.format(desc), 'bold'), + def annotate (self, desc = None): + if desc: + print format_text('\n{:<40}'.format(desc), 'bold'), if self.bad(): # print all the errors + print "" for x in self.rc_list: if not x.rc: print format_text("\n{0}".format(x.data), 'bold') + print "" print format_text("[FAILED]\n", 'red', 'bold') @@ -274,16 +277,15 @@ class CTRexStatelessClient(object): ############# helper functions section ############## - def validate_port_list(self, port_id): - if isinstance(port_id, list) or isinstance(port_id, set): - # 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()): - return True - else: + def validate_port_list(self, port_id_list): + if not isinstance(port_id_list, list): + print type(port_id_list) return False + # check each item of the sequence + return all([ (port_id >= 0) and (port_id < self.get_port_count()) + for port_id in port_id_list ]) + # some preprocessing for port argument def __ports (self, port_id_list): @@ -355,6 +357,8 @@ class CTRexStatelessClient(object): def disconnect(self): self.connected = False self.comm_link.disconnect() + return RC_OK() + ########### cached queries (no server traffic) ########### @@ -384,6 +388,9 @@ class CTRexStatelessClient(object): def get_connection_port (self): return self.comm_link.port + def get_connection_ip (self): + return self.comm_link.server + def get_acquired_ports(self): return [port.port_id for port in self.ports if port.is_acquired()] @@ -543,10 +550,22 @@ class CTRexStatelessClient(object): ######################### Console (high level) API ######################### + def cmd_ping (self): + rc = self.ping() + rc.annotate("Pinging the server on '{0}' port '{1}': ".format(self.get_connection_ip(), self.get_connection_port())) + return rc + + def cmd_connect (self): + rc = self.connect() + rc.annotate() + return rc + + def cmd_disconnect (self): + rc = self.disconnect() + rc.annotate() + return rc + # reset - # acquire, stop, remove streams and clear stats - # - # def cmd_reset (self): @@ -631,7 +650,7 @@ class CTRexStatelessClient(object): return True - + ################################# # ------ private classes ------ # class CCommLink(object): """describes the connectivity of the stateless client method""" diff --git a/scripts/automation/trex_control_plane/console/parsing_opts.py b/scripts/automation/trex_control_plane/console/parsing_opts.py index 252d33bf..0e11df74 100755 --- a/scripts/automation/trex_control_plane/console/parsing_opts.py +++ b/scripts/automation/trex_control_plane/console/parsing_opts.py @@ -139,8 +139,8 @@ class CCmdArgParser(argparse.ArgumentParser): opts.ports = self.stateless_client.get_port_ids() for port in opts.ports: - if not self.stateless_client.validate_port_list(port): - self.error("port id {0} is not a valid port id\n".format(port)) + if not self.stateless_client.validate_port_list([port]): + self.error("port id '{0}' is not a valid port id\n".format(port)) return opts diff --git a/scripts/automation/trex_control_plane/console/trex_console.py b/scripts/automation/trex_control_plane/console/trex_console.py index 5ba82dcb..bd79479d 100755 --- a/scripts/automation/trex_control_plane/console/trex_console.py +++ b/scripts/automation/trex_control_plane/console/trex_console.py @@ -195,12 +195,8 @@ class TRexConsole(cmd.Cmd): def do_ping (self, line): '''Ping the server\n''' - rc = self.stateless_client.ping() - if rc.good(): - print format_text("[SUCCESS]\n", 'green', 'bold') - else: - print "\n*** " + rc.err() + "\n" - print format_text("[FAILED]\n", 'red', 'bold') + rc = self.stateless_client.cmd_ping() + if rc.bad(): return def do_test (self, line): @@ -230,25 +226,18 @@ class TRexConsole(cmd.Cmd): def do_connect (self, line): '''Connects to the server\n''' - rc = self.stateless_client.connect() - if rc.good(): - print format_text("[SUCCESS]\n", 'green', 'bold') - else: - print "\n*** " + rc.err() + "\n" - print format_text("[FAILED]\n", 'red', 'bold') + rc = self.stateless_client.cmd_connect() + if rc.bad(): return def do_disconnect (self, line): '''Disconnect from the server\n''' - if not self.stateless_client.is_connected(): - print "Not connected to server\n" + rc = self.stateless_client.cmd_disconnect() + if rc.bad(): return - self.stateless_client.disconnect() - print format_text("[SUCCESS]\n", 'green', 'bold') - ############### start @@ -332,7 +321,7 @@ class TRexConsole(cmd.Cmd): '''Shows a graphical console\n''' if not self.stateless_client.is_connected(): - print "Not connected to server\n" + print format_text("\nNot connected to server\n", 'bold') return self.do_verbose('off') -- cgit 1.2.3-korg From 2dee3346a79146b8f042ccb3f105498694fc6c4b Mon Sep 17 00:00:00 2001 From: imarom Date: Sun, 15 Nov 2015 14:40:21 +0200 Subject: clean shell / client added script for run --- .../client/trex_stateless_client.py | 246 +++++++++++++++++++-- .../trex_control_plane/console/parsing_opts.py | 9 +- .../trex_control_plane/console/trex_console.py | 150 ++++--------- 3 files changed, 268 insertions(+), 137 deletions(-) (limited to 'scripts/automation/trex_control_plane/console') 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 011c9426..0df2ac5d 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -6,6 +6,7 @@ try: except ImportError: # support import for Python 3 import client.outer_packages + from client_utils.jsonrpc_client import JsonRpcClient, BatchMessage from client_utils.packet_builder import CTRexPktBuilder import json @@ -13,6 +14,8 @@ from common.trex_stats import * from common.trex_streams import * from collections import namedtuple from common.text_opts import * +import parsing_opts +import time from trex_async_client import CTRexAsyncClient @@ -75,6 +78,66 @@ def RC_ERR (err): return RC(False, err) +LoadedStreamList = namedtuple('LoadedStreamList', ['loaded', 'compiled']) + +# describes a stream DB +class CStreamsDB(object): + + def __init__(self): + self.stream_packs = {} + + def load_yaml_file (self, filename): + + stream_pack_name = filename + if stream_pack_name in self.get_loaded_streams_names(): + self.remove_stream_packs(stream_pack_name) + + stream_list = CStreamList() + loaded_obj = stream_list.load_yaml(filename) + + try: + compiled_streams = stream_list.compile_streams() + rc = self.load_streams(stream_pack_name, + LoadedStreamList(loaded_obj, + [StreamPack(v.stream_id, v.stream.dump()) + for k, v in compiled_streams.items()])) + + except Exception as e: + return None + + return self.get_stream_pack(stream_pack_name) + + def load_streams(self, name, LoadedStreamList_obj): + if name in self.stream_packs: + return False + else: + self.stream_packs[name] = LoadedStreamList_obj + return True + + def remove_stream_packs(self, *names): + removed_streams = [] + for name in names: + removed = self.stream_packs.pop(name) + if removed: + removed_streams.append(name) + return removed_streams + + def clear(self): + self.stream_packs.clear() + + def get_loaded_streams_names(self): + return self.stream_packs.keys() + + def stream_pack_exists (self, name): + return name in self.get_loaded_streams_names() + + def get_stream_pack(self, name): + if not self.stream_pack_exists(name): + return None + else: + return self.stream_packs.get(name) + + # describes a single port class Port: @@ -96,6 +159,9 @@ class Port: def err (self, msg): return RC_ERR("port {0} : {1}".format(self.port_id, msg)) + def ok (self): + return RC_OK() + # take the port def acquire (self, force = False): params = {"port_id": self.port_id, @@ -106,9 +172,9 @@ class Port: rc = self.transmit(command.method, command.params) if rc.success: self.handler = rc.data - return RC_OK() + return self.ok() else: - return RC_ERR(rc.data) + return self.err(rc.data) # release the port @@ -120,9 +186,9 @@ class Port: rc = self.transmit(command.method, command.params) if rc.success: self.handler = rc.data - return RC_OK() + return self.ok() else: - return RC_ERR(rc.data) + return self.err(rc.data) def is_acquired (self): return (self.handler != None) @@ -147,7 +213,7 @@ class Port: else: raise Exception("port {0}: bad state received from server '{1}'".format(self.port_id, sync_data['state'])) - return RC_OK() + return self.ok() # return TRUE if write commands @@ -178,7 +244,7 @@ class Port: # the only valid state now self.state = self.STATE_STREAMS - return RC_OK() + return self.ok() # remove stream from port def remove_stream (self, stream_id): @@ -197,7 +263,7 @@ class Port: self.streams[stream_id] = None - return RC_OK() + return self.ok() # remove all the streams def remove_all_streams (self): @@ -211,7 +277,7 @@ class Port: self.streams = {} - return RC_OK() + return self.ok() # start traffic def start (self, mul): @@ -234,7 +300,7 @@ class Port: self.state = self.STATE_TX - return RC_OK() + return self.ok() # stop traffic # with force ignores the cached state and sends the command @@ -253,7 +319,7 @@ class Port: # only valid state after stop self.state = self.STATE_STREAMS - return RC_OK() + return self.ok() @@ -273,6 +339,8 @@ class CTRexStatelessClient(object): self._async_client = CTRexAsyncClient(async_port) + self.streams_db = CStreamsDB() + self.connected = False ############# helper functions section ############## @@ -605,15 +673,16 @@ class CTRexStatelessClient(object): active_ports = list(set(self.get_active_ports()).intersection(port_id_list)) if not active_ports: - print format_text("No active traffic on porvided ports", 'bold') - return True + msg = "No active traffic on porvided ports" + print format_text(msg, 'bold') + return RC_ERR(msg) rc = self.stop_traffic(active_ports) rc.annotate("Stopping traffic on port(s) {0}:".format(port_id_list)) if rc.bad(): - return False + return rc - return True + return RC_OK() # start cmd def cmd_start (self, port_id_list, stream_list, mult, force): @@ -622,31 +691,161 @@ class CTRexStatelessClient(object): if active_ports: if not force: - print format_text("Port(s) {0} are active - please stop them or add '--force'".format(active_ports), 'bold') - return False + msg = "Port(s) {0} are active - please stop them or add '--force'".format(active_ports) + print format_text(msg, 'bold') + return RC_ERR(msg) else: rc = self.cmd_stop(active_ports) if not rc: - return False + return rc rc = self.remove_all_streams(port_id_list) - rc.annotate("Removing all streams from ports {0}:".format(port_id_list)) + rc.annotate("Removing all streams from port(s) {0}:".format(port_id_list)) if rc.bad(): - return False + return rc rc = self.add_stream_pack(stream_list.compiled, port_id_list) - rc.annotate("Attaching streams to port {0}:".format(port_id_list)) + rc.annotate("Attaching streams to port(s) {0}:".format(port_id_list)) if rc.bad(): - return False + return rc # finally, start the traffic rc = self.start_traffic(mult, port_id_list) - rc.annotate("Starting traffic on ports {0}:".format(port_id_list)) + rc.annotate("Starting traffic on port(s) {0}:".format(port_id_list)) if rc.bad(): - return False + return rc + + return RC_OK() + + ############## High Level API With Parser ################ + def cmd_start_line (self, line): + '''Start selected traffic in specified ports on TRex\n''' + # define a parser + parser = parsing_opts.gen_parser(self, + "start", + self.cmd_start_line.__doc__, + parsing_opts.PORT_LIST_WITH_ALL, + parsing_opts.FORCE, + parsing_opts.STREAM_FROM_PATH_OR_FILE, + parsing_opts.DURATION, + parsing_opts.MULTIPLIER) + + opts = parser.parse_args(line.split()) + + if opts is None: + return RC_ERR("bad command line paramters") + + if opts.db: + stream_list = self.stream_db.get_stream_pack(opts.db) + rc = RC(stream_list != None) + rc.annotate("Load stream pack (from DB):") + if rc.bad(): + return RC_ERR("Failed to load stream pack") + + else: + # load streams from file + stream_list = self.streams_db.load_yaml_file(opts.file[0]) + rc = RC(stream_list != None) + rc.annotate("Load stream pack (from file):") + if stream_list == None: + return RC_ERR("Failed to load stream pack") + + + return self.cmd_start(opts.ports, stream_list, opts.mult, opts.force) + + def cmd_stop_line (self, line): + '''Stop active traffic in specified ports on TRex\n''' + parser = parsing_opts.gen_parser(self, + "stop", + self.cmd_stop_line.__doc__, + parsing_opts.PORT_LIST_WITH_ALL) + + opts = parser.parse_args(line.split()) + if opts is None: + return RC_ERR("bad command line paramters") + + return self.cmd_stop(opts.ports) + + + def cmd_reset_line (self, line): + return self.cmd_reset() + + + def cmd_exit_line (self, line): + print format_text("Exiting\n", 'bold') + # a way to exit + return RC_ERR("exit") + + + def cmd_wait_line (self, line): + '''wait for a period of time\n''' + + parser = parsing_opts.gen_parser(self, + "wait", + self.cmd_wait_line.__doc__, + parsing_opts.DURATION) + + opts = parser.parse_args(line.split()) + if opts is None: + return RC_ERR("bad command line paramters") + + delay_sec = opts.d if opts.d else 1 + + print format_text("Waiting for {0} seconds...\n".format(delay_sec), 'bold') + time.sleep(delay_sec) + + return RC_OK() + + # run a script of commands + def run_script_file (self, filename): + + print format_text("\nRunning script file '{0}'...".format(filename), 'bold') + + rc = self.cmd_connect() + if rc.bad(): + return + + with open(filename) as f: + script_lines = f.readlines() + + cmd_table = {} + + # register all the commands + cmd_table['start'] = self.cmd_start_line + cmd_table['stop'] = self.cmd_stop_line + cmd_table['reset'] = self.cmd_reset_line + cmd_table['wait'] = self.cmd_wait_line + cmd_table['exit'] = self.cmd_exit_line + + for index, line in enumerate(script_lines): + line = line.strip() + if line == "": + continue + if line.startswith("#"): + continue + + sp = line.split(' ', 1) + cmd = sp[0] + if len(sp) == 2: + args = sp[1] + else: + args = "" + + print format_text("Executing line {0} : '{1}'\n".format(index, line)) + + if not cmd in cmd_table: + print "\n*** Error at line {0} : '{1}'\n".format(index, line) + print format_text("unknown command '{0}'\n".format(cmd), 'bold') + return False + + rc = cmd_table[cmd](args) + if rc.bad(): + return False + + print format_text("\n[Done]", 'bold') return True @@ -710,3 +909,4 @@ class CTRexStatelessClient(object): if __name__ == "__main__": pass + diff --git a/scripts/automation/trex_control_plane/console/parsing_opts.py b/scripts/automation/trex_control_plane/console/parsing_opts.py index 0e11df74..c154ce24 100755 --- a/scripts/automation/trex_control_plane/console/parsing_opts.py +++ b/scripts/automation/trex_control_plane/console/parsing_opts.py @@ -135,12 +135,13 @@ class CCmdArgParser(argparse.ArgumentParser): if opts is None: return None - if opts.all_ports: + if getattr(opts, "all_ports", None): opts.ports = self.stateless_client.get_port_ids() - for port in opts.ports: - if not self.stateless_client.validate_port_list([port]): - self.error("port id '{0}' is not a valid port id\n".format(port)) + if getattr(opts, "ports", None): + for port in opts.ports: + if not self.stateless_client.validate_port_list([port]): + self.error("port id '{0}' is not a valid port id\n".format(port)) return opts diff --git a/scripts/automation/trex_control_plane/console/trex_console.py b/scripts/automation/trex_control_plane/console/trex_console.py index 7931dfc9..88e8dede 100755 --- a/scripts/automation/trex_control_plane/console/trex_console.py +++ b/scripts/automation/trex_control_plane/console/trex_console.py @@ -30,72 +30,12 @@ import tty, termios import trex_root_path from common.trex_streams import * from client.trex_stateless_client import CTRexStatelessClient -from client.trex_stateless_client import RpcResponseStatus from common.text_opts import * from client_utils.general_utils import user_input, get_current_user -import parsing_opts import trex_status -from collections import namedtuple -__version__ = "1.0" - -LoadedStreamList = namedtuple('LoadedStreamList', ['loaded', 'compiled']) - -class CStreamsDB(object): - - def __init__(self): - self.stream_packs = {} - - def load_yaml_file (self, filename): - - stream_pack_name = filename - if stream_pack_name in self.get_loaded_streams_names(): - self.remove_stream_packs(stream_pack_name) - - stream_list = CStreamList() - loaded_obj = stream_list.load_yaml(filename) - - try: - compiled_streams = stream_list.compile_streams() - rc = self.load_streams(stream_pack_name, - LoadedStreamList(loaded_obj, - [StreamPack(v.stream_id, v.stream.dump()) - for k, v in compiled_streams.items()])) - - except Exception as e: - return None - - return self.get_stream_pack(stream_pack_name) - - def load_streams(self, name, LoadedStreamList_obj): - if name in self.stream_packs: - return False - else: - self.stream_packs[name] = LoadedStreamList_obj - return True - - def remove_stream_packs(self, *names): - removed_streams = [] - for name in names: - removed = self.stream_packs.pop(name) - if removed: - removed_streams.append(name) - return removed_streams - - def clear(self): - self.stream_packs.clear() - - def get_loaded_streams_names(self): - return self.stream_packs.keys() - def stream_pack_exists (self, name): - return name in self.get_loaded_streams_names() - - def get_stream_pack(self, name): - if not self.stream_pack_exists(name): - return None - else: - return self.stream_packs.get(name) +__version__ = "1.0" # @@ -111,15 +51,11 @@ class TRexConsole(cmd.Cmd): self.verbose = verbose self.acquire_all_ports = acquire_all_ports - self.do_connect("") - self.intro = "\n-=TRex Console v{ver}=-\n".format(ver=__version__) self.intro += "\nType 'help' or '?' for supported actions\n" self.postcmd(False, "") - self.streams_db = CStreamsDB() - ################### internal section ######################## @@ -155,10 +91,21 @@ class TRexConsole(cmd.Cmd): path = dir else: path = "." + + start_string = os.path.basename(text) - return [x - for x in os.listdir(path) - if x.startswith(start_string)] + + targets = [] + + for x in os.listdir(path): + if x.startswith(start_string): + y = os.path.join(path, x) + if os.path.isfile(y): + targets.append(x + ' ') + elif os.path.isdir(y): + targets.append(x + '/') + + return targets # annotation method @staticmethod @@ -256,37 +203,7 @@ class TRexConsole(cmd.Cmd): def do_start(self, line): '''Start selected traffic in specified ports on TRex\n''' - # make sure that the user wants to acquire all - parser = parsing_opts.gen_parser(self.stateless_client, - "start", - self.do_start.__doc__, - parsing_opts.PORT_LIST_WITH_ALL, - parsing_opts.FORCE, - parsing_opts.STREAM_FROM_PATH_OR_FILE, - parsing_opts.DURATION, - parsing_opts.MULTIPLIER) - - opts = parser.parse_args(line.split()) - - if opts is None: - return - - if opts.db: - stream_list = self.stream_db.get_stream_pack(opts.db) - self.annotate("Load stream pack (from DB):", (stream_list != None)) - if stream_list == None: - return - - else: - # load streams from file - stream_list = self.streams_db.load_yaml_file(opts.file[0]) - self.annotate("Load stream pack (from file):", (stream_list != None)) - if stream_list == None: - return - - - self.stateless_client.cmd_start(opts.ports, stream_list, opts.mult, opts.force) - return + self.stateless_client.cmd_start_line(line) def help_start(self): @@ -294,18 +211,9 @@ class TRexConsole(cmd.Cmd): ############# stop def do_stop(self, line): - '''Stop active traffic in specified ports on TRex\n''' - parser = parsing_opts.gen_parser(self.stateless_client, - "stop", - self.do_stop.__doc__, - parsing_opts.PORT_LIST_WITH_ALL) - - opts = parser.parse_args(line.split()) - if opts is None: - return + self.stateless_client.cmd_stop_line(line) - self.stateless_client.cmd_stop(opts.ports) - return + def help_stop(self): self.do_stop("-h") @@ -373,6 +281,14 @@ class TRexConsole(cmd.Cmd): do_exit = do_EOF = do_q = do_quit +# +def is_valid_file(filename): + if not os.path.isfile(filename): + raise argparse.ArgumentTypeError("The file '%s' does not exist" % filename) + + return filename + + def setParserOptions(): parser = argparse.ArgumentParser(prog="trex_console.py") @@ -402,6 +318,12 @@ def setParserOptions(): action="store_false", help="Acquire all ports on connect. Default is: ON.", default = True) + parser.add_argument("--batch", dest="batch", + nargs = 1, + type = is_valid_file, + help = "Run the console in a batch mode with file", + default = None) + return parser @@ -411,7 +333,15 @@ def main(): # Stateless client connection stateless_client = CTRexStatelessClient(options.user, options.server, options.port, options.pub) + rc = stateless_client.cmd_connect() + if rc.bad(): + return + if options.batch: + cont = stateless_client.run_script_file(options.batch[0]) + if not cont: + return + # console try: console = TRexConsole(stateless_client, options.acquire, options.verbose) -- cgit 1.2.3-korg From 56becbc13bc2edc1fe60afb6d788357a70147a43 Mon Sep 17 00:00:00 2001 From: imarom Date: Sun, 15 Nov 2015 16:34:16 +0200 Subject: few fixes to ZMQ client (timeout values) --- .../client/trex_stateless_client.py | 4 +- .../client_utils/jsonrpc_client.py | 56 +++++++++++----------- .../trex_control_plane/console/trex_console.py | 1 + 3 files changed, 32 insertions(+), 29 deletions(-) (limited to 'scripts/automation/trex_control_plane/console') 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 0df2ac5d..443466b2 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -419,7 +419,7 @@ class CTRexStatelessClient(object): return RC_OK() def is_connected (self): - return self.connected + return self.connected and self.comm_link.is_connected def disconnect(self): @@ -820,7 +820,7 @@ class CTRexStatelessClient(object): cmd_table['wait'] = self.cmd_wait_line cmd_table['exit'] = self.cmd_exit_line - for index, line in enumerate(script_lines): + for index, line in enumerate(script_lines, start = 1): line = line.strip() if line == "": continue diff --git a/scripts/automation/trex_control_plane/client_utils/jsonrpc_client.py b/scripts/automation/trex_control_plane/client_utils/jsonrpc_client.py index 077c82ad..b826f02f 100755 --- a/scripts/automation/trex_control_plane/client_utils/jsonrpc_client.py +++ b/scripts/automation/trex_control_plane/client_utils/jsonrpc_client.py @@ -110,45 +110,45 @@ class JsonRpcClient(object): return id, msg - def invoke_rpc_method (self, method_name, params = {}, block = True): + def invoke_rpc_method (self, method_name, params = {}): if not self.connected: return False, "Not connected to server" id, msg = self.create_jsonrpc_v2(method_name, params) - return self.send_raw_msg(msg, block) + return self.send_raw_msg(msg) # low level send of string message - def send_raw_msg (self, msg, block = True): + def send_raw_msg (self, msg): + self.verbose_msg("Sending Request To Server:\n\n" + self.pretty_json(msg) + "\n") - if block: - self.socket.send(msg) - else: + tries = 0 + while True: try: - self.socket.send(msg, flags = zmq.NOBLOCK) - except zmq.error.ZMQError as e: - self.disconnect() - return CmdResponse(False, "Failed To Get Send Message") - - got_response = False + self.socket.send(msg) + break + except zmq.Again: + sleep(0.1) + tries += 1 + if tries > 10: + self.disconnect() + return CmdResponse(False, "Failed to send message to server") + + + tries = 0 + while True: + try: + response = self.socket.recv() + break + except zmq.Again: + sleep(0.1) + tries += 1 + if tries > 10: + self.disconnect() + return CmdResponse(False, "Failed to get server response") - if block: - response = self.socket.recv() - got_response = True - else: - for i in xrange(0 ,10): - try: - response = self.socket.recv(flags = zmq.NOBLOCK) - got_response = True - break - except zmq.Again: - sleep(0.2) - - if not got_response: - self.disconnect() - return CmdResponse(False, "Failed To Get Server Response") self.verbose_msg("Server Response:\n\n" + self.pretty_json(response) + "\n") @@ -223,6 +223,8 @@ class JsonRpcClient(object): except zmq.error.ZMQError as e: return False, "ZMQ Error: Bad server or port name: " + str(e) + self.socket.setsockopt(zmq.SNDTIMEO, 5) + self.socket.setsockopt(zmq.RCVTIMEO, 5) self.connected = True diff --git a/scripts/automation/trex_control_plane/console/trex_console.py b/scripts/automation/trex_control_plane/console/trex_console.py index 88e8dede..7cb65fa6 100755 --- a/scripts/automation/trex_control_plane/console/trex_console.py +++ b/scripts/automation/trex_control_plane/console/trex_console.py @@ -33,6 +33,7 @@ from client.trex_stateless_client import CTRexStatelessClient from common.text_opts import * from client_utils.general_utils import user_input, get_current_user import trex_status +import parsing_opts __version__ = "1.0" -- cgit 1.2.3-korg From 94b1238942da24e47fb3e689bf695e815a604eb0 Mon Sep 17 00:00:00 2001 From: imarom Date: Sun, 15 Nov 2015 18:15:14 +0200 Subject: added duration to the RPC server (and all the way to the DP) *STILL NEEDS FIXING THE DP STOP SCHED MESSAGE" --- .../trex_control_plane/client/trex_stateless_client.py | 15 ++++++++------- .../automation/trex_control_plane/console/parsing_opts.py | 7 +++++-- src/gtest/trex_stateless_gtest.cpp | 15 +++++---------- src/rpc-server/commands/trex_rpc_cmd_stream.cpp | 7 ++++--- src/rpc-server/commands/trex_rpc_cmds.h | 2 +- src/stateless/cp/trex_stateless_port.cpp | 4 ++-- src/stateless/cp/trex_stateless_port.h | 2 +- src/stateless/cp/trex_streams_compiler.cpp | 3 --- src/stateless/cp/trex_streams_compiler.h | 8 -------- src/stateless/dp/trex_stateless_dp_core.cpp | 6 ++---- src/stateless/dp/trex_stateless_dp_core.h | 2 +- src/stateless/messaging/trex_stateless_messaging.cpp | 6 +++--- src/stateless/messaging/trex_stateless_messaging.h | 3 ++- 13 files changed, 34 insertions(+), 46 deletions(-) (limited to 'scripts/automation/trex_control_plane/console') 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 443466b2..4478ed3f 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -280,7 +280,7 @@ class Port: return self.ok() # start traffic - def start (self, mul): + def start (self, mul, duration): if self.state == self.STATE_DOWN: return self.err("Unable to start traffic - port is down") @@ -292,7 +292,8 @@ class Port: params = {"handler": self.handler, "port_id": self.port_id, - "mul": mul} + "mul": mul, + "duration": duration} rc, data = self.transmit("start_traffic", params) if not rc: @@ -580,14 +581,14 @@ class CTRexStatelessClient(object): return self.ports[port_id].get_stream_id_list() - def start_traffic (self, multiplier, port_id_list = None): + def start_traffic (self, multiplier, duration, port_id_list = None): port_id_list = self.__ports(port_id_list) rc = RC() for port_id in port_id_list: - rc.add(self.ports[port_id].start(multiplier)) + rc.add(self.ports[port_id].start(multiplier, duration)) return rc @@ -685,7 +686,7 @@ class CTRexStatelessClient(object): return RC_OK() # start cmd - def cmd_start (self, port_id_list, stream_list, mult, force): + def cmd_start (self, port_id_list, stream_list, mult, force, duration): active_ports = list(set(self.get_active_ports()).intersection(port_id_list)) @@ -713,7 +714,7 @@ class CTRexStatelessClient(object): # finally, start the traffic - rc = self.start_traffic(mult, port_id_list) + rc = self.start_traffic(mult, duration, port_id_list) rc.annotate("Starting traffic on port(s) {0}:".format(port_id_list)) if rc.bad(): return rc @@ -754,7 +755,7 @@ class CTRexStatelessClient(object): return RC_ERR("Failed to load stream pack") - return self.cmd_start(opts.ports, stream_list, opts.mult, opts.force) + return self.cmd_start(opts.ports, stream_list, opts.mult, opts.force, opts.duration) def cmd_stop_line (self, line): '''Stop active traffic in specified ports on TRex\n''' diff --git a/scripts/automation/trex_control_plane/console/parsing_opts.py b/scripts/automation/trex_control_plane/console/parsing_opts.py index c154ce24..d5c21af0 100755 --- a/scripts/automation/trex_control_plane/console/parsing_opts.py +++ b/scripts/automation/trex_control_plane/console/parsing_opts.py @@ -89,10 +89,13 @@ OPTIONS_DB = {MULTIPLIER: ArgumentPack(['-m', '--multiplier'], "dest": "all_ports", 'help': "Set this flag to apply the command on all available ports"}), DURATION: ArgumentPack(['-d'], - {"action": "store", + {'action': "store", 'metavar': 'TIME', - "type": match_time_unit, + 'dest': 'duration', + 'type': match_time_unit, + 'default': -1.0, 'help': "Set duration time for TRex."}), + FORCE: ArgumentPack(['--force'], {"action": "store_true", 'default': False, diff --git a/src/gtest/trex_stateless_gtest.cpp b/src/gtest/trex_stateless_gtest.cpp index 8b96ef88..c845c32e 100644 --- a/src/gtest/trex_stateless_gtest.cpp +++ b/src/gtest/trex_stateless_gtest.cpp @@ -252,10 +252,9 @@ TEST_F(basic_stl, single_pkt_burst1) { TrexStreamsCompiledObj comp_obj(0,1.0); - comp_obj.set_simulation_duration( 10.0); assert(compile.compile(streams, comp_obj) ); - TrexStatelessDpStart * lpstart = new TrexStatelessDpStart( comp_obj.clone() ); + TrexStatelessDpStart * lpstart = new TrexStatelessDpStart( comp_obj.clone(), 10.0 ); t1.m_msg = lpstart; @@ -303,10 +302,9 @@ TEST_F(basic_stl, single_pkt) { TrexStreamsCompiledObj comp_obj(0,1.0); - comp_obj.set_simulation_duration( 10.0); assert(compile.compile(streams, comp_obj) ); - TrexStatelessDpStart * lpstart = new TrexStatelessDpStart( comp_obj.clone() ); + TrexStatelessDpStart * lpstart = new TrexStatelessDpStart( comp_obj.clone(), 10 ); t1.m_msg = lpstart; @@ -361,10 +359,9 @@ TEST_F(basic_stl, multi_pkt1) { // stream - clean TrexStreamsCompiledObj comp_obj(0,1.0); - comp_obj.set_simulation_duration( 10.0); assert(compile.compile(streams, comp_obj) ); - TrexStatelessDpStart * lpstart = new TrexStatelessDpStart( comp_obj.clone() ); + TrexStatelessDpStart * lpstart = new TrexStatelessDpStart( comp_obj.clone(), 10 ); t1.m_msg = lpstart; @@ -425,10 +422,9 @@ TEST_F(basic_stl, multi_pkt2) { // stream - clean TrexStreamsCompiledObj comp_obj(0,5.0); - comp_obj.set_simulation_duration( 10.0); assert(compile.compile(streams, comp_obj) ); - TrexStatelessDpStart * lpstart = new TrexStatelessDpStart( comp_obj.clone() ); + TrexStatelessDpStart * lpstart = new TrexStatelessDpStart( comp_obj.clone(), 10 ); t1.m_msg = lpstart; @@ -472,10 +468,9 @@ TEST_F(basic_stl, multi_burst1) { TrexStreamsCompiledObj comp_obj(0,1.0); - comp_obj.set_simulation_duration( 40.0); assert(compile.compile(streams, comp_obj) ); - TrexStatelessDpStart * lpstart = new TrexStatelessDpStart( comp_obj.clone() ); + TrexStatelessDpStart * lpstart = new TrexStatelessDpStart( comp_obj.clone(), 40 ); t1.m_msg = lpstart; diff --git a/src/rpc-server/commands/trex_rpc_cmd_stream.cpp b/src/rpc-server/commands/trex_rpc_cmd_stream.cpp index e32073b0..5ec92afc 100644 --- a/src/rpc-server/commands/trex_rpc_cmd_stream.cpp +++ b/src/rpc-server/commands/trex_rpc_cmd_stream.cpp @@ -458,8 +458,9 @@ TrexRpcCmdGetStream::_run(const Json::Value ¶ms, Json::Value &result) { trex_rpc_cmd_rc_e TrexRpcCmdStartTraffic::_run(const Json::Value ¶ms, Json::Value &result) { - uint8_t port_id = parse_byte(params, "port_id", result); - double mul = parse_double(params, "mul", result); + uint8_t port_id = parse_byte(params, "port_id", result); + double mul = parse_double(params, "mul", result); + double duration = parse_double(params, "duration", result); if (port_id >= get_stateless_obj()->get_port_count()) { std::stringstream ss; @@ -470,7 +471,7 @@ TrexRpcCmdStartTraffic::_run(const Json::Value ¶ms, Json::Value &result) { TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); try { - port->start_traffic(mul); + port->start_traffic(mul, duration); } catch (const TrexRpcException &ex) { generate_execute_err(result, ex.what()); } diff --git a/src/rpc-server/commands/trex_rpc_cmds.h b/src/rpc-server/commands/trex_rpc_cmds.h index d7265ff2..b4f37e3b 100644 --- a/src/rpc-server/commands/trex_rpc_cmds.h +++ b/src/rpc-server/commands/trex_rpc_cmds.h @@ -105,7 +105,7 @@ TREX_RPC_CMD_DEFINE(TrexRpcCmdGetStream, "get_stream", 3, true); -TREX_RPC_CMD_DEFINE(TrexRpcCmdStartTraffic, "start_traffic", 2, true); +TREX_RPC_CMD_DEFINE(TrexRpcCmdStartTraffic, "start_traffic", 3, true); TREX_RPC_CMD_DEFINE(TrexRpcCmdStopTraffic, "stop_traffic", 1, true); TREX_RPC_CMD_DEFINE(TrexRpcCmdPauseTraffic, "pause_traffic", 1, true); TREX_RPC_CMD_DEFINE(TrexRpcCmdResumeTraffic, "resume_traffic", 1, true); diff --git a/src/stateless/cp/trex_stateless_port.cpp b/src/stateless/cp/trex_stateless_port.cpp index 7f2382d3..cbc5a328 100644 --- a/src/stateless/cp/trex_stateless_port.cpp +++ b/src/stateless/cp/trex_stateless_port.cpp @@ -86,7 +86,7 @@ TrexStatelessPort::release(void) { * */ void -TrexStatelessPort::start_traffic(double mul) { +TrexStatelessPort::start_traffic(double mul, double duration) { /* command allowed only on state stream */ verify_state(PORT_STATE_STREAMS); @@ -105,7 +105,7 @@ TrexStatelessPort::start_traffic(double mul) { } /* generate a message to all the relevant DP cores to start transmitting */ - TrexStatelessCpToDpMsgBase *start_msg = new TrexStatelessDpStart(compiled_obj); + TrexStatelessCpToDpMsgBase *start_msg = new TrexStatelessDpStart(compiled_obj, duration); send_message_to_dp(start_msg); diff --git a/src/stateless/cp/trex_stateless_port.h b/src/stateless/cp/trex_stateless_port.h index 90bf936e..b533f793 100644 --- a/src/stateless/cp/trex_stateless_port.h +++ b/src/stateless/cp/trex_stateless_port.h @@ -72,7 +72,7 @@ public: * start traffic * throws TrexException in case of an error */ - void start_traffic(double mul); + void start_traffic(double mul, double duration = -1); /** * stop traffic diff --git a/src/stateless/cp/trex_streams_compiler.cpp b/src/stateless/cp/trex_streams_compiler.cpp index 80cdb31c..580db51c 100644 --- a/src/stateless/cp/trex_streams_compiler.cpp +++ b/src/stateless/cp/trex_streams_compiler.cpp @@ -27,7 +27,6 @@ limitations under the License. * stream compiled object *************************************/ TrexStreamsCompiledObj::TrexStreamsCompiledObj(uint8_t port_id, double mul) : m_port_id(port_id), m_mul(mul) { - m_duration_sim=-1.0; } TrexStreamsCompiledObj::~TrexStreamsCompiledObj() { @@ -61,8 +60,6 @@ TrexStreamsCompiledObj::clone() { new_compiled_obj->m_mul = m_mul; - new_compiled_obj->m_duration_sim = m_duration_sim; - return new_compiled_obj; } diff --git a/src/stateless/cp/trex_streams_compiler.h b/src/stateless/cp/trex_streams_compiler.h index 78ac1ac7..44c8a0fc 100644 --- a/src/stateless/cp/trex_streams_compiler.h +++ b/src/stateless/cp/trex_streams_compiler.h @@ -48,13 +48,6 @@ public: return m_objs; } - void set_simulation_duration(double duration){ - m_duration_sim=duration; - } - - double get_simulation_duration(){ - return (m_duration_sim); - } /** * clone the compiled object * @@ -71,7 +64,6 @@ private: uint8_t m_port_id; double m_mul; - double m_duration_sim; /* duration for all simulation */ }; class TrexStreamsCompiler { diff --git a/src/stateless/dp/trex_stateless_dp_core.cpp b/src/stateless/dp/trex_stateless_dp_core.cpp index 96c18dbd..eabd6fdb 100644 --- a/src/stateless/dp/trex_stateless_dp_core.cpp +++ b/src/stateless/dp/trex_stateless_dp_core.cpp @@ -207,14 +207,12 @@ TrexStatelessDpCore::add_cont_stream(TrexStream * stream, } void -TrexStatelessDpCore::start_traffic(TrexStreamsCompiledObj *obj) { +TrexStatelessDpCore::start_traffic(TrexStreamsCompiledObj *obj, double duration) { for (auto single_stream : obj->get_objects()) { add_cont_stream(single_stream.m_stream,obj); } - double duration=obj->get_simulation_duration(); - - if ( duration >0.0){ + if ( duration > 0.0 ){ add_duration( duration ); } } diff --git a/src/stateless/dp/trex_stateless_dp_core.h b/src/stateless/dp/trex_stateless_dp_core.h index 1029213d..7448d215 100644 --- a/src/stateless/dp/trex_stateless_dp_core.h +++ b/src/stateless/dp/trex_stateless_dp_core.h @@ -75,7 +75,7 @@ public: * @param pkt * @param pkt_len */ - void start_traffic(TrexStreamsCompiledObj *obj); + void start_traffic(TrexStreamsCompiledObj *obj, double duration = -1); /** * stop all traffic for this core diff --git a/src/stateless/messaging/trex_stateless_messaging.cpp b/src/stateless/messaging/trex_stateless_messaging.cpp index 032559bc..d8ebc52c 100644 --- a/src/stateless/messaging/trex_stateless_messaging.cpp +++ b/src/stateless/messaging/trex_stateless_messaging.cpp @@ -26,7 +26,7 @@ limitations under the License. /************************* start traffic message ************************/ -TrexStatelessDpStart::TrexStatelessDpStart(TrexStreamsCompiledObj *obj) : m_obj(obj) { +TrexStatelessDpStart::TrexStatelessDpStart(TrexStreamsCompiledObj *obj, double duration) : m_obj(obj), m_duration(duration) { } @@ -39,7 +39,7 @@ TrexStatelessDpStart::clone() { TrexStreamsCompiledObj *new_obj = m_obj->clone(); - TrexStatelessCpToDpMsgBase *new_msg = new TrexStatelessDpStart(new_obj); + TrexStatelessCpToDpMsgBase *new_msg = new TrexStatelessDpStart(new_obj, m_duration); return new_msg; } @@ -53,7 +53,7 @@ TrexStatelessDpStart::~TrexStatelessDpStart() { bool TrexStatelessDpStart::handle(TrexStatelessDpCore *dp_core) { - dp_core->start_traffic(m_obj); + dp_core->start_traffic(m_obj, m_duration); return true; } diff --git a/src/stateless/messaging/trex_stateless_messaging.h b/src/stateless/messaging/trex_stateless_messaging.h index d288fc83..90897665 100644 --- a/src/stateless/messaging/trex_stateless_messaging.h +++ b/src/stateless/messaging/trex_stateless_messaging.h @@ -66,7 +66,7 @@ public: class TrexStatelessDpStart : public TrexStatelessCpToDpMsgBase { public: - TrexStatelessDpStart(TrexStreamsCompiledObj *obj); + TrexStatelessDpStart(TrexStreamsCompiledObj *obj, double duration); ~TrexStatelessDpStart(); @@ -77,6 +77,7 @@ public: private: TrexStreamsCompiledObj *m_obj; + double m_duration; }; /** -- cgit 1.2.3-korg From bb6dec2ed238069b6a0c079d2031246704b717c4 Mon Sep 17 00:00:00 2001 From: Dan Klein Date: Mon, 16 Nov 2015 21:59:31 +0200 Subject: created general trex console class added dynamic server async support fixed bugs --- .../trex_control_plane/client/trex_async_client.py | 5 +++-- .../trex_control_plane/client/trex_stateless_client.py | 2 +- .../trex_control_plane/console/trex_console.py | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) (limited to 'scripts/automation/trex_control_plane/console') 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 d13513bf..31bec93f 100644 --- a/scripts/automation/trex_control_plane/client/trex_async_client.py +++ b/scripts/automation/trex_control_plane/client/trex_async_client.py @@ -151,16 +151,17 @@ class TrexAsyncStatsManager(): class CTRexAsyncClient(): - def __init__ (self, port): + def __init__ (self, server, port): self.port = port + self.server = server self.raw_snapshot = {} self.stats = TrexAsyncStatsManager() - self.tr = "tcp://localhost:{0}".format(self.port) + self.tr = "tcp://{0}:{1}".format(self.server, self.port) print "\nConnecting To ZMQ Publisher At {0}".format(self.tr) self.active = True 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 4478ed3f..21f62ece 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -338,7 +338,7 @@ class CTRexStatelessClient(object): self._server_version = None self.__err_log = None - self._async_client = CTRexAsyncClient(async_port) + self._async_client = CTRexAsyncClient(server, async_port) self.streams_db = CStreamsDB() diff --git a/scripts/automation/trex_control_plane/console/trex_console.py b/scripts/automation/trex_control_plane/console/trex_console.py index 7cb65fa6..8e8b25ec 100755 --- a/scripts/automation/trex_control_plane/console/trex_console.py +++ b/scripts/automation/trex_control_plane/console/trex_console.py @@ -39,6 +39,24 @@ import parsing_opts __version__ = "1.0" +class TRexGeneralCmd(cmd.Cmd): + def __init__(self): + cmd.Cmd.__init__(self) + + def emptyline(self): + """Called when an empty line is entered in response to the prompt. + + This overriding is such that when empty line is passed, **nothing happens**. + """ + return + + def completenames(self, text, *ignored): + """ + This overriding is such that a space is added to name completion. + """ + dotext = 'do_'+text + return [a[3:]+' ' for a in self.get_names() if a.startswith(dotext)] + # # main console object class TRexConsole(cmd.Cmd): -- cgit 1.2.3-korg From 84e9d7a4a8bbb3afb4861652e2d56bc27097f794 Mon Sep 17 00:00:00 2001 From: Dan Klein Date: Thu, 19 Nov 2015 02:30:48 +0200 Subject: History feature is DONE Fixed bugs Cleaned code, more like PEP8 --- .../client/trex_stateless_client.py | 55 ++++++++++------------ .../trex_control_plane/console/trex_console.py | 52 ++++++++++++++++---- 2 files changed, 68 insertions(+), 39 deletions(-) (limited to 'scripts/automation/trex_control_plane/console') 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 21f62ece..3dcfae28 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -139,8 +139,7 @@ class CStreamsDB(object): # describes a single port -class Port: - +class Port(object): STATE_DOWN = 0 STATE_IDLE = 1 STATE_STREAMS = 2 @@ -156,14 +155,14 @@ class Port: self.streams = {} - def err (self, msg): + def err(self, msg): return RC_ERR("port {0} : {1}".format(self.port_id, msg)) - def ok (self): + def ok(self): return RC_OK() # take the port - def acquire (self, force = False): + def acquire(self, force = False): params = {"port_id": self.port_id, "user": self.user, "force": force} @@ -178,7 +177,7 @@ class Port: # release the port - def release (self): + def release(self): params = {"port_id": self.port_id, "handler": self.handler} @@ -190,25 +189,24 @@ class Port: else: return self.err(rc.data) - def is_acquired (self): + def is_acquired(self): return (self.handler != None) - def is_active (self): - return (self.state == self.STATE_TX ) or (self.state == self.STATE_PAUSE) - - def sync (self, sync_data): + def is_active(self): + return(self.state == self.STATE_TX ) or (self.state == self.STATE_PAUSE) + def sync(self, sync_data): self.handler = sync_data['handler'] - - if sync_data['state'] == "DOWN": + port_state = sync_data['state'].upper() + if port_state == "DOWN": self.state = self.STATE_DOWN - elif sync_data['state'] == "IDLE": + elif port_state == "IDLE": self.state = self.STATE_IDLE - elif sync_data['state'] == "STREAMS": + elif port_state == "STREAMS": self.state = self.STATE_STREAMS - elif sync_data['state'] == "TX": + elif port_state == "TX": self.state = self.STATE_TX - elif sync_data['state'] == "PAUSE": + elif port_state == "PAUSE": self.state = self.STATE_PAUSE else: raise Exception("port {0}: bad state received from server '{1}'".format(self.port_id, sync_data['state'])) @@ -217,14 +215,14 @@ class Port: # return TRUE if write commands - def is_port_writeable (self): - # operations on port can be done on state idle or state sreams + def is_port_writable (self): + # operations on port can be done on state idle or state streams return ((self.state == self.STATE_IDLE) or (self.state == self.STATE_STREAMS)) # add stream to the port def add_stream (self, stream_id, stream_obj): - if not self.is_port_writeable(): + if not self.is_port_writable(): return self.err("Please stop port before attempting to add streams") @@ -332,6 +330,7 @@ class CTRexStatelessClient(object): self.user = username self.comm_link = CTRexStatelessClient.CCommLink(server, sync_port, virtual) self.verbose = False + self.ports = [] self._conn_handler = {} self._active_ports = set() self._system_info = None @@ -378,7 +377,7 @@ class CTRexStatelessClient(object): ############ boot up section ################ # connection sequence - def connect (self): + def connect(self): self.connected = False @@ -394,7 +393,7 @@ class CTRexStatelessClient(object): self.system_info = data - # cache supported cmds + # cache supported commands rc, data = self.transmit("get_supported_cmds") if not rc: return RC_ERR(data) @@ -402,8 +401,7 @@ class CTRexStatelessClient(object): self.supported_cmds = data # create ports - self.ports = [] - for port_id in xrange(0, self.get_port_count()): + for port_id in xrange(self.get_port_count()): self.ports.append(Port(port_id, self.user, self.transmit)) # acquire all ports @@ -485,7 +483,6 @@ class CTRexStatelessClient(object): return RC_ERR(data) for port_info in data: - rc = self.ports[port_info['port_id']].sync(port_info) if rc.bad(): return rc @@ -619,23 +616,23 @@ class CTRexStatelessClient(object): ######################### Console (high level) API ######################### - def cmd_ping (self): + def cmd_ping(self): rc = self.ping() rc.annotate("Pinging the server on '{0}' port '{1}': ".format(self.get_connection_ip(), self.get_connection_port())) return rc - def cmd_connect (self): + def cmd_connect(self): rc = self.connect() rc.annotate() return rc - def cmd_disconnect (self): + def cmd_disconnect(self): rc = self.disconnect() rc.annotate() return rc # reset - def cmd_reset (self): + def cmd_reset(self): # sync with the server diff --git a/scripts/automation/trex_control_plane/console/trex_console.py b/scripts/automation/trex_control_plane/console/trex_console.py index 8e8b25ec..ea2f5f12 100755 --- a/scripts/automation/trex_control_plane/console/trex_console.py +++ b/scripts/automation/trex_control_plane/console/trex_console.py @@ -23,6 +23,7 @@ import json import ast import argparse import random +import readline import string import os import sys @@ -36,12 +37,38 @@ import trex_status import parsing_opts -__version__ = "1.0" +__version__ = "1.1" class TRexGeneralCmd(cmd.Cmd): def __init__(self): cmd.Cmd.__init__(self) + # configure history behaviour + self._history_file_dir = "/tmp/trex/console/" + self._history_file = self.get_history_file_full_path() + readline.set_history_length(100) + # load history, if any + self.load_console_history() + + + def get_console_identifier(self): + return self.__class__.__name__ + + def get_history_file_full_path(self): + return "{dir}{filename}.hist".format(dir=self._history_file_dir, + filename=self.get_console_identifier()) + + def load_console_history(self): + if os.path.exists(self._history_file): + readline.read_history_file(self._history_file) + return + + def save_console_history(self): + if not os.path.exists(self._history_file_dir): + os.makedirs(self._history_file_dir) + # os.mknod(self._history_file) + readline.write_history_file(self._history_file) + return def emptyline(self): """Called when an empty line is entered in response to the prompt. @@ -57,15 +84,22 @@ class TRexGeneralCmd(cmd.Cmd): dotext = 'do_'+text return [a[3:]+' ' for a in self.get_names() if a.startswith(dotext)] + def precmd(self, line): + # before doing anything, save history snapshot of the console + # this is done before executing the command in case of ungraceful application exit + self.save_console_history() + return line + + # # main console object -class TRexConsole(cmd.Cmd): +class TRexConsole(TRexGeneralCmd): """Trex Console""" - def __init__(self, stateless_client, acquire_all_ports = True, verbose = False): - cmd.Cmd.__init__(self) - + def __init__(self, stateless_client, acquire_all_ports=True, verbose=False): self.stateless_client = stateless_client + TRexGeneralCmd.__init__(self) + self.verbose = verbose self.acquire_all_ports = acquire_all_ports @@ -78,11 +112,9 @@ class TRexConsole(cmd.Cmd): ################### internal section ######################## - # a cool hack - i stole this function and added space - def completenames(self, text, *ignored): - dotext = 'do_'+text - return [a[3:]+' ' for a in self.get_names() if a.startswith(dotext)] - + def get_console_identifier(self): + return "{context}_{server}".format(context=self.__class__.__name__, + server=self.stateless_client.get_system_info()['hostname']) def register_main_console_methods(self): main_names = set(self.trex_console.get_names()).difference(set(dir(self.__class__))) -- cgit 1.2.3-korg From 903b855393acd411e85b25e6b2df1158d9fe2856 Mon Sep 17 00:00:00 2001 From: imarom Date: Sun, 22 Nov 2015 11:07:40 +0200 Subject: TUI back online --- .../trex_control_plane/client/trex_stateless_client.py | 18 ++++++++++++++++++ .../trex_control_plane/console/trex_console.py | 5 ++--- .../trex_control_plane/console/trex_status.py | 16 ++++------------ 3 files changed, 24 insertions(+), 15 deletions(-) (limited to 'scripts/automation/trex_control_plane/console') 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 dd11fb67..164cdb90 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -277,6 +277,17 @@ class Port(object): return self.ok() + # get a specific stream + def get_stream (self, stream_id): + if stream_id in self.streams: + return self.streams[stream_id] + else: + return None + + def get_all_streams (self): + return self.streams + + # start traffic def start (self, mul, duration): if self.state == self.STATE_DOWN: @@ -393,6 +404,13 @@ class CTRexStatelessClient(object): if not rc: return RC_ERR(data) + # version + rc, data = self.transmit("get_version") + if not rc: + return RC_ERR(data) + + self.server_version = data + # cache system info rc, data = self.transmit("get_system_info") if not rc: diff --git a/scripts/automation/trex_control_plane/console/trex_console.py b/scripts/automation/trex_control_plane/console/trex_console.py index ea2f5f12..995965fd 100755 --- a/scripts/automation/trex_control_plane/console/trex_console.py +++ b/scripts/automation/trex_control_plane/console/trex_console.py @@ -197,8 +197,6 @@ class TRexConsole(TRexGeneralCmd): if rc.bad(): return - def do_test (self, line): - print self.stateless_client.get_acquired_ports() # set verbose on / off def do_verbose(self, line): @@ -252,7 +250,7 @@ class TRexConsole(TRexGeneralCmd): return TRexConsole.tree_autocomplete(s[l - 1]) def do_start(self, line): - '''Start selected traffic in specified ports on TRex\n''' + '''Start selected traffic in specified port(s) on TRex\n''' self.stateless_client.cmd_start_line(line) @@ -262,6 +260,7 @@ class TRexConsole(TRexGeneralCmd): ############# stop def do_stop(self, line): + '''stops port(s) transmitting traffic\n''' self.stateless_client.cmd_stop_line(line) diff --git a/scripts/automation/trex_control_plane/console/trex_status.py b/scripts/automation/trex_control_plane/console/trex_status.py index a54b718e..869812a1 100644 --- a/scripts/automation/trex_control_plane/console/trex_status.py +++ b/scripts/automation/trex_control_plane/console/trex_status.py @@ -398,13 +398,9 @@ class TrexStatus(): self.general_stats = stateless_client.get_stats_async().get_general_stats() # fetch server info - rc, self.server_sys_info = self.stateless_client.get_system_info() - if not rc: - return + self.server_sys_info = self.stateless_client.get_system_info() - rc, self.server_version = self.stateless_client.get_version() - if not rc: - return + self.server_version = self.stateless_client.get_version() # list of owned ports self.owned_ports_list = self.stateless_client.get_acquired_ports() @@ -416,9 +412,7 @@ class TrexStatus(): self.owned_ports[str(port_id)] = {} self.owned_ports[str(port_id)]['streams'] = {} - rc, stream_list = self.stateless_client.get_all_streams(port_id) - if not rc: - raise Exception("unable to get streams") + stream_list = self.stateless_client.get_all_streams(port_id) self.owned_ports[str(port_id)] = stream_list @@ -481,9 +475,7 @@ class TrexStatus(): self.owned_ports[str(port_id)] = {} self.owned_ports[str(port_id)]['streams'] = {} - rc, stream_list = self.stateless_client.get_all_streams(port_id) - if not rc: - raise Exception("unable to get streams") + stream_list = self.stateless_client.get_all_streams(port_id) self.owned_ports[str(port_id)] = stream_list -- cgit 1.2.3-korg From 54c1f0fc29b3d4580f7a13cffbe625fe88e37b16 Mon Sep 17 00:00:00 2001 From: Hanoh Haim Date: Mon, 23 Nov 2015 14:10:19 +0200 Subject: add pause/resume into the console --- .../client/trex_stateless_client.py | 118 +++++++++++++++++++++ .../trex_control_plane/console/old_console.py | 12 +++ .../trex_control_plane/console/trex_console.py | 10 ++ src/stateless/cp/trex_stateless_port.cpp | 25 +++-- src/stateless/cp/trex_stateless_port.h | 5 + src/stateless/cp/trex_streams_compiler.cpp | 7 +- src/stateless/cp/trex_streams_compiler.h | 5 + src/stateless/dp/trex_stateless_dp_core.cpp | 1 + 8 files changed, 172 insertions(+), 11 deletions(-) (limited to 'scripts/automation/trex_control_plane/console') 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 164cdb90..7bcbf2c7 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -331,6 +331,40 @@ class Port(object): return self.ok() + def pause (self): + + if (self.state != self.STATE_TX) : + return self.err("port is not transmitting") + + params = {"handler": self.handler, + "port_id": self.port_id} + + rc, data = self.transmit("pause_traffic", params) + if not rc: + return self.err(data) + + # only valid state after stop + self.state = self.STATE_PAUSE + + return self.ok() + + def resume (self): + + if (self.state != self.STATE_PAUSE) : + return self.err("port is not in pause mode") + + params = {"handler": self.handler, + "port_id": self.port_id} + + rc, data = self.transmit("resume_traffic", params) + if not rc: + return self.err(data) + + # only valid state after stop + self.state = self.STATE_TX + + return self.ok() + ################# events handler ###################### def async_event_port_stopped (self): self.state = self.STATE_STREAMS @@ -615,6 +649,25 @@ class CTRexStatelessClient(object): return rc + def resume_traffic (self, port_id_list = None, force = False): + + port_id_list = self.__ports(port_id_list) + rc = RC() + + for port_id in port_id_list: + rc.add(self.ports[port_id].resume()) + + return rc + + def pause_traffic (self, port_id_list = None, force = False): + + port_id_list = self.__ports(port_id_list) + rc = RC() + + for port_id in port_id_list: + rc.add(self.ports[port_id].pause()) + + return rc def stop_traffic (self, port_id_list = None, force = False): @@ -707,6 +760,71 @@ class CTRexStatelessClient(object): return RC_OK() + # pause cmd + def cmd_pause (self, port_id_list): + + # find the relveant ports + active_ports = list(set(self.get_active_ports()).intersection(port_id_list)) + + if not active_ports: + msg = "No active traffic on porvided ports" + print format_text(msg, 'bold') + return RC_ERR(msg) + + rc = self.pause_traffic(active_ports) + rc.annotate("Pausing traffic on port(s) {0}:".format(port_id_list)) + if rc.bad(): + return rc + + return RC_OK() + + def cmd_pause_line (self, line): + '''Pause active traffic in specified ports on TRex\n''' + parser = parsing_opts.gen_parser(self, + "pause", + self.cmd_stop_line.__doc__, + parsing_opts.PORT_LIST_WITH_ALL) + + opts = parser.parse_args(line.split()) + if opts is None: + return RC_ERR("bad command line paramters") + + return self.cmd_pause(opts.ports) + + + # resume cmd + def cmd_resume (self, port_id_list): + + # find the relveant ports + active_ports = list(set(self.get_active_ports()).intersection(port_id_list)) + + if not active_ports: + msg = "No active traffic on porvided ports" + print format_text(msg, 'bold') + return RC_ERR(msg) + + rc = self.resume_traffic(active_ports) + rc.annotate("Resume traffic on port(s) {0}:".format(port_id_list)) + if rc.bad(): + return rc + + return RC_OK() + + + def cmd_resume_line (self, line): + '''Resume active traffic in specified ports on TRex\n''' + parser = parsing_opts.gen_parser(self, + "resume", + self.cmd_stop_line.__doc__, + parsing_opts.PORT_LIST_WITH_ALL) + + opts = parser.parse_args(line.split()) + if opts is None: + return RC_ERR("bad command line paramters") + + return self.cmd_resume(opts.ports) + + # start cmd def cmd_start (self, port_id_list, stream_list, mult, force, duration): diff --git a/scripts/automation/trex_control_plane/console/old_console.py b/scripts/automation/trex_control_plane/console/old_console.py index 93c7e3f4..9d61a3a6 100644 --- a/scripts/automation/trex_control_plane/console/old_console.py +++ b/scripts/automation/trex_control_plane/console/old_console.py @@ -636,6 +636,18 @@ class TRexConsole1(cmd.Cmd): res_ok = self.stop_traffic(port_list) return + def do_pause(self, line): + '''Pause active traffic in specified ports on TRex\n''' + parser = parsing_opts.gen_parser("stop", self.do_stop.__doc__, + parsing_opts.PORT_LIST_WITH_ALL) + opts = parser.parse_args(line.split()) + if opts is None: + # avoid further processing in this command + return + port_list = self.extract_port_list(opts) + res_ok = self.stop_traffic(port_list) + return + def help_stop(self): self.do_stop("-h") diff --git a/scripts/automation/trex_control_plane/console/trex_console.py b/scripts/automation/trex_control_plane/console/trex_console.py index 995965fd..c03f2a82 100755 --- a/scripts/automation/trex_control_plane/console/trex_console.py +++ b/scripts/automation/trex_control_plane/console/trex_console.py @@ -263,6 +263,16 @@ class TRexConsole(TRexGeneralCmd): '''stops port(s) transmitting traffic\n''' self.stateless_client.cmd_stop_line(line) + ############# stop + def do_pause(self, line): + '''pause port(s) transmitting traffic\n''' + self.stateless_client.cmd_pause_line(line) + + ############# stop + def do_resume(self, line): + '''resume port(s) transmitting traffic\n''' + self.stateless_client.cmd_resume_line(line) + def help_stop(self): diff --git a/src/stateless/cp/trex_stateless_port.cpp b/src/stateless/cp/trex_stateless_port.cpp index fbc5f7c7..40392e68 100644 --- a/src/stateless/cp/trex_stateless_port.cpp +++ b/src/stateless/cp/trex_stateless_port.cpp @@ -127,6 +127,9 @@ TrexStatelessPort::start_traffic(double mul, double duration) { TrexStatelessCpToDpMsgBase *start_msg = new TrexStatelessDpStart(m_port_id, event_id, compiled_obj, duration); + m_last_all_streams_continues = compiled_obj->get_all_streams_continues(); + m_last_duration =duration; + change_state(PORT_STATE_TX); send_message_to_dp(start_msg); @@ -143,7 +146,8 @@ TrexStatelessPort::start_traffic(double mul, double duration) { void TrexStatelessPort::stop_traffic(void) { - if (m_port_state != PORT_STATE_TX) { + if (!( (m_port_state == PORT_STATE_TX) + || (m_port_state ==PORT_STATE_PAUSE) )) { return; } @@ -164,14 +168,18 @@ TrexStatelessPort::pause_traffic(void) { verify_state(PORT_STATE_TX); - #if 0 - /* generate a message to all the relevant DP cores to start transmitting */ - TrexStatelessCpToDpMsgBase *stop_msg = new TrexStatelessDpStop(m_port_id); + if (m_last_all_streams_continues == false) { + throw TrexRpcException(" pause is supported when all streams are in continues mode "); + } + + if ( m_last_duration>0.0 ) { + throw TrexRpcException(" pause is supported when duration is not enable is start command "); + } + + TrexStatelessCpToDpMsgBase *stop_msg = new TrexStatelessDpPause(m_port_id); send_message_to_dp(stop_msg); - m_port_state = PORT_STATE_UP_IDLE; - #endif change_state(PORT_STATE_PAUSE); } @@ -180,14 +188,11 @@ TrexStatelessPort::resume_traffic(void) { verify_state(PORT_STATE_PAUSE); - #if 0 /* generate a message to all the relevant DP cores to start transmitting */ - TrexStatelessCpToDpMsgBase *stop_msg = new TrexStatelessDpStop(m_port_id); + TrexStatelessCpToDpMsgBase *stop_msg = new TrexStatelessDpResume(m_port_id); send_message_to_dp(stop_msg); - m_port_state = PORT_STATE_UP_IDLE; - #endif change_state(PORT_STATE_TX); } diff --git a/src/stateless/cp/trex_stateless_port.h b/src/stateless/cp/trex_stateless_port.h index 73157c15..006ec97c 100644 --- a/src/stateless/cp/trex_stateless_port.h +++ b/src/stateless/cp/trex_stateless_port.h @@ -207,6 +207,8 @@ public: return m_dp_events; } + + private: @@ -260,6 +262,9 @@ private: /* holds the DP cores associated with this port */ std::vector m_cores_id_list; + bool m_last_all_streams_continues; + double m_last_duration; + TrexDpPortEvents m_dp_events; }; diff --git a/src/stateless/cp/trex_streams_compiler.cpp b/src/stateless/cp/trex_streams_compiler.cpp index bdfc3c01..302863ae 100644 --- a/src/stateless/cp/trex_streams_compiler.cpp +++ b/src/stateless/cp/trex_streams_compiler.cpp @@ -136,6 +136,7 @@ private: * stream compiled object *************************************/ TrexStreamsCompiledObj::TrexStreamsCompiledObj(uint8_t port_id, double mul) : m_port_id(port_id), m_mul(mul) { + m_all_continues=false; } TrexStreamsCompiledObj::~TrexStreamsCompiledObj() { @@ -401,6 +402,7 @@ TrexStreamsCompiler::compile(const std::vector &streams, } + bool all_continues=true; /* for now we do something trivial, */ for (auto stream : streams) { @@ -408,6 +410,9 @@ TrexStreamsCompiler::compile(const std::vector &streams, if (!stream->m_enabled) { continue; } + if (stream->get_type() != TrexStream::stCONTINUOUS ) { + all_continues=false; + } int new_id= nodes.get(stream->m_stream_id)->m_compressed_stream_id; assert(new_id>=0); @@ -423,7 +428,7 @@ TrexStreamsCompiler::compile(const std::vector &streams, my_next_stream_id ); } - + obj.m_all_continues =all_continues; return true; } diff --git a/src/stateless/cp/trex_streams_compiler.h b/src/stateless/cp/trex_streams_compiler.h index 200f7ce9..17ca3c74 100644 --- a/src/stateless/cp/trex_streams_compiler.h +++ b/src/stateless/cp/trex_streams_compiler.h @@ -64,6 +64,10 @@ public: return (m_mul); } + bool get_all_streams_continues(){ + return (m_all_continues); + } + void Dump(FILE *fd); private: @@ -73,6 +77,7 @@ private: std::vector m_objs; + bool m_all_continues; uint8_t m_port_id; double m_mul; }; diff --git a/src/stateless/dp/trex_stateless_dp_core.cpp b/src/stateless/dp/trex_stateless_dp_core.cpp index 03b13d6c..9b4a6ad9 100644 --- a/src/stateless/dp/trex_stateless_dp_core.cpp +++ b/src/stateless/dp/trex_stateless_dp_core.cpp @@ -160,6 +160,7 @@ bool TrexStatelessDpPerPort::stop_traffic(uint8_t port_id, bool stop_on_id, int event_id){ + if (m_state == TrexStatelessDpPerPort::ppSTATE_IDLE) { assert(m_active_streams==0); return false; -- cgit 1.2.3-korg