diff options
author | 2016-01-24 04:01:30 -0500 | |
---|---|---|
committer | 2016-01-24 06:02:46 -0500 | |
commit | 6f4a51c126b7a78ee8e37d396ed2b61b05fa506c (patch) | |
tree | e8b1c20e0d02cb40fa1539f2896f07345625b6f3 | |
parent | db5b9d6085b3e5cf71e1abf42c7a745cb723e00e (diff) |
added example
-rw-r--r-- | api/stl/examples/stl_simple_burst.py | 53 | ||||
-rw-r--r-- | api/stl/examples/udp_64B.pcap | bin | 0 -> 104 bytes | |||
-rw-r--r-- | api/stl/profiles/burst.yaml | 39 | ||||
-rw-r--r-- | api/stl/trex_stl_api.py | 17 | ||||
-rwxr-xr-x | scripts/automation/trex_control_plane/client/trex_hltapi.py | 4 | ||||
-rwxr-xr-x | scripts/automation/trex_control_plane/client/trex_stateless_client.py | 11 | ||||
-rwxr-xr-x | scripts/automation/trex_control_plane/common/trex_stats.py | 5 | ||||
-rwxr-xr-x | scripts/automation/trex_control_plane/common/trex_streams.py | 1 | ||||
-rwxr-xr-x | scripts/automation/trex_control_plane/console/trex_console.py | 25 | ||||
-rw-r--r-- | scripts/automation/trex_control_plane/examples/interactive_stateless.py | 2 | ||||
-rw-r--r-- | scripts/stl_test_api/__init__.py | 108 | ||||
-rw-r--r-- | scripts/stl_test_api/trex_stl_api.py | 93 |
12 files changed, 134 insertions, 224 deletions
diff --git a/api/stl/examples/stl_simple_burst.py b/api/stl/examples/stl_simple_burst.py new file mode 100644 index 00000000..7efb574a --- /dev/null +++ b/api/stl/examples/stl_simple_burst.py @@ -0,0 +1,53 @@ +import sys +sys.path.insert(0, "../") + +import trex_stl_api + +from trex_stl_api import STLClient, STLError + +import time + +# define a simple burst test +def simple_burst (): + + passed = True + + try: + with STLClient() as c: + + # activate this for some logging information + #c.logger.set_verbose(c.logger.VERBOSE_REGULAR) + + # repeat for 5 times + for i in xrange(1, 6): + + # read the stats before + before_ipackets = c.get_stats()['total']['ipackets'] + + # inject burst profile on two ports and block until done + c.start(profiles = '../profiles/burst.yaml', ports = [0, 1], mult = "1gbps") + c.wait_on_traffic(ports = [0, 1]) + + after_ipackets = c.get_stats()['total']['ipackets'] + + print "Test iteration {0} - Packets Received: {1} ".format(i, (after_ipackets - before_ipackets)) + + # we have 600 packets in the burst and two ports + if (after_ipackets - before_ipackets) != (600 * 2): + passed = False + + # error handling + except STLError as e: + passed = False + print e + + + + if passed: + print "\nTest has passed :-)\n" + else: + print "\nTest has failed :-(\n" + + +simple_burst() + diff --git a/api/stl/examples/udp_64B.pcap b/api/stl/examples/udp_64B.pcap Binary files differnew file mode 100644 index 00000000..699b9c80 --- /dev/null +++ b/api/stl/examples/udp_64B.pcap diff --git a/api/stl/profiles/burst.yaml b/api/stl/profiles/burst.yaml new file mode 100644 index 00000000..dbd348c7 --- /dev/null +++ b/api/stl/profiles/burst.yaml @@ -0,0 +1,39 @@ +### Single stream UDP packet, 64B ### +##################################### +- name: stream0 + stream: + self_start: True + next_stream_id: stream1 + packet: + binary: udp_64B.pcap + mode: + type: single_burst + pps: 100 + total_pkts : 100 + rx_stats: [] + vm: [] + +- name: stream1 + stream: + self_start: False + next_stream_id: stream2 + packet: + binary: udp_64B.pcap + mode: + type: single_burst + pps: 100 + total_pkts : 200 + rx_stats: [] + vm: [] + +- name: stream2 + stream: + self_start: False + packet: + binary: udp_64B.pcap + mode: + type: single_burst + pps: 100 + total_pkts : 300 + rx_stats: [] + vm: [] diff --git a/api/stl/trex_stl_api.py b/api/stl/trex_stl_api.py new file mode 100644 index 00000000..aad39916 --- /dev/null +++ b/api/stl/trex_stl_api.py @@ -0,0 +1,17 @@ +import os +import sys +import time + + +# update the import path to include the stateless client +root_path = os.path.dirname(os.path.abspath(__file__)) + +sys.path.insert(0, os.path.join(root_path, '../../scripts/automation/trex_control_plane/client/')) +sys.path.insert(0, os.path.join(root_path, '../../scripts/automation/trex_control_plane/client_utils/')) +sys.path.insert(0, os.path.join(root_path, '../../scripts/automation/trex_control_plane/client_utils/')) + +# aliasing +import trex_stateless_client +STLClient = trex_stateless_client.STLClient +STLError = trex_stateless_client.STLError + diff --git a/scripts/automation/trex_control_plane/client/trex_hltapi.py b/scripts/automation/trex_control_plane/client/trex_hltapi.py index 848d5a9e..c25c73cb 100755 --- a/scripts/automation/trex_control_plane/client/trex_hltapi.py +++ b/scripts/automation/trex_control_plane/client/trex_hltapi.py @@ -2,7 +2,7 @@ import trex_root_path from client_utils.packet_builder import CTRexPktBuilder -from trex_stateless_client import CTRexStatelessClient +from trex_stateless_client import STLClient from common.trex_streams import * from client_utils.general_utils import id_count_gen import dpkt @@ -20,7 +20,7 @@ class CTRexHltApi(object): # sync = RPC, async = ZMQ def connect(self, device, port_list, username, sync_port = 4501, async_port = 4500, reset=False, break_locks=False): ret_dict = {"status": 0} - self.trex_client = CTRexStatelessClient(username, device, sync_port, async_port) + self.trex_client = STLClient(username, device, sync_port, async_port) rc = self.trex_client.connect() if rc.bad(): 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 c5d7e053..c1a4d1d1 100755 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -419,8 +419,8 @@ class CCommLink(object): ############################ ############################# ############################ ############################# -class CTRexStatelessClient(object): - """docstring for CTRexStatelessClient""" +class STLClient(object): + """docstring for STLClient""" def __init__(self, username = general_utils.get_current_user(), @@ -968,6 +968,7 @@ class CTRexStatelessClient(object): stats_obj = {} for stats_type in stats_opts: stats_obj.update(self.stats_generator.generate_single_statistic(port_id_list, stats_type)) + return stats_obj def _get_streams(self, port_id_list, streams_mask=set()): @@ -1244,9 +1245,11 @@ class CTRexStatelessClient(object): # pings the server on the RPC channel @__api_check(True) def ping(self): - rc = self.__ping() self.logger.pre_cmd( "Pinging the server on '{0}' port '{1}': ".format(self.connection_info['server'], - self.connection_info['sync_port'])) + self.connection_info['sync_port'])) + rc = self.__ping() + + self.logger.post_cmd(rc) if not rc: raise STLError(rc) diff --git a/scripts/automation/trex_control_plane/common/trex_stats.py b/scripts/automation/trex_control_plane/common/trex_stats.py index 9c2cd7f1..3f64310f 100755 --- a/scripts/automation/trex_control_plane/common/trex_stats.py +++ b/scripts/automation/trex_control_plane/common/trex_stats.py @@ -59,7 +59,7 @@ def calculate_diff_raw (samples): class CTRexInfoGenerator(object): """ This object is responsible of generating stats and information from objects maintained at - CTRexStatelessClient and the ports. + STLClient and the ports. """ def __init__(self, global_stats_ref, ports_dict_ref): @@ -477,6 +477,9 @@ class CPortStats(CTRexStats): raise TypeError("cannot add non stats object to stats") # main stats + if not self.latest_stats: + self.latest_stats = {} + self.__merge_dicts(self.latest_stats, x.latest_stats) # reference stats diff --git a/scripts/automation/trex_control_plane/common/trex_streams.py b/scripts/automation/trex_control_plane/common/trex_streams.py index 800b6d49..ea3d71d1 100755 --- a/scripts/automation/trex_control_plane/common/trex_streams.py +++ b/scripts/automation/trex_control_plane/common/trex_streams.py @@ -210,7 +210,6 @@ class CStream(object): setattr(self, k, kwargs[k]) # TODO: load to _pkt_bld_obj also when passed as byte array! elif isinstance(binary, str) and binary.endswith(".pcap"): - # self.load_packet_from_pcap(binary, kwargs[k]["meta"]) self._pkt_bld_obj.load_packet_from_pcap(binary) self._pkt_bld_obj.metadata = kwargs[k]["meta"] self.packet = self._pkt_bld_obj.dump_pkt() diff --git a/scripts/automation/trex_control_plane/console/trex_console.py b/scripts/automation/trex_control_plane/console/trex_console.py index 34494561..88ff45dc 100755 --- a/scripts/automation/trex_control_plane/console/trex_console.py +++ b/scripts/automation/trex_control_plane/console/trex_console.py @@ -29,7 +29,7 @@ import sys import tty, termios import trex_root_path from common.trex_streams import * -from client.trex_stateless_client import CTRexStatelessClient, LoggerApi, STLError +from client.trex_stateless_client import STLClient, LoggerApi, STLError from common.text_opts import * from client_utils.general_utils import user_input, get_current_user from client_utils import parsing_opts @@ -175,6 +175,7 @@ class TRexConsole(TRexGeneralCmd): ################### internal section ######################## def prompt_redraw (self): + self.postcmd(False, "") sys.stdout.write("\n" + self.prompt + readline.get_line_buffer()) sys.stdout.flush() @@ -293,9 +294,7 @@ class TRexConsole(TRexGeneralCmd): @verify_connected def do_ping (self, line): '''Ping the server\n''' - rc = self.stateless_client.ping() - if rc.bad(): - return + self.stateless_client.ping() # set verbose on / off @@ -632,9 +631,7 @@ def run_script_file (self, filename, stateless_client): stateless_client.logger.log(format_text("unknown command '{0}'\n".format(cmd), 'bold')) return False - rc = cmd_table[cmd](args) - if rc.bad(): - return False + cmd_table[cmd](args) stateless_client.logger.log(format_text("\n[Done]", 'bold')) @@ -670,7 +667,7 @@ def setParserOptions(): default = get_current_user(), type = str) - parser.add_argument("--verbose", dest="verbose", + parser.add_argument("-v", "--verbose", dest="verbose", action="store_true", help="Switch ON verbose option. Default is: OFF.", default = False) @@ -711,12 +708,12 @@ def main(): # Stateless client connection logger = ConsoleLogger() - stateless_client = CTRexStatelessClient(username = options.user, - server = options.server, - sync_port = options.port, - async_port = options.pub, - verbose_level = verbose_level, - logger = logger) + stateless_client = STLClient(username = options.user, + server = options.server, + sync_port = options.port, + async_port = options.pub, + verbose_level = verbose_level, + logger = logger) # TUI or no acquire will give us READ ONLY mode try: diff --git a/scripts/automation/trex_control_plane/examples/interactive_stateless.py b/scripts/automation/trex_control_plane/examples/interactive_stateless.py index e64b4755..f6ada17d 100644 --- a/scripts/automation/trex_control_plane/examples/interactive_stateless.py +++ b/scripts/automation/trex_control_plane/examples/interactive_stateless.py @@ -25,7 +25,7 @@ class InteractiveStatelessTRex(cmd.Cmd): self.verbose = verbose self.virtual = virtual - self.trex = CTRexStatelessClient(trex_host, trex_port, self.virtual) + self.trex = STLClient(trex_host, trex_port, self.virtual) self.DEFAULT_RUN_PARAMS = dict(m=1.5, nc=True, p=True, diff --git a/scripts/stl_test_api/__init__.py b/scripts/stl_test_api/__init__.py deleted file mode 100644 index 7381d88e..00000000 --- a/scripts/stl_test_api/__init__.py +++ /dev/null @@ -1,108 +0,0 @@ -# -# TRex stateless API -# provides a layer for communicating with TRex -# using Python API -# - -import sys -import os -import time -import cStringIO - -api_path = os.path.dirname(os.path.abspath(__file__)) -sys.path.insert(0, os.path.join(api_path, '../automation/trex_control_plane/client/')) - -from trex_stateless_client import CTRexStatelessClient, LoggerApi -from common import trex_stats - -# a basic test object -class BasicTestAPI(object): - - # exception class - class Failure(Exception): - def __init__ (self, rc): - self.msg = str(rc) - - def __str__ (self): - s = "\n\n******\n" - s += "Test has failed.\n\n" - s += "Error reported:\n\n {0}\n".format(self.msg) - return s - - - # logger for test - class Logger(LoggerApi): - def __init__ (self): - LoggerApi.__init__(self) - self.buffer = cStringIO.StringIO() - - def write (self, msg, newline = True): - line = str(msg) + ("\n" if newline else "") - self.buffer.write(line) - - #print line - - def flush (self): - pass - - - # main object - def __init__ (self, server = "localhost", sync_port = 4501, async_port = 4500): - self.logger = BasicTestAPI.Logger() - self.client = CTRexStatelessClient(logger = self.logger, - sync_port = sync_port, - async_port = async_port, - verbose_level = LoggerApi.VERBOSE_REGULAR) - - - self.__invalid_stats = True - - # connect to the stateless client - def connect (self): - rc = self.client.connect(mode = "RWF") - self.__verify_rc(rc) - - - # disconnect from the stateless client - def disconnect (self): - self.client.disconnect() - - - def inject_profile (self, filename, rate = "1", duration = None): - self.__invalid_stats = True - - cmd = "--total -f {0} -m {1}".format(filename, rate) - if duration: - cmd += " -d {0}".format(duration) - - rc = self.client.cmd_start_line(cmd) - self.__verify_rc(rc) - - - def wait_while_traffic_on (self, timeout = None): - while self.client.get_active_ports(): - time.sleep(0.1) - - - def get_stats (self): - if self.__invalid_stats: - # send a barrier - rc = self.client.block_on_stats() - self.__verify_rc(rc) - self.__invalid_stats = False - - total_stats = trex_stats.CPortStats(None) - - for port in self.client.ports.values(): - total_stats += port.port_stats - - return total_stats - - - # some internal methods - - # verify RC return value - def __verify_rc (self, rc): - if not rc: - raise self.Failure(rc) - diff --git a/scripts/stl_test_api/trex_stl_api.py b/scripts/stl_test_api/trex_stl_api.py deleted file mode 100644 index 6d85ee9f..00000000 --- a/scripts/stl_test_api/trex_stl_api.py +++ /dev/null @@ -1,93 +0,0 @@ -import sys -import os -import time -import cStringIO - -api_path = os.path.dirname(os.path.abspath(__file__)) -sys.path.insert(0, os.path.join(api_path, '../automation/trex_control_plane/client/')) - -from trex_stateless_client import CTRexStatelessClient, LoggerApi - -class BasicTest(object): - - # exception class - class Failure(Exception): - def __init__ (self, rc): - self.rc = rc - - def __str__ (self): - s = "\n******\n" - s += "Test has failed.\n\n" - s += "Error reported:\n\n {0}\n".format(str(self.rc.err())) - return s - - - # logger - class Logger(LoggerApi): - def __init__ (self): - LoggerApi.__init__(self) - self.buffer = cStringIO.StringIO() - - def write (self, msg, newline = True): - self.buffer.write(str(msg)) - - if newline: - self.buffer.write("\n") - - def flush (self): - pass - - - def __init__ (self): - self.logger = BasicTest.Logger() - self.client = CTRexStatelessClient(logger = self.logger) - - def connect (self): - rc = self.client.connect(mode = "RWF") - __verify_rc(rc) - - - def disconnect (self): - self.client.disconnect() - - - def __verify_rc (self, rc): - if rc.bad(): - raise self.Failure(rc) - - def inject_profile (self, filename, rate = "1", duration = None): - cmd = "-f {0} -m {1}".format(filename, rate) - if duration: - cmd += " -d {0}".format(duration) - - print cmd - rc = self.client.cmd_start_line(cmd) - self.__verify_rc(rc) - - - def wait_while_traffic_on (self, timeout = None): - while self.client.get_active_ports(): - time.sleep(0.1) - - -def start (): - test = BasicTest() - - try: - test.connect() - test.inject_profile("stl/imix_1pkt.yaml", rate = "5gbps", duration = 10) - test.wait_while_traffic_on() - finally: - test.disconnect() - - -def main (): - try: - start() - print "\nTest has passed :)\n" - except BasicTest.Failure as e: - print e - - - -#main() |